ai 0.0.0-85f9a635-20240518005312

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 (56) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +37 -0
  3. package/dist/index.d.mts +2287 -0
  4. package/dist/index.d.ts +2287 -0
  5. package/dist/index.js +3531 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/index.mjs +3454 -0
  8. package/dist/index.mjs.map +1 -0
  9. package/package.json +178 -0
  10. package/prompts/dist/index.d.mts +324 -0
  11. package/prompts/dist/index.d.ts +324 -0
  12. package/prompts/dist/index.js +178 -0
  13. package/prompts/dist/index.js.map +1 -0
  14. package/prompts/dist/index.mjs +146 -0
  15. package/prompts/dist/index.mjs.map +1 -0
  16. package/react/dist/index.d.mts +565 -0
  17. package/react/dist/index.d.ts +582 -0
  18. package/react/dist/index.js +1430 -0
  19. package/react/dist/index.js.map +1 -0
  20. package/react/dist/index.mjs +1391 -0
  21. package/react/dist/index.mjs.map +1 -0
  22. package/react/dist/index.server.d.mts +17 -0
  23. package/react/dist/index.server.d.ts +17 -0
  24. package/react/dist/index.server.js +50 -0
  25. package/react/dist/index.server.js.map +1 -0
  26. package/react/dist/index.server.mjs +23 -0
  27. package/react/dist/index.server.mjs.map +1 -0
  28. package/rsc/dist/index.d.ts +580 -0
  29. package/rsc/dist/index.mjs +18 -0
  30. package/rsc/dist/rsc-client.d.mts +1 -0
  31. package/rsc/dist/rsc-client.mjs +18 -0
  32. package/rsc/dist/rsc-client.mjs.map +1 -0
  33. package/rsc/dist/rsc-server.d.mts +516 -0
  34. package/rsc/dist/rsc-server.mjs +1900 -0
  35. package/rsc/dist/rsc-server.mjs.map +1 -0
  36. package/rsc/dist/rsc-shared.d.mts +94 -0
  37. package/rsc/dist/rsc-shared.mjs +346 -0
  38. package/rsc/dist/rsc-shared.mjs.map +1 -0
  39. package/solid/dist/index.d.mts +408 -0
  40. package/solid/dist/index.d.ts +408 -0
  41. package/solid/dist/index.js +1072 -0
  42. package/solid/dist/index.js.map +1 -0
  43. package/solid/dist/index.mjs +1044 -0
  44. package/solid/dist/index.mjs.map +1 -0
  45. package/svelte/dist/index.d.mts +484 -0
  46. package/svelte/dist/index.d.ts +484 -0
  47. package/svelte/dist/index.js +1778 -0
  48. package/svelte/dist/index.js.map +1 -0
  49. package/svelte/dist/index.mjs +1749 -0
  50. package/svelte/dist/index.mjs.map +1 -0
  51. package/vue/dist/index.d.mts +402 -0
  52. package/vue/dist/index.d.ts +402 -0
  53. package/vue/dist/index.js +1072 -0
  54. package/vue/dist/index.js.map +1 -0
  55. package/vue/dist/index.mjs +1034 -0
  56. package/vue/dist/index.mjs.map +1 -0
