@lovable.dev/workflows-sdk 0.0.0-dev.3 → 0.0.0-dev.31

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.
@@ -0,0 +1,171 @@
1
+ import { t as defaultDurableId } from "./durable-ids-LsygHSZC.js";
2
+ import { loadTypeScript, t as workflowHandlerSource } from "./graph-CBdqyIz5.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
+ step: -1
13
+ };
14
+ /** Operation ids provably reached more than once; dynamic ids are left to runtime. */
15
+ async function findDuplicateOperationIds(handler) {
16
+ const api = await loadTypeScript();
17
+ if (!api) return [];
18
+ const ts = api;
19
+ const handlerSource = workflowHandlerSource(handler);
20
+ if (!handlerSource) return [];
21
+ const declaration = ts.createSourceFile("handler.js", `const handler = ${handlerSource};`, ts.ScriptTarget.ESNext, true, ts.ScriptKind.JS).statements[0];
22
+ const fn = declaration && ts.isVariableStatement(declaration) ? declaration.declarationList.declarations[0]?.initializer : void 0;
23
+ if (!fn || !ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return [];
24
+ const parameter = fn.parameters[0]?.name;
25
+ if (!parameter || !ts.isIdentifier(parameter)) return [];
26
+ const context = parameter.text;
27
+ function literalString(node) {
28
+ return node && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) ? node.text : void 0;
29
+ }
30
+ function literalNumber(node) {
31
+ if (!node || !ts.isNumericLiteral(node)) return void 0;
32
+ const value = Number(node.text);
33
+ return Number.isFinite(value) ? value : void 0;
34
+ }
35
+ function property(options, name) {
36
+ if (!options || !ts.isObjectLiteralExpression(options)) return void 0;
37
+ for (const entry of options.properties) {
38
+ if (!ts.isPropertyAssignment(entry)) continue;
39
+ if ((ts.isIdentifier(entry.name) || ts.isStringLiteral(entry.name) ? entry.name.text : void 0) === name) return entry.initializer;
40
+ }
41
+ }
42
+ /** `undefined` when the options bag is unreadable, so the call is skipped. */
43
+ function explicitId(options) {
44
+ if (options === void 0) return false;
45
+ if (!ts.isObjectLiteralExpression(options)) return void 0;
46
+ if (options.properties.some((entry) => ts.isSpreadAssignment(entry))) return void 0;
47
+ const given = property(options, "id");
48
+ if (given === void 0) return false;
49
+ return literalString(given);
50
+ }
51
+ function defaultSource(call, method) {
52
+ switch (method) {
53
+ case "now":
54
+ case "uuid": return { kind: method };
55
+ case "sleep": {
56
+ const ms = literalNumber(call.arguments[0]);
57
+ return ms === void 0 ? void 0 : {
58
+ kind: "sleep",
59
+ ms
60
+ };
61
+ }
62
+ case "sleepUntil": {
63
+ const timestamp = literalNumber(call.arguments[0]);
64
+ return timestamp === void 0 ? void 0 : {
65
+ kind: "sleepUntil",
66
+ timestamp
67
+ };
68
+ }
69
+ case "waitForEvent": {
70
+ const name = literalString(call.arguments[0]);
71
+ return name === void 0 ? void 0 : {
72
+ kind: "waitForEvent",
73
+ name
74
+ };
75
+ }
76
+ case "approve": {
77
+ const title = literalString(property(call.arguments[0], "title"));
78
+ return title === void 0 ? void 0 : {
79
+ kind: "approve",
80
+ title
81
+ };
82
+ }
83
+ case "step": return;
84
+ }
85
+ }
86
+ function repeatedLoopRegion(parent, child) {
87
+ if (ts.isForOfStatement(parent) || ts.isForInStatement(parent)) return parent.statement === child;
88
+ if (ts.isForStatement(parent)) return parent.statement === child || parent.condition === child || parent.incrementor === child;
89
+ return ts.isWhileStatement(parent) || ts.isDoStatement(parent);
90
+ }
91
+ function placement(call) {
92
+ const branches = /* @__PURE__ */ new Map();
93
+ let repeatable = false;
94
+ let child = call;
95
+ let parent = call.parent;
96
+ while (parent && child !== fn) {
97
+ if (ts.isIfStatement(parent)) {
98
+ if (parent.thenStatement === child) branches.set(String(parent.pos), "then");
99
+ else if (parent.elseStatement === child) branches.set(String(parent.pos), "else");
100
+ } else if (ts.isConditionalExpression(parent)) {
101
+ if (parent.whenTrue === child) branches.set(String(parent.pos), "true");
102
+ else if (parent.whenFalse === child) branches.set(String(parent.pos), "false");
103
+ } else if (ts.isCaseClause(parent) || ts.isDefaultClause(parent)) branches.set(String(parent.parent.pos), String(parent.parent.clauses.indexOf(parent)));
104
+ else if (parent !== fn && (repeatedLoopRegion(parent, child) || ts.isFunctionLike(parent))) repeatable = true;
105
+ child = parent;
106
+ parent = parent.parent;
107
+ }
108
+ return {
109
+ branches,
110
+ repeatable
111
+ };
112
+ }
113
+ /** True when a shared conditional puts the two calls on different sides. */
114
+ function exclusive(a, b) {
115
+ for (const [node, side] of a.branches) {
116
+ const other = b.branches.get(node);
117
+ if (other !== void 0 && other !== side) return true;
118
+ }
119
+ return false;
120
+ }
121
+ const calls = [];
122
+ const visit = (node) => {
123
+ if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === context && node.expression.name.text in optionsArgument) {
124
+ const method = node.expression.name.text;
125
+ const index = optionsArgument[method];
126
+ const given = method === "step" ? literalString(node.arguments[0]) : explicitId(node.arguments[index]);
127
+ if (given !== void 0) {
128
+ const id = given === false ? void 0 : given;
129
+ const derived = id === void 0 ? defaultSource(node, method) : void 0;
130
+ const resolved = id ?? (derived && defaultDurableId(derived));
131
+ if (resolved) calls.push({
132
+ id: resolved,
133
+ method,
134
+ defaulted: id === void 0,
135
+ ...placement(node)
136
+ });
137
+ }
138
+ }
139
+ ts.forEachChild(node, visit);
140
+ };
141
+ visit(fn);
142
+ const found = /* @__PURE__ */ new Map();
143
+ for (const call of calls) {
144
+ if (call.method === "step") {
145
+ const reason = call.repeatable ? "loop" : calls.some((other) => other !== call && other.id === call.id) ? "repeated" : void 0;
146
+ if (reason) found.set(`step:${call.id}`, {
147
+ id: call.id,
148
+ method: call.method,
149
+ reason
150
+ });
151
+ continue;
152
+ }
153
+ if (!call.defaulted) continue;
154
+ if (call.repeatable) {
155
+ found.set(`${call.method}:${call.id}`, {
156
+ id: call.id,
157
+ method: call.method,
158
+ reason: "loop"
159
+ });
160
+ continue;
161
+ }
162
+ if (calls.some((other) => other !== call && other.id === call.id && !exclusive(call, other))) found.set(`${call.method}:${call.id}`, {
163
+ id: call.id,
164
+ method: call.method,
165
+ reason: "repeated"
166
+ });
167
+ }
168
+ return [...found.values()];
169
+ }
170
+ //#endregion
171
+ export { findDuplicateOperationIds };
@@ -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 };