From 228c978147a7ce90a31fcd804eac54097ce099ef Mon Sep 17 00:00:00 2001 From: Adam Weidman Date: Mon, 9 Mar 2026 00:26:58 -0400 Subject: [PATCH] simplify background execution helper usage in stream hook --- .../cli/src/ui/hooks/useGeminiStream.test.tsx | 7 +-- packages/cli/src/ui/hooks/useGeminiStream.ts | 62 +++++++------------ 2 files changed, 23 insertions(+), 46 deletions(-) diff --git a/packages/cli/src/ui/hooks/useGeminiStream.test.tsx b/packages/cli/src/ui/hooks/useGeminiStream.test.tsx index 53afe5022b..e60ff906fe 100644 --- a/packages/cli/src/ui/hooks/useGeminiStream.test.tsx +++ b/packages/cli/src/ui/hooks/useGeminiStream.test.tsx @@ -146,11 +146,8 @@ vi.mock('@google/gemini-cli-core', async (importOriginal) => { const actualCoreModule = (await importOriginal()) as any; return { ...actualCoreModule, - isBackgroundExecutionData: - actualCoreModule.isBackgroundExecutionData ?? - mockIsBackgroundExecutionData, - getBackgroundExecutionId: - actualCoreModule.getBackgroundExecutionId ?? mockGetBackgroundExecutionId, + isBackgroundExecutionData: mockIsBackgroundExecutionData, + getBackgroundExecutionId: mockGetBackgroundExecutionId, GitService: vi.fn(), GeminiClient: MockedGeminiClientClass, UserPromptEvent: MockedUserPromptEvent, diff --git a/packages/cli/src/ui/hooks/useGeminiStream.ts b/packages/cli/src/ui/hooks/useGeminiStream.ts index 8e768f1aea..65bbd8b684 100644 --- a/packages/cli/src/ui/hooks/useGeminiStream.ts +++ b/packages/cli/src/ui/hooks/useGeminiStream.ts @@ -41,6 +41,7 @@ import { isBackgroundExecutionData, } from '@google/gemini-cli-core'; import type { + BackgroundExecutionData, Config, EditorType, GeminiClient, @@ -102,13 +103,6 @@ interface BackgroundedToolInfo { initialOutput: string; } -type BackgroundExecutionDataLike = { - executionId?: number; - pid?: number; - command?: string; - initialOutput?: string; -} & Record; - enum StreamProcessingStatus { Completed, UserCancelled, @@ -120,38 +114,23 @@ const SUPPRESSED_TOOL_ERRORS_NOTE = const LOW_VERBOSITY_FAILURE_NOTE = 'This request failed. Press F12 for diagnostics, or run /settings and change "Error Verbosity" to full for full details.'; -function isBackgroundExecutionDataValidator( - candidate: unknown, -): candidate is (data: unknown) => data is BackgroundExecutionDataLike { - return typeof candidate === 'function'; -} - -function isBackgroundExecutionIdGetter( - candidate: unknown, -): candidate is (data: BackgroundExecutionDataLike) => number | undefined { - return typeof candidate === 'function'; -} - -function isBackgroundExecutionDataFromCore( - data: unknown, -): data is BackgroundExecutionDataLike { - const candidate: unknown = isBackgroundExecutionData; - if (isBackgroundExecutionDataValidator(candidate)) { - return candidate(data); - } - - return false; -} - -function getBackgroundExecutionIdFromCore( - data: BackgroundExecutionDataLike, +function normalizeBackgroundExecutionId( + data: BackgroundExecutionData, ): number | undefined { - const candidate: unknown = getBackgroundExecutionId; - if (isBackgroundExecutionIdGetter(candidate)) { - return candidate(data); - } + const executionId: unknown = getBackgroundExecutionId(data); + return typeof executionId === 'number' ? executionId : undefined; +} - return data.executionId ?? data.pid; +function normalizeBackgroundCommand(data: BackgroundExecutionData): string | undefined { + const command: unknown = data.command; + return typeof command === 'string' ? command : undefined; +} + +function normalizeBackgroundInitialOutput( + data: BackgroundExecutionData, +): string | undefined { + const initialOutput: unknown = data.initialOutput; + return typeof initialOutput === 'string' ? initialOutput : undefined; } function getBackgroundedToolInfo( @@ -159,19 +138,20 @@ function getBackgroundedToolInfo( ): BackgroundedToolInfo | undefined { const response = toolCall.response as ToolResponseWithParts; const rawData: unknown = response?.data; - if (!isBackgroundExecutionDataFromCore(rawData)) { + if (!isBackgroundExecutionData(rawData)) { return undefined; } - const executionId = getBackgroundExecutionIdFromCore(rawData); + const data: BackgroundExecutionData = rawData; + const executionId = normalizeBackgroundExecutionId(data); if (executionId === undefined) { return undefined; } return { executionId, - command: rawData.command ?? toolCall.request.name, - initialOutput: rawData.initialOutput ?? '', + command: normalizeBackgroundCommand(data) ?? toolCall.request.name, + initialOutput: normalizeBackgroundInitialOutput(data) ?? '', }; }