| 1 | import type { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml"; |
| 2 | import { Box, Static, Text, useStdout } from "ink"; |
| 3 | import React, { useCallback, useEffect, useRef, useState } from "react"; |
| 4 | |
| 5 | import type { ChatHistoryItem } from "../../../../../core/index.js"; |
| 6 | import type { MCPService } from "../../services/MCPService.js"; |
| 7 | import type { QueuedMessage } from "../../stream/messageQueue.js"; |
| 8 | import { useTerminalSize } from "../hooks/useTerminalSize.js"; |
| 9 | import { IntroMessage } from "../IntroMessage.js"; |
| 10 | |
| 11 | interface StaticChatContentProps { |
| 12 | showIntroMessage: boolean; |
| 13 | config?: AssistantUnrolled; |
| 14 | model?: ModelConfig; |
| 15 | mcpService?: MCPService; |
| 16 | chatHistory: ChatHistoryItem[]; |
| 17 | queuedMessages?: QueuedMessage[]; |
| 18 | renderMessage: (item: ChatHistoryItem, index: number) => React.ReactElement; |
| 19 | refreshTrigger?: number; // Add a prop to trigger refresh from parent |
| 20 | } |
| 21 | |
| 22 | export const StaticChatContent: React.FC<StaticChatContentProps> = ({ |
| 23 | showIntroMessage, |
| 24 | config, |
| 25 | model, |
| 26 | mcpService, |
| 27 | chatHistory, |
| 28 | queuedMessages = [], |
| 29 | renderMessage, |
| 30 | refreshTrigger, |
| 31 | }) => { |
| 32 | const { columns, rows } = useTerminalSize(); |
| 33 | const { stdout } = useStdout(); |
| 34 | |
| 35 | // State for managing static refresh with key-based remounting (gemini-cli approach) |
| 36 | const [staticKey, setStaticKey] = useState(0); |
| 37 | const isInitialMount = useRef(true); |
| 38 | |
| 39 | // Refresh function that clears terminal and remounts Static component |
| 40 | const refreshStatic = useCallback(() => { |
| 41 | // Clear terminal completely including scrollback buffer (3J) |
| 42 | stdout.write("\x1b[2J\x1b[H"); |
| 43 | setStaticKey((prev) => prev + 1); |
| 44 | stdout.write("\x1b[3J"); |
| 45 | }, [stdout]); |
| 46 | |
| 47 | // Debounced terminal resize handler (300ms like gemini-cli) |
| 48 | useEffect(() => { |
| 49 | // Skip refreshing Static during first mount |
| 50 | if (isInitialMount.current) { |
| 51 | isInitialMount.current = false; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // Debounce so it doesn't fire too often during resize |
| 56 | const handler = setTimeout(() => { |
| 57 | refreshStatic(); |
| 58 | }, 300); |
| 59 | |
| 60 | return () => { |
| 61 | clearTimeout(handler); |
| 62 | }; |
| 63 | }, [columns, rows, refreshStatic]); |
| 64 | |
| 65 | // Trigger refresh when refreshTrigger prop changes (for /clear command) |
| 66 | useEffect(() => { |
| 67 | if (refreshTrigger !== undefined && refreshTrigger > 0) { |
| 68 | refreshStatic(); |
| 69 | } |
| 70 | }, [refreshTrigger, refreshStatic]); |
| 71 | |
| 72 | // Filter out system messages without content |
| 73 | const filteredChatHistory = React.useMemo(() => { |
| 74 | return chatHistory.filter( |
| 75 | (item) => item.message.role !== "system" || item.message.content, |
| 76 | ); |
| 77 | }, [chatHistory]); |
| 78 | |
| 79 | // Split chat history into stable and pending items |
| 80 | // The last two items may have pending tool calls |
| 81 | const { staticItems, pendingItems } = React.useMemo(() => { |
| 82 | // Add intro message as first item if it should be shown |
| 83 | const staticItems: React.ReactElement[] = []; |
| 84 | if (showIntroMessage) { |
| 85 | staticItems.push( |
| 86 | <IntroMessage |
| 87 | key="intro" |
| 88 | config={config} |
| 89 | model={model} |
| 90 | mcpService={mcpService} |
| 91 | />, |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | const PENDING_ITEMS_COUNT = 1; |
| 96 | const stableCount = Math.max( |
| 97 | 0, |
| 98 | filteredChatHistory.length - PENDING_ITEMS_COUNT, |
| 99 | ); |
| 100 | const stableHistory = filteredChatHistory.slice(0, stableCount); |
| 101 | const pendingHistory = filteredChatHistory.slice(stableCount); |
| 102 | |
| 103 | // Add stable messages to static items |
| 104 | stableHistory.forEach((item, index) => { |
| 105 | staticItems.push(renderMessage(item, index)); |
| 106 | }); |
| 107 | |
| 108 | // Pending items will be rendered dynamically outside Static |
| 109 | const pendingItems = pendingHistory.map((item, index) => |
| 110 | renderMessage(item, stableCount + index), |
| 111 | ); |
| 112 | |
| 113 | return { |
| 114 | staticItems, |
| 115 | pendingItems, |
| 116 | }; |
| 117 | }, [ |
| 118 | showIntroMessage, |
| 119 | config, |
| 120 | model, |
| 121 | mcpService, |
| 122 | filteredChatHistory, |
| 123 | renderMessage, |
| 124 | ]); |
| 125 | |
| 126 | return ( |
| 127 | <Box flexDirection="column"> |
| 128 | {/* Static content - items that won't change */} |
| 129 | <Static |
| 130 | key={staticKey} |
| 131 | items={staticItems} |
| 132 | style={{ |
| 133 | width: columns - 1, |
| 134 | textWrap: "wrap", |
| 135 | }} |
| 136 | > |
| 137 | {(item) => item} |
| 138 | </Static> |
| 139 | |
| 140 | {/* Pending area - dynamically rendered items that can update */} |
| 141 | <Box flexDirection="column"> |
| 142 | {pendingItems.map((item, index) => ( |
| 143 | <React.Fragment key={`pending-${index}`}>{item}</React.Fragment> |
| 144 | ))} |
| 145 | </Box> |
| 146 | |
| 147 | {/* Queued messages - show at bottom with queue indicators */} |
| 148 | {queuedMessages.length > 0 && ( |
| 149 | <Box paddingLeft={2} paddingBottom={1}> |
| 150 | <Text color="gray" italic> |
| 151 | {queuedMessages.map((msg) => msg.message).join("\n")} |
| 152 | </Text> |
| 153 | </Box> |
| 154 | )} |
| 155 | </Box> |
| 156 | ); |
| 157 | }; |