| 1 | import path from "path"; |
| 2 | |
| 3 | import { Box, Text } from "ink"; |
| 4 | import React from "react"; |
| 5 | |
| 6 | import { getToolDisplayName } from "src/tools/index.js"; |
| 7 | |
| 8 | import { ColoredDiff } from "./ColoredDiff.js"; |
| 9 | import { ChecklistDisplay } from "./components/ChecklistDisplay.js"; |
| 10 | |
| 11 | const MAX_BASH_OUTPUT_LINES = 4; |
| 12 | |
| 13 | interface ToolResultSummaryProps { |
| 14 | toolName?: string; |
| 15 | content: string; |
| 16 | } |
| 17 | |
| 18 | const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({ |
| 19 | toolName, |
| 20 | content, |
| 21 | }) => { |
| 22 | if (!content) { |
| 23 | return ( |
| 24 | <Box> |
| 25 | <Text color="dim">⎿ </Text> |
| 26 | <Text color="dim"> No output</Text> |
| 27 | </Box> |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | const lines = content.split("\n").length; |
| 32 | const chars = content.length; |
| 33 | const displayName = toolName ? getToolDisplayName(toolName) : "Tool"; |
| 34 | |
| 35 | // Handle Checklist specially with styled display |
| 36 | if (toolName === "Checklist") { |
| 37 | return ( |
| 38 | <Box flexDirection="column"> |
| 39 | <Box marginBottom={1}> |
| 40 | <Text color="dim">⎿ </Text> |
| 41 | <Text color="blue">Task List Updated</Text> |
| 42 | </Box> |
| 43 | <ChecklistDisplay content={`Task list status:\n${content}`} /> |
| 44 | </Box> |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | // Handle Write/Edit/MultiEdit with diff specially |
| 49 | if ( |
| 50 | (toolName === "Write" || toolName === "Edit" || toolName === "MultiEdit") && |
| 51 | content.includes("Diff:\n") |
| 52 | ) { |
| 53 | const diffSection = content.split("Diff:\n")[1]; |
| 54 | if (diffSection) { |
| 55 | return ( |
| 56 | <Box flexDirection="column"> |
| 57 | <Box> |
| 58 | <Text color="dim">⎿ </Text> |
| 59 | <Text color="green"> |
| 60 | {toolName === "Edit" |
| 61 | ? " File edited successfully" |
| 62 | : toolName === "MultiEdit" |
| 63 | ? " File edited successfully" |
| 64 | : " File written successfully"} |
| 65 | </Text> |
| 66 | </Box> |
| 67 | <ColoredDiff diffContent={diffSection} /> |
| 68 | </Box> |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Handle terminal command output specially |
| 74 | if (toolName === "Bash") { |
| 75 | const isStderr = content.startsWith("Stderr:"); |
| 76 | const actualOutput = isStderr ? content.slice(7).trim() : content; |
| 77 | const outputLines = actualOutput.split("\n"); |
| 78 | |
| 79 | if (outputLines.length <= MAX_BASH_OUTPUT_LINES) { |
| 80 | // Show actual output for MAX_BASH_OUTPUT_LINES lines or fewer |
| 81 | return ( |
| 82 | <Box flexDirection="column"> |
| 83 | <Box> |
| 84 | <Text color="dim">⎿ </Text> |
| 85 | <Text color="dim"> Terminal output:</Text> |
| 86 | </Box> |
| 87 | <Box paddingLeft={2}> |
| 88 | <Text color={isStderr ? "red" : "white"}> |
| 89 | {actualOutput.trimEnd()} |
| 90 | </Text> |
| 91 | </Box> |
| 92 | </Box> |
| 93 | ); |
| 94 | } else { |
| 95 | // Show first MAX_BASH_OUTPUT_LINES lines with ellipsis for more lines |
| 96 | const firstLines = outputLines.slice(0, MAX_BASH_OUTPUT_LINES).join("\n"); |
| 97 | return ( |
| 98 | <Box flexDirection="column"> |
| 99 | <Box> |
| 100 | <Text color="dim">⎿ </Text> |
| 101 | <Text color="dim"> Terminal output:</Text> |
| 102 | </Box> |
| 103 | <Box paddingLeft={2}> |
| 104 | <Text color={isStderr ? "red" : "white"}> |
| 105 | {firstLines.trimEnd()} |
| 106 | </Text> |
| 107 | </Box> |
| 108 | <Box paddingLeft={2}> |
| 109 | <Text color="dim"> |
| 110 | ... +{outputLines.length - MAX_BASH_OUTPUT_LINES} lines |
| 111 | </Text> |
| 112 | </Box> |
| 113 | </Box> |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Handle all other cases with text summary |
| 119 | const getSummary = () => { |
| 120 | // Check if this is a user cancellation first |
| 121 | if (content === "Permission denied by user") { |
| 122 | return "Cancelled by user"; |
| 123 | } |
| 124 | |
| 125 | // Check if it was an error |
| 126 | if (content.startsWith("Error")) { |
| 127 | const lines = content.split("\n"); |
| 128 | return `Error: ${lines[0]}${lines.length > 1 ? "..." : ""}`; |
| 129 | } |
| 130 | |
| 131 | // Convert absolute paths to relative paths from workspace root |
| 132 | const formatPath = (filePath: string) => { |
| 133 | if (path.isAbsolute(filePath)) { |
| 134 | const workspaceRoot = process.cwd(); |
| 135 | const relativePath = path.relative(workspaceRoot, filePath); |
| 136 | return relativePath || filePath; |
| 137 | } |
| 138 | return filePath; |
| 139 | }; |
| 140 | |
| 141 | // Handle specific tool output formatting |
| 142 | switch (toolName) { |
| 143 | case "Read": |
| 144 | // Try to extract file path from content if it contains line numbers |
| 145 | if (content.includes("→")) { |
| 146 | const pathMatch = content.match(/^(.+?):/); |
| 147 | const filePath = pathMatch ? pathMatch[1] : "file"; |
| 148 | return `${displayName} ${formatPath(filePath)} (${lines} lines)`; |
| 149 | } |
| 150 | return `${displayName} tool output (${lines} lines)`; |
| 151 | |
| 152 | case "Write": |
| 153 | return content.includes("Successfully created file") |
| 154 | ? "File created successfully" |
| 155 | : "File updated successfully"; |
| 156 | |
| 157 | case "Edit": |
| 158 | return "File edited successfully"; |
| 159 | |
| 160 | case "MultiEdit": |
| 161 | return "File edited successfully"; |
| 162 | |
| 163 | case "List": |
| 164 | return `Listed ${lines} ${lines === 1 ? "item" : "items"}`; |
| 165 | |
| 166 | case "Search": |
| 167 | return `Found ${lines} ${lines === 1 ? "match" : "matches"}`; |
| 168 | |
| 169 | case "Diff": |
| 170 | return `Diff output (${lines} lines)`; |
| 171 | |
| 172 | default: |
| 173 | // Fallback for all tools using display name |
| 174 | if (chars > 1000) { |
| 175 | return `${displayName} output: ${lines} lines, ${chars} characters`; |
| 176 | } else if (lines > 10) { |
| 177 | return `${displayName} output: ${lines} lines`; |
| 178 | } else { |
| 179 | return content.slice(0, 100) + (content.length > 100 ? "..." : ""); |
| 180 | } |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | return ( |
| 185 | <Box> |
| 186 | <Text color="dim">⎿ </Text> |
| 187 | <Text color="dim"> {getSummary()}</Text> |
| 188 | </Box> |
| 189 | ); |
| 190 | }; |
| 191 | |
| 192 | export { ToolResultSummary }; |