@lovable.dev/workflows-sdk 0.0.0-dev.10 → 0.0.0-dev.11

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 CHANGED
@@ -433,12 +433,25 @@ answering the wrong one:
433
433
  { "status": "failed", "error": "non-deterministic replay: memo has approve \"thing\" (#0), replay produced step" }
434
434
  ```
435
435
 
436
- **A repeated operation needs an explicit id.** `ctx.now()`, `ctx.uuid()`,
436
+ **A repeated operation needs an explicit id — and the build says so.** `ctx.now()`, `ctx.uuid()`,
437
437
  `ctx.sleep(ms)` and friends fall back to a default id, and two calls sharing
438
438
  one cannot be told apart on replay, so the second is refused. Pass `{ id }` per
439
439
  call site: `ctx.now({ id: "started" })`. `ctx.step("id", …)` always has an
440
440
  explicit id, so repeating one in a loop is fine.
441
441
 
442
+ Manifest generation rejects this before a run exists, when the arguments are
443
+ literal:
444
+
445
+ ```text
446
+ workflow "greet" calls ctx.now() 2 times with the default id "now";
447
+ replay cannot tell them apart, so pass an explicit { id } per call site
448
+ ```
449
+
450
+ The same applies to two `ctx.sleep(1000)` calls, two waits on one event name,
451
+ or two approvals with one title — the default id is derived from the argument,
452
+ so only a repeated argument collides. A dynamic argument cannot be read at
453
+ build time and is left to the runtime check.
454
+
442
455
  **Handler code outside a step runs on every call.** Keep side effects inside
443
456
  `ctx.step`, which is memoized; a bare `fetch` in the handler body repeats each
444
457
  pass. A pass also allows only a bounded amount of such work — 5s by default —
@@ -0,0 +1,115 @@
1
+ import { t as defaultDurableId } from "./durable-ids-LsygHSZC.js";
2
+ import { loadTypeScript } from "./graph-CPDci8jW.js";
3
+ //#region src/manifests/duplicate-ids.ts
4
+ /** Where an explicit `{ id }` would sit for each operation. */
5
+ const optionsArgument = {
6
+ now: 0,
7
+ uuid: 0,
8
+ approve: 0,
9
+ sleep: 1,
10
+ sleepUntil: 1,
11
+ waitForEvent: 1
12
+ };
13
+ /**
14
+ * Durable calls sharing one defaulted id, which replay cannot tell apart. The
15
+ * pass sees literal arguments only; anything dynamic is left to the runtime.
16
+ */
17
+ async function findDuplicateDefaultIds(handler) {
18
+ const api = await loadTypeScript();
19
+ if (!api) return [];
20
+ const ts = api;
21
+ const source = ts.createSourceFile("handler.js", `const handler = ${Function.prototype.toString.call(handler)};`, ts.ScriptTarget.ESNext, true, ts.ScriptKind.JS);
22
+ const declaration = source.statements[0];
23
+ const fn = declaration && ts.isVariableStatement(declaration) ? declaration.declarationList.declarations[0]?.initializer : void 0;
24
+ if (!fn || !ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn) || source.text.includes("[native code]")) return [];
25
+ const parameter = fn.parameters[0]?.name;
26
+ if (!parameter || !ts.isIdentifier(parameter)) return [];
27
+ const context = parameter.text;
28
+ function literalString(node) {
29
+ return node && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) ? node.text : void 0;
30
+ }
31
+ function literalNumber(node) {
32
+ if (!node || !ts.isNumericLiteral(node)) return void 0;
33
+ const value = Number(node.text);
34
+ return Number.isFinite(value) ? value : void 0;
35
+ }
36
+ function hasExplicitId(options) {
37
+ if (!options || !ts.isObjectLiteralExpression(options)) return options !== void 0;
38
+ return options.properties.some((property) => {
39
+ if (ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) {
40
+ const name = property.name;
41
+ return (ts.isIdentifier(name) || ts.isStringLiteral(name)) && name.text === "id";
42
+ }
43
+ return ts.isSpreadAssignment(property);
44
+ });
45
+ }
46
+ function sourceOf(call, method) {
47
+ switch (method) {
48
+ case "now":
49
+ case "uuid": return { kind: method };
50
+ case "sleep": {
51
+ const ms = literalNumber(call.arguments[0]);
52
+ return ms === void 0 ? void 0 : {
53
+ kind: "sleep",
54
+ ms
55
+ };
56
+ }
57
+ case "sleepUntil": {
58
+ const timestamp = literalNumber(call.arguments[0]);
59
+ return timestamp === void 0 ? void 0 : {
60
+ kind: "sleepUntil",
61
+ timestamp
62
+ };
63
+ }
64
+ case "waitForEvent": {
65
+ const name = literalString(call.arguments[0]);
66
+ return name === void 0 ? void 0 : {
67
+ kind: "waitForEvent",
68
+ name
69
+ };
70
+ }
71
+ case "approve": {
72
+ const options = call.arguments[0];
73
+ const title = options && ts.isObjectLiteralExpression(options) ? titleOf(options) : void 0;
74
+ return title === void 0 ? void 0 : {
75
+ kind: "approve",
76
+ title
77
+ };
78
+ }
79
+ }
80
+ }
81
+ function titleOf(options) {
82
+ for (const property of options.properties) {
83
+ if (!ts.isPropertyAssignment(property)) continue;
84
+ const name = property.name;
85
+ if ((ts.isIdentifier(name) || ts.isStringLiteral(name) ? name.text : void 0) === "title") return literalString(property.initializer);
86
+ }
87
+ }
88
+ const counts = /* @__PURE__ */ new Map();
89
+ const visit = (node) => {
90
+ if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === context && node.expression.name.text in optionsArgument) {
91
+ const method = node.expression.name.text;
92
+ if (!hasExplicitId(node.arguments[optionsArgument[method]])) {
93
+ const derived = sourceOf(node, method);
94
+ if (derived) {
95
+ const id = defaultDurableId(derived);
96
+ const seen = counts.get(id);
97
+ if (seen) seen.count += 1;
98
+ else counts.set(id, {
99
+ method,
100
+ count: 1
101
+ });
102
+ }
103
+ }
104
+ }
105
+ ts.forEachChild(node, visit);
106
+ };
107
+ visit(fn);
108
+ return [...counts].filter(([, seen]) => seen.count > 1).map(([id, seen]) => ({
109
+ id,
110
+ method: seen.method,
111
+ count: seen.count
112
+ }));
113
+ }
114
+ //#endregion
115
+ export { findDuplicateDefaultIds };
@@ -0,0 +1,17 @@
1
+ //#region src/core/durable-ids.ts
2
+ /**
3
+ * Shared by the runtime and the build-time check, so the ids a build rejects
4
+ * are exactly the ones replay would collide.
5
+ */
6
+ function defaultDurableId(source) {
7
+ switch (source.kind) {
8
+ case "now": return "now";
9
+ case "uuid": return "uuid";
10
+ case "sleep": return `sleep:${source.ms}`;
11
+ case "sleepUntil": return `sleepUntil:${source.timestamp}`;
12
+ case "waitForEvent": return `event:${source.name}`;
13
+ case "approve": return `approve:${source.title}`;
14
+ }
15
+ }
16
+ //#endregion
17
+ export { defaultDurableId as t };
@@ -183,11 +183,13 @@ function serializeSchema(schema, direction, label) {
183
183
  }
184
184
  }
185
185
  async function generateManifests(definition) {
186
- const { extractWorkflowGraph } = await import("./graph-BgdiEUHq.js");
186
+ const { extractWorkflowGraph } = await import("./graph-CPDci8jW.js");
187
+ const { findDuplicateDefaultIds } = await import("./duplicate-ids-CSzTGBYE.js");
187
188
  const collection = registerWorkflows(definition);
188
189
  const manifests = [];
189
190
  for (const workflow of collection.workflows) {
190
191
  if (workflow.previousVersions?.length) throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" previousVersions are not supported yet`);
