-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathSubAgentPanel.tsx
More file actions
381 lines (354 loc) · 11.5 KB
/
Copy pathSubAgentPanel.tsx
File metadata and controls
381 lines (354 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import {
memo,
useEffect,
useRef,
useState,
useMemo,
type ReactNode,
} from 'react';
import type { ACPToolCall } from '../../../adapters/types';
import { useWebShellCustomization } from '../../../customization';
// Circular import with ToolGroup (agents render tool rows; agent tool
// rows render SubAgentPanel). Safe only while both modules dereference
// each other's exports at render time — never in top-level code.
import { ToolLine } from '../ToolGroup';
import { Markdown } from '../Markdown';
import { formatTimestamp } from '../../MessageTimestamp';
import {
formatDurationMs,
formatElapsed,
StatusIcon,
truncateText,
} from './toolDisplay';
import {
getAgentDisplayStatus,
formatTokenCount,
getAgentType,
getAgentDescription,
} from '../toolFormatting';
import chromeStyles from './ToolChrome.module.css';
import styles from './SubAgentPanel.module.css';
interface SubAgentPanelProps {
tool: ACPToolCall;
defaultExpanded?: boolean;
hideHeader?: boolean;
inline?: boolean;
}
interface TaskExecution {
type: 'task_execution';
subagentName?: string;
taskDescription?: string;
taskPrompt?: string;
status?: string;
result?: string;
tokenCount?: number;
toolCalls?: TaskToolCall[];
executionSummary?: {
totalToolCalls?: number;
totalDurationMs?: number;
inputTokens?: number;
outputTokens?: number;
totalTokens?: number;
estimatedCost?: number;
};
}
interface TaskToolCall {
callId: string;
name: string;
status: string;
args?: Record<string, unknown>;
description?: string;
}
function isTaskExecution(raw: unknown): raw is TaskExecution {
return (
!!raw &&
typeof raw === 'object' &&
(raw as Record<string, unknown>).type === 'task_execution'
);
}
/**
* Reveals a single sub-tool's wall-clock start time on hover in its top-right
* corner, mirroring how the main transcript surfaces each message's time —
* but via a scoped class pair (not MessageTimestamp) so the nested tooltip
* stays independent of the enclosing message's own time tooltip.
*/
function SubToolTime({
timestamp,
children,
}: {
timestamp?: number;
children: ReactNode;
}) {
if (timestamp === undefined) return <>{children}</>;
return (
<div className={styles.toolTimeRow}>
{children}
<span className={styles.toolTimeTip} aria-hidden="true">
{formatTimestamp(timestamp)}
</span>
</div>
);
}
const SubToolLine = memo(function SubToolLine({ tool }: { tool: ACPToolCall }) {
// Same row as the main transcript: one-line summary, expandable to
// the full output / diff / file content where the tool has any.
const body =
tool.subTools || tool.subContent ? (
<SubAgentPanel tool={tool} />
) : (
<ToolLine tool={tool} />
);
return <SubToolTime timestamp={tool.startTime}>{body}</SubToolTime>;
});
function TaskToolCallLine({ tc }: { tc: TaskToolCall }) {
const desc = tc.description || '';
return (
<div className={chromeStyles.line}>
<div className={chromeStyles.lineMain}>
<StatusIcon status={tc.status} />
<span className={chromeStyles.lineName}>{tc.name}</span>
{desc && (
<span className={chromeStyles.lineArg}>{truncateText(desc, 70)}</span>
)}
</div>
</div>
);
}
function getAgentResultText(tool: ACPToolCall): string {
if (tool.rawOutput && isTaskExecution(tool.rawOutput)) {
if (tool.rawOutput.result) return tool.rawOutput.result;
}
if (tool.content) {
for (const b of tool.content) {
if (b.type === 'content' && b.content?.text) return b.content.text;
}
}
if (tool.rawOutput) {
if (typeof tool.rawOutput === 'string') return tool.rawOutput;
const raw = tool.rawOutput as Record<string, unknown>;
if (typeof raw.output === 'string') return raw.output;
if (typeof raw.result === 'string') return raw.result;
if (typeof raw.content === 'string') return raw.content;
if (typeof raw.reason === 'string') return raw.reason;
if (
typeof raw.terminateReason === 'string' &&
raw.terminateReason !== 'GOAL'
) {
return raw.terminateReason;
}
if (typeof raw.error === 'string') return raw.error;
if (typeof raw.text === 'string') return raw.text;
}
return '';
}
type SubAgentTab = 'result' | 'tools';
/**
* Live sub-agent stream (thinking + output) shown while the agent runs.
* With compactThinking enabled it collapses to a 5-line window pinned to
* the newest content, with a toggle to the full scrollable view.
*/
function SubAgentStream({ text }: { text: string }) {
const { compactThinking } = useWebShellCustomization();
const [streamExpanded, setStreamExpanded] = useState(false);
const [overflowing, setOverflowing] = useState(false);
const streamRef = useRef<HTMLPreElement>(null);
const collapsed = compactThinking && !streamExpanded;
useEffect(() => {
const el = streamRef.current;
if (!el || !collapsed) return;
setOverflowing(el.scrollHeight > el.clientHeight);
// Pin the newest line into view while the stream grows.
el.scrollTop = el.scrollHeight;
}, [collapsed, text]);
useEffect(() => {
const el = streamRef.current;
if (!el || !collapsed) return;
const check = () => setOverflowing(el.scrollHeight > el.clientHeight);
const observer = new ResizeObserver(check);
observer.observe(el);
return () => observer.disconnect();
}, [collapsed]);
return (
<div>
<pre
ref={streamRef}
className={
collapsed
? `${styles.stream} ${styles.streamCollapsed}`
: styles.stream
}
>
{text}
</pre>
{compactThinking && (overflowing || streamExpanded) && (
<button
className={styles.expandToggle}
onClick={() => setStreamExpanded((v) => !v)}
aria-expanded={streamExpanded}
aria-label="Toggle agent stream details"
>
{streamExpanded ? '▲' : '▼'}
</button>
)}
</div>
);
}
/**
* Final agent result. The result is only on screen after the user
* explicitly opened the enclosing agent (tool row, accordion entry or
* panel header), so it renders in full straight away — capped to the
* same scrollable window as the live stream with compactThinking
* enabled, which keeps the opener within reach to collapse it again.
*/
function SubAgentResult({ content }: { content: string }) {
const { compactThinking } = useWebShellCustomization();
return (
<div className={compactThinking ? styles.scrollWindow : undefined}>
<Markdown content={content} />
</div>
);
}
/**
* Sub-tool list, capped to the same scrollable window as the result
* with compactThinking enabled. While the agent is still running the
* window follows the newest call; once it completes it snaps back to
* the top for reading.
*/
function SubAgentTools({
pinTail,
itemCount,
children,
}: {
pinTail: boolean;
itemCount: number;
children: ReactNode;
}) {
const { compactThinking } = useWebShellCustomization();
const windowRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const el = windowRef.current;
if (!el || !compactThinking) return;
el.scrollTop = pinTail ? el.scrollHeight : 0;
}, [compactThinking, pinTail, itemCount]);
return (
<div
ref={windowRef}
className={
compactThinking
? `${styles.tools} ${styles.scrollWindow}`
: styles.tools
}
>
{children}
</div>
);
}
export function SubAgentPanel({
tool,
defaultExpanded,
hideHeader,
inline,
}: SubAgentPanelProps) {
const isComplete = tool.status === 'completed' || tool.status === 'failed';
const displayStatus = getAgentDisplayStatus(tool);
const [expanded, setExpanded] = useState(defaultExpanded ?? false);
const [activeTab, setActiveTab] = useState<SubAgentTab>('result');
const taskExec = isTaskExecution(tool.rawOutput) ? tool.rawOutput : null;
const subToolCount =
tool.subTools?.length || taskExec?.toolCalls?.length || 0;
const description = getAgentDescription(tool);
const agentType = getAgentType(tool);
const elapsed =
formatElapsed(tool.startTime, tool.endTime) ||
formatDurationMs(taskExec?.executionSummary?.totalDurationMs);
const tokenCount =
taskExec?.tokenCount && taskExec.tokenCount > 0
? taskExec.tokenCount
: taskExec?.executionSummary?.totalTokens;
const tokens = tokenCount ? formatTokenCount(tokenCount) : '';
const resultText = isComplete ? getAgentResultText(tool) : '';
const taskToolCalls = useMemo(() => {
if (tool.subTools && tool.subTools.length > 0) return null;
return taskExec?.toolCalls || null;
}, [tool.subTools, taskExec]);
const hasResult = !!(tool.subContent || resultText);
const hasTools = !!(
(tool.subTools && tool.subTools.length > 0) ||
(taskToolCalls && taskToolCalls.length > 0)
);
const showTabs = hasResult && hasTools;
return (
<div className={inline ? undefined : styles.panel}>
{!hideHeader && (
<div className={styles.header} onClick={() => setExpanded(!expanded)}>
<StatusIcon status={displayStatus} />
<span className={chromeStyles.lineName}>{agentType}:</span>
{description && (
<span className={styles.desc}>{truncateText(description, 50)}</span>
)}
{isComplete && subToolCount > 0 && (
<span className={styles.meta}>· {subToolCount} tools</span>
)}
{elapsed && <span className={styles.meta}>· {elapsed}</span>}
{tokens && <span className={styles.meta}>· {tokens}</span>}
{!isComplete && (
<span className={styles.toggle}>{expanded ? '▼' : '▶'}</span>
)}
</div>
)}
{(expanded || hideHeader) && (
<div className={styles.body}>
{showTabs && (
<div className={styles.tabBar}>
<button
className={`${styles.tab} ${activeTab === 'result' ? styles.tabActive : ''}`}
onClick={() => setActiveTab('result')}
>
Result
</button>
<button
className={`${styles.tab} ${activeTab === 'tools' ? styles.tabActive : ''}`}
onClick={() => setActiveTab('tools')}
>
Tools ({subToolCount})
</button>
</div>
)}
{(!showTabs || activeTab === 'result') && hasResult && (
<div className={styles.content}>
{isComplete ? (
<SubAgentResult content={tool.subContent || resultText} />
) : (
tool.subContent && <SubAgentStream text={tool.subContent} />
)}
</div>
)}
{(!showTabs || activeTab === 'tools') && (
<>
{tool.subTools && tool.subTools.length > 0 && (
<SubAgentTools
pinTail={!isComplete}
itemCount={tool.subTools.length}
>
{tool.subTools.map((sub) => (
<SubToolLine key={sub.callId} tool={sub} />
))}
</SubAgentTools>
)}
{taskToolCalls && taskToolCalls.length > 0 && (
<SubAgentTools
pinTail={!isComplete}
itemCount={taskToolCalls.length}
>
{taskToolCalls.map((tc) => (
<TaskToolCallLine key={tc.callId} tc={tc} />
))}
</SubAgentTools>
)}
</>
)}
</div>
)}
</div>
);
}