UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

59 lines (58 loc) • 2.5 kB
import { c as normalizeOptionalString } from "./string-coerce-DW4mBlAt.js"; import { _ as uniqueStrings } from "./string-normalization-CRyoFBPt.js"; import { f as resolveAgentIdFromSessionKey, u as normalizeAgentId } from "./session-key-BsbwBJwv.js"; //#region src/plugin-sdk/session-transcript-memory-hit.ts const SESSION_TRANSCRIPT_MEMORY_HIT_PREFIX = "transcript"; function requireMemoryKeySegment(value, label) { const normalized = normalizeOptionalString(value); if (!normalized) throw new Error(`Cannot build session transcript memory hit key without ${label}.`); return encodeURIComponent(normalized); } function decodeMemoryKeySegment(value) { try { return normalizeOptionalString(decodeURIComponent(value)) ?? null; } catch { return null; } } function syntheticSessionKey(identity) { return `agent:${identity.agentId}:${identity.sessionId}`; } /** * Builds the memory hit key for one session transcript. */ function formatSessionTranscriptMemoryHitKey(params) { return `${SESSION_TRANSCRIPT_MEMORY_HIT_PREFIX}:${requireMemoryKeySegment(normalizeAgentId(params.agentId), "agentId")}:${requireMemoryKeySegment(params.sessionId, "sessionId")}`; } /** * Parses a session transcript memory hit key. */ function parseSessionTranscriptMemoryHitKey(key) { const parts = key.split(":"); if (parts.length !== 3 || parts[0] !== SESSION_TRANSCRIPT_MEMORY_HIT_PREFIX) return null; const agentId = decodeMemoryKeySegment(parts[1] ?? ""); const sessionId = decodeMemoryKeySegment(parts[2] ?? ""); if (!agentId || !sessionId) return null; return { agentId: normalizeAgentId(agentId), key: formatSessionTranscriptMemoryHitKey({ agentId, sessionId }), sessionId }; } /** * Maps a session transcript memory hit key back to visible session store keys. */ function resolveSessionTranscriptMemoryHitKeyToSessionKeys(params) { const identity = parseSessionTranscriptMemoryHitKey(params.key); if (!identity) return []; const deduped = uniqueStrings(Object.entries(params.store).filter(([sessionKey, entry]) => { return entry.sessionId === identity.sessionId && normalizeAgentId(resolveAgentIdFromSessionKey(sessionKey)) === identity.agentId; }).map(([sessionKey]) => sessionKey)); if (deduped.length > 0) return deduped; return params.includeSyntheticFallback === false ? [] : [syntheticSessionKey(identity)]; } //#endregion export { parseSessionTranscriptMemoryHitKey as n, resolveSessionTranscriptMemoryHitKeyToSessionKeys as r, formatSessionTranscriptMemoryHitKey as t };