|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { parseRequest } from "../src/responses/parser"; |
| 3 | + |
| 4 | +describe("Responses parser agent_message boundaries", () => { |
| 5 | + test("keeps agent_message between separate assistant reasoning turns", () => { |
| 6 | + const parsed = parseRequest({ |
| 7 | + model: "claude-fable-5", |
| 8 | + stream: true, |
| 9 | + input: [ |
| 10 | + { |
| 11 | + type: "reasoning", |
| 12 | + id: "rs_before_agent", |
| 13 | + summary: [ |
| 14 | + { |
| 15 | + type: "summary_text", |
| 16 | + text: "reasoning before the sub-agent response", |
| 17 | + }, |
| 18 | + ], |
| 19 | + encrypted_content: "opaque-signature-before", |
| 20 | + }, |
| 21 | + { |
| 22 | + type: "agent_message", |
| 23 | + author: "probe_all", |
| 24 | + recipient: "root", |
| 25 | + content: [ |
| 26 | + { |
| 27 | + type: "input_text", |
| 28 | + text: "sub-agent result", |
| 29 | + }, |
| 30 | + ], |
| 31 | + }, |
| 32 | + { |
| 33 | + type: "reasoning", |
| 34 | + id: "rs_after_agent", |
| 35 | + summary: [ |
| 36 | + { |
| 37 | + type: "summary_text", |
| 38 | + text: "reasoning after the sub-agent response", |
| 39 | + }, |
| 40 | + ], |
| 41 | + encrypted_content: "opaque-signature-after", |
| 42 | + }, |
| 43 | + { |
| 44 | + type: "function_call", |
| 45 | + id: "fc_after_agent", |
| 46 | + call_id: "call_after_agent", |
| 47 | + name: "shell_command", |
| 48 | + arguments: JSON.stringify({ command: "echo ok" }), |
| 49 | + }, |
| 50 | + { |
| 51 | + type: "function_call_output", |
| 52 | + call_id: "call_after_agent", |
| 53 | + output: "ok", |
| 54 | + }, |
| 55 | + ], |
| 56 | + }); |
| 57 | + |
| 58 | + const messages = parsed.context.messages; |
| 59 | + |
| 60 | + expect(messages.map((message) => message.role)).toEqual([ |
| 61 | + "assistant", |
| 62 | + "user", |
| 63 | + "assistant", |
| 64 | + "toolResult", |
| 65 | + ]); |
| 66 | + |
| 67 | + const firstAssistant = messages[0]; |
| 68 | + if (!firstAssistant || firstAssistant.role !== "assistant") { |
| 69 | + throw new Error("expected the first parsed message to be assistant"); |
| 70 | + } |
| 71 | + |
| 72 | + expect(firstAssistant.content).toHaveLength(1); |
| 73 | + expect(firstAssistant.content[0]).toMatchObject({ |
| 74 | + type: "thinking", |
| 75 | + thinking: "reasoning before the sub-agent response", |
| 76 | + itemId: "rs_before_agent", |
| 77 | + }); |
| 78 | + |
| 79 | + expect(messages[1]).toMatchObject({ |
| 80 | + role: "user", |
| 81 | + content: "sub-agent result", |
| 82 | + }); |
| 83 | + |
| 84 | + const secondAssistant = messages[2]; |
| 85 | + if (!secondAssistant || secondAssistant.role !== "assistant") { |
| 86 | + throw new Error("expected the third parsed message to be assistant"); |
| 87 | + } |
| 88 | + |
| 89 | + expect(secondAssistant.content).toHaveLength(2); |
| 90 | + expect(secondAssistant.content[0]).toMatchObject({ |
| 91 | + type: "thinking", |
| 92 | + thinking: "reasoning after the sub-agent response", |
| 93 | + itemId: "rs_after_agent", |
| 94 | + }); |
| 95 | + expect(secondAssistant.content[1]).toMatchObject({ |
| 96 | + type: "toolCall", |
| 97 | + id: "call_after_agent", |
| 98 | + name: "shell_command", |
| 99 | + arguments: { command: "echo ok" }, |
| 100 | + thoughtSignature: "fc_after_agent", |
| 101 | + }); |
| 102 | + |
| 103 | + expect(messages[3]).toMatchObject({ |
| 104 | + role: "toolResult", |
| 105 | + toolCallId: "call_after_agent", |
| 106 | + toolName: "shell_command", |
| 107 | + content: "ok", |
| 108 | + isError: false, |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments