File size: 3,613 Bytes
a8f5a2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b1f716
 
 
 
 
 
 
a8f5a2f
 
 
6b1f716
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8f5a2f
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// 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, "'\\''") + "'";
}