openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
110 lines (109 loc) • 5.1 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { j as parseAgentSessionKey } from "./session-key-CqAyZeVk.mjs";
import { r as createLazyRuntimeModule } from "./lazy-runtime-zGjszCtt.mjs";
import { i as getActivePreparedMemoryPromptSection, m as prepareMemoryPromptSection, n as buildMemoryPromptSection } from "./memory-state-BAtmlQOR.mjs";
import { t as compactionWatchdogs } from "./compaction-watchdog-DA9pkvn9.mjs";
import { normalizeStructuredPromptSection } from "@openclaw/ai/internal/shared";
//#region src/context-engine/delegate.ts
const loadCompactRuntime = createLazyRuntimeModule(() => import("./compact.runtime.js"));
function assertCompactionSessionIdentity(e) {
const t = normalizeOptionalString(e.sessionTarget?.agentId);
const n = normalizeOptionalString(e.sessionTarget?.sessionId);
const r = normalizeOptionalString(e.sessionTarget?.sessionKey);
const i = normalizeOptionalString(e.agentId);
const a = normalizeOptionalString(e.sessionId);
const o = normalizeOptionalString(e.sessionKey);
const s = parseAgentSessionKey(o)?.agentId;
if (i && t && i !== t || a && n && a !== n || o && r && o !== r || s && t && s !== t) throw new Error("Context-engine successor target conflicts with the caller session identity");
const c = t ?? i;
const l = parseAgentSessionKey(r ?? o)?.agentId;
if (l && c && l !== c) throw new Error("Context-engine successor session key conflicts with its agent identity");
}
/**
* Delegate a context-engine compaction request to OpenClaw's built-in runtime compaction path.
*
* This is the same bridge used by the legacy context engine. Third-party
* engines can call it from their own `compact()` implementations when they do
* not own the compaction algorithm but still need `/compact` and overflow
* recovery to use the stock runtime behavior.
*
* Note: `compactionTarget` is part of the public `compact()` contract, but the
* built-in runtime compaction path does not expose that knob. This helper
* ignores it to preserve legacy behavior; engines that need target-specific
* compaction should implement their own `compact()` algorithm.
*/
async function delegateCompactionToRuntime(e) {
const t = e.runtimeContext ?? {};
const { sessionFile: n, ...r } = t;
const i = e.sessionTarget ?? t.sessionTarget;
const a = e.agentId ?? t.agentId;
const o = e.sessionKey ?? t.sessionKey;
assertCompactionSessionIdentity({
agentId: a,
sessionId: e.sessionId,
sessionKey: o,
sessionTarget: i
});
const { compactEmbeddedAgentSessionOnDemand: s } = await loadCompactRuntime();
const c = e.abortSignal && compactionWatchdogs.get(e.abortSignal);
const l = e.currentTokenCount ?? (typeof t.currentTokenCount === "number" && Number.isFinite(t.currentTokenCount) && t.currentTokenCount > 0 ? Math.floor(t.currentTokenCount) : void 0);
const u = await s({
...r,
contextEngineRuntimeContext: t,
agentId: a,
sessionId: e.sessionId,
sessionKey: o,
sessionTarget: i,
tokenBudget: e.tokenBudget,
...l !== void 0 ? { currentTokenCount: l } : {},
force: e.force,
customInstructions: e.customInstructions,
abortSignal: e.abortSignal,
compactionTimeoutReset: c?.reset,
compactionDeadlineAt: c?.deadlineAt,
workspaceDir: typeof t.workspaceDir === "string" ? t.workspaceDir : process.cwd()
});
return {
ok: u.ok,
compacted: u.compacted,
reason: u.reason,
result: u.result ? {
summary: u.result.summary,
firstKeptEntryId: u.result.firstKeptEntryId,
tokensBefore: u.result.tokensBefore,
tokensAfter: u.result.tokensAfter,
details: u.result.details,
...u.result.sessionId ? { sessionId: u.result.sessionId } : {},
...u.result.sessionTarget ? { sessionTarget: u.result.sessionTarget } : {}
} : void 0
};
}
/**
* Build a context-engine-ready systemPromptAddition from the active memory
* plugin prompt path. This lets non-legacy engines explicitly opt into the
* same memory/wiki guidance that the legacy engine gets via system prompt
* assembly, without reimplementing memory prompt formatting.
*/
function renderMemorySystemPromptAddition(e, t) {
const n = buildMemoryPromptSection(e, t);
if (n.length === 0) return;
return normalizeStructuredPromptSection(n.join("\n")) || void 0;
}
function buildMemorySystemPromptAddition(e) {
const t = getActivePreparedMemoryPromptSection();
if (!t) return renderMemorySystemPromptAddition(e);
return renderMemorySystemPromptAddition({
availableTools: e.availableTools,
citationsMode: e.citationsMode ?? t.context.citationsMode,
agentId: e.agentId ?? t.context.agentId,
agentSessionKey: e.agentSessionKey ?? t.context.agentSessionKey,
sandboxed: e.sandboxed ?? t.context.sandboxed,
projectKey: e.projectKey ?? t.context.projectKey
}, t);
}
/** Prepare memory state asynchronously, then render it without prompt-path I/O. */
async function prepareMemorySystemPromptAddition(e) {
return renderMemorySystemPromptAddition(e, await prepareMemoryPromptSection(e));
}
//#endregion
export { delegateCompactionToRuntime as n, prepareMemorySystemPromptAddition as r, buildMemorySystemPromptAddition as t };