suppa-mcp-2 1.0.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/LICENSE +21 -0
- package/README.md +59 -0
- package/dist/auth.js +612 -0
- package/dist/bin.js +11 -0
- package/dist/config.js +168 -0
- package/dist/httpClient.js +269 -0
- package/dist/registry.js +111 -0
- package/dist/server.js +1533 -0
- package/dist/store.js +120 -0
- package/dist/tenants.js +408 -0
- package/dist/tools/automation.js +776 -0
- package/dist/tools/docs.js +534 -0
- package/dist/tools/entity.js +831 -0
- package/dist/tools/forms.js +550 -0
- package/dist/tools/migrate.js +936 -0
- package/dist/tools/nameMap.js +134 -0
- package/dist/tools/tasks.js +423 -0
- package/dist/tools/v1Fields.js +0 -0
- package/dist/tools/workspace.js +121 -0
- package/dist/utils.js +391 -0
- package/package.json +26 -0
|
@@ -0,0 +1,776 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Automation (workflow) read-only tools — AutomationWorkflow / runs / node catalog.
|
|
3
|
+
*
|
|
4
|
+
* Faithful 1:1 port of the Python `suppa_mcp/tools/automation.py`.
|
|
5
|
+
*
|
|
6
|
+
* Read-only grounding for a workflow-authoring agent. Automations are ordinary
|
|
7
|
+
* entities under /api/core/data (there is NO /automation/* controller). The
|
|
8
|
+
* node-type catalog uses the authoritative /api/automation/nodes endpoint when
|
|
9
|
+
* present, and is otherwise DERIVED from the `nodes` JSON of live workflows.
|
|
10
|
+
*/
|
|
11
|
+
import { makeRequest, searchEntity, selectEntity } from "../httpClient.js";
|
|
12
|
+
import { makeFilter, jsonResponse } from "../utils.js";
|
|
13
|
+
export const WORKFLOW_ENTITY = "AutomationWorkflow";
|
|
14
|
+
export const EXECUTE_ENTITY = "AutomationWorkflowExecute";
|
|
15
|
+
export const EXECUTE_DATA_ENTITY = "AutomationWorkflowExecuteData";
|
|
16
|
+
// No count endpoint exists (see tasks.count_tasks) — cap a single id page.
|
|
17
|
+
export const COUNT_CAP = 1000;
|
|
18
|
+
// Page size for the full-graph scan behind the derived node catalog.
|
|
19
|
+
const SCAN_PAGE = 200;
|
|
20
|
+
// Authoritative node-type catalog (newer builds; older ones 404 -> graph fallback).
|
|
21
|
+
const NODE_CATALOG_PATH = "/api/automation/nodes";
|
|
22
|
+
const _LIST_FIELDS = {
|
|
23
|
+
id: true,
|
|
24
|
+
name: true,
|
|
25
|
+
description: true,
|
|
26
|
+
isActive: true,
|
|
27
|
+
hasUnpublishedChanges: true,
|
|
28
|
+
priority: true,
|
|
29
|
+
publishedAt: true,
|
|
30
|
+
updatedAt: true,
|
|
31
|
+
};
|
|
32
|
+
const _GRAPH_FIELDS = {
|
|
33
|
+
id: true,
|
|
34
|
+
name: true,
|
|
35
|
+
description: true,
|
|
36
|
+
isActive: true,
|
|
37
|
+
priority: true,
|
|
38
|
+
hasUnpublishedChanges: true,
|
|
39
|
+
publishedAt: true,
|
|
40
|
+
nodes: true,
|
|
41
|
+
connections: true,
|
|
42
|
+
settings: true,
|
|
43
|
+
activeNodes: true,
|
|
44
|
+
activeConnections: true,
|
|
45
|
+
activeSettings: true,
|
|
46
|
+
};
|
|
47
|
+
const _RUN_FIELDS = {
|
|
48
|
+
id: true,
|
|
49
|
+
status: true,
|
|
50
|
+
mode: true,
|
|
51
|
+
startedAt: true,
|
|
52
|
+
finishedAt: true,
|
|
53
|
+
retryCount: true,
|
|
54
|
+
test: true,
|
|
55
|
+
triggerEvent: true,
|
|
56
|
+
triggerEntityName: true,
|
|
57
|
+
error: true,
|
|
58
|
+
};
|
|
59
|
+
function isPlainObject(v) {
|
|
60
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
61
|
+
}
|
|
62
|
+
function _envelope(rows, limit, offset, key) {
|
|
63
|
+
const hasMore = rows.length > limit;
|
|
64
|
+
return {
|
|
65
|
+
[key]: rows.slice(0, limit),
|
|
66
|
+
returned: Math.min(rows.length, limit),
|
|
67
|
+
has_more: hasMore,
|
|
68
|
+
next_offset: hasMore ? offset + limit : null,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Recursively drop ACL keys (any key starting with '$', i.e.
|
|
73
|
+
* $readAccess/$updateAccess/$instanceAccess). Returns a new structure; all
|
|
74
|
+
* real values — including legitimate nested nulls — are preserved verbatim.
|
|
75
|
+
*/
|
|
76
|
+
export function _clean(obj) {
|
|
77
|
+
if (isPlainObject(obj)) {
|
|
78
|
+
const out = {};
|
|
79
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
80
|
+
if (!String(k).startsWith("$")) {
|
|
81
|
+
out[k] = _clean(v);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
if (Array.isArray(obj)) {
|
|
87
|
+
return obj.map((v) => _clean(v));
|
|
88
|
+
}
|
|
89
|
+
return obj;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Drop keys whose value is None (structural-null omission). SHALLOW — only
|
|
93
|
+
* applied to dicts THIS layer constructs, never to platform payloads.
|
|
94
|
+
*/
|
|
95
|
+
export function _compact(d) {
|
|
96
|
+
const out = {};
|
|
97
|
+
for (const [k, v] of Object.entries(d)) {
|
|
98
|
+
if (v !== null && v !== undefined) {
|
|
99
|
+
out[k] = v;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* camelCase / kebab / snake -> Title Case words:
|
|
106
|
+
* 'emailSend' -> 'Email Send', 'triggerEntity' -> 'Trigger Entity'.
|
|
107
|
+
*/
|
|
108
|
+
export function _humanize(typeName) {
|
|
109
|
+
let s = (typeName || "").replace(/[-_]+/g, " ");
|
|
110
|
+
s = s.replace(/([a-z0-9])(?=[A-Z])/g, "$1 "); // split camelCase boundaries
|
|
111
|
+
return s
|
|
112
|
+
.split(/\s+/)
|
|
113
|
+
.filter((w) => w.length > 0)
|
|
114
|
+
.map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase())
|
|
115
|
+
.join(" ");
|
|
116
|
+
}
|
|
117
|
+
const _LOGIC_TYPES = new Set(["if", "switch", "merge", "loop", "filter"]);
|
|
118
|
+
/** Heuristic node grouping: trigger* -> trigger; if/switch/... -> logic; else action. */
|
|
119
|
+
export function _category(typeName) {
|
|
120
|
+
const t = typeName || "";
|
|
121
|
+
if (t.startsWith("trigger")) {
|
|
122
|
+
return "trigger";
|
|
123
|
+
}
|
|
124
|
+
if (_LOGIC_TYPES.has(t)) {
|
|
125
|
+
return "logic";
|
|
126
|
+
}
|
|
127
|
+
return "action";
|
|
128
|
+
}
|
|
129
|
+
/** Map an observed config value to a spec control-type string. */
|
|
130
|
+
export function _paramType(value) {
|
|
131
|
+
if (typeof value === "boolean") {
|
|
132
|
+
return "boolean";
|
|
133
|
+
}
|
|
134
|
+
if (typeof value === "number") {
|
|
135
|
+
return "number";
|
|
136
|
+
}
|
|
137
|
+
if (typeof value === "string") {
|
|
138
|
+
return value.includes("={{") ? "expression" : "string";
|
|
139
|
+
}
|
|
140
|
+
if (isPlainObject(value) || Array.isArray(value)) {
|
|
141
|
+
return "json";
|
|
142
|
+
}
|
|
143
|
+
return "string";
|
|
144
|
+
}
|
|
145
|
+
// Tolerant key lookup: real graphs use `type` + `parameters` (live-verified),
|
|
146
|
+
// but accept the common variants so derivation is robust across versions.
|
|
147
|
+
function _nodeType(n) {
|
|
148
|
+
return n.type ?? n.nodeType ?? n.kind ?? null;
|
|
149
|
+
}
|
|
150
|
+
function _nodeConfig(n) {
|
|
151
|
+
const cfg = n.config ?? n.data ?? n.parameters ?? {};
|
|
152
|
+
return isPlainObject(cfg) ? cfg : {};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The `nodes` column may be a list or an id-keyed dict — flatten to a list
|
|
156
|
+
* of node dicts.
|
|
157
|
+
*/
|
|
158
|
+
function _normalizeNodes(nodes) {
|
|
159
|
+
if (Array.isArray(nodes)) {
|
|
160
|
+
return nodes.filter((n) => isPlainObject(n));
|
|
161
|
+
}
|
|
162
|
+
if (isPlainObject(nodes)) {
|
|
163
|
+
return Object.values(nodes).filter((n) => isPlainObject(n));
|
|
164
|
+
}
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
/** Raw node -> {id, type, name?, config, position?}. config = parameters verbatim. */
|
|
168
|
+
function _normalizeNode(raw) {
|
|
169
|
+
const node = { id: raw.id ?? null, type: _nodeType(raw), config: _clean(_nodeConfig(raw)) };
|
|
170
|
+
if (raw.name !== null && raw.name !== undefined) {
|
|
171
|
+
node.name = raw.name;
|
|
172
|
+
}
|
|
173
|
+
if (raw.position !== null && raw.position !== undefined) {
|
|
174
|
+
node.position = raw.position;
|
|
175
|
+
}
|
|
176
|
+
return node;
|
|
177
|
+
}
|
|
178
|
+
/** Flatten {src: {handle: [{node, index}]}} -> [{from, fromHandle, to, toIndex}]. */
|
|
179
|
+
function _edgesFromConnections(connections) {
|
|
180
|
+
const edges = [];
|
|
181
|
+
if (!isPlainObject(connections)) {
|
|
182
|
+
return edges;
|
|
183
|
+
}
|
|
184
|
+
for (const [src, byHandle] of Object.entries(connections)) {
|
|
185
|
+
if (!isPlainObject(byHandle)) {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
for (const [handle, targets] of Object.entries(byHandle)) {
|
|
189
|
+
if (!Array.isArray(targets)) {
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
for (const t of targets) {
|
|
193
|
+
if (isPlainObject(t)) {
|
|
194
|
+
edges.push({
|
|
195
|
+
from: src,
|
|
196
|
+
fromHandle: handle,
|
|
197
|
+
to: t.node ?? null,
|
|
198
|
+
toIndex: t.index ?? 0,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return edges;
|
|
205
|
+
}
|
|
206
|
+
/** {nodes:[normalized], edges:[flattened]} — reused for live + published snapshots. */
|
|
207
|
+
function _normalizeGraph(nodes, connections) {
|
|
208
|
+
return {
|
|
209
|
+
nodes: _normalizeNodes(nodes).map((n) => _normalizeNode(n)),
|
|
210
|
+
edges: _edgesFromConnections(connections),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* runData [{nodeId,status,startedAt,finishedAt,data}] ->
|
|
215
|
+
* [{nodeId,status,startedAt?,finishedAt?,output?,error?}] (absent fields omitted).
|
|
216
|
+
*/
|
|
217
|
+
function _stepsFromRunData(runData) {
|
|
218
|
+
const steps = [];
|
|
219
|
+
if (!Array.isArray(runData)) {
|
|
220
|
+
return steps;
|
|
221
|
+
}
|
|
222
|
+
for (const entry of runData) {
|
|
223
|
+
if (isPlainObject(entry)) {
|
|
224
|
+
steps.push(_compact({
|
|
225
|
+
nodeId: entry.nodeId ?? null,
|
|
226
|
+
status: entry.status ?? null,
|
|
227
|
+
startedAt: entry.startedAt ?? null,
|
|
228
|
+
finishedAt: entry.finishedAt ?? null,
|
|
229
|
+
output: entry.data ?? null,
|
|
230
|
+
error: entry.error ?? null,
|
|
231
|
+
}));
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return steps;
|
|
235
|
+
}
|
|
236
|
+
/** List automation workflows with `nodeCount`. ACL stripped, null fields omitted. */
|
|
237
|
+
export async function listAutomations(search = "", isActive = null, limit = 20, offset = 0) {
|
|
238
|
+
limit = Math.max(1, Math.min(limit, 500));
|
|
239
|
+
const filters = [];
|
|
240
|
+
if (isActive !== null && isActive !== undefined) {
|
|
241
|
+
filters.push(makeFilter("isActive", isActive));
|
|
242
|
+
}
|
|
243
|
+
const rows = await searchEntity(WORKFLOW_ENTITY, filters, { ..._LIST_FIELDS, nodes: true }, limit + 1, offset, [{ field: "updatedAt", order: "desc" }], search);
|
|
244
|
+
const outRows = [];
|
|
245
|
+
for (const raw of rows) {
|
|
246
|
+
if (!isPlainObject(raw)) {
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
const r = _clean(raw);
|
|
250
|
+
const nodeCount = _normalizeNodes(r.nodes).length;
|
|
251
|
+
const row = _compact({
|
|
252
|
+
id: r.id ?? null,
|
|
253
|
+
name: r.name ?? null,
|
|
254
|
+
description: r.description ?? null,
|
|
255
|
+
isActive: r.isActive ?? null,
|
|
256
|
+
hasUnpublishedChanges: r.hasUnpublishedChanges ?? null,
|
|
257
|
+
priority: r.priority ?? null,
|
|
258
|
+
publishedAt: r.publishedAt ?? null,
|
|
259
|
+
updatedAt: r.updatedAt ?? null,
|
|
260
|
+
});
|
|
261
|
+
row.nodeCount = nodeCount;
|
|
262
|
+
outRows.push(row);
|
|
263
|
+
}
|
|
264
|
+
return jsonResponse(_envelope(outRows, limit, offset, "automations"));
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Get one automation workflow as a normalized graph: nodes [{id,type,name?,
|
|
268
|
+
* config,position?}] + edges [{from,fromHandle,to,toIndex}], settings, and the
|
|
269
|
+
* published snapshot. ACL keys stripped; structural nulls omitted; node `config`
|
|
270
|
+
* (parameters) preserved verbatim.
|
|
271
|
+
*/
|
|
272
|
+
export async function getAutomation(automationId) {
|
|
273
|
+
const rawRow = await selectEntity(WORKFLOW_ENTITY, [makeFilter("id", automationId)], _GRAPH_FIELDS);
|
|
274
|
+
if (rawRow === null) {
|
|
275
|
+
return jsonResponse({
|
|
276
|
+
error: `No automation ${automationId} (wrong id, deleted, or no access).`,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
const row = _clean(rawRow);
|
|
280
|
+
const out = _compact({
|
|
281
|
+
id: row.id ?? null,
|
|
282
|
+
name: row.name ?? null,
|
|
283
|
+
description: row.description ?? null,
|
|
284
|
+
isActive: row.isActive ?? null,
|
|
285
|
+
priority: row.priority ?? null,
|
|
286
|
+
hasUnpublishedChanges: row.hasUnpublishedChanges ?? null,
|
|
287
|
+
publishedAt: row.publishedAt ?? null,
|
|
288
|
+
});
|
|
289
|
+
const graph = _normalizeGraph(row.nodes, row.connections);
|
|
290
|
+
out.nodes = graph.nodes;
|
|
291
|
+
out.edges = graph.edges;
|
|
292
|
+
if (row.settings !== null && row.settings !== undefined) {
|
|
293
|
+
out.settings = row.settings;
|
|
294
|
+
}
|
|
295
|
+
// Emit the published snapshot only when one actually exists. Gate on
|
|
296
|
+
// truthiness so an unpublished workflow that stores empty activeNodes/
|
|
297
|
+
// activeConnections ([]/{}) is treated as "no snapshot".
|
|
298
|
+
if (isTruthy(row.activeNodes) || isTruthy(row.activeConnections) || isTruthy(row.activeSettings)) {
|
|
299
|
+
const published = _normalizeGraph(row.activeNodes, row.activeConnections);
|
|
300
|
+
if (row.activeSettings !== null && row.activeSettings !== undefined) {
|
|
301
|
+
published.settings = row.activeSettings;
|
|
302
|
+
}
|
|
303
|
+
out.published = published;
|
|
304
|
+
}
|
|
305
|
+
return jsonResponse(out);
|
|
306
|
+
}
|
|
307
|
+
/** Python truthiness for the gate: empty list / empty dict / null / undefined are falsy. */
|
|
308
|
+
function isTruthy(v) {
|
|
309
|
+
if (v === null || v === undefined) {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
if (Array.isArray(v)) {
|
|
313
|
+
return v.length > 0;
|
|
314
|
+
}
|
|
315
|
+
if (isPlainObject(v)) {
|
|
316
|
+
return Object.keys(v).length > 0;
|
|
317
|
+
}
|
|
318
|
+
return Boolean(v);
|
|
319
|
+
}
|
|
320
|
+
/** List runs of one automation, newest first. ACL stripped, null fields omitted. */
|
|
321
|
+
export async function listAutomationRuns(automationId, status = null, limit = 20, offset = 0) {
|
|
322
|
+
limit = Math.max(1, Math.min(limit, 500));
|
|
323
|
+
const filters = [makeFilter("workflow.id", automationId)];
|
|
324
|
+
if (status) {
|
|
325
|
+
filters.push(makeFilter("status", status));
|
|
326
|
+
}
|
|
327
|
+
const rows = await searchEntity(EXECUTE_ENTITY, filters, _RUN_FIELDS, limit + 1, offset, [
|
|
328
|
+
{ field: "startedAt", order: "desc" },
|
|
329
|
+
]);
|
|
330
|
+
const outRows = rows.filter((r) => isPlainObject(r)).map((r) => _compact(_clean(r)));
|
|
331
|
+
return jsonResponse(_envelope(outRows, limit, offset, "runs"));
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Get one run with its per-node `steps` (from runData) and `triggerPayload`
|
|
335
|
+
* (the run input). ACL stripped; null run-level fields omitted; no ExecuteData
|
|
336
|
+
* yields steps=[] (never null).
|
|
337
|
+
*/
|
|
338
|
+
export async function getAutomationRun(runId) {
|
|
339
|
+
const rawRun = await selectEntity(EXECUTE_ENTITY, [makeFilter("id", runId)], {
|
|
340
|
+
..._RUN_FIELDS,
|
|
341
|
+
waitTill: true,
|
|
342
|
+
createdAt: true,
|
|
343
|
+
workflow: { id: true, name: true },
|
|
344
|
+
});
|
|
345
|
+
if (rawRun === null) {
|
|
346
|
+
return jsonResponse({ error: `No run ${runId} (wrong id, deleted, or no access).` });
|
|
347
|
+
}
|
|
348
|
+
const run = _clean(rawRun);
|
|
349
|
+
const dataRows = await searchEntity(EXECUTE_DATA_ENTITY, [makeFilter("execution.id", runId)], { id: true, runData: true, executionState: true, workflowSnapshot: true }, 1);
|
|
350
|
+
const data = dataRows.length ? _clean(dataRows[0]) : {};
|
|
351
|
+
const out = _compact({
|
|
352
|
+
id: run.id ?? null,
|
|
353
|
+
status: run.status ?? null,
|
|
354
|
+
mode: run.mode ?? null,
|
|
355
|
+
startedAt: run.startedAt ?? null,
|
|
356
|
+
finishedAt: run.finishedAt ?? null,
|
|
357
|
+
test: run.test ?? null,
|
|
358
|
+
retryCount: run.retryCount ?? null,
|
|
359
|
+
triggerEvent: run.triggerEvent ?? null,
|
|
360
|
+
triggerEntityName: run.triggerEntityName ?? null,
|
|
361
|
+
error: run.error ?? null,
|
|
362
|
+
workflow: run.workflow ?? null,
|
|
363
|
+
});
|
|
364
|
+
out.steps = _stepsFromRunData(data.runData);
|
|
365
|
+
const state = data.executionState;
|
|
366
|
+
if (isPlainObject(state) && state.triggerPayload !== null && state.triggerPayload !== undefined) {
|
|
367
|
+
out.triggerPayload = state.triggerPayload;
|
|
368
|
+
}
|
|
369
|
+
return jsonResponse(out);
|
|
370
|
+
}
|
|
371
|
+
// --- derived node catalog (no backend endpoint — inferred from live graphs) --
|
|
372
|
+
/** Fetch every workflow's id/name/nodes/connections (paged). Read-only. */
|
|
373
|
+
async function _scanWorkflows() {
|
|
374
|
+
const out = [];
|
|
375
|
+
let offset = 0;
|
|
376
|
+
// eslint-disable-next-line no-constant-condition
|
|
377
|
+
while (true) {
|
|
378
|
+
const rows = await searchEntity(WORKFLOW_ENTITY, [], { id: true, name: true, nodes: true, connections: true }, SCAN_PAGE, offset, [{ field: "id", order: "asc" }]);
|
|
379
|
+
for (const r of rows) {
|
|
380
|
+
if (isPlainObject(r)) {
|
|
381
|
+
out.push(r);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (rows.length < SCAN_PAGE) {
|
|
385
|
+
return out;
|
|
386
|
+
}
|
|
387
|
+
offset += SCAN_PAGE;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* GET the authoritative node catalog (/api/automation/nodes).
|
|
392
|
+
*
|
|
393
|
+
* Returns the list of node-type descriptors on success, or `null` when the
|
|
394
|
+
* route is ABSENT (older builds 404) or the response is not a list — the
|
|
395
|
+
* fallback signal. Non-404 errors (e.g. 401) propagate.
|
|
396
|
+
*/
|
|
397
|
+
export async function _fetchNodeCatalog() {
|
|
398
|
+
let cat;
|
|
399
|
+
try {
|
|
400
|
+
cat = await makeRequest("GET", NODE_CATALOG_PATH);
|
|
401
|
+
}
|
|
402
|
+
catch (ex) {
|
|
403
|
+
// http_client formats every HTTP error as "HTTP <code> <reason>: <body>",
|
|
404
|
+
// so anchor on the prefix — a non-404 whose body merely contains "HTTP 404"
|
|
405
|
+
// must NOT be misread as "route absent" (and 401 must still surface).
|
|
406
|
+
const msg = ex instanceof Error ? ex.message : String(ex);
|
|
407
|
+
if (msg.startsWith("HTTP 404")) {
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
410
|
+
throw ex;
|
|
411
|
+
}
|
|
412
|
+
return Array.isArray(cat) ? cat : null;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* {node type: occurrences} across live graphs. Best-effort enrichment —
|
|
416
|
+
* never raises (returns {} on any failure).
|
|
417
|
+
*/
|
|
418
|
+
async function _nodeUsageCounts() {
|
|
419
|
+
const usage = {};
|
|
420
|
+
try {
|
|
421
|
+
for (const wf of await _scanWorkflows()) {
|
|
422
|
+
for (const n of _normalizeNodes(wf.nodes)) {
|
|
423
|
+
const t = _nodeType(n);
|
|
424
|
+
if (t) {
|
|
425
|
+
usage[t] = (usage[t] ?? 0) + 1;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
catch {
|
|
431
|
+
return {};
|
|
432
|
+
}
|
|
433
|
+
return usage;
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* From live graphs, gather for one node type: a sample value per config key,
|
|
437
|
+
* the OUTPUT handle names it emits, the INPUT indices it is wired into, and its
|
|
438
|
+
* usage count. Best-effort — returns empties on any failure.
|
|
439
|
+
*/
|
|
440
|
+
async function _graphEnrichment(nodeType) {
|
|
441
|
+
const samples = {};
|
|
442
|
+
const outHandles = new Set();
|
|
443
|
+
const inIndices = new Set();
|
|
444
|
+
let count = 0;
|
|
445
|
+
try {
|
|
446
|
+
for (const wf of await _scanWorkflows()) {
|
|
447
|
+
const nodes = _normalizeNodes(wf.nodes);
|
|
448
|
+
const idToType = {};
|
|
449
|
+
for (const n of nodes) {
|
|
450
|
+
idToType[n.id] = _nodeType(n);
|
|
451
|
+
}
|
|
452
|
+
for (const n of nodes) {
|
|
453
|
+
if (_nodeType(n) !== nodeType) {
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
count += 1;
|
|
457
|
+
for (const [k, v] of Object.entries(_nodeConfig(n))) {
|
|
458
|
+
if (!(k in samples)) {
|
|
459
|
+
samples[k] = v;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const conns = wf.connections;
|
|
464
|
+
if (isPlainObject(conns)) {
|
|
465
|
+
for (const [src, byHandle] of Object.entries(conns)) {
|
|
466
|
+
if (!isPlainObject(byHandle)) {
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
if (idToType[src] === nodeType) {
|
|
470
|
+
for (const h of Object.keys(byHandle)) {
|
|
471
|
+
outHandles.add(h);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
for (const targets of Object.values(byHandle)) {
|
|
475
|
+
if (Array.isArray(targets)) {
|
|
476
|
+
for (const tgt of targets) {
|
|
477
|
+
if (isPlainObject(tgt) && idToType[tgt.node] === nodeType) {
|
|
478
|
+
inIndices.add(tgt.index ?? 0);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
catch {
|
|
488
|
+
return [{}, new Set(), new Set(), 0];
|
|
489
|
+
}
|
|
490
|
+
return [samples, outHandles, inIndices, count];
|
|
491
|
+
}
|
|
492
|
+
/** Reduce observed control-types for one param to a single value (derived path). */
|
|
493
|
+
function _collapseParamType(types) {
|
|
494
|
+
if (types.size === 1) {
|
|
495
|
+
return types.values().next().value;
|
|
496
|
+
}
|
|
497
|
+
const isSubsetOfStrExpr = [...types].every((t) => t === "string" || t === "expression");
|
|
498
|
+
if (isSubsetOfStrExpr) {
|
|
499
|
+
return types.has("expression") ? "expression" : "string";
|
|
500
|
+
}
|
|
501
|
+
return "json";
|
|
502
|
+
}
|
|
503
|
+
/** Sort a list of strings the way Python's `sorted` does (codepoint order). */
|
|
504
|
+
function sortedStrings(items) {
|
|
505
|
+
return [...items].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
506
|
+
}
|
|
507
|
+
/** Fallback catalog derived from live graphs (build without the endpoint). */
|
|
508
|
+
async function _listNodeTypesDerived(s) {
|
|
509
|
+
const agg = {};
|
|
510
|
+
for (const wf of await _scanWorkflows()) {
|
|
511
|
+
for (const n of _normalizeNodes(wf.nodes)) {
|
|
512
|
+
const t = _nodeType(n);
|
|
513
|
+
if (!t) {
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
if (!(t in agg)) {
|
|
517
|
+
agg[t] = { count: 0, configKeys: new Set() };
|
|
518
|
+
}
|
|
519
|
+
const e = agg[t];
|
|
520
|
+
e.count += 1;
|
|
521
|
+
for (const k of Object.keys(_nodeConfig(n))) {
|
|
522
|
+
e.configKeys.add(k);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
const out = [];
|
|
527
|
+
for (const t of sortedStrings(Object.keys(agg))) {
|
|
528
|
+
if (s && !t.toLowerCase().includes(s)) {
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
const v = agg[t];
|
|
532
|
+
out.push({
|
|
533
|
+
type: t,
|
|
534
|
+
label: _humanize(t),
|
|
535
|
+
category: _category(t),
|
|
536
|
+
count: v.count,
|
|
537
|
+
configKeys: sortedStrings(v.configKeys),
|
|
538
|
+
source: "derived",
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
return out;
|
|
542
|
+
}
|
|
543
|
+
/** Fallback schema derived from live graphs (endpoint absent or type unknown). */
|
|
544
|
+
async function _nodeSchemaDerived(nodeType) {
|
|
545
|
+
const params = {};
|
|
546
|
+
const outHandles = new Set();
|
|
547
|
+
const inIndices = new Set();
|
|
548
|
+
let count = 0;
|
|
549
|
+
for (const wf of await _scanWorkflows()) {
|
|
550
|
+
const nodes = _normalizeNodes(wf.nodes);
|
|
551
|
+
const idToType = {};
|
|
552
|
+
for (const n of nodes) {
|
|
553
|
+
idToType[n.id] = _nodeType(n);
|
|
554
|
+
}
|
|
555
|
+
for (const n of nodes) {
|
|
556
|
+
if (_nodeType(n) !== nodeType) {
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
count += 1;
|
|
560
|
+
for (const [k, v] of Object.entries(_nodeConfig(n))) {
|
|
561
|
+
if (!(k in params)) {
|
|
562
|
+
params[k] = { types: new Set(), sample: v, seen: 0 };
|
|
563
|
+
}
|
|
564
|
+
const p = params[k];
|
|
565
|
+
p.types.add(_paramType(v));
|
|
566
|
+
p.seen += 1;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
const conns = wf.connections;
|
|
570
|
+
if (isPlainObject(conns)) {
|
|
571
|
+
for (const [src, byHandle] of Object.entries(conns)) {
|
|
572
|
+
if (!isPlainObject(byHandle)) {
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
if (idToType[src] === nodeType) {
|
|
576
|
+
for (const h of Object.keys(byHandle)) {
|
|
577
|
+
outHandles.add(h);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
for (const targets of Object.values(byHandle)) {
|
|
581
|
+
if (Array.isArray(targets)) {
|
|
582
|
+
for (const tgt of targets) {
|
|
583
|
+
if (isPlainObject(tgt) && idToType[tgt.node] === nodeType) {
|
|
584
|
+
inIndices.add(tgt.index ?? 0);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
const paramsOut = [];
|
|
593
|
+
for (const k of sortedStrings(Object.keys(params))) {
|
|
594
|
+
const v = params[k];
|
|
595
|
+
paramsOut.push(_compact({
|
|
596
|
+
name: k,
|
|
597
|
+
type: _collapseParamType(v.types),
|
|
598
|
+
required: count > 0 && v.seen === count,
|
|
599
|
+
sample: _clean(v.sample),
|
|
600
|
+
}));
|
|
601
|
+
}
|
|
602
|
+
return {
|
|
603
|
+
type: nodeType,
|
|
604
|
+
label: _humanize(nodeType),
|
|
605
|
+
category: _category(nodeType),
|
|
606
|
+
count: count,
|
|
607
|
+
params: paramsOut,
|
|
608
|
+
outputHandles: sortedStrings(outHandles).map((h) => ({ name: h })),
|
|
609
|
+
inputHandles: sortedNumbers(inIndices).map((i) => ({ index: i })),
|
|
610
|
+
inferred: true,
|
|
611
|
+
source: "derived",
|
|
612
|
+
note: "Derived from live graphs; 'options' and human field labels are " +
|
|
613
|
+
"frontend-only and not stored in the backend.",
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
/** Sort a set of numbers ascending (Python `sorted` on ints). */
|
|
617
|
+
function sortedNumbers(items) {
|
|
618
|
+
return [...items].sort((a, b) => a - b);
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* List available automation node types. Uses the authoritative catalog
|
|
622
|
+
* endpoint (/api/automation/nodes) when present, and falls back to deriving
|
|
623
|
+
* the catalog from live graphs on builds without the endpoint.
|
|
624
|
+
*/
|
|
625
|
+
export async function listAutomationNodeTypes(search = "") {
|
|
626
|
+
const s = (search || "").toLowerCase().trim();
|
|
627
|
+
const catalog = await _fetchNodeCatalog();
|
|
628
|
+
if (catalog === null) {
|
|
629
|
+
return jsonResponse(await _listNodeTypesDerived(s));
|
|
630
|
+
}
|
|
631
|
+
const usage = await _nodeUsageCounts();
|
|
632
|
+
const out = [];
|
|
633
|
+
for (const n of catalog) {
|
|
634
|
+
if (!isPlainObject(n)) {
|
|
635
|
+
continue;
|
|
636
|
+
}
|
|
637
|
+
const t = n.type;
|
|
638
|
+
const params = n.parameters;
|
|
639
|
+
// Skip abstract base entries that carry no parameter schema (e.g. {"type": "trigger"}).
|
|
640
|
+
if (!t || !isPlainObject(params)) {
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
if (s &&
|
|
644
|
+
!String(t).toLowerCase().includes(s) &&
|
|
645
|
+
!String(n.label ?? "").toLowerCase().includes(s)) {
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
out.push(_compact({
|
|
649
|
+
type: t,
|
|
650
|
+
label: n.label ?? null,
|
|
651
|
+
category: n.category ?? null,
|
|
652
|
+
description: n.description ?? null,
|
|
653
|
+
requiredParams: sortedStrings(Object.entries(params)
|
|
654
|
+
.filter(([, v]) => isPlainObject(v) && v.required)
|
|
655
|
+
.map(([k]) => k)),
|
|
656
|
+
paramCount: Object.keys(params).length,
|
|
657
|
+
usedCount: usage[t] ?? 0,
|
|
658
|
+
source: "catalog",
|
|
659
|
+
}));
|
|
660
|
+
}
|
|
661
|
+
out.sort((a, b) => {
|
|
662
|
+
const ka = String(a.type);
|
|
663
|
+
const kb = String(b.type);
|
|
664
|
+
return ka < kb ? -1 : ka > kb ? 1 : 0;
|
|
665
|
+
});
|
|
666
|
+
return jsonResponse(out);
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Schema for one automation node type. Uses the authoritative catalog endpoint
|
|
670
|
+
* when present, enriched with sample values and input/output handles observed
|
|
671
|
+
* in live graphs; falls back to a graph-derived schema on builds without the
|
|
672
|
+
* endpoint or for a type the catalog does not list.
|
|
673
|
+
*/
|
|
674
|
+
export async function getAutomationNodeSchema(nodeType) {
|
|
675
|
+
const catalog = await _fetchNodeCatalog();
|
|
676
|
+
if (catalog !== null) {
|
|
677
|
+
const entry = catalog.find((n) => isPlainObject(n) && n.type === nodeType && isPlainObject(n.parameters)) ?? null;
|
|
678
|
+
if (entry !== null) {
|
|
679
|
+
const [samples, outHandles, inIndices, count] = await _graphEnrichment(nodeType);
|
|
680
|
+
const params = [];
|
|
681
|
+
for (const [name, rawMeta] of Object.entries(entry.parameters)) {
|
|
682
|
+
const meta = isPlainObject(rawMeta) ? rawMeta : {};
|
|
683
|
+
params.push(_compact({
|
|
684
|
+
name: name,
|
|
685
|
+
type: meta.type ?? null,
|
|
686
|
+
required: Boolean(meta.required),
|
|
687
|
+
description: meta.description ?? null,
|
|
688
|
+
sample: name in samples ? _clean(samples[name]) : null,
|
|
689
|
+
}));
|
|
690
|
+
}
|
|
691
|
+
return jsonResponse(_compact({
|
|
692
|
+
type: nodeType,
|
|
693
|
+
label: entry.label ?? null,
|
|
694
|
+
category: entry.category ?? null,
|
|
695
|
+
description: entry.description ?? null,
|
|
696
|
+
params: params,
|
|
697
|
+
credentials: entry.credentials ?? null,
|
|
698
|
+
allowedCredentialTypes: entry.allowedCredentialTypes ?? null,
|
|
699
|
+
outputHandles: sortedStrings(outHandles).map((h) => ({ name: h })),
|
|
700
|
+
inputHandles: sortedNumbers(inIndices).map((i) => ({ index: i })),
|
|
701
|
+
usedCount: count,
|
|
702
|
+
source: "catalog",
|
|
703
|
+
}));
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
return jsonResponse(await _nodeSchemaDerived(nodeType));
|
|
707
|
+
}
|
|
708
|
+
// Registry entries that are abstract/polymorphic base tables have no
|
|
709
|
+
// materialized rows of their own; the backend 400s on a count query for them.
|
|
710
|
+
const _ABSTRACT_ENTITY_NAMES = new Set(["BaseEntity"]);
|
|
711
|
+
const _ABSTRACT_FLAG_KEYS = ["isAbstract", "abstract", "isBase", "isSystem"];
|
|
712
|
+
function _isAbstract(e) {
|
|
713
|
+
if (_ABSTRACT_ENTITY_NAMES.has(String(e.name ?? ""))) {
|
|
714
|
+
return true;
|
|
715
|
+
}
|
|
716
|
+
return _ABSTRACT_FLAG_KEYS.some((k) => e[k] === true);
|
|
717
|
+
}
|
|
718
|
+
/**
|
|
719
|
+
* Fuzzy-search the entity registry by name/title for entity disambiguation.
|
|
720
|
+
* Returns [{name, label, description?, recordCount, recordCountCapped?}].
|
|
721
|
+
* recordCount costs ONE capped id-page per match. Abstract/unqueryable entities
|
|
722
|
+
* yield recordCount=null + recordCountError instead of failing the whole search.
|
|
723
|
+
*/
|
|
724
|
+
export async function searchEntities(query, limit = 20) {
|
|
725
|
+
const schema = await makeRequest("GET", "/api/core/schema");
|
|
726
|
+
const q = (query || "").toLowerCase().trim();
|
|
727
|
+
limit = Math.max(1, limit);
|
|
728
|
+
const matches = [];
|
|
729
|
+
if (Array.isArray(schema)) {
|
|
730
|
+
for (const e of schema) {
|
|
731
|
+
if (!isPlainObject(e)) {
|
|
732
|
+
continue;
|
|
733
|
+
}
|
|
734
|
+
const name = String(e.name ?? "");
|
|
735
|
+
const title = String(e.title ?? "");
|
|
736
|
+
if (q && !name.toLowerCase().includes(q) && !title.toLowerCase().includes(q)) {
|
|
737
|
+
continue;
|
|
738
|
+
}
|
|
739
|
+
matches.push(e);
|
|
740
|
+
if (matches.length >= limit) {
|
|
741
|
+
break;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
const out = [];
|
|
746
|
+
for (const e of matches) {
|
|
747
|
+
const name = String(e.name ?? "");
|
|
748
|
+
const row = { name: name, label: String(e.title || name) };
|
|
749
|
+
const desc = String(e.description ?? "");
|
|
750
|
+
if (desc) {
|
|
751
|
+
row.description = desc;
|
|
752
|
+
}
|
|
753
|
+
// Abstract/polymorphic base tables (e.g. BaseEntity) cannot be counted —
|
|
754
|
+
// skip the probe, and otherwise GUARD it so one unqueryable entity never
|
|
755
|
+
// fails the whole search.
|
|
756
|
+
if (_isAbstract(e)) {
|
|
757
|
+
row.recordCount = null;
|
|
758
|
+
row.recordCountError = "abstract entity (not directly queryable)";
|
|
759
|
+
}
|
|
760
|
+
else {
|
|
761
|
+
try {
|
|
762
|
+
const rows = await searchEntity(name, [], { id: true }, COUNT_CAP);
|
|
763
|
+
row.recordCount = rows.length;
|
|
764
|
+
if (rows.length >= COUNT_CAP) {
|
|
765
|
+
row.recordCountCapped = true;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
catch (ex) {
|
|
769
|
+
row.recordCount = null;
|
|
770
|
+
row.recordCountError = ex instanceof Error ? ex.message : String(ex);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
out.push(row);
|
|
774
|
+
}
|
|
775
|
+
return jsonResponse(out);
|
|
776
|
+
}
|