@ai-sdk/workflow 1.0.0-canary.71 → 1.0.0-canary.72

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-sdk/workflow",
3
- "version": "1.0.0-canary.71",
3
+ "version": "1.0.0-canary.72",
4
4
  "description": "WorkflowAgent for building AI agents with AI SDK",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.js",
@@ -28,8 +28,8 @@
28
28
  "dependencies": {
29
29
  "ajv": "^8.20.0",
30
30
  "@ai-sdk/provider": "4.0.0-canary.17",
31
- "@ai-sdk/provider-utils": "5.0.0-canary.44",
32
- "ai": "7.0.0-canary.154"
31
+ "ai": "7.0.0-canary.155",
32
+ "@ai-sdk/provider-utils": "5.0.0-canary.44"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/node": "22.19.19",
@@ -333,7 +333,7 @@ export async function doStreamStep(
333
333
  stepTimeMs: 0,
334
334
  responseTimeMs: 0,
335
335
  toolExecutionMs: {},
336
- timeToFirstOutputTokenMs: undefined,
336
+ timeToFirstOutputMs: undefined,
337
337
  },
338
338
  warnings,
339
339
  request: {
package/src/index.ts CHANGED
@@ -18,6 +18,7 @@ export {
18
18
  type PrepareStepResult,
19
19
  type ProviderOptions,
20
20
  type WorkflowAgentOnAbortCallback,
21
+ type WorkflowAgentOnEndCallback,
21
22
  type WorkflowAgentOnErrorCallback,
22
23
  type WorkflowAgentOnFinishCallback,
23
24
  type WorkflowAgentOnStepFinishCallback,
@@ -497,6 +497,13 @@ export type WorkflowAgentOptions<
497
497
  /**
498
498
  * Callback that is called when the LLM response and all request tool executions are finished.
499
499
  */
500
+ onEnd?: WorkflowAgentOnEndCallback<TTools, TRuntimeContext>;
501
+
502
+ /**
503
+ * Callback that is called when the LLM response and all request tool executions are finished.
504
+ *
505
+ * @deprecated Use `onEnd` instead.
506
+ */
500
507
  onFinish?: WorkflowAgentOnFinishCallback<TTools, TRuntimeContext>;
501
508
 
502
509
  /**
@@ -536,7 +543,7 @@ export type WorkflowAgentOptions<
536
543
  /**
537
544
  * Callback that is called when the LLM response and all request tool executions are finished.
538
545
  */
539
- export type WorkflowAgentOnFinishCallback<
546
+ export type WorkflowAgentOnEndCallback<
540
547
  TTools extends ToolSet = ToolSet,
541
548
  TRuntimeContext extends Context = Context,
542
549
  OUTPUT = never,
@@ -561,6 +568,11 @@ export type WorkflowAgentOnFinishCallback<
561
568
  */
562
569
  readonly finishReason: FinishReason;
563
570
 
571
+ /**
572
+ * The total token usage across all steps.
573
+ */
574
+ readonly usage: LanguageModelUsage;
575
+
564
576
  /**
565
577
  * The total token usage across all steps.
566
578
  */
@@ -583,6 +595,17 @@ export type WorkflowAgentOnFinishCallback<
583
595
  readonly output: OUTPUT;
584
596
  }) => PromiseLike<void> | void;
585
597
 
598
+ /**
599
+ * Callback that is called when the LLM response and all request tool executions are finished.
600
+ *
601
+ * @deprecated Use `WorkflowAgentOnEndCallback` instead.
602
+ */
603
+ export type WorkflowAgentOnFinishCallback<
604
+ TTools extends ToolSet = ToolSet,
605
+ TRuntimeContext extends Context = Context,
606
+ OUTPUT = never,
607
+ > = WorkflowAgentOnEndCallback<TTools, TRuntimeContext, OUTPUT>;
608
+
586
609
  /**
587
610
  * Callback that is invoked when an error occurs during streaming.
588
611
  */
@@ -894,6 +917,14 @@ export type WorkflowAgentStreamOptions<
894
917
  * Callback that is called when the LLM response and all request tool executions
895
918
  * (for tools that have an `execute` function) are finished.
896
919
  */
920
+ onEnd?: WorkflowAgentOnEndCallback<TTools, TRuntimeContext, OUTPUT>;
921
+
922
+ /**
923
+ * Callback that is called when the LLM response and all request tool executions
924
+ * (for tools that have an `execute` function) are finished.
925
+ *
926
+ * @deprecated Use `onEnd` instead.
927
+ */
897
928
  onFinish?: WorkflowAgentOnFinishCallback<TTools, TRuntimeContext, OUTPUT>;
898
929
 
899
930
  /**
@@ -1107,7 +1138,7 @@ export class WorkflowAgent<
1107
1138
  TBaseTools,
1108
1139
  TRuntimeContext
1109
1140
  >;
1110
- private constructorOnFinish?: WorkflowAgentOnFinishCallback<
1141
+ private constructorOnEnd?: WorkflowAgentOnEndCallback<
1111
1142
  TBaseTools,
1112
1143
  TRuntimeContext
1113
1144
  >;
@@ -1140,7 +1171,8 @@ export class WorkflowAgent<
1140
1171
  this.experimentalDownload = options.experimental_download;
1141
1172
  this.prepareStep = options.prepareStep;
1142
1173
  this.constructorOnStepFinish = options.onStepFinish;
1143
- this.constructorOnFinish = options.onFinish;
1174
+ const { onFinish, onEnd = onFinish } = options;
1175
+ this.constructorOnEnd = onEnd;
1144
1176
  this.constructorOnStart = options.experimental_onStart;
1145
1177
  this.constructorOnStepStart = options.experimental_onStepStart;
1146
1178
  this.constructorOnToolExecutionStart = options.onToolExecutionStart;
@@ -1180,6 +1212,8 @@ export class WorkflowAgent<
1180
1212
  PARTIAL_OUTPUT
1181
1213
  >,
1182
1214
  ): Promise<WorkflowAgentStreamResult<TTools, OUTPUT>> {
1215
+ const { onFinish, onEnd = onFinish } = options;
1216
+
1183
1217
  // Call prepareCall to transform parameters before the agent loop
1184
1218
  let effectiveModel: LanguageModel = this.model;
1185
1219
  let effectiveInstructions = options.system ?? this.instructions;
@@ -1505,11 +1539,11 @@ export class WorkflowAgent<
1505
1539
  | undefined,
1506
1540
  options.onStepFinish,
1507
1541
  );
1508
- const mergedOnFinish = mergeCallbacks(
1509
- this.constructorOnFinish as
1510
- | WorkflowAgentOnFinishCallback<TTools, TRuntimeContext, OUTPUT>
1542
+ const mergedOnEnd = mergeCallbacks(
1543
+ this.constructorOnEnd as
1544
+ | WorkflowAgentOnEndCallback<TTools, TRuntimeContext, OUTPUT>
1511
1545
  | undefined,
1512
- options.onFinish,
1546
+ onEnd,
1513
1547
  );
1514
1548
  const mergedOnStart = mergeCallbacks(
1515
1549
  this.constructorOnStart as
@@ -2006,14 +2040,16 @@ export class WorkflowAgent<
2006
2040
 
2007
2041
  const messages = iterMessages as unknown as ModelMessage[];
2008
2042
 
2009
- if (mergedOnFinish && !wasAborted) {
2043
+ if (mergedOnEnd && !wasAborted) {
2010
2044
  const lastStep = steps[steps.length - 1];
2011
- await mergedOnFinish({
2045
+ const totalUsage = aggregateUsage(steps);
2046
+ await mergedOnEnd({
2012
2047
  steps,
2013
2048
  messages,
2014
2049
  text: lastStep?.text ?? '',
2015
2050
  finishReason: lastStep?.finishReason ?? 'other',
2016
- totalUsage: aggregateUsage(steps),
2051
+ usage: totalUsage,
2052
+ totalUsage,
2017
2053
  runtimeContext,
2018
2054
  toolsContext:
2019
2055
  toolsContext as unknown as InferToolSetContext<TTools>,
@@ -2023,10 +2059,12 @@ export class WorkflowAgent<
2023
2059
  if (!wasAborted && steps.length > 0) {
2024
2060
  const telemetrySteps = steps.map(normalizeStepForTelemetry);
2025
2061
  const lastStep = telemetrySteps[telemetrySteps.length - 1];
2062
+ const totalUsage = aggregateUsage(steps);
2026
2063
  await telemetryDispatcher.onEnd?.({
2027
2064
  ...lastStep,
2028
2065
  steps: telemetrySteps,
2029
- totalUsage: aggregateUsage(steps),
2066
+ usage: totalUsage,
2067
+ totalUsage,
2030
2068
  });
2031
2069
  }
2032
2070
 
@@ -2181,7 +2219,7 @@ export class WorkflowAgent<
2181
2219
  await options.onError({ error });
2182
2220
  }
2183
2221
  await telemetryDispatcher.onError?.(error);
2184
- // Don't throw yet - we want to call onFinish first
2222
+ // Don't throw yet - we want to call onEnd first
2185
2223
  }
2186
2224
 
2187
2225
  // Use the final messages from the iterator, or fall back to standardized messages
@@ -2214,15 +2252,17 @@ export class WorkflowAgent<
2214
2252
  }
2215
2253
  }
2216
2254
 
2217
- // Call onFinish callback if provided (always call, even on errors, but not on abort)
2218
- if (mergedOnFinish && !wasAborted) {
2255
+ // Call onEnd callback if provided (always call, even on errors, but not on abort)
2256
+ if (mergedOnEnd && !wasAborted) {
2219
2257
  const lastStep = steps[steps.length - 1];
2220
- await mergedOnFinish({
2258
+ const totalUsage = aggregateUsage(steps);
2259
+ await mergedOnEnd({
2221
2260
  steps,
2222
2261
  messages: messages as ModelMessage[],
2223
2262
  text: lastStep?.text ?? '',
2224
2263
  finishReason: lastStep?.finishReason ?? 'other',
2225
- totalUsage: aggregateUsage(steps),
2264
+ usage: totalUsage,
2265
+ totalUsage,
2226
2266
  runtimeContext,
2227
2267
  toolsContext: toolsContext as unknown as InferToolSetContext<TTools>,
2228
2268
  output: experimentalOutput,
@@ -2231,10 +2271,12 @@ export class WorkflowAgent<
2231
2271
  if (!wasAborted && steps.length > 0) {
2232
2272
  const telemetrySteps = steps.map(normalizeStepForTelemetry);
2233
2273
  const lastStep = telemetrySteps[telemetrySteps.length - 1];
2274
+ const totalUsage = aggregateUsage(steps);
2234
2275
  await telemetryDispatcher.onEnd?.({
2235
2276
  ...lastStep,
2236
2277
  steps: telemetrySteps,
2237
- totalUsage: aggregateUsage(steps),
2278
+ usage: totalUsage,
2279
+ totalUsage,
2238
2280
  });
2239
2281
  }
2240
2282