192
+ for (const duplicate of await findDuplicateDefaultIds(workflow.handler)) throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" calls ctx.${duplicate.method}() ${duplicate.count} times with the default id "${duplicate.id}"; replay cannot tell them apart, so pass an explicit { id } per call site`);
191
193
  const metadata = {
192
194
  id: workflow.id,
193
195
  ...workflow.description === void 0 ? {} : { description: workflow.description },
@@ -198,4 +198,4 @@ async function extractWorkflowGraph(handler) {
198
198
  return graph;
199
199
  }
200
200
  //#endregion
201
- export { extractWorkflowGraph };
201
+ export { extractWorkflowGraph, loadTypeScript };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { AnyWorkflowDefinition, AnyWorkflowDefinition as AnyWorkflowDefinition$1, Ctx, RunStore, StepContext, StepOptions, WorkflowDefinition, WorkflowEvent, WorkflowInput, WorkflowInput as WorkflowInput$1, WorkflowOutput, WorkflowState, createMiddleware, createWorkflow } from "@tanstack/workflow-core";
2
+ import "typescript";
2
3
  //#region src/core/cadence.d.ts
3
4
  type Weekday = "mon" | "tue" | "wed" | "thu" | "fri" | "sat" | "sun";
4
5
  type Cadence = {
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
- import { a as defineSchedule, c as weekly, i as registerWorkflows, n as createMiddleware, o as daily, r as createWorkflow, s as hourly, t as generateManifests } from "./generate-DyUHbtre.js";
1
+ import { a as defineSchedule, c as weekly, i as registerWorkflows, n as createMiddleware, o as daily, r as createWorkflow, s as hourly, t as generateManifests } from "./generate-DkZJkfZ1.js";
2
+ import { t as defaultDurableId } from "./durable-ids-LsygHSZC.js";
2
3
  import { WorkflowRequestParseError, inMemoryRunStore, parseWorkflowRequest, runWorkflow } from "@tanstack/workflow-core";
3
4
  //#region src/stacks/tanstack/sse.ts
4
5
  const HEADERS = {
@@ -265,15 +266,27 @@ async function replay(options) {
265
266
  runtime,
266
267
  signal: controller.signal
267
268
  })),
268
- sleep: (ms, opts) => durable("sleep", opts?.id ?? `sleep:${ms}`, void 0, { ms }, opts?.id === void 0),
269
- sleepUntil: (timestamp, opts) => durable("sleep", opts?.id ?? `sleepUntil:${timestamp}`, void 0, { timestamp }, opts?.id === void 0),
270
- waitForEvent: (name, opts) => durable("waitForEvent", opts?.id ?? `event:${name}`, void 0, { name }, opts?.id === void 0),
271
- approve: (opts) => durable("approve", opts.id ?? `approve:${opts.title}`, void 0, {
269
+ sleep: (ms, opts) => durable("sleep", opts?.id ?? defaultDurableId({
270
+ kind: "sleep",
271
+ ms
272
+ }), void 0, { ms }, opts?.id === void 0),
273
+ sleepUntil: (timestamp, opts) => durable("sleep", opts?.id ?? defaultDurableId({
274
+ kind: "sleepUntil",
275
+ timestamp
276
+ }), void 0, { timestamp }, opts?.id === void 0),
277
+ waitForEvent: (name, opts) => durable("waitForEvent", opts?.id ?? defaultDurableId({
278
+ kind: "waitForEvent",
279
+ name
280
+ }), void 0, { name }, opts?.id === void 0),
281
+ approve: (opts) => durable("approve", opts.id ?? defaultDurableId({
282
+ kind: "approve",
283
+ title: opts.title
284
+ }), void 0, {
272
285
  title: opts.title,
273
286
  ...opts.description === void 0 ? {} : { description: opts.description }
274
287
  }, opts.id === void 0),
275
- now: (opts) => durable("now", opts?.id ?? "now", async () => Date.now(), void 0, opts?.id === void 0),
276
- uuid: (opts) => durable("uuid", opts?.id ?? "uuid", async () => crypto.randomUUID(), void 0, opts?.id === void 0),
288
+ now: (opts) => durable("now", opts?.id ?? defaultDurableId({ kind: "now" }), async () => Date.now(), void 0, opts?.id === void 0),
289
+ uuid: (opts) => durable("uuid", opts?.id ?? defaultDurableId({ kind: "uuid" }), async () => crypto.randomUUID(), void 0, opts?.id === void 0),
277
290
  emit: (name, value) => {
278
291
  emitted.push({
279
292
  name,
package/dist/vite.js CHANGED
@@ -1,4 +1,4 @@
1
- import { t as generateManifests } from "./generate-DyUHbtre.js";
1
+ import { t as generateManifests } from "./generate-DkZJkfZ1.js";
2
2
  import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
3
3
  import { createServer } from "vite";
4
4
  import { lstat, mkdir, mkdtemp, readdir, rename, rm, writeFile } from "node:fs/promises";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lovable.dev/workflows-sdk",
3
- "version": "0.0.0-dev.10",
3
+ "version": "0.0.0-dev.11",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "tag": "dev",