fix openai compat tool argument parsing

This commit is contained in:
lpf
2026-05-11 19:28:15 +08:00
parent 45c3234316
commit a55fb6aa66
3 changed files with 23 additions and 2 deletions

View File

@@ -15,7 +15,7 @@ import (
"github.com/YspCoder/clawgo/pkg/logger"
)
var version = "1.2.9"
var version = "1.2.10"
var buildTime = "unknown"
const logo = ">"

View File

@@ -52,6 +52,10 @@ func parseOpenAICompatResponse(body []byte) (*LLMResponse, error) {
if len(choice.Message.ToolCalls) > 0 {
resp.ToolCalls = make([]ToolCall, 0, len(choice.Message.ToolCalls))
for _, tc := range choice.Message.ToolCalls {
args := map[string]interface{}{}
if strings.TrimSpace(tc.Function.Arguments) != "" {
_ = json.Unmarshal([]byte(tc.Function.Arguments), &args)
}
resp.ToolCalls = append(resp.ToolCalls, ToolCall{
ID: tc.ID,
Type: tc.Type,
@@ -59,7 +63,8 @@ func parseOpenAICompatResponse(body []byte) (*LLMResponse, error) {
Name: tc.Function.Name,
Arguments: tc.Function.Arguments,
},
Name: tc.Function.Name,
Name: tc.Function.Name,
Arguments: args,
})
}
}

View File

@@ -226,6 +226,22 @@ func TestParseOpenAICompatResponseCapturesReasoningContent(t *testing.T) {
}
}
func TestParseOpenAICompatResponsePopulatesToolArgumentsMap(t *testing.T) {
resp, err := parseOpenAICompatResponse([]byte(`{"choices":[{"message":{"tool_calls":[{"id":"call_1","type":"function","function":{"name":"remind","arguments":"{\"message\":\"开会\",\"time_expr\":\"10m\"}"}}]},"finish_reason":"tool_calls"}]}`))
if err != nil {
t.Fatalf("parseOpenAICompatResponse error: %v", err)
}
if len(resp.ToolCalls) != 1 {
t.Fatalf("tool calls = %#v, want one call", resp.ToolCalls)
}
if got := asString(resp.ToolCalls[0].Arguments["message"]); got != "开会" {
t.Fatalf("message = %q, want 开会", got)
}
if got := asString(resp.ToolCalls[0].Arguments["time_expr"]); got != "10m" {
t.Fatalf("time_expr = %q, want 10m", got)
}
}
func TestOpenAICompatMessagesIncludeReasoningContent(t *testing.T) {
msgs := openAICompatMessages([]Message{{
Role: "assistant",