Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
82 lines
1.8 KiB
TypeScript
82 lines
1.8 KiB
TypeScript
export interface PendingCall {
|
|
toolName: string;
|
|
arguments: Record<string, unknown>;
|
|
resolve: (result: unknown) => void;
|
|
reject: (error: Error) => void;
|
|
timer: ReturnType<typeof setTimeout>;
|
|
}
|
|
|
|
export class PendingCallsManager {
|
|
private pendingCalls: Record<string, PendingCall> = {};
|
|
|
|
async waitForResult(
|
|
callId: string,
|
|
toolName: string,
|
|
args: Record<string, unknown>,
|
|
timeoutMs: number,
|
|
): Promise<unknown> {
|
|
return await new Promise((resolve, reject) => {
|
|
const timer = setTimeout(() => {
|
|
if (this.pendingCalls[callId]) {
|
|
this.reject(callId, new Error('Worker tool execution timeout'));
|
|
}
|
|
}, timeoutMs);
|
|
|
|
this.pendingCalls[callId] = {
|
|
toolName,
|
|
arguments: args,
|
|
resolve,
|
|
reject,
|
|
timer,
|
|
};
|
|
});
|
|
}
|
|
|
|
resolve(callId: string, result: unknown): boolean {
|
|
const pending = this.pendingCalls[callId];
|
|
if (pending) {
|
|
clearTimeout(pending.timer);
|
|
pending.resolve(result);
|
|
delete this.pendingCalls[callId];
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
reject(callId: string, error: Error): boolean {
|
|
const pending = this.pendingCalls[callId];
|
|
if (pending) {
|
|
clearTimeout(pending.timer);
|
|
pending.reject(error);
|
|
delete this.pendingCalls[callId];
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
get(callId: string): PendingCall | undefined {
|
|
return this.pendingCalls[callId];
|
|
}
|
|
|
|
has(callId: string): boolean {
|
|
return callId in this.pendingCalls;
|
|
}
|
|
|
|
remove(callId: string): void {
|
|
delete this.pendingCalls[callId];
|
|
}
|
|
|
|
cleanupBySessionId(sessionId: string): void {
|
|
for (const callId of Object.keys(this.pendingCalls)) {
|
|
if (callId.startsWith(`${sessionId}_`)) {
|
|
const pending = this.pendingCalls[callId];
|
|
if (pending) {
|
|
clearTimeout(pending.timer);
|
|
pending.resolve(undefined);
|
|
}
|
|
delete this.pendingCalls[callId];
|
|
}
|
|
}
|
|
}
|
|
}
|