workflow 5.0.1 → 5.2.0

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.
Files changed (50) hide show
  1. package/README.md +6 -0
  2. package/dist/api-workflow.d.ts.map +1 -1
  3. package/dist/api-workflow.js +4 -2
  4. package/docs/ai/chat-session-modeling.mdx +8 -8
  5. package/docs/ai/human-in-the-loop.mdx +2 -2
  6. package/docs/ai/index.mdx +14 -14
  7. package/docs/ai/streaming-updates-from-tools.mdx +2 -2
  8. package/docs/api-reference/workflow-api/register-lifecycle-hooks.mdx +15 -5
  9. package/docs/api-reference/workflow-nest/index.mdx +4 -1
  10. package/docs/api-reference/workflow-nest/is-workflow-request.mdx +58 -0
  11. package/docs/api-reference/workflow-nest/meta.json +1 -0
  12. package/docs/api-reference/workflow-nest/workflow-controller.mdx +6 -2
  13. package/docs/api-reference/workflow-nest/workflow-module.mdx +9 -1
  14. package/docs/api-reference/workflow-observability/parse-step-name.mdx +1 -1
  15. package/docs/changelog/batched-event-writes.mdx +3 -3
  16. package/docs/configuration/framework-options.mdx +6 -0
  17. package/docs/configuration/runtime-tuning.mdx +7 -5
  18. package/docs/configuration/worlds.mdx +11 -8
  19. package/docs/cookbook/integrations/ai-sdk.mdx +8 -8
  20. package/docs/cookbook/integrations/chat-sdk.mdx +8 -8
  21. package/docs/cookbook/integrations/sandbox.mdx +8 -8
  22. package/docs/errors/node-js-module-in-workflow.mdx +36 -0
  23. package/docs/foundations/hooks.mdx +35 -0
  24. package/docs/foundations/serialization.mdx +16 -1
  25. package/docs/foundations/streaming.mdx +1 -1
  26. package/docs/getting-started/astro.mdx +7 -9
  27. package/docs/getting-started/express.mdx +4 -10
  28. package/docs/getting-started/fastify.mdx +4 -9
  29. package/docs/getting-started/hono.mdx +4 -10
  30. package/docs/getting-started/index.mdx +2 -2
  31. package/docs/getting-started/nestjs.mdx +157 -36
  32. package/docs/getting-started/next.mdx +15 -18
  33. package/docs/getting-started/nitro.mdx +4 -10
  34. package/docs/getting-started/nuxt.mdx +4 -10
  35. package/docs/getting-started/react-router/v7.mdx +6 -22
  36. package/docs/getting-started/react-router/v8.mdx +6 -22
  37. package/docs/getting-started/sveltekit.mdx +7 -9
  38. package/docs/getting-started/tanstack-start.mdx +7 -9
  39. package/docs/getting-started/vite.mdx +7 -9
  40. package/docs/how-it-works/code-transform.mdx +12 -8
  41. package/docs/how-it-works/encryption.mdx +3 -1
  42. package/docs/observability/lifecycle-hooks.mdx +69 -5
  43. package/docs/observability/tracing.mdx +3 -0
  44. package/docs/whats-new.mdx +7 -7
  45. package/docs/worlds/building-a-world.mdx +6 -1
  46. package/docs/worlds/local.mdx +4 -0
  47. package/docs/worlds/postgres.mdx +13 -33
  48. package/docs/worlds/upgrading-to-v5.mdx +1 -1
  49. package/docs/worlds/vercel.mdx +19 -9
  50. package/package.json +12 -12
@@ -48,42 +48,22 @@ WORKFLOW_POSTGRES_URL="postgres://user:password@host:5432/database"
48
48
 
49
49
  Run the migration script to create the necessary tables in your database. Ensure `WORKFLOW_POSTGRES_URL` or `DATABASE_URL` is set when running this command:
50
50
 
51
- <Tabs items={["npm", "pnpm", "Yarn", "Bun"]}>
52
-
53
- <Tab value="npm">
54
-
55
- ```bash
51
+ ```bash tab="npm"
56
52
  npx --package=@workflow/world-postgres bootstrap
57
53
  ```
58
54
 
59
- </Tab>
60
-
61
- <Tab value="pnpm">
62
-
63
- ```bash
55
+ ```bash tab="pnpm"
64
56
  pnpm dlx --package @workflow/world-postgres bootstrap
65
57
  ```
