Merge branch 'investigate/ui-network-responsive' into main

This commit is contained in:
Mohamed Boudra
2026-02-09 12:25:57 +07:00
29 changed files with 442 additions and 297 deletions
@@ -221,8 +221,10 @@ describe("DaemonClient", () => {
callId: "call_cli_stream",
name: "shell",
status: "running",
input: { command: "pwd" },
output: null,
detail: {
type: "shell",
command: "pwd",
},
error: null,
},
},
@@ -241,7 +243,9 @@ describe("DaemonClient", () => {
type: "tool_call";
status: string;
error: unknown;
output: unknown;
detail: {
type: string;
};
};
};
};
@@ -249,7 +253,7 @@ describe("DaemonClient", () => {
expect(streamMsg.payload.event.item.status).toBe("running");
expect(streamMsg.payload.event.item.error).toBeNull();
expect(streamMsg.payload.event.item.output).toBeNull();
expect(streamMsg.payload.event.item.detail.type).toBe("shell");
expect(logger.warn).not.toHaveBeenCalled();
});
@@ -288,7 +292,11 @@ describe("DaemonClient", () => {
callId: "call_cli_stream_legacy",
name: "shell",
status: "inProgress",
input: { command: "pwd" },
detail: {
type: "unknown",
rawInput: { command: "pwd" },
rawOutput: null,
},
},
},
},
@@ -338,8 +346,10 @@ describe("DaemonClient", () => {
callId: "call_cli_snapshot",
name: "shell",
status: "running",
input: { command: "pwd" },
output: null,
detail: {
type: "shell",
command: "pwd",
},
error: null,
},
},
@@ -361,7 +371,9 @@ describe("DaemonClient", () => {
type: "tool_call";
status: string;
error: unknown;
output: unknown;
detail: {
type: string;
};
};
};
}>;
@@ -373,7 +385,7 @@ describe("DaemonClient", () => {
if (firstTimeline?.type === "timeline" && firstTimeline.item.type === "tool_call") {
expect(firstTimeline.item.status).toBe("running");
expect(firstTimeline.item.error).toBeNull();
expect(firstTimeline.item.output).toBeNull();
expect(firstTimeline.item.detail.type).toBe("shell");
}
expect(logger.warn).not.toHaveBeenCalled();
});
@@ -10,19 +10,23 @@ function toolCallItem(params: {
output?: unknown | null;
error?: unknown;
metadata?: Record<string, unknown>;
detail?: Extract<AgentTimelineItem, { type: "tool_call" }>['detail'];
detail?: Extract<AgentTimelineItem, { type: "tool_call" }>["detail"];
}): Extract<AgentTimelineItem, { type: "tool_call" }> {
const status = params.status ?? "completed";
const detail =
params.detail ?? {
type: "unknown" as const,
rawInput: params.input ?? null,
rawOutput: params.output ?? null,
};
return {
type: "tool_call",
callId: params.callId,
name: params.name,
status,
input: params.input ?? null,
output: params.output ?? null,
detail,
error: status === "failed" ? params.error ?? { message: "failed" } : null,
metadata: params.metadata,
detail: params.detail,
};
}
@@ -1,4 +1,4 @@
import type { AgentTimelineItem } from "./agent-sdk-types.js";
import type { AgentTimelineItem, ToolCallDetail } from "./agent-sdk-types.js";
import { isLikelyExternalToolName } from "./tool-name-normalization.js";
import { buildToolCallDisplayModel } from "../../shared/tool-call-display.js";
@@ -45,6 +45,46 @@ function formatToolInputJson(input: unknown): string | null {
}
}
function hasNonEmptyObject(value: unknown): boolean {
return typeof value === "object" && value !== null && !Array.isArray(value) && Object.keys(value).length > 0;
}
function mergeUnknownValue(existing: unknown | null, incoming: unknown | null): unknown | null {
if (incoming === null) {
return existing;
}
if (!hasNonEmptyObject(incoming) && hasNonEmptyObject(existing)) {
return existing;
}
return incoming;
}
function mergeToolDetail(existing: ToolCallDetail, incoming: ToolCallDetail): ToolCallDetail {
if (existing.type === "unknown" && incoming.type !== "unknown") {
return incoming;
}
if (incoming.type === "unknown" && existing.type !== "unknown") {
return existing;
}
if (existing.type === "unknown" && incoming.type === "unknown") {
return {
type: "unknown",
rawInput: mergeUnknownValue(existing.rawInput, incoming.rawInput),
rawOutput: mergeUnknownValue(existing.rawOutput, incoming.rawOutput),
};
}
if (existing.type === incoming.type) {
return { ...existing, ...incoming };
}
return incoming;
}
function rawInputFromDetail(detail: ToolCallDetail): unknown {
return detail.type === "unknown" ? detail.rawInput : null;
}
/**
* Collapse timeline items:
* - Dedupe tool calls by callId (pending/completed -> single)
@@ -95,9 +135,7 @@ function collapseTimeline(items: AgentTimelineItem[]): AgentTimelineItem[] {
toolCallMap.set(item.callId, {
...existing,
...item,
input: item.input ?? existing.input,
output: item.output ?? existing.output,
detail: item.detail ?? existing.detail,
detail: mergeToolDetail(existing.detail, item.detail),
error: item.error,
metadata: item.metadata,
});
@@ -105,9 +143,7 @@ function collapseTimeline(items: AgentTimelineItem[]): AgentTimelineItem[] {
toolCallMap.set(item.callId, {
...existing,
...item,
input: item.input ?? existing.input,
output: item.output ?? existing.output,
detail: item.detail ?? existing.detail,
detail: mergeToolDetail(existing.detail, item.detail),
error: null,
metadata: item.metadata,
});
@@ -167,12 +203,10 @@ export function curateAgentActivity(
break;
case "tool_call": {
flushBuffers(lines, buffers);
const inputJson = formatToolInputJson(item.input);
const inputJson = formatToolInputJson(rawInputFromDetail(item.detail));
const display = buildToolCallDisplayModel({
name: item.name,
status: item.status,
input: item.input,
output: item.output,
error: item.error,
detail: item.detail,
metadata: item.metadata,
@@ -133,15 +133,18 @@ export type ToolCallDetail =
| {
type: "search";
query: string;
}
| {
type: "unknown";
rawInput: unknown | null;
rawOutput: unknown | null;
};
type ToolCallBase = {
type: "tool_call";
callId: string;
name: string;
input: unknown | null;
output: unknown | null;
detail?: ToolCallDetail;
detail: ToolCallDetail;
metadata?: Record<string, unknown>;
};
@@ -194,7 +197,6 @@ export type AgentStreamEvent =
| { type: "turn_failed"; provider: AgentProvider; error: string }
| { type: "turn_canceled"; provider: AgentProvider; reason: string }
| { type: "timeline"; item: AgentTimelineItem; provider: AgentProvider }
| { type: "provider_event"; provider: AgentProvider; raw: unknown }
| { type: "permission_requested"; provider: AgentProvider; request: AgentPermissionRequest }
| {
type: "permission_resolved";
@@ -89,8 +89,21 @@ function extractCommandText(input: unknown): string | null {
return null;
}
function extractToolCommand(detail: unknown): string | null {
if (!isKeyValueObject(detail) || typeof detail.type !== "string") {
return null;
}
if (detail.type === "shell" && typeof detail.command === "string") {
return detail.command;
}
if (detail.type === "unknown") {
return extractCommandText(detail.rawInput);
}
return null;
}
function isSleepCommandToolCall(item: ToolCallItem): boolean {
const inputCommand = extractCommandText(item.input)?.toLowerCase() ?? "";
const inputCommand = extractToolCommand(item.detail)?.toLowerCase() ?? "";
return inputCommand.includes("sleep 60");
}
@@ -98,7 +111,7 @@ function isPermissionCommandToolCall(item: ToolCallItem): boolean {
if (item.name === "permission_request") {
return false;
}
const inputCommand = extractCommandText(item.input)?.toLowerCase() ?? "";
const inputCommand = extractToolCommand(item.detail)?.toLowerCase() ?? "";
return inputCommand.includes("permission.txt");
}
@@ -376,7 +389,7 @@ async function startAgentMcpServer(): Promise<AgentMcpServerHandle> {
event.item.name.toLowerCase().includes("bash") &&
event.item.status === "pending"
) {
pendingCommand = extractCommandText(event.item.input);
pendingCommand = extractToolCommand(event.item.detail);
}
if (event.type === "turn_completed" || event.type === "turn_failed") {
break;
@@ -432,28 +445,20 @@ async function startAgentMcpServer(): Promise<AgentMcpServerHandle> {
item.name !== "permission_request"
);
const fileChangeEvent = toolCalls.find((item) => {
// Check for file changes in structured output.files array
if (isKeyValueObject(item.output)) {
const files = item.output.files;
if (Array.isArray(files) && files.some((file) => typeof file?.path === "string" && file.path.includes("tool-test.txt"))) {
return true;
}
if (item.detail.type === "write" || item.detail.type === "edit") {
return item.detail.filePath.includes("tool-test.txt");
}
// Also check for file path in output structure (write/edit tools)
if (isKeyValueObject(item.output)) {
const output = item.output;
if (output.type === "file_write" || output.type === "file_edit") {
const filePath = output.filePath;
if (typeof filePath === "string" && filePath.includes("tool-test.txt")) {
return true;
}
}
if (item.detail.type === "unknown") {
return (
rawContainsText(item.detail.rawInput, "tool-test.txt") ||
rawContainsText(item.detail.rawOutput, "tool-test.txt")
);
}
return false;
return rawContainsText(item.detail, "tool-test.txt");
});
const sawPwdCommand = commandEvents.some(
(item) => (extractCommandText(item.input) ?? "").toLowerCase().includes("pwd") && item.status === "completed"
(item) => (extractToolCommand(item.detail) ?? "").toLowerCase().includes("pwd") && item.status === "completed"
);
expect(completed).toBe(true);
@@ -950,13 +955,13 @@ async function startAgentMcpServer(): Promise<AgentMcpServerHandle> {
const liveSnapshots = extractAgentToolSnapshots(liveState);
const commandTool = liveSnapshots.find((snapshot) =>
snapshot.data.name.toLowerCase().includes("bash") &&
(extractCommandText(snapshot.data.input) ?? "").toLowerCase().includes("pwd")
(extractToolCommand(snapshot.data.detail) ?? "").toLowerCase().includes("pwd")
);
const editTool = liveSnapshots.find((snapshot) =>
rawContainsText(snapshot.data.result, "hydrate-proof.txt")
rawContainsText(snapshot.data.detail, "hydrate-proof.txt")
);
const readTool = liveSnapshots.find((snapshot) =>
rawContainsText(snapshot.data.result, "HYDRATION_PROOF_LINE_TWO")
rawContainsText(snapshot.data.detail, "HYDRATION_PROOF_LINE_TWO")
);
expect(commandTool).toBeTruthy();
@@ -993,23 +998,22 @@ async function startAgentMcpServer(): Promise<AgentMcpServerHandle> {
commandTool!,
hydratedMap,
(data) =>
rawContainsText(data.result, cwd),
rawContainsText(data.detail, cwd),
({ live, hydrated }) => {
expect(rawContainsText(live.result, cwd)).toBe(true);
expect(rawContainsText(hydrated.result, cwd)).toBe(true);
expect((extractCommandText(live.input) ?? "").toLowerCase()).toContain("pwd");
expect((extractCommandText(hydrated.input) ?? "").toLowerCase()).toContain("pwd");
expect(rawContainsText(live.detail, cwd)).toBe(true);
expect(rawContainsText(hydrated.detail, cwd)).toBe(true);
expect((extractToolCommand(live.detail) ?? "").toLowerCase()).toContain("pwd");
expect((extractToolCommand(hydrated.detail) ?? "").toLowerCase()).toContain("pwd");
}
);
assertHydratedReplica(
editTool!,
hydratedMap,
(data) =>
isFileWriteResult(data.result) &&
data.result.filePath.includes("hydrate-proof.txt"),
rawContainsText(data.detail, "hydrate-proof.txt"),
({ live, hydrated }) => {
const liveDiff = JSON.stringify(live.result ?? {});
const hydratedDiff = JSON.stringify(hydrated.result ?? {});
const liveDiff = JSON.stringify(live.detail ?? {});
const hydratedDiff = JSON.stringify(hydrated.detail ?? {});
expect(liveDiff).toContain("hydrate-proof.txt");
expect(hydratedDiff).toContain("hydrate-proof.txt");
}
@@ -1018,11 +1022,11 @@ async function startAgentMcpServer(): Promise<AgentMcpServerHandle> {
readTool!,
hydratedMap,
(data) =>
rawContainsText(data.result, "HYDRATION_PROOF_LINE_ONE") &&
rawContainsText(data.result, "HYDRATION_PROOF_LINE_TWO"),
rawContainsText(data.detail, "HYDRATION_PROOF_LINE_ONE") &&
rawContainsText(data.detail, "HYDRATION_PROOF_LINE_TWO"),
({ live, hydrated }) => {
const liveReads = JSON.stringify(live.result ?? {});
const hydratedReads = JSON.stringify(hydrated.result ?? {});
const liveReads = JSON.stringify(live.detail ?? {});
const hydratedReads = JSON.stringify(hydrated.detail ?? {});
expect(liveReads).toContain("HYDRATION_PROOF_LINE_ONE");
expect(hydratedReads).toContain("HYDRATION_PROOF_LINE_ONE");
expect(liveReads).toContain("HYDRATION_PROOF_LINE_TWO");
@@ -19,8 +19,6 @@ describe("claude tool-call mapper", () => {
expect(item.status).toBe("running");
expect(item.error).toBeNull();
expect(item.callId).toBe("claude-call-1");
expect(item.input).toEqual({ command: "pwd", cwd: "/tmp/repo" });
expect(item.output).toBeNull();
expect(item.detail?.type).toBe("shell");
if (item.detail?.type === "shell") {
expect(item.detail.command).toBe("pwd");
@@ -85,8 +83,6 @@ describe("claude tool-call mapper", () => {
expect(item.status).toBe("completed");
expect(item.error).toBeNull();
expect(item.callId).toBe("claude-call-2");
expect(item.input).toEqual({ file_path: "README.md" });
expect(item.output).toEqual({ content: "hello" });
expect(item.detail?.type).toBe("read");
if (item.detail?.type === "read") {
expect(item.detail.filePath).toBe("README.md");
@@ -141,8 +137,6 @@ describe("claude tool-call mapper", () => {
expect(item.status).toBe("failed");
expect(item.error).toEqual({ message: "Command failed" });
expect(item.callId).toBe("claude-call-3");
expect(item.input).toEqual({ command: "false" });
expect(item.output).toBeNull();
});
it("maps write/edit/search known shapes with distinct detail types", () => {
@@ -181,7 +175,7 @@ describe("claude tool-call mapper", () => {
});
});
it("keeps unknown tools canonical without detail", () => {
it("maps unknown tools to unknown detail with raw payloads", () => {
const item = mapClaudeCompletedToolCall({
callId: "claude-call-4",
name: "my_custom_tool",
@@ -191,8 +185,10 @@ describe("claude tool-call mapper", () => {
expect(item.status).toBe("completed");
expect(item.error).toBeNull();
expect(item.detail).toBeUndefined();
expect(item.input).toEqual({ foo: "bar" });
expect(item.output).toEqual({ ok: true });
expect(item.detail).toEqual({
type: "unknown",
rawInput: { foo: "bar" },
rawOutput: { ok: true },
});
});
});
@@ -49,20 +49,33 @@ function useTempCodexSessionDir(): () => void {
function hasShellCommand(item: AgentTimelineItem, commandFragment: string): boolean {
if (item.type !== "tool_call" || item.name !== "shell") return false;
const input = item.input as { command?: string } | undefined;
const command = input?.command ?? "";
if (item.detail.type === "shell") {
return item.detail.command.includes(commandFragment);
}
const unknownInput =
item.detail.type === "unknown" && typeof item.detail.rawInput === "object" && item.detail.rawInput
? (item.detail.rawInput as { command?: string })
: undefined;
const command = unknownInput?.command ?? "";
return command.includes(commandFragment);
}
function hasApplyPatchFile(item: AgentTimelineItem, fileName: string): boolean {
if (item.type !== "tool_call" || item.name !== "apply_patch") return false;
const input = item.input as { files?: Array<{ path?: string }> } | undefined;
const output = item.output as
| { files?: Array<{ path?: string; patch?: string }>; diff?: string }
| undefined;
const inInput = (input?.files ?? []).some((file) => file?.path === fileName);
const inOutput = (output?.files ?? []).some((file) => file?.path === fileName);
const inDiff = typeof output?.diff === "string" && output.diff.includes(fileName);
if (item.detail.type === "edit") {
return item.detail.filePath === fileName || (item.detail.unifiedDiff?.includes(fileName) ?? false);
}
const unknownInput =
item.detail.type === "unknown" && typeof item.detail.rawInput === "object" && item.detail.rawInput
? (item.detail.rawInput as { files?: Array<{ path?: string }> })
: undefined;
const unknownOutput =
item.detail.type === "unknown" && typeof item.detail.rawOutput === "object" && item.detail.rawOutput
? (item.detail.rawOutput as { files?: Array<{ path?: string; patch?: string }>; diff?: string })
: undefined;
const inInput = (unknownInput?.files ?? []).some((file) => file?.path === fileName);
const inOutput = (unknownOutput?.files ?? []).some((file) => file?.path === fileName);
const inDiff = typeof unknownOutput?.diff === "string" && unknownOutput.diff.includes(fileName);
return inInput || inOutput || inDiff;
}
@@ -1671,7 +1671,6 @@ class CodexAppServerAgentSession implements AgentSession {
}
private handleNotification(method: string, params: unknown): void {
this.emitEvent({ type: "provider_event", provider: CODEX_PROVIDER, raw: { method, params } });
const parsed = CodexNotificationSchema.parse({ method, params });
if (parsed.kind === "thread_started") {
@@ -20,7 +20,11 @@ describe("codex tool-call mapper", () => {
expect(item?.error).toBeNull();
expect(item?.callId).toBe("codex-call-1");
expect(item?.name).toBe("shell");
expect(item?.input).toEqual({ command: "pwd", cwd: "/tmp/repo" });
expect(item?.detail).toEqual({
type: "shell",
command: "pwd",
cwd: "/tmp/repo",
});
});
it("maps running known tool variants with detail for early summaries", () => {
@@ -133,7 +137,7 @@ describe("codex tool-call mapper", () => {
}
});
it("truncates large diff payloads deterministically in canonical output", () => {
it("truncates large diff payloads deterministically in canonical detail", () => {
const hugeDiff = `@@\\n-${"a".repeat(14_000)}\\n+${"b".repeat(14_000)}\\n`;
const item = mapCodexToolCallFromThreadItem(
{
@@ -147,11 +151,12 @@ describe("codex tool-call mapper", () => {
expect(item).toBeTruthy();
expect(item?.status).toBe("completed");
expect(item?.output).toBeTruthy();
const output = item?.output as { files?: Array<{ patch?: string }> };
expect(output.files?.[0]?.patch).toBeDefined();
expect(output.files?.[0]?.patch?.includes("...[truncated ")).toBe(true);
expect((output.files?.[0]?.patch?.length ?? 0) < hugeDiff.length).toBe(true);
expect(item?.detail?.type).toBe("edit");
if (item?.detail?.type === "edit") {
expect(item.detail.unifiedDiff).toBeDefined();
expect(item.detail.unifiedDiff?.includes("...[truncated ")).toBe(true);
expect((item.detail.unifiedDiff?.length ?? 0) < hugeDiff.length).toBe(true);
}
});
it("maps write/edit/search known variants with distinct detail types", () => {
@@ -218,7 +223,7 @@ describe("codex tool-call mapper", () => {
expect(item?.callId).toBe("codex-call-3");
});
it("keeps unknown tools canonical without detail", () => {
it("maps unknown tools to unknown detail with raw payloads", () => {
const item = mapCodexRolloutToolCall({
callId: "codex-call-4",
name: "my_custom_tool",
@@ -228,9 +233,11 @@ describe("codex tool-call mapper", () => {
expect(item.status).toBe("completed");
expect(item.error).toBeNull();
expect(item.detail).toBeUndefined();
expect(item.detail).toEqual({
type: "unknown",
rawInput: { foo: "bar" },
rawOutput: { ok: true },
});
expect(item.callId).toBe("codex-call-4");
expect(item.input).toEqual({ foo: "bar" });
expect(item.output).toEqual({ ok: true });
});
});
@@ -181,7 +181,7 @@ describe("opencode tool-call mapper", () => {
});
});
it("keeps unknown tools canonical without detail", () => {
it("maps unknown tools to unknown detail with raw payloads", () => {
const item = mapOpencodeToolCall({
toolName: "my_custom_tool",
callId: "opencode-call-4",
@@ -192,8 +192,10 @@ describe("opencode tool-call mapper", () => {
expect(item.status).toBe("completed");
expect(item.error).toBeNull();
expect(item.detail).toBeUndefined();
expect(item.input).toEqual({ foo: "bar" });
expect(item.output).toEqual({ ok: true });
expect(item.detail).toEqual({
type: "unknown",
rawInput: { foo: "bar" },
rawOutput: { ok: true },
});
});
});
+9 -4
View File
@@ -33,8 +33,10 @@ describe("serializeAgentStreamEvent", () => {
callId: "call_1",
name: "shell",
status: "running",
input: { command: "pwd" },
output: null,
detail: {
type: "shell",
command: "pwd",
},
error: null,
},
};
@@ -45,7 +47,6 @@ describe("serializeAgentStreamEvent", () => {
throw new Error("Expected timeline.tool_call event");
}
expect(serialized.item.status).toBe("running");
expect(serialized.item.output).toBeNull();
expect(serialized.item.error).toBeNull();
});
@@ -58,7 +59,11 @@ describe("serializeAgentStreamEvent", () => {
callId: "call_legacy",
name: "shell",
status: "inProgress",
input: { command: "pwd" },
detail: {
type: "unknown",
rawInput: { command: "pwd" },
rawOutput: null,
},
},
} satisfies unknown;
+37 -28
View File
@@ -1930,11 +1930,14 @@ export class Session {
name: "paseo_worktree_setup",
callId,
status: "running",
input: {
worktreePath: worktree.worktreePath,
branchName: worktree.branchName,
detail: {
type: "unknown",
rawInput: {
worktreePath: worktree.worktreePath,
branchName: worktree.branchName,
},
rawOutput: null,
},
output: null,
error: null,
});
if (!started) {
@@ -1952,18 +1955,21 @@ export class Session {
name: "paseo_worktree_setup",
callId,
status: "completed",
input: {
worktreePath: worktree.worktreePath,
branchName: worktree.branchName,
},
output: {
worktreePath: worktree.worktreePath,
commands: results.map((result) => ({
command: result.command,
cwd: result.cwd,
exitCode: result.exitCode,
output: `${result.stdout ?? ""}${result.stderr ? `\n${result.stderr}` : ""}`.trim(),
})),
detail: {
type: "unknown",
rawInput: {
worktreePath: worktree.worktreePath,
branchName: worktree.branchName,
},
rawOutput: {
worktreePath: worktree.worktreePath,
commands: results.map((result) => ({
command: result.command,
cwd: result.cwd,
exitCode: result.exitCode,
output: `${result.stdout ?? ""}${result.stderr ? `\n${result.stderr}` : ""}`.trim(),
})),
},
},
error: null,
});
@@ -1977,18 +1983,21 @@ export class Session {
name: "paseo_worktree_setup",
callId,
status: "failed",
input: {
worktreePath: worktree.worktreePath,
branchName: worktree.branchName,
},
output: {
worktreePath: worktree.worktreePath,
commands: results.map((result) => ({
command: result.command,
cwd: result.cwd,
exitCode: result.exitCode,
output: `${result.stdout ?? ""}${result.stderr ? `\n${result.stderr}` : ""}`.trim(),
})),
detail: {
type: "unknown",
rawInput: {
worktreePath: worktree.worktreePath,
branchName: worktree.branchName,
},
rawOutput: {
worktreePath: worktree.worktreePath,
commands: results.map((result) => ({
command: result.command,
cwd: result.cwd,
exitCode: result.exitCode,
output: `${result.stdout ?? ""}${result.stderr ? `\n${result.stderr}` : ""}`.trim(),
})),
},
},
error: { message },
});
@@ -246,8 +246,11 @@ class FakeAgentSession implements AgentSession {
name: tool.name,
callId,
status: "running",
input: tool.input ?? null,
output: null,
detail: {
type: "unknown",
rawInput: tool.input ?? null,
rawOutput: null,
},
error: null,
},
};
@@ -327,8 +330,11 @@ class FakeAgentSession implements AgentSession {
name: tool.name,
callId,
status: "completed",
input: tool.input ?? null,
output: toolOutput ?? { ok: true },
detail: {
type: "unknown",
rawInput: tool.input ?? null,
rawOutput: toolOutput ?? { ok: true },
},
error: null,
},
};
@@ -33,13 +33,11 @@ describe("shared messages stream parsing", () => {
callId: "call_live",
name: "shell",
status: "running",
input: { command: "ls" },
output: null,
error: null,
detail: {
type: "shell",
command: "ls",
},
error: null,
},
},
},
@@ -7,8 +7,10 @@ function canonicalBase() {
type: "tool_call" as const,
callId: "call_123",
name: "shell",
input: { command: "pwd" },
output: null,
detail: {
type: "shell" as const,
command: "pwd",
},
};
}
@@ -18,17 +20,12 @@ describe("shared messages tool_call schema", () => {
...canonicalBase(),
status: "running",
error: null,
detail: {
type: "shell",
command: "pwd",
},
});
const completed = AgentTimelineItemPayloadSchema.parse({
...canonicalBase(),
status: "completed",
error: null,
output: { output: "/tmp/repo" },
});
const failed = AgentTimelineItemPayloadSchema.parse({
@@ -54,8 +51,10 @@ describe("shared messages tool_call schema", () => {
type: "tool_call",
name: "shell",
status: "running",
input: { command: "pwd" },
output: null,
detail: {
type: "shell",
command: "pwd",
},
error: null,
});
@@ -82,12 +81,11 @@ describe("shared messages tool_call schema", () => {
error: null,
});
const missingOutput = AgentTimelineItemPayloadSchema.safeParse({
const missingDetail = AgentTimelineItemPayloadSchema.safeParse({
type: "tool_call",
callId: "call_missing_output",
callId: "call_missing_detail",
name: "shell",
status: "running",
input: { command: "pwd" },
error: null,
});
@@ -99,7 +97,7 @@ describe("shared messages tool_call schema", () => {
expect(completedWithError.success).toBe(false);
expect(failedWithoutError.success).toBe(false);
expect(missingOutput.success).toBe(false);
expect(missingDetail.success).toBe(false);
expect(legacyStatus.success).toBe(false);
});
});
+22 -33
View File
@@ -140,14 +140,22 @@ export const AgentPermissionRequestPayloadSchema: z.ZodType<AgentPermissionReque
metadata: z.record(z.unknown()).optional(),
});
// Structured tool result types for better client rendering
// These types define the structure of the `output` field in tool_call timeline items
export type StructuredToolResult =
| { type: "command"; command: string; output: string; exitCode?: number; cwd?: string }
| { type: "file_write"; filePath: string; oldContent: string; newContent: string }
| { type: "file_edit"; filePath: string; diff?: string; oldContent?: string; newContent?: string }
| { type: "file_read"; filePath: string; content: string }
| { type: "generic"; data: unknown };
const UnknownValueSchema = z.union([
z.null(),
z.boolean(),
z.number(),
z.string(),
z.array(z.unknown()),
z.object({}).passthrough(),
]);
const NonNullUnknownSchema = z.union([
z.boolean(),
z.number(),
z.string(),
z.array(z.unknown()),
z.object({}).passthrough(),
]);
const ToolCallDetailPayloadSchema: z.ZodType<ToolCallDetail> = z.discriminatedUnion("type", [
z.object({
@@ -180,32 +188,18 @@ const ToolCallDetailPayloadSchema: z.ZodType<ToolCallDetail> = z.discriminatedUn
type: z.literal("search"),
query: z.string(),
}),
]);
const NonUndefinedUnknownSchema = z.union([
z.null(),
z.boolean(),
z.number(),
z.string(),
z.array(z.unknown()),
z.object({}).passthrough(),
]);
const NonNullUnknownSchema = z.union([
z.boolean(),
z.number(),
z.string(),
z.array(z.unknown()),
z.object({}).passthrough(),
z.object({
type: z.literal("unknown"),
rawInput: UnknownValueSchema,
rawOutput: UnknownValueSchema,
}),
]);
const ToolCallBasePayloadSchema = z.object({
type: z.literal("tool_call"),
callId: z.string(),
name: z.string(),
input: NonUndefinedUnknownSchema,
output: NonUndefinedUnknownSchema,
detail: ToolCallDetailPayloadSchema.optional(),
detail: ToolCallDetailPayloadSchema,
metadata: z.record(z.unknown()).optional(),
});
@@ -280,11 +274,6 @@ export const AgentStreamEventPayloadSchema = z.discriminatedUnion("type", [
sessionId: z.string(),
provider: AgentProviderSchema,
}),
z.object({
type: z.literal("provider_event"),
provider: AgentProviderSchema,
raw: z.unknown(),
}),
z.object({
type: z.literal("turn_started"),
provider: AgentProviderSchema,
@@ -7,8 +7,6 @@ describe("shared tool-call display mapping", () => {
const display = buildToolCallDisplayModel({
name: "read_file",
status: "running",
input: { path: "/tmp/repo/src/index.ts" },
output: null,
error: null,
detail: {
type: "read",
@@ -23,13 +21,16 @@ describe("shared tool-call display mapping", () => {
});
});
it("does not infer summaries from raw input when detail is missing", () => {
it("does not infer summaries from unknown raw detail", () => {
const display = buildToolCallDisplayModel({
name: "exec_command",
status: "running",
input: { command: "npm test" },
output: null,
error: null,
detail: {
type: "unknown",
rawInput: { command: "npm test" },
rawOutput: null,
},
});
expect(display).toEqual({
@@ -37,13 +38,16 @@ describe("shared tool-call display mapping", () => {
});
});
it("keeps task metadata summary without detail", () => {
it("keeps task metadata summary on unknown detail", () => {
const display = buildToolCallDisplayModel({
name: "task",
status: "running",
input: null,
output: null,
error: null,
detail: {
type: "unknown",
rawInput: null,
rawOutput: null,
},
metadata: {
subAgentActivity: "Running tests",
},
@@ -59,9 +63,12 @@ describe("shared tool-call display mapping", () => {
const display = buildToolCallDisplayModel({
name: "shell",
status: "failed",
input: null,
output: null,
error: { message: "boom" },
detail: {
type: "unknown",
rawInput: null,
rawOutput: null,
},
});
expect(display.errorText).toBe('{\n "message": "boom"\n}');
+28 -26
View File
@@ -3,7 +3,7 @@ import { stripCwdPrefix } from "./path-utils.js";
export type ToolCallDisplayInput = Pick<
ToolCallTimelineItem,
"name" | "status" | "input" | "output" | "error" | "metadata" | "detail"
"name" | "status" | "error" | "metadata" | "detail"
> & {
cwd?: string;
};
@@ -62,33 +62,35 @@ export function buildToolCallDisplayModel(input: ToolCallDisplayInput): ToolCall
let displayName = humanizeToolName(input.name);
let summary: string | undefined;
if (input.detail) {
switch (input.detail.type) {
case "shell":
displayName = "Shell";
summary = input.detail.command;
break;
case "read":
displayName = "Read";
summary = stripCwdPrefix(input.detail.filePath, input.cwd);
break;
case "edit":
displayName = "Edit";
summary = stripCwdPrefix(input.detail.filePath, input.cwd);
break;
case "write":
displayName = "Write";
summary = stripCwdPrefix(input.detail.filePath, input.cwd);
break;
case "search":
displayName = "Search";
summary = input.detail.query;
break;
}
} else if (lowerName === "task") {
switch (input.detail.type) {
case "shell":
displayName = "Shell";
summary = input.detail.command;
break;
case "read":
displayName = "Read";
summary = stripCwdPrefix(input.detail.filePath, input.cwd);
break;
case "edit":
displayName = "Edit";
summary = stripCwdPrefix(input.detail.filePath, input.cwd);
break;
case "write":
displayName = "Write";
summary = stripCwdPrefix(input.detail.filePath, input.cwd);
break;
case "search":
displayName = "Search";
summary = input.detail.query;
break;
case "unknown":
break;
}
if (lowerName === "task" && input.detail.type === "unknown") {
displayName = "Task";
summary = isRecord(input.metadata) ? readString(input.metadata.subAgentActivity) : undefined;
} else if (lowerName === "thinking") {
} else if (lowerName === "thinking" && input.detail.type === "unknown") {
displayName = "Thinking";
}