@voltagent/core 0.1.69 → 0.1.71
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/dist/index.d.ts +341 -145
- package/dist/index.js +402 -349
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +402 -349
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -167,7 +167,7 @@ type AgentTool = BaseTool;
|
|
|
167
167
|
/**
|
|
168
168
|
* Tool options for creating a new tool
|
|
169
169
|
*/
|
|
170
|
-
type ToolOptions<T extends ToolSchema = ToolSchema> = {
|
|
170
|
+
type ToolOptions<T extends ToolSchema = ToolSchema, O extends ToolSchema | undefined = undefined> = {
|
|
171
171
|
/**
|
|
172
172
|
* Unique identifier for the tool
|
|
173
173
|
*/
|
|
@@ -184,15 +184,19 @@ type ToolOptions<T extends ToolSchema = ToolSchema> = {
|
|
|
184
184
|
* Tool parameter schema
|
|
185
185
|
*/
|
|
186
186
|
parameters: T;
|
|
187
|
+
/**
|
|
188
|
+
* Tool output schema (optional)
|
|
189
|
+
*/
|
|
190
|
+
outputSchema?: O;
|
|
187
191
|
/**
|
|
188
192
|
* Function to execute when the tool is called
|
|
189
193
|
*/
|
|
190
|
-
execute: (args: z.infer<T>, options?: ToolExecuteOptions) => Promise<unknown>;
|
|
194
|
+
execute: (args: z.infer<T>, options?: ToolExecuteOptions) => Promise<O extends ToolSchema ? z.infer<O> : unknown>;
|
|
191
195
|
};
|
|
192
196
|
/**
|
|
193
197
|
* Tool class for defining tools that agents can use
|
|
194
198
|
*/
|
|
195
|
-
declare class Tool<T extends ToolSchema = ToolSchema> {
|
|
199
|
+
declare class Tool<T extends ToolSchema = ToolSchema, O extends ToolSchema | undefined = undefined> {
|
|
196
200
|
/**
|
|
197
201
|
* Unique identifier for the tool
|
|
198
202
|
*/
|
|
@@ -209,23 +213,28 @@ declare class Tool<T extends ToolSchema = ToolSchema> {
|
|
|
209
213
|
* Tool parameter schema
|
|
210
214
|
*/
|
|
211
215
|
readonly parameters: T;
|
|
216
|
+
/**
|
|
217
|
+
* Tool output schema
|
|
218
|
+
*/
|
|
219
|
+
readonly outputSchema?: O;
|
|
212
220
|
/**
|
|
213
221
|
* Function to execute when the tool is called
|
|
214
222
|
*/
|
|
215
|
-
readonly execute: (args: z.infer<T>, options?: ToolExecuteOptions) => Promise<unknown>;
|
|
223
|
+
readonly execute: (args: z.infer<T>, options?: ToolExecuteOptions) => Promise<O extends ToolSchema ? z.infer<O> : unknown>;
|
|
216
224
|
/**
|
|
217
225
|
* Create a new tool
|
|
218
226
|
*/
|
|
219
|
-
constructor(options: ToolOptions<T>);
|
|
227
|
+
constructor(options: ToolOptions<T, O>);
|
|
220
228
|
}
|
|
221
229
|
/**
|
|
222
230
|
* Helper function for creating a new tool
|
|
223
231
|
*/
|
|
224
|
-
declare
|
|
232
|
+
declare function createTool<T extends ToolSchema>(options: ToolOptions<T, undefined>): Tool<T, undefined>;
|
|
233
|
+
declare function createTool<T extends ToolSchema, O extends ToolSchema>(options: ToolOptions<T, O>): Tool<T, O>;
|
|
225
234
|
/**
|
|
226
235
|
* Alias for createTool function
|
|
227
236
|
*/
|
|
228
|
-
declare const tool:
|
|
237
|
+
declare const tool: typeof createTool;
|
|
229
238
|
|
|
230
239
|
/**
|
|
231
240
|
* The base input type for the workflow
|
|
@@ -1832,7 +1841,7 @@ type ModelDynamicValue<T> = T | DynamicValue<T>;
|
|
|
1832
1841
|
/**
|
|
1833
1842
|
* Enhanced dynamic value for tools that supports static or dynamic values
|
|
1834
1843
|
*/
|
|
1835
|
-
type ToolsDynamicValue = (Tool<any> | Toolkit)[] | DynamicValue<(Tool<any> | Toolkit)[]>;
|
|
1844
|
+
type ToolsDynamicValue = (Tool<any, any> | Toolkit)[] | DynamicValue<(Tool<any, any> | Toolkit)[]>;
|
|
1836
1845
|
/**
|
|
1837
1846
|
* Provider options type for LLM configurations
|
|
1838
1847
|
*/
|
|
@@ -2670,7 +2679,7 @@ type ToolExecuteOptions = {
|
|
|
2670
2679
|
*/
|
|
2671
2680
|
[key: string]: any;
|
|
2672
2681
|
};
|
|
2673
|
-
type BaseTool = Tool<any>;
|
|
2682
|
+
type BaseTool = Tool<any, any>;
|
|
2674
2683
|
type BaseToolCall = {
|
|
2675
2684
|
name: string;
|
|
2676
2685
|
arguments: Record<string, any>;
|
|
@@ -4636,7 +4645,8 @@ declare class Agent<TProvider extends {
|
|
|
4636
4645
|
name: string;
|
|
4637
4646
|
description: string;
|
|
4638
4647
|
parameters: any;
|
|
4639
|
-
|
|
4648
|
+
outputSchema?: any;
|
|
4649
|
+
execute: (args: any, options?: ToolExecuteOptions) => Promise<any>;
|
|
4640
4650
|
}[];
|
|
4641
4651
|
subAgents: {
|
|
4642
4652
|
node_id: string;
|
|
@@ -4659,10 +4669,6 @@ declare class Agent<TProvider extends {
|
|
|
4659
4669
|
* Add step to history immediately and to conversation steps
|
|
4660
4670
|
*/
|
|
4661
4671
|
private addStepToHistory;
|
|
4662
|
-
/**
|
|
4663
|
-
* Handle tool execution errors by creating appropriate events and steps
|
|
4664
|
-
*/
|
|
4665
|
-
private handleToolError;
|
|
4666
4672
|
/**
|
|
4667
4673
|
* Update history entry
|
|
4668
4674
|
*/
|
|
@@ -4783,11 +4789,14 @@ type AgentConfig$1<SCHEMA extends z.ZodTypeAny> = PublicGenerateOptions & {
|
|
|
4783
4789
|
* ```ts
|
|
4784
4790
|
* const w = createWorkflow(
|
|
4785
4791
|
* andAgent(
|
|
4786
|
-
* (data) => `Generate a greeting for the user ${data.name}`,
|
|
4792
|
+
* ({ data }) => `Generate a greeting for the user ${data.name}`,
|
|
4787
4793
|
* agent,
|
|
4788
|
-
*
|
|
4794
|
+
* { schema: z.object({ greeting: z.string() }) }
|
|
4789
4795
|
* ),
|
|
4790
|
-
* andThen(
|
|
4796
|
+
* andThen({
|
|
4797
|
+
* id: "extract-greeting",
|
|
4798
|
+
* execute: async ({ data }) => data.greeting
|
|
4799
|
+
* })
|
|
4791
4800
|
* );
|
|
4792
4801
|
* ```
|
|
4793
4802
|
*
|
|
@@ -4825,6 +4834,10 @@ type WorkflowStepFuncConfig<INPUT, DATA, RESULT, SUSPEND_DATA, RESUME_DATA = any
|
|
|
4825
4834
|
interface WorkflowStepFunc<INPUT, DATA, RESULT, SUSPEND_DATA, RESUME_DATA = any> extends InternalBaseWorkflowStep<INPUT, DATA, RESULT, SUSPEND_DATA, RESUME_DATA> {
|
|
4826
4835
|
type: "func";
|
|
4827
4836
|
}
|
|
4837
|
+
interface WorkflowStepWorkflow<INPUT, DATA, RESULT, SUSPEND_DATA, RESUME_DATA = any> extends InternalBaseWorkflowStep<INPUT, DATA, RESULT, SUSPEND_DATA, RESUME_DATA> {
|
|
4838
|
+
type: "workflow";
|
|
4839
|
+
workflow: InternalWorkflow<INPUT, DATA, RESULT>;
|
|
4840
|
+
}
|
|
4828
4841
|
type WorkflowStepTapConfig<INPUT, DATA, _RESULT, SUSPEND_DATA, RESUME_DATA = any> = InternalWorkflowStepConfig<{
|
|
4829
4842
|
execute: InternalWorkflowFunc<INPUT, DATA, DangerouslyAllowAny, SUSPEND_DATA, RESUME_DATA>;
|
|
4830
4843
|
inputSchema?: z.ZodTypeAny;
|
|
@@ -4860,7 +4873,21 @@ interface WorkflowStepParallelAll<INPUT, DATA, RESULT> extends InternalBaseWorkf
|
|
|
4860
4873
|
type: "parallel-all";
|
|
4861
4874
|
steps: ReadonlyArray<InternalAnyWorkflowStep<INPUT, DATA, RESULT>>;
|
|
4862
4875
|
}
|
|
4863
|
-
type WorkflowStep<INPUT, DATA, RESULT, SUSPEND_DATA = any> = WorkflowStepAgent<INPUT, DATA, RESULT> | WorkflowStepFunc<INPUT, DATA, RESULT, SUSPEND_DATA> | WorkflowStepConditionalWhen<INPUT, DATA, RESULT> | WorkflowStepParallelAll<INPUT, DATA, RESULT> | WorkflowStepTap<INPUT, DATA, RESULT, SUSPEND_DATA> | WorkflowStepParallelRace<INPUT, DATA, RESULT>;
|
|
4876
|
+
type WorkflowStep<INPUT, DATA, RESULT, SUSPEND_DATA = any> = WorkflowStepAgent<INPUT, DATA, RESULT> | WorkflowStepFunc<INPUT, DATA, RESULT, SUSPEND_DATA> | WorkflowStepConditionalWhen<INPUT, DATA, RESULT> | WorkflowStepParallelAll<INPUT, DATA, RESULT> | WorkflowStepTap<INPUT, DATA, RESULT, SUSPEND_DATA> | WorkflowStepParallelRace<INPUT, DATA, RESULT> | WorkflowStepWorkflow<INPUT, DATA, RESULT, SUSPEND_DATA>;
|
|
4877
|
+
/**
|
|
4878
|
+
* Internal type to allow overriding the run method for the workflow
|
|
4879
|
+
*/
|
|
4880
|
+
interface InternalWorkflow<_INPUT, DATA, RESULT> extends Omit<Workflow<DangerouslyAllowAny, DangerouslyAllowAny>, "run"> {
|
|
4881
|
+
run: (input: InternalExtractWorkflowInputData<DATA>, options?: InternalWorkflowRunOptions) => Promise<{
|
|
4882
|
+
executionId: string;
|
|
4883
|
+
startAt: Date;
|
|
4884
|
+
endAt: Date;
|
|
4885
|
+
status: "completed";
|
|
4886
|
+
result: RESULT;
|
|
4887
|
+
}>;
|
|
4888
|
+
}
|
|
4889
|
+
interface InternalWorkflowRunOptions extends WorkflowRunOptions {
|
|
4890
|
+
}
|
|
4864
4891
|
|
|
4865
4892
|
/**
|
|
4866
4893
|
* Creates an async function step for the workflow
|
|
@@ -4868,17 +4895,23 @@ type WorkflowStep<INPUT, DATA, RESULT, SUSPEND_DATA = any> = WorkflowStepAgent<I
|
|
|
4868
4895
|
* @example
|
|
4869
4896
|
* ```ts
|
|
4870
4897
|
* const w = createWorkflow(
|
|
4871
|
-
* andThen(
|
|
4872
|
-
*
|
|
4873
|
-
*
|
|
4898
|
+
* andThen({
|
|
4899
|
+
* id: "process-data",
|
|
4900
|
+
* execute: async ({ data }) => {
|
|
4901
|
+
* const processed = await someAsyncOperation(data.value);
|
|
4902
|
+
* return { ...data, processed };
|
|
4903
|
+
* }
|
|
4874
4904
|
* }),
|
|
4875
|
-
* andThen(
|
|
4876
|
-
*
|
|
4905
|
+
* andThen({
|
|
4906
|
+
* id: "format-result",
|
|
4907
|
+
* execute: async ({ data }) => {
|
|
4908
|
+
* return { result: `Processed: ${data.processed}` };
|
|
4909
|
+
* }
|
|
4877
4910
|
* })
|
|
4878
4911
|
* );
|
|
4879
4912
|
* ```
|
|
4880
4913
|
*
|
|
4881
|
-
* @param
|
|
4914
|
+
* @param config - Configuration object with execute function and metadata
|
|
4882
4915
|
* @returns A workflow step that executes the function and returns the result
|
|
4883
4916
|
*/
|
|
4884
4917
|
declare function andThen<INPUT, DATA, RESULT, SUSPEND_DATA = DangerouslyAllowAny, RESUME_DATA = DangerouslyAllowAny>({ execute, inputSchema, outputSchema, suspendSchema, resumeSchema, ...config }: WorkflowStepFuncConfig<INPUT, DATA, RESULT, SUSPEND_DATA, RESUME_DATA>): WorkflowStepFunc<INPUT, DATA, RESULT, SUSPEND_DATA, RESUME_DATA>;
|
|
@@ -4890,24 +4923,25 @@ declare function andThen<INPUT, DATA, RESULT, SUSPEND_DATA = DangerouslyAllowAny
|
|
|
4890
4923
|
* ```ts
|
|
4891
4924
|
* const w = createWorkflow(
|
|
4892
4925
|
* andWhen({
|
|
4893
|
-
*
|
|
4894
|
-
*
|
|
4926
|
+
* id: "admin-permissions",
|
|
4927
|
+
* condition: async ({ data }) => data.userType === "admin",
|
|
4928
|
+
* execute: async ({ data }) => {
|
|
4895
4929
|
* return { ...data, permissions: ["read", "write", "delete"] };
|
|
4896
|
-
* }
|
|
4930
|
+
* }
|
|
4897
4931
|
* }),
|
|
4898
4932
|
* andWhen({
|
|
4899
|
-
*
|
|
4900
|
-
*
|
|
4901
|
-
*
|
|
4933
|
+
* id: "high-value-processing",
|
|
4934
|
+
* condition: async ({ data }) => data.value > 100,
|
|
4935
|
+
* step: andAgent(
|
|
4936
|
+
* ({ data }) => `Process high value transaction: ${data.value}`,
|
|
4902
4937
|
* agent,
|
|
4903
4938
|
* { schema: z.object({ processed: z.boolean() }) }
|
|
4904
4939
|
* )
|
|
4905
|
-
* )
|
|
4940
|
+
* })
|
|
4906
4941
|
* );
|
|
4907
4942
|
* ```
|
|
4908
4943
|
*
|
|
4909
|
-
* @param
|
|
4910
|
-
* @param stepOrFunc - Either a workflow step or an agent to execute when the condition is true
|
|
4944
|
+
* @param config - Configuration object with condition, step/execute function, and metadata
|
|
4911
4945
|
* @returns A conditional workflow step that executes the step only when the condition evaluates to true
|
|
4912
4946
|
*/
|
|
4913
4947
|
declare function andWhen<INPUT, DATA, RESULT>({ condition, step, inputSchema, outputSchema, suspendSchema, resumeSchema, ...config }: WorkflowStepConditionalWhenConfig<INPUT, DATA, RESULT>): WorkflowStepConditionalWhen<INPUT, DATA, RESULT>;
|
|
@@ -4918,29 +4952,41 @@ declare function andWhen<INPUT, DATA, RESULT>({ condition, step, inputSchema, ou
|
|
|
4918
4952
|
* @example
|
|
4919
4953
|
* ```ts
|
|
4920
4954
|
* const w = createWorkflow(
|
|
4921
|
-
* andAll(
|
|
4922
|
-
*
|
|
4923
|
-
*
|
|
4924
|
-
*
|
|
4925
|
-
*
|
|
4926
|
-
*
|
|
4927
|
-
*
|
|
4928
|
-
*
|
|
4929
|
-
*
|
|
4930
|
-
*
|
|
4931
|
-
* (
|
|
4932
|
-
*
|
|
4933
|
-
*
|
|
4934
|
-
*
|
|
4935
|
-
*
|
|
4936
|
-
*
|
|
4937
|
-
*
|
|
4938
|
-
*
|
|
4955
|
+
* andAll({
|
|
4956
|
+
* id: "parallel-fetch",
|
|
4957
|
+
* steps: [
|
|
4958
|
+
* andThen({
|
|
4959
|
+
* id: "fetch-user",
|
|
4960
|
+
* execute: async ({ data }) => {
|
|
4961
|
+
* const userInfo = await fetchUserInfo(data.userId);
|
|
4962
|
+
* return { userInfo };
|
|
4963
|
+
* }
|
|
4964
|
+
* }),
|
|
4965
|
+
* andThen({
|
|
4966
|
+
* id: "fetch-permissions",
|
|
4967
|
+
* execute: async ({ data }) => {
|
|
4968
|
+
* const permissions = await fetchPermissions(data.userId);
|
|
4969
|
+
* return { permissions };
|
|
4970
|
+
* }
|
|
4971
|
+
* }),
|
|
4972
|
+
* andAgent(
|
|
4973
|
+
* ({ data }) => `Generate recommendations for user ${data.userId}`,
|
|
4974
|
+
* agent,
|
|
4975
|
+
* { schema: z.object({ recommendations: z.array(z.string()) }) }
|
|
4976
|
+
* )
|
|
4977
|
+
* ]
|
|
4978
|
+
* }),
|
|
4979
|
+
* andThen({
|
|
4980
|
+
* id: "combine-results",
|
|
4981
|
+
* execute: async ({ data }) => {
|
|
4982
|
+
* // data is now an array: [{ userInfo }, { permissions }, { recommendations }]
|
|
4983
|
+
* return { combined: data.flat() };
|
|
4984
|
+
* }
|
|
4939
4985
|
* })
|
|
4940
4986
|
* );
|
|
4941
4987
|
* ```
|
|
4942
4988
|
*
|
|
4943
|
-
* @param
|
|
4989
|
+
* @param config - Configuration object with steps array and metadata
|
|
4944
4990
|
* @returns A workflow step that executes all steps simultaneously and returns their results as an array
|
|
4945
4991
|
*/
|
|
4946
4992
|
declare function andAll<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<InternalAnyWorkflowStep<INPUT, DATA, RESULT>>, INFERRED_RESULT = InternalInferWorkflowStepsResult<STEPS>>({ steps, ...config }: WorkflowStepParallelAllConfig<STEPS>): {
|
|
@@ -4958,31 +5004,43 @@ declare function andAll<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<Interna
|
|
|
4958
5004
|
* @example
|
|
4959
5005
|
* ```ts
|
|
4960
5006
|
* const w = createWorkflow(
|
|
4961
|
-
* andRace(
|
|
4962
|
-
*
|
|
4963
|
-
*
|
|
4964
|
-
*
|
|
4965
|
-
*
|
|
4966
|
-
*
|
|
4967
|
-
*
|
|
4968
|
-
*
|
|
4969
|
-
*
|
|
4970
|
-
*
|
|
4971
|
-
*
|
|
4972
|
-
*
|
|
4973
|
-
*
|
|
4974
|
-
*
|
|
4975
|
-
*
|
|
4976
|
-
*
|
|
4977
|
-
*
|
|
4978
|
-
*
|
|
4979
|
-
*
|
|
4980
|
-
*
|
|
5007
|
+
* andRace({
|
|
5008
|
+
* id: "race-data-sources",
|
|
5009
|
+
* steps: [
|
|
5010
|
+
* andThen({
|
|
5011
|
+
* id: "check-cache",
|
|
5012
|
+
* execute: async ({ data }) => {
|
|
5013
|
+
* // Fast operation
|
|
5014
|
+
* const cacheResult = await checkCache(data.query);
|
|
5015
|
+
* return { source: "cache", result: cacheResult };
|
|
5016
|
+
* }
|
|
5017
|
+
* }),
|
|
5018
|
+
* andThen({
|
|
5019
|
+
* id: "query-database",
|
|
5020
|
+
* execute: async ({ data }) => {
|
|
5021
|
+
* // Slower operation
|
|
5022
|
+
* const dbResult = await queryDatabase(data.query);
|
|
5023
|
+
* return { source: "database", result: dbResult };
|
|
5024
|
+
* }
|
|
5025
|
+
* }),
|
|
5026
|
+
* andAgent(
|
|
5027
|
+
* ({ data }) => `Generate fallback response for: ${data.query}`,
|
|
5028
|
+
* agent,
|
|
5029
|
+
* { schema: z.object({ source: z.literal("ai"), result: z.string() }) }
|
|
5030
|
+
* )
|
|
5031
|
+
* ]
|
|
5032
|
+
* }),
|
|
5033
|
+
* andThen({
|
|
5034
|
+
* id: "process-result",
|
|
5035
|
+
* execute: async ({ data }) => {
|
|
5036
|
+
* // data is the result from whichever step completed first
|
|
5037
|
+
* return { finalResult: data.result, source: data.source };
|
|
5038
|
+
* }
|
|
4981
5039
|
* })
|
|
4982
5040
|
* );
|
|
4983
5041
|
* ```
|
|
4984
5042
|
*
|
|
4985
|
-
* @param
|
|
5043
|
+
* @param config - Configuration object with steps array and metadata
|
|
4986
5044
|
* @returns A workflow step that executes all steps simultaneously and returns the result from the first step to complete
|
|
4987
5045
|
*/
|
|
4988
5046
|
declare function andRace<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<InternalAnyWorkflowStep<INPUT, DATA, RESULT>>, INFERRED_RESULT = InternalInferWorkflowStepsResult<STEPS>[number]>({ steps, ...config }: InternalWorkflowStepConfig<{
|
|
@@ -4998,7 +5056,27 @@ declare function andRace<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<Intern
|
|
|
4998
5056
|
|
|
4999
5057
|
/**
|
|
5000
5058
|
* A safe way to tap into the workflow state without affecting the result.
|
|
5001
|
-
*
|
|
5059
|
+
*
|
|
5060
|
+
* @example
|
|
5061
|
+
* ```ts
|
|
5062
|
+
* const w = createWorkflow(
|
|
5063
|
+
* andTap({
|
|
5064
|
+
* id: "log-processing",
|
|
5065
|
+
* execute: async ({ data }) => {
|
|
5066
|
+
* console.log("Processing data:", data);
|
|
5067
|
+
* }
|
|
5068
|
+
* }),
|
|
5069
|
+
* andThen({
|
|
5070
|
+
* id: "process-data",
|
|
5071
|
+
* execute: async ({ data }) => {
|
|
5072
|
+
* // data is unchanged from the tap step
|
|
5073
|
+
* return { ...data, processed: true };
|
|
5074
|
+
* }
|
|
5075
|
+
* })
|
|
5076
|
+
* );
|
|
5077
|
+
* ```
|
|
5078
|
+
*
|
|
5079
|
+
* @param config - Configuration object with execute function and metadata
|
|
5002
5080
|
* @returns A workflow step that executes the function
|
|
5003
5081
|
*/
|
|
5004
5082
|
declare function andTap<INPUT, DATA, RESULT, SUSPEND_DATA = DangerouslyAllowAny, RESUME_DATA = DangerouslyAllowAny>({ execute, inputSchema, suspendSchema, resumeSchema, ...config }: WorkflowStepTapConfig<INPUT, DATA, RESULT, SUSPEND_DATA, RESUME_DATA>): {
|
|
@@ -5014,6 +5092,50 @@ declare function andTap<INPUT, DATA, RESULT, SUSPEND_DATA = DangerouslyAllowAny,
|
|
|
5014
5092
|
|
|
5015
5093
|
/**
|
|
5016
5094
|
* Creates a workflow from multiple and* functions
|
|
5095
|
+
*
|
|
5096
|
+
* @example
|
|
5097
|
+
* ```ts
|
|
5098
|
+
* const workflow = createWorkflow({
|
|
5099
|
+
* id: "user-processing",
|
|
5100
|
+
* name: "User Processing Workflow",
|
|
5101
|
+
* purpose: "Process user data and generate personalized content",
|
|
5102
|
+
* input: z.object({ userId: z.string(), userType: z.enum(["admin", "user"]) }),
|
|
5103
|
+
* result: z.object({ processed: z.boolean(), content: z.string() }),
|
|
5104
|
+
* memory: new LibSQLStorage({ url: "file:memory.db" }) // Optional workflow-specific memory
|
|
5105
|
+
* },
|
|
5106
|
+
* andThen({
|
|
5107
|
+
* id: "fetch-user",
|
|
5108
|
+
* execute: async ({ data }) => {
|
|
5109
|
+
* const userInfo = await fetchUserInfo(data.userId);
|
|
5110
|
+
* return { ...data, userInfo };
|
|
5111
|
+
* }
|
|
5112
|
+
* }),
|
|
5113
|
+
* andWhen({
|
|
5114
|
+
* id: "admin-permissions",
|
|
5115
|
+
* condition: async ({ data }) => data.userType === "admin",
|
|
5116
|
+
* execute: async ({ data }) => ({ ...data, permissions: ["read", "write", "delete"] })
|
|
5117
|
+
* }),
|
|
5118
|
+
* andAgent(
|
|
5119
|
+
* ({ data }) => `Generate personalized content for ${data.userInfo.name}`,
|
|
5120
|
+
* agent,
|
|
5121
|
+
* { schema: z.object({ content: z.string() }) }
|
|
5122
|
+
* ),
|
|
5123
|
+
* andThen({
|
|
5124
|
+
* id: "finalize-result",
|
|
5125
|
+
* execute: async ({ data }) => ({
|
|
5126
|
+
* processed: true,
|
|
5127
|
+
* content: data.content
|
|
5128
|
+
* })
|
|
5129
|
+
* })
|
|
5130
|
+
* );
|
|
5131
|
+
*
|
|
5132
|
+
* // Run with optional memory override
|
|
5133
|
+
* const result = await workflow.run(
|
|
5134
|
+
* { userId: "123", userType: "admin" },
|
|
5135
|
+
* { memory: new LibSQLStorage({ url: "file:memory.db" }) }
|
|
5136
|
+
* );
|
|
5137
|
+
* ```
|
|
5138
|
+
*
|
|
5017
5139
|
* @param config - The workflow configuration
|
|
5018
5140
|
* @param steps - Variable number of and* functions to execute
|
|
5019
5141
|
* @returns A configured workflow instance
|
|
@@ -5058,23 +5180,30 @@ type AgentConfig<SCHEMA extends z.ZodTypeAny> = {
|
|
|
5058
5180
|
* result: z.object({ processed: z.boolean(), content: z.string() }),
|
|
5059
5181
|
* memory: new LibSQLStorage({ url: "file:memory.db" }) // Optional workflow-specific memory
|
|
5060
5182
|
* })
|
|
5061
|
-
* .andThen(
|
|
5062
|
-
*
|
|
5063
|
-
*
|
|
5183
|
+
* .andThen({
|
|
5184
|
+
* id: "fetch-user",
|
|
5185
|
+
* execute: async ({ data }) => {
|
|
5186
|
+
* const userInfo = await fetchUserInfo(data.userId);
|
|
5187
|
+
* return { ...data, userInfo };
|
|
5188
|
+
* }
|
|
5189
|
+
* })
|
|
5190
|
+
* .andWhen({
|
|
5191
|
+
* id: "admin-permissions",
|
|
5192
|
+
* condition: async ({ data }) => data.userType === "admin",
|
|
5193
|
+
* execute: async ({ data }) => ({ ...data, permissions: ["read", "write", "delete"] })
|
|
5064
5194
|
* })
|
|
5065
|
-
* .andWhen(
|
|
5066
|
-
* (data) => data.userType === "admin",
|
|
5067
|
-
* async (data) => ({ ...data, permissions: ["read", "write", "delete"] })
|
|
5068
|
-
* )
|
|
5069
5195
|
* .andAgent(
|
|
5070
|
-
* (data) => `Generate personalized content for ${data.userInfo.name}`,
|
|
5196
|
+
* ({ data }) => `Generate personalized content for ${data.userInfo.name}`,
|
|
5071
5197
|
* agent,
|
|
5072
5198
|
* { schema: z.object({ content: z.string() }) }
|
|
5073
5199
|
* )
|
|
5074
|
-
* .andThen(
|
|
5075
|
-
*
|
|
5076
|
-
*
|
|
5077
|
-
*
|
|
5200
|
+
* .andThen({
|
|
5201
|
+
* id: "finalize-result",
|
|
5202
|
+
* execute: async ({ data }) => ({
|
|
5203
|
+
* processed: true,
|
|
5204
|
+
* content: data.content
|
|
5205
|
+
* })
|
|
5206
|
+
* });
|
|
5078
5207
|
*
|
|
5079
5208
|
* // Run with optional memory override
|
|
5080
5209
|
* const result = await workflow.run(
|
|
@@ -5092,13 +5221,20 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5092
5221
|
*
|
|
5093
5222
|
* @example
|
|
5094
5223
|
* ```ts
|
|
5095
|
-
* const w = createWorkflowChain
|
|
5224
|
+
* const w = createWorkflowChain({
|
|
5225
|
+
* id: "greeting-workflow",
|
|
5226
|
+
* input: z.object({ name: z.string() }),
|
|
5227
|
+
* result: z.string()
|
|
5228
|
+
* })
|
|
5096
5229
|
* .andAgent(
|
|
5097
|
-
* (data) => `Generate a greeting for the user ${data.name}`,
|
|
5230
|
+
* ({ data }) => `Generate a greeting for the user ${data.name}`,
|
|
5098
5231
|
* agent,
|
|
5099
5232
|
* { schema: z.object({ greeting: z.string() }) }
|
|
5100
5233
|
* )
|
|
5101
|
-
* .andThen(
|
|
5234
|
+
* .andThen({
|
|
5235
|
+
* id: "extract-greeting",
|
|
5236
|
+
* execute: async ({ data }) => data.greeting
|
|
5237
|
+
* })
|
|
5102
5238
|
* ```
|
|
5103
5239
|
*
|
|
5104
5240
|
* @param task - The task (prompt) to execute for the agent, can be a string or a function that returns a string
|
|
@@ -5276,22 +5412,27 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5276
5412
|
* @example
|
|
5277
5413
|
* ```ts
|
|
5278
5414
|
* const workflow = createWorkflowChain(config)
|
|
5279
|
-
* .andWhen(
|
|
5280
|
-
*
|
|
5281
|
-
* async (data) =>
|
|
5282
|
-
*
|
|
5283
|
-
*
|
|
5284
|
-
*
|
|
5285
|
-
*
|
|
5286
|
-
*
|
|
5287
|
-
*
|
|
5288
|
-
*
|
|
5289
|
-
*
|
|
5290
|
-
*
|
|
5291
|
-
*
|
|
5292
|
-
*
|
|
5293
|
-
*
|
|
5294
|
-
*
|
|
5415
|
+
* .andWhen({
|
|
5416
|
+
* id: "admin-permissions",
|
|
5417
|
+
* condition: async ({ data }) => data.userType === "admin",
|
|
5418
|
+
* execute: async ({ data }) => ({ ...data, permissions: ["read", "write", "delete"] })
|
|
5419
|
+
* })
|
|
5420
|
+
* .andWhen({
|
|
5421
|
+
* id: "high-value-flag",
|
|
5422
|
+
* condition: async ({ data }) => data.value > 1000,
|
|
5423
|
+
* execute: async ({ data }) => ({ ...data, flagged: true, requiresReview: true })
|
|
5424
|
+
* })
|
|
5425
|
+
* .andWhen({
|
|
5426
|
+
* id: "process-pending",
|
|
5427
|
+
* condition: async ({ data }) => data.status === "pending",
|
|
5428
|
+
* execute: async ({ data }) => {
|
|
5429
|
+
* const result = await agent.generateObject(
|
|
5430
|
+
* `Process pending request for ${data.userId}`,
|
|
5431
|
+
* z.object({ processed: z.boolean() })
|
|
5432
|
+
* );
|
|
5433
|
+
* return { ...data, ...result.object };
|
|
5434
|
+
* }
|
|
5435
|
+
* });
|
|
5295
5436
|
* ```
|
|
5296
5437
|
*
|
|
5297
5438
|
* @param condition - Function that determines if the step should execute based on the current data
|
|
@@ -5334,13 +5475,15 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5334
5475
|
* ```ts
|
|
5335
5476
|
* const workflow = createWorkflowChain(config)
|
|
5336
5477
|
* .andTap({
|
|
5337
|
-
*
|
|
5478
|
+
* id: "log-translation",
|
|
5479
|
+
* execute: async ({ data }) => {
|
|
5338
5480
|
* console.log("🔄 Translating text:", data);
|
|
5339
5481
|
* }
|
|
5340
5482
|
* })
|
|
5341
5483
|
* .andThen({
|
|
5484
|
+
* id: "return-translation",
|
|
5342
5485
|
* // the input data is still the same as the andTap ONLY executes, it doesn't return anything
|
|
5343
|
-
* execute: async (data) => {
|
|
5486
|
+
* execute: async ({ data }) => {
|
|
5344
5487
|
* return { ...data, translatedText: data.translatedText };
|
|
5345
5488
|
* }
|
|
5346
5489
|
* });
|
|
@@ -5349,7 +5492,7 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5349
5492
|
* @param fn - The async function to execute with the current workflow data
|
|
5350
5493
|
* @returns A new chain with the tap step added
|
|
5351
5494
|
*/
|
|
5352
|
-
andTap<
|
|
5495
|
+
andTap<_NEW_DATA>(config: {
|
|
5353
5496
|
execute: (context: {
|
|
5354
5497
|
data: CURRENT_DATA;
|
|
5355
5498
|
state: any;
|
|
@@ -5368,29 +5511,65 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5368
5511
|
resumeSchema?: z.ZodTypeAny;
|
|
5369
5512
|
}): WorkflowChain<INPUT_SCHEMA, RESULT_SCHEMA, CURRENT_DATA, SUSPEND_SCHEMA, RESUME_SCHEMA>;
|
|
5370
5513
|
/**
|
|
5371
|
-
* Add a
|
|
5514
|
+
* Add a workflow step to the workflow
|
|
5372
5515
|
*
|
|
5373
5516
|
* @example
|
|
5374
5517
|
* ```ts
|
|
5518
|
+
* import { myWorkflow } from "./my-workflow";
|
|
5519
|
+
*
|
|
5375
5520
|
* const workflow = createWorkflowChain(config)
|
|
5376
|
-
* .
|
|
5377
|
-
*
|
|
5521
|
+
* .andThen({
|
|
5522
|
+
* id: "fetch-user",
|
|
5523
|
+
* execute: async ({ data }) => {
|
|
5378
5524
|
* const userInfo = await fetchUserInfo(data.userId);
|
|
5379
5525
|
* return { userInfo };
|
|
5380
|
-
* }
|
|
5381
|
-
*
|
|
5382
|
-
*
|
|
5383
|
-
*
|
|
5384
|
-
|
|
5385
|
-
|
|
5386
|
-
|
|
5387
|
-
*
|
|
5388
|
-
*
|
|
5389
|
-
*
|
|
5390
|
-
*
|
|
5391
|
-
*
|
|
5392
|
-
*
|
|
5393
|
-
*
|
|
5526
|
+
* }
|
|
5527
|
+
* })
|
|
5528
|
+
* .andWorkflow(myWorkflow)
|
|
5529
|
+
* ```
|
|
5530
|
+
*/
|
|
5531
|
+
andWorkflow<NEW_DATA>(workflow: InternalWorkflow<INPUT_SCHEMA, CURRENT_DATA, NEW_DATA>): WorkflowChain<INPUT_SCHEMA, RESULT_SCHEMA, NEW_DATA>;
|
|
5532
|
+
/**
|
|
5533
|
+
* Add a parallel execution step that runs multiple steps simultaneously and waits for all to complete
|
|
5534
|
+
*
|
|
5535
|
+
* @example
|
|
5536
|
+
* ```ts
|
|
5537
|
+
* const workflow = createWorkflowChain(config)
|
|
5538
|
+
* .andAll({
|
|
5539
|
+
* id: "parallel-fetch",
|
|
5540
|
+
* steps: [
|
|
5541
|
+
* {
|
|
5542
|
+
* id: "fetch-user",
|
|
5543
|
+
* execute: async ({ data }) => {
|
|
5544
|
+
* const userInfo = await fetchUserInfo(data.userId);
|
|
5545
|
+
* return { userInfo };
|
|
5546
|
+
* }
|
|
5547
|
+
* },
|
|
5548
|
+
* {
|
|
5549
|
+
* id: "fetch-permissions",
|
|
5550
|
+
* execute: async ({ data }) => {
|
|
5551
|
+
* const permissions = await fetchPermissions(data.userId);
|
|
5552
|
+
* return { permissions };
|
|
5553
|
+
* }
|
|
5554
|
+
* },
|
|
5555
|
+
* {
|
|
5556
|
+
* id: "generate-recommendations",
|
|
5557
|
+
* execute: async ({ data }) => {
|
|
5558
|
+
* const result = await agent.generateObject(
|
|
5559
|
+
* `Generate recommendations for user ${data.userId}`,
|
|
5560
|
+
* z.object({ recommendations: z.array(z.string()) })
|
|
5561
|
+
* );
|
|
5562
|
+
* return result.object;
|
|
5563
|
+
* }
|
|
5564
|
+
* }
|
|
5565
|
+
* ]
|
|
5566
|
+
* })
|
|
5567
|
+
* .andThen({
|
|
5568
|
+
* id: "combine-results",
|
|
5569
|
+
* execute: async ({ data }) => {
|
|
5570
|
+
* // data is now an array: [{ userInfo }, { permissions }, { recommendations }]
|
|
5571
|
+
* return { combined: data.flat() };
|
|
5572
|
+
* }
|
|
5394
5573
|
* });
|
|
5395
5574
|
* ```
|
|
5396
5575
|
*
|
|
@@ -5404,26 +5583,43 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5404
5583
|
* @example
|
|
5405
5584
|
* ```ts
|
|
5406
5585
|
* const workflow = createWorkflowChain(config)
|
|
5407
|
-
* .andRace(
|
|
5408
|
-
*
|
|
5409
|
-
*
|
|
5410
|
-
*
|
|
5411
|
-
*
|
|
5412
|
-
*
|
|
5413
|
-
*
|
|
5414
|
-
*
|
|
5415
|
-
*
|
|
5416
|
-
*
|
|
5417
|
-
*
|
|
5418
|
-
*
|
|
5419
|
-
*
|
|
5420
|
-
*
|
|
5421
|
-
*
|
|
5422
|
-
*
|
|
5423
|
-
*
|
|
5424
|
-
*
|
|
5425
|
-
*
|
|
5426
|
-
*
|
|
5586
|
+
* .andRace({
|
|
5587
|
+
* id: "race-data-sources",
|
|
5588
|
+
* steps: [
|
|
5589
|
+
* {
|
|
5590
|
+
* id: "check-cache",
|
|
5591
|
+
* execute: async ({ data }) => {
|
|
5592
|
+
* // Fast operation
|
|
5593
|
+
* const cacheResult = await checkCache(data.query);
|
|
5594
|
+
* return { source: "cache", result: cacheResult };
|
|
5595
|
+
* }
|
|
5596
|
+
* },
|
|
5597
|
+
* {
|
|
5598
|
+
* id: "query-database",
|
|
5599
|
+
* execute: async ({ data }) => {
|
|
5600
|
+
* // Slower operation
|
|
5601
|
+
* const dbResult = await queryDatabase(data.query);
|
|
5602
|
+
* return { source: "database", result: dbResult };
|
|
5603
|
+
* }
|
|
5604
|
+
* },
|
|
5605
|
+
* {
|
|
5606
|
+
* id: "ai-fallback",
|
|
5607
|
+
* execute: async ({ data }) => {
|
|
5608
|
+
* const result = await agent.generateObject(
|
|
5609
|
+
* `Generate fallback response for: ${data.query}`,
|
|
5610
|
+
* z.object({ source: z.literal("ai"), result: z.string() })
|
|
5611
|
+
* );
|
|
5612
|
+
* return result.object;
|
|
5613
|
+
* }
|
|
5614
|
+
* }
|
|
5615
|
+
* ]
|
|
5616
|
+
* })
|
|
5617
|
+
* .andThen({
|
|
5618
|
+
* id: "process-result",
|
|
5619
|
+
* execute: async ({ data }) => {
|
|
5620
|
+
* // data is the result from whichever step completed first
|
|
5621
|
+
* return { finalResult: data.result, source: data.source };
|
|
5622
|
+
* }
|
|
5427
5623
|
* });
|
|
5428
5624
|
* ```
|
|
5429
5625
|
*
|