66
58
 
67
- </Tab>
68
-
69
- <Tab value="Yarn">
70
-
71
- ```bash
59
+ ```bash tab="Yarn"
72
60
  yarn dlx --package @workflow/world-postgres bootstrap
73
61
  ```
74
62
 
75
- </Tab>
76
-
77
- <Tab value="Bun">
78
-
79
- ```bash
63
+ ```bash tab="Bun"
80
64
  bunx --package @workflow/world-postgres bootstrap
81
65
  ```
82
66
 
83
- </Tab>
84
-
85
- </Tabs>
86
-
87
67
  <Callout type="info">
88
68
  The migration is idempotent and can safely be run as a post-deployment lifecycle script.
89
69
  </Callout>
@@ -154,9 +134,9 @@ still adds overhead. Pulling in `workflow/runtime` from a server-startup hook pu
154
134
  the whole runtime into the cold-start path before the first request is served.
155
135
  </Callout>
156
136
 
157
- <Tabs items={["Next.js", "SvelteKit", "Nitro"]}>
137
+ <TabsWithChildren tabs={["Next.js","SvelteKit","Nitro"]}>
158
138
 
159
- <Tab value="Next.js">
139
+ <TabContent order={1}>
160
140
 
161
141
  Create an `instrumentation.ts` file in your project root:
162
142
 
