-
Notifications
You must be signed in to change notification settings - Fork 275
feat: support opaque_body in prompt messages #585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @BeautyyuYanli, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a flexible mechanism to embed provider-specific metadata directly within prompt messages. By adding an Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds an optional opaque_body field to PromptMessage and PromptMessageContent to allow for passthrough of provider-specific metadata. The changes include updating the struct definitions, modifying the custom UnmarshalJSON for PromptMessage to handle the new field, and extending tests to cover the new functionality. The implementation is correct, but I've identified a significant opportunity to improve the maintainability of the custom UnmarshalJSON function in llm.go. My feedback includes a suggestion to refactor this function to make it more robust and easier to manage in the future.
| if opaqueBody, ok := raw["opaque_body"]; ok { | ||
| p.OpaqueBody = opaqueBody | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this correctly handles the opaque_body field, the overall approach in UnmarshalJSON of manually unmarshalling each field from a map is brittle and hard to maintain. As new fields are added to PromptMessage, this function will need to be updated each time, increasing the risk of errors.
A more robust and idiomatic Go approach is to use a struct alias. This lets you leverage the default JSON unmarshaling for all standard fields and only write custom logic for the fields that need special handling (in this case, Content). This would automatically handle opaque_body and any future fields added to the struct.
I recommend refactoring the entire UnmarshalJSON function as follows:
func (p *PromptMessage) UnmarshalJSON(data []byte) error {
// Use an alias to avoid recursion with UnmarshalJSON.
type Alias PromptMessage
aux := &struct {
// Capture Content as raw JSON to handle its dual type.
Content json.RawMessage `json:"content"`
*Alias
}{
Alias: (*Alias)(p),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
// The original implementation required 'role' and 'content'.
// The validator will catch this later, but checking here provides an earlier, clearer error.
if p.Role == "" {
return errors.New("role field is required")
}
if len(aux.Content) == 0 || string(aux.Content) == "null" {
return errors.New("content field is required")
}
// Try to unmarshal content as a string.
var s string
if err := json.Unmarshal(aux.Content, &s); err == nil {
p.Content = s
return nil
}
// If not a string, try as a slice of PromptMessageContent.
var slice []PromptMessageContent
if err := json.Unmarshal(aux.Content, &slice); err != nil {
return err // Invalid content format
}
p.Content = slice
return nil
}This change would make the code more maintainable and less error-prone for future modifications.
This is part of langgenius/dify-official-plugins#2492
Summary
Adds optional
opaque_bodypassthrough for prompt messages so provider-specific metadata can be carried through without schema changes.Changes
opaque_bodytoPromptMessageandPromptMessageContent(omitempty).opaque_bodyinPromptMessageJSON unmarshal.opaque_body.Testing