@lovable.dev/workflows-sdk 0.0.0-dev.3 → 0.0.0-dev.31
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 -144
- package/dist/branch-metadata-CkZQpEi7.js +111 -0
- package/dist/context-escapes-Di8ThJie.js +78 -0
- package/dist/duplicate-ids-CRaMUWON.js +171 -0
- package/dist/durable-ids-LsygHSZC.js +17 -0
- package/dist/generate-Dk1_3N-g.js +6927 -0
- package/dist/graph-CBdqyIz5.js +649 -0
- package/dist/index.d.ts +824 -70
- package/dist/index.js +703 -2
- package/dist/nested-operations-BBGsI1HN.js +54 -0
- package/dist/operation-descriptions-CZAlt0vd.js +46 -0
- package/dist/step-metadata-Xksj4MjW.js +101 -0
- package/dist/vite.d.ts +17 -4
- package/dist/vite.js +294 -10
- package/package.json +4 -2
- package/dist/generate-DFS-ezZy.js +0 -115
- package/dist/graph-BgdiEUHq.js +0 -201
package/README.md
CHANGED
|
@@ -1,33 +1,23 @@
|
|
|
1
1
|
# @lovable.dev/workflows-sdk
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
Use `createWorkflow({ id, input, output }).handler(...)` to define a workflow
|
|
13
|
-
and `createMiddleware` to extend its typed context. Both builders come directly
|
|
14
|
-
from [`@tanstack/workflow-core`](https://github.com/TanStack/workflow).
|
|
15
|
-
Schema validation libraries implementing Standard Schema, such as Zod, provide
|
|
16
|
-
input and output inference.
|
|
17
|
-
|
|
18
|
-
Keep the app's workflow collection in `src/lib/workflows/index.ts`. Each workflow
|
|
19
|
-
lives in `src/lib/workflows/{workflow-name}/index.ts`, with its helpers alongside it:
|
|
20
|
-
|
|
21
|
-
```text
|
|
22
|
-
src/lib/workflows/
|
|
23
|
-
├── index.ts
|
|
24
|
-
└── greet/
|
|
25
|
-
├── index.ts
|
|
26
|
-
└── helpers.ts
|
|
3
|
+
Experimental SDK for authoring workflows in Lovable apps with
|
|
4
|
+
[`@tanstack/workflow-core`](https://github.com/TanStack/workflow). Releases stay
|
|
5
|
+
in the `0.0.0-dev` series and may change without compatibility guarantees.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @lovable.dev/workflows-sdk
|
|
27
11
|
```
|
|
28
12
|
|
|
29
|
-
|
|
30
|
-
|
|
13
|
+
## Define workflows
|
|
14
|
+
|
|
15
|
+
Use `createWorkflow({ id, version, input, output }).handler(...)` to define a workflow
|
|
16
|
+
and `createMiddleware` to extend its typed context. Standard Schema libraries
|
|
17
|
+
such as Zod provide input and output inference.
|
|
18
|
+
|
|
19
|
+
Keep definitions under `src/lib/workflows/{workflow-name}/` and default-export
|
|
20
|
+
one collection from `src/lib/workflows/index.ts`:
|
|
31
21
|
|
|
32
22
|
```ts
|
|
33
23
|
import { registerWorkflows } from "@lovable.dev/workflows-sdk";
|
|
@@ -36,126 +26,145 @@ import { greet } from "./greet";
|
|
|
36
26
|
export default registerWorkflows({ workflows: [greet] });
|
|
37
27
|
```
|
|
38
28
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
29
|
+
Workflow IDs contain 1–128 lowercase letters, digits, hyphens, or underscores,
|
|
30
|
+
starting with a letter or digit. They must be unique within the collection.
|
|
31
|
+
See the [authoring example](examples/src/lib/workflows/greet/index.ts) for
|
|
32
|
+
schemas, typed middleware, and a durable step.
|
|
33
|
+
|
|
34
|
+
## Workflow versions
|
|
35
|
+
|
|
36
|
+
Every workflow requires an explicit `version`, including retained definitions.
|
|
37
|
+
Start with `"v1"`; introduce a new label when changing behavior, schemas, or
|
|
38
|
+
relevant helpers. Keep existing version implementations unchanged while runs drain.
|
|
39
|
+
Registration rejects missing, empty, and invalid labels. Existing unversioned
|
|
40
|
+
runs must drain on their original SDK deployment before upgrading.
|
|
41
|
+
|
|
42
|
+
Keep the current definition in `workflows` and attach older definitions with
|
|
43
|
+
`.previousVersions([v1])` before `.handler(...)`. Each definition has the same
|
|
44
|
+
workflow ID and a distinct `version` passed to `createWorkflow`. Versions accept
|
|
45
|
+
1–128 letters, digits, dots, hyphens, or underscores, starting with a letter or
|
|
46
|
+
digit. The combined manifest filename must fit 255 characters. See the [versioned example](examples/src/lib/workflows/versioned/index.ts).
|
|
47
|
+
|
|
48
|
+
The build emits one manifest per version. The top-level definition has
|
|
49
|
+
`current: true`; retained definitions have `current: false` and no triggers.
|
|
50
|
+
Schedules reference the top-level definition and start its version.
|
|
51
|
+
|
|
52
|
+
Steps responses include `workflowVersion`. Persist it with the run and send it
|
|
53
|
+
on every later request, including an empty memo. A missing version on a
|
|
54
|
+
resume or an unavailable version fails before replay.
|
|
55
|
+
|
|
56
|
+
Keep old definitions and their relevant helpers unchanged until their runs and
|
|
57
|
+
pending execution attempts drain. Removing a definition makes its version
|
|
58
|
+
unavailable; existing runs never switch to the current version automatically.
|
|
59
|
+
|
|
60
|
+
## Durable operations
|
|
61
|
+
|
|
62
|
+
Every durable operation needs a static, user-facing description:
|
|
63
|
+
|
|
64
|
+
| Operation | Description field |
|
|
65
|
+
| --- | --- |
|
|
66
|
+
| `ctx.step` | Third argument: `meta.description` |
|
|
67
|
+
| `ctx.sleep`, `ctx.sleepUntil` | Second argument: `meta.description` |
|
|
68
|
+
| `ctx.waitForEvent` | Second argument: `meta.description` |
|
|
69
|
+
| `ctx.now`, `ctx.uuid` | First argument: `meta.description` |
|
|
70
|
+
| `ctx.approve` | First argument: `description` |
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
await ctx.step("load-customer", () => loadCustomer(ctx.input.customerId), {
|
|
74
|
+
meta: { description: "Load the customer record" },
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Step IDs must be unique within a workflow. Keep side effects inside step bodies;
|
|
79
|
+
handler code outside a step may run more than once. A step body is one atomic
|
|
80
|
+
execution unit and cannot contain another durable operation.
|
|
81
|
+
|
|
82
|
+
Keep durable control flow in the workflow handler. Do not pass, alias,
|
|
83
|
+
destructure, or return `ctx`, and do not detach methods such as `ctx.step`.
|
|
84
|
+
Pass extracted data such as `ctx.input.customerId` to helpers instead.
|
|
85
|
+
|
|
86
|
+
### Repeated steps
|
|
87
|
+
|
|
88
|
+
A step inside a loop or callback needs `meta.group` with a stable ID and
|
|
89
|
+
user-facing label. Each repeated step still needs its own dynamic ID:
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
ctx.step(`process-${item.id}`, () => processItem(item), {
|
|
93
|
+
meta: { description: "Process an item", group: { id: "process-items", label: "Process items" } },
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
For dynamic sequential work, await one step directly as the only statement in
|
|
98
|
+
a `for...of` loop. For dynamic parallel work, use a mapped `Promise.all()`.
|
|
99
|
+
Fixed `Promise.all([stepA, stepB])` branches and ordinary sequential steps do
|
|
100
|
+
not need a group.
|
|
45
101
|
|
|
46
|
-
|
|
47
|
-
|
|
102
|
+
### Conditional steps
|
|
103
|
+
|
|
104
|
+
Durable operations inside `if` and `else` branches include an outer-to-inner
|
|
105
|
+
`meta.branches` path. Both sides of one condition use the same static ID and
|
|
106
|
+
description:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
meta: {
|
|
110
|
+
description: "Publish the approved draft",
|
|
111
|
+
branches: [{ id: "editor-approved", description: "The editor approved the draft" }],
|
|
112
|
+
}
|
|
113
|
+
```
|
|
48
114
|
|
|
49
115
|
## Schedules
|
|
50
116
|
|
|
51
|
-
Declare
|
|
52
|
-
and include
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
### Step graph
|
|
103
|
-
|
|
104
|
-
Each manifest's `graph` contains `nodes` and directed `edges`. The SDK parses the
|
|
105
|
-
handler's function source without executing it. No additional registration is needed.
|
|
106
|
-
|
|
107
|
-
Graph inference reads the app's own `typescript`, an optional peer. Apps built
|
|
108
|
-
from a Lovable template already have it. Without it — or with a copy exposing no
|
|
109
|
-
stable JS API, as TypeScript 7 does before 7.1 — the build still succeeds and
|
|
110
|
-
every graph carries one `unknown` node and the diagnostic
|
|
111
|
-
`Install typescript to infer step graphs`.
|
|
112
|
-
Sequential awaited `ctx.step("name", callback)` calls produce order edges;
|
|
113
|
-
`await Promise.all([ctx.step(...), ctx.step(...)])` produces parallel branches
|
|
114
|
-
that join at the next step. Returned step promises work too. Keep step IDs literal.
|
|
115
|
-
|
|
116
|
-
| Field | Meaning |
|
|
117
|
+
Declare schedules with `defineSchedule({ id, workflow, cadence, timeZone,
|
|
118
|
+
input })` and include them in the collection's `triggers`. The workflow must be
|
|
119
|
+
part of the same collection, and schedule IDs must be unique. This API declares
|
|
120
|
+
schedules; Lovable execution is not available yet.
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
const morning = defineSchedule({
|
|
124
|
+
id: "morning-digest", workflow: digest, cadence: daily({ at: [9] }), timeZone: "Europe/Stockholm", input: {},
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
| Field | Default | Meaning |
|
|
129
|
+
| --- | --- | --- |
|
|
130
|
+
| `cadence` | — | `hourly()`, `daily({ at })`, or `weekly({ on, at })` |
|
|
131
|
+
| `timeZone` | — | Canonical IANA region name |
|
|
132
|
+
| `overlapPolicy` | `"skip"` | `skip`, `allow`, or `buffer-one` |
|
|
133
|
+
| `enabled` | `true` | `false` declares a paused schedule |
|
|
134
|
+
|
|
135
|
+
| Cadence | Example |
|
|
136
|
+
| --- | --- |
|
|
137
|
+
| Hourly | `hourly()` |
|
|
138
|
+
| Selected hours | `daily({ at: [0, 6, 12, 18] })` |
|
|
139
|
+
| Weekly | `weekly({ on: ["mon", "fri"], at: [0, 12] })` |
|
|
140
|
+
|
|
141
|
+
`daily` requires a non-empty `at` list. `weekly` requires non-empty `on` and
|
|
142
|
+
`at` lists and runs at every selected hour on every selected weekday. Hours are
|
|
143
|
+
whole numbers from 0 through 23; schedules do not accept minute or raw-cron
|
|
144
|
+
fields. `timeZone` is required and accepts `"UTC"` or a canonical IANA region
|
|
145
|
+
such as `"America/New_York"`. A scheduled hour is approximate: Lovable chooses
|
|
146
|
+
a stable offset within the corresponding scheduling window.
|
|
147
|
+
|
|
148
|
+
`overlapPolicy` controls what happens when a schedule becomes due while its
|
|
149
|
+
previous run is active. `skip` drops it, `allow` starts another run, and
|
|
150
|
+
`buffer-one` starts the earliest missed run after the active run finishes.
|
|
151
|
+
|
|
152
|
+
## Build integration
|
|
153
|
+
|
|
154
|
+
Add `workflowsPlugin()` before the TanStack Start plugin. Its `entry` defaults
|
|
155
|
+
to `src/lib/workflows/index.ts` and must default-export the registered
|
|
156
|
+
collection. See [the Vite example](examples/vite.config.ts).
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
plugins: [
|
|
160
|
+
workflowsPlugin(),
|
|
161
|
+
tanstackStart(),
|
|
162
|
+
];
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Imports
|
|
166
|
+
|
|
167
|
+
| Import | Provides |
|
|
117
168
|
| --- | --- |
|
|
118
|
-
|
|
|
119
|
-
|
|
|
120
|
-
| `nodes[].stepId` | Literal step name, present for known steps |
|
|
121
|
-
| `nodes[].label` | Step name or explanation of unsupported code |
|
|
122
|
-
| `edges[]` | `{ source, target, kind: "order" }`, referencing node IDs |
|
|
123
|
-
| `complete` | Whether the handler uses only supported syntax |
|
|
124
|
-
| `diagnostics` | Reasons the graph is partial |
|
|
125
|
-
|
|
126
|
-
For example, `read` followed by parallel `transform-a` and `transform-b`, then
|
|
127
|
-
`save`, produces `read → transform-a → save` and `read → transform-b → save`.
|
|
128
|
-
Repeated step names remain separate nodes. Node IDs are deterministic for the
|
|
129
|
-
same source, but may change when the handler is edited.
|
|
130
|
-
|
|
131
|
-
Graphs have `source: "inferred"` and `scope: "handler"`. They describe source
|
|
132
|
-
structure, not run history or data dependencies, and exclude middleware and
|
|
133
|
-
initialization. Branches, loops, helper calls, dynamic step IDs, nested steps,
|
|
134
|
-
and unawaited steps produce `unknown` nodes and `complete: false`; steps inside
|
|
135
|
-
unsupported code are not expanded. The UI should show these unknown nodes and
|
|
136
|
-
mark partial graphs. Transforms that rewrite the handler may also produce a
|
|
137
|
-
partial graph. A handler with no steps can have an empty graph.
|
|
138
|
-
|
|
139
|
-
## Next PRs
|
|
140
|
-
|
|
141
|
-
Connect the manifest and app code to Lovable deployment and runtime execution.
|
|
142
|
-
Define support for TanStack primitives, version routing, and schedule policies.
|
|
143
|
-
|
|
144
|
-
## Publishing dev builds
|
|
145
|
-
|
|
146
|
-
The [publishing workflow](../../.github/workflows/publish-workflows-sdk.yml)
|
|
147
|
-
publishes the version committed in `package.json` to npm's `latest` tag when SDK
|
|
148
|
-
changes land on `main`. Manage versions manually: `0.0.0-dev`, `0.0.0-dev.1`,
|
|
149
|
-
`0.0.0-dev.2`, and so on. The workflow rejects versions outside this series and
|
|
150
|
-
skips versions already published. Manual runs default to a dry run.
|
|
151
|
-
See [the setup guide](PUBLISHING.md) for the
|
|
152
|
-
one-time bootstrap and npm trusted-publisher configuration.
|
|
153
|
-
|
|
154
|
-
Install the newest build with `npm install @lovable.dev/workflows-sdk`, or pin
|
|
155
|
-
the exact version shown in the workflow run's summary. `latest` tracks every dev
|
|
156
|
-
publish while the SDK is pre-1.0; the older `dev` tag is frozen at `0.0.0-dev.2`.
|
|
157
|
-
|
|
158
|
-
The existing internal [`@lovable/workflow-sdk`](../../packages/workflow-sdk)
|
|
159
|
-
provides the engine contract. Use [`mcp-js`](../lovable-mcp-js) as the reference
|
|
160
|
-
for separating authoring, manifest extraction, and framework integration. The
|
|
161
|
-
workflow prototype's `ts/workflow-sdk` supplies adapter and manifest examples.
|
|
169
|
+
| `@lovable.dev/workflows-sdk` | Workflow authoring, registration, cadences, and schedules |
|
|
170
|
+
| `@lovable.dev/workflows-sdk/vite` | Lovable build integration |
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { loadTypeScript, t as workflowHandlerSource } from "./graph-CBdqyIz5.js";
|
|
2
|
+
//#region src/manifests/branch-metadata.ts
|
|
3
|
+
const optionsArgument = {
|
|
4
|
+
step: 2,
|
|
5
|
+
approve: 0,
|
|
6
|
+
sleep: 1,
|
|
7
|
+
sleepUntil: 1,
|
|
8
|
+
waitForEvent: 1,
|
|
9
|
+
now: 0,
|
|
10
|
+
uuid: 0
|
|
11
|
+
};
|
|
12
|
+
async function findBranchMetadataIssues(handler) {
|
|
13
|
+
const api = await loadTypeScript();
|
|
14
|
+
if (!api) return [];
|
|
15
|
+
const ts = api;
|
|
16
|
+
const handlerSource = workflowHandlerSource(handler);
|
|
17
|
+
if (!handlerSource) return [];
|
|
18
|
+
const declaration = ts.createSourceFile("handler.js", `const handler = ${handlerSource};`, ts.ScriptTarget.ESNext, true, ts.ScriptKind.JS).statements[0];
|
|
19
|
+
const fn = declaration && ts.isVariableStatement(declaration) ? declaration.declarationList.declarations[0]?.initializer : void 0;
|
|
20
|
+
if (!fn || !ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return [];
|
|
21
|
+
const parameter = fn.parameters[0]?.name;
|
|
22
|
+
if (!parameter || !ts.isIdentifier(parameter)) return [];
|
|
23
|
+
const context = parameter.text;
|
|
24
|
+
function property(node, name) {
|
|
25
|
+
if (!node || !ts.isObjectLiteralExpression(node)) return void 0;
|
|
26
|
+
for (const entry of node.properties) {
|
|
27
|
+
if (!ts.isPropertyAssignment(entry)) continue;
|
|
28
|
+
if ((ts.isIdentifier(entry.name) || ts.isStringLiteral(entry.name) ? entry.name.text : void 0) === name) return entry.initializer;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function literalString(node) {
|
|
32
|
+
return node && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) ? node.text : void 0;
|
|
33
|
+
}
|
|
34
|
+
function durableCall(node) {
|
|
35
|
+
if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression) || !ts.isIdentifier(node.expression.expression) || node.expression.expression.text !== context || !(node.expression.name.text in optionsArgument)) return;
|
|
36
|
+
return {
|
|
37
|
+
call: node,
|
|
38
|
+
method: node.expression.name.text
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function branchPath(call, method) {
|
|
42
|
+
const options = call.arguments[optionsArgument[method]];
|
|
43
|
+
const branches = property(property(options, "meta"), "branches");
|
|
44
|
+
if (!branches || !ts.isArrayLiteralExpression(branches)) return void 0;
|
|
45
|
+
const path = [];
|
|
46
|
+
for (const element of branches.elements) {
|
|
47
|
+
if (!ts.isObjectLiteralExpression(element)) return void 0;
|
|
48
|
+
const id = literalString(property(element, "id"));
|
|
49
|
+
const description = literalString(property(element, "description"));
|
|
50
|
+
if (id === void 0 || description === void 0) return void 0;
|
|
51
|
+
path.push({
|
|
52
|
+
id,
|
|
53
|
+
description
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return path;
|
|
57
|
+
}
|
|
58
|
+
const issues = [];
|
|
59
|
+
const metadataByBranch = /* @__PURE__ */ new Map();
|
|
60
|
+
const branchById = /* @__PURE__ */ new Map();
|
|
61
|
+
function visit(node, branches) {
|
|
62
|
+
if (issues.length > 0) return;
|
|
63
|
+
if (ts.isIfStatement(node)) {
|
|
64
|
+
visit(node.expression, branches);
|
|
65
|
+
visit(node.thenStatement, [...branches, node]);
|
|
66
|
+
if (node.elseStatement) visit(node.elseStatement, [...branches, node]);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const durable = durableCall(node);
|
|
70
|
+
if (durable) {
|
|
71
|
+
const path = branchPath(durable.call, durable.method);
|
|
72
|
+
const stepId = durable.method === "step" ? literalString(durable.call.arguments[0]) : void 0;
|
|
73
|
+
if (branches.length === 0 && path !== void 0 || branches.length > 0 && (path === void 0 || path.length !== branches.length)) {
|
|
74
|
+
issues.push({
|
|
75
|
+
kind: "path",
|
|
76
|
+
method: durable.method,
|
|
77
|
+
...stepId === void 0 ? {} : { stepId }
|
|
78
|
+
});
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
for (let index = 0; index < branches.length; index++) {
|
|
82
|
+
const branch = branches[index];
|
|
83
|
+
const entry = path?.[index];
|
|
84
|
+
if (!branch || !entry) continue;
|
|
85
|
+
const existing = metadataByBranch.get(branch);
|
|
86
|
+
if (existing && (existing.id !== entry.id || existing.description !== entry.description)) {
|
|
87
|
+
issues.push({
|
|
88
|
+
kind: "inconsistent",
|
|
89
|
+
branchId: existing.id
|
|
90
|
+
});
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const existingNode = branchById.get(entry.id);
|
|
94
|
+
if (existingNode && existingNode !== branch) {
|
|
95
|
+
issues.push({
|
|
96
|
+
kind: "duplicate-id",
|
|
97
|
+
branchId: entry.id
|
|
98
|
+
});
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
metadataByBranch.set(branch, entry);
|
|
102
|
+
branchById.set(entry.id, branch);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
ts.forEachChild(node, (child) => visit(child, branches));
|
|
106
|
+
}
|
|
107
|
+
visit(fn, []);
|
|
108
|
+
return issues;
|
|
109
|
+
}
|
|
110
|
+
//#endregion
|
|
111
|
+
export { findBranchMetadataIssues };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { loadTypeScript, t as workflowHandlerSource } from "./graph-CBdqyIz5.js";
|
|
2
|
+
//#region src/manifests/context-escapes.ts
|
|
3
|
+
const durableMethods = /* @__PURE__ */ new Set([
|
|
4
|
+
"step",
|
|
5
|
+
"approve",
|
|
6
|
+
"waitForEvent",
|
|
7
|
+
"sleep",
|
|
8
|
+
"sleepUntil",
|
|
9
|
+
"now",
|
|
10
|
+
"uuid"
|
|
11
|
+
]);
|
|
12
|
+
async function findWorkflowContextEscapes(handler) {
|
|
13
|
+
const api = await loadTypeScript();
|
|
14
|
+
if (!api) return [];
|
|
15
|
+
const ts = api;
|
|
16
|
+
const handlerSource = workflowHandlerSource(handler);
|
|
17
|
+
if (!handlerSource) return [];
|
|
18
|
+
const declaration = ts.createSourceFile("handler.js", `const handler = ${handlerSource};`, ts.ScriptTarget.ESNext, true, ts.ScriptKind.JS).statements[0];
|
|
19
|
+
const fn = declaration && ts.isVariableStatement(declaration) ? declaration.declarationList.declarations[0]?.initializer : void 0;
|
|
20
|
+
if (!fn || !ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return [];
|
|
21
|
+
const parameter = fn.parameters[0]?.name;
|
|
22
|
+
if (!parameter) return [];
|
|
23
|
+
if (!ts.isIdentifier(parameter)) return [{ kind: "destructured" }];
|
|
24
|
+
const context = parameter.text;
|
|
25
|
+
function bindingContains(node, name) {
|
|
26
|
+
if (ts.isIdentifier(node)) return node.text === name;
|
|
27
|
+
return node.elements.some((element) => !ts.isOmittedExpression(element) && bindingContains(element.name, name));
|
|
28
|
+
}
|
|
29
|
+
function contextMethod(node) {
|
|
30
|
+
if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === context) return node.name.text;
|
|
31
|
+
if (ts.isElementAccessExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === context && node.argumentExpression && (ts.isStringLiteral(node.argumentExpression) || ts.isNoSubstitutionTemplateLiteral(node.argumentExpression))) return node.argumentExpression.text;
|
|
32
|
+
}
|
|
33
|
+
function directCallTarget(node) {
|
|
34
|
+
let target = node;
|
|
35
|
+
while (ts.isParenthesizedExpression(target.parent)) target = target.parent;
|
|
36
|
+
return ts.isCallExpression(target.parent) && target.parent.expression === target;
|
|
37
|
+
}
|
|
38
|
+
function identifierIsValue(node) {
|
|
39
|
+
const parent = node.parent;
|
|
40
|
+
if (parent.name === node && !ts.isShorthandPropertyAssignment(parent)) return false;
|
|
41
|
+
return parent.label !== node;
|
|
42
|
+
}
|
|
43
|
+
const escapes = [];
|
|
44
|
+
const visit = (node, shadowed) => {
|
|
45
|
+
if (escapes.length > 0) return;
|
|
46
|
+
if (node !== fn && ts.isFunctionLike(node)) {
|
|
47
|
+
const hidesContext = node.parameters.some((item) => bindingContains(item.name, context));
|
|
48
|
+
ts.forEachChild(node, (child) => visit(child, shadowed || hidesContext));
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (!shadowed) {
|
|
52
|
+
const method = contextMethod(node);
|
|
53
|
+
if (method && durableMethods.has(method) && !directCallTarget(node)) {
|
|
54
|
+
escapes.push({
|
|
55
|
+
kind: "method",
|
|
56
|
+
context,
|
|
57
|
+
method
|
|
58
|
+
});
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (ts.isIdentifier(node) && node.text === context && identifierIsValue(node)) {
|
|
62
|
+
const parent = node.parent;
|
|
63
|
+
if (!(ts.isPropertyAccessExpression(parent) && parent.expression === node || ts.isElementAccessExpression(parent) && parent.expression === node && parent.argumentExpression && (ts.isStringLiteral(parent.argumentExpression) || ts.isNoSubstitutionTemplateLiteral(parent.argumentExpression)))) {
|
|
64
|
+
escapes.push({
|
|
65
|
+
kind: "context",
|
|
66
|
+
context
|
|
67
|
+
});
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
ts.forEachChild(node, (child) => visit(child, shadowed));
|
|
73
|
+
};
|
|
74
|
+
visit(fn, false);
|
|
75
|
+
return escapes;
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
export { findWorkflowContextEscapes };
|