refactor(sessions): share runtime transcript context resolution

This commit is contained in:
Vincent Koc
2026-06-22 22:39:32 +08:00
parent 28b374a8a7
commit 008d101b16
+34 -24
View File
@@ -1482,18 +1482,8 @@ async function persistExpectedSessionTranscriptTurn(
export async function resolveSessionTranscriptRuntimeTarget(
scope: SessionTranscriptRuntimeScope,
): Promise<SessionTranscriptRuntimeTarget> {
const agentId = scope.agentId ?? resolveAgentIdFromSessionKey(scope.sessionKey);
if (!agentId) {
throw new Error(`Cannot resolve transcript scope without an agent id: ${scope.sessionKey}`);
}
const sessionStore = scope.storePath
? loadSessionStore(scope.storePath, { skipCache: true })
: undefined;
const resolvedStoreEntry = sessionStore
? resolveSessionStoreEntry({ store: sessionStore, sessionKey: scope.sessionKey })
: undefined;
const sessionEntry = resolvedStoreEntry?.existing ?? loadSessionEntry(scope);
const sessionKey = resolvedStoreEntry?.normalizedKey ?? scope.sessionKey;
const { agentId, sessionEntry, sessionKey, sessionStore } =
resolveSessionTranscriptRuntimeContext(scope);
if (scope.sessionFile?.trim()) {
return {
agentId,
@@ -1552,18 +1542,8 @@ export async function resolveSessionTranscriptRuntimeTarget(
export async function resolveSessionTranscriptRuntimeReadTarget(
scope: SessionTranscriptRuntimeScope,
): Promise<SessionTranscriptRuntimeTarget> {
const agentId = scope.agentId ?? resolveAgentIdFromSessionKey(scope.sessionKey);
if (!agentId) {
throw new Error(`Cannot resolve transcript scope without an agent id: ${scope.sessionKey}`);
}
const sessionStore = scope.storePath
? loadSessionStore(scope.storePath, { skipCache: true })
: undefined;
const resolvedStoreEntry = sessionStore
? resolveSessionStoreEntry({ store: sessionStore, sessionKey: scope.sessionKey })
: undefined;
const sessionEntry = resolvedStoreEntry?.existing ?? loadSessionEntry(scope);
const sessionKey = resolvedStoreEntry?.normalizedKey ?? scope.sessionKey;
const { agentId, sessionEntry, sessionKey, sessionStore } =
resolveSessionTranscriptRuntimeContext(scope);
if (scope.sessionFile?.trim()) {
return {
agentId,
@@ -1599,6 +1579,36 @@ export async function resolveSessionTranscriptRuntimeReadTarget(
};
}
type SessionTranscriptRuntimeContext = {
agentId: string;
sessionEntry: SessionEntry | undefined;
sessionKey: string;
sessionStore: Record<string, SessionEntry> | undefined;
};
function resolveSessionTranscriptRuntimeContext(
scope: SessionTranscriptRuntimeScope,
): SessionTranscriptRuntimeContext {
const agentId = scope.agentId ?? resolveAgentIdFromSessionKey(scope.sessionKey);
if (!agentId) {
throw new Error(`Cannot resolve transcript scope without an agent id: ${scope.sessionKey}`);
}
const sessionStore = scope.storePath
? loadSessionStore(scope.storePath, { skipCache: true })
: undefined;
const resolvedStoreEntry = sessionStore
? resolveSessionStoreEntry({ store: sessionStore, sessionKey: scope.sessionKey })
: undefined;
const sessionEntry = resolvedStoreEntry?.existing ?? loadSessionEntry(scope);
const sessionKey = resolvedStoreEntry?.normalizedKey ?? scope.sessionKey;
return {
agentId,
sessionKey,
sessionStore,
sessionEntry,
};
}
/**
* Resolves the current file-backed target for read-only transcript callers.
* Unlike writer/runtime resolution, this does not persist missing sessionFile