mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-28 05:16:23 -06:00
refactor(sessions): share runtime transcript context resolution
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user