Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/sdk-typescript/src/daemon/ui/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ export interface DaemonUiAssistantDoneEvent extends DaemonUiEventBase {
/**
* Token usage the agent reports for one model round, carried on the daemon's
* `agent_message_chunk._meta.usage`. A turn issues one of these per model call,
* so a turn's total is the sum of its rounds. Counts are top-level only —
* sub-agent (delegated) usage arrives with a `parentToolCallId` and is excluded
* so a delegated call's tokens are not attributed to the parent turn.
* so a turn's total is the sum of its rounds. Sub-agent (delegated) usage is
* included in the spawning turn because it is part of that turn's real cost.
*/
export interface DaemonTurnUsage {
inputTokens: number;
Expand All @@ -131,7 +130,7 @@ export interface DaemonTurnUsage {
export interface DaemonUiAssistantUsageEvent extends DaemonUiEventBase {
type: 'assistant.usage';
usage: DaemonTurnUsage;
/** Set when the usage belongs to a sub-agent round; excluded from turn totals. */
/** Set when the usage belongs to a sub-agent round; folded into the parent turn total. */
parentToolCallId?: string;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/web-shell/client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ export interface WebShellProps {
renderFooter?: FooterRenderer;
/** Collapse thinking blocks to 5 lines with a click-to-expand toggle. */
compactThinking?: boolean;
/** Summarize each turn's intermediate steps behind an execution trace header. Defaults to collapseCompletedTurns when unset. */
summarizeSteps?: boolean;
/** Auto-collapse completed turns to just the prompt and final answer, with a per-turn toggle. Defaults to true. */
collapseCompletedTurns?: boolean;
/** Enable virtual scrolling only when rendered transcript rows exceed this threshold. Defaults to 200. */
Expand Down Expand Up @@ -698,6 +700,7 @@ export function App({
renderWelcomeHeader,
renderFooter,
compactThinking = false,
summarizeSteps,
collapseCompletedTurns = true,
virtualScrollThreshold,
markdown,
Expand Down Expand Up @@ -2775,8 +2778,10 @@ export function App({
onShowContextDetail={handleShowContextDetail}
catchingUp={connection.catchingUp}
isResponding={streamingState !== 'idle'}
summarizeSteps={summarizeSteps}
workspaceCwd={connection.workspaceCwd || ''}
shellOutputMaxLines={shellOutputMaxLines}
commands={commands}
showRetryHint={showRetryHint}
onRetryClick={handleRetry}
welcomeHeader={welcomeHeader}
Expand Down
4 changes: 3 additions & 1 deletion packages/web-shell/client/adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ export interface TurnCollapseHead {
/**
* Per-turn token usage, summed from the turn's assistant messages. Both fields
* are present together or the pair is undefined (older sessions stamp no
* usage). Sub-agent tokens are excluded (see the SDK reducer).
* usage). Sub-agent tokens are included (see the SDK reducer).
*/
inputTokens?: number;
outputTokens?: number;
/** Cached-read tokens — a subset of inputTokens, surfaced only when > 0. */
cachedTokens?: number;
/** Number of tool calls shown in this turn. */
toolCallCount?: number;
/**
* Prompt wall-clock (ms) for a still-running turn. Present only while the turn
* is active; the row ticks `now - liveStartedAt` once a second so the elapsed
Expand Down
6 changes: 6 additions & 0 deletions packages/web-shell/client/components/MessageItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { memo, type ReactElement } from 'react';
import type {
ACPToolCall,
CommandInfo,
Message,
PermissionRequest,
TodoItem,
Expand Down Expand Up @@ -32,6 +33,7 @@ interface MessageItemProps {
showRetryHint?: boolean;
onRetryClick?: () => void;
shellOutputMaxLines: number;
commands?: readonly CommandInfo[];
/** Present on a collapsible turn's prompt row; renders the collapse toggle. */
collapse?: TurnCollapseHead;
onToggleCollapse?: (turnId: string) => void;
Expand All @@ -47,6 +49,7 @@ export const MessageItem = memo(function MessageItem({
showRetryHint = false,
onRetryClick,
shellOutputMaxLines,
commands,
collapse,
onToggleCollapse,
}: MessageItemProps) {
Expand All @@ -57,6 +60,7 @@ export const MessageItem = memo(function MessageItem({
<UserMessage
content={message.content}
images={message.images}
commands={commands}
collapse={collapse}
onToggleCollapse={onToggleCollapse}
/>
Expand Down Expand Up @@ -148,6 +152,7 @@ function areMessageItemPropsEqual(
if (prev.showRetryHint !== next.showRetryHint) return false;
if (prev.onRetryClick !== next.onRetryClick) return false;
if (prev.shellOutputMaxLines !== next.shellOutputMaxLines) return false;
if (prev.commands !== next.commands) return false;
if (prev.onToggleCollapse !== next.onToggleCollapse) return false;
if (!turnCollapseEqual(prev.collapse, next.collapse)) return false;
return areMessagesEqual(prev.message, next.message);
Expand All @@ -167,6 +172,7 @@ function turnCollapseEqual(
a.inputTokens === b.inputTokens &&
a.outputTokens === b.outputTokens &&
a.cachedTokens === b.cachedTokens &&
a.toolCallCount === b.toolCallCount &&
a.liveStartedAt === b.liveStartedAt
);
}
Expand Down
132 changes: 127 additions & 5 deletions packages/web-shell/client/components/MessageList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ describe('applyTurnCollapse', () => {
turnId: 'u1',
collapsed: true,
hiddenCount: 1,
toolCallCount: 2,
});
expect(messageRow(out[1]).collapse).toBeUndefined();
});
Expand All @@ -412,7 +413,29 @@ describe('applyTurnCollapse', () => {
turnId: 'u1',
collapsed: false,
hiddenCount: 1,
toolCallCount: 2,
});
expect(out[1].trace).toBe(true);
expect(out[2].trace).toBeUndefined();
});

it('marks narration followed by a tool as trace immediately', () => {
const items = groupParallelAgents([
makeUserMessage('u1'),
{
id: 'a0',
role: 'assistant',
content: 'I will inspect the project.',
},
makeMultiToolGroup('g1'),
]);
const out = collapseItems(items, {
isResponding: true,
overrides: new Map([['u1', true]]),
});
expect(rowIds(out)).toEqual(['u1', 'a0', 'g1']);
expect(out[1].trace).toBe(true);
expect(out[2].trace).toBe(true);
});

it('tags but keeps the active turn expanded while responding', () => {
Expand Down Expand Up @@ -458,8 +481,9 @@ describe('applyTurnCollapse', () => {
usage: { inputTokens: 100, outputTokens: 20 },
},
]);
const head = messageRow(collapseItems(items, { isResponding: true })[0])
.collapse;
const head = messageRow(
collapseItems(items, { isResponding: true })[0],
).collapse;
// The streamed answer is provisional, not a step → nothing to fold, so no
// chevron flashes in then out when the turn completes.
expect(head?.hiddenCount).toBe(0);
Expand All @@ -475,8 +499,9 @@ describe('applyTurnCollapse', () => {
timestamp: 2_000,
},
]);
const head = messageRow(collapseItems(items, { isResponding: true })[0])
.collapse;
const head = messageRow(
collapseItems(items, { isResponding: true })[0],
).collapse;
expect(head?.liveStartedAt).toBe(1_000);
});

Expand Down Expand Up @@ -559,6 +584,7 @@ describe('applyTurnCollapse', () => {
turnId: 'u1',
collapsed: true,
hiddenCount: 2,
toolCallCount: 4,
});
});

Expand Down Expand Up @@ -719,9 +745,37 @@ describe('applyTurnCollapse', () => {
elapsedMs: 4_000,
inputTokens: 3100,
outputTokens: 5100,
toolCallCount: 1,
});
});

it('ignores non-step system timestamps when recording elapsed', () => {
const items = groupParallelAgents([
{ id: 'u1', role: 'user', content: 'hi', timestamp: 1_000 },
{
id: 'g1',
role: 'tool_group',
tools: [{ callId: 'c1', toolName: 'Read', status: 'completed' }],
timestamp: 2_000,
},
{
id: 'a1',
role: 'assistant',
content: 'final',
timestamp: 5_000,
},
{
id: 's1',
role: 'system',
content: 'late title refresh',
variant: 'info',
timestamp: 100_000,
},
]);
const head = messageRow(collapseItems(items)[0]).collapse;
expect(head?.elapsedMs).toBe(4_000);
});

it('sums token usage across a turn (hidden mid-turn text + final answer)', () => {
const items = groupParallelAgents([
{ id: 'u1', role: 'user', content: 'hi', timestamp: 1_000 },
Expand Down Expand Up @@ -750,6 +804,69 @@ describe('applyTurnCollapse', () => {
expect(head?.inputTokens).toBe(300);
expect(head?.outputTokens).toBe(130);
expect(head?.elapsedMs).toBe(3_000);
expect(head?.toolCallCount).toBe(1);
});

it('counts visible tool calls across regular and grouped agent rows', () => {
const items = groupParallelAgents([
{ id: 'u1', role: 'user', content: 'hi', timestamp: 1_000 },
{
id: 'g1',
role: 'tool_group',
tools: [
{ callId: 'c1', toolName: 'Read', status: 'completed' },
{ callId: 'c2', toolName: 'Write', status: 'completed' },
],
timestamp: 2_000,
},
{
id: 'agent-1',
role: 'tool_group',
tools: [
{
callId: 'a1',
toolName: 'agent',
status: 'completed',
subTools: [
{ callId: 'a1-read', toolName: 'Read', status: 'completed' },
{
callId: 'a1-shell',
toolName: 'Shell',
status: 'completed',
subTools: [
{
callId: 'a1-shell-child',
toolName: 'Parse',
status: 'completed',
},
],
},
],
},
],
timestamp: 3_000,
},
{
id: 'agent-2',
role: 'tool_group',
tools: [
{
callId: 'a2',
toolName: 'agent',
status: 'completed',
},
],
timestamp: 4_000,
},
{
id: 'a1',
role: 'assistant',
content: 'final',
timestamp: 5_000,
},
]);
const head = messageRow(collapseItems(items)[0]).collapse;
expect(head?.toolCallCount).toBe(4);
});

it('omits elapsed/usage when the turn carries no timestamps or usage', () => {
Expand All @@ -759,7 +876,12 @@ describe('applyTurnCollapse', () => {
makeAssistantMessage('a1'),
]);
const head = messageRow(collapseItems(items)[0]).collapse;
expect(head).toEqual({ turnId: 'u1', collapsed: true, hiddenCount: 1 });
expect(head).toEqual({
turnId: 'u1',
collapsed: true,
hiddenCount: 1,
toolCallCount: 2,
});
});

it('shows a chevron-less metrics seam on a step-less turn', () => {
Expand Down
Loading
Loading