| 1 | import { ToolCallDelta } from "../../.."; |
| 2 | import { createDelta } from "../systemToolUtils"; |
| 3 | import { ToolCallParseState } from "../types"; |
| 4 | |
| 5 | /* |
| 6 | Efficiently applies chunks to a tool call state as they come in |
| 7 | Expects chunks to be broken so that new lines and codeblocks are alone |
| 8 | For now, this parser collects entire arg before |
| 9 | This is because support for JSON booleans is tricky otherwise |
| 10 | */ |
| 11 | export function handleToolCallBuffer( |
| 12 | chunk: string, |
| 13 | state: ToolCallParseState, |
| 14 | ): ToolCallDelta | undefined { |
| 15 | // Add chunks |
| 16 | const lineIndex = state.currentLineIndex; |
| 17 | if (!state.lineChunks[lineIndex]) { |
| 18 | state.lineChunks[lineIndex] = []; |
| 19 | } |
| 20 | state.lineChunks[lineIndex].push(chunk); |
| 21 | |
| 22 | const isNewLine = chunk === "\n"; |
| 23 | if (isNewLine) { |
| 24 | state.currentLineIndex++; |
| 25 | } |
| 26 | |
| 27 | const line = state.lineChunks[lineIndex].join(""); |
| 28 | |
| 29 | switch (lineIndex) { |
| 30 | // The first line will be skipped (e.g. ```tool\n) |
| 31 | case 0: |
| 32 | // non-standard start sequences will sometimes include stuff in the tool_name line |
| 33 | const splitBuffer = line.split("\n"); |
| 34 | if (splitBuffer[0]) { |
| 35 | state.lineChunks[0] = [splitBuffer[0], "\n"]; |
| 36 | } |
| 37 | if (splitBuffer[1]) { |
| 38 | state.lineChunks[1] = [splitBuffer[1]]; |
| 39 | } |
| 40 | |
| 41 | state.currentLineIndex = 1; |
| 42 | // Tool name line - process once line 2 is reached |
| 43 | case 1: |
| 44 | if (isNewLine) { |
| 45 | const name = (line.split(/tool_?name:/i)[1] ?? "").trim(); |
| 46 | if (!name) { |
| 47 | throw new Error("Invalid tool name"); |
| 48 | } |
| 49 | return createDelta(name, "", state.toolCallId); |
| 50 | } |
| 51 | return; |
| 52 | default: |
| 53 | if (state.isWithinArgStart) { |
| 54 | if (isNewLine) { |
| 55 | const argName = (line.split(/begin_?arg:/i)[1] ?? "").trim(); |
| 56 | if (!argName) { |
| 57 | throw new Error("Invalid begin arg line"); |
| 58 | } |
| 59 | state.currentArgName = argName; |
| 60 | state.isWithinArgStart = false; |
| 61 | const argPrefix = state.processedArgNames.size === 0 ? "{" : ","; |
| 62 | return createDelta("", `${argPrefix}"${argName}":`, state.toolCallId); |
| 63 | } |
| 64 | } else if (state.currentArgName) { |
| 65 | if (isNewLine) { |
| 66 | const isEndArgTag = line.match(/end_?arg/i); |
| 67 | if (isEndArgTag) { |
| 68 | let trimmedValue = state.currentArgChunks.join("").trim(); |
| 69 | state.currentArgChunks.length = 0; |
| 70 | state.processedArgNames.add(state.currentArgName); |
| 71 | state.currentArgName = undefined; |
| 72 | |
| 73 | try { |
| 74 | if ( |
| 75 | trimmedValue.startsWith("[") || |
| 76 | trimmedValue.startsWith("{") |
| 77 | ) { |
| 78 | trimmedValue = trimmedValue.replace( |
| 79 | /"((?:\\[\s\S]|[^"\\])*?)"/g, |
| 80 | (match) => { |
| 81 | const content = match.slice(1, -1); |
| 82 | // Replace unescaped newlines |
| 83 | return ( |
| 84 | '"' + |
| 85 | content |
| 86 | .replace(/([^\\])\n/g, "$1\\n") |
| 87 | .replace(/^\n/g, "\\n") + |
| 88 | '"' |
| 89 | ); |
| 90 | }, |
| 91 | ); |
| 92 | } |
| 93 | const parsed = JSON.parse(trimmedValue); |
| 94 | const stringifiedArg = JSON.stringify(parsed); |
| 95 | return createDelta("", stringifiedArg, state.toolCallId); |
| 96 | } catch (e) { |
| 97 | const stringifiedArg = JSON.stringify(trimmedValue); |
| 98 | return createDelta("", stringifiedArg, state.toolCallId); |
| 99 | } |
| 100 | } else { |
| 101 | state.currentArgChunks.push(line); |
| 102 | } |
| 103 | } |
| 104 | } else { |
| 105 | // Check for entry into arg |
| 106 | const isBeginArgLine = line.match(/begin_?arg:/i); |
| 107 | if (isBeginArgLine) { |
| 108 | state.isWithinArgStart = true; |
| 109 | } |
| 110 | |
| 111 | // Check for exit |
| 112 | if (line === "```" || isNewLine) { |
| 113 | state.done = true; |
| 114 | // finish args JSON if applicable |
| 115 | if (state.processedArgNames.size > 0) { |
| 116 | return createDelta("", "}", state.toolCallId); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |