ai 3.1.27 → 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';
@@ -628,12 +658,16 @@ type ObjectStreamPart<T> = ObjectStreamInputPart | {
628
658
  The result of a `streamObject` call that contains the partial object stream and additional information.
629
659
  */
630
660
  declare class StreamObjectResult<T> {
631
- readonly originalStream: ReadableStream<ObjectStreamPart<T>>;
661
+ private readonly originalStream;
632
662
  /**
633
663
  Warnings from the model provider (e.g. unsupported settings)
634
664
  */
635
665
  readonly warnings: CallWarning[] | undefined;
636
666
  /**
667
+ The generated object (typed according to the schema). Resolved when the response is finished.
668
+ */
669
+ readonly object: Promise<T>;
670
+ /**
637
671
  The token usage of the generated response. Resolved when the response is finished.
638
672
  */
639
673
  readonly usage: Promise<TokenUsage>;
@@ -646,12 +680,14 @@ declare class StreamObjectResult<T> {
646
680
  */
647
681
  headers?: Record<string, string>;
648
682
  };
649
- constructor({ stream, warnings, rawResponse, }: {
683
+ constructor({ stream, warnings, rawResponse, schema, onFinish, }: {
650
684
  stream: ReadableStream<string | ObjectStreamInputPart>;
651
685
  warnings: CallWarning[] | undefined;
652
686
  rawResponse?: {
653
687
  headers?: Record<string, string>;
654
688
  };
689
+ schema: z.Schema<T>;
690
+ onFinish: Parameters<typeof streamObject<T>>[0]['onFinish'];
655
691
  });
656
692
  get partialObjectStream(): AsyncIterableStream<DeepPartial<T>>;
657
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';
@@ -628,12 +658,16 @@ type ObjectStreamPart<T> = ObjectStreamInputPart | {
628
658
  The result of a `streamObject` call that contains the partial object stream and additional information.
629
659
  */
630
660
  declare class StreamObjectResult<T> {
631
- readonly originalStream: ReadableStream<ObjectStreamPart<T>>;
661
+ private readonly originalStream;
632
662
  /**
633
663
  Warnings from the model provider (e.g. unsupported settings)
634
664
  */
635
665
  readonly warnings: CallWarning[] | undefined;
636
666
  /**
667
+ The generated object (typed according to the schema). Resolved when the response is finished.
668
+ */
669
+ readonly object: Promise<T>;
670
+ /**
637
671
  The token usage of the generated response. Resolved when the response is finished.
638
672
  */
639
673
  readonly usage: Promise<TokenUsage>;
@@ -646,12 +680,14 @@ declare class StreamObjectResult<T> {
646
680
  */
647
681
  headers?: Record<string, string>;
648
682
  };
649
- constructor({ stream, warnings, rawResponse, }: {
683
+ constructor({ stream, warnings, rawResponse, schema, onFinish, }: {
650
684
  stream: ReadableStream<string | ObjectStreamInputPart>;
651
685
  warnings: CallWarning[] | undefined;
652
686
  rawResponse?: {
653
687
  headers?: Record<string, string>;
654
688
  };
689
+ schema: z.Schema<T>;
690
+ onFinish: Parameters<typeof streamObject<T>>[0]['onFinish'];
655
691
  });
656
692
  get partialObjectStream(): AsyncIterableStream<DeepPartial<T>>;
657
693
  get fullStream(): AsyncIterableStream<ObjectStreamPart<T>>;
package/dist/index.js CHANGED
@@ -1080,6 +1080,7 @@ function parsePartialJson(jsonText) {
1080
1080
  }
1081
1081
 
1082
1082
  // core/generate-object/stream-object.ts
1083
+ var import_provider_utils4 = require("@ai-sdk/provider-utils");
1083
1084
  async function streamObject({
1084
1085
  model,
1085
1086
  schema,
@@ -1089,6 +1090,7 @@ async function streamObject({
1089
1090
  messages,
1090
1091
  maxRetries,
1091
1092
  abortSignal,
1093
+ onFinish,
1092
1094
  ...settings
1093
1095
  }) {
1094
1096
  const retry = retryWithExponentialBackoff({ maxRetries });
@@ -1203,22 +1205,34 @@ async function streamObject({
1203
1205
  return new StreamObjectResult({
1204
1206
  stream: result.stream.pipeThrough(new TransformStream(transformer)),
1205
1207
  warnings: result.warnings,
1206
- rawResponse: result.rawResponse
1208
+ rawResponse: result.rawResponse,
1209
+ schema,
1210
+ onFinish
1207
1211
  });
1208
1212
  }
1209
1213
  var StreamObjectResult = class {
1210
1214
  constructor({
1211
1215
  stream,
1212
1216
  warnings,
1213
- rawResponse
1217
+ rawResponse,
1218
+ schema,
1219
+ onFinish
1214
1220
  }) {
1215
1221
  this.warnings = warnings;
1216
1222
  this.rawResponse = rawResponse;
1223
+ let resolveObject;
1224
+ let rejectObject;
1225
+ this.object = new Promise((resolve, reject) => {
1226
+ resolveObject = resolve;
1227
+ rejectObject = reject;
1228
+ });
1217
1229
  let resolveUsage;
1218
1230
  this.usage = new Promise((resolve) => {
1219
1231
  resolveUsage = resolve;
1220
1232
  });
1221
1233
  let usage;
1234
+ let object;
1235
+ let error;
1222
1236
  let accumulatedText = "";
1223
1237
  let latestObject = void 0;
1224
1238
  this.originalStream = stream.pipeThrough(
@@ -1238,11 +1252,19 @@ var StreamObjectResult = class {
1238
1252
  switch (chunk.type) {
1239
1253
  case "finish": {
1240
1254
  usage = calculateTokenUsage(chunk.usage);
1241
- controller.enqueue({
1242
- ...chunk,
1243
- usage
1244
- });
1255
+ controller.enqueue({ ...chunk, usage });
1245
1256
  resolveUsage(usage);
1257
+ const validationResult = (0, import_provider_utils4.safeValidateTypes)({
1258
+ value: latestObject,
1259
+ schema
1260
+ });
1261
+ if (validationResult.success) {
1262
+ object = validationResult.value;
1263
+ resolveObject(object);
1264
+ } else {
1265
+ error = validationResult.error;
1266
+ rejectObject(error);
1267
+ }
1246
1268
  break;
1247
1269
  }
1248
1270
  default: {
@@ -1250,6 +1272,24 @@ var StreamObjectResult = class {
1250
1272
  break;
1251
1273
  }
1252
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
+ }
1253
1293
  }
1254
1294
  })
1255
1295
  );
@@ -1313,7 +1353,7 @@ function prepareToolsAndToolChoice({
1313
1353
 
1314
1354
  // core/generate-text/tool-call.ts
1315
1355
  var import_provider6 = require("@ai-sdk/provider");
1316
- var import_provider_utils4 = require("@ai-sdk/provider-utils");
1356
+ var import_provider_utils5 = require("@ai-sdk/provider-utils");
1317
1357
  function parseToolCall({
1318
1358
  toolCall,
1319
1359
  tools
@@ -1329,7 +1369,7 @@ function parseToolCall({
1329
1369
  availableTools: Object.keys(tools)
1330
1370
  });
1331
1371
  }
1332
- const parseResult = (0, import_provider_utils4.safeParseJSON)({
1372
+ const parseResult = (0, import_provider_utils5.safeParseJSON)({
1333
1373
  text: toolCall.args,
1334
1374
  schema: tool2.parameters
1335
1375
  });