@@ -174,9 +154,9 @@ export async function register() {
174
154
  Learn more about [Next.js Instrumentation](https://nextjs.org/docs/app/guides/instrumentation).
175
155
  </Callout>
176
156
 
177
- </Tab>
157
+ </TabContent>
178
158
 
179
- <Tab value="SvelteKit">
159
+ <TabContent order={2}>
180
160
 
181
161
  Create a `src/hooks.server.ts` file:
182
162
 
@@ -194,9 +174,9 @@ export const init: ServerInit = async () => {
194
174
  Learn more about [SvelteKit Hooks](https://svelte.dev/docs/kit/hooks).
195
175
  </Callout>
196
176
 
197
- </Tab>
177
+ </TabContent>
198
178
 
199
- <Tab value="Nitro">
179
+ <TabContent order={3}>
200
180
 
201
181
  Create a plugin to start the world on server initialization:
202
182
 
@@ -225,9 +205,9 @@ export default defineNitroConfig({
225
205
  Learn more about [Nitro Plugins](https://v3.nitro.build/docs/plugins).
226
206
  </Callout>
227
207
 
228
- </Tab>
208
+ </TabContent>
229
209
 
230
- </Tabs>
210
+ </TabsWithChildren>
231
211
 
232
212
  <Callout type="info">
233
213
  The Postgres World requires a long-lived worker process that polls the database for jobs. This does not work on serverless environments. For Vercel deployments, use the [Vercel World](/worlds/vercel) instead.
@@ -364,7 +344,7 @@ The Postgres World uses PostgreSQL as a durable backend:
364
344
 
365
345
  - **Storage**: Workflow runs, events, steps, and hooks are stored in PostgreSQL tables.
366
346
  - **Job queue**: [graphile-worker](https://github.com/graphile/worker) handles reliable job processing with retries.
367
- - **Streaming**: PostgreSQL NOTIFY/LISTEN enables real-time event distribution.
347
+ - **Streaming**: PostgreSQL NOTIFY/LISTEN enables real-time event distribution. If the database drops the `LISTEN` connection, the World reconnects every second and re-reads persisted chunks for open stream readers, so readers don't miss chunks written during the outage.
368
348
 
369
349
  This architecture ensures workflows survive application restarts with all state reliably persisted. For implementation details, see the [source code](https://github.com/vercel/workflow/tree/main/packages/world-postgres).
370
350
 
@@ -99,7 +99,7 @@ This bites rather than merely wasting memory because the runtime caches the *Wor
99
99
 
100
100
  **One World per process.** The workflow entrypoint's queue handler is now built from the runtime World that `getWorld()` returns, rather than from `getWorldHandlers()`. A stateful World is no longer instantiated twice in one process, so it stops getting duplicate connection pools and duplicate queue workers. If you added your own de-duplication to work around that, it is now redundant, though harmless if it keys on process-wide state.
101
101
 
102
- **Your own transport is your own business, except for the tracing.** How a World ships events to its backend is unconstrained: `@workflow/world-vercel` can opt into a WebSocket and falls back to HTTP. What is constrained is what a reader of a trace sees. A non-HTTP transport still has to emit the per-event client span that an HTTP write would, or the per-event view of a run silently disappears. See [`WORKFLOW_EVENTS_TRANSPORT`](/worlds/vercel#workflow_events_transport) for the span shape and attributes the Vercel World uses, including a separate span for the handshake.
102
+ **Your own transport is your own business, except for the tracing.** How a World ships events to its backend is unconstrained: `@workflow/world-vercel` uses a WebSocket by default and falls back to HTTP. What is constrained is what a reader of a trace sees. A non-HTTP transport still has to emit the per-event client span that an HTTP write would, or the per-event view of a run silently disappears. See [`WORKFLOW_EVENTS_TRANSPORT`](/worlds/vercel#workflow_events_transport) for the span shape and attributes the Vercel World uses, including a separate span for the handshake.
103
103
 
104
104
  **Replay reads the event log with `resolveData: 'skip-step-inputs'`.** A World may leave `input` out of `step_created` and `step_started` events for this value, and must otherwise treat it as `'all'`. A World that tests `resolveData === 'all'`, or validates against `['none', 'all']`, strips step results or rejects the read, and every replay fails. Test for `'none'` instead, or map with `entityResolveData()` from `@workflow/world`. `@workflow/world-testing` covers this case.
105
105
 
@@ -242,23 +242,33 @@ Maximum stream chunks written in one Vercel World request. Larger batches are sp
242
242
 
243
243
  ### `WORKFLOW_STREAMS_TRANSPORT`
244
244
 
245
- Experimental stream-write transport capability. Default: `http`. Set `WORKFLOW_STREAMS_TRANSPORT=ws` to attempt `workflow-stream-ws/v1`. The server authoritatively accepts or declines every upgrade; a decline goes directly to HTTP for that writer lifetime. Reads remain HTTP and demand-driven.
245
+ Stream writes upgrade to a WebSocket (`workflow-stream-ws/v1`) instead of one HTTP request per write group. Default: `ws`.
246
246
 
247
- This setting does not own tenant rollout policy and does not infer server support from package versions. HTTP remains the compatibility path. The protocol route (`/websockets/v1`) is versioned independently from REST v2/v4 and persisted workflow `specVersion` values.
247
+ Set `WORKFLOW_STREAMS_TRANSPORT=http` to opt out. That value is matched case-insensitively and ignoring surrounding whitespace; any other value, including unset, empty, or `ws`, uses the WebSocket. Stream reads always use HTTP.
248
248
 
249
- Writes and close requests execute serially. The first operation waits up to 250 ms for an opted-in socket to open; this is an implementation-level measurement knob, not protocol semantics. If the budget expires, or the upgrade fails or is declined before acceptance, the writer uses HTTP without racing the same operation over both transports. After the socket accepts a write, a missing acknowledgement has an unknown outcome: the writer fails rather than replaying the write over HTTP and risking a duplicate append. A throttled (429) write or close did not apply, so the writer retries it after the server's `Retry-After`, as the HTTP writer does, on the socket if it stays open (or its replacement, after a drain) and otherwise over HTTP. Past 30 seconds of cumulative wait, the write moves to HTTP, whose own 429 retry applies. A close that fails with a 5xx is retried over HTTP, since close is idempotent.
249
+ The setting is ignored when the World is configured with `projectConfig` and routes through the `api-workflow` proxy, which does not forward a WebSocket upgrade, so stream writes stay on HTTP.
250
250
 
251
- Before routine authentication expiry or server max duration, the server sends a v1 `drain` control. The client stops sending, lets an already-admitted request receive its reply, and reconnects after the server closes with code 1001. Authentication drains re-resolve a fresh bearer. A request that was sent but receives no reply before close still has an unknown outcome and is never replayed.
251
+ The client only offers the upgrade. The server accepts or declines each one, and a writer whose upgrade is declined or fails writes over HTTP for the rest of its lifetime. This setting does not decide which tenants may upgrade and does not infer server support from package versions. The protocol route (`/websockets/v1`) is versioned independently from REST v2/v4 and persisted workflow `specVersion` values.
252
+
253
+ A writer's first write group always goes over HTTP, and so does its second. The socket starts opening in the background once the second group's request is sent, and later groups keep going over HTTP until it opens, so no write waits for the upgrade. A writer that closes after one or two groups never uses the socket. If the socket has not opened within 10 seconds, or the upgrade fails or is declined, the writer stays on HTTP.
254
+
255
+ Writes and close requests execute serially, so a write never overtakes an earlier one on the other transport. After the socket accepts a write, a missing acknowledgement has an unknown outcome: the writer fails rather than replaying the write over HTTP and risking a duplicate append. A throttled (429) write or close did not apply, so the writer retries it after the server's `Retry-After`, as the HTTP writer does, on the socket if it stays open (or its replacement, after a drain) and otherwise over HTTP. Past 30 seconds of cumulative wait, the write moves to HTTP, whose own 429 retry applies. A close that fails with a 5xx is retried over HTTP, since close is idempotent.
256
+
257
+ Once a released writer's writes drain, its socket closes without closing the shared stream, and later writes through that same handle go over HTTP. Writers acquired with `getWritable()` inside a step release their socket when the step completes. External `getRun(runId).getWritable()` handles release it on the first `releaseLock()` after pending writes drain, so hold the writer lock for the whole burst to keep writing over the socket.
258
+
259
+ Before routine authentication expiry or server max duration, the server sends a v1 `drain` control. The client stops sending, lets an already-admitted request receive its reply, and reconnects after the server closes with code 1001. If the new socket is not open within 250 ms, the writer continues over HTTP. Authentication drains re-resolve a fresh bearer. A request that was sent but receives no reply before close still has an unknown outcome and is never replayed.
260
+
261
+ The server sends WebSocket pings to keep an idle socket open, and the client must answer them. The `ws` library the Vercel World uses answers pings automatically, so there is nothing to configure.
252
262
 
253
263
  Each stream WebSocket message is at most [`WORKFLOW_WS_MAX_MESSAGE_BYTES`](#workflow_ws_max_message_bytes) bytes. A write group larger than the limit is sent as several ordered write requests. A single chunk too large for one message is written over HTTP instead, as are the rest of its writer's writes.
254
264
 
255
265
  ### `WORKFLOW_EVENTS_TRANSPORT`
256
266
 
257
- Opt-in WebSocket transport for workflow run events, which ships them to the Vercel World over one socket per run instead of one HTTP request each. Default: `http`.
267
+ Workflow run events ship to the Vercel World over a WebSocket instead of one HTTP request each. Default: `ws`.
258
268
 
259
- Set `WORKFLOW_EVENTS_TRANSPORT=ws` to opt in. Only that value (case-insensitive) enables the WebSocket — any other value, including unset, empty, or `http`, keeps HTTP — so a typo fails toward the default rather than enabling a transport nobody asked for.
269
+ Set `WORKFLOW_EVENTS_TRANSPORT=http` to opt out. Only that exact value disables the WebSocket — any other value, including unset or empty, takes it — so a typo fails toward the default rather than silently pinning a deployment to HTTP.
260
270
 
261
- The setting is ignored when the World is configured with `projectConfig` and therefore routes through the `api-workflow` proxy: that endpoint is an HTTP-only REST gateway and does not forward a WebSocket upgrade, so events stay on HTTP. The fallback is silent, because the variable is typically set deployment-wide and a `projectConfig` World (such as the CLI) cannot act on it, and is reported once per process under `DEBUG=workflow:*`. `workflow.events.transport` on the per-write span records which transport actually carried a run.
271
+ The setting is ignored when the World is configured with `projectConfig` and therefore routes through the `api-workflow` proxy: that endpoint is an HTTP-only REST gateway and does not forward a WebSocket upgrade, so events stay on HTTP. The fallback is silent — now that the WebSocket is the default, nothing asked for it — and is reported once per process under `DEBUG=workflow:*`. `workflow.events.transport` on the per-write span records which transport actually carried a run.
262
272
 
263
273
  Event writes use the WebSocket only while that run's events channel is open. The queue handler opens it for every delivery, so workflows need nothing extra. Code that writes a run's events outside a delivery, such as a custom driver or a long-lived process, opens the channel with `openEventsChannel(runId)`, or those writes go over HTTP. Call the returned release when you're done, because an open socket keeps the process alive:
264
274
 
@@ -294,7 +304,7 @@ The WebSocket handshake is itself a span, `workflow.events.ws.connect`, so the c
294
304
 
295
305
  ### `WORKFLOW_EVENTS_TRANSPORT_WS_OVERRIDE_WORKFLOWS`
296
306
 
297
- Comma-separated workflows whose runs use the WebSocket events transport even when [`WORKFLOW_EVENTS_TRANSPORT`](#workflow_events_transport) is `http` or unset. Use it to move selected workflows onto the WebSocket while the rest of the deployment stays on HTTP. Default: none.
307
+ Comma-separated workflows whose runs use the WebSocket events transport even when [`WORKFLOW_EVENTS_TRANSPORT`](#workflow_events_transport) is `http`. Use it to move selected workflows onto the WebSocket while the rest of the deployment stays on HTTP. Default: none.
298
308
 
299
309
  Each entry is a workflow's function name, such as `processOrder`, or its full workflow name, such as `workflow//./src/workflows/order//processOrder`. Matching is exact and case-sensitive. A function name matches every workflow with that name, in any file.
300
310
 
@@ -302,7 +312,7 @@ Each entry is a workflow's function name, such as `processOrder`, or its full wo
302
312
  WORKFLOW_EVENTS_TRANSPORT_WS_OVERRIDE_WORKFLOWS=processOrder,syncInventory
303
313
  ```
304
314
 
305
- The override applies to the events channel the queue handler opens for each delivery of a listed workflow's run. The workflow name comes from the delivery's queue, so it covers the run's workflow and step invocations alike. `openEventsChannel(runId, config, { workflowName })` applies it to channels you open yourself; without `workflowName`, only `WORKFLOW_EVENTS_TRANSPORT=ws` opens one. When `WORKFLOW_EVENTS_TRANSPORT=ws` is set, every workflow already uses the WebSocket and the override has no effect.
315
+ The override applies to the events channel the queue handler opens for each delivery of a listed workflow's run. The workflow name comes from the delivery's queue, so it covers the run's workflow and step invocations alike. `openEventsChannel(runId, config, { workflowName })` applies it to channels you open yourself; without `workflowName`, a deployment with `WORKFLOW_EVENTS_TRANSPORT=http` opens none. Unless `WORKFLOW_EVENTS_TRANSPORT=http` is set, every workflow already uses the WebSocket and the override has no effect.
306
316
 
307
317
  Like other environment variables, the setting is fixed for each deployment, and a run stays on the deployment it started on. Changing it takes effect for runs started on the next deployment; runs already in progress keep the transport of the deployment they're pinned to.
308
318
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "workflow",
3
- "version": "5.0.1",
3
+ "version": "5.2.0",
4
4
  "description": "Workflow SDK - Build durable, resilient, and observable workflows",
5
5
  "main": "dist/typescript-plugin.cjs",
6
6
  "type": "module",
@@ -58,19 +58,19 @@
58
58
  }
59
59
  },
60
60
  "dependencies": {
61
- "@workflow/astro": "5.0.1",
62
- "@workflow/cli": "5.0.1",
63
- "@workflow/core": "5.0.1",
64
- "@workflow/errors": "5.0.1",
61
+ "@workflow/astro": "5.0.3",
62
+ "@workflow/cli": "5.0.3",
63
+ "@workflow/core": "5.2.0",
64
+ "@workflow/errors": "5.0.3",
65
65
  "@workflow/typescript-plugin": "5.0.0",
66
- "@workflow/utils": "5.0.0",
66
+ "@workflow/utils": "5.0.1",
67
67
  "ms": "2.1.3",
68
- "@workflow/next": "5.0.1",
69
- "@workflow/nest": "5.0.1",
70
- "@workflow/nitro": "5.0.1",
71
- "@workflow/nuxt": "5.0.1",
72
- "@workflow/sveltekit": "5.0.1",
73
- "@workflow/rollup": "5.0.1"
68
+ "@workflow/next": "5.0.3",
69
+ "@workflow/nest": "5.1.1",
70
+ "@workflow/nitro": "5.0.3",
71
+ "@workflow/nuxt": "5.0.3",
72
+ "@workflow/sveltekit": "5.0.3",
73
+ "@workflow/rollup": "5.0.3"
74
74
  },
75
75
  "devDependencies": {
76
76
  "@types/ms": "2.1.0",