'use client'; import { useState, useRef, memo, useCallback } from 'react'; import { IconCheck, IconX, IconTerminal } from '@tabler/icons-react'; import { GridSpinner } from '@/components/grid-spinner'; import { transformPathsInText } from '@/lib/utils'; import type { Message } from '@/lib/types/http'; import { ExpandableRow } from './expandable-row'; type ShellExecOutput = { exit_code?: number; stdout?: string; stderr?: string; }; type ShellExecPayload = { command?: string; work_dir?: string; description?: string; timeout?: number; background?: boolean; output?: ShellExecOutput; }; type NormalizedPayload = { kind?: string; shell_exec?: ShellExecPayload; }; type ToolExecuteMetadata = { tool_call_id?: string; status?: 'pending' | 'running' | 'complete' | 'error'; normalized?: NormalizedPayload; }; type ToolExecuteMessageProps = { comment: Message; worktreePath?: string; }; export const ToolExecuteMessage = memo(function ToolExecuteMessage({ comment, worktreePath }: ToolExecuteMessageProps) { const metadata = comment.metadata as ToolExecuteMetadata | undefined; const [manualExpandState, setManualExpandState] = useState(null); const prevStatusRef = useRef(metadata?.status); const status = metadata?.status; const shellExec = metadata?.normalized?.shell_exec; const output = shellExec?.output; const workDir = shellExec?.work_dir; const hasOutput = output?.stdout || output?.stderr; const hasExpandableContent = hasOutput || workDir; const isSuccess = status === 'complete' && (output?.exit_code === 0 || output?.exit_code === undefined); // Reset manual state when status changes (allows auto-expand behavior to resume) if (prevStatusRef.current !== status) { prevStatusRef.current = status; if (manualExpandState !== null) { setManualExpandState(null); } } // Derive expanded state: manual override takes precedence, otherwise auto-expand when running const autoExpanded = status === 'running'; const isExpanded = manualExpandState ?? autoExpanded; const handleToggle = useCallback(() => { setManualExpandState((prev) => !(prev ?? autoExpanded)); }, [autoExpanded]); const getStatusIcon = () => { switch (status) { case 'complete': return output?.exit_code === 0 ? : ; case 'error': return ; case 'running': return ; default: return null; } }; // Format work_dir for display (make relative if possible) const displayWorkDir = workDir ? transformPathsInText(workDir, worktreePath) : null; return ( } header={
{transformPathsInText(comment.content, worktreePath)} {!isSuccess && getStatusIcon()}
} hasExpandableContent={!!hasExpandableContent} isExpanded={isExpanded} onToggle={handleToggle} >
{/* Only show cwd when there's no output */} {displayWorkDir && !hasOutput && (
cwd:{' '} {displayWorkDir}
)} {output?.stdout && (
            {output.stdout}
          
)} {output?.stderr && (
            {output.stderr}
          
)}
); });