Browse Source

Merge pull request #1180 from RedwindA/fix/gemini-tool

🐛 fix(Gemini): improve JSON parsing for tool content handling
Calcium-Ion 9 months ago
parent
commit
bed19d5ca4
1 changed files with 16 additions and 1 deletions
  1. 16 1
      relay/channel/gemini/relay-gemini.go

+ 16 - 1
relay/channel/gemini/relay-gemini.go

@@ -205,7 +205,22 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest, info *relaycommon
 			} else if val, exists := tool_call_ids[message.ToolCallId]; exists {
 			} else if val, exists := tool_call_ids[message.ToolCallId]; exists {
 				name = val
 				name = val
 			}
 			}
-			contentMap := common.StrToMap(message.StringContent())
+			var contentMap map[string]interface{}
+			contentStr := message.StringContent()
+
+			// 1. 尝试解析为 JSON 对象
+			if err := json.Unmarshal([]byte(contentStr), &contentMap); err != nil {
+				// 2. 如果失败,尝试解析为 JSON 数组
+				var contentSlice []interface{}
+				if err := json.Unmarshal([]byte(contentStr), &contentSlice); err == nil {
+					// 如果是数组,包装成对象
+					contentMap = map[string]interface{}{"result": contentSlice}
+				} else {
+					// 3. 如果再次失败,作为纯文本处理
+					contentMap = map[string]interface{}{"content": contentStr}
+				}
+			}
+
 			functionResp := &FunctionResponse{
 			functionResp := &FunctionResponse{
 				Name:     name,
 				Name:     name,
 				Response: contentMap,
 				Response: contentMap,