@output.ai/core 0.5.0 → 0.5.2
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/package.json +1 -1
- package/src/errors.d.ts +15 -0
- package/src/index.d.ts +17 -864
- package/src/index.js +3 -28
- package/src/interface/evaluation_result.d.ts +160 -0
- package/src/interface/evaluation_result.js +202 -0
- package/src/interface/evaluator.d.ts +70 -0
- package/src/interface/evaluator.js +1 -202
- package/src/interface/evaluator.spec.js +1 -1
- package/src/interface/index.d.ts +9 -0
- package/src/interface/index.js +19 -0
- package/src/interface/step.d.ts +138 -0
- package/src/interface/step.js +1 -0
- package/src/interface/types.d.ts +27 -0
- package/src/interface/webhook.d.ts +84 -0
- package/src/interface/workflow.d.ts +273 -0
- package/src/interface/workflow.js +7 -38
- package/src/interface/workflow.spec.js +495 -0
- package/src/interface/workflow_context.js +31 -0
- package/src/interface/workflow_utils.d.ts +53 -0
- package/src/interface/workflow_utils.js +1 -0
- package/src/utils/index.d.ts +1 -9
- package/src/utils/utils.js +2 -12
- package/src/utils/utils.spec.js +39 -52
- package/src/worker/index.spec.js +180 -0
- package/src/worker/interceptors/workflow.js +2 -2
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import type { AnyZodSchema, TemporalActivityOptions } from './types.d.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Options for a step.
|
|
6
|
+
*/
|
|
7
|
+
export type StepOptions = {
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Temporal activity options for this step.
|
|
11
|
+
*/
|
|
12
|
+
activityOptions?: TemporalActivityOptions
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The handler function of a step.
|
|
17
|
+
*
|
|
18
|
+
* @param input - The step input; it matches the schema defined by `inputSchema`.
|
|
19
|
+
*
|
|
20
|
+
* @returns A value matching the schema defined by `outputSchema`.
|
|
21
|
+
*/
|
|
22
|
+
export type StepFunction<
|
|
23
|
+
InputSchema extends AnyZodSchema | undefined = undefined,
|
|
24
|
+
OutputSchema extends AnyZodSchema | undefined = undefined
|
|
25
|
+
> = InputSchema extends AnyZodSchema ?
|
|
26
|
+
( input: z.infer<InputSchema> ) => Promise<OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void> :
|
|
27
|
+
() => Promise<OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A wrapper around the user defined `fn` handler function.
|
|
31
|
+
*
|
|
32
|
+
* It accepts the same input and returns the same value, calling the user function inside.
|
|
33
|
+
*
|
|
34
|
+
* It adds input and output validation based on the `inputSchema`, `outputSchema`.
|
|
35
|
+
*
|
|
36
|
+
* @param input - The Step input; it matches the schema defined by `inputSchema`.
|
|
37
|
+
* @returns A value matching the schema defined by `outputSchema`.
|
|
38
|
+
*/
|
|
39
|
+
export type StepFunctionWrapper<StepFunction> =
|
|
40
|
+
Parameters<StepFunction> extends [infer Input] ?
|
|
41
|
+
( input: Input ) => ReturnType<StepFunction> :
|
|
42
|
+
() => ReturnType<StepFunction>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Creates a step.
|
|
46
|
+
*
|
|
47
|
+
* A step is a logical unit of work that can perform I/O. It is translated to a Temporal Activity.
|
|
48
|
+
*
|
|
49
|
+
* The step logic is defined in the `fn` handler function.
|
|
50
|
+
*
|
|
51
|
+
* The schema of the input that the function receives as the first argument is defined by the `inputSchema` option.
|
|
52
|
+
*
|
|
53
|
+
* The output of the `fn` handler must match the schema defined by `outputSchema`; otherwise, a validation error is raised.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```
|
|
57
|
+
* step( {
|
|
58
|
+
* name: 'process',
|
|
59
|
+
* description: 'A generic process',
|
|
60
|
+
* inputSchema: z.object( {
|
|
61
|
+
* value: z.number()
|
|
62
|
+
* } ),
|
|
63
|
+
* outputSchema: z.string(),
|
|
64
|
+
* fn: async input => {
|
|
65
|
+
* const result = await ai.call( input.value );
|
|
66
|
+
* return result as string;
|
|
67
|
+
* }
|
|
68
|
+
* } )
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @example Step without outputSchema
|
|
72
|
+
* ```
|
|
73
|
+
* step( {
|
|
74
|
+
* name: 'process',
|
|
75
|
+
* description: 'A generic process',
|
|
76
|
+
* inputSchema: z.object( {
|
|
77
|
+
* value: z.number()
|
|
78
|
+
* } ),
|
|
79
|
+
* fn: async input => {
|
|
80
|
+
* await ai.call( input.value );
|
|
81
|
+
* }
|
|
82
|
+
* } )
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @example Step without inputSchema
|
|
86
|
+
* ```
|
|
87
|
+
* step( {
|
|
88
|
+
* name: 'process',
|
|
89
|
+
* description: 'A generic process',
|
|
90
|
+
* outputSchema: z.string(),
|
|
91
|
+
* fn: async () => {
|
|
92
|
+
* const result = await ai.call();
|
|
93
|
+
* return result as string;
|
|
94
|
+
* }
|
|
95
|
+
* } )
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @example Step without inputSchema and outputSchema
|
|
99
|
+
* ```
|
|
100
|
+
* step( {
|
|
101
|
+
* name: 'process',
|
|
102
|
+
* description: 'A generic process',
|
|
103
|
+
* fn: async () => {
|
|
104
|
+
* await ai.call();
|
|
105
|
+
* }
|
|
106
|
+
* } )
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* @remarks
|
|
110
|
+
* - Never call another step from within a step.
|
|
111
|
+
* - Never call a workflow from within a step.
|
|
112
|
+
*
|
|
113
|
+
* @typeParam InputSchema - Zod schema of the fn's input.
|
|
114
|
+
* @typeParam OutputSchema - Zod schema of the fn's return.
|
|
115
|
+
*
|
|
116
|
+
* @throws {@link ValidationError}
|
|
117
|
+
* @throws {@link FatalError}
|
|
118
|
+
*
|
|
119
|
+
* @param params - Step parameters
|
|
120
|
+
* @param params.name - Human-readable step name (must start with a letter or underscore, followed by letters, numbers, or underscores)
|
|
121
|
+
* @param params.description - Description of the step
|
|
122
|
+
* @param params.inputSchema - Zod schema for the `fn` input
|
|
123
|
+
* @param params.outputSchema - Zod schema for the `fn` output
|
|
124
|
+
* @param params.fn - A handler function containing the step code
|
|
125
|
+
* @param params.options - Optional step options.
|
|
126
|
+
* @returns The same handler function set at `fn`
|
|
127
|
+
*/
|
|
128
|
+
export declare function step<
|
|
129
|
+
InputSchema extends AnyZodSchema | undefined = undefined,
|
|
130
|
+
OutputSchema extends AnyZodSchema | undefined = undefined
|
|
131
|
+
>( params: {
|
|
132
|
+
name: string;
|
|
133
|
+
description?: string;
|
|
134
|
+
inputSchema?: InputSchema;
|
|
135
|
+
outputSchema?: OutputSchema;
|
|
136
|
+
fn: StepFunction<InputSchema, OutputSchema>;
|
|
137
|
+
options?: StepOptions;
|
|
138
|
+
} ): StepFunctionWrapper<StepFunction<InputSchema, OutputSchema>>;
|
package/src/interface/step.js
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import type { ActivityOptions } from '@temporalio/workflow';
|
|
3
|
+
/**
|
|
4
|
+
* Similar to `Partial<T>` but applies to nested properties recursively, creating a deep optional variant of `T`:
|
|
5
|
+
* - Objects: All properties become optional, recursively.
|
|
6
|
+
* - Functions: Preserved as‑is (only the property itself becomes optional).
|
|
7
|
+
* - Primitives: Returned unchanged.
|
|
8
|
+
* Useful for config overrides with strong IntelliSense on nested fields and methods.
|
|
9
|
+
*/
|
|
10
|
+
export type DeepPartial<T> =
|
|
11
|
+
T extends ( ...args: any[] ) => unknown ? T : // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
12
|
+
T extends object ? { [K in keyof T]?: DeepPartial<T[K]> } :
|
|
13
|
+
T;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Type alias for any Zod schema type.
|
|
17
|
+
*/
|
|
18
|
+
export type AnyZodSchema = z.ZodType<any, any, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Native Temporal configurations for activities.
|
|
22
|
+
*
|
|
23
|
+
* All native options are accepted except 'versioningIntent', 'taskQueue', 'allowEagerDispatch'.
|
|
24
|
+
*
|
|
25
|
+
* @see {@link https://typescript.temporal.io/api/interfaces/common.ActivityOptions}
|
|
26
|
+
*/
|
|
27
|
+
export type TemporalActivityOptions = Omit<ActivityOptions, 'versioningIntent' | 'taskQueue' | 'allowEagerDispatch'>;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { SerializedFetchResponse } from '../utils/index.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Allowed HTTP methods for request helpers.
|
|
5
|
+
*/
|
|
6
|
+
export type HttpMethod = 'HEAD' | 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Send an POST HTTP request to a URL, optionally with a payload, then wait for a webhook response.
|
|
10
|
+
*
|
|
11
|
+
* The "Content-Type" is inferred from the payload type and can be overridden via the `headers` argument.
|
|
12
|
+
*
|
|
13
|
+
* If the body is not a type natively accepted by the Fetch API, it is serialized to a string: `JSON.stringify()` for objects, or `String()` for primitives.
|
|
14
|
+
*
|
|
15
|
+
* When a body is sent, the payload is wrapped together with the `workflowId` and sent as:
|
|
16
|
+
* @example
|
|
17
|
+
* ```js
|
|
18
|
+
* const finalPayload = {
|
|
19
|
+
* workflowId,
|
|
20
|
+
* payload
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* After dispatching the request, the workflow pauses and waits for a POST to `/workflow/:id/feedback` (where `:id` is the `workflowId`). When the API receives that request, its body is delivered back to the workflow and execution resumes.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```js
|
|
28
|
+
* const response = await sendPostRequestAndAwaitWebhook( {
|
|
29
|
+
* url: 'https://example.com/integration',
|
|
30
|
+
* payload: {
|
|
31
|
+
* }
|
|
32
|
+
* } );
|
|
33
|
+
*
|
|
34
|
+
* assert( response, 'the value sent back via the api' );
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @remarks
|
|
38
|
+
* - Only callable from within a workflow function; do not use in steps or evaluators.
|
|
39
|
+
* - Steps and evaluators are activity-based and are not designed to be paused.
|
|
40
|
+
* - If used within steps or evaluators, a compilation error will be raised.
|
|
41
|
+
* - Uses a Temporal Activity to dispatch the HTTP request, working around the runtime limitation for workflows.
|
|
42
|
+
* - Uses a Temporal Trigger to pause the workflow.
|
|
43
|
+
* - Uses a Temporal Signal to resume the workflow when the API responds.
|
|
44
|
+
*
|
|
45
|
+
* @param params - Parameters object
|
|
46
|
+
* @param params.url - Request URL
|
|
47
|
+
* @param params.payload - Request payload
|
|
48
|
+
* @param params.headers - Headers for the request
|
|
49
|
+
* @returns Resolves with the payload received by the webhook
|
|
50
|
+
*/
|
|
51
|
+
export declare function sendPostRequestAndAwaitWebhook( params: {
|
|
52
|
+
url: string;
|
|
53
|
+
payload?: object;
|
|
54
|
+
headers?: Record<string, string>;
|
|
55
|
+
} ): Promise<unknown>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Send an HTTP request to a URL.
|
|
59
|
+
*
|
|
60
|
+
* For POST or PUT requests, an optional payload can be sent as the body.
|
|
61
|
+
*
|
|
62
|
+
* The "Content-Type" is inferred from the payload type and can be overridden via the `headers` argument.
|
|
63
|
+
*
|
|
64
|
+
* If the body is not a type natively accepted by the Fetch API, it is serialized to a string: `JSON.stringify()` for objects, or `String()` for primitives.
|
|
65
|
+
*
|
|
66
|
+
* @remarks
|
|
67
|
+
* - Intended for use within workflow functions; do not use in steps or evaluators.
|
|
68
|
+
* - Steps and evaluators are activity-based and can perform HTTP requests directly.
|
|
69
|
+
* - If used within steps or evaluators, a compilation error will be raised.
|
|
70
|
+
* - Uses a Temporal Activity to dispatch the HTTP request, working around the runtime limitation for workflows.
|
|
71
|
+
*
|
|
72
|
+
* @param params - Parameters object
|
|
73
|
+
* @param params.url - Request URL
|
|
74
|
+
* @param params.method - The HTTP method (default: 'GET')
|
|
75
|
+
* @param params.payload - Request payload (only for POST/PUT)
|
|
76
|
+
* @param params.headers - Headers for the request
|
|
77
|
+
* @returns Resolves with an HTTP response serialized to a plain object
|
|
78
|
+
*/
|
|
79
|
+
export declare function sendHttpRequest( params: {
|
|
80
|
+
url: string;
|
|
81
|
+
method?: HttpMethod;
|
|
82
|
+
payload?: object;
|
|
83
|
+
headers?: Record<string, string>;
|
|
84
|
+
} ): Promise<SerializedFetchResponse>;
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import type { DeepPartial, AnyZodSchema, TemporalActivityOptions } from './types.d.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The second argument passed to the workflow's `fn` function.
|
|
6
|
+
*/
|
|
7
|
+
export type WorkflowContext<
|
|
8
|
+
InputSchema extends AnyZodSchema | undefined = undefined,
|
|
9
|
+
OutputSchema extends AnyZodSchema | undefined = undefined
|
|
10
|
+
> = {
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Functions that allow fine control over the underlying Temporal workflows
|
|
14
|
+
*/
|
|
15
|
+
control: {
|
|
16
|
+
/**
|
|
17
|
+
* Closes the current workflow execution successfully and creates a new workflow execution.
|
|
18
|
+
*
|
|
19
|
+
* The new workflow execution is in the same chain as the previous workflow, but it generates another trace file.
|
|
20
|
+
*
|
|
21
|
+
* It acts as a checkpoint when the workflow gets too long or approaches certain scaling limits.
|
|
22
|
+
*
|
|
23
|
+
* It accepts input with the same schema as the parent workflow function (`inputSchema`).
|
|
24
|
+
*
|
|
25
|
+
* Calling this function must be the last statement in the workflow, accompanied by a `return`:
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```js
|
|
29
|
+
* return control.continueAsNew();
|
|
30
|
+
* ```
|
|
31
|
+
* Upon returning, the parent workflow execution closes without any output, and the new execution takes its place.
|
|
32
|
+
*
|
|
33
|
+
* The function's return type matches `outputSchema`; although no value is returned, the execution is replaced.
|
|
34
|
+
*
|
|
35
|
+
* @see {@link https://docs.temporal.io/develop/typescript/continue-as-new}
|
|
36
|
+
*
|
|
37
|
+
* @param input - The input for the new run. Omit when the workflow has no input schema.
|
|
38
|
+
* @returns The workflow output type for type-checking; never returns at runtime.
|
|
39
|
+
*/
|
|
40
|
+
continueAsNew: InputSchema extends AnyZodSchema ?
|
|
41
|
+
( input: z.infer<InputSchema> ) => ( OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void ) :
|
|
42
|
+
() => ( OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void ),
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Indicates whether the Temporal runtime suggests continuing this workflow as new.
|
|
46
|
+
*
|
|
47
|
+
* Use this to decide whether to `continueAsNew` before long waits or at loop boundaries.
|
|
48
|
+
* Prefer returning the `continueAsNew(...)` call immediately when this becomes `true`.
|
|
49
|
+
*
|
|
50
|
+
* @see {@link https://docs.temporal.io/develop/typescript/continue-as-new#how-to-test}
|
|
51
|
+
*
|
|
52
|
+
* @returns True if a continue-as-new is suggested for the current run; otherwise false.
|
|
53
|
+
*/
|
|
54
|
+
isContinueAsNewSuggested: () => boolean
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Information about the workflow execution
|
|
59
|
+
*/
|
|
60
|
+
info: {
|
|
61
|
+
/**
|
|
62
|
+
* Internal Temporal workflow id.
|
|
63
|
+
*
|
|
64
|
+
* @see {@link https://docs.temporal.io/workflow-execution/workflowid-runid#workflow-id}
|
|
65
|
+
*/
|
|
66
|
+
workflowId: string
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Configuration for workflow invocations.
|
|
72
|
+
*
|
|
73
|
+
* Allows overriding Temporal Activity options for this workflow.
|
|
74
|
+
*/
|
|
75
|
+
export type WorkflowInvocationConfiguration<Context extends WorkflowContext = WorkflowContext> = {
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Temporal activity options for this invocation (overrides the workflow's default activity options).
|
|
79
|
+
*/
|
|
80
|
+
options?: TemporalActivityOptions,
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Configures whether this workflow runs detached.
|
|
84
|
+
* Detached workflows called without explicitly awaiting the result are "fire-and-forget" and may outlive the parent.
|
|
85
|
+
*/
|
|
86
|
+
detached?: boolean,
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Allow to overwrite properties of the "context" of workflows when called in tests environments.
|
|
90
|
+
*/
|
|
91
|
+
context?: DeepPartial<Context>
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Options for a workflow.
|
|
96
|
+
*/
|
|
97
|
+
export type WorkflowOptions = {
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Temporal activity options for activities invoked by this workflow.
|
|
101
|
+
*/
|
|
102
|
+
activityOptions?: TemporalActivityOptions,
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* When `true`, disables trace file generation for this workflow. Only has effect when tracing is enabled.
|
|
106
|
+
*/
|
|
107
|
+
disableTrace?: boolean
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The handler function of a workflow.
|
|
112
|
+
*
|
|
113
|
+
* @param input - The workflow input; it matches the schema defined by `inputSchema`.
|
|
114
|
+
* @param context - A context object with tools and information.
|
|
115
|
+
*
|
|
116
|
+
* @returns A value matching the schema defined by `outputSchema`.
|
|
117
|
+
*/
|
|
118
|
+
export type WorkflowFunction<
|
|
119
|
+
InputSchema extends AnyZodSchema | undefined = undefined,
|
|
120
|
+
OutputSchema extends AnyZodSchema | undefined = undefined
|
|
121
|
+
> = InputSchema extends AnyZodSchema ?
|
|
122
|
+
( input: z.infer<InputSchema>, context: WorkflowContext<InputSchema, OutputSchema> ) =>
|
|
123
|
+
Promise<OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void> :
|
|
124
|
+
( input?: undefined | null, context: WorkflowContext<InputSchema, OutputSchema> ) =>
|
|
125
|
+
Promise<OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void>;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* A wrapper around the user defined `fn` handler function.
|
|
129
|
+
*
|
|
130
|
+
* It accepts the same input and returns the same value, calling the user function inside.
|
|
131
|
+
*
|
|
132
|
+
* The second argument is a WorkflowInvocationConfiguration object, allowing workflows configuration overwrite.
|
|
133
|
+
*
|
|
134
|
+
* It adds input and output validation based on the `inputSchema`, `outputSchema`.
|
|
135
|
+
*
|
|
136
|
+
* @param input - The workflow input; it matches the schema defined by `inputSchema`.
|
|
137
|
+
* @param config - Additional configuration for the invocation.
|
|
138
|
+
* @returns A value matching the schema defined by `outputSchema`.
|
|
139
|
+
*/
|
|
140
|
+
export type WorkflowFunctionWrapper<WorkflowFunction> =
|
|
141
|
+
[Parameters<WorkflowFunction>[0]] extends [undefined | null] ?
|
|
142
|
+
( input?: undefined | null, config?: WorkflowInvocationConfiguration<Parameters<WorkflowFunction>[1]> ) =>
|
|
143
|
+
ReturnType<WorkflowFunction> :
|
|
144
|
+
( input: Parameters<WorkflowFunction>[0], config?: WorkflowInvocationConfiguration<Parameters<WorkflowFunction>[1]> ) =>
|
|
145
|
+
ReturnType<WorkflowFunction>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Creates a workflow.
|
|
149
|
+
*
|
|
150
|
+
* A workflow is an orchestration of one or more steps. It is translated to a Temporal Workflow.
|
|
151
|
+
*
|
|
152
|
+
* The workflow logic is defined in the `fn` handler function.
|
|
153
|
+
*
|
|
154
|
+
* The schema of the input that the function receives as the first argument is defined by `inputSchema`.
|
|
155
|
+
*
|
|
156
|
+
* The output of the `fn` handler must match `outputSchema`; otherwise, a validation error is raised.
|
|
157
|
+
*
|
|
158
|
+
* @remarks
|
|
159
|
+
* - Workflows should respect the same limitations as Temporal workflows.
|
|
160
|
+
* - Workflows can invoke steps or evaluators and cannot perform I/O directly.
|
|
161
|
+
* - The workflow `name` needs to be unique across all workflows in the project.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```
|
|
165
|
+
* import { step } from './my_steps.ts';
|
|
166
|
+
*
|
|
167
|
+
* workflow( {
|
|
168
|
+
* name: 'main',
|
|
169
|
+
* description: 'A generic workflow',
|
|
170
|
+
* inputSchema: z.object( {
|
|
171
|
+
* value: z.number()
|
|
172
|
+
* } ),
|
|
173
|
+
* outputSchema: z.string(),
|
|
174
|
+
* fn: async input => {
|
|
175
|
+
* const result = await step( input.value );
|
|
176
|
+
* return result as string;
|
|
177
|
+
* }
|
|
178
|
+
* } )
|
|
179
|
+
* ```
|
|
180
|
+
*
|
|
181
|
+
* @example Workflow without outputSchema
|
|
182
|
+
* ```
|
|
183
|
+
* import { step } from './my_steps.ts';
|
|
184
|
+
*
|
|
185
|
+
* workflow( {
|
|
186
|
+
* name: 'main',
|
|
187
|
+
* description: 'A generic workflow',
|
|
188
|
+
* inputSchema: z.object( {
|
|
189
|
+
* value: z.number()
|
|
190
|
+
* } ),
|
|
191
|
+
* fn: async input => {
|
|
192
|
+
* await step( input.value );
|
|
193
|
+
* }
|
|
194
|
+
* } )
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @example Workflow without inputSchema
|
|
198
|
+
* ```
|
|
199
|
+
* import { step } from './my_steps.ts';
|
|
200
|
+
*
|
|
201
|
+
* workflow( {
|
|
202
|
+
* name: 'main',
|
|
203
|
+
* description: 'A generic workflow',
|
|
204
|
+
* outputSchema: z.string(),
|
|
205
|
+
* fn: async () => {
|
|
206
|
+
* const result = await step();
|
|
207
|
+
* return result as string;
|
|
208
|
+
* }
|
|
209
|
+
* } )
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* @example Workflow without inputSchema and outputSchema
|
|
213
|
+
* ```
|
|
214
|
+
* import { step } from './my_steps.ts';
|
|
215
|
+
*
|
|
216
|
+
* workflow( {
|
|
217
|
+
* name: 'main',
|
|
218
|
+
* description: 'A generic workflow',
|
|
219
|
+
* fn: async () => {
|
|
220
|
+
* await step();
|
|
221
|
+
* }
|
|
222
|
+
* } )
|
|
223
|
+
* ```
|
|
224
|
+
*
|
|
225
|
+
* @example Using continueAsNew
|
|
226
|
+
* The function `continueAsNew` (same as Temporal) can be used to create a new workflow with the same ID and pass different input.
|
|
227
|
+
*
|
|
228
|
+
* ```
|
|
229
|
+
* import { step } from './my_steps.ts';
|
|
230
|
+
*
|
|
231
|
+
* workflow( {
|
|
232
|
+
* name: 'main',
|
|
233
|
+
* description: 'A generic workflow',
|
|
234
|
+
* inputSchema: z.object( {
|
|
235
|
+
* value: z.number()
|
|
236
|
+
* } ),
|
|
237
|
+
* outputSchema: z.string(),
|
|
238
|
+
* fn: async ( input, context ) => {
|
|
239
|
+
* const result = await step( input.value );
|
|
240
|
+
* if ( context.control.isContinueAsNewSuggested() ) {
|
|
241
|
+
* return context.control.continueAsNew( input );
|
|
242
|
+
* }
|
|
243
|
+
*
|
|
244
|
+
* return result as string;
|
|
245
|
+
* }
|
|
246
|
+
* } )
|
|
247
|
+
* ```
|
|
248
|
+
* @typeParam InputSchema - Zod schema of the fn's input.
|
|
249
|
+
* @typeParam OutputSchema - Zod schema of the fn's return.
|
|
250
|
+
*
|
|
251
|
+
* @throws {@link ValidationError}
|
|
252
|
+
* @throws {@link FatalError}
|
|
253
|
+
*
|
|
254
|
+
* @param params - Workflow parameters
|
|
255
|
+
* @param params.name - Human-readable workflow name (must start with a letter or underscore, followed by letters, numbers, or underscores).
|
|
256
|
+
* @param params.description - Description of the workflow
|
|
257
|
+
* @param params.inputSchema - Zod schema for workflow input
|
|
258
|
+
* @param params.outputSchema - Zod schema for workflow output
|
|
259
|
+
* @param params.fn - A function containing the workflow code
|
|
260
|
+
* @param params.options - Optional workflow options.
|
|
261
|
+
* @returns The same handler function set at `fn` with a different signature
|
|
262
|
+
*/
|
|
263
|
+
export declare function workflow<
|
|
264
|
+
InputSchema extends AnyZodSchema | undefined = undefined,
|
|
265
|
+
OutputSchema extends AnyZodSchema | undefined = undefined
|
|
266
|
+
>( params: {
|
|
267
|
+
name: string;
|
|
268
|
+
description?: string;
|
|
269
|
+
inputSchema?: InputSchema;
|
|
270
|
+
outputSchema?: OutputSchema;
|
|
271
|
+
fn: WorkflowFunction<InputSchema, OutputSchema>;
|
|
272
|
+
options?: WorkflowOptions;
|
|
273
|
+
} ): WorkflowFunctionWrapper<WorkflowFunction<InputSchema, OutputSchema>>;
|
|
@@ -3,40 +3,9 @@ import { proxyActivities, inWorkflowContext, executeChild, workflowInfo, uuid4,
|
|
|
3
3
|
import { validateWorkflow } from './validations/static.js';
|
|
4
4
|
import { validateWithSchema } from './validations/runtime.js';
|
|
5
5
|
import { SHARED_STEP_PREFIX, ACTIVITY_GET_TRACE_DESTINATIONS, METADATA_ACCESS_SYMBOL } from '#consts';
|
|
6
|
-
import { deepMerge,
|
|
6
|
+
import { deepMerge, setMetadata, toUrlSafeBase64 } from '#utils';
|
|
7
7
|
import { FatalError, ValidationError } from '#errors';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Context instance builder
|
|
11
|
-
*/
|
|
12
|
-
class Context {
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Builds a new context instance
|
|
16
|
-
* @param {object} options - Arguments to build a new context instance
|
|
17
|
-
* @param {string} workflowId
|
|
18
|
-
* @param {function} continueAsNew
|
|
19
|
-
* @param {function} isContinueAsNewSuggested
|
|
20
|
-
* @returns {object} context
|
|
21
|
-
*/
|
|
22
|
-
static build( { workflowId, continueAsNew, isContinueAsNewSuggested } ) {
|
|
23
|
-
return {
|
|
24
|
-
/**
|
|
25
|
-
* Control namespace: This object adds functions to interact with Temporal flow mechanisms
|
|
26
|
-
*/
|
|
27
|
-
control: {
|
|
28
|
-
continueAsNew,
|
|
29
|
-
isContinueAsNewSuggested
|
|
30
|
-
},
|
|
31
|
-
/**
|
|
32
|
-
* Info namespace: abstracts workflowInfo()
|
|
33
|
-
*/
|
|
34
|
-
info: {
|
|
35
|
-
workflowId
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
};
|
|
8
|
+
import { Context } from './workflow_context.js';
|
|
40
9
|
|
|
41
10
|
const defaultOptions = {
|
|
42
11
|
activityOptions: {
|
|
@@ -53,10 +22,10 @@ const defaultOptions = {
|
|
|
53
22
|
disableTrace: false
|
|
54
23
|
};
|
|
55
24
|
|
|
56
|
-
export function workflow( { name, description, inputSchema, outputSchema, fn, options } ) {
|
|
25
|
+
export function workflow( { name, description, inputSchema, outputSchema, fn, options = {} } ) {
|
|
57
26
|
validateWorkflow( { name, description, inputSchema, outputSchema, fn, options } );
|
|
58
27
|
|
|
59
|
-
const activityOptions =
|
|
28
|
+
const { disableTrace, activityOptions } = deepMerge( defaultOptions, options );
|
|
60
29
|
const steps = proxyActivities( activityOptions );
|
|
61
30
|
|
|
62
31
|
/**
|
|
@@ -71,7 +40,7 @@ export function workflow( { name, description, inputSchema, outputSchema, fn, op
|
|
|
71
40
|
if ( !inWorkflowContext() ) {
|
|
72
41
|
validateWithSchema( inputSchema, input, `Workflow ${name} input` );
|
|
73
42
|
const context = Context.build( { workflowId: 'test-workflow', continueAsNew: async () => {}, isContinueAsNewSuggested: () => false } );
|
|
74
|
-
const output = await fn( input, deepMerge( context, extra.context
|
|
43
|
+
const output = await fn( input, deepMerge( context, extra.context ) );
|
|
75
44
|
validateWithSchema( outputSchema, output, `Workflow ${name} output` );
|
|
76
45
|
return output;
|
|
77
46
|
}
|
|
@@ -89,7 +58,7 @@ export function workflow( { name, description, inputSchema, outputSchema, fn, op
|
|
|
89
58
|
const executionContext = memo.executionContext ?? {
|
|
90
59
|
workflowId,
|
|
91
60
|
workflowName: name,
|
|
92
|
-
disableTrace
|
|
61
|
+
disableTrace,
|
|
93
62
|
startTime: startTime.getTime()
|
|
94
63
|
};
|
|
95
64
|
|
|
@@ -131,7 +100,7 @@ export function workflow( { name, description, inputSchema, outputSchema, fn, op
|
|
|
131
100
|
executionContext,
|
|
132
101
|
parentId: workflowId,
|
|
133
102
|
// new configuration for activities of the child workflow, this will be omitted so it will use what that workflow have defined
|
|
134
|
-
...( extra?.options?.activityOptions && { activityOptions:
|
|
103
|
+
...( extra?.options?.activityOptions && { activityOptions: deepMerge( activityOptions, extra.options.activityOptions ) } )
|
|
135
104
|
}
|
|
136
105
|
} )
|
|
137
106
|
}, input, context );
|