ai 3.1.30 → 3.1.32

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.
Files changed (40) hide show
  1. package/dist/index.d.mts +4 -563
  2. package/dist/index.d.ts +4 -563
  3. package/dist/index.js +31 -508
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +33 -496
  6. package/dist/index.mjs.map +1 -1
  7. package/package.json +19 -34
  8. package/prompts/dist/index.d.mts +2 -129
  9. package/prompts/dist/index.d.ts +2 -129
  10. package/prompts/dist/index.js.map +1 -1
  11. package/prompts/dist/index.mjs.map +1 -1
  12. package/react/dist/index.d.mts +9 -648
  13. package/react/dist/index.d.ts +9 -648
  14. package/react/dist/index.js +4 -1441
  15. package/react/dist/index.js.map +1 -1
  16. package/react/dist/index.mjs +9 -1429
  17. package/react/dist/index.mjs.map +1 -1
  18. package/rsc/dist/rsc-server.mjs +44 -298
  19. package/rsc/dist/rsc-server.mjs.map +1 -1
  20. package/rsc/dist/rsc-shared.d.mts +5 -1
  21. package/rsc/dist/rsc-shared.mjs +69 -44
  22. package/rsc/dist/rsc-shared.mjs.map +1 -1
  23. package/solid/dist/index.d.mts +7 -465
  24. package/solid/dist/index.d.ts +7 -465
  25. package/solid/dist/index.js +3 -1057
  26. package/solid/dist/index.js.map +1 -1
  27. package/solid/dist/index.mjs +7 -1056
  28. package/solid/dist/index.mjs.map +1 -1
  29. package/svelte/dist/index.d.mts +12 -422
  30. package/svelte/dist/index.d.ts +12 -422
  31. package/svelte/dist/index.js +12 -768
  32. package/svelte/dist/index.js.map +1 -1
  33. package/svelte/dist/index.mjs +10 -762
  34. package/svelte/dist/index.mjs.map +1 -1
  35. package/vue/dist/index.d.mts +7 -459
  36. package/vue/dist/index.d.ts +7 -459
  37. package/vue/dist/index.js +3 -1057
  38. package/vue/dist/index.js.map +1 -1
  39. package/vue/dist/index.mjs +7 -1046
  40. 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
