fix: codex tool call
This commit is contained in:
parent
4cd3f99dd6
commit
0d16525fab
2 changed files with 72 additions and 3 deletions
|
|
@ -217,12 +217,18 @@ func buildCodexParams(messages []Message, tools []ToolDefinition, model string,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
for _, tc := range msg.ToolCalls {
|
for _, tc := range msg.ToolCalls {
|
||||||
argsJSON, _ := json.Marshal(tc.Arguments)
|
name, args, ok := resolveCodexToolCall(tc)
|
||||||
|
if !ok {
|
||||||
|
logger.WarnCF("provider.codex", "Skipping invalid tool call in history", map[string]interface{}{
|
||||||
|
"call_id": tc.ID,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
inputItems = append(inputItems, responses.ResponseInputItemUnionParam{
|
inputItems = append(inputItems, responses.ResponseInputItemUnionParam{
|
||||||
OfFunctionCall: &responses.ResponseFunctionToolCallParam{
|
OfFunctionCall: &responses.ResponseFunctionToolCallParam{
|
||||||
CallID: tc.ID,
|
CallID: tc.ID,
|
||||||
Name: tc.Name,
|
Name: name,
|
||||||
Arguments: string(argsJSON),
|
Arguments: args,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -267,6 +273,30 @@ func buildCodexParams(messages []Message, tools []ToolDefinition, model string,
|
||||||
return params
|
return params
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resolveCodexToolCall(tc ToolCall) (name string, arguments string, ok bool) {
|
||||||
|
name = tc.Name
|
||||||
|
if name == "" && tc.Function != nil {
|
||||||
|
name = tc.Function.Name
|
||||||
|
}
|
||||||
|
if name == "" {
|
||||||
|
return "", "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(tc.Arguments) > 0 {
|
||||||
|
argsJSON, err := json.Marshal(tc.Arguments)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", false
|
||||||
|
}
|
||||||
|
return name, string(argsJSON), true
|
||||||
|
}
|
||||||
|
|
||||||
|
if tc.Function != nil && tc.Function.Arguments != "" {
|
||||||
|
return name, tc.Function.Arguments, true
|
||||||
|
}
|
||||||
|
|
||||||
|
return name, "{}", true
|
||||||
|
}
|
||||||
|
|
||||||
func translateToolsForCodex(tools []ToolDefinition) []responses.ToolUnionParam {
|
func translateToolsForCodex(tools []ToolDefinition) []responses.ToolUnionParam {
|
||||||
result := make([]responses.ToolUnionParam, 0, len(tools))
|
result := make([]responses.ToolUnionParam, 0, len(tools))
|
||||||
for _, t := range tools {
|
for _, t := range tools {
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,45 @@ func TestBuildCodexParams_ToolCallConversation(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuildCodexParams_ToolCallFunctionFallback(t *testing.T) {
|
||||||
|
messages := []Message{
|
||||||
|
{Role: "user", Content: "Read a file"},
|
||||||
|
{
|
||||||
|
Role: "assistant",
|
||||||
|
ToolCalls: []ToolCall{
|
||||||
|
{
|
||||||
|
ID: "call_1",
|
||||||
|
Type: "function",
|
||||||
|
Function: &FunctionCall{
|
||||||
|
Name: "read_file",
|
||||||
|
Arguments: `{"path":"README.md"}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{Role: "tool", Content: "ok", ToolCallID: "call_1"},
|
||||||
|
}
|
||||||
|
|
||||||
|
params := buildCodexParams(messages, nil, "gpt-4o", map[string]interface{}{})
|
||||||
|
if params.Input.OfInputItemList == nil {
|
||||||
|
t.Fatal("Input.OfInputItemList should not be nil")
|
||||||
|
}
|
||||||
|
if len(params.Input.OfInputItemList) != 3 {
|
||||||
|
t.Fatalf("len(Input items) = %d, want 3", len(params.Input.OfInputItemList))
|
||||||
|
}
|
||||||
|
|
||||||
|
fc := params.Input.OfInputItemList[1].OfFunctionCall
|
||||||
|
if fc == nil {
|
||||||
|
t.Fatal("assistant tool call should be converted to function_call input item")
|
||||||
|
}
|
||||||
|
if fc.Name != "read_file" {
|
||||||
|
t.Errorf("Function call name = %q, want %q", fc.Name, "read_file")
|
||||||
|
}
|
||||||
|
if fc.Arguments != `{"path":"README.md"}` {
|
||||||
|
t.Errorf("Function call arguments = %q, want %q", fc.Arguments, `{"path":"README.md"}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestBuildCodexParams_WithTools(t *testing.T) {
|
func TestBuildCodexParams_WithTools(t *testing.T) {
|
||||||
tools := []ToolDefinition{
|
tools := []ToolDefinition{
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue