TestChatToolArgsStreamLive: each tool-call arguments fragment is forwarded as an EventToolArgs as it arrives, so the UI can tick its live token estimate while a file streams into write_file, not just once at stream end. Fragments concatenate to the full arguments and all precede the resolved EventTo
(t *testing.T)
| 160 | switch e.Kind { |
| 161 | case EventToolCall: |
| 162 | calls = append(calls, *e.ToolCall) |
| 163 | case EventDone: |
| 164 | final = e.Final |
| 165 | } |
| 166 | } |
| 167 | if len(calls) != 1 || calls[0].Name != "bash" || calls[0].ID != "call_1" { |
| 168 | t.Fatalf("want one bash call with call_id, got %+v", calls) |
| 169 | } |
| 170 | if cmd, _ := calls[0].Arguments["cmd"].(string); cmd != "ls" { |
| 171 | t.Fatalf("tool args wrong (done must replace, not append to, the deltas): %+v", calls[0].Arguments) |
| 172 | } |
| 173 | if final == nil || len(final.ToolCalls) != 1 || final.ToolCalls[0].Name != "bash" { |
| 174 | t.Fatalf("Final.ToolCalls should carry the bash call: %+v", final) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // TestChatToolCallFromDoneOnly: a server that streams no argument deltas and |
| 179 | // no arguments.done (only the finished item) must still resolve the call. |
| 180 | func TestChatToolCallFromDoneOnly(t *testing.T) { |
| 181 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 182 | sseOK(w, []string{ |
| 183 | callDone(0, "c1", "bash", `{"cmd":"ls"}`), |
| 184 | completed(5, 0), |
| 185 | }) |
| 186 | })) |
| 187 | defer srv.Close() |
| 188 | var got *chmctx.ToolCall |
| 189 | for _, e := range collect(New(srv.URL, "m", "").Chat(context.Background(), nil, nil)) { |
| 190 | if e.Kind == EventToolCall { |
| 191 | got = e.ToolCall |
| 192 | } |
| 193 | } |
| 194 | if got == nil || got.Name != "bash" || got.ID != "c1" { |
| 195 | t.Fatalf("tool call missing: %+v", got) |
| 196 | } |
| 197 | if cmd, _ := got.Arguments["cmd"].(string); cmd != "ls" { |
| 198 | t.Fatalf("args wrong: %+v", got.Arguments) |
| 199 | } |
| 200 | } |