Agents: share embedded run runtime state

This commit is contained in:
Vincent Koc
2026-03-11 19:49:36 -04:00
parent fb303d7d67
commit a744a48a0d

View File

@@ -11,12 +11,27 @@ type EmbeddedPiQueueHandle = {
abort: () => void;
};
const ACTIVE_EMBEDDED_RUNS = new Map<string, EmbeddedPiQueueHandle>();
type EmbeddedRunWaiter = {
resolve: (ended: boolean) => void;
timer: NodeJS.Timeout;
};
const EMBEDDED_RUN_WAITERS = new Map<string, Set<EmbeddedRunWaiter>>();
/**
* Use global singleton state so busy/streaming checks stay consistent even
* when the bundler emits multiple copies of this module into separate chunks.
*/
const _g = globalThis as typeof globalThis & {
__openclaw_embedded_run_state__?: {
activeRuns: Map<string, EmbeddedPiQueueHandle>;
waiters: Map<string, Set<EmbeddedRunWaiter>>;
};
};
const embeddedRunState = (_g.__openclaw_embedded_run_state__ ??= {
activeRuns: new Map<string, EmbeddedPiQueueHandle>(),
waiters: new Map<string, Set<EmbeddedRunWaiter>>(),
});
const ACTIVE_EMBEDDED_RUNS = embeddedRunState.activeRuns;
const EMBEDDED_RUN_WAITERS = embeddedRunState.waiters;
export function queueEmbeddedPiMessage(sessionId: string, text: string): boolean {
const handle = ACTIVE_EMBEDDED_RUNS.get(sessionId);