feat(exec-events): bead-2 - async exec event context
This commit is contained in:
parent
c9c2045419
commit
961dda6dfb
23
src/infra/exec-events-context.ts
Normal file
23
src/infra/exec-events-context.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { AsyncLocalStorage } from "node:async_hooks";
|
||||
import type { ExecEventContext } from "./exec-events.js";
|
||||
|
||||
const execEventContextStorage = new AsyncLocalStorage<ExecEventContext>();
|
||||
|
||||
function hasContextValues(context: ExecEventContext | undefined): boolean {
|
||||
if (!context) return false;
|
||||
return Boolean(context.runId || context.toolCallId || context.sessionKey);
|
||||
}
|
||||
|
||||
export function runWithExecEventContext<T>(context: ExecEventContext, fn: () => T): T {
|
||||
const existing = execEventContextStorage.getStore();
|
||||
const shouldBypass = !hasContextValues(context) && !hasContextValues(existing);
|
||||
if (shouldBypass) return fn();
|
||||
|
||||
const merged = existing ? { ...existing, ...context } : { ...context };
|
||||
return execEventContextStorage.run(merged, fn);
|
||||
}
|
||||
|
||||
export function getExecEventContext(): ExecEventContext | undefined {
|
||||
const store = execEventContextStorage.getStore();
|
||||
return store ? { ...store } : undefined;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user