@steerable/agent-shell-web 0.14.2 → 0.14.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/components/chat/ApprovalModal.tsx +426 -124
- package/src/components/chat/AssistantMessage.tsx +37 -11
- package/src/components/chat/ChatInput.tsx +172 -71
- package/src/components/chat/ChatProjectBadge.tsx +89 -23
- package/src/components/chat/ContextUsagePill.tsx +73 -21
- package/src/components/chat/ExecPolicyPicker.tsx +2 -2
- package/src/components/chat/ExecutedActionsCard.tsx +886 -180
- package/src/components/chat/LocalChatPanel.tsx +31 -5
- package/src/components/chat/Markdown.tsx +109 -11
- package/src/components/chat/ModelPicker.tsx +5 -2
- package/src/components/chat/PlanTodoList.tsx +184 -78
- package/src/components/chat/TurnProcessGroup.tsx +37 -3
- package/src/components/chat/TurnProcessTimeline.tsx +533 -50
- package/src/components/chat/executed-actions-model.ts +1103 -61
- package/src/components/chat/process-status.ts +72 -4
- package/src/components/chat/rich-tool-views.tsx +666 -0
- package/src/components/chat/todo-list-model.ts +15 -0
- package/src/components/chat/turn-timeline.ts +51 -4
- package/src/components/settings/LlmSettingsPanel.tsx +1 -1
- package/src/components/settings/llm-vendors.ts +1 -1
- package/src/lib/browser-dev-host-mock.ts +1 -1
- package/src/lib/chat-mode-store.ts +241 -0
- package/src/pages/AgentPage.tsx +183 -77
- package/src/styles/index.css +10 -25
|
@@ -8,10 +8,43 @@
|
|
|
8
8
|
* result_count / status / bytes / truncated 这些小字段原样保留。
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
import {
|
|
12
|
+
classifyShellCommand,
|
|
13
|
+
extractFileChangePreview,
|
|
14
|
+
extractSkillInvocation,
|
|
15
|
+
isBenignSearchNoMatch,
|
|
16
|
+
parseJsonValue,
|
|
17
|
+
parseTerminalExecution,
|
|
18
|
+
parseToolEnvelope,
|
|
19
|
+
truncateOutputMiddle,
|
|
20
|
+
type ClassifiedShellAction,
|
|
21
|
+
type FileChangePreview,
|
|
22
|
+
type TerminalExecutionView,
|
|
23
|
+
type ToolEnvelope,
|
|
24
|
+
type TruncatedOutput,
|
|
25
|
+
} from '@steerable/agent-protocol';
|
|
11
26
|
import { t } from '@/i18n';
|
|
12
27
|
import { agentLabel } from '@/i18n/agent-label';
|
|
13
28
|
import { findAgentForProfile } from './orchestration-children-model';
|
|
14
29
|
|
|
30
|
+
export {
|
|
31
|
+
classifyShellCommand,
|
|
32
|
+
extractFileChangePreview,
|
|
33
|
+
extractSkillInvocation,
|
|
34
|
+
isBenignSearchNoMatch,
|
|
35
|
+
parseJsonValue,
|
|
36
|
+
parseTerminalExecution,
|
|
37
|
+
parseToolEnvelope,
|
|
38
|
+
truncateOutputMiddle,
|
|
39
|
+
type ClassifiedShellAction,
|
|
40
|
+
type FileChangePreview,
|
|
41
|
+
type TerminalExecutionView,
|
|
42
|
+
type ToolEnvelope,
|
|
43
|
+
type TruncatedOutput,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const SLOW_TOOL_THRESHOLD_MS = 5_000;
|
|
47
|
+
|
|
15
48
|
interface WebToolResultData {
|
|
16
49
|
result_count?: unknown;
|
|
17
50
|
status?: unknown;
|
|
@@ -122,10 +155,218 @@ export interface ExecutedActionLike {
|
|
|
122
155
|
result?: unknown;
|
|
123
156
|
sandbox?: { backend?: string; enforcement: string };
|
|
124
157
|
approval?: { kind?: string; cached?: boolean; auto?: boolean };
|
|
125
|
-
view?: {
|
|
158
|
+
view?: {
|
|
159
|
+
card?: ActionCardKind;
|
|
160
|
+
kind?: string;
|
|
161
|
+
title?: string;
|
|
162
|
+
declared?: boolean;
|
|
163
|
+
};
|
|
126
164
|
durationMs?: number;
|
|
127
165
|
}
|
|
128
166
|
|
|
167
|
+
export type ActionCardKind = 'generic' | 'terminal' | 'diff' | 'search' | 'read' | 'web';
|
|
168
|
+
|
|
169
|
+
const KNOWN_TOOL_CARDS: Readonly<Record<string, ActionCardKind>> = {
|
|
170
|
+
local_exec_shell: 'terminal',
|
|
171
|
+
pwsh: 'terminal',
|
|
172
|
+
bash: 'terminal',
|
|
173
|
+
bash_session: 'terminal',
|
|
174
|
+
write_stdin: 'terminal',
|
|
175
|
+
local_run_snippet: 'terminal',
|
|
176
|
+
local_run_script: 'terminal',
|
|
177
|
+
local_read_file: 'read',
|
|
178
|
+
read_file: 'read',
|
|
179
|
+
view_image: 'read',
|
|
180
|
+
capture_display: 'read',
|
|
181
|
+
local_list_scripts: 'read',
|
|
182
|
+
worktree_list: 'read',
|
|
183
|
+
memory_read: 'read',
|
|
184
|
+
local_write_file: 'diff',
|
|
185
|
+
write_file: 'diff',
|
|
186
|
+
local_edit_file: 'diff',
|
|
187
|
+
edit_file: 'diff',
|
|
188
|
+
apply_patch: 'diff',
|
|
189
|
+
grep: 'search',
|
|
190
|
+
glob: 'search',
|
|
191
|
+
tool_search: 'search',
|
|
192
|
+
memory_search: 'search',
|
|
193
|
+
web_search: 'web',
|
|
194
|
+
web_fetch: 'web',
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/** Resolve visual card family from host declaration or known built-in tool table (never prefix-guessed). */
|
|
198
|
+
export function resolveActionCard(action: {
|
|
199
|
+
tool: string;
|
|
200
|
+
view?: { card?: ActionCardKind; declared?: boolean };
|
|
201
|
+
}): ActionCardKind {
|
|
202
|
+
if (action.view?.declared && action.view.card) return action.view.card;
|
|
203
|
+
return KNOWN_TOOL_CARDS[action.tool] ?? 'generic';
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export const MAX_WEB_SUMMARY_CHARS = 240;
|
|
207
|
+
const MAX_SUMMARY_TOOLTIP_CHARS = 500;
|
|
208
|
+
|
|
209
|
+
function normalizeSingleLine(raw: string): string {
|
|
210
|
+
return raw.replace(/\s+/g, ' ').trim();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function clipSingleLine(raw: string, max = MAX_WEB_SUMMARY_CHARS): string {
|
|
214
|
+
const clean = normalizeSingleLine(raw);
|
|
215
|
+
if (!clean) return '';
|
|
216
|
+
return clean.length > max ? `${clean.slice(0, max - 1)}…` : clean;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function formatReadRangeSuffix(obj: Record<string, unknown>): string {
|
|
220
|
+
const start = typeof obj.startLine === 'number' && Number.isFinite(obj.startLine) ? obj.startLine : null;
|
|
221
|
+
const end = typeof obj.endLine === 'number' && Number.isFinite(obj.endLine) ? obj.endLine : null;
|
|
222
|
+
if (start !== null && end !== null) return `:L${start}-${end}`;
|
|
223
|
+
if (start !== null) return `:L${start}`;
|
|
224
|
+
return '';
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function summarizeArgumentsWide(
|
|
228
|
+
args: unknown,
|
|
229
|
+
max = MAX_WEB_SUMMARY_CHARS,
|
|
230
|
+
): { summary: string | null; tooltip?: string } {
|
|
231
|
+
if (args === null || args === undefined) return { summary: null };
|
|
232
|
+
if (typeof args === 'string') {
|
|
233
|
+
const summary = clipSingleLine(args, max);
|
|
234
|
+
const tooltip = clipSingleLine(args, MAX_SUMMARY_TOOLTIP_CHARS);
|
|
235
|
+
return summary ? { summary, tooltip } : { summary: null };
|
|
236
|
+
}
|
|
237
|
+
if (typeof args !== 'object') {
|
|
238
|
+
const text = String(args);
|
|
239
|
+
return { summary: clipSingleLine(text, max) || null };
|
|
240
|
+
}
|
|
241
|
+
const obj = args as Record<string, unknown>;
|
|
242
|
+
for (const key of ['command', 'cmd', 'url', 'query', 'path', 'file', 'target', 'message']) {
|
|
243
|
+
const v = obj[key];
|
|
244
|
+
if (typeof v === 'string' && v.trim().length > 0) {
|
|
245
|
+
const raw =
|
|
246
|
+
key === 'path' || key === 'file' || key === 'target'
|
|
247
|
+
? `${v.trim()}${formatReadRangeSuffix(obj)}`
|
|
248
|
+
: v;
|
|
249
|
+
return {
|
|
250
|
+
summary: clipSingleLine(raw, max),
|
|
251
|
+
tooltip: clipSingleLine(raw, MAX_SUMMARY_TOOLTIP_CHARS),
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return { summary: null };
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Extract single-line summary and hover tooltip for web tool rows.
|
|
260
|
+
* Keeps CLI's 60-char `view.title` untouched while letting wide web rows fill
|
|
261
|
+
* available width before CSS `truncate` adds an ellipsis.
|
|
262
|
+
*/
|
|
263
|
+
export function extractWebActionSummary(action: {
|
|
264
|
+
tool: string;
|
|
265
|
+
arguments?: unknown;
|
|
266
|
+
result?: unknown;
|
|
267
|
+
view?: { card?: ActionCardKind; title?: string; declared?: boolean };
|
|
268
|
+
}): { summary: string | null; tooltip?: string } {
|
|
269
|
+
const asked = askRowSummary(action);
|
|
270
|
+
if (asked) return { summary: asked, tooltip: clipSingleLine(asked, MAX_SUMMARY_TOOLTIP_CHARS) };
|
|
271
|
+
|
|
272
|
+
const egress = egressRowSummary(action);
|
|
273
|
+
if (egress) return { summary: egress, tooltip: egress };
|
|
274
|
+
|
|
275
|
+
if (action.tool === 'delegate_subagent') {
|
|
276
|
+
const sub = summarizeDelegateSubagent(action.arguments);
|
|
277
|
+
const { task } = parseDelegateSubagent(action.arguments);
|
|
278
|
+
return {
|
|
279
|
+
summary: sub,
|
|
280
|
+
tooltip: task ? clipSingleLine(task, MAX_SUMMARY_TOOLTIP_CHARS) : undefined,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
if (isTaskFamilyTool(action.tool)) {
|
|
284
|
+
const summary = summarizeTaskAction(action.tool, action.arguments, action.result);
|
|
285
|
+
return { summary, tooltip: summary ?? undefined };
|
|
286
|
+
}
|
|
287
|
+
const runCode = summarizeRunCodeAction(action.tool, action.arguments, action.result);
|
|
288
|
+
if (runCode) return { summary: runCode, tooltip: runCode };
|
|
289
|
+
|
|
290
|
+
const web = summarizeWebAction(action.tool, action.arguments, action.result);
|
|
291
|
+
if (web) return { summary: web, tooltip: web };
|
|
292
|
+
|
|
293
|
+
const obj = asObject(action.arguments);
|
|
294
|
+
if (action.view?.declared) {
|
|
295
|
+
const card = action.view.card;
|
|
296
|
+
if (card === 'terminal' && obj) {
|
|
297
|
+
const raw =
|
|
298
|
+
stringField(obj, 'command') ??
|
|
299
|
+
stringField(obj, 'cmd') ??
|
|
300
|
+
stringField(obj, 'code') ??
|
|
301
|
+
stringField(obj, 'chars') ??
|
|
302
|
+
stringField(obj, 'scriptId');
|
|
303
|
+
if (raw) {
|
|
304
|
+
return {
|
|
305
|
+
summary: clipSingleLine(raw),
|
|
306
|
+
tooltip: clipSingleLine(raw, MAX_SUMMARY_TOOLTIP_CHARS),
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
if ((card === 'read' || card === 'diff') && obj) {
|
|
311
|
+
const rawPath =
|
|
312
|
+
stringField(obj, 'path') ?? stringField(obj, 'file') ?? stringField(obj, 'target');
|
|
313
|
+
if (rawPath) {
|
|
314
|
+
const withRange = `${rawPath}${formatReadRangeSuffix(obj)}`;
|
|
315
|
+
return {
|
|
316
|
+
summary: clipSingleLine(withRange),
|
|
317
|
+
tooltip: clipSingleLine(withRange, MAX_SUMMARY_TOOLTIP_CHARS),
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (card === 'search' && obj) {
|
|
322
|
+
const rawPattern = stringField(obj, 'pattern') ?? stringField(obj, 'query');
|
|
323
|
+
if (rawPattern) {
|
|
324
|
+
return {
|
|
325
|
+
summary: clipSingleLine(rawPattern),
|
|
326
|
+
tooltip: clipSingleLine(rawPattern, MAX_SUMMARY_TOOLTIP_CHARS),
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const declaredTitle = action.view.title?.trim() || null;
|
|
331
|
+
return { summary: declaredTitle, tooltip: declaredTitle ?? undefined };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return summarizeArgumentsWide(action.arguments);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Collapse consecutive identical `full` / `partial` sandbox pills within a
|
|
339
|
+
* contiguous tool block so repeated shell commands do not spam the same tag.
|
|
340
|
+
* Unsandboxed (`enforcement === 'none'` or unknown) is surfaced on every row.
|
|
341
|
+
*/
|
|
342
|
+
export function shouldShowSandboxBadge(
|
|
343
|
+
actions: ReadonlyArray<{ sandbox?: { backend?: string; enforcement: string } }>,
|
|
344
|
+
index: number,
|
|
345
|
+
): boolean {
|
|
346
|
+
const current = actions[index]?.sandbox;
|
|
347
|
+
if (!current) return false;
|
|
348
|
+
if (current.enforcement !== 'full' && current.enforcement !== 'partial') {
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
if (index <= 0) return true;
|
|
352
|
+
const prev = actions[index - 1]?.sandbox;
|
|
353
|
+
if (!prev) return true;
|
|
354
|
+
return prev.enforcement !== current.enforcement || prev.backend !== current.backend;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Inline sandbox pill policy inside the compact turn timeline:
|
|
359
|
+
* routine `'full'` / `'partial'` sandbox posture moves into the expanded
|
|
360
|
+
* terminal `cwd` subtitle; only unsandboxed (`enforcement === 'none'` or
|
|
361
|
+
* unrecognized) calls highlight inline.
|
|
362
|
+
*/
|
|
363
|
+
export function shouldShowInlineSandboxPill(action: {
|
|
364
|
+
sandbox?: { backend?: string; enforcement: string };
|
|
365
|
+
}): boolean {
|
|
366
|
+
if (!action.sandbox) return false;
|
|
367
|
+
return action.sandbox.enforcement !== 'full' && action.sandbox.enforcement !== 'partial';
|
|
368
|
+
}
|
|
369
|
+
|
|
129
370
|
export interface DelegateSubagentView {
|
|
130
371
|
profile: string | null;
|
|
131
372
|
task: string | null;
|
|
@@ -322,66 +563,6 @@ function stringField(obj: Record<string, unknown> | null, key: string): string |
|
|
|
322
563
|
return typeof value === 'string' && value.trim() ? value.trim() : null;
|
|
323
564
|
}
|
|
324
565
|
|
|
325
|
-
const ENVELOPE_KEYS = new Set([
|
|
326
|
-
'success',
|
|
327
|
-
'error',
|
|
328
|
-
'message',
|
|
329
|
-
'data',
|
|
330
|
-
'needsFollowup',
|
|
331
|
-
'terminal',
|
|
332
|
-
'nextAction',
|
|
333
|
-
]);
|
|
334
|
-
|
|
335
|
-
/** Sidecar `ToolResult` plus JSON 字符串;`fields` 是 data 与顶层业务字段的合并。 */
|
|
336
|
-
export interface ToolEnvelope {
|
|
337
|
-
success: boolean | null;
|
|
338
|
-
error: string | null;
|
|
339
|
-
message: string | null;
|
|
340
|
-
fields: Record<string, unknown>;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
function parseJsonValue(value: unknown): unknown {
|
|
344
|
-
if (typeof value !== 'string') return value;
|
|
345
|
-
const trimmed = value.trim();
|
|
346
|
-
if (!trimmed.startsWith('{') && !trimmed.startsWith('[')) return value;
|
|
347
|
-
try {
|
|
348
|
-
return JSON.parse(trimmed) as unknown;
|
|
349
|
-
} catch {
|
|
350
|
-
return value;
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
export function parseToolEnvelope(result: unknown): ToolEnvelope {
|
|
355
|
-
const parsed = parseJsonValue(result);
|
|
356
|
-
const obj = asObject(parsed);
|
|
357
|
-
if (!obj) {
|
|
358
|
-
return {
|
|
359
|
-
success: null,
|
|
360
|
-
error: null,
|
|
361
|
-
message: typeof parsed === 'string' && parsed.trim() ? parsed.trim() : null,
|
|
362
|
-
fields: {},
|
|
363
|
-
};
|
|
364
|
-
}
|
|
365
|
-
const data = asObject(obj.data);
|
|
366
|
-
const fields: Record<string, unknown> = {};
|
|
367
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
368
|
-
if (!ENVELOPE_KEYS.has(key) && value !== undefined) fields[key] = value;
|
|
369
|
-
}
|
|
370
|
-
if (data) {
|
|
371
|
-
for (const [key, value] of Object.entries(data)) {
|
|
372
|
-
if (key === 'durationMs' || value === undefined) continue;
|
|
373
|
-
fields[key] = value;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
const dataText = typeof obj.data === 'string' && obj.data.trim() ? obj.data.trim() : null;
|
|
377
|
-
return {
|
|
378
|
-
success: typeof obj.success === 'boolean' ? obj.success : null,
|
|
379
|
-
error: stringField(obj, 'error'),
|
|
380
|
-
message: stringField(obj, 'message') ?? dataText,
|
|
381
|
-
fields,
|
|
382
|
-
};
|
|
383
|
-
}
|
|
384
|
-
|
|
385
566
|
const DELEGATE_STATUS_LABELS: Record<string, string> = {
|
|
386
567
|
budget_exhausted: 'budget exhausted',
|
|
387
568
|
failed: 'failure',
|
|
@@ -762,3 +943,864 @@ function answerText(value: unknown): string {
|
|
|
762
943
|
.map((item) => item.trim())
|
|
763
944
|
.join(', ');
|
|
764
945
|
}
|
|
946
|
+
|
|
947
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
948
|
+
// R1 / R2 / R3 / R6: Turn Header Dedup, Rich Tool Previews & Tool Group Folding
|
|
949
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
950
|
+
|
|
951
|
+
/**
|
|
952
|
+
* Omit per-turn agent badge header in ordinary single-agent chats when the
|
|
953
|
+
* sole participant is already the active `currentAgent` and the user did not
|
|
954
|
+
* explicitly `@mention` an agent.
|
|
955
|
+
*/
|
|
956
|
+
export function shouldShowTurnAgentBadges(input: {
|
|
957
|
+
participants: ReadonlyArray<{ key: string }>;
|
|
958
|
+
currentAgent: { id: string } | null;
|
|
959
|
+
mentionedAgentIds: readonly string[];
|
|
960
|
+
}): boolean {
|
|
961
|
+
const { participants, currentAgent, mentionedAgentIds } = input;
|
|
962
|
+
if (participants.length === 0) return false;
|
|
963
|
+
if (participants.length > 1) return true;
|
|
964
|
+
if (mentionedAgentIds.length > 0) return true;
|
|
965
|
+
if (!currentAgent) return true;
|
|
966
|
+
return participants[0]?.key !== currentAgent.id;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
export interface ActionStatusInfo {
|
|
970
|
+
status: 'running' | 'succeeded' | 'failed';
|
|
971
|
+
benignNoMatch: boolean;
|
|
972
|
+
classifiedShell: ClassifiedShellAction | null;
|
|
973
|
+
terminal: TerminalExecutionView | null;
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
export function isShellActionTool(tool: string): boolean {
|
|
977
|
+
return tool === 'local_exec_shell' || tool === 'bash' || tool === 'pwsh' || tool === 'bash_session';
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
export function isActionRunning(action: {
|
|
981
|
+
tool: string;
|
|
982
|
+
arguments?: unknown;
|
|
983
|
+
result?: unknown;
|
|
984
|
+
}): boolean {
|
|
985
|
+
return action.result === undefined || action.result === null;
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
/**
|
|
989
|
+
* Interactive tool call waiting on human input (`ask_user`); must remain
|
|
990
|
+
* directly visible even if parallel sibling tools exist in the same round.
|
|
991
|
+
*/
|
|
992
|
+
export function isInteractiveRunningAction(action: {
|
|
993
|
+
tool: string;
|
|
994
|
+
arguments?: unknown;
|
|
995
|
+
result?: unknown;
|
|
996
|
+
}): boolean {
|
|
997
|
+
return action.tool === 'ask_user' && isActionRunning(action);
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
export function deriveActionStatusInfo(action: {
|
|
1001
|
+
tool: string;
|
|
1002
|
+
arguments?: unknown;
|
|
1003
|
+
result?: unknown;
|
|
1004
|
+
}): ActionStatusInfo {
|
|
1005
|
+
const isShell = isShellActionTool(action.tool);
|
|
1006
|
+
const args = asObject(action.arguments);
|
|
1007
|
+
const rawCmd = isShell ? (stringField(args, 'command') ?? stringField(args, 'cmd')) : null;
|
|
1008
|
+
const classifiedShell = rawCmd ? classifyShellCommand(rawCmd) : null;
|
|
1009
|
+
|
|
1010
|
+
if (action.result === undefined || action.result === null) {
|
|
1011
|
+
return {
|
|
1012
|
+
status: 'running',
|
|
1013
|
+
benignNoMatch: false,
|
|
1014
|
+
classifiedShell,
|
|
1015
|
+
terminal: null,
|
|
1016
|
+
};
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
const terminal =
|
|
1020
|
+
isShell || action.tool === 'local_run_snippet' || action.tool === 'local_run_script'
|
|
1021
|
+
? parseTerminalExecution(action)
|
|
1022
|
+
: null;
|
|
1023
|
+
|
|
1024
|
+
if (terminal && isBenignSearchNoMatch(classifiedShell, terminal)) {
|
|
1025
|
+
return {
|
|
1026
|
+
status: 'succeeded',
|
|
1027
|
+
benignNoMatch: true,
|
|
1028
|
+
classifiedShell,
|
|
1029
|
+
terminal,
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
if (terminal && terminal.exitCode !== null && terminal.exitCode !== 0) {
|
|
1034
|
+
return {
|
|
1035
|
+
status: 'failed',
|
|
1036
|
+
benignNoMatch: false,
|
|
1037
|
+
classifiedShell,
|
|
1038
|
+
terminal,
|
|
1039
|
+
};
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
const env = parseToolEnvelope(action.result);
|
|
1043
|
+
if (env.success === false || env.error) {
|
|
1044
|
+
return {
|
|
1045
|
+
status: 'failed',
|
|
1046
|
+
benignNoMatch: false,
|
|
1047
|
+
classifiedShell,
|
|
1048
|
+
terminal,
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
if (env.success === true) {
|
|
1052
|
+
return {
|
|
1053
|
+
status: 'succeeded',
|
|
1054
|
+
benignNoMatch: false,
|
|
1055
|
+
classifiedShell,
|
|
1056
|
+
terminal,
|
|
1057
|
+
};
|
|
1058
|
+
}
|
|
1059
|
+
if (typeof action.result === 'object' && action.result !== null && 'error' in action.result) {
|
|
1060
|
+
return {
|
|
1061
|
+
status: 'failed',
|
|
1062
|
+
benignNoMatch: false,
|
|
1063
|
+
classifiedShell,
|
|
1064
|
+
terminal,
|
|
1065
|
+
};
|
|
1066
|
+
}
|
|
1067
|
+
return {
|
|
1068
|
+
status: 'succeeded',
|
|
1069
|
+
benignNoMatch: false,
|
|
1070
|
+
classifiedShell,
|
|
1071
|
+
terminal,
|
|
1072
|
+
};
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
export interface ImageCropRegion {
|
|
1076
|
+
x: number;
|
|
1077
|
+
y: number;
|
|
1078
|
+
w: number;
|
|
1079
|
+
h: number;
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
export interface ImageActionPreview {
|
|
1083
|
+
isCapture: boolean;
|
|
1084
|
+
settled: boolean;
|
|
1085
|
+
path: string | null;
|
|
1086
|
+
displayName: string;
|
|
1087
|
+
inlineDataUrl: string | null;
|
|
1088
|
+
width: number | null;
|
|
1089
|
+
height: number | null;
|
|
1090
|
+
region: ImageCropRegion | null;
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
function fileBaseName(rawPath: string): string {
|
|
1094
|
+
const clean = rawPath.replace(/[\\/]+$/, '');
|
|
1095
|
+
const idx = Math.max(clean.lastIndexOf('/'), clean.lastIndexOf('\\'));
|
|
1096
|
+
return idx >= 0 ? clean.slice(idx + 1) : clean;
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
function parseCropRegion(raw: unknown): ImageCropRegion | null {
|
|
1100
|
+
const obj = asObject(raw);
|
|
1101
|
+
if (!obj) return null;
|
|
1102
|
+
const x = typeof obj.x === 'number' && Number.isFinite(obj.x) ? obj.x : null;
|
|
1103
|
+
const y = typeof obj.y === 'number' && Number.isFinite(obj.y) ? obj.y : null;
|
|
1104
|
+
const w =
|
|
1105
|
+
typeof obj.w === 'number' && Number.isFinite(obj.w)
|
|
1106
|
+
? obj.w
|
|
1107
|
+
: typeof obj.width === 'number' && Number.isFinite(obj.width)
|
|
1108
|
+
? obj.width
|
|
1109
|
+
: null;
|
|
1110
|
+
const h =
|
|
1111
|
+
typeof obj.h === 'number' && Number.isFinite(obj.h)
|
|
1112
|
+
? obj.h
|
|
1113
|
+
: typeof obj.height === 'number' && Number.isFinite(obj.height)
|
|
1114
|
+
? obj.height
|
|
1115
|
+
: null;
|
|
1116
|
+
if (x === null || y === null || w === null || h === null || w <= 0 || h <= 0) return null;
|
|
1117
|
+
return { x, y, w, h };
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
function extractInlineImageDataUrl(result: unknown, fields: Record<string, unknown>): string | null {
|
|
1121
|
+
const directUrl = stringField(fields, 'dataUrl') ?? stringField(fields, 'imageUrl');
|
|
1122
|
+
if (directUrl && directUrl.startsWith('data:image/')) return directUrl;
|
|
1123
|
+
const mime = stringField(fields, 'mimeType') ?? stringField(fields, 'mime');
|
|
1124
|
+
const b64 = stringField(fields, 'base64') ?? stringField(fields, 'imageBase64');
|
|
1125
|
+
if (mime && mime.startsWith('image/') && b64) {
|
|
1126
|
+
return `data:${mime};base64,${b64}`;
|
|
1127
|
+
}
|
|
1128
|
+
const contentArr = Array.isArray(fields.content)
|
|
1129
|
+
? fields.content
|
|
1130
|
+
: Array.isArray(asObject(parseJsonValue(result))?.content)
|
|
1131
|
+
? (asObject(parseJsonValue(result))?.content as unknown[])
|
|
1132
|
+
: null;
|
|
1133
|
+
if (contentArr) {
|
|
1134
|
+
for (const part of contentArr) {
|
|
1135
|
+
const p = asObject(part);
|
|
1136
|
+
if (!p) continue;
|
|
1137
|
+
if (p.type === 'image' && typeof p.data === 'string') {
|
|
1138
|
+
const partMime = stringField(p, 'mimeType') ?? 'image/png';
|
|
1139
|
+
return `data:${partMime};base64,${p.data}`;
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
return null;
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
export function parseImageAction(action: {
|
|
1147
|
+
tool: string;
|
|
1148
|
+
arguments?: unknown;
|
|
1149
|
+
result?: unknown;
|
|
1150
|
+
}): ImageActionPreview | null {
|
|
1151
|
+
const args = asObject(action.arguments);
|
|
1152
|
+
const env = parseToolEnvelope(action.result);
|
|
1153
|
+
const inlineDataUrl = extractInlineImageDataUrl(action.result, env.fields);
|
|
1154
|
+
const isImageTool = action.tool === 'view_image' || action.tool === 'capture_display';
|
|
1155
|
+
if (!isImageTool && !inlineDataUrl) return null;
|
|
1156
|
+
|
|
1157
|
+
const isCapture = action.tool === 'capture_display' || (!isImageTool && Boolean(inlineDataUrl));
|
|
1158
|
+
const settled = action.result !== undefined && action.result !== null;
|
|
1159
|
+
const argPath = stringField(args, 'path') ?? stringField(args, 'file');
|
|
1160
|
+
const resolvedSourcePath =
|
|
1161
|
+
stringField(env.fields, 'sourcePath') ??
|
|
1162
|
+
stringField(env.fields, 'path') ??
|
|
1163
|
+
stringField(env.fields, 'filePath');
|
|
1164
|
+
const rawPath = resolvedSourcePath ?? argPath;
|
|
1165
|
+
const displaySource = argPath ?? resolvedSourcePath;
|
|
1166
|
+
const displayName = displaySource
|
|
1167
|
+
? fileBaseName(displaySource)
|
|
1168
|
+
: isCapture
|
|
1169
|
+
? t('Screen')
|
|
1170
|
+
: t('Image');
|
|
1171
|
+
const width =
|
|
1172
|
+
typeof env.fields.originalWidth === 'number'
|
|
1173
|
+
? env.fields.originalWidth
|
|
1174
|
+
: typeof env.fields.width === 'number'
|
|
1175
|
+
? env.fields.width
|
|
1176
|
+
: null;
|
|
1177
|
+
const height =
|
|
1178
|
+
typeof env.fields.originalHeight === 'number'
|
|
1179
|
+
? env.fields.originalHeight
|
|
1180
|
+
: typeof env.fields.height === 'number'
|
|
1181
|
+
? env.fields.height
|
|
1182
|
+
: null;
|
|
1183
|
+
const region = parseCropRegion(args?.region ?? args?.crop);
|
|
1184
|
+
|
|
1185
|
+
return {
|
|
1186
|
+
isCapture,
|
|
1187
|
+
settled,
|
|
1188
|
+
path: rawPath,
|
|
1189
|
+
displayName,
|
|
1190
|
+
inlineDataUrl,
|
|
1191
|
+
width,
|
|
1192
|
+
height,
|
|
1193
|
+
region,
|
|
1194
|
+
};
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
export interface WebSearchResultItem {
|
|
1198
|
+
title: string;
|
|
1199
|
+
url: string;
|
|
1200
|
+
host: string;
|
|
1201
|
+
snippet: string | null;
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
export interface WebSearchActionView {
|
|
1205
|
+
query: string;
|
|
1206
|
+
resultCount: number | null;
|
|
1207
|
+
results: WebSearchResultItem[];
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
function hostFromUrl(rawUrl: string): string {
|
|
1211
|
+
try {
|
|
1212
|
+
return new URL(rawUrl).hostname.replace(/^www\./, '');
|
|
1213
|
+
} catch {
|
|
1214
|
+
return rawUrl;
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
export function parseWebSearchAction(action: {
|
|
1219
|
+
tool: string;
|
|
1220
|
+
arguments?: unknown;
|
|
1221
|
+
result?: unknown;
|
|
1222
|
+
}): WebSearchActionView | null {
|
|
1223
|
+
if (action.tool !== 'web_search') return null;
|
|
1224
|
+
const args = asObject(action.arguments);
|
|
1225
|
+
const query = stringField(args, 'query') ?? stringField(args, 'q') ?? '';
|
|
1226
|
+
const env = parseToolEnvelope(action.result);
|
|
1227
|
+
const rawList = Array.isArray(env.fields.results)
|
|
1228
|
+
? env.fields.results
|
|
1229
|
+
: Array.isArray(env.fields.items)
|
|
1230
|
+
? env.fields.items
|
|
1231
|
+
: Array.isArray(env.fields.sources)
|
|
1232
|
+
? env.fields.sources
|
|
1233
|
+
: [];
|
|
1234
|
+
const results: WebSearchResultItem[] = [];
|
|
1235
|
+
for (const row of rawList) {
|
|
1236
|
+
const obj = asObject(row);
|
|
1237
|
+
if (!obj) continue;
|
|
1238
|
+
const url = stringField(obj, 'url') ?? stringField(obj, 'link');
|
|
1239
|
+
if (!url) continue;
|
|
1240
|
+
const title = stringField(obj, 'title') ?? shortUrl(url) ?? url;
|
|
1241
|
+
const snippet =
|
|
1242
|
+
stringField(obj, 'snippet') ??
|
|
1243
|
+
stringField(obj, 'description') ??
|
|
1244
|
+
stringField(obj, 'summary');
|
|
1245
|
+
results.push({
|
|
1246
|
+
title,
|
|
1247
|
+
url,
|
|
1248
|
+
host: hostFromUrl(url),
|
|
1249
|
+
snippet,
|
|
1250
|
+
});
|
|
1251
|
+
}
|
|
1252
|
+
const explicitCount =
|
|
1253
|
+
typeof env.fields.result_count === 'number'
|
|
1254
|
+
? env.fields.result_count
|
|
1255
|
+
: typeof env.fields.resultCount === 'number'
|
|
1256
|
+
? env.fields.resultCount
|
|
1257
|
+
: results.length > 0
|
|
1258
|
+
? results.length
|
|
1259
|
+
: null;
|
|
1260
|
+
return {
|
|
1261
|
+
query,
|
|
1262
|
+
resultCount: explicitCount,
|
|
1263
|
+
results,
|
|
1264
|
+
};
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
export interface WebFetchActionView {
|
|
1268
|
+
url: string | null;
|
|
1269
|
+
host: string | null;
|
|
1270
|
+
displayTarget: string;
|
|
1271
|
+
title: string | null;
|
|
1272
|
+
status: number | null;
|
|
1273
|
+
sizeLabel: string | null;
|
|
1274
|
+
excerpt: string | null;
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
export function parseWebFetchAction(action: {
|
|
1278
|
+
tool: string;
|
|
1279
|
+
arguments?: unknown;
|
|
1280
|
+
result?: unknown;
|
|
1281
|
+
}): WebFetchActionView | null {
|
|
1282
|
+
if (action.tool !== 'web_fetch') return null;
|
|
1283
|
+
const args = asObject(action.arguments);
|
|
1284
|
+
const env = parseToolEnvelope(action.result);
|
|
1285
|
+
const url = stringField(args, 'url') ?? stringField(env.fields, 'url');
|
|
1286
|
+
const host = url ? hostFromUrl(url) : null;
|
|
1287
|
+
const title = stringField(env.fields, 'title');
|
|
1288
|
+
const displayTarget = title
|
|
1289
|
+
? host
|
|
1290
|
+
? `${clipSingleLine(title, 90)} (${host})`
|
|
1291
|
+
: clipSingleLine(title, 100)
|
|
1292
|
+
: shortUrl(url) ?? t('unknown address');
|
|
1293
|
+
const status = typeof env.fields.status === 'number' ? env.fields.status : null;
|
|
1294
|
+
const sizeLabel = formatBytes(env.fields.bytes);
|
|
1295
|
+
const excerpt =
|
|
1296
|
+
stringField(env.fields, 'content') ??
|
|
1297
|
+
stringField(env.fields, 'markdown') ??
|
|
1298
|
+
stringField(env.fields, 'text') ??
|
|
1299
|
+
(typeof action.result === 'string' && !action.result.trimStart().startsWith('{')
|
|
1300
|
+
? action.result
|
|
1301
|
+
: env.message);
|
|
1302
|
+
|
|
1303
|
+
return {
|
|
1304
|
+
url,
|
|
1305
|
+
host,
|
|
1306
|
+
displayTarget,
|
|
1307
|
+
title,
|
|
1308
|
+
status,
|
|
1309
|
+
sizeLabel,
|
|
1310
|
+
excerpt,
|
|
1311
|
+
};
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
export interface TodoItemView {
|
|
1315
|
+
id: string;
|
|
1316
|
+
content: string;
|
|
1317
|
+
status: 'pending' | 'in_progress' | 'completed' | 'cancelled';
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
export interface TodoWriteActionView {
|
|
1321
|
+
items: TodoItemView[];
|
|
1322
|
+
completedCount: number;
|
|
1323
|
+
totalCount: number;
|
|
1324
|
+
activeContent: string | null;
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
export function parseTodoWriteAction(action: {
|
|
1328
|
+
tool: string;
|
|
1329
|
+
arguments?: unknown;
|
|
1330
|
+
result?: unknown;
|
|
1331
|
+
}): TodoWriteActionView | null {
|
|
1332
|
+
if (action.tool !== 'todo_write' && action.tool !== 'update_plan') return null;
|
|
1333
|
+
const args = asObject(action.arguments);
|
|
1334
|
+
const env = parseToolEnvelope(action.result);
|
|
1335
|
+
const rawTodos = Array.isArray(args?.todos)
|
|
1336
|
+
? args.todos
|
|
1337
|
+
: Array.isArray(args?.plan)
|
|
1338
|
+
? args.plan
|
|
1339
|
+
: Array.isArray(env.fields.todos)
|
|
1340
|
+
? env.fields.todos
|
|
1341
|
+
: null;
|
|
1342
|
+
if (!rawTodos) return null;
|
|
1343
|
+
const items: TodoItemView[] = [];
|
|
1344
|
+
rawTodos.forEach((raw, idx) => {
|
|
1345
|
+
const row = asObject(raw);
|
|
1346
|
+
if (!row) return;
|
|
1347
|
+
const content = stringField(row, 'content') ?? stringField(row, 'step') ?? stringField(row, 'title');
|
|
1348
|
+
if (!content) return;
|
|
1349
|
+
const rawStatus = stringField(row, 'status') ?? 'pending';
|
|
1350
|
+
const status: TodoItemView['status'] =
|
|
1351
|
+
rawStatus === 'completed' || rawStatus === 'done'
|
|
1352
|
+
? 'completed'
|
|
1353
|
+
: rawStatus === 'in_progress' || rawStatus === 'active'
|
|
1354
|
+
? 'in_progress'
|
|
1355
|
+
: rawStatus === 'cancelled'
|
|
1356
|
+
? 'cancelled'
|
|
1357
|
+
: 'pending';
|
|
1358
|
+
items.push({
|
|
1359
|
+
id: stringField(row, 'id') ?? `todo-${idx}`,
|
|
1360
|
+
content,
|
|
1361
|
+
status,
|
|
1362
|
+
});
|
|
1363
|
+
});
|
|
1364
|
+
if (items.length === 0) return null;
|
|
1365
|
+
const completedCount = items.filter((item) => item.status === 'completed').length;
|
|
1366
|
+
const activeContent = items.find((item) => item.status === 'in_progress')?.content ?? null;
|
|
1367
|
+
return {
|
|
1368
|
+
items,
|
|
1369
|
+
completedCount,
|
|
1370
|
+
totalCount: items.length,
|
|
1371
|
+
activeContent,
|
|
1372
|
+
};
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
/** Concise `Question → Answer` summary for human-answered `ask_user` rows, masking secret answers. */
|
|
1376
|
+
export function askInlineSummary(action: {
|
|
1377
|
+
tool?: string;
|
|
1378
|
+
arguments?: unknown;
|
|
1379
|
+
result?: unknown;
|
|
1380
|
+
}): string {
|
|
1381
|
+
if (action.tool !== 'ask_user') return '';
|
|
1382
|
+
const unwrapped = unwrapAsk(action.result);
|
|
1383
|
+
if (!unwrapped || unwrapped.unattended === true) return askRowSummary(action);
|
|
1384
|
+
for (const q of unwrapped.questions) {
|
|
1385
|
+
const row = asObject(q);
|
|
1386
|
+
if (!row) continue;
|
|
1387
|
+
const text = stringField(row, 'text');
|
|
1388
|
+
if (!text) continue;
|
|
1389
|
+
const id = stringField(row, 'id');
|
|
1390
|
+
const isSecret = row.secret === true || row.isSecret === true || (id ? /secret|password|token|key/i.test(id) : false);
|
|
1391
|
+
const rawAnswer = id ? answerText(unwrapped.answers[id]) : '';
|
|
1392
|
+
if (!rawAnswer) return clipSingleLine(text, 120);
|
|
1393
|
+
const shownAnswer = isSecret ? '••••••' : clipSingleLine(rawAnswer, 80);
|
|
1394
|
+
return `${clipSingleLine(text, 80)} → ${shownAnswer}`;
|
|
1395
|
+
}
|
|
1396
|
+
return askRowSummary(action);
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
export interface MemoryActionView {
|
|
1400
|
+
kind: 'search' | 'read' | 'save';
|
|
1401
|
+
summary: string;
|
|
1402
|
+
scope: string | null;
|
|
1403
|
+
detail: string | null;
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
export function parseMemoryAction(action: {
|
|
1407
|
+
tool: string;
|
|
1408
|
+
arguments?: unknown;
|
|
1409
|
+
result?: unknown;
|
|
1410
|
+
}): MemoryActionView | null {
|
|
1411
|
+
if (!action.tool.startsWith('memory_')) return null;
|
|
1412
|
+
const args = asObject(action.arguments);
|
|
1413
|
+
const env = parseToolEnvelope(action.result);
|
|
1414
|
+
const scope = stringField(args, 'scope') ?? stringField(env.fields, 'scope');
|
|
1415
|
+
|
|
1416
|
+
if (action.tool === 'memory_search') {
|
|
1417
|
+
const query = stringField(args, 'query') ?? stringField(args, 'q') ?? t('All memories');
|
|
1418
|
+
return {
|
|
1419
|
+
kind: 'search',
|
|
1420
|
+
summary: clipSingleLine(query, 120),
|
|
1421
|
+
scope,
|
|
1422
|
+
detail: env.message,
|
|
1423
|
+
};
|
|
1424
|
+
}
|
|
1425
|
+
if (action.tool === 'memory_read' || action.tool === 'memory_get' || action.tool === 'memory_list') {
|
|
1426
|
+
const target =
|
|
1427
|
+
stringField(args, 'key') ??
|
|
1428
|
+
stringField(args, 'topic') ??
|
|
1429
|
+
stringField(args, 'path') ??
|
|
1430
|
+
stringField(args, 'id') ??
|
|
1431
|
+
scope ??
|
|
1432
|
+
t('Workspace memory');
|
|
1433
|
+
return {
|
|
1434
|
+
kind: 'read',
|
|
1435
|
+
summary: clipSingleLine(target, 120),
|
|
1436
|
+
scope,
|
|
1437
|
+
detail: stringField(env.fields, 'content') ?? env.message,
|
|
1438
|
+
};
|
|
1439
|
+
}
|
|
1440
|
+
const content =
|
|
1441
|
+
stringField(args, 'content') ??
|
|
1442
|
+
stringField(args, 'fact') ??
|
|
1443
|
+
stringField(args, 'memory') ??
|
|
1444
|
+
stringField(args, 'summary') ??
|
|
1445
|
+
stringField(args, 'key') ??
|
|
1446
|
+
env.message ??
|
|
1447
|
+
'';
|
|
1448
|
+
return {
|
|
1449
|
+
kind: 'save',
|
|
1450
|
+
summary: clipSingleLine(content, 120),
|
|
1451
|
+
scope,
|
|
1452
|
+
detail: content || null,
|
|
1453
|
+
};
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
function extractPresentFilePaths(args: unknown): string[] {
|
|
1457
|
+
const obj = asObject(args);
|
|
1458
|
+
if (!obj) return [];
|
|
1459
|
+
const rawList = Array.isArray(obj.files)
|
|
1460
|
+
? obj.files
|
|
1461
|
+
: Array.isArray(obj.paths)
|
|
1462
|
+
? obj.paths
|
|
1463
|
+
: typeof obj.path === 'string'
|
|
1464
|
+
? [obj.path]
|
|
1465
|
+
: [];
|
|
1466
|
+
const out: string[] = [];
|
|
1467
|
+
for (const item of rawList) {
|
|
1468
|
+
if (typeof item === 'string' && item.trim()) {
|
|
1469
|
+
out.push(item.trim());
|
|
1470
|
+
} else {
|
|
1471
|
+
const row = asObject(item);
|
|
1472
|
+
const p = stringField(row, 'path') ?? stringField(row, 'file');
|
|
1473
|
+
if (p) out.push(p);
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
return out;
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
/**
|
|
1480
|
+
* Filter out settled, succeeded `present_files` actions once the turn has
|
|
1481
|
+
* finished and `TurnFilesCard` already presents those deliverables below the answer.
|
|
1482
|
+
*/
|
|
1483
|
+
export function filterRedundantPresentFiles<T extends ExecutedActionLike>(
|
|
1484
|
+
actions: readonly T[],
|
|
1485
|
+
deliveredFilePaths?: ReadonlySet<string> | null,
|
|
1486
|
+
): T[] {
|
|
1487
|
+
if (!deliveredFilePaths || deliveredFilePaths.size === 0) return [...actions];
|
|
1488
|
+
const deliveredBaseNames = new Set(Array.from(deliveredFilePaths, (p) => fileBaseName(p)));
|
|
1489
|
+
return actions.filter((action) => {
|
|
1490
|
+
if (action.tool !== 'present_files') return true;
|
|
1491
|
+
const statusInfo = deriveActionStatusInfo(action);
|
|
1492
|
+
if (statusInfo.status !== 'succeeded') return true;
|
|
1493
|
+
const requested = extractPresentFilePaths(action.arguments);
|
|
1494
|
+
if (requested.length === 0) return false;
|
|
1495
|
+
return !requested.every(
|
|
1496
|
+
(p) => deliveredFilePaths.has(p) || deliveredBaseNames.has(fileBaseName(p)),
|
|
1497
|
+
);
|
|
1498
|
+
});
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
export type DisplayActionRow<T extends ExecutedActionLike = ExecutedActionLike> =
|
|
1502
|
+
| { kind: 'single'; action: T; originalIndex: number }
|
|
1503
|
+
| { kind: 'merged-read'; actions: T[]; files: string[]; firstIndex: number };
|
|
1504
|
+
|
|
1505
|
+
export type TimelineToolSegment<T extends ExecutedActionLike = ExecutedActionLike> =
|
|
1506
|
+
| { kind: 'item'; row: DisplayActionRow<T> }
|
|
1507
|
+
| {
|
|
1508
|
+
kind: 'fold-group';
|
|
1509
|
+
summary: string;
|
|
1510
|
+
isSearchOnly: boolean;
|
|
1511
|
+
rows: DisplayActionRow<T>[];
|
|
1512
|
+
rawCount: number;
|
|
1513
|
+
};
|
|
1514
|
+
|
|
1515
|
+
function plainReadFilesOf(action: ExecutedActionLike): string[] | null {
|
|
1516
|
+
const statusInfo = deriveActionStatusInfo(action);
|
|
1517
|
+
if (statusInfo.status !== 'succeeded') return null;
|
|
1518
|
+
if (shouldShowInlineSandboxPill(action) || Boolean(action.approval)) return null;
|
|
1519
|
+
if (extractSkillInvocation(action) !== null) return null;
|
|
1520
|
+
if (parseImageAction(action) !== null) return null;
|
|
1521
|
+
|
|
1522
|
+
if (action.tool === 'local_read_file' || action.tool === 'read_file') {
|
|
1523
|
+
const args = asObject(action.arguments);
|
|
1524
|
+
const rawPath = stringField(args, 'path') ?? stringField(args, 'file') ?? stringField(args, 'target');
|
|
1525
|
+
if (!rawPath) return null;
|
|
1526
|
+
return [`${rawPath}${formatReadRangeSuffix(args ?? {})}`];
|
|
1527
|
+
}
|
|
1528
|
+
if (statusInfo.classifiedShell?.kind === 'read') {
|
|
1529
|
+
return statusInfo.classifiedShell.files;
|
|
1530
|
+
}
|
|
1531
|
+
return null;
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
/** Merge consecutive succeeded plain-file reads into a single `Read a, b, c` row. */
|
|
1535
|
+
export function mergeConsecutiveReadRows<T extends ExecutedActionLike>(
|
|
1536
|
+
actions: readonly T[],
|
|
1537
|
+
): DisplayActionRow<T>[] {
|
|
1538
|
+
const rows: DisplayActionRow<T>[] = [];
|
|
1539
|
+
let i = 0;
|
|
1540
|
+
while (i < actions.length) {
|
|
1541
|
+
const current = actions[i]!;
|
|
1542
|
+
const firstFiles = plainReadFilesOf(current);
|
|
1543
|
+
if (!firstFiles) {
|
|
1544
|
+
rows.push({ kind: 'single', action: current, originalIndex: i });
|
|
1545
|
+
i += 1;
|
|
1546
|
+
continue;
|
|
1547
|
+
}
|
|
1548
|
+
const batchActions: T[] = [current];
|
|
1549
|
+
const batchFiles: string[] = [...firstFiles];
|
|
1550
|
+
let j = i + 1;
|
|
1551
|
+
while (j < actions.length) {
|
|
1552
|
+
const next = actions[j]!;
|
|
1553
|
+
const nextFiles = plainReadFilesOf(next);
|
|
1554
|
+
if (!nextFiles) break;
|
|
1555
|
+
batchActions.push(next);
|
|
1556
|
+
for (const f of nextFiles) {
|
|
1557
|
+
if (!batchFiles.includes(f)) batchFiles.push(f);
|
|
1558
|
+
}
|
|
1559
|
+
j += 1;
|
|
1560
|
+
}
|
|
1561
|
+
if (batchActions.length >= 2) {
|
|
1562
|
+
rows.push({
|
|
1563
|
+
kind: 'merged-read',
|
|
1564
|
+
actions: batchActions,
|
|
1565
|
+
files: batchFiles,
|
|
1566
|
+
firstIndex: i,
|
|
1567
|
+
});
|
|
1568
|
+
} else {
|
|
1569
|
+
rows.push({ kind: 'single', action: current, originalIndex: i });
|
|
1570
|
+
}
|
|
1571
|
+
i = j;
|
|
1572
|
+
}
|
|
1573
|
+
return rows;
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
export type FoldableRoundActionKind = 'explore' | 'search' | 'run' | 'image';
|
|
1577
|
+
|
|
1578
|
+
/**
|
|
1579
|
+
* True when an action is a settled, terminal housekeeping step (`present_files`,
|
|
1580
|
+
* or `todo_write` / `update_plan` with all items completed/cancelled) that
|
|
1581
|
+
* models frequently emit alongside the final conclusion in the closing round.
|
|
1582
|
+
*/
|
|
1583
|
+
export function isTerminalHousekeepingAction(action: ExecutedActionLike): boolean {
|
|
1584
|
+
if (deriveActionStatusInfo(action).status !== 'succeeded') return false;
|
|
1585
|
+
if (action.tool === 'present_files') return true;
|
|
1586
|
+
if (action.tool === 'todo_write' || action.tool === 'update_plan') {
|
|
1587
|
+
const todo = parseTodoWriteAction(action);
|
|
1588
|
+
return (
|
|
1589
|
+
todo !== null &&
|
|
1590
|
+
todo.items.every((item) => item.status === 'completed' || item.status === 'cancelled')
|
|
1591
|
+
);
|
|
1592
|
+
}
|
|
1593
|
+
return false;
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
function isDeniedApproval(approval: ExecutedActionLike['approval']): boolean {
|
|
1597
|
+
if (!approval?.kind) return false;
|
|
1598
|
+
return approval.kind === 'deny' || approval.kind === 'declined' || approval.kind === 'reject';
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
/**
|
|
1602
|
+
* Classify a settled tool action for cross-round timeline folding.
|
|
1603
|
+
* Returns `null` for high-value, failed, running, unsandboxed, or declined actions
|
|
1604
|
+
* so they remain directly visible at top level.
|
|
1605
|
+
*/
|
|
1606
|
+
export function classifyFoldableRoundAction(
|
|
1607
|
+
action: ExecutedActionLike,
|
|
1608
|
+
): FoldableRoundActionKind | null {
|
|
1609
|
+
const statusInfo = deriveActionStatusInfo(action);
|
|
1610
|
+
if (statusInfo.status !== 'succeeded') return null;
|
|
1611
|
+
if (shouldShowInlineSandboxPill(action) || isDeniedApproval(action.approval)) return null;
|
|
1612
|
+
if (action.tool === 'run_code') return null;
|
|
1613
|
+
if (action.tool === 'delegate_subagent' || isTaskFamilyTool(action.tool)) return null;
|
|
1614
|
+
if (action.tool === 'ask_user') return null;
|
|
1615
|
+
if (extractSkillInvocation(action) !== null) return null;
|
|
1616
|
+
if (extractFileChangePreview(action) !== null) return null;
|
|
1617
|
+
if (parseTodoWriteAction(action) !== null) return null;
|
|
1618
|
+
if (parseMemoryAction(action) !== null) return null;
|
|
1619
|
+
if (action.tool === 'web_search' || action.tool === 'web_fetch') return null;
|
|
1620
|
+
if (parseImageAction(action) !== null) {
|
|
1621
|
+
return action.tool === 'view_image' ? 'image' : null;
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
if (statusInfo.classifiedShell) {
|
|
1625
|
+
if (statusInfo.classifiedShell.kind === 'search') return 'search';
|
|
1626
|
+
if (statusInfo.classifiedShell.kind === 'list' || statusInfo.classifiedShell.kind === 'read') {
|
|
1627
|
+
return 'explore';
|
|
1628
|
+
}
|
|
1629
|
+
return 'run';
|
|
1630
|
+
}
|
|
1631
|
+
const card = resolveActionCard(action);
|
|
1632
|
+
if (card === 'search') return 'search';
|
|
1633
|
+
if (card === 'read') return 'explore';
|
|
1634
|
+
return 'run';
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
export function summarizeFoldedRoundKinds(
|
|
1638
|
+
kinds: readonly FoldableRoundActionKind[],
|
|
1639
|
+
): { summary: string; isSearchOnly: boolean } {
|
|
1640
|
+
if (kinds.length > 0 && kinds.every((kind) => kind === 'image')) {
|
|
1641
|
+
return {
|
|
1642
|
+
summary: t('Viewed {count} images', { count: kinds.length }),
|
|
1643
|
+
isSearchOnly: false,
|
|
1644
|
+
};
|
|
1645
|
+
}
|
|
1646
|
+
let exploreCount = 0;
|
|
1647
|
+
let searchCount = 0;
|
|
1648
|
+
let runCount = 0;
|
|
1649
|
+
for (const kind of kinds) {
|
|
1650
|
+
if (kind === 'search') {
|
|
1651
|
+
exploreCount += 1;
|
|
1652
|
+
searchCount += 1;
|
|
1653
|
+
} else if (kind === 'explore') {
|
|
1654
|
+
exploreCount += 1;
|
|
1655
|
+
} else if (kind === 'run') {
|
|
1656
|
+
runCount += 1;
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
const isSearchOnly = exploreCount > 0 && searchCount === exploreCount && runCount === 0;
|
|
1660
|
+
if (exploreCount > 0 && runCount === 0) {
|
|
1661
|
+
return { summary: t('Explored files'), isSearchOnly };
|
|
1662
|
+
}
|
|
1663
|
+
if (runCount > 0 && exploreCount === 0) {
|
|
1664
|
+
return {
|
|
1665
|
+
summary:
|
|
1666
|
+
runCount === 1
|
|
1667
|
+
? t('Ran 1 command')
|
|
1668
|
+
: t('Ran {count} commands', { count: runCount }),
|
|
1669
|
+
isSearchOnly: false,
|
|
1670
|
+
};
|
|
1671
|
+
}
|
|
1672
|
+
return {
|
|
1673
|
+
summary:
|
|
1674
|
+
runCount === 1
|
|
1675
|
+
? t('Explored files · ran 1 command')
|
|
1676
|
+
: t('Explored files · ran {count} commands', { count: runCount }),
|
|
1677
|
+
isSearchOnly: false,
|
|
1678
|
+
};
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1681
|
+
function classifyRoutineBucket(action: ExecutedActionLike): 'explore' | 'run' | 'high-value' {
|
|
1682
|
+
const foldable = classifyFoldableRoundAction(action);
|
|
1683
|
+
if (foldable === 'explore' || foldable === 'search') return 'explore';
|
|
1684
|
+
if (foldable === 'run') return 'run';
|
|
1685
|
+
return 'high-value';
|
|
1686
|
+
}
|
|
1687
|
+
|
|
1688
|
+
export function summarizeRoutineToolRows<T extends ExecutedActionLike>(
|
|
1689
|
+
rows: readonly DisplayActionRow<T>[],
|
|
1690
|
+
): { summary: string; isSearchOnly: boolean; rawCount: number } {
|
|
1691
|
+
let exploreCount = 0;
|
|
1692
|
+
let searchCount = 0;
|
|
1693
|
+
let runCount = 0;
|
|
1694
|
+
let rawCount = 0;
|
|
1695
|
+
|
|
1696
|
+
const inspectOne = (action: T) => {
|
|
1697
|
+
rawCount += 1;
|
|
1698
|
+
const statusInfo = deriveActionStatusInfo(action);
|
|
1699
|
+
if (statusInfo.classifiedShell) {
|
|
1700
|
+
if (statusInfo.classifiedShell.kind === 'search') {
|
|
1701
|
+
exploreCount += 1;
|
|
1702
|
+
searchCount += 1;
|
|
1703
|
+
return;
|
|
1704
|
+
}
|
|
1705
|
+
if (statusInfo.classifiedShell.kind === 'list' || statusInfo.classifiedShell.kind === 'read') {
|
|
1706
|
+
exploreCount += 1;
|
|
1707
|
+
return;
|
|
1708
|
+
}
|
|
1709
|
+
runCount += 1;
|
|
1710
|
+
return;
|
|
1711
|
+
}
|
|
1712
|
+
const card = resolveActionCard(action);
|
|
1713
|
+
if (card === 'search') {
|
|
1714
|
+
exploreCount += 1;
|
|
1715
|
+
searchCount += 1;
|
|
1716
|
+
} else if (card === 'read') {
|
|
1717
|
+
exploreCount += 1;
|
|
1718
|
+
} else {
|
|
1719
|
+
runCount += 1;
|
|
1720
|
+
}
|
|
1721
|
+
};
|
|
1722
|
+
|
|
1723
|
+
for (const row of rows) {
|
|
1724
|
+
if (row.kind === 'merged-read') {
|
|
1725
|
+
for (const act of row.actions) inspectOne(act);
|
|
1726
|
+
} else {
|
|
1727
|
+
inspectOne(row.action);
|
|
1728
|
+
}
|
|
1729
|
+
}
|
|
1730
|
+
|
|
1731
|
+
const isSearchOnly = exploreCount > 0 && searchCount === exploreCount && runCount === 0;
|
|
1732
|
+
if (exploreCount > 0 && runCount === 0) {
|
|
1733
|
+
return { summary: t('Explored files'), isSearchOnly, rawCount };
|
|
1734
|
+
}
|
|
1735
|
+
if (runCount > 0 && exploreCount === 0) {
|
|
1736
|
+
return {
|
|
1737
|
+
summary:
|
|
1738
|
+
runCount === 1
|
|
1739
|
+
? t('Ran 1 command')
|
|
1740
|
+
: t('Ran {count} commands', { count: runCount }),
|
|
1741
|
+
isSearchOnly: false,
|
|
1742
|
+
rawCount,
|
|
1743
|
+
};
|
|
1744
|
+
}
|
|
1745
|
+
return {
|
|
1746
|
+
summary:
|
|
1747
|
+
runCount === 1
|
|
1748
|
+
? t('Explored files · ran 1 command')
|
|
1749
|
+
: t('Explored files · ran {count} commands', { count: runCount }),
|
|
1750
|
+
isSearchOnly: false,
|
|
1751
|
+
rawCount,
|
|
1752
|
+
};
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
/**
|
|
1756
|
+
* Segment a timeline `tools` block into second-level collapsible `fold-group`s
|
|
1757
|
+
* (for ≥ 2 contiguous routine explore/run calls) while keeping high-value
|
|
1758
|
+
* artifacts (`view_image`, `skill`, `diff`, `todo_write`, `web_*`, `delegate_subagent`,
|
|
1759
|
+
* unsandboxed, running, or failed rows) directly visible at L1.
|
|
1760
|
+
*/
|
|
1761
|
+
export function segmentTimelineToolRows<T extends ExecutedActionLike>(
|
|
1762
|
+
actions: readonly T[],
|
|
1763
|
+
): TimelineToolSegment<T>[] {
|
|
1764
|
+
const merged = mergeConsecutiveReadRows(actions);
|
|
1765
|
+
const segments: TimelineToolSegment<T>[] = [];
|
|
1766
|
+
let routineBuffer: DisplayActionRow<T>[] = [];
|
|
1767
|
+
let routineRawCount = 0;
|
|
1768
|
+
|
|
1769
|
+
const flushRoutine = () => {
|
|
1770
|
+
if (routineBuffer.length === 0) return;
|
|
1771
|
+
if (routineRawCount >= 2) {
|
|
1772
|
+
const { summary, isSearchOnly, rawCount } = summarizeRoutineToolRows(routineBuffer);
|
|
1773
|
+
segments.push({
|
|
1774
|
+
kind: 'fold-group',
|
|
1775
|
+
summary,
|
|
1776
|
+
isSearchOnly,
|
|
1777
|
+
rows: routineBuffer,
|
|
1778
|
+
rawCount,
|
|
1779
|
+
});
|
|
1780
|
+
} else {
|
|
1781
|
+
for (const row of routineBuffer) {
|
|
1782
|
+
segments.push({ kind: 'item', row });
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
routineBuffer = [];
|
|
1786
|
+
routineRawCount = 0;
|
|
1787
|
+
};
|
|
1788
|
+
|
|
1789
|
+
for (const row of merged) {
|
|
1790
|
+
if (row.kind === 'merged-read') {
|
|
1791
|
+
routineBuffer.push(row);
|
|
1792
|
+
routineRawCount += row.actions.length;
|
|
1793
|
+
continue;
|
|
1794
|
+
}
|
|
1795
|
+
const bucket = classifyRoutineBucket(row.action);
|
|
1796
|
+
if (bucket === 'high-value') {
|
|
1797
|
+
flushRoutine();
|
|
1798
|
+
segments.push({ kind: 'item', row });
|
|
1799
|
+
} else {
|
|
1800
|
+
routineBuffer.push(row);
|
|
1801
|
+
routineRawCount += 1;
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
flushRoutine();
|
|
1805
|
+
return segments;
|
|
1806
|
+
}
|