- isStreamStringEqualToType: () => isStreamStringEqualToType,
95
- nanoid: () => generateId,
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");
@@ -1540,15 +1540,7 @@ function prepareResponseHeaders(init, { contentType }) {
1540
1540
 
1541
1541
  // core/generate-text/run-tools-transformation.ts
1542
1542
  var import_provider7 = require("@ai-sdk/provider");
1543
-
1544
- // shared/generate-id.ts
1545
- var import_non_secure = require("nanoid/non-secure");
1546
- var generateId = (0, import_non_secure.customAlphabet)(
1547
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1548
- 7
1549
- );
1550
-
1551
- // core/generate-text/run-tools-transformation.ts
1543
+ var import_ui_utils = require("@ai-sdk/ui-utils");
1552
1544
  function runToolsTransformation({
1553
1545
  tools,
1554
1546
  generatorStream
@@ -1597,7 +1589,7 @@ function runToolsTransformation({
1597
1589
  });
1598
1590
  controller.enqueue(toolCall);
1599
1591
  if (tool2.execute != null) {
1600
- const toolExecutionId = generateId();
1592
+ const toolExecutionId = (0, import_ui_utils.generateId)();
1601
1593
  outstandingToolCalls.add(toolExecutionId);
1602
1594
  tool2.execute(toolCall.args).then(
1603
1595
  (result) => {
@@ -1895,11 +1887,11 @@ var StreamTextResult = class {
1895
1887
  transform: async (chunk, controller) => {
1896
1888
  switch (chunk.type) {
1897
1889
  case "text-delta":
1898
- controller.enqueue(formatStreamPart("text", chunk.textDelta));
1890
+ controller.enqueue((0, import_ui_utils5.formatStreamPart)("text", chunk.textDelta));
1899
1891
  break;
1900
1892
  case "tool-call":
1901
1893
  controller.enqueue(
1902
- formatStreamPart("tool_call", {
1894
+ (0, import_ui_utils5.formatStreamPart)("tool_call", {
1903
1895
  toolCallId: chunk.toolCallId,
1904
1896
  toolName: chunk.toolName,
1905
1897
  args: chunk.args
@@ -1908,7 +1900,7 @@ var StreamTextResult = class {
1908
1900
  break;
1909
1901
  case "tool-result":
1910
1902
  controller.enqueue(
1911
- formatStreamPart("tool_result", {
1903
+ (0, import_ui_utils5.formatStreamPart)("tool_result", {
1912
1904
  toolCallId: chunk.toolCallId,
1913
1905
  toolName: chunk.toolName,
1914
1906
  args: chunk.args,
@@ -1918,7 +1910,7 @@ var StreamTextResult = class {
1918
1910
  break;
1919
1911
  case "error":
1920
1912
  controller.enqueue(
1921
- formatStreamPart("error", JSON.stringify(chunk.error))
1913
+ (0, import_ui_utils5.formatStreamPart)("error", JSON.stringify(chunk.error))
1922
1914
  );
1923
1915
  break;
1924
1916
  }
@@ -2075,282 +2067,6 @@ function tool(tool2) {
2075
2067
  // core/types/errors.ts
2076
2068
  var import_provider8 = require("@ai-sdk/provider");
2077
2069
 
2078
- // shared/stream-parts.ts
2079
- var textStreamPart = {
2080
- code: "0",
2081
- name: "text",
2082
- parse: (value) => {
2083
- if (typeof value !== "string") {
2084
- throw new Error('"text" parts expect a string value.');
2085
- }
2086
- return { type: "text", value };
2087
- }
2088
- };
2089
- var functionCallStreamPart = {
2090
- code: "1",
2091
- name: "function_call",
2092
- parse: (value) => {
2093
- 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") {
2094
- throw new Error(
2095
- '"function_call" parts expect an object with a "function_call" property.'
2096
- );
2097
- }
2098
- return {
2099
- type: "function_call",
2100
- value
2101
- };
2102
- }
2103
- };
2104
- var dataStreamPart = {
2105
- code: "2",
2106
- name: "data",
2107
- parse: (value) => {
2108
- if (!Array.isArray(value)) {
2109
- throw new Error('"data" parts expect an array value.');
2110
- }
2111
- return { type: "data", value };
2112
- }
2113
- };
2114
- var errorStreamPart = {
2115
- code: "3",
2116
- name: "error",
2117
- parse: (value) => {
2118
- if (typeof value !== "string") {
2119
- throw new Error('"error" parts expect a string value.');
2120
- }
2121
- return { type: "error", value };
2122
- }
2123
- };
2124
- var assistantMessageStreamPart = {
2125
- code: "4",
2126
- name: "assistant_message",
2127
- parse: (value) => {
2128
- 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(
2129
- (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"
2130
- )) {
2131
- throw new Error(
2132
- '"assistant_message" parts expect an object with an "id", "role", and "content" property.'
2133
- );
2134
- }
2135
- return {
2136
- type: "assistant_message",
2137
- value
2138
- };
2139
- }
2140
- };
2141
- var assistantControlDataStreamPart = {
2142
- code: "5",
2143
- name: "assistant_control_data",
2144
- parse: (value) => {
2145
- if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
2146
- throw new Error(
2147
- '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
2148
- );
2149
- }
2150
- return {
2151
- type: "assistant_control_data",
2152
- value: {
2153
- threadId: value.threadId,
2154
- messageId: value.messageId
2155
- }
2156
- };
2157
- }
2158
- };
2159
- var dataMessageStreamPart = {
2160
- code: "6",
2161
- name: "data_message",
2162
- parse: (value) => {
2163
- if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
2164
- throw new Error(
2165
- '"data_message" parts expect an object with a "role" and "data" property.'
2166
- );
2167
- }
2168
- return {
2169
- type: "data_message",
2170
- value
2171
- };
2172
- }
2173
- };
2174
- var toolCallsStreamPart = {
2175
- code: "7",
2176
- name: "tool_calls",
2177
- parse: (value) => {
2178
- 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(
2179
- (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"
2180
- )) {
2181
- throw new Error(
2182
- '"tool_calls" parts expect an object with a ToolCallPayload.'
2183
- );
2184
- }
2185
- return {
2186
- type: "tool_calls",
2187
- value
2188
- };
2189
- }
2190
- };
2191
- var messageAnnotationsStreamPart = {
2192
- code: "8",
2193
- name: "message_annotations",
2194
- parse: (value) => {
2195
- if (!Array.isArray(value)) {
2196
- throw new Error('"message_annotations" parts expect an array value.');
2197
- }
2198
- return { type: "message_annotations", value };
2199
- }
2200
- };
2201
- var toolCallStreamPart = {
2202
- code: "9",
2203
- name: "tool_call",
2204
- parse: (value) => {
2205
- 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") {
2206
- throw new Error(
2207
- '"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
2208
- );
2209
- }
2210
- return {
2211
- type: "tool_call",
2212
- value
2213
- };
2214
- }
2215
- };
2216
- var toolResultStreamPart = {
2217
- code: "a",
2218
- name: "tool_result",
2219
- parse: (value) => {
2220
- 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)) {
2221
- throw new Error(
2222
- '"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
2223
- );
2224
- }
2225
- return {
2226
- type: "tool_result",
2227
- value
2228
- };
2229
- }
2230
- };
2231
- var streamParts = [
2232
- textStreamPart,
2233
- functionCallStreamPart,
2234
- dataStreamPart,
2235
- errorStreamPart,
2236
- assistantMessageStreamPart,
2237
- assistantControlDataStreamPart,
2238
- dataMessageStreamPart,
2239
- toolCallsStreamPart,
2240
- messageAnnotationsStreamPart,
2241
- toolCallStreamPart,
2242
- toolResultStreamPart
2243
- ];
2244
- var streamPartsByCode = {
2245
- [textStreamPart.code]: textStreamPart,
2246
- [functionCallStreamPart.code]: functionCallStreamPart,
2247
- [dataStreamPart.code]: dataStreamPart,
2248
- [errorStreamPart.code]: errorStreamPart,
2249
- [assistantMessageStreamPart.code]: assistantMessageStreamPart,
2250
- [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
2251
- [dataMessageStreamPart.code]: dataMessageStreamPart,
2252
- [toolCallsStreamPart.code]: toolCallsStreamPart,
2253
- [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
2254
- [toolCallStreamPart.code]: toolCallStreamPart,
2255
- [toolResultStreamPart.code]: toolResultStreamPart
2256
- };
2257
- var StreamStringPrefixes = {
2258
- [textStreamPart.name]: textStreamPart.code,
2259
- [functionCallStreamPart.name]: functionCallStreamPart.code,
2260
- [dataStreamPart.name]: dataStreamPart.code,
2261
- [errorStreamPart.name]: errorStreamPart.code,
2262
- [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
2263
- [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
2264
- [dataMessageStreamPart.name]: dataMessageStreamPart.code,
2265
- [toolCallsStreamPart.name]: toolCallsStreamPart.code,
2266
- [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
2267
- [toolCallStreamPart.name]: toolCallStreamPart.code,
2268
- [toolResultStreamPart.name]: toolResultStreamPart.code
2269
- };
2270
- var validCodes = streamParts.map((part) => part.code);
2271
- var parseStreamPart = (line) => {
2272
- const firstSeparatorIndex = line.indexOf(":");
2273
- if (firstSeparatorIndex === -1) {
2274
- throw new Error("Failed to parse stream string. No separator found.");
2275
- }
2276
- const prefix = line.slice(0, firstSeparatorIndex);
2277
- if (!validCodes.includes(prefix)) {
2278
- throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
2279
- }
2280
- const code = prefix;
2281
- const textValue = line.slice(firstSeparatorIndex + 1);
2282
- const jsonValue = JSON.parse(textValue);
2283
- return streamPartsByCode[code].parse(jsonValue);
2284
- };
2285
- function formatStreamPart(type, value) {
2286
- const streamPart = streamParts.find((part) => part.name === type);
2287
- if (!streamPart) {
2288
- throw new Error(`Invalid stream part type: ${type}`);
2289
- }
2290
- return `${streamPart.code}:${JSON.stringify(value)}
2291
- `;
2292
- }
2293
-
2294
- // shared/read-data-stream.ts
2295
- var NEWLINE = "\n".charCodeAt(0);
2296
- function concatChunks(chunks, totalLength) {
2297
- const concatenatedChunks = new Uint8Array(totalLength);
2298
- let offset = 0;
2299
- for (const chunk of chunks) {
2300
- concatenatedChunks.set(chunk, offset);
2301
- offset += chunk.length;
2302
- }
2303
- chunks.length = 0;
2304
- return concatenatedChunks;
2305
- }
2306
- async function* readDataStream(reader, {
2307
- isAborted
2308
- } = {}) {
2309
- const decoder = new TextDecoder();
2310
- const chunks = [];
2311
- let totalLength = 0;
2312
- while (true) {
2313
- const { value } = await reader.read();
2314
- if (value) {
2315
- chunks.push(value);
2316
- totalLength += value.length;
2317
- if (value[value.length - 1] !== NEWLINE) {
2318
- continue;
2319
- }
2320
- }
2321
- if (chunks.length === 0) {
2322
- break;
2323
- }
2324
- const concatenatedChunks = concatChunks(chunks, totalLength);
2325
- totalLength = 0;
2326
- const streamParts2 = decoder.decode(concatenatedChunks, { stream: true }).split("\n").filter((line) => line !== "").map(parseStreamPart);
2327
- for (const streamPart of streamParts2) {
2328
- yield streamPart;
2329
- }
2330
- if (isAborted == null ? void 0 : isAborted()) {
2331
- reader.cancel();
2332
- break;
2333
- }
2334
- }
2335
- }
2336
-
2337
- // shared/utils.ts
2338
- function createChunkDecoder(complex) {
2339
- const decoder = new TextDecoder();
2340
- if (!complex) {
2341
- return function(chunk) {
2342
- if (!chunk)
2343
- return "";
2344
- return decoder.decode(chunk, { stream: true });
2345
- };
2346
- }
2347
- return function(chunk) {
2348
- const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
2349
- return decoded.map(parseStreamPart).filter(Boolean);
2350
- };
2351
- }
2352
- var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
2353
-
2354
2070
  // streams/ai-stream.ts
2355
2071
  var import_eventsource_parser = require("eventsource-parser");
2356
2072
  function createEventStreamTransformer(customParser) {
@@ -2474,6 +2190,7 @@ function readableFromAsyncIterable(iterable) {
2474
2190
  }
2475
2191
 
2476
2192
  // streams/stream-data.ts
2193
+ var import_ui_utils2 = require("@ai-sdk/ui-utils");
2477
2194
  var StreamData = class {
2478
2195
  constructor() {
2479
2196
  this.encoder = new TextEncoder();
@@ -2520,7 +2237,7 @@ var StreamData = class {
2520
2237
  throw new Error("Stream controller is not initialized.");
2521
2238
  }
2522
2239
  this.controller.enqueue(
2523
- this.encoder.encode(formatStreamPart("data", [value]))
2240
+ this.encoder.encode((0, import_ui_utils2.formatStreamPart)("data", [value]))
2524
2241
  );
2525
2242
  }
2526
2243
  appendMessageAnnotation(value) {
@@ -2531,7 +2248,7 @@ var StreamData = class {
2531
2248
  throw new Error("Stream controller is not initialized.");
2532
2249
  }
2533
2250
  this.controller.enqueue(
2534
- this.encoder.encode(formatStreamPart("message_annotations", [value]))
2251
+ this.encoder.encode((0, import_ui_utils2.formatStreamPart)("message_annotations", [value]))
2535
2252
  );
2536
2253
  }
2537
2254
  };
@@ -2541,7 +2258,7 @@ function createStreamDataTransformer() {
2541
2258
  return new TransformStream({
2542
2259
  transform: async (chunk, controller) => {
2543
2260
  const message = decoder.decode(chunk);
2544
- controller.enqueue(encoder.encode(formatStreamPart("text", message)));
2261
+ controller.enqueue(encoder.encode((0, import_ui_utils2.formatStreamPart)("text", message)));
2545
2262
  }
2546
2263
  });
2547
2264
  }
@@ -2595,6 +2312,7 @@ function AnthropicStream(res, cb) {
2595
2312
  }
2596
2313
 
2597
2314
  // streams/assistant-response.ts
2315
+ var import_ui_utils3 = require("@ai-sdk/ui-utils");
2598
2316
  function AssistantResponse({ threadId, messageId }, process2) {
2599
2317
  const stream = new ReadableStream({
2600
2318
  async start(controller) {
@@ -2602,17 +2320,17 @@ function AssistantResponse({ threadId, messageId }, process2) {
2602
2320
  const textEncoder = new TextEncoder();
2603
2321
  const sendMessage = (message) => {
2604
2322
  controller.enqueue(
2605
- textEncoder.encode(formatStreamPart("assistant_message", message))
2323
+ textEncoder.encode((0, import_ui_utils3.formatStreamPart)("assistant_message", message))
2606
2324
  );
2607
2325
  };
2608
2326
  const sendDataMessage = (message) => {
2609
2327
  controller.enqueue(
2610
- textEncoder.encode(formatStreamPart("data_message", message))
2328
+ textEncoder.encode((0, import_ui_utils3.formatStreamPart)("data_message", message))
2611
2329
  );
2612
2330
  };
2613
2331
  const sendError = (errorMessage) => {
2614
2332
  controller.enqueue(
2615
- textEncoder.encode(formatStreamPart("error", errorMessage))
2333
+ textEncoder.encode((0, import_ui_utils3.formatStreamPart)("error", errorMessage))
2616
2334
  );
2617
2335
  };
2618
2336
  const forwardStream = async (stream2) => {
@@ -2623,7 +2341,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
2623
2341
  case "thread.message.created": {
2624
2342
  controller.enqueue(
2625
2343
  textEncoder.encode(
2626
- formatStreamPart("assistant_message", {
2344
+ (0, import_ui_utils3.formatStreamPart)("assistant_message", {
2627
2345
  id: value.data.id,
2628
2346
  role: "assistant",
2629
2347
  content: [{ type: "text", text: { value: "" } }]
@@ -2637,7 +2355,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
2637
2355
  if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
2638
2356
  controller.enqueue(
2639
2357
  textEncoder.encode(
2640
- formatStreamPart("text", content.text.value)
2358
+ (0, import_ui_utils3.formatStreamPart)("text", content.text.value)
2641
2359
  )
2642
2360
  );
2643
2361
  }
@@ -2654,7 +2372,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
2654
2372
  };
2655
2373
  controller.enqueue(
2656
2374
  textEncoder.encode(
2657
- formatStreamPart("assistant_control_data", {
2375
+ (0, import_ui_utils3.formatStreamPart)("assistant_control_data", {
2658
2376
  threadId,
2659
2377
  messageId
2660
2378
  })
@@ -2969,6 +2687,7 @@ function MistralStream(response, callbacks) {
2969
2687
  }
2970
2688
 
2971
2689
  // streams/openai-stream.ts
2690
+ var import_ui_utils4 = require("@ai-sdk/ui-utils");
2972
2691
  function parseOpenAIStream() {
2973
2692
  const extract = chunkToText();
2974
2693
  return (data) => extract(JSON.parse(data));
@@ -3119,7 +2838,7 @@ function createFunctionCallTransformer(callbacks) {
3119
2838
  let aggregatedFinalCompletionResponse = "";
3120
2839
  let isFunctionStreamingIn = false;
3121
2840
  let functionCallMessages = callbacks[__internal__OpenAIFnMessagesSymbol] || [];
3122
- const decode = createChunkDecoder();
2841
+ const decode = (0, import_ui_utils4.createChunkDecoder)();
3123
2842
  return new TransformStream({
3124
2843
  async transform(chunk, controller) {
3125
2844
  const message = decode(chunk);
@@ -3133,7 +2852,7 @@ function createFunctionCallTransformer(callbacks) {
3133
2852
  }
3134
2853
  if (!isFunctionStreamingIn) {
3135
2854
  controller.enqueue(
3136
- textEncoder.encode(formatStreamPart("text", message))
2855
+ textEncoder.encode((0, import_ui_utils4.formatStreamPart)("text", message))
3137
2856
  );
3138
2857
  return;
3139
2858
  } else {
@@ -3244,7 +2963,7 @@ function createFunctionCallTransformer(callbacks) {
3244
2963
  if (!functionResponse) {
3245
2964
  controller.enqueue(
3246
2965
  textEncoder.encode(
3247
- formatStreamPart(
2966
+ (0, import_ui_utils4.formatStreamPart)(
3248
2967
  payload.function_call ? "function_call" : "tool_calls",
3249
2968
  // parse to prevent double-encoding:
3250
2969
  JSON.parse(aggregatedResponse)
@@ -3254,7 +2973,7 @@ function createFunctionCallTransformer(callbacks) {
3254
2973
  return;
3255
2974
  } else if (typeof functionResponse === "string") {
3256
2975
  controller.enqueue(
3257
- textEncoder.encode(formatStreamPart("text", functionResponse))
2976
+ textEncoder.encode((0, import_ui_utils4.formatStreamPart)("text", functionResponse))
3258
2977
  );
3259
2978
  aggregatedFinalCompletionResponse = functionResponse;
3260
2979
  return;
@@ -3421,200 +3140,6 @@ function streamToResponse(res, response, init, data) {
3421
3140
  read();
3422
3141
  }
3423
3142
 
3424
- // shared/parse-complex-response.ts
3425
- function assignAnnotationsToMessage(message, annotations) {
3426
- if (!message || !annotations || !annotations.length)
3427
- return message;
3428
- return { ...message, annotations: [...annotations] };
3429
- }
3430
- async function parseComplexResponse({
3431
- reader,
3432
- abortControllerRef,
3433
- update,
3434
- onToolCall,
3435
- onFinish,
3436
- generateId: generateId2 = generateId,
3437
- getCurrentDate = () => /* @__PURE__ */ new Date()
3438
- }) {
3439
- const createdAt = getCurrentDate();
3440
- const prefixMap = {
3441
- data: []
3442
- };
3443
- let message_annotations = void 0;
3444
- for await (const { type, value } of readDataStream(reader, {
3445
- isAborted: () => (abortControllerRef == null ? void 0 : abortControllerRef.current) === null
3446
- })) {
3447
- if (type === "text") {
3448
- if (prefixMap["text"]) {
3449
- prefixMap["text"] = {
3450
- ...prefixMap["text"],
3451
- content: (prefixMap["text"].content || "") + value
3452
- };
3453
- } else {
3454
- prefixMap["text"] = {
3455
- id: generateId2(),
3456
- role: "assistant",
3457
- content: value,
3458
- createdAt
3459
- };
3460
- }
3461
- }
3462
- if (type === "tool_call") {
3463
- if (prefixMap.text == null) {
3464
- prefixMap.text = {
3465
- id: generateId2(),
3466
- role: "assistant",
3467
- content: "",
3468
- createdAt
3469
- };
3470
- }
3471
- if (prefixMap.text.toolInvocations == null) {
3472
- prefixMap.text.toolInvocations = [];
3473
- }
3474
- prefixMap.text.toolInvocations.push(value);
3475
- if (onToolCall) {
3476
- const result = await onToolCall({ toolCall: value });
3477
- if (result != null) {
3478
- prefixMap.text.toolInvocations[prefixMap.text.toolInvocations.length - 1] = { ...value, result };
3479
- }
3480
- }
3481
- } else if (type === "tool_result") {
3482
- if (prefixMap.text == null) {
3483
- prefixMap.text = {
3484
- id: generateId2(),
3485
- role: "assistant",
3486
- content: "",
3487
- createdAt
3488
- };
3489
- }
3490
- if (prefixMap.text.toolInvocations == null) {
3491
- prefixMap.text.toolInvocations = [];
3492
- }
3493
- const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
3494
- (invocation) => invocation.toolCallId === value.toolCallId
3495
- );
3496
- if (toolInvocationIndex !== -1) {
3497
- prefixMap.text.toolInvocations[toolInvocationIndex] = value;
3498
- } else {
3499
- prefixMap.text.toolInvocations.push(value);
3500
- }
3501
- }
3502
- let functionCallMessage = null;
3503
- if (type === "function_call") {
3504
- prefixMap["function_call"] = {
3505
- id: generateId2(),
3506
- role: "assistant",
3507
- content: "",
3508
- function_call: value.function_call,
3509
- name: value.function_call.name,
3510
- createdAt
3511
- };
3512
- functionCallMessage = prefixMap["function_call"];
3513
- }
3514
- let toolCallMessage = null;
3515
- if (type === "tool_calls") {
3516
- prefixMap["tool_calls"] = {
3517
- id: generateId2(),
3518
- role: "assistant",
3519
- content: "",
3520
- tool_calls: value.tool_calls,
3521
- createdAt
3522
- };
3523
- toolCallMessage = prefixMap["tool_calls"];
3524
- }
3525
- if (type === "data") {
3526
- prefixMap["data"].push(...value);
3527
- }
3528
- let responseMessage = prefixMap["text"];
3529
- if (type === "message_annotations") {
3530
- if (!message_annotations) {
3531
- message_annotations = [...value];
3532
- } else {
3533
- message_annotations.push(...value);
3534
- }
3535
- functionCallMessage = assignAnnotationsToMessage(
3536
- prefixMap["function_call"],
3537
- message_annotations
3538
- );
3539
- toolCallMessage = assignAnnotationsToMessage(
3540
- prefixMap["tool_calls"],
3541
- message_annotations
3542
- );
3543
- responseMessage = assignAnnotationsToMessage(
3544
- prefixMap["text"],
3545
- message_annotations
3546
- );
3547
- }
3548
- if (message_annotations == null ? void 0 : message_annotations.length) {
3549
- const messagePrefixKeys = [
3550
- "text",
3551
- "function_call",
3552
- "tool_calls"
3553
- ];
3554
- messagePrefixKeys.forEach((key) => {
3555
- if (prefixMap[key]) {
3556
- prefixMap[key].annotations = [...message_annotations];
3557
- }
3558
- });
3559
- }
3560
- const merged = [functionCallMessage, toolCallMessage, responseMessage].filter(Boolean).map((message) => ({
3561
- ...assignAnnotationsToMessage(message, message_annotations)
3562
- }));
3563
- update(merged, [...prefixMap["data"]]);
3564
- }
3565
- onFinish == null ? void 0 : onFinish(prefixMap);
3566
- return {
3567
- messages: [
3568
- prefixMap.text,
3569
- prefixMap.function_call,
3570
- prefixMap.tool_calls
3571
- ].filter(Boolean),
3572
- data: prefixMap.data
3573
- };
3574
- }
3575
-
3576
- // streams/streaming-react-response.ts
3577
- var experimental_StreamingReactResponse = class {
3578
- constructor(res, options) {
3579
- var _a, _b;
3580
- let resolveFunc = () => {
3581
- };
3582
- let next = new Promise((resolve) => {
3583
- resolveFunc = resolve;
3584
- });
3585
- const processedStream = (options == null ? void 0 : options.data) != null ? mergeStreams((_a = options == null ? void 0 : options.data) == null ? void 0 : _a.stream, res) : res;
3586
- let lastPayload = void 0;
3587
- parseComplexResponse({
3588
- reader: processedStream.getReader(),
3589
- update: (merged, data) => {
3590
- var _a2, _b2, _c;
3591
- const content = (_b2 = (_a2 = merged[0]) == null ? void 0 : _a2.content) != null ? _b2 : "";
3592
- const ui = ((_c = options == null ? void 0 : options.ui) == null ? void 0 : _c.call(options, { content, data })) || content;
3593
- const payload = { ui, content };
3594
- const resolvePrevious = resolveFunc;
3595
- const nextRow = new Promise((resolve) => {
3596
- resolveFunc = resolve;
3597
- });
3598
- resolvePrevious({
3599
- next: nextRow,
3600
- ...payload
3601
- });
3602
- lastPayload = payload;
3603
- },
3604
- generateId: (_b = options == null ? void 0 : options.generateId) != null ? _b : generateId,
3605
- onFinish: () => {
3606
- if (lastPayload !== void 0) {
3607
- resolveFunc({
3608
- next: null,
3609
- ...lastPayload
3610
- });
3611
- }
3612
- }
3613
- });
3614
- return next;
3615
- }
3616
- };
3617
-
3618
3143
  // streams/streaming-text-response.ts
3619
3144
  var StreamingTextResponse = class extends Response {
3620
3145
  constructor(res, init, data) {
@@ -3678,14 +3203,12 @@ var StreamingTextResponse = class extends Response {
3678
3203
  convertDataContentToUint8Array,
3679
3204
  convertToCoreMessages,
3680
3205
  createCallbacksTransformer,
3681
- createChunkDecoder,
3682
3206
  createEventStreamTransformer,
3683
3207
  createStreamDataTransformer,
3684
3208
  embed,
3685
3209
  embedMany,
3686
3210
  experimental_AssistantResponse,
3687
3211
  experimental_StreamData,
3688
- experimental_StreamingReactResponse,
3689
3212
  experimental_generateObject,
3690
3213
  experimental_generateText,
3691
3214
  experimental_streamObject,
@@ -3694,8 +3217,8 @@ var StreamingTextResponse = class extends Response {
3694
3217
  generateId,
3695
3218
  generateObject,
3696
3219
  generateText,
3697
- isStreamStringEqualToType,
3698
3220
  nanoid,
3221
+ parseComplexResponse,
3699
3222
  parseStreamPart,
3700
3223
  readDataStream,
3701
3224
  readableFromAsyncIterable,