/** * @license * Copyright 2025 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import { type ToolCallRequestInfo, type ToolCallResponseInfo, ToolConfirmationOutcome, type ToolCallConfirmationDetails, type EditorType, Config, type ToolConfirmationPayload, type AnyDeclarativeTool, type AnyToolInvocation } from '../index.js'; import { type Part, type PartListUnion } from '@google/genai'; import { type ToolOutputSettingsProvider } from '../utils/toolOutputLimiter.js'; import type { AnsiOutput } from '../utils/terminalSerializer.js'; export type ValidatingToolCall = { status: 'validating'; request: ToolCallRequestInfo; tool: AnyDeclarativeTool; invocation: AnyToolInvocation; startTime?: number; outcome?: ToolConfirmationOutcome; }; export type ScheduledToolCall = { status: 'scheduled'; request: ToolCallRequestInfo; tool: AnyDeclarativeTool; invocation: AnyToolInvocation; startTime?: number; outcome?: ToolConfirmationOutcome; }; export type ErroredToolCall = { status: 'error'; request: ToolCallRequestInfo; response: ToolCallResponseInfo; tool?: AnyDeclarativeTool; durationMs?: number; outcome?: ToolConfirmationOutcome; }; export type SuccessfulToolCall = { status: 'success'; request: ToolCallRequestInfo; tool: AnyDeclarativeTool; response: ToolCallResponseInfo; invocation: AnyToolInvocation; durationMs?: number; outcome?: ToolConfirmationOutcome; }; export type ExecutingToolCall = { status: 'executing'; request: ToolCallRequestInfo; tool: AnyDeclarativeTool; invocation: AnyToolInvocation; liveOutput?: string | AnsiOutput; startTime?: number; outcome?: ToolConfirmationOutcome; pid?: number; }; export type CancelledToolCall = { status: 'cancelled'; request: ToolCallRequestInfo; response: ToolCallResponseInfo; tool: AnyDeclarativeTool; invocation: AnyToolInvocation; durationMs?: number; outcome?: ToolConfirmationOutcome; }; export type WaitingToolCall = { status: 'awaiting_approval'; request: ToolCallRequestInfo; tool: AnyDeclarativeTool; invocation: AnyToolInvocation; confirmationDetails: ToolCallConfirmationDetails; startTime?: number; outcome?: ToolConfirmationOutcome; }; export type Status = ToolCall['status']; export type ToolCall = ValidatingToolCall | ScheduledToolCall | ErroredToolCall | SuccessfulToolCall | ExecutingToolCall | CancelledToolCall | WaitingToolCall; export type CompletedToolCall = SuccessfulToolCall | CancelledToolCall | ErroredToolCall; export type ConfirmHandler = (toolCall: WaitingToolCall) => Promise; export type OutputUpdateHandler = (toolCallId: string, outputChunk: string | AnsiOutput) => void; export type AllToolCallsCompleteHandler = (completedToolCalls: CompletedToolCall[]) => Promise; export type ToolCallsUpdateHandler = (toolCalls: ToolCall[]) => void; export declare function convertToFunctionResponse(toolName: string, callId: string, llmContent: PartListUnion, config?: ToolOutputSettingsProvider): Part[]; export interface CoreToolSchedulerOptions { config: Config; outputUpdateHandler?: OutputUpdateHandler; onAllToolCallsComplete?: AllToolCallsCompleteHandler; onToolCallsUpdate?: ToolCallsUpdateHandler; getPreferredEditor: () => EditorType | undefined; onEditorClose: () => void; onEditorOpen?: () => void; toolContextInteractiveMode?: boolean; } export declare class CoreToolScheduler { private readonly logger; private toolRegistry; private toolCalls; private outputUpdateHandler?; private onAllToolCallsComplete?; private onToolCallsUpdate?; private getPreferredEditor; private config; private onEditorClose; private onEditorOpen?; private isFinalizingToolCalls; private isScheduling; private toolContextInteractiveMode; private requestQueue; private messageBusUnsubscribe?; private pendingConfirmations; private staleCorrelationIds; private pendingResults; private nextPublishIndex; private callIdToSignal; private processedConfirmations; private seenCallIds; private batchOutputConfig?; constructor(options: CoreToolSchedulerOptions); setCallbacks(options: CoreToolSchedulerOptions): void; /** * Handles message bus confirmation responses. * Called when PolicyEngine or other components respond via message bus. */ private handleMessageBusResponse; /** * Cleanup method to unsubscribe from message bus. * Should be called when scheduler is no longer needed. */ dispose(): void; private setStatusInternal; private setArgsInternal; private setPidInternal; private isRunning; private buildInvocation; /** * Build a friendly suggestion message when a tool can't be found. */ private getToolSuggestion; schedule(request: ToolCallRequestInfo | ToolCallRequestInfo[], signal: AbortSignal): Promise; private _schedule; handleConfirmationResponse(callId: string, originalOnConfirm: (outcome: ToolConfirmationOutcome, payload?: ToolConfirmationPayload) => Promise, outcome: ToolConfirmationOutcome, signal: AbortSignal, payload?: ToolConfirmationPayload, skipBusPublish?: boolean): Promise; private approveToolCall; private getPolicyContextFromInvocation; private evaluatePolicyDecision; private handlePolicyDenial; private publishConfirmationRequest; /** * Applies user-provided content changes to a tool call that is awaiting confirmation. * This method updates the tool's arguments and refreshes the confirmation prompt with a new diff * before the tool is scheduled for execution. * @private */ private _applyInlineModify; private bufferResult; private bufferError; /** * Buffer a cancelled placeholder so ordered publishing can skip past this * index without waiting forever. The tool is already transitioned to * 'cancelled' status before this is called. */ private bufferCancelled; private isPublishingBufferedResults; private pendingPublishRequest; private currentBatchSize; private publishBufferedResults; private publishResult; /** * Apply batch-level output limits for parallel tool batches. (#1301) * * `tool-output-max-tokens` is treated as a budget for the entire batch of * tool outputs combined, not per-tool. For batches of 2+ tools this method * divides the budget equally and stores the reduced per-tool limit in * {@link batchOutputConfig}, which {@link publishResult} picks up when * building function response parts. */ private applyBatchOutputLimits; /** * Launch a single scheduled tool call and wire up result buffering / error handling. * @requirement:HOOK-017,HOOK-019,HOOK-129,HOOK-131,HOOK-132,HOOK-134 - Hook result application */ private launchToolExecution; private attemptExecutionOfScheduledCalls; private checkAndNotifyCompletion; private notifyToolCallsUpdate; private setToolCallOutcome; private autoApproveCompatiblePendingTools; /** * Synchronously cancels all queued and active tool calls in the scheduler. * This updates the status of tracked tools to 'cancelled'. * Note: The actual async execution of tools is interrupted by the AbortSignal * passed during scheduling, which the caller is responsible for aborting. */ cancelAll(): void; }