UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

92 lines (91 loc) 4.16 kB
import { i as resolveGlobalSingleton } from "./global-singleton-Dc_stLtU.js"; import { t as pruneMapToMaxSize } from "./map-size-CNcWiFKu.js"; //#region src/agents/embedded-agent-runner/session-prompt-state.ts /** Process-local prompt projection state owned by an embedded session lifecycle. */ const MAX_SESSION_PROMPT_STATES = 64; const MAX_ACTIVE_PROJECT_KEYS = 4; const sessionPromptStates = resolveGlobalSingleton(Symbol.for("openclaw.embeddedSessionPromptStates"), () => /* @__PURE__ */ new Map()); function createToolResultPromptProjectionState() { return { replacements: /* @__PURE__ */ new Map(), frozen: /* @__PURE__ */ new Set(), ambiguousBaseKeys: /* @__PURE__ */ new Set(), sourceTextByKey: /* @__PURE__ */ new Map(), restoredCacheTtl: /* @__PURE__ */ new Map() }; } function createSessionPromptState() { return { activeProjectKeys: [], toolResults: createToolResultPromptProjectionState(), sentUserTurnIds: /* @__PURE__ */ new Set() }; } function cloneToolResultPromptProjectionState(state) { return { replacements: new Map(state.replacements), frozen: new Set(state.frozen), ambiguousBaseKeys: new Set(state.ambiguousBaseKeys), sourceTextByKey: new Map(state.sourceTextByKey), restoredCacheTtl: new Map(state.restoredCacheTtl) }; } /** Marker payload stays key-sized: soft trims are recomputed from canonical history, hard clears keep only their placeholder. */ function serializeCacheTtlToolResultProjections(state) { const marks = new Map(state.restoredCacheTtl); for (const [key, projection] of state.replacements) if (projection.cacheTtl === "soft") marks.set(key, { mode: "soft" }); else if (projection.cacheTtl === "hard" && projection.message.role === "toolResult") { const placeholder = projection.message.content.flatMap((block) => block.type === "text" ? [block.text] : []).join("\n"); marks.set(key, { mode: "hard", placeholder }); } return { prunedToolResults: [...marks].map(([key, mark]) => Object.assign({ key }, mark)), ambiguousToolResultBaseKeys: [...state.ambiguousBaseKeys] }; } function getEmbeddedSessionPromptState(sessionId) { const existing = sessionPromptStates.get(sessionId); if (existing) { sessionPromptStates.delete(sessionId); sessionPromptStates.set(sessionId, existing); return existing; } const created = createSessionPromptState(); sessionPromptStates.set(sessionId, created); pruneMapToMaxSize(sessionPromptStates, MAX_SESSION_PROMPT_STATES); return created; } /** Records the prepared repository identity and snapshots this session's LRU active set. */ function prepareEmbeddedSessionActiveProjectKeys(sessionId, projectKey) { const state = getEmbeddedSessionPromptState(sessionId); if (projectKey) { const existing = state.activeProjectKeys.indexOf(projectKey); if (existing >= 0) state.activeProjectKeys.splice(existing, 1); state.activeProjectKeys.unshift(projectKey); state.activeProjectKeys.length = Math.min(state.activeProjectKeys.length, MAX_ACTIVE_PROJECT_KEYS); } return [...state.activeProjectKeys]; } function clearEmbeddedSessionPromptStates(sessionIds) { for (const sessionId of sessionIds) { const normalized = sessionId?.trim(); if (normalized) sessionPromptStates.delete(normalized); } } function markSessionUserTurnsSent(state, messages) { for (const message of messages) { if (message.role !== "user") continue; const idempotencyKey = message.idempotencyKey; if (typeof idempotencyKey === "string" && idempotencyKey.length > 0) state.sentUserTurnIds.add(idempotencyKey); } } function hasSessionUserTurnBeenSent(state, message) { if (!message || message.role !== "user") return; const idempotencyKey = message.idempotencyKey; return typeof idempotencyKey === "string" && idempotencyKey.length > 0 ? state.sentUserTurnIds.has(idempotencyKey) : void 0; } //#endregion export { hasSessionUserTurnBeenSent as a, serializeCacheTtlToolResultProjections as c, getEmbeddedSessionPromptState as i, cloneToolResultPromptProjectionState as n, markSessionUserTurnsSent as o, createToolResultPromptProjectionState as r, prepareEmbeddedSessionActiveProjectKeys as s, clearEmbeddedSessionPromptStates as t };