@lovable.dev/workflows-sdk 0.0.0-dev.6 → 0.0.0-dev.60

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,113 @@
1
+ import { n as loadTypeScript, r as workflowHandlerSource } from "./graph-DSYE4aER.js";
2
+ //#region src/manifests/branch-metadata.ts
3
+ const optionsArgument = {
4
+ step: 2,
5
+ connector: 2,
6
+ llm: 1,
7
+ approve: 0,
8
+ sleep: 1,
9
+ sleepUntil: 1,
10
+ waitForEvent: 1,
11
+ now: 0,
12
+ uuid: 0
13
+ };
14
+ async function findBranchMetadataIssues(handler) {
15
+ const api = await loadTypeScript();
16
+ if (!api) return [];
17
+ const ts = api;
18
+ const handlerSource = workflowHandlerSource(handler);
19
+ if (!handlerSource) return [];
20
+ const declaration = ts.createSourceFile("handler.js", `const handler = ${handlerSource};`, ts.ScriptTarget.ESNext, true, ts.ScriptKind.JS).statements[0];
21
+ const fn = declaration && ts.isVariableStatement(declaration) ? declaration.declarationList.declarations[0]?.initializer : void 0;
22
+ if (!fn || !ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return [];
23
+ const parameter = fn.parameters[0]?.name;
24
+ if (!parameter || !ts.isIdentifier(parameter)) return [];
25
+ const context = parameter.text;
26
+ function property(node, name) {
27
+ if (!node || !ts.isObjectLiteralExpression(node)) return void 0;
28
+ for (const entry of node.properties) {
29
+ if (!ts.isPropertyAssignment(entry)) continue;
30
+ if ((ts.isIdentifier(entry.name) || ts.isStringLiteral(entry.name) ? entry.name.text : void 0) === name) return entry.initializer;
31
+ }
32
+ }
33
+ function literalString(node) {
34
+ return node && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) ? node.text : void 0;
35
+ }
36
+ function durableCall(node) {
37
+ 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;
38
+ return {
39
+ call: node,
40
+ method: node.expression.name.text
41
+ };
42
+ }
43
+ function branchPath(call, method) {
44
+ const options = call.arguments[optionsArgument[method]];
45
+ const branches = property(property(options, "meta"), "branches");
46
+ if (!branches || !ts.isArrayLiteralExpression(branches)) return void 0;
47
+ const path = [];
48
+ for (const element of branches.elements) {
49
+ if (!ts.isObjectLiteralExpression(element)) return void 0;
50
+ const id = literalString(property(element, "id"));
51
+ const description = literalString(property(element, "description"));
52
+ if (id === void 0 || description === void 0) return void 0;
53
+ path.push({
54
+ id,
55
+ description
56
+ });
57
+ }
58
+ return path;
59
+ }
60
+ const issues = [];
61
+ const metadataByBranch = /* @__PURE__ */ new Map();
62
+ const branchById = /* @__PURE__ */ new Map();
63
+ function visit(node, branches) {
64
+ if (issues.length > 0) return;
65
+ if (ts.isIfStatement(node)) {
66
+ visit(node.expression, branches);
67
+ visit(node.thenStatement, [...branches, node]);
68
+ if (node.elseStatement) visit(node.elseStatement, [...branches, node]);
69
+ return;
70
+ }
71
+ const durable = durableCall(node);
72
+ if (durable) {
73
+ const path = branchPath(durable.call, durable.method);
74
+ const stepId = durable.method === "step" || durable.method === "connector" ? literalString(durable.call.arguments[0]) : void 0;
75
+ if (branches.length === 0 && path !== void 0 || branches.length > 0 && (path === void 0 || path.length !== branches.length)) {
76
+ issues.push({
77
+ kind: "path",
78
+ method: durable.method,
79
+ ...stepId === void 0 ? {} : { stepId }
80
+ });
81
+ return;
82
+ }
83
+ for (let index = 0; index < branches.length; index++) {
84
+ const branch = branches[index];
85
+ const entry = path?.[index];
86
+ if (!branch || !entry) continue;
87
+ const existing = metadataByBranch.get(branch);
88
+ if (existing && (existing.id !== entry.id || existing.description !== entry.description)) {
89
+ issues.push({
90
+ kind: "inconsistent",
91
+ branchId: existing.id
92
+ });
93
+ return;
94
+ }
95
+ const existingNode = branchById.get(entry.id);
96
+ if (existingNode && existingNode !== branch) {
97
+ issues.push({
98
+ kind: "duplicate-id",
99
+ branchId: entry.id
100
+ });
101
+ return;
102
+ }
103
+ metadataByBranch.set(branch, entry);
104
+ branchById.set(entry.id, branch);
105
+ }
106
+ }
107
+ ts.forEachChild(node, (child) => visit(child, branches));
108
+ }
109
+ visit(fn, []);
110
+ return issues;
111
+ }
112
+ //#endregion
113
+ export { findBranchMetadataIssues };
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env node
2
+ import { a as loadManifests, i as writeManifests, n as PLUGIN_NAME, t as DEFAULT_ENTRY } from "../plugin-CXlzS4_y.js";
3
+ import { resolve } from "node:path";
4
+ import { existsSync } from "node:fs";
5
+ import { resolveConfig } from "vite";
6
+ //#region src/cli/generate-manifests.ts
7
+ const SDK_ERROR_PREFIX = "@lovable.dev/workflows-sdk:";
8
+ async function generate() {
9
+ const config = await resolveConfig({ logLevel: "silent" }, "build", "production", "production");
10
+ const plugin = config.plugins.find((candidate) => candidate.name === PLUGIN_NAME);
11
+ if (plugin) {
12
+ await writeManifests(config.root, await loadManifests(config, plugin.api));
13
+ return;
14
+ }
15
+ if (existsSync(resolve(config.root, "src/lib/workflows/index.ts"))) throw new Error(`${SDK_ERROR_PREFIX} ${DEFAULT_ENTRY} exists, but the Vite config has no workflowsPlugin(). Add workflowsPlugin() from "@lovable.dev/workflows-sdk/vite" to its plugins.`);
16
+ await writeManifests(config.root, []);
17
+ }
18
+ function describe(error) {
19
+ if (!(error instanceof Error)) return String(error);
20
+ const text = error.message.startsWith(SDK_ERROR_PREFIX) ? error.message : error.stack ?? error.message;
21
+ return error.cause === void 0 ? text : `${text}\nCaused by: ${describe(error.cause)}`;
22
+ }
23
+ try {
24
+ await generate();
25
+ } catch (error) {
26
+ console.error(describe(error));
27
+ process.exitCode = 1;
28
+ }
29
+ //#endregion
30
+ export {};
@@ -0,0 +1,80 @@
1
+ import { n as loadTypeScript, r as workflowHandlerSource } from "./graph-DSYE4aER.js";
2
+ //#region src/manifests/context-escapes.ts
3
+ const durableMethods = /* @__PURE__ */ new Set([
4
+ "step",
5
+ "llm",
6
+ "connector",
7
+ "approve",
8
+ "waitForEvent",
9
+ "sleep",
10
+ "sleepUntil",
11
+ "now",
12
+ "uuid"
13
+ ]);
14
+ async function findWorkflowContextEscapes(handler) {
15
+ const api = await loadTypeScript();
16
+ if (!api) return [];
17
+ const ts = api;
18
+ const handlerSource = workflowHandlerSource(handler);
19
+ if (!handlerSource) return [];
20
+ const declaration = ts.createSourceFile("handler.js", `const handler = ${handlerSource};`, ts.ScriptTarget.ESNext, true, ts.ScriptKind.JS).statements[0];
21
+ const fn = declaration && ts.isVariableStatement(declaration) ? declaration.declarationList.declarations[0]?.initializer : void 0;
22
+ if (!fn || !ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return [];
23
+ const parameter = fn.parameters[0]?.name;
24
+ if (!parameter) return [];
25
+ if (!ts.isIdentifier(parameter)) return [{ kind: "destructured" }];
26
+ const context = parameter.text;
27
+ function bindingContains(node, name) {
28
+ if (ts.isIdentifier(node)) return node.text === name;
29
+ return node.elements.some((element) => !ts.isOmittedExpression(element) && bindingContains(element.name, name));
30
+ }
31
+ function contextMethod(node) {
32
+ if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === context) return node.name.text;
33
+ 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;
34
+ }
35
+ function directCallTarget(node) {
36
+ let target = node;
37
+ while (ts.isParenthesizedExpression(target.parent)) target = target.parent;
38
+ return ts.isCallExpression(target.parent) && target.parent.expression === target;
39
+ }
40
+ function identifierIsValue(node) {
41
+ const parent = node.parent;
42
+ if (parent.name === node && !ts.isShorthandPropertyAssignment(parent)) return false;
43
+ return parent.label !== node;
44
+ }
45
+ const escapes = [];
46
+ const visit = (node, shadowed) => {
47
+ if (escapes.length > 0) return;
48
+ if (node !== fn && ts.isFunctionLike(node)) {
49
+ const hidesContext = node.parameters.some((item) => bindingContains(item.name, context));
50
+ ts.forEachChild(node, (child) => visit(child, shadowed || hidesContext));
51
+ return;
52
+ }
53
+ if (!shadowed) {
54
+ const method = contextMethod(node);
55
+ if (method && durableMethods.has(method) && !directCallTarget(node)) {
56
+ escapes.push({
57
+ kind: "method",
58
+ context,
59
+ method
60
+ });
61
+ return;
62
+ }
63
+ if (ts.isIdentifier(node) && node.text === context && identifierIsValue(node)) {
64
+ const parent = node.parent;
65
+ if (!(ts.isPropertyAccessExpression(parent) && parent.expression === node || ts.isElementAccessExpression(parent) && parent.expression === node && parent.argumentExpression && (ts.isStringLiteral(parent.argumentExpression) || ts.isNoSubstitutionTemplateLiteral(parent.argumentExpression)))) {
66
+ escapes.push({
67
+ kind: "context",
68
+ context
69
+ });
70
+ return;
71
+ }
72
+ }
73
+ }
74
+ ts.forEachChild(node, (child) => visit(child, shadowed));
75
+ };
76
+ visit(fn, false);
77
+ return escapes;
78
+ }
79
+ //#endregion
80
+ export { findWorkflowContextEscapes };
@@ -0,0 +1,105 @@
1
+ import { n as loadTypeScript, r as workflowHandlerSource } from "./graph-DSYE4aER.js";
2
+ //#region src/manifests/duplicate-ids.ts
3
+ /** Where an explicit `{ id }` would sit for each operation. */
4
+ const optionsArgument = {
5
+ now: 0,
6
+ uuid: 0,
7
+ approve: 0,
8
+ sleep: 1,
9
+ sleepUntil: 1,
10
+ waitForEvent: 1,
11
+ llm: 1,
12
+ step: -1,
13
+ connector: -1
14
+ };
15
+ /** Ids provably reached more than once, or why the handler could not be checked. */
16
+ async function findDuplicateOperationIds(handler) {
17
+ const api = await loadTypeScript();
18
+ if (!api) return { unchecked: "typescript" };
19
+ const ts = api;
20
+ const handlerSource = workflowHandlerSource(handler);
21
+ if (!handlerSource) return { unchecked: "handler" };
22
+ const declaration = ts.createSourceFile("handler.js", `const handler = ${handlerSource};`, ts.ScriptTarget.ESNext, true, ts.ScriptKind.JS).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)) return { unchecked: "handler" };
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 property(options, name) {
32
+ if (!options || !ts.isObjectLiteralExpression(options)) return void 0;
33
+ for (const entry of options.properties) {
34
+ if (!ts.isPropertyAssignment(entry)) continue;
35
+ if ((ts.isIdentifier(entry.name) || ts.isStringLiteral(entry.name) ? entry.name.text : void 0) === name) return entry.initializer;
36
+ }
37
+ }
38
+ /** `undefined` when the options bag is unreadable, so the call is skipped. */
39
+ function explicitId(options) {
40
+ if (options === void 0) return false;
41
+ if (!ts.isObjectLiteralExpression(options)) return void 0;
42
+ if (options.properties.some((entry) => ts.isSpreadAssignment(entry))) return void 0;
43
+ const given = property(options, "id");
44
+ if (given === void 0) return false;
45
+ return literalString(given);
46
+ }
47
+ function repeatedLoopRegion(parent, child) {
48
+ if (ts.isForOfStatement(parent) || ts.isForInStatement(parent)) return parent.statement === child;
49
+ if (ts.isForStatement(parent)) return parent.statement === child || parent.condition === child || parent.incrementor === child;
50
+ return ts.isWhileStatement(parent) || ts.isDoStatement(parent);
51
+ }
52
+ /** Whether anything between the call and the handler can run it more than once. */
53
+ function repeatable(call) {
54
+ let child = call;
55
+ let parent = call.parent;
56
+ while (parent && child !== fn) {
57
+ if (parent !== fn && (repeatedLoopRegion(parent, child) || ts.isFunctionLike(parent))) return true;
58
+ child = parent;
59
+ parent = parent.parent;
60
+ }
61
+ return false;
62
+ }
63
+ const calls = [];
64
+ const visit = (node) => {
65
+ if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === context && node.expression.name.text in optionsArgument) {
66
+ const method = node.expression.name.text;
67
+ const index = optionsArgument[method];
68
+ const given = index === -1 ? literalString(node.arguments[0]) : explicitId(node.arguments[index]);
69
+ if (given !== void 0) calls.push({
70
+ id: given === false ? "" : given,
71
+ method,
72
+ repeatable: repeatable(node)
73
+ });
74
+ }
75
+ ts.forEachChild(node, visit);
76
+ };
77
+ visit(fn);
78
+ const found = /* @__PURE__ */ new Map();
79
+ for (const call of calls) {
80
+ if (call.id === "") {
81
+ found.set(`${call.method}:missing:${found.size}`, {
82
+ id: "",
83
+ method: call.method,
84
+ reason: "missing"
85
+ });
86
+ continue;
87
+ }
88
+ if (call.repeatable) {
89
+ found.set(call.id, {
90
+ id: call.id,
91
+ method: call.method,
92
+ reason: "loop"
93
+ });
94
+ continue;
95
+ }
96
+ if (calls.some((other) => other !== call && other.id === call.id)) found.set(call.id, {
97
+ id: call.id,
98
+ method: call.method,
99
+ reason: "repeated"
100
+ });
101
+ }
102
+ return [...found.values()];
103
+ }
104
+ //#endregion
105
+ export { findDuplicateOperationIds };
@@ -0,0 +1,2 @@
1
+ import { t as DURABLE_ID_REQUIRED } from "./durable-ids-CdlFkNKw.js";
2
+ export { DURABLE_ID_REQUIRED };
@@ -0,0 +1,4 @@
1
+ //#region src/core/durable-ids.ts
2
+ const DURABLE_ID_REQUIRED = "every durable operation needs an explicit, workflow-unique id: ctx.approve({ id, title }), ctx.waitForEvent(name, { id }), ctx.sleep(ms, { id }), ctx.now({ id }), ctx.uuid({ id })";
3
+ //#endregion
4
+ export { DURABLE_ID_REQUIRED as t };