@noetic-tools/core 5.20.0-next.1 → 5.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/dist/adapters/openrouter.d.ts +1 -48
  2. package/dist/adapters/openrouter.js +3 -204
  3. package/dist/builders/acp-agent-checks.d.ts +20 -0
  4. package/dist/builders/acp-agent-checks.js +68 -0
  5. package/dist/builders/acp-agent-tool.d.ts +4 -3
  6. package/dist/builders/acp-agent-tool.js +5 -2
  7. package/dist/builders/connection-ceiling.d.ts +13 -0
  8. package/dist/builders/connection-ceiling.js +17 -0
  9. package/dist/builders/document-entries.d.ts +13 -0
  10. package/dist/builders/document-entries.js +18 -0
  11. package/dist/builders/dynamic-workflow.d.ts +53 -8
  12. package/dist/builders/dynamic-workflow.js +53 -25
  13. package/dist/builders/mcp-url-pattern.d.ts +13 -0
  14. package/dist/builders/mcp-url-pattern.js +85 -0
  15. package/dist/builders/step-builders.d.ts +14 -6
  16. package/dist/builders/step-builders.js +25 -2
  17. package/dist/builders/workflow-acp-grants.d.ts +42 -0
  18. package/dist/builders/workflow-acp-grants.js +224 -0
  19. package/dist/builders/workflow-child-nodes.js +8 -0
  20. package/dist/builders/workflow-connection-nodes.d.ts +13 -3
  21. package/dist/builders/workflow-connection-nodes.js +129 -3
  22. package/dist/builders/workflow-connections.js +2 -1
  23. package/dist/builders/workflow-hydrator.d.ts +24 -1
  24. package/dist/builders/workflow-hydrator.js +8 -167
  25. package/dist/builders/workflow-mcp-servers.d.ts +44 -0
  26. package/dist/builders/workflow-mcp-servers.js +327 -0
  27. package/dist/builders/workflow-tool-entries.d.ts +84 -0
  28. package/dist/builders/workflow-tool-entries.js +216 -0
  29. package/dist/connections/durability.d.ts +11 -1
  30. package/dist/connections/durability.js +34 -0
  31. package/dist/connections/entries.d.ts +33 -0
  32. package/dist/connections/entries.js +118 -0
  33. package/dist/connections/errors.d.ts +6 -0
  34. package/dist/connections/errors.js +12 -0
  35. package/dist/connections/harness-wiring.d.ts +4 -4
  36. package/dist/connections/harness-wiring.js +1 -1
  37. package/dist/connections/in-memory-connection.d.ts +2 -0
  38. package/dist/connections/in-memory-connection.js +5 -0
  39. package/dist/connections/model-tools.d.ts +35 -12
  40. package/dist/connections/model-tools.js +348 -202
  41. package/dist/connections/registry-types.d.ts +7 -0
  42. package/dist/connections/registry.d.ts +35 -6
  43. package/dist/connections/registry.js +183 -39
  44. package/dist/connections/step-tools.d.ts +64 -0
  45. package/dist/connections/step-tools.js +154 -0
  46. package/dist/connections/views.d.ts +3 -2
  47. package/dist/connections/views.js +15 -6
  48. package/dist/harness/agent-harness.d.ts +11 -1
  49. package/dist/harness/agent-harness.js +69 -8
  50. package/dist/harness/connection-gate.d.ts +49 -8
  51. package/dist/harness/connection-gate.js +137 -26
  52. package/dist/harness/deps/interpreter.d.ts +3 -3
  53. package/dist/harness/deps/interpreter.js +3 -3
  54. package/dist/harness/harness-options.d.ts +4 -3
  55. package/dist/harness/model-call.d.ts +0 -1
  56. package/dist/harness/model-call.js +0 -48
  57. package/dist/harness/model-request.js +3 -1
  58. package/dist/harness/model-schema.d.ts +1 -19
  59. package/dist/harness/model-schema.js +0 -28
  60. package/dist/harness/model-tool-round.js +2 -2
  61. package/dist/index.d.ts +5 -5
  62. package/dist/index.js +1 -1
  63. package/dist/interpreter/acp-events.d.ts +25 -1
  64. package/dist/interpreter/acp-events.js +53 -1
  65. package/dist/interpreter/acp-layers.d.ts +20 -0
  66. package/dist/interpreter/acp-layers.js +55 -0
  67. package/dist/interpreter/acp-tools.d.ts +108 -0
  68. package/dist/interpreter/acp-tools.js +462 -0
  69. package/dist/interpreter/collect-tools.d.ts +28 -0
  70. package/dist/interpreter/collect-tools.js +112 -15
  71. package/dist/interpreter/connection-steps.d.ts +0 -2
  72. package/dist/interpreter/connection-steps.js +30 -8
  73. package/dist/interpreter/execute-acp-agent.js +263 -72
  74. package/dist/interpreter/execute-action.d.ts +76 -1
  75. package/dist/interpreter/execute-action.js +35 -42
  76. package/dist/interpreter/execute.d.ts +2 -0
  77. package/dist/interpreter/execute.js +24 -1
  78. package/dist/interpreter/verify-host.js +2 -1
  79. package/dist/runtime/tool-call.d.ts +67 -0
  80. package/dist/runtime/tool-call.js +244 -0
  81. package/dist/runtime/tool-context.d.ts +1 -2
  82. package/dist/runtime/tool-context.js +6 -2
  83. package/dist/schemas/workflow.d.ts +1 -1
  84. package/dist/types/connection-entry.d.ts +22 -0
  85. package/dist/types/connection-entry.js +8 -0
  86. package/dist/util/harness-internals.d.ts +2 -0
  87. package/dist/util/root-context.d.ts +3 -0
  88. package/dist/util/root-context.js +8 -0
  89. package/dist/util/tool-name.d.ts +8 -0
  90. package/dist/util/tool-name.js +10 -0
  91. package/package.json +3 -3
  92. package/schema/noetic-workflow.schema.json +498 -128
