// Parses LFM3 assistant output:
// [thinking][visible text]\n\n\nV\n\n\n
// `text` is what the model generated after the prompt, which (with thinking on)
// already ended in "", so the opening tag is usually absent.
const CALL_RE = /\s*\n]+)>([\s\S]*?)(?:<\/function>\s*(?:<\/tool_call>|$)|$)/g;
const PARAM_RE = /\n]+)>\n?([\s\S]*?)(?:\n?<\/parameter>|$)/g;
export function parseAssistant(text, { thinkingOpen = true } = {}) {
let thinking = "";
let rest = text;
let thinkingDone = true;
const end = text.indexOf("");
if (end >= 0) {
thinking = text.slice(0, end).replace(/^\s*/, "");
rest = text.slice(end + "".length);
} else if (thinkingOpen) {
thinking = text.replace(/^\s*/, "");
rest = "";
thinkingDone = false;
}
const calls = [];
const firstCall = rest.indexOf("");
const content = (firstCall >= 0 ? rest.slice(0, firstCall) : rest).trim();
if (firstCall >= 0) {
const tail = rest.slice(firstCall);
for (const m of tail.matchAll(CALL_RE)) {
const args = {};
for (const p of m[2].matchAll(PARAM_RE)) args[p[1].trim()] = p[2];
calls.push({ name: m[1].trim(), arguments: args, complete: /<\/function>/.test(m[0]) });
}
// Fallback: JSON-style calls, {"name": ..., "arguments": {...}}
if (!calls.length) {
for (const m of tail.matchAll(/\s*(\{[\s\S]*?\})\s*<\/tool_call>/g)) {
try {
const j = JSON.parse(m[1]);
const args = typeof j.arguments === "string" ? JSON.parse(j.arguments) : (j.arguments ?? j.parameters ?? {});
if (j.name) calls.push({ name: String(j.name), arguments: Object.fromEntries(Object.entries(args).map(([k, v]) => [k, typeof v === "string" ? v : JSON.stringify(v)])), complete: true });
} catch {}
}
}
}
// Llama 3.x: the reply is a bare JSON call, {"name": ..., "parameters": {...}}, optionally
// after <|python_tag|>, several separated by ";". No tags around it.
if (firstCall < 0) {
const m = /^\s*(?:<\|python_tag\|>)?\s*(\{\s*"(?:name|type)"\s*:[\s\S]*)$/.exec(rest);
const bare = m && parseBareCalls(m[1]);
if (bare) return { thinking: thinking.trim(), thinkingDone, content: "", calls: bare };
}
return { thinking: thinking.trim(), thinkingDone, content, calls };
}
const stringArgs = (args) => Object.fromEntries(Object.entries(args ?? {}).map(([k, v]) => [k, typeof v === "string" ? v : JSON.stringify(v)]));
function parseBareCalls(text) {
const calls = [];
for (const part of text.split(/;\s*(?=\{)/)) {
try {
const j = JSON.parse(part.trim());
for (const c of Array.isArray(j) ? j : [j]) {
const name = c.name ?? c.function?.name;
if (!name) return null;
let args = c.parameters ?? c.arguments ?? c.function?.arguments ?? {};
if (typeof args === "string") { try { args = JSON.parse(args); } catch { args = {}; } }
calls.push({ name: String(name), arguments: stringArgs(args), complete: true });
}
} catch {
// Still streaming (or malformed): show it as a call being prepared.
const name = /"name"\s*:\s*"([^"]+)"/.exec(part)?.[1];
if (!name) return calls.length ? calls : null;
calls.push({ name, arguments: {}, complete: false });
}
}
return calls.length ? calls : null;
}
/** Quotes a string for bash. */
export function shQuote(s) {
return "'" + String(s).replace(/'/g, "'\\''") + "'";
}