Elffuss BraidHub

kikocis/continue public

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