Elffuss BraidHub

kikocis/continue public

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