| 1 | import { ChatMessage, PromptLog, TextMessagePart } from "../.."; |
| 2 | import { normalizeToMessageParts } from "../../util/messageContent"; |
| 3 | import { detectToolCallStart } from "./detectToolCallStart"; |
| 4 | import { |
| 5 | getInitialTooLCallParseState, |
| 6 | handleToolCallBuffer, |
| 7 | ToolCallParseState, |
| 8 | } from "./parseSystemToolCall"; |
| 9 | import { createDelta, splitAtCodeblocksAndNewLines } from "./systemToolUtils"; |
| 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 | ): AsyncGenerator<ChatMessage[], PromptLog | undefined> { |
| 28 | let buffer = ""; |
| 29 | let parseState: ToolCallParseState | undefined; |
| 30 | |
| 31 | while (true) { |
| 32 | const result = await messageGenerator.next(); |
| 33 | if (result.done) { |
| 34 | // Case: non-standard tool termination causes hanging args |
| 35 | if (parseState && !parseState.done && parseState.processedArgNames.size) { |
| 36 | yield [ |
| 37 | { |
| 38 | role: "assistant", |
| 39 | content: "", |
| 40 | toolCalls: [createDelta("", "}", parseState.toolCallId)], |
| 41 | }, |
| 42 | ]; |
| 43 | } |
| 44 | |
| 45 | return result.value; |
| 46 | } else { |
| 47 | for await (const message of result.value) { |
| 48 | if (abortController.signal.aborted || parseState?.done) { |
| 49 | break; |
| 50 | } |
| 51 | // Skip non-assistant messages or messages with native tool calls |
| 52 | if (message.role !== "assistant" || message.toolCalls) { |
| 53 | yield [message]; |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | const parts = normalizeToMessageParts(message); |
| 58 | |
| 59 | // Image output cannot be combined with tools |
| 60 | if (parts.find((part) => part.type === "imageUrl")) { |
| 61 | yield [message]; |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | const chunks = (parts as TextMessagePart[]) |
| 66 | .map((part) => splitAtCodeblocksAndNewLines(part.text)) |
| 67 | .flat(); |
| 68 | |
| 69 | for (const chunk of chunks) { |
| 70 | buffer += chunk; |
| 71 | if (!parseState) { |
| 72 | const { isInPartialStart, isInToolCall, modifiedBuffer } = |
| 73 | detectToolCallStart(buffer); |
| 74 | |
| 75 | if (isInPartialStart) { |
| 76 | continue; |
| 77 | } |
| 78 | if (isInToolCall) { |
| 79 | parseState = getInitialTooLCallParseState(); |
| 80 | buffer = modifiedBuffer; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (parseState && !parseState.done) { |
| 85 | const delta = handleToolCallBuffer(buffer, parseState); |
| 86 | if (delta) { |
| 87 | yield [ |
| 88 | { |
| 89 | ...message, |
| 90 | content: "", |
| 91 | toolCalls: [delta], |
| 92 | }, |
| 93 | ]; |
| 94 | } |
| 95 | } else { |
| 96 | // Prevent content after tool calls for now |
| 97 | if (parseState) { |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | // Yield normal assistant message |
| 102 | yield [ |
| 103 | { |
| 104 | ...message, |
| 105 | content: [{ type: "text", text: buffer }], |
| 106 | }, |
| 107 | ]; |
| 108 | } |
| 109 | buffer = ""; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |