@nylorun/harness 0.25.1-beta → 0.27.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 +144 -0
- package/HOST_CONTRACT.md +21 -18
- package/README.md +12 -13
- package/dist/api/executors.d.ts +18 -4
- package/dist/api/harness.d.ts +1 -1
- package/dist/api/harness.js +4 -4
- package/dist/api/host.d.ts +4 -0
- package/dist/api/host.js +62 -24
- package/dist/api/index.d.ts +1 -1
- package/dist/api/run.js +9 -0
- package/dist/definition/agent-definition.d.ts +2 -2
- package/dist/definition/agent-definition.js +2 -2
- package/dist/definition/registry.d.ts +3 -3
- package/dist/definition/registry.js +12 -15
- package/dist/loop/compaction/index.d.ts +4 -2
- package/dist/loop/compaction/index.js +78 -8
- package/dist/loop/dispatch.js +1 -1
- package/dist/loop/model/project.d.ts +6 -0
- package/dist/loop/model/project.js +59 -18
- package/dist/loop/run.js +2 -2
- package/dist/loop/state.js +0 -1
- package/dist/loop/step/canonicalize.d.ts +1 -3
- package/dist/loop/step/canonicalize.js +0 -15
- package/dist/loop/step/model-configuration.d.ts +4 -13
- package/dist/loop/step/model-configuration.js +12 -70
- package/dist/loop/step/resolve.js +0 -1
- package/dist/loop/step/run.js +33 -12
- package/dist/loop/step/runtime.d.ts +2 -2
- package/dist/loop/step/seal.js +0 -17
- package/dist/loop/step/slot-assembly.d.ts +5 -5
- package/dist/loop/step/slot-assembly.js +5 -5
- package/dist/loop/step/step-context.d.ts +5 -19
- package/dist/loop/step/step-context.js +9 -195
- package/dist/loop/transcript.js +7 -1
- package/dist/run/durable.d.ts +2 -6
- package/dist/run/durable.js +30 -40
- package/dist/types/execution.d.ts +0 -1
- package/package.json +6 -6
- package/dist/loop/step/compose.d.ts +0 -5
- package/dist/loop/step/compose.js +0 -109
- package/dist/loop/step/context-draft.d.ts +0 -8
- package/dist/loop/step/context-draft.js +0 -48
package/dist/loop/transcript.js
CHANGED
|
@@ -61,7 +61,7 @@ function entryAt(value, index) {
|
|
|
61
61
|
return result;
|
|
62
62
|
}
|
|
63
63
|
case "compaction": {
|
|
64
|
-
exactKeys(entry, ["kind", "turnId", "stepId", "summary", "trigger", "tokensBefore", "tokensAfter"], `Transcript entry ${index}`);
|
|
64
|
+
exactKeys(entry, ["kind", "turnId", "stepId", "summary", "trigger", "tokensBefore", "tokensAfter", "files"], `Transcript entry ${index}`);
|
|
65
65
|
requiredString(entry.stepId, `Transcript entry ${index} stepId`);
|
|
66
66
|
if (typeof entry.summary !== "string")
|
|
67
67
|
fail(`Transcript entry ${index} summary`);
|
|
@@ -70,6 +70,9 @@ function entryAt(value, index) {
|
|
|
70
70
|
for (const key of ["tokensBefore", "tokensAfter"])
|
|
71
71
|
if (!Number.isInteger(entry[key]) || entry[key] < 0)
|
|
72
72
|
fail(`Transcript entry ${index} ${key}`);
|
|
73
|
+
if (entry.files !== undefined &&
|
|
74
|
+
(!Array.isArray(entry.files) || entry.files.some((line) => typeof line !== "string")))
|
|
75
|
+
fail(`Transcript entry ${index} files`);
|
|
73
76
|
return Object.freeze({
|
|
74
77
|
kind: "compaction",
|
|
75
78
|
turnId: entry.turnId,
|
|
@@ -78,6 +81,9 @@ function entryAt(value, index) {
|
|
|
78
81
|
trigger: entry.trigger,
|
|
79
82
|
tokensBefore: entry.tokensBefore,
|
|
80
83
|
tokensAfter: entry.tokensAfter,
|
|
84
|
+
...(entry.files === undefined
|
|
85
|
+
? {}
|
|
86
|
+
: { files: Object.freeze([...entry.files]) }),
|
|
81
87
|
});
|
|
82
88
|
}
|
|
83
89
|
case "final":
|
package/dist/run/durable.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { AgentManifest } from "@nylorun/core/define";
|
|
|
2
2
|
import type { ExecutionInput, ExecutionState, RunResult } from "../types/execution.js";
|
|
3
3
|
import type { JsonObject } from "@nylorun/core/define";
|
|
4
4
|
import type { EffectOutcome } from "@nylorun/core/contracts";
|
|
5
|
-
import type { AgentRef
|
|
5
|
+
import type { AgentRef } from "@nylorun/core/define";
|
|
6
6
|
import { ENGINE_VERSION } from "../compatibility.js";
|
|
7
7
|
/** Private persistence contract for hosts; this is not a session wire type. */
|
|
8
8
|
export interface DurableCheckpoint {
|
|
@@ -30,7 +30,6 @@ export interface HostEffect {
|
|
|
30
30
|
readonly kind: "model" | "tool" | "delegation" | "agent";
|
|
31
31
|
/** Set on work for an agent used as a tool; `agentId` stays the session's root agent. */
|
|
32
32
|
readonly agent?: AgentRef;
|
|
33
|
-
readonly capabilityId?: string;
|
|
34
33
|
readonly toolName?: string;
|
|
35
34
|
/** Workflow node path (flow effects). */
|
|
36
35
|
readonly path?: string;
|
|
@@ -67,19 +66,16 @@ export type DurableResult = {
|
|
|
67
66
|
export interface DurableSessionTool {
|
|
68
67
|
/** The agent used as a tool that owns this tool; absent for the root agent. */
|
|
69
68
|
readonly agentId?: string;
|
|
70
|
-
readonly capabilityId: string;
|
|
71
69
|
readonly name: string;
|
|
72
70
|
readonly description?: string;
|
|
73
71
|
readonly inputSchema: JsonObject;
|
|
74
72
|
readonly outputSchema?: JsonObject;
|
|
75
|
-
/** `always`: each call waits for approval (a remote MCP server's, or its tool's, `approval`). */
|
|
76
|
-
readonly approval?: ApprovalMode;
|
|
77
73
|
/**
|
|
78
74
|
* Not in the model's tool list (R2b C10): the model finds it with `tool_search` and runs it
|
|
79
75
|
* with `tool_call`, which makes the call as this tool's own.
|
|
80
76
|
*/
|
|
81
77
|
readonly deferred?: boolean;
|
|
82
|
-
/** Text the model reads with the
|
|
78
|
+
/** Text the model reads with the Runtime's instructions while this tool is advertised. */
|
|
83
79
|
readonly instructions?: readonly string[];
|
|
84
80
|
}
|
|
85
81
|
export declare function createDurableCheckpoint(input: {
|
package/dist/run/durable.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { HarnessError } from "@nylorun/core/define";
|
|
2
|
+
import { effectIdOf } from "@nylorun/core/harness-api";
|
|
2
3
|
import { AgentManifestSchema } from "@nylorun/core/contracts";
|
|
3
|
-
import { TOOL_CALL_TOOL,
|
|
4
|
+
import { TOOL_CALL_TOOL, agentFrom, withHttpTarget } from "@nylorun/core/define";
|
|
4
5
|
import { definitionFor } from "../definition/agent-definition.js";
|
|
5
6
|
import { schemaFromJSON } from "@nylorun/core/define";
|
|
6
7
|
import { hashManifest } from "@nylorun/core/define";
|
|
@@ -43,7 +44,7 @@ export async function runDurable(options) {
|
|
|
43
44
|
const inFlight = new Set();
|
|
44
45
|
// Effect ids are derived from stable names, never call order, so replays line up.
|
|
45
46
|
const effect = async (kind, input, context, identity, target = {}) => {
|
|
46
|
-
const effectId =
|
|
47
|
+
const effectId = effectIdOf(checkpoint.turnId, checkpoint.segment, kind, identity);
|
|
47
48
|
const request = JSON.parse(JSON.stringify({
|
|
48
49
|
effectId,
|
|
49
50
|
sessionId: checkpoint.sessionId,
|
|
@@ -73,13 +74,12 @@ export async function runDurable(options) {
|
|
|
73
74
|
/** Tools of one agent. Each agent keeps its own patch order; children namespace their effects. */
|
|
74
75
|
const hostedTools = (ref) => {
|
|
75
76
|
let patchTail = Promise.resolve();
|
|
76
|
-
return (
|
|
77
|
+
return (tool) => ({
|
|
77
78
|
name: tool.name,
|
|
78
79
|
...(tool.description === undefined ? {} : { description: tool.description }),
|
|
79
80
|
inputSchema: schemaFromJSON(tool.inputSchema),
|
|
80
81
|
...(tool.outputSchema ? { outputSchema: schemaFromJSON(tool.outputSchema) } : {}),
|
|
81
|
-
//
|
|
82
|
-
...(tool.approval === "always" ? { approval: () => true } : {}),
|
|
82
|
+
// No approval here: the Tool Gate holds a call its policy asks a person about (R5 step 2).
|
|
83
83
|
async execute(args, ctx) {
|
|
84
84
|
const previous = patchTail;
|
|
85
85
|
let release;
|
|
@@ -99,7 +99,7 @@ export async function runDurable(options) {
|
|
|
99
99
|
info: ctx.info,
|
|
100
100
|
state: ctx.state.entries(),
|
|
101
101
|
resume: ctx.resume,
|
|
102
|
-
}, scoped(ref, ctx.invocationId), {
|
|
102
|
+
}, scoped(ref, ctx.invocationId), { toolName: tool.name, ...(ref ? { agent: ref } : {}) });
|
|
103
103
|
// Apply concurrently resolved patches in manifest call order, independent of host I/O timing.
|
|
104
104
|
await previous;
|
|
105
105
|
for (const [key, value] of Object.entries(outcome.statePatch ?? {}))
|
|
@@ -130,32 +130,31 @@ export async function runDurable(options) {
|
|
|
130
130
|
// Deferred tools stay out of the model's list; `tool_call` runs them (R2b C10).
|
|
131
131
|
const owned = mine.filter((tool) => !tool.deferred);
|
|
132
132
|
const deferred = new Map(mine.filter((tool) => tool.deferred).map((tool) => [tool.name, tool]));
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
133
|
+
const tools = {};
|
|
134
|
+
// Agents used as tools are rebuilt from their manifest body; the engine runs them. An
|
|
135
|
+
// HTTP tool keeps its target (the manifest's hash covers it) and runs as a host effect.
|
|
136
|
+
for (const tool of agent.tools ?? [])
|
|
137
|
+
if (tool.http)
|
|
138
|
+
// A pinned `approval` stays for the definition's hash; the Tool Gate holds the call.
|
|
139
|
+
tools[tool.name] = withHttpTarget(hostedTool(tool), {
|
|
140
|
+
http: tool.http,
|
|
141
|
+
...(tool.approval === undefined ? {} : { approval: tool.approval }),
|
|
142
|
+
});
|
|
143
|
+
else if (!tool.agent)
|
|
144
|
+
tools[tool.name] = hostedTool(tool);
|
|
145
|
+
// The tools the Runtime added (its `runtime` block) run as host effects too.
|
|
146
|
+
for (const tool of agent.runtime?.tools ?? [])
|
|
147
|
+
tools[tool.name] = hostedTool(tool);
|
|
148
|
+
for (const tool of owned)
|
|
149
|
+
tools[tool.name] =
|
|
150
|
+
tool.name === TOOL_CALL_TOOL
|
|
151
|
+
? deferredToolCall(tool, deferred, hostedTool)
|
|
152
|
+
: hostedTool(tool);
|
|
153
|
+
const implementations = { tools };
|
|
154
154
|
return definitionFor(agentFrom(agent, implementations, owned.length === 0
|
|
155
155
|
? undefined
|
|
156
156
|
: {
|
|
157
157
|
sessionTools: owned.map((tool) => ({
|
|
158
|
-
capabilityId: tool.capabilityId,
|
|
159
158
|
name: tool.name,
|
|
160
159
|
...(tool.instructions?.length ? { instructions: tool.instructions } : {}),
|
|
161
160
|
})),
|
|
@@ -219,8 +218,8 @@ function scoped(ref, identity) {
|
|
|
219
218
|
* `tool_call` (R2b C10): runs a deferred tool by name, as that tool's own call. It checks the
|
|
220
219
|
* arguments against the tool's `inputSchema` first, so a mistake comes back to the model as a
|
|
221
220
|
* failed result it can fix. The effect then names the tool, so the host routes it, shapes its
|
|
222
|
-
* result and records `tool.completed` as for a direct call
|
|
223
|
-
*
|
|
221
|
+
* result and records `tool.completed` as for a direct call, and the Tool Gate holds it as it
|
|
222
|
+
* would a direct call.
|
|
224
223
|
*/
|
|
225
224
|
function deferredToolCall(tool, deferred, hostedTool) {
|
|
226
225
|
const hosted = new Map();
|
|
@@ -228,19 +227,10 @@ function deferredToolCall(tool, deferred, hostedTool) {
|
|
|
228
227
|
const name = args?.name;
|
|
229
228
|
return typeof name === "string" ? deferred.get(name) : undefined;
|
|
230
229
|
};
|
|
231
|
-
const gated = [...deferred.values()].some((item) => item.approval === "always");
|
|
232
230
|
return {
|
|
233
231
|
name: tool.name,
|
|
234
232
|
...(tool.description === undefined ? {} : { description: tool.description }),
|
|
235
233
|
inputSchema: schemaFromJSON(tool.inputSchema),
|
|
236
|
-
...(gated
|
|
237
|
-
? {
|
|
238
|
-
approval: (args) => {
|
|
239
|
-
const target = targetOf(args);
|
|
240
|
-
return target?.approval === "always" ? `Approve ${target.name}?` : undefined;
|
|
241
|
-
},
|
|
242
|
-
}
|
|
243
|
-
: {}),
|
|
244
234
|
async execute(args, ctx) {
|
|
245
235
|
const call = args;
|
|
246
236
|
const target = targetOf(call);
|
|
@@ -261,7 +251,7 @@ function deferredToolCall(tool, deferred, hostedTool) {
|
|
|
261
251
|
};
|
|
262
252
|
let run = hosted.get(target.name);
|
|
263
253
|
if (!run) {
|
|
264
|
-
run = hostedTool(target
|
|
254
|
+
run = hostedTool(target);
|
|
265
255
|
hosted.set(target.name, run);
|
|
266
256
|
}
|
|
267
257
|
return run.execute(checked.value, ctx);
|
|
@@ -18,7 +18,6 @@ export type ExecutionInput = MessageInput | InputEvent | {
|
|
|
18
18
|
};
|
|
19
19
|
/** Portable reference to deployed code. Applications must preserve this value intact. */
|
|
20
20
|
export interface ToolReference {
|
|
21
|
-
readonly capabilityId: string;
|
|
22
21
|
readonly toolName: string;
|
|
23
22
|
readonly descriptor: {
|
|
24
23
|
readonly name: string;
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nylorun/harness",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.27.0-beta",
|
|
4
4
|
"description": "Nylorun's TypeScript agent execution engine. See github.com/nylorun/agents.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/nylorun/agents.git",
|
|
10
|
-
"directory": "harness"
|
|
10
|
+
"directory": "runtime/harness"
|
|
11
11
|
},
|
|
12
12
|
"homepage": "https://github.com/nylorun/agents",
|
|
13
13
|
"bugs": "https://github.com/nylorun/agents/issues",
|
|
@@ -57,9 +57,9 @@
|
|
|
57
57
|
"node": "^22.14.0 || ^24.0.0 || >=26.0.0"
|
|
58
58
|
},
|
|
59
59
|
"scripts": {
|
|
60
|
-
"build": "npm run clean && node
|
|
60
|
+
"build": "npm run clean && node ../../scripts/check-typescript-version.mjs && tsc -p tsconfig.json",
|
|
61
61
|
"check": "npm run format:check && npm run build && npm run test:types && npm test && node scripts/check-package.mjs",
|
|
62
|
-
"typecheck": "node
|
|
62
|
+
"typecheck": "node ../../scripts/check-typescript-version.mjs && tsc -p tsconfig.json --noEmit",
|
|
63
63
|
"test": "vitest run",
|
|
64
64
|
"test:types": "tsc -p test/types/tsconfig.json",
|
|
65
65
|
"format": "prettier --write .",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"eval:compaction": "vitest run test/compaction-eval.test.ts"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@types/node": "^26.
|
|
72
|
+
"@types/node": "^26.6.4",
|
|
73
73
|
"@typescript/native": "npm:typescript@^7.0.2",
|
|
74
74
|
"prettier": "^3.9.9",
|
|
75
75
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
"zod": "^4.6.5"
|
|
78
78
|
},
|
|
79
79
|
"dependencies": {
|
|
80
|
-
"@nylorun/core": "0.
|
|
80
|
+
"@nylorun/core": "0.21.0-beta"
|
|
81
81
|
},
|
|
82
82
|
"peerDependencies": {
|
|
83
83
|
"zod": "^4.6.5"
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { BoundMiddleware } from "@nylorun/core/define";
|
|
2
|
-
import type { StepResponse } from "@nylorun/core/define";
|
|
3
|
-
import type { ObserveEmit } from "../observe.js";
|
|
4
|
-
import { StepContext } from "./step-context.js";
|
|
5
|
-
export declare function runMiddleware(middleware: readonly BoundMiddleware[], context: StepContext, terminal: () => Promise<StepResponse>, observe: ObserveEmit): Promise<StepResponse>;
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { HostSuspension } from "../host-suspension.js";
|
|
2
|
-
import { HarnessError, isHarnessError } from "@nylorun/core/define";
|
|
3
|
-
import { isBrandedResponse, StepContext } from "./step-context.js";
|
|
4
|
-
export async function runMiddleware(middleware, context, terminal, observe) {
|
|
5
|
-
const dispatch = async (index) => {
|
|
6
|
-
if (context.currentTripwire)
|
|
7
|
-
return context.tripwire(context.currentTripwire);
|
|
8
|
-
if (index === middleware.length)
|
|
9
|
-
return terminal();
|
|
10
|
-
const item = middleware[index];
|
|
11
|
-
const lease = context.requestFacade(item.id, index);
|
|
12
|
-
let returned = false;
|
|
13
|
-
let nextCalledTwice = false;
|
|
14
|
-
let nextPromise;
|
|
15
|
-
observe({
|
|
16
|
-
type: "middleware.entered",
|
|
17
|
-
turnId: context.input.turnId,
|
|
18
|
-
stepId: context.input.stepId,
|
|
19
|
-
middlewareId: item.id,
|
|
20
|
-
});
|
|
21
|
-
try {
|
|
22
|
-
let handlerError;
|
|
23
|
-
let result;
|
|
24
|
-
try {
|
|
25
|
-
result = await item.handle(lease.value, () => {
|
|
26
|
-
if (returned)
|
|
27
|
-
throw new HarnessError("middleware.next-after-return", `Middleware '${item.id}' called next() after returning`, { details: { middlewareId: item.id } });
|
|
28
|
-
if (nextPromise) {
|
|
29
|
-
nextCalledTwice = true;
|
|
30
|
-
throw new HarnessError("middleware.next-called-twice", `Middleware '${item.id}' called next() more than once`, { details: { middlewareId: item.id } });
|
|
31
|
-
}
|
|
32
|
-
lease.revokeMutators();
|
|
33
|
-
nextPromise = dispatch(index + 1);
|
|
34
|
-
return nextPromise;
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
handlerError = error;
|
|
39
|
-
}
|
|
40
|
-
finally {
|
|
41
|
-
returned = true;
|
|
42
|
-
lease.revokeMutators();
|
|
43
|
-
}
|
|
44
|
-
let inner;
|
|
45
|
-
if (nextPromise) {
|
|
46
|
-
try {
|
|
47
|
-
inner = await nextPromise;
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
handlerError ??= error;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
if (handlerError instanceof HostSuspension)
|
|
54
|
-
throw handlerError;
|
|
55
|
-
if (handlerError) {
|
|
56
|
-
if (nextCalledTwice ||
|
|
57
|
-
(isHarnessError(handlerError) && handlerError.code === "middleware.next-called-twice")) {
|
|
58
|
-
return context.tripwire({
|
|
59
|
-
code: "middleware.next-called-twice",
|
|
60
|
-
message: message(handlerError),
|
|
61
|
-
scope: "execution",
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
return context.tripwire({
|
|
65
|
-
code: isHarnessError(handlerError) ? handlerError.code : "middleware.failed",
|
|
66
|
-
message: message(handlerError),
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
if (inner) {
|
|
70
|
-
if (result !== inner) {
|
|
71
|
-
return context.tripwire({
|
|
72
|
-
code: "middleware.invalid-response",
|
|
73
|
-
message: `Middleware '${item.id}' must return the StepResponse from next()`,
|
|
74
|
-
// Calling next() completed the step successfully; forgetting to
|
|
75
|
-
// return that response is isolated to this step. A non-response
|
|
76
|
-
// replacement remains a execution-level middleware-contract breach.
|
|
77
|
-
scope: result === undefined ? "step" : "execution",
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
return inner;
|
|
81
|
-
}
|
|
82
|
-
if (!isBrandedResponse(result)) {
|
|
83
|
-
return context.tripwire({
|
|
84
|
-
code: "middleware.invalid-response",
|
|
85
|
-
message: `Middleware '${item.id}' must return a branded StepResponse`,
|
|
86
|
-
scope: "execution",
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
return result;
|
|
90
|
-
}
|
|
91
|
-
finally {
|
|
92
|
-
observe({
|
|
93
|
-
type: "middleware.completed",
|
|
94
|
-
turnId: context.input.turnId,
|
|
95
|
-
stepId: context.input.stepId,
|
|
96
|
-
middlewareId: item.id,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
try {
|
|
101
|
-
return await dispatch(0);
|
|
102
|
-
}
|
|
103
|
-
finally {
|
|
104
|
-
context.seal();
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
function message(error) {
|
|
108
|
-
return error instanceof Error ? error.message : String(error);
|
|
109
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { ContextMutationOptions, ContextSnapshot } from "@nylorun/core/define";
|
|
2
|
-
import type { ContextItem } from "@nylorun/core/define";
|
|
3
|
-
/** Per-step model-visible context supplied explicitly by middleware. */
|
|
4
|
-
export declare class ContextDraft {
|
|
5
|
-
#private;
|
|
6
|
-
set(middlewareId: string, middlewareOrder: number, slot: string, items: readonly ContextItem[], options?: ContextMutationOptions): void;
|
|
7
|
-
snapshot(): ContextSnapshot;
|
|
8
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { HarnessError } from "@nylorun/core/define";
|
|
2
|
-
import { copyJson } from "@nylorun/core/define";
|
|
3
|
-
import { SlotDraft } from "./slot-assembly.js";
|
|
4
|
-
const CONTEXT_TYPE = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
|
|
5
|
-
/** Per-step model-visible context supplied explicitly by middleware. */
|
|
6
|
-
export class ContextDraft {
|
|
7
|
-
#slots = new SlotDraft();
|
|
8
|
-
set(middlewareId, middlewareOrder, slot, items, options) {
|
|
9
|
-
if (!Array.isArray(items))
|
|
10
|
-
throw new HarnessError("context.invalid-item", "Runtime context items must be an array");
|
|
11
|
-
this.#slots.set({
|
|
12
|
-
middlewareId,
|
|
13
|
-
middlewareOrder,
|
|
14
|
-
slot,
|
|
15
|
-
value: Object.freeze(items.map(normalizeItem)),
|
|
16
|
-
order: options?.order,
|
|
17
|
-
reason: options?.reason,
|
|
18
|
-
invalidSlot: "context.invalid-slot",
|
|
19
|
-
invalidOrder: "context.invalid-order",
|
|
20
|
-
invalidReason: "context.invalid-reason",
|
|
21
|
-
label: "Runtime context",
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
snapshot() {
|
|
25
|
-
const slots = this.#slots.values();
|
|
26
|
-
const items = Object.freeze(slots.flatMap((slot) => slot.value));
|
|
27
|
-
const contributors = Object.freeze(slots.map((slot) => contributor(slot.owner, slot.reason)));
|
|
28
|
-
return Object.freeze({ items, contributors });
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
function normalizeItem(item) {
|
|
32
|
-
if (!item || typeof item !== "object")
|
|
33
|
-
throw new HarnessError("context.invalid-item", "Runtime context items must be objects");
|
|
34
|
-
if (item.type !== undefined && (typeof item.type !== "string" || !CONTEXT_TYPE.test(item.type)))
|
|
35
|
-
throw new HarnessError("context.invalid-item-type", "Runtime context item type must match [A-Za-z][A-Za-z0-9_-]{0,63}");
|
|
36
|
-
return Object.freeze({
|
|
37
|
-
...(item.type === undefined ? {} : { type: item.type }),
|
|
38
|
-
value: copyJson(item.value),
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
function contributor(owner, reason) {
|
|
42
|
-
return Object.freeze({
|
|
43
|
-
middlewareId: owner.middlewareId,
|
|
44
|
-
slot: owner.slot,
|
|
45
|
-
order: owner.order,
|
|
46
|
-
...(reason === undefined ? {} : { reason }),
|
|
47
|
-
});
|
|
48
|
-
}
|