@ai-sdk/workflow 1.0.0-beta.3 → 1.0.0-beta.5
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 +14 -0
- package/dist/index.d.mts +73 -5
- package/dist/index.mjs +41 -29
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/workflow-agent.ts +313 -206
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @ai-sdk/workflow
|
|
2
2
|
|
|
3
|
+
## 1.0.0-beta.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- bf6c17b: Add `id` property to WorkflowAgent for telemetry identification, matching ToolLoopAgent's API surface.
|
|
8
|
+
- 3ca592a: Add `prompt` as an alternative to `messages` in `WorkflowAgent.stream()`, matching the `AgentCallParameters` pattern from ToolLoopAgent.
|
|
9
|
+
- eb49d29: Add constructor-level defaults for `stopWhen`, `activeTools`, `output`, `experimental_repairToolCall`, and `experimental_download` to WorkflowAgent, matching ToolLoopAgent's pattern. Stream-level values override constructor defaults.
|
|
10
|
+
|
|
11
|
+
## 1.0.0-beta.4
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- ai@7.0.0-beta.92
|
|
16
|
+
|
|
3
17
|
## 1.0.0-beta.3
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { LanguageModelV4, SharedV4ProviderOptions,
|
|
2
|
-
import { ToolSet, LanguageModel, SystemModelMessage, ToolChoice,
|
|
1
|
+
import { LanguageModelV4, SharedV4ProviderOptions, LanguageModelV4CallOptions, LanguageModelV4Prompt, LanguageModelV4StreamPart } from '@ai-sdk/provider';
|
|
2
|
+
import { ToolSet, LanguageModel, SystemModelMessage, ToolChoice, StopCondition, LanguageModelResponseMetadata, LanguageModelUsage, FinishReason, ToolCallRepairFunction, StepResult, StreamTextOnStepFinishCallback, ModelMessage, Experimental_LanguageModelStreamPart, UIMessage, UIMessageChunk, ChatTransport, PrepareSendMessagesRequest, PrepareReconnectToStreamRequest, ChatRequestOptions } from 'ai';
|
|
3
3
|
export { Experimental_LanguageModelStreamPart as ModelCallStreamPart, Output, ToolCallRepairFunction } from 'ai';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -271,6 +271,10 @@ type PrepareCallCallback<TTools extends ToolSet = ToolSet> = (options: PrepareCa
|
|
|
271
271
|
* Configuration options for creating a {@link WorkflowAgent} instance.
|
|
272
272
|
*/
|
|
273
273
|
interface WorkflowAgentOptions<TTools extends ToolSet = ToolSet> extends GenerationSettings {
|
|
274
|
+
/**
|
|
275
|
+
* The id of the agent.
|
|
276
|
+
*/
|
|
277
|
+
id?: string;
|
|
274
278
|
/**
|
|
275
279
|
* The model provider to use for the agent.
|
|
276
280
|
*
|
|
@@ -310,6 +314,39 @@ interface WorkflowAgentOptions<TTools extends ToolSet = ToolSet> extends Generat
|
|
|
310
314
|
* @default undefined
|
|
311
315
|
*/
|
|
312
316
|
experimental_context?: unknown;
|
|
317
|
+
/**
|
|
318
|
+
* Default stop condition for the agent loop. When the condition is an array,
|
|
319
|
+
* any of the conditions can be met to stop the generation.
|
|
320
|
+
*
|
|
321
|
+
* Per-stream `stopWhen` values passed to `stream()` override this default.
|
|
322
|
+
*/
|
|
323
|
+
stopWhen?: StopCondition<NoInfer<ToolSet>, any> | Array<StopCondition<NoInfer<ToolSet>, any>>;
|
|
324
|
+
/**
|
|
325
|
+
* Default set of active tools that limits which tools the model can call,
|
|
326
|
+
* without changing the tool call and result types in the result.
|
|
327
|
+
*
|
|
328
|
+
* Per-stream `activeTools` values passed to `stream()` override this default.
|
|
329
|
+
*/
|
|
330
|
+
activeTools?: Array<keyof NoInfer<TTools>>;
|
|
331
|
+
/**
|
|
332
|
+
* Default output specification for structured outputs.
|
|
333
|
+
* Use `Output.object({ schema })` for structured output or `Output.text()` for text output.
|
|
334
|
+
*
|
|
335
|
+
* Per-stream `output` values passed to `stream()` override this default.
|
|
336
|
+
*/
|
|
337
|
+
output?: OutputSpecification<any, any>;
|
|
338
|
+
/**
|
|
339
|
+
* Default function that attempts to repair a tool call that failed to parse.
|
|
340
|
+
*
|
|
341
|
+
* Per-stream `experimental_repairToolCall` values passed to `stream()` override this default.
|
|
342
|
+
*/
|
|
343
|
+
experimental_repairToolCall?: ToolCallRepairFunction<TTools>;
|
|
344
|
+
/**
|
|
345
|
+
* Default custom download function to use for URLs.
|
|
346
|
+
*
|
|
347
|
+
* Per-stream `experimental_download` values passed to `stream()` override this default.
|
|
348
|
+
*/
|
|
349
|
+
experimental_download?: DownloadFunction;
|
|
313
350
|
/**
|
|
314
351
|
* Default callback function called before each step in the agent loop.
|
|
315
352
|
* Use this to modify settings, manage context, or inject messages dynamically
|
|
@@ -439,11 +476,33 @@ type WorkflowAgentOnToolCallFinishCallback = (event: {
|
|
|
439
476
|
/**
|
|
440
477
|
* Options for the {@link WorkflowAgent.stream} method.
|
|
441
478
|
*/
|
|
442
|
-
|
|
479
|
+
type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never, PARTIAL_OUTPUT = never> = Partial<GenerationSettings> & ({
|
|
480
|
+
/**
|
|
481
|
+
* A prompt. It can be either a text prompt or a list of messages.
|
|
482
|
+
*
|
|
483
|
+
* You can either use `prompt` or `messages` but not both.
|
|
484
|
+
*/
|
|
485
|
+
prompt: string | Array<ModelMessage>;
|
|
486
|
+
/**
|
|
487
|
+
* A list of messages.
|
|
488
|
+
*
|
|
489
|
+
* You can either use `prompt` or `messages` but not both.
|
|
490
|
+
*/
|
|
491
|
+
messages?: never;
|
|
492
|
+
} | {
|
|
443
493
|
/**
|
|
444
494
|
* The conversation messages to process. Should follow the AI SDK's ModelMessage format.
|
|
495
|
+
*
|
|
496
|
+
* You can either use `prompt` or `messages` but not both.
|
|
445
497
|
*/
|
|
446
|
-
messages: ModelMessage
|
|
498
|
+
messages: Array<ModelMessage>;
|
|
499
|
+
/**
|
|
500
|
+
* A prompt. It can be either a text prompt or a list of messages.
|
|
501
|
+
*
|
|
502
|
+
* You can either use `prompt` or `messages` but not both.
|
|
503
|
+
*/
|
|
504
|
+
prompt?: never;
|
|
505
|
+
}) & {
|
|
447
506
|
/**
|
|
448
507
|
* Optional system prompt override. If provided, overrides the system prompt from the constructor.
|
|
449
508
|
*/
|
|
@@ -617,7 +676,7 @@ interface WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT =
|
|
|
617
676
|
* @default false
|
|
618
677
|
*/
|
|
619
678
|
preventClose?: boolean;
|
|
620
|
-
}
|
|
679
|
+
};
|
|
621
680
|
/**
|
|
622
681
|
* A tool call made by the model. Matches the AI SDK's tool call shape.
|
|
623
682
|
*/
|
|
@@ -717,6 +776,10 @@ interface WorkflowAgentStreamResult<TTools extends ToolSet = ToolSet, OUTPUT = n
|
|
|
717
776
|
* ```
|
|
718
777
|
*/
|
|
719
778
|
declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
|
|
779
|
+
/**
|
|
780
|
+
* The id of the agent.
|
|
781
|
+
*/
|
|
782
|
+
readonly id: string | undefined;
|
|
720
783
|
private model;
|
|
721
784
|
/**
|
|
722
785
|
* The tool set configured for this agent.
|
|
@@ -727,6 +790,11 @@ declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
|
|
|
727
790
|
private toolChoice?;
|
|
728
791
|
private telemetry?;
|
|
729
792
|
private experimentalContext;
|
|
793
|
+
private stopWhen?;
|
|
794
|
+
private activeTools?;
|
|
795
|
+
private output?;
|
|
796
|
+
private experimentalRepairToolCall?;
|
|
797
|
+
private experimentalDownload?;
|
|
730
798
|
private prepareStep?;
|
|
731
799
|
private constructorOnStepFinish?;
|
|
732
800
|
private constructorOnFinish?;
|
package/dist/index.mjs
CHANGED
|
@@ -548,12 +548,18 @@ function sanitizeProviderMetadataForToolCall(metadata) {
|
|
|
548
548
|
var WorkflowAgent = class {
|
|
549
549
|
constructor(options) {
|
|
550
550
|
var _a, _b;
|
|
551
|
+
this.id = options.id;
|
|
551
552
|
this.model = options.model;
|
|
552
553
|
this.tools = (_a = options.tools) != null ? _a : {};
|
|
553
554
|
this.instructions = (_b = options.instructions) != null ? _b : options.system;
|
|
554
555
|
this.toolChoice = options.toolChoice;
|
|
555
556
|
this.telemetry = options.experimental_telemetry;
|
|
556
557
|
this.experimentalContext = options.experimental_context;
|
|
558
|
+
this.stopWhen = options.stopWhen;
|
|
559
|
+
this.activeTools = options.activeTools;
|
|
560
|
+
this.output = options.output;
|
|
561
|
+
this.experimentalRepairToolCall = options.experimental_repairToolCall;
|
|
562
|
+
this.experimentalDownload = options.experimental_download;
|
|
557
563
|
this.prepareStep = options.prepareStep;
|
|
558
564
|
this.constructorOnStepFinish = options.onStepFinish;
|
|
559
565
|
this.constructorOnFinish = options.onFinish;
|
|
@@ -581,14 +587,16 @@ var WorkflowAgent = class {
|
|
|
581
587
|
throw new Error("Not implemented");
|
|
582
588
|
}
|
|
583
589
|
async stream(options) {
|
|
584
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
|
|
590
|
+
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;
|
|
585
591
|
let effectiveModel = this.model;
|
|
586
592
|
let effectiveInstructions = (_a = options.system) != null ? _a : this.instructions;
|
|
593
|
+
let effectivePrompt = options.prompt;
|
|
587
594
|
let effectiveMessages = options.messages;
|
|
588
595
|
let effectiveGenerationSettings = { ...this.generationSettings };
|
|
589
596
|
let effectiveExperimentalContext = (_b = options.experimental_context) != null ? _b : this.experimentalContext;
|
|
590
597
|
let effectiveToolChoiceFromPrepare = (_c = options.toolChoice) != null ? _c : this.toolChoice;
|
|
591
598
|
let effectiveTelemetryFromPrepare = (_d = options.experimental_telemetry) != null ? _d : this.telemetry;
|
|
599
|
+
const resolvedMessagesForPrepareCall = (_e = effectiveMessages != null ? effectiveMessages : typeof effectivePrompt === "string" ? [{ role: "user", content: effectivePrompt }] : effectivePrompt) != null ? _e : [];
|
|
592
600
|
if (this.prepareCall) {
|
|
593
601
|
const prepared = await this.prepareCall({
|
|
594
602
|
model: effectiveModel,
|
|
@@ -597,14 +605,16 @@ var WorkflowAgent = class {
|
|
|
597
605
|
toolChoice: effectiveToolChoiceFromPrepare,
|
|
598
606
|
experimental_telemetry: effectiveTelemetryFromPrepare,
|
|
599
607
|
experimental_context: effectiveExperimentalContext,
|
|
600
|
-
messages:
|
|
608
|
+
messages: resolvedMessagesForPrepareCall,
|
|
601
609
|
...effectiveGenerationSettings
|
|
602
610
|
});
|
|
603
611
|
if (prepared.model !== void 0) effectiveModel = prepared.model;
|
|
604
612
|
if (prepared.instructions !== void 0)
|
|
605
613
|
effectiveInstructions = prepared.instructions;
|
|
606
|
-
if (prepared.messages !== void 0)
|
|
614
|
+
if (prepared.messages !== void 0) {
|
|
607
615
|
effectiveMessages = prepared.messages;
|
|
616
|
+
effectivePrompt = void 0;
|
|
617
|
+
}
|
|
608
618
|
if (prepared.experimental_context !== void 0)
|
|
609
619
|
effectiveExperimentalContext = prepared.experimental_context;
|
|
610
620
|
if (prepared.toolChoice !== void 0)
|
|
@@ -634,7 +644,7 @@ var WorkflowAgent = class {
|
|
|
634
644
|
}
|
|
635
645
|
const prompt = await standardizePrompt({
|
|
636
646
|
system: effectiveInstructions,
|
|
637
|
-
messages: effectiveMessages
|
|
647
|
+
...effectivePrompt != null ? { prompt: effectivePrompt } : { messages: effectiveMessages }
|
|
638
648
|
});
|
|
639
649
|
const { approvedToolApprovals, deniedToolApprovals } = collectToolApprovalsFromMessages(prompt.messages);
|
|
640
650
|
if (approvedToolApprovals.length > 0 || deniedToolApprovals.length > 0) {
|
|
@@ -730,10 +740,10 @@ var WorkflowAgent = class {
|
|
|
730
740
|
const modelPrompt = await convertToLanguageModelPrompt({
|
|
731
741
|
prompt,
|
|
732
742
|
supportedUrls: {},
|
|
733
|
-
download: options.experimental_download
|
|
743
|
+
download: (_f = options.experimental_download) != null ? _f : this.experimentalDownload
|
|
734
744
|
});
|
|
735
745
|
const effectiveAbortSignal = mergeAbortSignals(
|
|
736
|
-
(
|
|
746
|
+
(_g = options.abortSignal) != null ? _g : effectiveGenerationSettings.abortSignal,
|
|
737
747
|
options.timeout != null ? AbortSignal.timeout(options.timeout) : void 0
|
|
738
748
|
);
|
|
739
749
|
const mergedGenerationSettings = {
|
|
@@ -793,7 +803,8 @@ var WorkflowAgent = class {
|
|
|
793
803
|
);
|
|
794
804
|
const effectiveToolChoice = effectiveToolChoiceFromPrepare;
|
|
795
805
|
const effectiveTelemetry = effectiveTelemetryFromPrepare;
|
|
796
|
-
const
|
|
806
|
+
const effectiveActiveTools = (_h = options.activeTools) != null ? _h : this.activeTools;
|
|
807
|
+
const effectiveTools = effectiveActiveTools && effectiveActiveTools.length > 0 ? filterTools(this.tools, effectiveActiveTools) : this.tools;
|
|
797
808
|
let experimentalContext = effectiveExperimentalContext;
|
|
798
809
|
const steps = [];
|
|
799
810
|
let lastStepToolCalls = [];
|
|
@@ -801,7 +812,7 @@ var WorkflowAgent = class {
|
|
|
801
812
|
if (mergedOnStart) {
|
|
802
813
|
await mergedOnStart({
|
|
803
814
|
model: effectiveModel,
|
|
804
|
-
messages:
|
|
815
|
+
messages: prompt.messages
|
|
805
816
|
});
|
|
806
817
|
}
|
|
807
818
|
const executeToolWithCallbacks = async (toolCall, tools, messages2, context) => {
|
|
@@ -850,12 +861,12 @@ var WorkflowAgent = class {
|
|
|
850
861
|
}
|
|
851
862
|
return result;
|
|
852
863
|
};
|
|
853
|
-
if ((
|
|
864
|
+
if ((_i = mergedGenerationSettings.abortSignal) == null ? void 0 : _i.aborted) {
|
|
854
865
|
if (options.onAbort) {
|
|
855
866
|
await options.onAbort({ steps });
|
|
856
867
|
}
|
|
857
868
|
return {
|
|
858
|
-
messages:
|
|
869
|
+
messages: prompt.messages,
|
|
859
870
|
steps,
|
|
860
871
|
toolCalls: [],
|
|
861
872
|
toolResults: [],
|
|
@@ -867,19 +878,19 @@ var WorkflowAgent = class {
|
|
|
867
878
|
tools: effectiveTools,
|
|
868
879
|
writable: options.writable,
|
|
869
880
|
prompt: modelPrompt,
|
|
870
|
-
stopConditions: options.stopWhen,
|
|
881
|
+
stopConditions: (_j = options.stopWhen) != null ? _j : this.stopWhen,
|
|
871
882
|
maxSteps: options.maxSteps,
|
|
872
883
|
onStepFinish: mergedOnStepFinish,
|
|
873
884
|
onStepStart: mergedOnStepStart,
|
|
874
885
|
onError: options.onError,
|
|
875
|
-
prepareStep: (
|
|
886
|
+
prepareStep: (_k = options.prepareStep) != null ? _k : this.prepareStep,
|
|
876
887
|
generationSettings: mergedGenerationSettings,
|
|
877
888
|
toolChoice: effectiveToolChoice,
|
|
878
889
|
experimental_context: experimentalContext,
|
|
879
890
|
experimental_telemetry: effectiveTelemetry,
|
|
880
|
-
includeRawChunks: (
|
|
881
|
-
repairToolCall: options.experimental_repairToolCall,
|
|
882
|
-
responseFormat: await ((
|
|
891
|
+
includeRawChunks: (_l = options.includeRawChunks) != null ? _l : false,
|
|
892
|
+
repairToolCall: (_m = options.experimental_repairToolCall) != null ? _m : this.experimentalRepairToolCall,
|
|
893
|
+
responseFormat: await ((_o = (_n = options.output) != null ? _n : this.output) == null ? void 0 : _o.responseFormat)
|
|
883
894
|
});
|
|
884
895
|
let finalMessages;
|
|
885
896
|
let encounteredError;
|
|
@@ -887,7 +898,7 @@ var WorkflowAgent = class {
|
|
|
887
898
|
try {
|
|
888
899
|
let result = await iterator.next();
|
|
889
900
|
while (!result.done) {
|
|
890
|
-
if ((
|
|
901
|
+
if ((_p = mergedGenerationSettings.abortSignal) == null ? void 0 : _p.aborted) {
|
|
891
902
|
wasAborted = true;
|
|
892
903
|
if (options.onAbort) {
|
|
893
904
|
await options.onAbort({ steps });
|
|
@@ -980,8 +991,8 @@ var WorkflowAgent = class {
|
|
|
980
991
|
await mergedOnFinish({
|
|
981
992
|
steps,
|
|
982
993
|
messages: messages2,
|
|
983
|
-
text: (
|
|
984
|
-
finishReason: (
|
|
994
|
+
text: (_q = lastStep == null ? void 0 : lastStep.text) != null ? _q : "",
|
|
995
|
+
finishReason: (_r = lastStep == null ? void 0 : lastStep.finishReason) != null ? _r : "other",
|
|
985
996
|
totalUsage: aggregateUsage(steps),
|
|
986
997
|
experimental_context: experimentalContext,
|
|
987
998
|
output: void 0
|
|
@@ -1005,8 +1016,8 @@ var WorkflowAgent = class {
|
|
|
1005
1016
|
}
|
|
1006
1017
|
}
|
|
1007
1018
|
if (options.writable) {
|
|
1008
|
-
const sendFinish = (
|
|
1009
|
-
const preventClose = (
|
|
1019
|
+
const sendFinish = (_s = options.sendFinish) != null ? _s : true;
|
|
1020
|
+
const preventClose = (_t = options.preventClose) != null ? _t : false;
|
|
1010
1021
|
if (sendFinish || !preventClose) {
|
|
1011
1022
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1012
1023
|
}
|
|
@@ -1099,14 +1110,15 @@ var WorkflowAgent = class {
|
|
|
1099
1110
|
await options.onError({ error });
|
|
1100
1111
|
}
|
|
1101
1112
|
}
|
|
1102
|
-
const messages = finalMessages != null ? finalMessages :
|
|
1113
|
+
const messages = finalMessages != null ? finalMessages : prompt.messages;
|
|
1114
|
+
const effectiveOutput = (_u = options.output) != null ? _u : this.output;
|
|
1103
1115
|
let experimentalOutput = void 0;
|
|
1104
|
-
if (
|
|
1116
|
+
if (effectiveOutput && steps.length > 0) {
|
|
1105
1117
|
const lastStep = steps[steps.length - 1];
|
|
1106
1118
|
const text = lastStep.text;
|
|
1107
1119
|
if (text) {
|
|
1108
1120
|
try {
|
|
1109
|
-
experimentalOutput = await
|
|
1121
|
+
experimentalOutput = await effectiveOutput.parseCompleteOutput(
|
|
1110
1122
|
{ text },
|
|
1111
1123
|
{
|
|
1112
1124
|
response: lastStep.response,
|
|
@@ -1126,8 +1138,8 @@ var WorkflowAgent = class {
|
|
|
1126
1138
|
await mergedOnFinish({
|
|
1127
1139
|
steps,
|
|
1128
1140
|
messages,
|
|
1129
|
-
text: (
|
|
1130
|
-
finishReason: (
|
|
1141
|
+
text: (_v = lastStep == null ? void 0 : lastStep.text) != null ? _v : "",
|
|
1142
|
+
finishReason: (_w = lastStep == null ? void 0 : lastStep.finishReason) != null ? _w : "other",
|
|
1131
1143
|
totalUsage: aggregateUsage(steps),
|
|
1132
1144
|
experimental_context: experimentalContext,
|
|
1133
1145
|
output: experimentalOutput
|
|
@@ -1135,8 +1147,8 @@ var WorkflowAgent = class {
|
|
|
1135
1147
|
}
|
|
1136
1148
|
if (encounteredError) {
|
|
1137
1149
|
if (options.writable) {
|
|
1138
|
-
const sendFinish = (
|
|
1139
|
-
const preventClose = (
|
|
1150
|
+
const sendFinish = (_x = options.sendFinish) != null ? _x : true;
|
|
1151
|
+
const preventClose = (_y = options.preventClose) != null ? _y : false;
|
|
1140
1152
|
if (sendFinish || !preventClose) {
|
|
1141
1153
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1142
1154
|
}
|
|
@@ -1144,8 +1156,8 @@ var WorkflowAgent = class {
|
|
|
1144
1156
|
throw encounteredError;
|
|
1145
1157
|
}
|
|
1146
1158
|
if (options.writable) {
|
|
1147
|
-
const sendFinish = (
|
|
1148
|
-
const preventClose = (
|
|
1159
|
+
const sendFinish = (_z = options.sendFinish) != null ? _z : true;
|
|
1160
|
+
const preventClose = (_A = options.preventClose) != null ? _A : false;
|
|
1149
1161
|
if (sendFinish || !preventClose) {
|
|
1150
1162
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1151
1163
|
}
|