@ai-sdk/workflow 2.0.16 → 2.0.18

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/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # @ai-sdk/workflow
2
2
 
3
+ ## 2.0.18
4
+
5
+ ### Patch Changes
6
+
7
+ - 238aff0: fix(workflow): preserve context and lifecycle callbacks for approved tools
8
+ - 05672ad: fix(workflow): stream failed tool executions as tool errors
9
+ - 3b6ef0c: fix(workflow): infer configured tool parts in WorkflowAgent UI messages
10
+ - d3b1ffb: feat(workflow): add stable onStart and onStepStart callbacks to WorkflowAgent
11
+ - 33d185e: fix(workflow): restrict prepareStep activeTools to configured tool names
12
+ - Updated dependencies [8b6b756]
13
+ - Updated dependencies [e07b577]
14
+ - ai@7.0.88
15
+
16
+ ## 2.0.17
17
+
18
+ ### Patch Changes
19
+
20
+ - Updated dependencies [850d863]
21
+ - ai@7.0.87
22
+
3
23
  ## 2.0.16
4
24
 
5
25
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { LanguageModelV4, LanguageModelV4CallOptions, SharedV4ProviderOptions, LanguageModelV4Prompt, LanguageModelV4StreamPart } from '@ai-sdk/provider';
2
2
  import { Context, HasRequiredKey, InferToolSetContext } from '@ai-sdk/provider-utils';
3
- import { ToolSet, Experimental_LanguageModelStreamPart, LanguageModel, Instructions, ToolChoice, TelemetryOptions as TelemetryOptions$1, StopCondition, ActiveTools, LanguageModelResponseMetadata, LanguageModelUsage, FinishReason, ToolCallRepairFunction, Experimental_SandboxSession, ModelMessage, StepResult, GenerateTextOnStepEndCallback, UIMessage, UIMessageChunk, ChatTransport, PrepareSendMessagesRequest, PrepareReconnectToStreamRequest, ChatRequestOptions } from 'ai';
3
+ import { ToolSet, Experimental_LanguageModelStreamPart, LanguageModel, Instructions, ToolChoice, TelemetryOptions as TelemetryOptions$1, StopCondition, ActiveTools, LanguageModelResponseMetadata, LanguageModelUsage, FinishReason, ToolCallRepairFunction, Experimental_SandboxSession, ModelMessage, StepResult, GenerateTextOnStepEndCallback, UIMessage, InferUITools, UIMessageChunk, ChatTransport, PrepareSendMessagesRequest, PrepareReconnectToStreamRequest, ChatRequestOptions } from 'ai';
4
4
  export { Output, ToolCallRepairFunction } from 'ai';
5
5
 
