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
9 import { parseExecShellCommandMeta } from './parsers/exec-shell-command';
10 import ToolCallBlock from './ToolCallBlock.svelte';
11 import { AlertTriangle, Check, Loader2, XCircle } from '@lucide/svelte';
12 import { CollapsibleTerminalBlock } from '$lib/components/app';
13 import { SETTINGS_KEYS, TOOL_RUNTIME_SCROLL_AT_BOTTOM_THRESHOLD_PX } from '$lib/constants';
14 import { AttachmentType } from '$lib/enums';
15 import { settingsStore, toolsStore } from '$lib/stores';
16 import type { AgenticSection, ToolResultLine } from '$lib/types';
17 import type { DatabaseMessageExtra } from '$lib/types';
20 type ExecShellExitStatus,
22 isExitCodeSummaryLine,
23 parseExecShellCommandError,
24 parseExecShellCommandExitStatus,
25 parseToolResultWithMedia
29 section: AgenticSection;
32 /** True while the agentic loop is streaming output chunks for THIS
33 * tool call. Drives max-height + auto-scroll while true; releases
34 * them when the loop reports this call as done. */
35 isExecuting?: boolean;
36 attachments?: DatabaseMessageExtra[];
37 onToggle?: () => void;
40 let { attachments, isExecuting = false, isStreaming, onToggle, open, section }: Props = $props();
42 // `isLive` covers all in-flight phases: pre-chunk spinner and
43 // streaming itself. Frozen output (tool done while agent continues)
45 const isLive = $derived(isExecuting);
47 const execShellMeta = $derived(parseExecShellCommandMeta(section));
48 const execShellError = $derived(parseExecShellCommandError(section.toolResult));
49 const execShellExitStatus: ExecShellExitStatus | undefined = $derived(
50 parseExecShellCommandExitStatus(section.toolResult)
53 const parsedLines: ToolResultLine[] = $derived(
54 section.toolResult ? parseToolResultWithMedia(section.toolResult, attachments) : []
57 // Drop the trailing "[exit code: N]" line - rendered as a colored
58 // badge below. During streaming we keep it so a partial stream still
59 // shows the status once the final chunk lands.
60 const outputLines: ToolResultLine[] = $derived(
61 execShellExitStatus && parsedLines.length > 0
62 ? parsedLines.slice(0, parsedLines.length - 1)
66 const isExitCodeFinalLine = $derived(
67 execShellExitStatus !== undefined &&
68 parsedLines.length > 0 &&
69 isExitCodeSummaryLine(parsedLines[parsedLines.length - 1].text, execShellExitStatus)
72 // Highlight just the command for the title; the (typically large)
73 // output blob uses bare monospace to skip hljs per-line highlighting.
74 const highlightedCommandHtml = $derived(
75 execShellMeta ? highlightCode(execShellMeta.command, 'bash') : ''
78 // The working directory the command ran with, persisted per call on the
79 // tool result message (it travels via the x-tool-cwd header, not the tool
80 // args). Reading it from the section keeps it accurate even if the
81 // conversation cwd changes later.
82 const cwd = $derived(section.toolCwd);
83 const home = $derived(toolsStore.serverHome);
84 const wdDisplay = $derived(abbreviateHome(cwd ?? '', home));
86 const exitBadgeClass = $derived(
87 execShellExitStatus?.timedOut
88 ? 'exit-badge warning'
89 : execShellExitStatus?.code === 0
90 ? 'exit-badge success'
91 : 'exit-badge failure'
94 const useFullHeightCodeBlocks = $derived(
95 Boolean(settingsStore.config[SETTINGS_KEYS.FULL_HEIGHT_CODE_BLOCKS])
98 const autoScroll = $derived(isLive && !useFullHeightCodeBlocks);
100 const SCROLL_BOTTOM_THRESHOLD_PX = TOOL_RUNTIME_SCROLL_AT_BOTTOM_THRESHOLD_PX;
102 let scrollEl: HTMLDivElement | undefined = $state();
103 let userScrolledUp = $state(false);
104 let lastScrollTop = 0;
105 let pendingFrame: number | null = null;
107 function isAtBottom(): boolean {
108 if (!scrollEl) return false;
111 scrollEl.scrollHeight - scrollEl.clientHeight - scrollEl.scrollTop <=
112 SCROLL_BOTTOM_THRESHOLD_PX
116 function scrollToBottomOnFrame() {
117 if (pendingFrame !== null || !scrollEl || userScrolledUp) return;
119 pendingFrame = requestAnimationFrame(() => {
122 // Re-check on rAF - user may scroll between scheduling and paint.
123 if (scrollEl && !userScrolledUp) {
124 scrollEl.scrollTop = scrollEl.scrollHeight;
129 function handleScrollEvent() {
130 if (!scrollEl) return;
132 const isScrollingUp = scrollEl.scrollTop < lastScrollTop;
134 if (isScrollingUp && !isAtBottom()) {
135 userScrolledUp = true;
136 } else if (isAtBottom()) {
137 userScrolledUp = false;
140 lastScrollTop = scrollEl.scrollTop;
144 void section.toolResult;
146 if (!scrollEl || !autoScroll) return;
148 scrollToBottomOnFrame();
152 // Catch layout changes that don't touch toolResult (line-wrap
153 // reflow, image attaches, hljs settle).
154 if (!scrollEl || !autoScroll) return;
156 const observer = new MutationObserver(() => scrollToBottomOnFrame());
158 observer.observe(scrollEl, {
164 return () => observer.disconnect();
168 // Reset on stream end so the next render (full-height) starts
171 userScrolledUp = false;
177 {#snippet execShellTitle()}
179 <span class="exec-wd" title={cwd}>{wdDisplay}</span>
180 <span class="exec-prompt">$</span>
183 {#if highlightedCommandHtml}
184 <span class="font-mono">{@html highlightedCommandHtml}</span>
186 <span class="font-mono">{execShellMeta?.command}</span>
194 meta={execShellMeta ? { errorMessage: execShellError } : null}
195 wrapper={CollapsibleTerminalBlock}
196 extraLiveStreaming={isLive}
197 spinIconWhenActive={true}
200 {#snippet titleSnippet()}
201 {@render execShellTitle()}
204 {#snippet children(_meta, ctx)}
206 <div class="flex items-start gap-2 text-xs text-muted-foreground/70">
207 <Loader2 class="h-3 w-3 animate-spin" />
210 {:else if execShellError}
211 <div class="flex items-start gap-2 text-xs text-red-600 italic dark:text-red-400">
212 <XCircle class="mt-0.5 h-3 w-3 shrink-0" />
213 <span>{execShellError}</span>
215 {:else if section.toolResult}
218 class="terminal-output"
219 class:is-clamped={!useFullHeightCodeBlocks}
220 onscroll={handleScrollEvent}
222 {#each outputLines as line, i (i)}
223 <div class="font-mono text-[11px] leading-relaxed whitespace-pre-wrap">{line.text}</div>
224 {#if line.media?.type === AttachmentType.IMAGE}
226 src={line.media.base64Url}
227 alt={line.media.name}
228 class="mt-2 mb-2 h-auto max-w-full rounded-lg"
234 {#if isExitCodeFinalLine && execShellExitStatus}
235 <div class={exitBadgeClass}>
236 {#if execShellExitStatus.timedOut}
237 <AlertTriangle class="h-3 w-3" />
238 <span>timed out</span>
239 <span class="exit-sep">·</span>
240 <span>exit {execShellExitStatus.code}</span>
241 {:else if execShellExitStatus.code === 0}
242 <Check class="h-3 w-3" />
245 <XCircle class="h-3 w-3" />
246 <span>exit {execShellExitStatus.code}</span>
257 --exec-wd-margin: 0.4rem;
261 font-family: var(--font-mono);
262 color: var(--muted-foreground);
263 margin-right: var(--exec-wd-margin);
267 font-family: var(--font-mono);
268 color: var(--muted-foreground);
270 margin-right: var(--exec-wd-margin);
274 overscroll-behavior: contain;
277 .terminal-output.is-clamped {
280 scrollbar-gutter: stable;
281 padding-right: 0.25rem;
285 display: inline-flex;
289 padding: 0.2rem 0.55rem;
290 border-radius: 0.375rem;
291 font-family: var(--font-mono);
294 letter-spacing: 0.01em;
298 .exit-badge.success {
299 background: color-mix(in oklch, var(--color-green-500, #22c55e) 14%, transparent);
300 color: var(--color-green-700, #15803d);
303 :global(.dark) .exit-badge.success {
304 background: color-mix(in oklch, var(--color-green-400, #4ade80) 18%, transparent);
305 color: var(--color-green-300, #86efac);
308 .exit-badge.failure {
309 background: color-mix(in oklch, var(--color-red-500, #ef4444) 14%, transparent);
310 color: var(--color-red-700, #b91c1c);
313 :global(.dark) .exit-badge.failure {
314 background: color-mix(in oklch, var(--color-red-400, #f87171) 18%, transparent);
315 color: var(--color-red-300, #fca5a5);
318 .exit-badge.warning {
319 background: color-mix(in oklch, var(--color-amber-500, #f59e0b) 14%, transparent);
320 color: var(--color-amber-700, #b45309);
323 :global(.dark) .exit-badge.warning {
324 background: color-mix(in oklch, var(--color-amber-400, #fbbf24) 18%, transparent);
325 color: var(--color-amber-300, #fcd34d);