@@ -7,24 +7,29 @@
7
7
  * `'ask'` read). A connection with no profile has no eager set, so its tools
8
8
  * are exposed as they are.
9
9
  * - `'all'`: every operation as its own tool.
10
+ * - `'call'`: only `<name>_ops` and `<name>_call`.
10
11
  * - `'none'`: none; the connection is for steps and triggers only.
11
12
  *
12
13
  * Tools are named `<name>_<op>` (dots become underscores). When two
13
14
  * connections offer the same profile operation, the model gets one tool named
14
15
  * 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; two or more `'call'` connections likewise share one `ops` and
17
+ * one `call`. Two operations that would still share a name are a
16
18
  * `CONNECTION_TOOL_COLLISION`. Every call passes the same gates as a step.
17
19
  *
18
- * `connectionTools(handle, { ops })` builds tools for one handle, for a
19
- * `callModel` step that narrows what it offers.
20
+ * The tools are built once per turn, from the harness's connections and the
21
+ * ones the turn attached, so a connection gives the same tools wherever it is
22
+ * attached. `connectionTools(handle, { ops })` picks part of them for a step,
23
+ * and each tool cuts itself to what the step picked (`cutForStep`).
20
24
  */
21
- import { frameworkCast } from '@noetic-tools/types';
25
+ import { frameworkCast, isConnectionEntry } from '@noetic-tools/types';
22
26
  import { z } from 'zod';
23
27
  import { harnessInternal } from '../util/harness-internals.js';
24
- import { toolIdempotencyKey } from './durability.js';
25
- import { deniedError, toolCollisionError } from './errors.js';
28
+ import { orderedCallId, toolIdempotencyKey } from './durability.js';
29
+ import { entryName } from './entries.js';
30
+ import { deniedError, invalidConnectionError, toolCollisionError } from './errors.js';
26
31
  import { matchesAnyGlob } from './glob.js';
27
- import { handleTarget } from './handle.js';
32
+ import { CONNECTION_NAME_PATTERN, handleTarget } from './handle.js';
28
33
  import { requirementFor } from './negotiation.js';
29
34
  import { refConnections } from './refs.js';
30
35
  import { isPlainRecord } from './values.js';
@@ -49,6 +54,11 @@ const ANY_OBJECT_INPUT = {
49
54
  },
