Elffuss BraidHub

kikocis/continue public

9d5865e0e05b · 6.1 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}
48
49export const MemoizedMessage = memo<MemoizedMessageProps>(
50 ({ item, index }) => {
51 const { message, toolCallStates, conversationSummary } = item;
52 const isUser = message.role === "user";
53 const isSystem = message.role === "system";
54 const isAssistant = message.role === "assistant";
55
56 // Handle system messages
57 if (isSystem) {
58 // TODO: Properly separate LLM system messages from UI informational messages
59 // using discriminated union types. For now, skip displaying the first system
60 // message which is typically the LLM's system prompt.
61 if (index === 0) {
62 return null;
63 }
64
65 return (
66 <Box key={index} marginBottom={1}>
67 <Text color="gray" italic>
68 {message.content}
69 </Text>
70 </Box>
71 );
72 }
73
74 // Handle conversation summary (compaction)
75 if (conversationSummary) {
76 return (
77 <Box
78 key={index}
79 marginBottom={1}
80 borderStyle="single"
81 borderBottom={false}
82 borderLeft={false}
83 borderRight={false}
84 borderColor="gray"
85 />
86 );
87 }
88
89 // Handle tool calls
90 if (toolCallStates && toolCallStates.length > 0) {
91 return (
92 <Box key={index} flexDirection="column">
93 {/* Render assistant message content if any */}
94 {message.content && (
95 <Box marginBottom={1}>
96 <Text color="white">●</Text>
97 <Text> </Text>
98 <MarkdownRenderer
99 content={formatMessageContentForDisplay(message.content)}
100 />
101 </Box>
102 )}
103
104 {/* Render tool calls */}
105 {toolCallStates.map((toolState) => {
106 const toolName = toolState.toolCall.function.name;
107 const toolArgs = toolState.parsedArgs;
108 const isCompleted = toolState.status === "done";
109 const isErrored =
110 toolState.status === "errored" || toolState.status === "canceled";
111
112 return (
113 <Box
114 key={toolState.toolCallId}
115 flexDirection="column"
116 marginBottom={1}
117 >
118 <Box>
119 <Text
120 color={
121 isErrored
122 ? "red"
123 : isCompleted
124 ? "green"
125 : toolState.status === "generated"
126 ? "yellow"
127 : "white"
128 }
129 >
130 {isCompleted || isErrored ? "●" : "○"}
131 </Text>
132 <Text color="white">
133 {" "}
134 <ToolCallTitle toolName={toolName} args={toolArgs} />
135 </Text>
136 </Box>
137
138 {isErrored ? (
139 <Box marginLeft={2}>
140 <Text color="red">
141 {toolState.output?.[0].content ?? "Tool execution failed"}
142 </Text>
143 </Box>
144 ) : (
145 toolState.output &&
146 toolState.output.length > 0 && (
147 <Box marginLeft={2}>
148 <ToolResultSummary
149 toolName={toolName}
150 content={toolState.output
151 .map((o) => o.content)
152 .join("\n")}
153 />
154 </Box>
155 )
156 )}
157 </Box>
158 );
159 })}
160 </Box>
161 );
162 }
163
164 // Handle regular messages
165 const isStreaming = isAssistant && !message.content && !toolCallStates;
166
167 return (
168 <Box key={index} marginBottom={1}>
169 <Text color={isUser ? "blue" : "white"}>●</Text>
170 <Text> </Text>
171 {isUser ? (
172 <Text color="gray">
173 {formatMessageContentForDisplay(message.content)}
174 </Text>
175 ) : (
176 <MarkdownRenderer
177 content={formatMessageContentForDisplay(message.content)}
178 />
179 )}
180 {isStreaming && <Text color="gray">▋</Text>}
181 </Box>
182 );
183 },
184 (prevProps, nextProps) => {
185 // Custom comparison function for better performance
186 // Only re-render if these properties change
187 const prevMessage = prevProps.item.message;
188 const nextMessage = nextProps.item.message;
189 const prevToolStates = prevProps.item.toolCallStates;
190 const nextToolStates = nextProps.item.toolCallStates;
191
192 return (
193 prevMessage.content === nextMessage.content &&
194 prevMessage.role === nextMessage.role &&
195 JSON.stringify(prevToolStates) === JSON.stringify(nextToolStates) &&
196 prevProps.item.conversationSummary ===
197 nextProps.item.conversationSummary &&
198 prevProps.index === nextProps.index
199 );
200 },
201);
202
203MemoizedMessage.displayName = "MemoizedMessage";