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