|
| 1 | +import React from "react"; |
| 2 | +import type { DisplayedMessage } from "@/common/types/message"; |
| 3 | +import { cn } from "@/common/lib/utils"; |
| 4 | +import { |
| 5 | + ToolContainer, |
| 6 | + ToolHeader, |
| 7 | + ExpandIcon, |
| 8 | + ToolDetails, |
| 9 | + DetailSection, |
| 10 | + DetailLabel, |
| 11 | + DetailContent, |
| 12 | + StatusIndicator, |
| 13 | +} from "../tools/shared/ToolPrimitives"; |
| 14 | +import { useToolExpansion } from "../tools/shared/toolUtils"; |
| 15 | + |
| 16 | +interface ScriptExecutionMessageProps { |
| 17 | + message: Extract<DisplayedMessage, { type: "script-execution" }>; |
| 18 | + className?: string; |
| 19 | +} |
| 20 | + |
| 21 | +function formatDuration(ms: number): string { |
| 22 | + if (!Number.isFinite(ms) || ms < 0) { |
| 23 | + return "unknown"; |
| 24 | + } |
| 25 | + if (ms < 1000) { |
| 26 | + return `${Math.round(ms)}ms`; |
| 27 | + } |
| 28 | + return `${Math.round(ms / 1000)}s`; |
| 29 | +} |
| 30 | + |
| 31 | +export const ScriptExecutionMessage: React.FC<ScriptExecutionMessageProps> = ({ |
| 32 | + message, |
| 33 | + className, |
| 34 | +}) => { |
| 35 | + const { expanded, toggleExpanded } = useToolExpansion(); |
| 36 | + const { result } = message; |
| 37 | + |
| 38 | + const exitBadgeClass = cn( |
| 39 | + "ml-2 inline-block shrink-0 rounded px-1.5 py-0.5 text-[10px] font-medium whitespace-nowrap", |
| 40 | + result.exitCode === 0 ? "bg-success text-on-success" : "bg-danger text-on-danger" |
| 41 | + ); |
| 42 | + |
| 43 | + const argsPreview = message.args.length > 0 ? ` ${message.args.join(" ")}` : ""; |
| 44 | + |
| 45 | + return ( |
| 46 | + <ToolContainer expanded={expanded} className={className}> |
| 47 | + <ToolHeader onClick={toggleExpanded}> |
| 48 | + <ExpandIcon expanded={expanded}>▶</ExpandIcon> |
| 49 | + <span aria-hidden="true">📝</span> |
| 50 | + <span className="font-monospace max-w-96 truncate"> |
| 51 | + {message.command || `/script ${message.scriptName}${argsPreview}`} |
| 52 | + </span> |
| 53 | + <span className="text-[10px] text-foreground-secondary ml-2 whitespace-nowrap"> |
| 54 | + took {formatDuration(result.wall_duration_ms)} |
| 55 | + </span> |
| 56 | + <span className={exitBadgeClass}>exit {result.exitCode}</span> |
| 57 | + <StatusIndicator status="completed">script</StatusIndicator> |
| 58 | + </ToolHeader> |
| 59 | + |
| 60 | + {expanded && ( |
| 61 | + <ToolDetails> |
| 62 | + <DetailSection> |
| 63 | + <DetailLabel>Command</DetailLabel> |
| 64 | + <DetailContent>{message.command}</DetailContent> |
| 65 | + </DetailSection> |
| 66 | + |
| 67 | + <DetailSection> |
| 68 | + <DetailLabel>Runtime info</DetailLabel> |
| 69 | + <div className="text-[11px] text-foreground-secondary"> |
| 70 | + {new Date(message.timestamp).toLocaleString()} •{" "} |
| 71 | + {formatDuration(result.wall_duration_ms)} |
| 72 | + </div> |
| 73 | + <div className="text-[11px] text-foreground-secondary"> |
| 74 | + Visible only to you; never sent to the model. |
| 75 | + </div> |
| 76 | + </DetailSection> |
| 77 | + |
| 78 | + {result.success === false && result.error && ( |
| 79 | + <DetailSection> |
| 80 | + <DetailLabel>Error</DetailLabel> |
| 81 | + <div className="text-danger bg-danger-overlay border-danger rounded border-l-2 px-2 py-1.5 text-[11px]"> |
| 82 | + {result.error} |
| 83 | + </div> |
| 84 | + </DetailSection> |
| 85 | + )} |
| 86 | + |
| 87 | + {result.output && ( |
| 88 | + <DetailSection> |
| 89 | + <DetailLabel>Stdout / Stderr</DetailLabel> |
| 90 | + <DetailContent>{result.output}</DetailContent> |
| 91 | + </DetailSection> |
| 92 | + )} |
| 93 | + |
| 94 | + {result.outputFile && ( |
| 95 | + <DetailSection> |
| 96 | + <DetailLabel>MUX_OUTPUT</DetailLabel> |
| 97 | + <DetailContent>{result.outputFile}</DetailContent> |
| 98 | + </DetailSection> |
| 99 | + )} |
| 100 | + |
| 101 | + {result.promptFile && ( |
| 102 | + <DetailSection> |
| 103 | + <DetailLabel>MUX_PROMPT</DetailLabel> |
| 104 | + <DetailContent>{result.promptFile}</DetailContent> |
| 105 | + </DetailSection> |
| 106 | + )} |
| 107 | + |
| 108 | + {result.truncated && ( |
| 109 | + <DetailSection> |
| 110 | + <DetailLabel>Truncation</DetailLabel> |
| 111 | + <div className="text-[11px] text-foreground-secondary"> |
| 112 | + Output truncated: {result.truncated.reason} ({result.truncated.totalLines} lines |
| 113 | + preserved) |
| 114 | + </div> |
| 115 | + </DetailSection> |
| 116 | + )} |
| 117 | + </ToolDetails> |
| 118 | + )} |
| 119 | + </ToolContainer> |
| 120 | + ); |
| 121 | +}; |
0 commit comments