openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
114 lines (113 loc) • 4.76 kB
JavaScript
import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
import { findCodeRegions, isInsideCode } from "openclaw/plugin-sdk/text-chunking";
//#region 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("\n", 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("\n", 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;
}
/** Detect Claude's legacy tool protocol only when it occupies standalone assistant lines. */
function hasClaudeRawToolInvocation(e) {
if (!e.includes("<invoke") || !e.includes("<parameter")) return false;
const t = findCodeRegions(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;
}
}
/** Project Claude-owned lifecycle records without widening the legacy parser event union. */
const parseClaudeCliJsonlLifecycleEvent = (e) => {
if (!e.includes("compacting") && !e.includes("compact_result")) return null;
const t = parseClaudeJsonlRecord(e);
if (!t) return null;
if (t.compact_result === "success" || t.compact_result === "failed") return {
kind: "compaction",
phase: "end",
completed: t.compact_result === "success"
};
if (t.type === "system" && t.subtype === "status") return t.status === "compacting" ? {
kind: "compaction",
phase: "start"
} : null;
return null;
};
/** Reject malformed terminal Claude results before the generic CLI runner accepts them as prose. */
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
};
};
//#endregion
export { parseClaudeCliJsonlLifecycleEvent as i, hasClaudeRawToolInvocation as n, parseClaudeCliJsonlEvent as r, classifyClaudeCliErrorCode as t };