@ai-sdk/workflow 1.0.0-beta.8 → 1.0.0-canary.32
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 +233 -0
- package/dist/index.d.mts +36 -26
- package/dist/index.mjs +120 -94
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -6
- package/src/do-stream-step.ts +7 -6
- package/src/index.ts +3 -3
- package/src/serializable-schema.ts +4 -0
- package/src/stream-text-iterator.ts +17 -37
- package/src/telemetry.ts +5 -5
- package/src/test/agent-e2e-workflows.ts +9 -9
- package/src/to-ui-message-chunk.ts +5 -2
- package/src/workflow-agent.ts +132 -89
- package/src/workflow-chat-transport.ts +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/workflow-agent.ts
|
|
2
2
|
import {
|
|
3
|
-
Output
|
|
3
|
+
Output,
|
|
4
|
+
experimental_filterActiveTools as filterActiveTools2
|
|
4
5
|
} from "ai";
|
|
5
6
|
import {
|
|
6
7
|
convertToLanguageModelPrompt,
|
|
@@ -9,11 +10,16 @@ import {
|
|
|
9
10
|
standardizePrompt
|
|
10
11
|
} from "ai/internal";
|
|
11
12
|
|
|
13
|
+
// src/stream-text-iterator.ts
|
|
14
|
+
import {
|
|
15
|
+
experimental_filterActiveTools as filterActiveTools
|
|
16
|
+
} from "ai";
|
|
17
|
+
|
|
12
18
|
// src/do-stream-step.ts
|
|
13
19
|
import {
|
|
14
|
-
experimental_streamLanguageModelCall as streamModelCall
|
|
20
|
+
experimental_streamLanguageModelCall as streamModelCall,
|
|
21
|
+
gateway
|
|
15
22
|
} from "ai";
|
|
16
|
-
import { gateway } from "ai";
|
|
17
23
|
|
|
18
24
|
// src/serializable-schema.ts
|
|
19
25
|
import { asSchema, jsonSchema } from "@ai-sdk/provider-utils";
|
|
@@ -22,12 +28,14 @@ import Ajv from "ajv";
|
|
|
22
28
|
function serializeToolSet(tools) {
|
|
23
29
|
return Object.fromEntries(
|
|
24
30
|
Object.entries(tools).map(([name, t]) => {
|
|
31
|
+
var _a;
|
|
25
32
|
const def = {
|
|
26
33
|
description: t.description,
|
|
27
34
|
inputSchema: asSchema(t.inputSchema).jsonSchema
|
|
28
35
|
};
|
|
29
36
|
if (t.type === "provider") {
|
|
30
37
|
def.type = "provider";
|
|
38
|
+
def.isProviderExecuted = (_a = t.isProviderExecuted) != null ? _a : false;
|
|
31
39
|
def.id = t.id;
|
|
32
40
|
def.args = t.args;
|
|
33
41
|
}
|
|
@@ -39,7 +47,7 @@ function resolveSerializableTools(tools) {
|
|
|
39
47
|
const ajv = new Ajv();
|
|
40
48
|
return Object.fromEntries(
|
|
41
49
|
Object.entries(tools).map(([name, t]) => {
|
|
42
|
-
var _a;
|
|
50
|
+
var _a, _b;
|
|
43
51
|
if (t.type === "provider") {
|
|
44
52
|
return [
|
|
45
53
|
name,
|
|
@@ -47,6 +55,7 @@ function resolveSerializableTools(tools) {
|
|
|
47
55
|
type: "provider",
|
|
48
56
|
id: t.id,
|
|
49
57
|
args: (_a = t.args) != null ? _a : {},
|
|
58
|
+
isProviderExecuted: (_b = t.isProviderExecuted) != null ? _b : false,
|
|
50
59
|
inputSchema: jsonSchema(t.inputSchema)
|
|
51
60
|
})
|
|
52
61
|
];
|
|
@@ -85,6 +94,7 @@ async function doStreamStep(conversationPrompt, modelInit, writable, serializedT
|
|
|
85
94
|
// pre-converted LanguageModelV4Prompt. standardizePrompt inside
|
|
86
95
|
// streamModelCall handles both formats.
|
|
87
96
|
messages: conversationPrompt,
|
|
97
|
+
allowSystemInMessages: true,
|
|
88
98
|
tools,
|
|
89
99
|
toolChoice: options == null ? void 0 : options.toolChoice,
|
|
90
100
|
includeRawChunks: options == null ? void 0 : options.includeRawChunks,
|
|
@@ -187,7 +197,8 @@ async function doStreamStep(conversationPrompt, modelInit, writable, serializedT
|
|
|
187
197
|
},
|
|
188
198
|
functionId: void 0,
|
|
189
199
|
metadata: void 0,
|
|
190
|
-
|
|
200
|
+
runtimeContext: void 0,
|
|
201
|
+
toolsContext: {},
|
|
191
202
|
content: [
|
|
192
203
|
...text ? [{ type: "text", text }] : [],
|
|
193
204
|
...toolCalls.filter((tc) => !tc.invalid).map((tc) => ({
|
|
@@ -265,7 +276,6 @@ async function* streamTextIterator({
|
|
|
265
276
|
writable,
|
|
266
277
|
model,
|
|
267
278
|
stopConditions,
|
|
268
|
-
maxSteps,
|
|
269
279
|
onStepFinish,
|
|
270
280
|
onStepStart,
|
|
271
281
|
onError,
|
|
@@ -273,12 +283,12 @@ async function* streamTextIterator({
|
|
|
273
283
|
generationSettings,
|
|
274
284
|
toolChoice,
|
|
275
285
|
experimental_context,
|
|
276
|
-
|
|
286
|
+
telemetry,
|
|
277
287
|
includeRawChunks = false,
|
|
278
288
|
repairToolCall,
|
|
279
289
|
responseFormat
|
|
280
290
|
}) {
|
|
281
|
-
var _a;
|
|
291
|
+
var _a, _b;
|
|
282
292
|
let conversationPrompt = [...prompt];
|
|
283
293
|
let currentModel = model;
|
|
284
294
|
let currentGenerationSettings = generationSettings != null ? generationSettings : {};
|
|
@@ -291,11 +301,7 @@ async function* streamTextIterator({
|
|
|
291
301
|
let stepNumber = 0;
|
|
292
302
|
let lastStep;
|
|
293
303
|
let lastStepWasToolCalls = false;
|
|
294
|
-
const effectiveMaxSteps = maxSteps != null ? maxSteps : Infinity;
|
|
295
304
|
while (!done) {
|
|
296
|
-
if (stepNumber >= effectiveMaxSteps) {
|
|
297
|
-
break;
|
|
298
|
-
}
|
|
299
305
|
if ((_a = currentGenerationSettings.abortSignal) == null ? void 0 : _a.aborted) {
|
|
300
306
|
break;
|
|
301
307
|
}
|
|
@@ -411,7 +417,10 @@ async function* streamTextIterator({
|
|
|
411
417
|
});
|
|
412
418
|
}
|
|
413
419
|
try {
|
|
414
|
-
const effectiveTools = currentActiveTools && currentActiveTools.length > 0 ?
|
|
420
|
+
const effectiveTools = currentActiveTools && currentActiveTools.length > 0 ? (_b = filterActiveTools({
|
|
421
|
+
tools,
|
|
422
|
+
activeTools: currentActiveTools
|
|
423
|
+
})) != null ? _b : tools : tools;
|
|
415
424
|
const serializedTools = serializeToolSet(effectiveTools);
|
|
416
425
|
const { toolCalls, finish, step, providerExecutedToolResults } = await doStreamStep(
|
|
417
426
|
conversationPrompt,
|
|
@@ -422,7 +431,7 @@ async function* streamTextIterator({
|
|
|
422
431
|
...currentGenerationSettings,
|
|
423
432
|
toolChoice: currentToolChoice,
|
|
424
433
|
includeRawChunks,
|
|
425
|
-
|
|
434
|
+
telemetry,
|
|
426
435
|
repairToolCall,
|
|
427
436
|
responseFormat
|
|
428
437
|
}
|
|
@@ -515,15 +524,6 @@ async function* streamTextIterator({
|
|
|
515
524
|
}
|
|
516
525
|
return conversationPrompt;
|
|
517
526
|
}
|
|
518
|
-
function filterToolSet(tools, activeTools) {
|
|
519
|
-
const filtered = {};
|
|
520
|
-
for (const toolName of activeTools) {
|
|
521
|
-
if (toolName in tools) {
|
|
522
|
-
filtered[toolName] = tools[toolName];
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
return filtered;
|
|
526
|
-
}
|
|
527
527
|
function sanitizeProviderMetadataForToolCall(metadata) {
|
|
528
528
|
if (metadata == null) return void 0;
|
|
529
529
|
const meta = metadata;
|
|
@@ -548,13 +548,13 @@ function sanitizeProviderMetadataForToolCall(metadata) {
|
|
|
548
548
|
// src/workflow-agent.ts
|
|
549
549
|
var WorkflowAgent = class {
|
|
550
550
|
constructor(options) {
|
|
551
|
-
var _a, _b;
|
|
551
|
+
var _a, _b, _c;
|
|
552
552
|
this.id = options.id;
|
|
553
553
|
this.model = options.model;
|
|
554
554
|
this.tools = (_a = options.tools) != null ? _a : {};
|
|
555
555
|
this.instructions = (_b = options.instructions) != null ? _b : options.system;
|
|
556
556
|
this.toolChoice = options.toolChoice;
|
|
557
|
-
this.telemetry = options.experimental_telemetry;
|
|
557
|
+
this.telemetry = (_c = options.telemetry) != null ? _c : options.experimental_telemetry;
|
|
558
558
|
this.experimentalContext = options.experimental_context;
|
|
559
559
|
this.stopWhen = options.stopWhen;
|
|
560
560
|
this.activeTools = options.activeTools;
|
|
@@ -566,8 +566,8 @@ var WorkflowAgent = class {
|
|
|
566
566
|
this.constructorOnFinish = options.onFinish;
|
|
567
567
|
this.constructorOnStart = options.experimental_onStart;
|
|
568
568
|
this.constructorOnStepStart = options.experimental_onStepStart;
|
|
569
|
-
this.
|
|
570
|
-
this.
|
|
569
|
+
this.constructorOnToolExecutionStart = options.experimental_onToolExecutionStart;
|
|
570
|
+
this.constructorOnToolExecutionEnd = options.experimental_onToolExecutionEnd;
|
|
571
571
|
this.prepareCall = options.prepareCall;
|
|
572
572
|
this.generationSettings = {
|
|
573
573
|
maxOutputTokens: options.maxOutputTokens,
|
|
@@ -588,7 +588,7 @@ var WorkflowAgent = class {
|
|
|
588
588
|
throw new Error("Not implemented");
|
|
589
589
|
}
|
|
590
590
|
async stream(options) {
|
|
591
|
-
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;
|
|
591
|
+
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;
|
|
592
592
|
let effectiveModel = this.model;
|
|
593
593
|
let effectiveInstructions = (_a = options.system) != null ? _a : this.instructions;
|
|
594
594
|
let effectivePrompt = options.prompt;
|
|
@@ -596,14 +596,15 @@ var WorkflowAgent = class {
|
|
|
596
596
|
let effectiveGenerationSettings = { ...this.generationSettings };
|
|
597
597
|
let effectiveExperimentalContext = (_b = options.experimental_context) != null ? _b : this.experimentalContext;
|
|
598
598
|
let effectiveToolChoiceFromPrepare = (_c = options.toolChoice) != null ? _c : this.toolChoice;
|
|
599
|
-
let effectiveTelemetryFromPrepare = (_d = options.
|
|
600
|
-
const resolvedMessagesForPrepareCall = (
|
|
599
|
+
let effectiveTelemetryFromPrepare = (_e = (_d = options.telemetry) != null ? _d : options.experimental_telemetry) != null ? _e : this.telemetry;
|
|
600
|
+
const resolvedMessagesForPrepareCall = (_f = effectiveMessages != null ? effectiveMessages : typeof effectivePrompt === "string" ? [{ role: "user", content: effectivePrompt }] : effectivePrompt) != null ? _f : [];
|
|
601
601
|
if (this.prepareCall) {
|
|
602
602
|
const prepared = await this.prepareCall({
|
|
603
603
|
model: effectiveModel,
|
|
604
604
|
tools: this.tools,
|
|
605
605
|
instructions: effectiveInstructions,
|
|
606
606
|
toolChoice: effectiveToolChoiceFromPrepare,
|
|
607
|
+
telemetry: effectiveTelemetryFromPrepare,
|
|
607
608
|
experimental_telemetry: effectiveTelemetryFromPrepare,
|
|
608
609
|
experimental_context: effectiveExperimentalContext,
|
|
609
610
|
messages: resolvedMessagesForPrepareCall,
|
|
@@ -620,7 +621,9 @@ var WorkflowAgent = class {
|
|
|
620
621
|
effectiveExperimentalContext = prepared.experimental_context;
|
|
621
622
|
if (prepared.toolChoice !== void 0)
|
|
622
623
|
effectiveToolChoiceFromPrepare = prepared.toolChoice;
|
|
623
|
-
if (prepared.
|
|
624
|
+
if (prepared.telemetry !== void 0)
|
|
625
|
+
effectiveTelemetryFromPrepare = prepared.telemetry;
|
|
626
|
+
else if (prepared.experimental_telemetry !== void 0)
|
|
624
627
|
effectiveTelemetryFromPrepare = prepared.experimental_telemetry;
|
|
625
628
|
if (prepared.maxOutputTokens !== void 0)
|
|
626
629
|
effectiveGenerationSettings.maxOutputTokens = prepared.maxOutputTokens;
|
|
@@ -645,6 +648,8 @@ var WorkflowAgent = class {
|
|
|
645
648
|
}
|
|
646
649
|
const prompt = await standardizePrompt({
|
|
647
650
|
system: effectiveInstructions,
|
|
651
|
+
allowSystemInMessages: true,
|
|
652
|
+
// TODO: consider exposing this as a parameter
|
|
648
653
|
...effectivePrompt != null ? { prompt: effectivePrompt } : { messages: effectiveMessages }
|
|
649
654
|
});
|
|
650
655
|
const { approvedToolApprovals, deniedToolApprovals } = collectToolApprovalsFromMessages(prompt.messages);
|
|
@@ -741,11 +746,11 @@ var WorkflowAgent = class {
|
|
|
741
746
|
const modelPrompt = await convertToLanguageModelPrompt({
|
|
742
747
|
prompt,
|
|
743
748
|
supportedUrls: {},
|
|
744
|
-
download: (
|
|
749
|
+
download: (_g = options.experimental_download) != null ? _g : this.experimentalDownload
|
|
745
750
|
});
|
|
746
751
|
const effectiveAbortSignal = mergeAbortSignals(
|
|
747
|
-
(
|
|
748
|
-
options.timeout
|
|
752
|
+
(_h = options.abortSignal) != null ? _h : effectiveGenerationSettings.abortSignal,
|
|
753
|
+
options.timeout
|
|
749
754
|
);
|
|
750
755
|
const mergedGenerationSettings = {
|
|
751
756
|
...effectiveGenerationSettings,
|
|
@@ -794,18 +799,21 @@ var WorkflowAgent = class {
|
|
|
794
799
|
this.constructorOnStepStart,
|
|
795
800
|
options.experimental_onStepStart
|
|
796
801
|
);
|
|
797
|
-
const
|
|
798
|
-
this.
|
|
799
|
-
options.
|
|
802
|
+
const mergedOnToolExecutionStart = mergeCallbacks(
|
|
803
|
+
this.constructorOnToolExecutionStart,
|
|
804
|
+
options.experimental_onToolExecutionStart
|
|
800
805
|
);
|
|
801
|
-
const
|
|
802
|
-
this.
|
|
803
|
-
options.
|
|
806
|
+
const mergedOnToolExecutionEnd = mergeCallbacks(
|
|
807
|
+
this.constructorOnToolExecutionEnd,
|
|
808
|
+
options.experimental_onToolExecutionEnd
|
|
804
809
|
);
|
|
805
810
|
const effectiveToolChoice = effectiveToolChoiceFromPrepare;
|
|
806
811
|
const effectiveTelemetry = effectiveTelemetryFromPrepare;
|
|
807
|
-
const effectiveActiveTools = (
|
|
808
|
-
const effectiveTools = effectiveActiveTools && effectiveActiveTools.length > 0 ?
|
|
812
|
+
const effectiveActiveTools = (_i = options.activeTools) != null ? _i : this.activeTools;
|
|
813
|
+
const effectiveTools = effectiveActiveTools && effectiveActiveTools.length > 0 ? (_j = filterActiveTools2({
|
|
814
|
+
tools: this.tools,
|
|
815
|
+
activeTools: effectiveActiveTools
|
|
816
|
+
})) != null ? _j : this.tools : this.tools;
|
|
809
817
|
let experimentalContext = effectiveExperimentalContext;
|
|
810
818
|
const steps = [];
|
|
811
819
|
let lastStepToolCalls = [];
|
|
@@ -823,8 +831,8 @@ var WorkflowAgent = class {
|
|
|
823
831
|
toolName: toolCall.toolName,
|
|
824
832
|
input: toolCall.input
|
|
825
833
|
};
|
|
826
|
-
if (
|
|
827
|
-
await
|
|
834
|
+
if (mergedOnToolExecutionStart) {
|
|
835
|
+
await mergedOnToolExecutionStart({
|
|
828
836
|
toolCall: toolCallEvent,
|
|
829
837
|
stepNumber: currentStepNumber
|
|
830
838
|
});
|
|
@@ -835,8 +843,8 @@ var WorkflowAgent = class {
|
|
|
835
843
|
result = await executeTool(toolCall, tools, messages2, context);
|
|
836
844
|
} catch (err) {
|
|
837
845
|
const durationMs2 = Date.now() - startTime;
|
|
838
|
-
if (
|
|
839
|
-
await
|
|
846
|
+
if (mergedOnToolExecutionEnd) {
|
|
847
|
+
await mergedOnToolExecutionEnd({
|
|
840
848
|
toolCall: toolCallEvent,
|
|
841
849
|
stepNumber: currentStepNumber,
|
|
842
850
|
durationMs: durationMs2,
|
|
@@ -847,10 +855,10 @@ var WorkflowAgent = class {
|
|
|
847
855
|
throw err;
|
|
848
856
|
}
|
|
849
857
|
const durationMs = Date.now() - startTime;
|
|
850
|
-
if (
|
|
858
|
+
if (mergedOnToolExecutionEnd) {
|
|
851
859
|
const isError = result.output && "type" in result.output && (result.output.type === "error-text" || result.output.type === "error-json");
|
|
852
860
|
if (isError) {
|
|
853
|
-
await
|
|
861
|
+
await mergedOnToolExecutionEnd({
|
|
854
862
|
toolCall: toolCallEvent,
|
|
855
863
|
stepNumber: currentStepNumber,
|
|
856
864
|
durationMs,
|
|
@@ -858,7 +866,7 @@ var WorkflowAgent = class {
|
|
|
858
866
|
error: "value" in result.output ? result.output.value : void 0
|
|
859
867
|
});
|
|
860
868
|
} else {
|
|
861
|
-
await
|
|
869
|
+
await mergedOnToolExecutionEnd({
|
|
862
870
|
toolCall: toolCallEvent,
|
|
863
871
|
stepNumber: currentStepNumber,
|
|
864
872
|
durationMs,
|
|
@@ -869,7 +877,7 @@ var WorkflowAgent = class {
|
|
|
869
877
|
}
|
|
870
878
|
return result;
|
|
871
879
|
};
|
|
872
|
-
if ((
|
|
880
|
+
if ((_k = mergedGenerationSettings.abortSignal) == null ? void 0 : _k.aborted) {
|
|
873
881
|
if (options.onAbort) {
|
|
874
882
|
await options.onAbort({ steps });
|
|
875
883
|
}
|
|
@@ -886,19 +894,18 @@ var WorkflowAgent = class {
|
|
|
886
894
|
tools: effectiveTools,
|
|
887
895
|
writable: options.writable,
|
|
888
896
|
prompt: modelPrompt,
|
|
889
|
-
stopConditions: (
|
|
890
|
-
maxSteps: options.maxSteps,
|
|
897
|
+
stopConditions: (_l = options.stopWhen) != null ? _l : this.stopWhen,
|
|
891
898
|
onStepFinish: mergedOnStepFinish,
|
|
892
899
|
onStepStart: mergedOnStepStart,
|
|
893
900
|
onError: options.onError,
|
|
894
|
-
prepareStep: (
|
|
901
|
+
prepareStep: (_m = options.prepareStep) != null ? _m : this.prepareStep,
|
|
895
902
|
generationSettings: mergedGenerationSettings,
|
|
896
903
|
toolChoice: effectiveToolChoice,
|
|
897
904
|
experimental_context: experimentalContext,
|
|
898
|
-
|
|
899
|
-
includeRawChunks: (
|
|
900
|
-
repairToolCall: (
|
|
901
|
-
responseFormat: await ((
|
|
905
|
+
telemetry: effectiveTelemetry,
|
|
906
|
+
includeRawChunks: (_n = options.includeRawChunks) != null ? _n : false,
|
|
907
|
+
repairToolCall: (_o = options.experimental_repairToolCall) != null ? _o : this.experimentalRepairToolCall,
|
|
908
|
+
responseFormat: await ((_q = (_p = options.output) != null ? _p : this.output) == null ? void 0 : _q.responseFormat)
|
|
902
909
|
});
|
|
903
910
|
let finalMessages;
|
|
904
911
|
let encounteredError;
|
|
@@ -906,7 +913,7 @@ var WorkflowAgent = class {
|
|
|
906
913
|
try {
|
|
907
914
|
let result = await iterator.next();
|
|
908
915
|
while (!result.done) {
|
|
909
|
-
if ((
|
|
916
|
+
if ((_r = mergedGenerationSettings.abortSignal) == null ? void 0 : _r.aborted) {
|
|
910
917
|
wasAborted = true;
|
|
911
918
|
if (options.onAbort) {
|
|
912
919
|
await options.onAbort({ steps });
|
|
@@ -928,10 +935,14 @@ var WorkflowAgent = class {
|
|
|
928
935
|
experimentalContext = context;
|
|
929
936
|
}
|
|
930
937
|
if (toolCalls.length > 0) {
|
|
931
|
-
const
|
|
938
|
+
const invalidToolCalls = toolCalls.filter((tc) => tc.invalid === true);
|
|
939
|
+
const validToolCalls = toolCalls.filter((tc) => tc.invalid !== true);
|
|
940
|
+
const nonProviderToolCalls = validToolCalls.filter(
|
|
932
941
|
(tc) => !tc.providerExecuted
|
|
933
942
|
);
|
|
934
|
-
const providerToolCalls =
|
|
943
|
+
const providerToolCalls = validToolCalls.filter(
|
|
944
|
+
(tc) => tc.providerExecuted
|
|
945
|
+
);
|
|
935
946
|
const approvalNeeded = await Promise.all(
|
|
936
947
|
nonProviderToolCalls.map(async (tc) => {
|
|
937
948
|
const tool2 = effectiveTools[tc.toolName];
|
|
@@ -972,14 +983,22 @@ var WorkflowAgent = class {
|
|
|
972
983
|
providerExecutedToolResults
|
|
973
984
|
)
|
|
974
985
|
);
|
|
975
|
-
const
|
|
986
|
+
const continuationInvalidResults = invalidToolCalls.map(
|
|
987
|
+
createInvalidToolResult
|
|
988
|
+
);
|
|
989
|
+
const resolvedResults = [
|
|
990
|
+
...executableResults,
|
|
991
|
+
...providerResults,
|
|
992
|
+
...continuationInvalidResults
|
|
993
|
+
];
|
|
994
|
+
const executedResults = [...executableResults, ...providerResults];
|
|
976
995
|
const allToolCalls = toolCalls.map((tc) => ({
|
|
977
996
|
type: "tool-call",
|
|
978
997
|
toolCallId: tc.toolCallId,
|
|
979
998
|
toolName: tc.toolName,
|
|
980
999
|
input: tc.input
|
|
981
1000
|
}));
|
|
982
|
-
const allToolResults =
|
|
1001
|
+
const allToolResults = executedResults.map((r) => {
|
|
983
1002
|
var _a2;
|
|
984
1003
|
return {
|
|
985
1004
|
type: "tool-result",
|
|
@@ -1001,8 +1020,8 @@ var WorkflowAgent = class {
|
|
|
1001
1020
|
await mergedOnFinish({
|
|
1002
1021
|
steps,
|
|
1003
1022
|
messages: messages2,
|
|
1004
|
-
text: (
|
|
1005
|
-
finishReason: (
|
|
1023
|
+
text: (_s = lastStep == null ? void 0 : lastStep.text) != null ? _s : "",
|
|
1024
|
+
finishReason: (_t = lastStep == null ? void 0 : lastStep.finishReason) != null ? _t : "other",
|
|
1006
1025
|
totalUsage: aggregateUsage(steps),
|
|
1007
1026
|
experimental_context: experimentalContext,
|
|
1008
1027
|
output: void 0
|
|
@@ -1026,8 +1045,8 @@ var WorkflowAgent = class {
|
|
|
1026
1045
|
}
|
|
1027
1046
|
}
|
|
1028
1047
|
if (options.writable) {
|
|
1029
|
-
const sendFinish = (
|
|
1030
|
-
const preventClose = (
|
|
1048
|
+
const sendFinish = (_u = options.sendFinish) != null ? _u : true;
|
|
1049
|
+
const preventClose = (_v = options.preventClose) != null ? _v : false;
|
|
1031
1050
|
if (sendFinish || !preventClose) {
|
|
1032
1051
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1033
1052
|
}
|
|
@@ -1054,26 +1073,31 @@ var WorkflowAgent = class {
|
|
|
1054
1073
|
const providerToolResults = providerToolCalls.map(
|
|
1055
1074
|
(toolCall) => resolveProviderToolResult(toolCall, providerExecutedToolResults)
|
|
1056
1075
|
);
|
|
1057
|
-
const
|
|
1076
|
+
const continuationInvalidToolResults = invalidToolCalls.map(
|
|
1077
|
+
createInvalidToolResult
|
|
1078
|
+
);
|
|
1079
|
+
const continuationToolResults = toolCalls.flatMap((tc) => {
|
|
1080
|
+
const invalidResult = continuationInvalidToolResults.find(
|
|
1081
|
+
(r) => r.toolCallId === tc.toolCallId
|
|
1082
|
+
);
|
|
1083
|
+
if (invalidResult) return [invalidResult];
|
|
1058
1084
|
const clientResult = clientToolResults.find(
|
|
1059
1085
|
(r) => r.toolCallId === tc.toolCallId
|
|
1060
1086
|
);
|
|
1061
|
-
if (clientResult) return clientResult;
|
|
1087
|
+
if (clientResult) return [clientResult];
|
|
1062
1088
|
const providerResult = providerToolResults.find(
|
|
1063
1089
|
(r) => r.toolCallId === tc.toolCallId
|
|
1064
1090
|
);
|
|
1065
|
-
if (providerResult) return providerResult;
|
|
1066
|
-
return
|
|
1067
|
-
type: "tool-result",
|
|
1068
|
-
toolCallId: tc.toolCallId,
|
|
1069
|
-
toolName: tc.toolName,
|
|
1070
|
-
output: { type: "text", value: "" }
|
|
1071
|
-
};
|
|
1091
|
+
if (providerResult) return [providerResult];
|
|
1092
|
+
return [];
|
|
1072
1093
|
});
|
|
1094
|
+
const executedToolResults = continuationToolResults.filter(
|
|
1095
|
+
(result2) => !invalidToolCalls.some((tc) => tc.toolCallId === result2.toolCallId)
|
|
1096
|
+
);
|
|
1073
1097
|
if (options.writable) {
|
|
1074
1098
|
await writeToolResultsWithStepBoundary(
|
|
1075
1099
|
options.writable,
|
|
1076
|
-
|
|
1100
|
+
executedToolResults.map((r) => {
|
|
1077
1101
|
var _a2;
|
|
1078
1102
|
return {
|
|
1079
1103
|
toolCallId: r.toolCallId,
|
|
@@ -1090,7 +1114,7 @@ var WorkflowAgent = class {
|
|
|
1090
1114
|
toolName: tc.toolName,
|
|
1091
1115
|
input: tc.input
|
|
1092
1116
|
}));
|
|
1093
|
-
lastStepToolResults =
|
|
1117
|
+
lastStepToolResults = executedToolResults.map((r) => {
|
|
1094
1118
|
var _a2;
|
|
1095
1119
|
return {
|
|
1096
1120
|
type: "tool-result",
|
|
@@ -1100,7 +1124,7 @@ var WorkflowAgent = class {
|
|
|
1100
1124
|
output: "value" in r.output ? r.output.value : void 0
|
|
1101
1125
|
};
|
|
1102
1126
|
});
|
|
1103
|
-
result = await iterator.next(
|
|
1127
|
+
result = await iterator.next(continuationToolResults);
|
|
1104
1128
|
} else {
|
|
1105
1129
|
lastStepToolCalls = [];
|
|
1106
1130
|
lastStepToolResults = [];
|
|
@@ -1122,7 +1146,7 @@ var WorkflowAgent = class {
|
|
|
1122
1146
|
}
|
|
1123
1147
|
}
|
|
1124
1148
|
const messages = finalMessages != null ? finalMessages : prompt.messages;
|
|
1125
|
-
const effectiveOutput = (
|
|
1149
|
+
const effectiveOutput = (_w = options.output) != null ? _w : this.output;
|
|
1126
1150
|
let experimentalOutput = void 0;
|
|
1127
1151
|
if (effectiveOutput && steps.length > 0) {
|
|
1128
1152
|
const lastStep = steps[steps.length - 1];
|
|
@@ -1149,8 +1173,8 @@ var WorkflowAgent = class {
|
|
|
1149
1173
|
await mergedOnFinish({
|
|
1150
1174
|
steps,
|
|
1151
1175
|
messages,
|
|
1152
|
-
text: (
|
|
1153
|
-
finishReason: (
|
|
1176
|
+
text: (_x = lastStep == null ? void 0 : lastStep.text) != null ? _x : "",
|
|
1177
|
+
finishReason: (_y = lastStep == null ? void 0 : lastStep.finishReason) != null ? _y : "other",
|
|
1154
1178
|
totalUsage: aggregateUsage(steps),
|
|
1155
1179
|
experimental_context: experimentalContext,
|
|
1156
1180
|
output: experimentalOutput
|
|
@@ -1158,8 +1182,8 @@ var WorkflowAgent = class {
|
|
|
1158
1182
|
}
|
|
1159
1183
|
if (encounteredError) {
|
|
1160
1184
|
if (options.writable) {
|
|
1161
|
-
const sendFinish = (
|
|
1162
|
-
const preventClose = (
|
|
1185
|
+
const sendFinish = (_z = options.sendFinish) != null ? _z : true;
|
|
1186
|
+
const preventClose = (_A = options.preventClose) != null ? _A : false;
|
|
1163
1187
|
if (sendFinish || !preventClose) {
|
|
1164
1188
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1165
1189
|
}
|
|
@@ -1167,8 +1191,8 @@ var WorkflowAgent = class {
|
|
|
1167
1191
|
throw encounteredError;
|
|
1168
1192
|
}
|
|
1169
1193
|
if (options.writable) {
|
|
1170
|
-
const sendFinish = (
|
|
1171
|
-
const preventClose = (
|
|
1194
|
+
const sendFinish = (_B = options.sendFinish) != null ? _B : true;
|
|
1195
|
+
const preventClose = (_C = options.preventClose) != null ? _C : false;
|
|
1172
1196
|
if (sendFinish || !preventClose) {
|
|
1173
1197
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1174
1198
|
}
|
|
@@ -1269,15 +1293,6 @@ function aggregateUsage(steps) {
|
|
|
1269
1293
|
totalTokens: inputTokens + outputTokens
|
|
1270
1294
|
};
|
|
1271
1295
|
}
|
|
1272
|
-
function filterTools(tools, activeTools) {
|
|
1273
|
-
const filtered = {};
|
|
1274
|
-
for (const toolName of activeTools) {
|
|
1275
|
-
if (toolName in tools) {
|
|
1276
|
-
filtered[toolName] = tools[toolName];
|
|
1277
|
-
}
|
|
1278
|
-
}
|
|
1279
|
-
return filtered;
|
|
1280
|
-
}
|
|
1281
1296
|
function getErrorMessage(error) {
|
|
1282
1297
|
if (error == null) {
|
|
1283
1298
|
return "unknown error";
|
|
@@ -1321,6 +1336,17 @@ function resolveProviderToolResult(toolCall, providerExecutedToolResults) {
|
|
|
1321
1336
|
}
|
|
1322
1337
|
};
|
|
1323
1338
|
}
|
|
1339
|
+
function createInvalidToolResult(toolCall) {
|
|
1340
|
+
return {
|
|
1341
|
+
type: "tool-result",
|
|
1342
|
+
toolCallId: toolCall.toolCallId,
|
|
1343
|
+
toolName: toolCall.toolName,
|
|
1344
|
+
output: {
|
|
1345
|
+
type: "error-text",
|
|
1346
|
+
value: getErrorMessage(toolCall.error)
|
|
1347
|
+
}
|
|
1348
|
+
};
|
|
1349
|
+
}
|
|
1324
1350
|
async function executeTool(toolCall, tools, messages, experimentalContext) {
|
|
1325
1351
|
const tool2 = tools[toolCall.toolName];
|
|
1326
1352
|
if (!tool2) throw new Error(`Tool "${toolCall.toolName}" not found`);
|