Files
open-design/e2e/tests/structured-streams.test.ts
TomandGitHub 5a0f954297 fix(daemon): emit tool_use from tool_execution_start in pi-rpc (#186)
* fix(daemon): emit tool_use from tool_execution_start in pi-rpc

The pi-rpc adapter emitted tool_use from message_end, which fires
before tool execution starts. The web UI pairs tool results to prior
tool_use events, so receiving tool_result without a preceding tool_use
broke tool card rendering and file auto-open behavior.

Move tool_use emission to tool_execution_start, matching the pattern
in copilot-stream.ts. Remove redundant tool call extraction from
message_end (tool_use is now emitted at execution time, usage is
already emitted from turn_end).

Extract mapPiRpcEvent as a pure exported function so tests exercise
the real event mapping logic instead of an inlined copy that can
diverge from production.

Ref: mrcfps review comments on PR #117

* docs(daemon): clarify mapPiRpcEvent mutability contract

The function mutates ctx.sentFirstToken to track streaming state.
Calling it "pure" is misleading; revised the doc comment to say
no I/O or child process interaction instead.

* fix(pi-rpc): remove redundant status(tool) emission from tool_execution_start

Now that tool_use fires inline from tool_execution_start, the
accompanying status(tool) event is redundant: tool_use already
carries the tool name, and the UI renders running state from the
tool card. The extra status pill breaks consecutive tool_use
grouping in AssistantMessage.buildBlocks. Aligns with
copilot-stream, which emits only tool_use from
tool.execution_start with no status event.
2026-05-02 16:06:37 +08:00

93 lines
2.8 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { createClaudeStreamHandler } from '../../apps/daemon/src/claude-stream.js';
import { createCopilotStreamHandler } from '../../apps/daemon/src/copilot-stream.js';
import { mapPiRpcEvent } from '../../apps/daemon/src/pi-rpc.js';
describe('structured agent stream fixtures', () => {
it('emits TodoWrite tool_use from Claude Code stream JSON', () => {
const events: unknown[] = [];
const handler = createClaudeStreamHandler((event: unknown) => events.push(event));
handler.feed(`${JSON.stringify({
type: 'assistant',
message: {
id: 'msg-1',
content: [
{
type: 'tool_use',
id: 'toolu-1',
name: 'TodoWrite',
input: {
todos: [{ content: 'Run QA', status: 'pending' }],
},
},
],
},
})}\n`);
handler.flush();
expect(events).toContainEqual({
type: 'tool_use',
id: 'toolu-1',
name: 'TodoWrite',
input: {
todos: [{ content: 'Run QA', status: 'pending' }],
},
});
});
it('emits TodoWrite tool_use from Pi RPC tool_execution events', () => {
const events: unknown[] = [];
const send = (_channel: string, payload: unknown) => { events.push(payload); };
const ctx = { runStartedAt: Date.now(), sentFirstToken: { value: false } };
mapPiRpcEvent(
{ type: 'tool_execution_start', toolCallId: 'pi-call-1', toolName: 'TodoWrite', args: { todos: [{ content: 'Run QA', status: 'pending' }] } },
send,
ctx,
);
mapPiRpcEvent(
{ type: 'tool_execution_end', toolCallId: 'pi-call-1', toolName: 'TodoWrite', result: { content: [{ type: 'text', text: 'written' }] }, isError: false },
send,
ctx,
);
expect(events).toContainEqual({
type: 'tool_use',
id: 'pi-call-1',
name: 'TodoWrite',
input: { todos: [{ content: 'Run QA', status: 'pending' }] },
});
expect(events).toContainEqual({
type: 'tool_result',
toolUseId: 'pi-call-1',
content: 'written',
isError: false,
});
});
it('emits TodoWrite tool_use from GitHub Copilot CLI JSON stream', () => {
const events: unknown[] = [];
const handler = createCopilotStreamHandler((event: unknown) => events.push(event));
handler.feed(`${JSON.stringify({
type: 'tool.execution_start',
data: {
toolCallId: 'call-1',
toolName: 'TodoWrite',
arguments: {
todos: [{ content: 'Run QA', status: 'pending' }],
},
},
})}\n`);
handler.flush();
expect(events).toContainEqual({
type: 'tool_use',
id: 'call-1',
name: 'TodoWrite',
input: {
todos: [{ content: 'Run QA', status: 'pending' }],
},
});
});
});