UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

4 lines (3 loc) • 1.4 kB
//!extensions/anthropic/cli-runtime-prompt-context.ts function createUserInput(e,t,n,r=false){return{type:`user`,message:{role:`user`,content:t},parent_tool_use_id:null,uuid:n,...r?{isSynthetic:true}:{},...e.sessionId?{session_id:e.sessionId}:{}}}const HOOK_CONTEXT_MAX_CHARS=1e4;const PROMPT_CONTEXT_HOOK_IDS=Array.from({length:128},(e,t)=>t===0?`UserPromptSubmit`:`UserPromptSubmit:${t}`);function promptContextHookSlot(e){return typeof e===`string`?PROMPT_CONTEXT_HOOK_IDS.indexOf(e):-1}function promptContextHookValue(e,t){const n=[e?.prependContext,e?.appendContext].filter(Boolean).join(` `);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}}export{promptContextHookValue as i,createUserInput as n,promptContextHookSlot as r,PROMPT_CONTEXT_HOOK_IDS as t};