ai 3.0.34 → 3.0.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "3.0.34",
3
+ "version": "3.0.35",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -57,8 +57,8 @@
57
57
  }
58
58
  },
59
59
  "dependencies": {
60
- "@ai-sdk/provider": "0.0.2",
61
- "@ai-sdk/provider-utils": "0.0.4",
60
+ "@ai-sdk/provider": "0.0.3",
61
+ "@ai-sdk/provider-utils": "0.0.5",
62
62
  "secure-json-parse": "2.7.0",
63
63
  "eventsource-parser": "1.1.2",
64
64
  "jsondiffpatch": "0.6.0",
@@ -109,11 +109,17 @@ declare function createStreamableUI(initialValue?: React.ReactNode): {
109
109
  */
110
110
  done(...args: [] | [React.ReactNode]): void;
111
111
  };
112
+ declare const STREAMABLE_VALUE_INTERNAL_LOCK: unique symbol;
112
113
  /**
113
114
  * Create a wrapped, changable value that can be streamed to the client.
114
115
  * On the client side, the value can be accessed via the readStreamableValue() API.
115
116
  */
116
- declare function createStreamableValue<T = any, E = any>(initialValue?: T): {
117
+ declare function createStreamableValue<T = any, E = any>(initialValue?: T | ReadableStream<T>): {
118
+ /**
119
+ * @internal This is an internal lock to prevent the value from being
120
+ * updated by the user.
121
+ */
122
+ [STREAMABLE_VALUE_INTERNAL_LOCK]: boolean;
117
123
  /**
118
124
  * The value of the streamable. This can be returned from a Server Action and
119
125
  * received by the client. To read the streamed values, use the
@@ -124,10 +130,36 @@ declare function createStreamableValue<T = any, E = any>(initialValue?: T): {
124
130
  * This method updates the current value with a new one.
125
131
  */
126
132
  update(value: T): void;
133
+ /**
134
+ * This method is used to append a delta string to the current value. It
135
+ * requires the current value of the streamable to be a string.
136
+ *
137
+ * @example
138
+ * ```jsx
139
+ * const streamable = createStreamableValue('hello');
140
+ * streamable.append(' world');
141
+ *
142
+ * // The value will be 'hello world'
143
+ * ```
144
+ */
145
+ append(value: T): void;
146
+ /**
147
+ * This method is used to signal that there is an error in the value stream.
148
+ * It will be thrown on the client side when consumed via
149
+ * `readStreamableValue` or `useStreamableValue`.
150
+ */
127
151
  error(error: any): void;
128
- done(...args: [
129
- ] | [T]): void;
152
+ /**
153
+ * This method marks the value as finalized. You can either call it without
154
+ * any parameters or with a new value as the final state.
155
+ * Once called, the value cannot be updated or appended anymore.
156
+ *
157
+ * This method is always **required** to be called, otherwise the response
158
+ * will be stuck in a loading state.
159
+ */
160
+ done(...args: [] | [T]): void;
130
161
  };
162
+
131
163
  type Streamable$1 = ReactNode | Promise<ReactNode>;
132
164
  type Renderer$1<T> = (props: T) => Streamable$1 | Generator<Streamable$1, Streamable$1, void> | AsyncGenerator<Streamable$1, Streamable$1, void>;
133
165
  /**
@@ -413,7 +445,9 @@ type RenderResult = {
413
445
  /**
414
446
  * `experimental_streamUI` is a helper function to create a streamable UI from LLMs.
415
447
  */
416
- declare function experimental_streamUI<TOOLS extends Record<string, RenderTool>>({ model, tools, system, prompt, messages, maxRetries, abortSignal, initial, text, ...settings }: CallSettings & Prompt & {
448
+ declare function experimental_streamUI<TOOLS extends {
449
+ [name: string]: z.ZodTypeAny;
450
+ } = {}>({ model, tools, system, prompt, messages, maxRetries, abortSignal, initial, text, ...settings }: CallSettings & Prompt & {
417
451
  /**
418
452
  * The language model to use.
419
453
  */
@@ -421,7 +455,9 @@ declare function experimental_streamUI<TOOLS extends Record<string, RenderTool>>
421
455
  /**
422
456
  * The tools that the model can call. The model needs to support calling tools.
423
457
  */
424
- tools?: TOOLS;
458
+ tools?: {
459
+ [name in keyof TOOLS]: RenderTool<TOOLS[name]>;
460
+ };
425
461
  text?: RenderText;
426
462
  initial?: ReactNode;
427
463
  }): Promise<RenderResult>;
@@ -107,11 +107,17 @@ declare function createStreamableUI(initialValue?: React.ReactNode): {
107
107
  */
108
108
  done(...args: [] | [React.ReactNode]): void;
109
109
  };
110
+ declare const STREAMABLE_VALUE_INTERNAL_LOCK: unique symbol;
110
111
  /**
111
112
  * Create a wrapped, changable value that can be streamed to the client.
112
113
  * On the client side, the value can be accessed via the readStreamableValue() API.
113
114
  */
114
- declare function createStreamableValue<T = any, E = any>(initialValue?: T): {
115
+ declare function createStreamableValue<T = any, E = any>(initialValue?: T | ReadableStream<T>): {
116
+ /**
117
+ * @internal This is an internal lock to prevent the value from being
118
+ * updated by the user.
119
+ */
120
+ [STREAMABLE_VALUE_INTERNAL_LOCK]: boolean;
115
121
  /**
116
122
  * The value of the streamable. This can be returned from a Server Action and
117
123
  * received by the client. To read the streamed values, use the
@@ -122,10 +128,36 @@ declare function createStreamableValue<T = any, E = any>(initialValue?: T): {
122
128
  * This method updates the current value with a new one.
123
129
  */
124
130
  update(value: T): void;
131
+ /**
132
+ * This method is used to append a delta string to the current value. It
133
+ * requires the current value of the streamable to be a string.
134
+ *
135
+ * @example
136
+ * ```jsx
137
+ * const streamable = createStreamableValue('hello');
138
+ * streamable.append(' world');
139
+ *
140
+ * // The value will be 'hello world'
141
+ * ```
142
+ */
143
+ append(value: T): void;
144
+ /**
145
+ * This method is used to signal that there is an error in the value stream.
146
+ * It will be thrown on the client side when consumed via
147
+ * `readStreamableValue` or `useStreamableValue`.
148
+ */
125
149
  error(error: any): void;
126
- done(...args: [
127
- ] | [T]): void;
150
+ /**
151
+ * This method marks the value as finalized. You can either call it without
152
+ * any parameters or with a new value as the final state.
153
+ * Once called, the value cannot be updated or appended anymore.
154
+ *
155
+ * This method is always **required** to be called, otherwise the response
156
+ * will be stuck in a loading state.
157
+ */
158
+ done(...args: [] | [T]): void;
128
159
  };
160
+
129
161
  type Streamable$1 = ReactNode | Promise<ReactNode>;
130
162
  type Renderer$1<T> = (props: T) => Streamable$1 | Generator<Streamable$1, Streamable$1, void> | AsyncGenerator<Streamable$1, Streamable$1, void>;
131
163
  /**
@@ -411,7 +443,9 @@ type RenderResult = {
411
443
  /**
412
444
  * `experimental_streamUI` is a helper function to create a streamable UI from LLMs.
413
445
  */
414
- declare function experimental_streamUI<TOOLS extends Record<string, RenderTool>>({ model, tools, system, prompt, messages, maxRetries, abortSignal, initial, text, ...settings }: CallSettings & Prompt & {
446
+ declare function experimental_streamUI<TOOLS extends {
447
+ [name: string]: z.ZodTypeAny;
448
+ } = {}>({ model, tools, system, prompt, messages, maxRetries, abortSignal, initial, text, ...settings }: CallSettings & Prompt & {
415
449
  /**
416
450
  * The language model to use.
417
451
  */
@@ -419,7 +453,9 @@ declare function experimental_streamUI<TOOLS extends Record<string, RenderTool>>
419
453
  /**
420
454
  * The tools that the model can call. The model needs to support calling tools.
421
455
  */
422
- tools?: TOOLS;
456
+ tools?: {
457
+ [name in keyof TOOLS]: RenderTool<TOOLS[name]>;
458
+ };
423
459
  text?: RenderText;
424
460
  initial?: ReactNode;
425
461
  }): Promise<RenderResult>;
@@ -1245,8 +1245,42 @@ function createStreamableUI(initialValue) {
1245
1245
  }
1246
1246
  };
1247
1247
  }
1248
+ var STREAMABLE_VALUE_INTERNAL_LOCK = Symbol("streamable.value.lock");
1248
1249
  function createStreamableValue(initialValue) {
1250
+ const isReadableStream = initialValue instanceof ReadableStream || typeof initialValue === "object" && initialValue !== null && "getReader" in initialValue && typeof initialValue.getReader === "function" && "locked" in initialValue && typeof initialValue.locked === "boolean";
1251
+ if (!isReadableStream) {
1252
+ return createStreamableValueImpl(initialValue);
1253
+ }
1254
+ const streamableValue = createStreamableValueImpl();
1255
+ streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = true;
1256
+ (async () => {
1257
+ try {
1258
+ const reader = initialValue.getReader();
1259
+ while (true) {
1260
+ const { value, done } = await reader.read();
1261
+ if (done) {
1262
+ break;
1263
+ }
1264
+ streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = false;
1265
+ if (typeof value === "string") {
1266
+ streamableValue.append(value);
1267
+ } else {
1268
+ streamableValue.update(value);
1269
+ }
1270
+ streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = true;
1271
+ }
1272
+ streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = false;
1273
+ streamableValue.done();
1274
+ } catch (e) {
1275
+ streamableValue[STREAMABLE_VALUE_INTERNAL_LOCK] = false;
1276
+ streamableValue.error(e);
1277
+ }
1278
+ })();
1279
+ return streamableValue;
1280
+ }
1281
+ function createStreamableValueImpl(initialValue) {
1249
1282
  let closed = false;
1283
+ let locked = false;
1250
1284
  let resolvable = createResolvablePromise();
1251
1285
  let currentValue = initialValue;
1252
1286
  let currentError;
@@ -1256,6 +1290,11 @@ function createStreamableValue(initialValue) {
1256
1290
  if (closed) {
1257
1291
  throw new Error(method + ": Value stream is already closed.");
1258
1292
  }
1293
+ if (locked) {
1294
+ throw new Error(
1295
+ method + ": Value stream is locked and cannot be updated."
1296
+ );
1297
+ }
1259
1298
  }
1260
1299
  let warningTimeout;
1261
1300
  function warnUnclosedStream() {
@@ -1302,6 +1341,13 @@ function createStreamableValue(initialValue) {
1302
1341
  currentValue = value;
1303
1342
  }
1304
1343
  return {
1344
+ /**
1345
+ * @internal This is an internal lock to prevent the value from being
1346
+ * updated by the user.
1347
+ */
1348
+ set [STREAMABLE_VALUE_INTERNAL_LOCK](state) {
1349
+ locked = state;
1350
+ },
1305
1351
  /**
1306
1352
  * The value of the streamable. This can be returned from a Server Action and
1307
1353
  * received by the client. To read the streamed values, use the
@@ -1322,6 +1368,48 @@ function createStreamableValue(initialValue) {
1322
1368
  resolvePrevious(createWrapped());
1323
1369
  warnUnclosedStream();
1324
1370
  },
1371
+ /**
1372
+ * This method is used to append a delta string to the current value. It
1373
+ * requires the current value of the streamable to be a string.
1374
+ *
1375
+ * @example
1376
+ * ```jsx
1377
+ * const streamable = createStreamableValue('hello');
1378
+ * streamable.append(' world');
1379
+ *
1380
+ * // The value will be 'hello world'
1381
+ * ```
1382
+ */
1383
+ append(value) {
1384
+ assertStream(".append()");
1385
+ if (typeof currentValue !== "string" && typeof currentValue !== "undefined") {
1386
+ throw new Error(
1387
+ `.append(): The current value is not a string. Received: ${typeof currentValue}`
1388
+ );
1389
+ }
1390
+ if (typeof value !== "string") {
1391
+ throw new Error(
1392
+ `.append(): The value is not a string. Received: ${typeof value}`
1393
+ );
1394
+ }
1395
+ const resolvePrevious = resolvable.resolve;
1396
+ resolvable = createResolvablePromise();
1397
+ if (typeof currentValue === "string") {
1398
+ currentPatchValue = [0, value];
1399
+ currentValue = currentValue + value;
1400
+ } else {
1401
+ currentPatchValue = void 0;
1402
+ currentValue = value;
1403
+ }
1404
+ currentPromise = resolvable.promise;
1405
+ resolvePrevious(createWrapped());
1406
+ warnUnclosedStream();
1407
+ },
1408
+ /**
1409
+ * This method is used to signal that there is an error in the value stream.
1410
+ * It will be thrown on the client side when consumed via
1411
+ * `readStreamableValue` or `useStreamableValue`.
1412
+ */
1325
1413
  error(error) {
1326
1414
  assertStream(".error()");
1327
1415
  if (warningTimeout) {
@@ -1332,6 +1420,14 @@ function createStreamableValue(initialValue) {
1332
1420
  currentPromise = void 0;
1333
1421
  resolvable.resolve({ error });
1334
1422
  },
1423
+ /**
1424
+ * This method marks the value as finalized. You can either call it without
1425
+ * any parameters or with a new value as the final state.
1426
+ * Once called, the value cannot be updated or appended anymore.
1427
+ *
1428
+ * This method is always **required** to be called, otherwise the response
1429
+ * will be stuck in a loading state.
1430
+ */
1335
1431
  done(...args) {
1336
1432
  assertStream(".done()");
1337
1433
  if (warningTimeout) {