| 1 | import { Box, Text } from "ink"; |
| 2 | import React, { memo } from "react"; |
| 3 | |
| 4 | import { MarkdownRenderer } from "../MarkdownRenderer.js"; |
| 5 | import { ToolResultSummary } from "../ToolResultSummary.js"; |
| 6 | import { DisplayMessage } from "../types.js"; |
| 7 | |
| 8 | interface MemoizedMessageProps { |
| 9 | message: DisplayMessage; |
| 10 | index: number; |
| 11 | } |
| 12 | |
| 13 | export const MemoizedMessage = memo<MemoizedMessageProps>( |
| 14 | ({ message, index }) => { |
| 15 | const isUser = message.role === "user"; |
| 16 | const isSystem = message.role === "system"; |
| 17 | |
| 18 | if (isSystem) { |
| 19 | switch (message.messageType) { |
| 20 | case "tool-start": |
| 21 | return ( |
| 22 | <Box key={index} marginLeft={1} marginBottom={1}> |
| 23 | {/* TODO: Change back to empty circle (○) with white color once we want to differentiate in-progress vs completed tool calls */} |
| 24 | <Text color="green"> |
| 25 | ●<Text color="white"> {message.content}</Text> |
| 26 | </Text> |
| 27 | </Box> |
| 28 | ); |
| 29 | |
| 30 | case "tool-result": |
| 31 | const isFailure = |
| 32 | message.toolResult?.startsWith("Permission denied") || |
| 33 | message.toolResult?.startsWith("Error"); |
| 34 | return ( |
| 35 | <Box |
| 36 | key={index} |
| 37 | marginLeft={1} |
| 38 | marginBottom={1} |
| 39 | flexDirection="column" |
| 40 | > |
| 41 | <Box> |
| 42 | <Text color={isFailure ? "red" : "green"}>●</Text> |
| 43 | <Text> {message.content}</Text> |
| 44 | </Box> |
| 45 | <Box marginLeft={2}> |
| 46 | <ToolResultSummary |
| 47 | toolName={message.toolName} |
| 48 | content={message.toolResult || ""} |
| 49 | /> |
| 50 | </Box> |
| 51 | </Box> |
| 52 | ); |
| 53 | |
| 54 | case "tool-error": |
| 55 | return ( |
| 56 | <Box key={index} marginLeft={1} marginBottom={1}> |
| 57 | <Text color="red" bold> |
| 58 | ✗{" "} |
| 59 | </Text> |
| 60 | <Text color="red">Tool error: {message.content}</Text> |
| 61 | </Box> |
| 62 | ); |
| 63 | |
| 64 | default: |
| 65 | return ( |
| 66 | <Box key={index} marginLeft={1} marginBottom={1}> |
| 67 | <Text color="gray" italic> |
| 68 | {message.content} |
| 69 | </Text> |
| 70 | </Box> |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Special rendering for compaction messages |
| 76 | if (message.messageType === "compaction") { |
| 77 | return ( |
| 78 | <Box |
| 79 | key={index} |
| 80 | marginLeft={1} |
| 81 | marginBottom={1} |
| 82 | borderStyle="single" |
| 83 | borderBottom={false} |
| 84 | borderLeft={false} |
| 85 | borderRight={false} |
| 86 | borderColor="gray" |
| 87 | /> |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | return ( |
| 92 | <Box key={index} marginLeft={1} marginBottom={1}> |
| 93 | <Text color={isUser ? "green" : "white"}>●</Text> |
| 94 | <Text> </Text> |
| 95 | {isUser ? ( |
| 96 | <Text color="gray">{message.content}</Text> |
| 97 | ) : ( |
| 98 | <MarkdownRenderer content={message.content} /> |
| 99 | )} |
| 100 | {message.isStreaming && <Text color="gray">▋</Text>} |
| 101 | </Box> |
| 102 | ); |
| 103 | }, |
| 104 | (prevProps, nextProps) => { |
| 105 | // Custom comparison function for better performance |
| 106 | // Only re-render if these properties change |
| 107 | return ( |
| 108 | prevProps.message.content === nextProps.message.content && |
| 109 | prevProps.message.role === nextProps.message.role && |
| 110 | prevProps.message.messageType === nextProps.message.messageType && |
| 111 | prevProps.message.isStreaming === nextProps.message.isStreaming && |
| 112 | prevProps.message.toolName === nextProps.message.toolName && |
| 113 | prevProps.message.toolResult === nextProps.message.toolResult && |
| 114 | prevProps.message.permissionRequestId === |
| 115 | nextProps.message.permissionRequestId && |
| 116 | prevProps.index === nextProps.index |
| 117 | ); |
| 118 | }, |
| 119 | ); |
| 120 | |
| 121 | MemoizedMessage.displayName = "MemoizedMessage"; |