Fix/issue 17070 (#17242)

Co-authored-by: Gal Zahavi <38544478+galz10@users.noreply.github.com>
This commit is contained in:
Ali Muthanna
2026-01-26 19:59:20 +03:00
committed by GitHub
parent 39e91ad633
commit 93c62a2bdc
7 changed files with 112 additions and 68 deletions

View File

@@ -4,8 +4,12 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { useCallback, useEffect, useRef } from 'react';
import type { Config, ResumedSessionData } from '@google/gemini-cli-core';
import { useCallback, useEffect, useRef, useState } from 'react';
import {
coreEvents,
type Config,
type ResumedSessionData,
} from '@google/gemini-cli-core';
import type { Part } from '@google/genai';
import type { HistoryItemWithoutId } from '../types.js';
import type { UseHistoryManagerReturn } from './useHistoryManager.js';
@@ -35,6 +39,8 @@ export function useSessionResume({
resumedSessionData,
isAuthenticating,
}: UseSessionResumeParams) {
const [isResuming, setIsResuming] = useState(false);
// Use refs to avoid dependency chain that causes infinite loop
const historyManagerRef = useRef(historyManager);
const refreshStaticRef = useRef(refreshStatic);
@@ -45,7 +51,7 @@ export function useSessionResume({
});
const loadHistoryForResume = useCallback(
(
async (
uiHistory: HistoryItemWithoutId[],
clientHistory: Array<{ role: 'user' | 'model'; parts: Part[] }>,
resumedData: ResumedSessionData,
@@ -55,17 +61,27 @@ export function useSessionResume({
return;
}
// Now that we have the client, load the history into the UI and the client.
setQuittingMessages(null);
historyManagerRef.current.clearItems();
uiHistory.forEach((item, index) => {
historyManagerRef.current.addItem(item, index, true);
});
refreshStaticRef.current(); // Force Static component to re-render with the updated history.
setIsResuming(true);
try {
// Now that we have the client, load the history into the UI and the client.
setQuittingMessages(null);
historyManagerRef.current.clearItems();
uiHistory.forEach((item, index) => {
historyManagerRef.current.addItem(item, index, true);
});
refreshStaticRef.current(); // Force Static component to re-render with the updated history.
// Give the history to the Gemini client.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
config.getGeminiClient()?.resumeChat(clientHistory, resumedData);
// Give the history to the Gemini client.
await config.getGeminiClient()?.resumeChat(clientHistory, resumedData);
} catch (error) {
coreEvents.emitFeedback(
'error',
'Failed to resume session. Please try again.',
error,
);
} finally {
setIsResuming(false);
}
},
[config, isGeminiClientInitialized, setQuittingMessages],
);
@@ -84,7 +100,7 @@ export function useSessionResume({
const historyData = convertSessionToHistoryFormats(
resumedSessionData.conversation.messages,
);
loadHistoryForResume(
void loadHistoryForResume(
historyData.uiHistory,
historyData.clientHistory,
resumedSessionData,
@@ -97,5 +113,5 @@ export function useSessionResume({
loadHistoryForResume,
]);
return { loadHistoryForResume };
return { loadHistoryForResume, isResuming };
}