UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

314 lines (313 loc) • 13.1 kB
import "./src-H3RMXyFL.mjs"; import { t as expectDefined } from "./expect-DDakjvvQ.mjs"; import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs"; import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs"; import { r as isIncognitoSessionKey } from "./session-key-DW1jcHbM.mjs"; import { j as parseAgentSessionKey } from "./session-key-CqAyZeVk.mjs"; import { c as resolveOpenClawAgentSqlitePath } from "./openclaw-agent-db.paths-DJxv80ws.mjs"; import { r as cloneEnvWithPlatformSemantics } from "./config-env-vars-ByeGN-9N.mjs"; import { n as listConfiguredSessionStoreAgentIds } from "./targets-configured-agents-CQxugAIz.mjs"; import { i as readWithSessionStoreTopology } from "./session-row-changes-BA4BqVwF.mjs"; import { t as captureRuntimeConfig } from "./runtime-source-projection-DTwMWCo9.mjs"; import { C as readAgentDatabaseReadAdmissionRefusal } from "./agent-database-admission-CL6wHJ_N.mjs"; import { r as prepareAgentDatabaseDeletionSnapshotRead } from "./agent-deletion-journal.read-DjOO2RZM.mjs"; import { n as readSessionEntriesFromStoreInWorker } from "./session-entry-read-runtime-1Fp1sybj.mjs"; import { t as captureSessionActorStorageOwner } from "./session-actor-storage-binding-DMwpDsij.mjs"; import { n as captureMemoryExactSessionReader } from "./session-accessor.memory-exact-read-DXDGhyTq.mjs"; import { b as withSessionHistoryWorkerReadCandidates, v as targetDiscoveryLane } from "./session-transcript-worker-resources-B9oklCSm.mjs"; import { s as withSessionHistoryWorkerDatabases } from "./session-transcript-worker-runtime-DMpH3wlh.mjs"; import "./targets-BVE0__Jb.mjs"; import { a as prepareSessionStoreTargetInventory, n as captureSessionStoreReadCandidates } from "./session-store-target-inventory-Cf7beBnr.mjs"; import { l as selectStoredSessionLineage, s as resolveStoredSessionKeyForAgentStore } from "./session-store-key-CEwU3noi.mjs"; import { r as resolveSessionParentSessionKey } from "./session-conversation-ANXEVkYh.mjs"; import { i as resolveGatewaySessionStoreTargetWithStoreAsync } from "./session-utils-store-lookup-BrmEKLOy.mjs"; import { t as resolveSessionConfiguredDefault } from "./session-model-ref-Cha0Enxi.mjs"; import { t as resolveDirectStoredModelOverride } from "./stored-model-overrides-CyqayMPa.mjs"; import { c as storeTargetKey, n as createGatewayRetainedStoreMatcher } from "./combined-store-paths-DOdKzQiO.mjs"; import { i as mergeCombinedSessionStore, r as listMemorySessionStoreTargets, t as applyGatewaySessionStoreAdmission } from "./combined-store-gateway-CHaruHqd.mjs"; import path from "node:path"; //#region src/gateway/session-utils-store-lineage.ts /** Prepare ordinary stored lineage through the existing Gateway worker owner. */ function createGatewaySessionLineageReaderAsync(e) { const t = /* @__PURE__ */ new Map(); const read = async (n, r, i) => { const a = await resolveGatewaySessionStoreTargetWithStoreAsync({ cfg: e, key: r, ...parseAgentSessionKey(r) ? {} : { agentId: n }, preserveQualifiedAddress: i, readOnly: true, exactRead: true, clone: false, projection: "list", targetDiscoveryCache: t }); return a.store[a.canonicalKey]; }; return { readStored: (e, t) => read(e, t, true), readAlias: (e, t) => read(t, e, false) }; } /** A model selection consumes its one prepared parent without synchronous fallback reads. */ async function prepareGatewaySessionEntryReader(e) { if (e.entry?.modelOverrideSource === "default" || resolveDirectStoredModelOverride({ sessionEntry: e.entry, defaultProvider: resolveSessionConfiguredDefault(e.cfg, e.agentId).provider })) return (t) => e.store[t]; const t = e.entry?.parentSessionKey?.trim(); const n = t && t !== e.sessionKey ? t : resolveSessionParentSessionKey(e.sessionKey); const r = /* @__PURE__ */ new Map(); if (n && n !== e.sessionKey && !e.store[n]) { if (n === "global" || n === "unknown") { const t = e.readSource ?? (await resolveGatewaySessionStoreTargetWithStoreAsync({ cfg: e.cfg, agentId: e.agentId, key: e.sessionKey, readOnly: true, exactRead: true })).readSource; if (t) { const e = await readSessionEntriesFromStoreInWorker({ agentId: t.agentId, storePath: t.path, sessionKeys: [n], snapshotFields: [] }); r.set(n, e.entries.find((e) => e.sessionKey === n)?.entry); } } else { const t = createGatewaySessionLineageReaderAsync(e.cfg); const i = normalizeAgentId(parseAgentSessionKey(n)?.agentId ?? e.agentId); const a = resolveStoredSessionKeyForAgentStore({ cfg: e.cfg, agentId: i, sessionKey: n, preserveQualifiedAddress: true }); const o = await t.readStored(i, a); const s = o === void 0 && !isIncognitoSessionKey(a) ? await t.readAlias(n, e.agentId) : void 0; const c = selectStoredSessionLineage({ cfg: e.cfg, agentId: e.agentId, sessionKey: n, read: () => o, readAlias: () => s }); r.set(n, c.value); } } return (t) => e.store[t] ?? r.get(t); } //#endregion //#region src/config/sessions/combined-store-gateway-read.ts async function loadCombinedSessionStoreForGatewayCoreAsync(e, t = {}) { const n = captureRuntimeConfig(e); const r = t.includeIncognito === false ? void 0 : captureSessionActorStorageOwner({ env: t.discovery?.env }); const i = cloneEnvWithPlatformSemantics(t.discovery?.env ?? (r ? { ...process.env, OPENCLAW_STATE_DIR: path.resolve(r.path, "../../../..") } : process.env)); i.OPENCLAW_STATE_DIR = resolveStateDir(i); const a = { ...t, ...t.discovery && { discovery: { ...t.discovery, env: i } } }; const read = async () => { const e = prepareSessionStoreTargetInventory(n, listConfiguredSessionStoreAgentIds(n), i, "recovery", "read"); const t = a.discovery ?? { env: i, snapshot: (await prepareAgentDatabaseDeletionSnapshotRead({ env: i }, "runtime-discovery").read()).snapshot }; return loadCombinedSessionStore(e, { ...a, discovery: t }); }; return a.discovery ? read() : readWithSessionStoreTopology(read); } /** Capture missing owners' lineage before synchronous display and model projection. */ async function prepareCombinedSessionStoreLineage(e, t, n, r) { const i = new Map(t.targets.incognitoTargets.map((e) => [storeTargetKey(e), captureMemoryExactSessionReader(e)?.entries(t.projection) ?? []])); const a = createGatewayRetainedStoreMatcher(e, r); const o = new Set(t.targets.preparedAgentIds); const s = []; for (const { target: e, storeTarget: r } of t.reads) { const i = t.targets.sharedStoreRowOwner; const c = i?.target.storePath === e.storePath && i.target.agentId === e.agentId ? i.agentId : e.agentId; o.add(e.agentId); o.add(r.agentId); o.add(c); for (const e of n(r)) { const t = normalizeAgentId(parseAgentSessionKey(e.sessionKey)?.agentId ?? c); if (a(r.storePath, t)) continue; o.add(t); s.push({ ...e, agentId: t }); } } for (const e of t.targets.incognitoTargets) for (const t of i.get(storeTargetKey(e)) ?? []) s.push({ ...t, agentId: e.agentId }); const identity = (e, t) => JSON.stringify([e, t]); const c = /* @__PURE__ */ new Map(); for (const { agentId: t, sessionKey: n, entry: i } of s) for (const s of [ i.parentSessionKey, i.spawnedBy, resolveSessionParentSessionKey(n) ]) { const n = s?.trim(); const i = n ? parseAgentSessionKey(n) : void 0; if (!n || !i) continue; const l = normalizeAgentId(i.agentId); if (!o.has(l) && !readAgentDatabaseReadAdmissionRefusal(l, { env: r?.env }) && !a(resolveOpenClawAgentSqlitePath({ agentId: l, env: r?.env }), l)) c.set(identity(t, n), { agentId: t, parentKey: n, owner: l, storedKey: resolveStoredSessionKeyForAgentStore({ cfg: e, agentId: l, sessionKey: n, preserveQualifiedAddress: true }) }); } const l = createGatewaySessionLineageReaderAsync(e); const u = /* @__PURE__ */ new Map(); for (const { owner: e, storedKey: t } of c.values()) { const n = identity(e, t); if (!u.has(n)) u.set(n, l.readStored(e, t)); } const d = new Map(await Promise.all([...u].map(async ([e, t]) => [e, await t]))); const f = new Map(await Promise.all([...c].map(async ([e, t]) => [e, d.get(identity(t.owner, t.storedKey)) === void 0 ? await l.readAlias(t.parentKey, t.agentId) : void 0]))); return { readStored: (e, t) => d.get(identity(e, t)), readAlias: (e, t) => f.get(identity(t, e)), readIncognitoEntries: (e) => i.get(storeTargetKey(e)) ?? [] }; } /** Prepare durable topology in one worker request before a synchronous projection publication. */ async function prepareCombinedSessionStoreForGatewayAsync(e, t, n) { const r = t.discovery?.env ?? process.env; return readCombinedSessionStoreTopology(prepareSessionStoreTargetInventory(e, listConfiguredSessionStoreAgentIds(e), r, "recovery", "read"), t, n); } async function readCombinedSessionStoreTopology(e, t, n) { const { config: r, env: i } = e; const a = t.discovery ?? { env: i, snapshot: (await prepareAgentDatabaseDeletionSnapshotRead({ env: i }, "runtime-discovery").read()).snapshot }; const o = createGatewayRetainedStoreMatcher(r, a); const s = e.recoveryExclusions; const c = new Set(s?.databasePaths); const l = [...e.candidates, ...(a.snapshot?.registeredAgentDatabases ?? []).flatMap(({ agentId: e, path: t }) => s?.agentIds.includes(e) ? [] : captureSessionStoreReadCandidates(t))].filter((e) => e.scope !== "sibling-family" && !c.has(e.path) && !c.has(e.physicalPath) && !o(e.path) && !o(e.physicalPath)); return withSessionHistoryWorkerReadCandidates(l, async (e) => { const o = await e.readCombinedTopology({ config: r, options: { ...t, includeIncognito: false, discovery: { ...a, recoveryExclusions: s } }, scopeAgentIds: n }); o.prepared.targets.incognitoTargets = t.includeIncognito === false ? [] : listMemorySessionStoreTargets(i).filter((e) => !o.prepared.targets.requestedAgentId || e.agentId === o.prepared.targets.requestedAgentId); o.prepared.targets = applyGatewaySessionStoreAdmission(r, { ...t, discovery: a }, o.prepared.targets); o.prepared.reads = o.prepared.targets.durableTargets.map((e) => ({ target: e, storeTarget: expectDefined(o.prepared.targets.physicalTargets.get(storeTargetKey(e)), "physical store") })); return o; }, targetDiscoveryLane); } /** Descriptive listings consume one captured topology; later changes belong to the next read. */ async function loadCombinedSessionStore(e, t) { const { config: n, env: r } = e; const { prepared: i } = await readCombinedSessionStoreTopology(e, t); const a = { ...r, OPENCLAW_STATE_DIR: r.OPENCLAW_STATE_DIR }; return withSessionHistoryWorkerDatabases(i.reads.map(({ storeTarget: e }) => ({ agentId: e.agentId, path: e.storePath, env: r })), async (e) => { const o = await Promise.all(i.reads.map(async ({ storeTarget: n }, r) => { const o = expectDefined(e[r], "retained session store"); const { entries: s } = t.transcriptSelection ? await o.readExactEntries({ sessionKeys: t.transcriptSelection.sessionKeys, selection: { kind: "transcript-aliases", sessionIds: t.transcriptSelection.sessionIds }, projection: "list", env: a }) : await o.readEntries({ ...n, env: a, projection: i.projection, clone: false }); return [storeTargetKey(n), s]; })); const s = new Map(o); const c = t.transcriptSelection; if (c && i.reads.length > 1) { const t = o.flatMap(([, e]) => e); const n = [.../* @__PURE__ */ new Set([...c.sessionKeys, ...t.map((e) => e.sessionKey)])]; const r = [.../* @__PURE__ */ new Set([...c.sessionIds, ...t.map(({ entry: e }) => e.sessionId.trim()).filter(Boolean)])]; await Promise.all(i.reads.map(async ({ storeTarget: t }, i) => { const o = await expectDefined(e[i], "retained session store").readExactEntries({ sessionKeys: n, selection: { kind: "transcript-aliases", sessionIds: r }, projection: "list", env: a }); s.set(storeTargetKey(t), o.entries); })); const l = [...new Set([...s.values()].flatMap((e) => e.map((e) => e.sessionKey)))]; await Promise.all(i.reads.map(async ({ storeTarget: t }, n) => { const r = await expectDefined(e[n], "retained session store").readExactEntries({ sessionKeys: l, projection: "list", env: a }); s.set(storeTargetKey(t), r.entries); })); } const readEntries = (e) => expectDefined(s.get(storeTargetKey(e)), "prepared session entries"); const l = c ? void 0 : await prepareCombinedSessionStoreLineage(n, i, readEntries, t.discovery); const u = { ...t, discovery: { env: r, snapshot: t.discovery?.snapshot } }; return mergeCombinedSessionStore(n, u, i, readEntries, l ?? { readStored: () => void 0, readAlias: () => void 0 }, l?.readIncognitoEntries); }); } //#endregion export { prepareGatewaySessionEntryReader as i, prepareCombinedSessionStoreForGatewayAsync as n, prepareCombinedSessionStoreLineage as r, loadCombinedSessionStoreForGatewayCoreAsync as t };