Elffuss BraidHub

kikocis/continue public

d595b705ef5a · 5.4 KiBRawPermalink
1import path from "path";
2
3import { Box, Text } from "ink";
4import React from "react";
5
6import { getToolDisplayName } from "src/tools/index.js";
7
8import { ColoredDiff } from "./ColoredDiff.js";
9import { ChecklistDisplay } from "./components/ChecklistDisplay.js";
10
11const MAX_BASH_OUTPUT_LINES = 4;
12
13interface ToolResultSummaryProps {
14 toolName?: string;
15 content: string;
16}
17
18const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
19 toolName,
20 content,
21}) => {
22 if (!content) {
23 return (
24 <Box>
25 <Text color="gray">⎿ </Text>
26 <Text color="gray"> 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="gray">⎿ </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="gray">⎿ </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="gray">⎿ </Text>
85 <Text color="gray"> Terminal output:</Text>
86 </Box>
87 <Box paddingLeft={2}>
88 <Text color={isStderr ? "red" : "white"}>{actualOutput}</Text>
89 </Box>
90 </Box>
91 );
92 } else {
93 // Show first MAX_BASH_OUTPUT_LINES lines with ellipsis for more lines
94 const firstLines = outputLines.slice(0, MAX_BASH_OUTPUT_LINES).join("\n");
95 return (
96 <Box flexDirection="column">
97 <Box>
98 <Text color="gray">⎿ </Text>
99 <Text color="gray"> Terminal output:</Text>
100 </Box>
101 <Box paddingLeft={2}>
102 <Text color={isStderr ? "red" : "white"}>{firstLines}</Text>
103 </Box>
104 <Box paddingLeft={2}>
105 <Text color="gray">
106 ... +{outputLines.length - MAX_BASH_OUTPUT_LINES} lines
107 </Text>
108 </Box>
109 </Box>
110 );
111 }
112 }
113
114 // Handle all other cases with text summary
115 const getSummary = () => {
116 // Check if this is a user cancellation first
117 if (content === "Permission denied by user") {
118 return "Cancelled by user";
119 }
120
121 // Check if it was an error
122 if (content.startsWith("Error")) {
123 const lines = content.split("\n");
124 return `Error: ${lines[0]}${lines.length > 1 ? "..." : ""}`;
125 }
126
127 // Convert absolute paths to relative paths from workspace root
128 const formatPath = (filePath: string) => {
129 if (path.isAbsolute(filePath)) {
130 const workspaceRoot = process.cwd();
131 const relativePath = path.relative(workspaceRoot, filePath);
132 return relativePath || filePath;
133 }
134 return filePath;
135 };
136
137 // Handle specific tool output formatting
138 switch (toolName) {
139 case "Read":
140 // Try to extract file path from content if it contains line numbers
141 if (content.includes("→")) {
142 const pathMatch = content.match(/^(.+?):/);
143 const filePath = pathMatch ? pathMatch[1] : "file";
144 return `${displayName} ${formatPath(filePath)} (${lines} lines)`;
145 }
146 return `${displayName} tool output (${lines} lines)`;
147
148 case "Write":
149 return content.includes("Successfully created file")
150 ? "File created successfully"
151 : "File updated successfully";
152
153 case "Edit":
154 return "File edited successfully";
155
156 case "MultiEdit":
157 return "File edited successfully";
158
159 case "List":
160 return `Listed ${lines} ${lines === 1 ? "item" : "items"}`;
161
162 case "Search":
163 return `Found ${lines} ${lines === 1 ? "match" : "matches"}`;
164
165 case "Diff":
166 return `Diff output (${lines} lines)`;
167
168 default:
169 // Fallback for all tools using display name
170 if (chars > 1000) {
171 return `${displayName} output: ${lines} lines, ${chars} characters`;
172 } else if (lines > 10) {
173 return `${displayName} output: ${lines} lines`;
174 } else {
175 return content.slice(0, 100) + (content.length > 100 ? "..." : "");
176 }
177 }
178 };
179
180 return (
181 <Box>
182 <Text color="gray">⎿ </Text>
183 <Text color="gray"> {getSummary()}</Text>
184 </Box>
185 );
186};
187
188export { ToolResultSummary };