|
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,95 @@ 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) |
| 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 | +} |
| 133 | + |
| 134 | +func TestPartialToolCallEventsNormalizeCumulativeArgumentSnapshots(t *testing.T) { |
| 135 | + // Some providers resend the full accumulated argument buffer on each tool-call |
| 136 | + // update. PartialToolCallEvent should still emit only the new suffix bytes. |
| 137 | + stream := newStreamBuilder(). |
| 138 | + AddToolCallName("call_abc", "write_file"). |
| 139 | + AddToolCallArguments("call_abc", `{"path":"story.md"`). |
| 140 | + AddToolCallArguments("call_abc", `{"path":"story.md","content":"Once upon a time"}`). |
| 141 | + AddStopWithUsage(10, 15). |
| 142 | + Build() |
| 143 | + |
| 144 | + sess := session.New(session.WithUserMessage("Write a story")) |
| 145 | + events := runSession(t, sess, stream) |
| 146 | + |
| 147 | + var partials []*PartialToolCallEvent |
| 148 | + for _, event := range events { |
| 149 | + if ev, ok := event.(*PartialToolCallEvent); ok { |
| 150 | + partials = append(partials, ev) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + require.Len(t, partials, 3) |
| 155 | + require.Equal(t, "write_file", partials[0].ToolCall.Function.Name) |
| 156 | + require.Empty(t, partials[0].ToolCall.Function.Arguments) |
| 157 | + require.Equal(t, `{"path":"story.md"`, partials[1].ToolCall.Function.Arguments) |
| 158 | + require.Equal(t, `,"content":"Once upon a time"}`, partials[2].ToolCall.Function.Arguments) |
| 159 | + |
| 160 | + messages := sess.GetAllMessages() |
| 161 | + var foundToolCall bool |
| 162 | + for _, msg := range messages { |
| 163 | + if msg.Message.Role == "assistant" && len(msg.Message.ToolCalls) > 0 { |
| 164 | + foundToolCall = true |
| 165 | + require.Equal(t, `{"path":"story.md","content":"Once upon a time"}`, |
| 166 | + msg.Message.ToolCalls[0].Function.Arguments) |
| 167 | + } |
| 168 | + } |
| 169 | + require.True(t, foundToolCall, "Expected to find complete tool call in session messages") |
| 170 | +} |
0 commit comments