UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

100 lines (99 loc) • 3.76 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; //#region src/config/sessions/cli-session-binding.ts const SHA256_HEX_PATTERN = /^[a-f0-9]{64}$/; function normalizeCliTranscriptCursor(e) { const t = normalizeOptionalString(e?.localSessionId); const n = normalizeOptionalString(e?.runId); const r = e?.entryId === null ? null : normalizeOptionalString(e?.entryId); return t && n && r !== void 0 ? { localSessionId: t, entryId: r, runId: n, userEntryId: normalizeOptionalString(e?.userEntryId) } : void 0; } function normalizeCliSessionReseedReceipt(e) { const t = normalizeOptionalString(e?.promptHash); const n = normalizeOptionalString(e?.localSessionId); const r = e?.userTurnDisposition; if (e?.version !== 1 || !t || !SHA256_HEX_PATTERN.test(t) || !n || r !== "persisted" && r !== "omitted") return; return { version: 1, promptHash: t, localSessionId: n, userTurnDisposition: r }; } /** * Re-own omitted reseed receipts when a reset intentionally preserves the * native CLI conversation. Persisted turns keep their old owner and fail open * because their canonical user row belongs to the archived local transcript. */ function rebindCliSessionReseedReceiptsForReset(e, t) { const n = normalizeOptionalString(t); if (!e || !n) return e; let r; for (const [t, i] of Object.entries(e)) { const a = normalizeCliSessionReseedReceipt(i.reseedReceipt); if (!a || a.userTurnDisposition !== "omitted") continue; r ??= { ...e }; r[t] = { ...i, reseedReceipt: { ...a, localSessionId: n } }; } return r ?? e; } /** Read the stored provider-keyed CLI session binding. */ function getCliSessionBinding(e, t) { if (!e) return; const n = normalizeProviderId(t); const r = e.cliSessionBindings?.[n]; const i = normalizeOptionalString(r?.sessionId); if (i) return { sessionId: i, resumeCheckpointId: normalizeOptionalString(r?.resumeCheckpointId), ...r?.forceReuse === true ? { forceReuse: true } : {}, ...r?.forkNextResume === true ? { forkNextResume: true } : {}, authProfileId: normalizeOptionalString(r?.authProfileId), authEpoch: normalizeOptionalString(r?.authEpoch), authEpochVersion: r?.authEpochVersion, extraSystemPromptHash: normalizeOptionalString(r?.extraSystemPromptHash), messageToolPolicyHash: normalizeOptionalString(r?.messageToolPolicyHash), promptToolNamesHash: normalizeOptionalString(r?.promptToolNamesHash), cwdHash: normalizeOptionalString(r?.cwdHash), mcpConfigHash: normalizeOptionalString(r?.mcpConfigHash), mcpResumeHash: normalizeOptionalString(r?.mcpResumeHash), reseedReceipt: normalizeCliSessionReseedReceipt(r?.reseedReceipt), transcriptCursor: normalizeCliTranscriptCursor(r?.transcriptCursor) }; const a = e.cliSessionIds?.[n]; const o = normalizeOptionalString(a); if (o) return { sessionId: o }; } function forkCliSessionBindings(e, t) { const n = /* @__PURE__ */ new Set([...Object.keys(e?.cliSessionBindings ?? {}), ...Object.keys(e?.cliSessionIds ?? {})]); const r = {}; for (const i of n) { const n = getCliSessionBinding(e, i); if (n?.resumeCheckpointId && t(i)) { const { reseedReceipt: e, transcriptCursor: t, forceReuse: a, ...o } = n; r[normalizeProviderId(i)] = { ...o, forkNextResume: true }; } } return Object.keys(r).length > 0 ? r : void 0; } function clearAllCliSessions(e) { e.cliSessionBindings = void 0; e.cliSessionIds = void 0; e.claudeCliSessionId = void 0; } //#endregion export { normalizeCliTranscriptCursor as a, normalizeCliSessionReseedReceipt as i, forkCliSessionBindings as n, rebindCliSessionReseedReceiptsForReset as o, getCliSessionBinding as r, clearAllCliSessions as t };