@voltagent/core 0.1.70 → 0.1.72
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 +349 -153
- package/dist/index.js +414 -244
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +414 -244
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
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
|
|
@@ -335,10 +344,13 @@ type InternalAnyWorkflowStep<INPUT, DATA = DangerouslyAllowAny, RESULT = Dangero
|
|
|
335
344
|
* Infer the result type from a list of steps
|
|
336
345
|
* @private - INTERNAL USE ONLY
|
|
337
346
|
*/
|
|
338
|
-
type InternalInferWorkflowStepsResult<STEPS extends ReadonlyArray<InternalAnyWorkflowStep<
|
|
339
|
-
[K in keyof STEPS]:
|
|
347
|
+
type InternalInferWorkflowStepsResult<STEPS extends ReadonlyArray<InternalAnyWorkflowStep<any, any, any, any, any>>> = {
|
|
348
|
+
[K in keyof STEPS]: ExtractExecuteResult<STEPS[K]>;
|
|
340
349
|
};
|
|
341
350
|
type InternalExtractWorkflowInputData<T> = TF.IsUnknown<T> extends true ? BaseMessage | BaseMessage[] | string : TF.IsAny<T> extends true ? BaseMessage | BaseMessage[] | string : T extends z.ZodType ? z.infer<T> : T;
|
|
351
|
+
type ExtractExecuteResult<T> = T extends {
|
|
352
|
+
execute: (...args: any[]) => infer R;
|
|
353
|
+
} ? R extends Promise<infer U> ? U : R : never;
|
|
342
354
|
|
|
343
355
|
type WorkflowStateStatus = "pending" | "running" | "completed" | "failed" | "suspended";
|
|
344
356
|
type WorkflowState<INPUT, RESULT> = {
|
|
@@ -1832,7 +1844,7 @@ type ModelDynamicValue<T> = T | DynamicValue<T>;
|
|
|
1832
1844
|
/**
|
|
1833
1845
|
* Enhanced dynamic value for tools that supports static or dynamic values
|
|
1834
1846
|
*/
|
|
1835
|
-
type ToolsDynamicValue = (Tool<any> | Toolkit)[] | DynamicValue<(Tool<any> | Toolkit)[]>;
|
|
1847
|
+
type ToolsDynamicValue = (Tool<any, any> | Toolkit)[] | DynamicValue<(Tool<any, any> | Toolkit)[]>;
|
|
1836
1848
|
/**
|
|
1837
1849
|
* Provider options type for LLM configurations
|
|
1838
1850
|
*/
|
|
@@ -2670,7 +2682,7 @@ type ToolExecuteOptions = {
|
|
|
2670
2682
|
*/
|
|
2671
2683
|
[key: string]: any;
|
|
2672
2684
|
};
|
|
2673
|
-
type BaseTool = Tool<any>;
|
|
2685
|
+
type BaseTool = Tool<any, any>;
|
|
2674
2686
|
type BaseToolCall = {
|
|
2675
2687
|
name: string;
|
|
2676
2688
|
arguments: Record<string, any>;
|
|
@@ -3536,6 +3548,17 @@ interface LibSQLStorageOptions extends MemoryOptions {
|
|
|
3536
3548
|
* @default 100
|
|
3537
3549
|
*/
|
|
3538
3550
|
storageLimit?: number;
|
|
3551
|
+
/**
|
|
3552
|
+
* Number of retry attempts for database operations when encountering busy/locked errors
|
|
3553
|
+
* @default 3
|
|
3554
|
+
*/
|
|
3555
|
+
retryAttempts?: number;
|
|
3556
|
+
/**
|
|
3557
|
+
* Base delay in milliseconds before retrying a failed operation
|
|
3558
|
+
* Uses a jittered exponential backoff strategy for better load distribution
|
|
3559
|
+
* @default 50
|
|
3560
|
+
*/
|
|
3561
|
+
baseDelayMs?: number;
|
|
3539
3562
|
}
|
|
3540
3563
|
/**
|
|
3541
3564
|
* A LibSQL storage implementation of the Memory and WorkflowMemory interfaces
|
|
@@ -3551,6 +3574,8 @@ declare class LibSQLStorage implements Memory {
|
|
|
3551
3574
|
private initialized;
|
|
3552
3575
|
private workflowExtension;
|
|
3553
3576
|
private logger;
|
|
3577
|
+
private retryAttempts;
|
|
3578
|
+
private baseDelayMs;
|
|
3554
3579
|
/**
|
|
3555
3580
|
* Create a new LibSQL storage
|
|
3556
3581
|
* @param options Configuration options
|
|
@@ -3568,6 +3593,20 @@ declare class LibSQLStorage implements Memory {
|
|
|
3568
3593
|
* @param data Additional data to log
|
|
3569
3594
|
*/
|
|
3570
3595
|
private debug;
|
|
3596
|
+
/**
|
|
3597
|
+
* Calculate delay with jitter for better load distribution
|
|
3598
|
+
* @param attempt Current retry attempt number
|
|
3599
|
+
* @returns Delay in milliseconds
|
|
3600
|
+
*/
|
|
3601
|
+
private calculateRetryDelay;
|
|
3602
|
+
/**
|
|
3603
|
+
* Execute a database operation with retry strategy
|
|
3604
|
+
* Implements jittered exponential backoff
|
|
3605
|
+
* @param operationFn The operation function to execute
|
|
3606
|
+
* @param operationName Operation name for logging
|
|
3607
|
+
* @returns The result of the operation
|
|
3608
|
+
*/
|
|
3609
|
+
private executeWithRetryStrategy;
|
|
3571
3610
|
/**
|
|
3572
3611
|
* Initialize workflow tables
|
|
3573
3612
|
*/
|
|
@@ -3610,7 +3649,7 @@ declare class LibSQLStorage implements Memory {
|
|
|
3610
3649
|
/**
|
|
3611
3650
|
* Close the database connection
|
|
3612
3651
|
*/
|
|
3613
|
-
close(): void
|
|
3652
|
+
close(): Promise<void>;
|
|
3614
3653
|
/**
|
|
3615
3654
|
* Add or update a history entry
|
|
3616
3655
|
* @param key Entry ID
|
|
@@ -4636,7 +4675,8 @@ declare class Agent<TProvider extends {
|
|
|
4636
4675
|
name: string;
|
|
4637
4676
|
description: string;
|
|
4638
4677
|
parameters: any;
|
|
4639
|
-
|
|
4678
|
+
outputSchema?: any;
|
|
4679
|
+
execute: (args: any, options?: ToolExecuteOptions) => Promise<any>;
|
|
4640
4680
|
}[];
|
|
4641
4681
|
subAgents: {
|
|
4642
4682
|
node_id: string;
|
|
@@ -4779,11 +4819,14 @@ type AgentConfig$1<SCHEMA extends z.ZodTypeAny> = PublicGenerateOptions & {
|
|
|
4779
4819
|
* ```ts
|
|
4780
4820
|
* const w = createWorkflow(
|
|
4781
4821
|
* andAgent(
|
|
4782
|
-
* (data) => `Generate a greeting for the user ${data.name}`,
|
|
4822
|
+
* ({ data }) => `Generate a greeting for the user ${data.name}`,
|
|
4783
4823
|
* agent,
|
|
4784
|
-
*
|
|
4824
|
+
* { schema: z.object({ greeting: z.string() }) }
|
|
4785
4825
|
* ),
|
|
4786
|
-
* andThen(
|
|
4826
|
+
* andThen({
|
|
4827
|
+
* id: "extract-greeting",
|
|
4828
|
+
* execute: async ({ data }) => data.greeting
|
|
4829
|
+
* })
|
|
4787
4830
|
* );
|
|
4788
4831
|
* ```
|
|
4789
4832
|
*
|
|
@@ -4882,17 +4925,23 @@ interface InternalWorkflowRunOptions extends WorkflowRunOptions {
|
|
|
4882
4925
|
* @example
|
|
4883
4926
|
* ```ts
|
|
4884
4927
|
* const w = createWorkflow(
|
|
4885
|
-
* andThen(
|
|
4886
|
-
*
|
|
4887
|
-
*
|
|
4928
|
+
* andThen({
|
|
4929
|
+
* id: "process-data",
|
|
4930
|
+
* execute: async ({ data }) => {
|
|
4931
|
+
* const processed = await someAsyncOperation(data.value);
|
|
4932
|
+
* return { ...data, processed };
|
|
4933
|
+
* }
|
|
4888
4934
|
* }),
|
|
4889
|
-
* andThen(
|
|
4890
|
-
*
|
|
4935
|
+
* andThen({
|
|
4936
|
+
* id: "format-result",
|
|
4937
|
+
* execute: async ({ data }) => {
|
|
4938
|
+
* return { result: `Processed: ${data.processed}` };
|
|
4939
|
+
* }
|
|
4891
4940
|
* })
|
|
4892
4941
|
* );
|
|
4893
4942
|
* ```
|
|
4894
4943
|
*
|
|
4895
|
-
* @param
|
|
4944
|
+
* @param config - Configuration object with execute function and metadata
|
|
4896
4945
|
* @returns A workflow step that executes the function and returns the result
|
|
4897
4946
|
*/
|
|
4898
4947
|
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 +4953,25 @@ declare function andThen<INPUT, DATA, RESULT, SUSPEND_DATA = DangerouslyAllowAny
|
|
|
4904
4953
|
* ```ts
|
|
4905
4954
|
* const w = createWorkflow(
|
|
4906
4955
|
* andWhen({
|
|
4907
|
-
*
|
|
4908
|
-
*
|
|
4956
|
+
* id: "admin-permissions",
|
|
4957
|
+
* condition: async ({ data }) => data.userType === "admin",
|
|
4958
|
+
* execute: async ({ data }) => {
|
|
4909
4959
|
* return { ...data, permissions: ["read", "write", "delete"] };
|
|
4910
|
-
* }
|
|
4960
|
+
* }
|
|
4911
4961
|
* }),
|
|
4912
4962
|
* andWhen({
|
|
4913
|
-
*
|
|
4914
|
-
*
|
|
4915
|
-
*
|
|
4963
|
+
* id: "high-value-processing",
|
|
4964
|
+
* condition: async ({ data }) => data.value > 100,
|
|
4965
|
+
* step: andAgent(
|
|
4966
|
+
* ({ data }) => `Process high value transaction: ${data.value}`,
|
|
4916
4967
|
* agent,
|
|
4917
4968
|
* { schema: z.object({ processed: z.boolean() }) }
|
|
4918
4969
|
* )
|
|
4919
|
-
* )
|
|
4970
|
+
* })
|
|
4920
4971
|
* );
|
|
4921
4972
|
* ```
|
|
4922
4973
|
*
|
|
4923
|
-
* @param
|
|
4924
|
-
* @param stepOrFunc - Either a workflow step or an agent to execute when the condition is true
|
|
4974
|
+
* @param config - Configuration object with condition, step/execute function, and metadata
|
|
4925
4975
|
* @returns A conditional workflow step that executes the step only when the condition evaluates to true
|
|
4926
4976
|
*/
|
|
4927
4977
|
declare function andWhen<INPUT, DATA, RESULT>({ condition, step, inputSchema, outputSchema, suspendSchema, resumeSchema, ...config }: WorkflowStepConditionalWhenConfig<INPUT, DATA, RESULT>): WorkflowStepConditionalWhen<INPUT, DATA, RESULT>;
|
|
@@ -4932,35 +4982,47 @@ declare function andWhen<INPUT, DATA, RESULT>({ condition, step, inputSchema, ou
|
|
|
4932
4982
|
* @example
|
|
4933
4983
|
* ```ts
|
|
4934
4984
|
* const w = createWorkflow(
|
|
4935
|
-
* andAll(
|
|
4936
|
-
*
|
|
4937
|
-
*
|
|
4938
|
-
*
|
|
4939
|
-
*
|
|
4940
|
-
*
|
|
4941
|
-
*
|
|
4942
|
-
*
|
|
4943
|
-
*
|
|
4944
|
-
*
|
|
4945
|
-
* (
|
|
4946
|
-
*
|
|
4947
|
-
*
|
|
4948
|
-
*
|
|
4949
|
-
*
|
|
4950
|
-
*
|
|
4951
|
-
*
|
|
4952
|
-
*
|
|
4985
|
+
* andAll({
|
|
4986
|
+
* id: "parallel-fetch",
|
|
4987
|
+
* steps: [
|
|
4988
|
+
* andThen({
|
|
4989
|
+
* id: "fetch-user",
|
|
4990
|
+
* execute: async ({ data }) => {
|
|
4991
|
+
* const userInfo = await fetchUserInfo(data.userId);
|
|
4992
|
+
* return { userInfo };
|
|
4993
|
+
* }
|
|
4994
|
+
* }),
|
|
4995
|
+
* andThen({
|
|
4996
|
+
* id: "fetch-permissions",
|
|
4997
|
+
* execute: async ({ data }) => {
|
|
4998
|
+
* const permissions = await fetchPermissions(data.userId);
|
|
4999
|
+
* return { permissions };
|
|
5000
|
+
* }
|
|
5001
|
+
* }),
|
|
5002
|
+
* andAgent(
|
|
5003
|
+
* ({ data }) => `Generate recommendations for user ${data.userId}`,
|
|
5004
|
+
* agent,
|
|
5005
|
+
* { schema: z.object({ recommendations: z.array(z.string()) }) }
|
|
5006
|
+
* )
|
|
5007
|
+
* ]
|
|
5008
|
+
* }),
|
|
5009
|
+
* andThen({
|
|
5010
|
+
* id: "combine-results",
|
|
5011
|
+
* execute: async ({ data }) => {
|
|
5012
|
+
* // data is now an array: [{ userInfo }, { permissions }, { recommendations }]
|
|
5013
|
+
* return { combined: data.flat() };
|
|
5014
|
+
* }
|
|
4953
5015
|
* })
|
|
4954
5016
|
* );
|
|
4955
5017
|
* ```
|
|
4956
5018
|
*
|
|
4957
|
-
* @param
|
|
5019
|
+
* @param config - Configuration object with steps array and metadata
|
|
4958
5020
|
* @returns A workflow step that executes all steps simultaneously and returns their results as an array
|
|
4959
5021
|
*/
|
|
4960
|
-
declare function andAll<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<InternalAnyWorkflowStep<INPUT, DATA, RESULT
|
|
5022
|
+
declare function andAll<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<InternalAnyWorkflowStep<INPUT, DATA, RESULT>>>({ steps, ...config }: WorkflowStepParallelAllConfig<STEPS>): {
|
|
4961
5023
|
type: "parallel-all";
|
|
4962
|
-
steps: InternalAnyWorkflowStep<INPUT, DATA,
|
|
4963
|
-
execute: (context: WorkflowExecuteContext<INPUT, DATA, any, any>) => Promise<
|
|
5024
|
+
steps: InternalAnyWorkflowStep<INPUT, DATA, InternalInferWorkflowStepsResult<STEPS>>[];
|
|
5025
|
+
execute: (context: WorkflowExecuteContext<INPUT, DATA, any, any>) => Promise<InternalInferWorkflowStepsResult<STEPS>>;
|
|
4964
5026
|
id: string;
|
|
4965
5027
|
name: string;
|
|
4966
5028
|
purpose: string;
|
|
@@ -4972,39 +5034,51 @@ declare function andAll<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<Interna
|
|
|
4972
5034
|
* @example
|
|
4973
5035
|
* ```ts
|
|
4974
5036
|
* const w = createWorkflow(
|
|
4975
|
-
* andRace(
|
|
4976
|
-
*
|
|
4977
|
-
*
|
|
4978
|
-
*
|
|
4979
|
-
*
|
|
4980
|
-
*
|
|
4981
|
-
*
|
|
4982
|
-
*
|
|
4983
|
-
*
|
|
4984
|
-
*
|
|
4985
|
-
*
|
|
4986
|
-
*
|
|
4987
|
-
*
|
|
4988
|
-
*
|
|
4989
|
-
*
|
|
4990
|
-
*
|
|
4991
|
-
*
|
|
4992
|
-
*
|
|
4993
|
-
*
|
|
4994
|
-
*
|
|
5037
|
+
* andRace({
|
|
5038
|
+
* id: "race-data-sources",
|
|
5039
|
+
* steps: [
|
|
5040
|
+
* andThen({
|
|
5041
|
+
* id: "check-cache",
|
|
5042
|
+
* execute: async ({ data }) => {
|
|
5043
|
+
* // Fast operation
|
|
5044
|
+
* const cacheResult = await checkCache(data.query);
|
|
5045
|
+
* return { source: "cache", result: cacheResult };
|
|
5046
|
+
* }
|
|
5047
|
+
* }),
|
|
5048
|
+
* andThen({
|
|
5049
|
+
* id: "query-database",
|
|
5050
|
+
* execute: async ({ data }) => {
|
|
5051
|
+
* // Slower operation
|
|
5052
|
+
* const dbResult = await queryDatabase(data.query);
|
|
5053
|
+
* return { source: "database", result: dbResult };
|
|
5054
|
+
* }
|
|
5055
|
+
* }),
|
|
5056
|
+
* andAgent(
|
|
5057
|
+
* ({ data }) => `Generate fallback response for: ${data.query}`,
|
|
5058
|
+
* agent,
|
|
5059
|
+
* { schema: z.object({ source: z.literal("ai"), result: z.string() }) }
|
|
5060
|
+
* )
|
|
5061
|
+
* ]
|
|
5062
|
+
* }),
|
|
5063
|
+
* andThen({
|
|
5064
|
+
* id: "process-result",
|
|
5065
|
+
* execute: async ({ data }) => {
|
|
5066
|
+
* // data is the result from whichever step completed first
|
|
5067
|
+
* return { finalResult: data.result, source: data.source };
|
|
5068
|
+
* }
|
|
4995
5069
|
* })
|
|
4996
5070
|
* );
|
|
4997
5071
|
* ```
|
|
4998
5072
|
*
|
|
4999
|
-
* @param
|
|
5073
|
+
* @param config - Configuration object with steps array and metadata
|
|
5000
5074
|
* @returns A workflow step that executes all steps simultaneously and returns the result from the first step to complete
|
|
5001
5075
|
*/
|
|
5002
|
-
declare function andRace<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<InternalAnyWorkflowStep<INPUT, DATA, RESULT
|
|
5076
|
+
declare function andRace<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<InternalAnyWorkflowStep<INPUT, DATA, RESULT>>>({ steps, ...config }: InternalWorkflowStepConfig<{
|
|
5003
5077
|
steps: STEPS;
|
|
5004
5078
|
}>): {
|
|
5005
5079
|
type: "parallel-race";
|
|
5006
|
-
steps: InternalAnyWorkflowStep<INPUT, DATA,
|
|
5007
|
-
execute: (context: WorkflowExecuteContext<INPUT, DATA, any, any>) => Promise<
|
|
5080
|
+
steps: InternalAnyWorkflowStep<INPUT, DATA, InternalInferWorkflowStepsResult<STEPS>[number]>[];
|
|
5081
|
+
execute: (context: WorkflowExecuteContext<INPUT, DATA, any, any>) => Promise<InternalInferWorkflowStepsResult<STEPS>[number]>;
|
|
5008
5082
|
id: string;
|
|
5009
5083
|
name: string;
|
|
5010
5084
|
purpose: string;
|
|
@@ -5012,7 +5086,27 @@ declare function andRace<INPUT, DATA, RESULT, STEPS extends ReadonlyArray<Intern
|
|
|
5012
5086
|
|
|
5013
5087
|
/**
|
|
5014
5088
|
* A safe way to tap into the workflow state without affecting the result.
|
|
5015
|
-
*
|
|
5089
|
+
*
|
|
5090
|
+
* @example
|
|
5091
|
+
* ```ts
|
|
5092
|
+
* const w = createWorkflow(
|
|
5093
|
+
* andTap({
|
|
5094
|
+
* id: "log-processing",
|
|
5095
|
+
* execute: async ({ data }) => {
|
|
5096
|
+
* console.log("Processing data:", data);
|
|
5097
|
+
* }
|
|
5098
|
+
* }),
|
|
5099
|
+
* andThen({
|
|
5100
|
+
* id: "process-data",
|
|
5101
|
+
* execute: async ({ data }) => {
|
|
5102
|
+
* // data is unchanged from the tap step
|
|
5103
|
+
* return { ...data, processed: true };
|
|
5104
|
+
* }
|
|
5105
|
+
* })
|
|
5106
|
+
* );
|
|
5107
|
+
* ```
|
|
5108
|
+
*
|
|
5109
|
+
* @param config - Configuration object with execute function and metadata
|
|
5016
5110
|
* @returns A workflow step that executes the function
|
|
5017
5111
|
*/
|
|
5018
5112
|
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 +5122,50 @@ declare function andTap<INPUT, DATA, RESULT, SUSPEND_DATA = DangerouslyAllowAny,
|
|
|
5028
5122
|
|
|
5029
5123
|
/**
|
|
5030
5124
|
* Creates a workflow from multiple and* functions
|
|
5125
|
+
*
|
|
5126
|
+
* @example
|
|
5127
|
+
* ```ts
|
|
5128
|
+
* const workflow = createWorkflow({
|
|
5129
|
+
* id: "user-processing",
|
|
5130
|
+
* name: "User Processing Workflow",
|
|
5131
|
+
* purpose: "Process user data and generate personalized content",
|
|
5132
|
+
* input: z.object({ userId: z.string(), userType: z.enum(["admin", "user"]) }),
|
|
5133
|
+
* result: z.object({ processed: z.boolean(), content: z.string() }),
|
|
5134
|
+
* memory: new LibSQLStorage({ url: "file:memory.db" }) // Optional workflow-specific memory
|
|
5135
|
+
* },
|
|
5136
|
+
* andThen({
|
|
5137
|
+
* id: "fetch-user",
|
|
5138
|
+
* execute: async ({ data }) => {
|
|
5139
|
+
* const userInfo = await fetchUserInfo(data.userId);
|
|
5140
|
+
* return { ...data, userInfo };
|
|
5141
|
+
* }
|
|
5142
|
+
* }),
|
|
5143
|
+
* andWhen({
|
|
5144
|
+
* id: "admin-permissions",
|
|
5145
|
+
* condition: async ({ data }) => data.userType === "admin",
|
|
5146
|
+
* execute: async ({ data }) => ({ ...data, permissions: ["read", "write", "delete"] })
|
|
5147
|
+
* }),
|
|
5148
|
+
* andAgent(
|
|
5149
|
+
* ({ data }) => `Generate personalized content for ${data.userInfo.name}`,
|
|
5150
|
+
* agent,
|
|
5151
|
+
* { schema: z.object({ content: z.string() }) }
|
|
5152
|
+
* ),
|
|
5153
|
+
* andThen({
|
|
5154
|
+
* id: "finalize-result",
|
|
5155
|
+
* execute: async ({ data }) => ({
|
|
5156
|
+
* processed: true,
|
|
5157
|
+
* content: data.content
|
|
5158
|
+
* })
|
|
5159
|
+
* })
|
|
5160
|
+
* );
|
|
5161
|
+
*
|
|
5162
|
+
* // Run with optional memory override
|
|
5163
|
+
* const result = await workflow.run(
|
|
5164
|
+
* { userId: "123", userType: "admin" },
|
|
5165
|
+
* { memory: new LibSQLStorage({ url: "file:memory.db" }) }
|
|
5166
|
+
* );
|
|
5167
|
+
* ```
|
|
5168
|
+
*
|
|
5031
5169
|
* @param config - The workflow configuration
|
|
5032
5170
|
* @param steps - Variable number of and* functions to execute
|
|
5033
5171
|
* @returns A configured workflow instance
|
|
@@ -5072,23 +5210,30 @@ type AgentConfig<SCHEMA extends z.ZodTypeAny> = {
|
|
|
5072
5210
|
* result: z.object({ processed: z.boolean(), content: z.string() }),
|
|
5073
5211
|
* memory: new LibSQLStorage({ url: "file:memory.db" }) // Optional workflow-specific memory
|
|
5074
5212
|
* })
|
|
5075
|
-
* .andThen(
|
|
5076
|
-
*
|
|
5077
|
-
*
|
|
5213
|
+
* .andThen({
|
|
5214
|
+
* id: "fetch-user",
|
|
5215
|
+
* execute: async ({ data }) => {
|
|
5216
|
+
* const userInfo = await fetchUserInfo(data.userId);
|
|
5217
|
+
* return { ...data, userInfo };
|
|
5218
|
+
* }
|
|
5219
|
+
* })
|
|
5220
|
+
* .andWhen({
|
|
5221
|
+
* id: "admin-permissions",
|
|
5222
|
+
* condition: async ({ data }) => data.userType === "admin",
|
|
5223
|
+
* execute: async ({ data }) => ({ ...data, permissions: ["read", "write", "delete"] })
|
|
5078
5224
|
* })
|
|
5079
|
-
* .andWhen(
|
|
5080
|
-
* (data) => data.userType === "admin",
|
|
5081
|
-
* async (data) => ({ ...data, permissions: ["read", "write", "delete"] })
|
|
5082
|
-
* )
|
|
5083
5225
|
* .andAgent(
|
|
5084
|
-
* (data) => `Generate personalized content for ${data.userInfo.name}`,
|
|
5226
|
+
* ({ data }) => `Generate personalized content for ${data.userInfo.name}`,
|
|
5085
5227
|
* agent,
|
|
5086
5228
|
* { schema: z.object({ content: z.string() }) }
|
|
5087
5229
|
* )
|
|
5088
|
-
* .andThen(
|
|
5089
|
-
*
|
|
5090
|
-
*
|
|
5091
|
-
*
|
|
5230
|
+
* .andThen({
|
|
5231
|
+
* id: "finalize-result",
|
|
5232
|
+
* execute: async ({ data }) => ({
|
|
5233
|
+
* processed: true,
|
|
5234
|
+
* content: data.content
|
|
5235
|
+
* })
|
|
5236
|
+
* });
|
|
5092
5237
|
*
|
|
5093
5238
|
* // Run with optional memory override
|
|
5094
5239
|
* const result = await workflow.run(
|
|
@@ -5106,13 +5251,20 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5106
5251
|
*
|
|
5107
5252
|
* @example
|
|
5108
5253
|
* ```ts
|
|
5109
|
-
* const w = createWorkflowChain
|
|
5254
|
+
* const w = createWorkflowChain({
|
|
5255
|
+
* id: "greeting-workflow",
|
|
5256
|
+
* input: z.object({ name: z.string() }),
|
|
5257
|
+
* result: z.string()
|
|
5258
|
+
* })
|
|
5110
5259
|
* .andAgent(
|
|
5111
|
-
* (data) => `Generate a greeting for the user ${data.name}`,
|
|
5260
|
+
* ({ data }) => `Generate a greeting for the user ${data.name}`,
|
|
5112
5261
|
* agent,
|
|
5113
5262
|
* { schema: z.object({ greeting: z.string() }) }
|
|
5114
5263
|
* )
|
|
5115
|
-
* .andThen(
|
|
5264
|
+
* .andThen({
|
|
5265
|
+
* id: "extract-greeting",
|
|
5266
|
+
* execute: async ({ data }) => data.greeting
|
|
5267
|
+
* })
|
|
5116
5268
|
* ```
|
|
5117
5269
|
*
|
|
5118
5270
|
* @param task - The task (prompt) to execute for the agent, can be a string or a function that returns a string
|
|
@@ -5290,22 +5442,27 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5290
5442
|
* @example
|
|
5291
5443
|
* ```ts
|
|
5292
5444
|
* const workflow = createWorkflowChain(config)
|
|
5293
|
-
* .andWhen(
|
|
5294
|
-
*
|
|
5295
|
-
* async (data) =>
|
|
5296
|
-
*
|
|
5297
|
-
*
|
|
5298
|
-
*
|
|
5299
|
-
*
|
|
5300
|
-
*
|
|
5301
|
-
*
|
|
5302
|
-
*
|
|
5303
|
-
*
|
|
5304
|
-
*
|
|
5305
|
-
*
|
|
5306
|
-
*
|
|
5307
|
-
*
|
|
5308
|
-
*
|
|
5445
|
+
* .andWhen({
|
|
5446
|
+
* id: "admin-permissions",
|
|
5447
|
+
* condition: async ({ data }) => data.userType === "admin",
|
|
5448
|
+
* execute: async ({ data }) => ({ ...data, permissions: ["read", "write", "delete"] })
|
|
5449
|
+
* })
|
|
5450
|
+
* .andWhen({
|
|
5451
|
+
* id: "high-value-flag",
|
|
5452
|
+
* condition: async ({ data }) => data.value > 1000,
|
|
5453
|
+
* execute: async ({ data }) => ({ ...data, flagged: true, requiresReview: true })
|
|
5454
|
+
* })
|
|
5455
|
+
* .andWhen({
|
|
5456
|
+
* id: "process-pending",
|
|
5457
|
+
* condition: async ({ data }) => data.status === "pending",
|
|
5458
|
+
* execute: async ({ data }) => {
|
|
5459
|
+
* const result = await agent.generateObject(
|
|
5460
|
+
* `Process pending request for ${data.userId}`,
|
|
5461
|
+
* z.object({ processed: z.boolean() })
|
|
5462
|
+
* );
|
|
5463
|
+
* return { ...data, ...result.object };
|
|
5464
|
+
* }
|
|
5465
|
+
* });
|
|
5309
5466
|
* ```
|
|
5310
5467
|
*
|
|
5311
5468
|
* @param condition - Function that determines if the step should execute based on the current data
|
|
@@ -5348,13 +5505,15 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5348
5505
|
* ```ts
|
|
5349
5506
|
* const workflow = createWorkflowChain(config)
|
|
5350
5507
|
* .andTap({
|
|
5351
|
-
*
|
|
5508
|
+
* id: "log-translation",
|
|
5509
|
+
* execute: async ({ data }) => {
|
|
5352
5510
|
* console.log("🔄 Translating text:", data);
|
|
5353
5511
|
* }
|
|
5354
5512
|
* })
|
|
5355
5513
|
* .andThen({
|
|
5514
|
+
* id: "return-translation",
|
|
5356
5515
|
* // the input data is still the same as the andTap ONLY executes, it doesn't return anything
|
|
5357
|
-
* execute: async (data) => {
|
|
5516
|
+
* execute: async ({ data }) => {
|
|
5358
5517
|
* return { ...data, translatedText: data.translatedText };
|
|
5359
5518
|
* }
|
|
5360
5519
|
* });
|
|
@@ -5363,7 +5522,7 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5363
5522
|
* @param fn - The async function to execute with the current workflow data
|
|
5364
5523
|
* @returns A new chain with the tap step added
|
|
5365
5524
|
*/
|
|
5366
|
-
andTap<
|
|
5525
|
+
andTap<_NEW_DATA>(config: {
|
|
5367
5526
|
execute: (context: {
|
|
5368
5527
|
data: CURRENT_DATA;
|
|
5369
5528
|
state: any;
|
|
@@ -5389,9 +5548,12 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5389
5548
|
* import { myWorkflow } from "./my-workflow";
|
|
5390
5549
|
*
|
|
5391
5550
|
* const workflow = createWorkflowChain(config)
|
|
5392
|
-
* .andThen(
|
|
5393
|
-
*
|
|
5394
|
-
*
|
|
5551
|
+
* .andThen({
|
|
5552
|
+
* id: "fetch-user",
|
|
5553
|
+
* execute: async ({ data }) => {
|
|
5554
|
+
* const userInfo = await fetchUserInfo(data.userId);
|
|
5555
|
+
* return { userInfo };
|
|
5556
|
+
* }
|
|
5395
5557
|
* })
|
|
5396
5558
|
* .andWorkflow(myWorkflow)
|
|
5397
5559
|
* ```
|
|
@@ -5403,24 +5565,41 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5403
5565
|
* @example
|
|
5404
5566
|
* ```ts
|
|
5405
5567
|
* const workflow = createWorkflowChain(config)
|
|
5406
|
-
* .andAll(
|
|
5407
|
-
*
|
|
5408
|
-
*
|
|
5409
|
-
*
|
|
5410
|
-
*
|
|
5411
|
-
*
|
|
5412
|
-
*
|
|
5413
|
-
*
|
|
5414
|
-
*
|
|
5415
|
-
*
|
|
5416
|
-
*
|
|
5417
|
-
*
|
|
5418
|
-
*
|
|
5419
|
-
*
|
|
5420
|
-
*
|
|
5421
|
-
*
|
|
5422
|
-
*
|
|
5423
|
-
*
|
|
5568
|
+
* .andAll({
|
|
5569
|
+
* id: "parallel-fetch",
|
|
5570
|
+
* steps: [
|
|
5571
|
+
* {
|
|
5572
|
+
* id: "fetch-user",
|
|
5573
|
+
* execute: async ({ data }) => {
|
|
5574
|
+
* const userInfo = await fetchUserInfo(data.userId);
|
|
5575
|
+
* return { userInfo };
|
|
5576
|
+
* }
|
|
5577
|
+
* },
|
|
5578
|
+
* {
|
|
5579
|
+
* id: "fetch-permissions",
|
|
5580
|
+
* execute: async ({ data }) => {
|
|
5581
|
+
* const permissions = await fetchPermissions(data.userId);
|
|
5582
|
+
* return { permissions };
|
|
5583
|
+
* }
|
|
5584
|
+
* },
|
|
5585
|
+
* {
|
|
5586
|
+
* id: "generate-recommendations",
|
|
5587
|
+
* execute: async ({ data }) => {
|
|
5588
|
+
* const result = await agent.generateObject(
|
|
5589
|
+
* `Generate recommendations for user ${data.userId}`,
|
|
5590
|
+
* z.object({ recommendations: z.array(z.string()) })
|
|
5591
|
+
* );
|
|
5592
|
+
* return result.object;
|
|
5593
|
+
* }
|
|
5594
|
+
* }
|
|
5595
|
+
* ]
|
|
5596
|
+
* })
|
|
5597
|
+
* .andThen({
|
|
5598
|
+
* id: "combine-results",
|
|
5599
|
+
* execute: async ({ data }) => {
|
|
5600
|
+
* // data is now an array: [{ userInfo }, { permissions }, { recommendations }]
|
|
5601
|
+
* return { combined: data.flat() };
|
|
5602
|
+
* }
|
|
5424
5603
|
* });
|
|
5425
5604
|
* ```
|
|
5426
5605
|
*
|
|
@@ -5434,26 +5613,43 @@ declare class WorkflowChain<INPUT_SCHEMA extends InternalBaseWorkflowInputSchema
|
|
|
5434
5613
|
* @example
|
|
5435
5614
|
* ```ts
|
|
5436
5615
|
* 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
|
-
*
|
|
5616
|
+
* .andRace({
|
|
5617
|
+
* id: "race-data-sources",
|
|
5618
|
+
* steps: [
|
|
5619
|
+
* {
|
|
5620
|
+
* id: "check-cache",
|
|
5621
|
+
* execute: async ({ data }) => {
|
|
5622
|
+
* // Fast operation
|
|
5623
|
+
* const cacheResult = await checkCache(data.query);
|
|
5624
|
+
* return { source: "cache", result: cacheResult };
|
|
5625
|
+
* }
|
|
5626
|
+
* },
|
|
5627
|
+
* {
|
|
5628
|
+
* id: "query-database",
|
|
5629
|
+
* execute: async ({ data }) => {
|
|
5630
|
+
* // Slower operation
|
|
5631
|
+
* const dbResult = await queryDatabase(data.query);
|
|
5632
|
+
* return { source: "database", result: dbResult };
|
|
5633
|
+
* }
|
|
5634
|
+
* },
|
|
5635
|
+
* {
|
|
5636
|
+
* id: "ai-fallback",
|
|
5637
|
+
* execute: async ({ data }) => {
|
|
5638
|
+
* const result = await agent.generateObject(
|
|
5639
|
+
* `Generate fallback response for: ${data.query}`,
|
|
5640
|
+
* z.object({ source: z.literal("ai"), result: z.string() })
|
|
5641
|
+
* );
|
|
5642
|
+
* return result.object;
|
|
5643
|
+
* }
|
|
5644
|
+
* }
|
|
5645
|
+
* ]
|
|
5646
|
+
* })
|
|
5647
|
+
* .andThen({
|
|
5648
|
+
* id: "process-result",
|
|
5649
|
+
* execute: async ({ data }) => {
|
|
5650
|
+
* // data is the result from whichever step completed first
|
|
5651
|
+
* return { finalResult: data.result, source: data.source };
|
|
5652
|
+
* }
|
|
5457
5653
|
* });
|
|
5458
5654
|
* ```
|
|
5459
5655
|
*
|