diff options
| author | Brandon Keiji <[email protected]> | 2025-06-03 02:10:54 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-02 19:10:54 -0700 |
| commit | 74801e900413c2ca48a2977680e715cd28aa76d9 (patch) | |
| tree | 54a054711f11d9dd5dc9d72aed2759b8d6e13b88 /packages/cli/src/ui/hooks/useGeminiStream.ts | |
| parent | 447826ab40354c01667328a651f6a0f239825c64 (diff) | |
refactor: maintain 1 GeminiChat per GeminiClient (#710)
Diffstat (limited to 'packages/cli/src/ui/hooks/useGeminiStream.ts')
| -rw-r--r-- | packages/cli/src/ui/hooks/useGeminiStream.ts | 37 |
1 files changed, 6 insertions, 31 deletions
diff --git a/packages/cli/src/ui/hooks/useGeminiStream.ts b/packages/cli/src/ui/hooks/useGeminiStream.ts index 423f3489..284709cf 100644 --- a/packages/cli/src/ui/hooks/useGeminiStream.ts +++ b/packages/cli/src/ui/hooks/useGeminiStream.ts @@ -17,7 +17,6 @@ import { Config, MessageSenderType, ToolCallRequestInfo, - GeminiChat, } from '@gemini-code/core'; import { type PartListUnion } from '@google/genai'; import { @@ -76,7 +75,6 @@ export const useGeminiStream = ( ) => { const [initError, setInitError] = useState<string | null>(null); const abortControllerRef = useRef<AbortController | null>(null); - const chatSessionRef = useRef<GeminiChat | null>(null); const geminiClientRef = useRef<GeminiClient | null>(null); const [isResponding, setIsResponding] = useState<boolean>(false); const [pendingHistoryItemRef, setPendingHistoryItem] = @@ -256,31 +254,6 @@ export const useGeminiStream = ( ], ); - const ensureChatSession = useCallback(async (): Promise<{ - client: GeminiClient | null; - chat: GeminiChat | null; - }> => { - const currentClient = geminiClientRef.current; - if (!currentClient) { - const errorMsg = 'Gemini client is not available.'; - setInitError(errorMsg); - addItem({ type: MessageType.ERROR, text: errorMsg }, Date.now()); - return { client: null, chat: null }; - } - - if (!chatSessionRef.current) { - try { - chatSessionRef.current = await currentClient.startChat(); - } catch (err: unknown) { - const errorMsg = `Failed to start chat: ${getErrorMessage(err)}`; - setInitError(errorMsg); - addItem({ type: MessageType.ERROR, text: errorMsg }, Date.now()); - return { client: currentClient, chat: null }; - } - } - return { client: currentClient, chat: chatSessionRef.current }; - }, [addItem]); - // --- Stream Event Handlers --- const handleContentEvent = useCallback( @@ -444,9 +417,12 @@ export const useGeminiStream = ( return; } - const { client, chat } = await ensureChatSession(); + const client = geminiClientRef.current; - if (!client || !chat) { + if (!client) { + const errorMsg = 'Gemini client is not available.'; + setInitError(errorMsg); + addItem({ type: MessageType.ERROR, text: errorMsg }, Date.now()); return; } @@ -454,7 +430,7 @@ export const useGeminiStream = ( setInitError(null); try { - const stream = client.sendMessageStream(chat, queryToSend, abortSignal); + const stream = client.sendMessageStream(queryToSend, abortSignal); const processingStatus = await processGeminiStreamEvents( stream, userMessageTimestamp, @@ -487,7 +463,6 @@ export const useGeminiStream = ( streamingState, setShowHelp, prepareQueryForGemini, - ensureChatSession, processGeminiStreamEvents, pendingHistoryItemRef, addItem, |
