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.js
CHANGED
@@ -75,26 +75,24 @@ __export(streams_exports, {
|
|
75
75
|
convertDataContentToUint8Array: () => convertDataContentToUint8Array,
|
76
76
|
convertToCoreMessages: () => convertToCoreMessages,
|
77
77
|
createCallbacksTransformer: () => createCallbacksTransformer,
|
78
|
-
createChunkDecoder: () => createChunkDecoder,
|
79
78
|
createEventStreamTransformer: () => createEventStreamTransformer,
|
80
79
|
createStreamDataTransformer: () => createStreamDataTransformer,
|
81
80
|
embed: () => embed,
|
82
81
|
embedMany: () => embedMany,
|
83
82
|
experimental_AssistantResponse: () => experimental_AssistantResponse,
|
84
83
|
experimental_StreamData: () => experimental_StreamData,
|
85
|
-
experimental_StreamingReactResponse: () => experimental_StreamingReactResponse,
|
86
84
|
experimental_generateObject: () => experimental_generateObject,
|
87
85
|
experimental_generateText: () => experimental_generateText,
|
88
86
|
experimental_streamObject: () => experimental_streamObject,
|
89
87
|
experimental_streamText: () => experimental_streamText,
|
90
|
-
formatStreamPart: () => formatStreamPart,
|
91
|
-
generateId: () => generateId,
|
88
|
+
formatStreamPart: () => import_ui_utils5.formatStreamPart,
|
89
|
+
generateId: () => import_provider_utils6.generateId,
|
92
90
|
generateObject: () => generateObject,
|
93
91
|
generateText: () => generateText,
|
94
|
-
|
95
|
-
|
96
|
-
parseStreamPart: () => parseStreamPart,
|
97
|
-
readDataStream: () => readDataStream,
|
92
|
+
nanoid: () => import_provider_utils6.generateId,
|
93
|
+
parseComplexResponse: () => import_ui_utils5.parseComplexResponse,
|
94
|
+
parseStreamPart: () => import_ui_utils5.parseStreamPart,
|
95
|
+
readDataStream: () => import_ui_utils5.readDataStream,
|
98
96
|
readableFromAsyncIterable: () => readableFromAsyncIterable,
|
99
97
|
streamObject: () => streamObject,
|
100
98
|
streamText: () => streamText,
|
@@ -103,6 +101,8 @@ __export(streams_exports, {
|
|
103
101
|
trimStartOfStreamHelper: () => trimStartOfStreamHelper
|
104
102
|
});
|
105
103
|
module.exports = __toCommonJS(streams_exports);
|
104
|
+
var import_ui_utils5 = require("@ai-sdk/ui-utils");
|
105
|
+
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
106
106
|
|
107
107
|
// core/util/retry-with-exponential-backoff.ts
|
108
108
|
var import_provider = require("@ai-sdk/provider");
|
@@ -379,7 +379,13 @@ function convertToLanguageModelMessage(message) {
|
|
379
379
|
content: [{ type: "text", text: message.content }]
|
380
380
|
};
|
381
381
|
}
|
382
|
-
return {
|
382
|
+
return {
|
383
|
+
role: "assistant",
|
384
|
+
content: message.content.filter(
|
385
|
+
// remove empty text parts:
|
386
|
+
(part) => part.type !== "text" || part.text !== ""
|
387
|
+
)
|
388
|
+
};
|
383
389
|
}
|
384
390
|
case "tool": {
|
385
391
|
return message;
|
@@ -1534,15 +1540,7 @@ function prepareResponseHeaders(init, { contentType }) {
|
|
1534
1540
|
|
1535
1541
|
// core/generate-text/run-tools-transformation.ts
|
1536
1542
|
var import_provider7 = require("@ai-sdk/provider");
|
1537
|
-
|
1538
|
-
// shared/generate-id.ts
|
1539
|
-
var import_non_secure = require("nanoid/non-secure");
|
1540
|
-
var generateId = (0, import_non_secure.customAlphabet)(
|
1541
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
1542
|
-
7
|
1543
|
-
);
|
1544
|
-
|
1545
|
-
// core/generate-text/run-tools-transformation.ts
|
1543
|
+
var import_ui_utils = require("@ai-sdk/ui-utils");
|
1546
1544
|
function runToolsTransformation({
|
1547
1545
|
tools,
|
1548
1546
|
generatorStream
|
@@ -1591,7 +1589,7 @@ function runToolsTransformation({
|
|
1591
1589
|
});
|
1592
1590
|
controller.enqueue(toolCall);
|
1593
1591
|
if (tool2.execute != null) {
|
1594
|
-
const toolExecutionId = generateId();
|
1592
|
+
const toolExecutionId = (0, import_ui_utils.generateId)();
|
1595
1593
|
outstandingToolCalls.add(toolExecutionId);
|
1596
1594
|
tool2.execute(toolCall.args).then(
|
1597
1595
|
(result) => {
|
@@ -1889,11 +1887,11 @@ var StreamTextResult = class {
|
|
1889
1887
|
transform: async (chunk, controller) => {
|
1890
1888
|
switch (chunk.type) {
|
1891
1889
|
case "text-delta":
|
1892
|
-
controller.enqueue(formatStreamPart("text", chunk.textDelta));
|
1890
|
+
controller.enqueue((0, import_ui_utils5.formatStreamPart)("text", chunk.textDelta));
|
1893
1891
|
break;
|
1894
1892
|
case "tool-call":
|
1895
1893
|
controller.enqueue(
|
1896
|
-
formatStreamPart("tool_call", {
|
1894
|
+
(0, import_ui_utils5.formatStreamPart)("tool_call", {
|
1897
1895
|
toolCallId: chunk.toolCallId,
|
1898
1896
|
toolName: chunk.toolName,
|
1899
1897
|
args: chunk.args
|
@@ -1902,7 +1900,7 @@ var StreamTextResult = class {
|
|
1902
1900
|
break;
|
1903
1901
|
case "tool-result":
|
1904
1902
|
controller.enqueue(
|
1905
|
-
formatStreamPart("tool_result", {
|
1903
|
+
(0, import_ui_utils5.formatStreamPart)("tool_result", {
|
1906
1904
|
toolCallId: chunk.toolCallId,
|
1907
1905
|
toolName: chunk.toolName,
|
1908
1906
|
args: chunk.args,
|
@@ -1912,7 +1910,7 @@ var StreamTextResult = class {
|
|
1912
1910
|
break;
|
1913
1911
|
case "error":
|
1914
1912
|
controller.enqueue(
|
1915
|
-
formatStreamPart("error", JSON.stringify(chunk.error))
|
1913
|
+
(0, import_ui_utils5.formatStreamPart)("error", JSON.stringify(chunk.error))
|
1916
1914
|
);
|
1917
1915
|
break;
|
1918
1916
|
}
|
@@ -2069,282 +2067,6 @@ function tool(tool2) {
|
|
2069
2067
|
// core/types/errors.ts
|
2070
2068
|
var import_provider8 = require("@ai-sdk/provider");
|
2071
2069
|
|
2072
|
-
// shared/stream-parts.ts
|
2073
|
-
var textStreamPart = {
|
2074
|
-
code: "0",
|
2075
|
-
name: "text",
|
2076
|
-
parse: (value) => {
|
2077
|
-
if (typeof value !== "string") {
|
2078
|
-
throw new Error('"text" parts expect a string value.');
|
2079
|
-
}
|
2080
|
-
return { type: "text", value };
|
2081
|
-
}
|
2082
|
-
};
|
2083
|
-
var functionCallStreamPart = {
|
2084
|
-
code: "1",
|
2085
|
-
name: "function_call",
|
2086
|
-
parse: (value) => {
|
2087
|
-
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") {
|
2088
|
-
throw new Error(
|
2089
|
-
'"function_call" parts expect an object with a "function_call" property.'
|
2090
|
-
);
|
2091
|
-
}
|
2092
|
-
return {
|
2093
|
-
type: "function_call",
|
2094
|
-
value
|
2095
|
-
};
|
2096
|
-
}
|
2097
|
-
};
|
2098
|
-
var dataStreamPart = {
|
2099
|
-
code: "2",
|
2100
|
-
name: "data",
|
2101
|
-
parse: (value) => {
|
2102
|
-
if (!Array.isArray(value)) {
|
2103
|
-
throw new Error('"data" parts expect an array value.');
|
2104
|
-
}
|
2105
|
-
return { type: "data", value };
|
2106
|
-
}
|
2107
|
-
};
|
2108
|
-
var errorStreamPart = {
|
2109
|
-
code: "3",
|
2110
|
-
name: "error",
|
2111
|
-
parse: (value) => {
|
2112
|
-
if (typeof value !== "string") {
|
2113
|
-
throw new Error('"error" parts expect a string value.');
|
2114
|
-
}
|
2115
|
-
return { type: "error", value };
|
2116
|
-
}
|
2117
|
-
};
|
2118
|
-
var assistantMessageStreamPart = {
|
2119
|
-
code: "4",
|
2120
|
-
name: "assistant_message",
|
2121
|
-
parse: (value) => {
|
2122
|
-
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(
|
2123
|
-
(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"
|
2124
|
-
)) {
|
2125
|
-
throw new Error(
|
2126
|
-
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
2127
|
-
);
|
2128
|
-
}
|
2129
|
-
return {
|
2130
|
-
type: "assistant_message",
|
2131
|
-
value
|
2132
|
-
};
|
2133
|
-
}
|
2134
|
-
};
|
2135
|
-
var assistantControlDataStreamPart = {
|
2136
|
-
code: "5",
|
2137
|
-
name: "assistant_control_data",
|
2138
|
-
parse: (value) => {
|
2139
|
-
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
2140
|
-
throw new Error(
|
2141
|
-
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
2142
|
-
);
|
2143
|
-
}
|
2144
|
-
return {
|
2145
|
-
type: "assistant_control_data",
|
2146
|
-
value: {
|
2147
|
-
threadId: value.threadId,
|
2148
|
-
messageId: value.messageId
|
2149
|
-
}
|
2150
|
-
};
|
2151
|
-
}
|
2152
|
-
};
|
2153
|
-
var dataMessageStreamPart = {
|
2154
|
-
code: "6",
|
2155
|
-
name: "data_message",
|
2156
|
-
parse: (value) => {
|
2157
|
-
if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
|
2158
|
-
throw new Error(
|
2159
|
-
'"data_message" parts expect an object with a "role" and "data" property.'
|
2160
|
-
);
|
2161
|
-
}
|
2162
|
-
return {
|
2163
|
-
type: "data_message",
|
2164
|
-
value
|
2165
|
-
};
|
2166
|
-
}
|
2167
|
-
};
|
2168
|
-
var toolCallsStreamPart = {
|
2169
|
-
code: "7",
|
2170
|
-
name: "tool_calls",
|
2171
|
-
parse: (value) => {
|
2172
|
-
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(
|
2173
|
-
(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"
|
2174
|
-
)) {
|
2175
|
-
throw new Error(
|
2176
|
-
'"tool_calls" parts expect an object with a ToolCallPayload.'
|
2177
|
-
);
|
2178
|
-
}
|
2179
|
-
return {
|
2180
|
-
type: "tool_calls",
|
2181
|
-
value
|
2182
|
-
};
|
2183
|
-
}
|
2184
|
-
};
|
2185
|
-
var messageAnnotationsStreamPart = {
|
2186
|
-
code: "8",
|
2187
|
-
name: "message_annotations",
|
2188
|
-
parse: (value) => {
|
2189
|
-
if (!Array.isArray(value)) {
|
2190
|
-
throw new Error('"message_annotations" parts expect an array value.');
|
2191
|
-
}
|
2192
|
-
return { type: "message_annotations", value };
|
2193
|
-
}
|
2194
|
-
};
|
2195
|
-
var toolCallStreamPart = {
|
2196
|
-
code: "9",
|
2197
|
-
name: "tool_call",
|
2198
|
-
parse: (value) => {
|
2199
|
-
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") {
|
2200
|
-
throw new Error(
|
2201
|
-
'"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
|
2202
|
-
);
|
2203
|
-
}
|
2204
|
-
return {
|
2205
|
-
type: "tool_call",
|
2206
|
-
value
|
2207
|
-
};
|
2208
|
-
}
|
2209
|
-
};
|
2210
|
-
var toolResultStreamPart = {
|
2211
|
-
code: "a",
|
2212
|
-
name: "tool_result",
|
2213
|
-
parse: (value) => {
|
2214
|
-
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)) {
|
2215
|
-
throw new Error(
|
2216
|
-
'"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
|
2217
|
-
);
|
2218
|
-
}
|
2219
|
-
return {
|
2220
|
-
type: "tool_result",
|
2221
|
-
value
|
2222
|
-
};
|
2223
|
-
}
|
2224
|
-
};
|
2225
|
-
var streamParts = [
|
2226
|
-
textStreamPart,
|
2227
|
-
functionCallStreamPart,
|
2228
|
-
dataStreamPart,
|
2229
|
-
errorStreamPart,
|
2230
|
-
assistantMessageStreamPart,
|
2231
|
-
assistantControlDataStreamPart,
|
2232
|
-
dataMessageStreamPart,
|
2233
|
-
toolCallsStreamPart,
|
2234
|
-
messageAnnotationsStreamPart,
|
2235
|
-
toolCallStreamPart,
|
2236
|
-
toolResultStreamPart
|
2237
|
-
];
|
2238
|
-
var streamPartsByCode = {
|
2239
|
-
[textStreamPart.code]: textStreamPart,
|
2240
|
-
[functionCallStreamPart.code]: functionCallStreamPart,
|
2241
|
-
[dataStreamPart.code]: dataStreamPart,
|
2242
|
-
[errorStreamPart.code]: errorStreamPart,
|
2243
|
-
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
2244
|
-
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
2245
|
-
[dataMessageStreamPart.code]: dataMessageStreamPart,
|
2246
|
-
[toolCallsStreamPart.code]: toolCallsStreamPart,
|
2247
|
-
[messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
|
2248
|
-
[toolCallStreamPart.code]: toolCallStreamPart,
|
2249
|
-
[toolResultStreamPart.code]: toolResultStreamPart
|
2250
|
-
};
|
2251
|
-
var StreamStringPrefixes = {
|
2252
|
-
[textStreamPart.name]: textStreamPart.code,
|
2253
|
-
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
2254
|
-
[dataStreamPart.name]: dataStreamPart.code,
|
2255
|
-
[errorStreamPart.name]: errorStreamPart.code,
|
2256
|
-
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
2257
|
-
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
2258
|
-
[dataMessageStreamPart.name]: dataMessageStreamPart.code,
|
2259
|
-
[toolCallsStreamPart.name]: toolCallsStreamPart.code,
|
2260
|
-
[messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
|
2261
|
-
[toolCallStreamPart.name]: toolCallStreamPart.code,
|
2262
|
-
[toolResultStreamPart.name]: toolResultStreamPart.code
|
2263
|
-
};
|
2264
|
-
var validCodes = streamParts.map((part) => part.code);
|
2265
|
-
var parseStreamPart = (line) => {
|
2266
|
-
const firstSeparatorIndex = line.indexOf(":");
|
2267
|
-
if (firstSeparatorIndex === -1) {
|
2268
|
-
throw new Error("Failed to parse stream string. No separator found.");
|
2269
|
-
}
|
2270
|
-
const prefix = line.slice(0, firstSeparatorIndex);
|
2271
|
-
if (!validCodes.includes(prefix)) {
|
2272
|
-
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
2273
|
-
}
|
2274
|
-
const code = prefix;
|
2275
|
-
const textValue = line.slice(firstSeparatorIndex + 1);
|
2276
|
-
const jsonValue = JSON.parse(textValue);
|
2277
|
-
return streamPartsByCode[code].parse(jsonValue);
|
2278
|
-
};
|
2279
|
-
function formatStreamPart(type, value) {
|
2280
|
-
const streamPart = streamParts.find((part) => part.name === type);
|
2281
|
-
if (!streamPart) {
|
2282
|
-
throw new Error(`Invalid stream part type: ${type}`);
|
2283
|
-
}
|
2284
|
-
return `${streamPart.code}:${JSON.stringify(value)}
|
2285
|
-
`;
|
2286
|
-
}
|
2287
|
-
|
2288
|
-
// shared/read-data-stream.ts
|
2289
|
-
var NEWLINE = "\n".charCodeAt(0);
|
2290
|
-
function concatChunks(chunks, totalLength) {
|
2291
|
-
const concatenatedChunks = new Uint8Array(totalLength);
|
2292
|
-
let offset = 0;
|
2293
|
-
for (const chunk of chunks) {
|
2294
|
-
concatenatedChunks.set(chunk, offset);
|
2295
|
-
offset += chunk.length;
|
2296
|
-
}
|
2297
|
-
chunks.length = 0;
|
2298
|
-
return concatenatedChunks;
|
2299
|
-
}
|
2300
|
-
async function* readDataStream(reader, {
|
2301
|
-
isAborted
|
2302
|
-
} = {}) {
|
2303
|
-
const decoder = new TextDecoder();
|
2304
|
-
const chunks = [];
|
2305
|
-
let totalLength = 0;
|
2306
|
-
while (true) {
|
2307
|
-
const { value } = await reader.read();
|
2308
|
-
if (value) {
|
2309
|
-
chunks.push(value);
|
2310
|
-
totalLength += value.length;
|
2311
|
-
if (value[value.length - 1] !== NEWLINE) {
|
2312
|
-
continue;
|
2313
|
-
}
|
2314
|
-
}
|
2315
|
-
if (chunks.length === 0) {
|
2316
|
-
break;
|
2317
|
-
}
|
2318
|
-
const concatenatedChunks = concatChunks(chunks, totalLength);
|
2319
|
-
totalLength = 0;
|
2320
|
-
const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
|
2321
|
-
for (const streamPart of streamParts2) {
|
2322
|
-
yield streamPart;
|
2323
|
-
}
|
2324
|
-
if (isAborted == null ? void 0 : isAborted()) {
|
2325
|
-
reader.cancel();
|
2326
|
-
break;
|
2327
|
-
}
|
2328
|
-
}
|
2329
|
-
}
|
2330
|
-
|
2331
|
-
// shared/utils.ts
|
2332
|
-
function createChunkDecoder(complex) {
|
2333
|
-
const decoder = new TextDecoder();
|
2334
|
-
if (!complex) {
|
2335
|
-
return function(chunk) {
|
2336
|
-
if (!chunk)
|
2337
|
-
return "";
|
2338
|
-
return decoder.decode(chunk, { stream: true });
|
2339
|
-
};
|
2340
|
-
}
|
2341
|
-
return function(chunk) {
|
2342
|
-
const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
|
2343
|
-
return decoded.map(parseStreamPart).filter(Boolean);
|
2344
|
-
};
|
2345
|
-
}
|
2346
|
-
var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
|
2347
|
-
|
2348
2070
|
// streams/ai-stream.ts
|
2349
2071
|
var import_eventsource_parser = require("eventsource-parser");
|
2350
2072
|
function createEventStreamTransformer(customParser) {
|
@@ -2468,6 +2190,7 @@ function readableFromAsyncIterable(iterable) {
|
|
2468
2190
|
}
|
2469
2191
|
|
2470
2192
|
// streams/stream-data.ts
|
2193
|
+
var import_ui_utils2 = require("@ai-sdk/ui-utils");
|
2471
2194
|
var StreamData = class {
|
2472
2195
|
constructor() {
|
2473
2196
|
this.encoder = new TextEncoder();
|
@@ -2514,7 +2237,7 @@ var StreamData = class {
|
|
2514
2237
|
throw new Error("Stream controller is not initialized.");
|
2515
2238
|
}
|
2516
2239
|
this.controller.enqueue(
|
2517
|
-
this.encoder.encode(formatStreamPart("data", [value]))
|
2240
|
+
this.encoder.encode((0, import_ui_utils2.formatStreamPart)("data", [value]))
|
2518
2241
|
);
|
2519
2242
|
}
|
2520
2243
|
appendMessageAnnotation(value) {
|
@@ -2525,7 +2248,7 @@ var StreamData = class {
|
|
2525
2248
|
throw new Error("Stream controller is not initialized.");
|
2526
2249
|
}
|
2527
2250
|
this.controller.enqueue(
|
2528
|
-
this.encoder.encode(formatStreamPart("message_annotations", [value]))
|
2251
|
+
this.encoder.encode((0, import_ui_utils2.formatStreamPart)("message_annotations", [value]))
|
2529
2252
|
);
|
2530
2253
|
}
|
2531
2254
|
};
|
@@ -2535,7 +2258,7 @@ function createStreamDataTransformer() {
|
|
2535
2258
|
return new TransformStream({
|
2536
2259
|
transform: async (chunk, controller) => {
|
2537
2260
|
const message = decoder.decode(chunk);
|
2538
|
-
controller.enqueue(encoder.encode(formatStreamPart("text", message)));
|
2261
|
+
controller.enqueue(encoder.encode((0, import_ui_utils2.formatStreamPart)("text", message)));
|
2539
2262
|
}
|
2540
2263
|
});
|
2541
2264
|
}
|
@@ -2589,6 +2312,7 @@ function AnthropicStream(res, cb) {
|
|
2589
2312
|
}
|
2590
2313
|
|
2591
2314
|
// streams/assistant-response.ts
|
2315
|
+
var import_ui_utils3 = require("@ai-sdk/ui-utils");
|
2592
2316
|
function AssistantResponse({ threadId, messageId }, process2) {
|
2593
2317
|
const stream = new ReadableStream({
|
2594
2318
|
async start(controller) {
|
@@ -2596,17 +2320,17 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2596
2320
|
const textEncoder = new TextEncoder();
|
2597
2321
|
const sendMessage = (message) => {
|
2598
2322
|
controller.enqueue(
|
2599
|
-
textEncoder.encode(formatStreamPart("assistant_message", message))
|
2323
|
+
textEncoder.encode((0, import_ui_utils3.formatStreamPart)("assistant_message", message))
|
2600
2324
|
);
|
2601
2325
|
};
|
2602
2326
|
const sendDataMessage = (message) => {
|
2603
2327
|
controller.enqueue(
|
2604
|
-
textEncoder.encode(formatStreamPart("data_message", message))
|
2328
|
+
textEncoder.encode((0, import_ui_utils3.formatStreamPart)("data_message", message))
|
2605
2329
|
);
|
2606
2330
|
};
|
2607
2331
|
const sendError = (errorMessage) => {
|
2608
2332
|
controller.enqueue(
|
2609
|
-
textEncoder.encode(formatStreamPart("error", errorMessage))
|
2333
|
+
textEncoder.encode((0, import_ui_utils3.formatStreamPart)("error", errorMessage))
|
2610
2334
|
);
|
2611
2335
|
};
|
2612
2336
|
const forwardStream = async (stream2) => {
|
@@ -2617,7 +2341,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2617
2341
|
case "thread.message.created": {
|
2618
2342
|
controller.enqueue(
|
2619
2343
|
textEncoder.encode(
|
2620
|
-
formatStreamPart("assistant_message", {
|
2344
|
+
(0, import_ui_utils3.formatStreamPart)("assistant_message", {
|
2621
2345
|
id: value.data.id,
|
2622
2346
|
role: "assistant",
|
2623
2347
|
content: [{ type: "text", text: { value: "" } }]
|
@@ -2631,7 +2355,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2631
2355
|
if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
|
2632
2356
|
controller.enqueue(
|
2633
2357
|
textEncoder.encode(
|
2634
|
-
formatStreamPart("text", content.text.value)
|
2358
|
+
(0, import_ui_utils3.formatStreamPart)("text", content.text.value)
|
2635
2359
|
)
|
2636
2360
|
);
|
2637
2361
|
}
|
@@ -2648,7 +2372,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2648
2372
|
};
|
2649
2373
|
controller.enqueue(
|
2650
2374
|
textEncoder.encode(
|
2651
|
-
formatStreamPart("assistant_control_data", {
|
2375
|
+
(0, import_ui_utils3.formatStreamPart)("assistant_control_data", {
|
2652
2376
|
threadId,
|
2653
2377
|
messageId
|
2654
2378
|
})
|
@@ -2963,6 +2687,7 @@ function MistralStream(response, callbacks) {
|
|
2963
2687
|
}
|
2964
2688
|
|
2965
2689
|
// streams/openai-stream.ts
|
2690
|
+
var import_ui_utils4 = require("@ai-sdk/ui-utils");
|
2966
2691
|
function parseOpenAIStream() {
|
2967
2692
|
const extract = chunkToText();
|
2968
2693
|
return (data) => extract(JSON.parse(data));
|
@@ -3113,7 +2838,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3113
2838
|
let aggregatedFinalCompletionResponse = "";
|
3114
2839
|
let isFunctionStreamingIn = false;
|
3115
2840
|
let functionCallMessages = callbacks[__internal__OpenAIFnMessagesSymbol] || [];
|
3116
|
-
const decode = createChunkDecoder();
|
2841
|
+
const decode = (0, import_ui_utils4.createChunkDecoder)();
|
3117
2842
|
return new TransformStream({
|
3118
2843
|
async transform(chunk, controller) {
|
3119
2844
|
const message = decode(chunk);
|
@@ -3127,7 +2852,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3127
2852
|
}
|
3128
2853
|
if (!isFunctionStreamingIn) {
|
3129
2854
|
controller.enqueue(
|
3130
|
-
textEncoder.encode(formatStreamPart("text", message))
|
2855
|
+
textEncoder.encode((0, import_ui_utils4.formatStreamPart)("text", message))
|
3131
2856
|
);
|
3132
2857
|
return;
|
3133
2858
|
} else {
|
@@ -3238,7 +2963,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3238
2963
|
if (!functionResponse) {
|
3239
2964
|
controller.enqueue(
|
3240
2965
|
textEncoder.encode(
|
3241
|
-
formatStreamPart(
|
2966
|
+
(0, import_ui_utils4.formatStreamPart)(
|
3242
2967
|
payload.function_call ? "function_call" : "tool_calls",
|
3243
2968
|
// parse to prevent double-encoding:
|
3244
2969
|
JSON.parse(aggregatedResponse)
|
@@ -3248,7 +2973,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3248
2973
|
return;
|
3249
2974
|
} else if (typeof functionResponse === "string") {
|
3250
2975
|
controller.enqueue(
|
3251
|
-
textEncoder.encode(formatStreamPart("text", functionResponse))
|
2976
|
+
textEncoder.encode((0, import_ui_utils4.formatStreamPart)("text", functionResponse))
|
3252
2977
|
);
|
3253
2978
|
aggregatedFinalCompletionResponse = functionResponse;
|
3254
2979
|
return;
|
@@ -3415,200 +3140,6 @@ function streamToResponse(res, response, init, data) {
|
|
3415
3140
|
read();
|
3416
3141
|
}
|
3417
3142
|
|
3418
|
-
// shared/parse-complex-response.ts
|
3419
|
-
function assignAnnotationsToMessage(message, annotations) {
|
3420
|
-
if (!message || !annotations || !annotations.length)
|
3421
|
-
return message;
|
3422
|
-
return { ...message, annotations: [...annotations] };
|
3423
|
-
}
|
3424
|
-
async function parseComplexResponse({
|
3425
|
-
reader,
|
3426
|
-
abortControllerRef,
|
3427
|
-
update,
|
3428
|
-
onToolCall,
|
3429
|
-
onFinish,
|
3430
|
-
generateId: generateId2 = generateId,
|
3431
|
-
getCurrentDate = () => /* @__PURE__ */ new Date()
|
3432
|
-
}) {
|
3433
|
-
const createdAt = getCurrentDate();
|
3434
|
-
const prefixMap = {
|
3435
|
-
data: []
|
3436
|
-
};
|
3437
|
-
let message_annotations = void 0;
|
3438
|
-
for await (const { type, value } of readDataStream(reader, {
|
3439
|
-
isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
|
3440
|
-
})) {
|
3441
|
-
if (type === "text") {
|
3442
|
-
if (prefixMap["text"]) {
|
3443
|
-
prefixMap["text"] = {
|
3444
|
-
...prefixMap["text"],
|
3445
|
-
content: (prefixMap["text"].content || "") + value
|
3446
|
-
};
|
3447
|
-
} else {
|
3448
|
-
prefixMap["text"] = {
|
3449
|
-
id: generateId2(),
|
3450
|
-
role: "assistant",
|
3451
|
-
content: value,
|
3452
|
-
createdAt
|
3453
|
-
};
|
3454
|
-
}
|
3455
|
-
}
|
3456
|
-
if (type === "tool_call") {
|
3457
|
-
if (prefixMap.text == null) {
|
3458
|
-
prefixMap.text = {
|
3459
|
-
id: generateId2(),
|
3460
|
-
role: "assistant",
|
3461
|
-
content: "",
|
3462
|
-
createdAt
|
3463
|
-
};
|
3464
|
-
}
|
3465
|
-
if (prefixMap.text.toolInvocations == null) {
|
3466
|
-
prefixMap.text.toolInvocations = [];
|
3467
|
-
}
|
3468
|
-
prefixMap.text.toolInvocations.push(value);
|
3469
|
-
if (onToolCall) {
|
3470
|
-
const result = await onToolCall({ toolCall: value });
|
3471
|
-
if (result != null) {
|
3472
|
-
prefixMap.text.toolInvocations[prefixMap.text.toolInvocations.length - 1] = { ...value, result };
|
3473
|
-
}
|
3474
|
-
}
|
3475
|
-
} else if (type === "tool_result") {
|
3476
|
-
if (prefixMap.text == null) {
|
3477
|
-
prefixMap.text = {
|
3478
|
-
id: generateId2(),
|
3479
|
-
role: "assistant",
|
3480
|
-
content: "",
|
3481
|
-
createdAt
|
3482
|
-
};
|
3483
|
-
}
|
3484
|
-
if (prefixMap.text.toolInvocations == null) {
|
3485
|
-
prefixMap.text.toolInvocations = [];
|
3486
|
-
}
|
3487
|
-
const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
|
3488
|
-
(invocation) => invocation.toolCallId === value.toolCallId
|
3489
|
-
);
|
3490
|
-
if (toolInvocationIndex !== -1) {
|
3491
|
-
prefixMap.text.toolInvocations[toolInvocationIndex] = value;
|
3492
|
-
} else {
|
3493
|
-
prefixMap.text.toolInvocations.push(value);
|
3494
|
-
}
|
3495
|
-
}
|
3496
|
-
let functionCallMessage = null;
|
3497
|
-
if (type === "function_call") {
|
3498
|
-
prefixMap["function_call"] = {
|
3499
|
-
id: generateId2(),
|
3500
|
-
role: "assistant",
|
3501
|
-
content: "",
|
3502
|
-
function_call: value.function_call,
|
3503
|
-
name: value.function_call.name,
|
3504
|
-
createdAt
|
3505
|
-
};
|
3506
|
-
functionCallMessage = prefixMap["function_call"];
|
3507
|
-
}
|
3508
|
-
let toolCallMessage = null;
|
3509
|
-
if (type === "tool_calls") {
|
3510
|
-
prefixMap["tool_calls"] = {
|
3511
|
-
id: generateId2(),
|
3512
|
-
role: "assistant",
|
3513
|
-
content: "",
|
3514
|
-
tool_calls: value.tool_calls,
|
3515
|
-
createdAt
|
3516
|
-
};
|
3517
|
-
toolCallMessage = prefixMap["tool_calls"];
|
3518
|
-
}
|
3519
|
-
if (type === "data") {
|
3520
|
-
prefixMap["data"].push(...value);
|
3521
|
-
}
|
3522
|
-
let responseMessage = prefixMap["text"];
|
3523
|
-
if (type === "message_annotations") {
|
3524
|
-
if (!message_annotations) {
|
3525
|
-
message_annotations = [...value];
|
3526
|
-
} else {
|
3527
|
-
message_annotations.push(...value);
|
3528
|
-
}
|
3529
|
-
functionCallMessage = assignAnnotationsToMessage(
|
3530
|
-
prefixMap["function_call"],
|
3531
|
-
message_annotations
|
3532
|
-
);
|
3533
|
-
toolCallMessage = assignAnnotationsToMessage(
|
3534
|
-
prefixMap["tool_calls"],
|
3535
|
-
message_annotations
|
3536
|
-
);
|
3537
|
-
responseMessage = assignAnnotationsToMessage(
|
3538
|
-
prefixMap["text"],
|
3539
|
-
message_annotations
|
3540
|
-
);
|
3541
|
-
}
|
3542
|
-
if (message_annotations == null ? void 0 : message_annotations.length) {
|
3543
|
-
const messagePrefixKeys = [
|
3544
|
-
"text",
|
3545
|
-
"function_call",
|
3546
|
-
"tool_calls"
|
3547
|
-
];
|
3548
|
-
messagePrefixKeys.forEach((key) => {
|
3549
|
-
if (prefixMap[key]) {
|
3550
|
-
prefixMap[key].annotations = [...message_annotations];
|
3551
|
-
}
|
3552
|
-
});
|
3553
|
-
}
|
3554
|
-
const merged = [functionCallMessage, toolCallMessage, responseMessage].filter(Boolean).map((message) => ({
|
3555
|
-
...assignAnnotationsToMessage(message, message_annotations)
|
3556
|
-
}));
|
3557
|
-
update(merged, [...prefixMap["data"]]);
|
3558
|
-
}
|
3559
|
-
onFinish == null ? void 0 : onFinish(prefixMap);
|
3560
|
-
return {
|
3561
|
-
messages: [
|
3562
|
-
prefixMap.text,
|
3563
|
-
prefixMap.function_call,
|
3564
|
-
prefixMap.tool_calls
|
3565
|
-
].filter(Boolean),
|
3566
|
-
data: prefixMap.data
|
3567
|
-
};
|
3568
|
-
}
|
3569
|
-
|
3570
|
-
// streams/streaming-react-response.ts
|
3571
|
-
var experimental_StreamingReactResponse = class {
|
3572
|
-
constructor(res, options) {
|
3573
|
-
var _a, _b;
|
3574
|
-
let resolveFunc = () => {
|
3575
|
-
};
|
3576
|
-
let next = new Promise((resolve) => {
|
3577
|
-
resolveFunc = resolve;
|
3578
|
-
});
|
3579
|
-
const processedStream = (options == null ? void 0 : options.data) != null ? mergeStreams((_a = options == null ? void 0 : options.data) == null ? void 0 : _a.stream, res) : res;
|
3580
|
-
let lastPayload = void 0;
|
3581
|
-
parseComplexResponse({
|
3582
|
-
reader: processedStream.getReader(),
|
3583
|
-
update: (merged, data) => {
|
3584
|
-
var _a2, _b2, _c;
|
3585
|
-
const content = (_b2 = (_a2 = merged[0]) == null ? void 0 : _a2.content) != null ? _b2 : "";
|
3586
|
-
const ui = ((_c = options == null ? void 0 : options.ui) == null ? void 0 : _c.call(options, { content, data })) || content;
|
3587
|
-
const payload = { ui, content };
|
3588
|
-
const resolvePrevious = resolveFunc;
|
3589
|
-
const nextRow = new Promise((resolve) => {
|
3590
|
-
resolveFunc = resolve;
|
3591
|
-
});
|
3592
|
-
resolvePrevious({
|
3593
|
-
next: nextRow,
|
3594
|
-
...payload
|
3595
|
-
});
|
3596
|
-
lastPayload = payload;
|
3597
|
-
},
|
3598
|
-
generateId: (_b = options == null ? void 0 : options.generateId) != null ? _b : generateId,
|
3599
|
-
onFinish: () => {
|
3600
|
-
if (lastPayload !== void 0) {
|
3601
|
-
resolveFunc({
|
3602
|
-
next: null,
|
3603
|
-
...lastPayload
|
3604
|
-
});
|
3605
|
-
}
|
3606
|
-
}
|
3607
|
-
});
|
3608
|
-
return next;
|
3609
|
-
}
|
3610
|
-
};
|
3611
|
-
|
3612
3143
|
// streams/streaming-text-response.ts
|
3613
3144
|
var StreamingTextResponse = class extends Response {
|
3614
3145
|
constructor(res, init, data) {
|
@@ -3672,14 +3203,12 @@ var StreamingTextResponse = class extends Response {
|
|
3672
3203
|
convertDataContentToUint8Array,
|
3673
3204
|
convertToCoreMessages,
|
3674
3205
|
createCallbacksTransformer,
|
3675
|
-
createChunkDecoder,
|
3676
3206
|
createEventStreamTransformer,
|
3677
3207
|
createStreamDataTransformer,
|
3678
3208
|
embed,
|
3679
3209
|
embedMany,
|
3680
3210
|
experimental_AssistantResponse,
|
3681
3211
|
experimental_StreamData,
|
3682
|
-
experimental_StreamingReactResponse,
|
3683
3212
|
experimental_generateObject,
|
3684
3213
|
experimental_generateText,
|
3685
3214
|
experimental_streamObject,
|
@@ -3688,8 +3217,8 @@ var StreamingTextResponse = class extends Response {
|
|
3688
3217
|
generateId,
|
3689
3218
|
generateObject,
|
3690
3219
|
generateText,
|
3691
|
-
isStreamStringEqualToType,
|
3692
3220
|
nanoid,
|
3221
|
+
parseComplexResponse,
|
3693
3222
|
parseStreamPart,
|
3694
3223
|
readDataStream,
|
3695
3224
|
readableFromAsyncIterable,
|