UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

57 lines (56 loc) • 2.24 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { o as sessionChanges } from "./session-row-changes-BA4BqVwF.mjs"; import { randomUUID } from "node:crypto"; //#region src/infra/agent-run-registry-state.ts const registryState = resolveGlobalSingleton(Symbol.for("openclaw.agentRunRegistry.state"), () => ({ contexts: /* @__PURE__ */ new Map(), owners: /* @__PURE__ */ new Map(), lifecycleGeneration: randomUUID(), version: 0 })); function getAgentRunRegistryState() { return registryState; } /** Reads the process-local version of the active-run projection inputs. */ function readAgentRunIndexVersion() { return registryState.version; } function getAgentRunLifecycleGeneration() { return registryState.lifecycleGeneration; } /** Lists registered runs bound to one current session identity. */ function listAgentRunsForSession(e) { const t = getAgentRunRegistryState(); const n = []; for (const [r, i] of t.contexts) if (i.sessionKey === e.sessionKey && (!i.sessionId || i.sessionId === e.sessionId) && i.lifecycleGeneration === t.lifecycleGeneration) n.push({ runId: r, lifecycleGeneration: i.lifecycleGeneration }); return n.toSorted((e, t) => e.runId.localeCompare(t.runId)); } function getAgentRunContextOwnerStatus(e, t, n) { const r = getAgentRunRegistryState(); const i = r.owners.get(e); if (n !== r.lifecycleGeneration || i?.lifecycleGeneration !== n || !i.claimIds.has(t)) return; return i.clearRequested ? "clear-requested" : "active"; } function bumpAgentRunIndexVersion(e, t) { getAgentRunRegistryState().version += 1; for (const n of t && (t.sessionKey !== e?.sessionKey || !t.sessionKey && t.sessionId !== e?.sessionId || t.agentId !== e?.agentId) ? [t, e] : [e]) { const { sessionKey: e, sessionId: t, agentId: r } = n ?? {}; sessionChanges.emit(e ? { sessionKey: e, agentId: r, scope: "runtime" } : { all: true, scope: "agent-runs", ...t ? { runtimeSession: { sessionId: t, agentId: r } } : {} }); } } //#endregion export { listAgentRunsForSession as a, getAgentRunRegistryState as i, getAgentRunContextOwnerStatus as n, readAgentRunIndexVersion as o, getAgentRunLifecycleGeneration as r, bumpAgentRunIndexVersion as t };