Elffuss BraidHub

kikocis/continue public

main · 6.3 KiBRawPermalink
1import { Box, Text } from "ink";
2import React, { memo } from "react";
3
4import { ToolCallTitle } from "src/tools/ToolCallTitle.js";
5
6import type { ChatHistoryItem } from "../../../../../core/index.js";
7import { MarkdownRenderer } from "../MarkdownRenderer.js";
8import { ToolResultSummary } from "../ToolResultSummary.js";
9
10/**
11 * Formats message content for display, converting message parts array back to
12 * user-friendly format with placeholders like [Image #1], [Pasted Text #1], etc.
13 */
14function formatMessageContentForDisplay(
15 content: import("../../../../../core/index.js").MessageContent,
16): string {
17 if (typeof content === "string") {
18 return content;
19 }
20
21 if (!Array.isArray(content)) {
22 return JSON.stringify(content);
23 }
24
25 // Convert message parts array back to display format with placeholders
26 let displayText = "";
27 let imageCounter = 0;
28
29 for (const part of content) {
30 if (part.type === "text") {
31 displayText += part.text;
32 } else if (part.type === "imageUrl") {
33 imageCounter++;
34 displayText += `[Image #${imageCounter}]`;
35 } else {
36 // Handle any other part types by converting to JSON
37 displayText += JSON.stringify(part);
38 }
39 }
40
41 return displayText;
42}
43
44interface MemoizedMessageProps {
45 item: ChatHistoryItem;
46 index: number;
47 hideBullet?: boolean;
48}
49
50export const MemoizedMessage = memo<MemoizedMessageProps>(
51 ({ item, index, hideBullet = false }) => {
52 const { message, toolCallStates, conversationSummary } = item;
53 const isUser = message.role === "user";
54 const isSystem = message.role === "system";
55 const isAssistant = message.role === "assistant";
56
57 // Handle system messages
58 if (isSystem) {
59 // TODO: Properly separate LLM system messages from UI informational messages
60 // using discriminated union types. For now, skip displaying the first system
61 // message which is typically the LLM's system prompt.
62 if (index === 0) {
63 return null;
64 }
65
66 return (
67 <Box key={index} marginBottom={1}>
68 <Text color="dim" italic>
69 {message.content}
70 </Text>
71 </Box>
72 );
73 }
74
75 // Handle conversation summary (compaction)
76 if (conversationSummary) {
77 return (
78 <Box
79 key={index}
80 marginBottom={1}
81 borderStyle="single"
82 borderBottom={false}
83 borderLeft={false}
84 borderRight={false}
85 borderColor="gray"
86 />
87 );
88 }
89
90 // Handle tool calls
91 if (toolCallStates && toolCallStates.length > 0) {
92 return (
93 <Box key={index} flexDirection="column">
94 {/* Render assistant message content if any */}
95 {message.content && (
96 <Box marginBottom={1}>
97 <Text color="white">{hideBullet ? " " : "●"}</Text>
98 <Text> </Text>
99 <MarkdownRenderer
100 content={formatMessageContentForDisplay(message.content)}
101 />
102 </Box>
103 )}
104
105 {/* Render tool calls */}
106 {toolCallStates.map((toolState) => {
107 const toolName = toolState.toolCall.function.name;
108 const toolArgs = toolState.parsedArgs;
109 const isCompleted = toolState.status === "done";
110 const isErrored =
111 toolState.status === "errored" || toolState.status === "canceled";
112
113 return (
114 <Box
115 key={toolState.toolCallId}
116 flexDirection="column"
117 marginBottom={1}
118 >
119 <Box>
120 <Text
121 color={
122 isErrored
123 ? "red"
124 : isCompleted
125 ? "green"
126 : toolState.status === "generated"
127 ? "yellow"
128 : "white"
129 }
130 >
131 {isCompleted || isErrored ? "●" : "○"}
132 </Text>
133 <Text color="white">
134 {" "}
135 <ToolCallTitle toolName={toolName} args={toolArgs} />
136 </Text>
137 </Box>
138
139 {isErrored ? (
140 <Box marginLeft={2}>
141 <Text color="red">
142 {toolState.output?.[0].content ?? "Tool execution failed"}
143 </Text>
144 </Box>
145 ) : (
146 toolState.output &&
147 toolState.output.length > 0 && (
148 <Box marginLeft={2}>
149 <ToolResultSummary
150 toolName={toolName}
151 content={toolState.output
152 .map((o) => o.content)
153 .join("\n")}
154 />
155 </Box>
156 )
157 )}
158 </Box>
159 );
160 })}
161 </Box>
162 );
163 }
164
165 // Handle regular messages
166 const isStreaming = isAssistant && !message.content && !toolCallStates;
167
168 return (
169 <Box key={index} marginBottom={1}>
170 <Text color={isUser ? "blue" : "white"}>{hideBullet ? " " : "●"}</Text>
171 <Text> </Text>
172 {isUser ? (
173 <Text color="dim">
174 {formatMessageContentForDisplay(message.content)}
175 </Text>
176 ) : (
177 <MarkdownRenderer
178 content={formatMessageContentForDisplay(message.content)}
179 />
180 )}
181 {isStreaming && <Text color="dim">▋</Text>}
182 </Box>
183 );
184 },
185 (prevProps, nextProps) => {
186 // Custom comparison function for better performance
187 // Only re-render if these properties change
188 const prevMessage = prevProps.item.message;
189 const nextMessage = nextProps.item.message;
190 const prevToolStates = prevProps.item.toolCallStates;
191 const nextToolStates = nextProps.item.toolCallStates;
192
193 return (
194 prevMessage.content === nextMessage.content &&
195 prevMessage.role === nextMessage.role &&
196 JSON.stringify(prevToolStates) === JSON.stringify(nextToolStates) &&
197 prevProps.item.conversationSummary ===
198 nextProps.item.conversationSummary &&
199 prevProps.index === nextProps.index &&
200 prevProps.hideBullet === nextProps.hideBullet
201 );
202 },
203);
204
205MemoizedMessage.displayName = "MemoizedMessage";