@lunora/workflow 1.0.0-alpha.9 → 1.0.0-alpha.90
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/README.md +32 -7
- package/dist/do/index.d.mts +16 -16
- package/dist/do/index.d.ts +16 -16
- package/dist/do/index.mjs +1 -44
- package/dist/index.d.mts +204 -129
- package/dist/index.d.ts +204 -129
- package/dist/index.mjs +1 -9
- package/dist/packem_shared/MAX_BRANCHES-Bcw3dss9.mjs +1 -0
- package/dist/packem_shared/NonRetryableError-BPu01mU8.mjs +1 -0
- package/dist/packem_shared/WorkflowsRestError-7vFcm7UK.mjs +1 -0
- package/dist/packem_shared/branch-marker-CCpWfS5k.mjs +1 -0
- package/dist/packem_shared/createRunStep-KjGNW5rm.mjs +1 -0
- package/dist/packem_shared/createWaitForEvent-D5Ohn2Pz.mjs +1 -0
- package/dist/packem_shared/createWorkflowContext-DBUnSy0_.mjs +1 -0
- package/dist/packem_shared/createWorkflowRunContext-CRTTub21.mjs +1 -0
- package/dist/packem_shared/createWorkflows-BwR_J9mp.mjs +1 -0
- package/dist/packem_shared/defineStep-D1-9eOnA.mjs +1 -0
- package/dist/packem_shared/defineWorkflow-DynRL6LP.mjs +1 -0
- package/dist/packem_shared/defineWorkflowEvent-suERPEEV.mjs +1 -0
- package/dist/packem_shared/isDuplicateInstanceError-CecnqNAa.mjs +1 -0
- package/dist/packem_shared/run-step-DJ107J5H.mjs +1 -0
- package/dist/packem_shared/types.d-zivJ8nEf.d.mts +686 -0
- package/dist/packem_shared/types.d-zivJ8nEf.d.ts +686 -0
- package/dist/packem_shared/workflow-binding-Dm9hBzx_.mjs +1 -0
- package/package.json +4 -3
- package/dist/packem_shared/MAX_BRANCHES-EO5xYS_3.mjs +0 -170
- package/dist/packem_shared/NonRetryableError-Dn2dTyBS.mjs +0 -27
- package/dist/packem_shared/WorkflowsRestError-zmjOxTR1.mjs +0 -117
- package/dist/packem_shared/createRunStep-8jOXxP2o.mjs +0 -54
- package/dist/packem_shared/createWorkflowContext-oxZ5zj3x.mjs +0 -14
- package/dist/packem_shared/createWorkflowRunContext-BDSHE8iO.mjs +0 -127
- package/dist/packem_shared/createWorkflows-wq4fs72q.mjs +0 -40
- package/dist/packem_shared/defineStep-DJQtLw7g.mjs +0 -28
- package/dist/packem_shared/defineWorkflow-DbUC-oCN.mjs +0 -15
- package/dist/packem_shared/types.d-C9W6rEWv.d.mts +0 -425
- package/dist/packem_shared/types.d-C9W6rEWv.d.ts +0 -425
package/README.md
CHANGED
|
@@ -85,10 +85,33 @@ The handler context bundles:
|
|
|
85
85
|
- `ctx.step` — the native Cloudflare durable-step API (`do` / `sleep` / `sleepUntil` / `waitForEvent`).
|
|
86
86
|
- `ctx.run(ref, args, opts?)` — call a Lunora query / mutation / action; wrap in `ctx.step.do(...)` for durability.
|
|
87
87
|
- `ctx.runStep(step, args, opts?)` — run a reusable, schema-validated `defineStep` as a durable step (see below).
|
|
88
|
+
- `ctx.waitForEvent(event, opts?)` — hibernate until a declared `defineWorkflowEvent` arrives; resolves with its validated payload (see below).
|
|
88
89
|
- `ctx.event` / `ctx.params` — the triggering event and its payload.
|
|
89
90
|
- `ctx.env` — the Worker bindings.
|
|
90
91
|
- `ctx.log` — a workflow-prefixed logger surfaced in `wrangler tail` / Studio.
|
|
91
92
|
|
|
93
|
+
### Declared events (`defineWorkflowEvent`)
|
|
94
|
+
|
|
95
|
+
`step.waitForEvent(name, { type })` and `instance.sendEvent({ type })` match on a bare string, and the payload crosses as `unknown` — so a typo hibernates the instance until its timeout with no error anywhere, and a changed payload shape resumes the workflow on garbage. Declare the event once and both ends import the same value:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
// lunora/events.ts
|
|
99
|
+
import { defineWorkflowEvent } from "@lunora/workflow";
|
|
100
|
+
import { v } from "@lunora/values";
|
|
101
|
+
|
|
102
|
+
export const orderApproved = defineWorkflowEvent("order-approved", v.object({ approvedBy: v.string() }));
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
// inside the workflow body — typed and parsed
|
|
107
|
+
const { approvedBy } = await ctx.waitForEvent(orderApproved, { name: "await approval", timeout: "7 days" });
|
|
108
|
+
|
|
109
|
+
// from a mutation/action — validated before the send
|
|
110
|
+
await ctx.workflows.get("orderPipeline").sendEvent(instanceId, orderApproved, { approvedBy });
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Pass a stable `{ name }` on any wait that can outlive a deploy: the step is otherwise labelled `event:<type>`, so renaming the event type also renames the durable step and an instance that already recorded the wait replays into a fresh one nothing will satisfy.
|
|
114
|
+
|
|
92
115
|
### Reusable steps (`defineStep`)
|
|
93
116
|
|
|
94
117
|
A step authored inline with `ctx.step.do("name", () => …)` is fine for one-offs, but `defineStep` lets you define a step **once** — schema-validated and reusable across workflows. Args are validated (with `@lunora/values`) **before** the body runs, and the return value is validated **after** (when you declare `returns`), so a bad payload fails fast instead of corrupting later steps. Scaffold one with `vis generate lunora-step --name=chargeOrder` (appends to `lunora/steps.ts`), or write it by hand:
|
|
@@ -169,7 +192,9 @@ export const checkout = mutation.input({ orderId: v.string() }).mutation(async (
|
|
|
169
192
|
});
|
|
170
193
|
```
|
|
171
194
|
|
|
172
|
-
A handle exposes `create({ id?, params?, retention? })`, `createBatch([...])
|
|
195
|
+
A handle exposes `create({ id?, params?, retention? })`, `createBatch({ instances })` or `createBatch({ count, params?, retention? })` (returns `{ created, errors }`, one error per instance it did not create), `deleteBatch([...ids])` (up to 100), `get(id)`, and `sendEvent(instanceId, event, payload)` — the typed delivery of a declared event (see above). `create`/`get` return the native Cloudflare instance, which exposes its own lifecycle: `status()`, `pause()`, `resume()`, `restart()`, `terminate()`, `delete()`, `subscribe({ cursor?, filter? })` (streams the instance's event history, then new events), and the untyped `sendEvent({ type, payload })`.
|
|
196
|
+
|
|
197
|
+
`defineWorkflow` also takes deploy settings, which codegen writes into the workflow's `wrangler.jsonc` entry: `schedules` (cron expressions that each start an instance), `limits: { steps }`, and `defaultRetention: { successRetention, errorRetention }`. Scheduled workflows are refused on the Node and celld targets.
|
|
173
198
|
|
|
174
199
|
### Runtime requirements
|
|
175
200
|
|
|
@@ -181,7 +206,7 @@ A handle exposes `create({ id?, params?, retention? })`, `createBatch([...])`, a
|
|
|
181
206
|
### Manual wiring (without codegen)
|
|
182
207
|
|
|
183
208
|
1. Author `lunora/workflows.ts` as above.
|
|
184
|
-
2. Re-export the generated class from your worker entry — wrangler requires every
|
|
209
|
+
2. Re-export the generated class from your worker entry — wrangler requires every workflow export to be a class the worker exports:
|
|
185
210
|
|
|
186
211
|
```ts
|
|
187
212
|
import LunoraWorkflow from "@lunora/workflow/do";
|
|
@@ -194,17 +219,17 @@ A handle exposes `create({ id?, params?, retention? })`, `createBatch([...])`, a
|
|
|
194
219
|
}
|
|
195
220
|
```
|
|
196
221
|
|
|
197
|
-
3.
|
|
222
|
+
3. Declare it in `wrangler.jsonc`'s `exports` (Wrangler 4.142.0+):
|
|
198
223
|
|
|
199
224
|
```jsonc
|
|
200
225
|
{
|
|
201
|
-
"
|
|
226
|
+
"exports": { "OrderPipelineWorkflow": { "type": "workflow", "name": "order-pipeline" } },
|
|
202
227
|
}
|
|
203
228
|
```
|
|
204
229
|
|
|
205
|
-
4. Build `ctx.workflows` from the
|
|
230
|
+
4. Build `ctx.workflows` from the invoking context's exports: `createWorkflows({ bindings: { orderPipeline: ctx.exports.OrderPipelineWorkflow } })`.
|
|
206
231
|
|
|
207
|
-
The `workflowClassName` / `
|
|
232
|
+
The `workflowClassName` / `workflowDefaultName` helpers produce exactly these names so codegen and config never disagree.
|
|
208
233
|
|
|
209
234
|
### Observing instances (REST client)
|
|
210
235
|
|
|
@@ -223,7 +248,7 @@ const detail = await client.getInstance({ workflowName: "order-pipeline", instan
|
|
|
223
248
|
await client.setInstanceStatus({ workflowName: "order-pipeline", instanceId: detail.id, action: "terminate" });
|
|
224
249
|
```
|
|
225
250
|
|
|
226
|
-
> This README covers the basics. For the full API, options, and guides, see the **[documentation](https://lunora.sh/docs/
|
|
251
|
+
> This README covers the basics. For the full API, options, and guides, see the **[documentation](https://lunora.sh/docs/packages/workflow)**.
|
|
227
252
|
|
|
228
253
|
## Related
|
|
229
254
|
|
package/dist/do/index.d.mts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from 'cloudflare:workers';
|
|
2
|
-
import { W as WorkflowDefinition } from "../packem_shared/types.d-
|
|
2
|
+
import { W as WorkflowDefinition } from "../packem_shared/types.d-zivJ8nEf.mjs";
|
|
3
3
|
import '@lunora/values';
|
|
4
4
|
/**
|
|
5
|
-
* Base class for the generated `WorkflowEntrypoint` classes. Applies a
|
|
6
|
-
* `defineWorkflow` definition onto Cloudflare's `WorkflowEntrypoint`: `run`
|
|
7
|
-
* assembles the Lunora context (native `step`/`event` + the `ctx.run` function
|
|
8
|
-
* dispatcher + a logger) and invokes the user's handler.
|
|
9
|
-
*
|
|
10
|
-
* Generated subclasses stay one line of behavior:
|
|
11
|
-
*
|
|
12
|
-
* ```ts
|
|
13
|
-
* export class OrderPipelineWorkflow extends LunoraWorkflow {
|
|
14
|
-
* constructor(ctx: ExecutionContext, env: Record
|
|
15
|
-
* super(ctx, env, orderPipeline, "orderPipeline");
|
|
16
|
-
* }
|
|
17
|
-
* }
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
5
|
+
* Base class for the generated `WorkflowEntrypoint` classes. Applies a
|
|
6
|
+
* `defineWorkflow` definition onto Cloudflare's `WorkflowEntrypoint`: `run`
|
|
7
|
+
* assembles the Lunora context (native `step`/`event` + the `ctx.run` function
|
|
8
|
+
* dispatcher + a logger) and invokes the user's handler.
|
|
9
|
+
*
|
|
10
|
+
* Generated subclasses stay one line of behavior:
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* export class OrderPipelineWorkflow extends LunoraWorkflow {
|
|
14
|
+
* constructor(ctx: ExecutionContext, env: Record<string, unknown>) {
|
|
15
|
+
* super(ctx, env, orderPipeline, "orderPipeline");
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
20
|
declare class LunoraWorkflow<Params = Record<string, unknown>, Output = unknown> extends WorkflowEntrypoint<Record<string, unknown>, Params> {
|
|
21
21
|
#private;
|
|
22
22
|
constructor(context: ConstructorParameters<typeof WorkflowEntrypoint>[0], env: Record<string, unknown>, definition: WorkflowDefinition<Params, Output>, exportName?: string);
|
package/dist/do/index.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from 'cloudflare:workers';
|
|
2
|
-
import { W as WorkflowDefinition } from "../packem_shared/types.d-
|
|
2
|
+
import { W as WorkflowDefinition } from "../packem_shared/types.d-zivJ8nEf.js";
|
|
3
3
|
import '@lunora/values';
|
|
4
4
|
/**
|
|
5
|
-
* Base class for the generated `WorkflowEntrypoint` classes. Applies a
|
|
6
|
-
* `defineWorkflow` definition onto Cloudflare's `WorkflowEntrypoint`: `run`
|
|
7
|
-
* assembles the Lunora context (native `step`/`event` + the `ctx.run` function
|
|
8
|
-
* dispatcher + a logger) and invokes the user's handler.
|
|
9
|
-
*
|
|
10
|
-
* Generated subclasses stay one line of behavior:
|
|
11
|
-
*
|
|
12
|
-
* ```ts
|
|
13
|
-
* export class OrderPipelineWorkflow extends LunoraWorkflow {
|
|
14
|
-
* constructor(ctx: ExecutionContext, env: Record
|
|
15
|
-
* super(ctx, env, orderPipeline, "orderPipeline");
|
|
16
|
-
* }
|
|
17
|
-
* }
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
5
|
+
* Base class for the generated `WorkflowEntrypoint` classes. Applies a
|
|
6
|
+
* `defineWorkflow` definition onto Cloudflare's `WorkflowEntrypoint`: `run`
|
|
7
|
+
* assembles the Lunora context (native `step`/`event` + the `ctx.run` function
|
|
8
|
+
* dispatcher + a logger) and invokes the user's handler.
|
|
9
|
+
*
|
|
10
|
+
* Generated subclasses stay one line of behavior:
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* export class OrderPipelineWorkflow extends LunoraWorkflow {
|
|
14
|
+
* constructor(ctx: ExecutionContext, env: Record<string, unknown>) {
|
|
15
|
+
* super(ctx, env, orderPipeline, "orderPipeline");
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
20
|
declare class LunoraWorkflow<Params = Record<string, unknown>, Output = unknown> extends WorkflowEntrypoint<Record<string, unknown>, Params> {
|
|
21
21
|
#private;
|
|
22
22
|
constructor(context: ConstructorParameters<typeof WorkflowEntrypoint>[0], env: Record<string, unknown>, definition: WorkflowDefinition<Params, Output>, exportName?: string);
|
package/dist/do/index.mjs
CHANGED
|
@@ -1,44 +1 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { NonRetryableError } from 'cloudflare:workflows';
|
|
3
|
-
import { convertNonRetryableError } from '../packem_shared/NonRetryableError-Dn2dTyBS.mjs';
|
|
4
|
-
import { extractBranchMarker, stripBranchMarker, signalBranchParentSafe, errorOutcome, okOutcome } from '../packem_shared/MAX_BRANCHES-EO5xYS_3.mjs';
|
|
5
|
-
import { createWorkflowRunContext } from '../packem_shared/createWorkflowRunContext-BDSHE8iO.mjs';
|
|
6
|
-
|
|
7
|
-
class LunoraWorkflow extends WorkflowEntrypoint {
|
|
8
|
-
/** The `lunora/workflows.ts` export name, for log correlation. */
|
|
9
|
-
#lunoraName;
|
|
10
|
-
/** The `defineWorkflow` result this entrypoint runs. */
|
|
11
|
-
#definition;
|
|
12
|
-
constructor(context, env, definition, exportName) {
|
|
13
|
-
super(context, env);
|
|
14
|
-
this.#definition = definition;
|
|
15
|
-
this.#lunoraName = exportName ?? "workflow";
|
|
16
|
-
}
|
|
17
|
-
async run(event, step) {
|
|
18
|
-
const nativeStep = step;
|
|
19
|
-
const marker = extractBranchMarker(event.payload);
|
|
20
|
-
const handlerEvent = marker ? { ...event, payload: stripBranchMarker(event.payload) } : event;
|
|
21
|
-
const context = createWorkflowRunContext({
|
|
22
|
-
env: this.env,
|
|
23
|
-
event: handlerEvent,
|
|
24
|
-
exportName: this.#lunoraName,
|
|
25
|
-
nonRetryableErrorClass: NonRetryableError,
|
|
26
|
-
step: nativeStep
|
|
27
|
-
});
|
|
28
|
-
let output;
|
|
29
|
-
try {
|
|
30
|
-
output = await this.#definition.handler(context);
|
|
31
|
-
} catch (error) {
|
|
32
|
-
if (marker) {
|
|
33
|
-
await signalBranchParentSafe({ env: this.env, log: context.log, step: nativeStep }, marker, errorOutcome(error));
|
|
34
|
-
}
|
|
35
|
-
return convertNonRetryableError(error, NonRetryableError);
|
|
36
|
-
}
|
|
37
|
-
if (marker) {
|
|
38
|
-
await signalBranchParentSafe({ env: this.env, log: context.log, step: nativeStep }, marker, okOutcome(output));
|
|
39
|
-
}
|
|
40
|
-
return output;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export { LunoraWorkflow as default };
|
|
1
|
+
import{WorkflowEntrypoint as l}from"cloudflare:workers";import{NonRetryableError as i}from"cloudflare:workflows";import{convertNonRetryableError as h}from"../packem_shared/NonRetryableError-BPu01mU8.mjs";import{extractBranchMarker as m,stripBranchMarker as x,signalBranchParentSafe as p,errorOutcome as f,okOutcome as u}from"../packem_shared/MAX_BRANCHES-Bcw3dss9.mjs";import{createWorkflowRunContext as y}from"../packem_shared/createWorkflowRunContext-CRTTub21.mjs";class N extends l{#r;#t;constructor(t,e,o,r){super(t,e),this.#t=o,this.#r=r??"workflow"}async run(t,e){const o=e,r=m(t.payload),c=r?{...t,payload:x(t.payload)}:t,a=y({env:this.env,exports:this.ctx.exports,event:c,exportName:this.#r,nonRetryableErrorClass:i,step:o});let n;try{n=await this.#t.handler(a)}catch(s){return r&&await p({env:this.env,exports:this.ctx.exports,log:a.log,step:o},r,f(s)),h(s,i)}return r&&await p({env:this.env,exports:this.ctx.exports,log:a.log,step:o},r,u(n)),n}}export{N as default};
|