feat: add Pi conversation worker pool

This commit is contained in:
2026-08-22 23:21:46 +08:00
parent 161f3f471b
commit 9f55eec9aa
15 changed files with 3600 additions and 0 deletions

View File

@@ -177,6 +177,7 @@ export class PiWorkerProcess {
private stopping = false;
private stopPromise: Promise<PiWorkerStopResult> | null = null;
private exitResult: Promise<{ code: number | null; signal: NodeJS.Signals | null }> | null = null;
private readonly invalidationListeners = new Set<(error: PiProcessError) => void>();
constructor(options: PiWorkerProcessOptions) {
this.options = options;
@@ -314,6 +315,11 @@ export class PiWorkerProcess {
return this.rpc.subscribe(listener);
}
subscribeInvalidation(listener: (error: PiProcessError) => void): () => void {
this.invalidationListeners.add(listener);
return () => this.invalidationListeners.delete(listener);
}
stop(): Promise<PiWorkerStopResult> {
if (!this.stopPromise) this.stopPromise = this.performStop();
return this.stopPromise;
@@ -345,6 +351,15 @@ export class PiWorkerProcess {
this.invalidation = error;
this.rpc?.invalidate(error);
this.generationValue += 1;
for (const listener of this.invalidationListeners) {
try {
listener(error);
} catch (listenerError) {
this.appendDiagnostic(
`[invalidation-listener] ${listenerError instanceof Error ? listenerError.message : String(listenerError)}\n`,
);
}
}
}
private appendDiagnostic(source: string): void {