UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

288 lines (287 loc) • 8.14 kB
//#region packages/tool-call-repair/src/grammar.ts /** Legacy marker some models emit after a serialized JSON tool request. */ const END_TOOL_REQUEST = "[END_TOOL_REQUEST]"; /** Harmony stream marker that introduces the target channel before a tool call. */ const HARMONY_CHANNEL_MARKER = "<|channel|>"; /** Harmony stream marker that may separate the header from the JSON payload. */ const HARMONY_MESSAGE_MARKER = "<|message|>"; /** Harmony stream marker that may close a serialized tool-call payload. */ const HARMONY_CALL_MARKER = "<|call|>"; /** Tool names in bracket/plain-text repairs intentionally match provider-safe ids only. */ function isPlainTextToolNameChar(e) { return Boolean(e && /[A-Za-z0-9_-]/.test(e)); } /** XML-ish function tags allow namespace punctuation used by some model families. */ function isXmlishNameChar(e) { return Boolean(e && /[A-Za-z0-9_.:-]/.test(e)); } /** Skips spaces and tabs only, preserving line boundaries for grammar decisions. */ function skipHorizontalWhitespace(e, t) { let n = t; while (n < e.length && (e[n] === " " || e[n] === " ")) n += 1; return n; } /** Skips indentation whitespace without crossing the current line boundary. */ function skipLineIndentation(e, t) { let n = t; while (n < e.length && /[^\S\r\n]/u.test(e[n] ?? "")) n += 1; return n; } /** Skips all JavaScript whitespace when line structure is no longer meaningful. */ function skipWhitespace(e, t) { let n = t; while (n < e.length && /\s/.test(e[n] ?? "")) n += 1; return n; } /** Consumes either Unix or Windows line endings and returns the first offset after them. */ function consumeLineBreak(e, t) { if (e[t] === "\r") return e[t + 1] === "\n" ? t + 2 : t + 1; if (e[t] === "\n") return t + 1; return null; } function consumeStructuralLineBreakAfterHorizontalWhitespace(e, t, n) { const r = skipHorizontalWhitespace(e, t); const i = consumeLineBreak(e, r); if (i !== null) return i; for (let e = t; e <= r; e += 1) if (n?.lineBreakOffsets.has(e)) return e; return null; } const utf8Encoder = new TextEncoder(); /** Returns the encoded byte length when a source span stays within its serialized limit. */ function utf8ByteLengthWithinLimit(e, t, n, r) { if (n - t > r) return null; const i = utf8Encoder.encode(e.slice(t, n)).byteLength; return i <= r ? i : null; } const FUNCTION_OPEN = "<function="; const FUNCTION_CLOSE = "</function>"; const PARAMETER_OPEN = "<parameter="; const PARAMETER_CLOSE = "</parameter>"; function startsWithAsciiMarkerIgnoreCase(e, t, n) { return e.slice(t, t + n.length).toLowerCase() === n; } function isAsciiMarkerPrefixIgnoreCase(e, t, n) { const r = e.slice(t, t + n.length).toLowerCase(); return r.length < n.length && n.startsWith(r); } function indexOfAsciiMarkerIgnoreCase(e, t, n) { for (let r = e.indexOf("<", n); r !== -1; r = e.indexOf("<", r + 1)) if (startsWithAsciiMarkerIgnoreCase(e, r, t)) return r; return -1; } /** Scan at most one character beyond the accepted tool-name limit. */ function scanToolCallNameEnd(e, t, n = isXmlishNameChar) { let r = t; while (n(e[r]) && r - t < 121) r += 1; return r; } /** Uncapped structural scan shared by parsing, stripping, and stream buffering. */ function scanXmlishToolCall(e, t = 0, n) { let r = t; let i; let a; if (e[r] === "<") { if (!startsWithAsciiMarkerIgnoreCase(e, r, FUNCTION_OPEN) && !isAsciiMarkerPrefixIgnoreCase(e, r, FUNCTION_OPEN)) return { kind: "invalid", at: t }; if (e.length - r < 10) return { kind: "prefix" }; r += 10; const n = r; r = scanToolCallNameEnd(e, n); a = { start: n, end: r }; i = "function"; if (r - n > 120) return { kind: "invalid", at: r }; if (r === e.length) return { kind: "prefix", candidate: { syntax: i, name: a, nameComplete: false, parameters: [] } }; if (r === n || e[r] !== ">") return { kind: "invalid", at: r }; r += 1; } else if (e[r] === "[") { r += 1; const t = r; r = scanToolCallNameEnd(e, t, isPlainTextToolNameChar); if (r - t > 120) return { kind: "invalid", at: r }; const o = e.slice(t, r); if (r === e.length && "tool".startsWith(o)) return { kind: "prefix" }; i = "named-bracket"; a = { start: t, end: r }; if (e[r] === ":" && o === "tool") { i = "tool-bracket"; r += 1; const t = r; r = scanToolCallNameEnd(e, t, isPlainTextToolNameChar); a = { start: t, end: r }; if (r - t > 120) return { kind: "invalid", at: r }; } if (r === e.length) return { kind: "prefix", candidate: { syntax: i, name: a, nameComplete: false, parameters: [] } }; if (a.start === a.end || e[r] !== "]") return { kind: "invalid", at: r }; r += 1; if (i === "named-bracket") { if (r === e.length) return { kind: "prefix", candidate: { syntax: i, name: a, nameComplete: true, parameters: [] } }; const t = consumeStructuralLineBreakAfterHorizontalWhitespace(e, r, n); if (t === null) return { kind: "invalid", at: r }; r = t; } } else return { kind: "invalid", at: t }; const o = r; const s = []; const candidate = (e, t) => ({ syntax: i, name: a, nameComplete: true, parameters: s, payload: { start: o, end: e }, ...t === void 0 ? {} : { activeParameterOpenEnd: t } }); let c; const prefix = (e, t) => ({ kind: "prefix", candidate: candidate(e, t), completeEnd: i === "tool-bracket" ? c : void 0 }); const complete = (e, t = e) => ({ kind: "complete", ...candidate(e), end: t }); while (true) { const t = skipWhitespace(e, r); if (t === e.length) return i === "tool-bracket" && c !== void 0 ? complete(c) : { kind: "prefix", candidate: candidate(e.length) }; if (startsWithAsciiMarkerIgnoreCase(e, t, FUNCTION_CLOSE)) return i !== "function" && s.length === 0 ? { kind: "invalid", at: t, candidate: candidate(t) } : complete(t, t + 11); if (isAsciiMarkerPrefixIgnoreCase(e, t, FUNCTION_CLOSE)) return prefix(t); if (startsWithAsciiMarkerIgnoreCase(e, t, PARAMETER_OPEN)) { const n = t + 11; const i = scanToolCallNameEnd(e, n); if (i - n > 120) return { kind: "invalid", at: t, candidate: candidate(t) }; if (i === e.length) return prefix(t); if (i === n || e[i] !== ">") return { kind: "invalid", at: t, candidate: candidate(t) }; const a = i + 1; const o = indexOfAsciiMarkerIgnoreCase(e, PARAMETER_CLOSE, a); if (o === -1) return prefix(e.length, a); const l = o + 12; s.push({ name: { start: n, end: i }, value: { start: a, end: o } }); r = l; c = l; continue; } if (isAsciiMarkerPrefixIgnoreCase(e, t, PARAMETER_OPEN)) return prefix(t); if (i === "tool-bracket" && c !== void 0) return complete(c); return { kind: "invalid", at: t, candidate: candidate(t) }; } } /** Continue the same object scan across parser spans or bounded stream chunks. */ function scanJsonObject(e, t, n = { depth: 0, escaped: false, inString: false }) { for (let r = t; r < e.length; r += 1) { const t = e[r]; if (n.inString) { if (n.escaped) n.escaped = false; else if (t === "\\") n.escaped = true; else if (t === "\"") n.inString = false; continue; } if (t === "\"") n.inString = true; else if (t === "{") n.depth += 1; else if (t === "}") { n.depth -= 1; if (n.depth === 0) return { kind: "complete", end: r + 1, state: n }; } } return { kind: "prefix", end: e.length, state: n }; } //#endregion export { startsWithAsciiMarkerIgnoreCase as _, consumeLineBreak as a, isAsciiMarkerPrefixIgnoreCase as c, scanJsonObject as d, scanToolCallNameEnd as f, skipWhitespace as g, skipLineIndentation as h, HARMONY_MESSAGE_MARKER as i, isPlainTextToolNameChar as l, skipHorizontalWhitespace as m, HARMONY_CALL_MARKER as n, consumeStructuralLineBreakAfterHorizontalWhitespace as o, scanXmlishToolCall as p, HARMONY_CHANNEL_MARKER as r, indexOfAsciiMarkerIgnoreCase as s, END_TOOL_REQUEST as t, isXmlishNameChar as u, utf8ByteLengthWithinLimit as v };