@@ -13,6 +13,7 @@ import (
1313 "github.com/google/uuid"
1414 "github.com/wavetermdev/waveterm/pkg/aiusechat/aiutil"
1515 "github.com/wavetermdev/waveterm/pkg/aiusechat/uctypes"
16+ "github.com/wavetermdev/waveterm/pkg/util/utilfn"
1617)
1718
1819// cleanSchemaForGemini removes fields from JSON Schema that Gemini doesn't accept
@@ -232,9 +233,36 @@ func ConvertToolResultsToGeminiChatMessage(toolResults []uctypes.AIToolResult) (
232233 }
233234
234235 response := make (map [string ]any )
236+ var nestedParts []GeminiMessagePart
237+
235238 if result .ErrorText != "" {
236239 response ["ok" ] = false
237240 response ["error" ] = result .ErrorText
241+ } else if strings .HasPrefix (result .Text , "data:" ) {
242+ mimeType , base64Data , err := utilfn .DecodeDataURL (result .Text )
243+ if err != nil {
244+ log .Printf ("gemini: failed to decode data URL in tool result: %v\n " , err )
245+ response ["ok" ] = false
246+ response ["error" ] = fmt .Sprintf ("failed to decode data URL: %v" , err )
247+ } else if strings .HasPrefix (mimeType , "image/" ) {
248+ // For image data URLs, use multimodal function response (Gemini 3 Pro+)
249+ displayName := fmt .Sprintf ("result_%s.%s" , result .ToolUseID [:8 ], strings .TrimPrefix (mimeType , "image/" ))
250+ response ["ok" ] = true
251+ response ["image" ] = map [string ]string {"$ref" : displayName }
252+
253+ // Add the image data as a nested part
254+ nestedParts = append (nestedParts , GeminiMessagePart {
255+ InlineData : & GeminiInlineData {
256+ MimeType : mimeType ,
257+ Data : base64 .StdEncoding .EncodeToString (base64Data ),
258+ DisplayName : displayName ,
259+ },
260+ })
261+ } else {
262+ log .Printf ("gemini: unsupported data URL mimetype in tool result: %s\n " , mimeType )
263+ response ["ok" ] = false
264+ response ["error" ] = fmt .Sprintf ("unsupported data URL mimetype: %s" , mimeType )
265+ }
238266 } else {
239267 response ["ok" ] = true
240268 response ["result" ] = result .Text
@@ -244,6 +272,7 @@ func ConvertToolResultsToGeminiChatMessage(toolResults []uctypes.AIToolResult) (
244272 FunctionResponse : & GeminiFunctionResponse {
245273 Name : result .ToolName ,
246274 Response : response ,
275+ Parts : nestedParts ,
247276 },
248277 })
249278 }
0 commit comments