@ai-sdk/workflow 2.0.17 → 2.0.19

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,29 @@
1
1
  # @ai-sdk/workflow
2
2
 
3
+ ## 2.0.19
4
+
5
+ ### Patch Changes
6
+
7
+ - 902ce3b: fix(workflow): respect empty activeTools lists
8
+ - c143af4: fix(workflow): propagate agent abort signals to local tool executions
9
+ - Updated dependencies [5190b67]
10
+ - @ai-sdk/provider@4.0.10
11
+ - @ai-sdk/provider-utils@5.0.35
12
+ - ai@7.0.89
13
+
14
+ ## 2.0.18
15
+
16
+ ### Patch Changes
17
+
18
+ - 238aff0: fix(workflow): preserve context and lifecycle callbacks for approved tools
19
+ - 05672ad: fix(workflow): stream failed tool executions as tool errors
20
+ - 3b6ef0c: fix(workflow): infer configured tool parts in WorkflowAgent UI messages
21
+ - d3b1ffb: feat(workflow): add stable onStart and onStepStart callbacks to WorkflowAgent
22
+ - 33d185e: fix(workflow): restrict prepareStep activeTools to configured tool names
23
+ - Updated dependencies [8b6b756]
24
+ - Updated dependencies [e07b577]
25
+ - ai@7.0.88
26
+
3
27
  ## 2.0.17
4
28
 
5
29
  ### 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
@@ -16,6 +16,7 @@ import {
16
16
  createRestrictedTelemetryDispatcher as createRestrictedTelemetryDispatcher2,
17
17
  collectToolApprovals,
18
18
  convertToLanguageModelPrompt,
19
+ mergeAbortSignals,
19
20
  mergeCallbacks,
20
21
  signToolApproval,
21
22
  standardizePrompt,
@@ -511,7 +512,7 @@ async function* streamTextIterator({
511
512
  toolsContext: currentToolsContext
512
513
  }));
