@noetic-tools/core 5.19.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/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 +21 -549
- package/dist/harness/model-request.d.ts +42 -0
- package/dist/harness/model-request.js +159 -0
- 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 +31 -1
- package/dist/runtime/durable/step-ledger.js +87 -1
- 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 +3 -3
- package/schema/noetic-workflow.schema.json +1170 -92
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model tools (spec 35 § Model tools). How a connection's operations reach the
|
|
3
|
+
* model is its `expose` setting:
|
|
4
|
+
*
|
|
5
|
+
* - `'eager'` (default): the operations its profile marks `eager`, plus
|
|
6
|
+
* `<name>_ops` (lists the rest) and `<name>_call` (runs one, or reads an
|
|
7
|
+
* `'ask'` read). A connection with no profile has no eager set, so its tools
|
|
8
|
+
* are exposed as they are.
|
|
9
|
+
* - `'all'`: every operation as its own tool.
|
|
10
|
+
* - `'none'`: none; the connection is for steps and triggers only.
|
|
11
|
+
*
|
|
12
|
+
* Tools are named `<name>_<op>` (dots become underscores). When two
|
|
13
|
+
* connections offer the same profile operation, the model gets one tool named
|
|
14
|
+
* after the operation (`ui_tap`) with a `target` argument naming the
|
|
15
|
+
* connection. Two operations that would still share a name are a
|
|
16
|
+
* `CONNECTION_TOOL_COLLISION`. Every call passes the same gates as a step.
|
|
17
|
+
*
|
|
18
|
+
* `connectionTools(handle, { ops })` builds tools for one handle, for a
|
|
19
|
+
* `callModel` step that narrows what it offers.
|
|
20
|
+
*/
|
|
21
|
+
import { frameworkCast } from '@noetic-tools/types';
|
|
22
|
+
import { z } from 'zod';
|
|
23
|
+
import { harnessInternal } from '../util/harness-internals.js';
|
|
24
|
+
import { toolIdempotencyKey } from './durability.js';
|
|
25
|
+
import { deniedError, toolCollisionError } from './errors.js';
|
|
26
|
+
import { matchesAnyGlob } from './glob.js';
|
|
27
|
+
import { handleTarget } from './handle.js';
|
|
28
|
+
import { requirementFor } from './negotiation.js';
|
|
29
|
+
import { refConnections } from './refs.js';
|
|
30
|
+
import { isPlainRecord } from './values.js';
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region Helpers
|
|
33
|
+
/** Accepts any JSON object; the gates validate against the operation's own schema. */
|
|
34
|
+
const ANY_OBJECT_INPUT = {
|
|
35
|
+
'~standard': {
|
|
36
|
+
version: 1,
|
|
37
|
+
vendor: 'noetic-connections',
|
|
38
|
+
validate: (value) => isPlainRecord(value)
|
|
39
|
+
? {
|
|
40
|
+
value,
|
|
41
|
+
}
|
|
42
|
+
: {
|
|
43
|
+
issues: [
|
|
44
|
+
{
|
|
45
|
+
message: 'expected an object of arguments',
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
const ANY_OUTPUT = z.unknown();
|
|
52
|
+
function sanitize(name) {
|
|
53
|
+
return name.replace(/[^A-Za-z0-9_-]/g, '_');
|
|
54
|
+
}
|
|
55
|
+
/** `<connection>_<op>` with dots and other characters a tool name cannot carry as `_`. */
|
|
56
|
+
export function connectionToolName(connection, op) {
|
|
57
|
+
return `${sanitize(connection)}_${sanitize(op)}`;
|
|
58
|
+
}
|
|
59
|
+
function toolContext(toolCtx) {
|
|
60
|
+
return frameworkCast(toolCtx);
|
|
61
|
+
}
|
|
62
|
+
/** The registry a harness exposes, if it has one. */
|
|
63
|
+
export function runtimeOf(harness) {
|
|
64
|
+
return harnessInternal(harness, 'connectionRegistry');
|
|
65
|
+
}
|
|
66
|
+
/** FNV-1a over `text`, as 8 hex digits: a short, stable digest. */
|
|
67
|
+
function digest(text) {
|
|
68
|
+
let hash = 0x811c9dc5;
|
|
69
|
+
for (let i = 0; i < text.length; i++) {
|
|
70
|
+
hash ^= text.charCodeAt(i);
|
|
71
|
+
hash = Math.imul(hash, 0x01000193);
|
|
72
|
+
}
|
|
73
|
+
return (hash >>> 0).toString(16).padStart(8, '0');
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The tool call's id, or — when the pipeline gave none — one derived from the
|
|
77
|
+
* operation and its arguments, so the idempotency key is stable across a resume.
|
|
78
|
+
*/
|
|
79
|
+
function callIdOf(toolCtx, op, args) {
|
|
80
|
+
const call = toolCtx.turnContext?.toolCall;
|
|
81
|
+
return call?.callId ?? call?.id ?? `derived-${digest(`${op}\u0000${JSON.stringify(args)}`)}`;
|
|
82
|
+
}
|
|
83
|
+
/** The run's abort signal: cancelling the run cancels the call and any long operation it waits on. */
|
|
84
|
+
function signalOf(ctx) {
|
|
85
|
+
return (frameworkCast(ctx).abortSignal ?? new AbortController().signal);
|
|
86
|
+
}
|
|
87
|
+
function scopeOf(ctx) {
|
|
88
|
+
return {
|
|
89
|
+
executionId: ctx.id,
|
|
90
|
+
threadId: ctx.threadId,
|
|
91
|
+
scopeKey: ctx.id,
|
|
92
|
+
signal: signalOf(ctx),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
async function callThrough(runtime, request) {
|
|
96
|
+
const ctx = request.toolCtx.ctx;
|
|
97
|
+
const scope = scopeOf(ctx);
|
|
98
|
+
const resolved = await runtime.resolve({
|
|
99
|
+
...request,
|
|
100
|
+
scope,
|
|
101
|
+
ctx,
|
|
102
|
+
});
|
|
103
|
+
const toolCallId = callIdOf(request.toolCtx, request.op, request.args);
|
|
104
|
+
const output = await resolved.call(request.op, request.args, {
|
|
105
|
+
signal: scope.signal,
|
|
106
|
+
caller: 'model',
|
|
107
|
+
idempotencyKey: toolIdempotencyKey(ctx.id, toolCallId),
|
|
108
|
+
toolCallId,
|
|
109
|
+
ctx,
|
|
110
|
+
});
|
|
111
|
+
// The model never sees a sensitive blob (spec 35 § Refs and blobs).
|
|
112
|
+
return runtime.withholdSensitive(output);
|
|
113
|
+
}
|
|
114
|
+
function withTarget(schema, targets) {
|
|
115
|
+
const properties = isPlainRecord(schema.properties) ? schema.properties : {};
|
|
116
|
+
return {
|
|
117
|
+
...schema,
|
|
118
|
+
type: 'object',
|
|
119
|
+
properties: {
|
|
120
|
+
...properties,
|
|
121
|
+
target: {
|
|
122
|
+
type: 'string',
|
|
123
|
+
enum: [
|
|
124
|
+
...targets,
|
|
125
|
+
],
|
|
126
|
+
description: 'The connection to run it on.',
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function withoutTarget(args) {
|
|
132
|
+
const { target: _target, ...rest } = args;
|
|
133
|
+
return rest;
|
|
134
|
+
}
|
|
135
|
+
function pickTarget(args, targets) {
|
|
136
|
+
const target = args.target;
|
|
137
|
+
if (typeof target === 'string' && targets.includes(target)) {
|
|
138
|
+
return target;
|
|
139
|
+
}
|
|
140
|
+
const byRef = [
|
|
141
|
+
...refConnections(args),
|
|
142
|
+
].filter((name) => targets.includes(name));
|
|
143
|
+
const routed = byRef.length === 1 ? byRef[0] : undefined;
|
|
144
|
+
if (routed) {
|
|
145
|
+
return routed;
|
|
146
|
+
}
|
|
147
|
+
throw deniedError(`pass target: one of ${targets.join(', ')}`);
|
|
148
|
+
}
|
|
149
|
+
function directTool(runtime, exposure) {
|
|
150
|
+
return {
|
|
151
|
+
name: connectionToolName(exposure.connection, exposure.op),
|
|
152
|
+
description: exposure.info.description ?? `${exposure.op} on ${exposure.connection}`,
|
|
153
|
+
input: ANY_OBJECT_INPUT,
|
|
154
|
+
inputJsonSchema: {
|
|
155
|
+
...exposure.info.inputSchema,
|
|
156
|
+
},
|
|
157
|
+
output: ANY_OUTPUT,
|
|
158
|
+
execute: (args, toolCtx) => callThrough(runtime, {
|
|
159
|
+
name: exposure.connection,
|
|
160
|
+
op: exposure.op,
|
|
161
|
+
args,
|
|
162
|
+
toolCtx: toolContext(toolCtx),
|
|
163
|
+
}),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function mergedTool(runtime, group) {
|
|
167
|
+
const first = group[0];
|
|
168
|
+
if (!first) {
|
|
169
|
+
throw deniedError('internal: empty tool group');
|
|
170
|
+
}
|
|
171
|
+
const targets = group.map((e) => e.connection);
|
|
172
|
+
return {
|
|
173
|
+
name: sanitize(first.op),
|
|
174
|
+
description: `${first.info.description ?? first.op} (on ${targets.join(' or ')}; pass target)`,
|
|
175
|
+
input: ANY_OBJECT_INPUT,
|
|
176
|
+
inputJsonSchema: withTarget(first.info.inputSchema, targets),
|
|
177
|
+
output: ANY_OUTPUT,
|
|
178
|
+
execute: (args, toolCtx) => callThrough(runtime, {
|
|
179
|
+
name: pickTarget(args, targets),
|
|
180
|
+
op: first.op,
|
|
181
|
+
args: withoutTarget(args),
|
|
182
|
+
toolCtx: toolContext(toolCtx),
|
|
183
|
+
}),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function opsTool(runtime, name, hidden) {
|
|
187
|
+
return {
|
|
188
|
+
name: `${sanitize(name)}_ops`,
|
|
189
|
+
description: `List the operations of ${name} that are not tools of their own, and the state you can read with ${sanitize(name)}_call({ read }).`,
|
|
190
|
+
input: ANY_OBJECT_INPUT,
|
|
191
|
+
inputJsonSchema: {
|
|
192
|
+
type: 'object',
|
|
193
|
+
properties: {
|
|
194
|
+
filter: {
|
|
195
|
+
type: 'string',
|
|
196
|
+
description: "A glob over operation names, e.g. 'ui.*'.",
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
output: ANY_OUTPUT,
|
|
201
|
+
execute: async (args) => {
|
|
202
|
+
const manifest = runtime.current(name)?.manifest;
|
|
203
|
+
const filter = typeof args.filter === 'string' ? args.filter : '*';
|
|
204
|
+
const operations = hidden
|
|
205
|
+
.filter((op) => matchesAnyGlob([
|
|
206
|
+
filter,
|
|
207
|
+
], op))
|
|
208
|
+
.map((op) => manifest?.operations[op])
|
|
209
|
+
.filter((info) => info !== undefined)
|
|
210
|
+
.map((info) => ({
|
|
211
|
+
op: info.name,
|
|
212
|
+
description: info.description,
|
|
213
|
+
inputSchema: info.inputSchema,
|
|
214
|
+
}));
|
|
215
|
+
const look = runtime.config(name)?.look ?? {};
|
|
216
|
+
const reads = Object.values(manifest?.resources ?? {})
|
|
217
|
+
.filter((r) => (look[r.name] ?? r.look ?? 'ask') === 'ask')
|
|
218
|
+
.map((r) => ({
|
|
219
|
+
read: r.name,
|
|
220
|
+
description: r.description,
|
|
221
|
+
}));
|
|
222
|
+
return {
|
|
223
|
+
operations,
|
|
224
|
+
reads,
|
|
225
|
+
};
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function callTool(runtime, name, allowed) {
|
|
230
|
+
return {
|
|
231
|
+
name: `${sanitize(name)}_call`,
|
|
232
|
+
description: `Run one operation of ${name} listed by ${sanitize(name)}_ops ({ op, args }), or read its state ({ read }).`,
|
|
233
|
+
input: ANY_OBJECT_INPUT,
|
|
234
|
+
inputJsonSchema: {
|
|
235
|
+
type: 'object',
|
|
236
|
+
properties: {
|
|
237
|
+
op: {
|
|
238
|
+
type: 'string',
|
|
239
|
+
},
|
|
240
|
+
args: {
|
|
241
|
+
type: 'object',
|
|
242
|
+
},
|
|
243
|
+
read: {
|
|
244
|
+
type: 'string',
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
output: ANY_OUTPUT,
|
|
249
|
+
execute: async (args, toolCtx) => {
|
|
250
|
+
const tc = toolContext(toolCtx);
|
|
251
|
+
if (typeof args.read === 'string') {
|
|
252
|
+
const resolved = await runtime.resolve({
|
|
253
|
+
name,
|
|
254
|
+
scope: scopeOf(tc.ctx),
|
|
255
|
+
ctx: tc.ctx,
|
|
256
|
+
});
|
|
257
|
+
const value = await resolved.read(args.read, {
|
|
258
|
+
signal: signalOf(tc.ctx),
|
|
259
|
+
});
|
|
260
|
+
return runtime.withholdSensitive(value);
|
|
261
|
+
}
|
|
262
|
+
const op = typeof args.op === 'string' ? args.op : '';
|
|
263
|
+
if (!allowed.includes(op)) {
|
|
264
|
+
throw deniedError(`'${op}' is not an operation ${sanitize(name)}_call can run`);
|
|
265
|
+
}
|
|
266
|
+
return callThrough(runtime, {
|
|
267
|
+
name,
|
|
268
|
+
op,
|
|
269
|
+
args: args.args ?? {},
|
|
270
|
+
toolCtx: tc,
|
|
271
|
+
});
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
function exposuresFor(runtime, name) {
|
|
276
|
+
const config = runtime.config(name);
|
|
277
|
+
const connection = runtime.current(name);
|
|
278
|
+
const expose = config?.expose ?? 'eager';
|
|
279
|
+
if (!connection || config?.pool || expose === 'none') {
|
|
280
|
+
return {
|
|
281
|
+
direct: [],
|
|
282
|
+
hidden: [],
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
const manifest = connection.manifest;
|
|
286
|
+
const hasProfile = Object.keys(manifest.profiles).length > 0;
|
|
287
|
+
const direct = [];
|
|
288
|
+
const hidden = [];
|
|
289
|
+
for (const [op, info] of Object.entries(manifest.operations)) {
|
|
290
|
+
const own = expose === 'all' || !hasProfile || info.eager === true;
|
|
291
|
+
if (own) {
|
|
292
|
+
direct.push({
|
|
293
|
+
connection: name,
|
|
294
|
+
op,
|
|
295
|
+
info,
|
|
296
|
+
});
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
hidden.push(op);
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
direct,
|
|
303
|
+
hidden,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
/** Tools by name, refusing a second tool under a name already taken. */
|
|
307
|
+
class ToolSet {
|
|
308
|
+
tools = new Map();
|
|
309
|
+
owners = new Map();
|
|
310
|
+
add(tool, owner) {
|
|
311
|
+
const existing = this.owners.get(tool.name);
|
|
312
|
+
if (existing !== undefined) {
|
|
313
|
+
throw toolCollisionError(tool.name, existing, owner);
|
|
314
|
+
}
|
|
315
|
+
this.owners.set(tool.name, owner);
|
|
316
|
+
this.tools.set(tool.name, tool);
|
|
317
|
+
}
|
|
318
|
+
list() {
|
|
319
|
+
return [
|
|
320
|
+
...this.tools.values(),
|
|
321
|
+
];
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
function needsGenericTools(runtime, name, hidden) {
|
|
325
|
+
const config = runtime.config(name);
|
|
326
|
+
if (config?.pool || config?.expose === 'none') {
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
if (hidden.length > 0) {
|
|
330
|
+
return true;
|
|
331
|
+
}
|
|
332
|
+
const manifest = runtime.current(name)?.manifest;
|
|
333
|
+
const look = runtime.config(name)?.look ?? {};
|
|
334
|
+
return Object.values(manifest?.resources ?? {}).some((r) => (look[r.name] ?? r.look ?? 'ask') === 'ask');
|
|
335
|
+
}
|
|
336
|
+
//#endregion
|
|
337
|
+
//#region Public API
|
|
338
|
+
/**
|
|
339
|
+
* The model tools for every ready connection in `runtime`, built fresh each
|
|
340
|
+
* turn so a renegotiated connection's tools change at the next turn, never
|
|
341
|
+
* mid-turn.
|
|
342
|
+
*
|
|
343
|
+
* @throws NoeticConfigError `CONNECTION_TOOL_COLLISION` when two operations would share a tool name.
|
|
344
|
+
*/
|
|
345
|
+
export function buildConnectionTools(runtime) {
|
|
346
|
+
const tools = new ToolSet();
|
|
347
|
+
const byProfileOp = new Map();
|
|
348
|
+
const plain = [];
|
|
349
|
+
for (const name of runtime.names()) {
|
|
350
|
+
const { direct, hidden } = exposuresFor(runtime, name);
|
|
351
|
+
for (const exposure of direct) {
|
|
352
|
+
if (!exposure.info.profile && !exposure.info.extension) {
|
|
353
|
+
plain.push(exposure);
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
const group = byProfileOp.get(exposure.op) ?? [];
|
|
357
|
+
group.push(exposure);
|
|
358
|
+
byProfileOp.set(exposure.op, group);
|
|
359
|
+
}
|
|
360
|
+
if (needsGenericTools(runtime, name, hidden)) {
|
|
361
|
+
tools.add(opsTool(runtime, name, hidden), `${name} (ops)`);
|
|
362
|
+
tools.add(callTool(runtime, name, hidden), `${name} (call)`);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
for (const [op, group] of byProfileOp) {
|
|
366
|
+
if (group.length > 1) {
|
|
367
|
+
tools.add(mergedTool(runtime, group), `'${op}' on ${group.map((g) => g.connection).join(', ')}`);
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
plain.push(...group);
|
|
371
|
+
}
|
|
372
|
+
for (const exposure of plain) {
|
|
373
|
+
tools.add(directTool(runtime, exposure), `'${exposure.op}' on ${exposure.connection}`);
|
|
374
|
+
}
|
|
375
|
+
return tools.list();
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* @public
|
|
379
|
+
* Tools for one connection handle, for a `callModel` step that narrows what it
|
|
380
|
+
* offers: one tool per profile operation matching `ops`, or — for a handle with
|
|
381
|
+
* no profile — one `<name>_call` tool limited to `ops`.
|
|
382
|
+
*/
|
|
383
|
+
export function connectionTools(handle, options = {}) {
|
|
384
|
+
const patterns = options.ops ?? [
|
|
385
|
+
'*',
|
|
386
|
+
];
|
|
387
|
+
const target = handleTarget(handle);
|
|
388
|
+
const profile = handle.profile;
|
|
389
|
+
const resolveRuntime = (toolCtx) => {
|
|
390
|
+
const runtime = runtimeOf(toolCtx.harness);
|
|
391
|
+
if (!runtime) {
|
|
392
|
+
throw deniedError('this harness has no connections');
|
|
393
|
+
}
|
|
394
|
+
return runtime;
|
|
395
|
+
};
|
|
396
|
+
if (!profile) {
|
|
397
|
+
const generic = {
|
|
398
|
+
name: `${sanitize(handle.name)}_call`,
|
|
399
|
+
description: `Run an operation of ${handle.name}: { op, args }.`,
|
|
400
|
+
input: ANY_OBJECT_INPUT,
|
|
401
|
+
inputJsonSchema: {
|
|
402
|
+
type: 'object',
|
|
403
|
+
properties: {
|
|
404
|
+
op: {
|
|
405
|
+
type: 'string',
|
|
406
|
+
},
|
|
407
|
+
args: {
|
|
408
|
+
type: 'object',
|
|
409
|
+
},
|
|
410
|
+
},
|
|
411
|
+
required: [
|
|
412
|
+
'op',
|
|
413
|
+
],
|
|
414
|
+
},
|
|
415
|
+
output: ANY_OUTPUT,
|
|
416
|
+
execute: (args, toolCtx) => {
|
|
417
|
+
const tc = toolContext(toolCtx);
|
|
418
|
+
const op = typeof args.op === 'string' ? args.op : '';
|
|
419
|
+
if (!matchesAnyGlob(patterns, op)) {
|
|
420
|
+
throw deniedError(`'${op}' is not offered to this step`);
|
|
421
|
+
}
|
|
422
|
+
return callThrough(resolveRuntime(tc), {
|
|
423
|
+
name: target,
|
|
424
|
+
op,
|
|
425
|
+
args: args.args ?? {},
|
|
426
|
+
toolCtx: tc,
|
|
427
|
+
requirement: requirementFor(handle),
|
|
428
|
+
fresh: handle.options.fresh,
|
|
429
|
+
setup: handle.options.setup,
|
|
430
|
+
});
|
|
431
|
+
},
|
|
432
|
+
};
|
|
433
|
+
return [
|
|
434
|
+
generic,
|
|
435
|
+
];
|
|
436
|
+
}
|
|
437
|
+
return Object.entries(profile.operations)
|
|
438
|
+
.filter(([op]) => matchesAnyGlob(patterns, op))
|
|
439
|
+
.map(([op, def]) => ({
|
|
440
|
+
name: connectionToolName(handle.name, op),
|
|
441
|
+
description: def.description,
|
|
442
|
+
input: ANY_OBJECT_INPUT,
|
|
443
|
+
inputJsonSchema: z.toJSONSchema(def.input, {
|
|
444
|
+
unrepresentable: 'any',
|
|
445
|
+
}),
|
|
446
|
+
output: ANY_OUTPUT,
|
|
447
|
+
execute: (args, toolCtx) => {
|
|
448
|
+
const tc = toolContext(toolCtx);
|
|
449
|
+
return callThrough(resolveRuntime(tc), {
|
|
450
|
+
name: target,
|
|
451
|
+
op,
|
|
452
|
+
args,
|
|
453
|
+
toolCtx: tc,
|
|
454
|
+
profile,
|
|
455
|
+
requirement: requirementFor(handle, {
|
|
456
|
+
operations: [
|
|
457
|
+
op,
|
|
458
|
+
],
|
|
459
|
+
}),
|
|
460
|
+
fresh: handle.options.fresh,
|
|
461
|
+
setup: handle.options.setup,
|
|
462
|
+
});
|
|
463
|
+
},
|
|
464
|
+
}));
|
|
465
|
+
}
|
|
466
|
+
//#endregion
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Negotiation (spec 35 § Negotiation). A handle's or manifest entry's
|
|
3
|
+
* requirements are semver ranges for a profile — which imply its parents'
|
|
4
|
+
* ranges (spec 36) — and its extensions, plus the optional operations, events
|
|
5
|
+
* and senses derived from use. A profile's required parts are implied by its
|
|
6
|
+
* version; only the parts it marks `required: false`, and parts it does not
|
|
7
|
+
* define (extension operations), are listed explicitly.
|
|
8
|
+
*/
|
|
9
|
+
import type { ConnectionHandle, ConnectionManifest, ConnectionRequirement } from '@noetic-tools/types';
|
|
10
|
+
/** What a workflow uses of a connection, from its steps or nodes. */
|
|
11
|
+
export interface ConnectionUse {
|
|
12
|
+
readonly operations?: ReadonlyArray<string>;
|
|
13
|
+
readonly events?: ReadonlyArray<string>;
|
|
14
|
+
readonly senses?: ReadonlyArray<string>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Checks `manifest` offers what `requirement` asks. `parents` are the parent
|
|
18
|
+
* profile ranges the required profile implies (`profile.parents`).
|
|
19
|
+
*
|
|
20
|
+
* @throws ConnectionUnsatisfiedError (`CONNECTION_UNSATISFIED`) naming the first missing part.
|
|
21
|
+
*/
|
|
22
|
+
export declare function checkRequirement(name: string, manifest: ConnectionManifest, requirement: ConnectionRequirement & {
|
|
23
|
+
readonly parents?: Readonly<Record<string, string>>;
|
|
24
|
+
}): void;
|
|
25
|
+
/**
|
|
26
|
+
* The requirement a handle and its use add up to: the profile at the handle's
|
|
27
|
+
* range, its extensions, and the operations, events and senses used that the
|
|
28
|
+
* profile does not already require.
|
|
29
|
+
*/
|
|
30
|
+
export declare function requirementFor(handle: ConnectionHandle, use?: ConnectionUse): ConnectionRequirement;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Negotiation (spec 35 § Negotiation). A handle's or manifest entry's
|
|
3
|
+
* requirements are semver ranges for a profile — which imply its parents'
|
|
4
|
+
* ranges (spec 36) — and its extensions, plus the optional operations, events
|
|
5
|
+
* and senses derived from use. A profile's required parts are implied by its
|
|
6
|
+
* version; only the parts it marks `required: false`, and parts it does not
|
|
7
|
+
* define (extension operations), are listed explicitly.
|
|
8
|
+
*/
|
|
9
|
+
import { ConnectionUnsatisfiedError, satisfiesRange } from '@noetic-tools/types';
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region Helpers
|
|
12
|
+
function extensionRequirement(entry) {
|
|
13
|
+
if (entry.kind === 'extension') {
|
|
14
|
+
return [
|
|
15
|
+
entry.id,
|
|
16
|
+
entry.defaultRange,
|
|
17
|
+
];
|
|
18
|
+
}
|
|
19
|
+
return [
|
|
20
|
+
entry.extension.id,
|
|
21
|
+
entry.range,
|
|
22
|
+
];
|
|
23
|
+
}
|
|
24
|
+
function isOptionalOrForeign(parts, name) {
|
|
25
|
+
const part = parts[name];
|
|
26
|
+
return part === undefined || part.required === false;
|
|
27
|
+
}
|
|
28
|
+
function unique(values) {
|
|
29
|
+
return [
|
|
30
|
+
...new Set(values),
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
function checkProfiles(name, manifest, required) {
|
|
34
|
+
for (const [profile, range] of Object.entries(required)) {
|
|
35
|
+
const version = manifest.profiles[profile];
|
|
36
|
+
if (version !== undefined && satisfiesRange(version, range)) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
throw new ConnectionUnsatisfiedError({
|
|
40
|
+
connection: name,
|
|
41
|
+
missing: {
|
|
42
|
+
kind: 'profile',
|
|
43
|
+
profile,
|
|
44
|
+
range,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function checkExtensions(name, manifest, required) {
|
|
50
|
+
for (const [extension, range] of Object.entries(required)) {
|
|
51
|
+
const version = manifest.extensions[extension];
|
|
52
|
+
if (version !== undefined && satisfiesRange(version, range)) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
throw new ConnectionUnsatisfiedError({
|
|
56
|
+
connection: name,
|
|
57
|
+
missing: {
|
|
58
|
+
kind: 'extension',
|
|
59
|
+
extension,
|
|
60
|
+
range,
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function checkParts(name, manifest, requirement) {
|
|
66
|
+
for (const op of requirement.operations ?? []) {
|
|
67
|
+
if (!manifest.operations[op]) {
|
|
68
|
+
throw new ConnectionUnsatisfiedError({
|
|
69
|
+
connection: name,
|
|
70
|
+
missing: {
|
|
71
|
+
kind: 'operation',
|
|
72
|
+
op,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
for (const event of requirement.events ?? []) {
|
|
78
|
+
if (!manifest.events[event]) {
|
|
79
|
+
throw new ConnectionUnsatisfiedError({
|
|
80
|
+
connection: name,
|
|
81
|
+
missing: {
|
|
82
|
+
kind: 'event',
|
|
83
|
+
event,
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
for (const sense of requirement.senses ?? []) {
|
|
89
|
+
if (!manifest.senses[sense]) {
|
|
90
|
+
throw new ConnectionUnsatisfiedError({
|
|
91
|
+
connection: name,
|
|
92
|
+
missing: {
|
|
93
|
+
kind: 'sense',
|
|
94
|
+
sense,
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region Public API
|
|
102
|
+
/**
|
|
103
|
+
* Checks `manifest` offers what `requirement` asks. `parents` are the parent
|
|
104
|
+
* profile ranges the required profile implies (`profile.parents`).
|
|
105
|
+
*
|
|
106
|
+
* @throws ConnectionUnsatisfiedError (`CONNECTION_UNSATISFIED`) naming the first missing part.
|
|
107
|
+
*/
|
|
108
|
+
export function checkRequirement(name, manifest, requirement) {
|
|
109
|
+
const profiles = {};
|
|
110
|
+
if (requirement.profile) {
|
|
111
|
+
profiles[requirement.profile.name] = requirement.profile.range;
|
|
112
|
+
}
|
|
113
|
+
checkProfiles(name, manifest, profiles);
|
|
114
|
+
checkProfiles(name, manifest, requirement.parents ?? {});
|
|
115
|
+
checkExtensions(name, manifest, requirement.extensions ?? {});
|
|
116
|
+
checkParts(name, manifest, requirement);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The requirement a handle and its use add up to: the profile at the handle's
|
|
120
|
+
* range, its extensions, and the operations, events and senses used that the
|
|
121
|
+
* profile does not already require.
|
|
122
|
+
*/
|
|
123
|
+
export function requirementFor(handle, use = {}) {
|
|
124
|
+
const profile = handle.profile;
|
|
125
|
+
const extensions = Object.fromEntries((handle.options.extensions ?? []).map(extensionRequirement));
|
|
126
|
+
const operations = unique(use.operations ?? []).filter((op) => !profile || isOptionalOrForeign(profile.operations, op));
|
|
127
|
+
const events = unique(use.events ?? []).filter((event) => !profile || isOptionalOrForeign(profile.events, event));
|
|
128
|
+
return {
|
|
129
|
+
...(profile
|
|
130
|
+
? {
|
|
131
|
+
profile: {
|
|
132
|
+
name: profile.name,
|
|
133
|
+
range: handle.range ?? profile.defaultRange,
|
|
134
|
+
},
|
|
135
|
+
}
|
|
136
|
+
: {}),
|
|
137
|
+
...(Object.keys(extensions).length > 0
|
|
138
|
+
? {
|
|
139
|
+
extensions,
|
|
140
|
+
}
|
|
141
|
+
: {}),
|
|
142
|
+
...(operations.length > 0
|
|
143
|
+
? {
|
|
144
|
+
operations,
|
|
145
|
+
}
|
|
146
|
+
: {}),
|
|
147
|
+
...(events.length > 0
|
|
148
|
+
? {
|
|
149
|
+
events,
|
|
150
|
+
}
|
|
151
|
+
: {}),
|
|
152
|
+
...(use.senses && use.senses.length > 0
|
|
153
|
+
? {
|
|
154
|
+
senses: unique(use.senses),
|
|
155
|
+
}
|
|
156
|
+
: {}),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
//#endregion
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A connection's `policy` (spec 35 § Gates, gate 4): `allow`, `deny` and
|
|
3
|
+
* `approve` globs over operation names. `deny` wins; a non-empty `allow` list
|
|
4
|
+
* refuses everything it does not match; `approve` matches need approval.
|
|
5
|
+
* Operations a profile marks `guarded` need approval unless `allow` names
|
|
6
|
+
* them explicitly.
|
|
7
|
+
*/
|
|
8
|
+
import type { ConnectionPolicy } from '@noetic-tools/types';
|
|
9
|
+
/** What the policy says about one call. */
|
|
10
|
+
export type PolicyVerdict = {
|
|
11
|
+
readonly kind: 'deny';
|
|
12
|
+
readonly reason: string;
|
|
13
|
+
} | {
|
|
14
|
+
readonly kind: 'allow';
|
|
15
|
+
/** The call must be approved before it is sent. */
|
|
16
|
+
readonly needsApproval: boolean;
|
|
17
|
+
/** `allow` names the operation, so a server's `guarded` refusal needs no approval. */
|
|
18
|
+
readonly explicitlyAllowed: boolean;
|
|
19
|
+
};
|
|
20
|
+
/** Applies `policy` to operation `op` (`guarded` from the operation's info). */
|
|
21
|
+
export declare function evaluatePolicy(policy: ConnectionPolicy | undefined, op: string, guarded: boolean): PolicyVerdict;
|