|
7 | 7 | import { createRoutedCustomToolRestoreBlockRewrite } from "../src/server/responses-custom-tool-repair"; |
8 | 8 | import { handleResponses } from "../src/server/responses"; |
9 | 9 | import type { OcxConfig } from "../src/types"; |
| 10 | +import { createTestTranslatorBudget } from "./helpers/translator-budget"; |
10 | 11 |
|
11 | 12 | function dataPayload(block: string): Record<string, unknown> { |
12 | 13 | const line = block.split(/\r?\n/).find(entry => entry.startsWith("data:")); |
@@ -167,6 +168,90 @@ describe("routed Responses custom-tool compatibility", () => { |
167 | 168 | rewrite.dispose?.(); |
168 | 169 | }); |
169 | 170 |
|
| 171 | + test("buffers argument events until a missing added event is identified by item done", () => { |
| 172 | + const budget = createTestTranslatorBudget(); |
| 173 | + const rewrite = createRoutedCustomToolRestoreBlockRewrite(new Set(["exec"]), budget); |
| 174 | + const deltaBlock = frame("response.function_call_arguments.delta", { |
| 175 | + output_index: 0, |
| 176 | + item_id: "fc_exec", |
| 177 | + delta: "{\"input\":\"echo", |
| 178 | + }); |
| 179 | + const argumentsDoneBlock = frame("response.function_call_arguments.done", { |
| 180 | + output_index: 0, |
| 181 | + item_id: "fc_exec", |
| 182 | + arguments: "{\"input\":\"echo ok\"}", |
| 183 | + }); |
| 184 | + |
| 185 | + expect(rewrite(deltaBlock)).toEqual([]); |
| 186 | + expect(rewrite(argumentsDoneBlock)).toEqual([]); |
| 187 | + expect(budget.snapshot().currentBytes).toBeGreaterThan(0); |
| 188 | + |
| 189 | + const replayed = rewrite(frame("response.output_item.done", { |
| 190 | + output_index: 0, |
| 191 | + item: { |
| 192 | + type: "function_call", |
| 193 | + id: "fc_exec", |
| 194 | + call_id: "call_exec", |
| 195 | + name: "exec", |
| 196 | + arguments: "{\"input\":\"echo ok\"}", |
| 197 | + status: "completed", |
| 198 | + }, |
| 199 | + })); |
| 200 | + expect(replayed.map(block => dataPayload(block).type)).toEqual([ |
| 201 | + "response.custom_tool_call_input.delta", |
| 202 | + "response.custom_tool_call_input.done", |
| 203 | + "response.output_item.done", |
| 204 | + ]); |
| 205 | + expect(dataPayload(replayed[0]!)).toMatchObject({ item_id: "ctc_exec", delta: "echo" }); |
| 206 | + expect(dataPayload(replayed[1]!)).toMatchObject({ item_id: "ctc_exec", input: "echo ok" }); |
| 207 | + expect(dataPayload(replayed[2]!).item).toMatchObject({ |
| 208 | + type: "custom_tool_call", |
| 209 | + id: "ctc_exec", |
| 210 | + name: "exec", |
| 211 | + input: "echo ok", |
| 212 | + }); |
| 213 | + expect(budget.snapshot().currentBytes).toBe(0); |
| 214 | + rewrite.dispose?.(); |
| 215 | + }); |
| 216 | + |
| 217 | + test("replays buffered events unchanged when item done identifies an ordinary function", () => { |
| 218 | + const budget = createTestTranslatorBudget(); |
| 219 | + const rewrite = createRoutedCustomToolRestoreBlockRewrite(new Set(["exec"]), budget); |
| 220 | + const deltaBlock = frame("response.function_call_arguments.delta", { |
| 221 | + output_index: 1, |
| 222 | + item_id: "fc_other", |
| 223 | + delta: "{}", |
| 224 | + }); |
| 225 | + |
| 226 | + expect(rewrite(deltaBlock)).toEqual([]); |
| 227 | + expect(budget.snapshot().currentBytes).toBeGreaterThan(0); |
| 228 | + |
| 229 | + const replayed = rewrite(frame("response.output_item.done", { |
| 230 | + output_index: 1, |
| 231 | + item: { |
| 232 | + type: "function_call", |
| 233 | + id: "fc_other", |
| 234 | + call_id: "call_other", |
| 235 | + name: "ordinary", |
| 236 | + arguments: "{}", |
| 237 | + status: "completed", |
| 238 | + }, |
| 239 | + })); |
| 240 | + expect(replayed).toEqual([deltaBlock, frame("response.output_item.done", { |
| 241 | + output_index: 1, |
| 242 | + item: { |
| 243 | + type: "function_call", |
| 244 | + id: "fc_other", |
| 245 | + call_id: "call_other", |
| 246 | + name: "ordinary", |
| 247 | + arguments: "{}", |
| 248 | + status: "completed", |
| 249 | + }, |
| 250 | + })]); |
| 251 | + expect(budget.snapshot().currentBytes).toBe(0); |
| 252 | + rewrite.dispose?.(); |
| 253 | + }); |
| 254 | + |
170 | 255 | test("keeps progressive exec input consistent for escaped control characters", () => { |
171 | 256 | const rewrite = createRoutedCustomToolRestoreBlockRewrite(new Set(["exec"])); |
172 | 257 | rewrite(frame("response.output_item.added", { |
@@ -250,9 +335,114 @@ describe("routed Responses custom-tool compatibility", () => { |
250 | 335 | expect(clientSse).toContain('"type":"response.custom_tool_call_input.done"'); |
251 | 336 | expect(clientSse).toContain('"input":"const apps = await sky.list_apps();"'); |
252 | 337 | expect(clientSse).not.toContain("response.function_call_arguments.done"); |
| 338 | + expect(clientSse).not.toContain('"type":"function_call"'); |
253 | 339 | expect(clientSse).toContain("data: [DONE]"); |
254 | 340 | } finally { |
255 | 341 | globalThis.fetch = savedFetch; |
256 | 342 | } |
257 | 343 | }); |
| 344 | + |
| 345 | + test("handleResponses restores routed custom calls in non-streaming JSON", async () => { |
| 346 | + const savedFetch = globalThis.fetch; |
| 347 | + const upstreamItem = { |
| 348 | + type: "function_call", |
| 349 | + id: "fc_exec", |
| 350 | + call_id: "call_exec", |
| 351 | + name: "exec", |
| 352 | + arguments: "{\"input\":\"const apps = await sky.list_apps();\"}", |
| 353 | + status: "completed", |
| 354 | + }; |
| 355 | + globalThis.fetch = (async () => new Response(JSON.stringify({ |
| 356 | + id: "resp_json", |
| 357 | + status: "completed", |
| 358 | + output: [upstreamItem], |
| 359 | + }), { headers: { "content-type": "application/json" } })) as typeof fetch; |
| 360 | + const config = { |
| 361 | + port: 0, |
| 362 | + defaultProvider: "fixture", |
| 363 | + providers: { |
| 364 | + fixture: { |
| 365 | + adapter: "openai-responses", |
| 366 | + baseUrl: "https://fixture.test/v1", |
| 367 | + authMode: "key", |
| 368 | + apiKey: "fixture-key", |
| 369 | + }, |
| 370 | + }, |
| 371 | + } as OcxConfig; |
| 372 | + |
| 373 | + try { |
| 374 | + const response = await handleResponses(new Request("http://localhost/v1/responses", { |
| 375 | + method: "POST", |
| 376 | + headers: { "content-type": "application/json" }, |
| 377 | + body: JSON.stringify({ |
| 378 | + model: "fixture/deepseek-v4-flash", |
| 379 | + stream: false, |
| 380 | + input: [{ role: "user", content: [{ type: "input_text", text: "list apps" }] }], |
| 381 | + tools: [{ type: "custom", name: "exec", description: "Run JavaScript", format: { type: "grammar", syntax: "lark" } }], |
| 382 | + }), |
| 383 | + }), config, { model: "", provider: "" }); |
| 384 | + const body = await response.json() as { output: Array<Record<string, unknown>> }; |
| 385 | + |
| 386 | + expect(body.output[0]).toMatchObject({ |
| 387 | + type: "custom_tool_call", |
| 388 | + id: "ctc_exec", |
| 389 | + name: "exec", |
| 390 | + input: "const apps = await sky.list_apps();", |
| 391 | + }); |
| 392 | + expect(body.output[0]).not.toHaveProperty("arguments"); |
| 393 | + } finally { |
| 394 | + globalThis.fetch = savedFetch; |
| 395 | + } |
| 396 | + }); |
| 397 | + |
| 398 | + test("handleResponses leaves custom tools native for forward-auth passthrough", async () => { |
| 399 | + const savedFetch = globalThis.fetch; |
| 400 | + let outboundBody: Record<string, unknown> | undefined; |
| 401 | + const upstreamItem = { |
| 402 | + type: "function_call", |
| 403 | + id: "fc_exec", |
| 404 | + call_id: "call_exec", |
| 405 | + name: "exec", |
| 406 | + arguments: "{\"input\":\"native\"}", |
| 407 | + status: "completed", |
| 408 | + }; |
| 409 | + globalThis.fetch = (async (_input, init) => { |
| 410 | + outboundBody = JSON.parse(String(init?.body)) as Record<string, unknown>; |
| 411 | + return new Response(JSON.stringify({ id: "resp_forward", status: "completed", output: [upstreamItem] }), { |
| 412 | + headers: { "content-type": "application/json" }, |
| 413 | + }); |
| 414 | + }) as typeof fetch; |
| 415 | + const config = { |
| 416 | + port: 0, |
| 417 | + defaultProvider: "fixture", |
| 418 | + providers: { |
| 419 | + fixture: { |
| 420 | + adapter: "openai-responses", |
| 421 | + baseUrl: "https://forward.fixture.test", |
| 422 | + authMode: "forward", |
| 423 | + }, |
| 424 | + }, |
| 425 | + } as OcxConfig; |
| 426 | + |
| 427 | + try { |
| 428 | + const response = await handleResponses(new Request("http://localhost/v1/responses", { |
| 429 | + method: "POST", |
| 430 | + headers: { "content-type": "application/json", authorization: "Bearer caller-token" }, |
| 431 | + body: JSON.stringify({ |
| 432 | + model: "fixture/native-model", |
| 433 | + stream: false, |
| 434 | + input: "run", |
| 435 | + tools: [{ type: "custom", name: "exec", description: "Run JavaScript", format: { type: "grammar", syntax: "lark" } }], |
| 436 | + }), |
| 437 | + }), config, { model: "", provider: "" }); |
| 438 | + const clientBody = await response.json() as { output: Array<Record<string, unknown>> }; |
| 439 | + const outboundTools = outboundBody?.tools as Array<Record<string, unknown>> | undefined; |
| 440 | + |
| 441 | + expect(outboundTools?.[0]).toMatchObject({ type: "custom", name: "exec" }); |
| 442 | + expect(clientBody.output[0]).toMatchObject({ type: "function_call", name: "exec" }); |
| 443 | + expect(clientBody.output[0]).not.toHaveProperty("input"); |
| 444 | + } finally { |
| 445 | + globalThis.fetch = savedFetch; |
| 446 | + } |
| 447 | + }); |
258 | 448 | }); |
0 commit comments