Add Tool Call ID (#12956)

* routes/types: add tool call id

---------

Co-authored-by: ParthSareen <parth.sareen@ollama.com>
This commit is contained in:
Grace
2025-11-04 16:43:33 -08:00
committed by GitHub
parent ba8c035846
commit 809b9c68fa
5 changed files with 122 additions and 16 deletions

View File

@@ -4,6 +4,8 @@ import (
"encoding/base64"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/ollama/ollama/api"
)
@@ -148,3 +150,71 @@ func TestNewError(t *testing.T) {
}
}
}
func TestToToolCallsPreservesIDs(t *testing.T) {
original := []api.ToolCall{
{
ID: "call_abc123",
Function: api.ToolCallFunction{
Index: 2,
Name: "get_weather",
Arguments: api.ToolCallFunctionArguments{
"location": "Seattle",
},
},
},
{
ID: "call_def456",
Function: api.ToolCallFunction{
Index: 7,
Name: "get_time",
Arguments: api.ToolCallFunctionArguments{
"timezone": "UTC",
},
},
},
}
toolCalls := make([]api.ToolCall, len(original))
copy(toolCalls, original)
got := ToToolCalls(toolCalls)
if len(got) != len(original) {
t.Fatalf("expected %d tool calls, got %d", len(original), len(got))
}
expected := []ToolCall{
{
ID: "call_abc123",
Type: "function",
Index: 2,
Function: struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
}{
Name: "get_weather",
Arguments: `{"location":"Seattle"}`,
},
},
{
ID: "call_def456",
Type: "function",
Index: 7,
Function: struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
}{
Name: "get_time",
Arguments: `{"timezone":"UTC"}`,
},
},
}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("tool calls mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(original, toolCalls); diff != "" {
t.Errorf("input tool calls mutated (-want +got):\n%s", diff)
}
}