Elffuss BraidHub

kikocis/continue public

32d3dfe694fd · 5.1 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 // Only put items in pending if they contain tool calls with "calling" status
78 // Everything else goes into static content
79 const { staticItems, pendingItems } = React.useMemo(() => {
80 const items: React.ReactElement[] = [];
81
82 // Add intro message as first item if it should be shown
83 if (showIntroMessage) {
84 items.push(
85 <IntroMessage
86 key="intro"
87 config={config}
88 model={model}
89 mcpService={mcpService}
90 />,
91 );
92 }
93
94 // Helper function to check if an item has pending tool calls
95 const hasPendingToolCalls = (item: ChatHistoryItem): boolean => {
96 return !!(
97 item.toolCallStates &&
98 item.toolCallStates.some(
99 (toolState) =>
100 toolState.status === "calling" ||
101 toolState.status === "generating" ||
102 toolState.status === "generated",
103 )
104 );
105 };
106
107 // Find the first message with pending tool calls from the end
108 let pendingStartIndex = filteredChatHistory.length;
109 for (let i = filteredChatHistory.length - 1; i >= 0; i--) {
110 if (hasPendingToolCalls(filteredChatHistory[i])) {
111 pendingStartIndex = i;
112 // If there's a message after this one, include it too as it might be related
113 if (i + 1 < filteredChatHistory.length) {
114 // Keep the pending start index as is, so we include the next message
115 }
116 break;
117 }
118 }
119
120 const stableHistory = filteredChatHistory.slice(0, pendingStartIndex);
121 const pendingHistory = filteredChatHistory.slice(pendingStartIndex);
122
123 // Add stable messages to static items
124 stableHistory.forEach((item, index) => {
125 items.push(renderMessage(item, index));
126 });
127
128 // Pending items will be rendered dynamically outside Static
129 const pendingElements = pendingHistory.map((item, index) =>
130 renderMessage(item, pendingStartIndex + index),
131 );
132
133 return {
134 staticItems: items,
135 pendingItems: pendingElements,
136 };
137 }, [
138 showIntroMessage,
139 config,
140 model,
141 mcpService,
142 filteredChatHistory,
143 renderMessage,
144 ]);
145
146 return (
147 <Box flexDirection="column">
148 {/* Static content - items that won't change */}
149 <Static
150 key={staticKey}
151 items={staticItems}
152 style={{
153 width: columns - 1,
154 textWrap: "wrap",
155 }}
156 >
157 {(item) => item}
158 </Static>
159
160 {/* Pending area - dynamically rendered items that can update */}
161 <Box flexDirection="column">
162 {pendingItems.map((item, index) => (
163 <React.Fragment key={`pending-${index}`}>{item}</React.Fragment>
164 ))}
165 </Box>
166 </Box>
167 );
168};