|
| 1 | +import { Box, Text } from "ink"; |
| 2 | +import Spinner from "ink-spinner"; |
| 3 | +import { memo } from "react"; |
| 4 | +import type { Message } from "../../agents/types.js"; |
| 5 | + |
| 6 | +type MessagesProps = { |
| 7 | + messages: ReadonlyArray<Message>; |
| 8 | + isProcessing: boolean; |
| 9 | +}; |
| 10 | + |
| 11 | +export const Messages = memo(({ messages, isProcessing }: MessagesProps) => { |
| 12 | + return ( |
| 13 | + <Box |
| 14 | + flexDirection="column" |
| 15 | + flexGrow={1} |
| 16 | + marginTop={1} |
| 17 | + marginBottom={1} |
| 18 | + minHeight={12} |
| 19 | + > |
| 20 | + {messages.length === 0 && ( |
| 21 | + <Text color="gray" dimColor> |
| 22 | + No messages yet. Say hi below! |
| 23 | + </Text> |
| 24 | + )} |
| 25 | + |
| 26 | + {messages.map((msg, index) => ( |
| 27 | + <Box key={index} marginBottom={0}> |
| 28 | + {msg.role === "user" && ( |
| 29 | + <Box marginBottom={1}> |
| 30 | + <Text color="white">{`| `}</Text> |
| 31 | + <Text color="gray">{msg.content}</Text> |
| 32 | + </Box> |
| 33 | + )} |
| 34 | + |
| 35 | + {msg.role === "assistant" && ( |
| 36 | + <Box marginBottom={1} paddingLeft={2}> |
| 37 | + <Text wrap="wrap" color="white"> |
| 38 | + {msg.content} |
| 39 | + </Text> |
| 40 | + </Box> |
| 41 | + )} |
| 42 | + |
| 43 | + {msg.role === "tool" && ( |
| 44 | + <Box marginBottom={1} paddingLeft={2}> |
| 45 | + <Text color="gray" dimColor> |
| 46 | + {msg.toolName && msg.content.includes("Executing") |
| 47 | + ? `[${msg.toolName}] ${msg.content |
| 48 | + .replace(`Executing ${msg.toolName}...`, "") |
| 49 | + .replace(/^\(.*\)\.\.\./, "")}` |
| 50 | + : msg.content |
| 51 | + .replace("✅", "") |
| 52 | + .replace("❌ Error:", "ERROR:") |
| 53 | + .trim()} |
| 54 | + </Text> |
| 55 | + </Box> |
| 56 | + )} |
| 57 | + |
| 58 | + {msg.role === "system" && ( |
| 59 | + <Box marginBottom={1} paddingLeft={2}> |
| 60 | + <Text color="red"> |
| 61 | + ERROR: {msg.content.replace("❌ Error:", "").trim()} |
| 62 | + </Text> |
| 63 | + </Box> |
| 64 | + )} |
| 65 | + </Box> |
| 66 | + ))} |
| 67 | + |
| 68 | + {isProcessing && ( |
| 69 | + <Box marginBottom={1} gap={1}> |
| 70 | + <Text color="green"> |
| 71 | + <Spinner type="star" /> |
| 72 | + </Text> |
| 73 | + <Text>Working...</Text> |
| 74 | + </Box> |
| 75 | + )} |
| 76 | + </Box> |
| 77 | + ); |
| 78 | +}); |
0 commit comments