refactor: remove unnecessary useRefs (#780)
This commit is contained in:
parent
d3a1026ae3
commit
2285bba66e
|
@ -117,6 +117,7 @@ vi.mock('@gemini-code/core', async (importOriginal) => {
|
||||||
getVertexAI: vi.fn(() => opts.vertexai),
|
getVertexAI: vi.fn(() => opts.vertexai),
|
||||||
getShowMemoryUsage: vi.fn(() => opts.showMemoryUsage ?? false),
|
getShowMemoryUsage: vi.fn(() => opts.showMemoryUsage ?? false),
|
||||||
getAccessibility: vi.fn(() => opts.accessibility ?? {}),
|
getAccessibility: vi.fn(() => opts.accessibility ?? {}),
|
||||||
|
getGeminiClient: vi.fn(() => ({})),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -201,6 +201,7 @@ export const App = ({
|
||||||
|
|
||||||
const { streamingState, submitQuery, initError, pendingHistoryItems } =
|
const { streamingState, submitQuery, initError, pendingHistoryItems } =
|
||||||
useGeminiStream(
|
useGeminiStream(
|
||||||
|
config.getGeminiClient(),
|
||||||
addItem,
|
addItem,
|
||||||
setShowHelp,
|
setShowHelp,
|
||||||
config,
|
config,
|
||||||
|
|
|
@ -304,6 +304,7 @@ describe('useGeminiStream', () => {
|
||||||
|
|
||||||
const { result, rerender } = renderHook(() =>
|
const { result, rerender } = renderHook(() =>
|
||||||
useGeminiStream(
|
useGeminiStream(
|
||||||
|
mockConfig.getGeminiClient(),
|
||||||
mockAddItem as unknown as UseHistoryManagerReturn['addItem'],
|
mockAddItem as unknown as UseHistoryManagerReturn['addItem'],
|
||||||
mockSetShowHelp,
|
mockSetShowHelp,
|
||||||
mockConfig,
|
mockConfig,
|
||||||
|
|
|
@ -65,6 +65,7 @@ enum StreamProcessingStatus {
|
||||||
* API interaction, and tool call lifecycle.
|
* API interaction, and tool call lifecycle.
|
||||||
*/
|
*/
|
||||||
export const useGeminiStream = (
|
export const useGeminiStream = (
|
||||||
|
geminiClient: GeminiClient | null,
|
||||||
addItem: UseHistoryManagerReturn['addItem'],
|
addItem: UseHistoryManagerReturn['addItem'],
|
||||||
setShowHelp: React.Dispatch<React.SetStateAction<boolean>>,
|
setShowHelp: React.Dispatch<React.SetStateAction<boolean>>,
|
||||||
config: Config,
|
config: Config,
|
||||||
|
@ -76,7 +77,6 @@ export const useGeminiStream = (
|
||||||
) => {
|
) => {
|
||||||
const [initError, setInitError] = useState<string | null>(null);
|
const [initError, setInitError] = useState<string | null>(null);
|
||||||
const abortControllerRef = useRef<AbortController | null>(null);
|
const abortControllerRef = useRef<AbortController | null>(null);
|
||||||
const geminiClientRef = useRef<GeminiClient | null>(null);
|
|
||||||
const [isResponding, setIsResponding] = useState<boolean>(false);
|
const [isResponding, setIsResponding] = useState<boolean>(false);
|
||||||
const [pendingHistoryItemRef, setPendingHistoryItem] =
|
const [pendingHistoryItemRef, setPendingHistoryItem] =
|
||||||
useStateAndRef<HistoryItemWithoutId | null>(null);
|
useStateAndRef<HistoryItemWithoutId | null>(null);
|
||||||
|
@ -142,19 +142,6 @@ export const useGeminiStream = (
|
||||||
return StreamingState.Idle;
|
return StreamingState.Idle;
|
||||||
}, [isResponding, toolCalls]);
|
}, [isResponding, toolCalls]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setInitError(null);
|
|
||||||
if (!geminiClientRef.current) {
|
|
||||||
try {
|
|
||||||
geminiClientRef.current = config.getGeminiClient();
|
|
||||||
} catch (error: unknown) {
|
|
||||||
const errorMsg = `Failed to initialize client: ${getErrorMessage(error) || 'Unknown error'}`;
|
|
||||||
setInitError(errorMsg);
|
|
||||||
addItem({ type: MessageType.ERROR, text: errorMsg }, Date.now());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [config, addItem]);
|
|
||||||
|
|
||||||
useInput((_input, key) => {
|
useInput((_input, key) => {
|
||||||
if (streamingState !== StreamingState.Idle && key.escape) {
|
if (streamingState !== StreamingState.Idle && key.escape) {
|
||||||
abortControllerRef.current?.abort();
|
abortControllerRef.current?.abort();
|
||||||
|
@ -450,9 +437,7 @@ export const useGeminiStream = (
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = geminiClientRef.current;
|
if (!geminiClient) {
|
||||||
|
|
||||||
if (!client) {
|
|
||||||
const errorMsg = 'Gemini client is not available.';
|
const errorMsg = 'Gemini client is not available.';
|
||||||
setInitError(errorMsg);
|
setInitError(errorMsg);
|
||||||
addItem({ type: MessageType.ERROR, text: errorMsg }, Date.now());
|
addItem({ type: MessageType.ERROR, text: errorMsg }, Date.now());
|
||||||
|
@ -463,7 +448,7 @@ export const useGeminiStream = (
|
||||||
setInitError(null);
|
setInitError(null);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const stream = client.sendMessageStream(queryToSend, abortSignal);
|
const stream = geminiClient.sendMessageStream(queryToSend, abortSignal);
|
||||||
const processingStatus = await processGeminiStreamEvents(
|
const processingStatus = await processGeminiStreamEvents(
|
||||||
stream,
|
stream,
|
||||||
userMessageTimestamp,
|
userMessageTimestamp,
|
||||||
|
@ -501,6 +486,7 @@ export const useGeminiStream = (
|
||||||
addItem,
|
addItem,
|
||||||
setPendingHistoryItem,
|
setPendingHistoryItem,
|
||||||
setInitError,
|
setInitError,
|
||||||
|
geminiClient,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ import {
|
||||||
Status as CoreStatus,
|
Status as CoreStatus,
|
||||||
logToolCall,
|
logToolCall,
|
||||||
} from '@gemini-code/core';
|
} from '@gemini-code/core';
|
||||||
import { useCallback, useEffect, useState, useRef } from 'react';
|
import { useCallback, useState, useMemo } from 'react';
|
||||||
import {
|
import {
|
||||||
HistoryItemToolGroup,
|
HistoryItemToolGroup,
|
||||||
IndividualToolCallDisplay,
|
IndividualToolCallDisplay,
|
||||||
|
@ -73,13 +73,9 @@ export function useReactToolScheduler(
|
||||||
const [toolCallsForDisplay, setToolCallsForDisplay] = useState<
|
const [toolCallsForDisplay, setToolCallsForDisplay] = useState<
|
||||||
TrackedToolCall[]
|
TrackedToolCall[]
|
||||||
>([]);
|
>([]);
|
||||||
const schedulerRef = useRef<CoreToolScheduler | null>(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
const outputUpdateHandler: OutputUpdateHandler = useCallback(
|
||||||
const outputUpdateHandler: OutputUpdateHandler = (
|
(toolCallId, outputChunk) => {
|
||||||
toolCallId,
|
|
||||||
outputChunk,
|
|
||||||
) => {
|
|
||||||
setPendingHistoryItem((prevItem) => {
|
setPendingHistoryItem((prevItem) => {
|
||||||
if (prevItem?.type === 'tool_group') {
|
if (prevItem?.type === 'tool_group') {
|
||||||
return {
|
return {
|
||||||
|
@ -104,11 +100,12 @@ export function useReactToolScheduler(
|
||||||
return tc;
|
return tc;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
};
|
},
|
||||||
|
[setPendingHistoryItem],
|
||||||
|
);
|
||||||
|
|
||||||
const allToolCallsCompleteHandler: AllToolCallsCompleteHandler = (
|
const allToolCallsCompleteHandler: AllToolCallsCompleteHandler = useCallback(
|
||||||
completedToolCalls,
|
(completedToolCalls) => {
|
||||||
) => {
|
|
||||||
completedToolCalls.forEach((call) => {
|
completedToolCalls.forEach((call) => {
|
||||||
let success = false;
|
let success = false;
|
||||||
let errorMessage: string | undefined;
|
let errorMessage: string | undefined;
|
||||||
|
@ -134,11 +131,12 @@ export function useReactToolScheduler(
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
onComplete(completedToolCalls);
|
onComplete(completedToolCalls);
|
||||||
};
|
},
|
||||||
|
[onComplete],
|
||||||
|
);
|
||||||
|
|
||||||
const toolCallsUpdateHandler: ToolCallsUpdateHandler = (
|
const toolCallsUpdateHandler: ToolCallsUpdateHandler = useCallback(
|
||||||
updatedCoreToolCalls: ToolCall[],
|
(updatedCoreToolCalls: ToolCall[]) => {
|
||||||
) => {
|
|
||||||
setToolCallsForDisplay((prevTrackedCalls) =>
|
setToolCallsForDisplay((prevTrackedCalls) =>
|
||||||
updatedCoreToolCalls.map((coreTc) => {
|
updatedCoreToolCalls.map((coreTc) => {
|
||||||
const existingTrackedCall = prevTrackedCalls.find(
|
const existingTrackedCall = prevTrackedCalls.find(
|
||||||
|
@ -152,27 +150,40 @@ export function useReactToolScheduler(
|
||||||
return newTrackedCall;
|
return newTrackedCall;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
};
|
|
||||||
|
|
||||||
schedulerRef.current = new CoreToolScheduler({
|
|
||||||
toolRegistry: config.getToolRegistry(),
|
|
||||||
outputUpdateHandler,
|
|
||||||
onAllToolCallsComplete: allToolCallsCompleteHandler,
|
|
||||||
onToolCallsUpdate: toolCallsUpdateHandler,
|
|
||||||
approvalMode: config.getApprovalMode(),
|
|
||||||
});
|
|
||||||
}, [config, onComplete, setPendingHistoryItem]);
|
|
||||||
|
|
||||||
const schedule: ScheduleFn = useCallback(
|
|
||||||
async (request: ToolCallRequestInfo | ToolCallRequestInfo[]) => {
|
|
||||||
schedulerRef.current?.schedule(request);
|
|
||||||
},
|
},
|
||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
|
|
||||||
const cancel: CancelFn = useCallback((reason: string = 'unspecified') => {
|
const scheduler = useMemo(
|
||||||
schedulerRef.current?.cancelAll(reason);
|
() =>
|
||||||
}, []);
|
new CoreToolScheduler({
|
||||||
|
toolRegistry: config.getToolRegistry(),
|
||||||
|
outputUpdateHandler,
|
||||||
|
onAllToolCallsComplete: allToolCallsCompleteHandler,
|
||||||
|
onToolCallsUpdate: toolCallsUpdateHandler,
|
||||||
|
approvalMode: config.getApprovalMode(),
|
||||||
|
}),
|
||||||
|
[
|
||||||
|
config,
|
||||||
|
outputUpdateHandler,
|
||||||
|
allToolCallsCompleteHandler,
|
||||||
|
toolCallsUpdateHandler,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
const schedule: ScheduleFn = useCallback(
|
||||||
|
async (request: ToolCallRequestInfo | ToolCallRequestInfo[]) => {
|
||||||
|
scheduler.schedule(request);
|
||||||
|
},
|
||||||
|
[scheduler],
|
||||||
|
);
|
||||||
|
|
||||||
|
const cancel: CancelFn = useCallback(
|
||||||
|
(reason: string = 'unspecified') => {
|
||||||
|
scheduler.cancelAll(reason);
|
||||||
|
},
|
||||||
|
[scheduler],
|
||||||
|
);
|
||||||
|
|
||||||
const markToolsAsSubmitted: MarkToolsAsSubmittedFn = useCallback(
|
const markToolsAsSubmitted: MarkToolsAsSubmittedFn = useCallback(
|
||||||
(callIdsToMark: string[]) => {
|
(callIdsToMark: string[]) => {
|
||||||
|
|
Loading…
Reference in New Issue