| 1 | import { SystemMessageToolsFramework } from "./types"; |
| 2 | |
| 3 | export function detectToolCallStart( |
| 4 | buffer: string, |
| 5 | toolCallFramework: SystemMessageToolsFramework, |
| 6 | ) { |
| 7 | const starts = toolCallFramework.acceptedToolCallStarts; |
| 8 | let modifiedBuffer = buffer; |
| 9 | let isInToolCall = false; |
| 10 | let isInPartialStart = false; |
| 11 | const lowerCaseBuffer = buffer.toLowerCase(); |
| 12 | for (let i = 0; i < starts.length; i++) { |
| 13 | const [start, replacement] = starts[i]; |
| 14 | if (lowerCaseBuffer.startsWith(start)) { |
| 15 | // for non-standard cases like no ```tool codeblock, etc, replace before adding to buffer, case insensitive |
| 16 | if (i !== 0) { |
| 17 | modifiedBuffer = buffer.replace(new RegExp(start, "i"), replacement); |
| 18 | } |
| 19 | isInToolCall = true; |
| 20 | break; |
| 21 | } else if (start.startsWith(lowerCaseBuffer)) { |
| 22 | isInPartialStart = true; |
| 23 | } |
| 24 | } |
| 25 | return { |
| 26 | isInToolCall, |
| 27 | isInPartialStart, |
| 28 | modifiedBuffer, |
| 29 | }; |
| 30 | } |