UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

173 lines (172 loc) 8.76 kB
import { n as getRuntimeConfig } from "./io.runtime-B9iJRs3w.js"; import { n as normalizeAgentId } from "./agent-id-CeT3w4ap.js"; import { D as tryResolveLegacyCompatibilityAgentId, o as listAgentIds, t as AgentSelectionRequiredError } from "./agent-scope-config-DcbEhP0R.js"; import { O as parseAgentSessionKey } from "./session-key-BnWWjqNc.js"; import "./legacy.default-agent-owner-BGwEdQRe.js"; import { a as getNodeSqliteKysely, i as executeSqliteQueryTakeFirstSync } from "./kysely-sync-COmh4HWh.js"; import { o as resolveSessionStorePathCore } from "./paths-CXdaYWF_.js"; import { n as resolvePersistedSessionStoreOwnerForKey } from "./session-store-owner-CR2Ag3xK.js"; import "./config-Cs0XXL3x.js"; import "./session-accessor-YsytfDtG.js"; import { u as loadSessionEntryReadOnly } from "./session-accessor.sqlite-entry-CWk3jL7s.js"; import { a as normalizeStoreSessionKey } from "./store-entry-CzRELcpv.js"; import { n as canonicalizeMainSessionAlias } from "./main-session-Br0F9dzh.js"; //#region src/acp/runtime/session-meta-keys.ts function getAcpSessionKysely(db) { return getNodeSqliteKysely(db); } function selectAcpSessionRow(db, sessionKey) { return executeSqliteQueryTakeFirstSync(db, getAcpSessionKysely(db).selectFrom("acp_sessions").selectAll().where("session_key", "=", sessionKey)); } const ACP_DATABASE_KEY_PREFIX = "@acp:v1:"; const ACP_LEGACY_AGENT_SCOPED_DB_KEY_PREFIX = "@agent:"; function buildAcpDatabaseSessionKey(storeSessionKey, agentId) { const normalizedKey = storeSessionKey.trim(); const identity = [agentId ? normalizeAgentId(agentId) : null, normalizedKey]; return `${ACP_DATABASE_KEY_PREFIX}${Buffer.from(JSON.stringify(identity), "utf8").toString("base64url")}`; } function parseAcpDatabaseSessionKey(sessionKey) { if (sessionKey.startsWith(ACP_DATABASE_KEY_PREFIX)) { try { const decoded = JSON.parse(Buffer.from(sessionKey.slice(8), "base64url").toString("utf8")); if (Array.isArray(decoded) && decoded.length === 2 && (decoded[0] === null || typeof decoded[0] === "string") && typeof decoded[1] === "string") return { ...decoded[0] ? { agentId: normalizeAgentId(decoded[0]) } : {}, storeSessionKey: decoded[1] }; } catch {} return { storeSessionKey: sessionKey }; } if (!sessionKey.startsWith(ACP_LEGACY_AGENT_SCOPED_DB_KEY_PREFIX)) return { storeSessionKey: sessionKey }; const remainder = sessionKey.slice(7); const separator = remainder.indexOf(":"); return separator > 0 ? { agentId: normalizeAgentId(remainder.slice(0, separator)), storeSessionKey: remainder.slice(separator + 1) } : { storeSessionKey: sessionKey }; } function parseAcpDatabaseSessionKeyCandidates(sessionKey) { const parsed = parseAcpDatabaseSessionKey(sessionKey); if (parsed.storeSessionKey === sessionKey && parsed.agentId === void 0) return [parsed]; return [parsed, { storeSessionKey: sessionKey }]; } function resolveAcpLegacyUnscopedOwner(cfg, storeSessionKey) { if (!cfg) return; const persistedOwner = resolvePersistedSessionStoreOwnerForKey(cfg, storeSessionKey); return persistedOwner.kind === "configured" ? persistedOwner.agentId : persistedOwner.kind === "none" ? tryResolveLegacyCompatibilityAgentId(cfg) : void 0; } function legacyAcpDatabaseSessionKeys(storeSessionKey, agentId, cfg) { const normalizedKey = storeSessionKey.trim(); const keys = []; if (agentId && !parseAgentSessionKey(normalizedKey)) keys.push(`${ACP_LEGACY_AGENT_SCOPED_DB_KEY_PREFIX}${normalizeAgentId(agentId)}:${normalizedKey}`); const compatibilityOwner = resolveAcpLegacyUnscopedOwner(cfg, normalizedKey); if (parseAgentSessionKey(normalizedKey) || !agentId || compatibilityOwner === normalizeAgentId(agentId)) keys.push(normalizedKey); return [...new Set(keys)]; } function acpSessionRowMatchesEntry(row, entry) { return row.session_id == null || row.session_id === entry?.lifecycleRevision || row.session_id === entry?.sessionId && (entry?.sessionStartedAt === void 0 || row.updated_at >= entry.sessionStartedAt); } function selectAcpSessionRowForStoreEntry(db, storeSessionKey, agentId, cfg, entry) { const databaseKey = buildAcpDatabaseSessionKey(storeSessionKey, agentId); for (const key of [databaseKey, ...legacyAcpDatabaseSessionKeys(storeSessionKey, agentId, cfg)]) { const row = selectAcpSessionRow(db, key); if (row && (!entry || acpSessionRowMatchesEntry(row, entry))) return row; } } function resolveReadableAcpSessionRow(params) { const { row, entry } = params; return row && acpSessionRowMatchesEntry(row, entry) ? row : void 0; } //#endregion //#region src/acp/runtime/session-meta-store.ts /** Store binding for ACP session metadata: resolves which session-store row owns a key. */ /** Join the logical ACP key to its canonical SQLite entry without renaming ACP metadata. */ function resolveStoreEntryForSessionKey(params) { const storeSessionKey = normalizeStoreSessionKey(params.sessionKey); if (!storeSessionKey) return { storeSessionKey }; return { storeSessionKey, entry: loadSessionEntryReadOnly({ ...params, sessionKey: storeSessionKey }) }; } /** Resolves the session store path that owns an ACP session key. */ function resolveSessionStorePathForAcp(params) { const cfg = params.cfg ?? getRuntimeConfig(); const parsed = parseAgentSessionKey(params.sessionKey); const requestedAgentId = params.agentId?.trim() ? normalizeAgentId(params.agentId) : void 0; const parsedAgentId = parsed?.agentId ? normalizeAgentId(parsed.agentId) : void 0; if (requestedAgentId && parsedAgentId && requestedAgentId !== parsedAgentId) throw new AgentSelectionRequiredError(listAgentIds(cfg), { surface: `ACP session key "${params.sessionKey}"`, hint: `Agent "${requestedAgentId}" does not own agent-scoped session key "${params.sessionKey}".` }); const persistedStoreOwner = resolvePersistedSessionStoreOwnerForKey(cfg, params.sessionKey); const agentId = requestedAgentId ?? parsedAgentId; if (requestedAgentId && persistedStoreOwner.kind === "configured" && requestedAgentId !== persistedStoreOwner.agentId) throw new AgentSelectionRequiredError(listAgentIds(cfg), { surface: `ACP session key "${params.sessionKey}"`, hint: `The shared fixed-store row belongs to agent "${persistedStoreOwner.agentId}", not agent "${requestedAgentId}".` }); if (persistedStoreOwner.kind === "retired") throw new AgentSelectionRequiredError(listAgentIds(cfg), { surface: `ACP session key "${params.sessionKey}"`, hint: `The shared fixed-store row belongs to retired agent "${persistedStoreOwner.agentId}".` }); const resolvedAgentId = agentId ?? (persistedStoreOwner.kind === "configured" ? persistedStoreOwner.agentId : void 0) ?? tryResolveLegacyCompatibilityAgentId(cfg); if (!resolvedAgentId) throw new AgentSelectionRequiredError(listAgentIds(cfg), { surface: `ACP session key "${params.sessionKey}"`, hint: "Pass an explicit agent owner for this ACP session." }); const storeSessionKey = canonicalizeMainSessionAlias({ cfg, sessionKey: params.sessionKey, agentId: resolvedAgentId }); const canonicalOwner = resolvePersistedSessionStoreOwnerForKey(cfg, storeSessionKey); if (canonicalOwner.kind === "retired" || canonicalOwner.kind === "configured" && canonicalOwner.agentId !== resolvedAgentId) throw new AgentSelectionRequiredError(listAgentIds(cfg), { surface: `ACP session key "${storeSessionKey}"`, hint: "The canonical fixed-store session has a different or retired owner. Select its recorded owner." }); return { cfg, storeSessionKey, agentId: resolvedAgentId, storePath: resolveSessionStorePathCore(cfg.session?.store, { agentId: resolvedAgentId, env: params.env }) }; } /** Reads the canonical session binding while retaining ACP's logical key. */ function readSessionEntryFromStore(params) { const { cfg, agentId, storePath, storeSessionKey: canonicalKey } = resolveSessionStorePathForAcp({ sessionKey: params.sessionKey, agentId: params.agentId, cfg: params.cfg, env: params.env }); try { const { storeSessionKey, entry } = resolveStoreEntryForSessionKey({ ...agentId ? { agentId } : {}, storePath, sessionKey: canonicalKey, ...params.clone === false ? { clone: false } : {} }); return { cfg, agentId, storePath, storeSessionKey, entry }; } catch { return { cfg, agentId, storePath, storeSessionKey: canonicalKey, storeReadFailed: true }; } } //#endregion export { buildAcpDatabaseSessionKey as a, parseAcpDatabaseSessionKeyCandidates as c, selectAcpSessionRowForStoreEntry as d, acpSessionRowMatchesEntry as i, resolveReadableAcpSessionRow as l, resolveSessionStorePathForAcp as n, getAcpSessionKysely as o, resolveStoreEntryForSessionKey as r, legacyAcpDatabaseSessionKeys as s, readSessionEntryFromStore as t, selectAcpSessionRow as u };