lightflow-engine 0.2.9 → 0.2.10

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.
@@ -1,9 +1,10 @@
1
1
  /**
2
2
  * Vercel Workflow compat: getWorkflowMetadata() returns { workflowRunId }.
3
3
  */
4
- import { current } from "../index.js";
4
+ import { getCurrent } from "../index.js";
5
5
  export function getWorkflowMetadata() {
6
- if (!current)
6
+ const ctx = getCurrent();
7
+ if (!ctx)
7
8
  throw new Error("getWorkflowMetadata() outside a workflow");
8
- return { workflowRunId: current.runId };
9
+ return { workflowRunId: ctx.runId };
9
10
  }
@@ -9,7 +9,7 @@
9
9
  */
10
10
  import { createHash } from "node:crypto";
11
11
  import { step, registerStep, } from "../index.js";
12
- import { current } from "../index.js";
12
+ import { getCurrent } from "../index.js";
13
13
  export { FatalError, CancelledError, sleep } from "../index.js";
14
14
  export { getWorkflowMetadata } from "../compat/metadata.js";
15
15
  // Vercel exports the step-aware fetch under both names; entry-agents
@@ -23,7 +23,7 @@ export { workflowFetch } from "../compat/fetch.js";
23
23
  * terminal done marker.
24
24
  */
25
25
  export function getWritable() {
26
- const ctx = current;
26
+ const ctx = getCurrent();
27
27
  if (!ctx)
28
28
  throw new Error("getWritable() outside a workflow");
29
29
  const underlying = {
package/dist/index.d.ts CHANGED
@@ -102,7 +102,7 @@ type Ctx = {
102
102
  chunks: string[];
103
103
  now: () => number;
104
104
  };
105
- export declare let current: Ctx | null;
105
+ export declare function getCurrent(): Ctx | null;
106
106
  export declare function getWorkflowMetadata(): {
107
107
  runId: string;
108
108
  };
package/dist/index.js CHANGED
@@ -39,28 +39,28 @@ export function registerWorkflow(id, fn) {
39
39
  export function registerStep(id, fn) {
40
40
  steps.set(id, fn);
41
41
  }
42
- export let current = globalThis.__lightflowCurrent ?? null;
43
- if (!globalThis.__lightflowCurrent) {
44
- globalThis.__lightflowCurrent = null;
42
+ // Cross-bundle execution context: Next.js may execute the engine and workflow
43
+ // code from different module instances of this file. `current` must be visible
44
+ // across all of them, so it lives on globalThis and is read via getCurrent().
45
+ export function getCurrent() {
46
+ return globalThis.__lightflowCurrent ?? null;
45
47
  }
46
- // Keep the live binding in sync: `current` assignments below must write both.
47
48
  function setCurrent(v) {
48
- current = v;
49
49
  globalThis.__lightflowCurrent = v;
50
50
  }
51
51
  // Rewrite plain assignments on `current` below to setCurrent(...)
52
52
  export function getWorkflowMetadata() {
53
- if (!current)
53
+ if (!getCurrent())
54
54
  throw new Error("getWorkflowMetadata() outside a workflow");
55
- return { runId: current.runId };
55
+ return { runId: getCurrent().runId };
56
56
  }
57
57
  /* ------------------------------------------------------------------ */
58
58
  /* The step id: derived from call order, so replay is deterministic */
59
59
  /* ------------------------------------------------------------------ */
60
60
  function nextStepKey() {
61
- if (!current)
61
+ if (!getCurrent())
62
62
  throw new Error("steps can only be called inside a workflow");
63
- return `step:${current.stepCalls}`;
63
+ return `step:${getCurrent().stepCalls}`;
64
64
  }
65
65
  /* ------------------------------------------------------------------ */
66
66
  /* Public API inside a workflow */
@@ -70,9 +70,9 @@ function nextStepKey() {
70
70
  * the function body is NOT re-executed.
71
71
  */
72
72
  export async function step(fn) {
73
- if (!current)
73
+ if (!getCurrent())
74
74
  return fn(); // plain call outside a workflow
75
- const ctx = current;
75
+ const ctx = getCurrent();
76
76
  const key = nextStepKey();
77
77
  ctx.stepCalls += 1;
78
78
  // Replay: memoized result for this call position? (O(1) map lookup)
@@ -163,11 +163,11 @@ async function maybeSnapshot(ctx) {
163
163
  }
164
164
  /** Durable sleep. Accepts milliseconds or an absolute Date. */
165
165
  export async function sleep(until) {
166
- if (!current) {
166
+ if (!getCurrent()) {
167
167
  const ms = until instanceof Date ? until.getTime() - Date.now() : until;
168
168
  return new Promise((r) => setTimeout(r, Math.max(0, ms)));
169
169
  }
170
- const ctx = current;
170
+ const ctx = getCurrent();
171
171
  const wakeAt = until instanceof Date ? until.getTime() : ctx.now() + until;
172
172
  const key = `sleep:${ctx.sleepCalls}`;
173
173
  ctx.sleepCalls += 1;
@@ -192,8 +192,8 @@ export class SuspendSignal {
192
192
  }
193
193
  /** Ordered output stream for a run. Chunks are persisted and replayable. */
194
194
  export function getWritable() {
195
- const ctx = current;
196
- if (!current)
195
+ const ctx = getCurrent();
196
+ if (!getCurrent())
197
197
  throw new Error("getWritable() outside a workflow");
198
198
  return {
199
199
  async write(chunk) {
@@ -377,7 +377,7 @@ export class Engine {
377
377
  return p;
378
378
  },
379
379
  };
380
- const prev = current;
380
+ const prev = getCurrent();
381
381
  setCurrent(ctx);
382
382
  try {
383
383
  const output = await fn(...args);
@@ -533,21 +533,21 @@ export class CancelledError extends Error {
533
533
  export function defineHook() {
534
534
  return {
535
535
  async create() {
536
- if (!current)
536
+ if (!getCurrent())
537
537
  throw new Error("hooks only inside a workflow");
538
538
  const token = `hook_${randomUUID().replace(/-/g, "").slice(0, 20)}`;
539
- const key = `hook:${current.hookCalls}`;
540
- current.hookCalls += 1;
541
- await current.store.createHook?.(current.runId, token, key);
539
+ const key = `hook:${getCurrent().hookCalls}`;
540
+ getCurrent().hookCalls += 1;
541
+ await getCurrent().store.createHook?.(getCurrent().runId, token, key);
542
542
  return { token };
543
543
  },
544
544
  };
545
545
  }
546
546
  /** Await a previously created hook until an external caller resolves it. */
547
547
  export async function hookResult(token) {
548
- if (!current)
548
+ if (!getCurrent())
549
549
  throw new Error("hooks only inside a workflow");
550
- const existing = await current.store.getHook?.(token);
550
+ const existing = await getCurrent().store.getHook?.(token);
551
551
  if (existing && existing.payload?.resolved) {
552
552
  return existing.payload.value;
553
553
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lightflow-engine",
3
- "version": "0.2.9",
3
+ "version": "0.2.10",
4
4
  "description": "A tiny durable workflow engine for Node.js and Postgres — with a Vercel Workflow drop-in compat layer.",
5
5
  "license": "MIT",
6
6
  "type": "module",