@segmentstream/ontology 0.102.0 → 0.105.0
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/CHANGELOG.md +42 -0
- package/README.md +11 -6
- package/dist/analyze.js +34 -3
- package/dist/index.d.ts +4 -4
- package/dist/index.js +3 -3
- package/dist/manifest.d.ts +25 -1
- package/dist/manifest.js +28 -2
- package/dist/migrate/model.d.ts +8 -0
- package/dist/nitro-workflows.d.ts +14 -0
- package/dist/nitro-workflows.js +62 -2
- package/dist/runtime/agent-web.d.ts +9 -2
- package/dist/runtime/agent-web.js +82 -3
- package/dist/runtime/agents.js +4 -1
- package/dist/server.d.ts +7 -0
- package/dist/server.js +9 -1
- package/dist/service/agent/server.d.ts +4 -0
- package/dist/service/agent/server.js +8 -1
- package/dist/service/agent/web-tokens.d.ts +15 -1
- package/dist/service/agent/web-tokens.js +82 -0
- package/dist/service/runtime-contract.d.ts +25 -1
- package/dist/service/schemas.d.ts +2 -0
- package/dist/service/service.js +18 -5
- package/dist/service/workflows.d.ts +2 -0
- package/dist/service/workflows.js +14 -1
- package/dist/sign-in.d.ts +18 -3
- package/dist/sign-in.js +23 -3
- package/dist/storage/tables.d.ts +1 -1
- package/dist/storage/tables.js +1 -1
- package/dist/workflow-runtime.d.ts +5 -0
- package/dist/workflow-runtime.js +9 -0
- package/dist/workflow-structure-extract.d.ts +52 -0
- package/dist/workflow-structure-extract.js +1096 -0
- package/dist/workflow-structure.d.ts +141 -0
- package/dist/workflow-structure.js +16 -0
- package/package.json +2 -2
|
@@ -0,0 +1,1096 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { WORKFLOW_STRUCTURE_VERSION, } from "./workflow-structure.js";
|
|
4
|
+
const readSource = (path) => {
|
|
5
|
+
try {
|
|
6
|
+
return readFileSync(path, "utf8");
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
/** The SDK's waits, imported from `workflow`. */
|
|
13
|
+
const WAITS = {
|
|
14
|
+
sleep: "sleep",
|
|
15
|
+
createHook: "hook",
|
|
16
|
+
createWebhook: "webhook",
|
|
17
|
+
};
|
|
18
|
+
/** Where the SDK's waits come from. */
|
|
19
|
+
const WAIT_MODULES = new Set(["workflow"]);
|
|
20
|
+
/** Where `agentTask` comes from. */
|
|
21
|
+
const AGENT_MODULES = new Set(["@segmentstream/ontology/workflows", "@segmentstream/ontology"]);
|
|
22
|
+
/** An entity handle's record operations, as a step calls them: `Lead.find(…)`. */
|
|
23
|
+
const RECORD_OPERATIONS = new Set([
|
|
24
|
+
"get",
|
|
25
|
+
"find",
|
|
26
|
+
"create",
|
|
27
|
+
"update",
|
|
28
|
+
"upsertBy",
|
|
29
|
+
"delete",
|
|
30
|
+
"link",
|
|
31
|
+
"unlink",
|
|
32
|
+
]);
|
|
33
|
+
/** A connection's request methods, as a step calls them with a literal path: `api.post("/x")`. */
|
|
34
|
+
const REQUEST_METHODS = new Set(["get", "post", "put", "patch", "delete", "head", "request"]);
|
|
35
|
+
const COMBINATORS = new Set(["all", "allSettled", "any", "race"]);
|
|
36
|
+
const EACH_METHODS = new Set(["map", "forEach", "flatMap"]);
|
|
37
|
+
/** Files the reading follows imports into: the project's own, not its packages. */
|
|
38
|
+
const EXTENSIONS = [".ts", ".tsx", ".mts", ".cts", ".js", ".mjs", ".jsx"];
|
|
39
|
+
const MAX_TEXT = 120;
|
|
40
|
+
const MAX_DESCRIPTION = 600;
|
|
41
|
+
const MAX_CALLS = 24;
|
|
42
|
+
const MAX_DEPTH = 24;
|
|
43
|
+
/** Collects one block's nodes, numbering them under the block's path. */
|
|
44
|
+
class Emitter {
|
|
45
|
+
prefix;
|
|
46
|
+
nodes = [];
|
|
47
|
+
#next = 0;
|
|
48
|
+
#note;
|
|
49
|
+
constructor(prefix) {
|
|
50
|
+
this.prefix = prefix;
|
|
51
|
+
}
|
|
52
|
+
id() {
|
|
53
|
+
return `${this.prefix}${this.#next++}`;
|
|
54
|
+
}
|
|
55
|
+
/** The comment of the statement being read, given to the first node it emits. */
|
|
56
|
+
withNote(note) {
|
|
57
|
+
this.#note = note;
|
|
58
|
+
}
|
|
59
|
+
push(node) {
|
|
60
|
+
const note = this.#note;
|
|
61
|
+
this.#note = undefined;
|
|
62
|
+
this.nodes.push(note === undefined || node.note !== undefined ? node : { ...node, note });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function collapse(text, max = MAX_TEXT) {
|
|
66
|
+
const single = text.replace(/\s+/g, " ").trim();
|
|
67
|
+
return single.length > max ? `${single.slice(0, max - 1)}…` : single;
|
|
68
|
+
}
|
|
69
|
+
function directiveOf(ts, node) {
|
|
70
|
+
const body = node.body;
|
|
71
|
+
if (body === undefined || !ts.isBlock(body)) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
for (const statement of body.statements) {
|
|
75
|
+
if (!ts.isExpressionStatement(statement) || !ts.isStringLiteral(statement.expression)) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
const text = statement.expression.text;
|
|
79
|
+
if (text === "use step" || text === "use workflow") {
|
|
80
|
+
return text;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The comment directly above a node, without its markers: the last group of comments not
|
|
87
|
+
* separated from the node, or from each other, by a blank line. JSDoc tags are left out.
|
|
88
|
+
*/
|
|
89
|
+
export function leadingComment(ts, node, source) {
|
|
90
|
+
const text = source.text;
|
|
91
|
+
const ranges = ts.getLeadingCommentRanges(text, node.getFullStart()) ?? [];
|
|
92
|
+
const group = [];
|
|
93
|
+
let end = node.getStart(source);
|
|
94
|
+
for (let index = ranges.length - 1; index >= 0; index -= 1) {
|
|
95
|
+
const range = ranges[index];
|
|
96
|
+
if (/\n[^\S\n]*\n/.test(text.slice(range.end, end))) {
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
group.unshift(range);
|
|
100
|
+
end = range.pos;
|
|
101
|
+
}
|
|
102
|
+
const lines = group.flatMap((range) => {
|
|
103
|
+
const raw = text.slice(range.pos, range.end);
|
|
104
|
+
if (range.kind === ts.SyntaxKind.SingleLineCommentTrivia) {
|
|
105
|
+
return [raw.replace(/^\/\/+\s?/, "")];
|
|
106
|
+
}
|
|
107
|
+
return raw
|
|
108
|
+
.replace(/^\/\*+/, "")
|
|
109
|
+
.replace(/\*+\/$/, "")
|
|
110
|
+
.split("\n")
|
|
111
|
+
.map((line) => line.replace(/^\s*\*\s?/, ""));
|
|
112
|
+
});
|
|
113
|
+
const kept = [];
|
|
114
|
+
for (const line of lines) {
|
|
115
|
+
if (/^\s*@\w/.test(line)) {
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
kept.push(line.trim());
|
|
119
|
+
}
|
|
120
|
+
const paragraphs = kept
|
|
121
|
+
.join("\n")
|
|
122
|
+
.split(/\n\s*\n/)
|
|
123
|
+
.map((paragraph) => paragraph.replace(/\s+/g, " ").trim())
|
|
124
|
+
.filter((paragraph) => paragraph !== "");
|
|
125
|
+
const description = paragraphs.join("\n\n");
|
|
126
|
+
if (description === "") {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
return description.length > MAX_DESCRIPTION
|
|
130
|
+
? `${description.slice(0, MAX_DESCRIPTION - 1)}…`
|
|
131
|
+
: description;
|
|
132
|
+
}
|
|
133
|
+
function hasExport(ts, node) {
|
|
134
|
+
return (ts.canHaveModifiers(node) &&
|
|
135
|
+
(ts.getModifiers(node) ?? []).some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword));
|
|
136
|
+
}
|
|
137
|
+
function hasDefault(ts, node) {
|
|
138
|
+
return (ts.canHaveModifiers(node) &&
|
|
139
|
+
(ts.getModifiers(node) ?? []).some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword));
|
|
140
|
+
}
|
|
141
|
+
function parseModule(ts, file, text) {
|
|
142
|
+
const source = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, scriptKind(ts, file));
|
|
143
|
+
const functions = new Map();
|
|
144
|
+
const exports = new Map();
|
|
145
|
+
const imports = new Map();
|
|
146
|
+
const values = new Set();
|
|
147
|
+
const declare = (name, node, described) => {
|
|
148
|
+
functions.set(name, { file, name, node, described, directive: directiveOf(ts, node) });
|
|
149
|
+
};
|
|
150
|
+
for (const statement of source.statements) {
|
|
151
|
+
if (ts.isFunctionDeclaration(statement) && statement.body !== undefined) {
|
|
152
|
+
const name = statement.name?.text ?? "default";
|
|
153
|
+
declare(name, statement, statement);
|
|
154
|
+
if (hasExport(ts, statement)) {
|
|
155
|
+
exports.set(hasDefault(ts, statement) ? "default" : name, name);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else if (ts.isVariableStatement(statement)) {
|
|
159
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
160
|
+
if (!ts.isIdentifier(declaration.name)) {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
const name = declaration.name.text;
|
|
164
|
+
const initializer = declaration.initializer;
|
|
165
|
+
if (initializer !== undefined &&
|
|
166
|
+
(ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer))) {
|
|
167
|
+
declare(name, initializer, statement);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
values.add(name);
|
|
171
|
+
}
|
|
172
|
+
if (hasExport(ts, statement)) {
|
|
173
|
+
exports.set(name, name);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else if (ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier)) {
|
|
178
|
+
const from = statement.moduleSpecifier.text;
|
|
179
|
+
const clause = statement.importClause;
|
|
180
|
+
if (clause === undefined || clause.isTypeOnly) {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (clause.name !== undefined) {
|
|
184
|
+
imports.set(clause.name.text, { module: from, name: "default" });
|
|
185
|
+
}
|
|
186
|
+
const bindings = clause.namedBindings;
|
|
187
|
+
if (bindings !== undefined && ts.isNamespaceImport(bindings)) {
|
|
188
|
+
imports.set(bindings.name.text, { module: from, name: "*" });
|
|
189
|
+
}
|
|
190
|
+
else if (bindings !== undefined) {
|
|
191
|
+
for (const element of bindings.elements) {
|
|
192
|
+
if (!element.isTypeOnly) {
|
|
193
|
+
imports.set(element.name.text, {
|
|
194
|
+
module: from,
|
|
195
|
+
name: (element.propertyName ?? element.name).text,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
else if (ts.isExportDeclaration(statement) &&
|
|
202
|
+
statement.moduleSpecifier === undefined &&
|
|
203
|
+
statement.exportClause !== undefined &&
|
|
204
|
+
ts.isNamedExports(statement.exportClause)) {
|
|
205
|
+
for (const element of statement.exportClause.elements) {
|
|
206
|
+
exports.set(element.name.text, (element.propertyName ?? element.name).text);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else if (ts.isExportAssignment(statement) && ts.isIdentifier(statement.expression)) {
|
|
210
|
+
exports.set("default", statement.expression.text);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return { file, source, functions, exports, imports, values };
|
|
214
|
+
}
|
|
215
|
+
function scriptKind(ts, file) {
|
|
216
|
+
if (file.endsWith(".tsx")) {
|
|
217
|
+
return ts.ScriptKind.TSX;
|
|
218
|
+
}
|
|
219
|
+
if (file.endsWith(".jsx")) {
|
|
220
|
+
return ts.ScriptKind.JSX;
|
|
221
|
+
}
|
|
222
|
+
if (/\.[cm]?js$/.test(file)) {
|
|
223
|
+
return ts.ScriptKind.JS;
|
|
224
|
+
}
|
|
225
|
+
return ts.ScriptKind.TS;
|
|
226
|
+
}
|
|
227
|
+
class Reading {
|
|
228
|
+
#ts;
|
|
229
|
+
#root;
|
|
230
|
+
#read;
|
|
231
|
+
#stepKey;
|
|
232
|
+
#agentSteps;
|
|
233
|
+
#modules = new Map();
|
|
234
|
+
steps = {};
|
|
235
|
+
complete = true;
|
|
236
|
+
#helpers = [];
|
|
237
|
+
constructor(input) {
|
|
238
|
+
this.#ts = input.ts;
|
|
239
|
+
this.#root = input.root;
|
|
240
|
+
this.#read = input.read;
|
|
241
|
+
this.#stepKey = input.stepKey;
|
|
242
|
+
this.#agentSteps = input.agentSteps;
|
|
243
|
+
}
|
|
244
|
+
module(file) {
|
|
245
|
+
if (!this.#modules.has(file)) {
|
|
246
|
+
const text = this.#read(file);
|
|
247
|
+
this.#modules.set(file, text === undefined ? undefined : parseModule(this.#ts, file, text));
|
|
248
|
+
}
|
|
249
|
+
return this.#modules.get(file);
|
|
250
|
+
}
|
|
251
|
+
/** A relative import's file, trying the extensions TypeScript resolves. */
|
|
252
|
+
#importedFile(from, specifier) {
|
|
253
|
+
if (!specifier.startsWith(".")) {
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
256
|
+
const base = resolve(dirname(from), specifier);
|
|
257
|
+
const stem = base.replace(/\.[cm]?jsx?$/, "");
|
|
258
|
+
const candidates = [
|
|
259
|
+
base,
|
|
260
|
+
...EXTENSIONS.map((extension) => `${stem}${extension}`),
|
|
261
|
+
...EXTENSIONS.map((extension) => `${base}${sep}index${extension}`),
|
|
262
|
+
];
|
|
263
|
+
return candidates.find((candidate) => this.module(candidate) !== undefined);
|
|
264
|
+
}
|
|
265
|
+
/** The function a name refers to in a module, following its relative imports. */
|
|
266
|
+
#function(module, name, depth = 0) {
|
|
267
|
+
const local = module.functions.get(name);
|
|
268
|
+
if (local !== undefined) {
|
|
269
|
+
return local;
|
|
270
|
+
}
|
|
271
|
+
const imported = module.imports.get(name);
|
|
272
|
+
if (imported === undefined || imported.name === "*" || depth > MAX_DEPTH) {
|
|
273
|
+
return undefined;
|
|
274
|
+
}
|
|
275
|
+
const file = this.#importedFile(module.file, imported.module);
|
|
276
|
+
const target = file === undefined ? undefined : this.module(file);
|
|
277
|
+
const exported = target?.exports.get(imported.name);
|
|
278
|
+
return target === undefined || exported === undefined
|
|
279
|
+
? undefined
|
|
280
|
+
: this.#function(target, exported, depth + 1);
|
|
281
|
+
}
|
|
282
|
+
resolve(module, callee, scope) {
|
|
283
|
+
const ts = this.#ts;
|
|
284
|
+
if (!ts.isIdentifier(callee)) {
|
|
285
|
+
return { kind: "none" };
|
|
286
|
+
}
|
|
287
|
+
const name = callee.text;
|
|
288
|
+
const nested = scope.get(name);
|
|
289
|
+
const imported = module.imports.get(name);
|
|
290
|
+
if (nested !== undefined) {
|
|
291
|
+
return nested.directive === "use step"
|
|
292
|
+
? { kind: "step", declared: nested }
|
|
293
|
+
: { kind: "helper", declared: nested };
|
|
294
|
+
}
|
|
295
|
+
if (imported !== undefined && WAIT_MODULES.has(imported.module)) {
|
|
296
|
+
const wait = WAITS[imported.name];
|
|
297
|
+
return wait === undefined ? { kind: "none" } : { kind: "wait", wait };
|
|
298
|
+
}
|
|
299
|
+
if (imported !== undefined && AGENT_MODULES.has(imported.module)) {
|
|
300
|
+
return imported.name === "agentTask" ? { kind: "agent" } : { kind: "none" };
|
|
301
|
+
}
|
|
302
|
+
const declared = this.#function(module, name);
|
|
303
|
+
if (declared === undefined) {
|
|
304
|
+
return { kind: "none" };
|
|
305
|
+
}
|
|
306
|
+
return declared.directive === "use step"
|
|
307
|
+
? { kind: "step", declared }
|
|
308
|
+
: { kind: "helper", declared };
|
|
309
|
+
}
|
|
310
|
+
stepKey(declared) {
|
|
311
|
+
const key = this.#stepKey(declared.file, declared.name);
|
|
312
|
+
if (this.steps[key] === undefined) {
|
|
313
|
+
const owner = this.module(declared.file);
|
|
314
|
+
const description = leadingComment(this.#ts, declared.described, owner.source);
|
|
315
|
+
const calls = this.#calls(owner, declared.node);
|
|
316
|
+
const { line } = owner.source.getLineAndCharacterOfPosition(declared.described.getStart(owner.source));
|
|
317
|
+
this.steps[key] = {
|
|
318
|
+
name: declared.name,
|
|
319
|
+
...(description === undefined ? {} : { description }),
|
|
320
|
+
...(calls.length === 0 ? {} : { calls }),
|
|
321
|
+
source: { file: relative(this.#root, declared.file).split(sep).join("/"), line: line + 1 },
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
return key;
|
|
325
|
+
}
|
|
326
|
+
get agentSteps() {
|
|
327
|
+
return this.#agentSteps;
|
|
328
|
+
}
|
|
329
|
+
/** What a step's code calls: entity actions and records, and connection requests. */
|
|
330
|
+
#calls(module, root) {
|
|
331
|
+
const ts = this.#ts;
|
|
332
|
+
const calls = [];
|
|
333
|
+
const seen = new Set();
|
|
334
|
+
const followed = new Set([root]);
|
|
335
|
+
// Entity handles and connections the project declares, or the SDK's own entities; not the
|
|
336
|
+
// namespaces of other packages, whose `get` or `find` are not the ontology's.
|
|
337
|
+
const known = (owner, name) => {
|
|
338
|
+
const imported = owner.imports.get(name);
|
|
339
|
+
return imported === undefined
|
|
340
|
+
? owner.values.has(name)
|
|
341
|
+
: imported.module.startsWith(".") || AGENT_MODULES.has(imported.module);
|
|
342
|
+
};
|
|
343
|
+
const add = (call) => {
|
|
344
|
+
const key = `${call.kind}:${call.target}.${call.name}:${"path" in call ? call.path : ""}`;
|
|
345
|
+
if (!seen.has(key) && calls.length < MAX_CALLS) {
|
|
346
|
+
seen.add(key);
|
|
347
|
+
calls.push(call);
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
const visit = (owner, node) => {
|
|
351
|
+
if (ts.isCallExpression(node)) {
|
|
352
|
+
const callee = node.expression;
|
|
353
|
+
const first = node.arguments[0];
|
|
354
|
+
const path = first !== undefined && ts.isStringLiteralLike(first) && first.text.startsWith("/")
|
|
355
|
+
? first.text
|
|
356
|
+
: undefined;
|
|
357
|
+
if (ts.isPropertyAccessExpression(callee) &&
|
|
358
|
+
ts.isPropertyAccessExpression(callee.expression) &&
|
|
359
|
+
callee.expression.name.text === "actions" &&
|
|
360
|
+
ts.isIdentifier(callee.expression.expression) &&
|
|
361
|
+
known(owner, callee.expression.expression.text)) {
|
|
362
|
+
add({
|
|
363
|
+
kind: "action",
|
|
364
|
+
target: callee.expression.expression.text,
|
|
365
|
+
name: callee.name.text,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
else if (ts.isPropertyAccessExpression(callee) &&
|
|
369
|
+
ts.isIdentifier(callee.expression) &&
|
|
370
|
+
known(owner, callee.expression.text)) {
|
|
371
|
+
const target = callee.expression.text;
|
|
372
|
+
const name = callee.name.text;
|
|
373
|
+
if (path !== undefined && REQUEST_METHODS.has(name)) {
|
|
374
|
+
add({ kind: "request", target, name, path });
|
|
375
|
+
}
|
|
376
|
+
else if (RECORD_OPERATIONS.has(name)) {
|
|
377
|
+
add({ kind: "records", target, name });
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
else if (ts.isIdentifier(callee)) {
|
|
381
|
+
// A helper of the project the step calls: its calls are the step's.
|
|
382
|
+
const helper = this.#function(owner, callee.text);
|
|
383
|
+
if (helper !== undefined &&
|
|
384
|
+
helper.directive === undefined &&
|
|
385
|
+
!followed.has(helper.node)) {
|
|
386
|
+
followed.add(helper.node);
|
|
387
|
+
const helperModule = this.module(helper.file);
|
|
388
|
+
if (helperModule !== undefined && helper.node.body !== undefined) {
|
|
389
|
+
visit(helperModule, helper.node.body);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
ts.forEachChild(node, (child) => visit(owner, child));
|
|
395
|
+
};
|
|
396
|
+
if (root.body !== undefined) {
|
|
397
|
+
visit(module, root.body);
|
|
398
|
+
}
|
|
399
|
+
return calls;
|
|
400
|
+
}
|
|
401
|
+
/** Inlines a helper's body once per path; a recursive helper is not followed. */
|
|
402
|
+
enterHelper(declared) {
|
|
403
|
+
const key = `${declared.file}#${declared.name}`;
|
|
404
|
+
if (this.#helpers.includes(key) || this.#helpers.length >= MAX_DEPTH) {
|
|
405
|
+
this.complete = false;
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
408
|
+
this.#helpers.push(key);
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
leaveHelper() {
|
|
412
|
+
this.#helpers.pop();
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
class BodyReader {
|
|
416
|
+
#ts;
|
|
417
|
+
#reading;
|
|
418
|
+
/** Hooks and webhooks the body created by name, which it waits for where it awaits them. */
|
|
419
|
+
#handles = new Map();
|
|
420
|
+
#awaited = new Set();
|
|
421
|
+
constructor(ts, reading) {
|
|
422
|
+
this.#ts = ts;
|
|
423
|
+
this.#reading = reading;
|
|
424
|
+
}
|
|
425
|
+
#text(context, node) {
|
|
426
|
+
return collapse(node.getText(context.module.source));
|
|
427
|
+
}
|
|
428
|
+
/** The functions declared directly in a body, which its calls may resolve to. */
|
|
429
|
+
#scope(context, body) {
|
|
430
|
+
const ts = this.#ts;
|
|
431
|
+
const scope = new Map(context.scope);
|
|
432
|
+
const add = (name, node, described) => {
|
|
433
|
+
scope.set(name, {
|
|
434
|
+
file: context.module.file,
|
|
435
|
+
name,
|
|
436
|
+
node,
|
|
437
|
+
described,
|
|
438
|
+
directive: directiveOf(ts, node),
|
|
439
|
+
});
|
|
440
|
+
};
|
|
441
|
+
for (const statement of body.statements) {
|
|
442
|
+
if (ts.isFunctionDeclaration(statement) && statement.name && statement.body) {
|
|
443
|
+
add(statement.name.text, statement, statement);
|
|
444
|
+
}
|
|
445
|
+
else if (ts.isVariableStatement(statement)) {
|
|
446
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
447
|
+
const initializer = declaration.initializer;
|
|
448
|
+
if (ts.isIdentifier(declaration.name) &&
|
|
449
|
+
initializer !== undefined &&
|
|
450
|
+
(ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer))) {
|
|
451
|
+
add(declaration.name.text, initializer, statement);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return scope;
|
|
457
|
+
}
|
|
458
|
+
/** A block of statements; `tail` when nothing of the workflow runs after it. */
|
|
459
|
+
block(context, statements, prefix, tail) {
|
|
460
|
+
const out = new Emitter(prefix);
|
|
461
|
+
this.statements(context, statements, out, tail);
|
|
462
|
+
return out.nodes;
|
|
463
|
+
}
|
|
464
|
+
statements(context, statements, out, tail) {
|
|
465
|
+
statements.forEach((statement, index) => {
|
|
466
|
+
out.withNote(leadingComment(this.#ts, statement, context.module.source));
|
|
467
|
+
this.statement(context, statement, out, tail && index === statements.length - 1);
|
|
468
|
+
out.withNote(undefined);
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
statement(context, statement, out, tail) {
|
|
472
|
+
const ts = this.#ts;
|
|
473
|
+
if (ts.isExpressionStatement(statement)) {
|
|
474
|
+
if (!ts.isStringLiteral(statement.expression)) {
|
|
475
|
+
this.expression(context, statement.expression, out);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
else if (ts.isVariableStatement(statement)) {
|
|
479
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
480
|
+
const initializer = declaration.initializer;
|
|
481
|
+
if (initializer === undefined ||
|
|
482
|
+
ts.isArrowFunction(initializer) ||
|
|
483
|
+
ts.isFunctionExpression(initializer)) {
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
const created = this.#created(context, initializer);
|
|
487
|
+
if (created !== undefined && ts.isIdentifier(declaration.name)) {
|
|
488
|
+
// `const hook = createHook()`: the workflow waits where it awaits `hook`, not here.
|
|
489
|
+
for (const argument of initializer.arguments) {
|
|
490
|
+
this.expression(context, argument, out);
|
|
491
|
+
}
|
|
492
|
+
this.#handles.set(declaration.name.text, created);
|
|
493
|
+
}
|
|
494
|
+
else {
|
|
495
|
+
this.expression(context, initializer, out);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
else if (ts.isReturnStatement(statement)) {
|
|
500
|
+
// An async function's returned promise is awaited by its caller, as `return await` is.
|
|
501
|
+
if (statement.expression !== undefined) {
|
|
502
|
+
this.expression(context, statement.expression, out, true);
|
|
503
|
+
}
|
|
504
|
+
if (context.workflow && !tail) {
|
|
505
|
+
out.push({ id: out.id(), kind: "exit", exit: "return" });
|
|
506
|
+
}
|
|
507
|
+
else if (!tail) {
|
|
508
|
+
// An early return of a helper or callback skips what follows it there, which the
|
|
509
|
+
// workflow's map cannot show as its own exit: the reading says it is not complete.
|
|
510
|
+
this.#reading.complete = false;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
else if (ts.isThrowStatement(statement)) {
|
|
514
|
+
// A throw fails the run unless a `catch` around it takes it, wherever the code throws.
|
|
515
|
+
this.expression(context, statement.expression, out);
|
|
516
|
+
out.push({ id: out.id(), kind: "exit", exit: "throw" });
|
|
517
|
+
}
|
|
518
|
+
else if (ts.isBlock(statement)) {
|
|
519
|
+
this.statements(context, statement.statements, out, tail);
|
|
520
|
+
}
|
|
521
|
+
else if (ts.isLabeledStatement(statement)) {
|
|
522
|
+
this.statement(context, statement.statement, out, tail);
|
|
523
|
+
}
|
|
524
|
+
else if (ts.isIfStatement(statement)) {
|
|
525
|
+
this.expression(context, statement.expression, out);
|
|
526
|
+
const id = out.id();
|
|
527
|
+
const paths = [
|
|
528
|
+
{ when: "true", body: this.#nested(context, statement.thenStatement, `${id}.0.`, tail) },
|
|
529
|
+
{
|
|
530
|
+
when: "false",
|
|
531
|
+
body: statement.elseStatement === undefined
|
|
532
|
+
? []
|
|
533
|
+
: this.#nested(context, statement.elseStatement, `${id}.1.`, tail),
|
|
534
|
+
},
|
|
535
|
+
];
|
|
536
|
+
if (paths.some((path) => path.body.length > 0)) {
|
|
537
|
+
out.push({
|
|
538
|
+
id,
|
|
539
|
+
kind: "branch",
|
|
540
|
+
condition: this.#text(context, statement.expression),
|
|
541
|
+
paths,
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
else if (ts.isSwitchStatement(statement)) {
|
|
546
|
+
this.expression(context, statement.expression, out);
|
|
547
|
+
const id = out.id();
|
|
548
|
+
const clauses = statement.caseBlock.clauses;
|
|
549
|
+
const paths = [];
|
|
550
|
+
let labels = [];
|
|
551
|
+
clauses.forEach((clause, index) => {
|
|
552
|
+
const final = index === clauses.length - 1;
|
|
553
|
+
labels.push(ts.isCaseClause(clause) ? this.#text(context, clause.expression) : "default");
|
|
554
|
+
// `case "a": case "b": …`: an empty clause runs the next one's statements.
|
|
555
|
+
if (clause.statements.length === 0 && !final) {
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
const statements = [...clause.statements];
|
|
559
|
+
const last = statements.at(-1);
|
|
560
|
+
if (last !== undefined && ts.isBreakStatement(last) && last.label === undefined) {
|
|
561
|
+
statements.pop();
|
|
562
|
+
}
|
|
563
|
+
else if (!final &&
|
|
564
|
+
!(last !== undefined && (ts.isReturnStatement(last) || ts.isThrowStatement(last)))) {
|
|
565
|
+
// Statements that fall through into the next clause: a path the map cannot draw.
|
|
566
|
+
this.#reading.complete = false;
|
|
567
|
+
}
|
|
568
|
+
paths.push({
|
|
569
|
+
when: labels.join(", "),
|
|
570
|
+
body: this.block(context, statements, `${id}.${paths.length}.`, tail),
|
|
571
|
+
});
|
|
572
|
+
labels = [];
|
|
573
|
+
});
|
|
574
|
+
if (!clauses.some((clause) => ts.isDefaultClause(clause))) {
|
|
575
|
+
paths.push({ when: "default", body: [] });
|
|
576
|
+
}
|
|
577
|
+
if (paths.some((path) => path.body.length > 0)) {
|
|
578
|
+
out.push({
|
|
579
|
+
id,
|
|
580
|
+
kind: "branch",
|
|
581
|
+
condition: this.#text(context, statement.expression),
|
|
582
|
+
paths,
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
else if (ts.isForOfStatement(statement) || ts.isForInStatement(statement)) {
|
|
587
|
+
this.expression(context, statement.expression, out);
|
|
588
|
+
const id = out.id();
|
|
589
|
+
const inner = new Emitter(`${id}.`);
|
|
590
|
+
// `for await (const delivery of webhook)`: each pass waits for the next call.
|
|
591
|
+
const handle = ts.isForOfStatement(statement) &&
|
|
592
|
+
statement.awaitModifier !== undefined &&
|
|
593
|
+
ts.isIdentifier(statement.expression)
|
|
594
|
+
? this.#handle(statement.expression.text)
|
|
595
|
+
: undefined;
|
|
596
|
+
if (handle !== undefined) {
|
|
597
|
+
inner.push({ id: inner.id(), kind: "wait", wait: handle });
|
|
598
|
+
}
|
|
599
|
+
this.statement(context, statement.statement, inner, false);
|
|
600
|
+
const body = inner.nodes;
|
|
601
|
+
const word = ts.isForOfStatement(statement) ? "of" : "in";
|
|
602
|
+
const declared = ts.isVariableDeclarationList(statement.initializer)
|
|
603
|
+
? statement.initializer.declarations
|
|
604
|
+
.map((item) => item.name.getText(context.module.source))
|
|
605
|
+
.join(", ")
|
|
606
|
+
: statement.initializer.getText(context.module.source);
|
|
607
|
+
if (body.length > 0) {
|
|
608
|
+
out.push({
|
|
609
|
+
id,
|
|
610
|
+
kind: "loop",
|
|
611
|
+
loop: ts.isForOfStatement(statement) ? "forOf" : "forIn",
|
|
612
|
+
over: collapse(`${declared} ${word} ${statement.expression.getText(context.module.source)}`),
|
|
613
|
+
body,
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
else if (ts.isForStatement(statement)) {
|
|
618
|
+
if (statement.initializer !== undefined &&
|
|
619
|
+
!ts.isVariableDeclarationList(statement.initializer)) {
|
|
620
|
+
this.expression(context, statement.initializer, out);
|
|
621
|
+
}
|
|
622
|
+
else if (statement.initializer !== undefined) {
|
|
623
|
+
for (const declaration of statement.initializer.declarations) {
|
|
624
|
+
if (declaration.initializer !== undefined) {
|
|
625
|
+
this.expression(context, declaration.initializer, out);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
const id = out.id();
|
|
630
|
+
const inner = new Emitter(`${id}.`);
|
|
631
|
+
if (statement.condition !== undefined) {
|
|
632
|
+
this.expression(context, statement.condition, inner);
|
|
633
|
+
}
|
|
634
|
+
this.statement(context, statement.statement, inner, false);
|
|
635
|
+
if (statement.incrementor !== undefined) {
|
|
636
|
+
this.expression(context, statement.incrementor, inner);
|
|
637
|
+
}
|
|
638
|
+
if (inner.nodes.length > 0) {
|
|
639
|
+
out.push({
|
|
640
|
+
id,
|
|
641
|
+
kind: "loop",
|
|
642
|
+
loop: "for",
|
|
643
|
+
over: statement.condition === undefined ? "" : this.#text(context, statement.condition),
|
|
644
|
+
body: inner.nodes,
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
else if (ts.isWhileStatement(statement) || ts.isDoStatement(statement)) {
|
|
649
|
+
const id = out.id();
|
|
650
|
+
const inner = new Emitter(`${id}.`);
|
|
651
|
+
if (ts.isWhileStatement(statement)) {
|
|
652
|
+
this.expression(context, statement.expression, inner);
|
|
653
|
+
}
|
|
654
|
+
this.statement(context, statement.statement, inner, false);
|
|
655
|
+
if (ts.isDoStatement(statement)) {
|
|
656
|
+
this.expression(context, statement.expression, inner);
|
|
657
|
+
}
|
|
658
|
+
if (inner.nodes.length > 0) {
|
|
659
|
+
out.push({
|
|
660
|
+
id,
|
|
661
|
+
kind: "loop",
|
|
662
|
+
loop: ts.isWhileStatement(statement) ? "while" : "doWhile",
|
|
663
|
+
over: this.#text(context, statement.expression),
|
|
664
|
+
body: inner.nodes,
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
else if (ts.isTryStatement(statement)) {
|
|
669
|
+
const id = out.id();
|
|
670
|
+
const body = this.block(context, statement.tryBlock.statements, `${id}.0.`, tail && statement.finallyBlock === undefined);
|
|
671
|
+
const caught = statement.catchClause === undefined
|
|
672
|
+
? undefined
|
|
673
|
+
: this.block(context, statement.catchClause.block.statements, `${id}.1.`, tail);
|
|
674
|
+
const final = statement.finallyBlock === undefined
|
|
675
|
+
? undefined
|
|
676
|
+
: this.block(context, statement.finallyBlock.statements, `${id}.2.`, tail);
|
|
677
|
+
if (body.length > 0 || (caught?.length ?? 0) > 0 || (final?.length ?? 0) > 0) {
|
|
678
|
+
out.push({
|
|
679
|
+
id,
|
|
680
|
+
kind: "try",
|
|
681
|
+
body,
|
|
682
|
+
...(caught === undefined ? {} : { catch: caught }),
|
|
683
|
+
...(final === undefined ? {} : { finally: final }),
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
else if (ts.isBreakStatement(statement) || ts.isContinueStatement(statement)) {
|
|
688
|
+
// A `break` or `continue` skips what follows it in the loop, which the map cannot draw;
|
|
689
|
+
// a `break` that ends a `switch` clause is read with its clause.
|
|
690
|
+
this.#reading.complete = false;
|
|
691
|
+
}
|
|
692
|
+
// Declarations of functions, classes and types and empty statements run nothing of their own.
|
|
693
|
+
}
|
|
694
|
+
/** The kind of handle an initializer creates: `createHook()` or `createWebhook()`. */
|
|
695
|
+
#created(context, initializer) {
|
|
696
|
+
const ts = this.#ts;
|
|
697
|
+
if (!ts.isCallExpression(initializer)) {
|
|
698
|
+
return undefined;
|
|
699
|
+
}
|
|
700
|
+
const resolved = this.#reading.resolve(context.module, initializer.expression, context.scope);
|
|
701
|
+
return resolved.kind === "wait" && resolved.wait !== "sleep" ? resolved.wait : undefined;
|
|
702
|
+
}
|
|
703
|
+
/** A created handle the code awaits here; `undefined` for any other name. */
|
|
704
|
+
#handle(name) {
|
|
705
|
+
const handle = this.#handles.get(name);
|
|
706
|
+
if (handle !== undefined) {
|
|
707
|
+
this.#awaited.add(name);
|
|
708
|
+
}
|
|
709
|
+
return handle;
|
|
710
|
+
}
|
|
711
|
+
#nested(context, statement, prefix, tail) {
|
|
712
|
+
const out = new Emitter(prefix);
|
|
713
|
+
this.statement(context, statement, out, tail);
|
|
714
|
+
return out.nodes;
|
|
715
|
+
}
|
|
716
|
+
/** A function given as a value: its body's nodes, read where the code runs it. */
|
|
717
|
+
#callback(context, node, prefix) {
|
|
718
|
+
const ts = this.#ts;
|
|
719
|
+
const out = new Emitter(prefix);
|
|
720
|
+
if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
|
|
721
|
+
if (ts.isBlock(node.body)) {
|
|
722
|
+
const inner = { ...context, scope: this.#scope(context, node.body), workflow: false };
|
|
723
|
+
this.statements(inner, node.body.statements, out, true);
|
|
724
|
+
}
|
|
725
|
+
else {
|
|
726
|
+
this.expression({ ...context, workflow: false }, node.body, out);
|
|
727
|
+
}
|
|
728
|
+
return out.nodes;
|
|
729
|
+
}
|
|
730
|
+
if (ts.isIdentifier(node)) {
|
|
731
|
+
const resolved = this.#reading.resolve(context.module, node, context.scope);
|
|
732
|
+
if (resolved.kind === "step") {
|
|
733
|
+
out.push({ id: out.id(), kind: "step", step: this.#reading.stepKey(resolved.declared) });
|
|
734
|
+
}
|
|
735
|
+
else if (resolved.kind === "helper") {
|
|
736
|
+
// A helper of the project given by name, as in `items.forEach(remindOne)`.
|
|
737
|
+
this.#helper(resolved.declared, out, false);
|
|
738
|
+
}
|
|
739
|
+
else if (context.module.imports.has(node.text)) {
|
|
740
|
+
// A function of another package, as in `items.map(execute)`: what it runs is unread.
|
|
741
|
+
this.#reading.complete = false;
|
|
742
|
+
out.push({ id: out.id(), kind: "unknown", label: node.text });
|
|
743
|
+
}
|
|
744
|
+
// A global such as `String` in `items.map(String)` runs nothing the map shows.
|
|
745
|
+
return out.nodes;
|
|
746
|
+
}
|
|
747
|
+
return undefined;
|
|
748
|
+
}
|
|
749
|
+
/** Inlines a helper of the project: its nodes join the block, numbered under the call. */
|
|
750
|
+
#helper(declared, out, awaited) {
|
|
751
|
+
const ts = this.#ts;
|
|
752
|
+
const owner = this.#reading.module(declared.file);
|
|
753
|
+
const body = declared.node.body;
|
|
754
|
+
if (owner === undefined || body === undefined || !this.#reading.enterHelper(declared)) {
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
const nested = new Emitter(`${out.id()}.`);
|
|
758
|
+
try {
|
|
759
|
+
const inner = {
|
|
760
|
+
module: owner,
|
|
761
|
+
scope: ts.isBlock(body)
|
|
762
|
+
? this.#scope({ module: owner, scope: new Map(), workflow: false }, body)
|
|
763
|
+
: new Map(),
|
|
764
|
+
workflow: false,
|
|
765
|
+
};
|
|
766
|
+
if (ts.isBlock(body)) {
|
|
767
|
+
this.statements(inner, body.statements, nested, true);
|
|
768
|
+
}
|
|
769
|
+
else {
|
|
770
|
+
this.expression(inner, body, nested, awaited);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
finally {
|
|
774
|
+
this.#reading.leaveHelper();
|
|
775
|
+
}
|
|
776
|
+
for (const node of nested.nodes) {
|
|
777
|
+
out.push(node);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
expression(context, expression, out, awaited = false) {
|
|
781
|
+
const ts = this.#ts;
|
|
782
|
+
if (ts.isAwaitExpression(expression)) {
|
|
783
|
+
this.expression(context, expression.expression, out, true);
|
|
784
|
+
}
|
|
785
|
+
else if (ts.isParenthesizedExpression(expression) ||
|
|
786
|
+
ts.isAsExpression(expression) ||
|
|
787
|
+
ts.isNonNullExpression(expression) ||
|
|
788
|
+
ts.isSatisfiesExpression(expression) ||
|
|
789
|
+
ts.isTypeAssertionExpression(expression)) {
|
|
790
|
+
this.expression(context, expression.expression, out, awaited);
|
|
791
|
+
}
|
|
792
|
+
else if (ts.isCallExpression(expression)) {
|
|
793
|
+
this.#call(context, expression, out, awaited);
|
|
794
|
+
}
|
|
795
|
+
else if (ts.isConditionalExpression(expression)) {
|
|
796
|
+
this.expression(context, expression.condition, out);
|
|
797
|
+
const id = out.id();
|
|
798
|
+
const whenTrue = new Emitter(`${id}.0.`);
|
|
799
|
+
const whenFalse = new Emitter(`${id}.1.`);
|
|
800
|
+
this.expression(context, expression.whenTrue, whenTrue, awaited);
|
|
801
|
+
this.expression(context, expression.whenFalse, whenFalse, awaited);
|
|
802
|
+
if (whenTrue.nodes.length > 0 || whenFalse.nodes.length > 0) {
|
|
803
|
+
out.push({
|
|
804
|
+
id,
|
|
805
|
+
kind: "branch",
|
|
806
|
+
condition: this.#text(context, expression.condition),
|
|
807
|
+
paths: [
|
|
808
|
+
{ when: "true", body: whenTrue.nodes },
|
|
809
|
+
{ when: "false", body: whenFalse.nodes },
|
|
810
|
+
],
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
else if (ts.isBinaryExpression(expression) &&
|
|
815
|
+
[
|
|
816
|
+
ts.SyntaxKind.AmpersandAmpersandToken,
|
|
817
|
+
ts.SyntaxKind.BarBarToken,
|
|
818
|
+
ts.SyntaxKind.QuestionQuestionToken,
|
|
819
|
+
].includes(expression.operatorToken.kind)) {
|
|
820
|
+
this.expression(context, expression.left, out);
|
|
821
|
+
const id = out.id();
|
|
822
|
+
const right = new Emitter(`${id}.0.`);
|
|
823
|
+
this.expression(context, expression.right, right, awaited);
|
|
824
|
+
if (right.nodes.length > 0) {
|
|
825
|
+
const operator = expression.operatorToken.kind;
|
|
826
|
+
const left = this.#text(context, expression.left);
|
|
827
|
+
// `a && b()` runs b when a holds, `a || b()` when it does not, `a ?? b()` when a is null.
|
|
828
|
+
const runs = operator === ts.SyntaxKind.BarBarToken ? "false" : "true";
|
|
829
|
+
out.push({
|
|
830
|
+
id,
|
|
831
|
+
kind: "branch",
|
|
832
|
+
condition: operator === ts.SyntaxKind.QuestionQuestionToken ? `${left} == null` : left,
|
|
833
|
+
paths: [
|
|
834
|
+
{ when: runs, body: right.nodes },
|
|
835
|
+
{ when: runs === "true" ? "false" : "true", body: [] },
|
|
836
|
+
],
|
|
837
|
+
});
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
else if (ts.isArrowFunction(expression) || ts.isFunctionExpression(expression)) {
|
|
841
|
+
// A function given as a value runs where it is called, which the reading cannot see; a
|
|
842
|
+
// step inside it is shown where the function is written, and the reading is not complete.
|
|
843
|
+
const nodes = this.#callback(context, expression, `${out.id()}.`);
|
|
844
|
+
if (nodes !== undefined && nodes.length > 0) {
|
|
845
|
+
this.#reading.complete = false;
|
|
846
|
+
for (const node of nodes) {
|
|
847
|
+
out.push(node);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
else if (ts.isIdentifier(expression)) {
|
|
852
|
+
const handle = awaited ? this.#handle(expression.text) : undefined;
|
|
853
|
+
if (handle !== undefined) {
|
|
854
|
+
out.push({ id: out.id(), kind: "wait", wait: handle });
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
857
|
+
const resolved = this.#reading.resolve(context.module, expression, context.scope);
|
|
858
|
+
if (resolved.kind === "step") {
|
|
859
|
+
// A step passed as a value runs where the code calls it.
|
|
860
|
+
this.#reading.complete = false;
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
else if (ts.isPropertyAccessExpression(expression)) {
|
|
864
|
+
// `page.leads`: the name after the dot is a property, never a value of its own.
|
|
865
|
+
this.expression(context, expression.expression, out);
|
|
866
|
+
}
|
|
867
|
+
else {
|
|
868
|
+
this.#evaluated(context, expression, out);
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* The values a node evaluates, in order, through what is not an expression itself: object
|
|
873
|
+
* members (`{ result: await send() }`), template spans, spreads and computed names. Methods,
|
|
874
|
+
* accessors and types written inside it run nothing here.
|
|
875
|
+
*/
|
|
876
|
+
#evaluated(context, node, out) {
|
|
877
|
+
const ts = this.#ts;
|
|
878
|
+
ts.forEachChild(node, (child) => {
|
|
879
|
+
if (ts.isPropertyAssignment(child)) {
|
|
880
|
+
if (ts.isComputedPropertyName(child.name)) {
|
|
881
|
+
this.expression(context, child.name.expression, out);
|
|
882
|
+
}
|
|
883
|
+
this.expression(context, child.initializer, out);
|
|
884
|
+
}
|
|
885
|
+
else if (ts.isShorthandPropertyAssignment(child)) {
|
|
886
|
+
// `{ send }` hands the value on, as an identifier written alone does.
|
|
887
|
+
this.expression(context, child.name, out);
|
|
888
|
+
}
|
|
889
|
+
else if (ts.isExpression(child)) {
|
|
890
|
+
this.expression(context, child, out);
|
|
891
|
+
}
|
|
892
|
+
else if (!ts.isTypeNode(child) && !ts.isFunctionLike(child)) {
|
|
893
|
+
this.#evaluated(context, child, out);
|
|
894
|
+
}
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
#call(context, call, out, awaited) {
|
|
898
|
+
const ts = this.#ts;
|
|
899
|
+
const callee = call.expression;
|
|
900
|
+
// Promise.all([…]), Promise.all(items.map(…)): branches at once.
|
|
901
|
+
if (ts.isPropertyAccessExpression(callee) &&
|
|
902
|
+
ts.isIdentifier(callee.expression) &&
|
|
903
|
+
callee.expression.text === "Promise" &&
|
|
904
|
+
COMBINATORS.has(callee.name.text)) {
|
|
905
|
+
const method = callee.name.text;
|
|
906
|
+
const argument = call.arguments[0];
|
|
907
|
+
const id = out.id();
|
|
908
|
+
if (argument !== undefined && ts.isArrayLiteralExpression(argument)) {
|
|
909
|
+
const branches = argument.elements.map((element, index) => {
|
|
910
|
+
const branch = new Emitter(`${id}.${index}.`);
|
|
911
|
+
this.expression(context, ts.isSpreadElement(element) ? element.expression : element, branch, true);
|
|
912
|
+
return branch.nodes;
|
|
913
|
+
});
|
|
914
|
+
if (branches.some((branch) => branch.length > 0)) {
|
|
915
|
+
out.push({ id, kind: "parallel", method, branches });
|
|
916
|
+
}
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
if (argument !== undefined &&
|
|
920
|
+
ts.isCallExpression(argument) &&
|
|
921
|
+
ts.isPropertyAccessExpression(argument.expression) &&
|
|
922
|
+
EACH_METHODS.has(argument.expression.name.text) &&
|
|
923
|
+
argument.arguments[0] !== undefined) {
|
|
924
|
+
this.expression(context, argument.expression.expression, out);
|
|
925
|
+
const body = this.#callback(context, argument.arguments[0], `${id}.0.`);
|
|
926
|
+
if (body === undefined) {
|
|
927
|
+
this.#reading.complete = false;
|
|
928
|
+
}
|
|
929
|
+
else if (body.length > 0) {
|
|
930
|
+
out.push({
|
|
931
|
+
id,
|
|
932
|
+
kind: "parallel",
|
|
933
|
+
method,
|
|
934
|
+
each: this.#text(context, argument.expression.expression),
|
|
935
|
+
branches: [body],
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
return;
|
|
939
|
+
}
|
|
940
|
+
// Promises made earlier: their steps were read where the code made them.
|
|
941
|
+
if (argument !== undefined) {
|
|
942
|
+
this.expression(context, argument, out);
|
|
943
|
+
}
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
// items.map(…), items.forEach(…): the callback once per element.
|
|
947
|
+
if (ts.isPropertyAccessExpression(callee) &&
|
|
948
|
+
EACH_METHODS.has(callee.name.text) &&
|
|
949
|
+
call.arguments[0] !== undefined &&
|
|
950
|
+
(ts.isArrowFunction(call.arguments[0]) ||
|
|
951
|
+
ts.isFunctionExpression(call.arguments[0]) ||
|
|
952
|
+
ts.isIdentifier(call.arguments[0]))) {
|
|
953
|
+
this.expression(context, callee.expression, out);
|
|
954
|
+
const id = out.id();
|
|
955
|
+
const body = this.#callback(context, call.arguments[0], `${id}.`);
|
|
956
|
+
if (body !== undefined && body.length > 0) {
|
|
957
|
+
out.push({
|
|
958
|
+
id,
|
|
959
|
+
kind: "loop",
|
|
960
|
+
loop: "each",
|
|
961
|
+
over: this.#text(context, callee.expression),
|
|
962
|
+
body,
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
if (!ts.isIdentifier(callee)) {
|
|
968
|
+
this.expression(context, callee, out);
|
|
969
|
+
}
|
|
970
|
+
for (const argument of call.arguments) {
|
|
971
|
+
this.expression(context, argument, out);
|
|
972
|
+
}
|
|
973
|
+
const resolved = this.#reading.resolve(context.module, callee, context.scope);
|
|
974
|
+
switch (resolved.kind) {
|
|
975
|
+
case "step":
|
|
976
|
+
out.push({ id: out.id(), kind: "step", step: this.#reading.stepKey(resolved.declared) });
|
|
977
|
+
return;
|
|
978
|
+
case "wait": {
|
|
979
|
+
if (resolved.wait !== "sleep" && !awaited) {
|
|
980
|
+
// A hook or webhook made where the reading cannot follow it to where it is awaited.
|
|
981
|
+
this.#reading.complete = false;
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
const first = call.arguments[0];
|
|
985
|
+
const detail = resolved.wait !== "sleep" || first === undefined
|
|
986
|
+
? undefined
|
|
987
|
+
: ts.isStringLiteralLike(first)
|
|
988
|
+
? first.text
|
|
989
|
+
: this.#text(context, first);
|
|
990
|
+
out.push({
|
|
991
|
+
id: out.id(),
|
|
992
|
+
kind: "wait",
|
|
993
|
+
wait: resolved.wait,
|
|
994
|
+
...(detail === undefined ? {} : { detail }),
|
|
995
|
+
});
|
|
996
|
+
return;
|
|
997
|
+
}
|
|
998
|
+
case "agent":
|
|
999
|
+
out.push({ id: out.id(), kind: "agent", steps: [...this.#reading.agentSteps] });
|
|
1000
|
+
return;
|
|
1001
|
+
case "helper":
|
|
1002
|
+
this.#helper(resolved.declared, out, awaited);
|
|
1003
|
+
return;
|
|
1004
|
+
case "none":
|
|
1005
|
+
if (awaited) {
|
|
1006
|
+
this.#reading.complete = false;
|
|
1007
|
+
out.push({ id: out.id(), kind: "unknown", label: this.#text(context, callee) });
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
workflow(module, declared) {
|
|
1012
|
+
const ts = this.#ts;
|
|
1013
|
+
const body = declared.node.body;
|
|
1014
|
+
if (body === undefined || !ts.isBlock(body)) {
|
|
1015
|
+
return [];
|
|
1016
|
+
}
|
|
1017
|
+
const context = { module, scope: new Map(), workflow: true };
|
|
1018
|
+
const nodes = this.block({ ...context, scope: this.#scope(context, body) }, body.statements, "", true);
|
|
1019
|
+
// A hook the body made but never awaits by name is waited for somewhere the reading cannot see.
|
|
1020
|
+
if ([...this.#handles.keys()].some((name) => !this.#awaited.has(name))) {
|
|
1021
|
+
this.#reading.complete = false;
|
|
1022
|
+
}
|
|
1023
|
+
return nodes;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* The structure of one workflow function, `name` in `file`. `stepKey` names a step function the
|
|
1028
|
+
* way the runs record it; `agentSteps` are the step keys of `agentTask`.
|
|
1029
|
+
*/
|
|
1030
|
+
export function readWorkflowStructure(input) {
|
|
1031
|
+
const reading = new Reading({
|
|
1032
|
+
ts: input.ts,
|
|
1033
|
+
root: input.root,
|
|
1034
|
+
read: input.read ?? readSource,
|
|
1035
|
+
stepKey: input.stepKey,
|
|
1036
|
+
agentSteps: input.agentSteps ?? [],
|
|
1037
|
+
});
|
|
1038
|
+
const source = reading.module(input.file);
|
|
1039
|
+
const declared = source?.functions.get(input.name);
|
|
1040
|
+
if (source === undefined || declared === undefined) {
|
|
1041
|
+
return { version: WORKFLOW_STRUCTURE_VERSION, body: [], steps: {}, complete: false };
|
|
1042
|
+
}
|
|
1043
|
+
const body = new BodyReader(input.ts, reading).workflow(source, declared);
|
|
1044
|
+
return {
|
|
1045
|
+
version: WORKFLOW_STRUCTURE_VERSION,
|
|
1046
|
+
body,
|
|
1047
|
+
steps: reading.steps,
|
|
1048
|
+
complete: reading.complete,
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
/**
|
|
1052
|
+
* The structures of every workflow in a build's manifest, by workflow id, read from the sources
|
|
1053
|
+
* under `root`. The step keys come from the manifest; a step it does not list is keyed as the SDK
|
|
1054
|
+
* names a project's own steps. A workflow whose reading throws is left out.
|
|
1055
|
+
*/
|
|
1056
|
+
export function readWorkflowStructures(input) {
|
|
1057
|
+
const stepIds = new Map();
|
|
1058
|
+
const agentSteps = [];
|
|
1059
|
+
for (const [file, steps] of Object.entries(input.manifest.steps ?? {})) {
|
|
1060
|
+
const absolute = resolve(input.root, file);
|
|
1061
|
+
for (const [name, step] of Object.entries(steps)) {
|
|
1062
|
+
stepIds.set(`${absolute}#${name}`, step.stepId);
|
|
1063
|
+
if (/(^|[\\/])agent-task\.[cm]?[jt]s$/.test(file)) {
|
|
1064
|
+
agentSteps.push(step.stepId);
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
const stepKey = (file, name) => stepIds.get(`${file}#${name}`) ??
|
|
1069
|
+
`step//./${relative(input.root, file)
|
|
1070
|
+
.split(sep)
|
|
1071
|
+
.join("/")
|
|
1072
|
+
.replace(/\.[cm]?[jt]sx?$/, "")}//${name}`;
|
|
1073
|
+
const structures = {};
|
|
1074
|
+
for (const [file, workflows] of Object.entries(input.manifest.workflows ?? {})) {
|
|
1075
|
+
if (input.skip?.(file) === true) {
|
|
1076
|
+
continue;
|
|
1077
|
+
}
|
|
1078
|
+
for (const [name, workflow] of Object.entries(workflows)) {
|
|
1079
|
+
try {
|
|
1080
|
+
structures[workflow.workflowId] = readWorkflowStructure({
|
|
1081
|
+
ts: input.ts,
|
|
1082
|
+
root: input.root,
|
|
1083
|
+
file: resolve(input.root, file),
|
|
1084
|
+
name,
|
|
1085
|
+
stepKey,
|
|
1086
|
+
agentSteps,
|
|
1087
|
+
...(input.read === undefined ? {} : { read: input.read }),
|
|
1088
|
+
});
|
|
1089
|
+
}
|
|
1090
|
+
catch (error) {
|
|
1091
|
+
input.onError?.(workflow.workflowId, error);
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
return structures;
|
|
1096
|
+
}
|