ai 3.1.29 → 3.1.31
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 +4 -563
- package/dist/index.d.ts +4 -563
- package/dist/index.js +38 -509
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +40 -497
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -33
- package/prompts/dist/index.d.mts +2 -129
- package/prompts/dist/index.d.ts +2 -129
- package/prompts/dist/index.js.map +1 -1
- package/prompts/dist/index.mjs.map +1 -1
- package/react/dist/index.d.mts +9 -648
- package/react/dist/index.d.ts +9 -648
- package/react/dist/index.js +4 -1441
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +9 -1429
- package/react/dist/index.mjs.map +1 -1
- package/rsc/dist/rsc-server.mjs +15 -236
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/solid/dist/index.d.mts +7 -465
- package/solid/dist/index.d.ts +7 -465
- package/solid/dist/index.js +3 -1057
- package/solid/dist/index.js.map +1 -1
- package/solid/dist/index.mjs +7 -1056
- package/solid/dist/index.mjs.map +1 -1
- package/svelte/dist/index.d.mts +12 -422
- package/svelte/dist/index.d.ts +12 -422
- package/svelte/dist/index.js +12 -768
- package/svelte/dist/index.js.map +1 -1
- package/svelte/dist/index.mjs +10 -762
- package/svelte/dist/index.mjs.map +1 -1
- package/vue/dist/index.d.mts +7 -459
- package/vue/dist/index.d.ts +7 -459
- package/vue/dist/index.js +3 -1057
- package/vue/dist/index.js.map +1 -1
- package/vue/dist/index.mjs +7 -1046
- package/vue/dist/index.mjs.map +1 -1
package/dist/index.mjs
CHANGED
@@ -4,6 +4,15 @@ var __export = (target, all) => {
|
|
4
4
|
__defProp(target, name, { get: all[name], enumerable: true });
|
5
5
|
};
|
6
6
|
|
7
|
+
// streams/index.ts
|
8
|
+
import {
|
9
|
+
formatStreamPart,
|
10
|
+
parseStreamPart,
|
11
|
+
readDataStream,
|
12
|
+
parseComplexResponse
|
13
|
+
} from "@ai-sdk/ui-utils";
|
14
|
+
import { generateId as generateId2, generateId as generateId3 } from "@ai-sdk/provider-utils";
|
15
|
+
|
7
16
|
// core/util/retry-with-exponential-backoff.ts
|
8
17
|
import { APICallError, RetryError } from "@ai-sdk/provider";
|
9
18
|
import { getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
|
@@ -282,7 +291,13 @@ function convertToLanguageModelMessage(message) {
|
|
282
291
|
content: [{ type: "text", text: message.content }]
|
283
292
|
};
|
284
293
|
}
|
285
|
-
return {
|
294
|
+
return {
|
295
|
+
role: "assistant",
|
296
|
+
content: message.content.filter(
|
297
|
+
// remove empty text parts:
|
298
|
+
(part) => part.type !== "text" || part.text !== ""
|
299
|
+
)
|
300
|
+
};
|
286
301
|
}
|
287
302
|
case "tool": {
|
288
303
|
return message;
|
@@ -1440,15 +1455,7 @@ function prepareResponseHeaders(init, { contentType }) {
|
|
1440
1455
|
|
1441
1456
|
// core/generate-text/run-tools-transformation.ts
|
1442
1457
|
import { NoSuchToolError as NoSuchToolError2 } from "@ai-sdk/provider";
|
1443
|
-
|
1444
|
-
// shared/generate-id.ts
|
1445
|
-
import { customAlphabet } from "nanoid/non-secure";
|
1446
|
-
var generateId = customAlphabet(
|
1447
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
1448
|
-
7
|
1449
|
-
);
|
1450
|
-
|
1451
|
-
// core/generate-text/run-tools-transformation.ts
|
1458
|
+
import { generateId } from "@ai-sdk/ui-utils";
|
1452
1459
|
function runToolsTransformation({
|
1453
1460
|
tools,
|
1454
1461
|
generatorStream
|
@@ -1992,282 +1999,6 @@ import {
|
|
1992
1999
|
UnsupportedJSONSchemaError
|
1993
2000
|
} from "@ai-sdk/provider";
|
1994
2001
|
|
1995
|
-
// shared/stream-parts.ts
|
1996
|
-
var textStreamPart = {
|
1997
|
-
code: "0",
|
1998
|
-
name: "text",
|
1999
|
-
parse: (value) => {
|
2000
|
-
if (typeof value !== "string") {
|
2001
|
-
throw new Error('"text" parts expect a string value.');
|
2002
|
-
}
|
2003
|
-
return { type: "text", value };
|
2004
|
-
}
|
2005
|
-
};
|
2006
|
-
var functionCallStreamPart = {
|
2007
|
-
code: "1",
|
2008
|
-
name: "function_call",
|
2009
|
-
parse: (value) => {
|
2010
|
-
if (value == null || typeof value !== "object" || !("function_call" in value) || typeof value.function_call !== "object" || value.function_call == null || !("name" in value.function_call) || !("arguments" in value.function_call) || typeof value.function_call.name !== "string" || typeof value.function_call.arguments !== "string") {
|
2011
|
-
throw new Error(
|
2012
|
-
'"function_call" parts expect an object with a "function_call" property.'
|
2013
|
-
);
|
2014
|
-
}
|
2015
|
-
return {
|
2016
|
-
type: "function_call",
|
2017
|
-
value
|
2018
|
-
};
|
2019
|
-
}
|
2020
|
-
};
|
2021
|
-
var dataStreamPart = {
|
2022
|
-
code: "2",
|
2023
|
-
name: "data",
|
2024
|
-
parse: (value) => {
|
2025
|
-
if (!Array.isArray(value)) {
|
2026
|
-
throw new Error('"data" parts expect an array value.');
|
2027
|
-
}
|
2028
|
-
return { type: "data", value };
|
2029
|
-
}
|
2030
|
-
};
|
2031
|
-
var errorStreamPart = {
|
2032
|
-
code: "3",
|
2033
|
-
name: "error",
|
2034
|
-
parse: (value) => {
|
2035
|
-
if (typeof value !== "string") {
|
2036
|
-
throw new Error('"error" parts expect a string value.');
|
2037
|
-
}
|
2038
|
-
return { type: "error", value };
|
2039
|
-
}
|
2040
|
-
};
|
2041
|
-
var assistantMessageStreamPart = {
|
2042
|
-
code: "4",
|
2043
|
-
name: "assistant_message",
|
2044
|
-
parse: (value) => {
|
2045
|
-
if (value == null || typeof value !== "object" || !("id" in value) || !("role" in value) || !("content" in value) || typeof value.id !== "string" || typeof value.role !== "string" || value.role !== "assistant" || !Array.isArray(value.content) || !value.content.every(
|
2046
|
-
(item) => item != null && typeof item === "object" && "type" in item && item.type === "text" && "text" in item && item.text != null && typeof item.text === "object" && "value" in item.text && typeof item.text.value === "string"
|
2047
|
-
)) {
|
2048
|
-
throw new Error(
|
2049
|
-
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
2050
|
-
);
|
2051
|
-
}
|
2052
|
-
return {
|
2053
|
-
type: "assistant_message",
|
2054
|
-
value
|
2055
|
-
};
|
2056
|
-
}
|
2057
|
-
};
|
2058
|
-
var assistantControlDataStreamPart = {
|
2059
|
-
code: "5",
|
2060
|
-
name: "assistant_control_data",
|
2061
|
-
parse: (value) => {
|
2062
|
-
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
2063
|
-
throw new Error(
|
2064
|
-
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
2065
|
-
);
|
2066
|
-
}
|
2067
|
-
return {
|
2068
|
-
type: "assistant_control_data",
|
2069
|
-
value: {
|
2070
|
-
threadId: value.threadId,
|
2071
|
-
messageId: value.messageId
|
2072
|
-
}
|
2073
|
-
};
|
2074
|
-
}
|
2075
|
-
};
|
2076
|
-
var dataMessageStreamPart = {
|
2077
|
-
code: "6",
|
2078
|
-
name: "data_message",
|
2079
|
-
parse: (value) => {
|
2080
|
-
if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
|
2081
|
-
throw new Error(
|
2082
|
-
'"data_message" parts expect an object with a "role" and "data" property.'
|
2083
|
-
);
|
2084
|
-
}
|
2085
|
-
return {
|
2086
|
-
type: "data_message",
|
2087
|
-
value
|
2088
|
-
};
|
2089
|
-
}
|
2090
|
-
};
|
2091
|
-
var toolCallsStreamPart = {
|
2092
|
-
code: "7",
|
2093
|
-
name: "tool_calls",
|
2094
|
-
parse: (value) => {
|
2095
|
-
if (value == null || typeof value !== "object" || !("tool_calls" in value) || typeof value.tool_calls !== "object" || value.tool_calls == null || !Array.isArray(value.tool_calls) || value.tool_calls.some(
|
2096
|
-
(tc) => tc == null || typeof tc !== "object" || !("id" in tc) || typeof tc.id !== "string" || !("type" in tc) || typeof tc.type !== "string" || !("function" in tc) || tc.function == null || typeof tc.function !== "object" || !("arguments" in tc.function) || typeof tc.function.name !== "string" || typeof tc.function.arguments !== "string"
|
2097
|
-
)) {
|
2098
|
-
throw new Error(
|
2099
|
-
'"tool_calls" parts expect an object with a ToolCallPayload.'
|
2100
|
-
);
|
2101
|
-
}
|
2102
|
-
return {
|
2103
|
-
type: "tool_calls",
|
2104
|
-
value
|
2105
|
-
};
|
2106
|
-
}
|
2107
|
-
};
|
2108
|
-
var messageAnnotationsStreamPart = {
|
2109
|
-
code: "8",
|
2110
|
-
name: "message_annotations",
|
2111
|
-
parse: (value) => {
|
2112
|
-
if (!Array.isArray(value)) {
|
2113
|
-
throw new Error('"message_annotations" parts expect an array value.');
|
2114
|
-
}
|
2115
|
-
return { type: "message_annotations", value };
|
2116
|
-
}
|
2117
|
-
};
|
2118
|
-
var toolCallStreamPart = {
|
2119
|
-
code: "9",
|
2120
|
-
name: "tool_call",
|
2121
|
-
parse: (value) => {
|
2122
|
-
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object") {
|
2123
|
-
throw new Error(
|
2124
|
-
'"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
|
2125
|
-
);
|
2126
|
-
}
|
2127
|
-
return {
|
2128
|
-
type: "tool_call",
|
2129
|
-
value
|
2130
|
-
};
|
2131
|
-
}
|
2132
|
-
};
|
2133
|
-
var toolResultStreamPart = {
|
2134
|
-
code: "a",
|
2135
|
-
name: "tool_result",
|
2136
|
-
parse: (value) => {
|
2137
|
-
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object" || !("result" in value)) {
|
2138
|
-
throw new Error(
|
2139
|
-
'"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
|
2140
|
-
);
|
2141
|
-
}
|
2142
|
-
return {
|
2143
|
-
type: "tool_result",
|
2144
|
-
value
|
2145
|
-
};
|
2146
|
-
}
|
2147
|
-
};
|
2148
|
-
var streamParts = [
|
2149
|
-
textStreamPart,
|
2150
|
-
functionCallStreamPart,
|
2151
|
-
dataStreamPart,
|
2152
|
-
errorStreamPart,
|
2153
|
-
assistantMessageStreamPart,
|
2154
|
-
assistantControlDataStreamPart,
|
2155
|
-
dataMessageStreamPart,
|
2156
|
-
toolCallsStreamPart,
|
2157
|
-
messageAnnotationsStreamPart,
|
2158
|
-
toolCallStreamPart,
|
2159
|
-
toolResultStreamPart
|
2160
|
-
];
|
2161
|
-
var streamPartsByCode = {
|
2162
|
-
[textStreamPart.code]: textStreamPart,
|
2163
|
-
[functionCallStreamPart.code]: functionCallStreamPart,
|
2164
|
-
[dataStreamPart.code]: dataStreamPart,
|
2165
|
-
[errorStreamPart.code]: errorStreamPart,
|
2166
|
-
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
2167
|
-
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
2168
|
-
[dataMessageStreamPart.code]: dataMessageStreamPart,
|
2169
|
-
[toolCallsStreamPart.code]: toolCallsStreamPart,
|
2170
|
-
[messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
|
2171
|
-
[toolCallStreamPart.code]: toolCallStreamPart,
|
2172
|
-
[toolResultStreamPart.code]: toolResultStreamPart
|
2173
|
-
};
|
2174
|
-
var StreamStringPrefixes = {
|
2175
|
-
[textStreamPart.name]: textStreamPart.code,
|
2176
|
-
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
2177
|
-
[dataStreamPart.name]: dataStreamPart.code,
|
2178
|
-
[errorStreamPart.name]: errorStreamPart.code,
|
2179
|
-
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
2180
|
-
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
2181
|
-
[dataMessageStreamPart.name]: dataMessageStreamPart.code,
|
2182
|
-
[toolCallsStreamPart.name]: toolCallsStreamPart.code,
|
2183
|
-
[messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
|
2184
|
-
[toolCallStreamPart.name]: toolCallStreamPart.code,
|
2185
|
-
[toolResultStreamPart.name]: toolResultStreamPart.code
|
2186
|
-
};
|
2187
|
-
var validCodes = streamParts.map((part) => part.code);
|
2188
|
-
var parseStreamPart = (line) => {
|
2189
|
-
const firstSeparatorIndex = line.indexOf(":");
|
2190
|
-
if (firstSeparatorIndex === -1) {
|
2191
|
-
throw new Error("Failed to parse stream string. No separator found.");
|
2192
|
-
}
|
2193
|
-
const prefix = line.slice(0, firstSeparatorIndex);
|
2194
|
-
if (!validCodes.includes(prefix)) {
|
2195
|
-
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
2196
|
-
}
|
2197
|
-
const code = prefix;
|
2198
|
-
const textValue = line.slice(firstSeparatorIndex + 1);
|
2199
|
-
const jsonValue = JSON.parse(textValue);
|
2200
|
-
return streamPartsByCode[code].parse(jsonValue);
|
2201
|
-
};
|
2202
|
-
function formatStreamPart(type, value) {
|
2203
|
-
const streamPart = streamParts.find((part) => part.name === type);
|
2204
|
-
if (!streamPart) {
|
2205
|
-
throw new Error(`Invalid stream part type: ${type}`);
|
2206
|
-
}
|
2207
|
-
return `${streamPart.code}:${JSON.stringify(value)}
|
2208
|
-
`;
|
2209
|
-
}
|
2210
|
-
|
2211
|
-
// shared/read-data-stream.ts
|
2212
|
-
var NEWLINE = "\n".charCodeAt(0);
|
2213
|
-
function concatChunks(chunks, totalLength) {
|
2214
|
-
const concatenatedChunks = new Uint8Array(totalLength);
|
2215
|
-
let offset = 0;
|
2216
|
-
for (const chunk of chunks) {
|
2217
|
-
concatenatedChunks.set(chunk, offset);
|
2218
|
-
offset += chunk.length;
|
2219
|
-
}
|
2220
|
-
chunks.length = 0;
|
2221
|
-
return concatenatedChunks;
|
2222
|
-
}
|
2223
|
-
async function* readDataStream(reader, {
|
2224
|
-
isAborted
|
2225
|
-
} = {}) {
|
2226
|
-
const decoder = new TextDecoder();
|
2227
|
-
const chunks = [];
|
2228
|
-
let totalLength = 0;
|
2229
|
-
while (true) {
|
2230
|
-
const { value } = await reader.read();
|
2231
|
-
if (value) {
|
2232
|
-
chunks.push(value);
|
2233
|
-
totalLength += value.length;
|
2234
|
-
if (value[value.length - 1] !== NEWLINE) {
|
2235
|
-
continue;
|
2236
|
-
}
|
2237
|
-
}
|
2238
|
-
if (chunks.length === 0) {
|
2239
|
-
break;
|
2240
|
-
}
|
2241
|
-
const concatenatedChunks = concatChunks(chunks, totalLength);
|
2242
|
-
totalLength = 0;
|
2243
|
-
const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
|
2244
|
-
for (const streamPart of streamParts2) {
|
2245
|
-
yield streamPart;
|
2246
|
-
}
|
2247
|
-
if (isAborted == null ? void 0 : isAborted()) {
|
2248
|
-
reader.cancel();
|
2249
|
-
break;
|
2250
|
-
}
|
2251
|
-
}
|
2252
|
-
}
|
2253
|
-
|
2254
|
-
// shared/utils.ts
|
2255
|
-
function createChunkDecoder(complex) {
|
2256
|
-
const decoder = new TextDecoder();
|
2257
|
-
if (!complex) {
|
2258
|
-
return function(chunk) {
|
2259
|
-
if (!chunk)
|
2260
|
-
return "";
|
2261
|
-
return decoder.decode(chunk, { stream: true });
|
2262
|
-
};
|
2263
|
-
}
|
2264
|
-
return function(chunk) {
|
2265
|
-
const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
|
2266
|
-
return decoded.map(parseStreamPart).filter(Boolean);
|
2267
|
-
};
|
2268
|
-
}
|
2269
|
-
var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
|
2270
|
-
|
2271
2002
|
// streams/ai-stream.ts
|
2272
2003
|
import {
|
2273
2004
|
createParser
|
@@ -2393,6 +2124,7 @@ function readableFromAsyncIterable(iterable) {
|
|
2393
2124
|
}
|
2394
2125
|
|
2395
2126
|
// streams/stream-data.ts
|
2127
|
+
import { formatStreamPart as formatStreamPart2 } from "@ai-sdk/ui-utils";
|
2396
2128
|
var StreamData = class {
|
2397
2129
|
constructor() {
|
2398
2130
|
this.encoder = new TextEncoder();
|
@@ -2439,7 +2171,7 @@ var StreamData = class {
|
|
2439
2171
|
throw new Error("Stream controller is not initialized.");
|
2440
2172
|
}
|
2441
2173
|
this.controller.enqueue(
|
2442
|
-
this.encoder.encode(
|
2174
|
+
this.encoder.encode(formatStreamPart2("data", [value]))
|
2443
2175
|
);
|
2444
2176
|
}
|
2445
2177
|
appendMessageAnnotation(value) {
|
@@ -2450,7 +2182,7 @@ var StreamData = class {
|
|
2450
2182
|
throw new Error("Stream controller is not initialized.");
|
2451
2183
|
}
|
2452
2184
|
this.controller.enqueue(
|
2453
|
-
this.encoder.encode(
|
2185
|
+
this.encoder.encode(formatStreamPart2("message_annotations", [value]))
|
2454
2186
|
);
|
2455
2187
|
}
|
2456
2188
|
};
|
@@ -2460,7 +2192,7 @@ function createStreamDataTransformer() {
|
|
2460
2192
|
return new TransformStream({
|
2461
2193
|
transform: async (chunk, controller) => {
|
2462
2194
|
const message = decoder.decode(chunk);
|
2463
|
-
controller.enqueue(encoder.encode(
|
2195
|
+
controller.enqueue(encoder.encode(formatStreamPart2("text", message)));
|
2464
2196
|
}
|
2465
2197
|
});
|
2466
2198
|
}
|
@@ -2514,6 +2246,9 @@ function AnthropicStream(res, cb) {
|
|
2514
2246
|
}
|
2515
2247
|
|
2516
2248
|
// streams/assistant-response.ts
|
2249
|
+
import {
|
2250
|
+
formatStreamPart as formatStreamPart3
|
2251
|
+
} from "@ai-sdk/ui-utils";
|
2517
2252
|
function AssistantResponse({ threadId, messageId }, process2) {
|
2518
2253
|
const stream = new ReadableStream({
|
2519
2254
|
async start(controller) {
|
@@ -2521,17 +2256,17 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2521
2256
|
const textEncoder = new TextEncoder();
|
2522
2257
|
const sendMessage = (message) => {
|
2523
2258
|
controller.enqueue(
|
2524
|
-
textEncoder.encode(
|
2259
|
+
textEncoder.encode(formatStreamPart3("assistant_message", message))
|
2525
2260
|
);
|
2526
2261
|
};
|
2527
2262
|
const sendDataMessage = (message) => {
|
2528
2263
|
controller.enqueue(
|
2529
|
-
textEncoder.encode(
|
2264
|
+
textEncoder.encode(formatStreamPart3("data_message", message))
|
2530
2265
|
);
|
2531
2266
|
};
|
2532
2267
|
const sendError = (errorMessage) => {
|
2533
2268
|
controller.enqueue(
|
2534
|
-
textEncoder.encode(
|
2269
|
+
textEncoder.encode(formatStreamPart3("error", errorMessage))
|
2535
2270
|
);
|
2536
2271
|
};
|
2537
2272
|
const forwardStream = async (stream2) => {
|
@@ -2542,7 +2277,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2542
2277
|
case "thread.message.created": {
|
2543
2278
|
controller.enqueue(
|
2544
2279
|
textEncoder.encode(
|
2545
|
-
|
2280
|
+
formatStreamPart3("assistant_message", {
|
2546
2281
|
id: value.data.id,
|
2547
2282
|
role: "assistant",
|
2548
2283
|
content: [{ type: "text", text: { value: "" } }]
|
@@ -2556,7 +2291,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2556
2291
|
if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
|
2557
2292
|
controller.enqueue(
|
2558
2293
|
textEncoder.encode(
|
2559
|
-
|
2294
|
+
formatStreamPart3("text", content.text.value)
|
2560
2295
|
)
|
2561
2296
|
);
|
2562
2297
|
}
|
@@ -2573,7 +2308,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2573
2308
|
};
|
2574
2309
|
controller.enqueue(
|
2575
2310
|
textEncoder.encode(
|
2576
|
-
|
2311
|
+
formatStreamPart3("assistant_control_data", {
|
2577
2312
|
threadId,
|
2578
2313
|
messageId
|
2579
2314
|
})
|
@@ -2888,6 +2623,10 @@ function MistralStream(response, callbacks) {
|
|
2888
2623
|
}
|
2889
2624
|
|
2890
2625
|
// streams/openai-stream.ts
|
2626
|
+
import {
|
2627
|
+
createChunkDecoder,
|
2628
|
+
formatStreamPart as formatStreamPart4
|
2629
|
+
} from "@ai-sdk/ui-utils";
|
2891
2630
|
function parseOpenAIStream() {
|
2892
2631
|
const extract = chunkToText();
|
2893
2632
|
return (data) => extract(JSON.parse(data));
|
@@ -3052,7 +2791,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3052
2791
|
}
|
3053
2792
|
if (!isFunctionStreamingIn) {
|
3054
2793
|
controller.enqueue(
|
3055
|
-
textEncoder.encode(
|
2794
|
+
textEncoder.encode(formatStreamPart4("text", message))
|
3056
2795
|
);
|
3057
2796
|
return;
|
3058
2797
|
} else {
|
@@ -3163,7 +2902,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3163
2902
|
if (!functionResponse) {
|
3164
2903
|
controller.enqueue(
|
3165
2904
|
textEncoder.encode(
|
3166
|
-
|
2905
|
+
formatStreamPart4(
|
3167
2906
|
payload.function_call ? "function_call" : "tool_calls",
|
3168
2907
|
// parse to prevent double-encoding:
|
3169
2908
|
JSON.parse(aggregatedResponse)
|
@@ -3173,7 +2912,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3173
2912
|
return;
|
3174
2913
|
} else if (typeof functionResponse === "string") {
|
3175
2914
|
controller.enqueue(
|
3176
|
-
textEncoder.encode(
|
2915
|
+
textEncoder.encode(formatStreamPart4("text", functionResponse))
|
3177
2916
|
);
|
3178
2917
|
aggregatedFinalCompletionResponse = functionResponse;
|
3179
2918
|
return;
|
@@ -3340,200 +3079,6 @@ function streamToResponse(res, response, init, data) {
|
|
3340
3079
|
read();
|
3341
3080
|
}
|
3342
3081
|
|
3343
|
-
// shared/parse-complex-response.ts
|
3344
|
-
function assignAnnotationsToMessage(message, annotations) {
|
3345
|
-
if (!message || !annotations || !annotations.length)
|
3346
|
-
return message;
|
3347
|
-
return { ...message, annotations: [...annotations] };
|
3348
|
-
}
|
3349
|
-
async function parseComplexResponse({
|
3350
|
-
reader,
|
3351
|
-
abortControllerRef,
|
3352
|
-
update,
|
3353
|
-
onToolCall,
|
3354
|
-
onFinish,
|
3355
|
-
generateId: generateId2 = generateId,
|
3356
|
-
getCurrentDate = () => /* @__PURE__ */ new Date()
|
3357
|
-
}) {
|
3358
|
-
const createdAt = getCurrentDate();
|
3359
|
-
const prefixMap = {
|
3360
|
-
data: []
|
3361
|
-
};
|
3362
|
-
let message_annotations = void 0;
|
3363
|
-
for await (const { type, value } of readDataStream(reader, {
|
3364
|
-
isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
|
3365
|
-
})) {
|
3366
|
-
if (type === "text") {
|
3367
|
-
if (prefixMap["text"]) {
|
3368
|
-
prefixMap["text"] = {
|
3369
|
-
...prefixMap["text"],
|
3370
|
-
content: (prefixMap["text"].content || "") + value
|
3371
|
-
};
|
3372
|
-
} else {
|
3373
|
-
prefixMap["text"] = {
|
3374
|
-
id: generateId2(),
|
3375
|
-
role: "assistant",
|
3376
|
-
content: value,
|
3377
|
-
createdAt
|
3378
|
-
};
|
3379
|
-
}
|
3380
|
-
}
|
3381
|
-
if (type === "tool_call") {
|
3382
|
-
if (prefixMap.text == null) {
|
3383
|
-
prefixMap.text = {
|
3384
|
-
id: generateId2(),
|
3385
|
-
role: "assistant",
|
3386
|
-
content: "",
|
3387
|
-
createdAt
|
3388
|
-
};
|
3389
|
-
}
|
3390
|
-
if (prefixMap.text.toolInvocations == null) {
|
3391
|
-
prefixMap.text.toolInvocations = [];
|
3392
|
-
}
|
3393
|
-
prefixMap.text.toolInvocations.push(value);
|
3394
|
-
if (onToolCall) {
|
3395
|
-
const result = await onToolCall({ toolCall: value });
|
3396
|
-
if (result != null) {
|
3397
|
-
prefixMap.text.toolInvocations[prefixMap.text.toolInvocations.length - 1] = { ...value, result };
|
3398
|
-
}
|
3399
|
-
}
|
3400
|
-
} else if (type === "tool_result") {
|
3401
|
-
if (prefixMap.text == null) {
|
3402
|
-
prefixMap.text = {
|
3403
|
-
id: generateId2(),
|
3404
|
-
role: "assistant",
|
3405
|
-
content: "",
|
3406
|
-
createdAt
|
3407
|
-
};
|
3408
|
-
}
|
3409
|
-
if (prefixMap.text.toolInvocations == null) {
|
3410
|
-
prefixMap.text.toolInvocations = [];
|
3411
|
-
}
|
3412
|
-
const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
|
3413
|
-
(invocation) => invocation.toolCallId === value.toolCallId
|
3414
|
-
);
|
3415
|
-
if (toolInvocationIndex !== -1) {
|
3416
|
-
prefixMap.text.toolInvocations[toolInvocationIndex] = value;
|
3417
|
-
} else {
|
3418
|
-
prefixMap.text.toolInvocations.push(value);
|
3419
|
-
}
|
3420
|
-
}
|
3421
|
-
let functionCallMessage = null;
|
3422
|
-
if (type === "function_call") {
|
3423
|
-
prefixMap["function_call"] = {
|
3424
|
-
id: generateId2(),
|
3425
|
-
role: "assistant",
|
3426
|
-
content: "",
|
3427
|
-
function_call: value.function_call,
|
3428
|
-
name: value.function_call.name,
|
3429
|
-
createdAt
|
3430
|
-
};
|
3431
|
-
functionCallMessage = prefixMap["function_call"];
|
3432
|
-
}
|
3433
|
-
let toolCallMessage = null;
|
3434
|
-
if (type === "tool_calls") {
|
3435
|
-
prefixMap["tool_calls"] = {
|
3436
|
-
id: generateId2(),
|
3437
|
-
role: "assistant",
|
3438
|
-
content: "",
|
3439
|
-
tool_calls: value.tool_calls,
|
3440
|
-
createdAt
|
3441
|
-
};
|
3442
|
-
toolCallMessage = prefixMap["tool_calls"];
|
3443
|
-
}
|
3444
|
-
if (type === "data") {
|
3445
|
-
prefixMap["data"].push(...value);
|
3446
|
-
}
|
3447
|
-
let responseMessage = prefixMap["text"];
|
3448
|
-
if (type === "message_annotations") {
|
3449
|
-
if (!message_annotations) {
|
3450
|
-
message_annotations = [...value];
|
3451
|
-
} else {
|
3452
|
-
message_annotations.push(...value);
|
3453
|
-
}
|
3454
|
-
functionCallMessage = assignAnnotationsToMessage(
|
3455
|
-
prefixMap["function_call"],
|
3456
|
-
message_annotations
|
3457
|
-
);
|
3458
|
-
toolCallMessage = assignAnnotationsToMessage(
|
3459
|
-
prefixMap["tool_calls"],
|
3460
|
-
message_annotations
|
3461
|
-
);
|
3462
|
-
responseMessage = assignAnnotationsToMessage(
|
3463
|
-
prefixMap["text"],
|
3464
|
-
message_annotations
|
3465
|
-
);
|
3466
|
-
}
|
3467
|
-
if (message_annotations == null ? void 0 : message_annotations.length) {
|
3468
|
-
const messagePrefixKeys = [
|
3469
|
-
"text",
|
3470
|
-
"function_call",
|
3471
|
-
"tool_calls"
|
3472
|
-
];
|
3473
|
-
messagePrefixKeys.forEach((key) => {
|
3474
|
-
if (prefixMap[key]) {
|
3475
|
-
prefixMap[key].annotations = [...message_annotations];
|
3476
|
-
}
|
3477
|
-
});
|
3478
|
-
}
|
3479
|
-
const merged = [functionCallMessage, toolCallMessage, responseMessage].filter(Boolean).map((message) => ({
|
3480
|
-
...assignAnnotationsToMessage(message, message_annotations)
|
3481
|
-
}));
|
3482
|
-
update(merged, [...prefixMap["data"]]);
|
3483
|
-
}
|
3484
|
-
onFinish == null ? void 0 : onFinish(prefixMap);
|
3485
|
-
return {
|
3486
|
-
messages: [
|
3487
|
-
prefixMap.text,
|
3488
|
-
prefixMap.function_call,
|
3489
|
-
prefixMap.tool_calls
|
3490
|
-
].filter(Boolean),
|
3491
|
-
data: prefixMap.data
|
3492
|
-
};
|
3493
|
-
}
|
3494
|
-
|
3495
|
-
// streams/streaming-react-response.ts
|
3496
|
-
var experimental_StreamingReactResponse = class {
|
3497
|
-
constructor(res, options) {
|
3498
|
-
var _a, _b;
|
3499
|
-
let resolveFunc = () => {
|
3500
|
-
};
|
3501
|
-
let next = new Promise((resolve) => {
|
3502
|
-
resolveFunc = resolve;
|
3503
|
-
});
|
3504
|
-
const processedStream = (options == null ? void 0 : options.data) != null ? mergeStreams((_a = options == null ? void 0 : options.data) == null ? void 0 : _a.stream, res) : res;
|
3505
|
-
let lastPayload = void 0;
|
3506
|
-
parseComplexResponse({
|
3507
|
-
reader: processedStream.getReader(),
|
3508
|
-
update: (merged, data) => {
|
3509
|
-
var _a2, _b2, _c;
|
3510
|
-
const content = (_b2 = (_a2 = merged[0]) == null ? void 0 : _a2.content) != null ? _b2 : "";
|
3511
|
-
const ui = ((_c = options == null ? void 0 : options.ui) == null ? void 0 : _c.call(options, { content, data })) || content;
|
3512
|
-
const payload = { ui, content };
|
3513
|
-
const resolvePrevious = resolveFunc;
|
3514
|
-
const nextRow = new Promise((resolve) => {
|
3515
|
-
resolveFunc = resolve;
|
3516
|
-
});
|
3517
|
-
resolvePrevious({
|
3518
|
-
next: nextRow,
|
3519
|
-
...payload
|
3520
|
-
});
|
3521
|
-
lastPayload = payload;
|
3522
|
-
},
|
3523
|
-
generateId: (_b = options == null ? void 0 : options.generateId) != null ? _b : generateId,
|
3524
|
-
onFinish: () => {
|
3525
|
-
if (lastPayload !== void 0) {
|
3526
|
-
resolveFunc({
|
3527
|
-
next: null,
|
3528
|
-
...lastPayload
|
3529
|
-
});
|
3530
|
-
}
|
3531
|
-
}
|
3532
|
-
});
|
3533
|
-
return next;
|
3534
|
-
}
|
3535
|
-
};
|
3536
|
-
|
3537
3082
|
// streams/streaming-text-response.ts
|
3538
3083
|
var StreamingTextResponse = class extends Response {
|
3539
3084
|
constructor(res, init, data) {
|
@@ -3596,24 +3141,22 @@ export {
|
|
3596
3141
|
convertDataContentToUint8Array,
|
3597
3142
|
convertToCoreMessages,
|
3598
3143
|
createCallbacksTransformer,
|
3599
|
-
createChunkDecoder,
|
3600
3144
|
createEventStreamTransformer,
|
3601
3145
|
createStreamDataTransformer,
|
3602
3146
|
embed,
|
3603
3147
|
embedMany,
|
3604
3148
|
experimental_AssistantResponse,
|
3605
3149
|
experimental_StreamData,
|
3606
|
-
experimental_StreamingReactResponse,
|
3607
3150
|
experimental_generateObject,
|
3608
3151
|
experimental_generateText,
|
3609
3152
|
experimental_streamObject,
|
3610
3153
|
experimental_streamText,
|
3611
3154
|
formatStreamPart,
|
3612
|
-
generateId,
|
3155
|
+
generateId2 as generateId,
|
3613
3156
|
generateObject,
|
3614
3157
|
generateText,
|
3615
|
-
|
3616
|
-
|
3158
|
+
generateId3 as nanoid,
|
3159
|
+
parseComplexResponse,
|
3617
3160
|
parseStreamPart,
|
3618
3161
|
readDataStream,
|
3619
3162
|
readableFromAsyncIterable,
|