@noetic-tools/core 5.19.1 → 5.19.3
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 +54 -2
- package/dist/builders/dynamic-workflow.js +247 -12
- package/dist/builders/every.d.ts +3 -3
- package/dist/builders/every.js +2 -1
- package/dist/builders/mcp-url-pattern.d.ts +13 -0
- package/dist/builders/mcp-url-pattern.js +85 -0
- 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-acp-grants.d.ts +42 -0
- package/dist/builders/workflow-acp-grants.js +224 -0
- package/dist/builders/workflow-child-nodes.d.ts +21 -0
- package/dist/builders/workflow-child-nodes.js +336 -0
- package/dist/builders/workflow-connection-nodes.d.ts +25 -0
- package/dist/builders/workflow-connection-nodes.js +198 -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 +56 -3
- package/dist/builders/workflow-hydrator.js +53 -520
- package/dist/builders/workflow-mcp-servers.d.ts +24 -0
- package/dist/builders/workflow-mcp-servers.js +220 -0
- 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 +170 -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 +33 -8
- 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 +25 -0
- package/dist/util/harness-internals.js +35 -0
- package/package.json +3 -3
- package/schema/noetic-workflow.schema.json +1309 -180
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `createInMemoryConnection()` (spec 35 § The provider contract): the whole
|
|
3
|
+
* provider contract as scripted functions, events and tasks, for tests. Given
|
|
4
|
+
* `provision`, it also backs pools.
|
|
5
|
+
*
|
|
6
|
+
* It behaves as a conforming server would: `guarded` operations refuse with
|
|
7
|
+
* `guarded` until the harness confirms them, `once` operations return their
|
|
8
|
+
* original outcome for a repeated idempotency key and refuse the same key with
|
|
9
|
+
* different arguments as `conflict`, and durable events replay from a cursor.
|
|
10
|
+
*/
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region Helpers
|
|
13
|
+
/** An error a scripted operation throws to refuse, carrying the wire payload. */
|
|
14
|
+
export class InMemoryRefusal extends Error {
|
|
15
|
+
payload;
|
|
16
|
+
constructor(payload) {
|
|
17
|
+
super(payload.message);
|
|
18
|
+
this.name = 'InMemoryRefusal';
|
|
19
|
+
this.payload = payload;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Builds an `InMemoryRefusal` for a scripted operation to throw. */
|
|
23
|
+
export function inMemoryRefusal(code, message, details) {
|
|
24
|
+
return new InMemoryRefusal({
|
|
25
|
+
code,
|
|
26
|
+
message,
|
|
27
|
+
...(details
|
|
28
|
+
? {
|
|
29
|
+
details,
|
|
30
|
+
}
|
|
31
|
+
: {}),
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
function errorResult(code, message) {
|
|
35
|
+
return {
|
|
36
|
+
status: 'error',
|
|
37
|
+
error: {
|
|
38
|
+
code,
|
|
39
|
+
message,
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function operationInfos(operations) {
|
|
44
|
+
const infos = {};
|
|
45
|
+
for (const [name, op] of Object.entries(operations)) {
|
|
46
|
+
const { run: _run, task: _task, inputSchema, ...rest } = op;
|
|
47
|
+
infos[name] = {
|
|
48
|
+
...rest,
|
|
49
|
+
name,
|
|
50
|
+
inputSchema: inputSchema ?? {
|
|
51
|
+
type: 'object',
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return infos;
|
|
56
|
+
}
|
|
57
|
+
function resourceInfos(resources) {
|
|
58
|
+
const infos = {};
|
|
59
|
+
for (const [name, resource] of Object.entries(resources)) {
|
|
60
|
+
const { read: _read, ...rest } = resource;
|
|
61
|
+
infos[name] = {
|
|
62
|
+
...rest,
|
|
63
|
+
name,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return infos;
|
|
67
|
+
}
|
|
68
|
+
function eventInfos(events) {
|
|
69
|
+
const infos = {};
|
|
70
|
+
for (const [name, event] of Object.entries(events)) {
|
|
71
|
+
const { matches: _matches, ...rest } = event;
|
|
72
|
+
infos[name] = {
|
|
73
|
+
...rest,
|
|
74
|
+
name,
|
|
75
|
+
replay: event.replay ?? 'durable',
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return infos;
|
|
79
|
+
}
|
|
80
|
+
function buildManifest(options) {
|
|
81
|
+
return {
|
|
82
|
+
profiles: options.profiles ?? {},
|
|
83
|
+
extensions: options.extensions ?? {},
|
|
84
|
+
operations: operationInfos(options.operations ?? {}),
|
|
85
|
+
resources: resourceInfos(options.resources ?? {}),
|
|
86
|
+
events: eventInfos(options.events ?? {}),
|
|
87
|
+
senses: options.senses ?? {},
|
|
88
|
+
...(options.determinism
|
|
89
|
+
? {
|
|
90
|
+
determinism: options.determinism,
|
|
91
|
+
}
|
|
92
|
+
: {}),
|
|
93
|
+
...(options.setup
|
|
94
|
+
? {
|
|
95
|
+
setup: options.setup,
|
|
96
|
+
}
|
|
97
|
+
: {}),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/** A listener-backed event stream: messages pushed to `listener` are iterated from `stream`. */
|
|
101
|
+
export function createListenerStream(listeners, request, ready) {
|
|
102
|
+
const queue = [];
|
|
103
|
+
const waiters = [];
|
|
104
|
+
let ended = false;
|
|
105
|
+
const finish = () => {
|
|
106
|
+
ended = true;
|
|
107
|
+
listeners.delete(listener);
|
|
108
|
+
for (const waiter of waiters.splice(0)) {
|
|
109
|
+
waiter({
|
|
110
|
+
value: undefined,
|
|
111
|
+
done: true,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const listener = {
|
|
116
|
+
event: request.event,
|
|
117
|
+
args: request.arguments ?? {},
|
|
118
|
+
push(message) {
|
|
119
|
+
if (ended) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const waiter = waiters.shift();
|
|
123
|
+
if (waiter) {
|
|
124
|
+
waiter({
|
|
125
|
+
value: message,
|
|
126
|
+
done: false,
|
|
127
|
+
});
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
queue.push(message);
|
|
131
|
+
},
|
|
132
|
+
end: finish,
|
|
133
|
+
};
|
|
134
|
+
request.signal.addEventListener('abort', finish, {
|
|
135
|
+
once: true,
|
|
136
|
+
});
|
|
137
|
+
const stream = {
|
|
138
|
+
ready: Promise.resolve(ready),
|
|
139
|
+
close: async () => finish(),
|
|
140
|
+
[Symbol.asyncIterator]() {
|
|
141
|
+
return {
|
|
142
|
+
next() {
|
|
143
|
+
const message = queue.shift();
|
|
144
|
+
if (message) {
|
|
145
|
+
return Promise.resolve({
|
|
146
|
+
value: message,
|
|
147
|
+
done: false,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
if (ended) {
|
|
151
|
+
return Promise.resolve({
|
|
152
|
+
value: undefined,
|
|
153
|
+
done: true,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return new Promise((resolve) => {
|
|
157
|
+
waiters.push(resolve);
|
|
158
|
+
});
|
|
159
|
+
},
|
|
160
|
+
async return() {
|
|
161
|
+
finish();
|
|
162
|
+
return {
|
|
163
|
+
value: undefined,
|
|
164
|
+
done: true,
|
|
165
|
+
};
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
return {
|
|
171
|
+
stream,
|
|
172
|
+
listener,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
function initialIdentity(options) {
|
|
176
|
+
return {
|
|
177
|
+
surface: options.surface ?? 'x-memory',
|
|
178
|
+
instance: options.instance ?? crypto.randomUUID(),
|
|
179
|
+
...(options.epoch !== undefined
|
|
180
|
+
? {
|
|
181
|
+
epoch: options.epoch,
|
|
182
|
+
}
|
|
183
|
+
: {}),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function createConnectionState(options) {
|
|
187
|
+
let resolveClosed = () => undefined;
|
|
188
|
+
const closed = new Promise((resolve) => {
|
|
189
|
+
resolveClosed = resolve;
|
|
190
|
+
});
|
|
191
|
+
return {
|
|
192
|
+
options,
|
|
193
|
+
operations: options.operations ?? {},
|
|
194
|
+
manifest: buildManifest(options),
|
|
195
|
+
identity: initialIdentity(options),
|
|
196
|
+
calls: [],
|
|
197
|
+
listeners: new Set(),
|
|
198
|
+
changeListeners: new Set(),
|
|
199
|
+
history: [],
|
|
200
|
+
onceResults: new Map(),
|
|
201
|
+
tasks: new Map(),
|
|
202
|
+
sequence: 0,
|
|
203
|
+
released: false,
|
|
204
|
+
closed,
|
|
205
|
+
resolveClosed,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function notifyChange(state, change) {
|
|
209
|
+
for (const listener of [
|
|
210
|
+
...state.changeListeners,
|
|
211
|
+
]) {
|
|
212
|
+
listener(change);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function setIdentity(state, identity) {
|
|
216
|
+
state.identity = identity;
|
|
217
|
+
notifyChange(state, 'identity');
|
|
218
|
+
return identity;
|
|
219
|
+
}
|
|
220
|
+
function applyManifestPatch(state, patch) {
|
|
221
|
+
if (patch.operations) {
|
|
222
|
+
state.operations = patch.operations;
|
|
223
|
+
}
|
|
224
|
+
state.manifest = {
|
|
225
|
+
...state.manifest,
|
|
226
|
+
...patch,
|
|
227
|
+
operations: operationInfos(state.operations),
|
|
228
|
+
};
|
|
229
|
+
notifyChange(state, 'manifest');
|
|
230
|
+
}
|
|
231
|
+
//#endregion
|
|
232
|
+
//#region Calls
|
|
233
|
+
async function runOperation(op, args, context) {
|
|
234
|
+
try {
|
|
235
|
+
const output = await op.run?.(args, context);
|
|
236
|
+
return {
|
|
237
|
+
status: 'ok',
|
|
238
|
+
output: output ?? null,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
catch (e) {
|
|
242
|
+
if (e instanceof InMemoryRefusal) {
|
|
243
|
+
return {
|
|
244
|
+
status: 'error',
|
|
245
|
+
error: e.payload,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
throw e;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
function startTask(state, task) {
|
|
252
|
+
const taskId = `task-${crypto.randomUUID().slice(0, 8)}`;
|
|
253
|
+
state.tasks.set(taskId, {
|
|
254
|
+
...task,
|
|
255
|
+
taskId,
|
|
256
|
+
answers: new Map(),
|
|
257
|
+
cancelled: false,
|
|
258
|
+
waiting: new Map(),
|
|
259
|
+
});
|
|
260
|
+
return {
|
|
261
|
+
status: 'task',
|
|
262
|
+
taskId,
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
async function callOperation(state, connection, record) {
|
|
266
|
+
state.calls.push(record);
|
|
267
|
+
const { op: opName, args, options: callOptions } = record;
|
|
268
|
+
const op = state.operations[opName];
|
|
269
|
+
if (!op) {
|
|
270
|
+
return errorResult('undeclared', `'${opName}' is not an operation of this connection`);
|
|
271
|
+
}
|
|
272
|
+
if (op.guarded && !callOptions.confirmed) {
|
|
273
|
+
return errorResult('guarded', `'${opName}' needs confirmation`);
|
|
274
|
+
}
|
|
275
|
+
const argsKey = JSON.stringify(args);
|
|
276
|
+
const previous = op.once ? state.onceResults.get(callOptions.idempotencyKey) : undefined;
|
|
277
|
+
if (previous) {
|
|
278
|
+
return previous.args === argsKey
|
|
279
|
+
? previous.result
|
|
280
|
+
: errorResult('conflict', 'idempotency key reused with different arguments');
|
|
281
|
+
}
|
|
282
|
+
const context = {
|
|
283
|
+
idempotencyKey: callOptions.idempotencyKey,
|
|
284
|
+
confirmed: callOptions.confirmed === true,
|
|
285
|
+
signal: callOptions.signal,
|
|
286
|
+
connection,
|
|
287
|
+
};
|
|
288
|
+
if (op.task) {
|
|
289
|
+
return startTask(state, {
|
|
290
|
+
op: opName,
|
|
291
|
+
args,
|
|
292
|
+
steps: op.task,
|
|
293
|
+
context,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
const result = await runOperation(op, args, {
|
|
297
|
+
...context,
|
|
298
|
+
answers: new Map(),
|
|
299
|
+
});
|
|
300
|
+
if (op.once) {
|
|
301
|
+
state.onceResults.set(callOptions.idempotencyKey, {
|
|
302
|
+
args: argsKey,
|
|
303
|
+
result,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
return result;
|
|
307
|
+
}
|
|
308
|
+
async function readResource(state, resource) {
|
|
309
|
+
const found = state.options.resources?.[resource];
|
|
310
|
+
if (!found) {
|
|
311
|
+
throw inMemoryRefusal('not_found', `no resource '${resource}'`);
|
|
312
|
+
}
|
|
313
|
+
return found.read();
|
|
314
|
+
}
|
|
315
|
+
//#endregion
|
|
316
|
+
//#region Tasks
|
|
317
|
+
async function reportTaskStep(task, step, resultOptions) {
|
|
318
|
+
if (resultOptions.signal.aborted || task.cancelled) {
|
|
319
|
+
throw new Error('task wait aborted');
|
|
320
|
+
}
|
|
321
|
+
resultOptions.onProgress?.({
|
|
322
|
+
taskId: task.taskId,
|
|
323
|
+
...step,
|
|
324
|
+
});
|
|
325
|
+
const question = step.request;
|
|
326
|
+
if (step.state !== 'input_required' || !question) {
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
await new Promise((resolve, reject) => {
|
|
330
|
+
task.waiting.set(question.id, resolve);
|
|
331
|
+
resultOptions.signal.addEventListener('abort', () => reject(new Error('task wait aborted')), {
|
|
332
|
+
once: true,
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
async function awaitTaskResult(state, taskId, resultOptions) {
|
|
337
|
+
const task = state.tasks.get(taskId);
|
|
338
|
+
if (!task) {
|
|
339
|
+
return errorResult('not_found', `no task '${taskId}'`);
|
|
340
|
+
}
|
|
341
|
+
for (const step of task.steps) {
|
|
342
|
+
await reportTaskStep(task, step, resultOptions);
|
|
343
|
+
}
|
|
344
|
+
const op = state.operations[task.op];
|
|
345
|
+
if (!op) {
|
|
346
|
+
return errorResult('undeclared', `'${task.op}' is gone`);
|
|
347
|
+
}
|
|
348
|
+
const outcome = await runOperation(op, task.args, {
|
|
349
|
+
...task.context,
|
|
350
|
+
answers: task.answers,
|
|
351
|
+
});
|
|
352
|
+
resultOptions.onProgress?.({
|
|
353
|
+
taskId,
|
|
354
|
+
state: outcome.status === 'ok' ? 'completed' : 'failed',
|
|
355
|
+
});
|
|
356
|
+
return outcome;
|
|
357
|
+
}
|
|
358
|
+
function createTaskApi(state) {
|
|
359
|
+
return {
|
|
360
|
+
async get(taskId) {
|
|
361
|
+
return {
|
|
362
|
+
taskId,
|
|
363
|
+
state: state.tasks.get(taskId)?.cancelled ? 'cancelled' : 'working',
|
|
364
|
+
};
|
|
365
|
+
},
|
|
366
|
+
result: (taskId, resultOptions) => awaitTaskResult(state, taskId, resultOptions),
|
|
367
|
+
async cancel(taskId) {
|
|
368
|
+
const task = state.tasks.get(taskId);
|
|
369
|
+
if (task) {
|
|
370
|
+
task.cancelled = true;
|
|
371
|
+
}
|
|
372
|
+
},
|
|
373
|
+
async respond(taskId, requestId, answer) {
|
|
374
|
+
const task = state.tasks.get(taskId);
|
|
375
|
+
if (!task) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
task.answers.set(requestId, answer);
|
|
379
|
+
task.waiting.get(requestId)?.();
|
|
380
|
+
},
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
//#endregion
|
|
384
|
+
//#region Events
|
|
385
|
+
function replayHistory(history, request, listener) {
|
|
386
|
+
if (request.cursor === undefined || request.cursor === null) {
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
const after = Number(request.cursor);
|
|
390
|
+
for (const event of history) {
|
|
391
|
+
if (event.type !== request.event || Number(event.cursor) <= after) {
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
listener.push({
|
|
395
|
+
type: 'event',
|
|
396
|
+
event,
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
function listenOn(state, request) {
|
|
401
|
+
const latest = state.history[state.history.length - 1]?.cursor ?? null;
|
|
402
|
+
const { stream, listener } = createListenerStream(state.listeners, request, {
|
|
403
|
+
cursor: latest,
|
|
404
|
+
});
|
|
405
|
+
replayHistory(state.history, request, listener);
|
|
406
|
+
state.listeners.add(listener);
|
|
407
|
+
return stream;
|
|
408
|
+
}
|
|
409
|
+
function emitEvent(state, emitted) {
|
|
410
|
+
const { type, payload, options: emitOptions } = emitted;
|
|
411
|
+
state.sequence += 1;
|
|
412
|
+
const event = {
|
|
413
|
+
id: emitOptions?.id ?? `evt-${state.sequence}`,
|
|
414
|
+
type,
|
|
415
|
+
time: new Date().toISOString(),
|
|
416
|
+
payload,
|
|
417
|
+
cursor: String(state.sequence),
|
|
418
|
+
...(emitOptions?.actionId
|
|
419
|
+
? {
|
|
420
|
+
actionId: emitOptions.actionId,
|
|
421
|
+
}
|
|
422
|
+
: {}),
|
|
423
|
+
};
|
|
424
|
+
state.history.push(event);
|
|
425
|
+
const matches = state.options.events?.[type]?.matches;
|
|
426
|
+
for (const listener of [
|
|
427
|
+
...state.listeners,
|
|
428
|
+
]) {
|
|
429
|
+
if (listener.event !== type) {
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
if (matches && !matches(listener.args, payload)) {
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
listener.push({
|
|
436
|
+
type: 'event',
|
|
437
|
+
event,
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
return event;
|
|
441
|
+
}
|
|
442
|
+
function broadcastGap(listeners, reason, type) {
|
|
443
|
+
for (const listener of [
|
|
444
|
+
...listeners,
|
|
445
|
+
]) {
|
|
446
|
+
if (type === undefined || listener.event === type) {
|
|
447
|
+
listener.push({
|
|
448
|
+
type: 'gap',
|
|
449
|
+
reason,
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
function terminateListeners(listeners, reason) {
|
|
455
|
+
for (const listener of [
|
|
456
|
+
...listeners,
|
|
457
|
+
]) {
|
|
458
|
+
listener.push({
|
|
459
|
+
type: 'terminated',
|
|
460
|
+
reason,
|
|
461
|
+
});
|
|
462
|
+
listener.end();
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
function endListeners(listeners) {
|
|
466
|
+
for (const listener of [
|
|
467
|
+
...listeners,
|
|
468
|
+
]) {
|
|
469
|
+
listener.end();
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
//#endregion
|
|
473
|
+
//#region Connection
|
|
474
|
+
function resetMethod(options) {
|
|
475
|
+
const reset = options.reset;
|
|
476
|
+
if (!reset) {
|
|
477
|
+
return {};
|
|
478
|
+
}
|
|
479
|
+
return {
|
|
480
|
+
async reset() {
|
|
481
|
+
reset();
|
|
482
|
+
},
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
function createConnection(options, setupGiven) {
|
|
486
|
+
const state = createConnectionState(options);
|
|
487
|
+
const connection = {
|
|
488
|
+
id: options.id ?? 'memory',
|
|
489
|
+
get manifest() {
|
|
490
|
+
return state.manifest;
|
|
491
|
+
},
|
|
492
|
+
get identity() {
|
|
493
|
+
return state.identity;
|
|
494
|
+
},
|
|
495
|
+
calls: state.calls,
|
|
496
|
+
get released() {
|
|
497
|
+
return state.released;
|
|
498
|
+
},
|
|
499
|
+
setupGiven,
|
|
500
|
+
call: (op, args, callOptions) => callOperation(state, connection, {
|
|
501
|
+
op,
|
|
502
|
+
args,
|
|
503
|
+
options: callOptions,
|
|
504
|
+
}),
|
|
505
|
+
read: (resource) => readResource(state, resource),
|
|
506
|
+
listen: (request) => listenOn(state, request),
|
|
507
|
+
tasks: createTaskApi(state),
|
|
508
|
+
...resetMethod(options),
|
|
509
|
+
onChange(listener) {
|
|
510
|
+
state.changeListeners.add(listener);
|
|
511
|
+
return () => {
|
|
512
|
+
state.changeListeners.delete(listener);
|
|
513
|
+
};
|
|
514
|
+
},
|
|
515
|
+
closed: state.closed,
|
|
516
|
+
async release() {
|
|
517
|
+
state.released = true;
|
|
518
|
+
endListeners(state.listeners);
|
|
519
|
+
state.resolveClosed({
|
|
520
|
+
reason: 'released',
|
|
521
|
+
});
|
|
522
|
+
},
|
|
523
|
+
emit: (type, payload, emitOptions) => emitEvent(state, {
|
|
524
|
+
type,
|
|
525
|
+
payload,
|
|
526
|
+
options: emitOptions,
|
|
527
|
+
}),
|
|
528
|
+
gap: (reason, type) => broadcastGap(state.listeners, reason, type),
|
|
529
|
+
terminate: (reason) => terminateListeners(state.listeners, reason),
|
|
530
|
+
replace: () => setIdentity(state, {
|
|
531
|
+
...state.identity,
|
|
532
|
+
instance: crypto.randomUUID(),
|
|
533
|
+
}),
|
|
534
|
+
bumpEpoch: () => setIdentity(state, {
|
|
535
|
+
...state.identity,
|
|
536
|
+
epoch: (state.identity.epoch ?? 0) + 1,
|
|
537
|
+
}),
|
|
538
|
+
updateManifest: (patch) => applyManifestPatch(state, patch),
|
|
539
|
+
drop(reason) {
|
|
540
|
+
terminateListeners(state.listeners, reason);
|
|
541
|
+
state.resolveClosed({
|
|
542
|
+
reason,
|
|
543
|
+
});
|
|
544
|
+
},
|
|
545
|
+
};
|
|
546
|
+
return connection;
|
|
547
|
+
}
|
|
548
|
+
//#endregion
|
|
549
|
+
//#region Public API
|
|
550
|
+
/**
|
|
551
|
+
* @public
|
|
552
|
+
* A scripted `ConnectionProvider` for tests: operations, reads, events and
|
|
553
|
+
* long operations as plain functions. With `provision`, it backs pools.
|
|
554
|
+
*/
|
|
555
|
+
export function createInMemoryConnection(options = {}) {
|
|
556
|
+
let current = createConnection(options);
|
|
557
|
+
let connects = 0;
|
|
558
|
+
const provisioned = [];
|
|
559
|
+
const provider = {
|
|
560
|
+
get connection() {
|
|
561
|
+
return current;
|
|
562
|
+
},
|
|
563
|
+
get provisioned() {
|
|
564
|
+
return provisioned;
|
|
565
|
+
},
|
|
566
|
+
get connects() {
|
|
567
|
+
return connects;
|
|
568
|
+
},
|
|
569
|
+
async connect(_opts) {
|
|
570
|
+
connects += 1;
|
|
571
|
+
if (options.failConnect) {
|
|
572
|
+
throw new Error(options.failConnect);
|
|
573
|
+
}
|
|
574
|
+
if (connects > 1) {
|
|
575
|
+
current = createConnection(options);
|
|
576
|
+
}
|
|
577
|
+
return [
|
|
578
|
+
current,
|
|
579
|
+
];
|
|
580
|
+
},
|
|
581
|
+
...(options.provision
|
|
582
|
+
? {
|
|
583
|
+
async provision(setup) {
|
|
584
|
+
const fresh = createConnection(options.provision?.(setup) ?? options, setup);
|
|
585
|
+
provisioned.push(fresh);
|
|
586
|
+
return fresh;
|
|
587
|
+
},
|
|
588
|
+
}
|
|
589
|
+
: {}),
|
|
590
|
+
};
|
|
591
|
+
return provider;
|
|
592
|
+
}
|
|
593
|
+
//#endregion
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The connection runtime (spec 35): registry, gates, refs, blobs, secrets,
|
|
3
|
+
* pools, events, triggers, sources, model tools and views.
|
|
4
|
+
*/
|
|
5
|
+
export { attachBlobs, checkBlobs, findBlobs } from './blobs.js';
|
|
6
|
+
export type { CronSchedule } from './cron-schedule.js';
|
|
7
|
+
export { cronMatches, nextCronRun, parseCronSchedule } from './cron-schedule.js';
|
|
8
|
+
export type { PendingConnectionEntry, ResumeDecision } from './durability.js';
|
|
9
|
+
export { resumeDecision, sameInstance, stepIdempotencyKey, toolIdempotencyKey, } from './durability.js';
|
|
10
|
+
export { bufferedEventStream } from './event-stream.js';
|
|
11
|
+
export type { GateContext, GateHooks, GateRequest } from './gates.js';
|
|
12
|
+
export { admitIncoming, gatedCall } from './gates.js';
|
|
13
|
+
export { matchesAnyGlob, matchesGlob } from './glob.js';
|
|
14
|
+
export { CONNECTION_NAME_PATTERN, connection, handleTarget, isConnectionHandle } from './handle.js';
|
|
15
|
+
export type { InMemoryCallContext, InMemoryCallRecord, InMemoryConnection, InMemoryConnectionOptions, InMemoryConnectionProvider, InMemoryEventType, InMemoryOperation, InMemoryResource, InMemoryTaskStep, } from './in-memory-connection.js';
|
|
16
|
+
export { createInMemoryConnection, InMemoryRefusal, inMemoryRefusal } from './in-memory-connection.js';
|
|
17
|
+
export { secretAllowedAt, validateJsonSchema } from './json-schema.js';
|
|
18
|
+
export type { ConnectionToolsOptions } from './model-tools.js';
|
|
19
|
+
export { buildConnectionTools, connectionToolName, connectionTools, runtimeOf, } from './model-tools.js';
|
|
20
|
+
export type { ConnectionUse } from './negotiation.js';
|
|
21
|
+
export { checkRequirement, requirementFor } from './negotiation.js';
|
|
22
|
+
export { evaluatePolicy } from './policy.js';
|
|
23
|
+
export { ConnectionPool, DEFAULT_POOL_WAIT_MS } from './pool.js';
|
|
24
|
+
export type { RefKindInfo, RefScope } from './refs.js';
|
|
25
|
+
export { kindInfoFrom, RefSealer, refConnections, refKindsOf } from './refs.js';
|
|
26
|
+
export type { ConnectionFrameworkEventRecord, ConnectionRuntime, ConnectionRuntimeOptions, RuntimeResolveRequest, } from './registry.js';
|
|
27
|
+
export { createConnectionRuntime } from './registry.js';
|
|
28
|
+
export type { ConnectionCaller, ConnectionRegistry, ConnectionScope, GatedCallOptions, ResolvedConnection, ResolveRequest, } from './registry-types.js';
|
|
29
|
+
export { checkSecrets, fillSecrets, findSecrets, SecretScrubber } from './secrets.js';
|
|
30
|
+
export { cron, defineSource } from './sources.js';
|
|
31
|
+
export type { TriggerDelivery, TriggerRunnerOptions } from './triggers.js';
|
|
32
|
+
export { TriggerRunner, validateTriggers } from './triggers.js';
|
|
33
|
+
export type { ConnectionViewState } from './views.js';
|
|
34
|
+
export { connectionViewLayer, connectionViewLayers, lineDiff } from './views.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The connection runtime (spec 35): registry, gates, refs, blobs, secrets,
|
|
3
|
+
* pools, events, triggers, sources, model tools and views.
|
|
4
|
+
*/
|
|
5
|
+
export { attachBlobs, checkBlobs, findBlobs } from './blobs.js';
|
|
6
|
+
export { cronMatches, nextCronRun, parseCronSchedule } from './cron-schedule.js';
|
|
7
|
+
export { resumeDecision, sameInstance, stepIdempotencyKey, toolIdempotencyKey, } from './durability.js';
|
|
8
|
+
export { bufferedEventStream } from './event-stream.js';
|
|
9
|
+
export { admitIncoming, gatedCall } from './gates.js';
|
|
10
|
+
export { matchesAnyGlob, matchesGlob } from './glob.js';
|
|
11
|
+
export { CONNECTION_NAME_PATTERN, connection, handleTarget, isConnectionHandle } from './handle.js';
|
|
12
|
+
export { createInMemoryConnection, InMemoryRefusal, inMemoryRefusal } from './in-memory-connection.js';
|
|
13
|
+
export { secretAllowedAt, validateJsonSchema } from './json-schema.js';
|
|
14
|
+
export { buildConnectionTools, connectionToolName, connectionTools, runtimeOf, } from './model-tools.js';
|
|
15
|
+
export { checkRequirement, requirementFor } from './negotiation.js';
|
|
16
|
+
export { evaluatePolicy } from './policy.js';
|
|
17
|
+
export { ConnectionPool, DEFAULT_POOL_WAIT_MS } from './pool.js';
|
|
18
|
+
export { kindInfoFrom, RefSealer, refConnections, refKindsOf } from './refs.js';
|
|
19
|
+
export { createConnectionRuntime } from './registry.js';
|
|
20
|
+
export { checkSecrets, fillSecrets, findSecrets, SecretScrubber } from './secrets.js';
|
|
21
|
+
export { cron, defineSource } from './sources.js';
|
|
22
|
+
export { TriggerRunner, validateTriggers } from './triggers.js';
|
|
23
|
+
export { connectionViewLayer, connectionViewLayers, lineDiff } from './views.js';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The JSON Schema subset the connection gates need (spec 35 § Gates, gate 2
|
|
3
|
+
* and gate 6). A connection's manifest publishes operation schemas as JSON
|
|
4
|
+
* Schema — from a profile through `z.toJSONSchema`, or from an MCP server's
|
|
5
|
+
* tool list — so the harness checks arguments against them without a profile
|
|
6
|
+
* object in hand.
|
|
7
|
+
*
|
|
8
|
+
* `validateJsonSchema` covers the keywords profiles and MCP tools use: `type`,
|
|
9
|
+
* `properties`, `required`, `additionalProperties`, `items`, `enum`, `const`,
|
|
10
|
+
* `anyOf`/`oneOf`/`allOf`, string length and pattern, number bounds, and local
|
|
11
|
+
* `$ref`s into `$defs`/`definitions`. Unknown keywords are ignored rather than
|
|
12
|
+
* failing a call. Nothing is ever defaulted on the caller's behalf.
|
|
13
|
+
*/
|
|
14
|
+
import type { JsonSchemaObject } from '@noetic-tools/types';
|
|
15
|
+
/** One step into a value: an object key or an array index. */
|
|
16
|
+
export type JsonPath = ReadonlyArray<string | number>;
|
|
17
|
+
/**
|
|
18
|
+
* Checks `value` against a JSON Schema.
|
|
19
|
+
* @returns One message per problem; empty when the value matches.
|
|
20
|
+
*/
|
|
21
|
+
export declare function validateJsonSchema(schema: JsonSchemaObject, value: unknown): string[];
|
|
22
|
+
/**
|
|
23
|
+
* Whether the field at `path` is `sensitive` — its schema accepts a
|
|
24
|
+
* `{ $secret }` value (spec 36 `sensitive()` publishes as `anyOf: [T, { $secret }]`).
|
|
25
|
+
*/
|
|
26
|
+
export declare function secretAllowedAt(schema: JsonSchemaObject, path: JsonPath): boolean;
|
|
27
|
+
/** Renders a path the way validation messages do: `.thread.id`, `[0]`. */
|
|
28
|
+
export declare function describeJsonPath(path: JsonPath): string;
|