@nylorun/harness 0.15.0-beta → 0.17.0-beta
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/CHANGELOG.md +28 -0
- package/HOST_CONTRACT.md +8 -6
- package/README.md +26 -6
- package/dist/compatibility.d.ts +1 -1
- package/dist/compatibility.js +1 -1
- package/dist/definition/agent-definition.d.ts +3 -0
- package/dist/definition/agent-definition.js +2 -0
- package/dist/definition/registry.js +1 -1
- package/dist/loop/run.js +146 -6
- package/dist/loop/state.js +23 -0
- package/dist/loop/step/hooks.d.ts +56 -0
- package/dist/loop/step/hooks.js +191 -0
- package/dist/loop/step/model-configuration.d.ts +5 -0
- package/dist/loop/step/model-configuration.js +14 -1
- package/dist/loop/step/run.d.ts +9 -1
- package/dist/loop/step/run.js +47 -29
- package/dist/loop/step/runtime.d.ts +14 -2
- package/dist/run/durable.d.ts +17 -1
- package/dist/run/durable.js +87 -63
- package/dist/run/index.d.ts +1 -1
- package/dist/types/client.d.ts +1 -1
- package/dist/types/execution.d.ts +20 -1
- package/package.json +6 -6
- package/dist/loop/step/dynamics.d.ts +0 -29
- package/dist/loop/step/dynamics.js +0 -147
package/dist/run/durable.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HarnessError } from "@nylorun/core/define";
|
|
2
|
+
import { HOOK_POINTS, hasHook } from "@nylorun/core/define";
|
|
2
3
|
import { AgentManifestSchema } from "@nylorun/core/contracts";
|
|
3
4
|
import { agentFrom } from "@nylorun/core/define";
|
|
4
5
|
import { definitionFor } from "../definition/agent-definition.js";
|
|
@@ -39,11 +40,11 @@ export async function runDurable(options) {
|
|
|
39
40
|
checkpoint.state.agentId !== manifest.id ||
|
|
40
41
|
checkpoint.state.executionId !== checkpoint.sessionId))
|
|
41
42
|
throw new HarnessError("execution.incompatible", "Checkpoint definition/session mismatch");
|
|
42
|
-
let serial = 0;
|
|
43
43
|
const pending = new Map();
|
|
44
44
|
const inFlight = new Set();
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
// Effect ids are derived from stable names, never call order, so replays line up.
|
|
46
|
+
const effect = async (kind, input, context, identity, target = {}) => {
|
|
47
|
+
const effectId = `${checkpoint.turnId}:${checkpoint.segment}:${kind}:${identity}`;
|
|
47
48
|
const request = JSON.parse(JSON.stringify({
|
|
48
49
|
effectId,
|
|
49
50
|
sessionId: checkpoint.sessionId,
|
|
@@ -53,8 +54,7 @@ export async function runDurable(options) {
|
|
|
53
54
|
kind,
|
|
54
55
|
input,
|
|
55
56
|
context,
|
|
56
|
-
...
|
|
57
|
-
...(toolName ? { toolName } : {}),
|
|
57
|
+
...target,
|
|
58
58
|
}));
|
|
59
59
|
const operation = host.resolveEffect(request);
|
|
60
60
|
inFlight.add(operation);
|
|
@@ -72,81 +72,95 @@ export async function runDurable(options) {
|
|
|
72
72
|
return result.outcome;
|
|
73
73
|
};
|
|
74
74
|
let patchTail = Promise.resolve();
|
|
75
|
+
const hostedTool = (capabilityId, tool) => ({
|
|
76
|
+
name: tool.name,
|
|
77
|
+
...(tool.description === undefined ? {} : { description: tool.description }),
|
|
78
|
+
inputSchema: schemaFromJSON(tool.inputSchema),
|
|
79
|
+
...(tool.outputSchema ? { outputSchema: schemaFromJSON(tool.outputSchema) } : {}),
|
|
80
|
+
async execute(args, ctx) {
|
|
81
|
+
const previous = patchTail;
|
|
82
|
+
let release;
|
|
83
|
+
patchTail = new Promise((resolve) => {
|
|
84
|
+
release = resolve;
|
|
85
|
+
});
|
|
86
|
+
try {
|
|
87
|
+
const outcome = await effect("tool", args, {
|
|
88
|
+
executionId: ctx.executionId,
|
|
89
|
+
turnId: ctx.turnId,
|
|
90
|
+
stepId: ctx.stepId,
|
|
91
|
+
callId: ctx.callId,
|
|
92
|
+
invocationId: ctx.invocationId,
|
|
93
|
+
idempotencyKey: ctx.idempotencyKey,
|
|
94
|
+
info: ctx.info,
|
|
95
|
+
state: ctx.state.entries(),
|
|
96
|
+
resume: ctx.resume,
|
|
97
|
+
}, ctx.invocationId, { capabilityId, toolName: tool.name });
|
|
98
|
+
// Apply concurrently resolved patches in manifest call order, independent of host I/O timing.
|
|
99
|
+
await previous;
|
|
100
|
+
for (const [key, value] of Object.entries(outcome.statePatch ?? {}))
|
|
101
|
+
ctx.state.set(key, value);
|
|
102
|
+
const value = outcome.value;
|
|
103
|
+
return value?.kind === "interaction-required"
|
|
104
|
+
? {
|
|
105
|
+
...value,
|
|
106
|
+
interaction: {
|
|
107
|
+
...value.interaction,
|
|
108
|
+
id: `${checkpoint.turnId}:${checkpoint.segment}:interaction:${ctx.invocationId}`,
|
|
109
|
+
},
|
|
110
|
+
}
|
|
111
|
+
: value;
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
await previous;
|
|
115
|
+
release();
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
});
|
|
75
119
|
const implementations = {};
|
|
120
|
+
const sessionTools = options.sessionTools ?? [];
|
|
76
121
|
for (const capability of manifest.capabilities) {
|
|
77
122
|
const tools = {};
|
|
78
123
|
for (const tool of capability.tools ?? [])
|
|
79
|
-
tools[tool.name] =
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
...(tool.outputSchema ? { outputSchema: schemaFromJSON(tool.outputSchema) } : {}),
|
|
84
|
-
async execute(args, ctx) {
|
|
85
|
-
const previous = patchTail;
|
|
86
|
-
let release;
|
|
87
|
-
patchTail = new Promise((resolve) => {
|
|
88
|
-
release = resolve;
|
|
89
|
-
});
|
|
90
|
-
try {
|
|
91
|
-
const outcome = await effect("tool", args, {
|
|
92
|
-
executionId: ctx.executionId,
|
|
93
|
-
turnId: ctx.turnId,
|
|
94
|
-
stepId: ctx.stepId,
|
|
95
|
-
callId: ctx.callId,
|
|
96
|
-
invocationId: ctx.invocationId,
|
|
97
|
-
idempotencyKey: ctx.idempotencyKey,
|
|
98
|
-
info: ctx.info,
|
|
99
|
-
state: ctx.state.entries(),
|
|
100
|
-
resume: ctx.resume,
|
|
101
|
-
}, capability.id, tool.name, ctx.invocationId);
|
|
102
|
-
// Apply concurrently resolved patches in manifest call order, independent of host I/O timing.
|
|
103
|
-
await previous;
|
|
104
|
-
for (const [key, value] of Object.entries(outcome.statePatch ?? {}))
|
|
105
|
-
ctx.state.set(key, value);
|
|
106
|
-
const value = outcome.value;
|
|
107
|
-
return value?.kind === "interaction-required"
|
|
108
|
-
? {
|
|
109
|
-
...value,
|
|
110
|
-
interaction: {
|
|
111
|
-
...value.interaction,
|
|
112
|
-
id: `${checkpoint.turnId}:${checkpoint.segment}:interaction:${ctx.invocationId}`,
|
|
113
|
-
},
|
|
114
|
-
}
|
|
115
|
-
: value;
|
|
116
|
-
}
|
|
117
|
-
finally {
|
|
118
|
-
await previous;
|
|
119
|
-
release();
|
|
120
|
-
}
|
|
121
|
-
},
|
|
122
|
-
};
|
|
124
|
+
tools[tool.name] = hostedTool(capability.id, tool);
|
|
125
|
+
for (const tool of sessionTools)
|
|
126
|
+
if (tool.capabilityId === capability.id)
|
|
127
|
+
tools[tool.name] = hostedTool(capability.id, tool);
|
|
123
128
|
implementations[capability.id] = {
|
|
124
129
|
tools,
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
beforeModelCall: async (args) => (await effect("beforeModelCall", args, { info: checkpoint.info }, capability.id))
|
|
128
|
-
.value,
|
|
129
|
-
}
|
|
130
|
-
: {}),
|
|
131
|
-
...(capability.afterModelCall
|
|
132
|
-
? {
|
|
133
|
-
afterModelCall: async (args, ctx) => (await effect("afterModelCall", args, ctx, capability.id)).value,
|
|
134
|
-
}
|
|
135
|
-
: {}),
|
|
130
|
+
// Hooks run through runHooks below, one effect per hook point; these only satisfy binding.
|
|
131
|
+
...hookPlaceholders(capability.hooks),
|
|
136
132
|
};
|
|
137
133
|
}
|
|
138
|
-
const definition = definitionFor(agentFrom(manifest, implementations
|
|
134
|
+
const definition = definitionFor(agentFrom(manifest, implementations, sessionTools.length === 0
|
|
135
|
+
? undefined
|
|
136
|
+
: {
|
|
137
|
+
sessionTools: sessionTools.map((tool) => ({
|
|
138
|
+
capabilityId: tool.capabilityId,
|
|
139
|
+
name: tool.name,
|
|
140
|
+
})),
|
|
141
|
+
}));
|
|
139
142
|
// Manifest reconstruction must retain definition identity, including schemas and instructions.
|
|
140
143
|
if (definition.hash !== checkpoint.manifestHash)
|
|
141
144
|
throw new HarnessError("execution.incompatible", "Reconstructed definition hash mismatch");
|
|
145
|
+
// All capabilities registered at a hook point share one effect, so one executor round trip.
|
|
146
|
+
const runHooks = async ({ point, capabilityIds, args, identity }) => {
|
|
147
|
+
const outcome = await effect("hook", args, { info: checkpoint.info }, identity, {
|
|
148
|
+
hook: { at: point.at, scope: point.scope, capabilityIds },
|
|
149
|
+
});
|
|
150
|
+
const results = outcome.value?.results;
|
|
151
|
+
return results !== null && typeof results === "object" && !Array.isArray(results)
|
|
152
|
+
? results
|
|
153
|
+
: {};
|
|
154
|
+
};
|
|
155
|
+
const hosted = Object.freeze({ ...definition, runHooks });
|
|
142
156
|
try {
|
|
143
|
-
const result = await withDeterministicIds(`${checkpoint.turnId}_${checkpoint.segment}`, () => execute(
|
|
157
|
+
const result = await withDeterministicIds(`${checkpoint.turnId}_${checkpoint.segment}`, () => execute(hosted, {
|
|
144
158
|
state: checkpoint.state ??
|
|
145
159
|
initializeExecutionState(definition, { executionId: checkpoint.sessionId }),
|
|
146
160
|
input: checkpoint.input,
|
|
147
161
|
info: checkpoint.info,
|
|
148
162
|
signal: options.signal,
|
|
149
|
-
onModelCall: async (call, ctx) => (await effect("model", call, { request: ctx.request, invocationId: ctx.invocationId },
|
|
163
|
+
onModelCall: async (call, ctx) => (await effect("model", call, { request: ctx.request, invocationId: ctx.invocationId }, ctx.invocationId)).value,
|
|
150
164
|
}));
|
|
151
165
|
return { status: result.status, checkpoint: { ...checkpoint, state: result.state }, result };
|
|
152
166
|
}
|
|
@@ -162,3 +176,13 @@ export async function runDurable(options) {
|
|
|
162
176
|
};
|
|
163
177
|
}
|
|
164
178
|
}
|
|
179
|
+
function hookPlaceholders(hooks) {
|
|
180
|
+
const placeholder = () => {
|
|
181
|
+
throw new HarnessError("execution.incompatible", "Hosted hooks run through the host effect");
|
|
182
|
+
};
|
|
183
|
+
const table = {};
|
|
184
|
+
for (const point of HOOK_POINTS)
|
|
185
|
+
if (hasHook(hooks, point.at, point.scope))
|
|
186
|
+
(table[point.at] ??= {})[point.scope] = placeholder;
|
|
187
|
+
return table;
|
|
188
|
+
}
|
package/dist/run/index.d.ts
CHANGED
|
@@ -29,5 +29,5 @@ export declare function bindingFromAgent(agent: BuiltAgent): RunBinding;
|
|
|
29
29
|
export { checkCompatibility, createExecutionState, validateExecutionState, execute };
|
|
30
30
|
export type { Implementations, ExecutionState, RunResult, RunOptions };
|
|
31
31
|
export { createDurableCheckpoint, runDurable } from "./durable.js";
|
|
32
|
-
export type { DurableCheckpoint, HostEffect, EffectResolution, DurableHost, DurableResult, } from "./durable.js";
|
|
32
|
+
export type { DurableCheckpoint, DurableSessionTool, HostEffect, EffectResolution, DurableHost, DurableResult, } from "./durable.js";
|
|
33
33
|
export type { ModelAdapter, ModelCandidate, ModelCall, ModelRequest } from "@nylorun/core/define";
|
package/dist/types/client.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { MessageInput } from "@nylorun/core/define";
|
|
|
3
3
|
import type { SavedToolCall } from "./execution.js";
|
|
4
4
|
/**
|
|
5
5
|
* Definition-parameterized client types (H11). Types only — harness does not implement Session.
|
|
6
|
-
* Runtime imports these for
|
|
6
|
+
* Runtime imports these for session and stream typing.
|
|
7
7
|
*/
|
|
8
8
|
export interface InputAccepted {
|
|
9
9
|
readonly status: "accepted" | "rejected";
|
|
@@ -58,6 +58,23 @@ export interface ExecutionPlan {
|
|
|
58
58
|
readonly immediate: readonly ToolResult[];
|
|
59
59
|
readonly calls: readonly SavedToolCall[];
|
|
60
60
|
}
|
|
61
|
+
/** Hook progress for the turn in flight. Cleared when the turn ends. */
|
|
62
|
+
export interface TurnHookState {
|
|
63
|
+
readonly turnId: string;
|
|
64
|
+
/** Text of the input that started the turn. */
|
|
65
|
+
readonly input?: string;
|
|
66
|
+
/** Validated before("turn") patch, applied to every step of the turn. */
|
|
67
|
+
readonly patch?: {
|
|
68
|
+
readonly capabilities?: Readonly<Record<string, boolean>>;
|
|
69
|
+
readonly tools?: Readonly<Record<string, boolean>>;
|
|
70
|
+
readonly instructions?: readonly string[];
|
|
71
|
+
};
|
|
72
|
+
/** Retries requested so far this turn, per hook point. */
|
|
73
|
+
readonly attempts: {
|
|
74
|
+
readonly afterStep: number;
|
|
75
|
+
readonly afterTurn: number;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
61
78
|
/** Versioned continuation data, not a live execution object. */
|
|
62
79
|
export interface ExecutionState {
|
|
63
80
|
readonly version: 1;
|
|
@@ -73,8 +90,10 @@ export interface ExecutionState {
|
|
|
73
90
|
readonly plan?: ExecutionPlan;
|
|
74
91
|
/** Abandoned actions retained for reconciliation, never automatically dispatched. */
|
|
75
92
|
readonly cancelledCalls?: readonly SavedToolCall[];
|
|
93
|
+
/** Hook progress for the current turn; present only while a turn with hooks is in flight. */
|
|
94
|
+
readonly turn?: TurnHookState;
|
|
76
95
|
/**
|
|
77
|
-
* Durable session memory (tools write via `ctx.state`;
|
|
96
|
+
* Durable session memory (tools write via `ctx.state`; hooks read it).
|
|
78
97
|
* Runtime persists this with the checkpoint.
|
|
79
98
|
*/
|
|
80
99
|
readonly state?: JsonObject;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nylorun/harness",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0-beta",
|
|
4
4
|
"description": "Nylorun's TypeScript agent execution engine. See github.com/nylorun/harness.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -70,15 +70,15 @@
|
|
|
70
70
|
"devDependencies": {
|
|
71
71
|
"@types/node": "^26.5.0",
|
|
72
72
|
"@typescript/native": "npm:typescript@^7.0.2",
|
|
73
|
-
"prettier": "^3.9.
|
|
73
|
+
"prettier": "^3.9.8",
|
|
74
74
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
75
|
-
"vitest": "^5.0.
|
|
76
|
-
"zod": "^4.
|
|
75
|
+
"vitest": "^5.0.1",
|
|
76
|
+
"zod": "^4.6.5"
|
|
77
77
|
},
|
|
78
78
|
"dependencies": {
|
|
79
|
-
"@nylorun/core": "0.
|
|
79
|
+
"@nylorun/core": "0.3.0-beta"
|
|
80
80
|
},
|
|
81
81
|
"peerDependencies": {
|
|
82
|
-
"zod": "^4.5
|
|
82
|
+
"zod": "^4.6.5"
|
|
83
83
|
}
|
|
84
84
|
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { AgentDefinition } from "../../definition/agent-definition.js";
|
|
2
|
-
import type { Decision } from "@nylorun/core/define";
|
|
3
|
-
import type { JsonValue } from "@nylorun/core/define";
|
|
4
|
-
import type { PromptItem } from "@nylorun/core/define";
|
|
5
|
-
import type { InputEvent } from "@nylorun/core/define";
|
|
6
|
-
import type { ModelConfigurationDraft } from "./model-configuration.js";
|
|
7
|
-
export interface DynamicsContext {
|
|
8
|
-
readonly agent: AgentDefinition;
|
|
9
|
-
readonly info?: unknown;
|
|
10
|
-
readonly step: number;
|
|
11
|
-
readonly arrivals: readonly InputEvent[];
|
|
12
|
-
readonly messages: readonly PromptItem[];
|
|
13
|
-
sessionState: Record<string, JsonValue>;
|
|
14
|
-
}
|
|
15
|
-
/** Run all beforeModelCall handlers in capability `.use()` order. */
|
|
16
|
-
export declare function applyBeforeModelCall(ctx: DynamicsContext, configuration: ModelConfigurationDraft): Promise<{
|
|
17
|
-
readonly blocked?: string;
|
|
18
|
-
}>;
|
|
19
|
-
/** Run all afterModelCall handlers; merge with strictest-wins rules. */
|
|
20
|
-
export declare function applyAfterModelCall(ctx: DynamicsContext, args: {
|
|
21
|
-
readonly text?: string;
|
|
22
|
-
readonly toolCalls: readonly {
|
|
23
|
-
readonly id: string;
|
|
24
|
-
readonly name: string;
|
|
25
|
-
readonly args: JsonValue;
|
|
26
|
-
}[];
|
|
27
|
-
}): Promise<Decision>;
|
|
28
|
-
export declare function mergeDecisions(decisions: readonly Decision[]): Decision;
|
|
29
|
-
export declare function hasDynamics(agent: AgentDefinition): boolean;
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { HarnessError } from "@nylorun/core/define";
|
|
2
|
-
/** Run all beforeModelCall handlers in capability `.use()` order. */
|
|
3
|
-
export async function applyBeforeModelCall(ctx, configuration) {
|
|
4
|
-
const patches = [];
|
|
5
|
-
for (const capability of ctx.agent.manifest.capabilities) {
|
|
6
|
-
const impl = ctx.agent.implementations[capability.id];
|
|
7
|
-
if (!impl?.beforeModelCall)
|
|
8
|
-
continue;
|
|
9
|
-
const raw = await impl.beforeModelCall({
|
|
10
|
-
input: projectInput(ctx.arrivals),
|
|
11
|
-
info: ctx.info,
|
|
12
|
-
state: { ...ctx.sessionState },
|
|
13
|
-
messages: ctx.messages,
|
|
14
|
-
step: ctx.step,
|
|
15
|
-
});
|
|
16
|
-
const patch = normalizeCapabilityPatch(capability.id, raw ?? {}, capability.id === "agent");
|
|
17
|
-
patches.push({ id: capability.id, patch, agentLevel: capability.id === "agent" });
|
|
18
|
-
}
|
|
19
|
-
if (!patches.length)
|
|
20
|
-
return {};
|
|
21
|
-
let blocked;
|
|
22
|
-
const capabilitySwitches = new Map();
|
|
23
|
-
const toolSwitches = new Map();
|
|
24
|
-
const extraInstructions = [];
|
|
25
|
-
for (const { id, patch, agentLevel } of patches) {
|
|
26
|
-
if (patch.block)
|
|
27
|
-
blocked = patch.block;
|
|
28
|
-
if (patch.state) {
|
|
29
|
-
for (const [key, value] of Object.entries(patch.state))
|
|
30
|
-
ctx.sessionState[key] = value;
|
|
31
|
-
}
|
|
32
|
-
if (patch.instructions)
|
|
33
|
-
extraInstructions.push(...patch.instructions);
|
|
34
|
-
if (patch.capabilities) {
|
|
35
|
-
for (const [name, on] of Object.entries(patch.capabilities)) {
|
|
36
|
-
if (!agentLevel && name !== id)
|
|
37
|
-
throw new HarnessError("configuration.invalid", `Capability '${id}' beforeModelCall may only toggle itself, not '${name}'`);
|
|
38
|
-
if (!ctx.agent.manifest.capabilities.some((item) => item.id === name))
|
|
39
|
-
throw new HarnessError("configuration.invalid", `beforeModelCall referenced unknown capability '${name}'`);
|
|
40
|
-
capabilitySwitches.set(name, on);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
if (patch.tools) {
|
|
44
|
-
for (const [name, on] of Object.entries(patch.tools)) {
|
|
45
|
-
const known = ctx.agent.manifest.capabilities.some((cap) => cap.tools?.some((tool) => tool.name === name));
|
|
46
|
-
if (!known)
|
|
47
|
-
throw new HarnessError("configuration.invalid", `beforeModelCall referenced unknown tool '${name}'`);
|
|
48
|
-
if (!agentLevel) {
|
|
49
|
-
const owned = ctx.agent.manifest.capabilities
|
|
50
|
-
.find((cap) => cap.id === id)
|
|
51
|
-
?.tools?.some((tool) => tool.name === name);
|
|
52
|
-
if (!owned)
|
|
53
|
-
throw new HarnessError("configuration.invalid", `Capability '${id}' beforeModelCall may only toggle its own tools`);
|
|
54
|
-
}
|
|
55
|
-
toolSwitches.set(name, on);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
if (blocked)
|
|
60
|
-
return { blocked };
|
|
61
|
-
let order = 10_000;
|
|
62
|
-
for (const [capabilityId, on] of capabilitySwitches) {
|
|
63
|
-
if (on)
|
|
64
|
-
continue;
|
|
65
|
-
configuration.setTools("dynamics", order++, capabilityId, []);
|
|
66
|
-
}
|
|
67
|
-
for (const [toolName, on] of toolSwitches) {
|
|
68
|
-
if (on)
|
|
69
|
-
continue;
|
|
70
|
-
for (const capability of ctx.agent.manifest.capabilities) {
|
|
71
|
-
const tools = ctx.agent.implementations[capability.id]?.tools;
|
|
72
|
-
if (!tools?.[toolName])
|
|
73
|
-
continue;
|
|
74
|
-
const remaining = Object.values(tools).filter((tool) => tool.name !== toolName);
|
|
75
|
-
configuration.setTools("dynamics", order++, capability.id, remaining);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
if (extraInstructions.length)
|
|
79
|
-
configuration.setInstructions("dynamics", order++, "dynamics", Object.freeze([...extraInstructions]));
|
|
80
|
-
return {};
|
|
81
|
-
}
|
|
82
|
-
/** Run all afterModelCall handlers; merge with strictest-wins rules. */
|
|
83
|
-
export async function applyAfterModelCall(ctx, args) {
|
|
84
|
-
const decisions = [];
|
|
85
|
-
for (const capability of ctx.agent.manifest.capabilities) {
|
|
86
|
-
const impl = ctx.agent.implementations[capability.id];
|
|
87
|
-
if (!impl?.afterModelCall)
|
|
88
|
-
continue;
|
|
89
|
-
const decision = await impl.afterModelCall(args, {
|
|
90
|
-
info: ctx.info,
|
|
91
|
-
state: { ...ctx.sessionState },
|
|
92
|
-
});
|
|
93
|
-
decisions.push(decision ?? {});
|
|
94
|
-
}
|
|
95
|
-
return mergeDecisions(decisions);
|
|
96
|
-
}
|
|
97
|
-
export function mergeDecisions(decisions) {
|
|
98
|
-
let block;
|
|
99
|
-
let retry;
|
|
100
|
-
const deny = new Map();
|
|
101
|
-
const approve = new Map();
|
|
102
|
-
let text;
|
|
103
|
-
for (const decision of decisions) {
|
|
104
|
-
if (decision.block)
|
|
105
|
-
block = decision.block;
|
|
106
|
-
if (decision.retry)
|
|
107
|
-
retry = decision.retry;
|
|
108
|
-
for (const item of decision.deny ?? [])
|
|
109
|
-
deny.set(item.id, item.reason);
|
|
110
|
-
for (const item of decision.approve ?? [])
|
|
111
|
-
approve.set(item.id, item.prompt);
|
|
112
|
-
if (decision.text !== undefined)
|
|
113
|
-
text = decision.text;
|
|
114
|
-
}
|
|
115
|
-
if (block)
|
|
116
|
-
return { block };
|
|
117
|
-
if (retry)
|
|
118
|
-
return { retry, ...(text === undefined ? {} : { text }) };
|
|
119
|
-
return {
|
|
120
|
-
...(text === undefined ? {} : { text }),
|
|
121
|
-
...(deny.size ? { deny: [...deny.entries()].map(([id, reason]) => ({ id, reason })) } : {}),
|
|
122
|
-
...(approve.size
|
|
123
|
-
? { approve: [...approve.entries()].map(([id, prompt]) => ({ id, prompt })) }
|
|
124
|
-
: {}),
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
export function hasDynamics(agent) {
|
|
128
|
-
return agent.manifest.capabilities.some((capability) => capability.beforeModelCall || capability.afterModelCall);
|
|
129
|
-
}
|
|
130
|
-
function normalizeCapabilityPatch(id, patch, agentLevel) {
|
|
131
|
-
if (patch.active === undefined)
|
|
132
|
-
return patch;
|
|
133
|
-
if (agentLevel)
|
|
134
|
-
throw new HarnessError("configuration.invalid", "Agent-level beforeModelCall must use capabilities{}, not active");
|
|
135
|
-
return {
|
|
136
|
-
...patch,
|
|
137
|
-
capabilities: { ...(patch.capabilities ?? {}), [id]: patch.active },
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
function projectInput(arrivals) {
|
|
141
|
-
const message = arrivals.find((item) => item.kind === "user-message");
|
|
142
|
-
if (!message)
|
|
143
|
-
return undefined;
|
|
144
|
-
if ("text" in message && typeof message.text === "string")
|
|
145
|
-
return message.text;
|
|
146
|
-
return undefined;
|
|
147
|
-
}
|