@nylorun/harness 0.16.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 +16 -0
- package/HOST_CONTRACT.md +7 -5
- 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/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 +8 -1
- package/dist/run/durable.js +31 -19
- package/dist/types/client.d.ts +1 -1
- package/dist/types/execution.d.ts +20 -1
- package/package.json +2 -2
- package/dist/loop/step/dynamics.d.ts +0 -29
- package/dist/loop/step/dynamics.js +0 -149
|
@@ -1,149 +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
|
-
ctx.agent.implementations[cap.id]?.tools?.[name] !== undefined);
|
|
47
|
-
if (!known)
|
|
48
|
-
throw new HarnessError("configuration.invalid", `beforeModelCall referenced unknown tool '${name}'`);
|
|
49
|
-
if (!agentLevel) {
|
|
50
|
-
const declared = ctx.agent.manifest.capabilities
|
|
51
|
-
.find((cap) => cap.id === id)
|
|
52
|
-
?.tools?.some((tool) => tool.name === name);
|
|
53
|
-
const owned = declared || ctx.agent.implementations[id]?.tools?.[name] !== undefined;
|
|
54
|
-
if (!owned)
|
|
55
|
-
throw new HarnessError("configuration.invalid", `Capability '${id}' beforeModelCall may only toggle its own tools`);
|
|
56
|
-
}
|
|
57
|
-
toolSwitches.set(name, on);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
if (blocked)
|
|
62
|
-
return { blocked };
|
|
63
|
-
let order = 10_000;
|
|
64
|
-
for (const [capabilityId, on] of capabilitySwitches) {
|
|
65
|
-
if (on)
|
|
66
|
-
continue;
|
|
67
|
-
configuration.setTools("dynamics", order++, capabilityId, []);
|
|
68
|
-
}
|
|
69
|
-
for (const [toolName, on] of toolSwitches) {
|
|
70
|
-
if (on)
|
|
71
|
-
continue;
|
|
72
|
-
for (const capability of ctx.agent.manifest.capabilities) {
|
|
73
|
-
const tools = ctx.agent.implementations[capability.id]?.tools;
|
|
74
|
-
if (!tools?.[toolName])
|
|
75
|
-
continue;
|
|
76
|
-
const remaining = Object.values(tools).filter((tool) => tool.name !== toolName);
|
|
77
|
-
configuration.setTools("dynamics", order++, capability.id, remaining);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (extraInstructions.length)
|
|
81
|
-
configuration.setInstructions("dynamics", order++, "dynamics", Object.freeze([...extraInstructions]));
|
|
82
|
-
return {};
|
|
83
|
-
}
|
|
84
|
-
/** Run all afterModelCall handlers; merge with strictest-wins rules. */
|
|
85
|
-
export async function applyAfterModelCall(ctx, args) {
|
|
86
|
-
const decisions = [];
|
|
87
|
-
for (const capability of ctx.agent.manifest.capabilities) {
|
|
88
|
-
const impl = ctx.agent.implementations[capability.id];
|
|
89
|
-
if (!impl?.afterModelCall)
|
|
90
|
-
continue;
|
|
91
|
-
const decision = await impl.afterModelCall(args, {
|
|
92
|
-
info: ctx.info,
|
|
93
|
-
state: { ...ctx.sessionState },
|
|
94
|
-
});
|
|
95
|
-
decisions.push(decision ?? {});
|
|
96
|
-
}
|
|
97
|
-
return mergeDecisions(decisions);
|
|
98
|
-
}
|
|
99
|
-
export function mergeDecisions(decisions) {
|
|
100
|
-
let block;
|
|
101
|
-
let retry;
|
|
102
|
-
const deny = new Map();
|
|
103
|
-
const approve = new Map();
|
|
104
|
-
let text;
|
|
105
|
-
for (const decision of decisions) {
|
|
106
|
-
if (decision.block)
|
|
107
|
-
block = decision.block;
|
|
108
|
-
if (decision.retry)
|
|
109
|
-
retry = decision.retry;
|
|
110
|
-
for (const item of decision.deny ?? [])
|
|
111
|
-
deny.set(item.id, item.reason);
|
|
112
|
-
for (const item of decision.approve ?? [])
|
|
113
|
-
approve.set(item.id, item.prompt);
|
|
114
|
-
if (decision.text !== undefined)
|
|
115
|
-
text = decision.text;
|
|
116
|
-
}
|
|
117
|
-
if (block)
|
|
118
|
-
return { block };
|
|
119
|
-
if (retry)
|
|
120
|
-
return { retry, ...(text === undefined ? {} : { text }) };
|
|
121
|
-
return {
|
|
122
|
-
...(text === undefined ? {} : { text }),
|
|
123
|
-
...(deny.size ? { deny: [...deny.entries()].map(([id, reason]) => ({ id, reason })) } : {}),
|
|
124
|
-
...(approve.size
|
|
125
|
-
? { approve: [...approve.entries()].map(([id, prompt]) => ({ id, prompt })) }
|
|
126
|
-
: {}),
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
export function hasDynamics(agent) {
|
|
130
|
-
return agent.manifest.capabilities.some((capability) => capability.beforeModelCall || capability.afterModelCall);
|
|
131
|
-
}
|
|
132
|
-
function normalizeCapabilityPatch(id, patch, agentLevel) {
|
|
133
|
-
if (patch.active === undefined)
|
|
134
|
-
return patch;
|
|
135
|
-
if (agentLevel)
|
|
136
|
-
throw new HarnessError("configuration.invalid", "Agent-level beforeModelCall must use capabilities{}, not active");
|
|
137
|
-
return {
|
|
138
|
-
...patch,
|
|
139
|
-
capabilities: { ...(patch.capabilities ?? {}), [id]: patch.active },
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
function projectInput(arrivals) {
|
|
143
|
-
const message = arrivals.find((item) => item.kind === "user-message");
|
|
144
|
-
if (!message)
|
|
145
|
-
return undefined;
|
|
146
|
-
if ("text" in message && typeof message.text === "string")
|
|
147
|
-
return message.text;
|
|
148
|
-
return undefined;
|
|
149
|
-
}
|