@molecule/api-ai-workflow-engine 1.0.0 → 1.0.1
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 +336 -0
- package/package.json +15 -9
package/README.md
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-08-04T01:47:41.708Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/api-ai-workflow-engine
|
|
10
|
+
|
|
11
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
+
|
|
15
|
+
`@molecule/api-ai-workflow-engine` — trigger / condition / action
|
|
16
|
+
workflow runner with pluggable handler registry + an optional AI
|
|
17
|
+
`ai_prompt` step (send a templated prompt to the bonded AI provider
|
|
18
|
+
mid-run).
|
|
19
|
+
|
|
20
|
+
Extracted from ai-workflow-automator flagship. Use it like an
|
|
21
|
+
embeddable Zapier engine inside your app.
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { createWorkflowEngine } from '@molecule/api-ai-workflow-engine'
|
|
27
|
+
|
|
28
|
+
const engine = createWorkflowEngine({
|
|
29
|
+
triggers: {
|
|
30
|
+
'webhook.received': async (ctx) => ctx.payload,
|
|
31
|
+
},
|
|
32
|
+
actions: {
|
|
33
|
+
'slack.message': async ({ channel, text }) => slackClient.chat.postMessage({ channel, text }),
|
|
34
|
+
},
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const run = await engine.execute({
|
|
38
|
+
trigger: 'webhook.received',
|
|
39
|
+
triggerInput: { payload: req.body },
|
|
40
|
+
steps: [
|
|
41
|
+
{ type: 'condition', expression: '$.event === "purchase"' },
|
|
42
|
+
// Native HTTP step — routed through the swappable `@molecule/api-http` core:
|
|
43
|
+
{
|
|
44
|
+
type: 'http',
|
|
45
|
+
method: 'POST',
|
|
46
|
+
url: 'https://hooks.example.com/${$.id}',
|
|
47
|
+
body: { amount: '${$.amount}' },
|
|
48
|
+
output: 'webhook',
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: 'action',
|
|
52
|
+
action: 'slack.message',
|
|
53
|
+
params: { channel: '#sales', text: 'New sale: ${$.amount}' },
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
})
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Type
|
|
60
|
+
|
|
61
|
+
`utility`
|
|
62
|
+
|
|
63
|
+
## Installation
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npm install @molecule/api-ai-workflow-engine @molecule/api-ai @molecule/api-bonds-default-express @molecule/api-database @molecule/api-http @molecule/api-i18n @molecule/api-middleware-validation
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## API
|
|
70
|
+
|
|
71
|
+
### Interfaces
|
|
72
|
+
|
|
73
|
+
#### `ActionStep`
|
|
74
|
+
|
|
75
|
+
A step that invokes a registered action handler with resolved params.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
interface ActionStep {
|
|
79
|
+
type: 'action'
|
|
80
|
+
/** Action key registered with the engine. */
|
|
81
|
+
action: string
|
|
82
|
+
/** Param template — `${$.x.y}` placeholders are resolved against context. */
|
|
83
|
+
params?: Record<string, unknown>
|
|
84
|
+
/** Where to write the action's return value into the context. */
|
|
85
|
+
output?: string
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### `AIPromptStep`
|
|
90
|
+
|
|
91
|
+
A step that sends a prompt to the bonded AI provider and writes the response into context.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
interface AIPromptStep {
|
|
95
|
+
type: 'ai_prompt'
|
|
96
|
+
prompt: string
|
|
97
|
+
/** Field on context to write the model's text response. */
|
|
98
|
+
output: string
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
#### `ConditionStep`
|
|
103
|
+
|
|
104
|
+
A step that evaluates a JS expression and short-circuits the run on false.
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
interface ConditionStep {
|
|
108
|
+
type: 'condition'
|
|
109
|
+
/** JS expression evaluated against `$` (the workflow context). */
|
|
110
|
+
expression: string
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### `DelayStep`
|
|
115
|
+
|
|
116
|
+
A step that pauses workflow execution for a fixed number of milliseconds.
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
interface DelayStep {
|
|
120
|
+
type: 'delay'
|
|
121
|
+
/** Milliseconds to sleep. */
|
|
122
|
+
ms: number
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
#### `EngineOptions`
|
|
127
|
+
|
|
128
|
+
Configuration passed to `createWorkflowEngine` — trigger and action handler maps.
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
interface EngineOptions {
|
|
132
|
+
triggers: Record<string, (ctx: Record<string, unknown>) => Promise<unknown> | unknown>
|
|
133
|
+
actions: Record<string, (params: Record<string, unknown>) => Promise<unknown> | unknown>
|
|
134
|
+
/**
|
|
135
|
+
* Custom condition evaluator. When provided it fully replaces the built-in
|
|
136
|
+
* evaluators — neither the safe interpreter nor `new Function` is used. Plug
|
|
137
|
+
* in your own hardened/sandboxed expression engine here.
|
|
138
|
+
*/
|
|
139
|
+
conditionEvaluator?: ConditionEvaluator
|
|
140
|
+
/**
|
|
141
|
+
* Opt in to evaluating `condition` expressions as full, UNSANDBOXED JavaScript
|
|
142
|
+
* via `new Function`. SECURITY: only enable for workflow definitions authored by
|
|
143
|
+
* TRUSTED developers/admins — never end-user input. Ignored when
|
|
144
|
+
* `conditionEvaluator` is supplied. Defaults to `false`, i.e. the safe,
|
|
145
|
+
* non-`new Function` interpreter ({@link safeEvaluateCondition}) is used.
|
|
146
|
+
*/
|
|
147
|
+
allowUnsafeConditionEval?: boolean
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
#### `HttpStep`
|
|
152
|
+
|
|
153
|
+
A step that performs an outbound HTTP request via the swappable
|
|
154
|
+
`@molecule/api-http` core (never a hardcoded fetch/axios). `${$.path}`
|
|
155
|
+
templates in `url`, `headers`, `params`, and `body` are resolved against
|
|
156
|
+
context before the request is sent.
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
interface HttpStep {
|
|
160
|
+
type: 'http'
|
|
161
|
+
/** Request URL (supports `${$.path}` templates). */
|
|
162
|
+
url: string
|
|
163
|
+
/** HTTP method; defaults to `GET`. */
|
|
164
|
+
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS'
|
|
165
|
+
/** Request headers (values support `${$.path}` templates). */
|
|
166
|
+
headers?: Record<string, string>
|
|
167
|
+
/** Query parameters (values support `${$.path}` templates). */
|
|
168
|
+
params?: Record<string, string | number | boolean | undefined>
|
|
169
|
+
/** Request body — objects are JSON-encoded by the http core (supports templates). */
|
|
170
|
+
body?: unknown
|
|
171
|
+
/** Where to write `{ status, statusText, headers, data }` into the context. */
|
|
172
|
+
output?: string
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
#### `WorkflowDefinition`
|
|
177
|
+
|
|
178
|
+
Describes a complete workflow: which trigger fires it and what steps to run.
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
interface WorkflowDefinition {
|
|
182
|
+
trigger: string
|
|
183
|
+
triggerInput?: Record<string, unknown>
|
|
184
|
+
steps: WorkflowStep[]
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
#### `WorkflowEngine`
|
|
189
|
+
|
|
190
|
+
Public interface for the workflow engine returned by `createWorkflowEngine`.
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
interface WorkflowEngine {
|
|
194
|
+
execute(definition: WorkflowDefinition): Promise<WorkflowRun>
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### `WorkflowRun`
|
|
199
|
+
|
|
200
|
+
Result returned by `WorkflowEngine.execute` — overall outcome plus per-step trace.
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
interface WorkflowRun {
|
|
204
|
+
ok: boolean
|
|
205
|
+
context: Record<string, unknown>
|
|
206
|
+
/** What happened at each step: 'executed' | 'skipped' | 'errored'. */
|
|
207
|
+
trace: Array<{
|
|
208
|
+
index: number
|
|
209
|
+
type: WorkflowStepType
|
|
210
|
+
outcome: 'executed' | 'skipped' | 'errored'
|
|
211
|
+
error?: string
|
|
212
|
+
}>
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Types
|
|
217
|
+
|
|
218
|
+
#### `ConditionEvaluator`
|
|
219
|
+
|
|
220
|
+
Signature for a pluggable condition evaluator. Return `true` to let the run
|
|
221
|
+
continue past the `condition` step, `false` to short-circuit it.
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
type ConditionEvaluator = (expression: string, context: Record<string, unknown>) => boolean
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
#### `WorkflowStep`
|
|
228
|
+
|
|
229
|
+
Discriminated union of all step types that a workflow definition may contain.
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
type WorkflowStep = ConditionStep | ActionStep | DelayStep | AIPromptStep | HttpStep
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
#### `WorkflowStepType`
|
|
236
|
+
|
|
237
|
+
Union of all supported step type discriminants.
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
type WorkflowStepType = 'condition' | 'action' | 'delay' | 'http' | 'ai_prompt'
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Functions
|
|
244
|
+
|
|
245
|
+
#### `createWorkflowEngine(opts)`
|
|
246
|
+
|
|
247
|
+
Creates a `WorkflowEngine` instance wired with the provided trigger and action handlers.
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
function createWorkflowEngine(opts: EngineOptions): WorkflowEngine
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
#### `safeEvaluateCondition(expression, context)`
|
|
254
|
+
|
|
255
|
+
Safely evaluates a workflow condition expression WITHOUT `new Function`/`eval`.
|
|
256
|
+
|
|
257
|
+
Supports member access against the context (`$.a.b`, `$.list[0]`), comparisons,
|
|
258
|
+
boolean logic, unary `!`/`-`, grouping, and literals. Function calls,
|
|
259
|
+
assignments, and arbitrary JavaScript are unsupported — such an expression is
|
|
260
|
+
unparseable and evaluates to `false` rather than executing. An unparseable or
|
|
261
|
+
throwing expression counts as a non-matching condition.
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
function safeEvaluateCondition(expression: string, context: Record<string, unknown>): boolean
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
- `expression` — The condition expression to evaluate.
|
|
268
|
+
- `context` — The workflow context (bound to `$`).
|
|
269
|
+
|
|
270
|
+
**Returns:** `true` if the expression is truthy, otherwise `false`.
|
|
271
|
+
|
|
272
|
+
#### `unsafeEvaluateCondition(expression, context)`
|
|
273
|
+
|
|
274
|
+
UNSAFE: evaluates a workflow condition expression as full, unsandboxed
|
|
275
|
+
JavaScript via `new Function`. This is arbitrary code execution in the API
|
|
276
|
+
process — only run expressions authored by TRUSTED developers/admins, NEVER
|
|
277
|
+
end-user input. Opt in explicitly via `EngineOptions.allowUnsafeConditionEval`;
|
|
278
|
+
it is never the default. A throwing or unparseable expression counts as `false`.
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
function unsafeEvaluateCondition(expression: string, context: Record<string, unknown>): boolean
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
- `expression` — The condition expression to evaluate as JavaScript.
|
|
285
|
+
- `context` — The workflow context (bound to `$`).
|
|
286
|
+
|
|
287
|
+
**Returns:** `true` if the expression is truthy, otherwise `false`.
|
|
288
|
+
|
|
289
|
+
## Injection Notes
|
|
290
|
+
|
|
291
|
+
### Requirements
|
|
292
|
+
|
|
293
|
+
Peer dependencies:
|
|
294
|
+
|
|
295
|
+
- `@molecule/api-bonds-default-express` ^1.0.1
|
|
296
|
+
- `@molecule/api-database` ^1.0.1
|
|
297
|
+
- `@molecule/api-i18n` ^1.0.1
|
|
298
|
+
- `@molecule/api-middleware-validation` ^1.0.1
|
|
299
|
+
- `@molecule/api-ai` ^1.0.1
|
|
300
|
+
- `@molecule/api-http` ^1.0.1
|
|
301
|
+
|
|
302
|
+
### Runtime Dependencies
|
|
303
|
+
|
|
304
|
+
- `@molecule/api-ai`
|
|
305
|
+
- `@molecule/api-bonds-default-express`
|
|
306
|
+
- `@molecule/api-database`
|
|
307
|
+
- `@molecule/api-http`
|
|
308
|
+
- `@molecule/api-i18n`
|
|
309
|
+
- `@molecule/api-middleware-validation`
|
|
310
|
+
|
|
311
|
+
SECURITY — `condition` step evaluation is SAFE BY DEFAULT. Expressions are run
|
|
312
|
+
through a small built-in interpreter ({@link safeEvaluateCondition}) that
|
|
313
|
+
supports member access, comparisons, boolean logic and literals but does NOT
|
|
314
|
+
use `new Function`/`eval`, cannot call functions or assign, and blocks
|
|
315
|
+
`__proto__`/`constructor`/`prototype` — so a malicious expression cannot
|
|
316
|
+
execute arbitrary code. Full unsandboxed JavaScript (`new Function`) is
|
|
317
|
+
OPT-IN only via `EngineOptions.allowUnsafeConditionEval: true` — enable it
|
|
318
|
+
ONLY for workflow definitions authored by TRUSTED developers/admins, never
|
|
319
|
+
from end-user input. You may also supply your own `EngineOptions.conditionEvaluator`
|
|
320
|
+
(e.g. a hardened sandbox), which replaces both built-ins. An expression that
|
|
321
|
+
throws or fails to parse counts as `false` — the run short-circuits with
|
|
322
|
+
`ok: true` (a skipped run, not a failure).
|
|
323
|
+
|
|
324
|
+
`'http'` steps ARE executed: the request is sent through the swappable
|
|
325
|
+
`@molecule/api-http` core (bond `@molecule/api-http-axios` etc. to change the
|
|
326
|
+
client — the engine hardcodes no HTTP library). `${$.path}` templates in
|
|
327
|
+
`url`/`headers`/`params`/`body` are resolved against context, the
|
|
328
|
+
`{ status, statusText, headers, data }` response is written to `output`, and a
|
|
329
|
+
trace entry is recorded. A non-2xx response (the http core throws an
|
|
330
|
+
`HttpError`) yields an `errored` trace entry and halts the run — never a
|
|
331
|
+
silent skip. Registering an `http.*` action still works too, if you want a
|
|
332
|
+
fully custom HTTP path. SSRF caution: validate any user-influenced `url`.
|
|
333
|
+
|
|
334
|
+
Only `ai_prompt` steps need a bonded `ai` provider (`@molecule/api-ai`);
|
|
335
|
+
with none bonded that step errors and the run returns `ok: false` with the
|
|
336
|
+
error on its trace entry. All other step types run without any AI provider.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-ai-workflow-engine",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Trigger / condition / action workflow runner with AI-assisted step suggestions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
|
-
"dist"
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md"
|
|
21
22
|
],
|
|
22
23
|
"keywords": [
|
|
23
24
|
"molecule",
|
|
@@ -27,18 +28,23 @@
|
|
|
27
28
|
"zapier"
|
|
28
29
|
],
|
|
29
30
|
"license": "Apache-2.0",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
34
|
+
"directory": "packages/api/ai/workflow-engine"
|
|
35
|
+
},
|
|
30
36
|
"devDependencies": {
|
|
31
|
-
"@molecule/api-http": "1.0.
|
|
37
|
+
"@molecule/api-http": "1.0.1",
|
|
32
38
|
"@types/node": "26.1.2",
|
|
33
39
|
"typescript": "6.0.3",
|
|
34
40
|
"vitest": "4.1.10"
|
|
35
41
|
},
|
|
36
42
|
"peerDependencies": {
|
|
37
|
-
"@molecule/api-bonds-default-express": "^1.0.
|
|
38
|
-
"@molecule/api-database": "^1.0.
|
|
39
|
-
"@molecule/api-i18n": "^1.0.
|
|
40
|
-
"@molecule/api-middleware-validation": "^1.0.
|
|
41
|
-
"@molecule/api-ai": "^1.0.
|
|
42
|
-
"@molecule/api-http": "^1.0.
|
|
43
|
+
"@molecule/api-bonds-default-express": "^1.0.1",
|
|
44
|
+
"@molecule/api-database": "^1.0.1",
|
|
45
|
+
"@molecule/api-i18n": "^1.0.1",
|
|
46
|
+
"@molecule/api-middleware-validation": "^1.0.1",
|
|
47
|
+
"@molecule/api-ai": "^1.0.1",
|
|
48
|
+
"@molecule/api-http": "^1.0.1"
|
|
43
49
|
}
|
|
44
50
|
}
|