simplify background execution helper usage in stream hook

This commit is contained in:
Adam Weidman
2026-03-09 00:26:58 -04:00
parent 9b72826078
commit 228c978147
2 changed files with 23 additions and 46 deletions
@@ -146,11 +146,8 @@ vi.mock('@google/gemini-cli-core', async (importOriginal) => {
const actualCoreModule = (await importOriginal()) as any; const actualCoreModule = (await importOriginal()) as any;
return { return {
...actualCoreModule, ...actualCoreModule,
isBackgroundExecutionData: isBackgroundExecutionData: mockIsBackgroundExecutionData,
actualCoreModule.isBackgroundExecutionData ?? getBackgroundExecutionId: mockGetBackgroundExecutionId,
mockIsBackgroundExecutionData,
getBackgroundExecutionId:
actualCoreModule.getBackgroundExecutionId ?? mockGetBackgroundExecutionId,
GitService: vi.fn(), GitService: vi.fn(),
GeminiClient: MockedGeminiClientClass, GeminiClient: MockedGeminiClientClass,
UserPromptEvent: MockedUserPromptEvent, UserPromptEvent: MockedUserPromptEvent,
+20 -40
View File
@@ -41,6 +41,7 @@ import {
isBackgroundExecutionData, isBackgroundExecutionData,
} from '@google/gemini-cli-core'; } from '@google/gemini-cli-core';
import type { import type {
BackgroundExecutionData,
Config, Config,
EditorType, EditorType,
GeminiClient, GeminiClient,
@@ -102,13 +103,6 @@ interface BackgroundedToolInfo {
initialOutput: string; initialOutput: string;
} }
type BackgroundExecutionDataLike = {
executionId?: number;
pid?: number;
command?: string;
initialOutput?: string;
} & Record<string, unknown>;
enum StreamProcessingStatus { enum StreamProcessingStatus {
Completed, Completed,
UserCancelled, UserCancelled,
@@ -120,38 +114,23 @@ const SUPPRESSED_TOOL_ERRORS_NOTE =
const LOW_VERBOSITY_FAILURE_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.'; 'This request failed. Press F12 for diagnostics, or run /settings and change "Error Verbosity" to full for full details.';
function isBackgroundExecutionDataValidator( function normalizeBackgroundExecutionId(
candidate: unknown, data: BackgroundExecutionData,
): 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,
): number | undefined { ): number | undefined {
const candidate: unknown = getBackgroundExecutionId; const executionId: unknown = getBackgroundExecutionId(data);
if (isBackgroundExecutionIdGetter(candidate)) { return typeof executionId === 'number' ? executionId : undefined;
return candidate(data);
} }
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( function getBackgroundedToolInfo(
@@ -159,19 +138,20 @@ function getBackgroundedToolInfo(
): BackgroundedToolInfo | undefined { ): BackgroundedToolInfo | undefined {
const response = toolCall.response as ToolResponseWithParts; const response = toolCall.response as ToolResponseWithParts;
const rawData: unknown = response?.data; const rawData: unknown = response?.data;
if (!isBackgroundExecutionDataFromCore(rawData)) { if (!isBackgroundExecutionData(rawData)) {
return undefined; return undefined;
} }
const executionId = getBackgroundExecutionIdFromCore(rawData); const data: BackgroundExecutionData = rawData;
const executionId = normalizeBackgroundExecutionId(data);
if (executionId === undefined) { if (executionId === undefined) {
return undefined; return undefined;
} }
return { return {
executionId, executionId,
command: rawData.command ?? toolCall.request.name, command: normalizeBackgroundCommand(data) ?? toolCall.request.name,
initialOutput: rawData.initialOutput ?? '', initialOutput: normalizeBackgroundInitialOutput(data) ?? '',
}; };
} }