-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix(#5626 #5832): handle multiple content chunks & images better #5839
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -63,25 +63,22 @@ pub fn format_messages(messages: &[Message], image_format: &ImageFormat) -> Vec< | |||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let mut output = Vec::new(); | ||||||||||||||||||||||||
| let mut content_array = Vec::new(); | ||||||||||||||||||||||||
| let mut text_array = Vec::new(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| for content in &message.content { | ||||||||||||||||||||||||
| match content { | ||||||||||||||||||||||||
| MessageContent::Text(text) => { | ||||||||||||||||||||||||
| if !text.text.is_empty() { | ||||||||||||||||||||||||
| // Check for image paths in the text | ||||||||||||||||||||||||
| if let Some(image_path) = detect_image_path(&text.text) { | ||||||||||||||||||||||||
| // Try to load and convert the image | ||||||||||||||||||||||||
| if let Ok(image) = load_image_file(image_path) { | ||||||||||||||||||||||||
| converted["content"] = json!([ | ||||||||||||||||||||||||
| {"type": "text", "text": text.text}, | ||||||||||||||||||||||||
| convert_image(&image, image_format) | ||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||
| content_array.push(json!({"type": "text", "text": text.text})); | ||||||||||||||||||||||||
| content_array.push(convert_image(&image, image_format)); | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| // If image loading fails, just use the text | ||||||||||||||||||||||||
| converted["content"] = json!(text.text); | ||||||||||||||||||||||||
| text_array.push(text.text.clone()); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| converted["content"] = json!(text.text); | ||||||||||||||||||||||||
| text_array.push(text.text.clone()); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
@@ -205,8 +202,7 @@ pub fn format_messages(messages: &[Message], image_format: &ImageFormat) -> Vec< | |||||||||||||||||||||||
| // Skip tool confirmation requests | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| MessageContent::Image(image) => { | ||||||||||||||||||||||||
| // Handle direct image content | ||||||||||||||||||||||||
| converted["content"] = json!([convert_image(image, image_format)]); | ||||||||||||||||||||||||
| content_array.push(convert_image(image, image_format)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| MessageContent::FrontendToolRequest(request) => match &request.tool_call { | ||||||||||||||||||||||||
| Ok(tool_call) => { | ||||||||||||||||||||||||
|
|
@@ -244,9 +240,16 @@ pub fn format_messages(messages: &[Message], image_format: &ImageFormat) -> Vec< | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if !content_array.is_empty() { | ||||||||||||||||||||||||
| converted["content"] = json!(content_array); | ||||||||||||||||||||||||
| } else if !text_array.is_empty() { | ||||||||||||||||||||||||
| converted["content"] = json!(text_array.join("\n")); | ||||||||||||||||||||||||
|
Comment on lines
+243
to
+246
|
||||||||||||||||||||||||
| if !content_array.is_empty() { | |
| converted["content"] = json!(content_array); | |
| } else if !text_array.is_empty() { | |
| converted["content"] = json!(text_array.join("\n")); | |
| // Merge text_array into content_array if text exists | |
| if !text_array.is_empty() { | |
| content_array.push(json!({"type": "text", "text": text_array.join("\n")})); | |
| } | |
| if !content_array.is_empty() { | |
| converted["content"] = json!(content_array); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
When an image path is detected and loaded successfully, the text is added to
content_arraybut subsequent non-image text goes totext_array. This will causetext_arraycontent to be lost since line 243-246 only usescontent_arraywhen it's non-empty. All text should go tocontent_arraywhen any images are present.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.
is this legit feedback @alexhancock ? it sounds convincing
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.
I think it may be slop as further down is consolidated.
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.
yeah below I do
which seems right