UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

31 lines (30 loc) • 1.62 kB
//#region extensions/anthropic/cli-runtime-prompt-context.ts const HOOK_CONTEXT_MAX_CHARS = 1e4; const PROMPT_CONTEXT_HOOK_IDS = Array.from({ length: 128 }, (e, t) => t === 0 ? "UserPromptSubmit" : `UserPromptSubmit:${t}`); /** The prompt-context segment a UserPromptSubmit callback carries, or -1 for other callbacks. */ function promptContextHookSlot(e) { return typeof e === "string" ? PROMPT_CONTEXT_HOOK_IDS.indexOf(e) : -1; } /** One callback's part of the turn's private context; empty when the context is shorter. */ function promptContextHookValue(e, t) { const n = [e?.prependContext, e?.appendContext].filter(Boolean).join("\n\n"); if (n.length <= HOOK_CONTEXT_MAX_CHARS) return t === 0 ? n : ""; const r = JSON.stringify(n).slice(1, -1).replaceAll("<", "\\u003c"); for (let e = 0, n = 0;; e++) { if (n === r.length) return ""; const i = `OpenClaw private context segment ${e + 1} (JSON string; concatenate decoded segments in order without separators, preserving original instruction/data labels):\n`; let a = e === PROMPT_CONTEXT_HOOK_IDS.length - 1 ? r.length : Math.min(n + HOOK_CONTEXT_MAX_CHARS - i.length - 2, r.length); const o = r.lastIndexOf("\\", a - 1); if (o >= n && o >= a - 5) { let e = o; while (e > n && r[e - 1] === "\\") e--; if ((o - e) % 2 === 0 && (o === a - 1 || r[o + 1] === "u" && o + 6 > a)) a = o; } const s = r.charCodeAt(a - 1); if (a < r.length && s >= 55296 && s <= 56319) a--; if (e === t) return `${i}"${r.slice(n, a)}"`; n = a; } } //#endregion export { promptContextHookSlot as n, promptContextHookValue as r, PROMPT_CONTEXT_HOOK_IDS as t };