|
1 | 1 | package runtime |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/stretchr/testify/require" |
7 | 8 |
|
8 | 9 | "github.com/docker/docker-agent/pkg/session" |
| 10 | + "github.com/docker/docker-agent/pkg/tools" |
9 | 11 | ) |
10 | 12 |
|
11 | 13 | // TestResponseAPIToolCallHandling verifies that tool calls from the Response API |
@@ -74,3 +76,57 @@ func TestResponseAPIMultipleToolCalls(t *testing.T) { |
74 | 76 | } |
75 | 77 | require.ElementsMatch(t, []string{"search", "calculate"}, toolCalls, "Expected both tool calls") |
76 | 78 | } |
| 79 | + |
| 80 | +func TestPartialToolCallEventsContainOnlyNewArgumentBytes(t *testing.T) { |
| 81 | + stream := newStreamBuilder(). |
| 82 | + AddToolCallName("call_abc", "write_file"). |
| 83 | + AddToolCallArguments("call_abc", `{"path":"story.md"`). |
| 84 | + AddToolCallArguments("call_abc", `,"content":"Once upon a time"}`). |
| 85 | + AddStopWithUsage(10, 15). |
| 86 | + Build() |
| 87 | + |
| 88 | + sess := session.New(session.WithUserMessage("Write a story")) |
| 89 | + events := runSession(t, sess, stream) |
| 90 | + |
| 91 | + var partials []*PartialToolCallEvent |
| 92 | + for _, event := range events { |
| 93 | + if ev, ok := event.(*PartialToolCallEvent); ok { |
| 94 | + partials = append(partials, ev) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + require.Len(t, partials, 3) |
| 99 | + require.Equal(t, "write_file", partials[0].ToolCall.Function.Name) |
| 100 | + require.Empty(t, partials[0].ToolCall.Function.Arguments) |
| 101 | + require.Equal(t, `{"path":"story.md"`, partials[1].ToolCall.Function.Arguments) //nolint:testifylint // testifylint wants us to use require.JSONEq but the expected value is not valid JSON |
| 102 | + require.Nil(t, partials[1].ToolDefinition) |
| 103 | + require.Equal(t, `,"content":"Once upon a time"}`, partials[2].ToolCall.Function.Arguments) |
| 104 | + require.Nil(t, partials[2].ToolDefinition) |
| 105 | + |
| 106 | + secondJSON, err := json.Marshal(partials[1]) |
| 107 | + require.NoError(t, err) |
| 108 | + require.NotContains(t, string(secondJSON), `"tool_definition"`) |
| 109 | +} |
| 110 | + |
| 111 | +func TestPartialToolCallEventJSONIncludesToolDefinitionOnlyWhenPresent(t *testing.T) { |
| 112 | + toolDef := &tools.Tool{Name: "write_file", Description: "Create file"} |
| 113 | + withDef := &PartialToolCallEvent{ |
| 114 | + Type: "partial_tool_call", |
| 115 | + ToolCall: tools.ToolCall{ID: "call_1", Type: "function", Function: tools.FunctionCall{Name: "write_file"}}, |
| 116 | + ToolDefinition: toolDef, |
| 117 | + AgentContext: newAgentContext("root"), |
| 118 | + } |
| 119 | + withoutDef := &PartialToolCallEvent{ |
| 120 | + Type: "partial_tool_call", |
| 121 | + ToolCall: tools.ToolCall{ID: "call_1", Type: "function", Function: tools.FunctionCall{Name: "write_file", Arguments: `{"path":"story.md"}`}}, |
| 122 | + AgentContext: newAgentContext("root"), |
| 123 | + } |
| 124 | + |
| 125 | + withDefJSON, err := json.Marshal(withDef) |
| 126 | + require.NoError(t, err) |
| 127 | + require.Contains(t, string(withDefJSON), `"tool_definition"`) |
| 128 | + |
| 129 | + withoutDefJSON, err := json.Marshal(withoutDef) |
| 130 | + require.NoError(t, err) |
| 131 | + require.NotContains(t, string(withoutDefJSON), `"tool_definition"`) |
| 132 | +} |
0 commit comments