@@ -0,0 +1,1900 @@
1
+ // rsc/ai-state.tsx
2
+ import { AsyncLocalStorage } from "async_hooks";
3
+ import * as jsondiffpatch from "jsondiffpatch";
4
+
5
+ // rsc/utils.tsx
6
+ import { Suspense } from "react";
7
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
8
+ function createResolvablePromise() {
9
+ let resolve, reject;
10
+ const promise = new Promise((res, rej) => {
11
+ resolve = res;
12
+ reject = rej;
13
+ });
14
+ return {
15
+ promise,
16
+ resolve,
17
+ reject
18
+ };
19
+ }
20
+ var R = [
21
+ async ({
22
+ c,
23
+ // current
24
+ n
25
+ // next
26
+ }) => {
27
+ const chunk = await n;
28
+ if (chunk.done) {
29
+ return chunk.value;
30
+ }
31
+ if (chunk.append) {
32
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
33
+ c,
34
+ /* @__PURE__ */ jsx(Suspense, { fallback: chunk.value, children: /* @__PURE__ */ jsx(R, { c: chunk.value, n: chunk.next }) })
35
+ ] });
36
+ }
37
+ return /* @__PURE__ */ jsx(Suspense, { fallback: chunk.value, children: /* @__PURE__ */ jsx(R, { c: chunk.value, n: chunk.next }) });
38
+ }
39
+ ][0];
40
+ function createSuspensedChunk(initialValue) {
41
+ const { promise, resolve, reject } = createResolvablePromise();
42
+ return {
43
+ row: /* @__PURE__ */ jsx(Suspense, { fallback: initialValue, children: /* @__PURE__ */ jsx(R, { c: initialValue, n: promise }) }),
44
+ resolve,
45
+ reject
46
+ };
47
+ }
48
+ var isFunction = (x) => typeof x === "function";
49
+ var consumeStream = async (stream) => {
50
+ const reader = stream.getReader();
51
+ while (true) {
52
+ const { done } = await reader.read();
53
+ if (done)
54
+ break;
55
+ }
56
+ };
57
+
58
+ // rsc/ai-state.tsx
59
+ var asyncAIStateStorage = new AsyncLocalStorage();
60
+ function getAIStateStoreOrThrow(message) {
61
+ const store = asyncAIStateStorage.getStore();
62
+ if (!store) {
63
+ throw new Error(message);
64
+ }
65
+ return store;
66
+ }
67
+ function withAIState({ state, options }, fn) {
68
+ return asyncAIStateStorage.run(
69
+ {
70
+ currentState: state,
71
+ originalState: state,
72
+ sealed: false,
73
+ options
74
+ },
75
+ fn
76
+ );
77
+ }
78
+ function getAIStateDeltaPromise() {
79
+ const store = getAIStateStoreOrThrow("Internal error occurred.");
80
+ return store.mutationDeltaPromise;
81
+ }
82
+ function sealMutableAIState() {
83
+ const store = getAIStateStoreOrThrow("Internal error occurred.");
84
+ store.sealed = true;
85
+ }
86
+ function getAIState(...args) {
87
+ const store = getAIStateStoreOrThrow(
88
+ "`getAIState` must be called within an AI Action."
89
+ );
90
+ if (args.length > 0) {
91
+ const key = args[0];
92
+ if (typeof store.currentState !== "object") {
93
+ throw new Error(
94
+ `You can't get the "${String(
95
+ key
96
+ )}" field from the AI state because it's not an object.`
97
+ );
98
+ }
99
+ return store.currentState[key];
100
+ }
101
+ return store.currentState;
102
+ }
103
+ function getMutableAIState(...args) {
104
+ const store = getAIStateStoreOrThrow(
105
+ "`getMutableAIState` must be called within an AI Action."
106
+ );
107
+ if (store.sealed) {
108
+ throw new Error(
109
+ "`getMutableAIState` must be called before returning from an AI Action. Please move it to the top level of the Action's function body."
110
+ );
111
+ }
112
+ if (!store.mutationDeltaPromise) {
113
+ const { promise, resolve } = createResolvablePromise();
114
+ store.mutationDeltaPromise = promise;
115
+ store.mutationDeltaResolve = resolve;
116
+ }
117
+ function doUpdate(newState, done) {
118
+ var _a, _b;
119
+ if (args.length > 0) {
120
+ if (typeof store.currentState !== "object") {
121
+ const key = args[0];
122
+ throw new Error(
123
+ `You can't modify the "${String(
124
+ key
125
+ )}" field of the AI state because it's not an object.`
126
+ );
127
+ }
128
+ }
129
+ if (isFunction(newState)) {
130
+ if (args.length > 0) {
131
+ store.currentState[args[0]] = newState(store.currentState[args[0]]);
132
+ } else {
133
+ store.currentState = newState(store.currentState);
134
+ }
135
+ } else {
136
+ if (args.length > 0) {
137
+ store.currentState[args[0]] = newState;
138
+ } else {
139
+ store.currentState = newState;
140
+ }
141
+ }
142
+ (_b = (_a = store.options).onSetAIState) == null ? void 0 : _b.call(_a, {
143
+ key: args.length > 0 ? args[0] : void 0,
144
+ state: store.currentState,
145
+ done
146
+ });
147
+ }
148
+ const mutableState = {
149
+ get: () => {
150
+ if (args.length > 0) {
151
+ const key = args[0];
152
+ if (typeof store.currentState !== "object") {
153
+ throw new Error(
154
+ `You can't get the "${String(
155
+ key
156
+ )}" field from the AI state because it's not an object.`
157
+ );
158
+ }
159
+ return store.currentState[key];
160
+ }
161
+ return store.currentState;
162
+ },
163
+ update: function update(newAIState) {
164
+ doUpdate(newAIState, false);
165
+ },
166
+ done: function done(...doneArgs) {
167
+ if (doneArgs.length > 0) {
168
+ doUpdate(doneArgs[0], true);
169
+ }
170
+ const delta = jsondiffpatch.diff(store.originalState, store.currentState);
171
+ store.mutationDeltaResolve(delta);
172
+ }
173
+ };
174
+ return mutableState;
175
+ }
176
+
177
+ // rsc/streamable.tsx
178
+ import zodToJsonSchema2 from "zod-to-json-schema";
179
+
180
+ // core/util/retry-with-exponential-backoff.ts
181
+ import { APICallError, RetryError } from "@ai-sdk/provider";
182
+ import { getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
183
+
184
+ // core/util/delay.ts
185
+ async function delay(delayInMs) {
186
+ return new Promise((resolve) => setTimeout(resolve, delayInMs));
187
+ }
188
+
189
+ // core/util/retry-with-exponential-backoff.ts
190
+ var retryWithExponentialBackoff = ({
191
+ maxRetries = 2,
192
+ initialDelayInMs = 2e3,
193
+ backoffFactor = 2
194
+ } = {}) => async (f) => _retryWithExponentialBackoff(f, {
195
+ maxRetries,
196
+ delayInMs: initialDelayInMs,
197
+ backoffFactor
198
+ });
199
+ async function _retryWithExponentialBackoff(f, {
200
+ maxRetries,
201
+ delayInMs,
202
+ backoffFactor
203
+ }, errors = []) {
204
+ try {
205
+ return await f();
206
+ } catch (error) {
207
+ if (isAbortError(error)) {
208
+ throw error;
209
+ }
210
+ if (maxRetries === 0) {
211
+ throw error;
212
+ }
213
+ const errorMessage = getErrorMessage(error);
214
+ const newErrors = [...errors, error];
215
+ const tryNumber = newErrors.length;
216
+ if (tryNumber > maxRetries) {
217
+ throw new RetryError({
218
+ message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
219
+ reason: "maxRetriesExceeded",
220
+ errors: newErrors
221
+ });
222
+ }
223
+ if (error instanceof Error && APICallError.isAPICallError(error) && error.isRetryable === true && tryNumber <= maxRetries) {
224
+ await delay(delayInMs);
225
+ return _retryWithExponentialBackoff(
226
+ f,
227
+ { maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
228
+ newErrors
229
+ );
230
+ }
231
+ if (tryNumber === 1) {
232
+ throw error;
233
+ }
234
+ throw new RetryError({
235
+ message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
236
+ reason: "errorNotRetryable",
237
+ errors: newErrors
238
+ });
239
+ }
240
+ }
241
+
242
+ // core/util/detect-image-mimetype.ts
243
+ var mimeTypeSignatures = [
244
+ { mimeType: "image/gif", bytes: [71, 73, 70] },
245
+ { mimeType: "image/png", bytes: [137, 80, 78, 71] },
246
+ { mimeType: "image/jpeg", bytes: [255, 216] },
247
+ { mimeType: "image/webp", bytes: [82, 73, 70, 70] }
248
+ ];
249
+ function detectImageMimeType(image) {
250
+ for (const { bytes, mimeType } of mimeTypeSignatures) {
251
+ if (image.length >= bytes.length && bytes.every((byte, index) => image[index] === byte)) {
252
+ return mimeType;
253
+ }
254
+ }
255
+ return void 0;
256
+ }
257
+
258
+ // core/prompt/data-content.ts
259
+ import { InvalidDataContentError } from "@ai-sdk/provider";
260
+ import {
261
+ convertBase64ToUint8Array,
262
+ convertUint8ArrayToBase64
263
+ } from "@ai-sdk/provider-utils";
264
+ function convertDataContentToUint8Array(content) {
265
+ if (content instanceof Uint8Array) {
266
+ return content;
267
+ }
268
+ if (typeof content === "string") {
269
+ try {
270
+ return convertBase64ToUint8Array(content);
271
+ } catch (error) {
272
+ throw new InvalidDataContentError({
273
+ message: "Invalid data content. Content string is not a base64-encoded image.",
274
+ content,
275
+ cause: error
276
+ });
277
+ }
278
+ }
279
+ if (content instanceof ArrayBuffer) {
280
+ return new Uint8Array(content);
281
+ }
282
+ throw new InvalidDataContentError({ content });
283
+ }
284
+
285
+ // core/prompt/convert-to-language-model-prompt.ts
286
+ function convertToLanguageModelPrompt(prompt) {
287
+ const languageModelMessages = [];
288
+ if (prompt.system != null) {
289
+ languageModelMessages.push({ role: "system", content: prompt.system });
290
+ }
291
+ switch (prompt.type) {
292
+ case "prompt": {
293
+ languageModelMessages.push({
294
+ role: "user",
295
+ content: [{ type: "text", text: prompt.prompt }]
296
+ });
297
+ break;
298
+ }
299
+ case "messages": {
300
+ languageModelMessages.push(
301
+ ...prompt.messages.map((message) => {
302
+ switch (message.role) {
303
+ case "system": {
304
+ return { role: "system", content: message.content };
305
+ }
306
+ case "user": {
307
+ if (typeof message.content === "string") {
308
+ return {
309
+ role: "user",
310
+ content: [{ type: "text", text: message.content }]
311
+ };
312
+ }
313
+ return {
314
+ role: "user",
315
+ content: message.content.map(
316
+ (part) => {
317
+ var _a;
318
+ switch (part.type) {
319
+ case "text": {
320
+ return part;
321
+ }
322
+ case "image": {
323
+ if (part.image instanceof URL) {
324
+ return {
325
+ type: "image",
326
+ image: part.image,
327
+ mimeType: part.mimeType
328
+ };
329
+ }
330
+ const imageUint8 = convertDataContentToUint8Array(
331
+ part.image
332
+ );
333
+ return {
334
+ type: "image",
335
+ image: imageUint8,
336
+ mimeType: (_a = part.mimeType) != null ? _a : detectImageMimeType(imageUint8)
337
+ };
338
+ }
339
+ }
340
+ }
341
+ )
342
+ };
343
+ }
344
+ case "assistant": {
345
+ if (typeof message.content === "string") {
346
+ return {
347
+ role: "assistant",
348
+ content: [{ type: "text", text: message.content }]
349
+ };
350
+ }
351
+ return { role: "assistant", content: message.content };
352
+ }
353
+ case "tool": {
354
+ return message;
355
+ }
356
+ }
357
+ })
358
+ );
359
+ break;
360
+ }
361
+ default: {
362
+ const _exhaustiveCheck = prompt;
363
+ throw new Error(`Unsupported prompt type: ${_exhaustiveCheck}`);
364
+ }
365
+ }
366
+ return languageModelMessages;
367
+ }
368
+
369
+ // core/prompt/get-validated-prompt.ts
370
+ import { InvalidPromptError } from "@ai-sdk/provider";
371
+ function getValidatedPrompt(prompt) {
372
+ if (prompt.prompt == null && prompt.messages == null) {
373
+ throw new InvalidPromptError({
374
+ prompt,
375
+ message: "prompt or messages must be defined"
376
+ });
377
+ }
378
+ if (prompt.prompt != null && prompt.messages != null) {
379
+ throw new InvalidPromptError({
380
+ prompt,
381
+ message: "prompt and messages cannot be defined at the same time"
382
+ });
383
+ }
384
+ return prompt.prompt != null ? {
385
+ type: "prompt",
386
+ prompt: prompt.prompt,
387
+ messages: void 0,
388
+ system: prompt.system
389
+ } : {
390
+ type: "messages",
391
+ prompt: void 0,
392
+ messages: prompt.messages,
393
+ // only possible case bc of checks above
394
+ system: prompt.system
395
+ };
396
+ }
397
+
398
+ // core/prompt/prepare-call-settings.ts
399
+ import { InvalidArgumentError } from "@ai-sdk/provider";
400
+ function prepareCallSettings({
401
+ maxTokens,
402
+ temperature,
403
+ topP,
404
+ presencePenalty,
405
+ frequencyPenalty,
406
+ seed,
407
+ maxRetries
408
+ }) {
409
+ if (maxTokens != null) {
410
+ if (!Number.isInteger(maxTokens)) {
411
+ throw new InvalidArgumentError({
412
+ parameter: "maxTokens",
413
+ value: maxTokens,
414
+ message: "maxTokens must be an integer"
415
+ });
416
+ }
417
+ if (maxTokens < 1) {
418
+ throw new InvalidArgumentError({
419
+ parameter: "maxTokens",
420
+ value: maxTokens,
421
+ message: "maxTokens must be >= 1"
422
+ });
423
+ }
424
+ }
425
+ if (temperature != null) {
426
+ if (typeof temperature !== "number") {
427
+ throw new InvalidArgumentError({
428
+ parameter: "temperature",
429
+ value: temperature,
430
+ message: "temperature must be a number"
431
+ });
432
+ }
433
+ }
434
+ if (topP != null) {
435
+ if (typeof topP !== "number") {
436
+ throw new InvalidArgumentError({
437
+ parameter: "topP",
438
+ value: topP,
439
+ message: "topP must be a number"
440
+ });
441
+ }
442
+ }
443
+ if (presencePenalty != null) {
444
+ if (typeof presencePenalty !== "number") {
445
+ throw new InvalidArgumentError({
446
+ parameter: "presencePenalty",
447
+ value: presencePenalty,
448
+ message: "presencePenalty must be a number"
449
+ });
450
+ }
451
+ }
452
+ if (frequencyPenalty != null) {
453
+ if (typeof frequencyPenalty !== "number") {
454
+ throw new InvalidArgumentError({
455
+ parameter: "frequencyPenalty",
456
+ value: frequencyPenalty,
457
+ message: "frequencyPenalty must be a number"
458
+ });
459
+ }
460
+ }
461
+ if (seed != null) {
462
+ if (!Number.isInteger(seed)) {
463
+ throw new InvalidArgumentError({
464
+ parameter: "seed",
465
+ value: seed,
466
+ message: "seed must be an integer"
467
+ });
468
+ }
469
+ }
470
+ if (maxRetries != null) {
471
+ if (!Number.isInteger(maxRetries)) {
472
+ throw new InvalidArgumentError({
473
+ parameter: "maxRetries",
474
+ value: maxRetries,
475
+ message: "maxRetries must be an integer"
476
+ });
477
+ }
478
+ if (maxRetries < 0) {
479
+ throw new InvalidArgumentError({
480
+ parameter: "maxRetries",
481
+ value: maxRetries,
482
+ message: "maxRetries must be >= 0"
483
+ });
484
+ }
485
+ }
486
+ return {
487
+ maxTokens,
488
+ temperature: temperature != null ? temperature : 0,
489
+ topP,
490
+ presencePenalty,
491
+ frequencyPenalty,
492
+ seed,
493
+ maxRetries: maxRetries != null ? maxRetries : 2
494
+ };
495
+ }
496
+
497
+ // core/util/convert-zod-to-json-schema.ts
498
+ import zodToJsonSchema from "zod-to-json-schema";
499
+ function convertZodToJSONSchema(zodSchema) {
500
+ return zodToJsonSchema(zodSchema);
501
+ }
502
+
503
+ // shared/stream-parts.ts
504
+ var textStreamPart = {
505
+ code: "0",
506
+ name: "text",
507
+ parse: (value) => {
508
+ if (typeof value !== "string") {
509
+ throw new Error('"text" parts expect a string value.');
510
+ }
511
+ return { type: "text", value };
512
+ }
513
+ };
514
+ var functionCallStreamPart = {
515
+ code: "1",
516
+ name: "function_call",
517
+ parse: (value) => {
518
+ 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") {
519
+ throw new Error(
520
+ '"function_call" parts expect an object with a "function_call" property.'
521
+ );
522
+ }
523
+ return {
524
+ type: "function_call",
525
+ value
526
+ };
527
+ }
528
+ };
529
+ var dataStreamPart = {
530
+ code: "2",
531
+ name: "data",
532
+ parse: (value) => {
533
+ if (!Array.isArray(value)) {
534
+ throw new Error('"data" parts expect an array value.');
535
+ }
536
+ return { type: "data", value };
537
+ }
538
+ };
539
+ var errorStreamPart = {
540
+ code: "3",
541
+ name: "error",
542
+ parse: (value) => {
543
+ if (typeof value !== "string") {
544
+ throw new Error('"error" parts expect a string value.');
545
+ }
546
+ return { type: "error", value };
547
+ }
548
+ };
549
+ var assistantMessageStreamPart = {
550
+ code: "4",
551
+ name: "assistant_message",
552
+ parse: (value) => {
553
+ 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(
554
+ (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"
555
+ )) {
556
+ throw new Error(
557
+ '"assistant_message" parts expect an object with an "id", "role", and "content" property.'
558
+ );
559
+ }
560
+ return {
561
+ type: "assistant_message",
562
+ value
563
+ };
564
+ }
565
+ };
566
+ var assistantControlDataStreamPart = {
567
+ code: "5",
568
+ name: "assistant_control_data",
569
+ parse: (value) => {
570
+ if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
571
+ throw new Error(
572
+ '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
573
+ );
574
+ }
575
+ return {
576
+ type: "assistant_control_data",
577
+ value: {
578
+ threadId: value.threadId,
579
+ messageId: value.messageId
580
+ }
581
+ };
582
+ }
583
+ };
584
+ var dataMessageStreamPart = {
585
+ code: "6",
586
+ name: "data_message",
587
+ parse: (value) => {
588
+ if (value == null || typeof value !== "object" || !("role" in value) || !("data" in value) || typeof value.role !== "string" || value.role !== "data") {
589
+ throw new Error(
590
+ '"data_message" parts expect an object with a "role" and "data" property.'
591
+ );
592
+ }
593
+ return {
594
+ type: "data_message",
595
+ value
596
+ };
597
+ }
598
+ };
599
+ var toolCallsStreamPart = {
600
+ code: "7",
601
+ name: "tool_calls",
602
+ parse: (value) => {
603
+ 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(
604
+ (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"
605
+ )) {
606
+ throw new Error(
607
+ '"tool_calls" parts expect an object with a ToolCallPayload.'
608
+ );
609
+ }
610
+ return {
611
+ type: "tool_calls",
612
+ value
613
+ };
614
+ }
615
+ };
616
+ var messageAnnotationsStreamPart = {
617
+ code: "8",
618
+ name: "message_annotations",
619
+ parse: (value) => {
620
+ if (!Array.isArray(value)) {
621
+ throw new Error('"message_annotations" parts expect an array value.');
622
+ }
623
+ return { type: "message_annotations", value };
624
+ }
625
+ };
626
+ var toolCallStreamPart = {
627
+ code: "9",
628
+ name: "tool_call",
629
+ parse: (value) => {
630
+ 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") {
631
+ throw new Error(
632
+ '"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
633
+ );
634
+ }
635
+ return {
636
+ type: "tool_call",
637
+ value
638
+ };
639
+ }
640
+ };
641
+ var toolResultStreamPart = {
642
+ code: "a",
643
+ name: "tool_result",
644
+ parse: (value) => {
645
+ 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)) {
646
+ throw new Error(
647
+ '"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
648
+ );
649
+ }
650
+ return {
651
+ type: "tool_result",
652
+ value
653
+ };
654
+ }
655
+ };
656
+ var streamParts = [
657
+ textStreamPart,
658
+ functionCallStreamPart,
659
+ dataStreamPart,
660
+ errorStreamPart,
661
+ assistantMessageStreamPart,
662
+ assistantControlDataStreamPart,
663
+ dataMessageStreamPart,
664
+ toolCallsStreamPart,
665
+ messageAnnotationsStreamPart,
666
+ toolCallStreamPart,
667
+ toolResultStreamPart
668
+ ];
669
+ var streamPartsByCode = {
670
+ [textStreamPart.code]: textStreamPart,
671
+ [functionCallStreamPart.code]: functionCallStreamPart,
672
+ [dataStreamPart.code]: dataStreamPart,
673
+ [errorStreamPart.code]: errorStreamPart,
674
+ [assistantMessageStreamPart.code]: assistantMessageStreamPart,
675
+ [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
676
+ [dataMessageStreamPart.code]: dataMessageStreamPart,
677
+ [toolCallsStreamPart.code]: toolCallsStreamPart,
678
+ [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
679
+ [toolCallStreamPart.code]: toolCallStreamPart,
680
+ [toolResultStreamPart.code]: toolResultStreamPart
681
+ };
682
+ var StreamStringPrefixes = {
683
+ [textStreamPart.name]: textStreamPart.code,
684
+ [functionCallStreamPart.name]: functionCallStreamPart.code,
685
+ [dataStreamPart.name]: dataStreamPart.code,
686
+ [errorStreamPart.name]: errorStreamPart.code,
687
+ [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
688
+ [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
689
+ [dataMessageStreamPart.name]: dataMessageStreamPart.code,
690
+ [toolCallsStreamPart.name]: toolCallsStreamPart.code,
691
+ [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
692
+ [toolCallStreamPart.name]: toolCallStreamPart.code,
693
+ [toolResultStreamPart.name]: toolResultStreamPart.code
694
+ };
695
+ var validCodes = streamParts.map((part) => part.code);
696
+ var parseStreamPart = (line) => {
697
+ const firstSeparatorIndex = line.indexOf(":");
698
+ if (firstSeparatorIndex === -1) {
699
+ throw new Error("Failed to parse stream string. No separator found.");
700
+ }
701
+ const prefix = line.slice(0, firstSeparatorIndex);
702
+ if (!validCodes.includes(prefix)) {
703
+ throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
704
+ }
705
+ const code = prefix;
706
+ const textValue = line.slice(firstSeparatorIndex + 1);
707
+ const jsonValue = JSON.parse(textValue);
708
+ return streamPartsByCode[code].parse(jsonValue);
709
+ };
710
+ function formatStreamPart(type, value) {
711
+ const streamPart = streamParts.find((part) => part.name === type);
712
+ if (!streamPart) {
713
+ throw new Error(`Invalid stream part type: ${type}`);
714
+ }
715
+ return `${streamPart.code}:${JSON.stringify(value)}
716
+ `;
717
+ }
718
+
719
+ // shared/utils.ts
720
+ function createChunkDecoder(complex) {
721
+ const decoder = new TextDecoder();
722
+ if (!complex) {
723
+ return function(chunk) {
724
+ if (!chunk)
725
+ return "";
726
+ return decoder.decode(chunk, { stream: true });
727
+ };
728
+ }
729
+ return function(chunk) {
730
+ const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
731
+ return decoded.map(parseStreamPart).filter(Boolean);
732
+ };
733
+ }
734
+
735
+ // streams/ai-stream.ts
736
+ import {
737
+ createParser
738
+ } from "eventsource-parser";
739
+ function createEventStreamTransformer(customParser) {
740
+ const textDecoder = new TextDecoder();
741
+ let eventSourceParser;
742
+ return new TransformStream({
743
+ async start(controller) {
744
+ eventSourceParser = createParser(
745
+ (event) => {
746
+ if ("data" in event && event.type === "event" && event.data === "[DONE]" || // Replicate doesn't send [DONE] but does send a 'done' event
747
+ // @see https://replicate.com/docs/streaming
748
+ event.event === "done") {
749
+ controller.terminate();
750
+ return;
751
+ }
752
+ if ("data" in event) {
753
+ const parsedMessage = customParser ? customParser(event.data, {
754
+ event: event.event
755
+ }) : event.data;
756
+ if (parsedMessage)
757
+ controller.enqueue(parsedMessage);
758
+ }
759
+ }
760
+ );
761
+ },
762
+ transform(chunk) {
763
+ eventSourceParser.feed(textDecoder.decode(chunk));
764
+ }
765
+ });
766
+ }
767
+ function createCallbacksTransformer(cb) {
768
+ const textEncoder = new TextEncoder();
769
+ let aggregatedResponse = "";
770
+ const callbacks = cb || {};
771
+ return new TransformStream({
772
+ async start() {
773
+ if (callbacks.onStart)
774
+ await callbacks.onStart();
775
+ },
776
+ async transform(message, controller) {
777
+ const content = typeof message === "string" ? message : message.content;
778
+ controller.enqueue(textEncoder.encode(content));
779
+ aggregatedResponse += content;
780
+ if (callbacks.onToken)
781
+ await callbacks.onToken(content);
782
+ if (callbacks.onText && typeof message === "string") {
783
+ await callbacks.onText(message);
784
+ }
785
+ },
786
+ async flush() {
787
+ const isOpenAICallbacks = isOfTypeOpenAIStreamCallbacks(callbacks);
788
+ if (callbacks.onCompletion) {
789
+ await callbacks.onCompletion(aggregatedResponse);
790
+ }
791
+ if (callbacks.onFinal && !isOpenAICallbacks) {
792
+ await callbacks.onFinal(aggregatedResponse);
793
+ }
794
+ }
795
+ });
796
+ }
797
+ function isOfTypeOpenAIStreamCallbacks(callbacks) {
798
+ return "experimental_onFunctionCall" in callbacks;
799
+ }
800
+ function trimStartOfStreamHelper() {
801
+ let isStreamStart = true;
802
+ return (text) => {
803
+ if (isStreamStart) {
804
+ text = text.trimStart();
805
+ if (text)
806
+ isStreamStart = false;
807
+ }
808
+ return text;
809
+ };
810
+ }
811
+ function AIStream(response, customParser, callbacks) {
812
+ if (!response.ok) {
813
+ if (response.body) {
814
+ const reader = response.body.getReader();
815
+ return new ReadableStream({
816
+ async start(controller) {
817
+ const { done, value } = await reader.read();
818
+ if (!done) {
819
+ const errorText = new TextDecoder().decode(value);
820
+ controller.error(new Error(`Response error: ${errorText}`));
821
+ }
822
+ }
823
+ });
824
+ } else {
825
+ return new ReadableStream({
826
+ start(controller) {
827
+ controller.error(new Error("Response error: No response body"));
828
+ }
829
+ });
830
+ }
831
+ }
832
+ const responseBodyStream = response.body || createEmptyReadableStream();
833
+ return responseBodyStream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
834
+ }
835
+ function createEmptyReadableStream() {
836
+ return new ReadableStream({
837
+ start(controller) {
838
+ controller.close();
839
+ }
840
+ });
841
+ }
842
+ function readableFromAsyncIterable(iterable) {
843
+ let it = iterable[Symbol.asyncIterator]();
844
+ return new ReadableStream({
845
+ async pull(controller) {
846
+ const { done, value } = await it.next();
847
+ if (done)
848
+ controller.close();
849
+ else
850
+ controller.enqueue(value);
851
+ },
852
+ async cancel(reason) {
853
+ var _a;
854
+ await ((_a = it.return) == null ? void 0 : _a.call(it, reason));
855
+ }
856
+ });
857
+ }
858
+
859
+ // streams/stream-data.ts
860
+ function createStreamDataTransformer() {
861
+ const encoder = new TextEncoder();
862
+ const decoder = new TextDecoder();
863
+ return new TransformStream({
864
+ transform: async (chunk, controller) => {
865
+ const message = decoder.decode(chunk);
866
+ controller.enqueue(encoder.encode(formatStreamPart("text", message)));
867
+ }
868
+ });
869
+ }
870
+
871
+ // streams/openai-stream.ts
872
+ function parseOpenAIStream() {
873
+ const extract = chunkToText();
874
+ return (data) => extract(JSON.parse(data));
875
+ }
876
+ async function* streamable(stream) {
877
+ const extract = chunkToText();
878
+ for await (let chunk of stream) {
879
+ if ("promptFilterResults" in chunk) {
880
+ chunk = {
881
+ id: chunk.id,
882
+ created: chunk.created.getDate(),
883
+ object: chunk.object,
884
+ // not exposed by Azure API
885
+ model: chunk.model,
886
+ // not exposed by Azure API
887
+ choices: chunk.choices.map((choice) => {
888
+ var _a, _b, _c, _d, _e, _f, _g;
889
+ return {
890
+ delta: {
891
+ content: (_a = choice.delta) == null ? void 0 : _a.content,
892
+ function_call: (_b = choice.delta) == null ? void 0 : _b.functionCall,
893
+ role: (_c = choice.delta) == null ? void 0 : _c.role,
894
+ tool_calls: ((_e = (_d = choice.delta) == null ? void 0 : _d.toolCalls) == null ? void 0 : _e.length) ? (_g = (_f = choice.delta) == null ? void 0 : _f.toolCalls) == null ? void 0 : _g.map((toolCall, index) => ({
895
+ index,
896
+ id: toolCall.id,
897
+ function: toolCall.function,
898
+ type: toolCall.type
899
+ })) : void 0
900
+ },
901
+ finish_reason: choice.finishReason,
902
+ index: choice.index
903
+ };
904
+ })
905
+ };
906
+ }
907
+ const text = extract(chunk);
908
+ if (text)
909
+ yield text;
910
+ }
911
+ }
912
+ function chunkToText() {
913
+ const trimStartOfStream = trimStartOfStreamHelper();
914
+ let isFunctionStreamingIn;
915
+ return (json) => {
916
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
917
+ if (isChatCompletionChunk(json)) {
918
+ const delta = (_a = json.choices[0]) == null ? void 0 : _a.delta;
919
+ if ((_b = delta.function_call) == null ? void 0 : _b.name) {
920
+ isFunctionStreamingIn = true;
921
+ return {
922
+ isText: false,
923
+ content: `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`
924
+ };
925
+ } else if ((_e = (_d = (_c = delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function) == null ? void 0 : _e.name) {
926
+ isFunctionStreamingIn = true;
927
+ const toolCall = delta.tool_calls[0];
928
+ if (toolCall.index === 0) {
929
+ return {
930
+ isText: false,
931
+ content: `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`
932
+ };
933
+ } else {
934
+ return {
935
+ isText: false,
936
+ content: `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`
937
+ };
938
+ }
939
+ } else if ((_h = delta.function_call) == null ? void 0 : _h.arguments) {
940
+ return {
941
+ isText: false,
942
+ content: cleanupArguments((_i = delta.function_call) == null ? void 0 : _i.arguments)
943
+ };
944
+ } else if ((_l = (_k = (_j = delta.tool_calls) == null ? void 0 : _j[0]) == null ? void 0 : _k.function) == null ? void 0 : _l.arguments) {
945
+ return {
946
+ isText: false,
947
+ content: cleanupArguments((_o = (_n = (_m = delta.tool_calls) == null ? void 0 : _m[0]) == null ? void 0 : _n.function) == null ? void 0 : _o.arguments)
948
+ };
949
+ } else if (isFunctionStreamingIn && (((_p = json.choices[0]) == null ? void 0 : _p.finish_reason) === "function_call" || ((_q = json.choices[0]) == null ? void 0 : _q.finish_reason) === "stop")) {
950
+ isFunctionStreamingIn = false;
951
+ return {
952
+ isText: false,
953
+ content: '"}}'
954
+ };
955
+ } else if (isFunctionStreamingIn && ((_r = json.choices[0]) == null ? void 0 : _r.finish_reason) === "tool_calls") {
956
+ isFunctionStreamingIn = false;
957
+ return {
958
+ isText: false,
959
+ content: '"}}]}'
960
+ };
961
+ }
962
+ }
963
+ const text = trimStartOfStream(
964
+ isChatCompletionChunk(json) && json.choices[0].delta.content ? json.choices[0].delta.content : isCompletion(json) ? json.choices[0].text : ""
965
+ );
966
+ return text;
967
+ };
968
+ function cleanupArguments(argumentChunk) {
969
+ let escapedPartialJson = argumentChunk.replace(/\\/g, "\\\\").replace(/\//g, "\\/").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/\f/g, "\\f");
970
+ return `${escapedPartialJson}`;
971
+ }
972
+ }
973
+ var __internal__OpenAIFnMessagesSymbol = Symbol(
974
+ "internal_openai_fn_messages"
975
+ );
976
+ function isChatCompletionChunk(data) {
977
+ return "choices" in data && data.choices && data.choices[0] && "delta" in data.choices[0];
978
+ }
979
+ function isCompletion(data) {
980
+ return "choices" in data && data.choices && data.choices[0] && "text" in data.choices[0];
981
+ }
982
+ function OpenAIStream(res, callbacks) {
983
+ const cb = callbacks;
984
+ let stream;
985
+ if (Symbol.asyncIterator in res) {
986
+ stream = readableFromAsyncIterable(streamable(res)).pipeThrough(
987
+ createCallbacksTransformer(
988
+ (cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
989
+ ...cb,
990
+ onFinal: void 0
991
+ } : {
992
+ ...cb
993
+ }
994
+ )
995
+ );
996
+ } else {
997
+ stream = AIStream(
998
+ res,
999
+ parseOpenAIStream(),
1000
+ (cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
1001
+ ...cb,
1002
+ onFinal: void 0
1003
+ } : {
1004
+ ...cb
1005
+ }
1006
+ );
1007
+ }
1008
+ if (cb && (cb.experimental_onFunctionCall || cb.experimental_onToolCall)) {
1009
+ const functionCallTransformer = createFunctionCallTransformer(cb);
1010
+ return stream.pipeThrough(functionCallTransformer);
1011
+ } else {
1012
+ return stream.pipeThrough(createStreamDataTransformer());
1013
+ }
1014
+ }
1015
+ function createFunctionCallTransformer(callbacks) {
1016
+ const textEncoder = new TextEncoder();
1017
+ let isFirstChunk = true;
1018
+ let aggregatedResponse = "";
1019
+ let aggregatedFinalCompletionResponse = "";
1020
+ let isFunctionStreamingIn = false;
1021
+ let functionCallMessages = callbacks[__internal__OpenAIFnMessagesSymbol] || [];
1022
+ const decode = createChunkDecoder();
1023
+ return new TransformStream({
1024
+ async transform(chunk, controller) {
1025
+ const message = decode(chunk);
1026
+ aggregatedFinalCompletionResponse += message;
1027
+ const shouldHandleAsFunction = isFirstChunk && (message.startsWith('{"function_call":') || message.startsWith('{"tool_calls":'));
1028
+ if (shouldHandleAsFunction) {
1029
+ isFunctionStreamingIn = true;
1030
+ aggregatedResponse += message;
1031
+ isFirstChunk = false;
1032
+ return;
1033
+ }
1034
+ if (!isFunctionStreamingIn) {
1035
+ controller.enqueue(
1036
+ textEncoder.encode(formatStreamPart("text", message))
1037
+ );
1038
+ return;
1039
+ } else {
1040
+ aggregatedResponse += message;
1041
+ }
1042
+ },
1043
+ async flush(controller) {
1044
+ try {
1045
+ if (!isFirstChunk && isFunctionStreamingIn && (callbacks.experimental_onFunctionCall || callbacks.experimental_onToolCall)) {
1046
+ isFunctionStreamingIn = false;
1047
+ const payload = JSON.parse(aggregatedResponse);
1048
+ let newFunctionCallMessages = [
1049
+ ...functionCallMessages
1050
+ ];
1051
+ let functionResponse = void 0;
1052
+ if (callbacks.experimental_onFunctionCall) {
1053
+ if (payload.function_call === void 0) {
1054
+ console.warn(
1055
+ "experimental_onFunctionCall should not be defined when using tools"
1056
+ );
1057
+ }
1058
+ const argumentsPayload = JSON.parse(
1059
+ payload.function_call.arguments
1060
+ );
1061
+ functionResponse = await callbacks.experimental_onFunctionCall(
1062
+ {
1063
+ name: payload.function_call.name,
1064
+ arguments: argumentsPayload
1065
+ },
1066
+ (result) => {
1067
+ newFunctionCallMessages = [
1068
+ ...functionCallMessages,
1069
+ {
1070
+ role: "assistant",
1071
+ content: "",
1072
+ function_call: payload.function_call
1073
+ },
1074
+ {
1075
+ role: "function",
1076
+ name: payload.function_call.name,
1077
+ content: JSON.stringify(result)
1078
+ }
1079
+ ];
1080
+ return newFunctionCallMessages;
1081
+ }
1082
+ );
1083
+ }
1084
+ if (callbacks.experimental_onToolCall) {
1085
+ const toolCalls = {
1086
+ tools: []
1087
+ };
1088
+ for (const tool of payload.tool_calls) {
1089
+ toolCalls.tools.push({
1090
+ id: tool.id,
1091
+ type: "function",
1092
+ func: {
1093
+ name: tool.function.name,
1094
+ arguments: JSON.parse(tool.function.arguments)
1095
+ }
1096
+ });
1097
+ }
1098
+ let responseIndex = 0;
1099
+ try {
1100
+ functionResponse = await callbacks.experimental_onToolCall(
1101
+ toolCalls,
1102
+ (result) => {
1103
+ if (result) {
1104
+ const { tool_call_id, function_name, tool_call_result } = result;
1105
+ newFunctionCallMessages = [
1106
+ ...newFunctionCallMessages,
1107
+ // Only append the assistant message if it's the first response
1108
+ ...responseIndex === 0 ? [
1109
+ {
1110
+ role: "assistant",
1111
+ content: "",
1112
+ tool_calls: payload.tool_calls.map(
1113
+ (tc) => ({
1114
+ id: tc.id,
1115
+ type: "function",
1116
+ function: {
1117
+ name: tc.function.name,
1118
+ // we send the arguments an object to the user, but as the API expects a string, we need to stringify it
1119
+ arguments: JSON.stringify(
1120
+ tc.function.arguments
1121
+ )
1122
+ }
1123
+ })
1124
+ )
1125
+ }
1126
+ ] : [],
1127
+ // Append the function call result message
1128
+ {
1129
+ role: "tool",
1130
+ tool_call_id,
1131
+ name: function_name,
1132
+ content: JSON.stringify(tool_call_result)
1133
+ }
1134
+ ];
1135
+ responseIndex++;
1136
+ }
1137
+ return newFunctionCallMessages;
1138
+ }
1139
+ );
1140
+ } catch (e) {
1141
+ console.error("Error calling experimental_onToolCall:", e);
1142
+ }
1143
+ }
1144
+ if (!functionResponse) {
1145
+ controller.enqueue(
1146
+ textEncoder.encode(
1147
+ formatStreamPart(
1148
+ payload.function_call ? "function_call" : "tool_calls",
1149
+ // parse to prevent double-encoding:
1150
+ JSON.parse(aggregatedResponse)
1151
+ )
1152
+ )
1153
+ );
1154
+ return;
1155
+ } else if (typeof functionResponse === "string") {
1156
+ controller.enqueue(
1157
+ textEncoder.encode(formatStreamPart("text", functionResponse))
1158
+ );
1159
+ aggregatedFinalCompletionResponse = functionResponse;
1160
+ return;
1161
+ }
1162
+ const filteredCallbacks = {
1163
+ ...callbacks,
1164
+ onStart: void 0
1165
+ };
1166
+ callbacks.onFinal = void 0;
1167
+ const openAIStream = OpenAIStream(functionResponse, {
1168
+ ...filteredCallbacks,
1169
+ [__internal__OpenAIFnMessagesSymbol]: newFunctionCallMessages
1170
+ });
1171
+ const reader = openAIStream.getReader();
1172
+ while (true) {
1173
+ const { done, value } = await reader.read();
1174
+ if (done) {
1175
+ break;
1176
+ }
1177
+ controller.enqueue(value);
1178
+ }
1179
+ }
1180
+ } finally {
1181
+ if (callbacks.onFinal && aggregatedFinalCompletionResponse) {
1182
+ await callbacks.onFinal(aggregatedFinalCompletionResponse);
1183
+ }
1184
+ }
1185
+ }
1186
+ });
1187
+ }
1188
+
1189
+ // rsc/constants.ts
1190
+ var STREAMABLE_VALUE_TYPE = Symbol.for("ui.streamable.value");
1191
+ var DEV_DEFAULT_STREAMABLE_WARNING_TIME = 15 * 1e3;
1192
+
1193
+ // rsc/streamable.tsx
1194
+ function createStreamableUI(initialValue) {
1195
+ let currentValue = initialValue;
1196
+ let closed = false;
1197
+ let { row, resolve, reject } = createSuspensedChunk(initialValue);
1198
+ function assertStream(method) {
1199
+ if (closed) {
1200
+ throw new Error(method + ": UI stream is already closed.");
1201
+ }
1202
+ }
1203
+ let warningTimeout;
1204
+ function warnUnclosedStream() {
1205
+ if (process.env.NODE_ENV === "development") {
1206
+ if (warningTimeout) {
1207
+ clearTimeout(warningTimeout);
1208
+ }
1209
+ warningTimeout = setTimeout(() => {
1210
+ console.warn(
1211
+ "The streamable UI has been slow to update. This may be a bug or a performance issue or you forgot to call `.done()`."
1212
+ );
1213
+ }, DEV_DEFAULT_STREAMABLE_WARNING_TIME);
1214
+ }
1215
+ }
1216
+ warnUnclosedStream();
1217
+ const streamable2 = {
1218
+ /**
1219
+ * The value of the streamable UI. This can be returned from a Server Action and received by the client.
1220
+ */
1221
+ value: row,
1222
+ /**
1223
+ * This method updates the current UI node. It takes a new UI node and replaces the old one.
1224
+ */
1225
+ update(value) {
1226
+ assertStream(".update()");
1227
+ if (value === currentValue) {
1228
+ warnUnclosedStream();
1229
+ return streamable2;
1230
+ }
1231
+ const resolvable = createResolvablePromise();
1232
+ currentValue = value;
1233
+ resolve({ value: currentValue, done: false, next: resolvable.promise });
1234
+ resolve = resolvable.resolve;
1235
+ reject = resolvable.reject;
1236
+ warnUnclosedStream();
1237
+ return streamable2;
1238
+ },
1239
+ /**
1240
+ * This method is used to append a new UI node to the end of the old one.
1241
+ * Once appended a new UI node, the previous UI node cannot be updated anymore.
1242
+ *
1243
+ * @example
1244
+ * ```jsx
1245
+ * const ui = createStreamableUI(<div>hello</div>)
1246
+ * ui.append(<div>world</div>)
1247
+ *
1248
+ * // The UI node will be:
1249
+ * // <>
1250
+ * // <div>hello</div>
1251
+ * // <div>world</div>
1252
+ * // </>
1253
+ * ```
1254
+ */
1255
+ append(value) {
1256
+ assertStream(".append()");
1257
+ const resolvable = createResolvablePromise();
1258
+ currentValue = value;
1259
+ resolve({ value, done: false, append: true, next: resolvable.promise });
1260
+ resolve = resolvable.resolve;
1261
+ reject = resolvable.reject;
1262
+ warnUnclosedStream();
1263
+ return streamable2;
1264
+ },
1265
+ /**
1266
+ * This method is used to signal that there is an error in the UI stream.
1267
+ * It will be thrown on the client side and caught by the nearest error boundary component.
1268
+ */
1269
+ error(error) {
1270
+ assertStream(".error()");
1271
+ if (warningTimeout) {
1272
+ clearTimeout(warningTimeout);
1273
+ }
1274
+ closed = true;
1275
+ reject(error);
1276
+ return streamable2;
1277
+ },
1278
+ /**
1279
+ * This method marks the UI node as finalized. You can either call it without any parameters or with a new UI node as the final state.
1280
+ * Once called, the UI node cannot be updated or appended anymore.
1281
+ *
1282
+ * This method is always **required** to be called, otherwise the response will be stuck in a loading state.
1283
+ */
1284
+ done(...args) {
1285
+ assertStream(".done()");
1286
+ if (warningTimeout) {
1287
+ clearTimeout(warningTimeout);
1288
+ }
1289
+ closed = true;
1290
+ if (args.length) {
1291
+ resolve({ value: args[0], done: true });
1292
+ return streamable2;
1293
+ }
1294
+ resolve({ value: currentValue, done: true });
1295
+ return streamable2;
1296
+ }
1297
+ };
1298
+ return streamable2;
1299
+ }
1300
+ var STREAMABLE_VALUE_INTERNAL_LOCK = Symbol("streamable.value.lock");
1301
+ function createStreamableValue(initialValue) {
1302
+ const isReadableStream = initialValue instanceof ReadableStream || typeof initialValue === "object" && initialValue !== null && "getReader" in initialValue && typeof initialValue.getReader === "function" && "locked" in initialValue && typeof initialValue.locked === "boolean";
1303
+ if (!isReadableStream) {
1304
+ return createStreamableValueImpl(initialValue);
1305
+ }
1306
+ const streamableValue = createStreamableValueImpl();
1307
+ streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = true;
1308
+ (async () => {
1309
+ try {
1310
+ const reader = initialValue.getReader();
1311
+ while (true) {
1312
+ const { value, done } = await reader.read();
1313
+ if (done) {
1314
+ break;
1315
+ }
1316
+ streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = false;
1317
+ if (typeof value === "string") {
1318
+ streamableValue.append(value);
1319
+ } else {
1320
+ streamableValue.update(value);
1321
+ }
1322
+ streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = true;
1323
+ }
1324
+ streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = false;
1325
+ streamableValue.done();
1326
+ } catch (e) {
1327
+ streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = false;
1328
+ streamableValue.error(e);
1329
+ }
1330
+ })();
1331
+ return streamableValue;
1332
+ }
1333
+ function createStreamableValueImpl(initialValue) {
1334
+ let closed = false;
1335
+ let locked = false;
1336
+ let resolvable = createResolvablePromise();
1337
+ let currentValue = initialValue;
1338
+ let currentError;
1339
+ let currentPromise = resolvable.promise;
1340
+ let currentPatchValue;
1341
+ function assertStream(method) {
1342
+ if (closed) {
1343
+ throw new Error(method + ": Value stream is already closed.");
1344
+ }
1345
+ if (locked) {
1346
+ throw new Error(
1347
+ method + ": Value stream is locked and cannot be updated."
1348
+ );
1349
+ }
1350
+ }
1351
+ let warningTimeout;
1352
+ function warnUnclosedStream() {
1353
+ if (process.env.NODE_ENV === "development") {
1354
+ if (warningTimeout) {
1355
+ clearTimeout(warningTimeout);
1356
+ }
1357
+ warningTimeout = setTimeout(() => {
1358
+ console.warn(
1359
+ "The streamable UI has been slow to update. This may be a bug or a performance issue or you forgot to call `.done()`."
1360
+ );
1361
+ }, DEV_DEFAULT_STREAMABLE_WARNING_TIME);
1362
+ }
1363
+ }
1364
+ warnUnclosedStream();
1365
+ function createWrapped(initialChunk) {
1366
+ let init;
1367
+ if (currentError !== void 0) {
1368
+ init = { error: currentError };
1369
+ } else {
1370
+ if (currentPatchValue && !initialChunk) {
1371
+ init = { diff: currentPatchValue };
1372
+ } else {
1373
+ init = { curr: currentValue };
1374
+ }
1375
+ }
1376
+ if (currentPromise) {
1377
+ init.next = currentPromise;
1378
+ }
1379
+ if (initialChunk) {
1380
+ init.type = STREAMABLE_VALUE_TYPE;
1381
+ }
1382
+ return init;
1383
+ }
1384
+ function updateValueStates(value) {
1385
+ currentPatchValue = void 0;
1386
+ if (typeof value === "string") {
1387
+ if (typeof currentValue === "string") {
1388
+ if (value.startsWith(currentValue)) {
1389
+ currentPatchValue = [0, value.slice(currentValue.length)];
1390
+ }
1391
+ }
1392
+ }
1393
+ currentValue = value;
1394
+ }
1395
+ const streamable2 = {
1396
+ /**
1397
+ * @internal This is an internal lock to prevent the value from being
1398
+ * updated by the user.
1399
+ */
1400
+ set [STREAMABLE_VALUE_INTERNAL_LOCK](state) {
1401
+ locked = state;
1402
+ },
1403
+ /**
1404
+ * The value of the streamable. This can be returned from a Server Action and
1405
+ * received by the client. To read the streamed values, use the
1406
+ * `readStreamableValue` or `useStreamableValue` APIs.
1407
+ */
1408
+ get value() {
1409
+ return createWrapped(true);
1410
+ },
1411
+ /**
1412
+ * This method updates the current value with a new one.
1413
+ */
1414
+ update(value) {
1415
+ assertStream(".update()");
1416
+ const resolvePrevious = resolvable.resolve;
1417
+ resolvable = createResolvablePromise();
1418
+ updateValueStates(value);
1419
+ currentPromise = resolvable.promise;
1420
+ resolvePrevious(createWrapped());
1421
+ warnUnclosedStream();
1422
+ return streamable2;
1423
+ },
1424
+ /**
1425
+ * This method is used to append a delta string to the current value. It
1426
+ * requires the current value of the streamable to be a string.
1427
+ *
1428
+ * @example
1429
+ * ```jsx
1430
+ * const streamable = createStreamableValue('hello');
1431
+ * streamable.append(' world');
1432
+ *
1433
+ * // The value will be 'hello world'
1434
+ * ```
1435
+ */
1436
+ append(value) {
1437
+ assertStream(".append()");
1438
+ if (typeof currentValue !== "string" && typeof currentValue !== "undefined") {
1439
+ throw new Error(
1440
+ `.append(): The current value is not a string. Received: ${typeof currentValue}`
1441
+ );
1442
+ }
1443
+ if (typeof value !== "string") {
1444
+ throw new Error(
1445
+ `.append(): The value is not a string. Received: ${typeof value}`
1446
+ );
1447
+ }
1448
+ const resolvePrevious = resolvable.resolve;
1449
+ resolvable = createResolvablePromise();
1450
+ if (typeof currentValue === "string") {
1451
+ currentPatchValue = [0, value];
1452
+ currentValue = currentValue + value;
1453
+ } else {
1454
+ currentPatchValue = void 0;
1455
+ currentValue = value;
1456
+ }
1457
+ currentPromise = resolvable.promise;
1458
+ resolvePrevious(createWrapped());
1459
+ warnUnclosedStream();
1460
+ return streamable2;
1461
+ },
1462
+ /**
1463
+ * This method is used to signal that there is an error in the value stream.
1464
+ * It will be thrown on the client side when consumed via
1465
+ * `readStreamableValue` or `useStreamableValue`.
1466
+ */
1467
+ error(error) {
1468
+ assertStream(".error()");
1469
+ if (warningTimeout) {
1470
+ clearTimeout(warningTimeout);
1471
+ }
1472
+ closed = true;
1473
+ currentError = error;
1474
+ currentPromise = void 0;
1475
+ resolvable.resolve({ error });
1476
+ return streamable2;
1477
+ },
1478
+ /**
1479
+ * This method marks the value as finalized. You can either call it without
1480
+ * any parameters or with a new value as the final state.
1481
+ * Once called, the value cannot be updated or appended anymore.
1482
+ *
1483
+ * This method is always **required** to be called, otherwise the response
1484
+ * will be stuck in a loading state.
1485
+ */
1486
+ done(...args) {
1487
+ assertStream(".done()");
1488
+ if (warningTimeout) {
1489
+ clearTimeout(warningTimeout);
1490
+ }
1491
+ closed = true;
1492
+ currentPromise = void 0;
1493
+ if (args.length) {
1494
+ updateValueStates(args[0]);
1495
+ resolvable.resolve(createWrapped());
1496
+ return streamable2;
1497
+ }
1498
+ resolvable.resolve({});
1499
+ return streamable2;
1500
+ }
1501
+ };
1502
+ return streamable2;
1503
+ }
1504
+ function render(options) {
1505
+ const ui = createStreamableUI(options.initial);
1506
+ const text = options.text ? options.text : ({ content }) => content;
1507
+ const functions = options.functions ? Object.entries(options.functions).map(
1508
+ ([name, { description, parameters }]) => {
1509
+ return {
1510
+ name,
1511
+ description,
1512
+ parameters: zodToJsonSchema2(parameters)
1513
+ };
1514
+ }
1515
+ ) : void 0;
1516
+ const tools = options.tools ? Object.entries(options.tools).map(
1517
+ ([name, { description, parameters }]) => {
1518
+ return {
1519
+ type: "function",
1520
+ function: {
1521
+ name,
1522
+ description,
1523
+ parameters: zodToJsonSchema2(parameters)
1524
+ }
1525
+ };
1526
+ }
1527
+ ) : void 0;
1528
+ if (functions && tools) {
1529
+ throw new Error(
1530
+ "You can't have both functions and tools defined. Please choose one or the other."
1531
+ );
1532
+ }
1533
+ let finished;
1534
+ async function handleRender(args, renderer, res) {
1535
+ if (!renderer)
1536
+ return;
1537
+ const resolvable = createResolvablePromise();
1538
+ if (finished) {
1539
+ finished = finished.then(() => resolvable.promise);
1540
+ } else {
1541
+ finished = resolvable.promise;
1542
+ }
1543
+ const value = renderer(args);
1544
+ if (value instanceof Promise || value && typeof value === "object" && "then" in value && typeof value.then === "function") {
1545
+ const node = await value;
1546
+ res.update(node);
1547
+ resolvable.resolve(void 0);
1548
+ } else if (value && typeof value === "object" && Symbol.asyncIterator in value) {
1549
+ const it = value;
1550
+ while (true) {
1551
+ const { done, value: value2 } = await it.next();
1552
+ res.update(value2);
1553
+ if (done)
1554
+ break;
1555
+ }
1556
+ resolvable.resolve(void 0);
1557
+ } else if (value && typeof value === "object" && Symbol.iterator in value) {
1558
+ const it = value;
1559
+ while (true) {
1560
+ const { done, value: value2 } = it.next();
1561
+ res.update(value2);
1562
+ if (done)
1563
+ break;
1564
+ }
1565
+ resolvable.resolve(void 0);
1566
+ } else {
1567
+ res.update(value);
1568
+ resolvable.resolve(void 0);
1569
+ }
1570
+ }
1571
+ (async () => {
1572
+ let hasFunction = false;
1573
+ let content = "";
1574
+ consumeStream(
1575
+ OpenAIStream(
1576
+ await options.provider.chat.completions.create({
1577
+ model: options.model,
1578
+ messages: options.messages,
1579
+ temperature: options.temperature,
1580
+ stream: true,
1581
+ ...functions ? {
1582
+ functions
1583
+ } : {},
1584
+ ...tools ? {
1585
+ tools
1586
+ } : {}
1587
+ }),
1588
+ {
1589
+ ...functions ? {
1590
+ async experimental_onFunctionCall(functionCallPayload) {
1591
+ var _a, _b;
1592
+ hasFunction = true;
1593
+ handleRender(
1594
+ functionCallPayload.arguments,
1595
+ (_b = (_a = options.functions) == null ? void 0 : _a[functionCallPayload.name]) == null ? void 0 : _b.render,
1596
+ ui
1597
+ );
1598
+ }
1599
+ } : {},
1600
+ ...tools ? {
1601
+ async experimental_onToolCall(toolCallPayload) {
1602
+ var _a, _b;
1603
+ hasFunction = true;
1604
+ for (const tool of toolCallPayload.tools) {
1605
+ handleRender(
1606
+ tool.func.arguments,
1607
+ (_b = (_a = options.tools) == null ? void 0 : _a[tool.func.name]) == null ? void 0 : _b.render,
1608
+ ui
1609
+ );
1610
+ }
1611
+ }
1612
+ } : {},
1613
+ onText(chunk) {
1614
+ content += chunk;
1615
+ handleRender({ content, done: false, delta: chunk }, text, ui);
1616
+ },
1617
+ async onFinal() {
1618
+ if (hasFunction) {
1619
+ await finished;
1620
+ ui.done();
1621
+ return;
1622
+ }
1623
+ handleRender({ content, done: true }, text, ui);
1624
+ await finished;
1625
+ ui.done();
1626
+ }
1627
+ }
1628
+ )
1629
+ );
1630
+ })();
1631
+ return ui.value;
1632
+ }
1633
+
1634
+ // rsc/stream-ui/stream-ui.tsx
1635
+ import {
1636
+ InvalidToolArgumentsError,
1637
+ NoSuchToolError
1638
+ } from "@ai-sdk/provider";
1639
+ import { safeParseJSON } from "@ai-sdk/provider-utils";
1640
+ var defaultTextRenderer = ({ content }) => content;
1641
+ async function streamUI({
1642
+ model,
1643
+ tools,
1644
+ system,
1645
+ prompt,
1646
+ messages,
1647
+ maxRetries,
1648
+ abortSignal,
1649
+ initial,
1650
+ text,
1651
+ ...settings
1652
+ }) {
1653
+ if (typeof model === "string") {
1654
+ throw new Error(
1655
+ "`model` cannot be a string in `streamUI`. Use the actual model instance instead."
1656
+ );
1657
+ }
1658
+ if ("functions" in settings) {
1659
+ throw new Error(
1660
+ "`functions` is not supported in `streamUI`, use `tools` instead."
1661
+ );
1662
+ }
1663
+ if ("provider" in settings) {
1664
+ throw new Error(
1665
+ "`provider` is no longer needed in `streamUI`. Use `model` instead."
1666
+ );
1667
+ }
1668
+ if (tools) {
1669
+ for (const [name, tool] of Object.entries(tools)) {
1670
+ if ("render" in tool) {
1671
+ throw new Error(
1672
+ "Tool definition in `streamUI` should not have `render` property. Use `generate` instead. Found in tool: " + name
1673
+ );
1674
+ }
1675
+ }
1676
+ }
1677
+ const ui = createStreamableUI(initial);
1678
+ const textRender = text || defaultTextRenderer;
1679
+ let finished;
1680
+ async function handleRender(args, renderer, res) {
1681
+ if (!renderer)
1682
+ return;
1683
+ const resolvable = createResolvablePromise();
1684
+ if (finished) {
1685
+ finished = finished.then(() => resolvable.promise);
1686
+ } else {
1687
+ finished = resolvable.promise;
1688
+ }
1689
+ const value = renderer(...args);
1690
+ if (value instanceof Promise || value && typeof value === "object" && "then" in value && typeof value.then === "function") {
1691
+ const node = await value;
1692
+ res.update(node);
1693
+ resolvable.resolve(void 0);
1694
+ } else if (value && typeof value === "object" && Symbol.asyncIterator in value) {
1695
+ const it = value;
1696
+ while (true) {
1697
+ const { done, value: value2 } = await it.next();
1698
+ res.update(value2);
1699
+ if (done)
1700
+ break;
1701
+ }
1702
+ resolvable.resolve(void 0);
1703
+ } else if (value && typeof value === "object" && Symbol.iterator in value) {
1704
+ const it = value;
1705
+ while (true) {
1706
+ const { done, value: value2 } = it.next();
1707
+ res.update(value2);
1708
+ if (done)
1709
+ break;
1710
+ }
1711
+ resolvable.resolve(void 0);
1712
+ } else {
1713
+ res.update(value);
1714
+ resolvable.resolve(void 0);
1715
+ }
1716
+ }
1717
+ const retry = retryWithExponentialBackoff({ maxRetries });
1718
+ const validatedPrompt = getValidatedPrompt({ system, prompt, messages });
1719
+ const result = await retry(
1720
+ () => model.doStream({
1721
+ mode: {
1722
+ type: "regular",
1723
+ tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool]) => ({
1724
+ type: "function",
1725
+ name,
1726
+ description: tool.description,
1727
+ parameters: convertZodToJSONSchema(tool.parameters)
1728
+ }))
1729
+ },
1730
+ ...prepareCallSettings(settings),
1731
+ inputFormat: validatedPrompt.type,
1732
+ prompt: convertToLanguageModelPrompt(validatedPrompt),
1733
+ abortSignal
1734
+ })
1735
+ );
1736
+ const [stream, forkedStream] = result.stream.tee();
1737
+ (async () => {
1738
+ try {
1739
+ let content = "";
1740
+ let hasToolCall = false;
1741
+ const reader = forkedStream.getReader();
1742
+ while (true) {
1743
+ const { done, value } = await reader.read();
1744
+ if (done)
1745
+ break;
1746
+ switch (value.type) {
1747
+ case "text-delta": {
1748
+ content += value.textDelta;
1749
+ handleRender(
1750
+ [{ content, done: false, delta: value.textDelta }],
1751
+ textRender,
1752
+ ui
1753
+ );
1754
+ break;
1755
+ }
1756
+ case "tool-call-delta": {
1757
+ hasToolCall = true;
1758
+ break;
1759
+ }
1760
+ case "tool-call": {
1761
+ const toolName = value.toolName;
1762
+ if (!tools) {
1763
+ throw new NoSuchToolError({ toolName });
1764
+ }
1765
+ const tool = tools[toolName];
1766
+ if (!tool) {
1767
+ throw new NoSuchToolError({
1768
+ toolName,
1769
+ availableTools: Object.keys(tools)
1770
+ });
1771
+ }
1772
+ const parseResult = safeParseJSON({
1773
+ text: value.args,
1774
+ schema: tool.parameters
1775
+ });
1776
+ if (parseResult.success === false) {
1777
+ throw new InvalidToolArgumentsError({
1778
+ toolName,
1779
+ toolArgs: value.args,
1780
+ cause: parseResult.error
1781
+ });
1782
+ }
1783
+ handleRender(
1784
+ [
1785
+ parseResult.value,
1786
+ {
1787
+ toolName,
1788
+ toolCallId: value.toolCallId
1789
+ }
1790
+ ],
1791
+ tool.generate,
1792
+ ui
1793
+ );
1794
+ break;
1795
+ }
1796
+ case "error": {
1797
+ throw value.error;
1798
+ }
1799
+ case "finish": {
1800
+ }
1801
+ }
1802
+ }
1803
+ if (hasToolCall) {
1804
+ await finished;
1805
+ ui.done();
1806
+ } else {
1807
+ handleRender([{ content, done: true }], textRender, ui);
1808
+ await finished;
1809
+ ui.done();
1810
+ }
1811
+ } catch (error) {
1812
+ ui.error(error);
1813
+ }
1814
+ })();
1815
+ return {
1816
+ ...result,
1817
+ stream,
1818
+ value: ui.value
1819
+ };
1820
+ }
1821
+
1822
+ // rsc/provider.tsx
1823
+ import * as React2 from "react";
1824
+ import { InternalAIProvider } from "./rsc-shared.mjs";
1825
+ import { jsx as jsx2 } from "react/jsx-runtime";
1826
+ async function innerAction({
1827
+ action,
1828
+ options
1829
+ }, state, ...args) {
1830
+ "use server";
1831
+ return await withAIState(
1832
+ {
1833
+ state,
1834
+ options
1835
+ },
1836
+ async () => {
1837
+ const result = await action(...args);
1838
+ sealMutableAIState();
1839
+ return [getAIStateDeltaPromise(), result];
1840
+ }
1841
+ );
1842
+ }
1843
+ function wrapAction(action, options) {
1844
+ return innerAction.bind(null, { action, options });
1845
+ }
1846
+ function createAI({
1847
+ actions,
1848
+ initialAIState,
1849
+ initialUIState,
1850
+ onSetAIState,
1851
+ onGetUIState
1852
+ }) {
1853
+ const wrappedActions = {};
1854
+ for (const name in actions) {
1855
+ wrappedActions[name] = wrapAction(actions[name], {
1856
+ onSetAIState
1857
+ });
1858
+ }
1859
+ const wrappedSyncUIState = onGetUIState ? wrapAction(onGetUIState, {}) : void 0;
1860
+ const AI = async (props) => {
1861
+ var _a, _b;
1862
+ if ("useState" in React2) {
1863
+ throw new Error(
1864
+ "This component can only be used inside Server Components."
1865
+ );
1866
+ }
1867
+ let uiState = (_a = props.initialUIState) != null ? _a : initialUIState;
1868
+ let aiState = (_b = props.initialAIState) != null ? _b : initialAIState;
1869
+ let aiStateDelta = void 0;
1870
+ if (wrappedSyncUIState) {
1871
+ const [newAIStateDelta, newUIState] = await wrappedSyncUIState(aiState);
1872
+ if (newUIState !== void 0) {
1873
+ aiStateDelta = newAIStateDelta;
1874
+ uiState = newUIState;
1875
+ }
1876
+ }
1877
+ return /* @__PURE__ */ jsx2(
1878
+ InternalAIProvider,
1879
+ {
1880
+ wrappedActions,
1881
+ wrappedSyncUIState,
1882
+ initialUIState: uiState,
1883
+ initialAIState: aiState,
1884
+ initialAIStatePatch: aiStateDelta,
1885
+ children: props.children
1886
+ }
1887
+ );
1888
+ };
1889
+ return AI;
1890
+ }
1891
+ export {
1892
+ createAI,
1893
+ createStreamableUI,
1894
+ createStreamableValue,
1895
+ getAIState,
1896
+ getMutableAIState,
1897
+ render,
1898
+ streamUI
1899
+ };
1900
+ //# sourceMappingURL=rsc-server.mjs.map