@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.
- package/dist/adapters/openrouter.d.ts +1 -48
- package/dist/adapters/openrouter.js +3 -204
- package/dist/builders/acp-agent-checks.d.ts +20 -0
- package/dist/builders/acp-agent-checks.js +68 -0
- package/dist/builders/acp-agent-tool.d.ts +4 -3
- package/dist/builders/acp-agent-tool.js +5 -2
- package/dist/builders/connection-ceiling.d.ts +13 -0
- package/dist/builders/connection-ceiling.js +17 -0
- package/dist/builders/document-entries.d.ts +13 -0
- package/dist/builders/document-entries.js +18 -0
- package/dist/builders/dynamic-workflow.d.ts +53 -8
- package/dist/builders/dynamic-workflow.js +53 -25
- package/dist/builders/mcp-url-pattern.d.ts +13 -0
- package/dist/builders/mcp-url-pattern.js +85 -0
- package/dist/builders/step-builders.d.ts +14 -6
- package/dist/builders/step-builders.js +25 -2
- 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.js +8 -0
- package/dist/builders/workflow-connection-nodes.d.ts +13 -3
- package/dist/builders/workflow-connection-nodes.js +129 -3
- package/dist/builders/workflow-connections.js +2 -1
- package/dist/builders/workflow-hydrator.d.ts +24 -1
- package/dist/builders/workflow-hydrator.js +8 -167
- package/dist/builders/workflow-mcp-servers.d.ts +44 -0
- package/dist/builders/workflow-mcp-servers.js +327 -0
- package/dist/builders/workflow-tool-entries.d.ts +84 -0
- package/dist/builders/workflow-tool-entries.js +216 -0
- package/dist/connections/durability.d.ts +11 -1
- package/dist/connections/durability.js +34 -0
- package/dist/connections/entries.d.ts +33 -0
- package/dist/connections/entries.js +118 -0
- package/dist/connections/errors.d.ts +6 -0
- package/dist/connections/errors.js +12 -0
- package/dist/connections/harness-wiring.d.ts +4 -4
- package/dist/connections/harness-wiring.js +1 -1
- package/dist/connections/in-memory-connection.d.ts +2 -0
- package/dist/connections/in-memory-connection.js +5 -0
- package/dist/connections/model-tools.d.ts +35 -12
- package/dist/connections/model-tools.js +348 -202
- package/dist/connections/registry-types.d.ts +7 -0
- package/dist/connections/registry.d.ts +35 -6
- package/dist/connections/registry.js +183 -39
- package/dist/connections/step-tools.d.ts +64 -0
- package/dist/connections/step-tools.js +154 -0
- package/dist/connections/views.d.ts +3 -2
- package/dist/connections/views.js +15 -6
- package/dist/harness/agent-harness.d.ts +11 -1
- package/dist/harness/agent-harness.js +69 -8
- package/dist/harness/connection-gate.d.ts +49 -8
- package/dist/harness/connection-gate.js +137 -26
- package/dist/harness/deps/interpreter.d.ts +3 -3
- package/dist/harness/deps/interpreter.js +3 -3
- package/dist/harness/harness-options.d.ts +4 -3
- package/dist/harness/model-call.d.ts +0 -1
- package/dist/harness/model-call.js +0 -48
- package/dist/harness/model-request.js +3 -1
- package/dist/harness/model-schema.d.ts +1 -19
- package/dist/harness/model-schema.js +0 -28
- package/dist/harness/model-tool-round.js +2 -2
- package/dist/index.d.ts +5 -5
- package/dist/index.js +1 -1
- package/dist/interpreter/acp-events.d.ts +25 -1
- package/dist/interpreter/acp-events.js +53 -1
- package/dist/interpreter/acp-layers.d.ts +20 -0
- package/dist/interpreter/acp-layers.js +55 -0
- package/dist/interpreter/acp-tools.d.ts +108 -0
- package/dist/interpreter/acp-tools.js +462 -0
- package/dist/interpreter/collect-tools.d.ts +28 -0
- package/dist/interpreter/collect-tools.js +112 -15
- package/dist/interpreter/connection-steps.d.ts +0 -2
- package/dist/interpreter/connection-steps.js +30 -8
- package/dist/interpreter/execute-acp-agent.js +263 -72
- package/dist/interpreter/execute-action.d.ts +76 -1
- package/dist/interpreter/execute-action.js +35 -42
- package/dist/interpreter/execute.d.ts +2 -0
- package/dist/interpreter/execute.js +24 -1
- package/dist/interpreter/verify-host.js +2 -1
- package/dist/runtime/tool-call.d.ts +67 -0
- package/dist/runtime/tool-call.js +244 -0
- package/dist/runtime/tool-context.d.ts +1 -2
- package/dist/runtime/tool-context.js +6 -2
- package/dist/schemas/workflow.d.ts +1 -1
- package/dist/types/connection-entry.d.ts +22 -0
- package/dist/types/connection-entry.js +8 -0
- package/dist/util/harness-internals.d.ts +2 -0
- package/dist/util/root-context.d.ts +3 -0
- package/dist/util/root-context.js +8 -0
- package/dist/util/tool-name.d.ts +8 -0
- package/dist/util/tool-name.js +10 -0
- package/package.json +3 -3
- package/schema/noetic-workflow.schema.json +498 -128
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs Noetic tools on an ACP agent's behalf.
|
|
3
|
+
*
|
|
4
|
+
* The agent reaches the tools over MCP; each call it makes lands here and runs
|
|
5
|
+
* through the same pipeline as a `callModel` tool call — the drain hold, the
|
|
6
|
+
* `beforeToolCall` layers, validation, `needsApproval`, execution, the result
|
|
7
|
+
* item, its events and span — so the agent's model sees exactly the string a
|
|
8
|
+
* model would, and the host sees the same record.
|
|
9
|
+
*/
|
|
10
|
+
import { bridgedToolTitles, defaultItemSchemaRegistry, frameworkCast, namesBridgedToolCall, } from '@noetic-tools/types';
|
|
11
|
+
import { ACP_TOOL_SERVER_NAME, acpDelegateStepOf, checkedAcpTools, } from '../builders/acp-agent-checks.js';
|
|
12
|
+
import { orderedCallId } from '../connections/durability.js';
|
|
13
|
+
import { offeredTools, stepConnectionsOf } from '../connections/step-tools.js';
|
|
14
|
+
import { ToolAttr } from '../observability/genai-attributes.js';
|
|
15
|
+
import { drainsOf } from '../runtime/drain.js';
|
|
16
|
+
import { createToolResultItem, executeToolCall, resolveWireJsonSchema } from '../runtime/tool-call.js';
|
|
17
|
+
import { sanitizeToolNameForWire } from '../util/tool-name.js';
|
|
18
|
+
import { openChildSpan, traceChild } from './child-span.js';
|
|
19
|
+
import { deduplicateTools } from './collect-tools.js';
|
|
20
|
+
import { resolveLazy, resolveToolsAndRestrictions, splitResolvedTools } from './execute-action.js';
|
|
21
|
+
import { isFunctionCall } from './typeguards.js';
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region Constants
|
|
24
|
+
/** MCP requires an object at the root of every input schema. */
|
|
25
|
+
const WRAPPED_ARG = 'value';
|
|
26
|
+
/**
|
|
27
|
+
* An agent announces a call over ACP and makes it over MCP; the two arrive on
|
|
28
|
+
* different connections. A call that beats its announcement waits this long
|
|
29
|
+
* for it, so its events and items carry the agent's id.
|
|
30
|
+
*/
|
|
31
|
+
const ANNOUNCEMENT_GRACE_MS = 50;
|
|
32
|
+
const CANCELLED_OUTPUT = 'Error: the step was cancelled before this tool call ran.';
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region Listing
|
|
35
|
+
/**
|
|
36
|
+
* An object root, which MCP and the providers behind agents accept as is.
|
|
37
|
+
* A root union is not one: Anthropic rejects `oneOf`/`anyOf`/`allOf` at the
|
|
38
|
+
* top of a tool schema, so a union is wrapped like any other root.
|
|
39
|
+
*/
|
|
40
|
+
function isObjectSchema(schema) {
|
|
41
|
+
return (schema.type === 'object' &&
|
|
42
|
+
schema.oneOf === undefined &&
|
|
43
|
+
schema.anyOf === undefined &&
|
|
44
|
+
schema.allOf === undefined);
|
|
45
|
+
}
|
|
46
|
+
/** A `$ref` into the old root, pointed at where the schema now sits. */
|
|
47
|
+
function rehomeRef(ref) {
|
|
48
|
+
if (!ref.startsWith('#') || ref.startsWith('#/definitions') || ref.startsWith('#/$defs')) {
|
|
49
|
+
return ref;
|
|
50
|
+
}
|
|
51
|
+
return `#/properties/${WRAPPED_ARG}${ref.slice(1)}`;
|
|
52
|
+
}
|
|
53
|
+
function rehomeRefs(value) {
|
|
54
|
+
if (Array.isArray(value)) {
|
|
55
|
+
return value.map(rehomeRefs);
|
|
56
|
+
}
|
|
57
|
+
if (typeof value !== 'object' || value === null) {
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
return Object.fromEntries(Object.entries(value).map(([key, nested]) => [
|
|
61
|
+
key,
|
|
62
|
+
key === '$ref' && typeof nested === 'string' ? rehomeRef(nested) : rehomeRefs(nested),
|
|
63
|
+
]));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A schema with a non-object root, carried as the one property `value`. Its
|
|
67
|
+
* definitions move to the new root and refs into the old root follow it, so
|
|
68
|
+
* every `$ref` still resolves.
|
|
69
|
+
*/
|
|
70
|
+
function wrapSchema(schema) {
|
|
71
|
+
const { $schema: _dialect, definitions, $defs, ...inner } = schema;
|
|
72
|
+
return {
|
|
73
|
+
...(definitions !== undefined
|
|
74
|
+
? {
|
|
75
|
+
definitions,
|
|
76
|
+
}
|
|
77
|
+
: {}),
|
|
78
|
+
...($defs !== undefined
|
|
79
|
+
? {
|
|
80
|
+
$defs,
|
|
81
|
+
}
|
|
82
|
+
: {}),
|
|
83
|
+
type: 'object',
|
|
84
|
+
properties: {
|
|
85
|
+
[WRAPPED_ARG]: rehomeRefs(inner),
|
|
86
|
+
},
|
|
87
|
+
required: [
|
|
88
|
+
WRAPPED_ARG,
|
|
89
|
+
],
|
|
90
|
+
additionalProperties: false,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function titleOf(tool) {
|
|
94
|
+
const title = tool.acp?.title;
|
|
95
|
+
return typeof title === 'string' ? title : undefined;
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region Item merge
|
|
99
|
+
function isOutputFor(item, callId) {
|
|
100
|
+
return item.type === 'function_call_output' && item.callId === callId;
|
|
101
|
+
}
|
|
102
|
+
/** The agent's own record of a call it made through the bridge, by its title or id. */
|
|
103
|
+
function namesBridgedCall(item, wireName) {
|
|
104
|
+
return namesBridgedToolCall(ACP_TOOL_SERVER_NAME, wireName, {
|
|
105
|
+
toolCallId: item.callId,
|
|
106
|
+
title: item.name,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Replace the agent's account of each bridged call with the bridge's own pair,
|
|
111
|
+
* in the agent's place in the turn. The pair keeps the bridge's `callId` — the
|
|
112
|
+
* one its events and approval saw — and joins the agent's record by that id,
|
|
113
|
+
* or else by the first unclaimed exact title. A call the agent never reported
|
|
114
|
+
* is appended.
|
|
115
|
+
*/
|
|
116
|
+
export function mergeBridgedItems(items, calls) {
|
|
117
|
+
const adopted = [];
|
|
118
|
+
const merged = [
|
|
119
|
+
...items,
|
|
120
|
+
];
|
|
121
|
+
const claimed = new Set();
|
|
122
|
+
const unclaimedCall = (match) => merged.findIndex((item) => isFunctionCall(item) && !claimed.has(item.callId) && match(item));
|
|
123
|
+
for (const call of calls) {
|
|
124
|
+
const byId = unclaimedCall((item) => item.callId === call.callItem.callId);
|
|
125
|
+
const index = byId >= 0 ? byId : unclaimedCall((item) => namesBridgedCall(item, call.wireName));
|
|
126
|
+
const found = index >= 0 ? merged[index] : undefined;
|
|
127
|
+
if (!found || !isFunctionCall(found)) {
|
|
128
|
+
merged.push(call.callItem, call.outputItem);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
claimed.add(found.callId);
|
|
132
|
+
if (found.callId !== call.callItem.callId) {
|
|
133
|
+
adopted.push(call.callItem.callId);
|
|
134
|
+
}
|
|
135
|
+
const outputIndex = merged.findIndex((item) => isOutputFor(item, found.callId));
|
|
136
|
+
merged[index] = call.callItem;
|
|
137
|
+
if (outputIndex >= 0) {
|
|
138
|
+
merged[outputIndex] = call.outputItem;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
merged.splice(index + 1, 0, call.outputItem);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
items: merged,
|
|
146
|
+
adopted,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
//#endregion
|
|
150
|
+
//#region Runner
|
|
151
|
+
/**
|
|
152
|
+
* The tool host a connection serves. Calls run one at a time, in arrival
|
|
153
|
+
* order — the `callModel` tool loop never runs two at once, and agents that
|
|
154
|
+
* call in parallel would otherwise race on shared state.
|
|
155
|
+
*/
|
|
156
|
+
export class AcpToolRunner {
|
|
157
|
+
source;
|
|
158
|
+
serverName = ACP_TOOL_SERVER_NAME;
|
|
159
|
+
tools;
|
|
160
|
+
byWireName;
|
|
161
|
+
wrapped;
|
|
162
|
+
binding;
|
|
163
|
+
queue = Promise.resolve();
|
|
164
|
+
finished = [];
|
|
165
|
+
round = 0;
|
|
166
|
+
/** Set when a call's pipeline threw: the turn is failing, so no further call runs. */
|
|
167
|
+
failed = false;
|
|
168
|
+
/** The agent's announced tool calls, by id, with their titles. */
|
|
169
|
+
announced = new Map();
|
|
170
|
+
/** Ids already given to a call this session; an agent may not reuse one. */
|
|
171
|
+
claimed = new Set();
|
|
172
|
+
constructor(source) {
|
|
173
|
+
this.source = source;
|
|
174
|
+
const wrapped = new Set();
|
|
175
|
+
this.tools = source.map((tool) => {
|
|
176
|
+
const name = sanitizeToolNameForWire(tool.name);
|
|
177
|
+
const schema = resolveWireJsonSchema({
|
|
178
|
+
schema: tool.input,
|
|
179
|
+
explicitJsonSchema: tool.inputJsonSchema,
|
|
180
|
+
what: `input schema of tool '${tool.name}'`,
|
|
181
|
+
});
|
|
182
|
+
if (!isObjectSchema(schema)) {
|
|
183
|
+
wrapped.add(name);
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
name,
|
|
187
|
+
description: tool.description,
|
|
188
|
+
inputSchema: isObjectSchema(schema) ? schema : wrapSchema(schema),
|
|
189
|
+
title: titleOf(tool),
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
this.wrapped = wrapped;
|
|
193
|
+
this.byWireName = new Map(source.map((tool) => [
|
|
194
|
+
sanitizeToolNameForWire(tool.name),
|
|
195
|
+
tool,
|
|
196
|
+
]));
|
|
197
|
+
}
|
|
198
|
+
/** Point the runner at the step whose turn is about to start. */
|
|
199
|
+
bind(binding) {
|
|
200
|
+
this.binding = binding;
|
|
201
|
+
this.round = 0;
|
|
202
|
+
this.failed = false;
|
|
203
|
+
}
|
|
204
|
+
/** Stop running calls: the turn they belonged to has ended. */
|
|
205
|
+
unbind() {
|
|
206
|
+
this.binding = undefined;
|
|
207
|
+
this.announced.clear();
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Settles once no call is queued or running. An agent may end its turn
|
|
211
|
+
* while a call it gave up on still runs; that call still belongs to the
|
|
212
|
+
* turn, as an in-flight `callModel` call belongs to its aborted round.
|
|
213
|
+
*/
|
|
214
|
+
async idle() {
|
|
215
|
+
let settled;
|
|
216
|
+
do {
|
|
217
|
+
settled = this.queue;
|
|
218
|
+
await settled;
|
|
219
|
+
} while (settled !== this.queue);
|
|
220
|
+
}
|
|
221
|
+
/** The tool's wire name when `title` is how an agent titles a call to it. */
|
|
222
|
+
wireNameOf(title) {
|
|
223
|
+
return this.tools.find((tool) => bridgedToolTitles(this.serverName, tool.name).includes(title))
|
|
224
|
+
?.name;
|
|
225
|
+
}
|
|
226
|
+
/** Note an agent `tool_call`, so a call without an id in `_meta` can take its id. */
|
|
227
|
+
observe(update) {
|
|
228
|
+
if (update.sessionUpdate !== 'tool_call') {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
this.announced.set(update.toolCallId, update.title);
|
|
232
|
+
}
|
|
233
|
+
/** The calls finished since the last take, in the order they ran. */
|
|
234
|
+
takeFinished() {
|
|
235
|
+
const finished = this.finished;
|
|
236
|
+
this.finished = [];
|
|
237
|
+
return finished;
|
|
238
|
+
}
|
|
239
|
+
call = (call) => {
|
|
240
|
+
const next = this.queue.then(() => this.run(call));
|
|
241
|
+
this.queue = next.catch(() => undefined);
|
|
242
|
+
return next;
|
|
243
|
+
};
|
|
244
|
+
async run(call) {
|
|
245
|
+
const binding = this.binding;
|
|
246
|
+
if (!binding) {
|
|
247
|
+
return {
|
|
248
|
+
output: `Error: unknown tool '${call.name}'`,
|
|
249
|
+
isError: true,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
await drainsOf(binding.ctx)?.hold(binding.ctx);
|
|
253
|
+
if (call.agentCallId !== undefined && this.claimed.has(call.agentCallId)) {
|
|
254
|
+
return {
|
|
255
|
+
output: `Error: tool call id '${call.agentCallId}' was already used in this session.`,
|
|
256
|
+
isError: true,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
const callId = await this.callIdFor(binding, call);
|
|
260
|
+
// Like the `callModel` loop, which starts no call once its step is aborted
|
|
261
|
+
// or a call's pipeline has thrown. Checked after every wait.
|
|
262
|
+
if (this.failed || binding.ctx.aborted || call.signal.aborted) {
|
|
263
|
+
return {
|
|
264
|
+
output: CANCELLED_OUTPUT,
|
|
265
|
+
isError: true,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
try {
|
|
269
|
+
return await this.runInRound(binding, call, callId);
|
|
270
|
+
}
|
|
271
|
+
catch (e) {
|
|
272
|
+
this.failed = true;
|
|
273
|
+
if (this.binding === binding) {
|
|
274
|
+
binding.fail(e);
|
|
275
|
+
}
|
|
276
|
+
return {
|
|
277
|
+
output: `Error: ${e instanceof Error ? e.message : String(e)}`,
|
|
278
|
+
isError: true,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
async runInRound(binding, call, callId) {
|
|
283
|
+
const round = this.round;
|
|
284
|
+
this.round += 1;
|
|
285
|
+
binding.events.toolEvent('tool_round_started', {
|
|
286
|
+
round,
|
|
287
|
+
toolCount: 1,
|
|
288
|
+
});
|
|
289
|
+
const tool = this.byWireName.get(call.name);
|
|
290
|
+
const span = openChildSpan(binding.ctx, 'tool.call', binding.stepId);
|
|
291
|
+
span.setAttribute(ToolAttr.NAME, call.name);
|
|
292
|
+
span.setAttribute(ToolAttr.NEEDS_APPROVAL, tool?.needsApproval ?? false);
|
|
293
|
+
const result = await traceChild(binding.ctx, span, async () => {
|
|
294
|
+
const executed = await this.execute(binding, call, callId, tool);
|
|
295
|
+
if (executed.approval !== undefined) {
|
|
296
|
+
span.setAttribute(ToolAttr.APPROVAL, executed.approval);
|
|
297
|
+
}
|
|
298
|
+
return executed;
|
|
299
|
+
});
|
|
300
|
+
binding.events.toolEvent('tool_round_completed', {
|
|
301
|
+
round,
|
|
302
|
+
toolCount: 1,
|
|
303
|
+
});
|
|
304
|
+
return {
|
|
305
|
+
output: result.output,
|
|
306
|
+
isError: result.error === true,
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* The agent's own id for the call: from its MCP request when it sent one,
|
|
311
|
+
* else from its matching announced `tool_call`, so events, approval, and
|
|
312
|
+
* items all carry the id the agent's own records use. A call the agent never
|
|
313
|
+
* named gets one from the step and its order, which a resumed step repeats.
|
|
314
|
+
*/
|
|
315
|
+
async callIdFor(binding, call) {
|
|
316
|
+
if (call.agentCallId !== undefined) {
|
|
317
|
+
this.claimed.add(call.agentCallId);
|
|
318
|
+
return call.agentCallId;
|
|
319
|
+
}
|
|
320
|
+
const announced = this.announcedId(call.name) ??
|
|
321
|
+
(await new Promise((resolve) => setTimeout(() => resolve(this.announcedId(call.name)), ANNOUNCEMENT_GRACE_MS)));
|
|
322
|
+
const callId = announced ?? orderedCallId(this, binding.ctx, binding.stepId);
|
|
323
|
+
this.claimed.add(callId);
|
|
324
|
+
return callId;
|
|
325
|
+
}
|
|
326
|
+
announcedId(wireName) {
|
|
327
|
+
for (const [id, title] of this.announced) {
|
|
328
|
+
if (!this.claimed.has(id) &&
|
|
329
|
+
namesBridgedToolCall(this.serverName, wireName, {
|
|
330
|
+
toolCallId: id,
|
|
331
|
+
title,
|
|
332
|
+
})) {
|
|
333
|
+
return id;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return undefined;
|
|
337
|
+
}
|
|
338
|
+
unwrap(call) {
|
|
339
|
+
if (!this.wrapped.has(call.name)) {
|
|
340
|
+
return call.args;
|
|
341
|
+
}
|
|
342
|
+
if (typeof call.args !== 'object' || call.args === null) {
|
|
343
|
+
return undefined;
|
|
344
|
+
}
|
|
345
|
+
return Reflect.get(call.args, WRAPPED_ARG);
|
|
346
|
+
}
|
|
347
|
+
async execute(binding, call, callId, tool) {
|
|
348
|
+
const args = this.unwrap(call);
|
|
349
|
+
binding.events.toolEvent('tool_call_started', {
|
|
350
|
+
name: call.name,
|
|
351
|
+
callId,
|
|
352
|
+
});
|
|
353
|
+
const result = await executeToolCall({
|
|
354
|
+
toolName: call.name,
|
|
355
|
+
args,
|
|
356
|
+
tools: this.source,
|
|
357
|
+
context: binding.ctx,
|
|
358
|
+
harness: binding.ctx.harness,
|
|
359
|
+
layers: binding.layers,
|
|
360
|
+
callId,
|
|
361
|
+
});
|
|
362
|
+
const callItem = {
|
|
363
|
+
id: callId,
|
|
364
|
+
type: 'function_call',
|
|
365
|
+
status: 'completed',
|
|
366
|
+
callId,
|
|
367
|
+
name: call.name,
|
|
368
|
+
arguments: JSON.stringify(args ?? {}),
|
|
369
|
+
};
|
|
370
|
+
const outputItem = createToolResultItem({
|
|
371
|
+
output: result.output,
|
|
372
|
+
callId,
|
|
373
|
+
roundItemSchemas: harnessItemSchemas(binding.ctx).extend(tool?.itemSchemas),
|
|
374
|
+
tool,
|
|
375
|
+
callItem,
|
|
376
|
+
args,
|
|
377
|
+
result: result.result,
|
|
378
|
+
error: result.error,
|
|
379
|
+
});
|
|
380
|
+
this.finished.push({
|
|
381
|
+
wireName: call.name,
|
|
382
|
+
callItem,
|
|
383
|
+
outputItem,
|
|
384
|
+
});
|
|
385
|
+
binding.events.itemAppended(outputItem);
|
|
386
|
+
binding.events.toolEvent('tool_call_completed', {
|
|
387
|
+
name: call.name,
|
|
388
|
+
callId,
|
|
389
|
+
error: result.error === true,
|
|
390
|
+
output: result.output,
|
|
391
|
+
});
|
|
392
|
+
return result;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* The harness's own item schema registry, which the `callModel` tool loop
|
|
397
|
+
* validates results against — not the context's, which layers extend.
|
|
398
|
+
*/
|
|
399
|
+
function harnessItemSchemas(ctx) {
|
|
400
|
+
return (frameworkCast(ctx.harness).itemSchemas ?? defaultItemSchemaRegistry);
|
|
401
|
+
}
|
|
402
|
+
//#endregion
|
|
403
|
+
//#region Public API
|
|
404
|
+
/**
|
|
405
|
+
* Resolve the step's tools into a runner, or `undefined` when it serves none.
|
|
406
|
+
* The set is what a `callModel` step's model may call: `tools: []` is none;
|
|
407
|
+
* `tools` given are those tools and the tools of the connections they list,
|
|
408
|
+
* by their harness names, plus the context layers' tools when the run has no
|
|
409
|
+
* unified tool set; no `tools` is every tool the run offers. A tool that runs
|
|
410
|
+
* this very step is left out, the same tool listed twice counts once, and
|
|
411
|
+
* server tools and colliding wire names are configuration errors.
|
|
412
|
+
*
|
|
413
|
+
* @throws NoeticConfigError `CONNECTION_INVALID` when function-form `tools`
|
|
414
|
+
* name a connection the turn never attached.
|
|
415
|
+
*/
|
|
416
|
+
export async function resolveAcpTools(step, ctx, layers) {
|
|
417
|
+
const baseCtx = frameworkCast(ctx);
|
|
418
|
+
const entries = await resolveLazy(step.tools, ctx);
|
|
419
|
+
if (entries && entries.length === 0) {
|
|
420
|
+
return undefined;
|
|
421
|
+
}
|
|
422
|
+
const listed = splitResolvedTools(step, entries, baseCtx);
|
|
423
|
+
if (listed) {
|
|
424
|
+
checkedAcpTools(step.id, [
|
|
425
|
+
...listed.tools,
|
|
426
|
+
...listed.serverTools,
|
|
427
|
+
]);
|
|
428
|
+
}
|
|
429
|
+
const tools = checkedAcpTools(step.id, offeredAcpTools(listed, layers, baseCtx).filter((tool) => acpDelegateStepOf(tool) !== step.id));
|
|
430
|
+
return tools.length > 0 ? new AcpToolRunner(tools) : undefined;
|
|
431
|
+
}
|
|
432
|
+
//#endregion
|
|
433
|
+
//#region Helpers
|
|
434
|
+
/**
|
|
435
|
+
* What the step offers of the turn's tool set: its own tools as given, and
|
|
436
|
+
* each listed connection's tools as the turn built them. With no `tools`, the
|
|
437
|
+
* run's whole set but for connections only other steps list, or those past
|
|
438
|
+
* its ceiling.
|
|
439
|
+
*/
|
|
440
|
+
function offeredAcpTools(listed, layers, ctx) {
|
|
441
|
+
const unified = ctx.unifiedTools;
|
|
442
|
+
if (!unified || unified.length === 0) {
|
|
443
|
+
return resolveToolsAndRestrictions(listed, layers, ctx).tools ?? [];
|
|
444
|
+
}
|
|
445
|
+
const turn = stepConnectionsOf(ctx);
|
|
446
|
+
const picked = offeredTools(unified, {
|
|
447
|
+
tools: [],
|
|
448
|
+
serverTools: [],
|
|
449
|
+
picks: listed?.picks ?? [],
|
|
450
|
+
}, turn) ?? [];
|
|
451
|
+
// A ceiling narrows only connection tools, so the step keeps the rest whole.
|
|
452
|
+
const own = listed && !listed.ceiling
|
|
453
|
+
? listed.tools
|
|
454
|
+
: (offeredTools(unified, listed, turn) ?? [
|
|
455
|
+
...unified,
|
|
456
|
+
]);
|
|
457
|
+
return deduplicateTools([
|
|
458
|
+
...own,
|
|
459
|
+
...picked,
|
|
460
|
+
]);
|
|
461
|
+
}
|
|
462
|
+
//#endregion
|
|
@@ -1,10 +1,38 @@
|
|
|
1
1
|
import type { Step, Tool } from '@noetic-tools/types';
|
|
2
|
+
import type { StepConnections } from '../connections/step-tools.js';
|
|
3
|
+
import type { AttachedEntry } from '../types/connection-entry.js';
|
|
2
4
|
/**
|
|
3
5
|
* Recursively walks a step tree and collects all Tool instances
|
|
4
6
|
* declared on LLM steps. Used to build the unified tool set
|
|
5
7
|
* that gets sent with every LLM call for prompt cache efficiency.
|
|
6
8
|
*/
|
|
7
9
|
export declare function collectAllTools<TContext = unknown, I = unknown, O = unknown>(step: Step<TContext, I, O>): Tool[];
|
|
10
|
+
/**
|
|
11
|
+
* The connection entries a step tree lists in its `callModel` and `acpAgent`
|
|
12
|
+
* steps' eager `tools` (an entry, or a pick made from one), and those a JSON
|
|
13
|
+
* document's `connections` declare, named, so the turn connects them with its
|
|
14
|
+
* own before its tool set is built. Repeats of one entry fold together.
|
|
15
|
+
*
|
|
16
|
+
* @throws NoeticConfigError `CONNECTION_INVALID` for an entry with no name.
|
|
17
|
+
*/
|
|
18
|
+
export declare function collectStepEntries(step: Step): AttachedEntry[];
|
|
19
|
+
/**
|
|
20
|
+
* Checks that every connection a model step picks by name is one the turn
|
|
21
|
+
* can see — the harness's, the turn's, or one a step or document attaches —
|
|
22
|
+
* once they are attached, so a misspelt name fails before any step runs.
|
|
23
|
+
*
|
|
24
|
+
* @throws NoeticConfigError `CONNECTION_UNKNOWN` for a pick naming no connection.
|
|
25
|
+
*/
|
|
26
|
+
export declare function checkPickNames(step: Step, turn: StepConnections): void;
|
|
27
|
+
/**
|
|
28
|
+
* Checks each model step that forces a tool (`toolChoice: { tool }`) and lists
|
|
29
|
+
* connections: the tool must be one the step is offered from the turn's set.
|
|
30
|
+
* Connection tool names come from the server, so the builder could not check
|
|
31
|
+
* them.
|
|
32
|
+
*
|
|
33
|
+
* @throws NoeticConfigError `UNKNOWN_TOOL_CHOICE` when the forced tool is not offered.
|
|
34
|
+
*/
|
|
35
|
+
export declare function checkToolChoices(step: Step, unified: ReadonlyArray<Tool>, turn: StepConnections | undefined): void;
|
|
8
36
|
/**
|
|
9
37
|
* Deduplicates tools by name, keeping the first occurrence.
|
|
10
38
|
*/
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import { frameworkCast, isServerToolSpec } from '@noetic-tools/types';
|
|
1
|
+
import { frameworkCast, isConnectionEntry, isConnectionPick, isServerToolSpec, NoeticConfigError, } from '@noetic-tools/types';
|
|
2
|
+
import { documentEntriesOf } from '../builders/document-entries.js';
|
|
3
|
+
import { namedFromList } from '../connections/entries.js';
|
|
4
|
+
import { unknownConnectionError } from '../connections/errors.js';
|
|
5
|
+
import { offeredTools, splitStepTools } from '../connections/step-tools.js';
|
|
2
6
|
import { stepChildren } from './step-walk.js';
|
|
3
7
|
/**
|
|
4
8
|
* Recursively walks a step tree and collects all Tool instances
|
|
@@ -12,6 +16,85 @@ export function collectAllTools(step) {
|
|
|
12
16
|
walkStep(frameworkCast(step), tools);
|
|
13
17
|
return deduplicateTools(tools);
|
|
14
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* The connection entries a step tree lists in its `callModel` and `acpAgent`
|
|
21
|
+
* steps' eager `tools` (an entry, or a pick made from one), and those a JSON
|
|
22
|
+
* document's `connections` declare, named, so the turn connects them with its
|
|
23
|
+
* own before its tool set is built. Repeats of one entry fold together.
|
|
24
|
+
*
|
|
25
|
+
* @throws NoeticConfigError `CONNECTION_INVALID` for an entry with no name.
|
|
26
|
+
*/
|
|
27
|
+
export function collectStepEntries(step) {
|
|
28
|
+
const found = [];
|
|
29
|
+
walkModelSteps(step, (model, tools) => {
|
|
30
|
+
const where = `${model.kind === 'callModel' ? 'callModel' : 'acpAgent'}('${model.id}') tools`;
|
|
31
|
+
for (const entry of tools) {
|
|
32
|
+
const listed = isConnectionPick(entry) ? entry.entry : entry;
|
|
33
|
+
if (listed === undefined || !isConnectionEntry(listed)) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
found.push({
|
|
37
|
+
...namedFromList(listed, where),
|
|
38
|
+
scope: 'step',
|
|
39
|
+
where,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
walkSteps(step, (each) => {
|
|
44
|
+
found.push(...documentEntriesOf(each));
|
|
45
|
+
});
|
|
46
|
+
return found;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Checks that every connection a model step picks by name is one the turn
|
|
50
|
+
* can see — the harness's, the turn's, or one a step or document attaches —
|
|
51
|
+
* once they are attached, so a misspelt name fails before any step runs.
|
|
52
|
+
*
|
|
53
|
+
* @throws NoeticConfigError `CONNECTION_UNKNOWN` for a pick naming no connection.
|
|
54
|
+
*/
|
|
55
|
+
export function checkPickNames(step, turn) {
|
|
56
|
+
const visible = turn.runtime.visibleNames(turn.rootId);
|
|
57
|
+
walkModelSteps(step, (_model, tools) => {
|
|
58
|
+
for (const entry of tools) {
|
|
59
|
+
if (!isConnectionPick(entry) || visible.includes(entry.connection)) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
throw unknownConnectionError(entry.connection, visible);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Checks each model step that forces a tool (`toolChoice: { tool }`) and lists
|
|
68
|
+
* connections: the tool must be one the step is offered from the turn's set.
|
|
69
|
+
* Connection tool names come from the server, so the builder could not check
|
|
70
|
+
* them.
|
|
71
|
+
*
|
|
72
|
+
* @throws NoeticConfigError `UNKNOWN_TOOL_CHOICE` when the forced tool is not offered.
|
|
73
|
+
*/
|
|
74
|
+
export function checkToolChoices(step, unified, turn) {
|
|
75
|
+
walkModelSteps(step, (model, tools) => {
|
|
76
|
+
if (model.kind !== 'callModel') {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const choice = model.toolChoice;
|
|
80
|
+
if (typeof choice !== 'object') {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const listed = splitStepTools(tools);
|
|
84
|
+
if (listed.picks.length === 0) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const offered = (offeredTools(unified, listed, turn) ?? []).map((tool) => tool.name);
|
|
88
|
+
if (offered.includes(choice.tool)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
throw new NoeticConfigError({
|
|
92
|
+
code: 'UNKNOWN_TOOL_CHOICE',
|
|
93
|
+
message: `callModel(${JSON.stringify(model.id)}) forces tool ${JSON.stringify(choice.tool)}, which its tools do not offer this turn.`,
|
|
94
|
+
hint: `Force one of the tools it is offered (${offered.join(', ') || 'none'}), or drop \`toolChoice\`.`,
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
15
98
|
/**
|
|
16
99
|
* Deduplicates tools by name, keeping the first occurrence.
|
|
17
100
|
*/
|
|
@@ -27,23 +110,37 @@ export function deduplicateTools(tools) {
|
|
|
27
110
|
];
|
|
28
111
|
}
|
|
29
112
|
function walkStep(step, out) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
113
|
+
// Inline server-tool specs (web search/fetch) are not client `Tool`s, and
|
|
114
|
+
// connection entries and picks name tools the turn builds from its
|
|
115
|
+
// connections — neither enters the pool from here.
|
|
116
|
+
walkModelSteps(step, (_model, tools) => {
|
|
117
|
+
for (const entry of tools) {
|
|
118
|
+
if (!isServerToolSpec(entry) && !isConnectionPick(entry) && !isConnectionEntry(entry)) {
|
|
119
|
+
out.push(entry);
|
|
120
|
+
}
|
|
33
121
|
}
|
|
34
|
-
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
function walkSteps(step, visit) {
|
|
125
|
+
visit(step);
|
|
126
|
+
for (const child of stepChildren(step)) {
|
|
127
|
+
walkSteps(child, visit);
|
|
35
128
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Calls `visit` with each `callModel` and `acpAgent` step and its eager
|
|
132
|
+
* `tools`. Function-form tools are resolved per execution from `ctx` and
|
|
133
|
+
* cannot contribute to the pre-computed unified set; consumers that need those
|
|
134
|
+
* tools in the pool register them via `AgentHarness.tools` instead.
|
|
135
|
+
*/
|
|
136
|
+
function walkModelSteps(step, visit) {
|
|
137
|
+
if (step.kind !== 'callModel' && step.kind !== 'acp-agent') {
|
|
138
|
+
for (const child of stepChildren(step)) {
|
|
139
|
+
walkModelSteps(child, visit);
|
|
140
|
+
}
|
|
42
141
|
return;
|
|
43
142
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
out.push(entry);
|
|
47
|
-
}
|
|
143
|
+
if (step.tools !== undefined && typeof step.tools !== 'function') {
|
|
144
|
+
visit(step, step.tools);
|
|
48
145
|
}
|
|
49
146
|
}
|
|
@@ -74,8 +74,6 @@ export declare function pathScopeKey(ctx: Context): string;
|
|
|
74
74
|
* when the harness has no connections.
|
|
75
75
|
*/
|
|
76
76
|
export declare function releasePathLeases(paths: ReadonlyArray<Context>): Promise<void>;
|
|
77
|
-
/** The ledger path of the step running on `ctx`, when it runs on a lane. */
|
|
78
|
-
export declare function stepPathOf(ctx: Context): string | undefined;
|
|
79
77
|
/** The run-and-step key a call carries, stable across a resume of the same run (spec 35 § Durability). */
|
|
80
78
|
export declare function idempotencyKeyFor(ctx: Context, stepId: string, ledgerKey?: string): string;
|
|
81
79
|
/**
|