@voltagent/core 0.1.70 → 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 +310 -144
- package/dist/index.js +340 -232
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +340 -232
- 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;
|
|
@@ -4779,11 +4789,14 @@ type AgentConfig$1<SCHEMA extends z.ZodTypeAny> = PublicGenerateOptions & {
|
|
|
4779
4789
|
* ```ts
|
|
4780
4790
|
* const w = createWorkflow(
|
|
4781
4791
|
* andAgent(
|
|
4782
|
-
* (data) => `Generate a greeting for the user ${data.name}`,
|
|
4792
|
+
* ({ data }) => `Generate a greeting for the user ${data.name}`,
|
|
4783
4793
|
* agent,
|
|
4784
|
-
*
|
|
4794
|
+
* { schema: z.object({ greeting: z.string() }) }
|
|
4785
4795
|
* ),
|
|
4786
|
-
* andThen(
|
|
4796
|
+
* andThen({
|
|
4797
|
+
* id: "extract-greeting",
|
|
4798
|
+
* execute: async ({ data }) => data.greeting
|
|
4799
|
+
* })
|
|
4787
4800
|
* );
|
|
4788
4801
|
* ```
|
|
4789
4802
|
*
|
|
@@ -4882,17 +4895,23 @@ interface InternalWorkflowRunOptions extends WorkflowRunOptions {
|
|
|
4882
4895
|
* @example
|
|
4883
4896
|
* ```ts
|
|
4884
4897
|
* const w = createWorkflow(
|
|
4885
|
-
* andThen(
|
|
4886
|
-
*
|
|
4887
|
-
*
|
|
4898
|
+
* andThen({
|
|
4899
|
+
* id: "process-data",
|
|
4900
|
+
* execute: async ({ data }) => {
|
|
4901
|
+
* const processed = await someAsyncOperation(data.value);
|
|
4902
|
+
* return { ...data, processed };
|
|
4903
|
+
* }
|
|
4888
4904
|
* }),
|
|
4889
|
-
* andThen(
|
|
4890
|
-
*
|
|
4905
|
+
* andThen({
|
|
4906
|
+
* id: "format-result",
|
|
4907
|
+
* execute: async ({ data }) => {
|
|
4908
|
+
* return { result: `Processed: ${data.processed}` };
|
|
4909
|
+
* }
|
|
4891
4910
|
* })
|
|
4892
4911
|
* );
|
|
4893
4912
|
* ```
|
|
4894
4913
|
*
|
|
4895
|
-
* @param
|
|
4914
|
+
* @param config - Configuration object with execute function and metadata
|
|
4896
4915
|
* @returns A workflow step that executes the function and returns the result
|
|
4897
4916
|
*/
|
|
4898
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>;
|
|
@@ -4904,24 +4923,25 @@ declare function andThen<INPUT, DATA, RESULT, SUSPEND_DATA = DangerouslyAllowAny
|
|
|
4904
4923
|
* ```ts
|
|
4905
4924
|
* const w = createWorkflow(
|
|
4906
4925
|
* andWhen({
|
|
4907
|
-
*
|
|
4908
|
-
*
|
|
4926
|
+
* id: "admin-permissions",
|
|
4927
|
+
* condition: async ({ data }) => data.userType === "admin",
|
|
4928
|
+
* execute: async ({ data }) => {
|
|
4909
4929
|
* return { ...data, permissions: ["read", "write", "delete"] };
|
|
4910
|
-
* }
|
|
4930
|
+
* }
|
|
4911
4931
|
* }),
|
|
4912
4932
|
* andWhen({
|
|
4913
|
-
*
|
|
4914
|
-
*
|
|
4915
|
-
*
|
|
4933
|
+
* id: "high-value-processing",
|
|
4934
|
+
* condition: async ({ data }) => data.value > 100,
|
|
4935
|
+
* step: andAgent(
|
|
4936
|
+
* ({ data }) => `Process high value transaction: ${data.value}`,
|
|
4916
4937
|
* agent,
|
|
4917
4938
|
* { schema: z.object({ processed: z.boolean() }) }
|
|
4918
4939
|
* )
|
|
4919
|
-
* )
|
|
4940
|
+
* })
|
|
4920
4941
|
* );
|
|
4921
4942
|
* ```
|
|
4922
4943
|
*
|
|
4923
|
-
* @param
|
|
4924
|
-
* @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
|
|
4925
4945
|
* @returns A conditional workflow step that executes the step only when the condition evaluates to true
|
|
4926
4946
|
*/
|
|
4927
4947
|
declare function andWhen<INPUT, DATA, RESULT>({ condition, step, inputSchema, outputSchema, suspendSchema, resumeSchema, ...config }: WorkflowStepConditionalWhenConfig<INPUT, DATA, RESULT>): WorkflowStepConditionalWhen<INPUT, DATA, RESULT>;
|
|
@@ -4932,29 +4952,41 @@ declare function andWhen<INPUT, DATA, RESULT>({ condition, step, inputSchema, ou
|
|
|
4932
4952
|
* @example
|
|
4933
4953
|
* ```ts
|
|
4934
4954
|
* const w = createWorkflow(
|
|
4935
|
-
* andAll(
|
|
4936
|
-
*
|
|
4937
|
-
*
|
|
4938
|
-
*
|
|
4939
|
-
*
|
|
4940
|
-
*
|
|
4941
|
-
*
|
|
4942
|
-
*
|
|
4943
|
-
*
|
|
4944
|
-
*
|
|
4945
|
-
* (
|
|
4946
|
-
*
|
|
4947
|
-
*
|
|
4948
|
-
*
|
|
4949
|
-
*
|
|
4950
|
-
*
|
|
4951
|
-
*
|
|
4952
|
-
*
|
|
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
|
+
* }
|
|
4953
4985
|
* })
|
|
4954
4986
|
* );
|
|
4955
4987
|
* ```
|
|
4956
4988
|
*
|
|
4957
|
-
* @param
|
|
4989
|
+
* @param config - Configuration object with steps array and metadata
|
|
4958
4990
|
* @returns A workflow step that executes all steps simultaneously and returns their results as an array
|
|
4959
4991
|
*/
|
|
4960
4992
|
declare function andAll<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<InternalAnyWorkflowStep<INPUT, DATA, RESULT>>, INFERRED_RESULT = InternalInferWorkflowStepsResult<STEPS>>({ steps, ...config }: WorkflowStepParallelAllConfig<STEPS>): {
|
|
@@ -4972,31 +5004,43 @@ declare function andAll<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<Interna
|
|
|
4972
5004
|
* @example
|
|
4973
5005
|
* ```ts
|
|
4974
5006
|
* const w = createWorkflow(
|
|
4975
|
-
* andRace(
|
|
4976
|
-
*
|
|
4977
|
-
*
|
|
4978
|
-
*
|
|
4979
|
-
*
|
|
4980
|
-
*
|
|
4981
|
-
*
|
|
4982
|
-
*
|
|
4983
|
-
*
|
|
4984
|
-
*
|
|
4985
|
-
*
|
|
4986
|
-
*
|
|
4987
|
-
*
|
|
4988
|
-
*
|
|
4989
|
-
*
|
|
4990
|
-
*
|
|
4991
|
-
*
|
|
4992
|
-
*
|
|
4993
|
-
*
|
|
4994
|
-
*
|
|
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
|
+
* }
|
|
4995
5039
|
* })
|
|
4996
5040
|
* );
|
|
4997
5041
|
* ```
|
|
4998
5042
|
*
|
|
4999
|
-
* @param
|
|
5043
|
+
* @param config - Configuration object with steps array and metadata
|
|
5000
5044
|
* @returns A workflow step that executes all steps simultaneously and returns the result from the first step to complete
|
|
5001
5045
|
*/
|
|
5002
5046
|
declare function andRace<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<InternalAnyWorkflowStep<INPUT, DATA, RESULT>>, INFERRED_RESULT = InternalInferWorkflowStepsResult<STEPS>[number]>({ steps, ...config }: InternalWorkflowStepConfig<{
|
|
@@ -5012,7 +5056,27 @@ declare function andRace<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<Intern
|
|
|
5012
5056
|
|
|
5013
5057
|
/**
|
|
5014
5058
|
* A safe way to tap into the workflow state without affecting the result.
|
|
5015
|
-
*
|
|
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
|
|
5016
5080
|
* @returns A workflow step that executes the function
|
|
5017
5081
|
*/
|
|
5018
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>): {
|
|
@@ -5028,6 +5092,50 @@ declare function andTap<INPUT, DATA, RESULT, SUSPEND_DATA = DangerouslyAllowAny,
|
|
|
5028
5092
|
|
|
5029
5093
|
/**
|
|
5030
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
|
+
*
|
|
5031
5139
|
* @param config - The workflow configuration
|
|
5032
5140
|
* @param steps - Variable number of and* functions to execute
|
|
5033
5141
|
* @returns A configured workflow instance
|
|
@@ -5072,23 +5180,30 @@ type AgentConfig<SCHEMA extends z.ZodTypeAny> = {
|
|
|
5072
5180
|
* result: z.object({ processed: z.boolean(), content: z.string() }),
|
|
5073
5181
|
* memory: new LibSQLStorage({ url: "file:memory.db" }) // Optional workflow-specific memory
|
|
5074
5182
|
* })
|
|
5075
|
-
* .andThen(
|
|
5076
|
-
*
|
|
5077
|
-
*
|
|
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"] })
|
|
5078
5194
|
* })
|
|
5079
|
-
* .andWhen(
|
|
5080
|
-
* (data) => data.userType === "admin",
|
|
5081
|
-
* async (data) => ({ ...data, permissions: ["read", "write", "delete"] })
|
|
5082
|
-
* )
|
|
5083
5195
|
* .andAgent(
|
|
5084
|
-
* (data) => `Generate personalized content for ${data.userInfo.name}`,
|
|
5196
|
+
* ({ data }) => `Generate personalized content for ${data.userInfo.name}`,
|
|
5085
5197
|
* agent,
|
|
5086
5198
|
* { schema: z.object({ content: z.string() }) }
|
|
5087
5199
|
* )
|
|
5088
|
-
* .andThen(
|
|
5089
|
-
*
|
|
5090
|
-
*
|
|
5091
|
-
*
|
|
5200
|
+
* .andThen({
|
|
5201
|
+
* id: "finalize-result",
|
|
5202
|
+
* execute: async ({ data }) => ({
|
|
5203
|
+
* processed: true,
|
|
5204
|
+
* content: data.content
|
|
5205
|
+
* })
|
|
5206
|
+
* });
|
|
5092
5207
|
*
|
|
5093
5208
|
* // Run with optional memory override
|
|
5094
5209
|
* const result = await workflow.run(
|
|
@@ -5106,13 +5221,20 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5106
5221
|
*
|
|
5107
5222
|
* @example
|
|
5108
5223
|
* ```ts
|
|
5109
|
-
* const w = createWorkflowChain
|
|
5224
|
+
* const w = createWorkflowChain({
|
|
5225
|
+
* id: "greeting-workflow",
|
|
5226
|
+
* input: z.object({ name: z.string() }),
|
|
5227
|
+
* result: z.string()
|
|
5228
|
+
* })
|
|
5110
5229
|
* .andAgent(
|
|
5111
|
-
* (data) => `Generate a greeting for the user ${data.name}`,
|
|
5230
|
+
* ({ data }) => `Generate a greeting for the user ${data.name}`,
|
|
5112
5231
|
* agent,
|
|
5113
5232
|
* { schema: z.object({ greeting: z.string() }) }
|
|
5114
5233
|
* )
|
|
5115
|
-
* .andThen(
|
|
5234
|
+
* .andThen({
|
|
5235
|
+
* id: "extract-greeting",
|
|
5236
|
+
* execute: async ({ data }) => data.greeting
|
|
5237
|
+
* })
|
|
5116
5238
|
* ```
|
|
5117
5239
|
*
|
|
5118
5240
|
* @param task - The task (prompt) to execute for the agent, can be a string or a function that returns a string
|
|
@@ -5290,22 +5412,27 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5290
5412
|
* @example
|
|
5291
5413
|
* ```ts
|
|
5292
5414
|
* const workflow = createWorkflowChain(config)
|
|
5293
|
-
* .andWhen(
|
|
5294
|
-
*
|
|
5295
|
-
* async (data) =>
|
|
5296
|
-
*
|
|
5297
|
-
*
|
|
5298
|
-
*
|
|
5299
|
-
*
|
|
5300
|
-
*
|
|
5301
|
-
*
|
|
5302
|
-
*
|
|
5303
|
-
*
|
|
5304
|
-
*
|
|
5305
|
-
*
|
|
5306
|
-
*
|
|
5307
|
-
*
|
|
5308
|
-
*
|
|
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
|
+
* });
|
|
5309
5436
|
* ```
|
|
5310
5437
|
*
|
|
5311
5438
|
* @param condition - Function that determines if the step should execute based on the current data
|
|
@@ -5348,13 +5475,15 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5348
5475
|
* ```ts
|
|
5349
5476
|
* const workflow = createWorkflowChain(config)
|
|
5350
5477
|
* .andTap({
|
|
5351
|
-
*
|
|
5478
|
+
* id: "log-translation",
|
|
5479
|
+
* execute: async ({ data }) => {
|
|
5352
5480
|
* console.log("🔄 Translating text:", data);
|
|
5353
5481
|
* }
|
|
5354
5482
|
* })
|
|
5355
5483
|
* .andThen({
|
|
5484
|
+
* id: "return-translation",
|
|
5356
5485
|
* // the input data is still the same as the andTap ONLY executes, it doesn't return anything
|
|
5357
|
-
* execute: async (data) => {
|
|
5486
|
+
* execute: async ({ data }) => {
|
|
5358
5487
|
* return { ...data, translatedText: data.translatedText };
|
|
5359
5488
|
* }
|
|
5360
5489
|
* });
|
|
@@ -5363,7 +5492,7 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5363
5492
|
* @param fn - The async function to execute with the current workflow data
|
|
5364
5493
|
* @returns A new chain with the tap step added
|
|
5365
5494
|
*/
|
|
5366
|
-
andTap<
|
|
5495
|
+
andTap<_NEW_DATA>(config: {
|
|
5367
5496
|
execute: (context: {
|
|
5368
5497
|
data: CURRENT_DATA;
|
|
5369
5498
|
state: any;
|
|
@@ -5389,9 +5518,12 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5389
5518
|
* import { myWorkflow } from "./my-workflow";
|
|
5390
5519
|
*
|
|
5391
5520
|
* const workflow = createWorkflowChain(config)
|
|
5392
|
-
* .andThen(
|
|
5393
|
-
*
|
|
5394
|
-
*
|
|
5521
|
+
* .andThen({
|
|
5522
|
+
* id: "fetch-user",
|
|
5523
|
+
* execute: async ({ data }) => {
|
|
5524
|
+
* const userInfo = await fetchUserInfo(data.userId);
|
|
5525
|
+
* return { userInfo };
|
|
5526
|
+
* }
|
|
5395
5527
|
* })
|
|
5396
5528
|
* .andWorkflow(myWorkflow)
|
|
5397
5529
|
* ```
|
|
@@ -5403,24 +5535,41 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5403
5535
|
* @example
|
|
5404
5536
|
* ```ts
|
|
5405
5537
|
* const workflow = createWorkflowChain(config)
|
|
5406
|
-
* .andAll(
|
|
5407
|
-
*
|
|
5408
|
-
*
|
|
5409
|
-
*
|
|
5410
|
-
*
|
|
5411
|
-
*
|
|
5412
|
-
*
|
|
5413
|
-
*
|
|
5414
|
-
*
|
|
5415
|
-
*
|
|
5416
|
-
*
|
|
5417
|
-
*
|
|
5418
|
-
*
|
|
5419
|
-
*
|
|
5420
|
-
*
|
|
5421
|
-
*
|
|
5422
|
-
*
|
|
5423
|
-
*
|
|
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
|
+
* }
|
|
5424
5573
|
* });
|
|
5425
5574
|
* ```
|
|
5426
5575
|
*
|
|
@@ -5434,26 +5583,43 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5434
5583
|
* @example
|
|
5435
5584
|
* ```ts
|
|
5436
5585
|
* const workflow = createWorkflowChain(config)
|
|
5437
|
-
* .andRace(
|
|
5438
|
-
*
|
|
5439
|
-
*
|
|
5440
|
-
*
|
|
5441
|
-
*
|
|
5442
|
-
*
|
|
5443
|
-
*
|
|
5444
|
-
*
|
|
5445
|
-
*
|
|
5446
|
-
*
|
|
5447
|
-
*
|
|
5448
|
-
*
|
|
5449
|
-
*
|
|
5450
|
-
*
|
|
5451
|
-
*
|
|
5452
|
-
*
|
|
5453
|
-
*
|
|
5454
|
-
*
|
|
5455
|
-
*
|
|
5456
|
-
*
|
|
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
|
+
* }
|
|
5457
5623
|
* });
|
|
5458
5624
|
* ```
|
|
5459
5625
|
*
|