UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

88 lines (87 loc) • 3.42 kB
import "./agent-scope-CESPhp3L.js"; import { u as normalizeAgentId } from "./session-key-BsbwBJwv.js"; import { c as resolveDefaultAgentId } from "./agent-scope-config-ChfGvhEr.js"; import { M as resolveStoredSessionKeyForAgentStore, k as canonicalizeSpawnedByForAgent } from "./store-CWhnH-Ye.js"; import { u as resolveStorePath } from "./paths-BcGVZo_k.js"; import { i as resolveSessionStoreTargets, n as resolveAgentSessionStoreTargetsSync, r as resolveAllAgentSessionStoreTargetsSync } from "./targets-BQdH23Bu.js"; import { u as listSessionEntries } from "./session-accessor-ArmkJJUW.js"; //#region src/config/sessions/combined-store-gateway.ts function isStorePathTemplate(store) { return typeof store === "string" && store.includes("{agentId}"); } function loadGatewayStoreEntries(storePath) { return Object.fromEntries(listSessionEntries({ clone: false, storePath }).map(({ sessionKey, entry }) => [sessionKey, entry])); } function mergeSessionEntryIntoCombined(params) { const { cfg, combined, entry, agentId, canonicalKey } = params; const existing = combined[canonicalKey]; if (existing && (existing.updatedAt ?? 0) > (entry.updatedAt ?? 0)) { const spawnedBy = canonicalizeSpawnedByForAgent(cfg, agentId, existing.spawnedBy ?? entry.spawnedBy); combined[canonicalKey] = { ...entry, ...existing, spawnedBy }; return; } const spawnedBy = canonicalizeSpawnedByForAgent(cfg, agentId, entry.spawnedBy ?? existing?.spawnedBy); if (!existing && entry.spawnedBy === spawnedBy) combined[canonicalKey] = entry; else combined[canonicalKey] = { ...existing, ...entry, spawnedBy }; } /** Loads and canonicalizes session entries for gateway views across one or more agent stores. */ function loadCombinedSessionStoreForGateway(cfg, opts = {}) { const storeConfig = cfg.session?.store; if (storeConfig && !isStorePathTemplate(storeConfig)) { const storePath = resolveStorePath(storeConfig); const defaultAgentId = normalizeAgentId(resolveDefaultAgentId(cfg)); const store = loadGatewayStoreEntries(storePath); const combined = {}; for (const [key, entry] of Object.entries(store)) mergeSessionEntryIntoCombined({ cfg, combined, entry, agentId: defaultAgentId, canonicalKey: resolveStoredSessionKeyForAgentStore({ cfg, agentId: defaultAgentId, sessionKey: key }) }); return { storePath, store: combined }; } const requestedAgentId = typeof opts.agentId === "string" && opts.agentId.trim() ? normalizeAgentId(opts.agentId) : void 0; const targets = requestedAgentId ? resolveAgentSessionStoreTargetsSync(cfg, requestedAgentId) : opts.configuredAgentsOnly === true ? resolveSessionStoreTargets(cfg, { allAgents: true }) : resolveAllAgentSessionStoreTargetsSync(cfg); const combined = {}; for (const target of targets) { const agentId = target.agentId; const storePath = target.storePath; const store = loadGatewayStoreEntries(storePath); for (const [key, entry] of Object.entries(store)) mergeSessionEntryIntoCombined({ cfg, combined, entry, agentId, canonicalKey: resolveStoredSessionKeyForAgentStore({ cfg, agentId, sessionKey: key }) }); } return { storePath: targets.length === 1 ? targets[0].storePath : typeof storeConfig === "string" && storeConfig.trim() ? storeConfig.trim() : "(multiple)", store: combined }; } //#endregion export { loadCombinedSessionStoreForGateway as t };