forked from wesm/agentsview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolBlock.svelte
More file actions
486 lines (446 loc) · 14 KB
/
ToolBlock.svelte
File metadata and controls
486 lines (446 loc) · 14 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
<!-- ABOUTME: Renders a collapsible tool call block with metadata tags and content. -->
<!-- ABOUTME: Supports Task tool calls with inline subagent conversation expansion. -->
<script lang="ts">
import type { ToolCall } from "../../api/types.js";
import SubagentInline from "./SubagentInline.svelte";
import {
extractToolParamMeta,
generateFallbackContent,
} from "../../utils/tool-params.js";
import { applyHighlight, escapeHTML } from "../../utils/highlight.js";
interface Props {
content: string;
label?: string;
toolCall?: ToolCall;
highlightQuery?: string;
isCurrentHighlight?: boolean;
}
let { content, label, toolCall, highlightQuery = "", isCurrentHighlight = false }: Props = $props();
let userCollapsed: boolean = $state(true);
let userOutputCollapsed: boolean = $state(true);
let userHistoryCollapsed: boolean = $state(true);
let userOverride: boolean = $state(false);
let userOutputOverride: boolean = $state(false);
let userHistoryOverride: boolean = $state(false);
let searchExpandedInput: boolean = $state(false);
let searchExpandedOutput: boolean = $state(false);
let searchExpandedHistory: boolean = $state(false);
let prevQuery: string = "";
// Auto-expand when a search match exists in input or output
// content. Only reset user overrides when the query itself
// changes, not when content updates (e.g. during streaming).
$effect(() => {
const hq = highlightQuery;
if (!hq.trim()) {
searchExpandedInput = false;
searchExpandedOutput = false;
prevQuery = hq;
return;
}
const q = hq.toLowerCase();
const inputText = (
taskPrompt ?? content ?? fallbackContent ?? ""
).toLowerCase();
const historyText = (
toolCall?.result_events?.map((event) => event.content).join("\n\n") ?? ""
).toLowerCase();
const outputText = (
[toolCall?.result_content ?? "", historyText].filter(Boolean).join("\n\n")
).toLowerCase();
searchExpandedInput = inputText.includes(q);
searchExpandedOutput = outputText.includes(q);
searchExpandedHistory = historyText.includes(q);
if (hq !== prevQuery) {
userOverride = false;
userOutputOverride = false;
userHistoryOverride = false;
prevQuery = hq;
}
});
let collapsed = $derived(
userOverride ? userCollapsed
: (searchExpandedInput || searchExpandedOutput) ? false
: userCollapsed,
);
let outputCollapsed = $derived(
userOutputOverride ? userOutputCollapsed
: searchExpandedOutput ? false
: userOutputCollapsed,
);
let historyCollapsed = $derived(
userHistoryOverride ? userHistoryCollapsed
: searchExpandedHistory ? false
: userHistoryCollapsed,
);
let outputPreviewLine = $derived.by(() => {
const rc = toolCall?.result_content;
if (!rc) return "";
const nl = rc.indexOf("\n");
return (nl === -1 ? rc : rc.slice(0, nl)).slice(0, 100);
});
let resultEvents = $derived(toolCall?.result_events ?? []);
let historyPreviewLine = $derived.by(() => {
const last = resultEvents[resultEvents.length - 1];
if (!last) return "";
return `${last.status}: ${last.content.split("\n")[0]}`.slice(0, 100);
});
/** Parsed input parameters from structured tool call data */
let inputParams = $derived.by(() => {
if (!toolCall?.input_json) return null;
try {
return JSON.parse(toolCall.input_json);
} catch {
return null;
}
});
let previewLine = $derived.by(() => {
const line = content.split("\n")[0]?.slice(0, 100) ?? "";
if (line) return line;
// For Edit/Write/Read with no content, show file path as preview
const filePath =
inputParams?.file_path ?? inputParams?.path ?? inputParams?.filePath;
if (filePath) return String(filePath).slice(0, 100);
// For glob/search tools, show pattern
if (inputParams?.pattern) return String(inputParams.pattern).slice(0, 100);
return "";
});
/** For Task tool calls, extract key metadata fields */
let taskMeta = $derived.by(() => {
if (!isTask || !inputParams)
return null;
const meta: { label: string; value: string }[] = [];
if (inputParams.subagent_type) {
meta.push({
label: "type",
value: inputParams.subagent_type,
});
}
if (inputParams.description) {
meta.push({
label: "description",
value: inputParams.description,
});
}
return meta.length ? meta : null;
});
/** For TaskCreate, show subject and description */
let taskCreateMeta = $derived.by(() => {
if (toolCall?.tool_name !== "TaskCreate" || !inputParams)
return null;
const meta: { label: string; value: string }[] = [];
if (inputParams.subject) {
meta.push({ label: "subject", value: inputParams.subject });
}
if (inputParams.description) {
meta.push({ label: "description", value: inputParams.description });
}
return meta.length ? meta : null;
});
/** For TaskUpdate, show taskId and status */
let taskUpdateMeta = $derived.by(() => {
if (toolCall?.tool_name !== "TaskUpdate" || !inputParams)
return null;
const meta: { label: string; value: string }[] = [];
if (inputParams.taskId) {
meta.push({ label: "task", value: `#${inputParams.taskId}` });
}
if (inputParams.status) {
meta.push({ label: "status", value: inputParams.status });
}
if (inputParams.subject) {
meta.push({ label: "subject", value: inputParams.subject });
}
return meta.length ? meta : null;
});
/** Extract metadata tags for common tool types */
let toolParamMeta = $derived.by(() => {
if (!inputParams || !toolCall) return null;
return extractToolParamMeta(toolCall.tool_name, inputParams, toolCall.category);
});
/** Combined metadata for any tool type */
let metaTags = $derived(
taskMeta ??
taskCreateMeta ??
taskUpdateMeta ??
toolParamMeta ??
null,
);
/** Generate content from input_json when regex content is empty.
* Try category first (e.g. "Edit"), then fall back to raw tool_name
* (e.g. "apply_patch") so tools that don't match their category's
* specific field patterns still get the generic key-value output. */
let fallbackContent = $derived.by(() => {
if (content || !inputParams || !toolCall) return null;
const cat = toolCall.category || null;
const result = cat ? generateFallbackContent(cat, inputParams) : null;
return result ?? generateFallbackContent(toolCall.tool_name, inputParams);
});
let isTask = $derived(
toolCall?.tool_name === "Task" ||
toolCall?.tool_name === "Agent" ||
toolCall?.category === "Task" ||
(toolCall?.tool_name?.includes("subagent") ?? false),
);
let taskPrompt = $derived(
isTask ? inputParams?.prompt ?? null : null,
);
let subagentSessionId = $derived(
isTask ? toolCall?.subagent_session_id ?? null : null,
);
</script>
<div class="tool-block">
<button
class="tool-header"
onclick={() => {
const sel = window.getSelection();
if (sel && sel.toString().length > 0) return;
userCollapsed = !userCollapsed;
userOverride = true;
}}
>
<span class="tool-chevron" class:open={!collapsed}>
▸
</span>
{#if label}
<span class="tool-label">{label}</span>
{/if}
{#if collapsed && previewLine}
<span class="tool-preview">{previewLine}</span>
{/if}
</button>
{#if !collapsed}
{#if metaTags}
<div class="tool-meta">
{#each metaTags as { label: metaLabel, value }}
<span class="meta-tag">
<span class="meta-label">{metaLabel}:</span>
{value}
</span>
{/each}
</div>
{/if}
{#if taskPrompt}
<pre class="tool-content" use:applyHighlight={{ q: highlightQuery, current: isCurrentHighlight, content: taskPrompt }}>{@html escapeHTML(taskPrompt)}</pre>
{:else if content}
<pre class="tool-content" use:applyHighlight={{ q: highlightQuery, current: isCurrentHighlight, content }}>{@html escapeHTML(content)}</pre>
{:else if fallbackContent}
<pre class="tool-content" use:applyHighlight={{ q: highlightQuery, current: isCurrentHighlight, content: fallbackContent }}>{@html escapeHTML(fallbackContent)}</pre>
{/if}
{#if toolCall?.result_content}
<button
class="output-header"
onclick={(e) => {
e.stopPropagation();
const sel = window.getSelection();
if (sel && sel.toString().length > 0) return;
userOutputCollapsed = !userOutputCollapsed;
userOutputOverride = true;
}}
>
<span class="tool-chevron" class:open={!outputCollapsed}>
▸
</span>
<span class="output-label">output</span>
{#if outputCollapsed && outputPreviewLine}
<span class="tool-preview">{outputPreviewLine}</span>
{/if}
</button>
{#if !outputCollapsed}
<pre class="tool-content output-content" use:applyHighlight={{ q: highlightQuery, current: isCurrentHighlight, content: toolCall.result_content }}>{@html escapeHTML(toolCall.result_content)}</pre>
{/if}
{/if}
{#if resultEvents.length > 0}
<button
class="history-header"
onclick={(e) => {
e.stopPropagation();
const sel = window.getSelection();
if (sel && sel.toString().length > 0) return;
userHistoryCollapsed = !userHistoryCollapsed;
userHistoryOverride = true;
}}
>
<span class="tool-chevron" class:open={!historyCollapsed}>
▸
</span>
<span class="output-label">history</span>
{#if historyCollapsed && historyPreviewLine}
<span class="tool-preview">{historyPreviewLine}</span>
{/if}
</button>
{#if !historyCollapsed}
<div class="result-history">
{#each resultEvents as event (event.event_index)}
<div class="result-event">
<div class="result-event-meta">
<span class="meta-tag">
<span class="meta-label">status:</span>
{event.status}
</span>
<span class="meta-tag">
<span class="meta-label">source:</span>
{event.source}
</span>
{#if event.agent_id}
<span class="meta-tag">
<span class="meta-label">agent:</span>
{event.agent_id}
</span>
{/if}
</div>
<pre class="tool-content output-content history-content" use:applyHighlight={{ q: highlightQuery, current: isCurrentHighlight, content: event.content }}>{@html escapeHTML(event.content)}</pre>
</div>
{/each}
</div>
{/if}
{/if}
{/if}
{#if subagentSessionId}
<SubagentInline sessionId={subagentSessionId} />
{/if}
</div>
<style>
.tool-block {
border-left: 2px solid var(--accent-amber);
background: var(--tool-bg);
border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
margin: 0;
}
.tool-header {
display: flex;
align-items: center;
gap: 6px;
padding: 6px 10px;
width: 100%;
text-align: left;
font-size: 12px;
color: var(--text-secondary);
min-width: 0;
border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
transition: background 0.1s;
user-select: text;
}
.tool-header:hover {
background: var(--bg-surface-hover);
color: var(--text-primary);
}
.tool-chevron {
display: inline-block;
font-size: 10px;
transition: transform 0.15s;
flex-shrink: 0;
color: var(--text-muted);
}
.tool-chevron.open {
transform: rotate(90deg);
}
.tool-label {
font-family: var(--font-mono);
font-weight: 500;
font-size: 11px;
color: var(--accent-amber);
white-space: nowrap;
flex-shrink: 0;
}
.tool-preview {
font-family: var(--font-mono);
font-size: 12px;
color: var(--text-muted);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
min-width: 0;
}
.tool-meta {
display: flex;
flex-wrap: wrap;
gap: 6px;
padding: 6px 14px;
border-top: 1px solid var(--border-muted);
}
.meta-tag {
font-family: var(--font-mono);
font-size: 11px;
color: var(--text-muted);
background: var(--bg-inset);
padding: 2px 6px;
border-radius: var(--radius-sm);
}
.meta-label {
color: var(--text-secondary);
font-weight: 500;
}
.tool-content {
padding: 8px 14px 10px;
font-family: var(--font-mono);
font-size: 12px;
color: var(--text-secondary);
line-height: 1.5;
overflow-x: auto;
border-top: 1px solid var(--border-muted);
}
.output-header {
display: flex;
align-items: center;
gap: 6px;
padding: 5px 10px;
width: 100%;
text-align: left;
font-size: 12px;
color: var(--text-secondary);
min-width: 0;
border-top: 1px solid var(--border-muted);
transition: background 0.1s;
user-select: text;
}
.output-header:hover {
background: var(--bg-surface-hover);
color: var(--text-primary);
}
.history-header {
display: flex;
align-items: center;
gap: 6px;
padding: 5px 10px;
width: 100%;
text-align: left;
font-size: 12px;
color: var(--text-secondary);
min-width: 0;
border-top: 1px solid var(--border-muted);
transition: background 0.1s;
user-select: text;
}
.history-header:hover {
background: var(--bg-surface-hover);
color: var(--text-primary);
}
.output-label {
font-family: var(--font-mono);
font-weight: 500;
font-size: 11px;
color: var(--text-secondary);
white-space: nowrap;
flex-shrink: 0;
}
.output-content {
max-height: 300px;
overflow-y: auto;
}
.result-history {
border-top: 1px solid var(--border-muted);
}
.result-event + .result-event {
border-top: 1px solid var(--border-muted);
}
.result-event-meta {
display: flex;
flex-wrap: wrap;
gap: 6px;
padding: 6px 14px 0;
}
.history-content {
border-top: 0;
margin-top: 0;
}
</style>