@ai-sdk/workflow 2.0.28 → 2.0.30
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 +24 -0
- package/dist/index.js +291 -64
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/add-tool-results-to-conversation.ts +108 -0
- package/src/do-stream-step.ts +18 -0
- package/src/stream-text-iterator.ts +157 -34
- package/src/workflow-agent.ts +105 -39
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @ai-sdk/workflow
|
|
2
2
|
|
|
3
|
+
## 2.0.30
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [615ac89]
|
|
8
|
+
- ai@7.0.99
|
|
9
|
+
|
|
10
|
+
## 2.0.29
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- 72d9e05: fix(workflow): preserve provider-executed tool results across terminal, deferred, and paused responses
|
|
15
|
+
- Updated dependencies [5ec21a6]
|
|
16
|
+
- Updated dependencies [db59d78]
|
|
17
|
+
- Updated dependencies [7469a3b]
|
|
18
|
+
- Updated dependencies [f87bf07]
|
|
19
|
+
- Updated dependencies [bc5cb7a]
|
|
20
|
+
- Updated dependencies [a5f449a]
|
|
21
|
+
- Updated dependencies [813bb36]
|
|
22
|
+
- Updated dependencies [c43e4b7]
|
|
23
|
+
- @ai-sdk/provider@4.0.14
|
|
24
|
+
- ai@7.0.98
|
|
25
|
+
- @ai-sdk/provider-utils@5.0.40
|
|
26
|
+
|
|
3
27
|
## 2.0.28
|
|
4
28
|
|
|
5
29
|
### Patch Changes
|
package/dist/index.js
CHANGED
|
@@ -23,6 +23,85 @@ import {
|
|
|
23
23
|
verifyToolApprovalSignature
|
|
24
24
|
} from "ai/internal";
|
|
25
25
|
|
|
26
|
+
// src/add-tool-results-to-conversation.ts
|
|
27
|
+
function addToolResultsToConversation({
|
|
28
|
+
messages,
|
|
29
|
+
toolResults,
|
|
30
|
+
providerExecutedToolCallIds,
|
|
31
|
+
providerExecutedToolResultPositions = []
|
|
32
|
+
}) {
|
|
33
|
+
var _a;
|
|
34
|
+
const providerResultIds = /* @__PURE__ */ new Set([
|
|
35
|
+
...providerExecutedToolCallIds,
|
|
36
|
+
...providerExecutedToolResultPositions.map((position) => position.toolCallId)
|
|
37
|
+
]);
|
|
38
|
+
const providerResults = /* @__PURE__ */ new Map();
|
|
39
|
+
const clientResults = [];
|
|
40
|
+
let assistantMessageIndex = -1;
|
|
41
|
+
let assistantMessage;
|
|
42
|
+
for (let index = messages.length - 1; index >= 0; index--) {
|
|
43
|
+
const message = messages[index];
|
|
44
|
+
if (message.role === "assistant") {
|
|
45
|
+
assistantMessageIndex = index;
|
|
46
|
+
assistantMessage = message;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
for (const toolResult of toolResults) {
|
|
51
|
+
if (providerResultIds.has(toolResult.toolCallId)) {
|
|
52
|
+
const results = (_a = providerResults.get(toolResult.toolCallId)) != null ? _a : [];
|
|
53
|
+
results.push(toolResult);
|
|
54
|
+
providerResults.set(toolResult.toolCallId, results);
|
|
55
|
+
} else {
|
|
56
|
+
clientResults.push(toolResult);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (providerResults.size > 0) {
|
|
60
|
+
if (assistantMessage != null) {
|
|
61
|
+
let content = [...assistantMessage.content];
|
|
62
|
+
for (const position of [...providerExecutedToolResultPositions].sort(
|
|
63
|
+
(a, b) => a.contentIndex - b.contentIndex
|
|
64
|
+
)) {
|
|
65
|
+
const results = providerResults.get(position.toolCallId);
|
|
66
|
+
if (results == null || results.length === 0) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const [result, ...remainingResults] = results;
|
|
70
|
+
content.splice(position.contentIndex, 0, result);
|
|
71
|
+
if (remainingResults.length === 0) {
|
|
72
|
+
providerResults.delete(position.toolCallId);
|
|
73
|
+
} else {
|
|
74
|
+
providerResults.set(position.toolCallId, remainingResults);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const contentWithFallbackResults = [];
|
|
78
|
+
for (const part of content) {
|
|
79
|
+
contentWithFallbackResults.push(part);
|
|
80
|
+
if (part.type !== "tool-call") {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const results = providerResults.get(part.toolCallId);
|
|
84
|
+
if (results == null) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
providerResults.delete(part.toolCallId);
|
|
88
|
+
contentWithFallbackResults.push(...results);
|
|
89
|
+
}
|
|
90
|
+
for (const results of providerResults.values()) {
|
|
91
|
+
contentWithFallbackResults.push(...results);
|
|
92
|
+
}
|
|
93
|
+
assistantMessage.content = contentWithFallbackResults;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (clientResults.length > 0) {
|
|
97
|
+
messages.push({
|
|
98
|
+
role: "tool",
|
|
99
|
+
content: clientResults
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return assistantMessageIndex < 0 ? [] : messages.slice(assistantMessageIndex);
|
|
103
|
+
}
|
|
104
|
+
|
|
26
105
|
// src/create-language-model-tool-result-output.ts
|
|
27
106
|
import {
|
|
28
107
|
createDefaultDownloadFunction,
|
|
@@ -420,7 +499,13 @@ async function doStreamStep(conversationPrompt, modelInit, writable, serializedT
|
|
|
420
499
|
toolCallId: part.toolCallId,
|
|
421
500
|
toolName: part.toolName,
|
|
422
501
|
result: part.output,
|
|
423
|
-
isError: false
|
|
502
|
+
isError: false,
|
|
503
|
+
dynamic: part.dynamic,
|
|
504
|
+
providerMetadata: part.providerMetadata
|
|
505
|
+
});
|
|
506
|
+
content.push({
|
|
507
|
+
type: "provider-tool-result",
|
|
508
|
+
toolCallId: part.toolCallId
|
|
424
509
|
});
|
|
425
510
|
}
|
|
426
511
|
break;
|
|
@@ -431,7 +516,13 @@ async function doStreamStep(conversationPrompt, modelInit, writable, serializedT
|
|
|
431
516
|
toolCallId: errorPart.toolCallId,
|
|
432
517
|
toolName: errorPart.toolName,
|
|
433
518
|
result: errorPart.error,
|
|
434
|
-
isError: true
|
|
519
|
+
isError: true,
|
|
520
|
+
dynamic: errorPart.dynamic,
|
|
521
|
+
providerMetadata: errorPart.providerMetadata
|
|
522
|
+
});
|
|
523
|
+
content.push({
|
|
524
|
+
type: "provider-tool-result",
|
|
525
|
+
toolCallId: errorPart.toolCallId
|
|
435
526
|
});
|
|
436
527
|
}
|
|
437
528
|
break;
|
|
@@ -570,7 +661,7 @@ async function* streamTextIterator({
|
|
|
570
661
|
responseFormat,
|
|
571
662
|
experimental_sandbox: sandbox
|
|
572
663
|
}) {
|
|
573
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
664
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
574
665
|
let conversationPrompt = [...prompt];
|
|
575
666
|
let currentModel = model;
|
|
576
667
|
let currentGenerationSettings = generationSettings != null ? generationSettings : {};
|
|
@@ -583,7 +674,8 @@ async function* streamTextIterator({
|
|
|
583
674
|
let _isFirstIteration = true;
|
|
584
675
|
let stepNumber = 0;
|
|
585
676
|
let lastStep;
|
|
586
|
-
let
|
|
677
|
+
let lastStepWasYielded = false;
|
|
678
|
+
const pendingDeferredToolCallIds = /* @__PURE__ */ new Set();
|
|
587
679
|
let wasAborted = false;
|
|
588
680
|
let terminalError;
|
|
589
681
|
let hasTerminalError = false;
|
|
@@ -741,11 +833,17 @@ async function* streamTextIterator({
|
|
|
741
833
|
toolsContext: currentToolsContext,
|
|
742
834
|
experimental_sandbox: stepSandbox
|
|
743
835
|
});
|
|
744
|
-
const step = buildStepResult(
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
836
|
+
const step = buildStepResult(
|
|
837
|
+
raw,
|
|
838
|
+
toolCalls,
|
|
839
|
+
finish,
|
|
840
|
+
providerExecutedToolResults,
|
|
841
|
+
{
|
|
842
|
+
stepNumber,
|
|
843
|
+
runtimeContext: currentRuntimeContext,
|
|
844
|
+
toolsContext: currentToolsContext
|
|
845
|
+
}
|
|
846
|
+
);
|
|
749
847
|
await ((_j = telemetryDispatcher.onLanguageModelCallEnd) == null ? void 0 : _j.call(telemetryDispatcher, {
|
|
750
848
|
callId: step.callId,
|
|
751
849
|
provider: (_g = (_f = step.model) == null ? void 0 : _f.provider) != null ? _g : "unknown",
|
|
@@ -760,13 +858,26 @@ async function* streamTextIterator({
|
|
|
760
858
|
stepNumber++;
|
|
761
859
|
steps.push(step);
|
|
762
860
|
lastStep = step;
|
|
763
|
-
|
|
861
|
+
lastStepWasYielded = false;
|
|
764
862
|
const finishReason = finish == null ? void 0 : finish.finishReason;
|
|
863
|
+
const isToolExecutionAllowed = finishReason === "tool-calls" || finishReason === "stop";
|
|
864
|
+
for (const toolCall of toolCalls) {
|
|
865
|
+
if (toolCall.providerExecuted && ((_k = serializedTools[toolCall.toolName]) == null ? void 0 : _k.supportsDeferredResults) && !providerExecutedToolResults.has(toolCall.toolCallId)) {
|
|
866
|
+
pendingDeferredToolCallIds.add(toolCall.toolCallId);
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
for (const toolCallId of providerExecutedToolResults.keys()) {
|
|
870
|
+
pendingDeferredToolCallIds.delete(toolCallId);
|
|
871
|
+
}
|
|
872
|
+
const shouldProcessTools = isToolExecutionAllowed && (toolCalls.length > 0 || providerExecutedToolResults.size > 0);
|
|
765
873
|
if (hasTerminalError) {
|
|
766
874
|
done = true;
|
|
767
|
-
} else if (
|
|
768
|
-
|
|
769
|
-
const
|
|
875
|
+
} else if (shouldProcessTools) {
|
|
876
|
+
lastStepWasYielded = true;
|
|
877
|
+
const {
|
|
878
|
+
content: assistantContent,
|
|
879
|
+
providerExecutedToolResultPositions
|
|
880
|
+
} = getAssistantMessageContent(step);
|
|
770
881
|
const includedToolCallIds = new Set(
|
|
771
882
|
assistantContent.flatMap(
|
|
772
883
|
(part) => part.type === "tool-call" ? [part.toolCallId] : []
|
|
@@ -786,25 +897,42 @@ async function* streamTextIterator({
|
|
|
786
897
|
runtimeContext: currentRuntimeContext,
|
|
787
898
|
toolsContext: currentToolsContext,
|
|
788
899
|
experimental_sandbox: stepSandbox,
|
|
789
|
-
providerExecutedToolResults
|
|
900
|
+
providerExecutedToolResults,
|
|
901
|
+
providerExecutedToolResultPositions
|
|
790
902
|
};
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
903
|
+
const responseMessages = addToolResultsToConversation({
|
|
904
|
+
messages: conversationPrompt,
|
|
905
|
+
toolResults,
|
|
906
|
+
providerExecutedToolCallIds: /* @__PURE__ */ new Set([
|
|
907
|
+
...toolCalls.flatMap(
|
|
908
|
+
(toolCall) => toolCall.providerExecuted ? [toolCall.toolCallId] : []
|
|
909
|
+
),
|
|
910
|
+
...providerExecutedToolResults.keys()
|
|
911
|
+
]),
|
|
912
|
+
providerExecutedToolResultPositions
|
|
794
913
|
});
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
const
|
|
914
|
+
step.response.messages.push(
|
|
915
|
+
...responseMessages
|
|
916
|
+
);
|
|
917
|
+
const stopConditionList = stopConditions == null ? [] : Array.isArray(stopConditions) ? stopConditions : [stopConditions];
|
|
918
|
+
const stopConditionMet = stopConditionList.some(
|
|
919
|
+
(test) => test({ steps })
|
|
920
|
+
);
|
|
921
|
+
const hasClientToolCalls = toolCalls.some(
|
|
922
|
+
(toolCall) => !toolCall.providerExecuted
|
|
923
|
+
);
|
|
924
|
+
done = stopConditionMet || !hasClientToolCalls && pendingDeferredToolCallIds.size === 0;
|
|
925
|
+
} else if (finishReason === "stop" || finishReason === "tool-calls") {
|
|
926
|
+
const { content: assistantContent } = getAssistantMessageContent(step);
|
|
803
927
|
if (assistantContent.length > 0) {
|
|
804
|
-
|
|
928
|
+
const assistantMessage = {
|
|
805
929
|
role: "assistant",
|
|
806
930
|
content: assistantContent
|
|
807
|
-
}
|
|
931
|
+
};
|
|
932
|
+
conversationPrompt.push(assistantMessage);
|
|
933
|
+
step.response.messages.push(
|
|
934
|
+
assistantMessage
|
|
935
|
+
);
|
|
808
936
|
}
|
|
809
937
|
done = true;
|
|
810
938
|
} else if (finishReason === "length") {
|
|
@@ -828,7 +956,7 @@ async function* streamTextIterator({
|
|
|
828
956
|
if (resolvedOnStepEnd) {
|
|
829
957
|
await resolvedOnStepEnd(step);
|
|
830
958
|
}
|
|
831
|
-
await ((
|
|
959
|
+
await ((_l = telemetryDispatcher.onStepEnd) == null ? void 0 : _l.call(telemetryDispatcher, normalizeStepForTelemetry(step)));
|
|
832
960
|
} catch (error) {
|
|
833
961
|
if (onError) {
|
|
834
962
|
await onError({ error });
|
|
@@ -836,7 +964,7 @@ async function* streamTextIterator({
|
|
|
836
964
|
throw error;
|
|
837
965
|
}
|
|
838
966
|
}
|
|
839
|
-
if (lastStep && !
|
|
967
|
+
if (lastStep && !lastStepWasYielded) {
|
|
840
968
|
yield {
|
|
841
969
|
toolCalls: [],
|
|
842
970
|
messages: conversationPrompt,
|
|
@@ -933,7 +1061,7 @@ function normalizeStepForTelemetry(step) {
|
|
|
933
1061
|
model: (_a = step.model) != null ? _a : { provider: "unknown", modelId: "unknown" }
|
|
934
1062
|
};
|
|
935
1063
|
}
|
|
936
|
-
function buildStepResult(raw, toolCalls, finish, opts) {
|
|
1064
|
+
function buildStepResult(raw, toolCalls, finish, providerExecutedToolResults, opts) {
|
|
937
1065
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
938
1066
|
const {
|
|
939
1067
|
content: rawContent,
|
|
@@ -1003,8 +1131,41 @@ function buildStepResult(raw, toolCalls, finish, opts) {
|
|
|
1003
1131
|
}
|
|
1004
1132
|
break;
|
|
1005
1133
|
}
|
|
1134
|
+
case "provider-tool-result": {
|
|
1135
|
+
const result = providerExecutedToolResults.get(part.toolCallId);
|
|
1136
|
+
if (result == null) {
|
|
1137
|
+
break;
|
|
1138
|
+
}
|
|
1139
|
+
const toolCall = toolCalls.find(
|
|
1140
|
+
(toolCall2) => toolCall2.toolCallId === result.toolCallId
|
|
1141
|
+
);
|
|
1142
|
+
const common = {
|
|
1143
|
+
toolCallId: result.toolCallId,
|
|
1144
|
+
toolName: result.toolName,
|
|
1145
|
+
input: toolCall == null ? void 0 : toolCall.input,
|
|
1146
|
+
providerExecuted: true,
|
|
1147
|
+
...result.dynamic === true || (toolCall == null ? void 0 : toolCall.dynamic) === true ? { dynamic: true } : {},
|
|
1148
|
+
...result.providerMetadata != null ? { providerMetadata: result.providerMetadata } : {},
|
|
1149
|
+
...(toolCall == null ? void 0 : toolCall.toolMetadata) != null ? { toolMetadata: toolCall.toolMetadata } : {}
|
|
1150
|
+
};
|
|
1151
|
+
content.push(
|
|
1152
|
+
result.isError ? {
|
|
1153
|
+
type: "tool-error",
|
|
1154
|
+
...common,
|
|
1155
|
+
error: result.result
|
|
1156
|
+
} : {
|
|
1157
|
+
type: "tool-result",
|
|
1158
|
+
...common,
|
|
1159
|
+
output: result.result
|
|
1160
|
+
}
|
|
1161
|
+
);
|
|
1162
|
+
break;
|
|
1163
|
+
}
|
|
1006
1164
|
}
|
|
1007
1165
|
}
|
|
1166
|
+
const toolResults = content.filter(
|
|
1167
|
+
(part) => part.type === "tool-result"
|
|
1168
|
+
);
|
|
1008
1169
|
return {
|
|
1009
1170
|
callId: "workflow-agent",
|
|
1010
1171
|
stepNumber: opts.stepNumber,
|
|
@@ -1028,9 +1189,9 @@ function buildStepResult(raw, toolCalls, finish, opts) {
|
|
|
1028
1189
|
toolCalls: validToolCalls,
|
|
1029
1190
|
staticToolCalls: validToolCalls.filter((tc) => tc.dynamic !== true),
|
|
1030
1191
|
dynamicToolCalls: validToolCalls.filter((tc) => tc.dynamic),
|
|
1031
|
-
toolResults
|
|
1032
|
-
staticToolResults:
|
|
1033
|
-
dynamicToolResults:
|
|
1192
|
+
toolResults,
|
|
1193
|
+
staticToolResults: toolResults.filter((result) => result.dynamic !== true),
|
|
1194
|
+
dynamicToolResults: toolResults.filter((result) => result.dynamic === true),
|
|
1034
1195
|
finishReason: (_f = finish == null ? void 0 : finish.finishReason) != null ? _f : "other",
|
|
1035
1196
|
rawFinishReason: finish == null ? void 0 : finish.rawFinishReason,
|
|
1036
1197
|
usage: (_g = finish == null ? void 0 : finish.usage) != null ? _g : {
|
|
@@ -1074,11 +1235,14 @@ function buildStepResult(raw, toolCalls, finish, opts) {
|
|
|
1074
1235
|
}
|
|
1075
1236
|
function getAssistantMessageContent(step) {
|
|
1076
1237
|
const content = [];
|
|
1238
|
+
const providerExecutedToolResultPositions = [];
|
|
1239
|
+
let contentIndex = 0;
|
|
1077
1240
|
for (const part of step.content) {
|
|
1078
1241
|
switch (part.type) {
|
|
1079
1242
|
case "text":
|
|
1080
1243
|
if (part.text.length > 0) {
|
|
1081
1244
|
content.push({ type: "text", text: part.text });
|
|
1245
|
+
contentIndex++;
|
|
1082
1246
|
}
|
|
1083
1247
|
break;
|
|
1084
1248
|
case "file":
|
|
@@ -1090,13 +1254,25 @@ function getAssistantMessageContent(step) {
|
|
|
1090
1254
|
providerOptions: part.providerMetadata
|
|
1091
1255
|
} : {}
|
|
1092
1256
|
});
|
|
1257
|
+
contentIndex++;
|
|
1093
1258
|
break;
|
|
1094
1259
|
case "tool-call":
|
|
1095
1260
|
content.push(toAssistantToolCallContent(part));
|
|
1261
|
+
contentIndex++;
|
|
1262
|
+
break;
|
|
1263
|
+
case "tool-result":
|
|
1264
|
+
case "tool-error":
|
|
1265
|
+
if (part.providerExecuted) {
|
|
1266
|
+
providerExecutedToolResultPositions.push({
|
|
1267
|
+
toolCallId: part.toolCallId,
|
|
1268
|
+
contentIndex
|
|
1269
|
+
});
|
|
1270
|
+
contentIndex++;
|
|
1271
|
+
}
|
|
1096
1272
|
break;
|
|
1097
1273
|
}
|
|
1098
1274
|
}
|
|
1099
|
-
return content;
|
|
1275
|
+
return { content, providerExecutedToolResultPositions };
|
|
1100
1276
|
}
|
|
1101
1277
|
function toAssistantToolCallContent(toolCall) {
|
|
1102
1278
|
const sanitizedMetadata = sanitizeProviderMetadataForToolCall(
|
|
@@ -1139,10 +1315,18 @@ function addToolResultsToStep(step, executedResults) {
|
|
|
1139
1315
|
if (step == null || executedResults.length === 0) {
|
|
1140
1316
|
return;
|
|
1141
1317
|
}
|
|
1142
|
-
const
|
|
1318
|
+
const existingProviderResultIds = new Set(
|
|
1319
|
+
step.content.flatMap(
|
|
1320
|
+
(part) => (part.type === "tool-result" || part.type === "tool-error") && part.providerExecuted ? [part.toolCallId] : []
|
|
1321
|
+
)
|
|
1322
|
+
);
|
|
1323
|
+
const toolOutputs = executedResults.flatMap((result) => {
|
|
1143
1324
|
const toolCall = step.toolCalls.find(
|
|
1144
1325
|
(toolCall2) => toolCall2.toolCallId === result.modelResult.toolCallId
|
|
1145
1326
|
);
|
|
1327
|
+
if (existingProviderResultIds.has(result.modelResult.toolCallId)) {
|
|
1328
|
+
return [];
|
|
1329
|
+
}
|
|
1146
1330
|
const common = {
|
|
1147
1331
|
toolCallId: result.modelResult.toolCallId,
|
|
1148
1332
|
toolName: result.modelResult.toolName,
|
|
@@ -1150,15 +1334,17 @@ function addToolResultsToStep(step, executedResults) {
|
|
|
1150
1334
|
...(toolCall == null ? void 0 : toolCall.dynamic) === true ? { dynamic: true } : {},
|
|
1151
1335
|
...(toolCall == null ? void 0 : toolCall.providerExecuted) === true ? { providerExecuted: true } : {}
|
|
1152
1336
|
};
|
|
1153
|
-
return
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1337
|
+
return [
|
|
1338
|
+
result.isError ? {
|
|
1339
|
+
type: "tool-error",
|
|
1340
|
+
...common,
|
|
1341
|
+
error: result.rawOutput
|
|
1342
|
+
} : {
|
|
1343
|
+
type: "tool-result",
|
|
1344
|
+
...common,
|
|
1345
|
+
output: result.rawOutput
|
|
1346
|
+
}
|
|
1347
|
+
];
|
|
1162
1348
|
});
|
|
1163
1349
|
step.content.push(...toolOutputs);
|
|
1164
1350
|
const toolResults = toolOutputs.filter(
|
|
@@ -1817,8 +2003,10 @@ var WorkflowAgent = class {
|
|
|
1817
2003
|
runtimeContext: yieldedRuntimeContext,
|
|
1818
2004
|
toolsContext: yieldedToolsContext,
|
|
1819
2005
|
experimental_sandbox: stepSandbox,
|
|
1820
|
-
providerExecutedToolResults
|
|
2006
|
+
providerExecutedToolResults,
|
|
2007
|
+
providerExecutedToolResultPositions
|
|
1821
2008
|
} = result.value;
|
|
2009
|
+
const capturedProviderToolResults = providerExecutedToolResults != null ? providerExecutedToolResults : /* @__PURE__ */ new Map();
|
|
1822
2010
|
const toolExecutionSandbox = stepSandbox != null ? stepSandbox : sandbox;
|
|
1823
2011
|
const currentStepNumber = steps.length;
|
|
1824
2012
|
if (step) {
|
|
@@ -1830,7 +2018,7 @@ var WorkflowAgent = class {
|
|
|
1830
2018
|
if (yieldedToolsContext !== void 0) {
|
|
1831
2019
|
toolsContext = yieldedToolsContext;
|
|
1832
2020
|
}
|
|
1833
|
-
if (toolCalls.length > 0) {
|
|
2021
|
+
if (toolCalls.length > 0 || capturedProviderToolResults.size > 0) {
|
|
1834
2022
|
const invalidToolCalls = toolCalls.filter((tc) => tc.invalid === true);
|
|
1835
2023
|
const validToolCalls = toolCalls.filter((tc) => tc.invalid !== true);
|
|
1836
2024
|
const nonProviderToolCalls = validToolCalls.filter(
|
|
@@ -1839,6 +2027,28 @@ var WorkflowAgent = class {
|
|
|
1839
2027
|
const providerToolCalls = validToolCalls.filter(
|
|
1840
2028
|
(tc) => tc.providerExecuted
|
|
1841
2029
|
);
|
|
2030
|
+
const providerToolCallsForResults = [
|
|
2031
|
+
...providerToolCalls,
|
|
2032
|
+
...[...capturedProviderToolResults.values()].flatMap(
|
|
2033
|
+
(providerResult) => {
|
|
2034
|
+
var _a2;
|
|
2035
|
+
return providerToolCalls.some(
|
|
2036
|
+
(toolCall) => toolCall.toolCallId === providerResult.toolCallId
|
|
2037
|
+
) ? [] : [
|
|
2038
|
+
{
|
|
2039
|
+
type: "tool-call",
|
|
2040
|
+
toolCallId: providerResult.toolCallId,
|
|
2041
|
+
toolName: providerResult.toolName,
|
|
2042
|
+
input: (_a2 = toolCalls.find(
|
|
2043
|
+
(toolCall) => toolCall.toolCallId === providerResult.toolCallId
|
|
2044
|
+
)) == null ? void 0 : _a2.input,
|
|
2045
|
+
providerExecuted: true,
|
|
2046
|
+
dynamic: providerResult.dynamic
|
|
2047
|
+
}
|
|
2048
|
+
];
|
|
2049
|
+
}
|
|
2050
|
+
)
|
|
2051
|
+
];
|
|
1842
2052
|
const approvalNeeded = await Promise.all(
|
|
1843
2053
|
nonProviderToolCalls.map(async (tc) => {
|
|
1844
2054
|
const tool2 = effectiveTools[tc.toolName];
|
|
@@ -1880,11 +2090,11 @@ var WorkflowAgent = class {
|
|
|
1880
2090
|
)
|
|
1881
2091
|
);
|
|
1882
2092
|
const providerResultEntries = await Promise.all(
|
|
1883
|
-
|
|
2093
|
+
providerToolCallsForResults.map(async (toolCall) => ({
|
|
1884
2094
|
toolCall,
|
|
1885
2095
|
result: await resolveProviderToolResult(
|
|
1886
2096
|
toolCall,
|
|
1887
|
-
|
|
2097
|
+
capturedProviderToolResults,
|
|
1888
2098
|
effectiveTools,
|
|
1889
2099
|
download
|
|
1890
2100
|
)
|
|
@@ -1905,9 +2115,7 @@ var WorkflowAgent = class {
|
|
|
1905
2115
|
const providerResults = providerResultEntries.flatMap(
|
|
1906
2116
|
({ result: result2 }) => result2 == null ? [] : [result2]
|
|
1907
2117
|
);
|
|
1908
|
-
const continuationInvalidResults = invalidToolCalls.map(
|
|
1909
|
-
createInvalidToolResult
|
|
1910
|
-
);
|
|
2118
|
+
const continuationInvalidResults = invalidToolCalls.filter((toolCall) => !toolCall.providerExecuted).map(createInvalidToolResult);
|
|
1911
2119
|
const resolvedResults = [
|
|
1912
2120
|
...executableResults.map((result2) => result2.modelResult),
|
|
1913
2121
|
...providerResults.map((result2) => result2.modelResult),
|
|
@@ -1933,12 +2141,19 @@ var WorkflowAgent = class {
|
|
|
1933
2141
|
};
|
|
1934
2142
|
});
|
|
1935
2143
|
addToolResultsToStep(step, executedResults);
|
|
1936
|
-
|
|
1937
|
-
iterMessages
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
2144
|
+
const responseMessages = addToolResultsToConversation({
|
|
2145
|
+
messages: iterMessages,
|
|
2146
|
+
toolResults: resolvedResults,
|
|
2147
|
+
providerExecutedToolCallIds: new Set(
|
|
2148
|
+
providerToolCallsForResults.map(
|
|
2149
|
+
(toolCall) => toolCall.toolCallId
|
|
2150
|
+
)
|
|
2151
|
+
),
|
|
2152
|
+
providerExecutedToolResultPositions
|
|
2153
|
+
});
|
|
2154
|
+
step == null ? void 0 : step.response.messages.push(
|
|
2155
|
+
...responseMessages
|
|
2156
|
+
);
|
|
1942
2157
|
const messages2 = iterMessages;
|
|
1943
2158
|
const lastStep2 = steps[steps.length - 1];
|
|
1944
2159
|
const totalUsage2 = aggregateUsage(steps);
|
|
@@ -2041,11 +2256,11 @@ var WorkflowAgent = class {
|
|
|
2041
2256
|
)
|
|
2042
2257
|
);
|
|
2043
2258
|
const providerToolResultEntries = await Promise.all(
|
|
2044
|
-
|
|
2259
|
+
providerToolCallsForResults.map(async (toolCall) => ({
|
|
2045
2260
|
toolCall,
|
|
2046
2261
|
result: await resolveProviderToolResult(
|
|
2047
2262
|
toolCall,
|
|
2048
|
-
|
|
2263
|
+
capturedProviderToolResults,
|
|
2049
2264
|
effectiveTools,
|
|
2050
2265
|
download
|
|
2051
2266
|
)
|
|
@@ -2066,9 +2281,7 @@ var WorkflowAgent = class {
|
|
|
2066
2281
|
const providerToolResults = providerToolResultEntries.flatMap(
|
|
2067
2282
|
({ result: result2 }) => result2 == null ? [] : [result2]
|
|
2068
2283
|
);
|
|
2069
|
-
const continuationInvalidToolResults = invalidToolCalls.map(
|
|
2070
|
-
createInvalidToolResult
|
|
2071
|
-
);
|
|
2284
|
+
const continuationInvalidToolResults = invalidToolCalls.filter((toolCall) => !toolCall.providerExecuted).map(createInvalidToolResult);
|
|
2072
2285
|
const executedToolResults = toolCalls.flatMap((tc) => {
|
|
2073
2286
|
const clientResult = clientToolResults.find(
|
|
2074
2287
|
(r) => r.modelResult.toolCallId === tc.toolCallId
|
|
@@ -2080,6 +2293,14 @@ var WorkflowAgent = class {
|
|
|
2080
2293
|
if (providerResult) return [providerResult];
|
|
2081
2294
|
return [];
|
|
2082
2295
|
});
|
|
2296
|
+
const currentToolCallIds = new Set(
|
|
2297
|
+
toolCalls.map((toolCall) => toolCall.toolCallId)
|
|
2298
|
+
);
|
|
2299
|
+
executedToolResults.push(
|
|
2300
|
+
...providerToolResults.filter(
|
|
2301
|
+
(result2) => !currentToolCallIds.has(result2.modelResult.toolCallId)
|
|
2302
|
+
)
|
|
2303
|
+
);
|
|
2083
2304
|
const continuationToolResults = toolCalls.flatMap((tc) => {
|
|
2084
2305
|
const invalidResult = continuationInvalidToolResults.find(
|
|
2085
2306
|
(r) => r.toolCallId === tc.toolCallId
|
|
@@ -2091,6 +2312,11 @@ var WorkflowAgent = class {
|
|
|
2091
2312
|
if (executedResult) return [executedResult.modelResult];
|
|
2092
2313
|
return [];
|
|
2093
2314
|
});
|
|
2315
|
+
continuationToolResults.push(
|
|
2316
|
+
...providerToolResults.flatMap(
|
|
2317
|
+
(result2) => currentToolCallIds.has(result2.modelResult.toolCallId) ? [] : [result2.modelResult]
|
|
2318
|
+
)
|
|
2319
|
+
);
|
|
2094
2320
|
if (options.writable) {
|
|
2095
2321
|
await writeToolResults(
|
|
2096
2322
|
options.writable,
|
|
@@ -2461,7 +2687,7 @@ async function resolveProviderToolResult(toolCall, providerExecutedToolResults,
|
|
|
2461
2687
|
};
|
|
2462
2688
|
}
|
|
2463
2689
|
const result = streamResult.result;
|
|
2464
|
-
const errorMode = streamResult.isError ?
|
|
2690
|
+
const errorMode = streamResult.isError ? "json" : "none";
|
|
2465
2691
|
return {
|
|
2466
2692
|
modelResult: {
|
|
2467
2693
|
type: "tool-result",
|
|
@@ -2476,7 +2702,8 @@ async function resolveProviderToolResult(toolCall, providerExecutedToolResults,
|
|
|
2476
2702
|
errorMode,
|
|
2477
2703
|
supportedUrls: {},
|
|
2478
2704
|
download
|
|
2479
|
-
})
|
|
2705
|
+
}),
|
|
2706
|
+
...streamResult.providerMetadata != null ? { providerOptions: streamResult.providerMetadata } : {}
|
|
2480
2707
|
},
|
|
2481
2708
|
rawOutput: result,
|
|
2482
2709
|
isError: streamResult.isError === true
|