revert behaviour

This commit is contained in:
Kunal Karmakar 2026-03-27 16:53:56 +00:00
parent 280f435a35
commit 98c55357ef
2 changed files with 24 additions and 14 deletions

View file

@ -61,8 +61,8 @@ func TranslateMessages(messages []protocoltypes.Message) (input responses.Respon
}) })
} }
for _, tc := range msg.ToolCalls { for _, tc := range msg.ToolCalls {
name, args := ResolveToolCall(tc) name, args, ok := ResolveToolCall(tc)
if name == "" { if !ok {
continue continue
} }
input = append(input, responses.ResponseInputItemUnionParam{ input = append(input, responses.ResponseInputItemUnionParam{
@ -152,28 +152,29 @@ func ParseDataAudioURL(mediaURL string) (format, data string, ok bool) {
} }
// ResolveToolCall extracts the function name and JSON arguments string from a ToolCall. // ResolveToolCall extracts the function name and JSON arguments string from a ToolCall.
func ResolveToolCall(tc protocoltypes.ToolCall) (name string, arguments string) { // Returns ok=false if the tool call has no name or if arguments fail to marshal.
func ResolveToolCall(tc protocoltypes.ToolCall) (name string, arguments string, ok bool) {
name = tc.Name name = tc.Name
if name == "" && tc.Function != nil { if name == "" && tc.Function != nil {
name = tc.Function.Name name = tc.Function.Name
} }
if name == "" { if name == "" {
return "", "" return "", "", false
} }
if len(tc.Arguments) > 0 { if len(tc.Arguments) > 0 {
argsJSON, err := json.Marshal(tc.Arguments) argsJSON, err := json.Marshal(tc.Arguments)
if err != nil { if err != nil {
return name, "{}" return "", "", false
} }
return name, string(argsJSON) return name, string(argsJSON), true
} }
if tc.Function != nil && tc.Function.Arguments != "" { if tc.Function != nil && tc.Function.Arguments != "" {
return name, tc.Function.Arguments return name, tc.Function.Arguments, true
} }
return name, "{}" return name, "{}", true
} }
// TranslateTools converts internal ToolDefinition entries to the OpenAI Responses API // TranslateTools converts internal ToolDefinition entries to the OpenAI Responses API

View file

@ -148,7 +148,10 @@ func TestResolveToolCall_FromNameAndArguments(t *testing.T) {
Name: "get_weather", Name: "get_weather",
Arguments: map[string]any{"city": "SF"}, Arguments: map[string]any{"city": "SF"},
} }
name, args := ResolveToolCall(tc) name, args, ok := ResolveToolCall(tc)
if !ok {
t.Fatal("expected ok=true")
}
if name != "get_weather" { if name != "get_weather" {
t.Errorf("name = %q, want %q", name, "get_weather") t.Errorf("name = %q, want %q", name, "get_weather")
} }
@ -165,7 +168,10 @@ func TestResolveToolCall_FromFunctionField(t *testing.T) {
Arguments: `{"path":"README.md"}`, Arguments: `{"path":"README.md"}`,
}, },
} }
name, args := ResolveToolCall(tc) name, args, ok := ResolveToolCall(tc)
if !ok {
t.Fatal("expected ok=true")
}
if name != "read_file" { if name != "read_file" {
t.Errorf("name = %q, want %q", name, "read_file") t.Errorf("name = %q, want %q", name, "read_file")
} }
@ -176,15 +182,18 @@ func TestResolveToolCall_FromFunctionField(t *testing.T) {
func TestResolveToolCall_EmptyName(t *testing.T) { func TestResolveToolCall_EmptyName(t *testing.T) {
tc := protocoltypes.ToolCall{} tc := protocoltypes.ToolCall{}
name, _ := ResolveToolCall(tc) _, _, ok := ResolveToolCall(tc)
if name != "" { if ok {
t.Errorf("name = %q, want empty", name) t.Error("expected ok=false for empty tool call")
} }
} }
func TestResolveToolCall_NoArgsFallsBackToEmptyObject(t *testing.T) { func TestResolveToolCall_NoArgsFallsBackToEmptyObject(t *testing.T) {
tc := protocoltypes.ToolCall{Name: "do_something"} tc := protocoltypes.ToolCall{Name: "do_something"}
name, args := ResolveToolCall(tc) name, args, ok := ResolveToolCall(tc)
if !ok {
t.Fatal("expected ok=true")
}
if name != "do_something" { if name != "do_something" {
t.Errorf("name = %q, want %q", name, "do_something") t.Errorf("name = %q, want %q", name, "do_something")
} }