Previously, when block streaming was disabled (the default), text generated between tool calls would only appear after all tools completed. This was because onBlockReply wasn't passed to the subscription when block streaming was off, so flushBlockReplyBuffer() before tool execution did nothing. Now onBlockReply is always passed, and when block streaming is disabled, block replies are sent directly during tool flush. Directly sent payloads are tracked to avoid duplicates in final payloads. Also fixes a race condition where tool summaries could be emitted before the typing indicator started by awaiting onAgentEvent in tool handlers.
110 lines
3.1 KiB
TypeScript
110 lines
3.1 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { subscribeEmbeddedPiSession } from "./pi-embedded-subscribe.js";
|
|
|
|
type StubSession = {
|
|
subscribe: (fn: (evt: unknown) => void) => () => void;
|
|
};
|
|
|
|
describe("subscribeEmbeddedPiSession", () => {
|
|
const _THINKING_TAG_CASES = [
|
|
{ tag: "think", open: "<think>", close: "</think>" },
|
|
{ tag: "thinking", open: "<thinking>", close: "</thinking>" },
|
|
{ tag: "thought", open: "<thought>", close: "</thought>" },
|
|
{ tag: "antthinking", open: "<antthinking>", close: "</antthinking>" },
|
|
] as const;
|
|
|
|
it("includes canvas action metadata in tool summaries", async () => {
|
|
let handler: ((evt: unknown) => void) | undefined;
|
|
const session: StubSession = {
|
|
subscribe: (fn) => {
|
|
handler = fn;
|
|
return () => {};
|
|
},
|
|
};
|
|
|
|
const onToolResult = vi.fn();
|
|
|
|
subscribeEmbeddedPiSession({
|
|
session: session as unknown as Parameters<typeof subscribeEmbeddedPiSession>[0]["session"],
|
|
runId: "run-canvas-tool",
|
|
verboseLevel: "on",
|
|
onToolResult,
|
|
});
|
|
|
|
handler?.({
|
|
type: "tool_execution_start",
|
|
toolName: "canvas",
|
|
toolCallId: "tool-canvas-1",
|
|
args: { action: "a2ui_push", jsonlPath: "/tmp/a2ui.jsonl" },
|
|
});
|
|
|
|
// Wait for async handler to complete
|
|
await Promise.resolve();
|
|
|
|
expect(onToolResult).toHaveBeenCalledTimes(1);
|
|
const payload = onToolResult.mock.calls[0][0];
|
|
expect(payload.text).toContain("🖼️");
|
|
expect(payload.text).toContain("canvas");
|
|
expect(payload.text).toContain("A2UI push");
|
|
expect(payload.text).toContain("/tmp/a2ui.jsonl");
|
|
});
|
|
it("skips tool summaries when shouldEmitToolResult is false", () => {
|
|
let handler: ((evt: unknown) => void) | undefined;
|
|
const session: StubSession = {
|
|
subscribe: (fn) => {
|
|
handler = fn;
|
|
return () => {};
|
|
},
|
|
};
|
|
|
|
const onToolResult = vi.fn();
|
|
|
|
subscribeEmbeddedPiSession({
|
|
session: session as unknown as Parameters<typeof subscribeEmbeddedPiSession>[0]["session"],
|
|
runId: "run-tool-off",
|
|
shouldEmitToolResult: () => false,
|
|
onToolResult,
|
|
});
|
|
|
|
handler?.({
|
|
type: "tool_execution_start",
|
|
toolName: "read",
|
|
toolCallId: "tool-2",
|
|
args: { path: "/tmp/b.txt" },
|
|
});
|
|
|
|
expect(onToolResult).not.toHaveBeenCalled();
|
|
});
|
|
it("emits tool summaries when shouldEmitToolResult overrides verbose", async () => {
|
|
let handler: ((evt: unknown) => void) | undefined;
|
|
const session: StubSession = {
|
|
subscribe: (fn) => {
|
|
handler = fn;
|
|
return () => {};
|
|
},
|
|
};
|
|
|
|
const onToolResult = vi.fn();
|
|
|
|
subscribeEmbeddedPiSession({
|
|
session: session as unknown as Parameters<typeof subscribeEmbeddedPiSession>[0]["session"],
|
|
runId: "run-tool-override",
|
|
verboseLevel: "off",
|
|
shouldEmitToolResult: () => true,
|
|
onToolResult,
|
|
});
|
|
|
|
handler?.({
|
|
type: "tool_execution_start",
|
|
toolName: "read",
|
|
toolCallId: "tool-3",
|
|
args: { path: "/tmp/c.txt" },
|
|
});
|
|
|
|
// Wait for async handler to complete
|
|
await Promise.resolve();
|
|
|
|
expect(onToolResult).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|