@ai-sdk-tool/parser 4.1.21 → 4.1.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ import {
5
5
  parse as parse2,
6
6
  stringify,
7
7
  unescapeXml
8
- } from "./chunk-NU2CVJEB.js";
8
+ } from "./chunk-2I56ZDEY.js";
9
9
  import {
10
10
  parse
11
11
  } from "./chunk-KLP3RUJ2.js";
@@ -898,10 +898,10 @@ function findTopLevelPropertyValueStart(text, property) {
898
898
  function extractTopLevelStringProperty(text, property) {
899
899
  const valueStart = findTopLevelPropertyValueStart(text, property);
900
900
  if (valueStart == null || valueStart >= text.length) {
901
- return void 0;
901
+ return;
902
902
  }
903
903
  if (text.charAt(valueStart) !== '"') {
904
- return void 0;
904
+ return;
905
905
  }
906
906
  let valueEnd = valueStart + 1;
907
907
  let escaped = false;
@@ -916,7 +916,7 @@ function extractTopLevelStringProperty(text, property) {
916
916
  }
917
917
  valueEnd += 1;
918
918
  }
919
- return void 0;
919
+ return;
920
920
  }
921
921
  function extractJsonValueSlice(text, valueStart) {
922
922
  if (valueStart >= text.length) {
@@ -1080,7 +1080,7 @@ function emitToolCallFromParsed(state, controller, parsedToolCall, tools) {
1080
1080
  }
1081
1081
  function canonicalizeArgumentsProgressInput(progress, toolName, tools) {
1082
1082
  if (progress.argumentsText === void 0 || !progress.argumentsComplete) {
1083
- return void 0;
1083
+ return;
1084
1084
  }
1085
1085
  try {
1086
1086
  const parsedArguments = parse(
@@ -1093,7 +1093,7 @@ function canonicalizeArgumentsProgressInput(progress, toolName, tools) {
1093
1093
  fallback: canonicalizeToolInput
1094
1094
  });
1095
1095
  } catch (e) {
1096
- return void 0;
1096
+ return;
1097
1097
  }
1098
1098
  }
1099
1099
  function emitToolInputProgress(state, controller, tools) {
@@ -3806,7 +3806,7 @@ function parseQwen3CoderToolParserClosedMatches(inner, outerNameAttr) {
3806
3806
  var _a, _b;
3807
3807
  const callBlockMatches = Array.from(inner.matchAll(CALL_BLOCK_RE));
3808
3808
  if (callBlockMatches.length === 0) {
3809
- return void 0;
3809
+ return;
3810
3810
  }
3811
3811
  const closedBlocks = [];
3812
3812
  let lastClosedEnd = 0;
@@ -3850,11 +3850,11 @@ function extractQwen3CoderToolNameFromMarkup(markup) {
3850
3850
  var _a, _b, _c, _d, _e;
3851
3851
  const match = markup.match(QWEN3CODER_TOOL_NAME_SALVAGE_REGEX);
3852
3852
  if (!match) {
3853
- return void 0;
3853
+ return;
3854
3854
  }
3855
3855
  const name = (_e = (_d = (_c = (_b = (_a = match[1]) != null ? _a : match[2]) != null ? _b : match[3]) != null ? _c : match[4]) != null ? _d : match[5]) != null ? _e : match[6];
3856
3856
  if (!name) {
3857
- return void 0;
3857
+ return;
3858
3858
  }
3859
3859
  const trimmed = name.trim();
3860
3860
  return trimmed.length > 0 ? trimmed : void 0;
@@ -4152,9 +4152,7 @@ var qwen3CoderProtocol = () => ({
4152
4152
  tryEmitToolCallSegment(synthetic, trailing);
4153
4153
  return true;
4154
4154
  };
4155
- const tryParseCallBlocksWithoutWrapper = () => {
4156
- return tryParseCallBlocksWithoutWrapperText(text);
4157
- };
4155
+ const tryParseCallBlocksWithoutWrapper = () => tryParseCallBlocksWithoutWrapperText(text);
4158
4156
  const tryParseSingleFunctionCall = () => {
4159
4157
  const lowerText = text.toLowerCase();
4160
4158
  const startIndex = lowerText.indexOf("<function");
@@ -5299,7 +5297,14 @@ function processToolCallMatch(text, tc, currentIndex, processedElements, options
5299
5297
  }
5300
5298
  addTextSegment(text.slice(currentIndex, tc.startIndex), processedElements);
5301
5299
  const result = parseYamlContent(tc.content);
5302
- if (!result.ok) {
5300
+ if (result.ok) {
5301
+ processedElements.push({
5302
+ type: "tool-call",
5303
+ toolCallId: generateToolCallId(),
5304
+ toolName: tc.toolName,
5305
+ input: JSON.stringify(result.value)
5306
+ });
5307
+ } else {
5303
5308
  const originalText = text.slice(tc.startIndex, tc.endIndex);
5304
5309
  const cause = yamlFailureCause(result.failure);
5305
5310
  (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, "Could not parse YAML tool call", {
@@ -5310,13 +5315,6 @@ function processToolCallMatch(text, tc, currentIndex, processedElements, options
5310
5315
  cause
5311
5316
  });
5312
5317
  processedElements.push({ type: "text", text: originalText });
5313
- } else {
5314
- processedElements.push({
5315
- type: "tool-call",
5316
- toolCallId: generateToolCallId(),
5317
- toolName: tc.toolName,
5318
- input: JSON.stringify(result.value)
5319
- });
5320
5318
  }
5321
5319
  return tc.endIndex;
5322
5320
  }
@@ -5343,337 +5341,335 @@ function stripTrailingPartialCloseTag(content, toolName) {
5343
5341
  )}${preservedLeadingWhitespace}`;
5344
5342
  return contentWithoutPartial.trimEnd();
5345
5343
  }
5346
- var yamlXmlProtocol = (_protocolOptions) => {
5347
- return {
5348
- formatTools({ tools, toolSystemPromptTemplate }) {
5349
- return formatToolsWithPromptTemplate({ tools, toolSystemPromptTemplate });
5350
- },
5351
- formatToolCall(toolCall) {
5352
- let args = {};
5353
- if (toolCall.input != null) {
5354
- try {
5355
- args = JSON.parse(toolCall.input);
5356
- } catch (e) {
5357
- args = { value: toolCall.input };
5358
- }
5344
+ var yamlXmlProtocol = (_protocolOptions) => ({
5345
+ formatTools({ tools, toolSystemPromptTemplate }) {
5346
+ return formatToolsWithPromptTemplate({ tools, toolSystemPromptTemplate });
5347
+ },
5348
+ formatToolCall(toolCall) {
5349
+ let args = {};
5350
+ if (toolCall.input != null) {
5351
+ try {
5352
+ args = JSON.parse(toolCall.input);
5353
+ } catch (e) {
5354
+ args = { value: toolCall.input };
5359
5355
  }
5360
- const yamlContent = YAML.stringify(args);
5361
- return `<${toolCall.toolName}>
5356
+ }
5357
+ const yamlContent = YAML.stringify(args);
5358
+ return `<${toolCall.toolName}>
5362
5359
  ${yamlContent}</${toolCall.toolName}>`;
5363
- },
5364
- parseGeneratedText({ text, tools, options }) {
5365
- const toolNames = extractToolNames(tools);
5366
- if (toolNames.length === 0) {
5367
- return [{ type: "text", text }];
5368
- }
5369
- const processedElements = [];
5370
- let currentIndex = 0;
5371
- let parseText = text;
5372
- let toolCalls = findToolCalls2(parseText, toolNames);
5373
- if (toolCalls.length === 0) {
5374
- const repaired = tryRepairXmlSelfClosingRootWithBody(
5375
- parseText,
5376
- toolNames
5377
- );
5378
- if (repaired) {
5379
- const repairedCalls = findToolCalls2(repaired, toolNames);
5380
- if (repairedCalls.length > 0) {
5381
- parseText = repaired;
5382
- toolCalls = repairedCalls;
5383
- }
5360
+ },
5361
+ parseGeneratedText({ text, tools, options }) {
5362
+ const toolNames = extractToolNames(tools);
5363
+ if (toolNames.length === 0) {
5364
+ return [{ type: "text", text }];
5365
+ }
5366
+ const processedElements = [];
5367
+ let currentIndex = 0;
5368
+ let parseText = text;
5369
+ let toolCalls = findToolCalls2(parseText, toolNames);
5370
+ if (toolCalls.length === 0) {
5371
+ const repaired = tryRepairXmlSelfClosingRootWithBody(
5372
+ parseText,
5373
+ toolNames
5374
+ );
5375
+ if (repaired) {
5376
+ const repairedCalls = findToolCalls2(repaired, toolNames);
5377
+ if (repairedCalls.length > 0) {
5378
+ parseText = repaired;
5379
+ toolCalls = repairedCalls;
5384
5380
  }
5385
5381
  }
5386
- for (const tc of toolCalls) {
5387
- currentIndex = processToolCallMatch(
5388
- parseText,
5389
- tc,
5390
- currentIndex,
5391
- processedElements,
5392
- options
5393
- );
5382
+ }
5383
+ for (const tc of toolCalls) {
5384
+ currentIndex = processToolCallMatch(
5385
+ parseText,
5386
+ tc,
5387
+ currentIndex,
5388
+ processedElements,
5389
+ options
5390
+ );
5391
+ }
5392
+ if (currentIndex < parseText.length) {
5393
+ addTextSegment(parseText.slice(currentIndex), processedElements);
5394
+ }
5395
+ return processedElements;
5396
+ },
5397
+ createStreamParser({ tools, options }) {
5398
+ const toolNames = extractToolNames(tools);
5399
+ let buffer = "";
5400
+ let currentToolCall = null;
5401
+ let currentTextId = null;
5402
+ let hasEmittedTextStart = false;
5403
+ const flushText = createFlushTextHandler(
5404
+ () => currentTextId,
5405
+ (newId) => {
5406
+ currentTextId = newId;
5407
+ },
5408
+ () => hasEmittedTextStart,
5409
+ (value) => {
5410
+ hasEmittedTextStart = value;
5394
5411
  }
5395
- if (currentIndex < parseText.length) {
5396
- addTextSegment(parseText.slice(currentIndex), processedElements);
5412
+ );
5413
+ const emitToolInputProgress2 = (controller, toolContent) => {
5414
+ if (!currentToolCall) {
5415
+ return;
5397
5416
  }
5398
- return processedElements;
5399
- },
5400
- createStreamParser({ tools, options }) {
5401
- const toolNames = extractToolNames(tools);
5402
- let buffer = "";
5403
- let currentToolCall = null;
5404
- let currentTextId = null;
5405
- let hasEmittedTextStart = false;
5406
- const flushText = createFlushTextHandler(
5407
- () => currentTextId,
5408
- (newId) => {
5409
- currentTextId = newId;
5410
- },
5411
- () => hasEmittedTextStart,
5412
- (value) => {
5413
- hasEmittedTextStart = value;
5414
- }
5415
- );
5416
- const emitToolInputProgress2 = (controller, toolContent) => {
5417
- if (!currentToolCall) {
5418
- return;
5419
- }
5420
- const parsedArgs = parseYamlContentForStreamProgress(toolContent);
5421
- if (parsedArgs === null) {
5422
- return;
5423
- }
5424
- const fullInput = stringifyToolInputWithSchema({
5425
- toolName: currentToolCall.name,
5426
- args: parsedArgs,
5417
+ const parsedArgs = parseYamlContentForStreamProgress(toolContent);
5418
+ if (parsedArgs === null) {
5419
+ return;
5420
+ }
5421
+ const fullInput = stringifyToolInputWithSchema({
5422
+ toolName: currentToolCall.name,
5423
+ args: parsedArgs,
5424
+ tools
5425
+ });
5426
+ if (fullInput === "{}" && toolContent.trim().length === 0) {
5427
+ return;
5428
+ }
5429
+ emitToolInputProgressDelta({
5430
+ controller,
5431
+ id: currentToolCall.toolCallId,
5432
+ state: currentToolCall,
5433
+ fullInput
5434
+ });
5435
+ };
5436
+ const processToolCallEnd = (controller, toolContent, toolName, toolCallId) => {
5437
+ var _a;
5438
+ const result = parseYamlContent(toolContent);
5439
+ flushText(controller);
5440
+ if (result.ok) {
5441
+ const finalInput = stringifyToolInputWithSchema({
5442
+ toolName,
5443
+ args: result.value,
5427
5444
  tools
5428
5445
  });
5429
- if (fullInput === "{}" && toolContent.trim().length === 0) {
5430
- return;
5431
- }
5432
- emitToolInputProgressDelta({
5433
- controller,
5434
- id: currentToolCall.toolCallId,
5435
- state: currentToolCall,
5436
- fullInput
5437
- });
5438
- };
5439
- const processToolCallEnd = (controller, toolContent, toolName, toolCallId) => {
5440
- var _a;
5441
- const result = parseYamlContent(toolContent);
5442
- flushText(controller);
5443
- if (!result.ok) {
5444
- const original = `<${toolName}>${toolContent}</${toolName}>`;
5445
- const emitRawFallback = shouldEmitRawToolCallTextOnError(options);
5446
- emitFailedToolInputLifecycle({
5446
+ if (currentToolCall && currentToolCall.toolCallId === toolCallId) {
5447
+ emitFinalizedToolInputLifecycle({
5447
5448
  controller,
5448
5449
  id: toolCallId,
5449
- emitRawToolCallTextOnError: emitRawFallback,
5450
- rawToolCallText: original,
5451
- emitRawText: (rawText) => {
5452
- flushText(controller, rawText);
5453
- }
5454
- });
5455
- (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, "Could not parse streaming YAML tool call", {
5456
- toolCall: original,
5457
- toolName,
5458
- toolCallId,
5459
- dropReason: "malformed-tool-call-body",
5460
- cause: yamlFailureCause(result.failure)
5461
- });
5462
- } else {
5463
- const finalInput = stringifyToolInputWithSchema({
5450
+ state: currentToolCall,
5464
5451
  toolName,
5465
- args: result.value,
5466
- tools
5467
- });
5468
- if (currentToolCall && currentToolCall.toolCallId === toolCallId) {
5469
- emitFinalizedToolInputLifecycle({
5470
- controller,
5471
- id: toolCallId,
5472
- state: currentToolCall,
5473
- toolName,
5474
- finalInput,
5475
- onMismatch: options == null ? void 0 : options.onError
5476
- });
5477
- } else {
5478
- enqueueToolInputEndAndCall({
5479
- controller,
5480
- id: toolCallId,
5481
- toolName,
5482
- input: finalInput
5483
- });
5484
- }
5485
- }
5486
- };
5487
- const finalizeUnclosedToolCall = (controller) => {
5488
- var _a;
5489
- if (!currentToolCall) {
5490
- return;
5491
- }
5492
- emitToolInputProgress2(controller, buffer);
5493
- const { name: toolName, toolCallId } = currentToolCall;
5494
- const reconciledBuffer = stripTrailingPartialCloseTag(buffer, toolName);
5495
- const result = parseYamlContent(reconciledBuffer);
5496
- flushText(controller);
5497
- if (!result.ok) {
5498
- const unfinishedContent = `<${toolName}>${buffer}`;
5499
- const emitRawFallback = shouldEmitRawToolCallTextOnError(options);
5500
- emitFailedToolInputLifecycle({
5501
- controller,
5502
- id: toolCallId,
5503
- emitRawToolCallTextOnError: emitRawFallback,
5504
- rawToolCallText: unfinishedContent,
5505
- emitRawText: (rawText) => {
5506
- flushText(controller, rawText);
5507
- }
5452
+ finalInput,
5453
+ onMismatch: options == null ? void 0 : options.onError
5508
5454
  });
5509
- (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
5510
- options,
5511
- "Could not complete streaming YAML tool call at finish.",
5512
- {
5513
- toolCall: unfinishedContent,
5514
- toolCallId,
5515
- toolName,
5516
- dropReason: "unfinished-tool-call",
5517
- cause: yamlFailureCause(result.failure)
5518
- }
5519
- );
5520
5455
  } else {
5521
- const finalInput = stringifyToolInputWithSchema({
5522
- toolName,
5523
- args: result.value,
5524
- tools
5525
- });
5526
- emitFinalizedToolInputLifecycle({
5456
+ enqueueToolInputEndAndCall({
5527
5457
  controller,
5528
5458
  id: toolCallId,
5529
- state: currentToolCall,
5530
5459
  toolName,
5531
- finalInput,
5532
- onMismatch: options == null ? void 0 : options.onError
5460
+ input: finalInput
5533
5461
  });
5534
5462
  }
5535
- buffer = "";
5536
- currentToolCall = null;
5537
- };
5538
- const handlePendingToolCall = (controller, endTag, toolName) => {
5539
- var _a;
5540
- const endIdx = buffer.indexOf(endTag);
5541
- if (endIdx === -1) {
5542
- emitToolInputProgress2(controller, buffer);
5543
- return false;
5544
- }
5545
- const content = buffer.slice(0, endIdx);
5546
- emitToolInputProgress2(controller, content);
5547
- buffer = buffer.slice(endIdx + endTag.length);
5548
- processToolCallEnd(
5463
+ } else {
5464
+ const original = `<${toolName}>${toolContent}</${toolName}>`;
5465
+ const emitRawFallback = shouldEmitRawToolCallTextOnError(options);
5466
+ emitFailedToolInputLifecycle({
5549
5467
  controller,
5550
- content,
5468
+ id: toolCallId,
5469
+ emitRawToolCallTextOnError: emitRawFallback,
5470
+ rawToolCallText: original,
5471
+ emitRawText: (rawText) => {
5472
+ flushText(controller, rawText);
5473
+ }
5474
+ });
5475
+ (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, "Could not parse streaming YAML tool call", {
5476
+ toolCall: original,
5551
5477
  toolName,
5552
- (_a = currentToolCall == null ? void 0 : currentToolCall.toolCallId) != null ? _a : generateToolCallId()
5478
+ toolCallId,
5479
+ dropReason: "malformed-tool-call-body",
5480
+ cause: yamlFailureCause(result.failure)
5481
+ });
5482
+ }
5483
+ };
5484
+ const finalizeUnclosedToolCall = (controller) => {
5485
+ var _a;
5486
+ if (!currentToolCall) {
5487
+ return;
5488
+ }
5489
+ emitToolInputProgress2(controller, buffer);
5490
+ const { name: toolName, toolCallId } = currentToolCall;
5491
+ const reconciledBuffer = stripTrailingPartialCloseTag(buffer, toolName);
5492
+ const result = parseYamlContent(reconciledBuffer);
5493
+ flushText(controller);
5494
+ if (result.ok) {
5495
+ const finalInput = stringifyToolInputWithSchema({
5496
+ toolName,
5497
+ args: result.value,
5498
+ tools
5499
+ });
5500
+ emitFinalizedToolInputLifecycle({
5501
+ controller,
5502
+ id: toolCallId,
5503
+ state: currentToolCall,
5504
+ toolName,
5505
+ finalInput,
5506
+ onMismatch: options == null ? void 0 : options.onError
5507
+ });
5508
+ } else {
5509
+ const unfinishedContent = `<${toolName}>${buffer}`;
5510
+ const emitRawFallback = shouldEmitRawToolCallTextOnError(options);
5511
+ emitFailedToolInputLifecycle({
5512
+ controller,
5513
+ id: toolCallId,
5514
+ emitRawToolCallTextOnError: emitRawFallback,
5515
+ rawToolCallText: unfinishedContent,
5516
+ emitRawText: (rawText) => {
5517
+ flushText(controller, rawText);
5518
+ }
5519
+ });
5520
+ (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
5521
+ options,
5522
+ "Could not complete streaming YAML tool call at finish.",
5523
+ {
5524
+ toolCall: unfinishedContent,
5525
+ toolCallId,
5526
+ toolName,
5527
+ dropReason: "unfinished-tool-call",
5528
+ cause: yamlFailureCause(result.failure)
5529
+ }
5553
5530
  );
5531
+ }
5532
+ buffer = "";
5533
+ currentToolCall = null;
5534
+ };
5535
+ const handlePendingToolCall = (controller, endTag, toolName) => {
5536
+ var _a;
5537
+ const endIdx = buffer.indexOf(endTag);
5538
+ if (endIdx === -1) {
5539
+ emitToolInputProgress2(controller, buffer);
5540
+ return false;
5541
+ }
5542
+ const content = buffer.slice(0, endIdx);
5543
+ emitToolInputProgress2(controller, content);
5544
+ buffer = buffer.slice(endIdx + endTag.length);
5545
+ processToolCallEnd(
5546
+ controller,
5547
+ content,
5548
+ toolName,
5549
+ (_a = currentToolCall == null ? void 0 : currentToolCall.toolCallId) != null ? _a : generateToolCallId()
5550
+ );
5551
+ currentToolCall = null;
5552
+ return true;
5553
+ };
5554
+ const flushSafeText = (controller) => {
5555
+ const maxTagLen = toolNames.length ? Math.max(...toolNames.map((n) => `<${n} />`.length)) : 0;
5556
+ const tail = Math.max(0, maxTagLen - 1);
5557
+ const safeLen = Math.max(0, buffer.length - tail);
5558
+ if (safeLen > 0) {
5559
+ flushText(controller, buffer.slice(0, safeLen));
5560
+ buffer = buffer.slice(safeLen);
5561
+ }
5562
+ };
5563
+ const handleNewToolTag = (controller, tagIndex, tagName, selfClosing, tagLength) => {
5564
+ if (tagIndex > 0) {
5565
+ flushText(controller, buffer.slice(0, tagIndex));
5566
+ }
5567
+ flushText(controller);
5568
+ if (selfClosing) {
5569
+ buffer = buffer.slice(tagIndex + tagLength);
5570
+ const toolCallId = generateToolCallId();
5571
+ currentToolCall = {
5572
+ name: tagName,
5573
+ toolCallId,
5574
+ emittedInput: ""
5575
+ };
5576
+ controller.enqueue({
5577
+ type: "tool-input-start",
5578
+ id: toolCallId,
5579
+ toolName: tagName
5580
+ });
5581
+ processToolCallEnd(controller, "", tagName, toolCallId);
5554
5582
  currentToolCall = null;
5555
- return true;
5556
- };
5557
- const flushSafeText = (controller) => {
5558
- const maxTagLen = toolNames.length ? Math.max(...toolNames.map((n) => `<${n} />`.length)) : 0;
5559
- const tail = Math.max(0, maxTagLen - 1);
5560
- const safeLen = Math.max(0, buffer.length - tail);
5561
- if (safeLen > 0) {
5562
- flushText(controller, buffer.slice(0, safeLen));
5563
- buffer = buffer.slice(safeLen);
5564
- }
5565
- };
5566
- const handleNewToolTag = (controller, tagIndex, tagName, selfClosing, tagLength) => {
5567
- if (tagIndex > 0) {
5568
- flushText(controller, buffer.slice(0, tagIndex));
5569
- }
5570
- flushText(controller);
5571
- if (selfClosing) {
5572
- buffer = buffer.slice(tagIndex + tagLength);
5573
- const toolCallId = generateToolCallId();
5574
- currentToolCall = {
5575
- name: tagName,
5576
- toolCallId,
5577
- emittedInput: ""
5578
- };
5579
- controller.enqueue({
5580
- type: "tool-input-start",
5581
- id: toolCallId,
5582
- toolName: tagName
5583
- });
5584
- processToolCallEnd(controller, "", tagName, toolCallId);
5585
- currentToolCall = null;
5583
+ } else {
5584
+ const startTag = `<${tagName}>`;
5585
+ buffer = buffer.slice(tagIndex + startTag.length);
5586
+ currentToolCall = {
5587
+ name: tagName,
5588
+ toolCallId: generateToolCallId(),
5589
+ emittedInput: ""
5590
+ };
5591
+ controller.enqueue({
5592
+ type: "tool-input-start",
5593
+ id: currentToolCall.toolCallId,
5594
+ toolName: tagName
5595
+ });
5596
+ }
5597
+ };
5598
+ const processBuffer = (controller) => {
5599
+ while (true) {
5600
+ if (currentToolCall) {
5601
+ const toolName = currentToolCall.name;
5602
+ const endTag = `</${toolName}>`;
5603
+ if (!handlePendingToolCall(controller, endTag, toolName)) {
5604
+ break;
5605
+ }
5586
5606
  } else {
5587
- const startTag = `<${tagName}>`;
5588
- buffer = buffer.slice(tagIndex + startTag.length);
5589
- currentToolCall = {
5590
- name: tagName,
5591
- toolCallId: generateToolCallId(),
5592
- emittedInput: ""
5593
- };
5594
- controller.enqueue({
5595
- type: "tool-input-start",
5596
- id: currentToolCall.toolCallId,
5597
- toolName: tagName
5598
- });
5599
- }
5600
- };
5601
- const processBuffer = (controller) => {
5602
- while (true) {
5603
- if (currentToolCall) {
5604
- const toolName = currentToolCall.name;
5605
- const endTag = `</${toolName}>`;
5606
- if (!handlePendingToolCall(controller, endTag, toolName)) {
5607
- break;
5608
- }
5609
- } else {
5610
- const { index, name, selfClosing, tagLength } = findEarliestToolTag(
5611
- buffer,
5612
- toolNames
5613
- );
5614
- if (index === -1) {
5615
- flushSafeText(controller);
5616
- break;
5617
- }
5618
- handleNewToolTag(controller, index, name, selfClosing, tagLength);
5607
+ const { index, name, selfClosing, tagLength } = findEarliestToolTag(
5608
+ buffer,
5609
+ toolNames
5610
+ );
5611
+ if (index === -1) {
5612
+ flushSafeText(controller);
5613
+ break;
5619
5614
  }
5615
+ handleNewToolTag(controller, index, name, selfClosing, tagLength);
5620
5616
  }
5621
- };
5622
- return new TransformStream({
5623
- transform(chunk, controller) {
5624
- var _a;
5625
- if (chunk.type === "finish") {
5626
- if (currentToolCall) {
5627
- finalizeUnclosedToolCall(controller);
5628
- } else if (buffer) {
5629
- flushText(controller, buffer);
5630
- buffer = "";
5631
- }
5632
- flushText(controller);
5633
- controller.enqueue(chunk);
5634
- return;
5635
- }
5636
- if (chunk.type !== "text-delta") {
5637
- if (!currentToolCall && buffer) {
5638
- flushText(controller, buffer);
5639
- buffer = "";
5640
- }
5641
- controller.enqueue(chunk);
5642
- return;
5643
- }
5644
- const textContent = (_a = chunk.delta) != null ? _a : "";
5645
- buffer += textContent;
5646
- processBuffer(controller);
5647
- },
5648
- flush(controller) {
5617
+ }
5618
+ };
5619
+ return new TransformStream({
5620
+ transform(chunk, controller) {
5621
+ var _a;
5622
+ if (chunk.type === "finish") {
5649
5623
  if (currentToolCall) {
5650
5624
  finalizeUnclosedToolCall(controller);
5651
5625
  } else if (buffer) {
5652
5626
  flushText(controller, buffer);
5653
5627
  buffer = "";
5654
5628
  }
5655
- if (currentTextId && hasEmittedTextStart) {
5656
- controller.enqueue({
5657
- type: "text-end",
5658
- id: currentTextId
5659
- });
5660
- hasEmittedTextStart = false;
5661
- currentTextId = null;
5629
+ flushText(controller);
5630
+ controller.enqueue(chunk);
5631
+ return;
5632
+ }
5633
+ if (chunk.type !== "text-delta") {
5634
+ if (!currentToolCall && buffer) {
5635
+ flushText(controller, buffer);
5636
+ buffer = "";
5662
5637
  }
5638
+ controller.enqueue(chunk);
5639
+ return;
5640
+ }
5641
+ const textContent = (_a = chunk.delta) != null ? _a : "";
5642
+ buffer += textContent;
5643
+ processBuffer(controller);
5644
+ },
5645
+ flush(controller) {
5646
+ if (currentToolCall) {
5647
+ finalizeUnclosedToolCall(controller);
5648
+ } else if (buffer) {
5649
+ flushText(controller, buffer);
5650
+ buffer = "";
5651
+ }
5652
+ if (currentTextId && hasEmittedTextStart) {
5653
+ controller.enqueue({
5654
+ type: "text-end",
5655
+ id: currentTextId
5656
+ });
5657
+ hasEmittedTextStart = false;
5658
+ currentTextId = null;
5663
5659
  }
5664
- });
5665
- },
5666
- extractToolCallSegments({ text, tools }) {
5667
- const toolNames = tools.map((t) => t.name).filter(Boolean);
5668
- if (toolNames.length === 0) {
5669
- return [];
5670
5660
  }
5671
- return findToolCalls2(text, toolNames).map(
5672
- (tc) => `<${tc.toolName}>${tc.content}</${tc.toolName}>`
5673
- );
5661
+ });
5662
+ },
5663
+ extractToolCallSegments({ text, tools }) {
5664
+ const toolNames = tools.map((t) => t.name).filter(Boolean);
5665
+ if (toolNames.length === 0) {
5666
+ return [];
5674
5667
  }
5675
- };
5676
- };
5668
+ return findToolCalls2(text, toolNames).map(
5669
+ (tc) => `<${tc.toolName}>${tc.content}</${tc.toolName}>`
5670
+ );
5671
+ }
5672
+ });
5677
5673
 
5678
5674
  // src/core/utils/dynamic-tool-schema.ts
5679
5675
  function createDynamicIfThenElseSchema(tools) {
@@ -5869,7 +5865,7 @@ function parseJsonCandidate(candidateText) {
5869
5865
  try {
5870
5866
  return parse(candidateText);
5871
5867
  } catch (e) {
5872
- return void 0;
5868
+ return;
5873
5869
  }
5874
5870
  }
5875
5871
  function extractCodeBlockCandidates(text) {
@@ -7853,4 +7849,4 @@ export {
7853
7849
  morphXmlToolMiddleware,
7854
7850
  yamlXmlToolMiddleware
7855
7851
  };
7856
- //# sourceMappingURL=chunk-GAJX7HXU.js.map
7852
+ //# sourceMappingURL=chunk-DKZ4GAQM.js.map