| 1 | import { ChatMessage, PromptLog, TextMessagePart } from "../.."; |
| 2 | import { normalizeToMessageParts } from "../../util/messageContent"; |
| 3 | import { detectToolCallStart } from "./detectToolCallStart"; |
| 4 | import { createDelta, splitAtCodeblocksAndNewLines } from "./systemToolUtils"; |
| 5 | import { |
| 6 | getInitialToolCallParseState, |
| 7 | SystemMessageToolsFramework, |
| 8 | ToolCallParseState, |
| 9 | } from "./types"; |
| 10 | |
| 11 | /* |
| 12 | Function to intercept tool calls in markdown code blocks format from a chat message stream |
| 13 | 1. Skips non-assistant messages |
| 14 | 2. Intercepts text that looks like a tool call in a markdown code block format: |
| 15 | ```tool |
| 16 | TOOL_NAME: example_tool |
| 17 | BEGIN_ARG: arg1 |
| 18 | value |
| 19 | END_ARG |
| 20 | ``` |
| 21 | 3. Parses tool calls line by line and generates proper tool call deltas |
| 22 | 4. Once the tool call is complete, resets state for potential future tool calls |
| 23 | */ |
| 24 | export async function* interceptSystemToolCalls( |
| 25 | messageGenerator: AsyncGenerator<ChatMessage[], PromptLog | undefined>, |
| 26 | abortController: AbortController, |
| 27 | systemToolFramework: SystemMessageToolsFramework, |
| 28 | ): AsyncGenerator<ChatMessage[], PromptLog | undefined> { |
| 29 | let buffer = ""; |
| 30 | let parseState: ToolCallParseState | undefined; |
| 31 | |
| 32 | while (true) { |
| 33 | const result = await messageGenerator.next(); |
| 34 | if (result.done) { |
| 35 | // Case: non-standard tool termination causes hanging args |
| 36 | if (parseState && !parseState.done && parseState.processedArgNames.size) { |
| 37 | yield [ |
| 38 | { |
| 39 | role: "assistant", |
| 40 | content: "", |
| 41 | toolCalls: [createDelta("", "}", parseState.toolCallId)], |
| 42 | }, |
| 43 | ]; |
| 44 | } |
| 45 | |
| 46 | return result.value; |
| 47 | } else { |
| 48 | for await (const message of result.value) { |
| 49 | if (abortController.signal.aborted || parseState?.done) { |
| 50 | break; |
| 51 | } |
| 52 | // Skip non-assistant messages or messages with native tool calls |
| 53 | if (message.role !== "assistant" || message.toolCalls) { |
| 54 | yield [message]; |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | const parts = normalizeToMessageParts(message); |
| 59 | |
| 60 | // Image output cannot be combined with tools |
| 61 | if (parts.find((part) => part.type === "imageUrl")) { |
| 62 | yield [message]; |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | const chunks = (parts as TextMessagePart[]) |
| 67 | .map((part) => splitAtCodeblocksAndNewLines(part.text)) |
| 68 | .flat(); |
| 69 | |
| 70 | for (const chunk of chunks) { |
| 71 | buffer += chunk; |
| 72 | if (!parseState) { |
| 73 | const { isInPartialStart, isInToolCall, modifiedBuffer } = |
| 74 | detectToolCallStart(buffer, systemToolFramework); |
| 75 | |
| 76 | if (isInPartialStart) { |
| 77 | continue; |
| 78 | } |
| 79 | if (isInToolCall) { |
| 80 | parseState = getInitialToolCallParseState(); |
| 81 | buffer = modifiedBuffer; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (parseState && !parseState.done) { |
| 86 | const delta = systemToolFramework.handleToolCallBuffer( |
| 87 | buffer, |
| 88 | parseState, |
| 89 | ); |
| 90 | if (delta) { |
| 91 | yield [ |
| 92 | { |
| 93 | ...message, |
| 94 | content: "", |
| 95 | toolCalls: [delta], |
| 96 | }, |
| 97 | ]; |
| 98 | } |
| 99 | } else { |
| 100 | // Prevent content after tool calls for now |
| 101 | if (parseState) { |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | // Yield normal assistant message |
| 106 | yield [ |
| 107 | { |
| 108 | ...message, |
| 109 | content: [{ type: "text", text: buffer }], |
| 110 | }, |
| 111 | ]; |
| 112 | } |
| 113 | buffer = ""; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |