/** * @license * Copyright 2025 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import React, { useState, useMemo, useCallback } from 'react'; import { Box, Static, Text } 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 { Tips } from './components/Tips.js'; import { ConsoleOutput } from './components/ConsolePatcher.js'; import { HistoryItemDisplay } from './components/HistoryItemDisplay.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 } = 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, handleSubmit: handleHistorySubmit } = useInputHistory({ userMessages, onSubmit: handleFinalSubmit, isActive: isInputActive, }); // --- Render Logic --- const staticallyRenderedHistoryItems = history.slice(0, -1); const updatableHistoryItem = history[history.length - 1]; 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 ( ); }} {updatableHistoryItem && ( )} {startupWarnings.length > 0 && ( {startupWarnings.map((warning, index) => ( {warning} ))} )} {isThemeDialogOpen ? ( ) : ( <> {isInputActive && ( <> cwd: {shortenPath(config.getTargetDir(), 70)} )} )} {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. )} )}