@burtson-labs/bandit-engine 2.0.103 → 2.0.104

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -21532,7 +21532,7 @@ TOOL USAGE PROTOCOL (conservative approach)
21532
21532
  * web_search() - when asked about recent/current events, breaking news, live information (weather, prices, sports scores), or when you need to look up documentation, libraries, APIs, error messages, or verify a specific fact
21533
21533
  * web_fetch() - to read the FULL contents of a specific URL you already have. Reach for this when the user wants to "tell me more", "go deeper", "read/open that article", or asks for details about a specific source, link, or article from an EARLIER answer: take that item's URL from the previous Sources list in this conversation and fetch it, then answer from the page's actual content (not just the prior summary)
21534
21534
  * image_generation() - ONLY when explicitly asked to create or generate an image
21535
- * create_file({"content": "...", "filename": "report.docx", "format": "docx"}) - when the user asks for a downloadable FILE (a document, spreadsheet, slides, markdown, code, etc.) or to "export"/"download"/"save" something. Formats: md, txt, csv, json, html, xml, yaml, docx, pptx. For docx/pptx write well-structured Markdown (use "## " headings to start each slide for pptx). It returns a temporary download link \u2014 ALWAYS tell the user the file expires (~1 hour). If it is unclear whether they want it shown inline vs. as a downloadable file, use ask_user first.
21535
+ * create_file({"content": "...", "filename": "report.docx", "format": "docx"}) - when the user asks for a downloadable FILE (a document, spreadsheet, slides, markdown, code, etc.) or to "export"/"download"/"save" something. Formats: md, txt, csv, json, html, xml, yaml, docx, pptx. PDF is NOT a supported format \u2014 if the user asks for a PDF, create a docx instead. For docx/pptx write well-structured Markdown (use "## " headings to start each slide for pptx). It returns a temporary download link \u2014 ALWAYS tell the user the file expires (~1 hour). If it is unclear whether they want it shown inline vs. as a downloadable file, use ask_user first.
21536
21536
  * ask_user({"questions": [{"question": "...", "header": "Format", "options": [{"label": "Inline (Recommended)"}, {"label": "Download a file"}]}]}) - when you are genuinely BLOCKED on a decision that is the USER's to make and cannot resolve from the request, context, or sensible defaults (e.g. show content inline vs. let them download it, which format/option they want). Renders clickable options the user answers in one step \u2014 better than asking in prose and ending your turn. Give 1-4 questions, each with 2-4 options; if one is clearly best, list it first and append " (Recommended)". The user may also type their own answer; act on it directly.
21537
21537
  - For general questions about concepts, definitions, explanations, or how-to topics, use your built-in knowledge WITHOUT calling tools.
21538
21538
  - Examples of what NOT to use tools for: "who are you?", "what is React?", "explain machine learning", "how does X work?", general programming questions.
@@ -21718,16 +21718,50 @@ ${fn}(${argStr})
21718
21718
  \`\`\``;
21719
21719
  }
21720
21720
  }
21721
- if (!/```(?:tool_code|TOOL_CODE)/.test(fullMessage)) {
21722
- const plainToolFence = /```[a-zA-Z0-9_-]*[ \t]*\n([ \t]*(?:web_search|web_fetch|image_generation|create_file|ask_user)[\s\S]*?)\n```/;
21723
- const fm = fullMessage.match(plainToolFence);
21724
- if (fm) {
21725
- fullMessage = fullMessage.replace(plainToolFence, `\`\`\`tool_code
21726
- ${fm[1].trim()}
21727
- \`\`\``);
21721
+ const extractToolCalls = (message) => {
21722
+ const KNOWN = ["web_search", "web_fetch", "image_generation", "create_file", "ask_user", "ask-user"];
21723
+ const out = [];
21724
+ const nameRe = new RegExp(`\\b(${KNOWN.join("|")})\\s*\\(`, "g");
21725
+ let m;
21726
+ while ((m = nameRe.exec(message)) !== null) {
21727
+ const functionName = m[1];
21728
+ const openIdx = m.index + m[0].length - 1;
21729
+ let depth = 0;
21730
+ let i = openIdx;
21731
+ let inStr = false;
21732
+ let esc = false;
21733
+ for (; i < message.length; i++) {
21734
+ const ch = message[i];
21735
+ if (inStr) {
21736
+ if (esc) esc = false;
21737
+ else if (ch === "\\") esc = true;
21738
+ else if (ch === '"') inStr = false;
21739
+ } else if (ch === '"') inStr = true;
21740
+ else if (ch === "(") depth++;
21741
+ else if (ch === ")") {
21742
+ depth--;
21743
+ if (depth === 0) {
21744
+ i++;
21745
+ break;
21746
+ }
21747
+ }
21748
+ }
21749
+ if (depth !== 0) continue;
21750
+ const params = message.slice(openIdx + 1, i - 1).trim();
21751
+ let start = m.index;
21752
+ let end = i;
21753
+ const fenceOpen = message.slice(0, m.index).match(/```[a-zA-Z0-9_-]*[ \t]*\n[ \t]*$/);
21754
+ if (fenceOpen) {
21755
+ start = m.index - fenceOpen[0].length;
21756
+ const fenceClose = message.slice(i).match(/^[ \t]*\n```/);
21757
+ if (fenceClose) end = i + fenceClose[0].length;
21758
+ }
21759
+ out.push({ raw: message.slice(start, end), functionName, params });
21760
+ nameRe.lastIndex = i;
21728
21761
  }
21729
- }
21730
- const toolCallMatches = fullMessage.match(/```(?:tool_code|TOOL_CODE)\s*\n([^`]+)\n```/gi);
21762
+ return out;
21763
+ };
21764
+ const toolCallMatches = extractToolCalls(fullMessage);
21731
21765
  let enhancedMessage = fullMessage;
21732
21766
  const summarizableResults = [];
21733
21767
  const inlineImageBlocks = [];
@@ -21735,13 +21769,10 @@ ${fm[1].trim()}
21735
21769
  if (toolCallMatches && toolCallMatches.length > 0 && mcpToolsAvailable) {
21736
21770
  debugLogger.info("Detected tool calls in AI response", {
21737
21771
  toolCallCount: toolCallMatches.length,
21738
- toolCalls: toolCallMatches
21772
+ toolCalls: toolCallMatches.map((t) => t.functionName)
21739
21773
  });
21740
- for (const match of toolCallMatches) {
21741
- const toolCallCode = match.replace(/```(?:tool_code|TOOL_CODE)\s*\n|\n```/gi, "").trim();
21742
- const functionCallMatch = toolCallCode.match(/^(\w+)\(\s*(.*?)\s*\)$/);
21743
- if (!functionCallMatch) continue;
21744
- const [, functionName, params] = functionCallMatch;
21774
+ for (const call of toolCallMatches) {
21775
+ const { raw: match, functionName, params } = call;
21745
21776
  try {
21746
21777
  let parsedParams = {};
21747
21778
  const raw = params.trim();