@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.
@@ -2862,7 +2862,7 @@ function handleOpeningTagSegment(src, lt, out, stack) {
2862
2862
  function extractSchemaProperties(schema) {
2863
2863
  const unwrapped = unwrapJsonSchema(schema);
2864
2864
  if (!unwrapped || typeof unwrapped !== "object") {
2865
- return void 0;
2865
+ return;
2866
2866
  }
2867
2867
  return unwrapped.properties;
2868
2868
  }
@@ -5061,10 +5061,10 @@ function findTopLevelPropertyValueStart(text, property) {
5061
5061
  function extractTopLevelStringProperty(text, property) {
5062
5062
  const valueStart = findTopLevelPropertyValueStart(text, property);
5063
5063
  if (valueStart == null || valueStart >= text.length) {
5064
- return void 0;
5064
+ return;
5065
5065
  }
5066
5066
  if (text.charAt(valueStart) !== '"') {
5067
- return void 0;
5067
+ return;
5068
5068
  }
5069
5069
  let valueEnd = valueStart + 1;
5070
5070
  let escaped = false;
@@ -5079,7 +5079,7 @@ function extractTopLevelStringProperty(text, property) {
5079
5079
  }
5080
5080
  valueEnd += 1;
5081
5081
  }
5082
- return void 0;
5082
+ return;
5083
5083
  }
5084
5084
  function extractJsonValueSlice(text, valueStart) {
5085
5085
  if (valueStart >= text.length) {
@@ -5243,7 +5243,7 @@ function emitToolCallFromParsed(state, controller, parsedToolCall, tools) {
5243
5243
  }
5244
5244
  function canonicalizeArgumentsProgressInput(progress, toolName, tools) {
5245
5245
  if (progress.argumentsText === void 0 || !progress.argumentsComplete) {
5246
- return void 0;
5246
+ return;
5247
5247
  }
5248
5248
  try {
5249
5249
  const parsedArguments = parse3(
@@ -5256,7 +5256,7 @@ function canonicalizeArgumentsProgressInput(progress, toolName, tools) {
5256
5256
  fallback: canonicalizeToolInput
5257
5257
  });
5258
5258
  } catch (e) {
5259
- return void 0;
5259
+ return;
5260
5260
  }
5261
5261
  }
5262
5262
  function emitToolInputProgress(state, controller, tools) {
@@ -7969,7 +7969,7 @@ function parseQwen3CoderToolParserClosedMatches(inner, outerNameAttr) {
7969
7969
  var _a, _b;
7970
7970
  const callBlockMatches = Array.from(inner.matchAll(CALL_BLOCK_RE));
7971
7971
  if (callBlockMatches.length === 0) {
7972
- return void 0;
7972
+ return;
7973
7973
  }
7974
7974
  const closedBlocks = [];
7975
7975
  let lastClosedEnd = 0;
@@ -8013,11 +8013,11 @@ function extractQwen3CoderToolNameFromMarkup(markup) {
8013
8013
  var _a, _b, _c, _d, _e;
8014
8014
  const match = markup.match(QWEN3CODER_TOOL_NAME_SALVAGE_REGEX);
8015
8015
  if (!match) {
8016
- return void 0;
8016
+ return;
8017
8017
  }
8018
8018
  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];
8019
8019
  if (!name) {
8020
- return void 0;
8020
+ return;
8021
8021
  }
8022
8022
  const trimmed = name.trim();
8023
8023
  return trimmed.length > 0 ? trimmed : void 0;
@@ -8315,9 +8315,7 @@ var qwen3CoderProtocol = () => ({
8315
8315
  tryEmitToolCallSegment(synthetic, trailing);
8316
8316
  return true;
8317
8317
  };
8318
- const tryParseCallBlocksWithoutWrapper = () => {
8319
- return tryParseCallBlocksWithoutWrapperText(text);
8320
- };
8318
+ const tryParseCallBlocksWithoutWrapper = () => tryParseCallBlocksWithoutWrapperText(text);
8321
8319
  const tryParseSingleFunctionCall = () => {
8322
8320
  const lowerText = text.toLowerCase();
8323
8321
  const startIndex = lowerText.indexOf("<function");
@@ -9461,7 +9459,14 @@ function processToolCallMatch(text, tc, currentIndex, processedElements, options
9461
9459
  }
9462
9460
  addTextSegment(text.slice(currentIndex, tc.startIndex), processedElements);
9463
9461
  const result = parseYamlContent(tc.content);
9464
- if (!result.ok) {
9462
+ if (result.ok) {
9463
+ processedElements.push({
9464
+ type: "tool-call",
9465
+ toolCallId: generateToolCallId(),
9466
+ toolName: tc.toolName,
9467
+ input: JSON.stringify(result.value)
9468
+ });
9469
+ } else {
9465
9470
  const originalText = text.slice(tc.startIndex, tc.endIndex);
9466
9471
  const cause = yamlFailureCause(result.failure);
9467
9472
  (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, "Could not parse YAML tool call", {
@@ -9472,13 +9477,6 @@ function processToolCallMatch(text, tc, currentIndex, processedElements, options
9472
9477
  cause
9473
9478
  });
9474
9479
  processedElements.push({ type: "text", text: originalText });
9475
- } else {
9476
- processedElements.push({
9477
- type: "tool-call",
9478
- toolCallId: generateToolCallId(),
9479
- toolName: tc.toolName,
9480
- input: JSON.stringify(result.value)
9481
- });
9482
9480
  }
9483
9481
  return tc.endIndex;
9484
9482
  }
@@ -9505,337 +9503,335 @@ function stripTrailingPartialCloseTag(content, toolName) {
9505
9503
  )}${preservedLeadingWhitespace}`;
9506
9504
  return contentWithoutPartial.trimEnd();
9507
9505
  }
9508
- var yamlXmlProtocol = (_protocolOptions) => {
9509
- return {
9510
- formatTools({ tools, toolSystemPromptTemplate }) {
9511
- return formatToolsWithPromptTemplate({ tools, toolSystemPromptTemplate });
9512
- },
9513
- formatToolCall(toolCall) {
9514
- let args = {};
9515
- if (toolCall.input != null) {
9516
- try {
9517
- args = JSON.parse(toolCall.input);
9518
- } catch (e) {
9519
- args = { value: toolCall.input };
9520
- }
9506
+ var yamlXmlProtocol = (_protocolOptions) => ({
9507
+ formatTools({ tools, toolSystemPromptTemplate }) {
9508
+ return formatToolsWithPromptTemplate({ tools, toolSystemPromptTemplate });
9509
+ },
9510
+ formatToolCall(toolCall) {
9511
+ let args = {};
9512
+ if (toolCall.input != null) {
9513
+ try {
9514
+ args = JSON.parse(toolCall.input);
9515
+ } catch (e) {
9516
+ args = { value: toolCall.input };
9521
9517
  }
9522
- const yamlContent = import_yaml.default.stringify(args);
9523
- return `<${toolCall.toolName}>
9518
+ }
9519
+ const yamlContent = import_yaml.default.stringify(args);
9520
+ return `<${toolCall.toolName}>
9524
9521
  ${yamlContent}</${toolCall.toolName}>`;
9525
- },
9526
- parseGeneratedText({ text, tools, options }) {
9527
- const toolNames = extractToolNames(tools);
9528
- if (toolNames.length === 0) {
9529
- return [{ type: "text", text }];
9530
- }
9531
- const processedElements = [];
9532
- let currentIndex = 0;
9533
- let parseText = text;
9534
- let toolCalls = findToolCalls2(parseText, toolNames);
9535
- if (toolCalls.length === 0) {
9536
- const repaired = tryRepairXmlSelfClosingRootWithBody(
9537
- parseText,
9538
- toolNames
9539
- );
9540
- if (repaired) {
9541
- const repairedCalls = findToolCalls2(repaired, toolNames);
9542
- if (repairedCalls.length > 0) {
9543
- parseText = repaired;
9544
- toolCalls = repairedCalls;
9545
- }
9522
+ },
9523
+ parseGeneratedText({ text, tools, options }) {
9524
+ const toolNames = extractToolNames(tools);
9525
+ if (toolNames.length === 0) {
9526
+ return [{ type: "text", text }];
9527
+ }
9528
+ const processedElements = [];
9529
+ let currentIndex = 0;
9530
+ let parseText = text;
9531
+ let toolCalls = findToolCalls2(parseText, toolNames);
9532
+ if (toolCalls.length === 0) {
9533
+ const repaired = tryRepairXmlSelfClosingRootWithBody(
9534
+ parseText,
9535
+ toolNames
9536
+ );
9537
+ if (repaired) {
9538
+ const repairedCalls = findToolCalls2(repaired, toolNames);
9539
+ if (repairedCalls.length > 0) {
9540
+ parseText = repaired;
9541
+ toolCalls = repairedCalls;
9546
9542
  }
9547
9543
  }
9548
- for (const tc of toolCalls) {
9549
- currentIndex = processToolCallMatch(
9550
- parseText,
9551
- tc,
9552
- currentIndex,
9553
- processedElements,
9554
- options
9555
- );
9544
+ }
9545
+ for (const tc of toolCalls) {
9546
+ currentIndex = processToolCallMatch(
9547
+ parseText,
9548
+ tc,
9549
+ currentIndex,
9550
+ processedElements,
9551
+ options
9552
+ );
9553
+ }
9554
+ if (currentIndex < parseText.length) {
9555
+ addTextSegment(parseText.slice(currentIndex), processedElements);
9556
+ }
9557
+ return processedElements;
9558
+ },
9559
+ createStreamParser({ tools, options }) {
9560
+ const toolNames = extractToolNames(tools);
9561
+ let buffer = "";
9562
+ let currentToolCall = null;
9563
+ let currentTextId = null;
9564
+ let hasEmittedTextStart = false;
9565
+ const flushText = createFlushTextHandler(
9566
+ () => currentTextId,
9567
+ (newId) => {
9568
+ currentTextId = newId;
9569
+ },
9570
+ () => hasEmittedTextStart,
9571
+ (value) => {
9572
+ hasEmittedTextStart = value;
9556
9573
  }
9557
- if (currentIndex < parseText.length) {
9558
- addTextSegment(parseText.slice(currentIndex), processedElements);
9574
+ );
9575
+ const emitToolInputProgress2 = (controller, toolContent) => {
9576
+ if (!currentToolCall) {
9577
+ return;
9559
9578
  }
9560
- return processedElements;
9561
- },
9562
- createStreamParser({ tools, options }) {
9563
- const toolNames = extractToolNames(tools);
9564
- let buffer = "";
9565
- let currentToolCall = null;
9566
- let currentTextId = null;
9567
- let hasEmittedTextStart = false;
9568
- const flushText = createFlushTextHandler(
9569
- () => currentTextId,
9570
- (newId) => {
9571
- currentTextId = newId;
9572
- },
9573
- () => hasEmittedTextStart,
9574
- (value) => {
9575
- hasEmittedTextStart = value;
9576
- }
9577
- );
9578
- const emitToolInputProgress2 = (controller, toolContent) => {
9579
- if (!currentToolCall) {
9580
- return;
9581
- }
9582
- const parsedArgs = parseYamlContentForStreamProgress(toolContent);
9583
- if (parsedArgs === null) {
9584
- return;
9585
- }
9586
- const fullInput = stringifyToolInputWithSchema({
9587
- toolName: currentToolCall.name,
9588
- args: parsedArgs,
9579
+ const parsedArgs = parseYamlContentForStreamProgress(toolContent);
9580
+ if (parsedArgs === null) {
9581
+ return;
9582
+ }
9583
+ const fullInput = stringifyToolInputWithSchema({
9584
+ toolName: currentToolCall.name,
9585
+ args: parsedArgs,
9586
+ tools
9587
+ });
9588
+ if (fullInput === "{}" && toolContent.trim().length === 0) {
9589
+ return;
9590
+ }
9591
+ emitToolInputProgressDelta({
9592
+ controller,
9593
+ id: currentToolCall.toolCallId,
9594
+ state: currentToolCall,
9595
+ fullInput
9596
+ });
9597
+ };
9598
+ const processToolCallEnd = (controller, toolContent, toolName, toolCallId) => {
9599
+ var _a;
9600
+ const result = parseYamlContent(toolContent);
9601
+ flushText(controller);
9602
+ if (result.ok) {
9603
+ const finalInput = stringifyToolInputWithSchema({
9604
+ toolName,
9605
+ args: result.value,
9589
9606
  tools
9590
9607
  });
9591
- if (fullInput === "{}" && toolContent.trim().length === 0) {
9592
- return;
9593
- }
9594
- emitToolInputProgressDelta({
9595
- controller,
9596
- id: currentToolCall.toolCallId,
9597
- state: currentToolCall,
9598
- fullInput
9599
- });
9600
- };
9601
- const processToolCallEnd = (controller, toolContent, toolName, toolCallId) => {
9602
- var _a;
9603
- const result = parseYamlContent(toolContent);
9604
- flushText(controller);
9605
- if (!result.ok) {
9606
- const original = `<${toolName}>${toolContent}</${toolName}>`;
9607
- const emitRawFallback = shouldEmitRawToolCallTextOnError(options);
9608
- emitFailedToolInputLifecycle({
9608
+ if (currentToolCall && currentToolCall.toolCallId === toolCallId) {
9609
+ emitFinalizedToolInputLifecycle({
9609
9610
  controller,
9610
9611
  id: toolCallId,
9611
- emitRawToolCallTextOnError: emitRawFallback,
9612
- rawToolCallText: original,
9613
- emitRawText: (rawText) => {
9614
- flushText(controller, rawText);
9615
- }
9616
- });
9617
- (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, "Could not parse streaming YAML tool call", {
9618
- toolCall: original,
9619
- toolName,
9620
- toolCallId,
9621
- dropReason: "malformed-tool-call-body",
9622
- cause: yamlFailureCause(result.failure)
9623
- });
9624
- } else {
9625
- const finalInput = stringifyToolInputWithSchema({
9612
+ state: currentToolCall,
9626
9613
  toolName,
9627
- args: result.value,
9628
- tools
9629
- });
9630
- if (currentToolCall && currentToolCall.toolCallId === toolCallId) {
9631
- emitFinalizedToolInputLifecycle({
9632
- controller,
9633
- id: toolCallId,
9634
- state: currentToolCall,
9635
- toolName,
9636
- finalInput,
9637
- onMismatch: options == null ? void 0 : options.onError
9638
- });
9639
- } else {
9640
- enqueueToolInputEndAndCall({
9641
- controller,
9642
- id: toolCallId,
9643
- toolName,
9644
- input: finalInput
9645
- });
9646
- }
9647
- }
9648
- };
9649
- const finalizeUnclosedToolCall = (controller) => {
9650
- var _a;
9651
- if (!currentToolCall) {
9652
- return;
9653
- }
9654
- emitToolInputProgress2(controller, buffer);
9655
- const { name: toolName, toolCallId } = currentToolCall;
9656
- const reconciledBuffer = stripTrailingPartialCloseTag(buffer, toolName);
9657
- const result = parseYamlContent(reconciledBuffer);
9658
- flushText(controller);
9659
- if (!result.ok) {
9660
- const unfinishedContent = `<${toolName}>${buffer}`;
9661
- const emitRawFallback = shouldEmitRawToolCallTextOnError(options);
9662
- emitFailedToolInputLifecycle({
9663
- controller,
9664
- id: toolCallId,
9665
- emitRawToolCallTextOnError: emitRawFallback,
9666
- rawToolCallText: unfinishedContent,
9667
- emitRawText: (rawText) => {
9668
- flushText(controller, rawText);
9669
- }
9614
+ finalInput,
9615
+ onMismatch: options == null ? void 0 : options.onError
9670
9616
  });
9671
- (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
9672
- options,
9673
- "Could not complete streaming YAML tool call at finish.",
9674
- {
9675
- toolCall: unfinishedContent,
9676
- toolCallId,
9677
- toolName,
9678
- dropReason: "unfinished-tool-call",
9679
- cause: yamlFailureCause(result.failure)
9680
- }
9681
- );
9682
9617
  } else {
9683
- const finalInput = stringifyToolInputWithSchema({
9684
- toolName,
9685
- args: result.value,
9686
- tools
9687
- });
9688
- emitFinalizedToolInputLifecycle({
9618
+ enqueueToolInputEndAndCall({
9689
9619
  controller,
9690
9620
  id: toolCallId,
9691
- state: currentToolCall,
9692
9621
  toolName,
9693
- finalInput,
9694
- onMismatch: options == null ? void 0 : options.onError
9622
+ input: finalInput
9695
9623
  });
9696
9624
  }
9697
- buffer = "";
9698
- currentToolCall = null;
9699
- };
9700
- const handlePendingToolCall = (controller, endTag, toolName) => {
9701
- var _a;
9702
- const endIdx = buffer.indexOf(endTag);
9703
- if (endIdx === -1) {
9704
- emitToolInputProgress2(controller, buffer);
9705
- return false;
9706
- }
9707
- const content = buffer.slice(0, endIdx);
9708
- emitToolInputProgress2(controller, content);
9709
- buffer = buffer.slice(endIdx + endTag.length);
9710
- processToolCallEnd(
9625
+ } else {
9626
+ const original = `<${toolName}>${toolContent}</${toolName}>`;
9627
+ const emitRawFallback = shouldEmitRawToolCallTextOnError(options);
9628
+ emitFailedToolInputLifecycle({
9711
9629
  controller,
9712
- content,
9630
+ id: toolCallId,
9631
+ emitRawToolCallTextOnError: emitRawFallback,
9632
+ rawToolCallText: original,
9633
+ emitRawText: (rawText) => {
9634
+ flushText(controller, rawText);
9635
+ }
9636
+ });
9637
+ (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, "Could not parse streaming YAML tool call", {
9638
+ toolCall: original,
9713
9639
  toolName,
9714
- (_a = currentToolCall == null ? void 0 : currentToolCall.toolCallId) != null ? _a : generateToolCallId()
9640
+ toolCallId,
9641
+ dropReason: "malformed-tool-call-body",
9642
+ cause: yamlFailureCause(result.failure)
9643
+ });
9644
+ }
9645
+ };
9646
+ const finalizeUnclosedToolCall = (controller) => {
9647
+ var _a;
9648
+ if (!currentToolCall) {
9649
+ return;
9650
+ }
9651
+ emitToolInputProgress2(controller, buffer);
9652
+ const { name: toolName, toolCallId } = currentToolCall;
9653
+ const reconciledBuffer = stripTrailingPartialCloseTag(buffer, toolName);
9654
+ const result = parseYamlContent(reconciledBuffer);
9655
+ flushText(controller);
9656
+ if (result.ok) {
9657
+ const finalInput = stringifyToolInputWithSchema({
9658
+ toolName,
9659
+ args: result.value,
9660
+ tools
9661
+ });
9662
+ emitFinalizedToolInputLifecycle({
9663
+ controller,
9664
+ id: toolCallId,
9665
+ state: currentToolCall,
9666
+ toolName,
9667
+ finalInput,
9668
+ onMismatch: options == null ? void 0 : options.onError
9669
+ });
9670
+ } else {
9671
+ const unfinishedContent = `<${toolName}>${buffer}`;
9672
+ const emitRawFallback = shouldEmitRawToolCallTextOnError(options);
9673
+ emitFailedToolInputLifecycle({
9674
+ controller,
9675
+ id: toolCallId,
9676
+ emitRawToolCallTextOnError: emitRawFallback,
9677
+ rawToolCallText: unfinishedContent,
9678
+ emitRawText: (rawText) => {
9679
+ flushText(controller, rawText);
9680
+ }
9681
+ });
9682
+ (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
9683
+ options,
9684
+ "Could not complete streaming YAML tool call at finish.",
9685
+ {
9686
+ toolCall: unfinishedContent,
9687
+ toolCallId,
9688
+ toolName,
9689
+ dropReason: "unfinished-tool-call",
9690
+ cause: yamlFailureCause(result.failure)
9691
+ }
9715
9692
  );
9693
+ }
9694
+ buffer = "";
9695
+ currentToolCall = null;
9696
+ };
9697
+ const handlePendingToolCall = (controller, endTag, toolName) => {
9698
+ var _a;
9699
+ const endIdx = buffer.indexOf(endTag);
9700
+ if (endIdx === -1) {
9701
+ emitToolInputProgress2(controller, buffer);
9702
+ return false;
9703
+ }
9704
+ const content = buffer.slice(0, endIdx);
9705
+ emitToolInputProgress2(controller, content);
9706
+ buffer = buffer.slice(endIdx + endTag.length);
9707
+ processToolCallEnd(
9708
+ controller,
9709
+ content,
9710
+ toolName,
9711
+ (_a = currentToolCall == null ? void 0 : currentToolCall.toolCallId) != null ? _a : generateToolCallId()
9712
+ );
9713
+ currentToolCall = null;
9714
+ return true;
9715
+ };
9716
+ const flushSafeText = (controller) => {
9717
+ const maxTagLen = toolNames.length ? Math.max(...toolNames.map((n) => `<${n} />`.length)) : 0;
9718
+ const tail = Math.max(0, maxTagLen - 1);
9719
+ const safeLen = Math.max(0, buffer.length - tail);
9720
+ if (safeLen > 0) {
9721
+ flushText(controller, buffer.slice(0, safeLen));
9722
+ buffer = buffer.slice(safeLen);
9723
+ }
9724
+ };
9725
+ const handleNewToolTag = (controller, tagIndex, tagName, selfClosing, tagLength) => {
9726
+ if (tagIndex > 0) {
9727
+ flushText(controller, buffer.slice(0, tagIndex));
9728
+ }
9729
+ flushText(controller);
9730
+ if (selfClosing) {
9731
+ buffer = buffer.slice(tagIndex + tagLength);
9732
+ const toolCallId = generateToolCallId();
9733
+ currentToolCall = {
9734
+ name: tagName,
9735
+ toolCallId,
9736
+ emittedInput: ""
9737
+ };
9738
+ controller.enqueue({
9739
+ type: "tool-input-start",
9740
+ id: toolCallId,
9741
+ toolName: tagName
9742
+ });
9743
+ processToolCallEnd(controller, "", tagName, toolCallId);
9716
9744
  currentToolCall = null;
9717
- return true;
9718
- };
9719
- const flushSafeText = (controller) => {
9720
- const maxTagLen = toolNames.length ? Math.max(...toolNames.map((n) => `<${n} />`.length)) : 0;
9721
- const tail = Math.max(0, maxTagLen - 1);
9722
- const safeLen = Math.max(0, buffer.length - tail);
9723
- if (safeLen > 0) {
9724
- flushText(controller, buffer.slice(0, safeLen));
9725
- buffer = buffer.slice(safeLen);
9726
- }
9727
- };
9728
- const handleNewToolTag = (controller, tagIndex, tagName, selfClosing, tagLength) => {
9729
- if (tagIndex > 0) {
9730
- flushText(controller, buffer.slice(0, tagIndex));
9731
- }
9732
- flushText(controller);
9733
- if (selfClosing) {
9734
- buffer = buffer.slice(tagIndex + tagLength);
9735
- const toolCallId = generateToolCallId();
9736
- currentToolCall = {
9737
- name: tagName,
9738
- toolCallId,
9739
- emittedInput: ""
9740
- };
9741
- controller.enqueue({
9742
- type: "tool-input-start",
9743
- id: toolCallId,
9744
- toolName: tagName
9745
- });
9746
- processToolCallEnd(controller, "", tagName, toolCallId);
9747
- currentToolCall = null;
9745
+ } else {
9746
+ const startTag = `<${tagName}>`;
9747
+ buffer = buffer.slice(tagIndex + startTag.length);
9748
+ currentToolCall = {
9749
+ name: tagName,
9750
+ toolCallId: generateToolCallId(),
9751
+ emittedInput: ""
9752
+ };
9753
+ controller.enqueue({
9754
+ type: "tool-input-start",
9755
+ id: currentToolCall.toolCallId,
9756
+ toolName: tagName
9757
+ });
9758
+ }
9759
+ };
9760
+ const processBuffer = (controller) => {
9761
+ while (true) {
9762
+ if (currentToolCall) {
9763
+ const toolName = currentToolCall.name;
9764
+ const endTag = `</${toolName}>`;
9765
+ if (!handlePendingToolCall(controller, endTag, toolName)) {
9766
+ break;
9767
+ }
9748
9768
  } else {
9749
- const startTag = `<${tagName}>`;
9750
- buffer = buffer.slice(tagIndex + startTag.length);
9751
- currentToolCall = {
9752
- name: tagName,
9753
- toolCallId: generateToolCallId(),
9754
- emittedInput: ""
9755
- };
9756
- controller.enqueue({
9757
- type: "tool-input-start",
9758
- id: currentToolCall.toolCallId,
9759
- toolName: tagName
9760
- });
9761
- }
9762
- };
9763
- const processBuffer = (controller) => {
9764
- while (true) {
9765
- if (currentToolCall) {
9766
- const toolName = currentToolCall.name;
9767
- const endTag = `</${toolName}>`;
9768
- if (!handlePendingToolCall(controller, endTag, toolName)) {
9769
- break;
9770
- }
9771
- } else {
9772
- const { index, name, selfClosing, tagLength } = findEarliestToolTag(
9773
- buffer,
9774
- toolNames
9775
- );
9776
- if (index === -1) {
9777
- flushSafeText(controller);
9778
- break;
9779
- }
9780
- handleNewToolTag(controller, index, name, selfClosing, tagLength);
9769
+ const { index, name, selfClosing, tagLength } = findEarliestToolTag(
9770
+ buffer,
9771
+ toolNames
9772
+ );
9773
+ if (index === -1) {
9774
+ flushSafeText(controller);
9775
+ break;
9781
9776
  }
9777
+ handleNewToolTag(controller, index, name, selfClosing, tagLength);
9782
9778
  }
9783
- };
9784
- return new TransformStream({
9785
- transform(chunk, controller) {
9786
- var _a;
9787
- if (chunk.type === "finish") {
9788
- if (currentToolCall) {
9789
- finalizeUnclosedToolCall(controller);
9790
- } else if (buffer) {
9791
- flushText(controller, buffer);
9792
- buffer = "";
9793
- }
9794
- flushText(controller);
9795
- controller.enqueue(chunk);
9796
- return;
9797
- }
9798
- if (chunk.type !== "text-delta") {
9799
- if (!currentToolCall && buffer) {
9800
- flushText(controller, buffer);
9801
- buffer = "";
9802
- }
9803
- controller.enqueue(chunk);
9804
- return;
9805
- }
9806
- const textContent = (_a = chunk.delta) != null ? _a : "";
9807
- buffer += textContent;
9808
- processBuffer(controller);
9809
- },
9810
- flush(controller) {
9779
+ }
9780
+ };
9781
+ return new TransformStream({
9782
+ transform(chunk, controller) {
9783
+ var _a;
9784
+ if (chunk.type === "finish") {
9811
9785
  if (currentToolCall) {
9812
9786
  finalizeUnclosedToolCall(controller);
9813
9787
  } else if (buffer) {
9814
9788
  flushText(controller, buffer);
9815
9789
  buffer = "";
9816
9790
  }
9817
- if (currentTextId && hasEmittedTextStart) {
9818
- controller.enqueue({
9819
- type: "text-end",
9820
- id: currentTextId
9821
- });
9822
- hasEmittedTextStart = false;
9823
- currentTextId = null;
9791
+ flushText(controller);
9792
+ controller.enqueue(chunk);
9793
+ return;
9794
+ }
9795
+ if (chunk.type !== "text-delta") {
9796
+ if (!currentToolCall && buffer) {
9797
+ flushText(controller, buffer);
9798
+ buffer = "";
9824
9799
  }
9800
+ controller.enqueue(chunk);
9801
+ return;
9802
+ }
9803
+ const textContent = (_a = chunk.delta) != null ? _a : "";
9804
+ buffer += textContent;
9805
+ processBuffer(controller);
9806
+ },
9807
+ flush(controller) {
9808
+ if (currentToolCall) {
9809
+ finalizeUnclosedToolCall(controller);
9810
+ } else if (buffer) {
9811
+ flushText(controller, buffer);
9812
+ buffer = "";
9813
+ }
9814
+ if (currentTextId && hasEmittedTextStart) {
9815
+ controller.enqueue({
9816
+ type: "text-end",
9817
+ id: currentTextId
9818
+ });
9819
+ hasEmittedTextStart = false;
9820
+ currentTextId = null;
9825
9821
  }
9826
- });
9827
- },
9828
- extractToolCallSegments({ text, tools }) {
9829
- const toolNames = tools.map((t) => t.name).filter(Boolean);
9830
- if (toolNames.length === 0) {
9831
- return [];
9832
9822
  }
9833
- return findToolCalls2(text, toolNames).map(
9834
- (tc) => `<${tc.toolName}>${tc.content}</${tc.toolName}>`
9835
- );
9823
+ });
9824
+ },
9825
+ extractToolCallSegments({ text, tools }) {
9826
+ const toolNames = tools.map((t) => t.name).filter(Boolean);
9827
+ if (toolNames.length === 0) {
9828
+ return [];
9836
9829
  }
9837
- };
9838
- };
9830
+ return findToolCalls2(text, toolNames).map(
9831
+ (tc) => `<${tc.toolName}>${tc.content}</${tc.toolName}>`
9832
+ );
9833
+ }
9834
+ });
9839
9835
 
9840
9836
  // src/core/utils/dynamic-tool-schema.ts
9841
9837
  function createDynamicIfThenElseSchema(tools) {
@@ -10016,7 +10012,7 @@ function parseJsonCandidate(candidateText) {
10016
10012
  try {
10017
10013
  return parse3(candidateText);
10018
10014
  } catch (e) {
10019
- return void 0;
10015
+ return;
10020
10016
  }
10021
10017
  }
10022
10018
  function extractCodeBlockCandidates(text) {