|
| 1 | +/** |
| 2 | + * Example: custom workflow builder wrapper. |
| 3 | + * |
| 4 | + * Annotate any factory function that returns a workflow-builder-like object |
| 5 | + * with `@hatchet-workflow`. The VS Code extension will then detect variables |
| 6 | + * created by this function and render their DAGs, just like standard |
| 7 | + * `hatchet.workflow(...)` calls. |
| 8 | + * |
| 9 | + * Optional tags: |
| 10 | + * @hatchet-task-method — name of the method used to register tasks |
| 11 | + * (defaults to "task") |
| 12 | + * @hatchet-task-parents — name of the option property that lists parent |
| 13 | + * tasks (defaults to "parents") |
| 14 | + */ |
| 15 | +import Hatchet, { |
| 16 | + type Context, |
| 17 | + type WorkflowDeclaration, |
| 18 | +} from '@hatchet-dev/typescript-sdk'; |
| 19 | +import type { CreateWorkflowTaskOpts } from '@hatchet-dev/typescript-sdk'; |
| 20 | + |
| 21 | +// ── Supporting types ────────────────────────────────────────────────────────── |
| 22 | + |
| 23 | +type JsonObject = Record<string, unknown>; |
| 24 | + |
| 25 | +interface Services { |
| 26 | + db: unknown; // replace with your actual DB/service client types |
| 27 | +} |
| 28 | + |
| 29 | +interface TaskOptions<TInput extends JsonObject, TOutput extends JsonObject> { |
| 30 | + /** Business logic to execute for this task. */ |
| 31 | + fn: (args: { input: TInput; services: Services }) => Promise<TOutput>; |
| 32 | + /** Tasks that must complete before this one runs. */ |
| 33 | + parents?: TaskHandle<JsonObject>[]; |
| 34 | + retries?: number; |
| 35 | + timeoutSeconds?: number; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * A lightweight handle returned by `WorkflowBuilder.task()`. |
| 40 | + * Pass these as `parents` to express DAG dependencies. |
| 41 | + */ |
| 42 | +export interface TaskHandle<TOutput extends JsonObject> { |
| 43 | + /** Display name shown in the DAG. */ |
| 44 | + readonly name: string; |
| 45 | + /** @internal Raw Hatchet task opts — used to wire up parents internally. */ |
| 46 | + readonly _taskDef: CreateWorkflowTaskOpts<any, any>; |
| 47 | +} |
| 48 | + |
| 49 | +interface WorkflowBuilderOptions<TInput extends JsonObject> { |
| 50 | + /** Workflow display name (shown in the Hatchet UI and the VS Code DAG view). */ |
| 51 | + name: string; |
| 52 | + version?: string; |
| 53 | + /** Called once per task invocation to produce scoped service clients. */ |
| 54 | + createServices: (input: TInput) => Promise<Services>; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * A fluent builder for Hatchet workflows with injected service context. |
| 59 | + * |
| 60 | + * Usage: |
| 61 | + * ```ts |
| 62 | + * const wf = createWorkflowBuilder({ name: 'my-workflow', createServices }); |
| 63 | + * const step1 = wf.task('step1', { fn: async ({ input, services }) => ... }); |
| 64 | + * const step2 = wf.task('step2', { parents: [step1], fn: async (...) => ... }); |
| 65 | + * export default wf.build(); |
| 66 | + * ``` |
| 67 | + */ |
| 68 | +export interface WorkflowBuilder<TInput extends JsonObject> { |
| 69 | + /** |
| 70 | + * Register a task on this workflow. |
| 71 | + * |
| 72 | + * @param name Unique task name shown in the DAG. |
| 73 | + * @param options Task configuration including the `fn` and optional `parents`. |
| 74 | + */ |
| 75 | + task<TOutput extends JsonObject>( |
| 76 | + name: string, |
| 77 | + options: TaskOptions<TInput, TOutput>, |
| 78 | + ): TaskHandle<TOutput>; |
| 79 | + |
| 80 | + /** Finalise the workflow and return the underlying `WorkflowDeclaration`. */ |
| 81 | + build(): WorkflowDeclaration; |
| 82 | +} |
| 83 | + |
| 84 | +// ── Factory function ────────────────────────────────────────────────────────── |
| 85 | + |
| 86 | +const hatchet = Hatchet.init(); |
| 87 | + |
| 88 | +/** |
| 89 | + * Create a workflow builder that automatically injects service context into |
| 90 | + * every task function. |
| 91 | + * |
| 92 | + * Annotated with `@hatchet-workflow` so the VS Code extension can detect |
| 93 | + * variables created by this function and render their task graphs. |
| 94 | + * |
| 95 | + * @hatchet-workflow |
| 96 | + * @hatchet-task-method task |
| 97 | + * @hatchet-task-parents parents |
| 98 | + */ |
| 99 | +export function createWorkflowBuilder<TInput extends JsonObject>( |
| 100 | + options: WorkflowBuilderOptions<TInput>, |
| 101 | +): WorkflowBuilder<TInput> { |
| 102 | + const wf = hatchet.workflow<TInput>({ name: options.name }); |
| 103 | + |
| 104 | + return { |
| 105 | + task<TOutput extends JsonObject>( |
| 106 | + name: string, |
| 107 | + taskOpts: TaskOptions<TInput, TOutput>, |
| 108 | + ): TaskHandle<TOutput> { |
| 109 | + const wrappedFn = async (input: TInput, _ctx: Context<TInput>): Promise<TOutput> => { |
| 110 | + const services = await options.createServices(input); |
| 111 | + return taskOpts.fn({ input, services }); |
| 112 | + }; |
| 113 | + |
| 114 | + const taskDef = wf.task({ |
| 115 | + name, |
| 116 | + retries: taskOpts.retries, |
| 117 | + ...(taskOpts.timeoutSeconds |
| 118 | + ? { executionTimeout: `${taskOpts.timeoutSeconds}s` } |
| 119 | + : {}), |
| 120 | + parents: taskOpts.parents?.map((p) => p._taskDef), |
| 121 | + fn: wrappedFn, |
| 122 | + }); |
| 123 | + |
| 124 | + return { name, _taskDef: taskDef }; |
| 125 | + }, |
| 126 | + |
| 127 | + build(): WorkflowDeclaration { |
| 128 | + return wf; |
| 129 | + }, |
| 130 | + }; |
| 131 | +} |
0 commit comments