6
6
  type ModelCallStreamPart<TTools extends ToolSet = ToolSet> = Experimental_LanguageModelStreamPart<TTools> | {
@@ -43,7 +43,7 @@ type InferWorkflowAgentTools<WORKFLOW_AGENT> = WORKFLOW_AGENT extends WorkflowAg
43
43
  /**
44
44
  * Infer the UI message type of a workflow agent.
45
45
  */
46
- type InferWorkflowAgentUIMessage<_WORKFLOW_AGENT, MESSAGE_METADATA = unknown> = UIMessage<MESSAGE_METADATA>;
46
+ type InferWorkflowAgentUIMessage<WORKFLOW_AGENT, MESSAGE_METADATA = unknown> = UIMessage<MESSAGE_METADATA, never, InferUITools<InferWorkflowAgentTools<WORKFLOW_AGENT>>>;
47
47
 
48
48
  /**
49
49
  * Output specification interface for structured outputs.
@@ -255,7 +255,7 @@ interface PrepareStepResult<TTools extends ToolSet = ToolSet, TRuntimeContext ex
255
255
  * Override the active tools for this step.
256
256
  * Limits the tools that are available for the model to call.
257
257
  */
258
- activeTools?: string[];
258
+ activeTools?: ActiveTools<NoInfer<TTools>>;
259
259
  /**
260
260
  * Updated runtime context for the current and subsequent steps.
261
261
  * Returning a value replaces the agent's runtime context.
@@ -453,10 +453,22 @@ type WorkflowAgentOptions<TTools extends ToolSet = ToolSet, TRuntimeContext exte
453
453
  /**
454
454
  * Callback called when the agent starts streaming, before any LLM calls.
455
455
  */
456
+ onStart?: WorkflowAgentOnStartCallback<TTools, TRuntimeContext>;
457
+ /**
458
+ * Callback called when the agent starts streaming, before any LLM calls.
459
+ *
460
+ * @deprecated Use `onStart` instead.
461
+ */
456
462
  experimental_onStart?: WorkflowAgentOnStartCallback<TTools, TRuntimeContext>;
457
463
  /**
458
464
  * Callback called before each step (LLM call) begins.
459
465
  */
466
+ onStepStart?: WorkflowAgentOnStepStartCallback<TTools, TRuntimeContext>;
467
+ /**
468
+ * Callback called before each step (LLM call) begins.
469
+ *
470
+ * @deprecated Use `onStepStart` instead.
471
+ */
460
472
  experimental_onStepStart?: WorkflowAgentOnStepStartCallback<TTools, TRuntimeContext>;
461
473
  /**
462
474
  * Callback called before a tool's execute function runs.
@@ -833,10 +845,22 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, TRuntimeContex
833
845
  /**
834
846
  * Callback called when the agent starts streaming, before any LLM calls.
835
847
  */
848
+ onStart?: WorkflowAgentOnStartCallback<TTools, TRuntimeContext>;
849
+ /**
850
+ * Callback called when the agent starts streaming, before any LLM calls.
851
+ *
852
+ * @deprecated Use `onStart` instead.
853
+ */
836
854
  experimental_onStart?: WorkflowAgentOnStartCallback<TTools, TRuntimeContext>;
837
855
  /**
838
856
  * Callback called before each step (LLM call) begins.
839
857
  */
858
+ onStepStart?: WorkflowAgentOnStepStartCallback<TTools, TRuntimeContext>;
859
+ /**
860
+ * Callback called before each step (LLM call) begins.
861
+ *
862
+ * @deprecated Use `onStepStart` instead.
863
+ */
840
864
  experimental_onStepStart?: WorkflowAgentOnStepStartCallback<TTools, TRuntimeContext>;
841
865
  /**
842
866
  * Callback called before a tool's execute function runs.
package/dist/index.js CHANGED
@@ -803,7 +803,7 @@ function sanitizeProviderMetadataForToolCall(metadata) {
803
803
  // src/workflow-agent.ts
804
804
  var WorkflowAgent = class {
805
805
  constructor(options) {
806
- var _a, _b, _c, _d, _e;
806
+ var _a, _b, _c, _d, _e, _f, _g;
807
807
  this.id = options.id;
808
808
  this.model = options.model;
809
809
  this.tools = (_a = options.tools) != null ? _a : {};
@@ -823,12 +823,12 @@ var WorkflowAgent = class {
823
823
  this.constructorOnStepEnd = (_d = options.onStepEnd) != null ? _d : options.onStepFinish;
824
824
  const { onFinish, onEnd = onFinish } = options;
825
825
  this.constructorOnEnd = onEnd;
826
- this.constructorOnStart = options.experimental_onStart;
827
- this.constructorOnStepStart = options.experimental_onStepStart;
826
+ this.constructorOnStart = (_e = options.onStart) != null ? _e : options.experimental_onStart;
827
+ this.constructorOnStepStart = (_f = options.onStepStart) != null ? _f : options.experimental_onStepStart;
828
828
  this.constructorOnToolExecutionStart = options.onToolExecutionStart;
829
829
  this.constructorOnToolExecutionEnd = options.onToolExecutionEnd;
830
830
  this.prepareCall = options.prepareCall;
831
- this.allowSystemInMessages = (_e = options.allowSystemInMessages) != null ? _e : false;
831
+ this.allowSystemInMessages = (_g = options.allowSystemInMessages) != null ? _g : false;
832
832
  this.generationSettings = {
833
833
  maxOutputTokens: options.maxOutputTokens,
834
834
  temperature: options.temperature,
@@ -849,7 +849,7 @@ var WorkflowAgent = class {
849
849
  throw new Error("Not implemented");
850
850
  }
851
851
  async stream(options) {
852
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U, _V;
852
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _P, _Q, _R, _S, _T, _U;
853
853
  const { onFinish, onEnd = onFinish } = options;
854
854
  let effectiveModel = this.model;
855
855
  let effectiveInstructions = (_b = (_a = options.instructions) != null ? _a : options.system) != null ? _b : this.instructions;
@@ -941,6 +941,14 @@ var WorkflowAgent = class {
941
941
  });
942
942
  const download = effectiveDownloadFromPrepare;
943
943
  const sandbox = (_n = options.experimental_sandbox) != null ? _n : this.experimentalSandbox;
944
+ const mergedOnToolExecutionStart = mergeCallbacks(
945
+ this.constructorOnToolExecutionStart,
946
+ options.onToolExecutionStart
947
+ );
948
+ const mergedOnToolExecutionEnd = mergeCallbacks(
949
+ this.constructorOnToolExecutionEnd,
950
+ options.onToolExecutionEnd
951
+ );
944
952
  const collectedApprovals = collectToolApprovals({
945
953
  messages: prompt.messages
946
954
  });
@@ -1044,106 +1052,25 @@ var WorkflowAgent = class {
1044
1052
  });
1045
1053
  continue;
1046
1054
  }
1047
- try {
1048
- const { execute } = tool2;
1049
- const resolvedContext = await resolveToolContext({
1050
- toolName: approval.toolName,
1051
- tool: tool2,
1052
- toolsContext: effectiveToolsContext
1053
- });
1054
- const toolCallEvent = {
1055
- type: "tool-call",
1055
+ const result = await executeToolWithCallbacks(
1056
+ {
1056
1057
  toolCallId: approval.toolCallId,
1057
1058
  toolName: approval.toolName,
1058
1059
  input: approval.input
1059
- };
1060
- const messages2 = prompt.messages;
1061
- await ((_p = telemetryDispatcher.onToolExecutionStart) == null ? void 0 : _p.call(telemetryDispatcher, {
1062
- toolCall: toolCallEvent,
1063
- stepNumber: 0,
1064
- messages: messages2,
1065
- toolContext: resolvedContext
1066
- }));
1067
- const startTime = Date.now();
1068
- const executeApprovedTool = () => execute(approval.input, {
1069
- toolCallId: approval.toolCallId,
1070
- messages: [],
1071
- context: resolvedContext,
1072
- experimental_sandbox: sandbox
1073
- });
1074
- const toolResult = telemetryDispatcher.executeTool != null ? await telemetryDispatcher.executeTool({
1075
- callId: "workflow-agent",
1076
- toolCallId: approval.toolCallId,
1077
- execute: executeApprovedTool
1078
- }) : await executeApprovedTool();
1079
- await ((_q = telemetryDispatcher.onToolExecutionEnd) == null ? void 0 : _q.call(telemetryDispatcher, {
1080
- toolCall: toolCallEvent,
1081
- stepNumber: 0,
1082
- durationMs: Date.now() - startTime,
1083
- messages: messages2,
1084
- toolContext: resolvedContext,
1085
- success: true,
1086
- output: toolResult
1087
- }));
1088
- toolResultContent.push({
1089
- type: "tool-result",
1090
- toolCallId: approval.toolCallId,
1091
- toolName: approval.toolName,
1092
- output: await createLanguageModelToolResultOutput({
1093
- toolCallId: approval.toolCallId,
1094
- toolName: approval.toolName,
1095
- input: approval.input,
1096
- output: toolResult,
1097
- tool: tool2,
1098
- errorMode: "none",
1099
- supportedUrls: {},
1100
- download
1101
- })
1102
- });
1103
- approvedRawResults.push({
1104
- toolCallId: approval.toolCallId,
1105
- toolName: approval.toolName,
1106
- input: approval.input,
1107
- output: toolResult
1108
- });
1109
- } catch (error) {
1110
- const errorMessage = getErrorMessage(error);
1111
- await ((_r = telemetryDispatcher.onToolExecutionEnd) == null ? void 0 : _r.call(telemetryDispatcher, {
1112
- toolCall: {
1113
- type: "tool-call",
1114
- toolCallId: approval.toolCallId,
1115
- toolName: approval.toolName,
1116
- input: approval.input
1117
- },
1118
- stepNumber: 0,
1119
- durationMs: 0,
1120
- messages: prompt.messages,
1121
- toolContext: void 0,
1122
- success: false,
1123
- error
1124
- }));
1125
- toolResultContent.push({
1126
- type: "tool-result",
1127
- toolCallId: approval.toolCallId,
1128
- toolName: approval.toolName,
1129
- output: await createLanguageModelToolResultOutput({
1130
- toolCallId: approval.toolCallId,
1131
- toolName: approval.toolName,
1132
- input: approval.input,
1133
- output: errorMessage,
1134
- tool: tool2,
1135
- errorMode: "text",
1136
- supportedUrls: {},
1137
- download
1138
- })
1139
- });
1140
- approvedRawResults.push({
1141
- toolCallId: approval.toolCallId,
1142
- toolName: approval.toolName,
1143
- input: approval.input,
1144
- output: errorMessage
1145
- });
1146
- }
1060
+ },
1061
+ this.tools,
1062
+ prompt.messages,
1063
+ effectiveToolsContext,
1064
+ 0,
1065
+ sandbox
1066
+ );
1067
+ toolResultContent.push(result.modelResult);
1068
+ approvedRawResults.push({
1069
+ toolCallId: approval.toolCallId,
1070
+ toolName: approval.toolName,
1071
+ input: approval.input,
1072
+ output: result.rawOutput
1073
+ });
1147
1074
  }
1148
1075
  }
1149
1076
  for (const denial of deniedToolApprovals) {
@@ -1207,7 +1134,7 @@ var WorkflowAgent = class {
1207
1134
  supportedUrls: {},
1208
1135
  download
1209
1136
  });
1210
- const effectiveAbortSignal = (_s = options.abortSignal) != null ? _s : effectiveGenerationSettings.abortSignal;
1137
+ const effectiveAbortSignal = (_p = options.abortSignal) != null ? _p : effectiveGenerationSettings.abortSignal;
1211
1138
  const timeoutAt = options.timeout == null ? void 0 : Date.now() + options.timeout;
1212
1139
  const mergedGenerationSettings = {
1213
1140
  ...effectiveGenerationSettings,
@@ -1242,12 +1169,12 @@ var WorkflowAgent = class {
1242
1169
  }
1243
1170
  };
1244
1171
  mergedGenerationSettings.headers = withUserAgentSuffix(
1245
- (_t = mergedGenerationSettings.headers) != null ? _t : {},
1172
+ (_q = mergedGenerationSettings.headers) != null ? _q : {},
1246
1173
  "ai-sdk-agent/workflow"
1247
1174
  );
1248
1175
  const mergedOnStepEnd = mergeCallbacks(
1249
1176
  this.constructorOnStepEnd,
1250
- (_u = options.onStepEnd) != null ? _u : options.onStepFinish
1177
+ (_r = options.onStepEnd) != null ? _r : options.onStepFinish
1251
1178
  );
1252
1179
  const mergedOnEnd = mergeCallbacks(
1253
1180
  this.constructorOnEnd,
@@ -1255,26 +1182,18 @@ var WorkflowAgent = class {
1255
1182
  );
1256
1183
  const mergedOnStart = mergeCallbacks(
1257
1184
  this.constructorOnStart,
1258
- options.experimental_onStart
1185
+ (_s = options.onStart) != null ? _s : options.experimental_onStart
1259
1186
  );
1260
1187
  const mergedOnStepStart = mergeCallbacks(
1261
1188
  this.constructorOnStepStart,
1262
- options.experimental_onStepStart
1263
- );
1264
- const mergedOnToolExecutionStart = mergeCallbacks(
1265
- this.constructorOnToolExecutionStart,
1266
- options.onToolExecutionStart
1267
- );
1268
- const mergedOnToolExecutionEnd = mergeCallbacks(
1269
- this.constructorOnToolExecutionEnd,
1270
- options.onToolExecutionEnd
1189
+ (_t = options.onStepStart) != null ? _t : options.experimental_onStepStart
1271
1190
  );
1272
1191
  const effectiveToolChoice = effectiveToolChoiceFromPrepare;
1273
1192
  const effectiveActiveTools = effectiveActiveToolsFromPrepare;
1274
- const effectiveTools = effectiveActiveTools && effectiveActiveTools.length > 0 ? (_v = filterActiveTools2({
1193
+ const effectiveTools = effectiveActiveTools && effectiveActiveTools.length > 0 ? (_u = filterActiveTools2({
1275
1194
  tools: this.tools,
1276
1195
  activeTools: effectiveActiveTools
1277
- })) != null ? _v : this.tools : this.tools;
1196
+ })) != null ? _u : this.tools : this.tools;
1278
1197
  const effectiveModelInfo = getModelInfo2(effectiveModel);
1279
1198
  let runtimeContext = effectiveRuntimeContext;
1280
1199
  let toolsContext = effectiveToolsContext;
@@ -1289,7 +1208,7 @@ var WorkflowAgent = class {
1289
1208
  toolsContext
1290
1209
  });
1291
1210
  }
1292
- await ((_y = telemetryDispatcher.onStart) == null ? void 0 : _y.call(telemetryDispatcher, {
1211
+ await ((_x = telemetryDispatcher.onStart) == null ? void 0 : _x.call(telemetryDispatcher, {
1293
1212
  callId: "workflow-agent",
1294
1213
  operationId: "ai.workflowAgent.stream",
1295
1214
  provider: effectiveModelInfo.provider,
@@ -1307,16 +1226,16 @@ var WorkflowAgent = class {
1307
1226
  frequencyPenalty: mergedGenerationSettings.frequencyPenalty,
1308
1227
  stopSequences: mergedGenerationSettings.stopSequences,
1309
1228
  seed: mergedGenerationSettings.seed,
1310
- maxRetries: (_w = mergedGenerationSettings.maxRetries) != null ? _w : 2,
1229
+ maxRetries: (_v = mergedGenerationSettings.maxRetries) != null ? _v : 2,
1311
1230
  timeout: void 0,
1312
1231
  headers: mergedGenerationSettings.headers,
1313
1232
  reasoning: mergedGenerationSettings.reasoning,
1314
1233
  providerOptions: mergedGenerationSettings.providerOptions,
1315
- output: (_x = options.output) != null ? _x : this.output,
1234
+ output: (_w = options.output) != null ? _w : this.output,
1316
1235
  runtimeContext,
1317
1236
  toolsContext
1318
1237
  }));
1319
- const executeToolWithCallbacks = async (toolCall, tools, messages2, perToolContexts, currentStepNumber = 0, stepSandbox) => {
1238
+ async function executeToolWithCallbacks(toolCall, tools, messages2, perToolContexts, currentStepNumber = 0, stepSandbox) {
1320
1239
  var _a2, _b2, _c2, _d2;
1321
1240
  const toolCallEvent = {
1322
1241
  type: "tool-call",
@@ -1431,7 +1350,7 @@ var WorkflowAgent = class {
1431
1350
  }));
1432
1351
  }
1433
1352
  return result;
1434
- };
1353
+ }
1435
1354
  const recordProviderExecutedToolTelemetry = async (toolCall, result, messages2, currentStepNumber) => {
1436
1355
  var _a2, _b2;
1437
1356
  const toolCallEvent = {
@@ -1462,7 +1381,7 @@ var WorkflowAgent = class {
1462
1381
  }
1463
1382
  }));
1464
1383
  };
1465
- if ((_z = mergedGenerationSettings.abortSignal) == null ? void 0 : _z.aborted) {
1384
+ if ((_y = mergedGenerationSettings.abortSignal) == null ? void 0 : _y.aborted) {
1466
1385
  if (options.onAbort) {
1467
1386
  await options.onAbort({ steps });
1468
1387
  }
@@ -1486,16 +1405,16 @@ var WorkflowAgent = class {
1486
1405
  stopConditions: effectiveStopWhenFromPrepare,
1487
1406
  onStepEnd: mergedOnStepEnd,
1488
1407
  onStepStart: mergedOnStepStart,
1489
- prepareStep: (_A = options.prepareStep) != null ? _A : this.prepareStep,
1408
+ prepareStep: (_z = options.prepareStep) != null ? _z : this.prepareStep,
1490
1409
  generationSettings: mergedGenerationSettings,
1491
1410
  toolChoice: effectiveToolChoice,
1492
1411
  runtimeContext,
1493
1412
  toolsContext,
1494
1413
  telemetry: effectiveTelemetry,
1495
- includeRawChunks: (_B = options.includeRawChunks) != null ? _B : false,
1414
+ includeRawChunks: (_A = options.includeRawChunks) != null ? _A : false,
1496
1415
  timeoutAt,
1497
- repairToolCall: (_D = (_C = options.repairToolCall) != null ? _C : options.experimental_repairToolCall) != null ? _D : this.repairToolCall,
1498
- responseFormat: await ((_F = (_E = options.output) != null ? _E : this.output) == null ? void 0 : _F.responseFormat),
1416
+ repairToolCall: (_C = (_B = options.repairToolCall) != null ? _B : options.experimental_repairToolCall) != null ? _C : this.repairToolCall,
1417
+ responseFormat: await ((_E = (_D = options.output) != null ? _D : this.output) == null ? void 0 : _E.responseFormat),
1499
1418
  experimental_sandbox: sandbox
1500
1419
  });
1501
1420
  let finalMessages;
@@ -1507,7 +1426,7 @@ var WorkflowAgent = class {
1507
1426
  try {
1508
1427
  let result = await iterator.next();
1509
1428
  while (!result.done) {
1510
- if ((_G = mergedGenerationSettings.abortSignal) == null ? void 0 : _G.aborted) {
1429
+ if ((_F = mergedGenerationSettings.abortSignal) == null ? void 0 : _F.aborted) {
1511
1430
  wasAborted = true;
1512
1431
  if (options.onAbort) {
1513
1432
  await options.onAbort({ steps });
@@ -1639,12 +1558,12 @@ var WorkflowAgent = class {
1639
1558
  const messages2 = iterMessages;
1640
1559
  const lastStep2 = steps[steps.length - 1];
1641
1560
  const totalUsage2 = aggregateUsage(steps);
1642
- const finishReason2 = (_H = lastStep2 == null ? void 0 : lastStep2.finishReason) != null ? _H : "other";
1561
+ const finishReason2 = (_G = lastStep2 == null ? void 0 : lastStep2.finishReason) != null ? _G : "other";
1643
1562
  if (mergedOnEnd && !wasAborted) {
1644
1563
  await mergedOnEnd({
1645
1564
  steps,
1646
1565
  messages: messages2,
1647
- text: (_I = lastStep2 == null ? void 0 : lastStep2.text) != null ? _I : "",
1566
+ text: (_H = lastStep2 == null ? void 0 : lastStep2.text) != null ? _H : "",
1648
1567
  finishReason: finishReason2,
1649
1568
  usage: totalUsage2,
1650
1569
  totalUsage: totalUsage2,
@@ -1656,7 +1575,7 @@ var WorkflowAgent = class {
1656
1575
  if (!wasAborted && steps.length > 0) {
1657
1576
  const telemetrySteps = steps.map(normalizeStepForTelemetry2);
1658
1577
  const lastTelemetryStep = telemetrySteps[telemetrySteps.length - 1];
1659
- await ((_J = telemetryDispatcher.onEnd) == null ? void 0 : _J.call(telemetryDispatcher, {
1578
+ await ((_I = telemetryDispatcher.onEnd) == null ? void 0 : _I.call(telemetryDispatcher, {
1660
1579
  ...lastTelemetryStep,
1661
1580
  steps: telemetrySteps,
1662
1581
  usage: totalUsage2,
@@ -1665,7 +1584,21 @@ var WorkflowAgent = class {
1665
1584
  }
1666
1585
  if (options.writable) {
1667
1586
  if (allToolResults.length > 0) {
1668
- await writeToolResults(options.writable, allToolResults);
1587
+ await writeToolResults(
1588
+ options.writable,
1589
+ executedResults.map((r) => {
1590
+ var _a2;
1591
+ return {
1592
+ toolCallId: r.modelResult.toolCallId,
1593
+ toolName: r.modelResult.toolName,
1594
+ input: (_a2 = toolCalls.find(
1595
+ (tc) => tc.toolCallId === r.modelResult.toolCallId
1596
+ )) == null ? void 0 : _a2.input,
1597
+ output: r.rawOutput,
1598
+ isError: r.isError
1599
+ };
1600
+ })
1601
+ );
1669
1602
  }
1670
1603
  const approvalToolCalls = pausedToolCalls.filter((_, i) => {
1671
1604
  const tcIndex = nonProviderToolCalls.indexOf(
@@ -1695,8 +1628,8 @@ var WorkflowAgent = class {
1695
1628
  }
1696
1629
  }
1697
1630
  if (options.writable) {
1698
- const sendFinish = (_K = options.sendFinish) != null ? _K : true;
1699
- const preventClose = (_L = options.preventClose) != null ? _L : false;
1631
+ const sendFinish = (_J = options.sendFinish) != null ? _J : true;
1632
+ const preventClose = (_K = options.preventClose) != null ? _K : false;
1700
1633
  if (sendFinish || !preventClose) {
1701
1634
  await closeStream(options.writable, preventClose, sendFinish);
1702
1635
  }
@@ -1779,7 +1712,8 @@ var WorkflowAgent = class {
1779
1712
  input: (_a2 = toolCalls.find(
1780
1713
  (tc) => tc.toolCallId === r.modelResult.toolCallId
1781
1714
  )) == null ? void 0 : _a2.input,
1782
- output: r.rawOutput
1715
+ output: r.rawOutput,
1716
+ isError: r.isError
1783
1717
  };
1784
1718
  }),
1785
1719
  true
@@ -1836,16 +1770,16 @@ var WorkflowAgent = class {
1836
1770
  } else if (options.onError) {
1837
1771
  await options.onError({ error });
1838
1772
  }
1839
- await ((_M = telemetryDispatcher.onError) == null ? void 0 : _M.call(telemetryDispatcher, error));
1773
+ await ((_L = telemetryDispatcher.onError) == null ? void 0 : _L.call(telemetryDispatcher, error));
1840
1774
  }
1841
1775
  if (hasTerminalError) {
1842
1776
  if (options.onError) {
1843
1777
  await options.onError({ error: terminalError });
1844
1778
  }
1845
- await ((_N = telemetryDispatcher.onError) == null ? void 0 : _N.call(telemetryDispatcher, terminalError));
1779
+ await ((_M = telemetryDispatcher.onError) == null ? void 0 : _M.call(telemetryDispatcher, terminalError));
1846
1780
  }
1847
1781
  const messages = finalMessages != null ? finalMessages : prompt.messages;
1848
- const effectiveOutput = (_O = options.output) != null ? _O : this.output;
1782
+ const effectiveOutput = (_N = options.output) != null ? _N : this.output;
1849
1783
  let experimentalOutput = void 0;
1850
1784
  if (effectiveOutput && steps.length > 0) {
1851
1785
  const lastStep2 = steps[steps.length - 1];
@@ -1870,12 +1804,12 @@ var WorkflowAgent = class {
1870
1804
  }
1871
1805
  const lastStep = steps[steps.length - 1];
1872
1806
  const totalUsage = aggregateUsage(steps);
1873
- const finishReason = (_P = lastStep == null ? void 0 : lastStep.finishReason) != null ? _P : "other";
1807
+ const finishReason = (_O = lastStep == null ? void 0 : lastStep.finishReason) != null ? _O : "other";
1874
1808
  if (mergedOnEnd && !wasAborted) {
1875
1809
  await mergedOnEnd({
1876
1810
  steps,
1877
1811
  messages,
1878
- text: (_Q = lastStep == null ? void 0 : lastStep.text) != null ? _Q : "",
1812
+ text: (_P = lastStep == null ? void 0 : lastStep.text) != null ? _P : "",
1879
1813
  finishReason,
1880
1814
  usage: totalUsage,
1881
1815
  totalUsage,
@@ -1887,7 +1821,7 @@ var WorkflowAgent = class {
1887
1821
  if (!wasAborted && steps.length > 0) {
1888
1822
  const telemetrySteps = steps.map(normalizeStepForTelemetry2);
1889
1823
  const lastTelemetryStep = telemetrySteps[telemetrySteps.length - 1];
1890
- await ((_R = telemetryDispatcher.onEnd) == null ? void 0 : _R.call(telemetryDispatcher, {
1824
+ await ((_Q = telemetryDispatcher.onEnd) == null ? void 0 : _Q.call(telemetryDispatcher, {
1891
1825
  ...lastTelemetryStep,
1892
1826
  steps: telemetrySteps,
1893
1827
  usage: totalUsage,
@@ -1896,8 +1830,8 @@ var WorkflowAgent = class {
1896
1830
  }
1897
1831
  if (hasEncounteredError) {
1898
1832
  if (options.writable) {
1899
- const sendFinish = (_S = options.sendFinish) != null ? _S : true;
1900
- const preventClose = (_T = options.preventClose) != null ? _T : false;
1833
+ const sendFinish = (_R = options.sendFinish) != null ? _R : true;
1834
+ const preventClose = (_S = options.preventClose) != null ? _S : false;
1901
1835
  if (sendFinish || !preventClose) {
1902
1836
  await closeStream(options.writable, preventClose, sendFinish);
1903
1837
  }
@@ -1905,8 +1839,8 @@ var WorkflowAgent = class {
1905
1839
  throw encounteredError;
1906
1840
  }
1907
1841
  if (options.writable) {
1908
- const sendFinish = (_U = options.sendFinish) != null ? _U : true;
1909
- const preventClose = (_V = options.preventClose) != null ? _V : false;
1842
+ const sendFinish = (_T = options.sendFinish) != null ? _T : true;
1843
+ const preventClose = (_U = options.preventClose) != null ? _U : false;
1910
1844
  if (sendFinish || !preventClose) {
1911
1845
  await closeStream(options.writable, preventClose, sendFinish);
1912
1846
  }
@@ -2048,13 +1982,21 @@ async function writeToolResults(writable, results, writeStepBoundary = false) {
2048
1982
  const writer = writable.getWriter();
2049
1983
  try {
2050
1984
  for (const r of results) {
2051
- await writer.write({
2052
- type: "tool-result",
2053
- toolCallId: r.toolCallId,
2054
- toolName: r.toolName,
2055
- input: r.input,
2056
- output: r.output
2057
- });
1985
+ await writer.write(
1986
+ r.isError ? {
1987
+ type: "tool-error",
1988
+ toolCallId: r.toolCallId,
1989
+ toolName: r.toolName,
1990
+ input: r.input,
1991
+ error: r.output
1992
+ } : {
1993
+ type: "tool-result",
1994
+ toolCallId: r.toolCallId,
1995
+ toolName: r.toolName,
1996
+ input: r.input,
1997
+ output: r.output
1998
+ }
1999
+ );
2058
2000
  }
2059
2001
  if (writeStepBoundary) {
2060
2002
  await writer.write({ type: "finish-step" });