ai 3.1.8 → 3.1.10

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
@@ -94,6 +94,53 @@ var EmbedResult = class {
94
94
  }
95
95
  };
96
96
 
97
+ // core/util/split-array.ts
98
+ function splitArray(array, chunkSize) {
99
+ if (chunkSize <= 0) {
100
+ throw new Error("chunkSize must be greater than 0");
101
+ }
102
+ const result = [];
103
+ for (let i = 0; i < array.length; i += chunkSize) {
104
+ result.push(array.slice(i, i + chunkSize));
105
+ }
106
+ return result;
107
+ }
108
+
109
+ // core/embed/embed-many.ts
110
+ async function embedMany({
111
+ model,
112
+ values,
113
+ maxRetries,
114
+ abortSignal
115
+ }) {
116
+ const retry = retryWithExponentialBackoff({ maxRetries });
117
+ const maxEmbeddingsPerCall = model.maxEmbeddingsPerCall;
118
+ if (maxEmbeddingsPerCall == null) {
119
+ const modelResponse = await retry(
120
+ () => model.doEmbed({ values, abortSignal })
121
+ );
122
+ return new EmbedManyResult({
123
+ values,
124
+ embeddings: modelResponse.embeddings
125
+ });
126
+ }
127
+ const valueChunks = splitArray(values, maxEmbeddingsPerCall);
128
+ const embeddings = [];
129
+ for (const chunk of valueChunks) {
130
+ const modelResponse = await retry(
131
+ () => model.doEmbed({ values: chunk, abortSignal })
132
+ );
133
+ embeddings.push(...modelResponse.embeddings);
134
+ }
135
+ return new EmbedManyResult({ values, embeddings });
136
+ }
137
+ var EmbedManyResult = class {
138
+ constructor(options) {
139
+ this.values = options.values;
140
+ this.embeddings = options.embeddings;
141
+ }
142
+ };
143
+
97
144
  // core/generate-object/generate-object.ts
98
145
  import { NoObjectGeneratedError } from "@ai-sdk/provider";
99
146
  import { safeParseJSON } from "@ai-sdk/provider-utils";
@@ -1593,7 +1640,7 @@ var StreamTextResult = class {
1593
1640
  "Content-Type": "text/plain; charset=utf-8",
1594
1641
  ...init == null ? void 0 : init.headers
1595
1642
  });
1596
- const reader = this.textStream.pipeThrough(createCallbacksTransformer(void 0)).pipeThrough(createStreamDataTransformer()).getReader();
1643
+ const reader = this.toAIStream().getReader();
1597
1644
  const read = async () => {
1598
1645
  try {
1599
1646
  while (true) {
@@ -1624,15 +1671,14 @@ var StreamTextResult = class {
1624
1671
  "Content-Type": "text/plain; charset=utf-8",
1625
1672
  ...init == null ? void 0 : init.headers
1626
1673
  });
1627
- const reader = this.textStream.getReader();
1674
+ const reader = this.textStream.pipeThrough(new TextEncoderStream()).getReader();
1628
1675
  const read = async () => {
1629
- const encoder = new TextEncoder();
1630
1676
  try {
1631
1677
  while (true) {
1632
1678
  const { done, value } = await reader.read();
1633
1679
  if (done)
1634
1680
  break;
1635
- response.write(encoder.encode(value));
1681
+ response.write(value);
1636
1682
  }
1637
1683
  } catch (error) {
1638
1684
  throw error;
@@ -1662,23 +1708,13 @@ var StreamTextResult = class {
1662
1708
  */
1663
1709
  toTextStreamResponse(init) {
1664
1710
  var _a;
1665
- const encoder = new TextEncoder();
1666
- return new Response(
1667
- this.textStream.pipeThrough(
1668
- new TransformStream({
1669
- transform(chunk, controller) {
1670
- controller.enqueue(encoder.encode(chunk));
1671
- }
1672
- })
1673
- ),
1674
- {
1675
- status: (_a = init == null ? void 0 : init.status) != null ? _a : 200,
1676
- headers: {
1677
- "Content-Type": "text/plain; charset=utf-8",
1678
- ...init == null ? void 0 : init.headers
1679
- }
1711
+ return new Response(this.textStream.pipeThrough(new TextEncoderStream()), {
1712
+ status: (_a = init == null ? void 0 : init.status) != null ? _a : 200,
1713
+ headers: {
1714
+ "Content-Type": "text/plain; charset=utf-8",
1715
+ ...init == null ? void 0 : init.headers
1680
1716
  }
1681
- );
1717
+ });
1682
1718
  }
1683
1719
  };
1684
1720
  var experimental_streamText = streamText;
@@ -3248,6 +3284,7 @@ export {
3248
3284
  AnthropicStream,
3249
3285
  AssistantResponse,
3250
3286
  CohereStream,
3287
+ EmbedManyResult,
3251
3288
  EmbedResult,
3252
3289
  EmptyResponseBodyError,
3253
3290
  GenerateObjectResult,
@@ -3286,6 +3323,7 @@ export {
3286
3323
  createEventStreamTransformer,
3287
3324
  createStreamDataTransformer,
3288
3325
  embed,
3326
+ embedMany,
3289
3327
  experimental_AssistantResponse,
3290
3328
  experimental_StreamData,
3291
3329
  experimental_StreamingReactResponse,