(flush)
| 1220 | } |
| 1221 | |
| 1222 | _drain(flush) { |
| 1223 | const out = []; |
| 1224 | const toolCalls = []; |
| 1225 | const openRe = /<function_calls\b/i; |
| 1226 | while (this.buffer) { |
| 1227 | const open = this.buffer.search(openRe); |
| 1228 | if (open === -1) { |
| 1229 | const hold = partialFunctionCallsPrefixLength(this.buffer); |
| 1230 | if (!flush && hold > 0) { |
| 1231 | out.push(this.buffer.slice(0, this.buffer.length - hold)); |
| 1232 | this.buffer = this.buffer.slice(this.buffer.length - hold); |
| 1233 | break; |
| 1234 | } |
| 1235 | out.push(this.buffer); |
| 1236 | this.buffer = ''; |
| 1237 | break; |
| 1238 | } |
| 1239 | |
| 1240 | out.push(this.buffer.slice(0, open)); |
| 1241 | const tail = this.buffer.slice(open); |
| 1242 | const closeMatch = tail.match(/<\/function_calls>/i); |
| 1243 | if (!closeMatch) { |
| 1244 | if (flush) { |
| 1245 | this.buffer = ''; |
| 1246 | } else { |
| 1247 | this.buffer = tail; |
| 1248 | } |
| 1249 | break; |
| 1250 | } |
| 1251 | |
| 1252 | const end = open + closeMatch.index + closeMatch[0].length; |
| 1253 | const block = this.buffer.slice(open, end); |
| 1254 | const parsed = parseNativeFunctionCallsFromText(block, this.callerLookup); |
| 1255 | out.push(parsed.text || ''); |
| 1256 | toolCalls.push(...(parsed.toolCalls || [])); |
| 1257 | this.buffer = this.buffer.slice(end); |
| 1258 | } |
| 1259 | return { text: out.join(''), toolCalls }; |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | function partialFunctionCallsPrefixLength(text) { |
no test coverage detected