@output.ai/core 0.5.1 → 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 +1 -32
- package/src/interface/workflow.spec.js +462 -46
- 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 -1
- package/src/worker/index.spec.js +180 -0
package/src/index.d.ts
CHANGED
|
@@ -1,24 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Export all types from the interface
|
|
3
|
+
*
|
|
4
|
+
*/
|
|
5
|
+
export * from './interface/index.d.ts';
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
|
-
*
|
|
7
|
-
* - Objects: All properties become optional, recursively.
|
|
8
|
-
* - Functions: Preserved as‑is (only the property itself becomes optional).
|
|
9
|
-
* - Primitives: Returned unchanged.
|
|
10
|
-
* Useful for config overrides with strong IntelliSense on nested fields and methods.
|
|
8
|
+
* Exports all errors
|
|
11
9
|
*/
|
|
12
|
-
|
|
13
|
-
T extends ( ...args: any[] ) => unknown ? T : // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
14
|
-
T extends object ? { [K in keyof T]?: DeepPartial<T[K]> } :
|
|
15
|
-
T;
|
|
10
|
+
export * from './errors.d.ts';
|
|
16
11
|
|
|
17
12
|
/**
|
|
18
13
|
* Expose z from Zod as a convenience.
|
|
19
14
|
*/
|
|
20
15
|
export { z } from 'zod';
|
|
21
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Continue the workflow as a new execution with fresh history.
|
|
19
|
+
*
|
|
20
|
+
* Re-exported from Temporal for advanced use cases. Prefer using
|
|
21
|
+
* `context.control.continueAsNew()` within workflows for type-safe usage.
|
|
22
|
+
*
|
|
23
|
+
* @see {@link https://docs.temporal.io/develop/typescript/continue-as-new}
|
|
24
|
+
*/
|
|
25
|
+
export { continueAsNew } from '@temporalio/workflow';
|
|
26
|
+
|
|
22
27
|
/**
|
|
23
28
|
* Exports Temporal's sleep() function for advanced use cases.
|
|
24
29
|
* Pause workflow execution for a specified duration.
|
|
@@ -42,855 +47,3 @@ export { z } from 'zod';
|
|
|
42
47
|
*
|
|
43
48
|
*/
|
|
44
49
|
export function sleep( ms: number | string ): Promise<void>;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Continue the workflow as a new execution with fresh history.
|
|
48
|
-
*
|
|
49
|
-
* Re-exported from Temporal for advanced use cases. Prefer using
|
|
50
|
-
* `context.control.continueAsNew()` within workflows for type-safe usage.
|
|
51
|
-
*
|
|
52
|
-
* @see {@link https://docs.temporal.io/develop/typescript/continue-as-new}
|
|
53
|
-
*/
|
|
54
|
-
export { continueAsNew } from '@temporalio/workflow';
|
|
55
|
-
|
|
56
|
-
/*
|
|
57
|
-
╭─────────────────────────╮
|
|
58
|
-
│ C O M M O N T Y P E S │╮
|
|
59
|
-
╰─────────────────────────╯│
|
|
60
|
-
╰─────────────────────────╯
|
|
61
|
-
*/
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Type alias for any Zod schema type.
|
|
65
|
-
*/
|
|
66
|
-
export type AnyZodSchema = z.ZodType<any, any, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Allowed HTTP methods for request helpers.
|
|
70
|
-
*/
|
|
71
|
-
export type HttpMethod = 'HEAD' | 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Native Temporal configurations for activities.
|
|
75
|
-
*
|
|
76
|
-
* All native options are accepted except 'versioningIntent', 'taskQueue', 'allowEagerDispatch'.
|
|
77
|
-
*
|
|
78
|
-
* @see {@link https://typescript.temporal.io/api/interfaces/common.ActivityOptions}
|
|
79
|
-
*/
|
|
80
|
-
export type TemporalActivityOptions = Omit<ActivityOptions, 'versioningIntent' | 'taskQueue' | 'allowEagerDispatch'>;
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Result of a single job executed by `executeInParallel`.
|
|
84
|
-
*
|
|
85
|
-
* @typeParam T - The return type of the job function
|
|
86
|
-
*/
|
|
87
|
-
export type ParallelJobResult<T> =
|
|
88
|
-
| { ok: true; result: T; index: number } |
|
|
89
|
-
{ ok: false; error: unknown; index: number };
|
|
90
|
-
|
|
91
|
-
/*
|
|
92
|
-
╭─────────╮
|
|
93
|
-
│ S T E P │╮
|
|
94
|
-
╰─────────╯│
|
|
95
|
-
╰─────────╯
|
|
96
|
-
*/
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Options for a step.
|
|
100
|
-
*/
|
|
101
|
-
export type StepOptions = {
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Temporal activity options for this step.
|
|
105
|
-
*/
|
|
106
|
-
activityOptions?: TemporalActivityOptions
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* The handler function of a step.
|
|
111
|
-
*
|
|
112
|
-
* @param input - The step input; it matches the schema defined by `inputSchema`.
|
|
113
|
-
*
|
|
114
|
-
* @returns A value matching the schema defined by `outputSchema`.
|
|
115
|
-
*/
|
|
116
|
-
export type StepFunction<
|
|
117
|
-
InputSchema extends AnyZodSchema | undefined = undefined,
|
|
118
|
-
OutputSchema extends AnyZodSchema | undefined = undefined
|
|
119
|
-
> = InputSchema extends AnyZodSchema ?
|
|
120
|
-
( input: z.infer<InputSchema> ) => Promise<OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void> :
|
|
121
|
-
() => Promise<OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void>;
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* A wrapper around the user defined `fn` handler function.
|
|
125
|
-
*
|
|
126
|
-
* It accepts the same input and returns the same value, calling the user function inside.
|
|
127
|
-
*
|
|
128
|
-
* It adds input and output validation based on the `inputSchema`, `outputSchema`.
|
|
129
|
-
*
|
|
130
|
-
* @param input - The Step input; it matches the schema defined by `inputSchema`.
|
|
131
|
-
* @returns A value matching the schema defined by `outputSchema`.
|
|
132
|
-
*/
|
|
133
|
-
export type StepFunctionWrapper<StepFunction> =
|
|
134
|
-
Parameters<StepFunction> extends [infer Input] ?
|
|
135
|
-
( input: Input ) => ReturnType<StepFunction> :
|
|
136
|
-
() => ReturnType<StepFunction>;
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Creates a step.
|
|
140
|
-
*
|
|
141
|
-
* A step is a logical unit of work that can perform I/O. It is translated to a Temporal Activity.
|
|
142
|
-
*
|
|
143
|
-
* The step logic is defined in the `fn` handler function.
|
|
144
|
-
*
|
|
145
|
-
* The schema of the input that the function receives as the first argument is defined by the `inputSchema` option.
|
|
146
|
-
*
|
|
147
|
-
* The output of the `fn` handler must match the schema defined by `outputSchema`; otherwise, a validation error is raised.
|
|
148
|
-
*
|
|
149
|
-
* @example
|
|
150
|
-
* ```
|
|
151
|
-
* step( {
|
|
152
|
-
* name: 'process',
|
|
153
|
-
* description: 'A generic process',
|
|
154
|
-
* inputSchema: z.object( {
|
|
155
|
-
* value: z.number()
|
|
156
|
-
* } ),
|
|
157
|
-
* outputSchema: z.string(),
|
|
158
|
-
* fn: async input => {
|
|
159
|
-
* const result = await ai.call( input.value );
|
|
160
|
-
* return result as string;
|
|
161
|
-
* }
|
|
162
|
-
* } )
|
|
163
|
-
* ```
|
|
164
|
-
*
|
|
165
|
-
* @example Step without outputSchema
|
|
166
|
-
* ```
|
|
167
|
-
* step( {
|
|
168
|
-
* name: 'process',
|
|
169
|
-
* description: 'A generic process',
|
|
170
|
-
* inputSchema: z.object( {
|
|
171
|
-
* value: z.number()
|
|
172
|
-
* } ),
|
|
173
|
-
* fn: async input => {
|
|
174
|
-
* await ai.call( input.value );
|
|
175
|
-
* }
|
|
176
|
-
* } )
|
|
177
|
-
* ```
|
|
178
|
-
*
|
|
179
|
-
* @example Step without inputSchema
|
|
180
|
-
* ```
|
|
181
|
-
* step( {
|
|
182
|
-
* name: 'process',
|
|
183
|
-
* description: 'A generic process',
|
|
184
|
-
* outputSchema: z.string(),
|
|
185
|
-
* fn: async () => {
|
|
186
|
-
* const result = await ai.call();
|
|
187
|
-
* return result as string;
|
|
188
|
-
* }
|
|
189
|
-
* } )
|
|
190
|
-
* ```
|
|
191
|
-
*
|
|
192
|
-
* @example Step without inputSchema and outputSchema
|
|
193
|
-
* ```
|
|
194
|
-
* step( {
|
|
195
|
-
* name: 'process',
|
|
196
|
-
* description: 'A generic process',
|
|
197
|
-
* fn: async () => {
|
|
198
|
-
* await ai.call();
|
|
199
|
-
* }
|
|
200
|
-
* } )
|
|
201
|
-
* ```
|
|
202
|
-
*
|
|
203
|
-
* @remarks
|
|
204
|
-
* - Never call another step from within a step.
|
|
205
|
-
* - Never call a workflow from within a step.
|
|
206
|
-
*
|
|
207
|
-
* @typeParam InputSchema - Zod schema of the fn's input.
|
|
208
|
-
* @typeParam OutputSchema - Zod schema of the fn's return.
|
|
209
|
-
*
|
|
210
|
-
* @throws {@link ValidationError}
|
|
211
|
-
* @throws {@link FatalError}
|
|
212
|
-
*
|
|
213
|
-
* @param params - Step parameters
|
|
214
|
-
* @param params.name - Human-readable step name (must start with a letter or underscore, followed by letters, numbers, or underscores)
|
|
215
|
-
* @param params.description - Description of the step
|
|
216
|
-
* @param params.inputSchema - Zod schema for the `fn` input
|
|
217
|
-
* @param params.outputSchema - Zod schema for the `fn` output
|
|
218
|
-
* @param params.fn - A handler function containing the step code
|
|
219
|
-
* @param params.options - Optional step options.
|
|
220
|
-
* @returns The same handler function set at `fn`
|
|
221
|
-
*/
|
|
222
|
-
export declare function step<
|
|
223
|
-
InputSchema extends AnyZodSchema | undefined = undefined,
|
|
224
|
-
OutputSchema extends AnyZodSchema | undefined = undefined
|
|
225
|
-
>( params: {
|
|
226
|
-
name: string;
|
|
227
|
-
description?: string;
|
|
228
|
-
inputSchema?: InputSchema;
|
|
229
|
-
outputSchema?: OutputSchema;
|
|
230
|
-
fn: StepFunction<InputSchema, OutputSchema>;
|
|
231
|
-
options?: StepOptions;
|
|
232
|
-
} ): StepFunctionWrapper<StepFunction<InputSchema, OutputSchema>>;
|
|
233
|
-
|
|
234
|
-
/*
|
|
235
|
-
╭─────────────────╮
|
|
236
|
-
│ W O R K F L O W │╮
|
|
237
|
-
╰─────────────────╯│
|
|
238
|
-
╰─────────────────╯
|
|
239
|
-
*/
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Configuration for workflow invocations.
|
|
243
|
-
*
|
|
244
|
-
* Allows overriding Temporal Activity options for this workflow.
|
|
245
|
-
*/
|
|
246
|
-
export type WorkflowInvocationConfiguration<Context extends WorkflowContext = WorkflowContext> = {
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Temporal activity options for this invocation (overrides the workflow's default activity options).
|
|
250
|
-
*/
|
|
251
|
-
options?: TemporalActivityOptions,
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Configures whether this workflow runs detached.
|
|
255
|
-
* Detached workflows called without explicitly awaiting the result are "fire-and-forget" and may outlive the parent.
|
|
256
|
-
*/
|
|
257
|
-
detached?: boolean
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* Allow to overwrite properties of the "context" of workflows when called in tests environments.
|
|
261
|
-
*/
|
|
262
|
-
context?: DeepPartial<Context>
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Options for a workflow.
|
|
267
|
-
*/
|
|
268
|
-
export type WorkflowOptions = {
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Temporal activity options for activities invoked by this workflow.
|
|
272
|
-
*/
|
|
273
|
-
activityOptions?: TemporalActivityOptions,
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* When `true`, disables trace file generation for this workflow. Only has effect when tracing is enabled.
|
|
277
|
-
*/
|
|
278
|
-
disableTrace?: boolean
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* The second argument passed to the workflow's `fn` function.
|
|
283
|
-
*/
|
|
284
|
-
export type WorkflowContext<
|
|
285
|
-
InputSchema extends AnyZodSchema | undefined = undefined,
|
|
286
|
-
OutputSchema extends AnyZodSchema | undefined = undefined
|
|
287
|
-
> = {
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Functions that allow fine control over the underlying Temporal workflows
|
|
291
|
-
*/
|
|
292
|
-
control: {
|
|
293
|
-
/**
|
|
294
|
-
* Closes the current workflow execution successfully and creates a new workflow execution.
|
|
295
|
-
*
|
|
296
|
-
* The new workflow execution is in the same chain as the previous workflow, but it generates another trace file.
|
|
297
|
-
*
|
|
298
|
-
* It acts as a checkpoint when the workflow gets too long or approaches certain scaling limits.
|
|
299
|
-
*
|
|
300
|
-
* It accepts input with the same schema as the parent workflow function (`inputSchema`).
|
|
301
|
-
*
|
|
302
|
-
* Calling this function must be the last statement in the workflow, accompanied by a `return`:
|
|
303
|
-
*
|
|
304
|
-
* @example
|
|
305
|
-
* ```js
|
|
306
|
-
* return control.continueAsNew();
|
|
307
|
-
* ```
|
|
308
|
-
* Upon returning, the parent workflow execution closes without any output, and the new execution takes its place.
|
|
309
|
-
*
|
|
310
|
-
* The function's return type matches `outputSchema`; although no value is returned, the execution is replaced.
|
|
311
|
-
*
|
|
312
|
-
* @see {@link https://docs.temporal.io/develop/typescript/continue-as-new}
|
|
313
|
-
*
|
|
314
|
-
* @param input - The input for the new run. Omit when the workflow has no input schema.
|
|
315
|
-
* @returns The workflow output type for type-checking; never returns at runtime.
|
|
316
|
-
*/
|
|
317
|
-
continueAsNew: InputSchema extends AnyZodSchema ?
|
|
318
|
-
( input: z.infer<InputSchema> ) => ( OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void ) :
|
|
319
|
-
() => ( OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void ),
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* Indicates whether the Temporal runtime suggests continuing this workflow as new.
|
|
323
|
-
*
|
|
324
|
-
* Use this to decide whether to `continueAsNew` before long waits or at loop boundaries.
|
|
325
|
-
* Prefer returning the `continueAsNew(...)` call immediately when this becomes `true`.
|
|
326
|
-
*
|
|
327
|
-
* @see {@link https://docs.temporal.io/develop/typescript/continue-as-new#how-to-test}
|
|
328
|
-
*
|
|
329
|
-
* @returns True if a continue-as-new is suggested for the current run; otherwise false.
|
|
330
|
-
*/
|
|
331
|
-
isContinueAsNewSuggested: () => boolean
|
|
332
|
-
},
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* Information about the workflow execution
|
|
336
|
-
*/
|
|
337
|
-
info: {
|
|
338
|
-
/**
|
|
339
|
-
* Internal Temporal workflow id.
|
|
340
|
-
*
|
|
341
|
-
* @see {@link https://docs.temporal.io/workflow-execution/workflowid-runid#workflow-id}
|
|
342
|
-
*/
|
|
343
|
-
workflowId: string
|
|
344
|
-
}
|
|
345
|
-
};
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
* The handler function of a workflow.
|
|
349
|
-
*
|
|
350
|
-
* @param input - The workflow input; it matches the schema defined by `inputSchema`.
|
|
351
|
-
* @param context - A context object with tools and information.
|
|
352
|
-
*
|
|
353
|
-
* @returns A value matching the schema defined by `outputSchema`.
|
|
354
|
-
*/
|
|
355
|
-
export type WorkflowFunction<
|
|
356
|
-
InputSchema extends AnyZodSchema | undefined = undefined,
|
|
357
|
-
OutputSchema extends AnyZodSchema | undefined = undefined
|
|
358
|
-
> = InputSchema extends AnyZodSchema ?
|
|
359
|
-
( input: z.infer<InputSchema>, context: WorkflowContext<InputSchema, OutputSchema> ) =>
|
|
360
|
-
Promise<OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void> :
|
|
361
|
-
( input?: undefined | null, context: WorkflowContext<InputSchema, OutputSchema> ) =>
|
|
362
|
-
Promise<OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void>;
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
* A wrapper around the user defined `fn` handler function.
|
|
366
|
-
*
|
|
367
|
-
* It accepts the same input and returns the same value, calling the user function inside.
|
|
368
|
-
*
|
|
369
|
-
* The second argument is a WorkflowInvocationConfiguration object, allowing workflows configuration overwrite.
|
|
370
|
-
*
|
|
371
|
-
* It adds input and output validation based on the `inputSchema`, `outputSchema`.
|
|
372
|
-
*
|
|
373
|
-
* @param input - The workflow input; it matches the schema defined by `inputSchema`.
|
|
374
|
-
* @param config - Additional configuration for the invocation.
|
|
375
|
-
* @returns A value matching the schema defined by `outputSchema`.
|
|
376
|
-
*/
|
|
377
|
-
export type WorkflowFunctionWrapper<WorkflowFunction> =
|
|
378
|
-
[Parameters<WorkflowFunction>[0]] extends [undefined | null] ?
|
|
379
|
-
( input?: undefined | null, config?: WorkflowInvocationConfiguration<Parameters<WorkflowFunction>[1]> ) =>
|
|
380
|
-
ReturnType<WorkflowFunction> :
|
|
381
|
-
( input: Parameters<WorkflowFunction>[0], config?: WorkflowInvocationConfiguration<Parameters<WorkflowFunction>[1]> ) =>
|
|
382
|
-
ReturnType<WorkflowFunction>;
|
|
383
|
-
|
|
384
|
-
/**
|
|
385
|
-
* Creates a workflow.
|
|
386
|
-
*
|
|
387
|
-
* A workflow is an orchestration of one or more steps. It is translated to a Temporal Workflow.
|
|
388
|
-
*
|
|
389
|
-
* The workflow logic is defined in the `fn` handler function.
|
|
390
|
-
*
|
|
391
|
-
* The schema of the input that the function receives as the first argument is defined by `inputSchema`.
|
|
392
|
-
*
|
|
393
|
-
* The output of the `fn` handler must match `outputSchema`; otherwise, a validation error is raised.
|
|
394
|
-
*
|
|
395
|
-
* @remarks
|
|
396
|
-
* - Workflows should respect the same limitations as Temporal workflows.
|
|
397
|
-
* - Workflows can invoke steps or evaluators and cannot perform I/O directly.
|
|
398
|
-
* - The workflow `name` needs to be unique across all workflows in the project.
|
|
399
|
-
*
|
|
400
|
-
* @example
|
|
401
|
-
* ```
|
|
402
|
-
* import { step } from './my_steps.ts';
|
|
403
|
-
*
|
|
404
|
-
* workflow( {
|
|
405
|
-
* name: 'main',
|
|
406
|
-
* description: 'A generic workflow',
|
|
407
|
-
* inputSchema: z.object( {
|
|
408
|
-
* value: z.number()
|
|
409
|
-
* } ),
|
|
410
|
-
* outputSchema: z.string(),
|
|
411
|
-
* fn: async input => {
|
|
412
|
-
* const result = await step( input.value );
|
|
413
|
-
* return result as string;
|
|
414
|
-
* }
|
|
415
|
-
* } )
|
|
416
|
-
* ```
|
|
417
|
-
*
|
|
418
|
-
* @example Workflow without outputSchema
|
|
419
|
-
* ```
|
|
420
|
-
* import { step } from './my_steps.ts';
|
|
421
|
-
*
|
|
422
|
-
* workflow( {
|
|
423
|
-
* name: 'main',
|
|
424
|
-
* description: 'A generic workflow',
|
|
425
|
-
* inputSchema: z.object( {
|
|
426
|
-
* value: z.number()
|
|
427
|
-
* } ),
|
|
428
|
-
* fn: async input => {
|
|
429
|
-
* await step( input.value );
|
|
430
|
-
* }
|
|
431
|
-
* } )
|
|
432
|
-
* ```
|
|
433
|
-
*
|
|
434
|
-
* @example Workflow without inputSchema
|
|
435
|
-
* ```
|
|
436
|
-
* import { step } from './my_steps.ts';
|
|
437
|
-
*
|
|
438
|
-
* workflow( {
|
|
439
|
-
* name: 'main',
|
|
440
|
-
* description: 'A generic workflow',
|
|
441
|
-
* outputSchema: z.string(),
|
|
442
|
-
* fn: async () => {
|
|
443
|
-
* const result = await step();
|
|
444
|
-
* return result as string;
|
|
445
|
-
* }
|
|
446
|
-
* } )
|
|
447
|
-
* ```
|
|
448
|
-
*
|
|
449
|
-
* @example Workflow without inputSchema and outputSchema
|
|
450
|
-
* ```
|
|
451
|
-
* import { step } from './my_steps.ts';
|
|
452
|
-
*
|
|
453
|
-
* workflow( {
|
|
454
|
-
* name: 'main',
|
|
455
|
-
* description: 'A generic workflow',
|
|
456
|
-
* fn: async () => {
|
|
457
|
-
* await step();
|
|
458
|
-
* }
|
|
459
|
-
* } )
|
|
460
|
-
* ```
|
|
461
|
-
*
|
|
462
|
-
* @example Using continueAsNew
|
|
463
|
-
* The function `continueAsNew` (same as Temporal) can be used to create a new workflow with the same ID and pass different input.
|
|
464
|
-
*
|
|
465
|
-
* ```
|
|
466
|
-
* import { step } from './my_steps.ts';
|
|
467
|
-
*
|
|
468
|
-
* workflow( {
|
|
469
|
-
* name: 'main',
|
|
470
|
-
* description: 'A generic workflow',
|
|
471
|
-
* inputSchema: z.object( {
|
|
472
|
-
* value: z.number()
|
|
473
|
-
* } ),
|
|
474
|
-
* outputSchema: z.string(),
|
|
475
|
-
* fn: async ( input, context ) => {
|
|
476
|
-
* const result = await step( input.value );
|
|
477
|
-
* if ( context.control.isContinueAsNewSuggested() ) {
|
|
478
|
-
* return context.control.continueAsNew( input );
|
|
479
|
-
* }
|
|
480
|
-
*
|
|
481
|
-
* return result as string;
|
|
482
|
-
* }
|
|
483
|
-
* } )
|
|
484
|
-
* ```
|
|
485
|
-
* @typeParam InputSchema - Zod schema of the fn's input.
|
|
486
|
-
* @typeParam OutputSchema - Zod schema of the fn's return.
|
|
487
|
-
*
|
|
488
|
-
* @throws {@link ValidationError}
|
|
489
|
-
* @throws {@link FatalError}
|
|
490
|
-
*
|
|
491
|
-
* @param params - Workflow parameters
|
|
492
|
-
* @param params.name - Human-readable workflow name (must start with a letter or underscore, followed by letters, numbers, or underscores).
|
|
493
|
-
* @param params.description - Description of the workflow
|
|
494
|
-
* @param params.inputSchema - Zod schema for workflow input
|
|
495
|
-
* @param params.outputSchema - Zod schema for workflow output
|
|
496
|
-
* @param params.fn - A function containing the workflow code
|
|
497
|
-
* @param params.options - Optional workflow options.
|
|
498
|
-
* @returns The same handler function set at `fn` with a different signature
|
|
499
|
-
*/
|
|
500
|
-
export declare function workflow<
|
|
501
|
-
InputSchema extends AnyZodSchema | undefined = undefined,
|
|
502
|
-
OutputSchema extends AnyZodSchema | undefined = undefined
|
|
503
|
-
>( params: {
|
|
504
|
-
name: string;
|
|
505
|
-
description?: string;
|
|
506
|
-
inputSchema?: InputSchema;
|
|
507
|
-
outputSchema?: OutputSchema;
|
|
508
|
-
fn: WorkflowFunction<InputSchema, OutputSchema>;
|
|
509
|
-
options?: WorkflowOptions;
|
|
510
|
-
} ): WorkflowFunctionWrapper<WorkflowFunction<InputSchema, OutputSchema>>;
|
|
511
|
-
|
|
512
|
-
/*
|
|
513
|
-
╭───────────────────╮
|
|
514
|
-
│ E V A L U A T O R │╮
|
|
515
|
-
╰───────────────────╯│
|
|
516
|
-
╰───────────────────╯
|
|
517
|
-
*/
|
|
518
|
-
|
|
519
|
-
/**
|
|
520
|
-
* A single feedback for an EvaluationResult
|
|
521
|
-
*/
|
|
522
|
-
export class EvaluationFeedback {
|
|
523
|
-
/**
|
|
524
|
-
* Issue found
|
|
525
|
-
*/
|
|
526
|
-
issue: string;
|
|
527
|
-
|
|
528
|
-
/**
|
|
529
|
-
* Improvement suggestion
|
|
530
|
-
*/
|
|
531
|
-
suggestion?: string;
|
|
532
|
-
|
|
533
|
-
/**
|
|
534
|
-
* Reference for the issue
|
|
535
|
-
*/
|
|
536
|
-
reference?: string;
|
|
537
|
-
|
|
538
|
-
/**
|
|
539
|
-
* Issue priority
|
|
540
|
-
*/
|
|
541
|
-
priority?: 'low' | 'medium' | 'high' | 'critical';
|
|
542
|
-
|
|
543
|
-
/**
|
|
544
|
-
* @constructor
|
|
545
|
-
* @param args
|
|
546
|
-
* @param args.issue
|
|
547
|
-
* @param args.suggestion
|
|
548
|
-
* @param args.reference
|
|
549
|
-
* @param args.priority
|
|
550
|
-
*/
|
|
551
|
-
constructor( args: {
|
|
552
|
-
issue: string;
|
|
553
|
-
suggestion?: string;
|
|
554
|
-
reference?: string;
|
|
555
|
-
priority?: 'low' | 'medium' | 'high' | 'critical';
|
|
556
|
-
} );
|
|
557
|
-
|
|
558
|
-
/**
|
|
559
|
-
* @returns The zod schema for this class
|
|
560
|
-
*/
|
|
561
|
-
static get schema(): z.ZodType;
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
/**
|
|
565
|
-
* Base constructor arguments for EvaluationResult classes
|
|
566
|
-
*/
|
|
567
|
-
export type EvaluationResultArgs<TValue = any> = { // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
568
|
-
/**
|
|
569
|
-
* The value of the evaluation
|
|
570
|
-
*/
|
|
571
|
-
value: TValue;
|
|
572
|
-
/**
|
|
573
|
-
* The confidence in the evaluation
|
|
574
|
-
*/
|
|
575
|
-
confidence: number;
|
|
576
|
-
/**
|
|
577
|
-
* The name of the evaluation
|
|
578
|
-
*/
|
|
579
|
-
name?: string;
|
|
580
|
-
/**
|
|
581
|
-
* The reasoning behind the result
|
|
582
|
-
*/
|
|
583
|
-
reasoning?: string;
|
|
584
|
-
/**
|
|
585
|
-
* Feedback for this evaluation
|
|
586
|
-
*/
|
|
587
|
-
feedback?: EvaluationFeedback[];
|
|
588
|
-
/**
|
|
589
|
-
* Dimensions of this evaluation
|
|
590
|
-
*/
|
|
591
|
-
dimensions?: Array<EvaluationStringResult | EvaluationNumberResult | EvaluationBooleanResult>;
|
|
592
|
-
};
|
|
593
|
-
|
|
594
|
-
/**
|
|
595
|
-
* Represents the result of an evaluation.
|
|
596
|
-
*
|
|
597
|
-
* Generic base class; evaluators must return an instance of an EvaluationResult subclass.
|
|
598
|
-
*/
|
|
599
|
-
export class EvaluationResult {
|
|
600
|
-
/**
|
|
601
|
-
* The name of the evaluation result
|
|
602
|
-
*/
|
|
603
|
-
name?: string;
|
|
604
|
-
|
|
605
|
-
/**
|
|
606
|
-
* The evaluation result value
|
|
607
|
-
*/
|
|
608
|
-
value: any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
609
|
-
|
|
610
|
-
/**
|
|
611
|
-
* The evaluation result confidence
|
|
612
|
-
*/
|
|
613
|
-
confidence: number;
|
|
614
|
-
|
|
615
|
-
/**
|
|
616
|
-
* The evaluation result reasoning
|
|
617
|
-
*/
|
|
618
|
-
reasoning?: string;
|
|
619
|
-
|
|
620
|
-
/**
|
|
621
|
-
* Feedback for this evaluation
|
|
622
|
-
*/
|
|
623
|
-
feedback: EvaluationFeedback[];
|
|
624
|
-
|
|
625
|
-
/**
|
|
626
|
-
* Dimensions of this evaluation
|
|
627
|
-
*/
|
|
628
|
-
dimensions: Array<EvaluationStringResult | EvaluationNumberResult | EvaluationBooleanResult>;
|
|
629
|
-
|
|
630
|
-
/**
|
|
631
|
-
* @constructor
|
|
632
|
-
* @param args
|
|
633
|
-
*/
|
|
634
|
-
constructor( args: EvaluationResultArgs );
|
|
635
|
-
|
|
636
|
-
/**
|
|
637
|
-
* @returns The zod schema for this class
|
|
638
|
-
*/
|
|
639
|
-
static get schema(): z.ZodType;
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
/**
|
|
643
|
-
* An evaluation result where the value is a string
|
|
644
|
-
* @extends EvaluationResult
|
|
645
|
-
*/
|
|
646
|
-
export class EvaluationStringResult extends EvaluationResult {
|
|
647
|
-
/**
|
|
648
|
-
* @constructor
|
|
649
|
-
* @param args
|
|
650
|
-
*/
|
|
651
|
-
constructor( args: EvaluationResultArgs<string> );
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
/**
|
|
655
|
-
* An evaluation result where the value is a number
|
|
656
|
-
* @extends EvaluationResult
|
|
657
|
-
*/
|
|
658
|
-
export class EvaluationNumberResult extends EvaluationResult {
|
|
659
|
-
/**
|
|
660
|
-
* @constructor
|
|
661
|
-
* @param args
|
|
662
|
-
*/
|
|
663
|
-
constructor( args: EvaluationResultArgs<number> );
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
/**
|
|
667
|
-
* An evaluation result where the value is a boolean
|
|
668
|
-
* @extends EvaluationResult
|
|
669
|
-
*/
|
|
670
|
-
export class EvaluationBooleanResult extends EvaluationResult {
|
|
671
|
-
/**
|
|
672
|
-
* @constructor
|
|
673
|
-
* @param args
|
|
674
|
-
*/
|
|
675
|
-
constructor( args: EvaluationResultArgs<boolean> );
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
/**
|
|
679
|
-
* Options for an evaluator.
|
|
680
|
-
*/
|
|
681
|
-
export type EvaluatorOptions = {
|
|
682
|
-
|
|
683
|
-
/**
|
|
684
|
-
* Temporal activity options for this evaluator.
|
|
685
|
-
*/
|
|
686
|
-
activityOptions?: TemporalActivityOptions
|
|
687
|
-
};
|
|
688
|
-
|
|
689
|
-
/**
|
|
690
|
-
* The handler function of an evaluator.
|
|
691
|
-
*
|
|
692
|
-
* @param input - The evaluator input; it matches the schema defined by `inputSchema`.
|
|
693
|
-
*
|
|
694
|
-
* @returns The result of the evaluation.
|
|
695
|
-
*/
|
|
696
|
-
export type EvaluatorFunction<
|
|
697
|
-
InputSchema extends AnyZodSchema | undefined = undefined,
|
|
698
|
-
Result extends EvaluationResult
|
|
699
|
-
> = InputSchema extends AnyZodSchema ?
|
|
700
|
-
( input: z.infer<InputSchema> ) => Promise<Result> :
|
|
701
|
-
() => Promise<Result>;
|
|
702
|
-
|
|
703
|
-
/**
|
|
704
|
-
* A wrapper around the user defined `fn` handler function.
|
|
705
|
-
*
|
|
706
|
-
* It has the same signature and returns the same value, calling the user function inside.
|
|
707
|
-
*
|
|
708
|
-
* It adds input validation based on the `inputSchema`.
|
|
709
|
-
*/
|
|
710
|
-
export type EvaluatorFunctionWrapper<EvaluatorFunction> =
|
|
711
|
-
Parameters<EvaluatorFunction> extends [infer Input] ?
|
|
712
|
-
( input: Input ) => ReturnType<EvaluatorFunction> :
|
|
713
|
-
() => ReturnType<EvaluatorFunction>;
|
|
714
|
-
|
|
715
|
-
/**
|
|
716
|
-
* Creates an evaluation function. It is similar to a step, but must return an EvaluationResult.
|
|
717
|
-
*
|
|
718
|
-
* It is translated to a Temporal Activity.
|
|
719
|
-
*
|
|
720
|
-
* @typeParam InputSchema - Zod schema of the fn's input.
|
|
721
|
-
* @typeParam Result - Return type of the fn, extends EvaluationResult.
|
|
722
|
-
*
|
|
723
|
-
* @throws {@link ValidationError}
|
|
724
|
-
* @throws {@link FatalError}
|
|
725
|
-
*
|
|
726
|
-
* @param params - Evaluator parameters
|
|
727
|
-
* @param params.name - Human-readable evaluator name (must start with a letter or underscore, followed by letters, numbers, or underscores)
|
|
728
|
-
* @param params.description - Description of the evaluator
|
|
729
|
-
* @param params.inputSchema - Zod schema for the `fn` input
|
|
730
|
-
* @param params.fn - A function containing the evaluator code
|
|
731
|
-
* @param params.options - Optional evaluator options.
|
|
732
|
-
* @returns A wrapper function around the `fn` function
|
|
733
|
-
*/
|
|
734
|
-
export declare function evaluator<
|
|
735
|
-
InputSchema extends AnyZodSchema,
|
|
736
|
-
Result extends EvaluationResult
|
|
737
|
-
>( params: {
|
|
738
|
-
name: string;
|
|
739
|
-
description?: string;
|
|
740
|
-
inputSchema: InputSchema;
|
|
741
|
-
fn: EvaluatorFunction<InputSchema, Result>;
|
|
742
|
-
options?: EvaluatorOptions;
|
|
743
|
-
} ): EvaluatorFunctionWrapper<EvaluatorFunction<InputSchema, Result>>;
|
|
744
|
-
|
|
745
|
-
/*
|
|
746
|
-
╭───────────╮
|
|
747
|
-
│ T O O L S │╮
|
|
748
|
-
╰───────────╯│
|
|
749
|
-
╰───────────╯
|
|
750
|
-
*/
|
|
751
|
-
|
|
752
|
-
/**
|
|
753
|
-
* Send an POST HTTP request to a URL, optionally with a payload, then wait for a webhook response.
|
|
754
|
-
*
|
|
755
|
-
* The "Content-Type" is inferred from the payload type and can be overridden via the `headers` argument.
|
|
756
|
-
*
|
|
757
|
-
* 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.
|
|
758
|
-
*
|
|
759
|
-
* When a body is sent, the payload is wrapped together with the `workflowId` and sent as:
|
|
760
|
-
* @example
|
|
761
|
-
* ```js
|
|
762
|
-
* const finalPayload = {
|
|
763
|
-
* workflowId,
|
|
764
|
-
* payload
|
|
765
|
-
* }
|
|
766
|
-
* ```
|
|
767
|
-
*
|
|
768
|
-
* 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.
|
|
769
|
-
*
|
|
770
|
-
* @example
|
|
771
|
-
* ```js
|
|
772
|
-
* const response = await sendPostRequestAndAwaitWebhook( {
|
|
773
|
-
* url: 'https://example.com/integration',
|
|
774
|
-
* payload: {
|
|
775
|
-
* }
|
|
776
|
-
* } );
|
|
777
|
-
*
|
|
778
|
-
* assert( response, 'the value sent back via the api' );
|
|
779
|
-
* ```
|
|
780
|
-
*
|
|
781
|
-
* @remarks
|
|
782
|
-
* - Only callable from within a workflow function; do not use in steps or evaluators.
|
|
783
|
-
* - Steps and evaluators are activity-based and are not designed to be paused.
|
|
784
|
-
* - If used within steps or evaluators, a compilation error will be raised.
|
|
785
|
-
* - Uses a Temporal Activity to dispatch the HTTP request, working around the runtime limitation for workflows.
|
|
786
|
-
* - Uses a Temporal Trigger to pause the workflow.
|
|
787
|
-
* - Uses a Temporal Signal to resume the workflow when the API responds.
|
|
788
|
-
*
|
|
789
|
-
* @param params - Parameters object
|
|
790
|
-
* @param params.url - Request URL
|
|
791
|
-
* @param params.payload - Request payload
|
|
792
|
-
* @param params.headers - Headers for the request
|
|
793
|
-
* @returns Resolves with the payload received by the webhook
|
|
794
|
-
*/
|
|
795
|
-
export declare function sendPostRequestAndAwaitWebhook( params: {
|
|
796
|
-
url: string;
|
|
797
|
-
payload?: object;
|
|
798
|
-
headers?: Record<string, string>;
|
|
799
|
-
} ): Promise<unknown>;
|
|
800
|
-
|
|
801
|
-
/**
|
|
802
|
-
* Send an HTTP request to a URL.
|
|
803
|
-
*
|
|
804
|
-
* For POST or PUT requests, an optional payload can be sent as the body.
|
|
805
|
-
*
|
|
806
|
-
* The "Content-Type" is inferred from the payload type and can be overridden via the `headers` argument.
|
|
807
|
-
*
|
|
808
|
-
* 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.
|
|
809
|
-
*
|
|
810
|
-
* @remarks
|
|
811
|
-
* - Intended for use within workflow functions; do not use in steps or evaluators.
|
|
812
|
-
* - Steps and evaluators are activity-based and can perform HTTP requests directly.
|
|
813
|
-
* - If used within steps or evaluators, a compilation error will be raised.
|
|
814
|
-
* - Uses a Temporal Activity to dispatch the HTTP request, working around the runtime limitation for workflows.
|
|
815
|
-
*
|
|
816
|
-
* @param params - Parameters object
|
|
817
|
-
* @param params.url - Request URL
|
|
818
|
-
* @param params.method - The HTTP method (default: 'GET')
|
|
819
|
-
* @param params.payload - Request payload (only for POST/PUT)
|
|
820
|
-
* @param params.headers - Headers for the request
|
|
821
|
-
* @returns Resolves with an HTTP response serialized to a plain object
|
|
822
|
-
*/
|
|
823
|
-
export declare function sendHttpRequest( params: {
|
|
824
|
-
url: string;
|
|
825
|
-
method?: HttpMethod;
|
|
826
|
-
payload?: object;
|
|
827
|
-
headers?: Record<string, string>;
|
|
828
|
-
} ): Promise<SerializedFetchResponse>;
|
|
829
|
-
|
|
830
|
-
/**
|
|
831
|
-
* Execute jobs in parallel with optional concurrency limit.
|
|
832
|
-
*
|
|
833
|
-
* Returns all job results (successes and failures) sorted by original job index.
|
|
834
|
-
* Each result contains `ok` (boolean), `index` (original position), and either
|
|
835
|
-
* `result` (on success) or `error` (on failure).
|
|
836
|
-
*
|
|
837
|
-
* Jobs must be wrapped in arrow functions—do not pass promises directly.
|
|
838
|
-
*
|
|
839
|
-
* @example
|
|
840
|
-
* ```ts
|
|
841
|
-
* const results = await executeInParallel( {
|
|
842
|
-
* jobs: [
|
|
843
|
-
* () => myStep( data1 ),
|
|
844
|
-
* () => myStep( data2 ),
|
|
845
|
-
* () => myStep( data3 )
|
|
846
|
-
* ],
|
|
847
|
-
* concurrency: 2
|
|
848
|
-
* } );
|
|
849
|
-
*
|
|
850
|
-
* // Handle the discriminated union (result only exists when ok is true)
|
|
851
|
-
* const successfulResults = results.filter( r => r.ok ).map( r => r.result );
|
|
852
|
-
*
|
|
853
|
-
* // Or handle each result individually
|
|
854
|
-
* for ( const r of results ) {
|
|
855
|
-
* if ( r.ok ) {
|
|
856
|
-
* console.log( `Job ${r.index} succeeded:`, r.result );
|
|
857
|
-
* } else {
|
|
858
|
-
* console.log( `Job ${r.index} failed:`, r.error );
|
|
859
|
-
* }
|
|
860
|
-
* }
|
|
861
|
-
* ```
|
|
862
|
-
*
|
|
863
|
-
* @param params - Parameters object
|
|
864
|
-
* @param params.jobs - Array of arrow functions returning step/activity calls (not promises directly)
|
|
865
|
-
* @param params.concurrency - Max concurrent jobs (default: Infinity)
|
|
866
|
-
* @param params.onJobCompleted - Optional callback invoked as each job completes (in completion order)
|
|
867
|
-
* @returns Array of results sorted by original job index
|
|
868
|
-
*/
|
|
869
|
-
export declare function executeInParallel<T>( params: {
|
|
870
|
-
jobs: Array<() => Promise<T> | T>;
|
|
871
|
-
concurrency?: number;
|
|
872
|
-
onJobCompleted?: ( result: ParallelJobResult<T> ) => void;
|
|
873
|
-
} ): Promise<Array<ParallelJobResult<T>>>;
|
|
874
|
-
|
|
875
|
-
/*
|
|
876
|
-
╭─────────────╮
|
|
877
|
-
│ E R R O R S │╮
|
|
878
|
-
╰─────────────╯│
|
|
879
|
-
╰─────────────╯
|
|
880
|
-
*/
|
|
881
|
-
|
|
882
|
-
/**
|
|
883
|
-
* Error indicating a non-recoverable failure.
|
|
884
|
-
*
|
|
885
|
-
* Throw this error to end the workflow execution altogether without retries.
|
|
886
|
-
*/
|
|
887
|
-
export class FatalError extends Error { }
|
|
888
|
-
|
|
889
|
-
/**
|
|
890
|
-
* Error indicating invalid input or schema validation issues.
|
|
891
|
-
*
|
|
892
|
-
* This error is thrown when there are validation errors, either in the input or output, for steps, evaluators, and workflows.
|
|
893
|
-
*
|
|
894
|
-
* It will end the workflow execution without retries.
|
|
895
|
-
*/
|
|
896
|
-
export class ValidationError extends Error { }
|