Spaces:
Running
Running
Download js/toolcalls.js from ereniko/LocalWebAgent: direct link, hf CLI and curl.
- Browser
- Download file 3.61 kB
-
https://huggingface.co/spaces/ereniko/LocalWebAgent/resolve/main/js/toolcalls.js
- Command line
-
hf download hf://spaces/ereniko/LocalWebAgent/js/toolcalls.js
-
curl -L -o toolcalls.js https://huggingface.co/spaces/ereniko/LocalWebAgent/resolve/main/js/toolcalls.js
3.61 kB
| // Parses LFM3 assistant output: | |
| // [thinking]</think>[visible text]<tool_call>\n<function=NAME>\n<parameter=K>\nV\n</parameter>\n</function>\n</tool_call> | |
| // `text` is what the model generated after the prompt, which (with thinking on) | |
| // already ended in "<think>", so the opening tag is usually absent. | |
| const CALL_RE = /<tool_call>\s*<function=([^>\n]+)>([\s\S]*?)(?:<\/function>\s*(?:<\/tool_call>|$)|$)/g; | |
| const PARAM_RE = /<parameter=([^>\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("</think>"); | |
| if (end >= 0) { | |
| thinking = text.slice(0, end).replace(/^\s*<think>/, ""); | |
| rest = text.slice(end + "</think>".length); | |
| } else if (thinkingOpen) { | |
| thinking = text.replace(/^\s*<think>/, ""); | |
| rest = ""; | |
| thinkingDone = false; | |
| } | |
| const calls = []; | |
| const firstCall = rest.indexOf("<tool_call>"); | |
| 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, <tool_call>{"name": ..., "arguments": {...}}</tool_call> | |
| if (!calls.length) { | |
| for (const m of tail.matchAll(/<tool_call>\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, "'\\''") + "'"; | |
| } | |