Files
gemini-cli/packages/cli/src/ui/App.tsx
T

136 lines
4.1 KiB
TypeScript
Raw Normal View History

2025-04-18 19:09:41 -04:00
import React, { useState } from 'react';
import { Box, Text } from 'ink';
2025-04-15 21:41:08 -07:00
import type { HistoryItem } from './types.js';
import { useGeminiStream } from './hooks/useGeminiStream.js';
import { useLoadingIndicator } from './hooks/useLoadingIndicator.js';
2025-04-18 19:09:41 -04:00
import { Header } from './components/Header.js';
import { Tips } from './components/Tips.js';
import { HistoryDisplay } from './components/HistoryDisplay.js';
import { LoadingIndicator } from './components/LoadingIndicator.js';
import { InputPrompt } from './components/InputPrompt.js';
import { Footer } from './components/Footer.js';
2025-04-17 12:03:02 -07:00
import { StreamingState } from '../core/gemini-stream.js';
2025-04-15 21:41:08 -07:00
import { PartListUnion } from '@google/genai';
2025-04-18 19:09:41 -04:00
import { ITermDetectionWarning } from './utils/itermDetection.js';
2025-04-18 21:55:02 +01:00
import {
useStartupWarnings,
useInitializationErrorEffect,
} from './hooks/useAppEffects.js';
2025-04-15 21:41:08 -07:00
interface AppProps {
2025-04-17 18:06:21 -04:00
directory: string;
2025-04-15 21:41:08 -07:00
}
2025-04-18 19:09:41 -04:00
export const App = ({ directory }: AppProps) => {
const [query, setQuery] = useState('');
2025-04-17 18:06:21 -04:00
const [history, setHistory] = useState<HistoryItem[]>([]);
2025-04-18 21:55:02 +01:00
const [startupWarnings, setStartupWarnings] = useState<string[]>([]);
2025-04-17 18:06:21 -04:00
const { streamingState, submitQuery, initError } =
useGeminiStream(setHistory);
const { elapsedTime, currentLoadingPhrase } =
useLoadingIndicator(streamingState);
2025-04-15 21:41:08 -07:00
2025-04-18 21:55:02 +01:00
useStartupWarnings(setStartupWarnings);
useInitializationErrorEffect(initError, history, setHistory);
2025-04-17 18:06:21 -04:00
const handleInputSubmit = (value: PartListUnion) => {
submitQuery(value)
.then(() => {
setQuery('');
})
.catch(() => {
setQuery('');
});
};
2025-04-15 21:41:08 -07:00
const isWaitingForToolConfirmation = history.some(
(item) =>
item.type === 'tool_group' &&
item.tools.some((tool) => tool.confirmationDetails !== undefined),
);
const isInputActive = streamingState === StreamingState.Idle && !initError;
2025-04-17 18:06:21 -04:00
return (
<Box flexDirection="column" padding={1} marginBottom={1} width="100%">
<Header cwd={directory} />
2025-04-15 21:41:08 -07:00
2025-04-18 21:55:02 +01:00
{startupWarnings.length > 0 && (
<Box
borderStyle="round"
borderColor="yellow"
paddingX={1}
marginY={1}
flexDirection="column"
>
{startupWarnings.map((warning, index) => (
<Text key={index} color="yellow">
{warning}
</Text>
))}
</Box>
)}
2025-04-17 18:06:21 -04:00
<Tips />
2025-04-15 21:41:08 -07:00
2025-04-17 18:06:21 -04:00
{initError &&
streamingState !== StreamingState.Responding &&
!isWaitingForToolConfirmation && (
<Box
borderStyle="round"
borderColor="red"
paddingX={1}
marginBottom={1}
>
{history.find(
(item) => item.type === 'error' && item.text?.includes(initError),
)?.text ? (
<Text color="red">
{
history.find(
(item) =>
item.type === 'error' && item.text?.includes(initError),
)?.text
}
</Text>
) : (
<>
<Text color="red">Initialization Error: {initError}</Text>
<Text color="red">
{' '}
Please check API key and configuration.
</Text>
</>
2025-04-15 21:41:08 -07:00
)}
2025-04-17 18:06:21 -04:00
</Box>
)}
2025-04-15 21:41:08 -07:00
2025-04-17 18:06:21 -04:00
<Box flexDirection="column">
<HistoryDisplay history={history} onSubmit={handleInputSubmit} />
<LoadingIndicator
isLoading={streamingState === StreamingState.Responding}
currentLoadingPhrase={currentLoadingPhrase}
elapsedTime={elapsedTime}
/>
</Box>
2025-04-15 21:41:08 -07:00
{!isWaitingForToolConfirmation && isInputActive && (
2025-04-17 18:06:21 -04:00
<InputPrompt
query={query}
setQuery={setQuery}
onSubmit={handleInputSubmit}
isActive={isInputActive}
/>
)}
2025-04-15 21:41:08 -07:00
{process.env.TERM_PROGRAM === 'iTerm.app' && (
<Box marginTop={1}>
<Text dimColor>Note: Flickering may occur in iTerm.</Text>
</Box>
)}
2025-04-17 18:06:21 -04:00
<Footer queryLength={query.length} />
2025-04-18 14:39:05 -07:00
<ITermDetectionWarning />
2025-04-17 18:06:21 -04:00
</Box>
);
2025-04-15 21:41:08 -07:00
};