@lovable.dev/workflows-sdk 0.0.0-dev.2 → 0.0.0-dev.20
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 +578 -40
- package/dist/branch-metadata-CZOTNcFb.js +111 -0
- package/dist/context-escapes-B25pWh07.js +78 -0
- package/dist/duplicate-ids-DElsXi_P.js +166 -0
- package/dist/durable-ids-LsygHSZC.js +17 -0
- package/dist/generate-BXCYaFf_.js +279 -0
- package/dist/graph-CHhlUDD3.js +470 -0
- package/dist/index.d.ts +280 -26
- package/dist/index.js +555 -3
- package/dist/nested-operations-A16hbdxU.js +54 -0
- package/dist/operation-descriptions-BgyQzZ2-.js +46 -0
- package/dist/step-metadata-DJ1CNwqI.js +64 -0
- package/dist/vite.d.ts +18 -5
- package/dist/vite.js +293 -9
- package/package.json +3 -2
- package/dist/graph-extract-CGLWzaln.js +0 -119
- package/dist/manifest-DIeHMgS8.js +0 -114
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { loadTypeScript, t as workflowHandlerSource } from "./graph-CHhlUDD3.js";
|
|
2
|
+
//#region src/manifests/branch-metadata.ts
|
|
3
|
+
const optionsArgument = {
|
|
4
|
+
step: 2,
|
|
5
|
+
approve: 0,
|
|
6
|
+
sleep: 1,
|
|
7
|
+
sleepUntil: 1,
|
|
8
|
+
waitForEvent: 1,
|
|
9
|
+
now: 0,
|
|
10
|
+
uuid: 0
|
|
11
|
+
};
|
|
12
|
+
async function findBranchMetadataIssues(handler) {
|
|
13
|
+
const api = await loadTypeScript();
|
|
14
|
+
if (!api) return [];
|
|
15
|
+
const ts = api;
|
|
16
|
+
const handlerSource = workflowHandlerSource(handler);
|
|
17
|
+
if (!handlerSource) return [];
|
|
18
|
+
const declaration = ts.createSourceFile("handler.js", `const handler = ${handlerSource};`, ts.ScriptTarget.ESNext, true, ts.ScriptKind.JS).statements[0];
|
|
19
|
+
const fn = declaration && ts.isVariableStatement(declaration) ? declaration.declarationList.declarations[0]?.initializer : void 0;
|
|
20
|
+
if (!fn || !ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return [];
|
|
21
|
+
const parameter = fn.parameters[0]?.name;
|
|
22
|
+
if (!parameter || !ts.isIdentifier(parameter)) return [];
|
|
23
|
+
const context = parameter.text;
|
|
24
|
+
function property(node, name) {
|
|
25
|
+
if (!node || !ts.isObjectLiteralExpression(node)) return void 0;
|
|
26
|
+
for (const entry of node.properties) {
|
|
27
|
+
if (!ts.isPropertyAssignment(entry)) continue;
|
|
28
|
+
if ((ts.isIdentifier(entry.name) || ts.isStringLiteral(entry.name) ? entry.name.text : void 0) === name) return entry.initializer;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function literalString(node) {
|
|
32
|
+
return node && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) ? node.text : void 0;
|
|
33
|
+
}
|
|
34
|
+
function durableCall(node) {
|
|
35
|
+
if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression) || !ts.isIdentifier(node.expression.expression) || node.expression.expression.text !== context || !(node.expression.name.text in optionsArgument)) return;
|
|
36
|
+
return {
|
|
37
|
+
call: node,
|
|
38
|
+
method: node.expression.name.text
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function branchPath(call, method) {
|
|
42
|
+
const options = call.arguments[optionsArgument[method]];
|
|
43
|
+
const branches = property(property(options, "meta"), "branches");
|
|
44
|
+
if (!branches || !ts.isArrayLiteralExpression(branches)) return void 0;
|
|
45
|
+
const path = [];
|
|
46
|
+
for (const element of branches.elements) {
|
|
47
|
+
if (!ts.isObjectLiteralExpression(element)) return void 0;
|
|
48
|
+
const id = literalString(property(element, "id"));
|
|
49
|
+
const description = literalString(property(element, "description"));
|
|
50
|
+
if (id === void 0 || description === void 0) return void 0;
|
|
51
|
+
path.push({
|
|
52
|
+
id,
|
|
53
|
+
description
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return path;
|
|
57
|
+
}
|
|
58
|
+
const issues = [];
|
|
59
|
+
const metadataByBranch = /* @__PURE__ */ new Map();
|
|
60
|
+
const branchById = /* @__PURE__ */ new Map();
|
|
61
|
+
function visit(node, branches) {
|
|
62
|
+
if (issues.length > 0) return;
|
|
63
|
+
if (ts.isIfStatement(node)) {
|
|
64
|
+
visit(node.expression, branches);
|
|
65
|
+
visit(node.thenStatement, [...branches, node]);
|
|
66
|
+
if (node.elseStatement) visit(node.elseStatement, [...branches, node]);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const durable = durableCall(node);
|
|
70
|
+
if (durable) {
|
|
71
|
+
const path = branchPath(durable.call, durable.method);
|
|
72
|
+
const stepId = durable.method === "step" ? literalString(durable.call.arguments[0]) : void 0;
|
|
73
|
+
if (branches.length === 0 && path !== void 0 || branches.length > 0 && (path === void 0 || path.length !== branches.length)) {
|
|
74
|
+
issues.push({
|
|
75
|
+
kind: "path",
|
|
76
|
+
method: durable.method,
|
|
77
|
+
...stepId === void 0 ? {} : { stepId }
|
|
78
|
+
});
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
for (let index = 0; index < branches.length; index++) {
|
|
82
|
+
const branch = branches[index];
|
|
83
|
+
const entry = path?.[index];
|
|
84
|
+
if (!branch || !entry) continue;
|
|
85
|
+
const existing = metadataByBranch.get(branch);
|
|
86
|
+
if (existing && (existing.id !== entry.id || existing.description !== entry.description)) {
|
|
87
|
+
issues.push({
|
|
88
|
+
kind: "inconsistent",
|
|
89
|
+
branchId: existing.id
|
|
90
|
+
});
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const existingNode = branchById.get(entry.id);
|
|
94
|
+
if (existingNode && existingNode !== branch) {
|
|
95
|
+
issues.push({
|
|
96
|
+
kind: "duplicate-id",
|
|
97
|
+
branchId: entry.id
|
|
98
|
+
});
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
metadataByBranch.set(branch, entry);
|
|
102
|
+
branchById.set(entry.id, branch);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
ts.forEachChild(node, (child) => visit(child, branches));
|
|
106
|
+
}
|
|
107
|
+
visit(fn, []);
|
|
108
|
+
return issues;
|
|
109
|
+
}
|
|
110
|
+
//#endregion
|
|
111
|
+
export { findBranchMetadataIssues };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { loadTypeScript, t as workflowHandlerSource } from "./graph-CHhlUDD3.js";
|
|
2
|
+
//#region src/manifests/context-escapes.ts
|
|
3
|
+
const durableMethods = /* @__PURE__ */ new Set([
|
|
4
|
+
"step",
|
|
5
|
+
"approve",
|
|
6
|
+
"waitForEvent",
|
|
7
|
+
"sleep",
|
|
8
|
+
"sleepUntil",
|
|
9
|
+
"now",
|
|
10
|
+
"uuid"
|
|
11
|
+
]);
|
|
12
|
+
async function findWorkflowContextEscapes(handler) {
|
|
13
|
+
const api = await loadTypeScript();
|
|
14
|
+
if (!api) return [];
|
|
15
|
+
const ts = api;
|
|
16
|
+
const handlerSource = workflowHandlerSource(handler);
|
|
17
|
+
if (!handlerSource) return [];
|
|
18
|
+
const declaration = ts.createSourceFile("handler.js", `const handler = ${handlerSource};`, ts.ScriptTarget.ESNext, true, ts.ScriptKind.JS).statements[0];
|
|
19
|
+
const fn = declaration && ts.isVariableStatement(declaration) ? declaration.declarationList.declarations[0]?.initializer : void 0;
|
|
20
|
+
if (!fn || !ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return [];
|
|
21
|
+
const parameter = fn.parameters[0]?.name;
|
|
22
|
+
if (!parameter) return [];
|
|
23
|
+
if (!ts.isIdentifier(parameter)) return [{ kind: "destructured" }];
|
|
24
|
+
const context = parameter.text;
|
|
25
|
+
function bindingContains(node, name) {
|
|
26
|
+
if (ts.isIdentifier(node)) return node.text === name;
|
|
27
|
+
return node.elements.some((element) => !ts.isOmittedExpression(element) && bindingContains(element.name, name));
|
|
28
|
+
}
|
|
29
|
+
function contextMethod(node) {
|
|
30
|
+
if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === context) return node.name.text;
|
|
31
|
+
if (ts.isElementAccessExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === context && node.argumentExpression && (ts.isStringLiteral(node.argumentExpression) || ts.isNoSubstitutionTemplateLiteral(node.argumentExpression))) return node.argumentExpression.text;
|
|
32
|
+
}
|
|
33
|
+
function directCallTarget(node) {
|
|
34
|
+
let target = node;
|
|
35
|
+
while (ts.isParenthesizedExpression(target.parent)) target = target.parent;
|
|
36
|
+
return ts.isCallExpression(target.parent) && target.parent.expression === target;
|
|
37
|
+
}
|
|
38
|
+
function identifierIsValue(node) {
|
|
39
|
+
const parent = node.parent;
|
|
40
|
+
if (parent.name === node && !ts.isShorthandPropertyAssignment(parent)) return false;
|
|
41
|
+
return parent.label !== node;
|
|
42
|
+
}
|
|
43
|
+
const escapes = [];
|
|
44
|
+
const visit = (node, shadowed) => {
|
|
45
|
+
if (escapes.length > 0) return;
|
|
46
|
+
if (node !== fn && ts.isFunctionLike(node)) {
|
|
47
|
+
const hidesContext = node.parameters.some((item) => bindingContains(item.name, context));
|
|
48
|
+
ts.forEachChild(node, (child) => visit(child, shadowed || hidesContext));
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (!shadowed) {
|
|
52
|
+
const method = contextMethod(node);
|
|
53
|
+
if (method && durableMethods.has(method) && !directCallTarget(node)) {
|
|
54
|
+
escapes.push({
|
|
55
|
+
kind: "method",
|
|
56
|
+
context,
|
|
57
|
+
method
|
|
58
|
+
});
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (ts.isIdentifier(node) && node.text === context && identifierIsValue(node)) {
|
|
62
|
+
const parent = node.parent;
|
|
63
|
+
if (!(ts.isPropertyAccessExpression(parent) && parent.expression === node || ts.isElementAccessExpression(parent) && parent.expression === node && parent.argumentExpression && (ts.isStringLiteral(parent.argumentExpression) || ts.isNoSubstitutionTemplateLiteral(parent.argumentExpression)))) {
|
|
64
|
+
escapes.push({
|
|
65
|
+
kind: "context",
|
|
66
|
+
context
|
|
67
|
+
});
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
ts.forEachChild(node, (child) => visit(child, shadowed));
|
|
73
|
+
};
|
|
74
|
+
visit(fn, false);
|
|
75
|
+
return escapes;
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
export { findWorkflowContextEscapes };
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { t as defaultDurableId } from "./durable-ids-LsygHSZC.js";
|
|
2
|
+
import { loadTypeScript, t as workflowHandlerSource } from "./graph-CHhlUDD3.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 placement(call) {
|
|
87
|
+
const branches = /* @__PURE__ */ new Map();
|
|
88
|
+
let repeatable = false;
|
|
89
|
+
let child = call;
|
|
90
|
+
let parent = call.parent;
|
|
91
|
+
while (parent && child !== fn) {
|
|
92
|
+
if (ts.isIfStatement(parent)) {
|
|
93
|
+
if (parent.thenStatement === child) branches.set(String(parent.pos), "then");
|
|
94
|
+
else if (parent.elseStatement === child) branches.set(String(parent.pos), "else");
|
|
95
|
+
} else if (ts.isConditionalExpression(parent)) {
|
|
96
|
+
if (parent.whenTrue === child) branches.set(String(parent.pos), "true");
|
|
97
|
+
else if (parent.whenFalse === child) branches.set(String(parent.pos), "false");
|
|
98
|
+
} else if (ts.isCaseClause(parent) || ts.isDefaultClause(parent)) branches.set(String(parent.parent.pos), String(parent.parent.clauses.indexOf(parent)));
|
|
99
|
+
else if (parent !== fn && (ts.isForStatement(parent) || ts.isForOfStatement(parent) || ts.isForInStatement(parent) || ts.isWhileStatement(parent) || ts.isDoStatement(parent) || ts.isFunctionLike(parent))) repeatable = true;
|
|
100
|
+
child = parent;
|
|
101
|
+
parent = parent.parent;
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
branches,
|
|
105
|
+
repeatable
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/** True when a shared conditional puts the two calls on different sides. */
|
|
109
|
+
function exclusive(a, b) {
|
|
110
|
+
for (const [node, side] of a.branches) {
|
|
111
|
+
const other = b.branches.get(node);
|
|
112
|
+
if (other !== void 0 && other !== side) return true;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
const calls = [];
|
|
117
|
+
const visit = (node) => {
|
|
118
|
+
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === context && node.expression.name.text in optionsArgument) {
|
|
119
|
+
const method = node.expression.name.text;
|
|
120
|
+
const index = optionsArgument[method];
|
|
121
|
+
const given = method === "step" ? literalString(node.arguments[0]) : explicitId(node.arguments[index]);
|
|
122
|
+
if (given !== void 0) {
|
|
123
|
+
const id = given === false ? void 0 : given;
|
|
124
|
+
const derived = id === void 0 ? defaultSource(node, method) : void 0;
|
|
125
|
+
const resolved = id ?? (derived && defaultDurableId(derived));
|
|
126
|
+
if (resolved) calls.push({
|
|
127
|
+
id: resolved,
|
|
128
|
+
method,
|
|
129
|
+
defaulted: id === void 0,
|
|
130
|
+
...placement(node)
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
ts.forEachChild(node, visit);
|
|
135
|
+
};
|
|
136
|
+
visit(fn);
|
|
137
|
+
const found = /* @__PURE__ */ new Map();
|
|
138
|
+
for (const call of calls) {
|
|
139
|
+
if (call.method === "step") {
|
|
140
|
+
const reason = call.repeatable ? "loop" : calls.some((other) => other !== call && other.id === call.id) ? "repeated" : void 0;
|
|
141
|
+
if (reason) found.set(`step:${call.id}`, {
|
|
142
|
+
id: call.id,
|
|
143
|
+
method: call.method,
|
|
144
|
+
reason
|
|
145
|
+
});
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (!call.defaulted) continue;
|
|
149
|
+
if (call.repeatable) {
|
|
150
|
+
found.set(`${call.method}:${call.id}`, {
|
|
151
|
+
id: call.id,
|
|
152
|
+
method: call.method,
|
|
153
|
+
reason: "loop"
|
|
154
|
+
});
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (calls.some((other) => other !== call && other.id === call.id && !exclusive(call, other))) found.set(`${call.method}:${call.id}`, {
|
|
158
|
+
id: call.id,
|
|
159
|
+
method: call.method,
|
|
160
|
+
reason: "repeated"
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return [...found.values()];
|
|
164
|
+
}
|
|
165
|
+
//#endregion
|
|
166
|
+
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 };
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { createMiddleware, createWorkflow } from "@tanstack/workflow-core";
|
|
2
|
+
//#region src/core/identifier.ts
|
|
3
|
+
function assertIdentifier(kind, id) {
|
|
4
|
+
if (typeof id !== "string" || id.trim() === "" || id !== id.trim()) throw new Error(`@lovable.dev/workflows-sdk: ${kind} IDs must be non-empty and have no surrounding whitespace`);
|
|
5
|
+
}
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/core/cadence.ts
|
|
8
|
+
const WEEKDAYS = [
|
|
9
|
+
"mon",
|
|
10
|
+
"tue",
|
|
11
|
+
"wed",
|
|
12
|
+
"thu",
|
|
13
|
+
"fri",
|
|
14
|
+
"sat",
|
|
15
|
+
"sun"
|
|
16
|
+
];
|
|
17
|
+
const WEEKDAY_NAMES = {
|
|
18
|
+
mon: "mon",
|
|
19
|
+
monday: "mon",
|
|
20
|
+
tue: "tue",
|
|
21
|
+
tuesday: "tue",
|
|
22
|
+
wed: "wed",
|
|
23
|
+
wednesday: "wed",
|
|
24
|
+
thu: "thu",
|
|
25
|
+
thursday: "thu",
|
|
26
|
+
fri: "fri",
|
|
27
|
+
friday: "fri",
|
|
28
|
+
sat: "sat",
|
|
29
|
+
saturday: "sat",
|
|
30
|
+
sun: "sun",
|
|
31
|
+
sunday: "sun"
|
|
32
|
+
};
|
|
33
|
+
function fail$1(message) {
|
|
34
|
+
throw new Error(`@lovable.dev/workflows-sdk: ${message}`);
|
|
35
|
+
}
|
|
36
|
+
function parseWeekday(value, label) {
|
|
37
|
+
const day = typeof value === "string" ? WEEKDAY_NAMES[value.toLowerCase()] : void 0;
|
|
38
|
+
if (day === void 0) fail$1(`${label} names an unknown day "${String(value)}"; use ${WEEKDAYS.join(", ")}`);
|
|
39
|
+
return day;
|
|
40
|
+
}
|
|
41
|
+
function parseDays(value, label) {
|
|
42
|
+
if (!Array.isArray(value) || value.length === 0) fail$1(`${label} must be a non-empty array of days`);
|
|
43
|
+
const days = [...new Set(value.map((entry) => parseWeekday(entry, label)))].sort((a, b) => WEEKDAYS.indexOf(a) - WEEKDAYS.indexOf(b));
|
|
44
|
+
return days.length === WEEKDAYS.length ? null : days;
|
|
45
|
+
}
|
|
46
|
+
function parseHoursOfDay(value, label) {
|
|
47
|
+
const entries = Array.isArray(value) ? value : [value];
|
|
48
|
+
if (entries.length === 0) fail$1(`${label} must name at least one hour`);
|
|
49
|
+
for (const entry of entries) if (!Number.isInteger(entry) || entry < 0 || entry > 23) fail$1(`${label}: ${JSON.stringify(entry)} must be a whole hour from 0 to 23; the platform picks the minute`);
|
|
50
|
+
return [...new Set(entries)].sort((a, b) => a - b);
|
|
51
|
+
}
|
|
52
|
+
function hourly(options) {
|
|
53
|
+
if (options !== void 0) fail$1(`hourly() takes no options; the platform decides where in the hour a schedule runs`);
|
|
54
|
+
return { kind: "hourly" };
|
|
55
|
+
}
|
|
56
|
+
function daily(options = {}) {
|
|
57
|
+
return {
|
|
58
|
+
kind: "daily",
|
|
59
|
+
at: options.at === void 0 ? null : parseHoursOfDay(options.at, "daily({ at })"),
|
|
60
|
+
days: options.days === void 0 ? null : parseDays(options.days, "daily({ days })")
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function weekly(options) {
|
|
64
|
+
if (typeof options !== "object" || options === null) fail$1(`weekly() requires { on: "monday", at: 7 }`);
|
|
65
|
+
return {
|
|
66
|
+
kind: "daily",
|
|
67
|
+
at: options.at === void 0 ? null : parseHoursOfDay(options.at, "weekly({ at })"),
|
|
68
|
+
days: [parseWeekday(options.on, "weekly({ on })")]
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function isHourSet(value) {
|
|
72
|
+
if (value === null) return true;
|
|
73
|
+
if (!Array.isArray(value) || value.length === 0) return false;
|
|
74
|
+
return value.every((hour, index) => Number.isInteger(hour) && hour >= 0 && hour <= 23 && (index === 0 || hour > value[index - 1]));
|
|
75
|
+
}
|
|
76
|
+
function isDaySet(value) {
|
|
77
|
+
if (value === null) return true;
|
|
78
|
+
if (!Array.isArray(value) || value.length === 0 || value.length >= WEEKDAYS.length) return false;
|
|
79
|
+
const indexes = value.map((day) => WEEKDAYS.indexOf(day));
|
|
80
|
+
return indexes.every((index, position) => index >= 0 && (position === 0 || index > indexes[position - 1]));
|
|
81
|
+
}
|
|
82
|
+
function isCadence(value) {
|
|
83
|
+
if (typeof value !== "object" || value === null) return false;
|
|
84
|
+
const cadence = value;
|
|
85
|
+
if (cadence.kind === "hourly") return Object.keys(cadence).length === 1;
|
|
86
|
+
if (cadence.kind === "daily") return isHourSet(cadence.at) && isDaySet(cadence.days);
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/core/schedule.ts
|
|
91
|
+
const OVERLAP_POLICIES = [
|
|
92
|
+
"skip",
|
|
93
|
+
"allow",
|
|
94
|
+
"buffer-one"
|
|
95
|
+
];
|
|
96
|
+
function fail(message) {
|
|
97
|
+
throw new Error(`@lovable.dev/workflows-sdk: ${message}`);
|
|
98
|
+
}
|
|
99
|
+
const CANONICAL_ZONES = new Set(typeof Intl.supportedValuesOf === "function" ? Intl.supportedValuesOf("timeZone") : []);
|
|
100
|
+
function resolveTimeZone(schedule) {
|
|
101
|
+
const timeZone = schedule.timeZone;
|
|
102
|
+
if (typeof timeZone !== "string" || timeZone === "") fail(`schedule "${schedule.id}" needs a timeZone, such as "Europe/Stockholm" or "UTC"`);
|
|
103
|
+
if (timeZone !== "UTC" && CANONICAL_ZONES.size > 0 && !CANONICAL_ZONES.has(timeZone)) fail(`schedule "${schedule.id}" timeZone "${timeZone}" must be "UTC" or a canonical IANA name such as "Europe/Stockholm"; fixed offsets and aliases such as "EST", "GMT", "Etc/GMT+5", and "US/Pacific" are not accepted`);
|
|
104
|
+
new Intl.DateTimeFormat("en", { timeZone });
|
|
105
|
+
return timeZone;
|
|
106
|
+
}
|
|
107
|
+
function defineSchedule(schedule) {
|
|
108
|
+
assertIdentifier("schedule", schedule.id);
|
|
109
|
+
if (!isCadence(schedule.cadence)) fail(`schedule "${schedule.id}" cadence must come from hourly(), daily(), or weekly()`);
|
|
110
|
+
const cadence = schedule.cadence;
|
|
111
|
+
const timeZone = resolveTimeZone(schedule);
|
|
112
|
+
const overlapPolicy = schedule.overlapPolicy ?? "skip";
|
|
113
|
+
if (!OVERLAP_POLICIES.includes(overlapPolicy)) fail(`schedule "${schedule.id}" overlapPolicy must be one of ${OVERLAP_POLICIES.join(", ")}`);
|
|
114
|
+
const enabled = schedule.enabled ?? true;
|
|
115
|
+
if (typeof enabled !== "boolean") fail(`schedule "${schedule.id}" enabled must be a boolean`);
|
|
116
|
+
return {
|
|
117
|
+
...schedule,
|
|
118
|
+
type: "schedule",
|
|
119
|
+
cadence,
|
|
120
|
+
timeZone,
|
|
121
|
+
overlapPolicy,
|
|
122
|
+
enabled
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/core/workflow.ts
|
|
127
|
+
function registerWorkflows(definition) {
|
|
128
|
+
for (const key of Object.keys(definition)) {
|
|
129
|
+
if (key === "workflows" || key === "triggers") continue;
|
|
130
|
+
throw new Error(key === "schedules" ? `@lovable.dev/workflows-sdk: registerWorkflows() no longer takes "schedules"; rename the key to "triggers"` : `@lovable.dev/workflows-sdk: registerWorkflows() does not take "${key}"`);
|
|
131
|
+
}
|
|
132
|
+
const { workflows, triggers = [] } = definition;
|
|
133
|
+
const ids = /* @__PURE__ */ new Set();
|
|
134
|
+
for (const workflow of workflows) {
|
|
135
|
+
if (workflow.__kind !== "workflow" || typeof workflow.handler !== "function") throw new Error("@lovable.dev/workflows-sdk: expected a completed createWorkflow().handler() definition");
|
|
136
|
+
assertIdentifier("workflow", workflow.id);
|
|
137
|
+
if (!/^[a-z0-9][a-z0-9_-]{0,127}$/.test(workflow.id)) throw new Error("@lovable.dev/workflows-sdk: workflow names must be 1–128 lowercase letters, digits, hyphens or underscores, starting with a letter or digit");
|
|
138
|
+
if (ids.has(workflow.id)) throw new Error(`@lovable.dev/workflows-sdk: duplicate workflow ID "${workflow.id}"`);
|
|
139
|
+
ids.add(workflow.id);
|
|
140
|
+
}
|
|
141
|
+
const scheduleIds = /* @__PURE__ */ new Set();
|
|
142
|
+
const registeredTriggers = triggers.map((trigger) => {
|
|
143
|
+
const definition = defineSchedule(trigger);
|
|
144
|
+
if (scheduleIds.has(definition.id)) throw new Error(`@lovable.dev/workflows-sdk: duplicate schedule ID "${definition.id}"`);
|
|
145
|
+
if (!workflows.includes(definition.workflow)) throw new Error(`@lovable.dev/workflows-sdk: schedule "${definition.id}" references an unregistered workflow`);
|
|
146
|
+
scheduleIds.add(definition.id);
|
|
147
|
+
return definition;
|
|
148
|
+
});
|
|
149
|
+
return {
|
|
150
|
+
workflows: [...workflows],
|
|
151
|
+
triggers: registeredTriggers
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region src/manifests/json.ts
|
|
156
|
+
function toJson(value, jsonPath, ancestors = /* @__PURE__ */ new Set()) {
|
|
157
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return value;
|
|
158
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
159
|
+
if (typeof value !== "object" || ancestors.has(value)) throw new Error(`@lovable.dev/workflows-sdk: ${jsonPath} must contain finite, acyclic JSON values`);
|
|
160
|
+
ancestors.add(value);
|
|
161
|
+
try {
|
|
162
|
+
if (Object.getOwnPropertySymbols(value).length > 0) throw new Error(`@lovable.dev/workflows-sdk: ${jsonPath} must not contain symbol keys`);
|
|
163
|
+
if (Array.isArray(value)) {
|
|
164
|
+
if (Object.keys(value).length !== value.length) throw new Error(`@lovable.dev/workflows-sdk: ${jsonPath} must be a dense JSON array without extra properties`);
|
|
165
|
+
return Array.from(value, (item, index) => toJson(item, `${jsonPath}[${index}]`, ancestors));
|
|
166
|
+
}
|
|
167
|
+
if (Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) throw new Error(`@lovable.dev/workflows-sdk: ${jsonPath} must contain plain JSON objects`);
|
|
168
|
+
return Object.fromEntries(Object.keys(value).sort().map((key) => [key, toJson(value[key], `${jsonPath}.${key}`, ancestors)]));
|
|
169
|
+
} finally {
|
|
170
|
+
ancestors.delete(value);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
//#endregion
|
|
174
|
+
//#region src/manifests/generate.ts
|
|
175
|
+
function serializeSchema(schema, direction, label) {
|
|
176
|
+
if (!schema) return void 0;
|
|
177
|
+
const standard = schema["~standard"];
|
|
178
|
+
if (typeof standard.jsonSchema?.[direction] !== "function") throw new Error(`@lovable.dev/workflows-sdk: ${label} must support Standard JSON Schema conversion`);
|
|
179
|
+
try {
|
|
180
|
+
return toJson(standard.jsonSchema[direction]({ target: "draft-2020-12" }), label);
|
|
181
|
+
} catch (cause) {
|
|
182
|
+
throw new Error(`@lovable.dev/workflows-sdk: cannot serialize ${label}`, { cause });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
async function generateManifests(definition) {
|
|
186
|
+
const { extractWorkflowGraph } = await import("./graph-CHhlUDD3.js");
|
|
187
|
+
const { findBranchMetadataIssues } = await import("./branch-metadata-CZOTNcFb.js");
|
|
188
|
+
const { findWorkflowContextEscapes } = await import("./context-escapes-B25pWh07.js");
|
|
189
|
+
const { findDuplicateOperationIds } = await import("./duplicate-ids-DElsXi_P.js");
|
|
190
|
+
const { findNestedDurableOperations } = await import("./nested-operations-A16hbdxU.js");
|
|
191
|
+
const { findOperationDescriptionIssues } = await import("./operation-descriptions-BgyQzZ2-.js");
|
|
192
|
+
const { findStepMetadataIssues } = await import("./step-metadata-DJ1CNwqI.js");
|
|
193
|
+
const collection = registerWorkflows(definition);
|
|
194
|
+
const manifests = [];
|
|
195
|
+
for (const workflow of collection.workflows) {
|
|
196
|
+
if (workflow.previousVersions?.length) throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" previousVersions are not supported yet`);
|
|
197
|
+
const contextEscape = (await findWorkflowContextEscapes(workflow.handler))[0];
|
|
198
|
+
if (contextEscape?.kind === "destructured") throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" destructures its workflow context parameter; accept one context identifier and read its properties directly in the workflow handler`);
|
|
199
|
+
if (contextEscape?.kind === "method") throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" uses ${contextEscape.context}.${contextEscape.method} as a value; durable context methods must be called directly in the workflow handler`);
|
|
200
|
+
if (contextEscape?.kind === "context") throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" uses ${contextEscape.context} as a value; the workflow context cannot be passed or aliased, so pass only extracted values such as ${contextEscape.context}.input to helpers`);
|
|
201
|
+
const nested = (await findNestedDurableOperations(workflow.handler))[0];
|
|
202
|
+
if (nested) {
|
|
203
|
+
const parent = nested.parentStepId ? `ctx.step(${JSON.stringify(nested.parentStepId)})` : "ctx.step()";
|
|
204
|
+
throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" calls ctx.${nested.method}() inside ${parent}; step bodies are atomic and cannot contain durable operations`);
|
|
205
|
+
}
|
|
206
|
+
for (const duplicate of await findDuplicateOperationIds(workflow.handler)) {
|
|
207
|
+
if (duplicate.method === "step") {
|
|
208
|
+
const use = duplicate.reason === "loop" ? "inside a loop or callback" : "more than once";
|
|
209
|
+
throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" uses step id ${JSON.stringify(duplicate.id)} ${use}; every step id must be unique within a workflow`);
|
|
210
|
+
}
|
|
211
|
+
const where = duplicate.reason === "loop" ? "inside a loop" : "more than once";
|
|
212
|
+
throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" calls ctx.${duplicate.method}() ${where} with the default id "${duplicate.id}"; replay cannot tell the occurrences apart, so pass an explicit { id } per call site`);
|
|
213
|
+
}
|
|
214
|
+
const metadataIssue = (await findStepMetadataIssues(workflow.handler))[0];
|
|
215
|
+
if (metadataIssue?.kind === "description") {
|
|
216
|
+
const step = metadataIssue.stepId ? `ctx.step(${JSON.stringify(metadataIssue.stepId)})` : "ctx.step()";
|
|
217
|
+
throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" ${step} must include a static { meta: { description: "..." } }`);
|
|
218
|
+
}
|
|
219
|
+
if (metadataIssue?.kind === "group") {
|
|
220
|
+
const step = metadataIssue.stepId ? `ctx.step(${JSON.stringify(metadataIssue.stepId)})` : "ctx.step()";
|
|
221
|
+
throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" ${step} is declared in a loop or callback and must include a static { meta: { group: { id: "...", label: "..." } } }`);
|
|
222
|
+
}
|
|
223
|
+
if (metadataIssue?.kind === "duplicate-group") throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" uses group id ${JSON.stringify(metadataIssue.groupId)} at more than one repeated step callsite; every group id must identify one callsite`);
|
|
224
|
+
const operationDescriptionIssue = (await findOperationDescriptionIssues(workflow.handler))[0];
|
|
225
|
+
if (operationDescriptionIssue) {
|
|
226
|
+
const { method } = operationDescriptionIssue;
|
|
227
|
+
const description = method === "approve" ? "{ description: \"...\" }" : "{ meta: { description: \"...\" } }";
|
|
228
|
+
throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" ctx.${method}() must include a static ${description}`);
|
|
229
|
+
}
|
|
230
|
+
const branchMetadataIssue = (await findBranchMetadataIssues(workflow.handler))[0];
|
|
231
|
+
if (branchMetadataIssue?.kind === "path") {
|
|
232
|
+
const operation = branchMetadataIssue.method === "step" && branchMetadataIssue.stepId ? `ctx.step(${JSON.stringify(branchMetadataIssue.stepId)})` : `ctx.${branchMetadataIssue.method}()`;
|
|
233
|
+
throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" ${operation} must include a static meta.branches path matching its enclosing if/else branches; each entry needs { id, description }`);
|
|
234
|
+
}
|
|
235
|
+
if (branchMetadataIssue?.kind === "inconsistent") throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" uses inconsistent metadata for branch ${JSON.stringify(branchMetadataIssue.branchId)}; every operation under one if statement must use the same branch id and description`);
|
|
236
|
+
if (branchMetadataIssue?.kind === "duplicate-id") throw new Error(`@lovable.dev/workflows-sdk: workflow "${workflow.id}" uses branch id ${JSON.stringify(branchMetadataIssue.branchId)} for more than one if statement; every branch id must identify one condition`);
|
|
237
|
+
const metadata = {
|
|
238
|
+
id: workflow.id,
|
|
239
|
+
...workflow.description === void 0 ? {} : { description: workflow.description },
|
|
240
|
+
...workflow.version === void 0 ? {} : { version: workflow.version },
|
|
241
|
+
...workflow.inputSchema ? { inputSchema: serializeSchema(workflow.inputSchema, "input", `${workflow.id}.input`) } : {},
|
|
242
|
+
...workflow.outputSchema ? { outputSchema: serializeSchema(workflow.outputSchema, "output", `${workflow.id}.output`) } : {},
|
|
243
|
+
...workflow.stateSchema ? { stateSchema: serializeSchema(workflow.stateSchema, "output", `${workflow.id}.state`) } : {}
|
|
244
|
+
};
|
|
245
|
+
const triggers = [];
|
|
246
|
+
for (const schedule of collection.triggers.filter((trigger) => trigger.workflow === workflow)) {
|
|
247
|
+
const input = toJson(schedule.input, `schedule "${schedule.id}" input`);
|
|
248
|
+
const schema = schedule.workflow.inputSchema;
|
|
249
|
+
if (schema) {
|
|
250
|
+
const result = await schema["~standard"].validate(input);
|
|
251
|
+
if (result.issues) throw new Error(`@lovable.dev/workflows-sdk: schedule "${schedule.id}" input is invalid: ${result.issues.map((issue) => issue.message).join("; ")}`);
|
|
252
|
+
}
|
|
253
|
+
triggers.push({
|
|
254
|
+
type: schedule.type,
|
|
255
|
+
id: schedule.id,
|
|
256
|
+
cadence: schedule.cadence,
|
|
257
|
+
timeZone: schedule.timeZone,
|
|
258
|
+
enabled: schedule.enabled,
|
|
259
|
+
overlapPolicy: schedule.overlapPolicy,
|
|
260
|
+
input
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
triggers.sort((a, b) => {
|
|
264
|
+
const key = (entry) => `${entry.type}\u0000${entry.id}`;
|
|
265
|
+
return key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0;
|
|
266
|
+
});
|
|
267
|
+
const manifest = {
|
|
268
|
+
version: 1,
|
|
269
|
+
workflow: metadata,
|
|
270
|
+
triggers,
|
|
271
|
+
graph: await extractWorkflowGraph(workflow.handler)
|
|
272
|
+
};
|
|
273
|
+
toJson(manifest, "manifest");
|
|
274
|
+
manifests.push(manifest);
|
|
275
|
+
}
|
|
276
|
+
return manifests.sort((a, b) => a.workflow.id < b.workflow.id ? -1 : a.workflow.id > b.workflow.id ? 1 : 0);
|
|
277
|
+
}
|
|
278
|
+
//#endregion
|
|
279
|
+
export { defineSchedule as a, weekly as c, registerWorkflows as i, createMiddleware as n, daily as o, createWorkflow as r, hourly as s, generateManifests as t };
|