ai 6.0.272 → 6.0.273

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.
@@ -164,7 +164,7 @@ function detectMediaType({
164
164
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
165
165
 
166
166
  // src/version.ts
167
- var VERSION = true ? "6.0.272" : "0.0.0-test";
167
+ var VERSION = true ? "6.0.273" : "0.0.0-test";
168
168
 
169
169
  // src/util/download/download.ts
170
170
  var download = async ({
@@ -144,7 +144,7 @@ import {
144
144
  } from "@ai-sdk/provider-utils";
145
145
 
146
146
  // src/version.ts
147
- var VERSION = true ? "6.0.272" : "0.0.0-test";
147
+ var VERSION = true ? "6.0.273" : "0.0.0-test";
148
148
 
149
149
  // src/util/download/download.ts
150
150
  var download = async ({
@@ -1450,6 +1450,13 @@ To see `streamText` in action, check out [these examples](#examples).
1450
1450
  description:
1451
1451
  'The raw reason why the generation finished (from the provider).',
1452
1452
  },
1453
+ {
1454
+ name: 'output',
1455
+ type: 'COMPLETE_OUTPUT | undefined',
1456
+ isOptional: true,
1457
+ description:
1458
+ 'The parsed output when an output setting was provided and parsing succeeded.',
1459
+ },
1453
1460
  {
1454
1461
  name: 'usage',
1455
1462
  type: 'LanguageModelUsage',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "6.0.272",
3
+ "version": "6.0.273",
4
4
  "description": "AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "@opentelemetry/api": "^1.9.0",
48
- "@ai-sdk/gateway": "3.0.185",
48
+ "@ai-sdk/gateway": "3.0.186",
49
49
  "@ai-sdk/provider": "3.0.15",
50
50
  "@ai-sdk/provider-utils": "4.0.50"
51
51
  },
@@ -33,7 +33,9 @@ export {
33
33
  } from './stop-condition';
34
34
  export {
35
35
  streamText,
36
+ type StreamTextEndEvent,
36
37
  type StreamTextOnChunkCallback,
38
+ type StreamTextOnEndCallback,
37
39
  type StreamTextOnErrorCallback,
38
40
  type StreamTextOnFinishCallback,
39
41
  type StreamTextOnStartCallback,
@@ -214,14 +214,31 @@ export type StreamTextOnChunkCallback<TOOLS extends ToolSet> = (event: {
214
214
  >;
215
215
  }) => PromiseLike<void> | void;
216
216
 
217
+ export type StreamTextEndEvent<
218
+ TOOLS extends ToolSet = ToolSet,
219
+ OUTPUT extends Output = Output,
220
+ > = OnFinishEvent<TOOLS> & {
221
+ /**
222
+ * The parsed output when an output setting was provided and parsing
223
+ * succeeded.
224
+ */
225
+ readonly output?: InferCompleteOutput<OUTPUT>;
226
+ };
227
+
228
+ export type StreamTextOnEndCallback<
229
+ TOOLS extends ToolSet = ToolSet,
230
+ OUTPUT extends Output = Output,
231
+ > = (event: StreamTextEndEvent<TOOLS, OUTPUT>) => PromiseLike<void> | void;
232
+
217
233
  /**
218
234
  * Callback that is set using the `onFinish` option.
219
235
  *
220
236
  * @param event - The event that is passed to the callback.
221
237
  */
222
- export type StreamTextOnFinishCallback<TOOLS extends ToolSet> = (
223
- event: OnFinishEvent<TOOLS>,
224
- ) => PromiseLike<void> | void;
238
+ export type StreamTextOnFinishCallback<
239
+ TOOLS extends ToolSet = ToolSet,
240
+ OUTPUT extends Output = Output,
241
+ > = StreamTextOnEndCallback<TOOLS, OUTPUT>;
225
242
 
226
243
  /**
227
244
  * Callback that is set using the `onAbort` option.
@@ -492,7 +509,7 @@ export function streamText<
492
509
  *
493
510
  * The usage is the combined usage of all steps.
494
511
  */
495
- onFinish?: StreamTextOnFinishCallback<TOOLS>;
512
+ onFinish?: StreamTextOnFinishCallback<NoInfer<TOOLS>, NoInfer<OUTPUT>>;
496
513
 
497
514
  onAbort?: StreamTextOnAbortCallback<TOOLS>;
498
515
 
@@ -758,6 +775,8 @@ class DefaultStreamTextResult<
758
775
  Awaited<StreamTextResult<TOOLS, OUTPUT>['steps']>
759
776
  >();
760
777
 
778
+ private outputPromise: Promise<InferCompleteOutput<OUTPUT>> | undefined;
779
+
761
780
  private readonly addStream: (
762
781
  stream: ReadableStream<TextStreamPart<TOOLS>>,
763
782
  callbacks?: {
@@ -862,7 +881,9 @@ class DefaultStreamTextResult<
862
881
  // callbacks:
863
882
  onChunk: undefined | StreamTextOnChunkCallback<TOOLS>;
864
883
  onError: StreamTextOnErrorCallback;
865
- onFinish: undefined | StreamTextOnFinishCallback<TOOLS>;
884
+ onFinish:
885
+ | undefined
886
+ | StreamTextOnFinishCallback<NoInfer<TOOLS>, NoInfer<OUTPUT>>;
866
887
  onAbort: undefined | StreamTextOnAbortCallback<TOOLS>;
867
888
  onStepFinish: undefined | StreamTextOnStepFinishCallback<TOOLS>;
868
889
  onStart: undefined | StreamTextOnStartCallback<TOOLS, OUTPUT>;
@@ -1211,43 +1232,61 @@ class DefaultStreamTextResult<
1211
1232
 
1212
1233
  // call onFinish callback:
1213
1234
  const finalStep = recordedSteps[recordedSteps.length - 1];
1214
-
1215
- await notify({
1216
- event: {
1217
- stepNumber: finalStep.stepNumber,
1218
- model: finalStep.model,
1219
- functionId: finalStep.functionId,
1220
- metadata: finalStep.metadata,
1221
- experimental_context: finalStep.experimental_context,
1222
- finishReason: finalStep.finishReason,
1223
- rawFinishReason: finalStep.rawFinishReason,
1224
- totalUsage,
1225
- usage: finalStep.usage,
1226
- content: finalStep.content,
1227
- text: finalStep.text,
1228
- reasoningText: finalStep.reasoningText,
1229
- reasoning: finalStep.reasoning,
1230
- files: finalStep.files,
1231
- sources: finalStep.sources,
1232
- toolCalls: finalStep.toolCalls,
1233
- staticToolCalls: finalStep.staticToolCalls,
1234
- dynamicToolCalls: finalStep.dynamicToolCalls,
1235
- toolResults: finalStep.toolResults,
1236
- staticToolResults: finalStep.staticToolResults,
1237
- dynamicToolResults: finalStep.dynamicToolResults,
1238
- request: finalStep.request,
1239
- response: finalStep.response,
1240
- warnings: finalStep.warnings,
1241
- providerMetadata: finalStep.providerMetadata,
1242
- steps: recordedSteps,
1243
- },
1244
- callbacks: [
1245
- onFinish,
1246
- globalTelemetry.onFinish as
1235
+ const onFinishEvent: OnFinishEvent<TOOLS> = {
1236
+ stepNumber: finalStep.stepNumber,
1237
+ model: finalStep.model,
1238
+ functionId: finalStep.functionId,
1239
+ metadata: finalStep.metadata,
1240
+ experimental_context: finalStep.experimental_context,
1241
+ finishReason: finalStep.finishReason,
1242
+ rawFinishReason: finalStep.rawFinishReason,
1243
+ totalUsage,
1244
+ usage: finalStep.usage,
1245
+ content: finalStep.content,
1246
+ text: finalStep.text,
1247
+ reasoningText: finalStep.reasoningText,
1248
+ reasoning: finalStep.reasoning,
1249
+ files: finalStep.files,
1250
+ sources: finalStep.sources,
1251
+ toolCalls: finalStep.toolCalls,
1252
+ staticToolCalls: finalStep.staticToolCalls,
1253
+ dynamicToolCalls: finalStep.dynamicToolCalls,
1254
+ toolResults: finalStep.toolResults,
1255
+ staticToolResults: finalStep.staticToolResults,
1256
+ dynamicToolResults: finalStep.dynamicToolResults,
1257
+ request: finalStep.request,
1258
+ response: finalStep.response,
1259
+ warnings: finalStep.warnings,
1260
+ providerMetadata: finalStep.providerMetadata,
1261
+ steps: recordedSteps,
1262
+ };
1263
+ const onFinishWithOutput =
1264
+ onFinish == null
1265
+ ? undefined
1266
+ : async (event: OnFinishEvent<TOOLS>) => {
1267
+ const parsedOutput =
1268
+ output == null
1269
+ ? undefined
1270
+ : await self.getOutputPromise().catch(() => undefined);
1271
+
1272
+ await onFinish({
1273
+ ...event,
1274
+ ...(output != null ? { output: parsedOutput } : {}),
1275
+ });
1276
+ };
1277
+
1278
+ await Promise.all([
1279
+ notify({
1280
+ event: onFinishEvent,
1281
+ callbacks: onFinishWithOutput,
1282
+ }),
1283
+ notify({
1284
+ event: onFinishEvent,
1285
+ callbacks: globalTelemetry.onFinish as
1247
1286
  | undefined
1248
- | StreamTextOnFinishCallback<TOOLS>,
1249
- ],
1250
- });
1287
+ | ((event: OnFinishEvent<TOOLS>) => PromiseLike<void> | void),
1288
+ }),
1289
+ ]);
1251
1290
 
1252
1291
  // Add response information to the root span:
1253
1292
  rootSpan.setAttributes(
@@ -2634,18 +2673,26 @@ class DefaultStreamTextResult<
2634
2673
  return createAsyncIterableStream(this.teeStream().pipeThrough(transform));
2635
2674
  }
2636
2675
 
2676
+ private getOutputPromise(): Promise<InferCompleteOutput<OUTPUT>> {
2677
+ if (this.outputPromise == null) {
2678
+ this.outputPromise = this.finalStep.then(step => {
2679
+ const output = this.outputSpecification ?? text();
2680
+ return output.parseCompleteOutput(
2681
+ { text: step.text },
2682
+ {
2683
+ response: step.response,
2684
+ usage: step.usage,
2685
+ finishReason: step.finishReason,
2686
+ },
2687
+ );
2688
+ });
2689
+ }
2690
+
2691
+ return this.outputPromise;
2692
+ }
2693
+
2637
2694
  get output(): Promise<InferCompleteOutput<OUTPUT>> {
2638
- return this.finalStep.then(step => {
2639
- const output = this.outputSpecification ?? text();
2640
- return output.parseCompleteOutput(
2641
- { text: step.text },
2642
- {
2643
- response: step.response,
2644
- usage: step.usage,
2645
- finishReason: step.finishReason,
2646
- },
2647
- );
2648
- });
2695
+ return this.getOutputPromise();
2649
2696
  }
2650
2697
 
2651
2698
  toUIMessageStream<UI_MESSAGE extends UIMessage>({