mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-05-17 22:28:57 +08:00
fix openai compat tool argument parsing
This commit is contained in:
@@ -15,7 +15,7 @@ import (
|
|||||||
"github.com/YspCoder/clawgo/pkg/logger"
|
"github.com/YspCoder/clawgo/pkg/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var version = "1.2.9"
|
var version = "1.2.10"
|
||||||
var buildTime = "unknown"
|
var buildTime = "unknown"
|
||||||
|
|
||||||
const logo = ">"
|
const logo = ">"
|
||||||
|
|||||||
@@ -52,6 +52,10 @@ func parseOpenAICompatResponse(body []byte) (*LLMResponse, error) {
|
|||||||
if len(choice.Message.ToolCalls) > 0 {
|
if len(choice.Message.ToolCalls) > 0 {
|
||||||
resp.ToolCalls = make([]ToolCall, 0, len(choice.Message.ToolCalls))
|
resp.ToolCalls = make([]ToolCall, 0, len(choice.Message.ToolCalls))
|
||||||
for _, tc := range 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{
|
resp.ToolCalls = append(resp.ToolCalls, ToolCall{
|
||||||
ID: tc.ID,
|
ID: tc.ID,
|
||||||
Type: tc.Type,
|
Type: tc.Type,
|
||||||
@@ -59,7 +63,8 @@ func parseOpenAICompatResponse(body []byte) (*LLMResponse, error) {
|
|||||||
Name: tc.Function.Name,
|
Name: tc.Function.Name,
|
||||||
Arguments: tc.Function.Arguments,
|
Arguments: tc.Function.Arguments,
|
||||||
},
|
},
|
||||||
Name: tc.Function.Name,
|
Name: tc.Function.Name,
|
||||||
|
Arguments: args,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) {
|
func TestOpenAICompatMessagesIncludeReasoningContent(t *testing.T) {
|
||||||
msgs := openAICompatMessages([]Message{{
|
msgs := openAICompatMessages([]Message{{
|
||||||
Role: "assistant",
|
Role: "assistant",
|
||||||
|
|||||||
Reference in New Issue
Block a user