import { readWorkflowRuntimeConfig, type WorkflowRuntimeConfig } from './config.js'; export type WorkflowResource = 'root' | 'model' | 'tool' | 'process'; interface Waiter { resolve: (lease: SchedulerLease) => void; reject: (error: unknown) => void; signal?: AbortSignal; queuedAt: number; } export interface SchedulerLease { queuedMs: number; release(): void; } /** FIFO between requests of one run, round-robin between workflow roots. */ class FairPool { private active = 0; private readonly queues = new Map(); private roots: string[] = []; private cursor = 0; constructor(readonly limit: number) {} acquire(rootRunId: string, signal?: AbortSignal): Promise { signal?.throwIfAborted(); if (this.active < this.limit && this.queues.size === 0) { this.active++; return Promise.resolve(this.lease(0)); } return new Promise((resolve, reject) => { const waiter: Waiter = { resolve, reject, signal, queuedAt: Date.now(), }; let queue = this.queues.get(rootRunId); if (!queue) { queue = []; this.queues.set(rootRunId, queue); this.roots.push(rootRunId); } queue.push(waiter); const abort = () => { const current = this.queues.get(rootRunId); const index = current?.indexOf(waiter) ?? -1; if (index >= 0) current!.splice(index, 1); this.prune(rootRunId); reject(signal?.reason ?? Object.assign(new Error('已取消'), { name: 'AbortError' })); }; signal?.addEventListener('abort', abort, { once: true }); const originalResolve = waiter.resolve; waiter.resolve = (lease) => { signal?.removeEventListener('abort', abort); originalResolve(lease); }; }); } async run( rootRunId: string, fn: (queuedMs: number) => Promise, signal?: AbortSignal, ): Promise { const lease = await this.acquire(rootRunId, signal); try { signal?.throwIfAborted(); return await fn(lease.queuedMs); } finally { lease.release(); } } private lease(queuedMs: number): SchedulerLease { let released = false; return { queuedMs, release: () => { if (released) return; released = true; this.dispatch(); }, }; } private dispatch(): void { const next = this.takeNext(); if (!next) { this.active--; return; } next.resolve(this.lease(Date.now() - next.queuedAt)); } private takeNext(): Waiter | null { while (this.roots.length > 0) { if (this.cursor >= this.roots.length) this.cursor = 0; const root = this.roots[this.cursor]!; const queue = this.queues.get(root); if (!queue?.length) { this.prune(root); continue; } const waiter = queue.shift()!; if (queue.length === 0) this.prune(root); else this.cursor = (this.cursor + 1) % this.roots.length; if (waiter.signal?.aborted) { waiter.reject(waiter.signal.reason); continue; } return waiter; } return null; } private prune(root: string): void { const queue = this.queues.get(root); if (queue?.length) return; this.queues.delete(root); const index = this.roots.indexOf(root); if (index < 0) return; this.roots.splice(index, 1); if (index < this.cursor) this.cursor--; if (this.cursor >= this.roots.length) this.cursor = 0; } } export class WorkflowScheduler { readonly config: WorkflowRuntimeConfig; private readonly pools: Record; constructor(config: WorkflowRuntimeConfig = readWorkflowRuntimeConfig()) { this.config = config; this.pools = { root: new FairPool(config.maxActiveRuns), model: new FairPool(config.modelConcurrency), tool: new FairPool(config.toolConcurrency), process: new FairPool(config.processConcurrency), }; } acquire(resource: WorkflowResource, rootRunId: string, signal?: AbortSignal) { return this.pools[resource].acquire(rootRunId, signal); } run( resource: WorkflowResource, rootRunId: string, fn: (queuedMs: number) => Promise, signal?: AbortSignal, ) { return this.pools[resource].run(rootRunId, fn, signal); } } let shared: WorkflowScheduler | undefined; export function sharedWorkflowScheduler(): WorkflowScheduler { return (shared ??= new WorkflowScheduler()); } export function resetWorkflowSchedulerForTests(): void { shared = undefined; }