Elffuss BraidHub

kikocis/continue public

4e7d29ea58e1 · 5.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
10interface MemoizedMessageProps {
11 item: ChatHistoryItem;
12 index: number;
13}
14
15export const MemoizedMessage = memo<MemoizedMessageProps>(
16 ({ item, index }) => {
17 const { message, toolCallStates, conversationSummary } = item;
18 const isUser = message.role === "user";
19 const isSystem = message.role === "system";
20 const isAssistant = message.role === "assistant";
21
22 // Handle system messages
23 if (isSystem) {
24 // TODO: Properly separate LLM system messages from UI informational messages
25 // using discriminated union types. For now, skip displaying the first system
26 // message which is typically the LLM's system prompt.
27 if (index === 0) {
28 return null;
29 }
30
31 return (
32 <Box key={index} marginBottom={1}>
33 <Text color="gray" italic>
34 {message.content}
35 </Text>
36 </Box>
37 );
38 }
39
40 // Handle conversation summary (compaction)
41 if (conversationSummary) {
42 return (
43 <Box
44 key={index}
45 marginBottom={1}
46 borderStyle="single"
47 borderBottom={false}
48 borderLeft={false}
49 borderRight={false}
50 borderColor="gray"
51 />
52 );
53 }
54
55 // Handle tool calls
56 if (toolCallStates && toolCallStates.length > 0) {
57 return (
58 <Box key={index} flexDirection="column">
59 {/* Render assistant message content if any */}
60 {message.content && (
61 <Box marginBottom={1}>
62 <Text color="white">●</Text>
63 <Text> </Text>
64 <MarkdownRenderer
65 content={
66 typeof message.content === "string"
67 ? message.content
68 : JSON.stringify(message.content)
69 }
70 />
71 </Box>
72 )}
73
74 {/* Render tool calls */}
75 {toolCallStates.map((toolState) => {
76 const toolName = toolState.toolCall.function.name;
77 const toolArgs = toolState.parsedArgs;
78 const isCompleted = toolState.status === "done";
79 const isErrored =
80 toolState.status === "errored" || toolState.status === "canceled";
81
82 return (
83 <Box
84 key={toolState.toolCallId}
85 flexDirection="column"
86 marginBottom={1}
87 >
88 <Box>
89 <Text
90 color={isErrored ? "red" : isCompleted ? "green" : "white"}
91 >
92 {isCompleted || isErrored ? "●" : "○"}
93 </Text>
94 <Text color="white">
95 {" "}
96 <ToolCallTitle toolName={toolName} args={toolArgs} />
97 </Text>
98 </Box>
99
100 {isErrored ? (
101 <Box marginLeft={2}>
102 <Text color="red">
103 {toolState.output?.[0].content ?? "Tool execution failed"}
104 </Text>
105 </Box>
106 ) : (
107 toolState.output &&
108 toolState.output.length > 0 && (
109 <Box marginLeft={2}>
110 <ToolResultSummary
111 toolName={toolName}
112 content={toolState.output
113 .map((o) => o.content)
114 .join("\n")}
115 />
116 </Box>
117 )
118 )}
119 </Box>
120 );
121 })}
122 </Box>
123 );
124 }
125
126 // Handle regular messages
127 const isStreaming = isAssistant && !message.content && !toolCallStates;
128
129 return (
130 <Box key={index} marginBottom={1}>
131 <Text color={isUser ? "blue" : "white"}>●</Text>
132 <Text> </Text>
133 {isUser ? (
134 <Text color="gray">
135 {typeof message.content === "string"
136 ? message.content
137 : JSON.stringify(message.content)}
138 </Text>
139 ) : (
140 <MarkdownRenderer
141 content={
142 typeof message.content === "string"
143 ? message.content
144 : JSON.stringify(message.content)
145 }
146 />
147 )}
148 {isStreaming && <Text color="gray">▋</Text>}
149 </Box>
150 );
151 },
152 (prevProps, nextProps) => {
153 // Custom comparison function for better performance
154 // Only re-render if these properties change
155 const prevMessage = prevProps.item.message;
156 const nextMessage = nextProps.item.message;
157 const prevToolStates = prevProps.item.toolCallStates;
158 const nextToolStates = nextProps.item.toolCallStates;
159
160 return (
161 prevMessage.content === nextMessage.content &&
162 prevMessage.role === nextMessage.role &&
163 JSON.stringify(prevToolStates) === JSON.stringify(nextToolStates) &&
164 prevProps.item.conversationSummary ===
165 nextProps.item.conversationSummary &&
166 prevProps.index === nextProps.index
167 );
168 },
169);
170
171MemoizedMessage.displayName = "MemoizedMessage";