@noetic-tools/core 5.19.1-next.1 → 5.20.0-next.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/dist/adapters/openrouter.js +5 -0
- package/dist/builders/control-flow-builders.d.ts +15 -6
- package/dist/builders/control-flow-builders.js +8 -0
- package/dist/builders/decide-builders.d.ts +2 -5
- package/dist/builders/dynamic-workflow.d.ts +16 -1
- package/dist/builders/dynamic-workflow.js +222 -11
- package/dist/builders/every.d.ts +3 -3
- package/dist/builders/every.js +2 -1
- package/dist/builders/provide-builder.d.ts +3 -2
- package/dist/builders/provide-builder.js +2 -1
- package/dist/builders/sequence.d.ts +14 -0
- package/dist/builders/sequence.js +103 -0
- package/dist/builders/spawn-builder.d.ts +3 -2
- package/dist/builders/spawn-builder.js +3 -2
- package/dist/builders/step-builders.d.ts +80 -1
- package/dist/builders/step-builders.js +115 -1
- package/dist/builders/verify-builder.d.ts +79 -0
- package/dist/builders/verify-builder.js +69 -0
- package/dist/builders/workflow-child-nodes.d.ts +21 -0
- package/dist/builders/workflow-child-nodes.js +335 -0
- package/dist/builders/workflow-connection-nodes.d.ts +17 -0
- package/dist/builders/workflow-connection-nodes.js +160 -0
- package/dist/builders/workflow-connections.d.ts +90 -0
- package/dist/builders/workflow-connections.js +560 -0
- package/dist/builders/workflow-control-nodes.d.ts +13 -0
- package/dist/builders/workflow-control-nodes.js +213 -0
- package/dist/builders/workflow-hydrator.d.ts +44 -3
- package/dist/builders/workflow-hydrator.js +53 -495
- package/dist/builders/workflow-verify.d.ts +19 -0
- package/dist/builders/workflow-verify.js +194 -0
- package/dist/conditions/conditions.d.ts +7 -5
- package/dist/conditions/conditions.js +7 -4
- package/dist/connections/blobs.d.ts +33 -0
- package/dist/connections/blobs.js +77 -0
- package/dist/connections/cron-schedule.d.ts +32 -0
- package/dist/connections/cron-schedule.js +213 -0
- package/dist/connections/durability.d.ts +65 -0
- package/dist/connections/durability.js +54 -0
- package/dist/connections/errors.d.ts +30 -0
- package/dist/connections/errors.js +100 -0
- package/dist/connections/event-stream.d.ts +24 -0
- package/dist/connections/event-stream.js +176 -0
- package/dist/connections/gates.d.ts +94 -0
- package/dist/connections/gates.js +271 -0
- package/dist/connections/glob.d.ts +10 -0
- package/dist/connections/glob.js +46 -0
- package/dist/connections/handle.d.ts +22 -0
- package/dist/connections/handle.js +84 -0
- package/dist/connections/harness-wiring.d.ts +52 -0
- package/dist/connections/harness-wiring.js +143 -0
- package/dist/connections/in-memory-connection.d.ts +147 -0
- package/dist/connections/in-memory-connection.js +593 -0
- package/dist/connections/index.d.ts +34 -0
- package/dist/connections/index.js +23 -0
- package/dist/connections/json-schema.d.ts +28 -0
- package/dist/connections/json-schema.js +338 -0
- package/dist/connections/model-tools.d.ts +46 -0
- package/dist/connections/model-tools.js +466 -0
- package/dist/connections/negotiation.d.ts +30 -0
- package/dist/connections/negotiation.js +159 -0
- package/dist/connections/policy.d.ts +21 -0
- package/dist/connections/policy.js +34 -0
- package/dist/connections/pool.d.ts +111 -0
- package/dist/connections/pool.js +339 -0
- package/dist/connections/refs.d.ts +74 -0
- package/dist/connections/refs.js +316 -0
- package/dist/connections/registry-types.d.ts +141 -0
- package/dist/connections/registry-types.js +14 -0
- package/dist/connections/registry.d.ts +125 -0
- package/dist/connections/registry.js +775 -0
- package/dist/connections/secrets.d.ts +59 -0
- package/dist/connections/secrets.js +153 -0
- package/dist/connections/settle.d.ts +9 -0
- package/dist/connections/settle.js +26 -0
- package/dist/connections/sources.d.ts +27 -0
- package/dist/connections/sources.js +300 -0
- package/dist/connections/triggers.d.ts +78 -0
- package/dist/connections/triggers.js +310 -0
- package/dist/connections/values.d.ts +26 -0
- package/dist/connections/values.js +93 -0
- package/dist/connections/views.d.ts +31 -0
- package/dist/connections/views.js +176 -0
- package/dist/expression/check.js +80 -1
- package/dist/expression/jsonata.d.ts +14 -4
- package/dist/expression/jsonata.js +27 -9
- package/dist/expression/step-outputs.d.ts +25 -0
- package/dist/expression/step-outputs.js +78 -0
- package/dist/harness/agent-harness.d.ts +69 -166
- package/dist/harness/agent-harness.js +170 -262
- package/dist/harness/call-span-collector.d.ts +33 -0
- package/dist/harness/call-span-collector.js +83 -0
- package/dist/harness/call-spend.d.ts +2 -0
- package/dist/harness/call-spend.js +10 -14
- package/dist/harness/connection-gate.d.ts +78 -0
- package/dist/harness/connection-gate.js +169 -0
- package/dist/harness/deps/interpreter.d.ts +1 -0
- package/dist/harness/deps/interpreter.js +1 -0
- package/dist/harness/deps/runtime.d.ts +1 -1
- package/dist/harness/deps/types.d.ts +1 -1
- package/dist/harness/harness-options.d.ts +153 -0
- package/dist/harness/harness-options.js +36 -0
- package/dist/harness/harness-tracing.d.ts +16 -0
- package/dist/harness/harness-tracing.js +35 -0
- package/dist/harness/llm-client.d.ts +71 -0
- package/dist/harness/llm-client.js +218 -0
- package/dist/harness/model-call.d.ts +2 -7
- package/dist/harness/model-call.js +141 -558
- package/dist/harness/model-request.d.ts +42 -0
- package/dist/harness/model-request.js +159 -0
- package/dist/harness/model-rounds.d.ts +18 -2
- package/dist/harness/model-rounds.js +39 -8
- package/dist/harness/model-stream.d.ts +39 -0
- package/dist/harness/model-stream.js +131 -0
- package/dist/harness/model-tool-round.d.ts +37 -0
- package/dist/harness/model-tool-round.js +197 -0
- package/dist/harness/spend.d.ts +2 -0
- package/dist/harness/spend.js +5 -1
- package/dist/index.d.ts +31 -6
- package/dist/index.js +18 -1
- package/dist/interpreter/connection-steps.d.ts +194 -0
- package/dist/interpreter/connection-steps.js +537 -0
- package/dist/interpreter/execute-action.d.ts +2 -8
- package/dist/interpreter/execute-action.js +3 -178
- package/dist/interpreter/execute-call.d.ts +19 -0
- package/dist/interpreter/execute-call.js +99 -0
- package/dist/interpreter/execute-control.d.ts +6 -1
- package/dist/interpreter/execute-control.js +14 -0
- package/dist/interpreter/execute-invoke-tool.d.ts +11 -0
- package/dist/interpreter/execute-invoke-tool.js +183 -0
- package/dist/interpreter/execute-verify.d.ts +21 -0
- package/dist/interpreter/execute-verify.js +391 -0
- package/dist/interpreter/execute-wait.d.ts +21 -0
- package/dist/interpreter/execute-wait.js +233 -0
- package/dist/interpreter/execute.js +24 -133
- package/dist/interpreter/step-kind-dispatch.d.ts +24 -0
- package/dist/interpreter/step-kind-dispatch.js +157 -0
- package/dist/interpreter/step-walk.d.ts +2 -0
- package/dist/interpreter/step-walk.js +24 -0
- package/dist/interpreter/verify-host.d.ts +40 -0
- package/dist/interpreter/verify-host.js +245 -0
- package/dist/runtime/durable/step-ledger.d.ts +34 -2
- package/dist/runtime/durable/step-ledger.js +97 -3
- package/dist/runtime/in-memory-artifact-store.d.ts +24 -0
- package/dist/runtime/in-memory-artifact-store.js +240 -0
- package/dist/schemas/workflow.d.ts +2 -2
- package/dist/schemas/workflow.js +1 -1
- package/dist/util/harness-internals.d.ts +23 -0
- package/dist/util/harness-internals.js +35 -0
- package/package.json +2 -2
- package/schema/noetic-workflow.schema.json +1170 -92
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The connection side of JSON workflows (spec 35 § JSON workflows, § What a
|
|
3
|
+
* document may use): which connection each `call`, `wait` and `verify` node
|
|
4
|
+
* runs on, the operations and events a document uses per manifest entry, the
|
|
5
|
+
* requirement each entry negotiates, value wrappers, and the host allowlist.
|
|
6
|
+
*/
|
|
7
|
+
import { frameworkCast, NoeticConfigError, parseProfileRequirement } from '@noetic-tools/types';
|
|
8
|
+
import { compileExpression, compute, computeMaybe } from '../expression/jsonata.js';
|
|
9
|
+
import { isWorkflowOpShorthand, stepListEntries } from '../schemas/workflow.js';
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region Errors
|
|
12
|
+
function unknownConnection(name, where, manifest) {
|
|
13
|
+
const declared = Object.keys(manifest);
|
|
14
|
+
return new NoeticConfigError({
|
|
15
|
+
code: 'CONNECTION_UNKNOWN',
|
|
16
|
+
message: `${where} uses connection '${name}', which the document's connections do not declare.`,
|
|
17
|
+
hint: declared.length > 0
|
|
18
|
+
? `Use one of ${declared.join(', ')}, or add '${name}' to the document's "connections".`
|
|
19
|
+
: `Declare it in the document: "connections": { "${name}": { "profile": "<profile>@<range>" } }.`,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
function noConnection(where, manifest) {
|
|
23
|
+
return new NoeticConfigError({
|
|
24
|
+
code: 'CONNECTION_INVALID',
|
|
25
|
+
message: `${where} names no connection, and the document has ${Object.keys(manifest).length} connections, so none is implied.`,
|
|
26
|
+
hint: 'Give the node a "connection", or run it inside a verify whose subject is the connection.',
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function notAllowed(message, hint) {
|
|
30
|
+
return new NoeticConfigError({
|
|
31
|
+
code: 'CONNECTION_NOT_ALLOWED',
|
|
32
|
+
message,
|
|
33
|
+
hint,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region Value wrappers
|
|
38
|
+
function isObject(value) {
|
|
39
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
40
|
+
}
|
|
41
|
+
/** Whether `value` is a wrapper whose one key is `key`. */
|
|
42
|
+
function isWrapper(value, key) {
|
|
43
|
+
return isObject(value) && Object.hasOwn(value, key) && Object.keys(value).length === 1;
|
|
44
|
+
}
|
|
45
|
+
/** Whether a value passes through as is: a `$secret`, `$ref` or `$blob` the registry resolves. */
|
|
46
|
+
function isOpaqueWrapper(value) {
|
|
47
|
+
return isWrapper(value, '$secret') || isWrapper(value, '$ref') || isWrapper(value, '$blob');
|
|
48
|
+
}
|
|
49
|
+
function compileExprValue(value, where, limits) {
|
|
50
|
+
const compiled = compileExpression(String(value.$expr), where, limits);
|
|
51
|
+
return (input, bindings) => computeMaybe(compiled, input, bindings);
|
|
52
|
+
}
|
|
53
|
+
function compileArray(values, where, limits) {
|
|
54
|
+
const items = values.map((v) => compileValue(frameworkCast(v), where, limits));
|
|
55
|
+
return async (input, bindings) => {
|
|
56
|
+
const out = [];
|
|
57
|
+
for (const item of items) {
|
|
58
|
+
const resolved = await item(input, bindings);
|
|
59
|
+
out.push(resolved === undefined ? null : resolved);
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function compileObject(value, where, limits) {
|
|
65
|
+
const fields = Object.entries(value).map(([key, v]) => [
|
|
66
|
+
key,
|
|
67
|
+
compileValue(frameworkCast(v), where, limits),
|
|
68
|
+
]);
|
|
69
|
+
return async (input, bindings) => {
|
|
70
|
+
const out = {};
|
|
71
|
+
for (const [key, field] of fields) {
|
|
72
|
+
const resolved = await field(input, bindings);
|
|
73
|
+
// An expression with no value leaves the field out rather than sending null.
|
|
74
|
+
if (resolved !== undefined) {
|
|
75
|
+
out[key] = resolved;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Compiles a connection argument value (spec 35 § JSON workflows, Values):
|
|
83
|
+
* `{ "$expr" }` evaluates against the node's input, `{ "$literal" }` passes its
|
|
84
|
+
* value through untouched, `{ "$secret" }`, `{ "$ref" }` and `{ "$blob" }` pass
|
|
85
|
+
* through for the registry to resolve, and arrays and objects compile field by field.
|
|
86
|
+
*
|
|
87
|
+
* @throws `NoeticConfigError` `WORKFLOW_VALIDATION_FAILED` when an expression does not parse.
|
|
88
|
+
*/
|
|
89
|
+
export function compileValue(value, where, limits) {
|
|
90
|
+
if (Array.isArray(value)) {
|
|
91
|
+
return compileArray(value, where, limits);
|
|
92
|
+
}
|
|
93
|
+
if (!isObject(value)) {
|
|
94
|
+
return async () => value;
|
|
95
|
+
}
|
|
96
|
+
if (isWrapper(value, '$expr')) {
|
|
97
|
+
return compileExprValue(value, where, limits);
|
|
98
|
+
}
|
|
99
|
+
if (isWrapper(value, '$literal')) {
|
|
100
|
+
const literal = value.$literal;
|
|
101
|
+
return async () => literal;
|
|
102
|
+
}
|
|
103
|
+
if (isOpaqueWrapper(value)) {
|
|
104
|
+
return async () => value;
|
|
105
|
+
}
|
|
106
|
+
return compileObject(value, where, limits);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Compiles a node's `args`: an object of values, or one `{ "$expr" }` computing
|
|
110
|
+
* them all (which must give a value).
|
|
111
|
+
*/
|
|
112
|
+
export function compileArgs(args, where, limits) {
|
|
113
|
+
if (args === undefined) {
|
|
114
|
+
return async () => ({});
|
|
115
|
+
}
|
|
116
|
+
if (isWrapper(args, '$expr')) {
|
|
117
|
+
const compiled = compileExpression(String(frameworkCast(args).$expr), where, limits);
|
|
118
|
+
return (input, bindings) => compute(compiled, input, bindings);
|
|
119
|
+
}
|
|
120
|
+
return compileValue(frameworkCast(args), where, limits);
|
|
121
|
+
}
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region Connection resolution
|
|
124
|
+
/**
|
|
125
|
+
* The manifest names a node's connection ref stands for: one name, every fan-out
|
|
126
|
+
* name for an expression inside a fan-out, or the enclosing connection(s) when absent.
|
|
127
|
+
*
|
|
128
|
+
* @throws `NoeticConfigError` `CONNECTION_UNKNOWN` for a name the manifest does not declare; `CONNECTION_INVALID` when no connection is named or implied.
|
|
129
|
+
*/
|
|
130
|
+
function namesOf(ref, where, scope) {
|
|
131
|
+
if (typeof ref === 'string') {
|
|
132
|
+
if (!Object.hasOwn(scope.manifest, ref)) {
|
|
133
|
+
throw unknownConnection(ref, where, scope.manifest);
|
|
134
|
+
}
|
|
135
|
+
return [
|
|
136
|
+
ref,
|
|
137
|
+
];
|
|
138
|
+
}
|
|
139
|
+
if (ref !== undefined) {
|
|
140
|
+
// An expression picks at run time: within a fan-out, any of its names.
|
|
141
|
+
return scope.fanOut ?? Object.keys(scope.manifest);
|
|
142
|
+
}
|
|
143
|
+
if (scope.enclosing) {
|
|
144
|
+
return scope.enclosing;
|
|
145
|
+
}
|
|
146
|
+
const declared = Object.keys(scope.manifest);
|
|
147
|
+
if (declared.length === 1) {
|
|
148
|
+
return declared;
|
|
149
|
+
}
|
|
150
|
+
throw noConnection(where, scope.manifest);
|
|
151
|
+
}
|
|
152
|
+
function useOf(scope, name) {
|
|
153
|
+
const existing = scope.uses.get(name);
|
|
154
|
+
if (existing) {
|
|
155
|
+
return existing;
|
|
156
|
+
}
|
|
157
|
+
const created = {
|
|
158
|
+
ops: new Set(),
|
|
159
|
+
events: new Set(),
|
|
160
|
+
secrets: [],
|
|
161
|
+
};
|
|
162
|
+
scope.uses.set(name, created);
|
|
163
|
+
return created;
|
|
164
|
+
}
|
|
165
|
+
/** Every `{ "$secret" }` in a value, with the dotted path of the field holding it. */
|
|
166
|
+
function secretsIn(value, path) {
|
|
167
|
+
if (isWrapper(value, '$secret')) {
|
|
168
|
+
return [
|
|
169
|
+
{
|
|
170
|
+
name: String(frameworkCast(value).$secret),
|
|
171
|
+
field: path.join('.'),
|
|
172
|
+
},
|
|
173
|
+
];
|
|
174
|
+
}
|
|
175
|
+
if (isWrapper(value, '$expr')) {
|
|
176
|
+
return [];
|
|
177
|
+
}
|
|
178
|
+
// A `$literal` passes its value through untouched, so a `$secret` inside it is
|
|
179
|
+
// still resolved by the gates: it counts as a use of that secret.
|
|
180
|
+
if (isWrapper(value, '$literal')) {
|
|
181
|
+
return secretsIn(frameworkCast(value).$literal, path);
|
|
182
|
+
}
|
|
183
|
+
if (Array.isArray(value)) {
|
|
184
|
+
return value.flatMap((v) => secretsIn(v, path));
|
|
185
|
+
}
|
|
186
|
+
if (!isObject(value)) {
|
|
187
|
+
return [];
|
|
188
|
+
}
|
|
189
|
+
return Object.entries(value).flatMap(([key, v]) => secretsIn(v, [
|
|
190
|
+
...path,
|
|
191
|
+
key,
|
|
192
|
+
]));
|
|
193
|
+
}
|
|
194
|
+
function recordCall(call, names, scope) {
|
|
195
|
+
const { op, args } = call;
|
|
196
|
+
for (const name of names) {
|
|
197
|
+
const use = useOf(scope, name);
|
|
198
|
+
use.ops.add(op);
|
|
199
|
+
for (const secret of secretsIn(args, [])) {
|
|
200
|
+
use.secrets.push({
|
|
201
|
+
name: secret.name,
|
|
202
|
+
op,
|
|
203
|
+
field: secret.field,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* The secrets a node's `args` write out, each with the dotted field it fills.
|
|
210
|
+
* The gates refuse any other secret a document call sends — one an expression
|
|
211
|
+
* produced at run time is never something the allowlist was checked against.
|
|
212
|
+
*/
|
|
213
|
+
export function documentSecretsOf(args) {
|
|
214
|
+
return secretsIn(args, []);
|
|
215
|
+
}
|
|
216
|
+
//#endregion
|
|
217
|
+
//#region Walk
|
|
218
|
+
function walkEntries(list, parentId, scope) {
|
|
219
|
+
for (const entry of stepListEntries(list)) {
|
|
220
|
+
walkEntry(entry, parentId, scope);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
function walkEntry(entry, parentId, scope) {
|
|
224
|
+
if (!isWorkflowOpShorthand(entry)) {
|
|
225
|
+
walkNode(entry, scope);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
for (const [op, args] of Object.entries(entry)) {
|
|
229
|
+
const where = `operation '${op}' in node '${parentId}'`;
|
|
230
|
+
recordCall({
|
|
231
|
+
op,
|
|
232
|
+
args,
|
|
233
|
+
}, namesOf(undefined, where, scope), scope);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
function walkWait(node, scope) {
|
|
237
|
+
const names = namesOf(node.connection, `wait node '${node.id}'`, scope);
|
|
238
|
+
for (const name of names) {
|
|
239
|
+
useOf(scope, name).events.add(node.event);
|
|
240
|
+
}
|
|
241
|
+
if (node.around !== undefined) {
|
|
242
|
+
walkEntries(node.around, node.id, {
|
|
243
|
+
...scope,
|
|
244
|
+
enclosing: names,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function subjectNames(node, scope) {
|
|
249
|
+
const where = `verify node '${node.id}'`;
|
|
250
|
+
if (Array.isArray(node.subject)) {
|
|
251
|
+
return node.subject.flatMap((name) => namesOf(name, where, scope));
|
|
252
|
+
}
|
|
253
|
+
return namesOf(node.subject, where, scope);
|
|
254
|
+
}
|
|
255
|
+
function walkVerify(node, scope) {
|
|
256
|
+
const subjects = subjectNames(node, scope);
|
|
257
|
+
for (const witness of node.witnesses ?? []) {
|
|
258
|
+
namesOf(witness, `witnesses of verify node '${node.id}'`, scope);
|
|
259
|
+
}
|
|
260
|
+
walkEntries(node.act, node.id, {
|
|
261
|
+
...scope,
|
|
262
|
+
enclosing: subjects,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
function walkInParallel(node, scope) {
|
|
266
|
+
const over = node.over;
|
|
267
|
+
if (Array.isArray(over)) {
|
|
268
|
+
for (const name of over) {
|
|
269
|
+
namesOf(name, `over of inParallel node '${node.id}'`, scope);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const inner = Array.isArray(over)
|
|
273
|
+
? {
|
|
274
|
+
...scope,
|
|
275
|
+
fanOut: over,
|
|
276
|
+
}
|
|
277
|
+
: scope;
|
|
278
|
+
for (const child of node.each
|
|
279
|
+
? [
|
|
280
|
+
node.each,
|
|
281
|
+
]
|
|
282
|
+
: (node.paths ?? [])) {
|
|
283
|
+
walkNode(child, inner);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/** Walks every node that can hold a connection use; other kinds recurse through their children. */
|
|
287
|
+
function walkNode(node, scope) {
|
|
288
|
+
switch (node.kind) {
|
|
289
|
+
case 'call':
|
|
290
|
+
recordCall({
|
|
291
|
+
op: node.op,
|
|
292
|
+
args: node.args,
|
|
293
|
+
}, namesOf(node.connection, `call node '${node.id}'`, scope), scope);
|
|
294
|
+
return;
|
|
295
|
+
case 'wait':
|
|
296
|
+
walkWait(node, scope);
|
|
297
|
+
return;
|
|
298
|
+
case 'verify':
|
|
299
|
+
walkVerify(node, scope);
|
|
300
|
+
return;
|
|
301
|
+
case 'sequence':
|
|
302
|
+
walkEntries(node.steps, node.id, scope);
|
|
303
|
+
return;
|
|
304
|
+
case 'inParallel':
|
|
305
|
+
walkInParallel(node, scope);
|
|
306
|
+
return;
|
|
307
|
+
default:
|
|
308
|
+
for (const child of directChildren(node)) {
|
|
309
|
+
walkNode(child, scope);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/** Child nodes of the kinds `walkNode` does not handle itself. */
|
|
314
|
+
function directChildren(node) {
|
|
315
|
+
switch (node.kind) {
|
|
316
|
+
case 'conditional':
|
|
317
|
+
return [
|
|
318
|
+
...node.routes.map((r) => r.target),
|
|
319
|
+
...(node.default
|
|
320
|
+
? [
|
|
321
|
+
node.default,
|
|
322
|
+
]
|
|
323
|
+
: []),
|
|
324
|
+
];
|
|
325
|
+
case 'routeSwitch':
|
|
326
|
+
return [
|
|
327
|
+
...Object.values(node.cases),
|
|
328
|
+
node.uncertain,
|
|
329
|
+
];
|
|
330
|
+
case 'spawn':
|
|
331
|
+
case 'withContext':
|
|
332
|
+
return [
|
|
333
|
+
node.child,
|
|
334
|
+
];
|
|
335
|
+
case 'loop':
|
|
336
|
+
return [
|
|
337
|
+
node.body,
|
|
338
|
+
];
|
|
339
|
+
case 'schedule':
|
|
340
|
+
return [
|
|
341
|
+
node.step,
|
|
342
|
+
];
|
|
343
|
+
case 'subflow':
|
|
344
|
+
return node.document
|
|
345
|
+
? [
|
|
346
|
+
node.document.root,
|
|
347
|
+
]
|
|
348
|
+
: [];
|
|
349
|
+
default:
|
|
350
|
+
return [];
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
//#endregion
|
|
354
|
+
//#region Public API
|
|
355
|
+
/**
|
|
356
|
+
* Works out what a document uses of each manifest entry — the operations and
|
|
357
|
+
* events its nodes use and the secrets it passes — checking every connection a
|
|
358
|
+
* node names against the manifest. An inline subflow's nodes count against the
|
|
359
|
+
* enclosing document's manifest.
|
|
360
|
+
*
|
|
361
|
+
* @throws `NoeticConfigError` `CONNECTION_UNKNOWN` for a connection the manifest does not declare, `CONNECTION_INVALID` for a node whose connection is neither named nor implied.
|
|
362
|
+
*/
|
|
363
|
+
export function documentConnectionUses(doc) {
|
|
364
|
+
const scope = {
|
|
365
|
+
manifest: doc.connections ?? {},
|
|
366
|
+
enclosing: undefined,
|
|
367
|
+
fanOut: undefined,
|
|
368
|
+
uses: new Map(),
|
|
369
|
+
};
|
|
370
|
+
walkNode(doc.root, scope);
|
|
371
|
+
return scope.uses;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Checks a document's connection nodes against its manifest: every name is
|
|
375
|
+
* declared, and a source entry (one declaring only `events`) is waited on only
|
|
376
|
+
* for the events it lists and never called.
|
|
377
|
+
*
|
|
378
|
+
* @throws `NoeticConfigError` `CONNECTION_UNKNOWN`, `CONNECTION_INVALID`, or `CONNECTION_UNSATISFIED`-shaped `CONNECTION_INVALID` for a source used beyond its events.
|
|
379
|
+
*/
|
|
380
|
+
export function checkDocumentConnections(doc) {
|
|
381
|
+
const uses = documentConnectionUses(doc);
|
|
382
|
+
const manifest = doc.connections ?? {};
|
|
383
|
+
for (const [name, use] of uses) {
|
|
384
|
+
const entry = manifest[name];
|
|
385
|
+
if (!entry ||
|
|
386
|
+
entry.profile !== undefined ||
|
|
387
|
+
entry.extensions !== undefined ||
|
|
388
|
+
entry.events === undefined) {
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
const declared = new Set(entry.events);
|
|
392
|
+
const undeclared = [
|
|
393
|
+
...use.events,
|
|
394
|
+
].filter((event) => !declared.has(event));
|
|
395
|
+
if (undeclared.length > 0 || use.ops.size > 0) {
|
|
396
|
+
throw new NoeticConfigError({
|
|
397
|
+
code: 'CONNECTION_INVALID',
|
|
398
|
+
message: `Connection '${name}' declares only the events ${entry.events.join(', ')}, but the document ${use.ops.size > 0
|
|
399
|
+
? `calls ${[
|
|
400
|
+
...use.ops,
|
|
401
|
+
].join(', ')} on it`
|
|
402
|
+
: `waits for ${undeclared.join(', ')}`}.`,
|
|
403
|
+
hint: `List every event the document waits for in "events", or give '${name}' a "profile" whose operations it calls.`,
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return uses;
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* The requirement a manifest entry negotiates: its profile and extension ranges,
|
|
411
|
+
* plus the operations and events the document uses of it.
|
|
412
|
+
*/
|
|
413
|
+
export function manifestRequirement(entry, use) {
|
|
414
|
+
const profile = entry.profile === undefined ? undefined : parseProfileRequirement(entry.profile);
|
|
415
|
+
const events = new Set([
|
|
416
|
+
...(entry.events ?? []),
|
|
417
|
+
...(use?.events ?? []),
|
|
418
|
+
]);
|
|
419
|
+
const operations = [
|
|
420
|
+
...(use?.ops ?? []),
|
|
421
|
+
];
|
|
422
|
+
return {
|
|
423
|
+
...(profile
|
|
424
|
+
? {
|
|
425
|
+
profile: {
|
|
426
|
+
name: profile.profile,
|
|
427
|
+
range: profile.range,
|
|
428
|
+
},
|
|
429
|
+
}
|
|
430
|
+
: {}),
|
|
431
|
+
...(entry.extensions
|
|
432
|
+
? {
|
|
433
|
+
extensions: entry.extensions,
|
|
434
|
+
}
|
|
435
|
+
: {}),
|
|
436
|
+
...(operations.length > 0
|
|
437
|
+
? {
|
|
438
|
+
operations,
|
|
439
|
+
}
|
|
440
|
+
: {}),
|
|
441
|
+
...(events.size > 0
|
|
442
|
+
? {
|
|
443
|
+
events: [
|
|
444
|
+
...events,
|
|
445
|
+
],
|
|
446
|
+
}
|
|
447
|
+
: {}),
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* A handle for a manifest entry, carrying the entry's requirement in place of a
|
|
452
|
+
* profile object (core never imports profile packages).
|
|
453
|
+
*/
|
|
454
|
+
export function documentHandle(name, entry, use) {
|
|
455
|
+
const handle = {
|
|
456
|
+
kind: 'connection-handle',
|
|
457
|
+
name,
|
|
458
|
+
profile: undefined,
|
|
459
|
+
options: {
|
|
460
|
+
...(entry.from
|
|
461
|
+
? {
|
|
462
|
+
from: entry.from,
|
|
463
|
+
}
|
|
464
|
+
: {}),
|
|
465
|
+
...(entry.fresh
|
|
466
|
+
? {
|
|
467
|
+
fresh: entry.fresh,
|
|
468
|
+
}
|
|
469
|
+
: {}),
|
|
470
|
+
...(entry.setup
|
|
471
|
+
? {
|
|
472
|
+
setup: entry.setup,
|
|
473
|
+
}
|
|
474
|
+
: {}),
|
|
475
|
+
},
|
|
476
|
+
_requirement: manifestRequirement(entry, use),
|
|
477
|
+
};
|
|
478
|
+
return handle;
|
|
479
|
+
}
|
|
480
|
+
/** The requirement a document handle carries, when it is one. */
|
|
481
|
+
export function documentRequirementOf(handle) {
|
|
482
|
+
return frameworkCast(handle)._requirement;
|
|
483
|
+
}
|
|
484
|
+
//#endregion
|
|
485
|
+
//#region Allowlist
|
|
486
|
+
/** `*` matches any run of characters; everything else literally. */
|
|
487
|
+
export function globMatches(pattern, value) {
|
|
488
|
+
const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*');
|
|
489
|
+
return new RegExp(`^${escaped}$`).test(value);
|
|
490
|
+
}
|
|
491
|
+
function anyGlob(patterns, value) {
|
|
492
|
+
return (patterns ?? []).some((p) => globMatches(p, value));
|
|
493
|
+
}
|
|
494
|
+
function checkSetup(name, setup, allowed) {
|
|
495
|
+
for (const [field, value] of Object.entries(setup ?? {})) {
|
|
496
|
+
const patterns = allowed.setup?.[field];
|
|
497
|
+
const text = typeof value === 'string' ? value : JSON.stringify(value);
|
|
498
|
+
if (patterns && anyGlob(patterns, text)) {
|
|
499
|
+
continue;
|
|
500
|
+
}
|
|
501
|
+
throw notAllowed(`Connection '${name}' sets setup field '${field}' to ${JSON.stringify(value)}, which the host does not allow.`, patterns
|
|
502
|
+
? `Use a value matching ${patterns.join(', ')}.`
|
|
503
|
+
: `Leave '${field}' to the host, or ask the host to allow it in the connection's allowlist "setup".`);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
function checkUse(name, use, allowed) {
|
|
507
|
+
for (const op of use.ops) {
|
|
508
|
+
if (!anyGlob(allowed.ops, op)) {
|
|
509
|
+
throw notAllowed(`The document calls '${op}' on '${name}', which the host does not allow.`, `Allowed operations: ${(allowed.ops ?? []).join(', ') || '(none)'}.`);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
for (const event of use.events) {
|
|
513
|
+
if (!anyGlob(allowed.events, event)) {
|
|
514
|
+
throw notAllowed(`The document waits for '${event}' on '${name}', which the host does not allow.`, `Allowed events: ${(allowed.events ?? []).join(', ') || '(none)'}.`);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
for (const secret of use.secrets) {
|
|
518
|
+
const binding = `${secret.op}.${secret.field}`;
|
|
519
|
+
if (allowed.secrets?.[secret.name] === binding) {
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
throw notAllowed(`The document uses secret '${secret.name}' in ${binding} on '${name}', which the host does not allow.`, allowed.secrets?.[secret.name]
|
|
523
|
+
? `Secret '${secret.name}' may fill only ${allowed.secrets[secret.name]}.`
|
|
524
|
+
: `The host allows no secret named '${secret.name}' on '${name}'.`);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Checks a document against the host's allowlist (spec 35 § What a document may
|
|
529
|
+
* use). Each manifest entry binds to the harness connection `from ?? name`, which
|
|
530
|
+
* the allowlist must name; the operations, events, setup values and secrets the
|
|
531
|
+
* document uses of it must all be granted.
|
|
532
|
+
*
|
|
533
|
+
* @throws `NoeticConfigError` `CONNECTION_NOT_ALLOWED` when the document asks for more than the allowlist grants (or has connections and there is no allowlist); `CONNECTION_UNKNOWN` / `CONNECTION_INVALID` from `checkDocumentConnections`.
|
|
534
|
+
*/
|
|
535
|
+
export function checkConnectionAllowlist(doc, allowlist) {
|
|
536
|
+
const manifest = doc.connections ?? {};
|
|
537
|
+
const uses = checkDocumentConnections(doc);
|
|
538
|
+
const names = Object.keys(manifest);
|
|
539
|
+
if (names.length === 0) {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
if (!allowlist) {
|
|
543
|
+
throw notAllowed(`The document asks for connections (${names.join(', ')}), but the host allows none.`, 'Pass an allowlist, e.g. dynamicWorkflow({ connections: { browsers: { ops: ["ui.*"] } } }).');
|
|
544
|
+
}
|
|
545
|
+
for (const name of names) {
|
|
546
|
+
const entry = manifest[name];
|
|
547
|
+
const target = entry?.from ?? name;
|
|
548
|
+
const allowed = allowlist[target];
|
|
549
|
+
if (!entry || !allowed) {
|
|
550
|
+
throw notAllowed(`The document asks for connection '${target}', which the host does not allow.`, `Allowed connections: ${Object.keys(allowlist).join(', ') || '(none)'}.`);
|
|
551
|
+
}
|
|
552
|
+
checkSetup(name, entry.setup, allowed);
|
|
553
|
+
checkUse(name, uses.get(name) ?? {
|
|
554
|
+
ops: new Set(),
|
|
555
|
+
events: new Set(),
|
|
556
|
+
secrets: [],
|
|
557
|
+
}, allowed);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
//#endregion
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hydrators for the control-flow nodes of a JSON workflow: `conditional`,
|
|
3
|
+
* `decide`, `routeSwitch`, `loop` (with its until predicates), and `schedule`.
|
|
4
|
+
*/
|
|
5
|
+
import type { ContextData } from '@noetic-tools/context';
|
|
6
|
+
import type { Step } from '@noetic-tools/types';
|
|
7
|
+
import type { WorkflowNode } from '../schemas/workflow.js';
|
|
8
|
+
import type { HydrationContext } from './workflow-hydrator.js';
|
|
9
|
+
export declare function hydrateConditionalNode(node: WorkflowNode, ctx: HydrationContext): Step<ContextData, string, string>;
|
|
10
|
+
export declare function hydrateDecideNode(node: WorkflowNode, _ctx: HydrationContext): Step<ContextData, string, string>;
|
|
11
|
+
export declare function hydrateRouteSwitchNode(node: WorkflowNode, ctx: HydrationContext): Step<ContextData, string, string>;
|
|
12
|
+
export declare function hydrateLoopNode(node: WorkflowNode, ctx: HydrationContext): Step<ContextData, string, string>;
|
|
13
|
+
export declare function hydrateScheduleNode(node: WorkflowNode, ctx: HydrationContext): Step<ContextData, string, string>;
|