import type { RunQueryOptions } from '../../../../src/plugin-sdk.js'; import type { WorkflowScheduler } from './scheduler.js'; /** Both scripted calls and TAOR tools use the same process classification. */ export function toolResource(name: string): 'tool' | 'process' { return ['Bash', 'PowerShell', 'TerminalCapture'].includes(name) ? 'process' : 'tool'; } export function scheduledAgentExecutor(options: { execute: NonNullable; scheduler: WorkflowScheduler; rootRunId: string; signal?: AbortSignal; isDelegating?: (name: string) => boolean; onStart: (name: string, queuedMs: number) => void; onFinish?: (name: string, ok: boolean, elapsedMs: number) => void; }): NonNullable { return (name, input, signal, context) => { const effectiveSignal = signal && options.signal ? AbortSignal.any([signal, options.signal]) : (signal ?? options.signal); const execute = async (queuedMs: number) => { effectiveSignal?.throwIfAborted(); options.onStart(name, queuedMs); const startedAt = Date.now(); let ok = false; try { const result = await options.execute( name, input, effectiveSignal, context, ); effectiveSignal?.throwIfAborted(); ok = result.ok; return result; } finally { options.onFinish?.(name, ok, Date.now() - startedAt); } }; return options.isDelegating?.(name) ? execute(0) : options.scheduler.run( toolResource(name), options.rootRunId, execute, effectiveSignal, ); }; } export function unknownToolNames( requested: readonly string[], known: ReadonlySet, ): string[] { return requested.filter((name) => !known.has(name)); }