forked from QwenLM/qwen-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentExecutionDisplay.tsx
More file actions
570 lines (523 loc) · 17 KB
/
AgentExecutionDisplay.tsx
File metadata and controls
570 lines (523 loc) · 17 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/**
* @license
* Copyright 2025 Qwen
* SPDX-License-Identifier: Apache-2.0
*/
import React, { useMemo } from 'react';
import { Box, Text } from 'ink';
import type {
AgentResultDisplay,
AgentStatsSummary,
Config,
} from '@qwen-code/qwen-code-core';
import { theme } from '../../../semantic-colors.js';
import { useKeypress } from '../../../hooks/useKeypress.js';
import { COLOR_OPTIONS } from '../constants.js';
import { fmtDuration } from '../utils.js';
import { ToolConfirmationMessage } from '../../messages/ToolConfirmationMessage.js';
export type DisplayMode = 'compact' | 'default' | 'verbose';
export interface AgentExecutionDisplayProps {
data: AgentResultDisplay;
availableHeight?: number;
childWidth: number;
config: Config;
/** Whether this display's confirmation prompt should respond to keyboard input. */
isFocused?: boolean;
/** Whether another subagent's approval currently holds the focus lock, blocking this one. */
isWaitingForOtherApproval?: boolean;
}
const getStatusColor = (
status:
| AgentResultDisplay['status']
| 'executing'
| 'success'
| 'awaiting_approval',
) => {
switch (status) {
case 'running':
case 'executing':
case 'awaiting_approval':
return theme.status.warning;
case 'completed':
case 'success':
return theme.status.success;
case 'cancelled':
return theme.status.warning;
case 'failed':
return theme.status.error;
default:
return theme.text.secondary;
}
};
const getStatusText = (status: AgentResultDisplay['status']) => {
switch (status) {
case 'running':
return 'Running';
case 'completed':
return 'Completed';
case 'cancelled':
return 'User Cancelled';
case 'failed':
return 'Failed';
default:
return 'Unknown';
}
};
const MAX_TOOL_CALLS = 5;
const MAX_TASK_PROMPT_LINES = 5;
/**
* Component to display subagent execution progress and results.
* This is now a pure component that renders the provided SubagentExecutionResultDisplay data.
* Real-time updates are handled by the parent component updating the data prop.
*/
export const AgentExecutionDisplay: React.FC<AgentExecutionDisplayProps> = ({
data,
availableHeight,
childWidth,
config,
isFocused = true,
isWaitingForOtherApproval = false,
}) => {
const [displayMode, setDisplayMode] = React.useState<DisplayMode>('compact');
const agentColor = useMemo(() => {
const colorOption = COLOR_OPTIONS.find(
(option) => option.name === data.subagentColor,
);
return colorOption?.value || theme.text.accent;
}, [data.subagentColor]);
const footerText = React.useMemo(() => {
// This component only listens to keyboard shortcut events when the subagent is running
if (data.status !== 'running') return '';
if (displayMode === 'default') {
const hasMoreLines =
data.taskPrompt.split('\n').length > MAX_TASK_PROMPT_LINES;
const hasMoreToolCalls =
data.toolCalls && data.toolCalls.length > MAX_TOOL_CALLS;
if (hasMoreToolCalls || hasMoreLines) {
return 'Press ctrl+e to show less, ctrl+f to show more.';
}
return 'Press ctrl+e to show less.';
}
if (displayMode === 'verbose') {
return 'Press ctrl+f to show less.';
}
return '';
}, [displayMode, data]);
// Handle keyboard shortcuts to control display mode
useKeypress(
(key) => {
if (key.ctrl && key.name === 'e') {
// ctrl+e toggles between compact and default
setDisplayMode((current) =>
current === 'compact' ? 'default' : 'compact',
);
} else if (key.ctrl && key.name === 'f') {
// ctrl+f toggles between default and verbose
setDisplayMode((current) =>
current === 'default' ? 'verbose' : 'default',
);
}
},
{ isActive: true },
);
if (displayMode === 'compact') {
return (
<Box flexDirection="column">
{/* Header: Agent name and status */}
{!data.pendingConfirmation && (
<Box flexDirection="row">
<Text bold color={agentColor}>
{data.subagentName}
</Text>
<StatusDot status={data.status} />
<StatusIndicator status={data.status} />
</Box>
)}
{/* Running state: Show current tool call and progress */}
{data.status === 'running' && (
<>
{/* Current tool call */}
{data.toolCalls && data.toolCalls.length > 0 && (
<Box flexDirection="column">
<ToolCallItem
toolCall={data.toolCalls[data.toolCalls.length - 1]}
compact={true}
/>
{/* Show count of additional tool calls if there are more than 1 */}
{data.toolCalls.length > 1 && !data.pendingConfirmation && (
<Box flexDirection="row" paddingLeft={4}>
<Text color={theme.text.secondary}>
+{data.toolCalls.length - 1} more tool calls (ctrl+e to
expand)
</Text>
</Box>
)}
</Box>
)}
{/* Inline approval prompt when awaiting confirmation */}
{data.pendingConfirmation && (
<Box flexDirection="column" marginTop={1} paddingLeft={1}>
{isWaitingForOtherApproval && (
<Box marginBottom={0}>
<Text color={theme.text.secondary} dimColor>
⏳ Waiting for other approval...
</Text>
</Box>
)}
<ToolConfirmationMessage
confirmationDetails={data.pendingConfirmation}
isFocused={isFocused}
availableTerminalHeight={availableHeight}
contentWidth={childWidth - 4}
compactMode={true}
config={config}
/>
</Box>
)}
</>
)}
{/* Completed state: Show summary line */}
{data.status === 'completed' && data.executionSummary && (
<Box flexDirection="row" marginTop={1}>
<Text color={theme.text.secondary}>
Execution Summary: {data.executionSummary.totalToolCalls} tool
uses · {data.executionSummary.totalTokens.toLocaleString()} tokens
· {fmtDuration(data.executionSummary.totalDurationMs)}
</Text>
</Box>
)}
{/* Failed/Cancelled state: Show error reason */}
{data.status === 'failed' && (
<Box flexDirection="row" marginTop={1}>
<Text color={theme.status.error}>
Failed: {data.terminateReason}
</Text>
</Box>
)}
</Box>
);
}
// Default and verbose modes use normal layout
return (
<Box flexDirection="column" paddingX={1} gap={1}>
{/* Header with subagent name and status */}
<Box flexDirection="row">
<Text bold color={agentColor}>
{data.subagentName}
</Text>
<StatusDot status={data.status} />
<StatusIndicator status={data.status} />
</Box>
{/* Task description */}
<TaskPromptSection
taskPrompt={data.taskPrompt}
displayMode={displayMode}
/>
{/* Progress section for running tasks */}
{data.status === 'running' &&
data.toolCalls &&
data.toolCalls.length > 0 && (
<Box flexDirection="column">
<ToolCallsList
toolCalls={data.toolCalls}
displayMode={displayMode}
/>
</Box>
)}
{/* Inline approval prompt when awaiting confirmation */}
{data.pendingConfirmation && (
<Box flexDirection="column">
{isWaitingForOtherApproval && (
<Box marginBottom={0}>
<Text color={theme.text.secondary} dimColor>
⏳ Waiting for other approval...
</Text>
</Box>
)}
<ToolConfirmationMessage
confirmationDetails={data.pendingConfirmation}
config={config}
isFocused={isFocused}
availableTerminalHeight={availableHeight}
contentWidth={childWidth - 4}
compactMode={true}
/>
</Box>
)}
{/* Results section for completed/failed tasks */}
{(data.status === 'completed' ||
data.status === 'failed' ||
data.status === 'cancelled') && (
<ResultsSection data={data} displayMode={displayMode} />
)}
{/* Footer with keyboard shortcuts */}
{footerText && (
<Box flexDirection="row">
<Text color={theme.text.secondary}>{footerText}</Text>
</Box>
)}
</Box>
);
};
/**
* Task prompt section with truncation support
*/
const TaskPromptSection: React.FC<{
taskPrompt: string;
displayMode: DisplayMode;
}> = ({ taskPrompt, displayMode }) => {
const lines = taskPrompt.split('\n');
const shouldTruncate = lines.length > 10;
const showFull = displayMode === 'verbose';
const displayLines = showFull ? lines : lines.slice(0, MAX_TASK_PROMPT_LINES);
return (
<Box flexDirection="column" gap={1}>
<Box flexDirection="row">
<Text color={theme.text.primary}>Task Detail: </Text>
{shouldTruncate && displayMode === 'default' && (
<Text color={theme.text.secondary}>
{' '}
Showing the first {MAX_TASK_PROMPT_LINES} lines.
</Text>
)}
</Box>
<Box paddingLeft={1}>
<Text wrap="wrap">
{displayLines.join('\n') + (shouldTruncate && !showFull ? '...' : '')}
</Text>
</Box>
</Box>
);
};
/**
* Status dot component with similar height as text
*/
const StatusDot: React.FC<{
status: AgentResultDisplay['status'];
}> = ({ status }) => (
<Box marginLeft={1} marginRight={1}>
<Text color={getStatusColor(status)}>●</Text>
</Box>
);
/**
* Status indicator component
*/
const StatusIndicator: React.FC<{
status: AgentResultDisplay['status'];
}> = ({ status }) => {
const color = getStatusColor(status);
const text = getStatusText(status);
return <Text color={color}>{text}</Text>;
};
/**
* Tool calls list - format consistent with ToolInfo in ToolMessage.tsx
*/
const ToolCallsList: React.FC<{
toolCalls: AgentResultDisplay['toolCalls'];
displayMode: DisplayMode;
}> = ({ toolCalls, displayMode }) => {
const calls = toolCalls || [];
const shouldTruncate = calls.length > MAX_TOOL_CALLS;
const showAll = displayMode === 'verbose';
const displayCalls = showAll ? calls : calls.slice(-MAX_TOOL_CALLS); // Show last 5
// Reverse the order to show most recent first
const reversedDisplayCalls = [...displayCalls].reverse();
return (
<Box flexDirection="column">
<Box flexDirection="row" marginBottom={1}>
<Text color={theme.text.primary}>Tools:</Text>
{shouldTruncate && displayMode === 'default' && (
<Text color={theme.text.secondary}>
{' '}
Showing the last {MAX_TOOL_CALLS} of {calls.length} tools.
</Text>
)}
</Box>
{reversedDisplayCalls.map((toolCall, index) => (
<ToolCallItem key={`${toolCall.name}-${index}`} toolCall={toolCall} />
))}
</Box>
);
};
/**
* Individual tool call item - consistent with ToolInfo format
*/
const ToolCallItem: React.FC<{
toolCall: {
name: string;
status: 'executing' | 'awaiting_approval' | 'success' | 'failed';
error?: string;
args?: Record<string, unknown>;
result?: string;
resultDisplay?: string;
description?: string;
};
compact?: boolean;
}> = ({ toolCall, compact = false }) => {
const STATUS_INDICATOR_WIDTH = 3;
// Map subagent status to ToolCallStatus-like display
const statusIcon = React.useMemo(() => {
const color = getStatusColor(toolCall.status);
switch (toolCall.status) {
case 'executing':
return <Text color={color}>⊷</Text>; // Using same as ToolMessage
case 'awaiting_approval':
return <Text color={theme.status.warning}>?</Text>;
case 'success':
return <Text color={color}>✓</Text>;
case 'failed':
return (
<Text color={color} bold>
x
</Text>
);
default:
return <Text color={color}>o</Text>;
}
}, [toolCall.status]);
const description = React.useMemo(() => {
if (!toolCall.description) return '';
const firstLine = toolCall.description.split('\n')[0];
return firstLine.length > 80
? firstLine.substring(0, 80) + '...'
: firstLine;
}, [toolCall.description]);
// Get first line of resultDisplay for truncated output
const truncatedOutput = React.useMemo(() => {
if (!toolCall.resultDisplay) return '';
const firstLine = toolCall.resultDisplay.split('\n')[0];
return firstLine.length > 80
? firstLine.substring(0, 80) + '...'
: firstLine;
}, [toolCall.resultDisplay]);
return (
<Box flexDirection="column" paddingLeft={1} marginBottom={0}>
{/* First line: status icon + tool name + description (consistent with ToolInfo) */}
<Box flexDirection="row">
<Box minWidth={STATUS_INDICATOR_WIDTH}>{statusIcon}</Box>
<Text wrap="truncate-end">
<Text>{toolCall.name}</Text>{' '}
<Text color={theme.text.secondary}>{description}</Text>
{toolCall.error && (
<Text color={theme.status.error}> - {toolCall.error}</Text>
)}
</Text>
</Box>
{/* Second line: truncated returnDisplay output - hidden in compact mode */}
{!compact && truncatedOutput && (
<Box flexDirection="row" paddingLeft={STATUS_INDICATOR_WIDTH}>
<Text color={theme.text.secondary}>{truncatedOutput}</Text>
</Box>
)}
</Box>
);
};
/**
* Execution summary details component
*/
const ExecutionSummaryDetails: React.FC<{
data: AgentResultDisplay;
displayMode: DisplayMode;
}> = ({ data, displayMode: _displayMode }) => {
const stats = data.executionSummary;
if (!stats) {
return (
<Box flexDirection="column" paddingLeft={1}>
<Text color={theme.text.secondary}>• No summary available</Text>
</Box>
);
}
return (
<Box flexDirection="column" paddingLeft={1}>
<Text>
• <Text>Duration: {fmtDuration(stats.totalDurationMs)}</Text>
</Text>
<Text>
• <Text>Rounds: {stats.rounds}</Text>
</Text>
<Text>
• <Text>Tokens: {stats.totalTokens.toLocaleString()}</Text>
</Text>
</Box>
);
};
/**
* Tool usage statistics component
*/
const ToolUsageStats: React.FC<{
executionSummary?: AgentStatsSummary;
}> = ({ executionSummary }) => {
if (!executionSummary) {
return (
<Box flexDirection="column" paddingLeft={1}>
<Text color={theme.text.secondary}>• No tool usage data available</Text>
</Box>
);
}
return (
<Box flexDirection="column" paddingLeft={1}>
<Text>
• <Text>Total Calls:</Text> {executionSummary.totalToolCalls}
</Text>
<Text>
• <Text>Success Rate:</Text>{' '}
<Text color={theme.status.success}>
{executionSummary.successRate.toFixed(1)}%
</Text>{' '}
(
<Text color={theme.status.success}>
{executionSummary.successfulToolCalls} success
</Text>
,{' '}
<Text color={theme.status.error}>
{executionSummary.failedToolCalls} failed
</Text>
)
</Text>
</Box>
);
};
/**
* Results section for completed executions - matches the clean layout from the image
*/
const ResultsSection: React.FC<{
data: AgentResultDisplay;
displayMode: DisplayMode;
}> = ({ data, displayMode }) => (
<Box flexDirection="column" gap={1}>
{/* Tool calls section - clean list format */}
{data.toolCalls && data.toolCalls.length > 0 && (
<ToolCallsList toolCalls={data.toolCalls} displayMode={displayMode} />
)}
{/* Execution Summary section - hide when cancelled */}
{data.status === 'completed' && (
<Box flexDirection="column">
<Box flexDirection="row" marginBottom={1}>
<Text color={theme.text.primary}>Execution Summary:</Text>
</Box>
<ExecutionSummaryDetails data={data} displayMode={displayMode} />
</Box>
)}
{/* Tool Usage section - hide when cancelled */}
{data.status === 'completed' && data.executionSummary && (
<Box flexDirection="column">
<Box flexDirection="row" marginBottom={1}>
<Text color={theme.text.primary}>Tool Usage:</Text>
</Box>
<ToolUsageStats executionSummary={data.executionSummary} />
</Box>
)}
{/* Error reason for failed tasks */}
{data.status === 'cancelled' && (
<Box flexDirection="row">
<Text color={theme.status.warning}>⏹ User Cancelled</Text>
</Box>
)}
{data.status === 'failed' && (
<Box flexDirection="row">
<Text color={theme.status.error}>Task Failed: </Text>
<Text color={theme.status.error}>{data.terminateReason}</Text>
</Box>
)}
</Box>
);