@nylorun/harness 0.22.1-beta → 0.23.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 +84 -0
- package/HOST_CONTRACT.md +17 -19
- package/README.md +5 -30
- package/dist/api/executors.d.ts +5 -4
- package/dist/api/harness.d.ts +0 -10
- package/dist/api/harness.js +1 -52
- package/dist/api/host.d.ts +2 -8
- package/dist/api/host.js +3 -15
- package/dist/api/run.d.ts +0 -11
- package/dist/api/run.js +1 -11
- package/dist/compatibility.d.ts +4 -12
- package/dist/compatibility.js +3 -11
- package/dist/definition/agent-definition.d.ts +0 -3
- package/dist/definition/agent-definition.js +0 -2
- package/dist/flow/checkpoint.d.ts +16 -0
- package/dist/flow/checkpoint.js +6 -2
- package/dist/flow/context.d.ts +19 -10
- package/dist/flow/context.js +36 -20
- package/dist/flow/engine.d.ts +25 -7
- package/dist/flow/engine.js +304 -38
- package/dist/flow/index.d.ts +3 -5
- package/dist/flow/index.js +3 -5
- package/dist/flow/paths.d.ts +1 -8
- package/dist/flow/paths.js +1 -12
- package/dist/flow/tool.d.ts +14 -0
- package/dist/flow/tool.js +67 -0
- package/dist/flow/types.d.ts +30 -3
- package/dist/flow/types.js +14 -1
- package/dist/loop/run.js +5 -134
- package/dist/loop/state.js +2 -22
- package/dist/loop/step/run.d.ts +1 -10
- package/dist/loop/step/run.js +2 -101
- package/dist/loop/step/runtime.d.ts +0 -17
- package/dist/run/durable.d.ts +7 -11
- package/dist/run/durable.js +14 -33
- package/dist/run/index.d.ts +3 -4
- package/dist/run/index.js +1 -2
- package/dist/types/execution.d.ts +5 -18
- package/package.json +2 -2
- package/dist/flow/chain.d.ts +0 -8
- package/dist/flow/chain.js +0 -27
- package/dist/flow/loop.d.ts +0 -35
- package/dist/flow/loop.js +0 -238
- package/dist/flow/map.d.ts +0 -7
- package/dist/flow/map.js +0 -64
- package/dist/flow/node.d.ts +0 -31
- package/dist/flow/node.js +0 -113
- package/dist/flow/parallel.d.ts +0 -7
- package/dist/flow/parallel.js +0 -47
- package/dist/flow/switch.d.ts +0 -8
- package/dist/flow/switch.js +0 -35
- package/dist/flow/v2.d.ts +0 -12
- package/dist/flow/v2.js +0 -370
- package/dist/loop/step/hooks.d.ts +0 -56
- package/dist/loop/step/hooks.js +0 -191
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import type { AgentDefinition, Implementations } from "../../definition/agent-definition.js";
|
|
2
|
-
import type { Decision, HookAt, HookScope, TurnDecision } from "@nylorun/core/define";
|
|
3
|
-
import type { JsonValue } from "@nylorun/core/define";
|
|
4
|
-
import type { InputEvent } from "@nylorun/core/define";
|
|
5
|
-
import type { ModelConfigurationDraft } from "./model-configuration.js";
|
|
6
|
-
export interface HookPoint {
|
|
7
|
-
readonly at: HookAt;
|
|
8
|
-
readonly scope: HookScope;
|
|
9
|
-
}
|
|
10
|
-
/** Runs one hook point for the listed capabilities; results are keyed by capability id. */
|
|
11
|
-
export type HookRunner = (request: {
|
|
12
|
-
readonly point: HookPoint;
|
|
13
|
-
readonly capabilityIds: readonly string[];
|
|
14
|
-
readonly args: unknown;
|
|
15
|
-
/** Stable name of this call within the turn, used to journal it durably. */
|
|
16
|
-
readonly identity: string;
|
|
17
|
-
}) => Promise<Readonly<Record<string, unknown>>>;
|
|
18
|
-
/** The part of a before-hook Patch that shapes model calls. JSON, so a turn can persist it. */
|
|
19
|
-
export interface CallPatch {
|
|
20
|
-
readonly capabilities?: Readonly<Record<string, boolean>>;
|
|
21
|
-
readonly tools?: Readonly<Record<string, boolean>>;
|
|
22
|
-
readonly instructions?: readonly string[];
|
|
23
|
-
}
|
|
24
|
-
export interface CombinedPatch {
|
|
25
|
-
readonly block?: string;
|
|
26
|
-
readonly state?: Readonly<Record<string, JsonValue>>;
|
|
27
|
-
readonly patch: CallPatch;
|
|
28
|
-
}
|
|
29
|
-
export declare function localHookRunner(implementations: Implementations): HookRunner;
|
|
30
|
-
/** Capability ids registered at a hook point, in manifest (`.use()`) order. */
|
|
31
|
-
export declare function hookRegistrants(agent: AgentDefinition, point: HookPoint): readonly string[];
|
|
32
|
-
export declare function hasAnyHooks(agent: AgentDefinition): boolean;
|
|
33
|
-
/** Run a hook point, returning results in manifest order. Skips the call when nobody registered. */
|
|
34
|
-
export declare function callHooks(agent: AgentDefinition, point: HookPoint, args: unknown, identity: string): Promise<readonly {
|
|
35
|
-
readonly id: string;
|
|
36
|
-
readonly result: unknown;
|
|
37
|
-
}[]>;
|
|
38
|
-
/** Validate and merge before-hook Patches in manifest order. */
|
|
39
|
-
export declare function combinePatches(agent: AgentDefinition, results: readonly {
|
|
40
|
-
readonly id: string;
|
|
41
|
-
readonly result: unknown;
|
|
42
|
-
}[]): CombinedPatch;
|
|
43
|
-
/** Layer a step patch over the turn patch: step toggles win, instructions concatenate. */
|
|
44
|
-
export declare function layerPatches(turn: CallPatch | undefined, step: CallPatch | undefined): CallPatch;
|
|
45
|
-
/** Apply a patch to this model call's configuration. Toggles hide tools; hooks keep running. */
|
|
46
|
-
export declare function applyPatch(configuration: ModelConfigurationDraft, patch: CallPatch, order: number): void;
|
|
47
|
-
/** Merge after("step") Decisions; strictest wins. */
|
|
48
|
-
export declare function mergeDecisions(decisions: readonly Decision[]): Decision;
|
|
49
|
-
/** Merge after("turn") decisions: block, then retry, then a replacement answer. */
|
|
50
|
-
export declare function mergeTurnDecisions(decisions: readonly TurnDecision[]): TurnDecision;
|
|
51
|
-
export declare function asDecisions<T>(results: readonly {
|
|
52
|
-
readonly id: string;
|
|
53
|
-
readonly result: unknown;
|
|
54
|
-
}[]): T[];
|
|
55
|
-
/** The text of the user message that started a turn, when there is one. */
|
|
56
|
-
export declare function projectInput(arrivals: readonly InputEvent[]): string | undefined;
|
package/dist/loop/step/hooks.js
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import { HarnessError, hasHook, runHookPoint } from "@nylorun/core/define";
|
|
2
|
-
export function localHookRunner(implementations) {
|
|
3
|
-
return ({ point, capabilityIds, args }) => runHookPoint(implementations, { ...point, capabilityIds }, args);
|
|
4
|
-
}
|
|
5
|
-
/** Capability ids registered at a hook point, in manifest (`.use()`) order. */
|
|
6
|
-
export function hookRegistrants(agent, point) {
|
|
7
|
-
return agent.manifest.capabilities
|
|
8
|
-
.filter((capability) => hasHook(capability.hooks, point.at, point.scope))
|
|
9
|
-
.map((capability) => capability.id);
|
|
10
|
-
}
|
|
11
|
-
export function hasAnyHooks(agent) {
|
|
12
|
-
return agent.manifest.capabilities.some((capability) => capability.hooks?.length);
|
|
13
|
-
}
|
|
14
|
-
/** Run a hook point, returning results in manifest order. Skips the call when nobody registered. */
|
|
15
|
-
export async function callHooks(agent, point, args, identity) {
|
|
16
|
-
const capabilityIds = hookRegistrants(agent, point);
|
|
17
|
-
if (!capabilityIds.length)
|
|
18
|
-
return [];
|
|
19
|
-
const results = await agent.runHooks({ point, capabilityIds, args, identity });
|
|
20
|
-
return capabilityIds.map((id) => ({ id, result: results[id] ?? {} }));
|
|
21
|
-
}
|
|
22
|
-
/** Validate and merge before-hook Patches in manifest order. */
|
|
23
|
-
export function combinePatches(agent, results) {
|
|
24
|
-
let block;
|
|
25
|
-
const state = {};
|
|
26
|
-
const capabilities = {};
|
|
27
|
-
const tools = {};
|
|
28
|
-
const instructions = [];
|
|
29
|
-
for (const { id, result } of results) {
|
|
30
|
-
const agentLevel = id === "agent";
|
|
31
|
-
const patch = normalizeCapabilityPatch(id, asObject(result, id), agentLevel);
|
|
32
|
-
if (patch.block !== undefined)
|
|
33
|
-
block = checkedString(patch.block, id, "block");
|
|
34
|
-
if (patch.state !== undefined)
|
|
35
|
-
Object.assign(state, asObject(patch.state, id));
|
|
36
|
-
if (patch.instructions !== undefined) {
|
|
37
|
-
if (!Array.isArray(patch.instructions) ||
|
|
38
|
-
patch.instructions.some((item) => typeof item !== "string"))
|
|
39
|
-
throw invalid(`Capability '${id}' hook instructions must be strings`);
|
|
40
|
-
instructions.push(...patch.instructions);
|
|
41
|
-
}
|
|
42
|
-
for (const [name, on] of Object.entries(asObject(patch.capabilities ?? {}, id))) {
|
|
43
|
-
if (!agentLevel && name !== id)
|
|
44
|
-
throw invalid(`Capability '${id}' hook may only toggle itself, not '${name}'`);
|
|
45
|
-
if (!agent.manifest.capabilities.some((item) => item.id === name))
|
|
46
|
-
throw invalid(`Hook referenced unknown capability '${name}'`);
|
|
47
|
-
capabilities[name] = checkedBoolean(on, id);
|
|
48
|
-
}
|
|
49
|
-
for (const [name, on] of Object.entries(asObject(patch.tools ?? {}, id))) {
|
|
50
|
-
const owner = toolOwner(agent, name);
|
|
51
|
-
if (owner === undefined)
|
|
52
|
-
throw invalid(`Hook referenced unknown tool '${name}'`);
|
|
53
|
-
if (!agentLevel && owner !== id)
|
|
54
|
-
throw invalid(`Capability '${id}' hook may only toggle its own tools`);
|
|
55
|
-
tools[name] = checkedBoolean(on, id);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return {
|
|
59
|
-
...(block === undefined ? {} : { block }),
|
|
60
|
-
...(Object.keys(state).length ? { state } : {}),
|
|
61
|
-
patch: compact({ capabilities, tools, instructions }),
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
/** Layer a step patch over the turn patch: step toggles win, instructions concatenate. */
|
|
65
|
-
export function layerPatches(turn, step) {
|
|
66
|
-
return compact({
|
|
67
|
-
capabilities: { ...turn?.capabilities, ...step?.capabilities },
|
|
68
|
-
tools: { ...turn?.tools, ...step?.tools },
|
|
69
|
-
instructions: [...(turn?.instructions ?? []), ...(step?.instructions ?? [])],
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
/** Apply a patch to this model call's configuration. Toggles hide tools; hooks keep running. */
|
|
73
|
-
export function applyPatch(configuration, patch, order) {
|
|
74
|
-
const offCapabilities = Object.entries(patch.capabilities ?? {})
|
|
75
|
-
.filter(([, on]) => !on)
|
|
76
|
-
.map(([id]) => id);
|
|
77
|
-
const offTools = Object.entries(patch.tools ?? {})
|
|
78
|
-
.filter(([, on]) => !on)
|
|
79
|
-
.map(([name]) => name);
|
|
80
|
-
if (offCapabilities.length || offTools.length)
|
|
81
|
-
configuration.exclude({ capabilities: offCapabilities, tools: offTools });
|
|
82
|
-
if (patch.instructions?.length)
|
|
83
|
-
configuration.setInstructions("dynamics", order, "dynamics", Object.freeze([...patch.instructions]));
|
|
84
|
-
}
|
|
85
|
-
/** Merge after("step") Decisions; strictest wins. */
|
|
86
|
-
export function mergeDecisions(decisions) {
|
|
87
|
-
let block;
|
|
88
|
-
let retry;
|
|
89
|
-
const deny = new Map();
|
|
90
|
-
const approve = new Map();
|
|
91
|
-
let text;
|
|
92
|
-
for (const decision of decisions) {
|
|
93
|
-
if (decision.block)
|
|
94
|
-
block = decision.block;
|
|
95
|
-
if (decision.retry)
|
|
96
|
-
retry = decision.retry;
|
|
97
|
-
for (const item of decision.deny ?? [])
|
|
98
|
-
deny.set(item.id, item.reason);
|
|
99
|
-
for (const item of decision.approve ?? [])
|
|
100
|
-
approve.set(item.id, item.prompt);
|
|
101
|
-
if (decision.text !== undefined)
|
|
102
|
-
text = decision.text;
|
|
103
|
-
}
|
|
104
|
-
if (block)
|
|
105
|
-
return { block };
|
|
106
|
-
if (retry)
|
|
107
|
-
return { retry, ...(text === undefined ? {} : { text }) };
|
|
108
|
-
return {
|
|
109
|
-
...(text === undefined ? {} : { text }),
|
|
110
|
-
...(deny.size ? { deny: [...deny.entries()].map(([id, reason]) => ({ id, reason })) } : {}),
|
|
111
|
-
...(approve.size
|
|
112
|
-
? { approve: [...approve.entries()].map(([id, prompt]) => ({ id, prompt })) }
|
|
113
|
-
: {}),
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
/** Merge after("turn") decisions: block, then retry, then a replacement answer. */
|
|
117
|
-
export function mergeTurnDecisions(decisions) {
|
|
118
|
-
let block;
|
|
119
|
-
let retry;
|
|
120
|
-
let text;
|
|
121
|
-
let output;
|
|
122
|
-
for (const decision of decisions) {
|
|
123
|
-
if (decision.block)
|
|
124
|
-
block = decision.block;
|
|
125
|
-
if (decision.retry)
|
|
126
|
-
retry = decision.retry;
|
|
127
|
-
if (decision.text !== undefined)
|
|
128
|
-
text = decision.text;
|
|
129
|
-
if (decision.output !== undefined)
|
|
130
|
-
output = decision.output;
|
|
131
|
-
}
|
|
132
|
-
if (block)
|
|
133
|
-
return { block };
|
|
134
|
-
if (retry)
|
|
135
|
-
return { retry };
|
|
136
|
-
return {
|
|
137
|
-
...(text === undefined ? {} : { text }),
|
|
138
|
-
...(output === undefined ? {} : { output }),
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
export function asDecisions(results) {
|
|
142
|
-
return results.map(({ id, result }) => asObject(result, id));
|
|
143
|
-
}
|
|
144
|
-
/** The text of the user message that started a turn, when there is one. */
|
|
145
|
-
export function projectInput(arrivals) {
|
|
146
|
-
const message = arrivals.find((item) => item.kind === "user-message");
|
|
147
|
-
if (!message)
|
|
148
|
-
return undefined;
|
|
149
|
-
if ("text" in message && typeof message.text === "string")
|
|
150
|
-
return message.text;
|
|
151
|
-
return undefined;
|
|
152
|
-
}
|
|
153
|
-
function toolOwner(agent, name) {
|
|
154
|
-
return agent.manifest.capabilities.find((capability) => capability.tools?.some((tool) => tool.name === name) ||
|
|
155
|
-
agent.implementations[capability.id]?.tools?.[name] !== undefined)?.id;
|
|
156
|
-
}
|
|
157
|
-
function normalizeCapabilityPatch(id, patch, agentLevel) {
|
|
158
|
-
if (patch.active === undefined)
|
|
159
|
-
return patch;
|
|
160
|
-
if (agentLevel)
|
|
161
|
-
throw invalid("Agent-level before hooks must use capabilities{}, not active");
|
|
162
|
-
return {
|
|
163
|
-
...patch,
|
|
164
|
-
capabilities: { ...(patch.capabilities ?? {}), [id]: checkedBoolean(patch.active, id) },
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
function compact(patch) {
|
|
168
|
-
return {
|
|
169
|
-
...(Object.keys(patch.capabilities).length ? { capabilities: patch.capabilities } : {}),
|
|
170
|
-
...(Object.keys(patch.tools).length ? { tools: patch.tools } : {}),
|
|
171
|
-
...(patch.instructions.length ? { instructions: patch.instructions } : {}),
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
function asObject(value, id) {
|
|
175
|
-
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
176
|
-
throw invalid(`Capability '${id}' hook must return an object`);
|
|
177
|
-
return value;
|
|
178
|
-
}
|
|
179
|
-
function checkedBoolean(value, id) {
|
|
180
|
-
if (typeof value !== "boolean")
|
|
181
|
-
throw invalid(`Capability '${id}' hook toggles must be booleans`);
|
|
182
|
-
return value;
|
|
183
|
-
}
|
|
184
|
-
function checkedString(value, id, field) {
|
|
185
|
-
if (typeof value !== "string")
|
|
186
|
-
throw invalid(`Capability '${id}' hook ${field} must be a string`);
|
|
187
|
-
return value;
|
|
188
|
-
}
|
|
189
|
-
function invalid(message) {
|
|
190
|
-
return new HarnessError("configuration.invalid", message);
|
|
191
|
-
}
|