@ai-sdk/workflow 1.0.0-beta.2 → 1.0.0-beta.20
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 +133 -0
- package/dist/index.d.mts +128 -38
- package/dist/index.mjs +83 -76
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/do-stream-step.ts +10 -25
- package/src/index.ts +5 -4
- package/src/providers/mock.ts +79 -92
- package/src/stream-text-iterator.ts +10 -26
- package/src/telemetry.ts +5 -5
- package/src/workflow-agent.ts +426 -304
package/dist/index.mjs
CHANGED
|
@@ -77,18 +77,7 @@ function resolveSerializableTools(tools) {
|
|
|
77
77
|
async function doStreamStep(conversationPrompt, modelInit, writable, serializedTools, options) {
|
|
78
78
|
"use step";
|
|
79
79
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
80
|
-
|
|
81
|
-
if (typeof modelInit === "string") {
|
|
82
|
-
model = gateway.languageModel(modelInit);
|
|
83
|
-
} else if (typeof modelInit === "function") {
|
|
84
|
-
model = await modelInit();
|
|
85
|
-
} else if (typeof modelInit === "object" && modelInit !== null && "modelId" in modelInit) {
|
|
86
|
-
model = modelInit;
|
|
87
|
-
} else {
|
|
88
|
-
throw new Error(
|
|
89
|
-
'Invalid "model initialization" argument. Must be a string, a LanguageModel instance, or a function that returns a LanguageModel instance.'
|
|
90
|
-
);
|
|
91
|
-
}
|
|
80
|
+
const model = typeof modelInit === "string" ? gateway.languageModel(modelInit) : modelInit;
|
|
92
81
|
const tools = serializedTools ? resolveSerializableTools(serializedTools) : void 0;
|
|
93
82
|
const { stream: modelStream } = await streamModelCall({
|
|
94
83
|
model,
|
|
@@ -198,7 +187,8 @@ async function doStreamStep(conversationPrompt, modelInit, writable, serializedT
|
|
|
198
187
|
},
|
|
199
188
|
functionId: void 0,
|
|
200
189
|
metadata: void 0,
|
|
201
|
-
|
|
190
|
+
runtimeContext: void 0,
|
|
191
|
+
toolsContext: {},
|
|
202
192
|
content: [
|
|
203
193
|
...text ? [{ type: "text", text }] : [],
|
|
204
194
|
...toolCalls.filter((tc) => !tc.invalid).map((tc) => ({
|
|
@@ -276,7 +266,6 @@ async function* streamTextIterator({
|
|
|
276
266
|
writable,
|
|
277
267
|
model,
|
|
278
268
|
stopConditions,
|
|
279
|
-
maxSteps,
|
|
280
269
|
onStepFinish,
|
|
281
270
|
onStepStart,
|
|
282
271
|
onError,
|
|
@@ -302,11 +291,7 @@ async function* streamTextIterator({
|
|
|
302
291
|
let stepNumber = 0;
|
|
303
292
|
let lastStep;
|
|
304
293
|
let lastStepWasToolCalls = false;
|
|
305
|
-
const effectiveMaxSteps = maxSteps != null ? maxSteps : Infinity;
|
|
306
294
|
while (!done) {
|
|
307
|
-
if (stepNumber >= effectiveMaxSteps) {
|
|
308
|
-
break;
|
|
309
|
-
}
|
|
310
295
|
if ((_a = currentGenerationSettings.abortSignal) == null ? void 0 : _a.aborted) {
|
|
311
296
|
break;
|
|
312
297
|
}
|
|
@@ -417,7 +402,8 @@ async function* streamTextIterator({
|
|
|
417
402
|
await onStepStart({
|
|
418
403
|
stepNumber,
|
|
419
404
|
model: currentModel,
|
|
420
|
-
messages: conversationPrompt
|
|
405
|
+
messages: conversationPrompt,
|
|
406
|
+
steps: [...steps]
|
|
421
407
|
});
|
|
422
408
|
}
|
|
423
409
|
try {
|
|
@@ -559,12 +545,18 @@ function sanitizeProviderMetadataForToolCall(metadata) {
|
|
|
559
545
|
var WorkflowAgent = class {
|
|
560
546
|
constructor(options) {
|
|
561
547
|
var _a, _b;
|
|
548
|
+
this.id = options.id;
|
|
562
549
|
this.model = options.model;
|
|
563
550
|
this.tools = (_a = options.tools) != null ? _a : {};
|
|
564
551
|
this.instructions = (_b = options.instructions) != null ? _b : options.system;
|
|
565
552
|
this.toolChoice = options.toolChoice;
|
|
566
553
|
this.telemetry = options.experimental_telemetry;
|
|
567
554
|
this.experimentalContext = options.experimental_context;
|
|
555
|
+
this.stopWhen = options.stopWhen;
|
|
556
|
+
this.activeTools = options.activeTools;
|
|
557
|
+
this.output = options.output;
|
|
558
|
+
this.experimentalRepairToolCall = options.experimental_repairToolCall;
|
|
559
|
+
this.experimentalDownload = options.experimental_download;
|
|
568
560
|
this.prepareStep = options.prepareStep;
|
|
569
561
|
this.constructorOnStepFinish = options.onStepFinish;
|
|
570
562
|
this.constructorOnFinish = options.onFinish;
|
|
@@ -592,14 +584,16 @@ var WorkflowAgent = class {
|
|
|
592
584
|
throw new Error("Not implemented");
|
|
593
585
|
}
|
|
594
586
|
async stream(options) {
|
|
595
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
|
|
587
|
+
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;
|
|
596
588
|
let effectiveModel = this.model;
|
|
597
589
|
let effectiveInstructions = (_a = options.system) != null ? _a : this.instructions;
|
|
590
|
+
let effectivePrompt = options.prompt;
|
|
598
591
|
let effectiveMessages = options.messages;
|
|
599
592
|
let effectiveGenerationSettings = { ...this.generationSettings };
|
|
600
593
|
let effectiveExperimentalContext = (_b = options.experimental_context) != null ? _b : this.experimentalContext;
|
|
601
594
|
let effectiveToolChoiceFromPrepare = (_c = options.toolChoice) != null ? _c : this.toolChoice;
|
|
602
595
|
let effectiveTelemetryFromPrepare = (_d = options.experimental_telemetry) != null ? _d : this.telemetry;
|
|
596
|
+
const resolvedMessagesForPrepareCall = (_e = effectiveMessages != null ? effectiveMessages : typeof effectivePrompt === "string" ? [{ role: "user", content: effectivePrompt }] : effectivePrompt) != null ? _e : [];
|
|
603
597
|
if (this.prepareCall) {
|
|
604
598
|
const prepared = await this.prepareCall({
|
|
605
599
|
model: effectiveModel,
|
|
@@ -608,14 +602,16 @@ var WorkflowAgent = class {
|
|
|
608
602
|
toolChoice: effectiveToolChoiceFromPrepare,
|
|
609
603
|
experimental_telemetry: effectiveTelemetryFromPrepare,
|
|
610
604
|
experimental_context: effectiveExperimentalContext,
|
|
611
|
-
messages:
|
|
605
|
+
messages: resolvedMessagesForPrepareCall,
|
|
612
606
|
...effectiveGenerationSettings
|
|
613
607
|
});
|
|
614
608
|
if (prepared.model !== void 0) effectiveModel = prepared.model;
|
|
615
609
|
if (prepared.instructions !== void 0)
|
|
616
610
|
effectiveInstructions = prepared.instructions;
|
|
617
|
-
if (prepared.messages !== void 0)
|
|
611
|
+
if (prepared.messages !== void 0) {
|
|
618
612
|
effectiveMessages = prepared.messages;
|
|
613
|
+
effectivePrompt = void 0;
|
|
614
|
+
}
|
|
619
615
|
if (prepared.experimental_context !== void 0)
|
|
620
616
|
effectiveExperimentalContext = prepared.experimental_context;
|
|
621
617
|
if (prepared.toolChoice !== void 0)
|
|
@@ -645,7 +641,7 @@ var WorkflowAgent = class {
|
|
|
645
641
|
}
|
|
646
642
|
const prompt = await standardizePrompt({
|
|
647
643
|
system: effectiveInstructions,
|
|
648
|
-
messages: effectiveMessages
|
|
644
|
+
...effectivePrompt != null ? { prompt: effectivePrompt } : { messages: effectiveMessages }
|
|
649
645
|
});
|
|
650
646
|
const { approvedToolApprovals, deniedToolApprovals } = collectToolApprovalsFromMessages(prompt.messages);
|
|
651
647
|
if (approvedToolApprovals.length > 0 || deniedToolApprovals.length > 0) {
|
|
@@ -741,10 +737,10 @@ var WorkflowAgent = class {
|
|
|
741
737
|
const modelPrompt = await convertToLanguageModelPrompt({
|
|
742
738
|
prompt,
|
|
743
739
|
supportedUrls: {},
|
|
744
|
-
download: options.experimental_download
|
|
740
|
+
download: (_f = options.experimental_download) != null ? _f : this.experimentalDownload
|
|
745
741
|
});
|
|
746
742
|
const effectiveAbortSignal = mergeAbortSignals(
|
|
747
|
-
(
|
|
743
|
+
(_g = options.abortSignal) != null ? _g : effectiveGenerationSettings.abortSignal,
|
|
748
744
|
options.timeout != null ? AbortSignal.timeout(options.timeout) : void 0
|
|
749
745
|
);
|
|
750
746
|
const mergedGenerationSettings = {
|
|
@@ -804,7 +800,8 @@ var WorkflowAgent = class {
|
|
|
804
800
|
);
|
|
805
801
|
const effectiveToolChoice = effectiveToolChoiceFromPrepare;
|
|
806
802
|
const effectiveTelemetry = effectiveTelemetryFromPrepare;
|
|
807
|
-
const
|
|
803
|
+
const effectiveActiveTools = (_h = options.activeTools) != null ? _h : this.activeTools;
|
|
804
|
+
const effectiveTools = effectiveActiveTools && effectiveActiveTools.length > 0 ? filterTools(this.tools, effectiveActiveTools) : this.tools;
|
|
808
805
|
let experimentalContext = effectiveExperimentalContext;
|
|
809
806
|
const steps = [];
|
|
810
807
|
let lastStepToolCalls = [];
|
|
@@ -812,61 +809,68 @@ var WorkflowAgent = class {
|
|
|
812
809
|
if (mergedOnStart) {
|
|
813
810
|
await mergedOnStart({
|
|
814
811
|
model: effectiveModel,
|
|
815
|
-
messages:
|
|
812
|
+
messages: prompt.messages
|
|
816
813
|
});
|
|
817
814
|
}
|
|
818
|
-
const executeToolWithCallbacks = async (toolCall, tools, messages2, context) => {
|
|
815
|
+
const executeToolWithCallbacks = async (toolCall, tools, messages2, context, currentStepNumber = 0) => {
|
|
816
|
+
const toolCallEvent = {
|
|
817
|
+
type: "tool-call",
|
|
818
|
+
toolCallId: toolCall.toolCallId,
|
|
819
|
+
toolName: toolCall.toolName,
|
|
820
|
+
input: toolCall.input
|
|
821
|
+
};
|
|
819
822
|
if (mergedOnToolCallStart) {
|
|
820
823
|
await mergedOnToolCallStart({
|
|
821
|
-
toolCall:
|
|
822
|
-
|
|
823
|
-
toolCallId: toolCall.toolCallId,
|
|
824
|
-
toolName: toolCall.toolName,
|
|
825
|
-
input: toolCall.input
|
|
826
|
-
}
|
|
824
|
+
toolCall: toolCallEvent,
|
|
825
|
+
stepNumber: currentStepNumber
|
|
827
826
|
});
|
|
828
827
|
}
|
|
828
|
+
const startTime = Date.now();
|
|
829
829
|
let result;
|
|
830
830
|
try {
|
|
831
831
|
result = await executeTool(toolCall, tools, messages2, context);
|
|
832
832
|
} catch (err) {
|
|
833
|
+
const durationMs2 = Date.now() - startTime;
|
|
833
834
|
if (mergedOnToolCallFinish) {
|
|
834
835
|
await mergedOnToolCallFinish({
|
|
835
|
-
toolCall:
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
input: toolCall.input
|
|
840
|
-
},
|
|
836
|
+
toolCall: toolCallEvent,
|
|
837
|
+
stepNumber: currentStepNumber,
|
|
838
|
+
durationMs: durationMs2,
|
|
839
|
+
success: false,
|
|
841
840
|
error: err
|
|
842
841
|
});
|
|
843
842
|
}
|
|
844
843
|
throw err;
|
|
845
844
|
}
|
|
845
|
+
const durationMs = Date.now() - startTime;
|
|
846
846
|
if (mergedOnToolCallFinish) {
|
|
847
847
|
const isError = result.output && "type" in result.output && (result.output.type === "error-text" || result.output.type === "error-json");
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
},
|
|
855
|
-
...isError ? {
|
|
848
|
+
if (isError) {
|
|
849
|
+
await mergedOnToolCallFinish({
|
|
850
|
+
toolCall: toolCallEvent,
|
|
851
|
+
stepNumber: currentStepNumber,
|
|
852
|
+
durationMs,
|
|
853
|
+
success: false,
|
|
856
854
|
error: "value" in result.output ? result.output.value : void 0
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
855
|
+
});
|
|
856
|
+
} else {
|
|
857
|
+
await mergedOnToolCallFinish({
|
|
858
|
+
toolCall: toolCallEvent,
|
|
859
|
+
stepNumber: currentStepNumber,
|
|
860
|
+
durationMs,
|
|
861
|
+
success: true,
|
|
862
|
+
output: result.output && "value" in result.output ? result.output.value : void 0
|
|
863
|
+
});
|
|
864
|
+
}
|
|
861
865
|
}
|
|
862
866
|
return result;
|
|
863
867
|
};
|
|
864
|
-
if ((
|
|
868
|
+
if ((_i = mergedGenerationSettings.abortSignal) == null ? void 0 : _i.aborted) {
|
|
865
869
|
if (options.onAbort) {
|
|
866
870
|
await options.onAbort({ steps });
|
|
867
871
|
}
|
|
868
872
|
return {
|
|
869
|
-
messages:
|
|
873
|
+
messages: prompt.messages,
|
|
870
874
|
steps,
|
|
871
875
|
toolCalls: [],
|
|
872
876
|
toolResults: [],
|
|
@@ -878,19 +882,18 @@ var WorkflowAgent = class {
|
|
|
878
882
|
tools: effectiveTools,
|
|
879
883
|
writable: options.writable,
|
|
880
884
|
prompt: modelPrompt,
|
|
881
|
-
stopConditions: options.stopWhen,
|
|
882
|
-
maxSteps: options.maxSteps,
|
|
885
|
+
stopConditions: (_j = options.stopWhen) != null ? _j : this.stopWhen,
|
|
883
886
|
onStepFinish: mergedOnStepFinish,
|
|
884
887
|
onStepStart: mergedOnStepStart,
|
|
885
888
|
onError: options.onError,
|
|
886
|
-
prepareStep: (
|
|
889
|
+
prepareStep: (_k = options.prepareStep) != null ? _k : this.prepareStep,
|
|
887
890
|
generationSettings: mergedGenerationSettings,
|
|
888
891
|
toolChoice: effectiveToolChoice,
|
|
889
892
|
experimental_context: experimentalContext,
|
|
890
893
|
experimental_telemetry: effectiveTelemetry,
|
|
891
|
-
includeRawChunks: (
|
|
892
|
-
repairToolCall: options.experimental_repairToolCall,
|
|
893
|
-
responseFormat: await ((
|
|
894
|
+
includeRawChunks: (_l = options.includeRawChunks) != null ? _l : false,
|
|
895
|
+
repairToolCall: (_m = options.experimental_repairToolCall) != null ? _m : this.experimentalRepairToolCall,
|
|
896
|
+
responseFormat: await ((_o = (_n = options.output) != null ? _n : this.output) == null ? void 0 : _o.responseFormat)
|
|
894
897
|
});
|
|
895
898
|
let finalMessages;
|
|
896
899
|
let encounteredError;
|
|
@@ -898,7 +901,7 @@ var WorkflowAgent = class {
|
|
|
898
901
|
try {
|
|
899
902
|
let result = await iterator.next();
|
|
900
903
|
while (!result.done) {
|
|
901
|
-
if ((
|
|
904
|
+
if ((_p = mergedGenerationSettings.abortSignal) == null ? void 0 : _p.aborted) {
|
|
902
905
|
wasAborted = true;
|
|
903
906
|
if (options.onAbort) {
|
|
904
907
|
await options.onAbort({ steps });
|
|
@@ -912,6 +915,7 @@ var WorkflowAgent = class {
|
|
|
912
915
|
context,
|
|
913
916
|
providerExecutedToolResults
|
|
914
917
|
} = result.value;
|
|
918
|
+
const currentStepNumber = steps.length;
|
|
915
919
|
if (step) {
|
|
916
920
|
steps.push(step);
|
|
917
921
|
}
|
|
@@ -952,7 +956,8 @@ var WorkflowAgent = class {
|
|
|
952
956
|
toolCall,
|
|
953
957
|
effectiveTools,
|
|
954
958
|
iterMessages,
|
|
955
|
-
experimentalContext
|
|
959
|
+
experimentalContext,
|
|
960
|
+
currentStepNumber
|
|
956
961
|
)
|
|
957
962
|
)
|
|
958
963
|
);
|
|
@@ -991,8 +996,8 @@ var WorkflowAgent = class {
|
|
|
991
996
|
await mergedOnFinish({
|
|
992
997
|
steps,
|
|
993
998
|
messages: messages2,
|
|
994
|
-
text: (
|
|
995
|
-
finishReason: (
|
|
999
|
+
text: (_q = lastStep == null ? void 0 : lastStep.text) != null ? _q : "",
|
|
1000
|
+
finishReason: (_r = lastStep == null ? void 0 : lastStep.finishReason) != null ? _r : "other",
|
|
996
1001
|
totalUsage: aggregateUsage(steps),
|
|
997
1002
|
experimental_context: experimentalContext,
|
|
998
1003
|
output: void 0
|
|
@@ -1016,8 +1021,8 @@ var WorkflowAgent = class {
|
|
|
1016
1021
|
}
|
|
1017
1022
|
}
|
|
1018
1023
|
if (options.writable) {
|
|
1019
|
-
const sendFinish = (
|
|
1020
|
-
const preventClose = (
|
|
1024
|
+
const sendFinish = (_s = options.sendFinish) != null ? _s : true;
|
|
1025
|
+
const preventClose = (_t = options.preventClose) != null ? _t : false;
|
|
1021
1026
|
if (sendFinish || !preventClose) {
|
|
1022
1027
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1023
1028
|
}
|
|
@@ -1036,7 +1041,8 @@ var WorkflowAgent = class {
|
|
|
1036
1041
|
toolCall,
|
|
1037
1042
|
effectiveTools,
|
|
1038
1043
|
iterMessages,
|
|
1039
|
-
experimentalContext
|
|
1044
|
+
experimentalContext,
|
|
1045
|
+
currentStepNumber
|
|
1040
1046
|
)
|
|
1041
1047
|
)
|
|
1042
1048
|
);
|
|
@@ -1110,14 +1116,15 @@ var WorkflowAgent = class {
|
|
|
1110
1116
|
await options.onError({ error });
|
|
1111
1117
|
}
|
|
1112
1118
|
}
|
|
1113
|
-
const messages = finalMessages != null ? finalMessages :
|
|
1119
|
+
const messages = finalMessages != null ? finalMessages : prompt.messages;
|
|
1120
|
+
const effectiveOutput = (_u = options.output) != null ? _u : this.output;
|
|
1114
1121
|
let experimentalOutput = void 0;
|
|
1115
|
-
if (
|
|
1122
|
+
if (effectiveOutput && steps.length > 0) {
|
|
1116
1123
|
const lastStep = steps[steps.length - 1];
|
|
1117
1124
|
const text = lastStep.text;
|
|
1118
1125
|
if (text) {
|
|
1119
1126
|
try {
|
|
1120
|
-
experimentalOutput = await
|
|
1127
|
+
experimentalOutput = await effectiveOutput.parseCompleteOutput(
|
|
1121
1128
|
{ text },
|
|
1122
1129
|
{
|
|
1123
1130
|
response: lastStep.response,
|
|
@@ -1137,8 +1144,8 @@ var WorkflowAgent = class {
|
|
|
1137
1144
|
await mergedOnFinish({
|
|
1138
1145
|
steps,
|
|
1139
1146
|
messages,
|
|
1140
|
-
text: (
|
|
1141
|
-
finishReason: (
|
|
1147
|
+
text: (_v = lastStep == null ? void 0 : lastStep.text) != null ? _v : "",
|
|
1148
|
+
finishReason: (_w = lastStep == null ? void 0 : lastStep.finishReason) != null ? _w : "other",
|
|
1142
1149
|
totalUsage: aggregateUsage(steps),
|
|
1143
1150
|
experimental_context: experimentalContext,
|
|
1144
1151
|
output: experimentalOutput
|
|
@@ -1146,8 +1153,8 @@ var WorkflowAgent = class {
|
|
|
1146
1153
|
}
|
|
1147
1154
|
if (encounteredError) {
|
|
1148
1155
|
if (options.writable) {
|
|
1149
|
-
const sendFinish = (
|
|
1150
|
-
const preventClose = (
|
|
1156
|
+
const sendFinish = (_x = options.sendFinish) != null ? _x : true;
|
|
1157
|
+
const preventClose = (_y = options.preventClose) != null ? _y : false;
|
|
1151
1158
|
if (sendFinish || !preventClose) {
|
|
1152
1159
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1153
1160
|
}
|
|
@@ -1155,8 +1162,8 @@ var WorkflowAgent = class {
|
|
|
1155
1162
|
throw encounteredError;
|
|
1156
1163
|
}
|
|
1157
1164
|
if (options.writable) {
|
|
1158
|
-
const sendFinish = (
|
|
1159
|
-
const preventClose = (
|
|
1165
|
+
const sendFinish = (_z = options.sendFinish) != null ? _z : true;
|
|
1166
|
+
const preventClose = (_A = options.preventClose) != null ? _A : false;
|
|
1160
1167
|
if (sendFinish || !preventClose) {
|
|
1161
1168
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1162
1169
|
}
|