@ai-sdk/workflow 1.0.0-canary.93 → 1.0.0
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 +168 -0
- package/dist/index.d.mts +32 -1
- package/dist/index.mjs +109 -62
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/stream-text-iterator.ts +12 -0
- package/src/test/agent-e2e-workflows.ts +86 -0
- package/src/test/test-sandbox.ts +26 -0
- package/src/workflow-agent.ts +113 -7
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// src/workflow-agent.ts
|
|
2
2
|
import {
|
|
3
3
|
getErrorMessage,
|
|
4
|
-
validateTypes
|
|
4
|
+
validateTypes,
|
|
5
|
+
withUserAgentSuffix
|
|
5
6
|
} from "@ai-sdk/provider-utils";
|
|
6
7
|
import {
|
|
7
8
|
Output,
|
|
@@ -365,9 +366,10 @@ async function* streamTextIterator({
|
|
|
365
366
|
telemetry,
|
|
366
367
|
includeRawChunks = false,
|
|
367
368
|
repairToolCall,
|
|
368
|
-
responseFormat
|
|
369
|
+
responseFormat,
|
|
370
|
+
experimental_sandbox: sandbox
|
|
369
371
|
}) {
|
|
370
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
372
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
371
373
|
let conversationPrompt = [...prompt];
|
|
372
374
|
let currentModel = model;
|
|
373
375
|
let currentGenerationSettings = generationSettings != null ? generationSettings : {};
|
|
@@ -390,6 +392,7 @@ async function* streamTextIterator({
|
|
|
390
392
|
if ((_a = currentGenerationSettings.abortSignal) == null ? void 0 : _a.aborted) {
|
|
391
393
|
break;
|
|
392
394
|
}
|
|
395
|
+
let stepSandbox = sandbox;
|
|
393
396
|
if (prepareStep) {
|
|
394
397
|
const prepareResult = await prepareStep({
|
|
395
398
|
model: currentModel,
|
|
@@ -397,8 +400,10 @@ async function* streamTextIterator({
|
|
|
397
400
|
steps,
|
|
398
401
|
messages: conversationPrompt,
|
|
399
402
|
runtimeContext: currentRuntimeContext,
|
|
400
|
-
toolsContext: currentToolsContext
|
|
403
|
+
toolsContext: currentToolsContext,
|
|
404
|
+
experimental_sandbox: sandbox
|
|
401
405
|
});
|
|
406
|
+
stepSandbox = (_b = prepareResult == null ? void 0 : prepareResult.experimental_sandbox) != null ? _b : sandbox;
|
|
402
407
|
if ((prepareResult == null ? void 0 : prepareResult.model) !== void 0) {
|
|
403
408
|
currentModel = prepareResult.model;
|
|
404
409
|
}
|
|
@@ -508,7 +513,7 @@ async function* streamTextIterator({
|
|
|
508
513
|
});
|
|
509
514
|
}
|
|
510
515
|
const stepStartModelInfo = getModelInfo(currentModel);
|
|
511
|
-
await ((
|
|
516
|
+
await ((_c = telemetryDispatcher.onStepStart) == null ? void 0 : _c.call(telemetryDispatcher, {
|
|
512
517
|
callId: "workflow-agent",
|
|
513
518
|
provider: stepStartModelInfo.provider,
|
|
514
519
|
modelId: stepStartModelInfo.modelId,
|
|
@@ -525,13 +530,13 @@ async function* streamTextIterator({
|
|
|
525
530
|
toolsContext: currentToolsContext
|
|
526
531
|
}));
|
|
527
532
|
try {
|
|
528
|
-
const effectiveTools = currentActiveTools && currentActiveTools.length > 0 ? (
|
|
533
|
+
const effectiveTools = currentActiveTools && currentActiveTools.length > 0 ? (_d = filterActiveTools({
|
|
529
534
|
tools,
|
|
530
535
|
activeTools: currentActiveTools
|
|
531
|
-
})) != null ?
|
|
536
|
+
})) != null ? _d : tools : tools;
|
|
532
537
|
const serializedTools = serializeToolSet(effectiveTools);
|
|
533
538
|
const modelCallInfo = getModelInfo(currentModel);
|
|
534
|
-
await ((
|
|
539
|
+
await ((_e = telemetryDispatcher.onLanguageModelCallStart) == null ? void 0 : _e.call(telemetryDispatcher, {
|
|
535
540
|
callId: "workflow-agent",
|
|
536
541
|
provider: modelCallInfo.provider,
|
|
537
542
|
modelId: modelCallInfo.modelId,
|
|
@@ -565,10 +570,10 @@ async function* streamTextIterator({
|
|
|
565
570
|
stepNumber
|
|
566
571
|
}
|
|
567
572
|
);
|
|
568
|
-
await ((
|
|
573
|
+
await ((_j = telemetryDispatcher.onLanguageModelCallEnd) == null ? void 0 : _j.call(telemetryDispatcher, {
|
|
569
574
|
callId: step.callId,
|
|
570
|
-
provider: (
|
|
571
|
-
modelId: (
|
|
575
|
+
provider: (_g = (_f = step.model) == null ? void 0 : _f.provider) != null ? _g : "unknown",
|
|
576
|
+
modelId: (_i = (_h = step.model) == null ? void 0 : _h.modelId) != null ? _i : "unknown",
|
|
572
577
|
finishReason: step.finishReason,
|
|
573
578
|
usage: step.usage,
|
|
574
579
|
content: step.content,
|
|
@@ -603,6 +608,7 @@ async function* streamTextIterator({
|
|
|
603
608
|
step,
|
|
604
609
|
runtimeContext: currentRuntimeContext,
|
|
605
610
|
toolsContext: currentToolsContext,
|
|
611
|
+
experimental_sandbox: stepSandbox,
|
|
606
612
|
providerExecutedToolResults
|
|
607
613
|
};
|
|
608
614
|
conversationPrompt.push({
|
|
@@ -647,7 +653,7 @@ async function* streamTextIterator({
|
|
|
647
653
|
if (resolvedOnStepEnd) {
|
|
648
654
|
await resolvedOnStepEnd(step);
|
|
649
655
|
}
|
|
650
|
-
await ((
|
|
656
|
+
await ((_k = telemetryDispatcher.onStepEnd) == null ? void 0 : _k.call(telemetryDispatcher, normalizeStepForTelemetry(step)));
|
|
651
657
|
} catch (error) {
|
|
652
658
|
if (onError) {
|
|
653
659
|
await onError({ error });
|
|
@@ -661,7 +667,8 @@ async function* streamTextIterator({
|
|
|
661
667
|
messages: conversationPrompt,
|
|
662
668
|
step: lastStep,
|
|
663
669
|
runtimeContext: currentRuntimeContext,
|
|
664
|
-
toolsContext: currentToolsContext
|
|
670
|
+
toolsContext: currentToolsContext,
|
|
671
|
+
experimental_sandbox: sandbox
|
|
665
672
|
};
|
|
666
673
|
}
|
|
667
674
|
return conversationPrompt;
|
|
@@ -701,7 +708,7 @@ function sanitizeProviderMetadataForToolCall(metadata) {
|
|
|
701
708
|
// src/workflow-agent.ts
|
|
702
709
|
var WorkflowAgent = class {
|
|
703
710
|
constructor(options) {
|
|
704
|
-
var _a, _b, _c;
|
|
711
|
+
var _a, _b, _c, _d;
|
|
705
712
|
this.id = options.id;
|
|
706
713
|
this.model = options.model;
|
|
707
714
|
this.tools = (_a = options.tools) != null ? _a : {};
|
|
@@ -715,6 +722,7 @@ var WorkflowAgent = class {
|
|
|
715
722
|
this.output = options.output;
|
|
716
723
|
this.experimentalRepairToolCall = options.experimental_repairToolCall;
|
|
717
724
|
this.experimentalDownload = options.experimental_download;
|
|
725
|
+
this.experimentalSandbox = options.experimental_sandbox;
|
|
718
726
|
this.prepareStep = options.prepareStep;
|
|
719
727
|
this.constructorOnStepEnd = (_c = options.onStepEnd) != null ? _c : options.onStepFinish;
|
|
720
728
|
const { onFinish, onEnd = onFinish } = options;
|
|
@@ -724,6 +732,7 @@ var WorkflowAgent = class {
|
|
|
724
732
|
this.constructorOnToolExecutionStart = options.onToolExecutionStart;
|
|
725
733
|
this.constructorOnToolExecutionEnd = options.onToolExecutionEnd;
|
|
726
734
|
this.prepareCall = options.prepareCall;
|
|
735
|
+
this.allowSystemInMessages = (_d = options.allowSystemInMessages) != null ? _d : false;
|
|
727
736
|
this.generationSettings = {
|
|
728
737
|
maxOutputTokens: options.maxOutputTokens,
|
|
729
738
|
temperature: options.temperature,
|
|
@@ -743,7 +752,7 @@ var WorkflowAgent = class {
|
|
|
743
752
|
throw new Error("Not implemented");
|
|
744
753
|
}
|
|
745
754
|
async stream(options) {
|
|
746
|
-
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;
|
|
755
|
+
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;
|
|
747
756
|
const { onFinish, onEnd = onFinish } = options;
|
|
748
757
|
let effectiveModel = this.model;
|
|
749
758
|
let effectiveInstructions = (_a = options.system) != null ? _a : this.instructions;
|
|
@@ -811,11 +820,11 @@ var WorkflowAgent = class {
|
|
|
811
820
|
});
|
|
812
821
|
const prompt = await standardizePrompt({
|
|
813
822
|
system: effectiveInstructions,
|
|
814
|
-
allowSystemInMessages:
|
|
815
|
-
// TODO: consider exposing this as a parameter
|
|
823
|
+
allowSystemInMessages: this.allowSystemInMessages,
|
|
816
824
|
...effectivePrompt != null ? { prompt: effectivePrompt } : { messages: effectiveMessages }
|
|
817
825
|
});
|
|
818
826
|
const download = (_i = options.experimental_download) != null ? _i : this.experimentalDownload;
|
|
827
|
+
const sandbox = (_j = options.experimental_sandbox) != null ? _j : this.experimentalSandbox;
|
|
819
828
|
const collectedApprovals = collectToolApprovals({
|
|
820
829
|
messages: prompt.messages
|
|
821
830
|
});
|
|
@@ -825,6 +834,7 @@ var WorkflowAgent = class {
|
|
|
825
834
|
toolName: collected.toolCall.toolName,
|
|
826
835
|
input: collected.toolCall.input,
|
|
827
836
|
reason: collected.approvalResponse.reason,
|
|
837
|
+
providerExecuted: collected.toolCall.providerExecuted === true,
|
|
828
838
|
collected
|
|
829
839
|
})
|
|
830
840
|
);
|
|
@@ -833,14 +843,24 @@ var WorkflowAgent = class {
|
|
|
833
843
|
toolCallId: collected.toolCall.toolCallId,
|
|
834
844
|
toolName: collected.toolCall.toolName,
|
|
835
845
|
input: collected.toolCall.input,
|
|
836
|
-
reason: collected.approvalResponse.reason
|
|
846
|
+
reason: collected.approvalResponse.reason,
|
|
847
|
+
providerExecuted: collected.toolCall.providerExecuted === true
|
|
837
848
|
})
|
|
838
849
|
);
|
|
850
|
+
const providerExecutedApprovalIds = new Set(
|
|
851
|
+
[
|
|
852
|
+
...collectedApprovals.approvedToolApprovals,
|
|
853
|
+
...collectedApprovals.deniedToolApprovals
|
|
854
|
+
].filter((collected) => collected.toolCall.providerExecuted === true).map((collected) => collected.approvalResponse.approvalId)
|
|
855
|
+
);
|
|
839
856
|
if (approvedToolApprovals.length > 0 || deniedToolApprovals.length > 0) {
|
|
840
857
|
const _toolResultMessages = [];
|
|
841
858
|
const toolResultContent = [];
|
|
842
859
|
const approvedRawResults = [];
|
|
843
860
|
for (const approval of approvedToolApprovals) {
|
|
861
|
+
if (approval.providerExecuted) {
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
844
864
|
const tool2 = this.tools[approval.toolName];
|
|
845
865
|
if (tool2 && typeof tool2.execute === "function") {
|
|
846
866
|
if (!tool2.needsApproval) {
|
|
@@ -873,7 +893,7 @@ var WorkflowAgent = class {
|
|
|
873
893
|
runtimeContext: effectiveRuntimeContext
|
|
874
894
|
});
|
|
875
895
|
if (policyDenied.length > 0) {
|
|
876
|
-
revalidationReason = (
|
|
896
|
+
revalidationReason = (_k = policyDenied[0].approvalResponse.reason) != null ? _k : "Tool approval denied";
|
|
877
897
|
}
|
|
878
898
|
} catch (error) {
|
|
879
899
|
revalidationReason = getErrorMessage(error);
|
|
@@ -910,7 +930,7 @@ var WorkflowAgent = class {
|
|
|
910
930
|
input: approval.input
|
|
911
931
|
};
|
|
912
932
|
const messages2 = prompt.messages;
|
|
913
|
-
await ((
|
|
933
|
+
await ((_l = telemetryDispatcher.onToolExecutionStart) == null ? void 0 : _l.call(telemetryDispatcher, {
|
|
914
934
|
toolCall: toolCallEvent,
|
|
915
935
|
stepNumber: 0,
|
|
916
936
|
messages: messages2,
|
|
@@ -920,14 +940,15 @@ var WorkflowAgent = class {
|
|
|
920
940
|
const executeApprovedTool = () => execute(approval.input, {
|
|
921
941
|
toolCallId: approval.toolCallId,
|
|
922
942
|
messages: [],
|
|
923
|
-
context: resolvedContext
|
|
943
|
+
context: resolvedContext,
|
|
944
|
+
experimental_sandbox: sandbox
|
|
924
945
|
});
|
|
925
946
|
const toolResult = telemetryDispatcher.executeTool != null ? await telemetryDispatcher.executeTool({
|
|
926
947
|
callId: "workflow-agent",
|
|
927
948
|
toolCallId: approval.toolCallId,
|
|
928
949
|
execute: executeApprovedTool
|
|
929
950
|
}) : await executeApprovedTool();
|
|
930
|
-
await ((
|
|
951
|
+
await ((_m = telemetryDispatcher.onToolExecutionEnd) == null ? void 0 : _m.call(telemetryDispatcher, {
|
|
931
952
|
toolCall: toolCallEvent,
|
|
932
953
|
stepNumber: 0,
|
|
933
954
|
durationMs: Date.now() - startTime,
|
|
@@ -959,7 +980,7 @@ var WorkflowAgent = class {
|
|
|
959
980
|
});
|
|
960
981
|
} catch (error) {
|
|
961
982
|
const errorMessage = getErrorMessage(error);
|
|
962
|
-
await ((
|
|
983
|
+
await ((_n = telemetryDispatcher.onToolExecutionEnd) == null ? void 0 : _n.call(telemetryDispatcher, {
|
|
963
984
|
toolCall: {
|
|
964
985
|
type: "tool-call",
|
|
965
986
|
toolCallId: approval.toolCallId,
|
|
@@ -998,6 +1019,9 @@ var WorkflowAgent = class {
|
|
|
998
1019
|
}
|
|
999
1020
|
}
|
|
1000
1021
|
for (const denial of deniedToolApprovals) {
|
|
1022
|
+
if (denial.providerExecuted) {
|
|
1023
|
+
continue;
|
|
1024
|
+
}
|
|
1001
1025
|
toolResultContent.push({
|
|
1002
1026
|
type: "tool-result",
|
|
1003
1027
|
toolCallId: denial.toolCallId,
|
|
@@ -1012,15 +1036,21 @@ var WorkflowAgent = class {
|
|
|
1012
1036
|
for (const msg of prompt.messages) {
|
|
1013
1037
|
if (msg.role === "assistant" && Array.isArray(msg.content)) {
|
|
1014
1038
|
const filtered = msg.content.filter(
|
|
1015
|
-
(p) => p.type !== "tool-approval-request"
|
|
1039
|
+
(p) => p.type !== "tool-approval-request" || providerExecutedApprovalIds.has(p.approvalId)
|
|
1016
1040
|
);
|
|
1017
1041
|
if (filtered.length > 0) {
|
|
1018
1042
|
cleanedMessages.push({ ...msg, content: filtered });
|
|
1019
1043
|
}
|
|
1020
1044
|
} else if (msg.role === "tool") {
|
|
1021
|
-
const filtered = msg.content.
|
|
1022
|
-
(p
|
|
1023
|
-
|
|
1045
|
+
const filtered = msg.content.flatMap((p) => {
|
|
1046
|
+
if (p.type !== "tool-approval-response") {
|
|
1047
|
+
return [p];
|
|
1048
|
+
}
|
|
1049
|
+
if (!providerExecutedApprovalIds.has(p.approvalId)) {
|
|
1050
|
+
return [];
|
|
1051
|
+
}
|
|
1052
|
+
return [{ ...p, providerExecuted: true }];
|
|
1053
|
+
});
|
|
1024
1054
|
if (filtered.length > 0) {
|
|
1025
1055
|
cleanedMessages.push({ ...msg, content: filtered });
|
|
1026
1056
|
}
|
|
@@ -1050,7 +1080,7 @@ var WorkflowAgent = class {
|
|
|
1050
1080
|
download
|
|
1051
1081
|
});
|
|
1052
1082
|
const effectiveAbortSignal = mergeAbortSignals(
|
|
1053
|
-
(
|
|
1083
|
+
(_o = options.abortSignal) != null ? _o : effectiveGenerationSettings.abortSignal,
|
|
1054
1084
|
options.timeout
|
|
1055
1085
|
);
|
|
1056
1086
|
const mergedGenerationSettings = {
|
|
@@ -1084,9 +1114,13 @@ var WorkflowAgent = class {
|
|
|
1084
1114
|
providerOptions: options.providerOptions
|
|
1085
1115
|
}
|
|
1086
1116
|
};
|
|
1117
|
+
mergedGenerationSettings.headers = withUserAgentSuffix(
|
|
1118
|
+
(_p = mergedGenerationSettings.headers) != null ? _p : {},
|
|
1119
|
+
"ai-sdk-agent/workflow"
|
|
1120
|
+
);
|
|
1087
1121
|
const mergedOnStepEnd = mergeCallbacks(
|
|
1088
1122
|
this.constructorOnStepEnd,
|
|
1089
|
-
(
|
|
1123
|
+
(_q = options.onStepEnd) != null ? _q : options.onStepFinish
|
|
1090
1124
|
);
|
|
1091
1125
|
const mergedOnEnd = mergeCallbacks(
|
|
1092
1126
|
this.constructorOnEnd,
|
|
@@ -1109,11 +1143,11 @@ var WorkflowAgent = class {
|
|
|
1109
1143
|
options.onToolExecutionEnd
|
|
1110
1144
|
);
|
|
1111
1145
|
const effectiveToolChoice = effectiveToolChoiceFromPrepare;
|
|
1112
|
-
const effectiveActiveTools = (
|
|
1113
|
-
const effectiveTools = effectiveActiveTools && effectiveActiveTools.length > 0 ? (
|
|
1146
|
+
const effectiveActiveTools = (_r = options.activeTools) != null ? _r : this.activeTools;
|
|
1147
|
+
const effectiveTools = effectiveActiveTools && effectiveActiveTools.length > 0 ? (_s = filterActiveTools2({
|
|
1114
1148
|
tools: this.tools,
|
|
1115
1149
|
activeTools: effectiveActiveTools
|
|
1116
|
-
})) != null ?
|
|
1150
|
+
})) != null ? _s : this.tools : this.tools;
|
|
1117
1151
|
const effectiveModelInfo = getModelInfo2(effectiveModel);
|
|
1118
1152
|
let runtimeContext = effectiveRuntimeContext;
|
|
1119
1153
|
let toolsContext = effectiveToolsContext;
|
|
@@ -1128,7 +1162,7 @@ var WorkflowAgent = class {
|
|
|
1128
1162
|
toolsContext
|
|
1129
1163
|
});
|
|
1130
1164
|
}
|
|
1131
|
-
await ((
|
|
1165
|
+
await ((_v = telemetryDispatcher.onStart) == null ? void 0 : _v.call(telemetryDispatcher, {
|
|
1132
1166
|
callId: "workflow-agent",
|
|
1133
1167
|
operationId: "ai.workflowAgent.stream",
|
|
1134
1168
|
provider: effectiveModelInfo.provider,
|
|
@@ -1146,15 +1180,15 @@ var WorkflowAgent = class {
|
|
|
1146
1180
|
frequencyPenalty: mergedGenerationSettings.frequencyPenalty,
|
|
1147
1181
|
stopSequences: mergedGenerationSettings.stopSequences,
|
|
1148
1182
|
seed: mergedGenerationSettings.seed,
|
|
1149
|
-
maxRetries: (
|
|
1183
|
+
maxRetries: (_t = mergedGenerationSettings.maxRetries) != null ? _t : 2,
|
|
1150
1184
|
timeout: void 0,
|
|
1151
1185
|
headers: mergedGenerationSettings.headers,
|
|
1152
1186
|
providerOptions: mergedGenerationSettings.providerOptions,
|
|
1153
|
-
output: (
|
|
1187
|
+
output: (_u = options.output) != null ? _u : this.output,
|
|
1154
1188
|
runtimeContext,
|
|
1155
1189
|
toolsContext
|
|
1156
1190
|
}));
|
|
1157
|
-
const executeToolWithCallbacks = async (toolCall, tools, messages2, perToolContexts, currentStepNumber = 0) => {
|
|
1191
|
+
const executeToolWithCallbacks = async (toolCall, tools, messages2, perToolContexts, currentStepNumber = 0, stepSandbox) => {
|
|
1158
1192
|
var _a2, _b2, _c2, _d2;
|
|
1159
1193
|
const toolCallEvent = {
|
|
1160
1194
|
type: "tool-call",
|
|
@@ -1186,7 +1220,14 @@ var WorkflowAgent = class {
|
|
|
1186
1220
|
const startTime = Date.now();
|
|
1187
1221
|
let result;
|
|
1188
1222
|
try {
|
|
1189
|
-
const execute = () => executeTool(
|
|
1223
|
+
const execute = () => executeTool(
|
|
1224
|
+
toolCall,
|
|
1225
|
+
tools,
|
|
1226
|
+
messages2,
|
|
1227
|
+
resolvedContext,
|
|
1228
|
+
download,
|
|
1229
|
+
stepSandbox
|
|
1230
|
+
);
|
|
1190
1231
|
result = telemetryDispatcher.executeTool != null ? await telemetryDispatcher.executeTool({
|
|
1191
1232
|
callId: "workflow-agent",
|
|
1192
1233
|
toolCallId: toolCall.toolCallId,
|
|
@@ -1293,7 +1334,7 @@ var WorkflowAgent = class {
|
|
|
1293
1334
|
}
|
|
1294
1335
|
}));
|
|
1295
1336
|
};
|
|
1296
|
-
if ((
|
|
1337
|
+
if ((_w = mergedGenerationSettings.abortSignal) == null ? void 0 : _w.aborted) {
|
|
1297
1338
|
if (options.onAbort) {
|
|
1298
1339
|
await options.onAbort({ steps });
|
|
1299
1340
|
}
|
|
@@ -1310,19 +1351,20 @@ var WorkflowAgent = class {
|
|
|
1310
1351
|
tools: effectiveTools,
|
|
1311
1352
|
writable: options.writable,
|
|
1312
1353
|
prompt: modelPrompt,
|
|
1313
|
-
stopConditions: (
|
|
1354
|
+
stopConditions: (_x = options.stopWhen) != null ? _x : this.stopWhen,
|
|
1314
1355
|
onStepEnd: mergedOnStepEnd,
|
|
1315
1356
|
onStepStart: mergedOnStepStart,
|
|
1316
1357
|
onError: options.onError,
|
|
1317
|
-
prepareStep: (
|
|
1358
|
+
prepareStep: (_y = options.prepareStep) != null ? _y : this.prepareStep,
|
|
1318
1359
|
generationSettings: mergedGenerationSettings,
|
|
1319
1360
|
toolChoice: effectiveToolChoice,
|
|
1320
1361
|
runtimeContext,
|
|
1321
1362
|
toolsContext,
|
|
1322
1363
|
telemetry: effectiveTelemetry,
|
|
1323
|
-
includeRawChunks: (
|
|
1324
|
-
repairToolCall: (
|
|
1325
|
-
responseFormat: await ((
|
|
1364
|
+
includeRawChunks: (_z = options.includeRawChunks) != null ? _z : false,
|
|
1365
|
+
repairToolCall: (_A = options.experimental_repairToolCall) != null ? _A : this.experimentalRepairToolCall,
|
|
1366
|
+
responseFormat: await ((_C = (_B = options.output) != null ? _B : this.output) == null ? void 0 : _C.responseFormat),
|
|
1367
|
+
experimental_sandbox: sandbox
|
|
1326
1368
|
});
|
|
1327
1369
|
let finalMessages;
|
|
1328
1370
|
let encounteredError;
|
|
@@ -1330,7 +1372,7 @@ var WorkflowAgent = class {
|
|
|
1330
1372
|
try {
|
|
1331
1373
|
let result = await iterator.next();
|
|
1332
1374
|
while (!result.done) {
|
|
1333
|
-
if ((
|
|
1375
|
+
if ((_D = mergedGenerationSettings.abortSignal) == null ? void 0 : _D.aborted) {
|
|
1334
1376
|
wasAborted = true;
|
|
1335
1377
|
if (options.onAbort) {
|
|
1336
1378
|
await options.onAbort({ steps });
|
|
@@ -1343,8 +1385,10 @@ var WorkflowAgent = class {
|
|
|
1343
1385
|
step,
|
|
1344
1386
|
runtimeContext: yieldedRuntimeContext,
|
|
1345
1387
|
toolsContext: yieldedToolsContext,
|
|
1388
|
+
experimental_sandbox: stepSandbox,
|
|
1346
1389
|
providerExecutedToolResults
|
|
1347
1390
|
} = result.value;
|
|
1391
|
+
const toolExecutionSandbox = stepSandbox != null ? stepSandbox : sandbox;
|
|
1348
1392
|
const currentStepNumber = steps.length;
|
|
1349
1393
|
if (step) {
|
|
1350
1394
|
steps.push(step);
|
|
@@ -1399,7 +1443,8 @@ var WorkflowAgent = class {
|
|
|
1399
1443
|
effectiveTools,
|
|
1400
1444
|
iterMessages,
|
|
1401
1445
|
toolsContext,
|
|
1402
|
-
currentStepNumber
|
|
1446
|
+
currentStepNumber,
|
|
1447
|
+
toolExecutionSandbox
|
|
1403
1448
|
)
|
|
1404
1449
|
)
|
|
1405
1450
|
);
|
|
@@ -1463,8 +1508,8 @@ var WorkflowAgent = class {
|
|
|
1463
1508
|
await mergedOnEnd({
|
|
1464
1509
|
steps,
|
|
1465
1510
|
messages: messages2,
|
|
1466
|
-
text: (
|
|
1467
|
-
finishReason: (
|
|
1511
|
+
text: (_E = lastStep == null ? void 0 : lastStep.text) != null ? _E : "",
|
|
1512
|
+
finishReason: (_F = lastStep == null ? void 0 : lastStep.finishReason) != null ? _F : "other",
|
|
1468
1513
|
usage: totalUsage,
|
|
1469
1514
|
totalUsage,
|
|
1470
1515
|
runtimeContext,
|
|
@@ -1476,7 +1521,7 @@ var WorkflowAgent = class {
|
|
|
1476
1521
|
const telemetrySteps = steps.map(normalizeStepForTelemetry2);
|
|
1477
1522
|
const lastStep = telemetrySteps[telemetrySteps.length - 1];
|
|
1478
1523
|
const totalUsage = aggregateUsage(steps);
|
|
1479
|
-
await ((
|
|
1524
|
+
await ((_G = telemetryDispatcher.onEnd) == null ? void 0 : _G.call(telemetryDispatcher, {
|
|
1480
1525
|
...lastStep,
|
|
1481
1526
|
steps: telemetrySteps,
|
|
1482
1527
|
usage: totalUsage,
|
|
@@ -1501,8 +1546,8 @@ var WorkflowAgent = class {
|
|
|
1501
1546
|
}
|
|
1502
1547
|
}
|
|
1503
1548
|
if (options.writable) {
|
|
1504
|
-
const sendFinish = (
|
|
1505
|
-
const preventClose = (
|
|
1549
|
+
const sendFinish = (_H = options.sendFinish) != null ? _H : true;
|
|
1550
|
+
const preventClose = (_I = options.preventClose) != null ? _I : false;
|
|
1506
1551
|
if (sendFinish || !preventClose) {
|
|
1507
1552
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1508
1553
|
}
|
|
@@ -1522,7 +1567,8 @@ var WorkflowAgent = class {
|
|
|
1522
1567
|
effectiveTools,
|
|
1523
1568
|
iterMessages,
|
|
1524
1569
|
toolsContext,
|
|
1525
|
-
currentStepNumber
|
|
1570
|
+
currentStepNumber,
|
|
1571
|
+
toolExecutionSandbox
|
|
1526
1572
|
)
|
|
1527
1573
|
)
|
|
1528
1574
|
);
|
|
@@ -1625,10 +1671,10 @@ var WorkflowAgent = class {
|
|
|
1625
1671
|
} else if (options.onError) {
|
|
1626
1672
|
await options.onError({ error });
|
|
1627
1673
|
}
|
|
1628
|
-
await ((
|
|
1674
|
+
await ((_J = telemetryDispatcher.onError) == null ? void 0 : _J.call(telemetryDispatcher, error));
|
|
1629
1675
|
}
|
|
1630
1676
|
const messages = finalMessages != null ? finalMessages : prompt.messages;
|
|
1631
|
-
const effectiveOutput = (
|
|
1677
|
+
const effectiveOutput = (_K = options.output) != null ? _K : this.output;
|
|
1632
1678
|
let experimentalOutput = void 0;
|
|
1633
1679
|
if (effectiveOutput && steps.length > 0) {
|
|
1634
1680
|
const lastStep = steps[steps.length - 1];
|
|
@@ -1656,8 +1702,8 @@ var WorkflowAgent = class {
|
|
|
1656
1702
|
await mergedOnEnd({
|
|
1657
1703
|
steps,
|
|
1658
1704
|
messages,
|
|
1659
|
-
text: (
|
|
1660
|
-
finishReason: (
|
|
1705
|
+
text: (_L = lastStep == null ? void 0 : lastStep.text) != null ? _L : "",
|
|
1706
|
+
finishReason: (_M = lastStep == null ? void 0 : lastStep.finishReason) != null ? _M : "other",
|
|
1661
1707
|
usage: totalUsage,
|
|
1662
1708
|
totalUsage,
|
|
1663
1709
|
runtimeContext,
|
|
@@ -1669,7 +1715,7 @@ var WorkflowAgent = class {
|
|
|
1669
1715
|
const telemetrySteps = steps.map(normalizeStepForTelemetry2);
|
|
1670
1716
|
const lastStep = telemetrySteps[telemetrySteps.length - 1];
|
|
1671
1717
|
const totalUsage = aggregateUsage(steps);
|
|
1672
|
-
await ((
|
|
1718
|
+
await ((_N = telemetryDispatcher.onEnd) == null ? void 0 : _N.call(telemetryDispatcher, {
|
|
1673
1719
|
...lastStep,
|
|
1674
1720
|
steps: telemetrySteps,
|
|
1675
1721
|
usage: totalUsage,
|
|
@@ -1678,8 +1724,8 @@ var WorkflowAgent = class {
|
|
|
1678
1724
|
}
|
|
1679
1725
|
if (encounteredError) {
|
|
1680
1726
|
if (options.writable) {
|
|
1681
|
-
const sendFinish = (
|
|
1682
|
-
const preventClose = (
|
|
1727
|
+
const sendFinish = (_O = options.sendFinish) != null ? _O : true;
|
|
1728
|
+
const preventClose = (_P = options.preventClose) != null ? _P : false;
|
|
1683
1729
|
if (sendFinish || !preventClose) {
|
|
1684
1730
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1685
1731
|
}
|
|
@@ -1687,8 +1733,8 @@ var WorkflowAgent = class {
|
|
|
1687
1733
|
throw encounteredError;
|
|
1688
1734
|
}
|
|
1689
1735
|
if (options.writable) {
|
|
1690
|
-
const sendFinish = (
|
|
1691
|
-
const preventClose = (
|
|
1736
|
+
const sendFinish = (_Q = options.sendFinish) != null ? _Q : true;
|
|
1737
|
+
const preventClose = (_R = options.preventClose) != null ? _R : false;
|
|
1692
1738
|
if (sendFinish || !preventClose) {
|
|
1693
1739
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1694
1740
|
}
|
|
@@ -1874,7 +1920,7 @@ function getToolCallbackMessages(messages) {
|
|
|
1874
1920
|
const withoutAssistantToolCall = ((_a = messages.at(-1)) == null ? void 0 : _a.role) === "assistant" ? messages.slice(0, -1) : messages;
|
|
1875
1921
|
return withoutAssistantToolCall;
|
|
1876
1922
|
}
|
|
1877
|
-
async function executeTool(toolCall, tools, messages, context, download) {
|
|
1923
|
+
async function executeTool(toolCall, tools, messages, context, download, sandbox) {
|
|
1878
1924
|
const tool2 = tools[toolCall.toolName];
|
|
1879
1925
|
if (!tool2) throw new Error(`Tool "${toolCall.toolName}" not found`);
|
|
1880
1926
|
if (typeof tool2.execute !== "function") {
|
|
@@ -1891,7 +1937,8 @@ async function executeTool(toolCall, tools, messages, context, download) {
|
|
|
1891
1937
|
// Pass the conversation messages to the tool so it has context about the conversation
|
|
1892
1938
|
messages,
|
|
1893
1939
|
// Pass per-tool context to the tool (resolved from `toolsContext`)
|
|
1894
|
-
context
|
|
1940
|
+
context,
|
|
1941
|
+
experimental_sandbox: sandbox
|
|
1895
1942
|
});
|
|
1896
1943
|
} catch (error) {
|
|
1897
1944
|
const errorMessage = getErrorMessage(error);
|