513
514
  try {
514
- const effectiveTools = currentActiveTools && currentActiveTools.length > 0 ? (_d = filterActiveTools({
515
+ const effectiveTools = currentActiveTools !== void 0 ? (_d = filterActiveTools({
515
516
  tools,
516
517
  activeTools: currentActiveTools
517
518
  })) != null ? _d : tools : tools;
@@ -803,7 +804,7 @@ function sanitizeProviderMetadataForToolCall(metadata) {
803
804
  // src/workflow-agent.ts
804
805
  var WorkflowAgent = class {
805
806
  constructor(options) {
806
- var _a, _b, _c, _d, _e;
807
+ var _a, _b, _c, _d, _e, _f, _g;
807
808
  this.id = options.id;
808
809
  this.model = options.model;
809
810
  this.tools = (_a = options.tools) != null ? _a : {};
@@ -823,12 +824,12 @@ var WorkflowAgent = class {
823
824
  this.constructorOnStepEnd = (_d = options.onStepEnd) != null ? _d : options.onStepFinish;
824
825
  const { onFinish, onEnd = onFinish } = options;
825
826
  this.constructorOnEnd = onEnd;
826
- this.constructorOnStart = options.experimental_onStart;
827
- this.constructorOnStepStart = options.experimental_onStepStart;
827
+ this.constructorOnStart = (_e = options.onStart) != null ? _e : options.experimental_onStart;
828
+ this.constructorOnStepStart = (_f = options.onStepStart) != null ? _f : options.experimental_onStepStart;
828
829
  this.constructorOnToolExecutionStart = options.onToolExecutionStart;
829
830
  this.constructorOnToolExecutionEnd = options.onToolExecutionEnd;
830
831
  this.prepareCall = options.prepareCall;
831
- this.allowSystemInMessages = (_e = options.allowSystemInMessages) != null ? _e : false;
832
+ this.allowSystemInMessages = (_g = options.allowSystemInMessages) != null ? _g : false;
832
833
  this.generationSettings = {
833
834
  maxOutputTokens: options.maxOutputTokens,
834
835
  temperature: options.temperature,
@@ -849,7 +850,7 @@ var WorkflowAgent = class {
849
850
  throw new Error("Not implemented");
850
851
  }
851
852
  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;
853
+ 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
854
  const { onFinish, onEnd = onFinish } = options;
854
855
  let effectiveModel = this.model;
855
856
  let effectiveInstructions = (_b = (_a = options.instructions) != null ? _a : options.system) != null ? _b : this.instructions;
@@ -941,6 +942,19 @@ var WorkflowAgent = class {
941
942
  });
942
943
  const download = effectiveDownloadFromPrepare;
943
944
  const sandbox = (_n = options.experimental_sandbox) != null ? _n : this.experimentalSandbox;
945
+ const effectiveAbortSignal = mergeAbortSignals(
946
+ (_o = options.abortSignal) != null ? _o : effectiveGenerationSettings.abortSignal,
947
+ options.timeout
948
+ );
949
+ const timeoutAt = options.timeout == null ? void 0 : Date.now() + options.timeout;
950
+ const mergedOnToolExecutionStart = mergeCallbacks(
951
+ this.constructorOnToolExecutionStart,
952
+ options.onToolExecutionStart
953
+ );
954
+ const mergedOnToolExecutionEnd = mergeCallbacks(
955
+ this.constructorOnToolExecutionEnd,
956
+ options.onToolExecutionEnd
957
+ );
944
958
  const collectedApprovals = collectToolApprovals({
945
959
  messages: prompt.messages
946
960
  });
@@ -1021,7 +1035,7 @@ var WorkflowAgent = class {
1021
1035
  invalidToolApprovals[0].error
1022
1036
  );
1023
1037
  } else if (policyDenied.length > 0) {
1024
- revalidationReason = (_o = policyDenied[0].approvalResponse.reason) != null ? _o : "Tool approval denied";
1038
+ revalidationReason = (_p = policyDenied[0].approvalResponse.reason) != null ? _p : "Tool approval denied";
1025
1039
  }
1026
1040
  } catch (error) {
1027
1041
  revalidationReason = getErrorMessage(error);
@@ -1044,106 +1058,25 @@ var WorkflowAgent = class {
1044
1058
  });
1045
1059
  continue;
1046
1060
  }
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",
1061
+ const result = await executeToolWithCallbacks(
1062
+ {
1056
1063
  toolCallId: approval.toolCallId,
1057
1064
  toolName: approval.toolName,
1058
1065
  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
- }
1066
+ },
1067
+ this.tools,
1068
+ prompt.messages,
1069
+ effectiveToolsContext,
1070
+ 0,
1071
+ sandbox
1072
+ );
1073
+ toolResultContent.push(result.modelResult);
1074
+ approvedRawResults.push({
1075
+ toolCallId: approval.toolCallId,
1076
+ toolName: approval.toolName,
1077
+ input: approval.input,
1078
+ output: result.rawOutput
1079
+ });
1147
1080
  }
1148
1081
  }
1149
1082
  for (const denial of deniedToolApprovals) {
@@ -1207,8 +1140,6 @@ var WorkflowAgent = class {
1207
1140
  supportedUrls: {},
1208
1141
  download
1209
1142
  });
1210
- const effectiveAbortSignal = (_s = options.abortSignal) != null ? _s : effectiveGenerationSettings.abortSignal;
1211
- const timeoutAt = options.timeout == null ? void 0 : Date.now() + options.timeout;
1212
1143
  const mergedGenerationSettings = {
1213
1144
  ...effectiveGenerationSettings,
1214
1145
  ...options.maxOutputTokens !== void 0 && {
@@ -1242,12 +1173,12 @@ var WorkflowAgent = class {
1242
1173
  }
1243
1174
  };
1244
1175
  mergedGenerationSettings.headers = withUserAgentSuffix(
1245
- (_t = mergedGenerationSettings.headers) != null ? _t : {},
1176
+ (_q = mergedGenerationSettings.headers) != null ? _q : {},
1246
1177
  "ai-sdk-agent/workflow"
1247
1178
  );
1248
1179
  const mergedOnStepEnd = mergeCallbacks(
1249
1180
  this.constructorOnStepEnd,
1250
- (_u = options.onStepEnd) != null ? _u : options.onStepFinish
1181
+ (_r = options.onStepEnd) != null ? _r : options.onStepFinish
1251
1182
  );
1252
1183
  const mergedOnEnd = mergeCallbacks(
1253
1184
  this.constructorOnEnd,
@@ -1255,26 +1186,18 @@ var WorkflowAgent = class {
1255
1186
  );
1256
1187
  const mergedOnStart = mergeCallbacks(
1257
1188
  this.constructorOnStart,
1258
- options.experimental_onStart
1189
+ (_s = options.onStart) != null ? _s : options.experimental_onStart
1259
1190
  );
1260
1191
  const mergedOnStepStart = mergeCallbacks(
1261
1192
  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
1193
+ (_t = options.onStepStart) != null ? _t : options.experimental_onStepStart
1271
1194
  );
1272
1195
  const effectiveToolChoice = effectiveToolChoiceFromPrepare;
1273
1196
  const effectiveActiveTools = effectiveActiveToolsFromPrepare;
1274
- const effectiveTools = effectiveActiveTools && effectiveActiveTools.length > 0 ? (_v = filterActiveTools2({
1197
+ const effectiveTools = effectiveActiveTools !== void 0 ? (_u = filterActiveTools2({
1275
1198
  tools: this.tools,
1276
1199
  activeTools: effectiveActiveTools
1277
- })) != null ? _v : this.tools : this.tools;
1200
+ })) != null ? _u : this.tools : this.tools;
1278
1201
  const effectiveModelInfo = getModelInfo2(effectiveModel);
1279
1202
  let runtimeContext = effectiveRuntimeContext;
1280
1203
  let toolsContext = effectiveToolsContext;
@@ -1289,7 +1212,7 @@ var WorkflowAgent = class {
1289
1212
  toolsContext
1290
1213
  });
1291
1214
  }
1292
- await ((_y = telemetryDispatcher.onStart) == null ? void 0 : _y.call(telemetryDispatcher, {
1215
+ await ((_x = telemetryDispatcher.onStart) == null ? void 0 : _x.call(telemetryDispatcher, {
1293
1216
  callId: "workflow-agent",
1294
1217
  operationId: "ai.workflowAgent.stream",
1295
1218
  provider: effectiveModelInfo.provider,
@@ -1307,16 +1230,16 @@ var WorkflowAgent = class {
1307
1230
  frequencyPenalty: mergedGenerationSettings.frequencyPenalty,
1308
1231
  stopSequences: mergedGenerationSettings.stopSequences,
1309
1232
  seed: mergedGenerationSettings.seed,
1310
- maxRetries: (_w = mergedGenerationSettings.maxRetries) != null ? _w : 2,
1233
+ maxRetries: (_v = mergedGenerationSettings.maxRetries) != null ? _v : 2,
1311
1234
  timeout: void 0,
1312
1235
  headers: mergedGenerationSettings.headers,
1313
1236
  reasoning: mergedGenerationSettings.reasoning,
1314
1237
  providerOptions: mergedGenerationSettings.providerOptions,
1315
- output: (_x = options.output) != null ? _x : this.output,
1238
+ output: (_w = options.output) != null ? _w : this.output,
1316
1239
  runtimeContext,
1317
1240
  toolsContext
1318
1241
  }));
1319
- const executeToolWithCallbacks = async (toolCall, tools, messages2, perToolContexts, currentStepNumber = 0, stepSandbox) => {
1242
+ async function executeToolWithCallbacks(toolCall, tools, messages2, perToolContexts, currentStepNumber = 0, stepSandbox) {
1320
1243
  var _a2, _b2, _c2, _d2;
1321
1244
  const toolCallEvent = {
1322
1245
  type: "tool-call",
@@ -1353,6 +1276,7 @@ var WorkflowAgent = class {
1353
1276
  tools,
1354
1277
  messages2,
1355
1278
  resolvedContext,
1279
+ effectiveAbortSignal,
1356
1280
  download,
1357
1281
  stepSandbox
1358
1282
  );
@@ -1431,7 +1355,7 @@ var WorkflowAgent = class {
1431
1355
  }));
1432
1356
  }
1433
1357
  return result;
1434
- };
1358
+ }
1435
1359
  const recordProviderExecutedToolTelemetry = async (toolCall, result, messages2, currentStepNumber) => {
1436
1360
  var _a2, _b2;
1437
1361
  const toolCallEvent = {
@@ -1462,7 +1386,7 @@ var WorkflowAgent = class {
1462
1386
  }
1463
1387
  }));
1464
1388
  };
1465
- if ((_z = mergedGenerationSettings.abortSignal) == null ? void 0 : _z.aborted) {
1389
+ if ((_y = mergedGenerationSettings.abortSignal) == null ? void 0 : _y.aborted) {
1466
1390
  if (options.onAbort) {
1467
1391
  await options.onAbort({ steps });
1468
1392
  }
@@ -1486,16 +1410,16 @@ var WorkflowAgent = class {
1486
1410
  stopConditions: effectiveStopWhenFromPrepare,
1487
1411
  onStepEnd: mergedOnStepEnd,
1488
1412
  onStepStart: mergedOnStepStart,
1489
- prepareStep: (_A = options.prepareStep) != null ? _A : this.prepareStep,
1413
+ prepareStep: (_z = options.prepareStep) != null ? _z : this.prepareStep,
1490
1414
  generationSettings: mergedGenerationSettings,
1491
1415
  toolChoice: effectiveToolChoice,
1492
1416
  runtimeContext,
1493
1417
  toolsContext,
1494
1418
  telemetry: effectiveTelemetry,
1495
- includeRawChunks: (_B = options.includeRawChunks) != null ? _B : false,
1419
+ includeRawChunks: (_A = options.includeRawChunks) != null ? _A : false,
1496
1420
  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),
1421
+ repairToolCall: (_C = (_B = options.repairToolCall) != null ? _B : options.experimental_repairToolCall) != null ? _C : this.repairToolCall,
1422
+ responseFormat: await ((_E = (_D = options.output) != null ? _D : this.output) == null ? void 0 : _E.responseFormat),
1499
1423
  experimental_sandbox: sandbox
1500
1424
  });
1501
1425
  let finalMessages;
@@ -1507,7 +1431,7 @@ var WorkflowAgent = class {
1507
1431
  try {
1508
1432
  let result = await iterator.next();
1509
1433
  while (!result.done) {
1510
- if ((_G = mergedGenerationSettings.abortSignal) == null ? void 0 : _G.aborted) {
1434
+ if ((_F = mergedGenerationSettings.abortSignal) == null ? void 0 : _F.aborted) {
1511
1435
  wasAborted = true;
1512
1436
  if (options.onAbort) {
1513
1437
  await options.onAbort({ steps });
@@ -1639,12 +1563,12 @@ var WorkflowAgent = class {
1639
1563
  const messages2 = iterMessages;
1640
1564
  const lastStep2 = steps[steps.length - 1];
1641
1565
  const totalUsage2 = aggregateUsage(steps);
1642
- const finishReason2 = (_H = lastStep2 == null ? void 0 : lastStep2.finishReason) != null ? _H : "other";
1566
+ const finishReason2 = (_G = lastStep2 == null ? void 0 : lastStep2.finishReason) != null ? _G : "other";
1643
1567
  if (mergedOnEnd && !wasAborted) {
1644
1568
  await mergedOnEnd({
1645
1569
  steps,
1646
1570
  messages: messages2,
1647
- text: (_I = lastStep2 == null ? void 0 : lastStep2.text) != null ? _I : "",
1571
+ text: (_H = lastStep2 == null ? void 0 : lastStep2.text) != null ? _H : "",
1648
1572
  finishReason: finishReason2,
1649
1573
  usage: totalUsage2,
1650
1574
  totalUsage: totalUsage2,
@@ -1656,7 +1580,7 @@ var WorkflowAgent = class {
1656
1580
  if (!wasAborted && steps.length > 0) {
1657
1581
  const telemetrySteps = steps.map(normalizeStepForTelemetry2);
1658
1582
  const lastTelemetryStep = telemetrySteps[telemetrySteps.length - 1];
1659
- await ((_J = telemetryDispatcher.onEnd) == null ? void 0 : _J.call(telemetryDispatcher, {
1583
+ await ((_I = telemetryDispatcher.onEnd) == null ? void 0 : _I.call(telemetryDispatcher, {
1660
1584
  ...lastTelemetryStep,
1661
1585
  steps: telemetrySteps,
1662
1586
  usage: totalUsage2,
@@ -1665,7 +1589,21 @@ var WorkflowAgent = class {
1665
1589
  }
1666
1590
  if (options.writable) {
1667
1591
  if (allToolResults.length > 0) {
1668
- await writeToolResults(options.writable, allToolResults);
1592
+ await writeToolResults(
1593
+ options.writable,
1594
+ executedResults.map((r) => {
1595
+ var _a2;
1596
+ return {
1597
+ toolCallId: r.modelResult.toolCallId,
1598
+ toolName: r.modelResult.toolName,
1599
+ input: (_a2 = toolCalls.find(
1600
+ (tc) => tc.toolCallId === r.modelResult.toolCallId
1601
+ )) == null ? void 0 : _a2.input,
1602
+ output: r.rawOutput,
1603
+ isError: r.isError
1604
+ };
1605
+ })
1606
+ );
1669
1607
  }
1670
1608
  const approvalToolCalls = pausedToolCalls.filter((_, i) => {
1671
1609
  const tcIndex = nonProviderToolCalls.indexOf(
@@ -1695,8 +1633,8 @@ var WorkflowAgent = class {
1695
1633
  }
1696
1634
  }
1697
1635
  if (options.writable) {
1698
- const sendFinish = (_K = options.sendFinish) != null ? _K : true;
1699
- const preventClose = (_L = options.preventClose) != null ? _L : false;
1636
+ const sendFinish = (_J = options.sendFinish) != null ? _J : true;
1637
+ const preventClose = (_K = options.preventClose) != null ? _K : false;
1700
1638
  if (sendFinish || !preventClose) {
1701
1639
  await closeStream(options.writable, preventClose, sendFinish);
1702
1640
  }
@@ -1779,7 +1717,8 @@ var WorkflowAgent = class {
1779
1717
  input: (_a2 = toolCalls.find(
1780
1718
  (tc) => tc.toolCallId === r.modelResult.toolCallId
1781
1719
  )) == null ? void 0 : _a2.input,
1782
- output: r.rawOutput
1720
+ output: r.rawOutput,
1721
+ isError: r.isError
1783
1722
  };
1784
1723
  }),
1785
1724
  true
@@ -1836,16 +1775,16 @@ var WorkflowAgent = class {
1836
1775
  } else if (options.onError) {
1837
1776
  await options.onError({ error });
1838
1777
  }
1839
- await ((_M = telemetryDispatcher.onError) == null ? void 0 : _M.call(telemetryDispatcher, error));
1778
+ await ((_L = telemetryDispatcher.onError) == null ? void 0 : _L.call(telemetryDispatcher, error));
1840
1779
  }
1841
1780
  if (hasTerminalError) {
1842
1781
  if (options.onError) {
1843
1782
  await options.onError({ error: terminalError });
1844
1783
  }
1845
- await ((_N = telemetryDispatcher.onError) == null ? void 0 : _N.call(telemetryDispatcher, terminalError));
1784
+ await ((_M = telemetryDispatcher.onError) == null ? void 0 : _M.call(telemetryDispatcher, terminalError));
1846
1785
  }
1847
1786
  const messages = finalMessages != null ? finalMessages : prompt.messages;
1848
- const effectiveOutput = (_O = options.output) != null ? _O : this.output;
1787
+ const effectiveOutput = (_N = options.output) != null ? _N : this.output;
1849
1788
  let experimentalOutput = void 0;
1850
1789
  if (effectiveOutput && steps.length > 0) {
1851
1790
  const lastStep2 = steps[steps.length - 1];
@@ -1870,12 +1809,12 @@ var WorkflowAgent = class {
1870
1809
  }
1871
1810
  const lastStep = steps[steps.length - 1];
1872
1811
  const totalUsage = aggregateUsage(steps);
1873
- const finishReason = (_P = lastStep == null ? void 0 : lastStep.finishReason) != null ? _P : "other";
1812
+ const finishReason = (_O = lastStep == null ? void 0 : lastStep.finishReason) != null ? _O : "other";
1874
1813
  if (mergedOnEnd && !wasAborted) {
1875
1814
  await mergedOnEnd({
1876
1815
  steps,
1877
1816
  messages,
1878
- text: (_Q = lastStep == null ? void 0 : lastStep.text) != null ? _Q : "",
1817
+ text: (_P = lastStep == null ? void 0 : lastStep.text) != null ? _P : "",
1879
1818
  finishReason,
1880
1819
  usage: totalUsage,
1881
1820
  totalUsage,
@@ -1887,7 +1826,7 @@ var WorkflowAgent = class {
1887
1826
  if (!wasAborted && steps.length > 0) {
1888
1827
  const telemetrySteps = steps.map(normalizeStepForTelemetry2);
1889
1828
  const lastTelemetryStep = telemetrySteps[telemetrySteps.length - 1];
1890
- await ((_R = telemetryDispatcher.onEnd) == null ? void 0 : _R.call(telemetryDispatcher, {
1829
+ await ((_Q = telemetryDispatcher.onEnd) == null ? void 0 : _Q.call(telemetryDispatcher, {
1891
1830
  ...lastTelemetryStep,
1892
1831
  steps: telemetrySteps,
1893
1832
  usage: totalUsage,
@@ -1896,8 +1835,8 @@ var WorkflowAgent = class {
1896
1835
  }
1897
1836
  if (hasEncounteredError) {
1898
1837
  if (options.writable) {
1899
- const sendFinish = (_S = options.sendFinish) != null ? _S : true;
1900
- const preventClose = (_T = options.preventClose) != null ? _T : false;
1838
+ const sendFinish = (_R = options.sendFinish) != null ? _R : true;
1839
+ const preventClose = (_S = options.preventClose) != null ? _S : false;
1901
1840
  if (sendFinish || !preventClose) {
1902
1841
  await closeStream(options.writable, preventClose, sendFinish);
1903
1842
  }
@@ -1905,8 +1844,8 @@ var WorkflowAgent = class {
1905
1844
  throw encounteredError;
1906
1845
  }
1907
1846
  if (options.writable) {
1908
- const sendFinish = (_U = options.sendFinish) != null ? _U : true;
1909
- const preventClose = (_V = options.preventClose) != null ? _V : false;
1847
+ const sendFinish = (_T = options.sendFinish) != null ? _T : true;
1848
+ const preventClose = (_U = options.preventClose) != null ? _U : false;
1910
1849
  if (sendFinish || !preventClose) {
1911
1850
  await closeStream(options.writable, preventClose, sendFinish);
1912
1851
  }
@@ -2048,13 +1987,21 @@ async function writeToolResults(writable, results, writeStepBoundary = false) {
2048
1987
  const writer = writable.getWriter();
2049
1988
  try {
2050
1989
  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
- });
1990
+ await writer.write(
1991
+ r.isError ? {
1992
+ type: "tool-error",
1993
+ toolCallId: r.toolCallId,
1994
+ toolName: r.toolName,
1995
+ input: r.input,
1996
+ error: r.output
1997
+ } : {
1998
+ type: "tool-result",
1999
+ toolCallId: r.toolCallId,
2000
+ toolName: r.toolName,
2001
+ input: r.input,
2002
+ output: r.output
2003
+ }
2004
+ );
2058
2005
  }
2059
2006
  if (writeStepBoundary) {
2060
2007
  await writer.write({ type: "finish-step" });
@@ -2177,7 +2124,7 @@ function getToolCallbackMessages(messages) {
2177
2124
  const withoutAssistantToolCall = ((_a = messages.at(-1)) == null ? void 0 : _a.role) === "assistant" ? messages.slice(0, -1) : messages;
2178
2125
  return withoutAssistantToolCall;
2179
2126
  }
2180
- async function executeTool(toolCall, tools, messages, context, download, sandbox) {
2127
+ async function executeTool(toolCall, tools, messages, context, abortSignal, download, sandbox) {
2181
2128
  const tool2 = tools[toolCall.toolName];
2182
2129
  if (!tool2) throw new Error(`Tool "${toolCall.toolName}" not found`);
2183
2130
  if (typeof tool2.execute !== "function") {
@@ -2193,6 +2140,8 @@ async function executeTool(toolCall, tools, messages, context, download, sandbox
2193
2140
  toolCallId: toolCall.toolCallId,
2194
2141
  // Pass the conversation messages to the tool so it has context about the conversation
2195
2142
  messages,
2143
+ // Pass the effective agent signal so in-flight tool work can cooperatively cancel
2144
+ abortSignal,
2196
2145
  // Pass per-tool context to the tool (resolved from `toolsContext`)
2197
2146
  context,
2198
2147
  experimental_sandbox: sandbox