const TOOL_CALL_PATTERN = /\s*([\s\S]*?)\s*<\/tool_call>/g; /** @param {unknown} value */ function normalizeCall(value) { if (!value || typeof value !== "object") { throw new Error("The AI returned an invalid tool call"); } const call = /** @type {Record} */ (value); const name = call.name ?? (call.function && typeof call.function === "object" ? /** @type {Record} */ (call.function).name : undefined); let args = call.arguments ?? call.args ?? (call.function && typeof call.function === "object" ? /** @type {Record} */ (call.function).arguments : undefined); if (typeof args === "string") args = JSON.parse(args); if (typeof name !== "string" || !args || typeof args !== "object") { throw new Error("The AI returned an invalid tool call"); } return { name, arguments: /** @type {Record} */ (args) }; } /** @param {string} output */ export function parseToolCalls(output) { const matches = [...output.matchAll(TOOL_CALL_PATTERN)]; if (matches.length) { return matches.flatMap((match) => { const value = JSON.parse(match[1]); return (Array.isArray(value) ? value : [value]).map(normalizeCall); }); } if (output.includes("