|
| 1 | +import { normalizeOptionalString } from "../../packages/normalization-core/src/string-coerce.js"; |
| 2 | +import { uniqueStrings } from "../../packages/normalization-core/src/string-normalization.js"; |
| 3 | +import { |
| 4 | + loadTranscriptEvents, |
| 5 | + resolveSessionTranscriptRuntimeTarget, |
| 6 | +} from "../config/sessions/session-accessor.js"; |
| 7 | +import type { SessionEntry } from "../config/sessions/types.js"; |
| 8 | +import { normalizeAgentId, resolveAgentIdFromSessionKey } from "../routing/session-key.js"; |
| 9 | + |
| 10 | +const SESSION_TRANSCRIPT_MEMORY_HIT_PREFIX = "transcript"; |
| 11 | + |
| 12 | +export type SessionTranscriptEvent = unknown; |
| 13 | + |
| 14 | +export type SessionTranscriptIdentity = { |
| 15 | + agentId: string; |
| 16 | + memoryKey: SessionTranscriptMemoryHitKey; |
| 17 | + sessionId: string; |
| 18 | + sessionKey: string; |
| 19 | +}; |
| 20 | + |
| 21 | +export type SessionTranscriptMemoryHitIdentity = { |
| 22 | + agentId: string; |
| 23 | + key: SessionTranscriptMemoryHitKey; |
| 24 | + sessionId: string; |
| 25 | +}; |
| 26 | + |
| 27 | +export type SessionTranscriptMemoryHitKey = `transcript:${string}:${string}`; |
| 28 | + |
| 29 | +export type SessionTranscriptReadParams = { |
| 30 | + agentId?: string; |
| 31 | + env?: NodeJS.ProcessEnv; |
| 32 | + hydrateSkillPromptRefs?: boolean; |
| 33 | + sessionId: string; |
| 34 | + sessionKey: string; |
| 35 | + storePath?: string; |
| 36 | + threadId?: string | number; |
| 37 | +}; |
| 38 | + |
| 39 | +export type SessionTranscriptMemoryHitKeyParams = { |
| 40 | + agentId: string; |
| 41 | + sessionId: string; |
| 42 | +}; |
| 43 | + |
| 44 | +export type ResolveSessionTranscriptMemoryHitKeyParams = { |
| 45 | + includeSyntheticFallback?: boolean; |
| 46 | + key: string; |
| 47 | + store: Record<string, SessionEntry>; |
| 48 | +}; |
| 49 | + |
| 50 | +function requireMemoryKeySegment(value: string, label: string): string { |
| 51 | + const normalized = normalizeOptionalString(value); |
| 52 | + if (!normalized) { |
| 53 | + throw new Error(`Cannot build session transcript memory hit key without ${label}.`); |
| 54 | + } |
| 55 | + return encodeURIComponent(normalized); |
| 56 | +} |
| 57 | + |
| 58 | +function decodeMemoryKeySegment(value: string): string | null { |
| 59 | + try { |
| 60 | + return normalizeOptionalString(decodeURIComponent(value)) ?? null; |
| 61 | + } catch { |
| 62 | + return null; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function syntheticSessionKey(identity: SessionTranscriptMemoryHitIdentity): string { |
| 67 | + return `agent:${identity.agentId}:${identity.sessionId}`; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Builds the storage-neutral memory hit key for one session transcript. |
| 72 | + */ |
| 73 | +export function formatSessionTranscriptMemoryHitKey( |
| 74 | + params: SessionTranscriptMemoryHitKeyParams, |
| 75 | +): SessionTranscriptMemoryHitKey { |
| 76 | + const agentId = requireMemoryKeySegment(normalizeAgentId(params.agentId), "agentId"); |
| 77 | + const sessionId = requireMemoryKeySegment(params.sessionId, "sessionId"); |
| 78 | + return `${SESSION_TRANSCRIPT_MEMORY_HIT_PREFIX}:${agentId}:${sessionId}`; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Parses a storage-neutral session transcript memory hit key. |
| 83 | + */ |
| 84 | +export function parseSessionTranscriptMemoryHitKey( |
| 85 | + key: string, |
| 86 | +): SessionTranscriptMemoryHitIdentity | null { |
| 87 | + const parts = key.split(":"); |
| 88 | + if (parts.length !== 3 || parts[0] !== SESSION_TRANSCRIPT_MEMORY_HIT_PREFIX) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + const agentId = decodeMemoryKeySegment(parts[1] ?? ""); |
| 92 | + const sessionId = decodeMemoryKeySegment(parts[2] ?? ""); |
| 93 | + if (!agentId || !sessionId) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + return { |
| 97 | + agentId: normalizeAgentId(agentId), |
| 98 | + key: formatSessionTranscriptMemoryHitKey({ agentId, sessionId }), |
| 99 | + sessionId, |
| 100 | + }; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Resolves the public identity for a transcript without returning its file path. |
| 105 | + */ |
| 106 | +export async function resolveSessionTranscriptIdentity( |
| 107 | + params: SessionTranscriptReadParams, |
| 108 | +): Promise<SessionTranscriptIdentity> { |
| 109 | + const target = await resolveSessionTranscriptRuntimeTarget(params); |
| 110 | + const agentId = normalizeAgentId(target.agentId); |
| 111 | + return { |
| 112 | + agentId, |
| 113 | + memoryKey: formatSessionTranscriptMemoryHitKey({ agentId, sessionId: target.sessionId }), |
| 114 | + sessionId: target.sessionId, |
| 115 | + sessionKey: target.sessionKey, |
| 116 | + }; |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Reads transcript events by public session identity instead of file path. |
| 121 | + */ |
| 122 | +export async function readSessionTranscriptEvents( |
| 123 | + params: SessionTranscriptReadParams, |
| 124 | +): Promise<SessionTranscriptEvent[]> { |
| 125 | + return await loadTranscriptEvents(params); |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Maps a storage-neutral memory hit key back to visible session store keys. |
| 130 | + */ |
| 131 | +export function resolveSessionTranscriptMemoryHitKeyToSessionKeys( |
| 132 | + params: ResolveSessionTranscriptMemoryHitKeyParams, |
| 133 | +): string[] { |
| 134 | + const identity = parseSessionTranscriptMemoryHitKey(params.key); |
| 135 | + if (!identity) { |
| 136 | + return []; |
| 137 | + } |
| 138 | + const matches = Object.entries(params.store) |
| 139 | + .filter(([sessionKey, entry]) => { |
| 140 | + return ( |
| 141 | + entry.sessionId === identity.sessionId && |
| 142 | + normalizeAgentId(resolveAgentIdFromSessionKey(sessionKey)) === identity.agentId |
| 143 | + ); |
| 144 | + }) |
| 145 | + .map(([sessionKey]) => sessionKey); |
| 146 | + const deduped = uniqueStrings(matches); |
| 147 | + if (deduped.length > 0) { |
| 148 | + return deduped; |
| 149 | + } |
| 150 | + return params.includeSyntheticFallback === false ? [] : [syntheticSessionKey(identity)]; |
| 151 | +} |
0 commit comments