Elffuss BraidHub

kikocis/continue public

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