ai 3.1.9 → 3.1.11
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.d.mts +57 -6
- package/dist/index.d.ts +57 -6
- package/dist/index.js +178 -75
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +176 -75
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -9
- package/react/dist/index.d.mts +2 -5
- package/react/dist/index.d.ts +2 -5
- package/rsc/dist/rsc-server.mjs.map +1 -1
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.
|
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(
|
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
|
-
|
1666
|
-
|
1667
|
-
|
1668
|
-
|
1669
|
-
|
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;
|
@@ -2162,86 +2198,61 @@ var StreamData = class {
|
|
2162
2198
|
constructor() {
|
2163
2199
|
this.encoder = new TextEncoder();
|
2164
2200
|
this.controller = null;
|
2165
|
-
// closing the stream is synchronous, but we want to return a promise
|
2166
|
-
// in case we're doing async work
|
2167
|
-
this.isClosedPromise = null;
|
2168
|
-
this.isClosedPromiseResolver = void 0;
|
2169
2201
|
this.isClosed = false;
|
2170
|
-
|
2171
|
-
this.data = [];
|
2172
|
-
this.messageAnnotations = [];
|
2173
|
-
this.isClosedPromise = new Promise((resolve) => {
|
2174
|
-
this.isClosedPromiseResolver = resolve;
|
2175
|
-
});
|
2202
|
+
this.warningTimeout = null;
|
2176
2203
|
const self = this;
|
2177
|
-
this.stream = new
|
2204
|
+
this.stream = new ReadableStream({
|
2178
2205
|
start: async (controller) => {
|
2179
2206
|
self.controller = controller;
|
2180
|
-
|
2181
|
-
|
2182
|
-
|
2183
|
-
|
2184
|
-
|
2185
|
-
);
|
2186
|
-
self.data = [];
|
2187
|
-
controller.enqueue(encodedData);
|
2188
|
-
}
|
2189
|
-
if (self.messageAnnotations.length) {
|
2190
|
-
const encodedMessageAnnotations = self.encoder.encode(
|
2191
|
-
formatStreamPart("message_annotations", self.messageAnnotations)
|
2192
|
-
);
|
2193
|
-
self.messageAnnotations = [];
|
2194
|
-
controller.enqueue(encodedMessageAnnotations);
|
2207
|
+
if (process.env.NODE_ENV === "development") {
|
2208
|
+
self.warningTimeout = setTimeout(() => {
|
2209
|
+
console.warn(
|
2210
|
+
"The data stream is hanging. Did you forget to close it with `data.close()`?"
|
2211
|
+
);
|
2212
|
+
}, 3e3);
|
2195
2213
|
}
|
2196
|
-
controller.enqueue(chunk);
|
2197
2214
|
},
|
2198
|
-
|
2199
|
-
|
2200
|
-
|
2201
|
-
|
2202
|
-
);
|
2203
|
-
}, 3e3) : null;
|
2204
|
-
await self.isClosedPromise;
|
2205
|
-
if (warningTimeout !== null) {
|
2206
|
-
clearTimeout(warningTimeout);
|
2207
|
-
}
|
2208
|
-
if (self.data.length) {
|
2209
|
-
const encodedData = self.encoder.encode(
|
2210
|
-
formatStreamPart("data", self.data)
|
2211
|
-
);
|
2212
|
-
controller.enqueue(encodedData);
|
2213
|
-
}
|
2214
|
-
if (self.messageAnnotations.length) {
|
2215
|
-
const encodedData = self.encoder.encode(
|
2216
|
-
formatStreamPart("message_annotations", self.messageAnnotations)
|
2217
|
-
);
|
2218
|
-
controller.enqueue(encodedData);
|
2219
|
-
}
|
2215
|
+
pull: (controller) => {
|
2216
|
+
},
|
2217
|
+
cancel: (reason) => {
|
2218
|
+
this.isClosed = true;
|
2220
2219
|
}
|
2221
2220
|
});
|
2222
2221
|
}
|
2223
2222
|
async close() {
|
2224
|
-
var _a;
|
2225
2223
|
if (this.isClosed) {
|
2226
2224
|
throw new Error("Data Stream has already been closed.");
|
2227
2225
|
}
|
2228
2226
|
if (!this.controller) {
|
2229
2227
|
throw new Error("Stream controller is not initialized.");
|
2230
2228
|
}
|
2231
|
-
|
2229
|
+
this.controller.close();
|
2232
2230
|
this.isClosed = true;
|
2231
|
+
if (this.warningTimeout) {
|
2232
|
+
clearTimeout(this.warningTimeout);
|
2233
|
+
}
|
2233
2234
|
}
|
2234
2235
|
append(value) {
|
2235
2236
|
if (this.isClosed) {
|
2236
2237
|
throw new Error("Data Stream has already been closed.");
|
2237
2238
|
}
|
2238
|
-
this.
|
2239
|
+
if (!this.controller) {
|
2240
|
+
throw new Error("Stream controller is not initialized.");
|
2241
|
+
}
|
2242
|
+
this.controller.enqueue(
|
2243
|
+
this.encoder.encode(formatStreamPart("data", [value]))
|
2244
|
+
);
|
2239
2245
|
}
|
2240
2246
|
appendMessageAnnotation(value) {
|
2241
2247
|
if (this.isClosed) {
|
2242
2248
|
throw new Error("Data Stream has already been closed.");
|
2243
2249
|
}
|
2244
|
-
this.
|
2250
|
+
if (!this.controller) {
|
2251
|
+
throw new Error("Stream controller is not initialized.");
|
2252
|
+
}
|
2253
|
+
this.controller.enqueue(
|
2254
|
+
this.encoder.encode(formatStreamPart("message_annotations", [value]))
|
2255
|
+
);
|
2245
2256
|
}
|
2246
2257
|
};
|
2247
2258
|
function createStreamDataTransformer() {
|
@@ -3015,6 +3026,94 @@ async function ReplicateStream(res, cb, options) {
|
|
3015
3026
|
);
|
3016
3027
|
}
|
3017
3028
|
|
3029
|
+
// core/util/merge-streams.ts
|
3030
|
+
function mergeStreams(stream1, stream2) {
|
3031
|
+
const reader1 = stream1.getReader();
|
3032
|
+
const reader2 = stream2.getReader();
|
3033
|
+
let lastRead1 = void 0;
|
3034
|
+
let lastRead2 = void 0;
|
3035
|
+
let stream1Done = false;
|
3036
|
+
let stream2Done = false;
|
3037
|
+
async function readStream1(controller) {
|
3038
|
+
try {
|
3039
|
+
if (lastRead1 == null) {
|
3040
|
+
lastRead1 = reader1.read();
|
3041
|
+
}
|
3042
|
+
const result = await lastRead1;
|
3043
|
+
lastRead1 = void 0;
|
3044
|
+
if (!result.done) {
|
3045
|
+
controller.enqueue(result.value);
|
3046
|
+
} else {
|
3047
|
+
controller.close();
|
3048
|
+
}
|
3049
|
+
} catch (error) {
|
3050
|
+
controller.error(error);
|
3051
|
+
}
|
3052
|
+
}
|
3053
|
+
async function readStream2(controller) {
|
3054
|
+
try {
|
3055
|
+
if (lastRead2 == null) {
|
3056
|
+
lastRead2 = reader2.read();
|
3057
|
+
}
|
3058
|
+
const result = await lastRead2;
|
3059
|
+
lastRead2 = void 0;
|
3060
|
+
if (!result.done) {
|
3061
|
+
controller.enqueue(result.value);
|
3062
|
+
} else {
|
3063
|
+
controller.close();
|
3064
|
+
}
|
3065
|
+
} catch (error) {
|
3066
|
+
controller.error(error);
|
3067
|
+
}
|
3068
|
+
}
|
3069
|
+
return new ReadableStream({
|
3070
|
+
async pull(controller) {
|
3071
|
+
try {
|
3072
|
+
if (stream1Done) {
|
3073
|
+
readStream2(controller);
|
3074
|
+
return;
|
3075
|
+
}
|
3076
|
+
if (stream2Done) {
|
3077
|
+
readStream1(controller);
|
3078
|
+
return;
|
3079
|
+
}
|
3080
|
+
if (lastRead1 == null) {
|
3081
|
+
lastRead1 = reader1.read();
|
3082
|
+
}
|
3083
|
+
if (lastRead2 == null) {
|
3084
|
+
lastRead2 = reader2.read();
|
3085
|
+
}
|
3086
|
+
const { result, reader } = await Promise.race([
|
3087
|
+
lastRead1.then((result2) => ({ result: result2, reader: reader1 })),
|
3088
|
+
lastRead2.then((result2) => ({ result: result2, reader: reader2 }))
|
3089
|
+
]);
|
3090
|
+
if (!result.done) {
|
3091
|
+
controller.enqueue(result.value);
|
3092
|
+
}
|
3093
|
+
if (reader === reader1) {
|
3094
|
+
lastRead1 = void 0;
|
3095
|
+
if (result.done) {
|
3096
|
+
readStream2(controller);
|
3097
|
+
stream1Done = true;
|
3098
|
+
}
|
3099
|
+
} else {
|
3100
|
+
lastRead2 = void 0;
|
3101
|
+
if (result.done) {
|
3102
|
+
stream2Done = true;
|
3103
|
+
readStream1(controller);
|
3104
|
+
}
|
3105
|
+
}
|
3106
|
+
} catch (error) {
|
3107
|
+
controller.error(error);
|
3108
|
+
}
|
3109
|
+
},
|
3110
|
+
cancel() {
|
3111
|
+
reader1.cancel();
|
3112
|
+
reader2.cancel();
|
3113
|
+
}
|
3114
|
+
});
|
3115
|
+
}
|
3116
|
+
|
3018
3117
|
// shared/parse-complex-response.ts
|
3019
3118
|
function assignAnnotationsToMessage(message, annotations) {
|
3020
3119
|
if (!message || !annotations || !annotations.length)
|
@@ -3169,7 +3268,7 @@ var experimental_StreamingReactResponse = class {
|
|
3169
3268
|
let next = new Promise((resolve) => {
|
3170
3269
|
resolveFunc = resolve;
|
3171
3270
|
});
|
3172
|
-
const processedStream = (options == null ? void 0 : options.data) != null ?
|
3271
|
+
const processedStream = (options == null ? void 0 : options.data) != null ? mergeStreams((_a = options == null ? void 0 : options.data) == null ? void 0 : _a.stream, res) : res;
|
3173
3272
|
let lastPayload = void 0;
|
3174
3273
|
parseComplexResponse({
|
3175
3274
|
reader: processedStream.getReader(),
|
@@ -3207,7 +3306,7 @@ var StreamingTextResponse = class extends Response {
|
|
3207
3306
|
constructor(res, init, data) {
|
3208
3307
|
let processedStream = res;
|
3209
3308
|
if (data) {
|
3210
|
-
processedStream =
|
3309
|
+
processedStream = mergeStreams(data.stream, res);
|
3211
3310
|
}
|
3212
3311
|
super(processedStream, {
|
3213
3312
|
...init,
|
@@ -3248,6 +3347,7 @@ export {
|
|
3248
3347
|
AnthropicStream,
|
3249
3348
|
AssistantResponse,
|
3250
3349
|
CohereStream,
|
3350
|
+
EmbedManyResult,
|
3251
3351
|
EmbedResult,
|
3252
3352
|
EmptyResponseBodyError,
|
3253
3353
|
GenerateObjectResult,
|
@@ -3286,6 +3386,7 @@ export {
|
|
3286
3386
|
createEventStreamTransformer,
|
3287
3387
|
createStreamDataTransformer,
|
3288
3388
|
embed,
|
3389
|
+
embedMany,
|
3289
3390
|
experimental_AssistantResponse,
|
3290
3391
|
experimental_StreamData,
|
3291
3392
|
experimental_StreamingReactResponse,
|