| 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 | <Text color="white">○ {message.content}</Text> |
| 24 | </Box> |
| 25 | ); |
| 26 | |
| 27 | case "tool-result": |
| 28 | const isFailure = |
| 29 | message.toolResult?.startsWith("Permission denied") || |
| 30 | message.toolResult?.startsWith("Error"); |
| 31 | return ( |
| 32 | <Box |
| 33 | key={index} |
| 34 | marginLeft={1} |
| 35 | marginBottom={1} |
| 36 | flexDirection="column" |
| 37 | > |
| 38 | <Box> |
| 39 | <Text color={isFailure ? "red" : "green"}>●</Text> |
| 40 | <Text> {message.content}</Text> |
| 41 | </Box> |
| 42 | <Box marginLeft={2}> |
| 43 | <ToolResultSummary |
| 44 | toolName={message.toolName} |
| 45 | content={message.toolResult || ""} |
| 46 | /> |
| 47 | </Box> |
| 48 | </Box> |
| 49 | ); |
| 50 | |
| 51 | case "tool-error": |
| 52 | return ( |
| 53 | <Box key={index} marginLeft={1} marginBottom={1}> |
| 54 | <Text color="red" bold> |
| 55 | ✗{" "} |
| 56 | </Text> |
| 57 | <Text color="red">Tool error: {message.content}</Text> |
| 58 | </Box> |
| 59 | ); |
| 60 | |
| 61 | default: |
| 62 | return ( |
| 63 | <Box key={index} marginLeft={1} marginBottom={1}> |
| 64 | <Text color="gray" italic> |
| 65 | {message.content} |
| 66 | </Text> |
| 67 | </Box> |
| 68 | ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Special rendering for compaction messages |
| 73 | if (message.messageType === "compaction") { |
| 74 | return ( |
| 75 | <Box |
| 76 | key={index} |
| 77 | marginLeft={1} |
| 78 | marginBottom={1} |
| 79 | borderStyle="single" |
| 80 | borderBottom={false} |
| 81 | borderLeft={false} |
| 82 | borderRight={false} |
| 83 | borderColor="gray" |
| 84 | /> |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | return ( |
| 89 | <Box key={index} marginLeft={1} marginBottom={1}> |
| 90 | <Text color={isUser ? "green" : "magenta"}>●</Text> |
| 91 | <Text> </Text> |
| 92 | {isUser ? ( |
| 93 | <Text color="gray">{message.content}</Text> |
| 94 | ) : ( |
| 95 | <MarkdownRenderer content={message.content} /> |
| 96 | )} |
| 97 | {message.isStreaming && <Text color="gray">▋</Text>} |
| 98 | </Box> |
| 99 | ); |
| 100 | }, |
| 101 | (prevProps, nextProps) => { |
| 102 | // Custom comparison function for better performance |
| 103 | // Only re-render if these properties change |
| 104 | return ( |
| 105 | prevProps.message.content === nextProps.message.content && |
| 106 | prevProps.message.role === nextProps.message.role && |
| 107 | prevProps.message.messageType === nextProps.message.messageType && |
| 108 | prevProps.message.isStreaming === nextProps.message.isStreaming && |
| 109 | prevProps.message.toolName === nextProps.message.toolName && |
| 110 | prevProps.message.toolResult === nextProps.message.toolResult && |
| 111 | prevProps.message.permissionRequestId === |
| 112 | nextProps.message.permissionRequestId && |
| 113 | prevProps.index === nextProps.index |
| 114 | ); |
| 115 | }, |
| 116 | ); |
| 117 | |
| 118 | MemoizedMessage.displayName = "MemoizedMessage"; |