@noetic-tools/core 5.20.0-next.1 → 5.20.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-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
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
* Action step handlers: run, llm, spawn, provide.
|
|
3
3
|
*/
|
|
4
4
|
import { createMessage, estimateTokens, extractAssistantText, frameworkCast, isNoeticError, isOutputCodec, NoeticConfigError, NoeticErrorImpl, } from '@noetic-tools/types';
|
|
5
|
+
import { checkLazyEntries, offeredTools, splitCeiling, splitStepTools, stepConnectionsOf, withOffered, } from '../connections/step-tools.js';
|
|
5
6
|
import { chargeSpent } from '../util/spent.js';
|
|
6
7
|
import { allocateBudgets, assembleView, ContextImpl, commitLayerUsage, computeLayerUsage, contextToExecCtx, createContextCacheStore, DEFAULT_PROJECTION, defaultItemSchemaRegistry, disposeLayers, emitFrameworkEvent, getBroadcaster, INTERRUPT_REASON, initLayers, lineageKey, noteCacheOutcome, resolveCacheConfig, resolveLayerTools, returnLayers, shouldEmit, snapshotCwdState, spawnLayers, unpinLayers, } from './action-deps.js';
|
|
7
|
-
import {
|
|
8
|
+
import { SteeringAction } from './action-types.js';
|
|
8
9
|
import { openChildSpan, traceChild } from './child-span.js';
|
|
9
10
|
import { cloneWithGuard } from './clone-guard.js';
|
|
10
11
|
import { collectAllTools, deduplicateTools } from './collect-tools.js';
|
|
@@ -69,7 +70,7 @@ function computeDelay(retry, attempt) {
|
|
|
69
70
|
}
|
|
70
71
|
//#endregion
|
|
71
72
|
//#region llm
|
|
72
|
-
const MAX_STEERING_RETRIES = 3;
|
|
73
|
+
export const MAX_STEERING_RETRIES = 3;
|
|
73
74
|
const emptyRecall = [];
|
|
74
75
|
/**
|
|
75
76
|
* Merge re-render recall outputs over the base recall results, replacing entries
|
|
@@ -118,16 +119,21 @@ export async function resolveLazy(value, ctx) {
|
|
|
118
119
|
* every LLM call sends the full set and step.tools narrows via allowedToolNames.
|
|
119
120
|
*
|
|
120
121
|
* Semantics:
|
|
121
|
-
* step.tools = undefined → unrestricted (all tools, no allowedToolNames)
|
|
122
|
+
* step.tools = undefined → unrestricted (all tools, no allowedToolNames),
|
|
123
|
+
* except the tools of connections only some step lists
|
|
124
|
+
* (a document node's ceiling narrows its connections)
|
|
122
125
|
* step.tools = [] → no tools at all
|
|
123
|
-
* step.tools = [a, b] → full set sent, restrict to a and b
|
|
126
|
+
* step.tools = [a, b] → full set sent, restrict to a and b (a connection
|
|
127
|
+
* entry or pick restricts to its connection's tools,
|
|
128
|
+
* each cut to what it picks and sent in place)
|
|
124
129
|
*
|
|
125
130
|
* Fallback: when no unified set exists (e.g. harness.run() called directly),
|
|
126
131
|
* merge step tools with layer tools as before.
|
|
127
132
|
*/
|
|
128
|
-
function resolveToolsAndRestrictions(
|
|
133
|
+
export function resolveToolsAndRestrictions(listed, layers, ctx) {
|
|
134
|
+
const stepTools = listed?.tools;
|
|
129
135
|
// stepTools = [] → explicit opt-out
|
|
130
|
-
if (
|
|
136
|
+
if (listed && !listed.ceiling && listed.tools.length === 0 && listed.picks.length === 0) {
|
|
131
137
|
return {
|
|
132
138
|
tools: undefined,
|
|
133
139
|
allowedToolNames: undefined,
|
|
@@ -135,12 +141,10 @@ function resolveToolsAndRestrictions(stepTools, layers, ctx) {
|
|
|
135
141
|
}
|
|
136
142
|
const unified = ctx.unifiedTools;
|
|
137
143
|
if (unified && unified.length > 0) {
|
|
138
|
-
const
|
|
144
|
+
const offered = offeredTools(unified, listed, stepConnectionsOf(ctx));
|
|
139
145
|
return {
|
|
140
|
-
tools:
|
|
141
|
-
|
|
142
|
-
],
|
|
143
|
-
allowedToolNames,
|
|
146
|
+
tools: withOffered(unified, offered),
|
|
147
|
+
allowedToolNames: offered?.map((t) => t.name),
|
|
144
148
|
};
|
|
145
149
|
}
|
|
146
150
|
// Fallback: no unified set (direct harness.run() path)
|
|
@@ -245,41 +249,30 @@ async function finalizeStepOutput(params) {
|
|
|
245
249
|
return frameworkCast(params.lastText);
|
|
246
250
|
}
|
|
247
251
|
/**
|
|
248
|
-
* `
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
* (
|
|
253
|
-
*
|
|
254
|
-
*
|
|
252
|
+
* Splits a step's resolved `tools`. Client tools and connection picks flow
|
|
253
|
+
* through the unified-tool / allowedToolNames / steering machinery; server-tool
|
|
254
|
+
* specs bypass it and are stamped onto the model request (`_serverTools`) for
|
|
255
|
+
* the model caller to wrap via the SDK's `serverTool()`. `undefined`
|
|
256
|
+
* (unrestricted) is preserved, so the `[] = opt-out` semantics stay intact.
|
|
257
|
+
*
|
|
258
|
+
* @throws NoeticConfigError `CONNECTION_INVALID` when function-form tools return
|
|
259
|
+
* a connection the turn never attached.
|
|
255
260
|
*/
|
|
256
|
-
function
|
|
257
|
-
if (
|
|
258
|
-
return
|
|
259
|
-
clientStepTools: undefined,
|
|
260
|
-
serverToolSpecs: [],
|
|
261
|
-
};
|
|
261
|
+
export function splitResolvedTools(step, resolved, ctx) {
|
|
262
|
+
if (resolved === undefined) {
|
|
263
|
+
return splitCeiling(step);
|
|
262
264
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
for (const entry of resolvedStepTools) {
|
|
266
|
-
if (isServerToolSpec(entry)) {
|
|
267
|
-
serverToolSpecs.push(entry);
|
|
268
|
-
continue;
|
|
269
|
-
}
|
|
270
|
-
clientStepTools.push(entry);
|
|
265
|
+
if (typeof step.tools === 'function') {
|
|
266
|
+
checkLazyEntries(step.id, resolved, stepConnectionsOf(ctx));
|
|
271
267
|
}
|
|
272
|
-
return
|
|
273
|
-
clientStepTools,
|
|
274
|
-
serverToolSpecs,
|
|
275
|
-
};
|
|
268
|
+
return splitStepTools(resolved);
|
|
276
269
|
}
|
|
277
270
|
/**
|
|
278
271
|
* Append user input — through layer pipeline if layers exist, otherwise direct.
|
|
279
272
|
* The append pipeline may request a context re-render (e.g. a layer that
|
|
280
273
|
* expanded an input reference into new content); applied after recall.
|
|
281
274
|
*/
|
|
282
|
-
async function appendUserInput(params) {
|
|
275
|
+
export async function appendUserInput(params) {
|
|
283
276
|
const { ctx, layers, input } = params;
|
|
284
277
|
if (typeof input !== 'string' || input.length === 0) {
|
|
285
278
|
return [];
|
|
@@ -298,7 +291,7 @@ async function appendUserInput(params) {
|
|
|
298
291
|
* Splits the context budget across layers via `allocateBudgets`. An empty map
|
|
299
292
|
* when the step runs without layers.
|
|
300
293
|
*/
|
|
301
|
-
function computeBudgetMap(params) {
|
|
294
|
+
export function computeBudgetMap(params) {
|
|
302
295
|
const { layers, policy, systemPromptTokens } = params;
|
|
303
296
|
if (layers === undefined || layers.length === 0) {
|
|
304
297
|
return new Map();
|
|
@@ -321,7 +314,7 @@ function computeBudgetMap(params) {
|
|
|
321
314
|
* steering retry loop because retries replay the same context. Re-render
|
|
322
315
|
* requests collected from the input-append pipeline are applied on top.
|
|
323
316
|
*/
|
|
324
|
-
async function gatherRecallResults(params) {
|
|
317
|
+
export async function gatherRecallResults(params) {
|
|
325
318
|
const { ctx, layers, recallQuery, budgetMap, slotOf, rerenderRequests } = params;
|
|
326
319
|
if (layers === undefined || layers.length === 0) {
|
|
327
320
|
return emptyRecall;
|
|
@@ -507,14 +500,14 @@ export async function executeCallModel(step, input, ctx, layers) {
|
|
|
507
500
|
}
|
|
508
501
|
const baseInstructions = await resolveLazy(step.instructions, ctx);
|
|
509
502
|
const { outputCodec, outputSchema, resolvedInstructions } = resolveOutputMode(step.output, baseInstructions);
|
|
510
|
-
const
|
|
511
|
-
const
|
|
503
|
+
const listedTools = splitResolvedTools(step, await resolveLazy(step.tools, ctx), baseCtx);
|
|
504
|
+
const serverToolSpecs = listedTools?.serverTools ?? [];
|
|
512
505
|
const rerenderRequests = await appendUserInput({
|
|
513
506
|
ctx: baseCtx,
|
|
514
507
|
layers,
|
|
515
508
|
input,
|
|
516
509
|
});
|
|
517
|
-
const { tools: resolvedTools, allowedToolNames } = resolveToolsAndRestrictions(
|
|
510
|
+
const { tools: resolvedTools, allowedToolNames } = resolveToolsAndRestrictions(listedTools, layers, baseCtx);
|
|
518
511
|
// Resolve the projection policy (step override > harness default > fallback)
|
|
519
512
|
// and split the context budget across layers via allocateBudgets.
|
|
520
513
|
const policy = step.projection ?? baseCtx.harness.config.projection ?? DEFAULT_PROJECTION;
|
|
@@ -34,3 +34,5 @@ export declare function resolveLedgerKey<TContext, I, O>(step: Step<TContext, I,
|
|
|
34
34
|
* @throws `NoeticError` with kind `cancelled` if the context is aborted.
|
|
35
35
|
*/
|
|
36
36
|
export declare function execute<TContext = ContextData, I = unknown, O = unknown>(step: Step<TContext, I, O>, input: I, ctx: Context<TContext>): Promise<O>;
|
|
37
|
+
/** True while an `execute()` call is running a step on this root context. */
|
|
38
|
+
export declare function isRootRunning(ctx: Context): boolean;
|
|
@@ -180,7 +180,30 @@ async function recordToLedger(opts) {
|
|
|
180
180
|
* @throws `NoeticError` with kind `step_failed` if max depth is exceeded or an unknown step kind is encountered.
|
|
181
181
|
* @throws `NoeticError` with kind `cancelled` if the context is aborted.
|
|
182
182
|
*/
|
|
183
|
-
export
|
|
183
|
+
export function execute(step, input, ctx) {
|
|
184
|
+
// Only the outermost call on a root is marked; every step under it passes
|
|
185
|
+
// straight through, adding no frame to deep recursion.
|
|
186
|
+
if (ctx.parent !== null || runningRoots.has(ctx.id)) {
|
|
187
|
+
return executeStep(step, input, ctx);
|
|
188
|
+
}
|
|
189
|
+
runningRoots.add(ctx.id);
|
|
190
|
+
return executeStep(step, input, ctx).finally(() => {
|
|
191
|
+
runningRoots.delete(ctx.id);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Root contexts some `execute()` call is running a step on, by id: a step runs
|
|
196
|
+
* on a lane wrapping its context, which is a different object with the same id. A `harness.run`
|
|
197
|
+
* made from inside one (a tool running a step on `toolCtx.ctx`) joins that
|
|
198
|
+
* run rather than starting a new one, so it neither re-initializes the
|
|
199
|
+
* context's layers nor, on finishing, closes what the outer run still uses.
|
|
200
|
+
*/
|
|
201
|
+
const runningRoots = new Set();
|
|
202
|
+
/** True while an `execute()` call is running a step on this root context. */
|
|
203
|
+
export function isRootRunning(ctx) {
|
|
204
|
+
return runningRoots.has(ctx.id);
|
|
205
|
+
}
|
|
206
|
+
async function executeStep(step, input, ctx) {
|
|
184
207
|
const baseCtx = frameworkCast(ctx);
|
|
185
208
|
// Under a graceful stop no new step starts: it waits here until the tree is cancelled.
|
|
186
209
|
const drains = drainsOf(baseCtx);
|
|
@@ -8,9 +8,10 @@
|
|
|
8
8
|
import { frameworkCast, NoeticConfigError } from '@noetic-tools/types';
|
|
9
9
|
import { AnomalyKind, OvpChannelIdSchema, VerifyConfigErrorCode } from '@noetic-tools/types/verify';
|
|
10
10
|
import { z } from 'zod';
|
|
11
|
+
import { stepPathOf } from '../connections/durability.js';
|
|
11
12
|
import { createInMemoryArtifactStore } from '../runtime/in-memory-artifact-store.js';
|
|
12
13
|
import { harnessInternal } from '../util/harness-internals.js';
|
|
13
|
-
import { idempotencyKeyFor, runRootOf
|
|
14
|
+
import { idempotencyKeyFor, runRootOf } from './connection-steps.js';
|
|
14
15
|
//#region Constants
|
|
15
16
|
/** The resource a server lists its blind spots at (spec 36 § Parts every server provides). */
|
|
16
17
|
const COVERAGE_RESOURCE = 'noetic://coverage';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tool-call pipeline shared by every caller that runs a Noetic tool on a
|
|
3
|
+
* model's behalf: the `callModel` tool loop, and ACP agents calling bridged
|
|
4
|
+
* tools over MCP. Keeping one copy is what makes the two behave the same.
|
|
5
|
+
*/
|
|
6
|
+
import type { ContextLayer } from '@noetic-tools/context';
|
|
7
|
+
import type { AgentHarnessContract, Context, Item, ItemSchemaRegistry, StandardSchemaV1, Tool } from '@noetic-tools/types';
|
|
8
|
+
import type { ApprovalOutcome } from './tool-approval.js';
|
|
9
|
+
/**
|
|
10
|
+
* Resolve the JSON Schema sent to the model. Zod keeps its legacy-compatible
|
|
11
|
+
* fast path. For other validators, an explicit override wins over the Standard
|
|
12
|
+
* JSON Schema v1 companion trait.
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export declare function resolveWireJsonSchema(params: {
|
|
16
|
+
schema: StandardSchemaV1;
|
|
17
|
+
explicitJsonSchema?: Record<string, unknown>;
|
|
18
|
+
what: string;
|
|
19
|
+
}): Record<string, unknown>;
|
|
20
|
+
/** @internal */
|
|
21
|
+
export interface ExecuteToolCallParams {
|
|
22
|
+
toolName: string;
|
|
23
|
+
args: unknown;
|
|
24
|
+
tools: ReadonlyArray<Tool>;
|
|
25
|
+
context: Context;
|
|
26
|
+
harness: AgentHarnessContract;
|
|
27
|
+
layers?: ContextLayer[];
|
|
28
|
+
/**
|
|
29
|
+
* The call's id — the model's `function_call` id, or an ACP agent's own id
|
|
30
|
+
* for a bridged call. Keys the tool-UI region and, through
|
|
31
|
+
* `ToolExecutionContext.callId`, a connection tool's idempotency key.
|
|
32
|
+
*/
|
|
33
|
+
callId?: string;
|
|
34
|
+
}
|
|
35
|
+
/** @internal What one model-requested tool call produced. */
|
|
36
|
+
export interface ToolCallResult {
|
|
37
|
+
output: string;
|
|
38
|
+
result?: unknown;
|
|
39
|
+
error?: boolean;
|
|
40
|
+
/** Set when the tool declares `needsApproval` and the call reached the approver. */
|
|
41
|
+
approval?: ApprovalOutcome;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* @internal Execute a single tool call. Gates run in order: the
|
|
45
|
+
* `beforeToolCall` layers, input validation, then — for a tool that declares
|
|
46
|
+
* `needsApproval` — the harness approver. A refusal at any gate becomes an
|
|
47
|
+
* error tool result; the tool never runs.
|
|
48
|
+
*/
|
|
49
|
+
export declare function executeToolCall(params: ExecuteToolCallParams): Promise<ToolCallResult>;
|
|
50
|
+
/**
|
|
51
|
+
* Build and validate a `function_call_output` item for a tool result.
|
|
52
|
+
*
|
|
53
|
+
* `roundItemSchemas` must be the OWNER-SCOPED registry for the called tool
|
|
54
|
+
* (harness base extended with only that tool's `itemSchemas`), so one tool's
|
|
55
|
+
* `toolResults` schemas never reject a sibling tool's result items. Tools
|
|
56
|
+
* without `itemSchemas` fall back to the base structural parse.
|
|
57
|
+
*/
|
|
58
|
+
export declare function createToolResultItem({ output, callId, roundItemSchemas, tool, callItem, args, result, error, }: {
|
|
59
|
+
output: string;
|
|
60
|
+
callId: string;
|
|
61
|
+
roundItemSchemas: ItemSchemaRegistry;
|
|
62
|
+
tool?: Tool;
|
|
63
|
+
callItem?: Item;
|
|
64
|
+
args?: unknown;
|
|
65
|
+
result?: unknown;
|
|
66
|
+
error?: boolean;
|
|
67
|
+
}): Item;
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tool-call pipeline shared by every caller that runs a Noetic tool on a
|
|
3
|
+
* model's behalf: the `callModel` tool loop, and ACP agents calling bridged
|
|
4
|
+
* tools over MCP. Keeping one copy is what makes the two behave the same.
|
|
5
|
+
*/
|
|
6
|
+
import { frameworkCast, isStandardJsonSchema, isZodSchema, NoeticConfigError, SteeringAction, validateSchema, } from '@noetic-tools/types';
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
import { sanitizeToolNameForWire } from '../util/tool-name.js';
|
|
9
|
+
import { askApproval } from './tool-approval.js';
|
|
10
|
+
import { buildToolExecutionContext } from './tool-context.js';
|
|
11
|
+
import { emitToolUi } from './tool-ui.js';
|
|
12
|
+
//#region Wire names and schemas
|
|
13
|
+
/**
|
|
14
|
+
* Resolve the JSON Schema sent to the model. Zod keeps its legacy-compatible
|
|
15
|
+
* fast path. For other validators, an explicit override wins over the Standard
|
|
16
|
+
* JSON Schema v1 companion trait.
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export function resolveWireJsonSchema(params) {
|
|
20
|
+
const { schema, explicitJsonSchema, what } = params;
|
|
21
|
+
if (isZodSchema(schema)) {
|
|
22
|
+
return sanitizeJsonSchema(z.toJSONSchema(schema, {
|
|
23
|
+
target: 'draft-07',
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
if (explicitJsonSchema) {
|
|
27
|
+
return sanitizeJsonSchema(explicitJsonSchema);
|
|
28
|
+
}
|
|
29
|
+
if (isStandardJsonSchema(schema)) {
|
|
30
|
+
try {
|
|
31
|
+
return sanitizeJsonSchema(schema['~standard'].jsonSchema.input({
|
|
32
|
+
target: 'draft-07',
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// Fall through to the actionable configuration error below.
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
throw new NoeticConfigError({
|
|
40
|
+
code: 'MISSING_JSON_SCHEMA',
|
|
41
|
+
message: `The ${what} cannot be converted to JSON Schema.`,
|
|
42
|
+
hint: 'Use a schema implementing StandardJSONSchemaV1 or pass `inputJsonSchema` on tool() / `outputJsonSchema` on step.llm().',
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function sanitizeJsonSchema(value) {
|
|
46
|
+
return Object.fromEntries(Object.entries(value)
|
|
47
|
+
.filter(([key]) => !key.startsWith('~'))
|
|
48
|
+
.map(([key, nested]) => [
|
|
49
|
+
key,
|
|
50
|
+
sanitizeJsonSchemaValue(nested),
|
|
51
|
+
]));
|
|
52
|
+
}
|
|
53
|
+
function sanitizeJsonSchemaValue(value) {
|
|
54
|
+
if (Array.isArray(value)) {
|
|
55
|
+
return value.map(sanitizeJsonSchemaValue);
|
|
56
|
+
}
|
|
57
|
+
if (typeof value === 'object' && value !== null) {
|
|
58
|
+
return sanitizeJsonSchema(frameworkCast(value));
|
|
59
|
+
}
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
function isAsyncGenerator(value) {
|
|
63
|
+
if (typeof value !== 'object' || value === null) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return Symbol.asyncIterator in value;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* @internal Execute a single tool call. Gates run in order: the
|
|
70
|
+
* `beforeToolCall` layers, input validation, then — for a tool that declares
|
|
71
|
+
* `needsApproval` — the harness approver. A refusal at any gate becomes an
|
|
72
|
+
* error tool result; the tool never runs.
|
|
73
|
+
*/
|
|
74
|
+
export async function executeToolCall(params) {
|
|
75
|
+
// Model sees sanitised tool names (see `sanitizeToolNameForWire`). Match
|
|
76
|
+
// against both the original and sanitised name so internal identity (e.g.
|
|
77
|
+
// `plan/updatePrd` used by steering whitelists, skill docs, and the
|
|
78
|
+
// `plan()` layer's `beforeToolCall` hook) stays intact while the wire name is
|
|
79
|
+
// provider-compliant.
|
|
80
|
+
const matchedTool = params.tools.find((t) => t.name === params.toolName || sanitizeToolNameForWire(t.name) === params.toolName);
|
|
81
|
+
if (!matchedTool) {
|
|
82
|
+
return {
|
|
83
|
+
output: `Error: unknown tool '${params.toolName}'`,
|
|
84
|
+
error: true,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (params.layers && params.layers.length > 0) {
|
|
88
|
+
const decision = await params.harness.beforeToolCall(params.layers, params.toolName, params.args, params.context, params.callId);
|
|
89
|
+
if (decision.action !== SteeringAction.Allow) {
|
|
90
|
+
return refusedResult(decision);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const validated = await validateSchema(matchedTool.input, params.args);
|
|
94
|
+
if (!validated.success) {
|
|
95
|
+
return {
|
|
96
|
+
output: `Error: invalid arguments for tool '${params.toolName}': ${validated.zodError.message}`,
|
|
97
|
+
error: true,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const parsedArgs = validated.value;
|
|
101
|
+
const approval = matchedTool.needsApproval
|
|
102
|
+
? await askApproval({
|
|
103
|
+
harness: params.harness,
|
|
104
|
+
toolName: matchedTool.name,
|
|
105
|
+
toolArgs: parsedArgs,
|
|
106
|
+
callId: params.callId,
|
|
107
|
+
ctx: params.context,
|
|
108
|
+
})
|
|
109
|
+
: undefined;
|
|
110
|
+
if (approval && approval.decision.action !== SteeringAction.Allow) {
|
|
111
|
+
return {
|
|
112
|
+
...refusedResult(approval.decision),
|
|
113
|
+
approval: approval.outcome,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const executed = await runMatchedTool(params, matchedTool, parsedArgs);
|
|
117
|
+
return {
|
|
118
|
+
...executed,
|
|
119
|
+
approval: approval?.outcome,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function refusedResult(decision) {
|
|
123
|
+
if (decision.action === SteeringAction.Guide) {
|
|
124
|
+
return {
|
|
125
|
+
output: `Tool call redirected: ${decision.guidance}`,
|
|
126
|
+
error: true,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
output: `Tool call denied: ${decision.guidance ?? 'steering rule violation'}`,
|
|
131
|
+
error: true,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
async function runMatchedTool(params, matchedTool, parsedArgs) {
|
|
135
|
+
const toolCtx = buildToolExecutionContext(params.context, params.harness, params.callId);
|
|
136
|
+
const callId = params.callId;
|
|
137
|
+
const uiBase = callId !== undefined
|
|
138
|
+
? {
|
|
139
|
+
ctx: params.context,
|
|
140
|
+
tool: matchedTool,
|
|
141
|
+
callId,
|
|
142
|
+
args: parsedArgs,
|
|
143
|
+
}
|
|
144
|
+
: undefined;
|
|
145
|
+
if (uiBase) {
|
|
146
|
+
emitToolUi({
|
|
147
|
+
...uiBase,
|
|
148
|
+
phase: 'call',
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
const executionResult = matchedTool.execute(parsedArgs, toolCtx);
|
|
153
|
+
// Generator tools stream progress; drive them here so tool-UI `progress`
|
|
154
|
+
// fragments emit per yield (the non-UI case just consumes to the return).
|
|
155
|
+
let result;
|
|
156
|
+
if (isAsyncGenerator(executionResult)) {
|
|
157
|
+
const events = [];
|
|
158
|
+
for (;;) {
|
|
159
|
+
const next = await executionResult.next();
|
|
160
|
+
if (next.done) {
|
|
161
|
+
result = next.value;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
events.push(next.value);
|
|
165
|
+
if (uiBase) {
|
|
166
|
+
emitToolUi({
|
|
167
|
+
...uiBase,
|
|
168
|
+
phase: 'progress',
|
|
169
|
+
events,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
result = await executionResult;
|
|
176
|
+
}
|
|
177
|
+
if (uiBase) {
|
|
178
|
+
emitToolUi({
|
|
179
|
+
...uiBase,
|
|
180
|
+
phase: 'result',
|
|
181
|
+
output: result,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
output: toolOutputText(result),
|
|
186
|
+
result,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
catch (e) {
|
|
190
|
+
if (uiBase) {
|
|
191
|
+
emitToolUi({
|
|
192
|
+
...uiBase,
|
|
193
|
+
phase: 'error',
|
|
194
|
+
error: e,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
output: `Error: ${e instanceof Error ? e.message : String(e)}`,
|
|
199
|
+
error: true,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* The string a tool result reaches the model as. A tool with no result
|
|
205
|
+
* (`undefined`, e.g. `output: z.void()`) reaches it as an empty string — a
|
|
206
|
+
* result every provider and every MCP client accepts — rather than no output.
|
|
207
|
+
*/
|
|
208
|
+
function toolOutputText(result) {
|
|
209
|
+
if (typeof result === 'string') {
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
return JSON.stringify(result) ?? '';
|
|
213
|
+
}
|
|
214
|
+
//#endregion
|
|
215
|
+
//#region Result items
|
|
216
|
+
/**
|
|
217
|
+
* Build and validate a `function_call_output` item for a tool result.
|
|
218
|
+
*
|
|
219
|
+
* `roundItemSchemas` must be the OWNER-SCOPED registry for the called tool
|
|
220
|
+
* (harness base extended with only that tool's `itemSchemas`), so one tool's
|
|
221
|
+
* `toolResults` schemas never reject a sibling tool's result items. Tools
|
|
222
|
+
* without `itemSchemas` fall back to the base structural parse.
|
|
223
|
+
*/
|
|
224
|
+
export function createToolResultItem({ output, callId, roundItemSchemas, tool, callItem, args, result, error, }) {
|
|
225
|
+
const baseItem = {
|
|
226
|
+
id: crypto.randomUUID(),
|
|
227
|
+
status: 'completed',
|
|
228
|
+
type: 'function_call_output',
|
|
229
|
+
callId,
|
|
230
|
+
output,
|
|
231
|
+
};
|
|
232
|
+
const decorated = tool?.decorateResultItem && callItem?.type === 'function_call'
|
|
233
|
+
? tool.decorateResultItem({
|
|
234
|
+
baseItem,
|
|
235
|
+
callItem,
|
|
236
|
+
args,
|
|
237
|
+
result,
|
|
238
|
+
output,
|
|
239
|
+
error,
|
|
240
|
+
})
|
|
241
|
+
: baseItem;
|
|
242
|
+
return roundItemSchemas.parseWithCategory(decorated, 'toolResults');
|
|
243
|
+
}
|
|
244
|
+
//#endregion
|
|
@@ -1,3 +1,2 @@
|
|
|
1
1
|
import type { AgentHarnessContract, Context, ToolExecutionContext } from '@noetic-tools/types';
|
|
2
|
-
|
|
3
|
-
export declare function buildToolExecutionContext(ctx: Context, harness: AgentHarnessContract, turnContext?: TurnContext): ToolExecutionContext;
|
|
2
|
+
export declare function buildToolExecutionContext(ctx: Context, harness: AgentHarnessContract, callId?: string): ToolExecutionContext;
|
|
@@ -8,7 +8,7 @@ function buildToolContext(harness, ctx) {
|
|
|
8
8
|
},
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
|
-
export function buildToolExecutionContext(ctx, harness,
|
|
11
|
+
export function buildToolExecutionContext(ctx, harness, callId) {
|
|
12
12
|
const toolContext = buildToolContext(harness, ctx);
|
|
13
13
|
return {
|
|
14
14
|
ctx,
|
|
@@ -18,6 +18,10 @@ export function buildToolExecutionContext(ctx, harness, turnContext) {
|
|
|
18
18
|
context: toolContext,
|
|
19
19
|
assembledView: ctx.itemLog.items,
|
|
20
20
|
lastStepMeta: ctx.lastStepMeta,
|
|
21
|
-
|
|
21
|
+
...(callId === undefined
|
|
22
|
+
? {}
|
|
23
|
+
: {
|
|
24
|
+
callId,
|
|
25
|
+
}),
|
|
22
26
|
};
|
|
23
27
|
}
|
|
@@ -3,5 +3,5 @@
|
|
|
3
3
|
* consumers can validate workflow documents without importing core.
|
|
4
4
|
* This barrel keeps core's public and intra-core import paths unchanged.
|
|
5
5
|
*/
|
|
6
|
-
export type { AcpAgentWorkflowNode, CallModelWorkflowNode, CallWorkflowNode, ComputeWorkflowNode, ConditionalRoute, ConditionalWorkflowNode, DecideWorkflowNode, InParallelWorkflowNode, InvokeToolWorkflowNode, LoopWorkflowNode, OutputCodecRef, RouteSwitchWorkflowNode, RunCodeWorkflowNode, ScheduleWorkflowNode, SequenceWorkflowNode, SpawnWorkflowNode, SubflowWorkflowNode, UntilPredicate, VerifyWorkflowNode, WaitWorkflowNode, WithContextWorkflowNode, WorkflowArgs, WorkflowConnectionRef, WorkflowConnectionRequirement, WorkflowCustomItem, WorkflowDocument, WorkflowEmitItems, WorkflowExprValue, WorkflowGraph, WorkflowLiteralValue, WorkflowNode, WorkflowOpShorthand, WorkflowReplayKey, WorkflowSecretValue, WorkflowStepEntry, WorkflowStepList, WorkflowValue, WorkflowVerifyClose, WorkflowVerifyMatcher, } from '@noetic-tools/types';
|
|
6
|
+
export type { AcpAgentWorkflowNode, CallModelWorkflowNode, CallWorkflowNode, ComputeWorkflowNode, ConditionalRoute, ConditionalWorkflowNode, DecideWorkflowNode, InParallelWorkflowNode, InvokeToolWorkflowNode, LoopWorkflowNode, OutputCodecRef, RouteSwitchWorkflowNode, RunCodeWorkflowNode, ScheduleWorkflowNode, SequenceWorkflowNode, SpawnWorkflowNode, SubflowDocument, SubflowWorkflowNode, UntilPredicate, VerifyWorkflowNode, WaitWorkflowNode, WithContextWorkflowNode, WorkflowArgs, WorkflowConnectionRef, WorkflowConnectionRequirement, WorkflowConnectionToolEntry, WorkflowCustomItem, WorkflowDocument, WorkflowEmitItems, WorkflowExprValue, WorkflowGraph, WorkflowLiteralValue, WorkflowMcpServer, WorkflowMcpToolEntry, WorkflowNode, WorkflowOpShorthand, WorkflowReplayKey, WorkflowSecretValue, WorkflowStepConnectionEntry, WorkflowStepEntry, WorkflowStepList, WorkflowToolEntry, WorkflowTypedToolEntry, WorkflowValue, WorkflowVerifyClose, WorkflowVerifyMatcher, } from '@noetic-tools/types';
|
|
7
7
|
export { isWorkflowOpShorthand, MergeStrategy, MergeStrategySchema, shorthandStepId, stepEntryToNode, stepListEntries, UntilPredicateSchema, validateWorkflow, verifyMatcherFromJson, WorkflowArgsSchema, WorkflowConnectionRequirementSchema, WorkflowDocumentSchema, WorkflowNodeSchema, WorkflowOpShorthandSchema, WorkflowValueSchema, WorkflowVerifyMatcherSchema, walkWorkflow, workflowDepth, workflowGraph, } from '@noetic-tools/types';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The connection entries a turn attaches (spec 35 § Attaching connections).
|
|
3
|
+
* They live here, below both, because builders record a document's entries on
|
|
4
|
+
* the step they build and the connection registry attaches them, and builders
|
|
5
|
+
* must not import the registry.
|
|
6
|
+
*/
|
|
7
|
+
import type { ConnectionConfig } from '@noetic-tools/types';
|
|
8
|
+
/** An entry with its name settled. */
|
|
9
|
+
export interface NamedEntry {
|
|
10
|
+
readonly name: string;
|
|
11
|
+
readonly config: ConnectionConfig;
|
|
12
|
+
}
|
|
13
|
+
/** Where an attached entry lives: the turn's own `connections`, or a step's `tools`. */
|
|
14
|
+
export type AttachedScope = 'turn' | 'step';
|
|
15
|
+
/** An entry a turn attaches. */
|
|
16
|
+
export interface AttachedEntry extends NamedEntry {
|
|
17
|
+
readonly scope: AttachedScope;
|
|
18
|
+
/** Where it was given, for errors (`execute() connections`, `callModel('triage') tools`). */
|
|
19
|
+
readonly where: string;
|
|
20
|
+
/** A view layer renders its reads in the turn's context. */
|
|
21
|
+
readonly viewed?: boolean;
|
|
22
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The connection entries a turn attaches (spec 35 § Attaching connections).
|
|
3
|
+
* They live here, below both, because builders record a document's entries on
|
|
4
|
+
* the step they build and the connection registry attaches them, and builders
|
|
5
|
+
* must not import the registry.
|
|
6
|
+
*/
|
|
7
|
+
export {};
|
|
8
|
+
//#endregion
|
|
@@ -11,6 +11,8 @@ export interface HarnessInternalSlots {
|
|
|
11
11
|
connectionRegistry: unknown;
|
|
12
12
|
/** The verifier `step.verify` uses (spec 37). */
|
|
13
13
|
verifier: unknown;
|
|
14
|
+
/** The harness's `environment.secrets` resolver (spec 35 § Secrets). */
|
|
15
|
+
secrets: unknown;
|
|
14
16
|
}
|
|
15
17
|
type HarnessInternalKey = keyof HarnessInternalSlots;
|
|
16
18
|
/** Records internal slots of a harness, merged over any it already has. */
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider APIs (Anthropic via OpenRouter) enforce tool names match
|
|
3
|
+
* `^[a-zA-Z0-9_-]{1,64}$`. Noetic's internal layer-tool names use `layerId/fn`
|
|
4
|
+
* which contains a forbidden `/`. We translate to a wire-safe form only at
|
|
5
|
+
* the SDK boundary; internal tool-name identity (and every codebase reference
|
|
6
|
+
* to names like `plan/updatePrd`) is preserved.
|
|
7
|
+
*/
|
|
8
|
+
export declare function sanitizeToolNameForWire(name: string): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider APIs (Anthropic via OpenRouter) enforce tool names match
|
|
3
|
+
* `^[a-zA-Z0-9_-]{1,64}$`. Noetic's internal layer-tool names use `layerId/fn`
|
|
4
|
+
* which contains a forbidden `/`. We translate to a wire-safe form only at
|
|
5
|
+
* the SDK boundary; internal tool-name identity (and every codebase reference
|
|
6
|
+
* to names like `plan/updatePrd`) is preserved.
|
|
7
|
+
*/
|
|
8
|
+
export function sanitizeToolNameForWire(name) {
|
|
9
|
+
return name.replace(/[^a-zA-Z0-9_-]/g, '_');
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noetic-tools/core",
|
|
3
|
-
"version": "5.20.0
|
|
3
|
+
"version": "5.20.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Step primitives, interpreter, runtime, and context layer contract for Noetic agents.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -68,8 +68,8 @@
|
|
|
68
68
|
}
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"@noetic-tools/context": "^1.11.1
|
|
72
|
-
"@noetic-tools/types": "^3.18.0
|
|
71
|
+
"@noetic-tools/context": "^1.11.1",
|
|
72
|
+
"@noetic-tools/types": "^3.18.0",
|
|
73
73
|
"@openrouter/agent": "^0.11.0",
|
|
74
74
|
"@openrouter/sdk": "1.4.25",
|
|
75
75
|
"jsonata": "2.2.2",
|