@lovable.dev/workflows-sdk 0.0.0-dev.4 → 0.0.0-dev.40
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 +153 -252
- package/dist/branch-metadata-7KoYCvVR.js +111 -0
- package/dist/context-escapes-5F9gQw59.js +78 -0
- package/dist/duplicate-ids-CJTxhI0b.js +103 -0
- package/dist/durable-ids-Cx341nJQ.js +17 -0
- package/dist/generate-Bv_Ep6yM.js +6943 -0
- package/dist/graph-oG4uFJ0o.js +656 -0
- package/dist/index.d.ts +934 -100
- package/dist/index.js +677 -14
- package/dist/nested-operations-CiJJpl5w.js +54 -0
- package/dist/operation-descriptions-CARt7Myx.js +46 -0
- package/dist/step-metadata-CdZSDCpP.js +101 -0
- package/dist/vite.d.ts +17 -4
- package/dist/vite.js +293 -9
- package/dist/wait-deadlines-DTUJbmVC.js +63 -0
- package/dist/wait-deadlines-DpK-Fgnd.js +4 -0
- package/package.json +3 -1
- package/dist/generate-IXn3QGVb.js +0 -333
- package/dist/graph-BgdiEUHq.js +0 -201
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
import { _ as WorkflowManifestStepNodeV1Schema, a as WorkflowManifestGraphEdgeV1Schema, b as WorkflowManifestWaitForEventNodeV1Schema, c as WorkflowManifestLlmNodeV1Schema, d as WorkflowManifestOrderEdgeV1Schema, f as WorkflowManifestParallelNodeV1Schema, g as WorkflowManifestStepExecutionPolicyV1Schema, h as WorkflowManifestSleepUntilNodeV1Schema, i as WorkflowManifestBranchNodeV1Schema, l as WorkflowManifestLoopNodeV1Schema, m as WorkflowManifestSleepNodeV1Schema, n as WorkflowManifestApproveNodeV1Schema, o as WorkflowManifestGraphNodeV1Schema, p as WorkflowManifestRepeatedStepV1Schema, r as WorkflowManifestBranchEdgeV1Schema, s as WorkflowManifestGraphV1Schema, u as WorkflowManifestNowNodeV1Schema, v as WorkflowManifestUnknownNodeV1Schema, x as create, y as WorkflowManifestUuidNodeV1Schema } from "./generate-Bv_Ep6yM.js";
|
|
2
|
+
//#region src/manifests/handler-source.ts
|
|
3
|
+
const nativeFunctionSource = /\{\s*\[native code\]\s*\}\s*$/;
|
|
4
|
+
function workflowHandlerSource(handler) {
|
|
5
|
+
const source = Function.prototype.toString.call(handler);
|
|
6
|
+
return nativeFunctionSource.test(source) ? void 0 : source;
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/manifests/graph.ts
|
|
10
|
+
const durableMethods = {
|
|
11
|
+
step: 0,
|
|
12
|
+
llm: 0,
|
|
13
|
+
approve: 0,
|
|
14
|
+
waitForEvent: 0,
|
|
15
|
+
sleep: 0,
|
|
16
|
+
sleepUntil: 0,
|
|
17
|
+
now: 0,
|
|
18
|
+
uuid: 0
|
|
19
|
+
};
|
|
20
|
+
let loaded;
|
|
21
|
+
async function loadTypeScript() {
|
|
22
|
+
if (loaded !== void 0) return loaded;
|
|
23
|
+
try {
|
|
24
|
+
const module = await import("typescript");
|
|
25
|
+
const api = module.default ?? module;
|
|
26
|
+
loaded = typeof api?.createSourceFile === "function" ? api : null;
|
|
27
|
+
} catch {
|
|
28
|
+
loaded = null;
|
|
29
|
+
}
|
|
30
|
+
return loaded;
|
|
31
|
+
}
|
|
32
|
+
async function extractWorkflowGraph(workflow) {
|
|
33
|
+
const graph = create(WorkflowManifestGraphV1Schema, {
|
|
34
|
+
source: "inferred",
|
|
35
|
+
scope: "handler",
|
|
36
|
+
complete: true,
|
|
37
|
+
nodes: [],
|
|
38
|
+
edges: [],
|
|
39
|
+
diagnostics: []
|
|
40
|
+
});
|
|
41
|
+
function addNode(label, kind, parents) {
|
|
42
|
+
const id = `node-${graph.nodes.length}`;
|
|
43
|
+
graph.nodes.push(create(WorkflowManifestGraphNodeV1Schema, {
|
|
44
|
+
id,
|
|
45
|
+
label,
|
|
46
|
+
kind
|
|
47
|
+
}));
|
|
48
|
+
const seen = /* @__PURE__ */ new Set();
|
|
49
|
+
for (const parent of parents) {
|
|
50
|
+
const key = JSON.stringify(parent);
|
|
51
|
+
if (seen.has(key)) continue;
|
|
52
|
+
seen.add(key);
|
|
53
|
+
if (parent.kind === "order") graph.edges.push(create(WorkflowManifestGraphEdgeV1Schema, {
|
|
54
|
+
source: parent.source,
|
|
55
|
+
target: id,
|
|
56
|
+
kind: {
|
|
57
|
+
case: "order",
|
|
58
|
+
value: create(WorkflowManifestOrderEdgeV1Schema)
|
|
59
|
+
}
|
|
60
|
+
}));
|
|
61
|
+
else graph.edges.push(create(WorkflowManifestGraphEdgeV1Schema, {
|
|
62
|
+
source: parent.source,
|
|
63
|
+
target: id,
|
|
64
|
+
kind: {
|
|
65
|
+
case: "branch",
|
|
66
|
+
value: create(WorkflowManifestBranchEdgeV1Schema, { outcome: parent.outcome })
|
|
67
|
+
}
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
return [{
|
|
71
|
+
source: id,
|
|
72
|
+
kind: "order"
|
|
73
|
+
}];
|
|
74
|
+
}
|
|
75
|
+
function degrade(reason) {
|
|
76
|
+
graph.complete = false;
|
|
77
|
+
if (!graph.diagnostics.includes(reason)) graph.diagnostics.push(reason);
|
|
78
|
+
}
|
|
79
|
+
function unknown(reason, parents) {
|
|
80
|
+
degrade(reason);
|
|
81
|
+
return addNode(reason, {
|
|
82
|
+
case: "unknown",
|
|
83
|
+
value: create(WorkflowManifestUnknownNodeV1Schema)
|
|
84
|
+
}, parents);
|
|
85
|
+
}
|
|
86
|
+
const api = await loadTypeScript();
|
|
87
|
+
if (!api) {
|
|
88
|
+
unknown("Install typescript to infer step graphs", []);
|
|
89
|
+
return graph;
|
|
90
|
+
}
|
|
91
|
+
const ts = api;
|
|
92
|
+
const handlerSource = workflowHandlerSource(workflow.handler);
|
|
93
|
+
if (!handlerSource) {
|
|
94
|
+
unknown("Handler source is unavailable", []);
|
|
95
|
+
return graph;
|
|
96
|
+
}
|
|
97
|
+
const source = ts.createSourceFile("handler.js", `const handler = ${handlerSource};`, ts.ScriptTarget.ESNext, true, ts.ScriptKind.JS);
|
|
98
|
+
const declaration = source.statements[0];
|
|
99
|
+
const fn = declaration && ts.isVariableStatement(declaration) ? declaration.declarationList.declarations[0]?.initializer : void 0;
|
|
100
|
+
if (!fn || !ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) {
|
|
101
|
+
unknown("Handler source is unavailable", []);
|
|
102
|
+
return graph;
|
|
103
|
+
}
|
|
104
|
+
const parameter = fn.parameters[0]?.name;
|
|
105
|
+
if (parameter && !ts.isIdentifier(parameter)) {
|
|
106
|
+
unknown("Destructured workflow context is not supported yet", []);
|
|
107
|
+
return graph;
|
|
108
|
+
}
|
|
109
|
+
const context = parameter?.text;
|
|
110
|
+
function contains(node, predicate, skipFunctions = false) {
|
|
111
|
+
if (skipFunctions && ts.isFunctionLike(node)) return false;
|
|
112
|
+
return predicate(node) || Boolean(ts.forEachChild(node, (child) => contains(child, predicate, skipFunctions) || void 0));
|
|
113
|
+
}
|
|
114
|
+
function durableCall(node) {
|
|
115
|
+
return ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === context && node.expression.name.text in durableMethods;
|
|
116
|
+
}
|
|
117
|
+
function stringLiteral(node) {
|
|
118
|
+
return node && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) ? node.text : void 0;
|
|
119
|
+
}
|
|
120
|
+
function numberLiteral(node) {
|
|
121
|
+
if (!node || !ts.isNumericLiteral(node)) return void 0;
|
|
122
|
+
const value = Number(node.text);
|
|
123
|
+
return Number.isFinite(value) ? value : void 0;
|
|
124
|
+
}
|
|
125
|
+
function runtimeRetry(retry) {
|
|
126
|
+
return {
|
|
127
|
+
maxAttempts: Math.min(4294967295, Math.max(1, Math.floor(retry?.maxAttempts ?? 1))),
|
|
128
|
+
retryBackoffStrategy: typeof retry?.backoff === "function" ? 3 : retry?.backoff === "fixed" ? 2 : 1,
|
|
129
|
+
retryBaseMs: retry?.baseMs ?? 500,
|
|
130
|
+
hasCustomShouldRetry: retry?.shouldRetry !== void 0
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function templateReference(node, parameters) {
|
|
134
|
+
while (ts.isParenthesizedExpression(node)) node = node.expression;
|
|
135
|
+
if (ts.isIdentifier(node)) return parameters.has(node.text) ? node.text : void 0;
|
|
136
|
+
if (ts.isPropertyAccessExpression(node) && !node.questionDotToken) {
|
|
137
|
+
const expression = templateReference(node.expression, parameters);
|
|
138
|
+
return expression === void 0 ? void 0 : `${expression}.${node.name.text}`;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function stepIdTemplate(node, parameters) {
|
|
142
|
+
if (!node || !ts.isTemplateExpression(node)) return void 0;
|
|
143
|
+
let template = node.head.text;
|
|
144
|
+
for (const span of node.templateSpans) {
|
|
145
|
+
const reference = templateReference(span.expression, parameters);
|
|
146
|
+
if (reference === void 0) return void 0;
|
|
147
|
+
template += `{${reference}}${span.literal.text}`;
|
|
148
|
+
}
|
|
149
|
+
return template;
|
|
150
|
+
}
|
|
151
|
+
function mappedStep(node) {
|
|
152
|
+
while (ts.isParenthesizedExpression(node)) node = node.expression;
|
|
153
|
+
if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression) || node.expression.name.text !== "map" || node.arguments.length !== 1) return;
|
|
154
|
+
const callback = node.arguments[0];
|
|
155
|
+
if (!ts.isArrowFunction(callback) && !ts.isFunctionExpression(callback)) return void 0;
|
|
156
|
+
const parameters = new Set(callback.parameters.flatMap((parameter) => ts.isIdentifier(parameter.name) ? [parameter.name.text] : []));
|
|
157
|
+
let body;
|
|
158
|
+
if (ts.isBlock(callback.body)) {
|
|
159
|
+
const statement = callback.body.statements.length === 1 ? callback.body.statements[0] : void 0;
|
|
160
|
+
body = statement && ts.isReturnStatement(statement) ? statement.expression : void 0;
|
|
161
|
+
} else body = callback.body;
|
|
162
|
+
while (body && (ts.isParenthesizedExpression(body) || ts.isAwaitExpression(body))) body = body.expression;
|
|
163
|
+
if (!body || !durableCall(body) || body.expression.name.text !== "step") return;
|
|
164
|
+
const template = stepIdTemplate(body.arguments[0], parameters);
|
|
165
|
+
return template === void 0 ? void 0 : {
|
|
166
|
+
call: body,
|
|
167
|
+
template
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
function sequentialLoopStep(node) {
|
|
171
|
+
if (node.awaitModifier || !ts.isVariableDeclarationList(node.initializer)) return void 0;
|
|
172
|
+
const declarations = node.initializer.declarations;
|
|
173
|
+
if (declarations.length !== 1) return void 0;
|
|
174
|
+
const declaration = declarations[0];
|
|
175
|
+
if (!ts.isIdentifier(declaration.name) || declaration.initializer) return void 0;
|
|
176
|
+
let statement = node.statement;
|
|
177
|
+
if (ts.isBlock(statement)) statement = statement.statements.length === 1 ? statement.statements[0] : void 0;
|
|
178
|
+
if (!statement || !ts.isExpressionStatement(statement)) return void 0;
|
|
179
|
+
let body = statement.expression;
|
|
180
|
+
while (ts.isParenthesizedExpression(body)) body = body.expression;
|
|
181
|
+
if (!ts.isAwaitExpression(body)) return void 0;
|
|
182
|
+
let awaited = body.expression;
|
|
183
|
+
while (ts.isParenthesizedExpression(awaited)) awaited = awaited.expression;
|
|
184
|
+
if (!durableCall(awaited) || awaited.expression.name.text !== "step") return;
|
|
185
|
+
const template = stepIdTemplate(awaited.arguments[0], /* @__PURE__ */ new Set([declaration.name.text]));
|
|
186
|
+
return template === void 0 ? void 0 : {
|
|
187
|
+
call: awaited,
|
|
188
|
+
template
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function readableOptions(node) {
|
|
192
|
+
return node && ts.isObjectLiteralExpression(node) && node.properties.every(ts.isPropertyAssignment) ? node : void 0;
|
|
193
|
+
}
|
|
194
|
+
function option(options, name) {
|
|
195
|
+
for (const property of options?.properties ?? []) {
|
|
196
|
+
if (!ts.isPropertyAssignment(property)) continue;
|
|
197
|
+
if ((ts.isIdentifier(property.name) || ts.isStringLiteral(property.name) ? property.name.text : void 0) === name) return property.initializer;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function branchMetadata(meta) {
|
|
201
|
+
const branches = option(meta, "branches");
|
|
202
|
+
if (!branches || !ts.isArrayLiteralExpression(branches)) return void 0;
|
|
203
|
+
const path = [];
|
|
204
|
+
for (const element of branches.elements) {
|
|
205
|
+
const entry = readableOptions(element);
|
|
206
|
+
const id = stringLiteral(option(entry, "id"));
|
|
207
|
+
const description = stringLiteral(option(entry, "description"));
|
|
208
|
+
if (id === void 0 || description === void 0) return void 0;
|
|
209
|
+
path.push({
|
|
210
|
+
id,
|
|
211
|
+
description
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return path;
|
|
215
|
+
}
|
|
216
|
+
function operationMeta(call) {
|
|
217
|
+
const method = call.expression.name.text;
|
|
218
|
+
const optionsIndex = method === "step" ? 2 : method === "llm" || method === "sleep" || method === "sleepUntil" || method === "waitForEvent" ? 1 : 0;
|
|
219
|
+
return readableOptions(option(readableOptions(call.arguments[optionsIndex]), "meta"));
|
|
220
|
+
}
|
|
221
|
+
function operationBranches(call) {
|
|
222
|
+
return branchMetadata(operationMeta(call));
|
|
223
|
+
}
|
|
224
|
+
function stepMetadata(call) {
|
|
225
|
+
const meta = readableOptions(option(readableOptions(call.arguments[2]), "meta"));
|
|
226
|
+
const description = stringLiteral(option(meta, "description"));
|
|
227
|
+
if (description === void 0) return void 0;
|
|
228
|
+
const groupOptions = readableOptions(option(meta, "group"));
|
|
229
|
+
if (!groupOptions) return { description };
|
|
230
|
+
const id = stringLiteral(option(groupOptions, "id"));
|
|
231
|
+
const label = stringLiteral(option(groupOptions, "label"));
|
|
232
|
+
return id === void 0 || label === void 0 ? { description } : {
|
|
233
|
+
description,
|
|
234
|
+
group: {
|
|
235
|
+
id,
|
|
236
|
+
label
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function stepExecutionPolicy(options) {
|
|
241
|
+
const retryNode = option(options, "retry");
|
|
242
|
+
let retryPolicy = runtimeRetry(workflow.defaultStepRetry);
|
|
243
|
+
if (retryNode) {
|
|
244
|
+
const retryOptions = readableOptions(retryNode);
|
|
245
|
+
const maxAttemptsNode = option(retryOptions, "maxAttempts");
|
|
246
|
+
const maxAttempts = maxAttemptsNode === void 0 ? 1 : numberLiteral(maxAttemptsNode);
|
|
247
|
+
const backoffNode = option(retryOptions, "backoff");
|
|
248
|
+
const baseMsNode = option(retryOptions, "baseMs");
|
|
249
|
+
const baseMs = baseMsNode === void 0 ? 500 : numberLiteral(baseMsNode);
|
|
250
|
+
const shouldRetryNode = option(retryOptions, "shouldRetry");
|
|
251
|
+
const backoff = backoffNode === void 0 ? "exponential" : stringLiteral(backoffNode) ?? (ts.isArrowFunction(backoffNode) || ts.isFunctionExpression(backoffNode) || ts.isIdentifier(backoffNode) ? "custom" : void 0);
|
|
252
|
+
const supportedBackoff = backoff === "exponential" || backoff === "fixed" || backoff === "custom";
|
|
253
|
+
const customCondition = shouldRetryNode === void 0 ? false : ts.isArrowFunction(shouldRetryNode) || ts.isFunctionExpression(shouldRetryNode) || ts.isIdentifier(shouldRetryNode) ? true : void 0;
|
|
254
|
+
if (retryOptions && maxAttempts !== void 0 && baseMs !== void 0 && supportedBackoff && customCondition !== void 0) retryPolicy = {
|
|
255
|
+
maxAttempts: Math.min(4294967295, Math.max(1, Math.floor(maxAttempts))),
|
|
256
|
+
retryBackoffStrategy: backoff === "custom" ? 3 : backoff === "fixed" ? 2 : 1,
|
|
257
|
+
retryBaseMs: baseMs,
|
|
258
|
+
hasCustomShouldRetry: customCondition
|
|
259
|
+
};
|
|
260
|
+
else {
|
|
261
|
+
degrade("Dynamic step retry policies are not recorded");
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const timeoutNode = option(options, "timeout");
|
|
266
|
+
const timeoutMs = numberLiteral(timeoutNode);
|
|
267
|
+
if (timeoutNode !== void 0 && timeoutMs === void 0) {
|
|
268
|
+
degrade("Dynamic step timeouts are not recorded");
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
return create(WorkflowManifestStepExecutionPolicyV1Schema, {
|
|
272
|
+
...retryPolicy,
|
|
273
|
+
...timeoutMs === void 0 || timeoutMs <= 0 ? {} : { timeoutMs }
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
function operationDescription(options) {
|
|
277
|
+
return stringLiteral(option(readableOptions(option(options, "meta")), "description"));
|
|
278
|
+
}
|
|
279
|
+
/** Undefined when the call names no static id, which the graph cannot invent. */
|
|
280
|
+
function durableStepId(argument, options) {
|
|
281
|
+
if (argument && !options) return void 0;
|
|
282
|
+
const given = option(options, "id");
|
|
283
|
+
return given ? stringLiteral(given) : void 0;
|
|
284
|
+
}
|
|
285
|
+
function durableNode(call, parents) {
|
|
286
|
+
const method = call.expression.name.text;
|
|
287
|
+
if (method === "step") {
|
|
288
|
+
const stepId = stringLiteral(call.arguments[0]);
|
|
289
|
+
if (stepId === void 0) return unknown("Dynamic step IDs are not supported yet", parents);
|
|
290
|
+
const metadata = stepMetadata(call);
|
|
291
|
+
if (!metadata) return unknown("Step descriptions must be static strings", parents);
|
|
292
|
+
const executionPolicy = stepExecutionPolicy(readableOptions(call.arguments[2]));
|
|
293
|
+
return addNode(stepId, {
|
|
294
|
+
case: "step",
|
|
295
|
+
value: create(WorkflowManifestStepNodeV1Schema, {
|
|
296
|
+
stepId,
|
|
297
|
+
description: metadata.description,
|
|
298
|
+
...executionPolicy === void 0 ? {} : { executionPolicy }
|
|
299
|
+
})
|
|
300
|
+
}, parents);
|
|
301
|
+
}
|
|
302
|
+
if (method === "llm") {
|
|
303
|
+
const options = readableOptions(call.arguments[1]);
|
|
304
|
+
const stepId = durableStepId(call.arguments[1], options);
|
|
305
|
+
if (stepId === void 0) return unknown("ctx.llm needs a static { id }", parents);
|
|
306
|
+
const description = operationDescription(options);
|
|
307
|
+
if (description === void 0) return unknown("LLM descriptions must be static strings", parents);
|
|
308
|
+
const model = stringLiteral(option(readableOptions(call.arguments[0]), "model"));
|
|
309
|
+
const executionPolicy = stepExecutionPolicy(options);
|
|
310
|
+
if (executionPolicy) executionPolicy.timeoutMs = Math.min(executionPolicy.timeoutMs ?? 12e4, 6e5);
|
|
311
|
+
return addNode(stepId, {
|
|
312
|
+
case: "llm",
|
|
313
|
+
value: create(WorkflowManifestLlmNodeV1Schema, {
|
|
314
|
+
stepId,
|
|
315
|
+
description,
|
|
316
|
+
...executionPolicy === void 0 ? {} : { executionPolicy },
|
|
317
|
+
...model === void 0 ? {} : { model }
|
|
318
|
+
})
|
|
319
|
+
}, parents);
|
|
320
|
+
}
|
|
321
|
+
if (method === "approve") {
|
|
322
|
+
const options = readableOptions(call.arguments[0]);
|
|
323
|
+
const title = stringLiteral(option(options, "title"));
|
|
324
|
+
if (title === void 0) return unknown("Dynamic approval titles are not supported yet", parents);
|
|
325
|
+
const stepId = durableStepId(call.arguments[0], options);
|
|
326
|
+
if (stepId === void 0) return unknown("ctx.approve needs a static { id }", parents);
|
|
327
|
+
const description = stringLiteral(option(options, "description"));
|
|
328
|
+
if (description === void 0) return unknown("Approval descriptions must be static strings", parents);
|
|
329
|
+
const deadline = numberLiteral(option(options, "timeoutMs"));
|
|
330
|
+
return addNode(title, {
|
|
331
|
+
case: "approve",
|
|
332
|
+
value: create(WorkflowManifestApproveNodeV1Schema, {
|
|
333
|
+
stepId,
|
|
334
|
+
description,
|
|
335
|
+
title,
|
|
336
|
+
...deadline === void 0 ? {} : { deadline }
|
|
337
|
+
})
|
|
338
|
+
}, parents);
|
|
339
|
+
}
|
|
340
|
+
if (method === "waitForEvent") {
|
|
341
|
+
const event = stringLiteral(call.arguments[0]);
|
|
342
|
+
if (event === void 0) return unknown("Dynamic event names are not supported yet", parents);
|
|
343
|
+
const options = readableOptions(call.arguments[1]);
|
|
344
|
+
const stepId = durableStepId(call.arguments[1], options);
|
|
345
|
+
if (stepId === void 0) return unknown("Dynamic waitForEvent IDs are not supported yet", parents);
|
|
346
|
+
const description = operationDescription(options);
|
|
347
|
+
if (description === void 0) return unknown("Wait descriptions must be static strings", parents);
|
|
348
|
+
const deadline = numberLiteral(option(options, "timeoutMs"));
|
|
349
|
+
return addNode(event, {
|
|
350
|
+
case: "waitForEvent",
|
|
351
|
+
value: create(WorkflowManifestWaitForEventNodeV1Schema, {
|
|
352
|
+
stepId,
|
|
353
|
+
description,
|
|
354
|
+
event,
|
|
355
|
+
...deadline === void 0 ? {} : { deadline }
|
|
356
|
+
})
|
|
357
|
+
}, parents);
|
|
358
|
+
}
|
|
359
|
+
if (method === "sleep") {
|
|
360
|
+
const ms = numberLiteral(call.arguments[0]);
|
|
361
|
+
if (ms === void 0) return unknown("Dynamic sleep durations are not supported yet", parents);
|
|
362
|
+
const options = readableOptions(call.arguments[1]);
|
|
363
|
+
const stepId = durableStepId(call.arguments[1], options);
|
|
364
|
+
if (stepId === void 0) return unknown("Dynamic sleep IDs are not supported yet", parents);
|
|
365
|
+
const description = operationDescription(options);
|
|
366
|
+
if (description === void 0) return unknown("Sleep descriptions must be static strings", parents);
|
|
367
|
+
return addNode(`sleep ${ms}ms`, {
|
|
368
|
+
case: "sleep",
|
|
369
|
+
value: create(WorkflowManifestSleepNodeV1Schema, {
|
|
370
|
+
stepId,
|
|
371
|
+
description,
|
|
372
|
+
ms
|
|
373
|
+
})
|
|
374
|
+
}, parents);
|
|
375
|
+
}
|
|
376
|
+
if (method === "sleepUntil") {
|
|
377
|
+
const timestamp = numberLiteral(call.arguments[0]);
|
|
378
|
+
if (timestamp === void 0) return unknown("Dynamic sleepUntil timestamps are not supported yet", parents);
|
|
379
|
+
const options = readableOptions(call.arguments[1]);
|
|
380
|
+
const stepId = durableStepId(call.arguments[1], options);
|
|
381
|
+
if (stepId === void 0) return unknown("Dynamic sleepUntil IDs are not supported yet", parents);
|
|
382
|
+
const description = operationDescription(options);
|
|
383
|
+
if (description === void 0) return unknown("Sleep descriptions must be static strings", parents);
|
|
384
|
+
return addNode(`sleep until ${timestamp}`, {
|
|
385
|
+
case: "sleepUntil",
|
|
386
|
+
value: create(WorkflowManifestSleepUntilNodeV1Schema, {
|
|
387
|
+
stepId,
|
|
388
|
+
description,
|
|
389
|
+
timestamp
|
|
390
|
+
})
|
|
391
|
+
}, parents);
|
|
392
|
+
}
|
|
393
|
+
const options = readableOptions(call.arguments[0]);
|
|
394
|
+
const stepId = durableStepId(call.arguments[0], options);
|
|
395
|
+
if (stepId === void 0) return unknown(`Dynamic ${method} IDs are not supported yet`, parents);
|
|
396
|
+
const description = operationDescription(options);
|
|
397
|
+
if (description === void 0) return unknown(`${method} descriptions must be static strings`, parents);
|
|
398
|
+
return addNode(stepId, method === "now" ? {
|
|
399
|
+
case: "now",
|
|
400
|
+
value: create(WorkflowManifestNowNodeV1Schema, {
|
|
401
|
+
stepId,
|
|
402
|
+
description
|
|
403
|
+
})
|
|
404
|
+
} : {
|
|
405
|
+
case: "uuid",
|
|
406
|
+
value: create(WorkflowManifestUuidNodeV1Schema, {
|
|
407
|
+
stepId,
|
|
408
|
+
description
|
|
409
|
+
})
|
|
410
|
+
}, parents);
|
|
411
|
+
}
|
|
412
|
+
function path(tails, reachable = true) {
|
|
413
|
+
return {
|
|
414
|
+
tails: reachable ? tails : [],
|
|
415
|
+
reachable
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
function mergePaths(...items) {
|
|
419
|
+
const tails = [];
|
|
420
|
+
const seen = /* @__PURE__ */ new Set();
|
|
421
|
+
for (const item of items) for (const tail of item.tails) {
|
|
422
|
+
const key = JSON.stringify(tail);
|
|
423
|
+
if (seen.has(key)) continue;
|
|
424
|
+
seen.add(key);
|
|
425
|
+
tails.push(tail);
|
|
426
|
+
}
|
|
427
|
+
return path(tails, items.some((item) => item.reachable));
|
|
428
|
+
}
|
|
429
|
+
function expression(node, parents, awaited = false) {
|
|
430
|
+
if (ts.isParenthesizedExpression(node)) return expression(node.expression, parents, awaited);
|
|
431
|
+
if (ts.isAwaitExpression(node)) return expression(node.expression, parents, true);
|
|
432
|
+
if (durableCall(node)) {
|
|
433
|
+
let tails = awaited ? durableNode(node, parents) : unknown("Unawaited durable calls are not supported yet", parents);
|
|
434
|
+
if (node.arguments.some((argument) => contains(argument, durableCall))) tails = unknown("Nested durable calls are not supported yet", tails);
|
|
435
|
+
return {
|
|
436
|
+
tails,
|
|
437
|
+
thrown: path(tails)
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
if (awaited && ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === "Promise" && node.expression.name.text === "all" && node.arguments.length === 1) {
|
|
441
|
+
const argument = node.arguments[0];
|
|
442
|
+
if (ts.isArrayLiteralExpression(argument)) {
|
|
443
|
+
const elements = argument.elements;
|
|
444
|
+
if (elements.some((element) => contains(element, ts.isAwaitExpression, true))) {
|
|
445
|
+
const tails = unknown("Await expressions inside Promise.all are not supported yet", parents);
|
|
446
|
+
return {
|
|
447
|
+
tails,
|
|
448
|
+
thrown: path(tails)
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
if (elements.length === 0) return {
|
|
452
|
+
tails: parents,
|
|
453
|
+
thrown: path([], false)
|
|
454
|
+
};
|
|
455
|
+
const flows = elements.map((element) => {
|
|
456
|
+
while (ts.isParenthesizedExpression(element)) element = element.expression;
|
|
457
|
+
if (ts.isCallExpression(element)) return expression(element, parents, true);
|
|
458
|
+
const tails = unknown("Dynamic Promise.all entries are not followed", parents);
|
|
459
|
+
return {
|
|
460
|
+
tails,
|
|
461
|
+
thrown: path(tails)
|
|
462
|
+
};
|
|
463
|
+
});
|
|
464
|
+
return {
|
|
465
|
+
tails: flows.flatMap((flow) => flow.tails),
|
|
466
|
+
thrown: mergePaths(...flows.map((flow) => flow.thrown))
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
const mapped = mappedStep(argument);
|
|
470
|
+
if (mapped !== void 0) {
|
|
471
|
+
const metadata = stepMetadata(mapped.call);
|
|
472
|
+
const executionPolicy = stepExecutionPolicy(readableOptions(mapped.call.arguments[2]));
|
|
473
|
+
const tails = metadata?.group ? addNode(metadata.group.label, {
|
|
474
|
+
case: "parallel",
|
|
475
|
+
value: create(WorkflowManifestParallelNodeV1Schema, {
|
|
476
|
+
groupId: metadata.group.id,
|
|
477
|
+
step: create(WorkflowManifestRepeatedStepV1Schema, {
|
|
478
|
+
stepIdTemplate: mapped.template,
|
|
479
|
+
description: metadata.description,
|
|
480
|
+
...executionPolicy === void 0 ? {} : { executionPolicy }
|
|
481
|
+
})
|
|
482
|
+
})
|
|
483
|
+
}, parents) : unknown("Dynamic parallel steps need static group metadata", parents);
|
|
484
|
+
return {
|
|
485
|
+
tails,
|
|
486
|
+
thrown: path(tails)
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
if (contains(node, (child) => ts.isCallExpression(child) || ts.isAwaitExpression(child) || ts.isNewExpression(child))) {
|
|
491
|
+
const tails = unknown("Helper calls and dynamic expressions are not followed", parents);
|
|
492
|
+
return {
|
|
493
|
+
tails,
|
|
494
|
+
thrown: path(tails)
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
if (ts.isIdentifier(node) && node.text === context) return {
|
|
498
|
+
tails: unknown("Workflow context aliases are not followed", parents),
|
|
499
|
+
thrown: path([], false)
|
|
500
|
+
};
|
|
501
|
+
return {
|
|
502
|
+
tails: parents,
|
|
503
|
+
thrown: path([], false)
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
function branchInfo(statement, depth) {
|
|
507
|
+
let result;
|
|
508
|
+
const visit = (node) => {
|
|
509
|
+
if (result) return;
|
|
510
|
+
if (durableCall(node)) {
|
|
511
|
+
result = operationBranches(node)?.[depth];
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
ts.forEachChild(node, visit);
|
|
515
|
+
};
|
|
516
|
+
visit(statement.thenStatement);
|
|
517
|
+
if (statement.elseStatement) visit(statement.elseStatement);
|
|
518
|
+
return result;
|
|
519
|
+
}
|
|
520
|
+
function statementFlow(normal, thrown = path([], false), returned = path([], false), stopped = path([], false)) {
|
|
521
|
+
return {
|
|
522
|
+
normal,
|
|
523
|
+
thrown,
|
|
524
|
+
returned,
|
|
525
|
+
stopped
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
function statements(items, parents, branchDepth) {
|
|
529
|
+
let normal = path(parents);
|
|
530
|
+
let thrown = path([], false);
|
|
531
|
+
let returned = path([], false);
|
|
532
|
+
let stopped = path([], false);
|
|
533
|
+
for (const statement of items) {
|
|
534
|
+
if (!normal.reachable) break;
|
|
535
|
+
const flow = walkStatement(statement, normal.tails, branchDepth);
|
|
536
|
+
normal = flow.normal;
|
|
537
|
+
thrown = mergePaths(thrown, flow.thrown);
|
|
538
|
+
returned = mergePaths(returned, flow.returned);
|
|
539
|
+
stopped = mergePaths(stopped, flow.stopped);
|
|
540
|
+
}
|
|
541
|
+
return statementFlow(normal, thrown, returned, stopped);
|
|
542
|
+
}
|
|
543
|
+
function walkStatement(statement, parents, branchDepth) {
|
|
544
|
+
if (ts.isBlock(statement)) return statements(statement.statements, parents, branchDepth);
|
|
545
|
+
if (ts.isReturnStatement(statement)) {
|
|
546
|
+
const result = statement.expression ? expression(statement.expression, parents, true) : {
|
|
547
|
+
tails: parents,
|
|
548
|
+
thrown: path([], false)
|
|
549
|
+
};
|
|
550
|
+
return statementFlow(path([], false), result.thrown, path(result.tails));
|
|
551
|
+
}
|
|
552
|
+
if (ts.isThrowStatement(statement)) {
|
|
553
|
+
const result = expression(statement.expression, parents);
|
|
554
|
+
return statementFlow(path([], false), mergePaths(result.thrown, path(result.tails)));
|
|
555
|
+
}
|
|
556
|
+
if (ts.isVariableStatement(statement)) {
|
|
557
|
+
if (statement.declarationList.declarations.some((item) => contains(item.name, (child) => ts.isIdentifier(child) && (child.text === "Promise" || child.text === context)))) {
|
|
558
|
+
const tails = unknown("Shadowed context or Promise bindings are not supported yet", parents);
|
|
559
|
+
return statementFlow(path([], false), path([], false), path([], false), path(tails));
|
|
560
|
+
}
|
|
561
|
+
let tails = parents;
|
|
562
|
+
let thrown = path([], false);
|
|
563
|
+
for (const item of statement.declarationList.declarations) {
|
|
564
|
+
if (!item.initializer) continue;
|
|
565
|
+
const result = expression(item.initializer, tails);
|
|
566
|
+
tails = result.tails;
|
|
567
|
+
thrown = mergePaths(thrown, result.thrown);
|
|
568
|
+
}
|
|
569
|
+
return statementFlow(path(tails), thrown);
|
|
570
|
+
}
|
|
571
|
+
if (ts.isExpressionStatement(statement)) {
|
|
572
|
+
const result = expression(statement.expression, parents);
|
|
573
|
+
return statementFlow(path(result.tails), result.thrown);
|
|
574
|
+
}
|
|
575
|
+
if (ts.isIfStatement(statement)) {
|
|
576
|
+
const conditionFlow = expression(statement.expression, parents);
|
|
577
|
+
const metadata = branchInfo(statement, branchDepth);
|
|
578
|
+
const condition = statement.expression.getText(source);
|
|
579
|
+
const branchTails = metadata ? addNode(metadata.description, {
|
|
580
|
+
case: "branch",
|
|
581
|
+
value: create(WorkflowManifestBranchNodeV1Schema, {
|
|
582
|
+
branchId: metadata.id,
|
|
583
|
+
condition,
|
|
584
|
+
description: metadata.description
|
|
585
|
+
})
|
|
586
|
+
}, conditionFlow.tails) : conditionFlow.tails;
|
|
587
|
+
const sourceId = metadata ? branchTails[0]?.source : void 0;
|
|
588
|
+
const thenParents = sourceId ? [{
|
|
589
|
+
source: sourceId,
|
|
590
|
+
kind: "branch",
|
|
591
|
+
outcome: true
|
|
592
|
+
}] : branchTails;
|
|
593
|
+
const elseParents = sourceId ? [{
|
|
594
|
+
source: sourceId,
|
|
595
|
+
kind: "branch",
|
|
596
|
+
outcome: false
|
|
597
|
+
}] : branchTails;
|
|
598
|
+
const thenFlow = walkStatement(statement.thenStatement, thenParents, branchDepth + 1);
|
|
599
|
+
const elseFlow = statement.elseStatement ? walkStatement(statement.elseStatement, elseParents, branchDepth + 1) : statementFlow(path(elseParents));
|
|
600
|
+
return statementFlow(mergePaths(thenFlow.normal, elseFlow.normal), mergePaths(conditionFlow.thrown, thenFlow.thrown, elseFlow.thrown), mergePaths(thenFlow.returned, elseFlow.returned), mergePaths(thenFlow.stopped, elseFlow.stopped));
|
|
601
|
+
}
|
|
602
|
+
if (ts.isTryStatement(statement)) {
|
|
603
|
+
const tryFlow = walkStatement(statement.tryBlock, parents, branchDepth);
|
|
604
|
+
const catchBlock = statement.catchClause?.block;
|
|
605
|
+
const catchHasDurableOperations = catchBlock ? contains(catchBlock, durableCall) : false;
|
|
606
|
+
const catchHasAbruptCompletion = catchBlock ? contains(catchBlock, (node) => ts.isReturnStatement(node) || ts.isThrowStatement(node), true) : false;
|
|
607
|
+
let catchFlow = statementFlow(path([], false));
|
|
608
|
+
if (catchBlock && (tryFlow.thrown.reachable || catchHasDurableOperations || catchHasAbruptCompletion)) if (catchHasDurableOperations || catchHasAbruptCompletion) catchFlow = walkStatement(catchBlock, unknown(catchHasDurableOperations ? "Durable catch paths are conditional on errors" : "Catch completion paths are conditional on errors", tryFlow.thrown.reachable ? tryFlow.thrown.tails : parents), branchDepth);
|
|
609
|
+
else catchFlow = statementFlow(path(tryFlow.thrown.tails));
|
|
610
|
+
const normal = mergePaths(tryFlow.normal, catchFlow.normal);
|
|
611
|
+
const thrown = catchBlock ? catchFlow.thrown : tryFlow.thrown;
|
|
612
|
+
const returned = mergePaths(tryFlow.returned, catchFlow.returned);
|
|
613
|
+
const stopped = mergePaths(tryFlow.stopped, catchFlow.stopped);
|
|
614
|
+
const flow = statementFlow(normal, thrown, returned, stopped);
|
|
615
|
+
if (!statement.finallyBlock) return flow;
|
|
616
|
+
const finallyHasDurableOperations = contains(statement.finallyBlock, durableCall);
|
|
617
|
+
const finallyHasAbruptCompletion = contains(statement.finallyBlock, (node) => ts.isReturnStatement(node) || ts.isThrowStatement(node), true);
|
|
618
|
+
if (!finallyHasDurableOperations && !finallyHasAbruptCompletion) return flow;
|
|
619
|
+
const finallyParents = mergePaths(normal, thrown, returned, stopped);
|
|
620
|
+
const finallyFlow = walkStatement(statement.finallyBlock, finallyParents.tails, branchDepth);
|
|
621
|
+
const completedFinally = finallyFlow.normal.reachable;
|
|
622
|
+
return statementFlow(path(finallyFlow.normal.tails, normal.reachable && completedFinally), mergePaths(finallyFlow.thrown, path(finallyFlow.normal.tails, thrown.reachable && completedFinally)), mergePaths(finallyFlow.returned, path(finallyFlow.normal.tails, returned.reachable && completedFinally)), mergePaths(finallyFlow.stopped, path(finallyFlow.normal.tails, stopped.reachable && completedFinally)));
|
|
623
|
+
}
|
|
624
|
+
if (ts.isForOfStatement(statement)) {
|
|
625
|
+
const repeated = sequentialLoopStep(statement);
|
|
626
|
+
if (repeated) {
|
|
627
|
+
const iterable = expression(statement.expression, parents);
|
|
628
|
+
const metadata = stepMetadata(repeated.call);
|
|
629
|
+
const executionPolicy = stepExecutionPolicy(readableOptions(repeated.call.arguments[2]));
|
|
630
|
+
const tails = metadata?.group ? addNode(metadata.group.label, {
|
|
631
|
+
case: "loop",
|
|
632
|
+
value: create(WorkflowManifestLoopNodeV1Schema, {
|
|
633
|
+
groupId: metadata.group.id,
|
|
634
|
+
step: create(WorkflowManifestRepeatedStepV1Schema, {
|
|
635
|
+
stepIdTemplate: repeated.template,
|
|
636
|
+
description: metadata.description,
|
|
637
|
+
...executionPolicy === void 0 ? {} : { executionPolicy }
|
|
638
|
+
})
|
|
639
|
+
})
|
|
640
|
+
}, iterable.tails) : unknown("Dynamic sequential steps need static group metadata", iterable.tails);
|
|
641
|
+
return statementFlow(path(tails), mergePaths(iterable.thrown, path(tails)));
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
if (ts.isEmptyStatement(statement)) return statementFlow(path(parents));
|
|
645
|
+
return statementFlow(path(unknown("Loops and other control flow are not supported yet", parents)));
|
|
646
|
+
}
|
|
647
|
+
const tails = [];
|
|
648
|
+
if (!ts.isBlock(fn.body)) {
|
|
649
|
+
expression(fn.body, tails, true);
|
|
650
|
+
return graph;
|
|
651
|
+
}
|
|
652
|
+
statements(fn.body.statements, tails, 0);
|
|
653
|
+
return graph;
|
|
654
|
+
}
|
|
655
|
+
//#endregion
|
|
656
|
+
export { extractWorkflowGraph, loadTypeScript, workflowHandlerSource as t };
|