50
55
  };
51
56
  const ANY_OUTPUT = z.unknown();
57
+ const CUTS = new WeakMap();
58
+ function cutting(tool, cut) {
59
+ CUTS.set(tool, cut);
60
+ return tool;
61
+ }
52
62
  function sanitize(name) {
53
63
  return name.replace(/[^A-Za-z0-9_-]/g, '_');
54
64
  }
@@ -63,22 +73,9 @@ function toolContext(toolCtx) {
63
73
  export function runtimeOf(harness) {
64
74
  return harnessInternal(harness, 'connectionRegistry');
65
75
  }
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)}`)}`;
76
+ /** The tool call's id, or — when a step runs the tool directly and there is no call — one from the step and the call's order. */
77
+ function callIdOf(toolCtx) {
78
+ return toolCtx.callId ?? orderedCallId(toolCtx.ctx, toolCtx.ctx);
82
79
  }
83
80
  /** The run's abort signal: cancelling the run cancels the call and any long operation it waits on. */
84
81
  function signalOf(ctx) {
@@ -99,8 +96,9 @@ async function callThrough(runtime, request) {
99
96
  ...request,
100
97
  scope,
101
98
  ctx,
99
+ caller: 'model',
102
100
  });
103
- const toolCallId = callIdOf(request.toolCtx, request.op, request.args);
101
+ const toolCallId = callIdOf(request.toolCtx);
104
102
  const output = await resolved.call(request.op, request.args, {
105
103
  signal: scope.signal,
106
104
  caller: 'model',
@@ -147,7 +145,7 @@ function pickTarget(args, targets) {
147
145
  throw deniedError(`pass target: one of ${targets.join(', ')}`);
148
146
  }
149
147
  function directTool(runtime, exposure) {
150
- return {
148
+ const tool = {
151
149
  name: connectionToolName(exposure.connection, exposure.op),
152
150
  description: exposure.info.description ?? `${exposure.op} on ${exposure.connection}`,
153
151
  input: ANY_OBJECT_INPUT,
@@ -162,6 +160,7 @@ function directTool(runtime, exposure) {
162
160
  toolCtx: toolContext(toolCtx),
163
161
  }),
164
162
  };
163
+ return cutting(tool, (allows) => (allows(exposure.connection, exposure.op) ? tool : undefined));
165
164
  }
166
165
  function mergedTool(runtime, group) {
167
166
  const first = group[0];
@@ -169,7 +168,7 @@ function mergedTool(runtime, group) {
169
168
  throw deniedError('internal: empty tool group');
170
169
  }
171
170
  const targets = group.map((e) => e.connection);
172
- return {
171
+ const tool = {
173
172
  name: sanitize(first.op),
174
173
  description: `${first.info.description ?? first.op} (on ${targets.join(' or ')}; pass target)`,
175
174
  input: ANY_OBJECT_INPUT,
@@ -182,99 +181,228 @@ function mergedTool(runtime, group) {
182
181
  toolCtx: toolContext(toolCtx),
183
182
  }),
184
183
  };
184
+ return cutting(tool, (allows) => {
185
+ const kept = group.filter((e) => allows(e.connection, e.op));
186
+ if (kept.length === group.length) {
187
+ return tool;
188
+ }
189
+ return kept.length > 0 ? mergedTool(runtime, kept) : undefined;
190
+ });
191
+ }
192
+ /**
193
+ * The reads `<name>_call({ read })` serves: the connection's own `'ask'` reads
194
+ * (every read when no view shows any, see `hasView`) that the step may use.
195
+ */
196
+ function askReads(turn, generic) {
197
+ const { name } = generic;
198
+ const manifest = turn.runtime.current(name, turn.rootId)?.manifest;
199
+ const viewed = hasView(turn, name);
200
+ const look = turn.runtime.config(name, turn.rootId)?.look ?? {};
201
+ return Object.values(manifest?.resources ?? {})
202
+ .filter((r) => !viewed || (look[r.name] ?? r.look ?? 'ask') === 'ask')
203
+ .filter((r) => generic.reads(r.name))
204
+ .map((r) => ({
205
+ read: r.name,
206
+ description: r.description,
207
+ }));
208
+ }
209
+ /** Whether a view layer renders the connection's `'turn'` and `'effect'` reads. */
210
+ function hasView(turn, name) {
211
+ return turn.runtime.viewed(name, turn.rootId);
185
212
  }
186
- function opsTool(runtime, name, hidden) {
213
+ /** What `<name>_ops` lists for one connection. */
214
+ function listOps(turn, generic, filter) {
215
+ const { name, hidden } = generic;
216
+ const manifest = turn.runtime.current(name, turn.rootId)?.manifest;
217
+ const operations = hidden
218
+ .filter((op) => matchesAnyGlob([
219
+ filter,
220
+ ], op))
221
+ .map((op) => manifest?.operations[op])
222
+ .filter((info) => info !== undefined)
223
+ .map((info) => ({
224
+ op: info.name,
225
+ description: info.description,
226
+ inputSchema: info.inputSchema,
227
+ }));
187
228
  return {
229
+ operations,
230
+ reads: askReads(turn, generic),
231
+ };
232
+ }
233
+ /** Runs what `<name>_call` was asked: one listed operation, or a read. */
234
+ async function runCall(turn, generic, call) {
235
+ const { args, toolCtx } = call;
236
+ const { runtime } = turn;
237
+ const { name, hidden } = generic;
238
+ if (typeof args.read === 'string') {
239
+ const read = args.read;
240
+ if (!askReads(turn, generic).some((r) => r.read === read)) {
241
+ throw deniedError(`'${read}' is not a read ${sanitize(name)}_call can serve`);
242
+ }
243
+ const resolved = await runtime.resolve({
244
+ name,
245
+ scope: scopeOf(toolCtx.ctx),
246
+ ctx: toolCtx.ctx,
247
+ caller: 'model',
248
+ });
249
+ const value = await resolved.read(read, {
250
+ signal: signalOf(toolCtx.ctx),
251
+ });
252
+ return runtime.withholdSensitive(value);
253
+ }
254
+ const op = typeof args.op === 'string' ? args.op : '';
255
+ if (!hidden.includes(op)) {
256
+ throw deniedError(`'${op}' is not an operation ${sanitize(name)}_call can run`);
257
+ }
258
+ return callThrough(runtime, {
259
+ name,
260
+ op,
261
+ args: args.args ?? {},
262
+ toolCtx,
263
+ });
264
+ }
265
+ const everyRead = () => true;
266
+ const OPS_SCHEMA = {
267
+ type: 'object',
268
+ properties: {
269
+ filter: {
270
+ type: 'string',
271
+ description: "A glob over operation names, e.g. 'ui.*'.",
272
+ },
273
+ },
274
+ };
275
+ const CALL_SCHEMA = {
276
+ type: 'object',
277
+ properties: {
278
+ op: {
279
+ type: 'string',
280
+ },
281
+ args: {
282
+ type: 'object',
283
+ },
284
+ read: {
285
+ type: 'string',
286
+ },
287
+ },
288
+ };
289
+ function filterOf(args) {
290
+ return typeof args.filter === 'string' ? args.filter : '*';
291
+ }
292
+ /** `<name>_ops` and `<name>_call` for one connection. */
293
+ function genericTools(turn, generic) {
294
+ const { name } = generic;
295
+ const ops = {
188
296
  name: `${sanitize(name)}_ops`,
189
297
  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
298
  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
- },
299
+ inputJsonSchema: OPS_SCHEMA,
200
300
  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
- },
301
+ execute: async (args) => listOps(turn, generic, filterOf(args)),
227
302
  };
228
- }
229
- function callTool(runtime, name, allowed) {
230
- return {
303
+ const call = {
231
304
  name: `${sanitize(name)}_call`,
232
305
  description: `Run one operation of ${name} listed by ${sanitize(name)}_ops ({ op, args }), or read its state ({ read }).`,
233
306
  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
- },
307
+ inputJsonSchema: CALL_SCHEMA,
308
+ output: ANY_OUTPUT,
309
+ execute: (args, toolCtx) => runCall(turn, generic, {
310
+ args,
311
+ toolCtx: toolContext(toolCtx),
312
+ }),
313
+ };
314
+ const tools = [
315
+ ops,
316
+ call,
317
+ ];
318
+ return tools.map((tool, i) => cutting(tool, (allows) => {
319
+ const cut = cutGeneric(turn, generic, allows);
320
+ if (cut === 'whole') {
321
+ return tool;
322
+ }
323
+ return cut === 'none' ? undefined : genericTools(turn, cut)[i];
324
+ }));
325
+ }
326
+ /** One `ops` and one `call` shared by every `'call'` connection, each taking `target`. */
327
+ function mergedGenericTools(turn, group) {
328
+ const targets = group.map((g) => g.name);
329
+ const byName = new Map(group.map((g) => [
330
+ g.name,
331
+ g,
332
+ ]));
333
+ const generic = (args) => {
334
+ const found = byName.get(pickTarget(args, targets));
335
+ if (!found) {
336
+ throw deniedError(`pass target: one of ${targets.join(', ')}`);
337
+ }
338
+ return found;
339
+ };
340
+ const ops = {
341
+ name: 'ops',
342
+ description: `List the operations of ${targets.join(' or ')} (pass target), and the state you can read with call({ target, read }).`,
343
+ input: ANY_OBJECT_INPUT,
344
+ inputJsonSchema: withTarget(OPS_SCHEMA, targets),
345
+ output: ANY_OUTPUT,
346
+ execute: async (args) => {
347
+ return listOps(turn, generic(args), filterOf(args));
247
348
  },
349
+ };
350
+ const call = {
351
+ name: 'call',
352
+ description: `Run one operation listed by ops on ${targets.join(' or ')} ({ target, op, args }), or read its state ({ target, read }).`,
353
+ input: ANY_OBJECT_INPUT,
354
+ inputJsonSchema: withTarget(CALL_SCHEMA, targets),
248
355
  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`);
356
+ execute: async (args, toolCtx) => runCall(turn, generic(args), {
357
+ args: withoutTarget(args),
358
+ toolCtx: toolContext(toolCtx),
359
+ }),
360
+ };
361
+ const tools = [
362
+ ops,
363
+ call,
364
+ ];
365
+ return tools.map((tool, i) => cutting(tool, (allows) => {
366
+ const cuts = group.map((g) => cutGeneric(turn, g, allows));
367
+ if (cuts.every((cut) => cut === 'whole')) {
368
+ return tool;
369
+ }
370
+ const kept = group.flatMap((member, j) => {
371
+ const cut = cuts[j];
372
+ if (cut === 'none' || cut === undefined) {
373
+ return [];
265
374
  }
266
- return callThrough(runtime, {
267
- name,
268
- op,
269
- args: args.args ?? {},
270
- toolCtx: tc,
271
- });
272
- },
375
+ return [
376
+ cut === 'whole' ? member : cut,
377
+ ];
378
+ });
379
+ return kept.length > 0 ? mergedGenericTools(turn, kept)[i] : undefined;
380
+ }));
381
+ }
382
+ /**
383
+ * `generic` cut to what a step may use: `'whole'` when it may use all of it,
384
+ * `'none'` when it may use nothing, else the part it may.
385
+ */
386
+ function cutGeneric(turn, generic, allows) {
387
+ const { name } = generic;
388
+ const hidden = generic.hidden.filter((op) => allows(name, op));
389
+ const reads = askReads(turn, generic);
390
+ const kept = reads.filter((r) => allows(name, r.read));
391
+ if (hidden.length === generic.hidden.length && kept.length === reads.length) {
392
+ return 'whole';
393
+ }
394
+ if (hidden.length === 0 && kept.length === 0) {
395
+ return 'none';
396
+ }
397
+ return {
398
+ name,
399
+ hidden,
400
+ reads: (read) => generic.reads(read) && allows(name, read),
273
401
  };
274
402
  }
275
- function exposuresFor(runtime, name) {
276
- const config = runtime.config(name);
277
- const connection = runtime.current(name);
403
+ function exposuresFor(turn, name) {
404
+ const config = turn.runtime.config(name, turn.rootId);
405
+ const connection = turn.runtime.current(name, turn.rootId);
278
406
  const expose = config?.expose ?? 'eager';
279
407
  if (!connection || config?.pool || expose === 'none') {
280
408
  return {
@@ -287,7 +415,7 @@ function exposuresFor(runtime, name) {
287
415
  const direct = [];
288
416
  const hidden = [];
289
417
  for (const [op, info] of Object.entries(manifest.operations)) {
290
- const own = expose === 'all' || !hasProfile || info.eager === true;
418
+ const own = expose !== 'call' && (expose === 'all' || !hasProfile || info.eager === true);
291
419
  if (own) {
292
420
  direct.push({
293
421
  connection: name,
@@ -321,33 +449,59 @@ class ToolSet {
321
449
  ];
322
450
  }
323
451
  }
324
- function needsGenericTools(runtime, name, hidden) {
325
- const config = runtime.config(name);
452
+ function needsGenericTools(turn, name, hidden) {
453
+ const config = turn.runtime.config(name, turn.rootId);
326
454
  if (config?.pool || config?.expose === 'none') {
327
455
  return false;
328
456
  }
329
- if (hidden.length > 0) {
330
- return true;
457
+ return (hidden.length > 0 ||
458
+ askReads(turn, {
459
+ name,
460
+ hidden,
461
+ reads: everyRead,
462
+ }).length > 0);
463
+ }
464
+ /** Adds each `'call'` connection's ops and call, merged when there are two or more. */
465
+ function addGenericTools(turn, tools, groups) {
466
+ const { generics, called } = groups;
467
+ for (const generic of generics) {
468
+ for (const tool of genericTools(turn, generic)) {
469
+ tools.add(tool, `${generic.name} (${tool.name.endsWith('_ops') ? 'ops' : 'call'})`);
470
+ }
471
+ }
472
+ if (called.length > 1) {
473
+ for (const tool of mergedGenericTools(turn, called)) {
474
+ tools.add(tool, `${tool.name} on ${called.map((g) => g.name).join(', ')}`);
475
+ }
476
+ return;
477
+ }
478
+ for (const generic of called) {
479
+ for (const tool of genericTools(turn, generic)) {
480
+ tools.add(tool, `${generic.name} (${tool.name.endsWith('_ops') ? 'ops' : 'call'})`);
481
+ }
331
482
  }
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
483
  }
336
484
  //#endregion
337
485
  //#region Public API
338
486
  /**
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.
487
+ * The model tools for every ready connection of a turn — the harness's, and
488
+ * the ones attached under `rootId` — built fresh each turn so a renegotiated
489
+ * connection's tools change at the next turn, never mid-turn.
342
490
  *
343
491
  * @throws NoeticConfigError `CONNECTION_TOOL_COLLISION` when two operations would share a tool name.
344
492
  */
345
- export function buildConnectionTools(runtime) {
493
+ export function buildConnectionTools(runtime, rootId) {
494
+ const turn = {
495
+ runtime,
496
+ rootId,
497
+ };
346
498
  const tools = new ToolSet();
347
499
  const byProfileOp = new Map();
348
500
  const plain = [];
349
- for (const name of runtime.names()) {
350
- const { direct, hidden } = exposuresFor(runtime, name);
501
+ const generics = [];
502
+ const called = [];
503
+ for (const name of runtime.visibleNames(rootId)) {
504
+ const { direct, hidden } = exposuresFor(turn, name);
351
505
  for (const exposure of direct) {
352
506
  if (!exposure.info.profile && !exposure.info.extension) {
353
507
  plain.push(exposure);
@@ -357,11 +511,20 @@ export function buildConnectionTools(runtime) {
357
511
  group.push(exposure);
358
512
  byProfileOp.set(exposure.op, group);
359
513
  }
360
- if (needsGenericTools(runtime, name, hidden)) {
361
- tools.add(opsTool(runtime, name, hidden), `${name} (ops)`);
362
- tools.add(callTool(runtime, name, hidden), `${name} (call)`);
514
+ if (!needsGenericTools(turn, name, hidden)) {
515
+ continue;
363
516
  }
517
+ const isCalled = runtime.config(name, rootId)?.expose === 'call';
518
+ (isCalled ? called : generics).push({
519
+ name,
520
+ hidden,
521
+ reads: everyRead,
522
+ });
364
523
  }
524
+ addGenericTools(turn, tools, {
525
+ generics,
526
+ called,
527
+ });
365
528
  for (const [op, group] of byProfileOp) {
366
529
  if (group.length > 1) {
367
530
  tools.add(mergedTool(runtime, group), `'${op}' on ${group.map((g) => g.connection).join(', ')}`);
@@ -374,93 +537,76 @@ export function buildConnectionTools(runtime) {
374
537
  }
375
538
  return tools.list();
376
539
  }
540
+ /**
541
+ * Cuts a tool built by `buildConnectionTools` to what a step may use (see
542
+ * `StepCut`): a merged tool keeps only the targets the step may use, its
543
+ * `target` enum and routing with them, and `<name>_ops` / `<name>_call` only
544
+ * the operations and reads. Returns `'not-connection'` for any other tool.
545
+ */
546
+ export function cutForStep(tool, allows) {
547
+ const cut = CUTS.get(tool);
548
+ return cut ? cut(allows) : 'not-connection';
549
+ }
377
550
  /**
378
551
  * @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`.
552
+ * Picks part of one connection's model tools for a step: the tools the turn
553
+ * built for it, under the same names — `<name>_<op>`, `<name>_ops` /
554
+ * `<name>_call`, and merged profile tools that take `target` — narrowed to the
555
+ * operations matching `ops`. A step that lists the pick is offered a tool only
556
+ * when every operation the tool can run matches, so `<name>_call` comes with a
557
+ * pick only when every operation behind it does. Picking from an entry
558
+ * (`mcp({ name, … })`) attaches it to the step, as listing the entry would.
559
+ * A name picks a connection the harness or the turn attaches; it is checked
560
+ * when the turn's tool set is built.
561
+ *
562
+ * @throws NoeticConfigError `CONNECTION_INVALID` for a handle with `fresh` (a
563
+ * pool has no model tools), an entry with no name, or a malformed name. The
564
+ * turn fails with `CONNECTION_UNKNOWN` when no connection has the name.
382
565
  */
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');
566
+ export function connectionTools(target, options = {}) {
567
+ const ops = options.ops === undefined
568
+ ? {}
569
+ : {
570
+ ops: [
571
+ ...options.ops,
572
+ ],
573
+ };
574
+ if (typeof target === 'string') {
575
+ if (!CONNECTION_NAME_PATTERN.test(target)) {
576
+ throw invalidConnectionError(`connectionTools(${JSON.stringify(target)}) is not a connection name.`, 'A connection name is a letter followed by letters, digits, _ or -.');
393
577
  }
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
- },
578
+ return {
579
+ kind: 'connection-pick',
580
+ connection: target,
581
+ ...ops,
432
582
  };
433
- return [
434
- generic,
435
- ];
436
583
  }
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,
584
+ if (isConnectionEntry(target)) {
585
+ const name = entryName(target);
586
+ if (name === undefined) {
587
+ throw invalidConnectionError('connectionTools() was given a connection with no name.', "Name it on the provider: connectionTools(mcp({ name: 'linear', … })).");
588
+ }
589
+ return {
590
+ kind: 'connection-pick',
591
+ connection: name,
592
+ ...ops,
593
+ entry: target,
594
+ };
595
+ }
596
+ if (target.options.fresh) {
597
+ throw invalidConnectionError(`connectionTools(connection('${target.name}')) names a fresh connection.`, 'A pool gives no model tools; drive it from steps (step.call) instead.');
598
+ }
599
+ const profile = target.profile;
600
+ return {
601
+ kind: 'connection-pick',
602
+ connection: handleTarget(target),
603
+ ...ops,
604
+ ...(profile
605
+ ? {
606
+ requirement: requirementFor(target),
454
607
  profile,
455
- requirement: requirementFor(handle, {
456
- operations: [
457
- op,
458
- ],
459
- }),
460
- fresh: handle.options.fresh,
461
- setup: handle.options.setup,
462
- });
463
- },
464
- }));
608
+ }
609
+ : {}),
610
+ };
465
611
  }
466
612
  //#endregion