fix(translator): simplify tool response handling and adjust JSON schema updates in Gemini modules

This commit is contained in:
Luis Pater
2025-11-05 22:48:50 +08:00
parent a318dff8b0
commit 2a73d8c4a3
6 changed files with 10 additions and 22 deletions

View File

@@ -14,7 +14,7 @@
本项目由 Z智谱 提供赞助, 他们通过 GLM CODING PLAN 对本项目提供技术支持。 本项目由 Z智谱 提供赞助, 他们通过 GLM CODING PLAN 对本项目提供技术支持。
GLM CODING PLAN 是专为AI编码打造的订阅套餐每月最低仅需20元即可在十余款主流AI编码工具如Claude Code、中畅享智谱旗舰模型GLM-4.6,为开发者提供顶尖的编码体验。 GLM CODING PLAN 是专为AI编码打造的订阅套餐每月最低仅需20元即可在十余款主流AI编码工具如 Claude Code、Cline、Roo Code 中畅享智谱旗舰模型GLM-4.6,为开发者提供顶尖的编码体验。
智谱AI为本软件提供了特别优惠使用以下链接购买可以享受九折优惠https://www.bigmodel.cn/claude-code?ic=RRVJPB5SII 智谱AI为本软件提供了特别优惠使用以下链接购买可以享受九折优惠https://www.bigmodel.cn/claude-code?ic=RRVJPB5SII

View File

@@ -99,7 +99,7 @@ func ConvertClaudeRequestToCLI(modelName string, inputRawJSON []byte, _ bool) []
if len(toolCallIDs) > 1 { if len(toolCallIDs) > 1 {
funcName = strings.Join(toolCallIDs[0:len(toolCallIDs)-1], "-") funcName = strings.Join(toolCallIDs[0:len(toolCallIDs)-1], "-")
} }
responseData := contentResult.Get("content").String() responseData := contentResult.Get("content").Raw
functionResponse := client.FunctionResponse{Name: funcName, Response: map[string]interface{}{"result": responseData}} functionResponse := client.FunctionResponse{Name: funcName, Response: map[string]interface{}{"result": responseData}}
clientContent.Parts = append(clientContent.Parts, client.Part{FunctionResponse: &functionResponse}) clientContent.Parts = append(clientContent.Parts, client.Part{FunctionResponse: &functionResponse})
} }
@@ -127,6 +127,7 @@ func ConvertClaudeRequestToCLI(modelName string, inputRawJSON []byte, _ bool) []
inputSchema := inputSchemaResult.Raw inputSchema := inputSchemaResult.Raw
tool, _ := sjson.Delete(toolResult.Raw, "input_schema") tool, _ := sjson.Delete(toolResult.Raw, "input_schema")
tool, _ = sjson.SetRaw(tool, "parametersJsonSchema", inputSchema) tool, _ = sjson.SetRaw(tool, "parametersJsonSchema", inputSchema)
tool, _ = sjson.Delete(tool, "strict")
var toolDeclaration any var toolDeclaration any
if err := json.Unmarshal([]byte(tool), &toolDeclaration); err == nil { if err := json.Unmarshal([]byte(tool), &toolDeclaration); err == nil {
tools[0].FunctionDeclarations = append(tools[0].FunctionDeclarations, toolDeclaration) tools[0].FunctionDeclarations = append(tools[0].FunctionDeclarations, toolDeclaration)

View File

@@ -149,11 +149,7 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
toolCallID := m.Get("tool_call_id").String() toolCallID := m.Get("tool_call_id").String()
if toolCallID != "" { if toolCallID != "" {
c := m.Get("content") c := m.Get("content")
if c.Type == gjson.String { toolResponses[toolCallID] = c.Raw
toolResponses[toolCallID] = c.String()
} else if c.IsObject() && c.Get("type").String() == "text" {
toolResponses[toolCallID] = c.Get("text").String()
}
} }
} }
} }
@@ -254,7 +250,7 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
if resp == "" { if resp == "" {
resp = "{}" resp = "{}"
} }
toolNode, _ = sjson.SetRawBytes(toolNode, "parts."+itoa(pp)+".functionResponse.response", []byte(`{"result":`+quoteIfNeeded(resp)+`}`)) toolNode, _ = sjson.SetBytes(toolNode, "parts."+itoa(pp)+".functionResponse.response.result", []byte(resp))
pp++ pp++
} }
} }

View File

@@ -92,7 +92,7 @@ func ConvertClaudeRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
if len(toolCallIDs) > 1 { if len(toolCallIDs) > 1 {
funcName = strings.Join(toolCallIDs[0:len(toolCallIDs)-1], "-") funcName = strings.Join(toolCallIDs[0:len(toolCallIDs)-1], "-")
} }
responseData := contentResult.Get("content").String() responseData := contentResult.Get("content").Raw
functionResponse := client.FunctionResponse{Name: funcName, Response: map[string]interface{}{"result": responseData}} functionResponse := client.FunctionResponse{Name: funcName, Response: map[string]interface{}{"result": responseData}}
clientContent.Parts = append(clientContent.Parts, client.Part{FunctionResponse: &functionResponse}) clientContent.Parts = append(clientContent.Parts, client.Part{FunctionResponse: &functionResponse})
} }

View File

@@ -150,14 +150,11 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
toolCallID := m.Get("tool_call_id").String() toolCallID := m.Get("tool_call_id").String()
if toolCallID != "" { if toolCallID != "" {
c := m.Get("content") c := m.Get("content")
if c.Type == gjson.String { toolResponses[toolCallID] = c.Raw
toolResponses[toolCallID] = c.String()
} else if c.IsObject() && c.Get("type").String() == "text" {
toolResponses[toolCallID] = c.Get("text").String()
}
} }
} }
} }
fmt.Printf("11111")
for i := 0; i < len(arr); i++ { for i := 0; i < len(arr); i++ {
m := arr[i] m := arr[i]
@@ -280,7 +277,7 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
if resp == "" { if resp == "" {
resp = "{}" resp = "{}"
} }
toolNode, _ = sjson.SetRawBytes(toolNode, "parts."+itoa(pp)+".functionResponse.response", []byte(`{"result":`+quoteIfNeeded(resp)+`}`)) toolNode, _ = sjson.SetBytes(toolNode, "parts."+itoa(pp)+".functionResponse.response.result", []byte(resp))
pp++ pp++
} }
} }

View File

@@ -143,17 +143,11 @@ func ConvertOpenAIResponsesRequestToGemini(modelName string, inputRawJSON []byte
} }
functionResponse, _ = sjson.Set(functionResponse, "functionResponse.name", functionName) functionResponse, _ = sjson.Set(functionResponse, "functionResponse.name", functionName)
// Also set response.name to align with docs/convert-2.md
functionResponse, _ = sjson.Set(functionResponse, "functionResponse.response.name", functionName)
// Parse output JSON string and set as response content // Parse output JSON string and set as response content
if output != "" { if output != "" {
outputResult := gjson.Parse(output) outputResult := gjson.Parse(output)
if outputResult.IsObject() { functionResponse, _ = sjson.Set(functionResponse, "functionResponse.response.result", outputResult.Raw)
functionResponse, _ = sjson.SetRaw(functionResponse, "functionResponse.response.content", outputResult.String())
} else {
functionResponse, _ = sjson.Set(functionResponse, "functionResponse.response.content", output)
}
} }
functionContent, _ = sjson.SetRaw(functionContent, "parts.-1", functionResponse) functionContent, _ = sjson.SetRaw(functionContent, "parts.-1", functionResponse)