@onlyflows/servicenow-mcp 2.2.0-dev.6 → 2.2.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/.env.example +6 -3
- package/CHANGELOG.md +286 -0
- package/README.md +504 -101
- package/dist/atf-form.d.ts +3 -15
- package/dist/atf-form.d.ts.map +1 -1
- package/dist/atf-result-cache.d.ts +23 -59
- package/dist/atf-result-cache.d.ts.map +1 -1
- package/dist/auth.d.ts +1 -0
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +17 -0
- package/dist/auth.js.map +1 -1
- package/dist/batch-limits.d.ts +18 -0
- package/dist/batch-limits.d.ts.map +1 -0
- package/dist/batch-limits.js +21 -0
- package/dist/batch-limits.js.map +1 -0
- package/dist/bounded-input.d.ts +49 -0
- package/dist/bounded-input.d.ts.map +1 -0
- package/dist/bounded-input.js +81 -0
- package/dist/bounded-input.js.map +1 -0
- package/dist/client.d.ts +25 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +92 -18
- package/dist/client.js.map +1 -1
- package/dist/encoded-query-policy.d.ts +11 -0
- package/dist/encoded-query-policy.d.ts.map +1 -1
- package/dist/encoded-query-policy.js +10 -1
- package/dist/encoded-query-policy.js.map +1 -1
- package/dist/field-policy.d.ts +82 -3
- package/dist/field-policy.d.ts.map +1 -1
- package/dist/field-policy.js +609 -80
- package/dist/field-policy.js.map +1 -1
- package/dist/html-response.d.ts +34 -0
- package/dist/html-response.d.ts.map +1 -0
- package/dist/html-response.js +72 -0
- package/dist/html-response.js.map +1 -0
- package/dist/http-runtime.d.ts.map +1 -1
- package/dist/http-runtime.js +8 -0
- package/dist/http-runtime.js.map +1 -1
- package/dist/incident-journal-policy.d.ts +27 -5
- package/dist/incident-journal-policy.d.ts.map +1 -1
- package/dist/incident-journal-policy.js +34 -21
- package/dist/incident-journal-policy.js.map +1 -1
- package/dist/json-shape-limits.d.ts +71 -0
- package/dist/json-shape-limits.d.ts.map +1 -0
- package/dist/json-shape-limits.js +131 -0
- package/dist/json-shape-limits.js.map +1 -0
- package/dist/metadata-cache.d.ts +46 -21
- package/dist/metadata-cache.d.ts.map +1 -1
- package/dist/metadata-cache.js +228 -195
- package/dist/metadata-cache.js.map +1 -1
- package/dist/policy-card.d.ts +131 -0
- package/dist/policy-card.d.ts.map +1 -0
- package/dist/policy-card.js +409 -0
- package/dist/policy-card.js.map +1 -0
- package/dist/profile-manager.d.ts +7 -3
- package/dist/profile-manager.d.ts.map +1 -1
- package/dist/profile-manager.js +66 -3
- package/dist/profile-manager.js.map +1 -1
- package/dist/record-identifier-policy.d.ts +2 -8
- package/dist/record-identifier-policy.d.ts.map +1 -1
- package/dist/risky-grants.d.ts +25 -0
- package/dist/risky-grants.d.ts.map +1 -0
- package/dist/risky-grants.js +77 -0
- package/dist/risky-grants.js.map +1 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +26 -5
- package/dist/server.js.map +1 -1
- package/dist/setup-prompts.js +1 -0
- package/dist/setup-prompts.js.map +1 -1
- package/dist/setup.d.ts +1 -1
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +300 -58
- package/dist/setup.js.map +1 -1
- package/dist/stdio-runtime.d.ts +15 -0
- package/dist/stdio-runtime.d.ts.map +1 -1
- package/dist/stdio-runtime.js +60 -0
- package/dist/stdio-runtime.js.map +1 -1
- package/dist/structured-query.d.ts +100 -115
- package/dist/structured-query.d.ts.map +1 -1
- package/dist/structured-query.js +132 -16
- package/dist/structured-query.js.map +1 -1
- package/dist/table-policy.d.ts +10 -4
- package/dist/table-policy.d.ts.map +1 -1
- package/dist/table-policy.js +1 -1
- package/dist/table-policy.js.map +1 -1
- package/dist/tool-error.d.ts +18 -0
- package/dist/tool-error.d.ts.map +1 -1
- package/dist/tool-error.js +69 -0
- package/dist/tool-error.js.map +1 -1
- package/dist/tool-table-access.d.ts.map +1 -1
- package/dist/tool-table-access.js +32 -2
- package/dist/tool-table-access.js.map +1 -1
- package/dist/tools/aggregate.d.ts +13 -19
- package/dist/tools/aggregate.d.ts.map +1 -1
- package/dist/tools/aggregate.js +2 -3
- package/dist/tools/aggregate.js.map +1 -1
- package/dist/tools/all-fields-cap.d.ts.map +1 -1
- package/dist/tools/all-fields-cap.js +3 -38
- package/dist/tools/all-fields-cap.js.map +1 -1
- package/dist/tools/atf-author-module.d.ts +28 -7
- package/dist/tools/atf-author-module.d.ts.map +1 -1
- package/dist/tools/atf-author-module.js +4 -2
- package/dist/tools/atf-author-module.js.map +1 -1
- package/dist/tools/atf-contracts.d.ts +135 -829
- package/dist/tools/atf-contracts.d.ts.map +1 -1
- package/dist/tools/atf-shared.js +2 -2
- package/dist/tools/atf-shared.js.map +1 -1
- package/dist/tools/atf-step-catalog.d.ts +3 -7
- package/dist/tools/atf-step-catalog.d.ts.map +1 -1
- package/dist/tools/atf-step-catalog.js +12 -5
- package/dist/tools/atf-step-catalog.js.map +1 -1
- package/dist/tools/atf.d.ts +8 -24
- package/dist/tools/atf.d.ts.map +1 -1
- package/dist/tools/atf.js +2 -2
- package/dist/tools/atf.js.map +1 -1
- package/dist/tools/attach.d.ts +6 -22
- package/dist/tools/attach.d.ts.map +1 -1
- package/dist/tools/batch.d.ts +18 -71
- package/dist/tools/batch.d.ts.map +1 -1
- package/dist/tools/batch.js +544 -96
- package/dist/tools/batch.js.map +1 -1
- package/dist/tools/catalog.d.ts +2 -2
- package/dist/tools/catalog.d.ts.map +1 -1
- package/dist/tools/catalog.js +7 -1
- package/dist/tools/catalog.js.map +1 -1
- package/dist/tools/codesearch.d.ts +1 -13
- package/dist/tools/codesearch.d.ts.map +1 -1
- package/dist/tools/create-module.d.ts +9 -14
- package/dist/tools/create-module.d.ts.map +1 -1
- package/dist/tools/create-module.js +4 -8
- package/dist/tools/create-module.js.map +1 -1
- package/dist/tools/delete.d.ts +1 -9
- package/dist/tools/delete.d.ts.map +1 -1
- package/dist/tools/delete.js +2 -3
- package/dist/tools/delete.js.map +1 -1
- package/dist/tools/discover.d.ts +6 -14
- package/dist/tools/discover.d.ts.map +1 -1
- package/dist/tools/flow-view-contracts.d.ts +1023 -0
- package/dist/tools/flow-view-contracts.d.ts.map +1 -0
- package/dist/tools/flow-view-contracts.js +570 -0
- package/dist/tools/flow-view-contracts.js.map +1 -0
- package/dist/tools/flow-view-graph.d.ts +86 -0
- package/dist/tools/flow-view-graph.d.ts.map +1 -0
- package/dist/tools/flow-view-graph.js +993 -0
- package/dist/tools/flow-view-graph.js.map +1 -0
- package/dist/tools/flow-view-gzip.d.ts +52 -0
- package/dist/tools/flow-view-gzip.d.ts.map +1 -0
- package/dist/tools/flow-view-gzip.js +179 -0
- package/dist/tools/flow-view-gzip.js.map +1 -0
- package/dist/tools/flow-view-mermaid.d.ts +37 -0
- package/dist/tools/flow-view-mermaid.d.ts.map +1 -0
- package/dist/tools/flow-view-mermaid.js +337 -0
- package/dist/tools/flow-view-mermaid.js.map +1 -0
- package/dist/tools/flow-view-module.d.ts +61 -0
- package/dist/tools/flow-view-module.d.ts.map +1 -0
- package/dist/tools/flow-view-module.js +378 -0
- package/dist/tools/flow-view-module.js.map +1 -0
- package/dist/tools/flow-view-policy.d.ts +106 -0
- package/dist/tools/flow-view-policy.d.ts.map +1 -0
- package/dist/tools/flow-view-policy.js +461 -0
- package/dist/tools/flow-view-policy.js.map +1 -0
- package/dist/tools/flow-view-renderers.d.ts +68 -0
- package/dist/tools/flow-view-renderers.d.ts.map +1 -0
- package/dist/tools/flow-view-renderers.js +513 -0
- package/dist/tools/flow-view-renderers.js.map +1 -0
- package/dist/tools/flow-view-table.d.ts +115 -0
- package/dist/tools/flow-view-table.d.ts.map +1 -0
- package/dist/tools/flow-view-table.js +1002 -0
- package/dist/tools/flow-view-table.js.map +1 -0
- package/dist/tools/get-module.d.ts +36 -42
- package/dist/tools/get-module.d.ts.map +1 -1
- package/dist/tools/health.d.ts +9 -6
- package/dist/tools/health.d.ts.map +1 -1
- package/dist/tools/incident-journal-module.d.ts +8 -7
- package/dist/tools/incident-journal-module.d.ts.map +1 -1
- package/dist/tools/index.d.ts +4 -4
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +43 -6
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/markdown.d.ts +27 -0
- package/dist/tools/markdown.d.ts.map +1 -1
- package/dist/tools/markdown.js +104 -1
- package/dist/tools/markdown.js.map +1 -1
- package/dist/tools/nl.d.ts +1 -11
- package/dist/tools/nl.d.ts.map +1 -1
- package/dist/tools/profile.d.ts +1 -1
- package/dist/tools/profile.d.ts.map +1 -1
- package/dist/tools/profile.js +19 -1
- package/dist/tools/profile.js.map +1 -1
- package/dist/tools/query-module.d.ts +59 -71
- package/dist/tools/query-module.d.ts.map +1 -1
- package/dist/tools/query-module.js +2 -2
- package/dist/tools/query-module.js.map +1 -1
- package/dist/tools/record-write-shared.d.ts.map +1 -1
- package/dist/tools/record-write-shared.js +19 -2
- package/dist/tools/record-write-shared.js.map +1 -1
- package/dist/tools/relationships.d.ts +6 -22
- package/dist/tools/relationships.d.ts.map +1 -1
- package/dist/tools/result-envelope.d.ts +2255 -1357
- package/dist/tools/result-envelope.d.ts.map +1 -1
- package/dist/tools/result-envelope.js +24 -2
- package/dist/tools/result-envelope.js.map +1 -1
- package/dist/tools/schema-module.d.ts +36 -21
- package/dist/tools/schema-module.d.ts.map +1 -1
- package/dist/tools/schema-module.js +106 -65
- package/dist/tools/schema-module.js.map +1 -1
- package/dist/tools/script.d.ts +1 -11
- package/dist/tools/script.d.ts.map +1 -1
- package/dist/tools/syslog.d.ts +11 -18
- package/dist/tools/syslog.d.ts.map +1 -1
- package/dist/tools/syslog.js +2 -3
- package/dist/tools/syslog.js.map +1 -1
- package/dist/tools/table-hierarchy.d.ts +35 -0
- package/dist/tools/table-hierarchy.d.ts.map +1 -0
- package/dist/tools/table-hierarchy.js +74 -0
- package/dist/tools/table-hierarchy.js.map +1 -0
- package/dist/tools/tool-module.d.ts +40 -15
- package/dist/tools/tool-module.d.ts.map +1 -1
- package/dist/tools/tool-module.js +755 -205
- package/dist/tools/tool-module.js.map +1 -1
- package/dist/tools/update-module.d.ts +11 -17
- package/dist/tools/update-module.d.ts.map +1 -1
- package/dist/tools/update-module.js +10 -12
- package/dist/tools/update-module.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/dist/write-value-policy.d.ts +20 -12
- package/dist/write-value-policy.d.ts.map +1 -1
- package/dist/write-value-policy.js +179 -67
- package/dist/write-value-policy.js.map +1 -1
- package/docs/ADDING-TOOLS.md +22 -13
- package/docs/CLIENT-SETUP.md +110 -52
- package/docs/CROSS-CLIENT-RELEASE-MATRIX.md +2 -2
- package/docs/FLOW-VIEW.md +214 -0
- package/docs/PRODUCTION-SECURITY.md +154 -25
- package/docs/RELEASE-VALIDATION.md +2 -2
- package/docs/TESTING-OAUTH.md +1 -1
- package/docs/V2-MIGRATION.md +404 -30
- package/package.json +20 -6
- package/scripts/release-validate.mjs +56 -0
- package/server.json +22 -0
- package/tools/inspector/package-lock.json +163 -201
|
@@ -0,0 +1,993 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Graph builder for `sn_flow_view`: turns the loader's source-neutral
|
|
4
|
+
* `NormalizedFlowDefinition` (plus already-loaded subflows) into the
|
|
5
|
+
* `FlowGraph` the renderers consume.
|
|
6
|
+
*
|
|
7
|
+
* Pure and synchronous. No I/O, no policy decisions: every text and input it
|
|
8
|
+
* receives was already projected through field policy, so this module only
|
|
9
|
+
* shapes, bounds and links.
|
|
10
|
+
*
|
|
11
|
+
* Conventions (documented for Task E and Task F):
|
|
12
|
+
* - Node ids are generated, `n1..`, in walk order. The trigger is `n0`.
|
|
13
|
+
* - `nodes` is ordered root flow first (pre-order), then each expanded flow in
|
|
14
|
+
* expansion order (breadth first), each again in pre-order.
|
|
15
|
+
* - `FlowNode.depth` is relative to the node's OWN flow (0 = top level of that
|
|
16
|
+
* flow). The renderer indents an expanded subflow one level below its call
|
|
17
|
+
* site. `FlowNode.step` is the full outline path: a subflow body is
|
|
18
|
+
* numbered under its call site ("3" calls a subflow, whose steps are
|
|
19
|
+
* "3.1", "3.2", ...), so steps are unique across the whole graph.
|
|
20
|
+
*
|
|
21
|
+
* @module tools/flow-view-graph
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.FLOW_GRAPH_LADDER_TEXT_CHARS = exports.FLOW_GRAPH_BUDGET_STEPS = void 0;
|
|
25
|
+
exports.subflowCallTargets = subflowCallTargets;
|
|
26
|
+
exports.fitFlowGraphToBudget = fitFlowGraphToBudget;
|
|
27
|
+
exports.buildFlowGraph = buildFlowGraph;
|
|
28
|
+
const flow_view_contracts_js_1 = require("./flow-view-contracts.js");
|
|
29
|
+
const L = flow_view_contracts_js_1.FLOW_VIEW_LIMITS;
|
|
30
|
+
const B = flow_view_contracts_js_1.FLOW_GRAPH_SCHEMA_BOUNDS;
|
|
31
|
+
const SYS_ID = /^[0-9a-f]{32}$/u;
|
|
32
|
+
const PILL = /\{\{([^{}]{1,500})\}\}/gu;
|
|
33
|
+
const LOGIC_KIND_SET = new Set(flow_view_contracts_js_1.FLOW_LOGIC_KINDS);
|
|
34
|
+
const REDACTION_SET = new Set(flow_view_contracts_js_1.REDACTION_REASONS);
|
|
35
|
+
const TABLE_SET = new Set(flow_view_contracts_js_1.FLOW_VIEW_TABLES);
|
|
36
|
+
const TERMINAL_KINDS = new Set(["end_flow", "exit_loop", "skip_iteration"]);
|
|
37
|
+
/** Ladder step indices reported by {@link fitFlowGraphToBudget}. */
|
|
38
|
+
exports.FLOW_GRAPH_BUDGET_STEPS = Object.freeze({
|
|
39
|
+
FITS: 0,
|
|
40
|
+
VALUES_500: 1,
|
|
41
|
+
VALUES_120: 2,
|
|
42
|
+
INPUTS_DROPPED: 3,
|
|
43
|
+
TEXTS_CUT: 4,
|
|
44
|
+
NODES_HALVED: 5,
|
|
45
|
+
PILLS_VARIABLES_DROPPED: 6,
|
|
46
|
+
NODES_HALVED_REPEATEDLY: 7,
|
|
47
|
+
});
|
|
48
|
+
/** Descriptions, annotations and conditions are cut to this many units by the ladder. */
|
|
49
|
+
exports.FLOW_GRAPH_LADDER_TEXT_CHARS = 200;
|
|
50
|
+
/** Distinct, valid subflow targets called by a flow, in step order. */
|
|
51
|
+
function subflowCallTargets(def) {
|
|
52
|
+
const seen = new Set();
|
|
53
|
+
for (const step of def.steps) {
|
|
54
|
+
if (step.kind !== "subflow")
|
|
55
|
+
continue;
|
|
56
|
+
const id = step.subflow?.flow_sys_id;
|
|
57
|
+
if (typeof id === "string" && SYS_ID.test(id))
|
|
58
|
+
seen.add(id);
|
|
59
|
+
}
|
|
60
|
+
return [...seen];
|
|
61
|
+
}
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// Small helpers
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
function cutUnits(text, max) {
|
|
66
|
+
if (text.length <= max)
|
|
67
|
+
return text;
|
|
68
|
+
let end = max;
|
|
69
|
+
const last = text.charCodeAt(end - 1);
|
|
70
|
+
if (last >= 0xd800 && last <= 0xdbff)
|
|
71
|
+
end -= 1;
|
|
72
|
+
return text.slice(0, end);
|
|
73
|
+
}
|
|
74
|
+
function cleanText(raw, max) {
|
|
75
|
+
const out = { text: null };
|
|
76
|
+
if (!raw || typeof raw !== "object")
|
|
77
|
+
return out;
|
|
78
|
+
const original = typeof raw.original_length === "number" && Number.isInteger(raw.original_length) &&
|
|
79
|
+
raw.original_length >= 0 ? raw.original_length : undefined;
|
|
80
|
+
if (typeof raw.text === "string") {
|
|
81
|
+
if (raw.text.length > max) {
|
|
82
|
+
out.text = cutUnits(raw.text, max);
|
|
83
|
+
out.original_length = Math.max(original ?? 0, raw.text.length);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
out.text = raw.text;
|
|
87
|
+
if (original !== undefined)
|
|
88
|
+
out.original_length = original;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
else if (original !== undefined) {
|
|
92
|
+
out.original_length = original;
|
|
93
|
+
}
|
|
94
|
+
if (typeof raw.redacted === "string" && REDACTION_SET.has(raw.redacted)) {
|
|
95
|
+
out.redacted = raw.redacted;
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
function shortString(value, max) {
|
|
100
|
+
return typeof value === "string" ? cutUnits(value, max) : null;
|
|
101
|
+
}
|
|
102
|
+
function finiteOrder(value) {
|
|
103
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
104
|
+
}
|
|
105
|
+
function validRef(ref) {
|
|
106
|
+
if (!ref || typeof ref !== "object")
|
|
107
|
+
return undefined;
|
|
108
|
+
if (!TABLE_SET.has(ref.table) || typeof ref.sys_id !== "string" || !SYS_ID.test(ref.sys_id))
|
|
109
|
+
return undefined;
|
|
110
|
+
return { table: ref.table, sys_id: ref.sys_id };
|
|
111
|
+
}
|
|
112
|
+
function byteLength(value) {
|
|
113
|
+
return Buffer.byteLength(JSON.stringify(value));
|
|
114
|
+
}
|
|
115
|
+
function headFamily(kind) {
|
|
116
|
+
if (kind === "if")
|
|
117
|
+
return "if";
|
|
118
|
+
if (kind === "try")
|
|
119
|
+
return "try";
|
|
120
|
+
if (kind === "top_level_try")
|
|
121
|
+
return "top_level_try";
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
function branchFamily(kind) {
|
|
125
|
+
if (kind === "else_if" || kind === "else")
|
|
126
|
+
return "if";
|
|
127
|
+
if (kind === "catch")
|
|
128
|
+
return "try";
|
|
129
|
+
if (kind === "top_level_catch")
|
|
130
|
+
return "top_level_try";
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
// ---------------------------------------------------------------------------
|
|
134
|
+
// Builder
|
|
135
|
+
// ---------------------------------------------------------------------------
|
|
136
|
+
class GraphBuilder {
|
|
137
|
+
input;
|
|
138
|
+
nodes = [];
|
|
139
|
+
sources = [];
|
|
140
|
+
flowDefs = [];
|
|
141
|
+
headers = [];
|
|
142
|
+
edges = [];
|
|
143
|
+
limits = new Set();
|
|
144
|
+
warnings = new Set();
|
|
145
|
+
edgeKeys = new Set();
|
|
146
|
+
expanded = new Map();
|
|
147
|
+
queue = [];
|
|
148
|
+
stepsSeen = 0;
|
|
149
|
+
itemsDropped = 0;
|
|
150
|
+
rootEntry;
|
|
151
|
+
constructor(input) {
|
|
152
|
+
this.input = input;
|
|
153
|
+
}
|
|
154
|
+
build() {
|
|
155
|
+
const { root } = this.input;
|
|
156
|
+
this.registerFlow(root, undefined);
|
|
157
|
+
const rootCtx = this.prepare(root, 0, 1, [root.header.sys_id], "");
|
|
158
|
+
this.rootEntry = this.walkFlow(rootCtx);
|
|
159
|
+
for (let next = this.queue.shift(); next; next = this.queue.shift()) {
|
|
160
|
+
const entry = this.walkFlow(next.ctx);
|
|
161
|
+
if (entry !== undefined)
|
|
162
|
+
this.edge(next.callId, entry, "subflow");
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// -- flows ---------------------------------------------------------------
|
|
166
|
+
registerFlow(def, callId) {
|
|
167
|
+
const index = this.headers.length;
|
|
168
|
+
const h = def.header;
|
|
169
|
+
const header = {
|
|
170
|
+
index,
|
|
171
|
+
source: "table",
|
|
172
|
+
sys_id: h.sys_id,
|
|
173
|
+
name: cleanText(h.name, L.MAX_TITLE_CHARS),
|
|
174
|
+
description: cleanText(h.description, B.MAX_TEXT_CHARS),
|
|
175
|
+
type: h.type === "flow" || h.type === "subflow" ? h.type : null,
|
|
176
|
+
active: typeof h.active === "boolean" ? h.active : null,
|
|
177
|
+
status: shortString(h.status, B.MAX_SHORT_CHARS),
|
|
178
|
+
version: shortString(h.version, B.MAX_SHORT_CHARS),
|
|
179
|
+
scope: shortString(h.scope, B.MAX_SHORT_CHARS),
|
|
180
|
+
};
|
|
181
|
+
if (callId !== undefined)
|
|
182
|
+
header.called_from = callId;
|
|
183
|
+
this.headers.push(header);
|
|
184
|
+
this.flowDefs.push(def);
|
|
185
|
+
this.expanded.set(h.sys_id, index);
|
|
186
|
+
for (const w of def.warnings)
|
|
187
|
+
this.warnings.add(w);
|
|
188
|
+
for (const l of def.limits_hit)
|
|
189
|
+
this.limits.add(l);
|
|
190
|
+
this.itemsDropped += Math.max(0, def.items_dropped | 0);
|
|
191
|
+
this.stepsSeen += def.steps.length;
|
|
192
|
+
return index;
|
|
193
|
+
}
|
|
194
|
+
/** Validate steps, then compute roots (including orphans) for one flow. */
|
|
195
|
+
prepare(def, index, level, path, prefix) {
|
|
196
|
+
const steps = [];
|
|
197
|
+
const byUid = new Map();
|
|
198
|
+
const position = new Map();
|
|
199
|
+
const kinds = new Map();
|
|
200
|
+
for (const step of def.steps) {
|
|
201
|
+
const valid = step && typeof step.uid === "string" && step.uid !== "" &&
|
|
202
|
+
(step.kind === "action" || step.kind === "logic" || step.kind === "subflow") &&
|
|
203
|
+
!byUid.has(step.uid);
|
|
204
|
+
if (!valid) {
|
|
205
|
+
this.itemsDropped += 1;
|
|
206
|
+
this.warnings.add("items_dropped_invalid");
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
position.set(step, steps.length);
|
|
210
|
+
steps.push(step);
|
|
211
|
+
byUid.set(step.uid, step);
|
|
212
|
+
kinds.set(step, this.logicKindOf(step));
|
|
213
|
+
}
|
|
214
|
+
const ctx = {
|
|
215
|
+
index, level, path, prefix, steps, position, byUid, kinds,
|
|
216
|
+
children: new Map(), roots: [], promoted: [], visited: new Set(),
|
|
217
|
+
};
|
|
218
|
+
const top = [];
|
|
219
|
+
const unknownParent = [];
|
|
220
|
+
for (const step of steps) {
|
|
221
|
+
const parent = step.parent_uid;
|
|
222
|
+
if (parent === null || parent === undefined || parent === "")
|
|
223
|
+
top.push(step);
|
|
224
|
+
else if (!byUid.has(parent))
|
|
225
|
+
unknownParent.push(step);
|
|
226
|
+
else {
|
|
227
|
+
const list = ctx.children.get(parent);
|
|
228
|
+
if (list)
|
|
229
|
+
list.push(step);
|
|
230
|
+
else
|
|
231
|
+
ctx.children.set(parent, [step]);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// Reachability from real roots, then from orphans, then from cycle heads.
|
|
235
|
+
const reached = new Set();
|
|
236
|
+
const mark = (start) => {
|
|
237
|
+
const stack = [start];
|
|
238
|
+
while (stack.length > 0) {
|
|
239
|
+
const cur = stack.pop();
|
|
240
|
+
if (reached.has(cur.uid))
|
|
241
|
+
continue;
|
|
242
|
+
reached.add(cur.uid);
|
|
243
|
+
for (const kid of ctx.children.get(cur.uid) ?? [])
|
|
244
|
+
stack.push(kid);
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
for (const step of top)
|
|
248
|
+
mark(step);
|
|
249
|
+
const promoted = [];
|
|
250
|
+
for (const step of this.sortSteps(ctx, unknownParent)) {
|
|
251
|
+
if (!reached.has(step.uid)) {
|
|
252
|
+
promoted.push(step);
|
|
253
|
+
mark(step);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
for (const step of this.sortSteps(ctx, steps)) {
|
|
257
|
+
if (!reached.has(step.uid)) {
|
|
258
|
+
promoted.push(step);
|
|
259
|
+
mark(step);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (promoted.length > 0)
|
|
263
|
+
this.warnings.add("orphan_steps");
|
|
264
|
+
ctx.roots = top;
|
|
265
|
+
ctx.promoted = promoted;
|
|
266
|
+
return ctx;
|
|
267
|
+
}
|
|
268
|
+
logicKindOf(step) {
|
|
269
|
+
if (step.kind !== "logic")
|
|
270
|
+
return null;
|
|
271
|
+
const kind = step.logic_kind;
|
|
272
|
+
if (typeof kind === "string" && LOGIC_KIND_SET.has(kind) && kind !== "other")
|
|
273
|
+
return kind;
|
|
274
|
+
this.warnings.add("unknown_logic_type");
|
|
275
|
+
return "other";
|
|
276
|
+
}
|
|
277
|
+
sortSteps(ctx, list) {
|
|
278
|
+
return [...list].sort((a, b) => {
|
|
279
|
+
const oa = finiteOrder(a.order);
|
|
280
|
+
const ob = finiteOrder(b.order);
|
|
281
|
+
if (oa !== ob) {
|
|
282
|
+
if (oa === null)
|
|
283
|
+
return 1;
|
|
284
|
+
if (ob === null)
|
|
285
|
+
return -1;
|
|
286
|
+
if (oa !== ob)
|
|
287
|
+
return oa - ob;
|
|
288
|
+
}
|
|
289
|
+
return (ctx.position.get(a) ?? 0) - (ctx.position.get(b) ?? 0);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
// -- walking -------------------------------------------------------------
|
|
293
|
+
walkFlow(ctx) {
|
|
294
|
+
const seq = this.sequence(ctx.roots, ctx, 0, undefined, ctx.prefix, ctx.promoted);
|
|
295
|
+
return seq?.entry;
|
|
296
|
+
}
|
|
297
|
+
edge(from, to, kind) {
|
|
298
|
+
const key = `${from}>${to}:${kind}`;
|
|
299
|
+
if (this.edgeKeys.has(key))
|
|
300
|
+
return;
|
|
301
|
+
this.edgeKeys.add(key);
|
|
302
|
+
this.edges.push({ from, to, kind });
|
|
303
|
+
}
|
|
304
|
+
arrange(list, ctx, prefix, tail = []) {
|
|
305
|
+
const live = list.filter((step) => !ctx.visited.has(step.uid));
|
|
306
|
+
const liveTail = tail.filter((step) => !ctx.visited.has(step.uid));
|
|
307
|
+
const sorted = [...this.sortSteps(ctx, live), ...this.sortSteps(ctx, liveTail)];
|
|
308
|
+
const inList = new Map();
|
|
309
|
+
for (const step of sorted)
|
|
310
|
+
inList.set(step.uid, step);
|
|
311
|
+
const branchHead = new Map();
|
|
312
|
+
// Branches of each head, in `sorted` order; built once so grouping is linear.
|
|
313
|
+
const branchesOf = new Map();
|
|
314
|
+
const lastHead = new Map();
|
|
315
|
+
for (const step of sorted) {
|
|
316
|
+
const kind = ctx.kinds.get(step) ?? null;
|
|
317
|
+
const family = branchFamily(kind);
|
|
318
|
+
if (family) {
|
|
319
|
+
let head = step.connected_uid ? inList.get(step.connected_uid) : undefined;
|
|
320
|
+
if (head && (head === step || headFamily(ctx.kinds.get(head) ?? null) !== family))
|
|
321
|
+
head = undefined;
|
|
322
|
+
head ??= lastHead.get(family);
|
|
323
|
+
if (head) {
|
|
324
|
+
branchHead.set(step, head);
|
|
325
|
+
const branches = branchesOf.get(head);
|
|
326
|
+
if (branches)
|
|
327
|
+
branches.push(step);
|
|
328
|
+
else
|
|
329
|
+
branchesOf.set(head, [step]);
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
this.warnings.add("orphan_steps");
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
const own = headFamily(kind);
|
|
336
|
+
if (own)
|
|
337
|
+
lastHead.set(own, step);
|
|
338
|
+
}
|
|
339
|
+
const groups = [];
|
|
340
|
+
let counter = 0;
|
|
341
|
+
const entry = (step) => {
|
|
342
|
+
counter += 1;
|
|
343
|
+
return { step, num: prefix === "" ? String(counter) : `${prefix}.${counter}` };
|
|
344
|
+
};
|
|
345
|
+
for (const step of sorted) {
|
|
346
|
+
if (branchHead.has(step))
|
|
347
|
+
continue;
|
|
348
|
+
const head = entry(step);
|
|
349
|
+
const attached = [...(branchesOf.get(step) ?? [])];
|
|
350
|
+
// else_if branches first, the else last; each ordered by `order`.
|
|
351
|
+
const rank = (s) => (ctx.kinds.get(s) === "else" ? 1 : 0);
|
|
352
|
+
attached.sort((a, b) => rank(a) - rank(b));
|
|
353
|
+
groups.push({ head, branches: attached.map(entry) });
|
|
354
|
+
}
|
|
355
|
+
return groups;
|
|
356
|
+
}
|
|
357
|
+
sequence(list, ctx, depth, parent, prefix, tail = []) {
|
|
358
|
+
const groups = this.arrange(list, ctx, prefix, tail);
|
|
359
|
+
let first;
|
|
360
|
+
let open = [];
|
|
361
|
+
for (const group of groups) {
|
|
362
|
+
const result = this.group(group, ctx, depth, parent);
|
|
363
|
+
if (first === undefined)
|
|
364
|
+
first = result.entry;
|
|
365
|
+
for (const exit of open)
|
|
366
|
+
this.edge(exit.from, result.entry, exit.kind);
|
|
367
|
+
open = result.exits;
|
|
368
|
+
}
|
|
369
|
+
return first === undefined ? undefined : { entry: first, exits: open };
|
|
370
|
+
}
|
|
371
|
+
makeNode(entry, ctx, depth, parent) {
|
|
372
|
+
const step = entry.step;
|
|
373
|
+
ctx.visited.add(step.uid);
|
|
374
|
+
const id = `n${this.nodes.length + 1}`;
|
|
375
|
+
const node = {
|
|
376
|
+
id,
|
|
377
|
+
flow_index: ctx.index,
|
|
378
|
+
kind: step.kind,
|
|
379
|
+
step: entry.num,
|
|
380
|
+
depth,
|
|
381
|
+
order: finiteOrder(step.order),
|
|
382
|
+
title: cleanText(step.title, L.MAX_TITLE_CHARS),
|
|
383
|
+
annotation: cleanText(step.annotation, B.MAX_TEXT_CHARS),
|
|
384
|
+
};
|
|
385
|
+
if (step.kind === "logic")
|
|
386
|
+
node.logic_kind = ctx.kinds.get(step) ?? "other";
|
|
387
|
+
if (parent !== undefined)
|
|
388
|
+
node.parent = parent;
|
|
389
|
+
if (step.condition)
|
|
390
|
+
node.condition = cleanText(step.condition, B.MAX_TEXT_CHARS);
|
|
391
|
+
const ref = validRef(step.ref);
|
|
392
|
+
if (ref)
|
|
393
|
+
node.ref = ref;
|
|
394
|
+
this.nodes.push(node);
|
|
395
|
+
this.sources.push(step);
|
|
396
|
+
return id;
|
|
397
|
+
}
|
|
398
|
+
kids(step, ctx) {
|
|
399
|
+
return (ctx.children.get(step.uid) ?? []).filter((kid) => !ctx.visited.has(kid.uid));
|
|
400
|
+
}
|
|
401
|
+
/** Walk the children of `owner` one level deeper; undefined when empty or too deep. */
|
|
402
|
+
childSequence(owner, ownerDepth, ownerNum, children, ctx) {
|
|
403
|
+
if (children.length === 0)
|
|
404
|
+
return undefined;
|
|
405
|
+
if (ownerDepth + 1 > L.MAX_NESTING) {
|
|
406
|
+
this.limits.add("max_nesting");
|
|
407
|
+
return undefined;
|
|
408
|
+
}
|
|
409
|
+
return this.sequence(children, ctx, ownerDepth + 1, owner, ownerNum);
|
|
410
|
+
}
|
|
411
|
+
/** Body of a block: edge of `kind` to its first child, or the owner is the open end. */
|
|
412
|
+
body(owner, ownerDepth, ownerNum, step, kind, ctx) {
|
|
413
|
+
const seq = this.childSequence(owner, ownerDepth, ownerNum, this.kids(step, ctx), ctx);
|
|
414
|
+
if (!seq)
|
|
415
|
+
return [{ from: owner, kind }];
|
|
416
|
+
this.edge(owner, seq.entry, kind);
|
|
417
|
+
return seq.exits;
|
|
418
|
+
}
|
|
419
|
+
group(group, ctx, depth, parent) {
|
|
420
|
+
const { head } = group;
|
|
421
|
+
const kind = ctx.kinds.get(head.step) ?? null;
|
|
422
|
+
switch (kind) {
|
|
423
|
+
case "if":
|
|
424
|
+
return this.ifGroup(group, ctx, depth, parent);
|
|
425
|
+
case "try":
|
|
426
|
+
case "top_level_try":
|
|
427
|
+
return this.tryGroup(group, ctx, depth, parent);
|
|
428
|
+
case "for_each":
|
|
429
|
+
case "do_until":
|
|
430
|
+
return this.loop(head, ctx, depth, parent);
|
|
431
|
+
case "parallel":
|
|
432
|
+
return this.fanOut(head, ctx, depth, parent, "parallel");
|
|
433
|
+
case "decision":
|
|
434
|
+
return this.fanOut(head, ctx, depth, parent, "next");
|
|
435
|
+
default:
|
|
436
|
+
return this.plain(head, ctx, depth, parent, kind);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
ifGroup(group, ctx, depth, parent) {
|
|
440
|
+
const head = group.head;
|
|
441
|
+
const id = this.makeNode(head, ctx, depth, parent);
|
|
442
|
+
const exits = this.body(id, depth, head.num, head.step, "true", ctx);
|
|
443
|
+
let lastCondition = id;
|
|
444
|
+
let hasElse = false;
|
|
445
|
+
for (const branch of group.branches) {
|
|
446
|
+
const branchId = this.makeNode(branch, ctx, depth, parent);
|
|
447
|
+
this.edge(lastCondition, branchId, "false");
|
|
448
|
+
if (ctx.kinds.get(branch.step) === "else_if") {
|
|
449
|
+
exits.push(...this.body(branchId, depth, branch.num, branch.step, "true", ctx));
|
|
450
|
+
lastCondition = branchId;
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
hasElse = true;
|
|
454
|
+
exits.push(...this.body(branchId, depth, branch.num, branch.step, "next", ctx));
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (!hasElse)
|
|
458
|
+
exits.push({ from: lastCondition, kind: "false" });
|
|
459
|
+
return { entry: id, exits };
|
|
460
|
+
}
|
|
461
|
+
tryGroup(group, ctx, depth, parent) {
|
|
462
|
+
const head = group.head;
|
|
463
|
+
const id = this.makeNode(head, ctx, depth, parent);
|
|
464
|
+
const exits = this.body(id, depth, head.num, head.step, "next", ctx);
|
|
465
|
+
for (const branch of group.branches) {
|
|
466
|
+
const catchId = this.makeNode(branch, ctx, depth, parent);
|
|
467
|
+
this.edge(id, catchId, "error");
|
|
468
|
+
exits.push(...this.body(catchId, depth, branch.num, branch.step, "next", ctx));
|
|
469
|
+
}
|
|
470
|
+
return { entry: id, exits };
|
|
471
|
+
}
|
|
472
|
+
loop(head, ctx, depth, parent) {
|
|
473
|
+
const id = this.makeNode(head, ctx, depth, parent);
|
|
474
|
+
const seq = this.childSequence(id, depth, head.num, this.kids(head.step, ctx), ctx);
|
|
475
|
+
if (seq) {
|
|
476
|
+
this.edge(id, seq.entry, "loop_body");
|
|
477
|
+
for (const exit of seq.exits)
|
|
478
|
+
this.edge(exit.from, id, "loop_back");
|
|
479
|
+
}
|
|
480
|
+
return { entry: id, exits: [{ from: id, kind: "next" }] };
|
|
481
|
+
}
|
|
482
|
+
/** Parallel and Decision: every child starts its own branch; all branches join after. */
|
|
483
|
+
fanOut(head, ctx, depth, parent, edgeKind) {
|
|
484
|
+
const id = this.makeNode(head, ctx, depth, parent);
|
|
485
|
+
const children = this.kids(head.step, ctx);
|
|
486
|
+
if (children.length === 0)
|
|
487
|
+
return { entry: id, exits: [{ from: id, kind: "next" }] };
|
|
488
|
+
if (depth + 1 > L.MAX_NESTING) {
|
|
489
|
+
this.limits.add("max_nesting");
|
|
490
|
+
return { entry: id, exits: [{ from: id, kind: "next" }] };
|
|
491
|
+
}
|
|
492
|
+
const exits = [];
|
|
493
|
+
const groups = this.arrange(children, ctx, head.num);
|
|
494
|
+
for (const group of groups) {
|
|
495
|
+
for (const entry of [group.head, ...group.branches]) {
|
|
496
|
+
const branchKind = ctx.kinds.get(entry.step) ?? null;
|
|
497
|
+
const isBranch = branchKind === "parallel_branch" || branchKind === "decision_branch";
|
|
498
|
+
if (isBranch) {
|
|
499
|
+
const branchId = this.makeNode(entry, ctx, depth + 1, id);
|
|
500
|
+
this.edge(id, branchId, edgeKind);
|
|
501
|
+
exits.push(...this.body(branchId, depth + 1, entry.num, entry.step, "next", ctx));
|
|
502
|
+
}
|
|
503
|
+
else {
|
|
504
|
+
const single = this.group({ head: entry, branches: [] }, ctx, depth + 1, id);
|
|
505
|
+
this.edge(id, single.entry, edgeKind);
|
|
506
|
+
exits.push(...single.exits);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return { entry: id, exits };
|
|
511
|
+
}
|
|
512
|
+
/** Leaf steps and unknown containers (sequential). */
|
|
513
|
+
plain(head, ctx, depth, parent, kind) {
|
|
514
|
+
const step = head.step;
|
|
515
|
+
const id = this.makeNode(head, ctx, depth, parent);
|
|
516
|
+
if (step.kind === "subflow")
|
|
517
|
+
this.subflowCall(id, step, ctx);
|
|
518
|
+
const terminal = kind !== null && TERMINAL_KINDS.has(kind);
|
|
519
|
+
const inner = this.body(id, depth, head.num, step, "next", ctx);
|
|
520
|
+
return { entry: id, exits: terminal ? [] : inner };
|
|
521
|
+
}
|
|
522
|
+
// -- subflows ------------------------------------------------------------
|
|
523
|
+
subflowCall(id, step, ctx) {
|
|
524
|
+
const node = this.nodes[this.nodes.length - 1];
|
|
525
|
+
const raw = step.subflow?.flow_sys_id;
|
|
526
|
+
const target = typeof raw === "string" && SYS_ID.test(raw) ? raw : null;
|
|
527
|
+
const info = {
|
|
528
|
+
flow_sys_id: target,
|
|
529
|
+
name: cleanText(step.subflow?.name, L.MAX_TITLE_CHARS),
|
|
530
|
+
};
|
|
531
|
+
node.subflow = info;
|
|
532
|
+
if (target === null) {
|
|
533
|
+
info.not_expanded = "not_found";
|
|
534
|
+
this.warnings.add("subflow_not_found");
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
if (ctx.path.includes(target)) {
|
|
538
|
+
info.not_expanded = "cycle";
|
|
539
|
+
this.warnings.add("subflow_cycle");
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
const existing = this.expanded.get(target);
|
|
543
|
+
if (existing !== undefined) {
|
|
544
|
+
info.expanded_index = existing;
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
if (ctx.level >= this.input.options.depth) {
|
|
548
|
+
info.not_expanded = "depth";
|
|
549
|
+
this.limits.add("max_depth");
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
const loaded = this.input.subflows?.get(target);
|
|
553
|
+
if (loaded !== undefined && "unavailable" in loaded) {
|
|
554
|
+
info.not_expanded = loaded.unavailable;
|
|
555
|
+
this.warnings.add(loaded.unavailable === "denied"
|
|
556
|
+
? "subflow_denied"
|
|
557
|
+
: loaded.unavailable === "too_large"
|
|
558
|
+
? "subflow_too_large"
|
|
559
|
+
: loaded.unavailable === "timeout"
|
|
560
|
+
? "subflow_timeout"
|
|
561
|
+
: "subflow_not_found");
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
if (loaded === undefined) {
|
|
565
|
+
info.not_expanded = "limit";
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
if (this.headers.length - 1 >= L.MAX_SUBFLOW_EXPANSIONS) {
|
|
569
|
+
info.not_expanded = "limit";
|
|
570
|
+
this.limits.add("max_subflow_expansions");
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
const index = this.registerFlow(loaded, id);
|
|
574
|
+
info.expanded_index = index;
|
|
575
|
+
const prefix = node.step;
|
|
576
|
+
this.queue.push({
|
|
577
|
+
ctx: this.prepare(loaded, index, ctx.level + 1, [...ctx.path, target], prefix),
|
|
578
|
+
def: loaded,
|
|
579
|
+
callId: id,
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
// ---------------------------------------------------------------------------
|
|
584
|
+
// Inputs, pills, variables
|
|
585
|
+
// ---------------------------------------------------------------------------
|
|
586
|
+
class PillTable {
|
|
587
|
+
pills = [];
|
|
588
|
+
index = new Map();
|
|
589
|
+
hitLimit = false;
|
|
590
|
+
register(token, label) {
|
|
591
|
+
const key = `${token}\u0000${label ?? ""}`;
|
|
592
|
+
const known = this.index.get(key);
|
|
593
|
+
if (known !== undefined)
|
|
594
|
+
return known;
|
|
595
|
+
if (this.pills.length >= L.MAX_PILLS) {
|
|
596
|
+
this.hitLimit = true;
|
|
597
|
+
return undefined;
|
|
598
|
+
}
|
|
599
|
+
this.pills.push({ token, label });
|
|
600
|
+
this.index.set(key, this.pills.length - 1);
|
|
601
|
+
return this.pills.length - 1;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
function labelLookup(cache) {
|
|
605
|
+
const map = new Map();
|
|
606
|
+
const lower = new Map();
|
|
607
|
+
for (const pair of cache ?? []) {
|
|
608
|
+
if (!Array.isArray(pair) || typeof pair[0] !== "string" || typeof pair[1] !== "string")
|
|
609
|
+
continue;
|
|
610
|
+
if (!map.has(pair[0]))
|
|
611
|
+
map.set(pair[0], pair[1]);
|
|
612
|
+
const key = pair[0].toLowerCase();
|
|
613
|
+
if (!lower.has(key))
|
|
614
|
+
lower.set(key, pair[1]);
|
|
615
|
+
}
|
|
616
|
+
// Exact names win; the lowercased spelling is a fallback.
|
|
617
|
+
const merged = new Map(lower);
|
|
618
|
+
for (const [k, v] of map)
|
|
619
|
+
merged.set(k, v);
|
|
620
|
+
return merged;
|
|
621
|
+
}
|
|
622
|
+
function projectInputs(inputs, ic) {
|
|
623
|
+
if (!Array.isArray(inputs) || inputs.length === 0)
|
|
624
|
+
return undefined;
|
|
625
|
+
if (inputs.length > L.MAX_INPUTS_PER_NODE)
|
|
626
|
+
ic.limits.add("inputs_per_node");
|
|
627
|
+
const out = [];
|
|
628
|
+
for (const raw of inputs.slice(0, L.MAX_INPUTS_PER_NODE)) {
|
|
629
|
+
if (!raw || typeof raw !== "object")
|
|
630
|
+
continue;
|
|
631
|
+
const input = {
|
|
632
|
+
name: cutUnits(typeof raw.name === "string" ? raw.name : "", L.MAX_NAME_CHARS),
|
|
633
|
+
label: shortString(raw.label, L.MAX_TITLE_CHARS),
|
|
634
|
+
type: shortString(raw.type, L.MAX_NAME_CHARS),
|
|
635
|
+
value: null,
|
|
636
|
+
};
|
|
637
|
+
if (typeof raw.value === "string") {
|
|
638
|
+
if (raw.value.length > L.MAX_INPUT_VALUE_CHARS) {
|
|
639
|
+
input.value = cutUnits(raw.value, L.MAX_INPUT_VALUE_CHARS);
|
|
640
|
+
const original = typeof raw.original_length === "number" ? raw.original_length : 0;
|
|
641
|
+
input.original_length = Math.max(original, raw.value.length);
|
|
642
|
+
ic.limits.add("input_value_chars");
|
|
643
|
+
}
|
|
644
|
+
else {
|
|
645
|
+
input.value = raw.value;
|
|
646
|
+
if (typeof raw.original_length === "number" && Number.isInteger(raw.original_length) &&
|
|
647
|
+
raw.original_length >= 0) {
|
|
648
|
+
input.original_length = raw.original_length;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
else if (typeof raw.original_length === "number" && Number.isInteger(raw.original_length) &&
|
|
653
|
+
raw.original_length >= 0) {
|
|
654
|
+
input.original_length = raw.original_length;
|
|
655
|
+
}
|
|
656
|
+
if (typeof raw.redacted === "string" && REDACTION_SET.has(raw.redacted)) {
|
|
657
|
+
input.redacted = raw.redacted;
|
|
658
|
+
}
|
|
659
|
+
if (raw.script === true)
|
|
660
|
+
input.script = true;
|
|
661
|
+
if (input.value !== null && input.redacted === undefined && input.script !== true) {
|
|
662
|
+
const indices = extractPills(input.value, ic);
|
|
663
|
+
if (indices.length > 0)
|
|
664
|
+
input.pills = indices;
|
|
665
|
+
}
|
|
666
|
+
out.push(input);
|
|
667
|
+
}
|
|
668
|
+
return out.length > 0 ? out : undefined;
|
|
669
|
+
}
|
|
670
|
+
function extractPills(value, ic) {
|
|
671
|
+
const indices = [];
|
|
672
|
+
for (const match of value.matchAll(PILL)) {
|
|
673
|
+
const token = match[0];
|
|
674
|
+
const inner = match[1].trim();
|
|
675
|
+
if (inner === "" || token.length > B.MAX_PILL_TOKEN_CHARS)
|
|
676
|
+
continue;
|
|
677
|
+
const found = ic.labels.get(inner) ?? ic.labels.get(inner.toLowerCase());
|
|
678
|
+
const label = found === undefined ? null : cutUnits(found, L.MAX_PILL_LABEL_CHARS);
|
|
679
|
+
const index = ic.pills.register(token, label);
|
|
680
|
+
if (index !== undefined && !indices.includes(index))
|
|
681
|
+
indices.push(index);
|
|
682
|
+
}
|
|
683
|
+
if (ic.pills.hitLimit)
|
|
684
|
+
ic.limits.add("pills");
|
|
685
|
+
return indices;
|
|
686
|
+
}
|
|
687
|
+
// ---------------------------------------------------------------------------
|
|
688
|
+
// Truncation and the byte-fitting ladder
|
|
689
|
+
// ---------------------------------------------------------------------------
|
|
690
|
+
function sortedLimits(limits) {
|
|
691
|
+
const set = new Set(limits);
|
|
692
|
+
return flow_view_contracts_js_1.FLOW_LIMIT_HITS.filter((hit) => set.has(hit));
|
|
693
|
+
}
|
|
694
|
+
function sortedWarnings(warnings) {
|
|
695
|
+
const set = new Set(warnings);
|
|
696
|
+
return flow_view_contracts_js_1.FLOW_WARNING_CODES.filter((code) => set.has(code)).slice(0, L.MAX_WARNINGS);
|
|
697
|
+
}
|
|
698
|
+
function allInputs(graph) {
|
|
699
|
+
const out = [...(graph.trigger?.inputs ?? [])];
|
|
700
|
+
for (const node of graph.nodes)
|
|
701
|
+
out.push(...(node.inputs ?? []));
|
|
702
|
+
return out;
|
|
703
|
+
}
|
|
704
|
+
function recount(graph) {
|
|
705
|
+
const inputs = allInputs(graph);
|
|
706
|
+
graph.counts.nodes_returned = graph.nodes.filter((node) => node.kind !== "more").length;
|
|
707
|
+
graph.counts.inputs_redacted = inputs.filter((i) => i.redacted !== undefined && i.redacted !== "limit").length;
|
|
708
|
+
graph.counts.inputs_truncated = inputs.filter((i) => i.redacted === "limit" || (i.original_length !== undefined && i.redacted === undefined)).length;
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Keep the first `keep` step nodes (pre-order prefix, so parents survive),
|
|
712
|
+
* drop the rest and stand a single `more` node in for them. Edges into dropped
|
|
713
|
+
* nodes are redirected to `more`; edges out of dropped nodes disappear.
|
|
714
|
+
*/
|
|
715
|
+
function truncateSteps(graph, keep) {
|
|
716
|
+
const steps = graph.nodes.filter((node) => node.kind !== "more");
|
|
717
|
+
const oldMore = graph.nodes.find((node) => node.kind === "more");
|
|
718
|
+
if (keep >= steps.length && !oldMore)
|
|
719
|
+
return;
|
|
720
|
+
const kept = steps.slice(0, keep);
|
|
721
|
+
const keptIds = new Set(kept.map((node) => node.id));
|
|
722
|
+
const removed = steps.length - kept.length;
|
|
723
|
+
const omitted = (oldMore?.omitted_steps ?? 0) + removed;
|
|
724
|
+
const moreId = `n${kept.length + 1}`;
|
|
725
|
+
const live = (id) => id === "n0" || keptIds.has(id);
|
|
726
|
+
const edges = [];
|
|
727
|
+
const seen = new Set();
|
|
728
|
+
const push = (edge) => {
|
|
729
|
+
const key = `${edge.from}>${edge.to}:${edge.kind}`;
|
|
730
|
+
if (seen.has(key))
|
|
731
|
+
return;
|
|
732
|
+
seen.add(key);
|
|
733
|
+
edges.push(edge);
|
|
734
|
+
};
|
|
735
|
+
let pointsAtMore = false;
|
|
736
|
+
for (const edge of graph.edges) {
|
|
737
|
+
if (!live(edge.from))
|
|
738
|
+
continue;
|
|
739
|
+
if (live(edge.to))
|
|
740
|
+
push(edge);
|
|
741
|
+
else {
|
|
742
|
+
pointsAtMore = true;
|
|
743
|
+
push({ from: edge.from, to: moreId, kind: edge.kind });
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
if (!pointsAtMore) {
|
|
747
|
+
const tail = kept.length > 0 ? kept[kept.length - 1].id : graph.trigger ? "n0" : undefined;
|
|
748
|
+
if (tail !== undefined)
|
|
749
|
+
push({ from: tail, to: moreId, kind: "next" });
|
|
750
|
+
}
|
|
751
|
+
const more = {
|
|
752
|
+
id: moreId,
|
|
753
|
+
flow_index: 0,
|
|
754
|
+
kind: "more",
|
|
755
|
+
step: "",
|
|
756
|
+
depth: 0,
|
|
757
|
+
order: null,
|
|
758
|
+
title: { text: null },
|
|
759
|
+
annotation: { text: null },
|
|
760
|
+
omitted_steps: omitted,
|
|
761
|
+
};
|
|
762
|
+
graph.nodes = [...kept, more];
|
|
763
|
+
graph.edges = edges;
|
|
764
|
+
for (const flow of graph.flows) {
|
|
765
|
+
if (flow.called_from !== undefined && !keptIds.has(flow.called_from))
|
|
766
|
+
delete flow.called_from;
|
|
767
|
+
}
|
|
768
|
+
graph.limits_hit = sortedLimits([...graph.limits_hit, "max_nodes"]);
|
|
769
|
+
recount(graph);
|
|
770
|
+
}
|
|
771
|
+
function cutValues(graph, max) {
|
|
772
|
+
for (const input of allInputs(graph)) {
|
|
773
|
+
if (input.value !== null && input.value.length > max) {
|
|
774
|
+
input.original_length = Math.max(input.original_length ?? 0, input.value.length);
|
|
775
|
+
input.value = cutUnits(input.value, max);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
function dropInputValues(graph) {
|
|
780
|
+
for (const input of allInputs(graph)) {
|
|
781
|
+
if (input.redacted === undefined)
|
|
782
|
+
input.redacted = "limit";
|
|
783
|
+
input.value = null;
|
|
784
|
+
delete input.pills;
|
|
785
|
+
delete input.script;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
function dropPillsAndVariables(graph) {
|
|
789
|
+
for (const input of allInputs(graph))
|
|
790
|
+
delete input.pills;
|
|
791
|
+
graph.pills = [];
|
|
792
|
+
graph.variables = [];
|
|
793
|
+
}
|
|
794
|
+
/** Cut a long FlowText in place, recording its original length. */
|
|
795
|
+
function cutText(text, max) {
|
|
796
|
+
if (!text || text.text === null || text.text.length <= max)
|
|
797
|
+
return;
|
|
798
|
+
text.original_length = Math.max(text.original_length ?? 0, text.text.length);
|
|
799
|
+
text.text = cutUnits(text.text, max);
|
|
800
|
+
}
|
|
801
|
+
function cutLongTexts(graph) {
|
|
802
|
+
for (const flow of graph.flows)
|
|
803
|
+
cutText(flow.description, exports.FLOW_GRAPH_LADDER_TEXT_CHARS);
|
|
804
|
+
if (graph.trigger)
|
|
805
|
+
cutText(graph.trigger.condition, exports.FLOW_GRAPH_LADDER_TEXT_CHARS);
|
|
806
|
+
for (const node of graph.nodes) {
|
|
807
|
+
cutText(node.annotation, exports.FLOW_GRAPH_LADDER_TEXT_CHARS);
|
|
808
|
+
cutText(node.condition, exports.FLOW_GRAPH_LADDER_TEXT_CHARS);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* A cheap lower bound on the serialized size: every UTF-16 unit of an input
|
|
813
|
+
* value is at least one UTF-8 byte. When it alone passes the target, the
|
|
814
|
+
* graph cannot fit and is not serialized to find out.
|
|
815
|
+
*/
|
|
816
|
+
function inputValueUnits(graph) {
|
|
817
|
+
let units = 0;
|
|
818
|
+
for (const input of allInputs(graph))
|
|
819
|
+
units += input.value?.length ?? 0;
|
|
820
|
+
return units;
|
|
821
|
+
}
|
|
822
|
+
function stepCount(graph) {
|
|
823
|
+
return graph.nodes.filter((node) => node.kind !== "more").length;
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Reduce `graph` in place until `Buffer.byteLength(JSON.stringify(graph))` is
|
|
827
|
+
* at most `targetBytes`, applying the ladder of the design (section 2.3) in
|
|
828
|
+
* order and stopping at the first step that fits:
|
|
829
|
+
*
|
|
830
|
+
* 1. input values cut to 500 characters;
|
|
831
|
+
* 2. input values cut to 120 characters;
|
|
832
|
+
* 3. input values dropped (names kept, `redacted: "limit"`);
|
|
833
|
+
* 4. flow descriptions, step annotations and conditions cut to 200 units
|
|
834
|
+
* (`original_length` set);
|
|
835
|
+
* 5. step nodes halved, with a single `more` node;
|
|
836
|
+
* 6. pills and variables dropped;
|
|
837
|
+
* 7. (fallback) step nodes halved again until the graph fits or none remain.
|
|
838
|
+
*
|
|
839
|
+
* A measurement whose outcome is already known (the input values alone are
|
|
840
|
+
* larger than the target) is skipped, so a graph of very large values is not
|
|
841
|
+
* serialized whole; the result is the same.
|
|
842
|
+
*
|
|
843
|
+
* `response_bytes` is added to `limits_hit` whenever any step runs.
|
|
844
|
+
* Returns the step that made it fit (0 = already fit); when even the empty
|
|
845
|
+
* skeleton is larger than the target the last step is returned.
|
|
846
|
+
*/
|
|
847
|
+
function fitFlowGraphToBudget(graph, targetBytes) {
|
|
848
|
+
const fits = () => inputValueUnits(graph) <= targetBytes && byteLength(graph) <= targetBytes;
|
|
849
|
+
if (fits())
|
|
850
|
+
return exports.FLOW_GRAPH_BUDGET_STEPS.FITS;
|
|
851
|
+
graph.limits_hit = sortedLimits([...graph.limits_hit, "response_bytes"]);
|
|
852
|
+
const halve = () => {
|
|
853
|
+
const steps = stepCount(graph);
|
|
854
|
+
truncateSteps(graph, Math.floor(steps / 2));
|
|
855
|
+
};
|
|
856
|
+
const ladder = [
|
|
857
|
+
() => cutValues(graph, 500),
|
|
858
|
+
() => cutValues(graph, 120),
|
|
859
|
+
() => dropInputValues(graph),
|
|
860
|
+
() => cutLongTexts(graph),
|
|
861
|
+
halve,
|
|
862
|
+
() => dropPillsAndVariables(graph),
|
|
863
|
+
];
|
|
864
|
+
for (let i = 0; i < ladder.length; i += 1) {
|
|
865
|
+
ladder[i]();
|
|
866
|
+
recount(graph);
|
|
867
|
+
if (fits())
|
|
868
|
+
return i + 1;
|
|
869
|
+
}
|
|
870
|
+
while (stepCount(graph) > 0) {
|
|
871
|
+
halve();
|
|
872
|
+
if (fits())
|
|
873
|
+
break;
|
|
874
|
+
}
|
|
875
|
+
return exports.FLOW_GRAPH_BUDGET_STEPS.NODES_HALVED_REPEATEDLY;
|
|
876
|
+
}
|
|
877
|
+
// ---------------------------------------------------------------------------
|
|
878
|
+
// Entry point
|
|
879
|
+
// ---------------------------------------------------------------------------
|
|
880
|
+
/**
|
|
881
|
+
* Build the `FlowGraph` for one root flow and the subflows already loaded for
|
|
882
|
+
* it. Deterministic: the same input always gives the same graph, byte for byte.
|
|
883
|
+
*/
|
|
884
|
+
function buildFlowGraph(input) {
|
|
885
|
+
const { options } = input;
|
|
886
|
+
const builder = new GraphBuilder(input);
|
|
887
|
+
builder.build();
|
|
888
|
+
for (const hit of input.limits_hit ?? [])
|
|
889
|
+
builder.limits.add(hit);
|
|
890
|
+
for (const code of input.warnings ?? [])
|
|
891
|
+
builder.warnings.add(code);
|
|
892
|
+
const trigger = buildTrigger(input.root);
|
|
893
|
+
const nodes = builder.nodes;
|
|
894
|
+
// Inputs and pills run over every node so pill indices follow walk order;
|
|
895
|
+
// truncation below only removes a suffix.
|
|
896
|
+
const pills = new PillTable();
|
|
897
|
+
const labelMaps = builder.flowDefs.map((def) => labelLookup(def.label_cache));
|
|
898
|
+
if (trigger && options.include_inputs) {
|
|
899
|
+
const ic = { labels: labelMaps[0], pills, limits: builder.limits };
|
|
900
|
+
const projected = projectInputs(input.root.trigger?.inputs, ic);
|
|
901
|
+
if (projected)
|
|
902
|
+
trigger.inputs = projected;
|
|
903
|
+
}
|
|
904
|
+
const capacity = Math.max(0, options.max_nodes - (trigger ? 1 : 0));
|
|
905
|
+
const keepCount = Math.min(nodes.length, capacity);
|
|
906
|
+
if (options.include_inputs) {
|
|
907
|
+
for (let i = 0; i < keepCount; i += 1) {
|
|
908
|
+
const node = nodes[i];
|
|
909
|
+
const ic = { labels: labelMaps[node.flow_index], pills, limits: builder.limits };
|
|
910
|
+
const projected = projectInputs(builder.sources[i].inputs, ic);
|
|
911
|
+
if (projected)
|
|
912
|
+
node.inputs = projected;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
const variables = [];
|
|
916
|
+
builder.flowDefs.forEach((def, flowIndex) => {
|
|
917
|
+
for (const v of def.variables ?? []) {
|
|
918
|
+
if (variables.length >= B.MAX_VARIABLES)
|
|
919
|
+
break;
|
|
920
|
+
if (!v || (v.scope !== "input" && v.scope !== "output" && v.scope !== "variable"))
|
|
921
|
+
continue;
|
|
922
|
+
variables.push({
|
|
923
|
+
scope: v.scope,
|
|
924
|
+
flow_index: flowIndex,
|
|
925
|
+
name: cutUnits(typeof v.name === "string" ? v.name : "", L.MAX_NAME_CHARS),
|
|
926
|
+
label: shortString(v.label, L.MAX_TITLE_CHARS),
|
|
927
|
+
type: shortString(v.type, L.MAX_NAME_CHARS),
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
});
|
|
931
|
+
const edges = [...builder.edges];
|
|
932
|
+
if (trigger && builder.rootEntry !== undefined) {
|
|
933
|
+
edges.unshift({ from: "n0", to: builder.rootEntry, kind: "next" });
|
|
934
|
+
}
|
|
935
|
+
const graph = {
|
|
936
|
+
schema_version: 1,
|
|
937
|
+
source: "table",
|
|
938
|
+
flows: builder.headers,
|
|
939
|
+
trigger,
|
|
940
|
+
nodes,
|
|
941
|
+
edges,
|
|
942
|
+
pills: pills.pills,
|
|
943
|
+
variables,
|
|
944
|
+
options: {
|
|
945
|
+
depth: options.depth,
|
|
946
|
+
include_inputs: options.include_inputs,
|
|
947
|
+
max_nodes: options.max_nodes,
|
|
948
|
+
format: options.format,
|
|
949
|
+
},
|
|
950
|
+
limits_hit: [],
|
|
951
|
+
warnings: [],
|
|
952
|
+
counts: {
|
|
953
|
+
steps_seen: builder.stepsSeen,
|
|
954
|
+
nodes_returned: 0,
|
|
955
|
+
inputs_redacted: 0,
|
|
956
|
+
inputs_truncated: 0,
|
|
957
|
+
items_dropped: builder.itemsDropped,
|
|
958
|
+
},
|
|
959
|
+
};
|
|
960
|
+
if (nodes.length > capacity) {
|
|
961
|
+
builder.limits.add("max_nodes");
|
|
962
|
+
graph.limits_hit = sortedLimits(builder.limits);
|
|
963
|
+
truncateSteps(graph, capacity);
|
|
964
|
+
}
|
|
965
|
+
if (graph.edges.length > L.MAX_EDGES) {
|
|
966
|
+
graph.edges = graph.edges.slice(0, L.MAX_EDGES);
|
|
967
|
+
builder.limits.add("edges");
|
|
968
|
+
}
|
|
969
|
+
graph.limits_hit = sortedLimits([...builder.limits, ...graph.limits_hit]);
|
|
970
|
+
graph.warnings = sortedWarnings(builder.warnings);
|
|
971
|
+
// Flow headers keep a call site only while that node survives.
|
|
972
|
+
const ids = new Set(graph.nodes.map((node) => node.id));
|
|
973
|
+
for (const flow of graph.flows) {
|
|
974
|
+
if (flow.called_from !== undefined && !ids.has(flow.called_from))
|
|
975
|
+
delete flow.called_from;
|
|
976
|
+
}
|
|
977
|
+
recount(graph);
|
|
978
|
+
fitFlowGraphToBudget(graph, Math.floor(options.max_response_bytes * L.STRUCTURED_SHARE));
|
|
979
|
+
return graph;
|
|
980
|
+
}
|
|
981
|
+
function buildTrigger(root) {
|
|
982
|
+
const t = root.trigger;
|
|
983
|
+
if (!t)
|
|
984
|
+
return null;
|
|
985
|
+
return {
|
|
986
|
+
node_id: "n0",
|
|
987
|
+
type: cleanText(t.type, L.MAX_TITLE_CHARS),
|
|
988
|
+
table: cleanText(t.table, L.MAX_TITLE_CHARS),
|
|
989
|
+
condition: cleanText(t.condition, B.MAX_TEXT_CHARS),
|
|
990
|
+
schedule: cleanText(t.schedule, L.MAX_TITLE_CHARS),
|
|
991
|
+
};
|
|
992
|
+
}
|
|
993
|
+
//# sourceMappingURL=flow-view-graph.js.map
|