]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/blob
8128ee30dd18a800fca86512ebf4614b297f5811
[pkg/ggml/sources/llama.cpp] /
1 <script lang="ts">
2 // Block for `exec_shell_command`. Unlike the other tools, this
3 // renderer uses CollapsibleTerminalBlock (terminal-style frame)
4 // and treats "live" output chunks as active even after the call
5 // resolved, so the spinner stays on while stdout is still flowing.
6 // The scroll-to-bottom auto-scroll logic mirrors what was here
7 // before extraction.
8
9 import { Check, Loader2, XCircle, AlertTriangle } from '@lucide/svelte';
10 import { CollapsibleTerminalBlock } from '$lib/components/app';
11 import { SETTINGS_KEYS } from '$lib/constants';
12 import { config } from '$lib/stores/settings.svelte';
13 import { TOOL_RUNTIME_SCROLL_AT_BOTTOM_THRESHOLD_PX } from '$lib/constants/auto-scroll';
14 import {
15 abbreviateHome,
16 highlightCode,
17 isExitCodeSummaryLine,
18 parseExecShellCommandError,
19 parseExecShellCommandExitStatus,
20 parseToolResultWithImages,
21 type AgenticSection,
22 type ExecShellExitStatus,
23 type ToolResultLine
24 } from '$lib/utils';
25 import { toolsStore } from '$lib/stores/tools.svelte';
26 import { parseExecShellCommandMeta } from './parsers/exec-shell-command';
27 import type { DatabaseMessageExtra } from '$lib/types';
28 import ToolCallBlock from './ToolCallBlock.svelte';
29
30 interface Props {
31 section: AgenticSection;
32 open: boolean;
33 isStreaming: boolean;
34 /** True while the agentic loop is streaming output chunks for THIS
35 * tool call. Drives max-height + auto-scroll while true; releases
36 * them when the loop reports this call as done. */
37 isExecuting?: boolean;
38 attachments?: DatabaseMessageExtra[];
39 onToggle?: () => void;
40 }
41
42 let { section, open, isStreaming, isExecuting = false, attachments, onToggle }: Props = $props();
43
44 // `isLive` covers all in-flight phases: pre-chunk spinner and
45 // streaming itself. Frozen output (tool done while agent continues)
46 // is not live.
47 const isLive = $derived(isExecuting);
48
49 const execShellMeta = $derived(parseExecShellCommandMeta(section));
50 const execShellError = $derived(parseExecShellCommandError(section.toolResult));
51 const execShellExitStatus: ExecShellExitStatus | undefined = $derived(
52 parseExecShellCommandExitStatus(section.toolResult)
53 );
54
55 const parsedLines: ToolResultLine[] = $derived(
56 section.toolResult ? parseToolResultWithImages(section.toolResult, attachments) : []
57 );
58
59 // Drop the trailing "[exit code: N]" line - rendered as a colored
60 // badge below. During streaming we keep it so a partial stream still
61 // shows the status once the final chunk lands.
62 const outputLines: ToolResultLine[] = $derived(
63 execShellExitStatus && parsedLines.length > 0
64 ? parsedLines.slice(0, parsedLines.length - 1)
65 : parsedLines
66 );
67
68 const isExitCodeFinalLine = $derived(
69 execShellExitStatus !== undefined &&
70 parsedLines.length > 0 &&
71 isExitCodeSummaryLine(parsedLines[parsedLines.length - 1].text, execShellExitStatus)
72 );
73
74 // Highlight just the command for the title; the (typically large)
75 // output blob uses bare monospace to skip hljs per-line highlighting.
76 const highlightedCommandHtml = $derived(
77 execShellMeta ? highlightCode(execShellMeta.command, 'bash') : ''
78 );
79
80 // The working directory the command ran with, persisted per call on the
81 // tool result message (it travels via the x-tool-cwd header, not the tool
82 // args). Reading it from the section keeps it accurate even if the
83 // conversation cwd changes later.
84 const cwd = $derived(section.toolCwd);
85 const home = $derived(toolsStore.serverHome);
86 const wdDisplay = $derived(abbreviateHome(cwd ?? '', home));
87
88 const exitBadgeClass = $derived(
89 execShellExitStatus?.timedOut
90 ? 'exit-badge warning'
91 : execShellExitStatus?.code === 0
92 ? 'exit-badge success'
93 : 'exit-badge failure'
94 );
95
96 const useFullHeightCodeBlocks = $derived(
97 Boolean(config()[SETTINGS_KEYS.FULL_HEIGHT_CODE_BLOCKS])
98 );
99
100 const autoScroll = $derived(isLive && !useFullHeightCodeBlocks);
101
102 const SCROLL_BOTTOM_THRESHOLD_PX = TOOL_RUNTIME_SCROLL_AT_BOTTOM_THRESHOLD_PX;
103
104 let scrollEl: HTMLDivElement | undefined = $state();
105 let userScrolledUp = $state(false);
106 let lastScrollTop = 0;
107 let pendingFrame: number | null = null;
108
109 function isAtBottom(): boolean {
110 if (!scrollEl) return false;
111 return (
112 scrollEl.scrollHeight - scrollEl.clientHeight - scrollEl.scrollTop <=
113 SCROLL_BOTTOM_THRESHOLD_PX
114 );
115 }
116
117 function scrollToBottomOnFrame() {
118 if (pendingFrame !== null || !scrollEl || userScrolledUp) return;
119 pendingFrame = requestAnimationFrame(() => {
120 pendingFrame = null;
121
122 // Re-check on rAF - user may scroll between scheduling and paint.
123 if (scrollEl && !userScrolledUp) {
124 scrollEl.scrollTop = scrollEl.scrollHeight;
125 }
126 });
127 }
128
129 function handleScrollEvent() {
130 if (!scrollEl) return;
131 const isScrollingUp = scrollEl.scrollTop < lastScrollTop;
132 if (isScrollingUp && !isAtBottom()) {
133 userScrolledUp = true;
134 } else if (isAtBottom()) {
135 userScrolledUp = false;
136 }
137 lastScrollTop = scrollEl.scrollTop;
138 }
139
140 $effect(() => {
141 void section.toolResult;
142 if (!scrollEl || !autoScroll) return;
143 scrollToBottomOnFrame();
144 });
145
146 $effect(() => {
147 // Catch layout changes that don't touch toolResult (line-wrap
148 // reflow, image attaches, hljs settle).
149 if (!scrollEl || !autoScroll) return;
150
151 const observer = new MutationObserver(() => scrollToBottomOnFrame());
152 observer.observe(scrollEl, {
153 childList: true,
154 subtree: true,
155 characterData: true
156 });
157
158 return () => observer.disconnect();
159 });
160
161 $effect(() => {
162 // Reset on stream end so the next render (full-height) starts
163 // pinned.
164 if (!isLive) {
165 userScrolledUp = false;
166 lastScrollTop = 0;
167 }
168 });
169 </script>
170
171 {#snippet execShellTitle()}
172 {#if cwd}
173 <span class="exec-wd" title={cwd}>{wdDisplay}</span>
174 <span class="exec-prompt">$</span>
175 {/if}
176
177 {#if highlightedCommandHtml}
178 <span class="font-mono">{@html highlightedCommandHtml}</span>
179 {:else}
180 <span class="font-mono">{execShellMeta?.command}</span>
181 {/if}
182 {/snippet}
183
184 <ToolCallBlock
185 {section}
186 {open}
187 {isStreaming}
188 meta={execShellMeta ? { errorMessage: execShellError } : null}
189 wrapper={CollapsibleTerminalBlock}
190 extraLiveStreaming={isLive}
191 spinIconWhenActive={true}
192 {onToggle}
193 >
194 {#snippet titleSnippet()}
195 {@render execShellTitle()}
196 {/snippet}
197
198 {#snippet children(_meta, ctx)}
199 {#if ctx.isPending}
200 <div class="flex items-start gap-2 text-xs text-muted-foreground/70">
201 <Loader2 class="h-3 w-3 animate-spin" />
202 Running...
203 </div>
204 {:else if execShellError}
205 <div class="flex items-start gap-2 text-xs text-red-600 italic dark:text-red-400">
206 <XCircle class="mt-0.5 h-3 w-3 shrink-0" />
207 <span>{execShellError}</span>
208 </div>
209 {:else if section.toolResult}
210 <div
211 bind:this={scrollEl}
212 class="terminal-output"
213 class:is-clamped={!useFullHeightCodeBlocks}
214 onscroll={handleScrollEvent}
215 >
216 {#each outputLines as line, i (i)}
217 <div class="font-mono text-[11px] leading-relaxed whitespace-pre-wrap">{line.text}</div>
218 {#if line.image}
219 <img
220 src={line.image.base64Url}
221 alt={line.image.name}
222 class="mt-2 mb-2 h-auto max-w-full rounded-lg"
223 loading="lazy"
224 />
225 {/if}
226 {/each}
227
228 {#if isExitCodeFinalLine && execShellExitStatus}
229 <div class={exitBadgeClass}>
230 {#if execShellExitStatus.timedOut}
231 <AlertTriangle class="h-3 w-3" />
232 <span>timed out</span>
233 <span class="exit-sep">&middot;</span>
234 <span>exit {execShellExitStatus.code}</span>
235 {:else if execShellExitStatus.code === 0}
236 <Check class="h-3 w-3" />
237 <span>exit 0</span>
238 {:else}
239 <XCircle class="h-3 w-3" />
240 <span>exit {execShellExitStatus.code}</span>
241 {/if}
242 </div>
243 {/if}
244 </div>
245 {/if}
246 {/snippet}
247 </ToolCallBlock>
248
249 <style>
250 :root {
251 --exec-wd-margin: 0.4rem;
252 }
253
254 .exec-wd {
255 font-family: var(--font-mono);
256 color: var(--muted-foreground);
257 margin-right: var(--exec-wd-margin);
258 }
259
260 .exec-prompt {
261 font-family: var(--font-mono);
262 color: var(--muted-foreground);
263 opacity: 0.55;
264 margin-right: var(--exec-wd-margin);
265 }
266
267 .terminal-output {
268 overscroll-behavior: contain;
269 }
270
271 .terminal-output.is-clamped {
272 max-height: 28rem;
273 overflow-y: auto;
274 scrollbar-gutter: stable;
275 padding-right: 0.25rem;
276 }
277
278 .exit-badge {
279 display: inline-flex;
280 align-items: center;
281 gap: 0.35rem;
282 margin-top: 0.5rem;
283 padding: 0.2rem 0.55rem;
284 border-radius: 0.375rem;
285 font-family: var(--font-mono);
286 font-size: 11px;
287 font-weight: 500;
288 letter-spacing: 0.01em;
289 line-height: 1;
290 }
291
292 .exit-badge.success {
293 background: color-mix(in oklch, var(--color-green-500, #22c55e) 14%, transparent);
294 color: var(--color-green-700, #15803d);
295 }
296
297 :global(.dark) .exit-badge.success {
298 background: color-mix(in oklch, var(--color-green-400, #4ade80) 18%, transparent);
299 color: var(--color-green-300, #86efac);
300 }
301
302 .exit-badge.failure {
303 background: color-mix(in oklch, var(--color-red-500, #ef4444) 14%, transparent);
304 color: var(--color-red-700, #b91c1c);
305 }
306
307 :global(.dark) .exit-badge.failure {
308 background: color-mix(in oklch, var(--color-red-400, #f87171) 18%, transparent);
309 color: var(--color-red-300, #fca5a5);
310 }
311
312 .exit-badge.warning {
313 background: color-mix(in oklch, var(--color-amber-500, #f59e0b) 14%, transparent);
314 color: var(--color-amber-700, #b45309);
315 }
316
317 :global(.dark) .exit-badge.warning {
318 background: color-mix(in oklch, var(--color-amber-400, #fbbf24) 18%, transparent);
319 color: var(--color-amber-300, #fcd34d);
320 }
321
322 .exit-sep {
323 opacity: 0.45;
324 }
325 </style>