openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 4.41 kB
JavaScript
import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{r as isInsideCode}from"./code-ranges-DvhlRpPR.mjs";import{n as findMarkdownCodeRegions}from"./reasoning-tags-B1fciaOu.mjs";import"./string-coerce-runtime-BYDH9Quw.mjs";import"./text-chunking-BAqqd_7-.mjs";
//!extensions/anthropic/cli-output.ts
const CLAUDE_RAW_TOOL_OUTPUT_ERROR=`Claude CLI returned malformed tool output (invalid request format): raw tool protocol appeared as assistant text. OpenClaw refused to persist or deliver it.`;function classifyClaudeCliErrorCode(e){return/Failed to refresh OAuth token: another Claude Code process is refreshing it or exited mid-refresh\b/i.test(e)?`refresh_contention`:void 0}const RAW_INVOKE_TAG_RE=/<invoke(?=[ \t>])[^<>\r\n]*>/gu;const RAW_PARAMETER_TAG_RE=/<parameter(?=[ \t>])[^<>\r\n]*>/gu;const RAW_INVOKE_CLOSE_TAG_RE=/<\/invoke[ \t]*>/gu;const RAW_PARAMETER_LOOKAHEAD_CHARS=2048;const OBSERVED_TRUNCATED_LEAK_PREFIXES=/* @__PURE__ */ new Set([`call`,`count`,`court`]);function readTagNameAttribute(e,t){const n=/[ \t]+([^\s=/>]+)[ \t]*=[ \t]*(?:"([^"]*)"|'([^']*)')/gy;const r=e.length-1;let i=t.length+1;while(i<r){n.lastIndex=i;const t=n.exec(e);if(!t)return``;if(t[1]===`name`)return t[2]??t[3]??``;i=n.lastIndex}return``}function isStandaloneLineTag(e,t){const n=e.lastIndexOf(`
`,t-1)+1;return/^[ ]{0,3}$/u.test(e.slice(n,t))}function isStandaloneOpeningTagLine(e,t,n){if(!isStandaloneLineTag(e,t))return false;const r=e.indexOf(`
`,t+n);const i=e.slice(t+n,r===-1?e.length:r);return/^[ \t]*\r?$/u.test(i)}function hasObservedTruncatedLeakPrefix(e,t,n){const r=e.slice(0,t).split(/\r?\n/gu).findLast(e=>e.trim().length>0)?.trim();return r===n||r!==void 0&&OBSERVED_TRUNCATED_LEAK_PREFIXES.has(r)}function findNextStandaloneTag(e,t,n,r){const i=new RegExp(r.source,r.flags);i.lastIndex=t;for(let t=i.exec(e);t;t=i.exec(e))if(isStandaloneOpeningTagLine(e,t.index,t[0].length)&&!isInsideCode(t.index,n))return t.index;return null}function hasClaudeRawToolInvocation(e){if(!e.includes(`<invoke`)||!e.includes(`<parameter`))return false;const t=findMarkdownCodeRegions(e);RAW_INVOKE_TAG_RE.lastIndex=0;for(const n of e.matchAll(RAW_INVOKE_TAG_RE)){const r=n.index;const i=readTagNameAttribute(n[0],`invoke`);if(!/^[A-Z]/u.test(i)&&!i.startsWith(`mcp__`)||!isStandaloneOpeningTagLine(e,r,n[0].length)||isInsideCode(r,t))continue;const a=r+n[0].length;const o=findNextStandaloneTag(e,a,t,RAW_INVOKE_CLOSE_TAG_RE);const s=findNextStandaloneTag(e,a,t,RAW_INVOKE_TAG_RE);const c=o!==null&&(s===null||o<s)?o:null;const l=Math.min(e.length,a+RAW_PARAMETER_LOOKAHEAD_CHARS,c??e.length,s??e.length);const u=e.slice(a,l);let d=null;for(const n of u.matchAll(RAW_PARAMETER_TAG_RE)){const r=a+n.index;if(readTagNameAttribute(n[0],`parameter`)&&isStandaloneLineTag(e,r)&&!isInsideCode(r,t)){d=r;break}}if(d===null)continue;if(c!==null||hasObservedTruncatedLeakPrefix(e,r,i))return true}return false}function parseClaudeJsonlRecord(e){try{const t=JSON.parse(e);return isRecord(t)?t:null}catch{return null}}function readClaudeTokenCount(e){return typeof e===`number`&&Number.isSafeInteger(e)&&e>=0?e:void 0}const parseClaudeCliJsonlLifecycleEvent=e=>{if(!/compacting|compact_result|compact_boundary/u.test(e))return null;const t=parseClaudeJsonlRecord(e);if(t?.compact_result===`failed`)return{kind:`compaction`,phase:`end`,completed:false};if(t?.type===`system`&&t.subtype===`compact_boundary`){const e=isRecord(t.compact_metadata)?t.compact_metadata:{};const n=readClaudeTokenCount(e.pre_tokens);const r=readClaudeTokenCount(e.post_tokens);return{kind:`compaction`,phase:`end`,completed:true,...e.trigger===`auto`||e.trigger===`manual`?{trigger:e.trigger}:{},...n!==void 0?{tokensBefore:n}:{},...r!==void 0?{tokensAfter:r}:{}}}return t?.type===`system`&&t.subtype===`status`&&t.status===`compacting`?{kind:`compaction`,phase:`start`}:null};const parseClaudeCliJsonlEvent=e=>{if(!((e.includes(`<invoke`)||e.includes(`\\u003cinvoke`)||e.includes(`\\u003Cinvoke`))&&(e.includes(`<parameter`)||e.includes(`\\u003cparameter`)||e.includes(`\\u003Cparameter`))))return null;const t=parseClaudeJsonlRecord(e);if(!t||t.type!==`result`||typeof t.result!==`string`||!hasClaudeRawToolInvocation(t.result))return null;return{kind:`result`,errorText:CLAUDE_RAW_TOOL_OUTPUT_ERROR}};export{parseClaudeCliJsonlLifecycleEvent as i,hasClaudeRawToolInvocation as n,parseClaudeCliJsonlEvent as r,classifyClaudeCliErrorCode as t};