ai 3.1.30 → 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 +31 -508
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +33 -496
- 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 +8 -235
- 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";
|
@@ -1446,15 +1455,7 @@ function prepareResponseHeaders(init, { contentType }) {
|
|
1446
1455
|
|
1447
1456
|
// core/generate-text/run-tools-transformation.ts
|
1448
1457
|
import { NoSuchToolError as NoSuchToolError2 } from "@ai-sdk/provider";
|
1449
|
-
|
1450
|
-
// shared/generate-id.ts
|
1451
|
-
import { customAlphabet } from "nanoid/non-secure";
|
1452
|
-
var generateId = customAlphabet(
|
1453
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
1454
|
-
7
|
1455
|
-
);
|
1456
|
-
|
1457
|
-
// core/generate-text/run-tools-transformation.ts
|
1458
|
+
import { generateId } from "@ai-sdk/ui-utils";
|
1458
1459
|
function runToolsTransformation({
|
1459
1460
|
tools,
|
1460
1461
|
generatorStream
|
@@ -1998,282 +1999,6 @@ import {
|
|
1998
1999
|
UnsupportedJSONSchemaError
|
1999
2000
|
} from "@ai-sdk/provider";
|
2000
2001
|
|
2001
|
-
// shared/stream-parts.ts
|
2002
|
-
var textStreamPart = {
|
2003
|
-
code: "0",
|
2004
|
-
name: "text",
|
2005
|
-
parse: (value) => {
|
2006
|
-
if (typeof value !== "string") {
|
2007
|
-
throw new Error('"text" parts expect a string value.');
|
2008
|
-
}
|
2009
|
-
return { type: "text", value };
|
2010
|
-
}
|
2011
|
-
};
|
2012
|
-
var functionCallStreamPart = {
|
2013
|
-
code: "1",
|
2014
|
-
name: "function_call",
|
2015
|
-
parse: (value) => {
|
2016
|
-
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") {
|
2017
|
-
throw new Error(
|
2018
|
-
'"function_call" parts expect an object with a "function_call" property.'
|
2019
|
-
);
|
2020
|
-
}
|
2021
|
-
return {
|
2022
|
-
type: "function_call",
|
2023
|
-
value
|
2024
|
-
};
|
2025
|
-
}
|
2026
|
-
};
|
2027
|
-
var dataStreamPart = {
|
2028
|
-
code: "2",
|
2029
|
-
name: "data",
|
2030
|
-
parse: (value) => {
|
2031
|
-
if (!Array.isArray(value)) {
|
2032
|
-
throw new Error('"data" parts expect an array value.');
|
2033
|
-
}
|
2034
|
-
return { type: "data", value };
|
2035
|
-
}
|
2036
|
-
};
|
2037
|
-
var errorStreamPart = {
|
2038
|
-
code: "3",
|
2039
|
-
name: "error",
|
2040
|
-
parse: (value) => {
|
2041
|
-
if (typeof value !== "string") {
|
2042
|
-
throw new Error('"error" parts expect a string value.');
|
2043
|
-
}
|
2044
|
-
return { type: "error", value };
|
2045
|
-
}
|
2046
|
-
};
|
2047
|
-
var assistantMessageStreamPart = {
|
2048
|
-
code: "4",
|
2049
|
-
name: "assistant_message",
|
2050
|
-
parse: (value) => {
|
2051
|
-
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(
|
2052
|
-
(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"
|
2053
|
-
)) {
|
2054
|
-
throw new Error(
|
2055
|
-
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
2056
|
-
);
|
2057
|
-
}
|
2058
|
-
return {
|
2059
|
-
type: "assistant_message",
|
2060
|
-
value
|
2061
|
-
};
|
2062
|
-
}
|
2063
|
-
};
|
2064
|
-
var assistantControlDataStreamPart = {
|
2065
|
-
code: "5",
|
2066
|
-
name: "assistant_control_data",
|
2067
|
-
parse: (value) => {
|
2068
|
-
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
2069
|
-
throw new Error(
|
2070
|
-
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
2071
|
-
);
|
2072
|
-
}
|
2073
|
-
return {
|
2074
|
-
type: "assistant_control_data",
|
2075
|
-
value: {
|
2076
|
-
threadId: value.threadId,
|
2077
|
-
messageId: value.messageId
|
2078
|
-
}
|
2079
|
-
};
|
2080
|
-
}
|
2081
|
-
};
|
2082
|
-
var dataMessageStreamPart = {
|
2083
|
-
code: "6",
|
2084
|
-
name: "data_message",
|
2085
|
-
parse: (value) => {
|
2086
|
-
if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
|
2087
|
-
throw new Error(
|
2088
|
-
'"data_message" parts expect an object with a "role" and "data" property.'
|
2089
|
-
);
|
2090
|
-
}
|
2091
|
-
return {
|
2092
|
-
type: "data_message",
|
2093
|
-
value
|
2094
|
-
};
|
2095
|
-
}
|
2096
|
-
};
|
2097
|
-
var toolCallsStreamPart = {
|
2098
|
-
code: "7",
|
2099
|
-
name: "tool_calls",
|
2100
|
-
parse: (value) => {
|
2101
|
-
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(
|
2102
|
-
(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"
|
2103
|
-
)) {
|
2104
|
-
throw new Error(
|
2105
|
-
'"tool_calls" parts expect an object with a ToolCallPayload.'
|
2106
|
-
);
|
2107
|
-
}
|
2108
|
-
return {
|
2109
|
-
type: "tool_calls",
|
2110
|
-
value
|
2111
|
-
};
|
2112
|
-
}
|
2113
|
-
};
|
2114
|
-
var messageAnnotationsStreamPart = {
|
2115
|
-
code: "8",
|
2116
|
-
name: "message_annotations",
|
2117
|
-
parse: (value) => {
|
2118
|
-
if (!Array.isArray(value)) {
|
2119
|
-
throw new Error('"message_annotations" parts expect an array value.');
|
2120
|
-
}
|
2121
|
-
return { type: "message_annotations", value };
|
2122
|
-
}
|
2123
|
-
};
|
2124
|
-
var toolCallStreamPart = {
|
2125
|
-
code: "9",
|
2126
|
-
name: "tool_call",
|
2127
|
-
parse: (value) => {
|
2128
|
-
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") {
|
2129
|
-
throw new Error(
|
2130
|
-
'"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
|
2131
|
-
);
|
2132
|
-
}
|
2133
|
-
return {
|
2134
|
-
type: "tool_call",
|
2135
|
-
value
|
2136
|
-
};
|
2137
|
-
}
|
2138
|
-
};
|
2139
|
-
var toolResultStreamPart = {
|
2140
|
-
code: "a",
|
2141
|
-
name: "tool_result",
|
2142
|
-
parse: (value) => {
|
2143
|
-
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)) {
|
2144
|
-
throw new Error(
|
2145
|
-
'"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
|
2146
|
-
);
|
2147
|
-
}
|
2148
|
-
return {
|
2149
|
-
type: "tool_result",
|
2150
|
-
value
|
2151
|
-
};
|
2152
|
-
}
|
2153
|
-
};
|
2154
|
-
var streamParts = [
|
2155
|
-
textStreamPart,
|
2156
|
-
functionCallStreamPart,
|
2157
|
-
dataStreamPart,
|
2158
|
-
errorStreamPart,
|
2159
|
-
assistantMessageStreamPart,
|
2160
|
-
assistantControlDataStreamPart,
|
2161
|
-
dataMessageStreamPart,
|
2162
|
-
toolCallsStreamPart,
|
2163
|
-
messageAnnotationsStreamPart,
|
2164
|
-
toolCallStreamPart,
|
2165
|
-
toolResultStreamPart
|
2166
|
-
];
|
2167
|
-
var streamPartsByCode = {
|
2168
|
-
[textStreamPart.code]: textStreamPart,
|
2169
|
-
[functionCallStreamPart.code]: functionCallStreamPart,
|
2170
|
-
[dataStreamPart.code]: dataStreamPart,
|
2171
|
-
[errorStreamPart.code]: errorStreamPart,
|
2172
|
-
[assistantMessageStreamPart.code]: assistantMessageStreamPart,
|
2173
|
-
[assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
|
2174
|
-
[dataMessageStreamPart.code]: dataMessageStreamPart,
|
2175
|
-
[toolCallsStreamPart.code]: toolCallsStreamPart,
|
2176
|
-
[messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
|
2177
|
-
[toolCallStreamPart.code]: toolCallStreamPart,
|
2178
|
-
[toolResultStreamPart.code]: toolResultStreamPart
|
2179
|
-
};
|
2180
|
-
var StreamStringPrefixes = {
|
2181
|
-
[textStreamPart.name]: textStreamPart.code,
|
2182
|
-
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
2183
|
-
[dataStreamPart.name]: dataStreamPart.code,
|
2184
|
-
[errorStreamPart.name]: errorStreamPart.code,
|
2185
|
-
[assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
|
2186
|
-
[assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
|
2187
|
-
[dataMessageStreamPart.name]: dataMessageStreamPart.code,
|
2188
|
-
[toolCallsStreamPart.name]: toolCallsStreamPart.code,
|
2189
|
-
[messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
|
2190
|
-
[toolCallStreamPart.name]: toolCallStreamPart.code,
|
2191
|
-
[toolResultStreamPart.name]: toolResultStreamPart.code
|
2192
|
-
};
|
2193
|
-
var validCodes = streamParts.map((part) => part.code);
|
2194
|
-
var parseStreamPart = (line) => {
|
2195
|
-
const firstSeparatorIndex = line.indexOf(":");
|
2196
|
-
if (firstSeparatorIndex === -1) {
|
2197
|
-
throw new Error("Failed to parse stream string. No separator found.");
|
2198
|
-
}
|
2199
|
-
const prefix = line.slice(0, firstSeparatorIndex);
|
2200
|
-
if (!validCodes.includes(prefix)) {
|
2201
|
-
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
2202
|
-
}
|
2203
|
-
const code = prefix;
|
2204
|
-
const textValue = line.slice(firstSeparatorIndex + 1);
|
2205
|
-
const jsonValue = JSON.parse(textValue);
|
2206
|
-
return streamPartsByCode[code].parse(jsonValue);
|
2207
|
-
};
|
2208
|
-
function formatStreamPart(type, value) {
|
2209
|
-
const streamPart = streamParts.find((part) => part.name === type);
|
2210
|
-
if (!streamPart) {
|
2211
|
-
throw new Error(`Invalid stream part type: ${type}`);
|
2212
|
-
}
|
2213
|
-
return `${streamPart.code}:${JSON.stringify(value)}
|
2214
|
-
`;
|
2215
|
-
}
|
2216
|
-
|
2217
|
-
// shared/read-data-stream.ts
|
2218
|
-
var NEWLINE = "\n".charCodeAt(0);
|
2219
|
-
function concatChunks(chunks, totalLength) {
|
2220
|
-
const concatenatedChunks = new Uint8Array(totalLength);
|
2221
|
-
let offset = 0;
|
2222
|
-
for (const chunk of chunks) {
|
2223
|
-
concatenatedChunks.set(chunk, offset);
|
2224
|
-
offset += chunk.length;
|
2225
|
-
}
|
2226
|
-
chunks.length = 0;
|
2227
|
-
return concatenatedChunks;
|
2228
|
-
}
|
2229
|
-
async function* readDataStream(reader, {
|
2230
|
-
isAborted
|
2231
|
-
} = {}) {
|
2232
|
-
const decoder = new TextDecoder();
|
2233
|
-
const chunks = [];
|
2234
|
-
let totalLength = 0;
|
2235
|
-
while (true) {
|
2236
|
-
const { value } = await reader.read();
|
2237
|
-
if (value) {
|
2238
|
-
chunks.push(value);
|
2239
|
-
totalLength += value.length;
|
2240
|
-
if (value[value.length - 1] !== NEWLINE) {
|
2241
|
-
continue;
|
2242
|
-
}
|
2243
|
-
}
|
2244
|
-
if (chunks.length === 0) {
|
2245
|
-
break;
|
2246
|
-
}
|
2247
|
-
const concatenatedChunks = concatChunks(chunks, totalLength);
|
2248
|
-
totalLength = 0;
|
2249
|
-
const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
|
2250
|
-
for (const streamPart of streamParts2) {
|
2251
|
-
yield streamPart;
|
2252
|
-
}
|
2253
|
-
if (isAborted == null ? void 0 : isAborted()) {
|
2254
|
-
reader.cancel();
|
2255
|
-
break;
|
2256
|
-
}
|
2257
|
-
}
|
2258
|
-
}
|
2259
|
-
|
2260
|
-
// shared/utils.ts
|
2261
|
-
function createChunkDecoder(complex) {
|
2262
|
-
const decoder = new TextDecoder();
|
2263
|
-
if (!complex) {
|
2264
|
-
return function(chunk) {
|
2265
|
-
if (!chunk)
|
2266
|
-
return "";
|
2267
|
-
return decoder.decode(chunk, { stream: true });
|
2268
|
-
};
|
2269
|
-
}
|
2270
|
-
return function(chunk) {
|
2271
|
-
const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
|
2272
|
-
return decoded.map(parseStreamPart).filter(Boolean);
|
2273
|
-
};
|
2274
|
-
}
|
2275
|
-
var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
|
2276
|
-
|
2277
2002
|
// streams/ai-stream.ts
|
2278
2003
|
import {
|
2279
2004
|
createParser
|
@@ -2399,6 +2124,7 @@ function readableFromAsyncIterable(iterable) {
|
|
2399
2124
|
}
|
2400
2125
|
|
2401
2126
|
// streams/stream-data.ts
|
2127
|
+
import { formatStreamPart as formatStreamPart2 } from "@ai-sdk/ui-utils";
|
2402
2128
|
var StreamData = class {
|
2403
2129
|
constructor() {
|
2404
2130
|
this.encoder = new TextEncoder();
|
@@ -2445,7 +2171,7 @@ var StreamData = class {
|
|
2445
2171
|
throw new Error("Stream controller is not initialized.");
|
2446
2172
|
}
|
2447
2173
|
this.controller.enqueue(
|
2448
|
-
this.encoder.encode(
|
2174
|
+
this.encoder.encode(formatStreamPart2("data", [value]))
|
2449
2175
|
);
|
2450
2176
|
}
|
2451
2177
|
appendMessageAnnotation(value) {
|
@@ -2456,7 +2182,7 @@ var StreamData = class {
|
|
2456
2182
|
throw new Error("Stream controller is not initialized.");
|
2457
2183
|
}
|
2458
2184
|
this.controller.enqueue(
|
2459
|
-
this.encoder.encode(
|
2185
|
+
this.encoder.encode(formatStreamPart2("message_annotations", [value]))
|
2460
2186
|
);
|
2461
2187
|
}
|
2462
2188
|
};
|
@@ -2466,7 +2192,7 @@ function createStreamDataTransformer() {
|
|
2466
2192
|
return new TransformStream({
|
2467
2193
|
transform: async (chunk, controller) => {
|
2468
2194
|
const message = decoder.decode(chunk);
|
2469
|
-
controller.enqueue(encoder.encode(
|
2195
|
+
controller.enqueue(encoder.encode(formatStreamPart2("text", message)));
|
2470
2196
|
}
|
2471
2197
|
});
|
2472
2198
|
}
|
@@ -2520,6 +2246,9 @@ function AnthropicStream(res, cb) {
|
|
2520
2246
|
}
|
2521
2247
|
|
2522
2248
|
// streams/assistant-response.ts
|
2249
|
+
import {
|
2250
|
+
formatStreamPart as formatStreamPart3
|
2251
|
+
} from "@ai-sdk/ui-utils";
|
2523
2252
|
function AssistantResponse({ threadId, messageId }, process2) {
|
2524
2253
|
const stream = new ReadableStream({
|
2525
2254
|
async start(controller) {
|
@@ -2527,17 +2256,17 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2527
2256
|
const textEncoder = new TextEncoder();
|
2528
2257
|
const sendMessage = (message) => {
|
2529
2258
|
controller.enqueue(
|
2530
|
-
textEncoder.encode(
|
2259
|
+
textEncoder.encode(formatStreamPart3("assistant_message", message))
|
2531
2260
|
);
|
2532
2261
|
};
|
2533
2262
|
const sendDataMessage = (message) => {
|
2534
2263
|
controller.enqueue(
|
2535
|
-
textEncoder.encode(
|
2264
|
+
textEncoder.encode(formatStreamPart3("data_message", message))
|
2536
2265
|
);
|
2537
2266
|
};
|
2538
2267
|
const sendError = (errorMessage) => {
|
2539
2268
|
controller.enqueue(
|
2540
|
-
textEncoder.encode(
|
2269
|
+
textEncoder.encode(formatStreamPart3("error", errorMessage))
|
2541
2270
|
);
|
2542
2271
|
};
|
2543
2272
|
const forwardStream = async (stream2) => {
|
@@ -2548,7 +2277,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2548
2277
|
case "thread.message.created": {
|
2549
2278
|
controller.enqueue(
|
2550
2279
|
textEncoder.encode(
|
2551
|
-
|
2280
|
+
formatStreamPart3("assistant_message", {
|
2552
2281
|
id: value.data.id,
|
2553
2282
|
role: "assistant",
|
2554
2283
|
content: [{ type: "text", text: { value: "" } }]
|
@@ -2562,7 +2291,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2562
2291
|
if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
|
2563
2292
|
controller.enqueue(
|
2564
2293
|
textEncoder.encode(
|
2565
|
-
|
2294
|
+
formatStreamPart3("text", content.text.value)
|
2566
2295
|
)
|
2567
2296
|
);
|
2568
2297
|
}
|
@@ -2579,7 +2308,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
2579
2308
|
};
|
2580
2309
|
controller.enqueue(
|
2581
2310
|
textEncoder.encode(
|
2582
|
-
|
2311
|
+
formatStreamPart3("assistant_control_data", {
|
2583
2312
|
threadId,
|
2584
2313
|
messageId
|
2585
2314
|
})
|
@@ -2894,6 +2623,10 @@ function MistralStream(response, callbacks) {
|
|
2894
2623
|
}
|
2895
2624
|
|
2896
2625
|
// streams/openai-stream.ts
|
2626
|
+
import {
|
2627
|
+
createChunkDecoder,
|
2628
|
+
formatStreamPart as formatStreamPart4
|
2629
|
+
} from "@ai-sdk/ui-utils";
|
2897
2630
|
function parseOpenAIStream() {
|
2898
2631
|
const extract = chunkToText();
|
2899
2632
|
return (data) => extract(JSON.parse(data));
|
@@ -3058,7 +2791,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3058
2791
|
}
|
3059
2792
|
if (!isFunctionStreamingIn) {
|
3060
2793
|
controller.enqueue(
|
3061
|
-
textEncoder.encode(
|
2794
|
+
textEncoder.encode(formatStreamPart4("text", message))
|
3062
2795
|
);
|
3063
2796
|
return;
|
3064
2797
|
} else {
|
@@ -3169,7 +2902,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3169
2902
|
if (!functionResponse) {
|
3170
2903
|
controller.enqueue(
|
3171
2904
|
textEncoder.encode(
|
3172
|
-
|
2905
|
+
formatStreamPart4(
|
3173
2906
|
payload.function_call ? "function_call" : "tool_calls",
|
3174
2907
|
// parse to prevent double-encoding:
|
3175
2908
|
JSON.parse(aggregatedResponse)
|
@@ -3179,7 +2912,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
3179
2912
|
return;
|
3180
2913
|
} else if (typeof functionResponse === "string") {
|
3181
2914
|
controller.enqueue(
|
3182
|
-
textEncoder.encode(
|
2915
|
+
textEncoder.encode(formatStreamPart4("text", functionResponse))
|
3183
2916
|
);
|
3184
2917
|
aggregatedFinalCompletionResponse = functionResponse;
|
3185
2918
|
return;
|
@@ -3346,200 +3079,6 @@ function streamToResponse(res, response, init, data) {
|
|
3346
3079
|
read();
|
3347
3080
|
}
|
3348
3081
|
|
3349
|
-
// shared/parse-complex-response.ts
|
3350
|
-
function assignAnnotationsToMessage(message, annotations) {
|
3351
|
-
if (!message || !annotations || !annotations.length)
|
3352
|
-
return message;
|
3353
|
-
return { ...message, annotations: [...annotations] };
|
3354
|
-
}
|
3355
|
-
async function parseComplexResponse({
|
3356
|
-
reader,
|
3357
|
-
abortControllerRef,
|
3358
|
-
update,
|
3359
|
-
onToolCall,
|
3360
|
-
onFinish,
|
3361
|
-
generateId: generateId2 = generateId,
|
3362
|
-
getCurrentDate = () => /* @__PURE__ */ new Date()
|
3363
|
-
}) {
|
3364
|
-
const createdAt = getCurrentDate();
|
3365
|
-
const prefixMap = {
|
3366
|
-
data: []
|
3367
|
-
};
|
3368
|
-
let message_annotations = void 0;
|
3369
|
-
for await (const { type, value } of readDataStream(reader, {
|
3370
|
-
isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
|
3371
|
-
})) {
|
3372
|
-
if (type === "text") {
|
3373
|
-
if (prefixMap["text"]) {
|
3374
|
-
prefixMap["text"] = {
|
3375
|
-
...prefixMap["text"],
|
3376
|
-
content: (prefixMap["text"].content || "") + value
|
3377
|
-
};
|
3378
|
-
} else {
|
3379
|
-
prefixMap["text"] = {
|
3380
|
-
id: generateId2(),
|
3381
|
-
role: "assistant",
|
3382
|
-
content: value,
|
3383
|
-
createdAt
|
3384
|
-
};
|
3385
|
-
}
|
3386
|
-
}
|
3387
|
-
if (type === "tool_call") {
|
3388
|
-
if (prefixMap.text == null) {
|
3389
|
-
prefixMap.text = {
|
3390
|
-
id: generateId2(),
|
3391
|
-
role: "assistant",
|
3392
|
-
content: "",
|
3393
|
-
createdAt
|
3394
|
-
};
|
3395
|
-
}
|
3396
|
-
if (prefixMap.text.toolInvocations == null) {
|
3397
|
-
prefixMap.text.toolInvocations = [];
|
3398
|
-
}
|
3399
|
-
prefixMap.text.toolInvocations.push(value);
|
3400
|
-
if (onToolCall) {
|
3401
|
-
const result = await onToolCall({ toolCall: value });
|
3402
|
-
if (result != null) {
|
3403
|
-
prefixMap.text.toolInvocations[prefixMap.text.toolInvocations.length - 1] = { ...value, result };
|
3404
|
-
}
|
3405
|
-
}
|
3406
|
-
} else if (type === "tool_result") {
|
3407
|
-
if (prefixMap.text == null) {
|
3408
|
-
prefixMap.text = {
|
3409
|
-
id: generateId2(),
|
3410
|
-
role: "assistant",
|
3411
|
-
content: "",
|
3412
|
-
createdAt
|
3413
|
-
};
|
3414
|
-
}
|
3415
|
-
if (prefixMap.text.toolInvocations == null) {
|
3416
|
-
prefixMap.text.toolInvocations = [];
|
3417
|
-
}
|
3418
|
-
const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
|
3419
|
-
(invocation) => invocation.toolCallId === value.toolCallId
|
3420
|
-
);
|
3421
|
-
if (toolInvocationIndex !== -1) {
|
3422
|
-
prefixMap.text.toolInvocations[toolInvocationIndex] = value;
|
3423
|
-
} else {
|
3424
|
-
prefixMap.text.toolInvocations.push(value);
|
3425
|
-
}
|
3426
|
-
}
|
3427
|
-
let functionCallMessage = null;
|
3428
|
-
if (type === "function_call") {
|
3429
|
-
prefixMap["function_call"] = {
|
3430
|
-
id: generateId2(),
|
3431
|
-
role: "assistant",
|
3432
|
-
content: "",
|
3433
|
-
function_call: value.function_call,
|
3434
|
-
name: value.function_call.name,
|
3435
|
-
createdAt
|
3436
|
-
};
|
3437
|
-
functionCallMessage = prefixMap["function_call"];
|
3438
|
-
}
|
3439
|
-
let toolCallMessage = null;
|
3440
|
-
if (type === "tool_calls") {
|
3441
|
-
prefixMap["tool_calls"] = {
|
3442
|
-
id: generateId2(),
|
3443
|
-
role: "assistant",
|
3444
|
-
content: "",
|
3445
|
-
tool_calls: value.tool_calls,
|
3446
|
-
createdAt
|
3447
|
-
};
|
3448
|
-
toolCallMessage = prefixMap["tool_calls"];
|
3449
|
-
}
|
3450
|
-
if (type === "data") {
|
3451
|
-
prefixMap["data"].push(...value);
|
3452
|
-
}
|
3453
|
-
let responseMessage = prefixMap["text"];
|
3454
|
-
if (type === "message_annotations") {
|
3455
|
-
if (!message_annotations) {
|
3456
|
-
message_annotations = [...value];
|
3457
|
-
} else {
|
3458
|
-
message_annotations.push(...value);
|
3459
|
-
}
|
3460
|
-
functionCallMessage = assignAnnotationsToMessage(
|
3461
|
-
prefixMap["function_call"],
|
3462
|
-
message_annotations
|
3463
|
-
);
|
3464
|
-
toolCallMessage = assignAnnotationsToMessage(
|
3465
|
-
prefixMap["tool_calls"],
|
3466
|
-
message_annotations
|
3467
|
-
);
|
3468
|
-
responseMessage = assignAnnotationsToMessage(
|
3469
|
-
prefixMap["text"],
|
3470
|
-
message_annotations
|
3471
|
-
);
|
3472
|
-
}
|
3473
|
-
if (message_annotations == null ? void 0 : message_annotations.length) {
|
3474
|
-
const messagePrefixKeys = [
|
3475
|
-
"text",
|
3476
|
-
"function_call",
|
3477
|
-
"tool_calls"
|
3478
|
-
];
|
3479
|
-
messagePrefixKeys.forEach((key) => {
|
3480
|
-
if (prefixMap[key]) {
|
3481
|
-
prefixMap[key].annotations = [...message_annotations];
|
3482
|
-
}
|
3483
|
-
});
|
3484
|
-
}
|
3485
|
-
const merged = [functionCallMessage, toolCallMessage, responseMessage].filter(Boolean).map((message) => ({
|
3486
|
-
...assignAnnotationsToMessage(message, message_annotations)
|
3487
|
-
}));
|
3488
|
-
update(merged, [...prefixMap["data"]]);
|
3489
|
-
}
|
3490
|
-
onFinish == null ? void 0 : onFinish(prefixMap);
|
3491
|
-
return {
|
3492
|
-
messages: [
|
3493
|
-
prefixMap.text,
|
3494
|
-
prefixMap.function_call,
|
3495
|
-
prefixMap.tool_calls
|
3496
|
-
].filter(Boolean),
|
3497
|
-
data: prefixMap.data
|
3498
|
-
};
|
3499
|
-
}
|
3500
|
-
|
3501
|
-
// streams/streaming-react-response.ts
|
3502
|
-
var experimental_StreamingReactResponse = class {
|
3503
|
-
constructor(res, options) {
|
3504
|
-
var _a, _b;
|
3505
|
-
let resolveFunc = () => {
|
3506
|
-
};
|
3507
|
-
let next = new Promise((resolve) => {
|
3508
|
-
resolveFunc = resolve;
|
3509
|
-
});
|
3510
|
-
const processedStream = (options == null ? void 0 : options.data) != null ? mergeStreams((_a = options == null ? void 0 : options.data) == null ? void 0 : _a.stream, res) : res;
|
3511
|
-
let lastPayload = void 0;
|
3512
|
-
parseComplexResponse({
|
3513
|
-
reader: processedStream.getReader(),
|
3514
|
-
update: (merged, data) => {
|
3515
|
-
var _a2, _b2, _c;
|
3516
|
-
const content = (_b2 = (_a2 = merged[0]) == null ? void 0 : _a2.content) != null ? _b2 : "";
|
3517
|
-
const ui = ((_c = options == null ? void 0 : options.ui) == null ? void 0 : _c.call(options, { content, data })) || content;
|
3518
|
-
const payload = { ui, content };
|
3519
|
-
const resolvePrevious = resolveFunc;
|
3520
|
-
const nextRow = new Promise((resolve) => {
|
3521
|
-
resolveFunc = resolve;
|
3522
|
-
});
|
3523
|
-
resolvePrevious({
|
3524
|
-
next: nextRow,
|
3525
|
-
...payload
|
3526
|
-
});
|
3527
|
-
lastPayload = payload;
|
3528
|
-
},
|
3529
|
-
generateId: (_b = options == null ? void 0 : options.generateId) != null ? _b : generateId,
|
3530
|
-
onFinish: () => {
|
3531
|
-
if (lastPayload !== void 0) {
|
3532
|
-
resolveFunc({
|
3533
|
-
next: null,
|
3534
|
-
...lastPayload
|
3535
|
-
});
|
3536
|
-
}
|
3537
|
-
}
|
3538
|
-
});
|
3539
|
-
return next;
|
3540
|
-
}
|
3541
|
-
};
|
3542
|
-
|
3543
3082
|
// streams/streaming-text-response.ts
|
3544
3083
|
var StreamingTextResponse = class extends Response {
|
3545
3084
|
constructor(res, init, data) {
|
@@ -3602,24 +3141,22 @@ export {
|
|
3602
3141
|
convertDataContentToUint8Array,
|
3603
3142
|
convertToCoreMessages,
|
3604
3143
|
createCallbacksTransformer,
|
3605
|
-
createChunkDecoder,
|
3606
3144
|
createEventStreamTransformer,
|
3607
3145
|
createStreamDataTransformer,
|
3608
3146
|
embed,
|
3609
3147
|
embedMany,
|
3610
3148
|
experimental_AssistantResponse,
|
3611
3149
|
experimental_StreamData,
|
3612
|
-
experimental_StreamingReactResponse,
|
3613
3150
|
experimental_generateObject,
|
3614
3151
|
experimental_generateText,
|
3615
3152
|
experimental_streamObject,
|
3616
3153
|
experimental_streamText,
|
3617
3154
|
formatStreamPart,
|
3618
|
-
generateId,
|
3155
|
+
generateId2 as generateId,
|
3619
3156
|
generateObject,
|
3620
3157
|
generateText,
|
3621
|
-
|
3622
|
-
|
3158
|
+
generateId3 as nanoid,
|
3159
|
+
parseComplexResponse,
|
3623
3160
|
parseStreamPart,
|
3624
3161
|
readDataStream,
|
3625
3162
|
readableFromAsyncIterable,
|