ai 4.1.7 → 4.1.8

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.
package/dist/index.mjs CHANGED
@@ -5584,6 +5584,113 @@ var experimental_wrapLanguageModel = ({
5584
5584
  };
5585
5585
  };
5586
5586
 
5587
+ // core/util/get-potential-start-index.ts
5588
+ function getPotentialStartIndex(text2, searchedText) {
5589
+ if (searchedText.length === 0) {
5590
+ return null;
5591
+ }
5592
+ const directIndex = text2.indexOf(searchedText);
5593
+ if (directIndex !== -1) {
5594
+ return directIndex;
5595
+ }
5596
+ for (let i = text2.length - 1; i >= 0; i--) {
5597
+ const suffix = text2.substring(i);
5598
+ if (searchedText.startsWith(suffix)) {
5599
+ return i;
5600
+ }
5601
+ }
5602
+ return null;
5603
+ }
5604
+
5605
+ // core/middleware/extract-reasoning-middleware.ts
5606
+ function extractReasoningMiddleware({
5607
+ tagName,
5608
+ separator = "\n"
5609
+ }) {
5610
+ const openingTag = `<${tagName}>`;
5611
+ const closingTag = `</${tagName}>`;
5612
+ return {
5613
+ wrapGenerate: async ({ doGenerate }) => {
5614
+ const { text: text2, ...rest } = await doGenerate();
5615
+ if (text2 == null) {
5616
+ return { text: text2, ...rest };
5617
+ }
5618
+ const regexp = new RegExp(`${openingTag}(.*?)${closingTag}`, "gs");
5619
+ const matches = Array.from(text2.matchAll(regexp));
5620
+ if (!matches.length) {
5621
+ return { text: text2, ...rest };
5622
+ }
5623
+ const reasoning = matches.map((match) => match[1]).join(separator);
5624
+ let textWithoutReasoning = text2;
5625
+ for (let i = matches.length - 1; i >= 0; i--) {
5626
+ const match = matches[i];
5627
+ const beforeMatch = textWithoutReasoning.slice(0, match.index);
5628
+ const afterMatch = textWithoutReasoning.slice(
5629
+ match.index + match[0].length
5630
+ );
5631
+ textWithoutReasoning = beforeMatch + (beforeMatch.length > 0 && afterMatch.length > 0 ? separator : "") + afterMatch;
5632
+ }
5633
+ return { text: textWithoutReasoning, reasoning, ...rest };
5634
+ },
5635
+ wrapStream: async ({ doStream }) => {
5636
+ const { stream, ...rest } = await doStream();
5637
+ let isFirstReasoning = true;
5638
+ let isFirstText = true;
5639
+ let afterSwitch = false;
5640
+ let isReasoning = false;
5641
+ let buffer = "";
5642
+ return {
5643
+ stream: stream.pipeThrough(
5644
+ new TransformStream({
5645
+ transform: (chunk, controller) => {
5646
+ if (chunk.type !== "text-delta") {
5647
+ controller.enqueue(chunk);
5648
+ return;
5649
+ }
5650
+ buffer += chunk.textDelta;
5651
+ function publish(text2) {
5652
+ if (text2.length > 0) {
5653
+ const prefix = afterSwitch && (isReasoning ? !isFirstReasoning : !isFirstText) ? separator : "";
5654
+ controller.enqueue({
5655
+ type: isReasoning ? "reasoning" : "text-delta",
5656
+ textDelta: prefix + text2
5657
+ });
5658
+ afterSwitch = false;
5659
+ if (isReasoning) {
5660
+ isFirstReasoning = false;
5661
+ } else {
5662
+ isFirstText = false;
5663
+ }
5664
+ }
5665
+ }
5666
+ do {
5667
+ const nextTag = isReasoning ? closingTag : openingTag;
5668
+ const startIndex = getPotentialStartIndex(buffer, nextTag);
5669
+ if (startIndex == null) {
5670
+ publish(buffer);
5671
+ buffer = "";
5672
+ break;
5673
+ }
5674
+ publish(buffer.slice(0, startIndex));
5675
+ const foundFullMatch = startIndex + nextTag.length <= buffer.length;
5676
+ if (foundFullMatch) {
5677
+ buffer = buffer.slice(startIndex + nextTag.length);
5678
+ isReasoning = !isReasoning;
5679
+ afterSwitch = true;
5680
+ } else {
5681
+ buffer = buffer.slice(startIndex);
5682
+ break;
5683
+ }
5684
+ } while (true);
5685
+ }
5686
+ })
5687
+ ),
5688
+ ...rest
5689
+ };
5690
+ }
5691
+ };
5692
+ }
5693
+
5587
5694
  // core/prompt/append-client-message.ts
5588
5695
  function appendClientMessage({
5589
5696
  messages,
@@ -6219,6 +6326,7 @@ export {
6219
6326
  experimental_customProvider,
6220
6327
  generateImage as experimental_generateImage,
6221
6328
  experimental_wrapLanguageModel,
6329
+ extractReasoningMiddleware,
6222
6330
  formatAssistantStreamPart,
6223
6331
  formatDataStreamPart3 as formatDataStreamPart,
6224
6332
  generateId2 as generateId,