ai 4.1.6 → 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
@@ -219,12 +219,7 @@ _a = symbol;
219
219
 
220
220
  // util/retry-with-exponential-backoff.ts
221
221
  import { APICallError } from "@ai-sdk/provider";
222
- import { getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
223
-
224
- // util/delay.ts
225
- async function delay(delayInMs) {
226
- return delayInMs == null ? Promise.resolve() : new Promise((resolve) => setTimeout(resolve, delayInMs));
227
- }
222
+ import { delay, getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
228
223
 
229
224
  // util/retry-error.ts
230
225
  import { AISDKError as AISDKError2 } from "@ai-sdk/provider";
@@ -4158,6 +4153,7 @@ var object = ({
4158
4153
 
4159
4154
  // core/generate-text/smooth-stream.ts
4160
4155
  import { InvalidArgumentError as InvalidArgumentError2 } from "@ai-sdk/provider";
4156
+ import { delay as originalDelay } from "@ai-sdk/provider-utils";
4161
4157
  var CHUNKING_REGEXPS = {
4162
4158
  word: /\s*\S+\s+/m,
4163
4159
  line: /[^\n]*\n/m
@@ -4165,7 +4161,7 @@ var CHUNKING_REGEXPS = {
4165
4161
  function smoothStream({
4166
4162
  delayInMs = 10,
4167
4163
  chunking = "word",
4168
- _internal: { delay: delay2 = delay } = {}
4164
+ _internal: { delay: delay2 = originalDelay } = {}
4169
4165
  } = {}) {
4170
4166
  const chunkingRegexp = typeof chunking === "string" ? CHUNKING_REGEXPS[chunking] : chunking;
4171
4167
  if (chunkingRegexp == null) {
@@ -5588,6 +5584,113 @@ var experimental_wrapLanguageModel = ({
5588
5584
  };
5589
5585
  };
5590
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
+
5591
5694
  // core/prompt/append-client-message.ts
5592
5695
  function appendClientMessage({
5593
5696
  messages,
@@ -5818,6 +5921,7 @@ function magnitude(vector) {
5818
5921
  }
5819
5922
 
5820
5923
  // core/util/simulate-readable-stream.ts
5924
+ import { delay as delayFunction } from "@ai-sdk/provider-utils";
5821
5925
  function simulateReadableStream({
5822
5926
  chunks,
5823
5927
  initialDelayInMs = 0,
@@ -5825,7 +5929,7 @@ function simulateReadableStream({
5825
5929
  _internal
5826
5930
  }) {
5827
5931
  var _a15;
5828
- const delay2 = (_a15 = _internal == null ? void 0 : _internal.delay) != null ? _a15 : delay;
5932
+ const delay2 = (_a15 = _internal == null ? void 0 : _internal.delay) != null ? _a15 : delayFunction;
5829
5933
  let index = 0;
5830
5934
  return new ReadableStream({
5831
5935
  async pull(controller) {
@@ -6222,6 +6326,7 @@ export {
6222
6326
  experimental_customProvider,
6223
6327
  generateImage as experimental_generateImage,
6224
6328
  experimental_wrapLanguageModel,
6329
+ extractReasoningMiddleware,
6225
6330
  formatAssistantStreamPart,
6226
6331
  formatDataStreamPart3 as formatDataStreamPart,
6227
6332
  generateId2 as generateId,