@lovable.dev/workflows-sdk 0.0.0-dev.5 → 0.0.0-dev.52
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 +184 -317
- package/dist/branch-metadata-DIu0sMPS.js +113 -0
- package/dist/context-escapes-CMZlpkct.js +80 -0
- package/dist/duplicate-ids-DiBCVnwz.js +105 -0
- package/dist/durable-ids-Cx341nJQ.js +17 -0
- package/dist/generate-C9MJlJBn.js +6958 -0
- package/dist/graph-RIBcr2Xd.js +791 -0
- package/dist/index.d.ts +970 -103
- package/dist/index.js +802 -16
- package/dist/llm-validation-DdDO6csU.js +61 -0
- package/dist/nested-operations-xQdnHHR7.js +60 -0
- package/dist/operation-descriptions-BGSwAA0h.js +47 -0
- package/dist/step-metadata-CzUMB8XT.js +105 -0
- package/dist/vite.d.ts +3 -7
- package/dist/vite.js +143 -29
- package/dist/wait-deadlines-Dg0U2ZK5.js +63 -0
- package/dist/wait-deadlines-DpK-Fgnd.js +4 -0
- package/package.json +6 -7
- package/dist/generate-IXn3QGVb.js +0 -333
- package/dist/graph-BgdiEUHq.js +0 -201
- package/dist/stacks/tanstack/index.d.ts +0 -31
- package/dist/stacks/tanstack/index.js +0 -99
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
//#region src/core/llm-validation.ts
|
|
2
|
+
function fail(message) {
|
|
3
|
+
throw new Error(`@lovable.dev/workflows-sdk: ctx.llm() ${message}`);
|
|
4
|
+
}
|
|
5
|
+
function object(value) {
|
|
6
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
7
|
+
}
|
|
8
|
+
function uint32(value) {
|
|
9
|
+
return typeof value === "number" && Number.isInteger(value) && value > 0 && value <= 4294967295;
|
|
10
|
+
}
|
|
11
|
+
function assertLlmModel(model) {
|
|
12
|
+
if (typeof model !== "string" || !/^[^/\s]+\/[^\s]+$/.test(model)) fail("requires a provider-prefixed model ID, such as openai/gpt-5");
|
|
13
|
+
}
|
|
14
|
+
function assertLlmRequest(request) {
|
|
15
|
+
if (!object(request)) fail("requires a request object");
|
|
16
|
+
if (Object.keys(request).some((key) => ![
|
|
17
|
+
"model",
|
|
18
|
+
"prompt",
|
|
19
|
+
"messages",
|
|
20
|
+
"system",
|
|
21
|
+
"maxTokens"
|
|
22
|
+
].includes(key))) fail("request contains unsupported fields");
|
|
23
|
+
assertLlmModel(request.model);
|
|
24
|
+
if (request.prompt !== void 0 === (request.messages !== void 0)) fail("requires exactly one of prompt or messages");
|
|
25
|
+
if (request.prompt !== void 0 && typeof request.prompt !== "string") fail("prompt must be a string");
|
|
26
|
+
if (request.messages !== void 0) {
|
|
27
|
+
if (!Array.isArray(request.messages) || request.messages.length === 0) fail("messages must be a nonempty array");
|
|
28
|
+
for (const message of request.messages) if (!object(message) || message.role !== "user" && message.role !== "assistant" || typeof message.content !== "string" || Object.keys(message).some((key) => key !== "role" && key !== "content")) fail("messages must contain only user/assistant roles and string content");
|
|
29
|
+
}
|
|
30
|
+
if (request.system !== void 0 && typeof request.system !== "string") fail("system must be a string");
|
|
31
|
+
if (request.maxTokens !== void 0 && !uint32(request.maxTokens)) fail("maxTokens must be an integer between 1 and 4294967295");
|
|
32
|
+
}
|
|
33
|
+
function assertLlmOptions(options) {
|
|
34
|
+
if (!object(options)) fail("requires an options object");
|
|
35
|
+
if (Object.keys(options).some((key) => ![
|
|
36
|
+
"id",
|
|
37
|
+
"meta",
|
|
38
|
+
"timeout",
|
|
39
|
+
"retry"
|
|
40
|
+
].includes(key))) fail("options contain unsupported fields");
|
|
41
|
+
if (typeof options.id !== "string" || options.id.trim() === "") fail("requires a nonempty id");
|
|
42
|
+
if (!object(options.meta) || typeof options.meta.description !== "string" || options.meta.description.trim() === "") fail("requires a nonempty meta.description");
|
|
43
|
+
const group = options.meta.group;
|
|
44
|
+
if (group !== void 0 && (!object(group) || typeof group.id !== "string" || group.id.trim() === "" || typeof group.label !== "string" || group.label.trim() === "")) fail("meta.group must contain a nonempty id and label");
|
|
45
|
+
if (options.timeout !== void 0 && (typeof options.timeout !== "number" || !Number.isFinite(options.timeout) || options.timeout <= 0)) fail("timeout must be finite and positive");
|
|
46
|
+
const retry = options.retry;
|
|
47
|
+
if (retry === void 0) return;
|
|
48
|
+
if (!object(retry)) fail("retry must be an object with serializable settings");
|
|
49
|
+
if (Object.keys(retry).some((key) => ![
|
|
50
|
+
"maxAttempts",
|
|
51
|
+
"backoff",
|
|
52
|
+
"baseMs",
|
|
53
|
+
"shouldRetry"
|
|
54
|
+
].includes(key))) fail("retry contains unsupported fields");
|
|
55
|
+
if (!uint32(retry.maxAttempts)) fail("retry.maxAttempts must be an integer between 1 and 4294967295");
|
|
56
|
+
if (retry.backoff !== void 0 && retry.backoff !== "fixed" && retry.backoff !== "exponential") fail("retry.backoff must be fixed or exponential; callbacks are not supported");
|
|
57
|
+
if (retry.shouldRetry !== void 0) fail("retry.shouldRetry callbacks are not supported");
|
|
58
|
+
if (retry.baseMs !== void 0 && (typeof retry.baseMs !== "number" || !Number.isFinite(retry.baseMs) || retry.baseMs < 0)) fail("retry.baseMs must be finite and nonnegative");
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
export { assertLlmOptions as n, assertLlmRequest as r, assertLlmModel as t };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { loadTypeScript, t as workflowHandlerSource } from "./graph-RIBcr2Xd.js";
|
|
2
|
+
//#region src/manifests/nested-operations.ts
|
|
3
|
+
const durableMethods = [
|
|
4
|
+
"step",
|
|
5
|
+
"llm",
|
|
6
|
+
"connector",
|
|
7
|
+
"approve",
|
|
8
|
+
"waitForEvent",
|
|
9
|
+
"sleep",
|
|
10
|
+
"sleepUntil",
|
|
11
|
+
"now",
|
|
12
|
+
"uuid"
|
|
13
|
+
];
|
|
14
|
+
async function findNestedDurableOperations(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 methodOf(node) {
|
|
27
|
+
if (!ts.isCallExpression(node)) return void 0;
|
|
28
|
+
const expression = node.expression;
|
|
29
|
+
let name;
|
|
30
|
+
if (ts.isPropertyAccessExpression(expression) && ts.isIdentifier(expression.expression) && expression.expression.text === context) name = expression.name.text;
|
|
31
|
+
else if (ts.isElementAccessExpression(expression) && ts.isIdentifier(expression.expression) && expression.expression.text === context && expression.argumentExpression && (ts.isStringLiteral(expression.argumentExpression) || ts.isNoSubstitutionTemplateLiteral(expression.argumentExpression))) name = expression.argumentExpression.text;
|
|
32
|
+
return durableMethods.find((method) => method === name);
|
|
33
|
+
}
|
|
34
|
+
function stepIdOf(node) {
|
|
35
|
+
const argument = node.arguments[0];
|
|
36
|
+
return argument && (ts.isStringLiteral(argument) || ts.isNoSubstitutionTemplateLiteral(argument)) ? argument.text : void 0;
|
|
37
|
+
}
|
|
38
|
+
const nested = [];
|
|
39
|
+
const visit = (node, parent) => {
|
|
40
|
+
const method = methodOf(node);
|
|
41
|
+
if (method && parent) nested.push({
|
|
42
|
+
method,
|
|
43
|
+
parentMethod: parent.method,
|
|
44
|
+
parentStepId: parent.stepId
|
|
45
|
+
});
|
|
46
|
+
if ((method === "step" || method === "connector") && ts.isCallExpression(node)) {
|
|
47
|
+
visit(node.expression, parent);
|
|
48
|
+
node.arguments.forEach((argument, index) => visit(argument, index === 1 ? {
|
|
49
|
+
method,
|
|
50
|
+
stepId: stepIdOf(node)
|
|
51
|
+
} : parent));
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
ts.forEachChild(node, (child) => visit(child, parent));
|
|
55
|
+
};
|
|
56
|
+
visit(fn);
|
|
57
|
+
return nested;
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
export { findNestedDurableOperations };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { loadTypeScript, t as workflowHandlerSource } from "./graph-RIBcr2Xd.js";
|
|
2
|
+
//#region src/manifests/operation-descriptions.ts
|
|
3
|
+
const optionsArgument = {
|
|
4
|
+
approve: 0,
|
|
5
|
+
sleep: 1,
|
|
6
|
+
sleepUntil: 1,
|
|
7
|
+
waitForEvent: 1,
|
|
8
|
+
now: 0,
|
|
9
|
+
uuid: 0,
|
|
10
|
+
llm: 1
|
|
11
|
+
};
|
|
12
|
+
async function findOperationDescriptionIssues(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 literalString(node) {
|
|
25
|
+
return node && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) ? node.text : void 0;
|
|
26
|
+
}
|
|
27
|
+
function property(node, name) {
|
|
28
|
+
if (!node || !ts.isObjectLiteralExpression(node)) return void 0;
|
|
29
|
+
for (const entry of node.properties) {
|
|
30
|
+
if (!ts.isPropertyAssignment(entry)) continue;
|
|
31
|
+
if ((ts.isIdentifier(entry.name) || ts.isStringLiteral(entry.name) ? entry.name.text : void 0) === name) return entry.initializer;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const issues = [];
|
|
35
|
+
const visit = (node) => {
|
|
36
|
+
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === context && node.expression.name.text in optionsArgument) {
|
|
37
|
+
const method = node.expression.name.text;
|
|
38
|
+
const options = node.arguments[optionsArgument[method]];
|
|
39
|
+
if ((method === "approve" ? literalString(property(options, "description")) : literalString(property(property(options, "meta"), "description"))) === void 0) issues.push({ method });
|
|
40
|
+
}
|
|
41
|
+
ts.forEachChild(node, visit);
|
|
42
|
+
};
|
|
43
|
+
visit(fn);
|
|
44
|
+
return issues;
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { findOperationDescriptionIssues };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { loadTypeScript, t as workflowHandlerSource } from "./graph-RIBcr2Xd.js";
|
|
2
|
+
//#region src/manifests/step-metadata.ts
|
|
3
|
+
async function findStepMetadataIssues(handler) {
|
|
4
|
+
const api = await loadTypeScript();
|
|
5
|
+
if (!api) return [];
|
|
6
|
+
const ts = api;
|
|
7
|
+
const handlerSource = workflowHandlerSource(handler);
|
|
8
|
+
if (!handlerSource) return [];
|
|
9
|
+
const declaration = ts.createSourceFile("handler.js", `const handler = ${handlerSource};`, ts.ScriptTarget.ESNext, true, ts.ScriptKind.JS).statements[0];
|
|
10
|
+
const fn = declaration && ts.isVariableStatement(declaration) ? declaration.declarationList.declarations[0]?.initializer : void 0;
|
|
11
|
+
if (!fn || !ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return [];
|
|
12
|
+
const parameter = fn.parameters[0]?.name;
|
|
13
|
+
if (!parameter || !ts.isIdentifier(parameter)) return [];
|
|
14
|
+
const context = parameter.text;
|
|
15
|
+
function literalString(node) {
|
|
16
|
+
return node && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) ? node.text : void 0;
|
|
17
|
+
}
|
|
18
|
+
function property(node, name) {
|
|
19
|
+
if (!node || !ts.isObjectLiteralExpression(node)) return void 0;
|
|
20
|
+
for (const entry of node.properties) {
|
|
21
|
+
if (!ts.isPropertyAssignment(entry)) continue;
|
|
22
|
+
if ((ts.isIdentifier(entry.name) || ts.isStringLiteral(entry.name) ? entry.name.text : void 0) === name) return entry.initializer;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function repeatedLoopRegion(parent, child) {
|
|
26
|
+
if (ts.isForOfStatement(parent) || ts.isForInStatement(parent)) return parent.statement === child;
|
|
27
|
+
if (ts.isForStatement(parent)) return parent.statement === child || parent.condition === child || parent.incrementor === child;
|
|
28
|
+
return ts.isWhileStatement(parent) || ts.isDoStatement(parent);
|
|
29
|
+
}
|
|
30
|
+
function repeatable(call) {
|
|
31
|
+
let child = call;
|
|
32
|
+
let parent = call.parent;
|
|
33
|
+
while (parent && parent !== fn) {
|
|
34
|
+
if (repeatedLoopRegion(parent, child) || ts.isFunctionLike(parent)) return true;
|
|
35
|
+
child = parent;
|
|
36
|
+
parent = parent.parent;
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
function loopAncestors(call) {
|
|
41
|
+
const loops = [];
|
|
42
|
+
let child = call;
|
|
43
|
+
let parent = call.parent;
|
|
44
|
+
while (parent && parent !== fn) {
|
|
45
|
+
if (repeatedLoopRegion(parent, child)) loops.push(parent);
|
|
46
|
+
child = parent;
|
|
47
|
+
parent = parent.parent;
|
|
48
|
+
}
|
|
49
|
+
return loops;
|
|
50
|
+
}
|
|
51
|
+
function isDirectSequentialLoopStep(loop, call) {
|
|
52
|
+
if (!ts.isForOfStatement(loop) || loop.awaitModifier || !ts.isVariableDeclarationList(loop.initializer)) return false;
|
|
53
|
+
const declarations = loop.initializer.declarations;
|
|
54
|
+
if (declarations.length !== 1 || !ts.isIdentifier(declarations[0].name) || declarations[0].initializer) return false;
|
|
55
|
+
let statement = loop.statement;
|
|
56
|
+
if (ts.isBlock(statement)) statement = statement.statements.length === 1 ? statement.statements[0] : void 0;
|
|
57
|
+
if (!statement || !ts.isExpressionStatement(statement)) return false;
|
|
58
|
+
let expression = statement.expression;
|
|
59
|
+
while (ts.isParenthesizedExpression(expression)) expression = expression.expression;
|
|
60
|
+
if (!ts.isAwaitExpression(expression)) return false;
|
|
61
|
+
let awaited = expression.expression;
|
|
62
|
+
while (ts.isParenthesizedExpression(awaited)) awaited = awaited.expression;
|
|
63
|
+
return awaited === call;
|
|
64
|
+
}
|
|
65
|
+
const issues = [];
|
|
66
|
+
const groups = /* @__PURE__ */ new Set();
|
|
67
|
+
const visit = (node) => {
|
|
68
|
+
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === context && (node.expression.name.text === "step" || node.expression.name.text === "llm" || node.expression.name.text === "connector")) {
|
|
69
|
+
const method = node.expression.name.text;
|
|
70
|
+
const stepId = literalString(method === "llm" ? property(node.arguments[1], "id") : node.arguments[0]);
|
|
71
|
+
const loops = loopAncestors(node);
|
|
72
|
+
if (loops.length > 0 && (loops.length !== 1 || !isDirectSequentialLoopStep(loops[0], node))) issues.push({
|
|
73
|
+
kind: "loop-form",
|
|
74
|
+
method,
|
|
75
|
+
...stepId === void 0 ? {} : { stepId }
|
|
76
|
+
});
|
|
77
|
+
const meta = property(node.arguments[method === "llm" ? 1 : 2], "meta");
|
|
78
|
+
if (literalString(property(meta, "description")) === void 0) issues.push({
|
|
79
|
+
kind: "description",
|
|
80
|
+
method,
|
|
81
|
+
...stepId === void 0 ? {} : { stepId }
|
|
82
|
+
});
|
|
83
|
+
if (repeatable(node)) {
|
|
84
|
+
const group = property(meta, "group");
|
|
85
|
+
const groupId = literalString(property(group, "id"));
|
|
86
|
+
const label = literalString(property(group, "label"));
|
|
87
|
+
if (groupId === void 0 || label === void 0) issues.push({
|
|
88
|
+
kind: "group",
|
|
89
|
+
method,
|
|
90
|
+
...stepId === void 0 ? {} : { stepId }
|
|
91
|
+
});
|
|
92
|
+
else if (groups.has(groupId)) issues.push({
|
|
93
|
+
kind: "duplicate-group",
|
|
94
|
+
groupId
|
|
95
|
+
});
|
|
96
|
+
else groups.add(groupId);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
ts.forEachChild(node, visit);
|
|
100
|
+
};
|
|
101
|
+
visit(fn);
|
|
102
|
+
return issues;
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
export { findStepMetadataIssues };
|
package/dist/vite.d.ts
CHANGED
|
@@ -10,15 +10,11 @@ interface WorkflowsPluginOptions {
|
|
|
10
10
|
path?: string;
|
|
11
11
|
/** Route parameter carrying the workflow ID. @default "workflowId" */
|
|
12
12
|
workflowIdParam?: string;
|
|
13
|
-
/** @default false */
|
|
14
|
-
runRoute?: boolean;
|
|
15
13
|
}
|
|
16
14
|
/**
|
|
17
|
-
* Build the collection's manifests and
|
|
18
|
-
*
|
|
15
|
+
* Build the collection's manifests and emit the route serving them; one `entry`
|
|
16
|
+
* feeds both, so the two can never describe different sets.
|
|
19
17
|
*/
|
|
20
18
|
declare function workflowsPlugin(options?: WorkflowsPluginOptions): Plugin;
|
|
21
|
-
/** @deprecated Renamed to `workflowsPlugin`, which also emits the run route. */
|
|
22
|
-
declare const workflowsManifest: typeof workflowsPlugin;
|
|
23
19
|
//#endregion
|
|
24
|
-
export { type WorkflowsPluginOptions,
|
|
20
|
+
export { type WorkflowsPluginOptions, workflowsPlugin };
|
package/dist/vite.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { t as generateManifests } from "./generate-
|
|
2
|
-
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
1
|
+
import { t as generateManifests } from "./generate-C9MJlJBn.js";
|
|
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";
|
|
5
5
|
import { lstatSync, mkdirSync, readFileSync, readdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
@@ -18,9 +18,10 @@ async function writeManifests(root, manifests) {
|
|
|
18
18
|
const directory = join(lovable, "workflows");
|
|
19
19
|
await ensureDirectory(directory);
|
|
20
20
|
const staging = await mkdtemp(join(directory, ".tmp-"));
|
|
21
|
-
const
|
|
21
|
+
const nameOf = (manifest) => `${manifest.workflowId}.${manifest.workflowVersion}.json`;
|
|
22
|
+
const names = new Set(manifests.map(nameOf));
|
|
22
23
|
try {
|
|
23
|
-
for (const manifest of manifests) await writeFile(join(staging,
|
|
24
|
+
for (const manifest of manifests) await writeFile(join(staging, nameOf(manifest)), JSON.stringify(manifest, null, 2) + "\n");
|
|
24
25
|
for (const name of names) await rename(join(staging, name), join(directory, name));
|
|
25
26
|
for (const entry of await readdir(directory, { withFileTypes: true })) if (entry.name.endsWith(".json") && !names.has(entry.name) && !entry.isDirectory()) await rm(join(directory, entry.name), { force: true });
|
|
26
27
|
} finally {
|
|
@@ -31,6 +32,101 @@ async function writeManifests(root, manifests) {
|
|
|
31
32
|
}
|
|
32
33
|
}
|
|
33
34
|
//#endregion
|
|
35
|
+
//#region src/vite/alias.ts
|
|
36
|
+
function normalize(path) {
|
|
37
|
+
return path.split(sep).join("/");
|
|
38
|
+
}
|
|
39
|
+
function parseTsconfig(text) {
|
|
40
|
+
const withoutComments = text.replace(/"(?:\\.|[^"\\])*"|(\/\/[^\n]*|\/\*[\s\S]*?\*\/)/g, (match, comment) => comment ? "" : match);
|
|
41
|
+
return JSON.parse(withoutComments.replace(/,(\s*[}\]])/g, "$1"));
|
|
42
|
+
}
|
|
43
|
+
function isRecord(value) {
|
|
44
|
+
return typeof value === "object" && value !== null;
|
|
45
|
+
}
|
|
46
|
+
/** Wildcard `paths` entries (`"@/*": ["./src/*"]`), the only shape an emitted import can use. */
|
|
47
|
+
function tsconfigAliases(root, tsconfigPath = "tsconfig.json") {
|
|
48
|
+
let parsed;
|
|
49
|
+
try {
|
|
50
|
+
parsed = parseTsconfig(readFileSync(resolve(root, tsconfigPath), "utf8"));
|
|
51
|
+
} catch {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
if (!isRecord(parsed)) return [];
|
|
55
|
+
const options = parsed.compilerOptions;
|
|
56
|
+
if (!isRecord(options)) return [];
|
|
57
|
+
const paths = options.paths;
|
|
58
|
+
if (!isRecord(paths)) return [];
|
|
59
|
+
const baseUrl = typeof options.baseUrl === "string" ? resolve(root, options.baseUrl) : root;
|
|
60
|
+
const entries = [];
|
|
61
|
+
for (const [pattern, targets] of Object.entries(paths)) {
|
|
62
|
+
if (!pattern.endsWith("/*") || !Array.isArray(targets)) continue;
|
|
63
|
+
const target = targets[0];
|
|
64
|
+
if (typeof target !== "string" || !target.endsWith("/*")) continue;
|
|
65
|
+
entries.push({
|
|
66
|
+
prefix: pattern.slice(0, -2),
|
|
67
|
+
dir: resolve(baseUrl, target.slice(0, -2)),
|
|
68
|
+
sources: {
|
|
69
|
+
vite: false,
|
|
70
|
+
tsconfig: true
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return entries;
|
|
75
|
+
}
|
|
76
|
+
/** String-`find` entries of a resolved Vite `resolve.alias`; a regex alias cannot be re-emitted. */
|
|
77
|
+
function viteAliases(alias) {
|
|
78
|
+
if (!alias) return [];
|
|
79
|
+
const entries = [];
|
|
80
|
+
for (const { find, replacement } of alias) {
|
|
81
|
+
if (typeof find !== "string" || !find || !isAbsolute(replacement)) continue;
|
|
82
|
+
entries.push({
|
|
83
|
+
prefix: find.replace(/\/$/, ""),
|
|
84
|
+
dir: replacement.replace(/\/$/, ""),
|
|
85
|
+
sources: {
|
|
86
|
+
vite: true,
|
|
87
|
+
tsconfig: false
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return entries;
|
|
92
|
+
}
|
|
93
|
+
function merge(entries) {
|
|
94
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
95
|
+
for (const entry of entries) {
|
|
96
|
+
const key = `${entry.prefix}::${normalize(entry.dir)}`;
|
|
97
|
+
const existing = byKey.get(key);
|
|
98
|
+
if (existing) {
|
|
99
|
+
existing.sources.vite ||= entry.sources.vite;
|
|
100
|
+
existing.sources.tsconfig ||= entry.sources.tsconfig;
|
|
101
|
+
} else byKey.set(key, {
|
|
102
|
+
...entry,
|
|
103
|
+
sources: { ...entry.sources }
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return [...byKey.values()];
|
|
107
|
+
}
|
|
108
|
+
function contains(dir, file) {
|
|
109
|
+
return normalize(file).startsWith(`${normalize(dir)}/`);
|
|
110
|
+
}
|
|
111
|
+
function score(entry) {
|
|
112
|
+
if (entry.sources.vite && entry.sources.tsconfig) return 2;
|
|
113
|
+
return entry.sources.vite ? 1 : 0;
|
|
114
|
+
}
|
|
115
|
+
function isResolvable(entry, tsconfigPathsPlugin) {
|
|
116
|
+
return entry.sources.vite || tsconfigPathsPlugin;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The aliased specifier for `target`, or `undefined` when no alias resolves it.
|
|
120
|
+
* Prefers an alias both sources agree on, then the deepest directory.
|
|
121
|
+
*/
|
|
122
|
+
function aliasedSpecifier(target, entries, tsconfigPathsPlugin = false) {
|
|
123
|
+
const matches = merge(entries).filter((entry) => contains(entry.dir, target) && isResolvable(entry, tsconfigPathsPlugin));
|
|
124
|
+
if (matches.length === 0) return void 0;
|
|
125
|
+
matches.sort((a, b) => score(b) - score(a) || normalize(b.dir).length - normalize(a.dir).length);
|
|
126
|
+
const best = matches[0];
|
|
127
|
+
return `${best.prefix}/${normalize(relative(best.dir, target)).replace(/\.tsx?$/, "")}`;
|
|
128
|
+
}
|
|
129
|
+
//#endregion
|
|
34
130
|
//#region src/vite/route-emitter.ts
|
|
35
131
|
const GENERATED_BANNER = "// AUTO-GENERATED by @lovable.dev/workflows-sdk — do not edit. Regenerated by the Vite plugin.\n// To take ownership, delete this banner line; the plugin then leaves the file alone.";
|
|
36
132
|
function isFileMissing(error) {
|
|
@@ -61,22 +157,28 @@ function relativeImportSpecifier(routeFile, target) {
|
|
|
61
157
|
const withoutExt = normalizePath(relative(dirname(routeFile), target)).replace(/\.tsx?$/, "");
|
|
62
158
|
return withoutExt.startsWith(".") ? withoutExt : `./${withoutExt}`;
|
|
63
159
|
}
|
|
64
|
-
function
|
|
160
|
+
function collectionSpecifier(routeFile, entry, aliases, tsconfigPathsPlugin) {
|
|
161
|
+
return aliasedSpecifier(entry, aliases, tsconfigPathsPlugin) ?? relativeImportSpecifier(routeFile, entry);
|
|
162
|
+
}
|
|
163
|
+
const SURFACES = [{
|
|
164
|
+
segment: "steps",
|
|
165
|
+
factory: "createStepsHandler"
|
|
166
|
+
}];
|
|
167
|
+
function buildRouteSource(routeFile, routeLiteral, param, specifier, root, factory) {
|
|
65
168
|
return `${GENERATED_BANNER}
|
|
66
169
|
// route: ${routeLiteral}
|
|
67
170
|
// emitted to: ${normalizePath(relative(root, routeFile))}
|
|
68
|
-
// This route is unauthenticated: any caller can run any declared workflow.
|
|
69
171
|
|
|
70
172
|
import { createFileRoute } from "@tanstack/react-router";
|
|
71
173
|
|
|
72
|
-
import {
|
|
174
|
+
import { ${factory} } from "@lovable.dev/workflows-sdk";
|
|
73
175
|
|
|
74
|
-
import workflows from "${
|
|
176
|
+
import workflows from "${specifier}";
|
|
75
177
|
|
|
76
178
|
export const Route = createFileRoute("${routeLiteral}")({
|
|
77
179
|
server: {
|
|
78
180
|
handlers: {
|
|
79
|
-
POST:
|
|
181
|
+
POST: ${factory}(workflows, { workflowIdParam: "${param}" }),
|
|
80
182
|
},
|
|
81
183
|
},
|
|
82
184
|
});
|
|
@@ -129,22 +231,30 @@ function findAdoptedFiles(routesDir) {
|
|
|
129
231
|
function createRouteEmitter(options) {
|
|
130
232
|
const param = assertParamShape(options.workflowIdParam);
|
|
131
233
|
const pathSegments = assertUrlPathShape(options.path);
|
|
132
|
-
const
|
|
133
|
-
const
|
|
234
|
+
const base = `/${pathSegments}/$${param}`;
|
|
235
|
+
const surfaces = SURFACES.map((surface) => ({
|
|
236
|
+
...surface,
|
|
237
|
+
literal: surface.segment ? `${base}/${surface.segment}` : base,
|
|
238
|
+
fileName: surface.segment ? join(...pathSegments.split("/"), `$${param}`, `${surface.segment}.ts`) : join(...pathSegments.split("/"), `$${param}.ts`)
|
|
239
|
+
}));
|
|
134
240
|
let root = process.cwd();
|
|
135
241
|
let entry = resolve(root, options.entry);
|
|
136
242
|
let routesDir = resolve(root, options.routesDir);
|
|
137
|
-
let
|
|
243
|
+
let routeFiles = surfaces.map((surface) => resolve(routesDir, surface.fileName));
|
|
244
|
+
let aliases = [];
|
|
245
|
+
let tsconfigPathsPlugin = false;
|
|
138
246
|
return {
|
|
139
247
|
entryPath: () => entry,
|
|
140
|
-
setRoot(next) {
|
|
248
|
+
setRoot(next, context) {
|
|
141
249
|
root = next;
|
|
250
|
+
aliases = [...viteAliases(context?.alias), ...tsconfigAliases(next)];
|
|
251
|
+
tsconfigPathsPlugin = context?.tsconfigPathsPlugin === true;
|
|
142
252
|
entry = resolve(root, options.entry);
|
|
143
253
|
assertContains(root, entry, `entry "${options.entry}"`);
|
|
144
254
|
routesDir = resolve(root, options.routesDir);
|
|
145
255
|
assertContains(root, routesDir, `routesDir "${options.routesDir}"`);
|
|
146
|
-
|
|
147
|
-
assertContains(routesDir,
|
|
256
|
+
routeFiles = surfaces.map((surface) => resolve(routesDir, surface.fileName));
|
|
257
|
+
routeFiles.forEach((file, index) => assertContains(routesDir, file, `path "${surfaces[index].literal}"`));
|
|
148
258
|
},
|
|
149
259
|
regenerate() {
|
|
150
260
|
let entryExists = true;
|
|
@@ -155,10 +265,12 @@ function createRouteEmitter(options) {
|
|
|
155
265
|
entryExists = false;
|
|
156
266
|
}
|
|
157
267
|
const expected = /* @__PURE__ */ new Set();
|
|
158
|
-
if (entryExists
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
268
|
+
if (entryExists) surfaces.forEach((surface, index) => {
|
|
269
|
+
const file = routeFiles[index];
|
|
270
|
+
expected.add(file);
|
|
271
|
+
const specifier = collectionSpecifier(file, entry, aliases, tsconfigPathsPlugin);
|
|
272
|
+
writeIfChanged(file, buildRouteSource(file, surface.literal, param, specifier, root, surface.factory));
|
|
273
|
+
});
|
|
162
274
|
for (const file of findAdoptedFiles(routesDir)) {
|
|
163
275
|
if (expected.has(file)) continue;
|
|
164
276
|
try {
|
|
@@ -172,25 +284,29 @@ function createRouteEmitter(options) {
|
|
|
172
284
|
}
|
|
173
285
|
//#endregion
|
|
174
286
|
//#region src/vite/plugin.ts
|
|
287
|
+
const TSCONFIG_PATHS_PLUGINS = /* @__PURE__ */ new Set(["vite-tsconfig-paths", "vite-plugin-tsconfig-paths"]);
|
|
175
288
|
/**
|
|
176
|
-
* Build the collection's manifests and
|
|
177
|
-
*
|
|
289
|
+
* Build the collection's manifests and emit the route serving them; one `entry`
|
|
290
|
+
* feeds both, so the two can never describe different sets.
|
|
178
291
|
*/
|
|
179
292
|
function workflowsPlugin(options = {}) {
|
|
180
293
|
const entry = options.entry ?? "src/lib/workflows/index.ts";
|
|
294
|
+
const path = options.path ?? "/api/workflows";
|
|
181
295
|
const emitter = createRouteEmitter({
|
|
182
296
|
entry,
|
|
183
297
|
routesDir: options.routesDir ?? "src/routes",
|
|
184
|
-
path
|
|
185
|
-
workflowIdParam: options.workflowIdParam ?? "workflowId"
|
|
186
|
-
runRoute: options.runRoute === true
|
|
298
|
+
path,
|
|
299
|
+
workflowIdParam: options.workflowIdParam ?? "workflowId"
|
|
187
300
|
});
|
|
188
301
|
let config;
|
|
189
302
|
return {
|
|
190
303
|
name: "@lovable.dev/workflows-sdk",
|
|
191
304
|
configResolved(resolved) {
|
|
192
305
|
config = resolved;
|
|
193
|
-
emitter.setRoot(config.root
|
|
306
|
+
emitter.setRoot(config.root, {
|
|
307
|
+
alias: config.resolve.alias,
|
|
308
|
+
tsconfigPathsPlugin: config.plugins.some((plugin) => TSCONFIG_PATHS_PLUGINS.has(plugin.name))
|
|
309
|
+
});
|
|
194
310
|
emitter.regenerate();
|
|
195
311
|
},
|
|
196
312
|
configureServer(server) {
|
|
@@ -232,7 +348,7 @@ function workflowsPlugin(options = {}) {
|
|
|
232
348
|
try {
|
|
233
349
|
const module = await server.ssrLoadModule(entryPath);
|
|
234
350
|
if (!module.default || !Array.isArray(module.default.workflows) || !Array.isArray(module.default.triggers)) throw new Error(`@lovable.dev/workflows-sdk: ${entry} must default-export a registerWorkflows() collection`);
|
|
235
|
-
const manifests = await generateManifests(module.default);
|
|
351
|
+
const manifests = await generateManifests(module.default, { routePath: path });
|
|
236
352
|
await writeManifests(config.root, manifests);
|
|
237
353
|
} finally {
|
|
238
354
|
for (const file of server.moduleGraph.fileToModulesMap.keys()) this.addWatchFile(file);
|
|
@@ -241,7 +357,5 @@ function workflowsPlugin(options = {}) {
|
|
|
241
357
|
}
|
|
242
358
|
};
|
|
243
359
|
}
|
|
244
|
-
/** @deprecated Renamed to `workflowsPlugin`, which also emits the run route. */
|
|
245
|
-
const workflowsManifest = workflowsPlugin;
|
|
246
360
|
//#endregion
|
|
247
|
-
export {
|
|
361
|
+
export { workflowsPlugin };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { loadTypeScript, t as workflowHandlerSource } from "./graph-RIBcr2Xd.js";
|
|
2
|
+
//#region src/manifests/wait-deadlines.ts
|
|
3
|
+
const optionsArgument = {
|
|
4
|
+
approve: 0,
|
|
5
|
+
waitForEvent: 1
|
|
6
|
+
};
|
|
7
|
+
async function findWaitDeadlineIssues(handler) {
|
|
8
|
+
const api = await loadTypeScript();
|
|
9
|
+
if (!api) return [];
|
|
10
|
+
const ts = api;
|
|
11
|
+
const handlerSource = workflowHandlerSource(handler);
|
|
12
|
+
if (!handlerSource) return [];
|
|
13
|
+
const declaration = ts.createSourceFile("handler.js", `const handler = ${handlerSource};`, ts.ScriptTarget.ESNext, true, ts.ScriptKind.JS).statements[0];
|
|
14
|
+
const fn = declaration && ts.isVariableStatement(declaration) ? declaration.declarationList.declarations[0]?.initializer : void 0;
|
|
15
|
+
if (!fn || !ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return [];
|
|
16
|
+
const parameter = fn.parameters[0]?.name;
|
|
17
|
+
if (!parameter || !ts.isIdentifier(parameter)) return [];
|
|
18
|
+
const context = parameter.text;
|
|
19
|
+
function property(node, name) {
|
|
20
|
+
if (!node || !ts.isObjectLiteralExpression(node)) return void 0;
|
|
21
|
+
let result;
|
|
22
|
+
for (const entry of node.properties) {
|
|
23
|
+
if (ts.isSpreadAssignment(entry) || entry.name && ts.isComputedPropertyName(entry.name)) {
|
|
24
|
+
if (name === "timeoutMs") return node;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if ((entry.name && (ts.isIdentifier(entry.name) || ts.isStringLiteral(entry.name)) ? entry.name.text : void 0) === name) {
|
|
28
|
+
if (result !== void 0) return node;
|
|
29
|
+
result = ts.isPropertyAssignment(entry) ? entry.initializer : node;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
const issues = [];
|
|
35
|
+
const visit = (node) => {
|
|
36
|
+
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === context && node.expression.name.text in optionsArgument) {
|
|
37
|
+
const method = node.expression.name.text;
|
|
38
|
+
const options = node.arguments[optionsArgument[method]];
|
|
39
|
+
const given = property(options, "timeoutMs");
|
|
40
|
+
if (property(options, "deadline") !== void 0) issues.push({
|
|
41
|
+
method,
|
|
42
|
+
reason: "absolute"
|
|
43
|
+
});
|
|
44
|
+
if (given === void 0) issues.push({
|
|
45
|
+
method,
|
|
46
|
+
reason: "missing"
|
|
47
|
+
});
|
|
48
|
+
else if (!ts.isNumericLiteral(given)) issues.push({
|
|
49
|
+
method,
|
|
50
|
+
reason: "dynamic"
|
|
51
|
+
});
|
|
52
|
+
else if (!(Number(given.text) >= 6e4 && Number.isFinite(Number(given.text)))) issues.push({
|
|
53
|
+
method,
|
|
54
|
+
reason: "invalid"
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
ts.forEachChild(node, visit);
|
|
58
|
+
};
|
|
59
|
+
visit(fn);
|
|
60
|
+
return issues;
|
|
61
|
+
}
|
|
62
|
+
//#endregion
|
|
63
|
+
export { findWaitDeadlineIssues };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
//#region src/core/wait-deadlines.ts
|
|
2
|
+
const WAIT_DEADLINE_REQUIRED = "every waiting operation needs a static timeoutMs duration in milliseconds (a finite numeric literal of at least 60000 (one minute)), measured from when the wait first parks: ctx.approve({ id, title, timeoutMs: 3600000 }), ctx.waitForEvent(name, { id, timeoutMs: 3600000 }); absolute deadlines are not supported";
|
|
3
|
+
//#endregion
|
|
4
|
+
export { WAIT_DEADLINE_REQUIRED };
|