ai 3.1.28 → 3.1.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -582,7 +582,7 @@ If set and supported by the model, calls will generate deterministic results.
582
582
  @return
583
583
  A result object for accessing the partial object stream and additional information.
584
584
  */
585
- declare function streamObject<T>({ model, schema, mode, system, prompt, messages, maxRetries, abortSignal, ...settings }: CallSettings & Prompt & {
585
+ declare function streamObject<T>({ model, schema, mode, system, prompt, messages, maxRetries, abortSignal, onFinish, ...settings }: CallSettings & Prompt & {
586
586
  /**
587
587
  The language model to use.
588
588
  */
@@ -606,6 +606,36 @@ Please note that most providers do not support all modes.
606
606
  Default and recommended: 'auto' (best mode for the model).
607
607
  */
608
608
  mode?: 'auto' | 'json' | 'tool' | 'grammar';
609
+ /**
610
+ Callback that is called when the LLM response and the final object validation are finished.
611
+ */
612
+ onFinish?: (event: {
613
+ /**
614
+ The token usage of the generated response.
615
+ */
616
+ usage: TokenUsage;
617
+ /**
618
+ The generated object (typed according to the schema). Can be undefined if the final object does not match the schema.
619
+ */
620
+ object: T | undefined;
621
+ /**
622
+ Optional error object. This is e.g. a TypeValidationError when the final object does not match the schema.
623
+ */
624
+ error: unknown | undefined;
625
+ /**
626
+ Optional raw response data.
627
+ */
628
+ rawResponse?: {
629
+ /**
630
+ Response headers.
631
+ */
632
+ headers?: Record<string, string>;
633
+ };
634
+ /**
635
+ Warnings from the model provider (e.g. unsupported settings).
636
+ */
637
+ warnings?: CallWarning[];
638
+ }) => Promise<void> | void;
609
639
  }): Promise<StreamObjectResult<T>>;
610
640
  type ObjectStreamInputPart = {
611
641
  type: 'error';
@@ -650,13 +680,14 @@ declare class StreamObjectResult<T> {
650
680
  */
651
681
  headers?: Record<string, string>;
652
682
  };
653
- constructor({ stream, warnings, rawResponse, schema, }: {
683
+ constructor({ stream, warnings, rawResponse, schema, onFinish, }: {
654
684
  stream: ReadableStream<string | ObjectStreamInputPart>;
655
685
  warnings: CallWarning[] | undefined;
656
686
  rawResponse?: {
657
687
  headers?: Record<string, string>;
658
688
  };
659
689
  schema: z.Schema<T>;
690
+ onFinish: Parameters<typeof streamObject<T>>[0]['onFinish'];
660
691
  });
661
692
  get partialObjectStream(): AsyncIterableStream<DeepPartial<T>>;
662
693
  get fullStream(): AsyncIterableStream<ObjectStreamPart<T>>;
package/dist/index.d.ts CHANGED
@@ -582,7 +582,7 @@ If set and supported by the model, calls will generate deterministic results.
582
582
  @return
583
583
  A result object for accessing the partial object stream and additional information.
584
584
  */
585
- declare function streamObject<T>({ model, schema, mode, system, prompt, messages, maxRetries, abortSignal, ...settings }: CallSettings & Prompt & {
585
+ declare function streamObject<T>({ model, schema, mode, system, prompt, messages, maxRetries, abortSignal, onFinish, ...settings }: CallSettings & Prompt & {
586
586
  /**
587
587
  The language model to use.
588
588
  */
@@ -606,6 +606,36 @@ Please note that most providers do not support all modes.
606
606
  Default and recommended: 'auto' (best mode for the model).
607
607
  */
608
608
  mode?: 'auto' | 'json' | 'tool' | 'grammar';
609
+ /**
610
+ Callback that is called when the LLM response and the final object validation are finished.
611
+ */
612
+ onFinish?: (event: {
613
+ /**
614
+ The token usage of the generated response.
615
+ */
616
+ usage: TokenUsage;
617
+ /**
618
+ The generated object (typed according to the schema). Can be undefined if the final object does not match the schema.
619
+ */
620
+ object: T | undefined;
621
+ /**
622
+ Optional error object. This is e.g. a TypeValidationError when the final object does not match the schema.
623
+ */
624
+ error: unknown | undefined;
625
+ /**
626
+ Optional raw response data.
627
+ */
628
+ rawResponse?: {
629
+ /**
630
+ Response headers.
631
+ */
632
+ headers?: Record<string, string>;
633
+ };
634
+ /**
635
+ Warnings from the model provider (e.g. unsupported settings).
636
+ */
637
+ warnings?: CallWarning[];
638
+ }) => Promise<void> | void;
609
639
  }): Promise<StreamObjectResult<T>>;
610
640
  type ObjectStreamInputPart = {
611
641
  type: 'error';
@@ -650,13 +680,14 @@ declare class StreamObjectResult<T> {
650
680
  */
651
681
  headers?: Record<string, string>;
652
682
  };
653
- constructor({ stream, warnings, rawResponse, schema, }: {
683
+ constructor({ stream, warnings, rawResponse, schema, onFinish, }: {
654
684
  stream: ReadableStream<string | ObjectStreamInputPart>;
655
685
  warnings: CallWarning[] | undefined;
656
686
  rawResponse?: {
657
687
  headers?: Record<string, string>;
658
688
  };
659
689
  schema: z.Schema<T>;
690
+ onFinish: Parameters<typeof streamObject<T>>[0]['onFinish'];
660
691
  });
661
692
  get partialObjectStream(): AsyncIterableStream<DeepPartial<T>>;
662
693
  get fullStream(): AsyncIterableStream<ObjectStreamPart<T>>;
package/dist/index.js CHANGED
@@ -1090,6 +1090,7 @@ async function streamObject({
1090
1090
  messages,
1091
1091
  maxRetries,
1092
1092
  abortSignal,
1093
+ onFinish,
1093
1094
  ...settings
1094
1095
  }) {
1095
1096
  const retry = retryWithExponentialBackoff({ maxRetries });
@@ -1205,7 +1206,8 @@ async function streamObject({
1205
1206
  stream: result.stream.pipeThrough(new TransformStream(transformer)),
1206
1207
  warnings: result.warnings,
1207
1208
  rawResponse: result.rawResponse,
1208
- schema
1209
+ schema,
1210
+ onFinish
1209
1211
  });
1210
1212
  }
1211
1213
  var StreamObjectResult = class {
@@ -1213,7 +1215,8 @@ var StreamObjectResult = class {
1213
1215
  stream,
1214
1216
  warnings,
1215
1217
  rawResponse,
1216
- schema
1218
+ schema,
1219
+ onFinish
1217
1220
  }) {
1218
1221
  this.warnings = warnings;
1219
1222
  this.rawResponse = rawResponse;
@@ -1228,6 +1231,8 @@ var StreamObjectResult = class {
1228
1231
  resolveUsage = resolve;
1229
1232
  });
1230
1233
  let usage;
1234
+ let object;
1235
+ let error;
1231
1236
  let accumulatedText = "";
1232
1237
  let latestObject = void 0;
1233
1238
  this.originalStream = stream.pipeThrough(
@@ -1254,9 +1259,11 @@ var StreamObjectResult = class {
1254
1259
  schema
1255
1260
  });
1256
1261
  if (validationResult.success) {
1257
- resolveObject(validationResult.value);
1262
+ object = validationResult.value;
1263
+ resolveObject(object);
1258
1264
  } else {
1259
- rejectObject(validationResult.error);
1265
+ error = validationResult.error;
1266
+ rejectObject(error);
1260
1267
  }
1261
1268
  break;
1262
1269
  }
@@ -1265,6 +1272,24 @@ var StreamObjectResult = class {
1265
1272
  break;
1266
1273
  }
1267
1274
  }
1275
+ },
1276
+ // invoke onFinish callback and resolve toolResults promise when the stream is about to close:
1277
+ async flush(controller) {
1278
+ try {
1279
+ await (onFinish == null ? void 0 : onFinish({
1280
+ usage: usage != null ? usage : {
1281
+ promptTokens: NaN,
1282
+ completionTokens: NaN,
1283
+ totalTokens: NaN
1284
+ },
1285
+ object,
1286
+ error,
1287
+ rawResponse,
1288
+ warnings
1289
+ }));
1290
+ } catch (error2) {
1291
+ controller.error(error2);
1292
+ }
1268
1293
  }
1269
1294
  })
1270
1295
  );