From 0ad3816a23390950d84c9f331e6a21db29a3d2d6 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 18 Aug 2025 19:47:18 -0700 Subject: [PATCH 1/3] Fix terminal resize rendering issues with debounced refresh and performance optimizations Implemented solution based on gemini-cli approach: - Add debounced terminal refresh (300ms) to StaticChatContent component - Use staticKey to force complete Static component remounting on resize - Clear terminal with ANSI escape codes before remount to prevent artifacts - Skip initial mount to avoid unnecessary refresh during startup Performance optimizations: - Memoize expensive operations in IntroMessage (parseArgs, mcpService.getState) - Add Ctrl+L manual refresh as fallback for corrupted terminal state - Ensure MemoizedMessage and MarkdownRenderer remain optimized This eliminates the multiple input box artifacts and provides smooth resize handling without expensive re-computations on every resize. --- src/ui/IntroMessage.tsx | 28 +++++---- src/ui/components/StaticChatContent.tsx | 79 +++++++++++++++---------- src/ui/hooks/useUserInput.ts | 6 ++ 3 files changed, 70 insertions(+), 43 deletions(-) diff --git a/src/ui/IntroMessage.tsx b/src/ui/IntroMessage.tsx index 96895e046..c8011d49e 100644 --- a/src/ui/IntroMessage.tsx +++ b/src/ui/IntroMessage.tsx @@ -1,6 +1,6 @@ import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml"; import { Box, Text } from "ink"; -import React from "react"; +import React, { useMemo } from "react"; import { parseArgs } from "../args.js"; import { MCPService } from "../services/MCPService.js"; @@ -19,20 +19,24 @@ const IntroMessage: React.FC = ({ model, mcpService, }) => { - const mcpPrompts = mcpService.getState().prompts ?? []; + // Memoize expensive operations to avoid running on every resize + const { mcpPrompts, allRules, modelCapable } = useMemo(() => { + const mcpPrompts = mcpService.getState().prompts ?? []; + // Show all rules in a single section + const args = parseArgs(); + const commandLineRules = args.rules || []; + const configRules = + config.rules?.map((rule: any) => + typeof rule === "string" ? rule : rule?.name || "Unknown", + ) || []; - // Show all rules in a single section - const args = parseArgs(); - const commandLineRules = args.rules || []; - const configRules = - config.rules?.map((rule: any) => - typeof rule === "string" ? rule : rule?.name || "Unknown", - ) || []; + const allRules = [...commandLineRules, ...configRules]; - const allRules = [...commandLineRules, ...configRules]; + // Check if model is capable - now checking both name and model properties + const modelCapable = isModelCapable(model.provider, model.name, model.model); - // Check if model is capable - now checking both name and model properties - const modelCapable = isModelCapable(model.provider, model.name, model.model); + return { mcpPrompts, allRules, modelCapable }; + }, [config.rules, model.provider, model.name, model.model, mcpService]); return ( diff --git a/src/ui/components/StaticChatContent.tsx b/src/ui/components/StaticChatContent.tsx index 8294b81a0..f196b1ad9 100644 --- a/src/ui/components/StaticChatContent.tsx +++ b/src/ui/components/StaticChatContent.tsx @@ -1,6 +1,6 @@ import type { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml"; -import { Static } from "ink"; -import React from "react"; +import { Static, useStdout } from "ink"; +import React, { useCallback, useEffect, useRef, useState } from "react"; import type { MCPService } from "../../services/MCPService.js"; import { useTerminalSize } from "../hooks/useTerminalSize.js"; @@ -24,55 +24,72 @@ export const StaticChatContent: React.FC = ({ messages, renderMessage, }) => { - const { columns } = useTerminalSize(); - // Create a combined array of items to render + const { columns, rows } = useTerminalSize(); + const { stdout } = useStdout(); + + // State for managing static refresh with key-based remounting (gemini-cli approach) + const [staticKey, setStaticKey] = useState(0); + const isInitialMount = useRef(true); + + // Refresh function that clears terminal and remounts Static component + const refreshStatic = useCallback(() => { + // Clear terminal completely before remounting + stdout.write('\x1b[2J\x1b[H'); + setStaticKey((prev) => prev + 1); + }, [stdout]); + + // Debounced terminal resize handler (300ms like gemini-cli) + useEffect(() => { + // Skip refreshing Static during first mount + if (isInitialMount.current) { + isInitialMount.current = false; + return; + } + + // Debounce so it doesn't fire too often during resize + const handler = setTimeout(() => { + refreshStatic(); + }, 300); + + return () => { + clearTimeout(handler); + }; + }, [columns, rows, refreshStatic]); + + // Create static items array (similar to gemini-cli's approach) const staticItems = React.useMemo(() => { - const items: Array<{ - type: "intro" | "message"; - data: DisplayMessage | null; - index?: number; - }> = []; + const items: React.ReactElement[] = []; // Add intro message as first item if it should be shown if (showIntroMessage && config && model && mcpService) { - items.push({ type: "intro", data: null }); + items.push( + + ); } // Add all chat messages messages.forEach((message, index) => { - items.push({ type: "message", data: message, index }); + items.push(renderMessage(message, index)); }); return items; - }, [showIntroMessage, config, model, mcpService, messages]); + }, [showIntroMessage, config, model, mcpService, messages, renderMessage]); return ( - {(item) => { - if (item.type === "intro" && config && model && mcpService) { - return ( - - ); - } else if ( - item.type === "message" && - item.data && - item.index !== undefined - ) { - return renderMessage(item.data, item.index); - } - return null; - }} + {(item) => item} ); }; diff --git a/src/ui/hooks/useUserInput.ts b/src/ui/hooks/useUserInput.ts index 5d22fe0fd..ae0dea719 100644 --- a/src/ui/hooks/useUserInput.ts +++ b/src/ui/hooks/useUserInput.ts @@ -21,6 +21,12 @@ export function handleControlKeys(options: ControlKeysOptions): boolean { return true; } + // Handle Ctrl+L to refresh screen (clear terminal artifacts) + if (key.ctrl && input === "l") { + process.stdout.write("\x1b[2J\x1b[H"); + return true; + } + // Handle Shift+Tab to cycle through modes if (key.tab && key.shift && !showSlashCommands && !showFileSearch) { cycleModes().catch((error) => { From 08c4563f397325a0bf642a5b6f320f17f6e8465d Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 18 Aug 2025 21:06:44 -0700 Subject: [PATCH 2/3] chore: format --- src/ui/IntroMessage.tsx | 6 +++++- src/ui/components/StaticChatContent.tsx | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/ui/IntroMessage.tsx b/src/ui/IntroMessage.tsx index c8011d49e..16d06e379 100644 --- a/src/ui/IntroMessage.tsx +++ b/src/ui/IntroMessage.tsx @@ -33,7 +33,11 @@ const IntroMessage: React.FC = ({ const allRules = [...commandLineRules, ...configRules]; // Check if model is capable - now checking both name and model properties - const modelCapable = isModelCapable(model.provider, model.name, model.model); + const modelCapable = isModelCapable( + model.provider, + model.name, + model.model, + ); return { mcpPrompts, allRules, modelCapable }; }, [config.rules, model.provider, model.name, model.model, mcpService]); diff --git a/src/ui/components/StaticChatContent.tsx b/src/ui/components/StaticChatContent.tsx index f196b1ad9..39c23cd4f 100644 --- a/src/ui/components/StaticChatContent.tsx +++ b/src/ui/components/StaticChatContent.tsx @@ -26,15 +26,15 @@ export const StaticChatContent: React.FC = ({ }) => { const { columns, rows } = useTerminalSize(); const { stdout } = useStdout(); - + // State for managing static refresh with key-based remounting (gemini-cli approach) const [staticKey, setStaticKey] = useState(0); const isInitialMount = useRef(true); - + // Refresh function that clears terminal and remounts Static component const refreshStatic = useCallback(() => { // Clear terminal completely before remounting - stdout.write('\x1b[2J\x1b[H'); + stdout.write("\x1b[2J\x1b[H"); setStaticKey((prev) => prev + 1); }, [stdout]); @@ -68,7 +68,7 @@ export const StaticChatContent: React.FC = ({ config={config} model={model} mcpService={mcpService} - /> + />, ); } From e07354bc2874224aa8a9ad0a9aae70ddd97cd842 Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 19 Aug 2025 09:13:29 -0700 Subject: [PATCH 3/3] address feedback about memo --- src/ui/IntroMessage.tsx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ui/IntroMessage.tsx b/src/ui/IntroMessage.tsx index 16d06e379..7aad51014 100644 --- a/src/ui/IntroMessage.tsx +++ b/src/ui/IntroMessage.tsx @@ -19,9 +19,11 @@ const IntroMessage: React.FC = ({ model, mcpService, }) => { + // Get MCP prompts directly (not memoized since they can change after first render) + const mcpPrompts = mcpService.getState().prompts ?? []; + // Memoize expensive operations to avoid running on every resize - const { mcpPrompts, allRules, modelCapable } = useMemo(() => { - const mcpPrompts = mcpService.getState().prompts ?? []; + const { allRules, modelCapable } = useMemo(() => { // Show all rules in a single section const args = parseArgs(); const commandLineRules = args.rules || []; @@ -39,8 +41,8 @@ const IntroMessage: React.FC = ({ model.model, ); - return { mcpPrompts, allRules, modelCapable }; - }, [config.rules, model.provider, model.name, model.model, mcpService]); + return { allRules, modelCapable }; + }, [config.rules, model.provider, model.name, model.model]); return (