UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

484 lines (483 loc) • 17.5 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; 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 { n as listAgentEntries } from "./agent-roster-t1ooUdXp.mjs"; import { l as resolveSessionStorePathCore } from "./paths-DvQ5AJvD.mjs"; import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs"; import { n as listConfiguredSessionStoreAgentIds } from "./targets-configured-agents-CQxugAIz.mjs"; import "./agent-scope-BwB8YH6y.mjs"; import { n as resolveSessionStoreCompatibilityAgentId } from "./legacy.default-agent-owner-DpmHgK0A.mjs"; import { C as readAgentDatabaseReadAdmissionRefusal, r as assertAgentDatabaseReadAdmitted } from "./agent-database-admission-CL6wHJ_N.mjs"; import { i as getPreparedAgentDeletionDiscovery } from "./agent-deletion-journal-status-CZ5XyH3P.mjs"; import { c as listOpenClawRegisteredAgentDatabases, f as readOpenClawAgentDatabaseRegistryToken } from "./openclaw-agent-db-registry-listing-CbXBL50a.mjs"; import "./openclaw-agent-db-CwXW4hGa.mjs"; import { n as getAgentDatabaseStartupAdmission } from "./agent-database-startup-C_V1xkCB.mjs"; import { n as canonicalizeMainSessionAlias } from "./main-session-BNL3-mRV.mjs"; import { t as memorySessionActorRegistry } from "./session-actor-memory-registry--2J6Yi-z.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 { h as canonicalSessionKeyMigrationRequiredError } from "./session-canonical-key-q6kSkP3K.mjs"; import { a as resolveAllAgentSessionStoreTargetsSync, f as dedupeSessionStoreTargetsBySqliteTarget, r as resolveAgentSessionStoreTargetsSync } from "./targets-BVE0__Jb.mjs"; import { n as listKnownSessionStoreAgentIds } from "./targets-discovery.native-nF7R3wA1.mjs"; import { i as resolveSessionStoreAgentId, l as selectStoredSessionLineage, s as resolveStoredSessionKeyForAgentStore } from "./session-store-key-CEwU3noi.mjs"; import { a as resolveCombinedDatabasePath, c as storeTargetKey, i as isStorePathTemplate, n as createGatewayRetainedStoreMatcher, o as resolveCombinedStorePath, r as gatewayDiscoveryReadOptions, s as resolveSharedStoreRowOwner, t as createGatewayConfiguredDatabaseMatcher } from "./combined-store-paths-DOdKzQiO.mjs"; import path from "node:path"; //#region src/config/sessions/combined-store-model-sources.ts function createSessionModelSources(e, t, n, r, i) { const a = /* @__PURE__ */ new Map(); const o = /* @__PURE__ */ new Map(); const logicalKey = (e, t) => `${normalizeAgentId(e)}\0${t}`; return { prepareStore(s) { const c = storeTargetKey(s); let l = a.get(c); if (!l) { l = { entries: {}, readers: /* @__PURE__ */ new Map() }; a.set(c, l); } const { entries: u, readers: d } = l; return (a, c, l) => { u[c] = l; const f = logicalKey(a, c); if (!o.has(f)) o.set(f, l); const p = a; let m = d.get(p); if (!m) { const c = /* @__PURE__ */ new Map(); const select = (l) => { if (l === "global" || l === "unknown") return { key: l, value: u[l] }; const d = parseAgentSessionKey(l); const f = normalizeAgentId(d?.agentId ?? a); if (i(f, s.storePath)) return { key: l, value: void 0 }; const p = readAgentDatabaseReadAdmissionRefusal(f); if (p) { const e = `${p.reason}\n${p.repairHint}`; if (!t.includes(e)) t.push(e); return { key: l, value: void 0 }; } const m = c.get(l); if (m) return m; const h = selectStoredSessionLineage({ cfg: e, agentId: f, sessionKey: l, read(e, t) { const i = logicalKey(e, t); if (d && n && !n.has(e) && !o.has(i)) o.set(i, r.readStored(e, t)); return o.get(i); }, readAlias(e, t) { return d && n && !n.has(e) ? r.readAlias(l, a) : o.get(logicalKey(e, t)); } }); c.set(l, h); return h; }; m = { readSourceEntry: (e) => select(e).value, resolveSourceKey: (e) => select(e).key }; d.set(p, m); } return m; }; }, remove(e, t) { const n = a.get(storeTargetKey(e.storeTarget)); if (n) delete n.entries[t]; o.delete(logicalKey(e.agentId, t)); } }; } //#endregion //#region src/config/sessions/combined-store-gateway.ts function capturePhysicalStoreTargets() { const e = /* @__PURE__ */ new Map(); return { physicalTargets: e, onResolvedTarget: (t, n) => { e.set(storeTargetKey(t), n); } }; } let preparedConfiguredSessionStoreTargets; function mergeSessionEntryIntoCombined(e) { const { combined: t, entry: n, target: r, canonicalKey: i } = e; const a = e.projectedKey ?? i; const o = t[a]; if (o && (i === "global" || i === "unknown")) return; if (o) throw canonicalSessionKeyMigrationRequiredError(`duplicate rows resolve to canonical session key ${i}`); t[a] = n; e.targetsBySessionKey.set(a, r); } function projectGatewaySessionEntry(e, t, n) { let r = t; if (e.session?.scope === "global") for (const i of ["parentSessionKey", "spawnedBy"]) { const a = r[i]; const o = a ? parseAgentSessionKey(a) : null; if (a && o) { const s = n?.(a) ?? canonicalizeMainSessionAlias({ cfg: e, agentId: o.agentId, sessionKey: a }); if (s !== a) { if (r === t) r = { ...t }; r[i] = s; } } } return r; } function mergeOpenIncognitoStores(e) { const t = []; for (const n of e.targets) { const r = e.readEntries ? e.readEntries(n) : captureMemoryExactSessionReader(n)?.entries(e.projection) ?? []; let i = false; const a = e.modelSources.prepareStore(n); const o = { agentId: n.agentId, storeTarget: n }; for (const { sessionKey: t, entry: s } of r) { if (!isIncognitoSessionKey(t) || s.incognito !== true) continue; mergeSessionEntryIntoCombined({ combined: e.combined, targetsBySessionKey: e.targetsBySessionKey, entry: s, target: { ...o, entry: s, ...a(n.agentId, t, s) }, canonicalKey: t }); i = true; } if (i) t.push(n.storePath); } return t; } function isConfiguredGatewaySessionEntry(e, t, n, r) { const isConfiguredSessionKey = (n) => { const r = normalizeOptionalString(n); return Boolean(r && t.has(normalizeAgentId(resolveSessionStoreAgentId(e, r)))); }; return n === "global" || n === "unknown" || isConfiguredSessionKey(n) || isConfiguredSessionKey(r.spawnedBy) || isConfiguredSessionKey(r.parentSessionKey); } function filterCombinedStoreToConfiguredAgents(e) { for (const [t, n] of Object.entries(e.store)) { const r = e.targetsBySessionKey.get(t)?.storeKey ?? t; if (!isConfiguredGatewaySessionEntry(e.cfg, e.configuredAgentIds, r, n)) { e.modelSources.remove(expectDefined(e.targetsBySessionKey.get(t), "filtered row target"), r); delete e.store[t]; e.targetsBySessionKey.delete(t); } } } function resolvePreparedConfiguredSessionStoreTargets(e, t) { const n = gatewayDiscoveryReadOptions(e, t); const r = t ? void 0 : readOpenClawAgentDatabaseRegistryToken(); const i = t ? void 0 : getPreparedAgentDeletionDiscovery(resolveOpenClawStateSqlitePath()); const a = t ? void 0 : preparedConfiguredSessionStoreTargets; if (a?.cfg === e && a.registryToken === r && a.deletionFacts === i) return a.resolved; const o = e.session?.store; const s = normalizeAgentId(resolveSessionStoreCompatibilityAgentId(e)); const c = listConfiguredSessionStoreAgentIds(e); const l = new Set(c); const u = []; const { physicalTargets: d, onResolvedTarget: f } = capturePhysicalStoreTargets(); let p; const m = dedupeSessionStoreTargetsBySqliteTarget([...(n.registeredDatabases ?? listOpenClawRegisteredAgentDatabases()).map(({ agentId: e, path: t }) => ({ agentId: e, storePath: t })), ...c.map((e) => ({ agentId: e, storePath: resolveSessionStorePathCore(o, { agentId: e, env: n.env }) }))].filter((e) => !n.excludeTarget(e)), { defaultAgentId: s, ...n, onResolvedTarget: f, onDiagnostic: (e) => u.push(e.message), onSharedTarget: (t, r) => { p ??= resolveSharedStoreRowOwner(e, t, r, n.env); } }); const h = Object.freeze({ configuredAgentIds: l, defaultAgentId: s, diagnostics: Object.freeze(u), durableStorePath: resolveCombinedDatabasePath(m, d), durableTargets: Object.freeze(m.map((e) => Object.freeze({ ...e }))), incognitoTargets: [], sharedStoreRowOwner: p, physicalTargets: d, storeConfig: o }); if (r) preparedConfiguredSessionStoreTargets = { cfg: e, registryToken: r, deletionFacts: i, resolved: h }; return h; } function listMemorySessionStoreTargets(e) { const t = captureSessionActorStorageOwner({ env: e }); const n = t ? path.resolve(t.path, "../../../..") : resolveStateDir(e); const r = memorySessionActorRegistry.list().filter((e) => path.resolve(e.path, "../../../..") === n).map((e) => ({ agentId: e.agentId, storePath: e.path })); if (t && !r.some((e) => e.storePath === t.path)) r.push({ agentId: t.agentId, storePath: t.path }); return r; } function resolveGatewaySessionStoreTopology(e, t) { const n = gatewayDiscoveryReadOptions(e, t.discovery); const r = e.session?.store; const i = []; const a = typeof t.agentId === "string" && t.agentId.trim() ? normalizeAgentId(t.agentId) : void 0; const o = t.includeIncognito === false ? [] : listMemorySessionStoreTargets(n.env).filter((e) => !a || e.agentId === a); if (t.configuredAgentsOnly === true && !a) return { ...resolvePreparedConfiguredSessionStoreTargets(e, t.discovery), incognitoTargets: o }; const s = normalizeAgentId(resolveSessionStoreCompatibilityAgentId(e)); const { physicalTargets: c, onResolvedTarget: l } = capturePhysicalStoreTargets(); if (r && !isStorePathTemplate(r)) { const t = [.../* @__PURE__ */ new Set([ ...listAgentEntries(e).map((e) => normalizeAgentId(e.id)), ...listKnownSessionStoreAgentIds(e, n), s, ...a ? [a] : [] ])]; let u; return { defaultAgentId: s, diagnostics: i, durableTargets: dedupeSessionStoreTargetsBySqliteTarget(t.map((e) => ({ agentId: e, storePath: resolveSessionStorePathCore(r, { agentId: e, env: n.env }) })).filter((e) => !n.excludeTarget(e)), { defaultAgentId: s, ...n, onResolvedTarget: l, onDiagnostic: (e) => i.push(e.message), onSharedTarget: (t, r) => { u ??= resolveSharedStoreRowOwner(e, t, r, n.env); } }), incognitoTargets: o, requestedAgentId: a, preparedAgentIds: a ? new Set(t) : void 0, sharedStoreRowOwner: u, physicalTargets: c, storeConfig: r }; } return { defaultAgentId: s, diagnostics: i, durableTargets: a ? dedupeSessionStoreTargetsBySqliteTarget(resolveAgentSessionStoreTargetsSync(e, a, n), { defaultAgentId: s, onResolvedTarget: l, ...n }) : resolveAllAgentSessionStoreTargetsSync(e, { onResolvedTarget: l, ...n }), incognitoTargets: o, physicalTargets: c, requestedAgentId: a, preparedAgentIds: a ? /* @__PURE__ */ new Set([a]) : void 0, storeConfig: r }; } function resolveGatewaySessionStoreTargets(e, t = {}) { const n = gatewayDiscoveryReadOptions(e, t.discovery); if (t.agentId?.trim()) assertAgentDatabaseReadAdmitted(t.agentId, { env: n.env }); let r = resolveGatewaySessionStoreTopology(e, t); if (t.preserveSentinelOwners === "physical") { const { physicalTargets: t, onResolvedTarget: i } = capturePhysicalStoreTargets(); const a = /* @__PURE__ */ new Map(); const o = resolveAllAgentSessionStoreTargetsSync(e, { ...n, onResolvedTarget: ({ agentId: e }, t) => a.set(t.storePath, { agentId: e, order: a.size }) }); const s = dedupeSessionStoreTargetsBySqliteTarget([ ...r.durableTargets, ...o, ...(n.registeredDatabases ?? listOpenClawRegisteredAgentDatabases()).filter(({ path: e }) => ![...r.physicalTargets.values()].some((t) => t.storePath === e)).map(({ agentId: e, path: t }) => ({ agentId: e, storePath: t })) ].filter((e) => !n.excludeTarget(e)), { defaultAgentId: r.defaultAgentId, onResolvedTarget: i, ...n }); r = { ...r, durableTargets: s, physicalTargets: t, groupDiscovery: a }; } const i = createGatewayRetainedStoreMatcher(e, t.discovery); const a = r.durableTargets.filter((e) => { const t = r.physicalTargets.get(storeTargetKey(e)); return !i(t?.storePath ?? e.storePath, e.agentId); }); r = { ...r, durableTargets: a, ...r.durableStorePath === void 0 ? {} : { durableStorePath: resolveCombinedDatabasePath(a, r.physicalTargets) } }; return applyGatewaySessionStoreAdmission(e, t, r); } /** Admission remains with the Gateway that owns the startup refusal state. */ function applyGatewaySessionStoreAdmission(e, t, n) { const r = [...n.diagnostics]; const i = t.discovery?.env ?? process.env; if (t.agentId?.trim()) assertAgentDatabaseReadAdmitted(t.agentId, { env: i }); const a = t.preserveSentinelOwners === "physical" && getAgentDatabaseStartupAdmission() ? createGatewayConfiguredDatabaseMatcher(e, i) : void 0; const admitted = (e) => { const t = n.physicalTargets.get(storeTargetKey(e)); if (a && t && !a(t.storePath)) return false; const o = readAgentDatabaseReadAdmissionRefusal(e.agentId, { env: i }) ?? (t && readAgentDatabaseReadAdmissionRefusal(t.agentId, { env: i })); if (!o) return true; const s = `${o.reason}\n${o.repairHint}`; if (!r.includes(s)) r.push(s); return false; }; const o = n.durableTargets.filter(admitted); const s = n.incognitoTargets.filter((e) => admitted(e)); if (o.length === n.durableTargets.length && s.length === n.incognitoTargets.length) return n; return { ...n, diagnostics: r, durableTargets: o, incognitoTargets: s, ...n.durableStorePath === void 0 ? {} : { durableStorePath: resolveCombinedDatabasePath(o, n.physicalTargets) } }; } function prepareCombinedSessionStore(e, t) { const n = resolveGatewaySessionStoreTargets(e, t); return { projection: t.projection ?? "list", targets: n, reads: n.durableTargets.map((e) => ({ target: e, storeTarget: expectDefined(n.physicalTargets.get(storeTargetKey(e)), "physical store") })) }; } function mergeCombinedSessionStore(e, t, n, r, i, a) { const o = t.discovery?.env; const s = createGatewayRetainedStoreMatcher(e, t.discovery); const { projection: c } = n; const { configuredAgentIds: l, diagnostics: u, durableStorePath: d, durableTargets: f, incognitoTargets: p, physicalTargets: m, requestedAgentId: h, preparedAgentIds: g, sharedStoreRowOwner: _, storeConfig: v } = n.targets; const y = {}; const b = /* @__PURE__ */ new Map(); const x = [...u]; const S = createSessionModelSources(e, x, g, i, (e, t) => Boolean(s(t, e))); for (const { target: i, storeTarget: a } of n.reads) { const c = i.agentId; const l = i.storePath; const u = r(a); assertAgentDatabaseReadAdmitted(c, { env: o }); assertAgentDatabaseReadAdmitted(a.agentId, { env: o }); const d = _?.target.storePath === l && _.target.agentId === c ? _.agentId : c; t.onStoreLoaded?.(a, d, n.targets.groupDiscovery?.get(a.storePath) ?? null); g?.add(c); g?.add(a.agentId); g?.add(d); const f = S.prepareStore(a); for (const { sessionKey: n, entry: r } of u) { const i = parseAgentSessionKey(n); const o = resolveStoredSessionKeyForAgentStore({ cfg: e, agentId: i ? a.agentId : d, sessionKey: n, preserveQualifiedAddress: true }); if (n !== o) throw canonicalSessionKeyMigrationRequiredError(`non-canonical persisted row resolves to session key ${o}`); const c = normalizeAgentId(i?.agentId ?? d); if (s(a.storePath, c)) continue; g?.add(c); const l = f(c, o, r); if (h && c !== h) continue; const u = t.preserveSentinelOwners && (o === "global" || o === "unknown") ? JSON.stringify([o, t.preserveSentinelOwners === "physical" ? `${c}\0${a.storePath}` : c]) : o; mergeSessionEntryIntoCombined({ combined: y, targetsBySessionKey: b, entry: r, target: { agentId: c, storeTarget: a, entry: r, ...l, ...u !== o ? { storeKey: o } : {} }, canonicalKey: o, projectedKey: u }); } } const C = mergeOpenIncognitoStores({ combined: y, targetsBySessionKey: b, modelSources: S, projection: c, targets: p, readEntries: a }); if (l) filterCombinedStoreToConfiguredAgents({ cfg: e, configuredAgentIds: l, store: y, targetsBySessionKey: b, modelSources: S }); for (const [t, n] of b) y[t] = projectGatewaySessionEntry(e, y[t], n.resolveSourceKey); const w = f.map((e) => e.storePath); const T = d ?? resolveCombinedDatabasePath(f, m); return { diagnostics: x, durableStorePath: T, durableTargets: f, storePath: v && !isStorePathTemplate(v) ? C.length > 0 ? "(multiple)" : T : resolveCombinedStorePath([...w, ...C], v), store: y, targetsBySessionKey: b }; } //#endregion export { prepareCombinedSessionStore as a, mergeCombinedSessionStore as i, isConfiguredGatewaySessionEntry as n, projectGatewaySessionEntry as o, listMemorySessionStoreTargets as r, applyGatewaySessionStoreAdmission as t };