|
3 | 3 | anthropicErrorBody, |
4 | 4 | anthropicErrorType, |
5 | 5 | anthropicUsage, |
| 6 | + collectAnthropicMessage, |
6 | 7 | responsesJsonToAnthropicMessage, |
7 | 8 | responsesSseToAnthropicSse, |
8 | 9 | } from "../src/claude/outbound"; |
@@ -207,3 +208,144 @@ describe("claude outbound non-stream + helpers", () => { |
207 | 208 | expect(anthropicUsage({ input_tokens: 7, output_tokens: 2 })).toEqual({ input_tokens: 7, output_tokens: 2, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 }); |
208 | 209 | }); |
209 | 210 | }); |
| 211 | + |
| 212 | +describe("claude outbound web_search translation", () => { |
| 213 | + const wsItem = (over: Record<string, unknown> = {}) => ({ |
| 214 | + type: "web_search_call", id: "ws_1", status: "completed", |
| 215 | + action: { type: "search", query: "latest bun release" }, |
| 216 | + sources: [ |
| 217 | + { url: "https://bun.sh/blog", title: "Bun Blog" }, |
| 218 | + { url: "https://github.com/oven-sh/bun/releases" }, |
| 219 | + ], |
| 220 | + ...over, |
| 221 | + }); |
| 222 | + |
| 223 | + test("T1 single search -> server_tool_use + web_search_tool_result pair with usage count", async () => { |
| 224 | + const upstream = [ |
| 225 | + sse("response.created", { response: { id: "r", status: "in_progress" } }), |
| 226 | + sse("response.output_item.added", { output_index: 0, item: { type: "web_search_call", id: "ws_1", status: "in_progress" } }), |
| 227 | + sse("response.output_item.done", { output_index: 0, item: wsItem() }), |
| 228 | + sse("response.output_item.added", { output_index: 1, item: { type: "message", id: "m1" } }), |
| 229 | + sse("response.output_text.delta", { item_id: "m1", output_index: 1, content_index: 0, delta: "answer" }), |
| 230 | + sse("response.output_item.done", { output_index: 1, item: { type: "message", id: "m1" } }), |
| 231 | + sse("response.completed", { response: { status: "completed", usage: { input_tokens: 9, output_tokens: 3 } } }), |
| 232 | + ].join(""); |
| 233 | + const events = await collectEvents(responsesSseToAnthropicSse(streamFrom(upstream), "m")); |
| 234 | + expect(events.map(e => e.name)).toEqual([ |
| 235 | + "message_start", "ping", |
| 236 | + "content_block_start", "content_block_delta", "content_block_stop", // server_tool_use |
| 237 | + "content_block_start", "content_block_stop", // web_search_tool_result |
| 238 | + "content_block_start", "content_block_delta", "content_block_stop", // text |
| 239 | + "message_delta", "message_stop", |
| 240 | + ]); |
| 241 | + // server_tool_use start has no inline input; query arrives via input_json_delta. |
| 242 | + expect(events[2].data.content_block).toEqual({ type: "server_tool_use", id: "ws_1", name: "web_search" }); |
| 243 | + expect(events[3].data.delta).toEqual({ type: "input_json_delta", partial_json: JSON.stringify({ query: "latest bun release" }) }); |
| 244 | + const result = events[5].data.content_block; |
| 245 | + expect(result.type).toBe("web_search_tool_result"); |
| 246 | + expect(result.tool_use_id).toBe("ws_1"); |
| 247 | + expect(result.content).toEqual([ |
| 248 | + { type: "web_search_result", title: "Bun Blog", url: "https://bun.sh/blog" }, |
| 249 | + { type: "web_search_result", title: "", url: "https://github.com/oven-sh/bun/releases" }, |
| 250 | + ]); |
| 251 | + const usage = events[10].data.usage; |
| 252 | + expect(usage.server_tool_use).toEqual({ web_search_requests: 1 }); |
| 253 | + // stop_reason stays end_turn: server_tool_use is not a client tool call. |
| 254 | + expect(events[10].data.delta.stop_reason).toBe("end_turn"); |
| 255 | + }); |
| 256 | + |
| 257 | + test("T2 multi-search -> two pairs, usage 2, monotonic indexes", async () => { |
| 258 | + const upstream = [ |
| 259 | + sse("response.created", { response: {} }), |
| 260 | + sse("response.output_item.done", { output_index: 0, item: wsItem() }), |
| 261 | + sse("response.output_item.done", { output_index: 1, item: wsItem({ id: "ws_2", action: { type: "search", queries: ["a", "b"] }, sources: [] }) }), |
| 262 | + sse("response.completed", { response: { status: "completed", usage: {} } }), |
| 263 | + ].join(""); |
| 264 | + const events = await collectEvents(responsesSseToAnthropicSse(streamFrom(upstream), "m")); |
| 265 | + const starts = events.filter(e => e.name === "content_block_start"); |
| 266 | + expect(starts.map(e => e.data.content_block.type)).toEqual([ |
| 267 | + "server_tool_use", "web_search_tool_result", "server_tool_use", "web_search_tool_result", |
| 268 | + ]); |
| 269 | + const indexes = starts.map(e => e.data.index); |
| 270 | + expect(indexes).toEqual([0, 1, 2, 3]); |
| 271 | + // Batched queries keep the plural form in input. |
| 272 | + const secondDelta = events.filter(e => e.name === "content_block_delta")[1]; |
| 273 | + expect(secondDelta.data.delta.partial_json).toBe(JSON.stringify({ queries: ["a", "b"] })); |
| 274 | + const msgDelta = events.find(e => e.name === "message_delta")!; |
| 275 | + expect(msgDelta.data.usage.server_tool_use).toEqual({ web_search_requests: 2 }); |
| 276 | + }); |
| 277 | + |
| 278 | + test("T3 failed search -> error-shaped content, NOT counted in usage", async () => { |
| 279 | + const upstream = [ |
| 280 | + sse("response.created", { response: {} }), |
| 281 | + sse("response.output_item.done", { output_index: 0, item: wsItem({ status: "failed", sources: undefined }) }), |
| 282 | + sse("response.completed", { response: { status: "completed", usage: {} } }), |
| 283 | + ].join(""); |
| 284 | + const events = await collectEvents(responsesSseToAnthropicSse(streamFrom(upstream), "m")); |
| 285 | + const result = events.filter(e => e.name === "content_block_start")[1].data.content_block; |
| 286 | + expect(result.content).toEqual({ type: "web_search_tool_result_error", error_code: "unavailable" }); |
| 287 | + const msgDelta = events.find(e => e.name === "message_delta")!; |
| 288 | + expect(msgDelta.data.usage.server_tool_use).toBeUndefined(); |
| 289 | + }); |
| 290 | + |
| 291 | + test("T4 no sources -> empty hits array still emits the pair (search count registers)", async () => { |
| 292 | + const upstream = [ |
| 293 | + sse("response.created", { response: {} }), |
| 294 | + sse("response.output_item.done", { output_index: 0, item: wsItem({ sources: undefined }) }), |
| 295 | + sse("response.completed", { response: { status: "completed", usage: {} } }), |
| 296 | + ].join(""); |
| 297 | + const events = await collectEvents(responsesSseToAnthropicSse(streamFrom(upstream), "m")); |
| 298 | + const result = events.filter(e => e.name === "content_block_start")[1].data.content_block; |
| 299 | + expect(result.content).toEqual([]); |
| 300 | + expect(events.find(e => e.name === "message_delta")!.data.usage.server_tool_use).toEqual({ web_search_requests: 1 }); |
| 301 | + }); |
| 302 | + |
| 303 | + test("T5 JSON path: pair emitted, stop_reason preserved, failed not counted", () => { |
| 304 | + const msg = responsesJsonToAnthropicMessage({ |
| 305 | + status: "completed", |
| 306 | + output: [ |
| 307 | + wsItem(), |
| 308 | + wsItem({ id: "ws_9", status: "failed" }), |
| 309 | + { type: "message", id: "m", role: "assistant", content: [{ type: "output_text", text: "done" }] }, |
| 310 | + ], |
| 311 | + usage: { input_tokens: 5, output_tokens: 1 }, |
| 312 | + }, "claude-ocx-x") as Record<string, any>; |
| 313 | + expect(msg.stop_reason).toBe("end_turn"); |
| 314 | + expect(msg.content[0]).toEqual({ type: "server_tool_use", id: "ws_1", name: "web_search", input: { query: "latest bun release" } }); |
| 315 | + expect(msg.content[1]).toMatchObject({ type: "web_search_tool_result", tool_use_id: "ws_1" }); |
| 316 | + expect(msg.content[1].content).toHaveLength(2); |
| 317 | + expect(msg.content.map((c: Record<string, unknown>) => c.type)).toEqual([ |
| 318 | + "server_tool_use", "web_search_tool_result", "server_tool_use", "web_search_tool_result", "text", |
| 319 | + ]); |
| 320 | + expect(msg.content[3]).toMatchObject({ type: "web_search_tool_result", tool_use_id: "ws_9" }); |
| 321 | + expect(msg.content[3].content).toEqual({ type: "web_search_tool_result_error", error_code: "unavailable" }); |
| 322 | + expect(msg.usage.server_tool_use).toEqual({ web_search_requests: 1 }); |
| 323 | + }); |
| 324 | + |
| 325 | + test("T6 regression: turn without web_search emits no server_tool_use and no usage field", async () => { |
| 326 | + const upstream = [ |
| 327 | + sse("response.created", { response: {} }), |
| 328 | + sse("response.output_item.added", { output_index: 0, item: { type: "message", id: "m1" } }), |
| 329 | + sse("response.output_text.delta", { item_id: "m1", output_index: 0, content_index: 0, delta: "plain" }), |
| 330 | + sse("response.completed", { response: { status: "completed", usage: { input_tokens: 2, output_tokens: 1 } } }), |
| 331 | + ].join(""); |
| 332 | + const events = await collectEvents(responsesSseToAnthropicSse(streamFrom(upstream), "m")); |
| 333 | + expect(events.some(e => e.name === "content_block_start" && e.data.content_block.type === "server_tool_use")).toBe(false); |
| 334 | + expect(events.find(e => e.name === "message_delta")!.data.usage.server_tool_use).toBeUndefined(); |
| 335 | + }); |
| 336 | + |
| 337 | + test("T7 collect path: server_tool_use input survives aggregation, usage passthrough", async () => { |
| 338 | + const upstream = [ |
| 339 | + sse("response.created", { response: {} }), |
| 340 | + sse("response.output_item.done", { output_index: 0, item: wsItem() }), |
| 341 | + sse("response.output_text.delta", { item_id: "m1", output_index: 1, content_index: 0, delta: "hi" }), |
| 342 | + sse("response.completed", { response: { status: "completed", usage: { input_tokens: 4, output_tokens: 2 } } }), |
| 343 | + ].join(""); |
| 344 | + const anthropicSse = responsesSseToAnthropicSse(streamFrom(upstream), "m"); |
| 345 | + const msg = await collectAnthropicMessage(anthropicSse, "m") as Record<string, any>; |
| 346 | + expect(msg.content.map((c: Record<string, unknown>) => c.type)).toEqual(["server_tool_use", "web_search_tool_result", "text"]); |
| 347 | + expect(msg.content[0].input).toEqual({ query: "latest bun release" }); |
| 348 | + expect(msg.content[1].tool_use_id).toBe("ws_1"); |
| 349 | + expect(msg.usage.server_tool_use).toEqual({ web_search_requests: 1 }); |
| 350 | + }); |
| 351 | +}); |
0 commit comments