ai 3.1.28 → 3.1.30

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
@@ -379,7 +379,13 @@ function convertToLanguageModelMessage(message) {
379
379
  content: [{ type: "text", text: message.content }]
380
380
  };
381
381
  }
382
- return { role: "assistant", content: message.content };
382
+ return {
383
+ role: "assistant",
384
+ content: message.content.filter(
385
+ // remove empty text parts:
386
+ (part) => part.type !== "text" || part.text !== ""
387
+ )
388
+ };
383
389
  }
384
390
  case "tool": {
385
391
  return message;
@@ -1090,6 +1096,7 @@ async function streamObject({
1090
1096
  messages,
1091
1097
  maxRetries,
1092
1098
  abortSignal,
1099
+ onFinish,
1093
1100
  ...settings
1094
1101
  }) {
1095
1102
  const retry = retryWithExponentialBackoff({ maxRetries });
@@ -1205,7 +1212,8 @@ async function streamObject({
1205
1212
  stream: result.stream.pipeThrough(new TransformStream(transformer)),
1206
1213
  warnings: result.warnings,
1207
1214
  rawResponse: result.rawResponse,
1208
- schema
1215
+ schema,
1216
+ onFinish
1209
1217
  });
1210
1218
  }
1211
1219
  var StreamObjectResult = class {
@@ -1213,7 +1221,8 @@ var StreamObjectResult = class {
1213
1221
  stream,
1214
1222
  warnings,
1215
1223
  rawResponse,
1216
- schema
1224
+ schema,
1225
+ onFinish
1217
1226
  }) {
1218
1227
  this.warnings = warnings;
1219
1228
  this.rawResponse = rawResponse;
@@ -1228,6 +1237,8 @@ var StreamObjectResult = class {
1228
1237
  resolveUsage = resolve;
1229
1238
  });
1230
1239
  let usage;
1240
+ let object;
1241
+ let error;
1231
1242
  let accumulatedText = "";
1232
1243
  let latestObject = void 0;
1233
1244
  this.originalStream = stream.pipeThrough(
@@ -1254,9 +1265,11 @@ var StreamObjectResult = class {
1254
1265
  schema
1255
1266
  });
1256
1267
  if (validationResult.success) {
1257
- resolveObject(validationResult.value);
1268
+ object = validationResult.value;
1269
+ resolveObject(object);
1258
1270
  } else {
1259
- rejectObject(validationResult.error);
1271
+ error = validationResult.error;
1272
+ rejectObject(error);
1260
1273
  }
1261
1274
  break;
1262
1275
  }
@@ -1265,6 +1278,24 @@ var StreamObjectResult = class {
1265
1278
  break;
1266
1279
  }
1267
1280
  }
1281
+ },
1282
+ // invoke onFinish callback and resolve toolResults promise when the stream is about to close:
1283
+ async flush(controller) {
1284
+ try {
1285
+ await (onFinish == null ? void 0 : onFinish({
1286
+ usage: usage != null ? usage : {
1287
+ promptTokens: NaN,
1288
+ completionTokens: NaN,
1289
+ totalTokens: NaN
1290
+ },
1291
+ object,
1292
+ error,
1293
+ rawResponse,
1294
+ warnings
1295
+ }));
1296
+ } catch (error2) {
1297
+ controller.error(error2);
1298
+ }
1268
1299
  }
1269
1300
  })
1270
1301
  );