/** * @license * Copyright 2025 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import React, { useState, useMemo, useCallback } from 'react'; import { Box, Static, Text, useStdout } from 'ink'; import { StreamingState, type HistoryItem } from './types.js'; import { useGeminiStream } from './hooks/useGeminiStream.js'; import { useLoadingIndicator } from './hooks/useLoadingIndicator.js'; import { useInputHistory } from './hooks/useInputHistory.js'; import { useThemeCommand } from './hooks/useThemeCommand.js'; import { Header } from './components/Header.js'; import { LoadingIndicator } from './components/LoadingIndicator.js'; import { InputPrompt } from './components/InputPrompt.js'; import { Footer } from './components/Footer.js'; import { ThemeDialog } from './components/ThemeDialog.js'; import { useStartupWarnings } from './hooks/useAppEffects.js'; import { shortenPath, type Config } from '@gemini-code/server'; import { Colors } from './colors.js'; import { Intro } from './components/Intro.js'; import { Tips } from './components/Tips.js'; import { ConsoleOutput } from './components/ConsolePatcher.js'; import { HistoryItemDisplay } from './components/HistoryItemDisplay.js'; import { useCompletion } from './hooks/useCompletion.js'; import { SuggestionsDisplay } from './components/SuggestionsDisplay.js'; import { isAtCommand } from './utils/commandUtils.js'; interface AppProps { config: Config; cliVersion: string; } export const App = ({ config, cliVersion }: AppProps) => { const [history, setHistory] = useState([]); const [startupWarnings, setStartupWarnings] = useState([]); const { streamingState, submitQuery, initError, debugMessage, slashCommands, } = useGeminiStream(setHistory, config); const { elapsedTime, currentLoadingPhrase } = useLoadingIndicator(streamingState); const { isThemeDialogOpen, openThemeDialog, handleThemeSelect, handleThemeHighlight, } = useThemeCommand(); useStartupWarnings(setStartupWarnings); const handleFinalSubmit = useCallback( (submittedValue: string) => { const trimmedValue = submittedValue.trim(); if (trimmedValue === '/theme') { openThemeDialog(); } else if (trimmedValue.length > 0) { submitQuery(submittedValue); } }, [openThemeDialog, submitQuery], ); const userMessages = useMemo( () => history .filter( (item): item is HistoryItem & { type: 'user'; text: string } => item.type === 'user' && typeof item.text === 'string' && item.text.trim() !== '', ) .map((item) => item.text), [history], ); const isInputActive = streamingState === StreamingState.Idle && !initError; const { query, setQuery, handleSubmit: handleHistorySubmit, inputKey, setInputKey, } = useInputHistory({ userMessages, onSubmit: handleFinalSubmit, isActive: isInputActive, }); const completion = useCompletion( query, config.getTargetDir(), isInputActive && isAtCommand(query), ); // --- Render Logic --- const { staticallyRenderedHistoryItems, updatableHistoryItems } = getHistoryRenderSlices(history); // Get terminal width const { stdout } = useStdout(); const terminalWidth = stdout?.columns ?? 80; // Calculate width for suggestions, leave some padding const suggestionsWidth = Math.max(60, Math.floor(terminalWidth * 0.8)); return ( {/* * The Static component is an Ink intrinsic in which there can only be 1 per application. * Because of this restriction we're hacking it slightly by having a 'header' item here to * ensure that it's statically rendered. * * Background on the Static Item: Anything in the Static component is written a single time * to the console. Think of it like doing a console.log and then never using ANSI codes to * clear that content ever again. Effectively it has a moving frame that every time new static * content is set it'll flush content to the terminal and move the area which it's "clearing" * down a notch. Without Static the area which gets erased and redrawn continuously grows. */} {(item, index) => { if (item === 'header') { return (
); } const historyItem = item as HistoryItem; return ( ); }} {updatableHistoryItems.length > 0 && ( {updatableHistoryItems.map((historyItem) => ( ))} )} {startupWarnings.length > 0 && ( {startupWarnings.map((warning, index) => ( {warning} ))} )} {isThemeDialogOpen ? ( ) : ( <> {isInputActive && ( <> cwd: {shortenPath(config.getTargetDir(), 70)} {completion.showSuggestions && ( )} )} )} {initError && streamingState !== StreamingState.Responding && ( {history.find( (item) => item.type === 'error' && item.text?.includes(initError), )?.text ? ( { history.find( (item) => item.type === 'error' && item.text?.includes(initError), )?.text } ) : ( <> Initialization Error: {initError} {' '} Please check API key and configuration. )} )}