@nylorun/core 0.8.0-beta → 0.9.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 +212 -0
- package/dist/compatibility.d.ts +42 -4
- package/dist/compatibility.js +65 -3
- package/dist/contracts.d.ts +2165 -328
- package/dist/contracts.js +1098 -99
- package/dist/define.d.ts +5 -5
- package/dist/define.js +3 -2
- package/dist/definition/assemble.js +15 -20
- package/dist/definition/bind-tool.js +1 -0
- package/dist/definition/bound.d.ts +2 -0
- package/dist/definition/builder.d.ts +4 -10
- package/dist/definition/builder.js +2 -26
- package/dist/definition/declaration.js +6 -2
- package/dist/definition/flow/compile.d.ts +0 -2
- package/dist/definition/flow/compile.js +1 -29
- package/dist/definition/flow/types.d.ts +1 -3
- package/dist/definition/model-failure.d.ts +4 -0
- package/dist/definition/model-failure.js +22 -0
- package/dist/definition/sandbox-capability.d.ts +12 -0
- package/dist/definition/sandbox-capability.js +33 -0
- package/dist/definition/schema.js +3 -0
- package/dist/definition/workflow/build.d.ts +3 -13
- package/dist/definition/workflow/build.js +3 -25
- package/dist/definition/workflow/chain.js +0 -1
- package/dist/definition/workflow/loop.js +0 -2
- package/dist/definition/workflow/map.js +0 -1
- package/dist/definition/workflow/parallel.js +0 -1
- package/dist/definition/workflow/runnable.d.ts +0 -4
- package/dist/definition/workflow/runnable.js +0 -34
- package/dist/definition/workflow/switch.js +0 -2
- package/dist/errors.d.ts +3 -1
- package/dist/types/middleware.d.ts +1 -3
- package/dist/types/model.d.ts +32 -1
- package/dist/types/observe.d.ts +7 -1
- package/dist/types/tool.d.ts +6 -0
- package/dist/types/transcript.d.ts +14 -1
- package/dist/utils/sandbox.d.ts +0 -4
- package/dist/utils/sandbox.js +0 -16
- package/package.json +1 -1
- package/dist/definition/sandbox.d.ts +0 -29
- package/dist/definition/sandbox.js +0 -93
|
@@ -37,25 +37,6 @@ export function builtAgentOf(run) {
|
|
|
37
37
|
return run.build();
|
|
38
38
|
failOne("workflow.invalid-runnable", "Expected a built agent or Agent builder");
|
|
39
39
|
}
|
|
40
|
-
/** Collect sandbox specs declared on an agent (and its delegates, one level). */
|
|
41
|
-
export function sandboxSpecsOf(agent) {
|
|
42
|
-
const specs = [];
|
|
43
|
-
const binding = bindingFromAgent(agent);
|
|
44
|
-
for (const cap of binding.manifest.capabilities) {
|
|
45
|
-
if (cap.sandbox)
|
|
46
|
-
specs.push(cap.sandbox);
|
|
47
|
-
}
|
|
48
|
-
for (const tool of binding.tools) {
|
|
49
|
-
const child = tool.delegate?.agent;
|
|
50
|
-
if (!child)
|
|
51
|
-
continue;
|
|
52
|
-
for (const cap of child.manifest.capabilities) {
|
|
53
|
-
if (cap.sandbox)
|
|
54
|
-
specs.push(cap.sandbox);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return specs;
|
|
58
|
-
}
|
|
59
40
|
/**
|
|
60
41
|
* Project a tool for use as a workflow node.
|
|
61
42
|
* Unlike agent tools, workflow tool nodes may take any JSON Schema root type (workflows.md §9).
|
|
@@ -114,7 +95,6 @@ function agentNode(agent) {
|
|
|
114
95
|
return {
|
|
115
96
|
node: { agent: agent.id },
|
|
116
97
|
agents: { [agent.id]: binding },
|
|
117
|
-
sandboxSpecs: sandboxSpecsOf(agent),
|
|
118
98
|
};
|
|
119
99
|
}
|
|
120
100
|
function resolveRunnable(run, rename) {
|
|
@@ -130,7 +110,6 @@ function resolveRunnable(run, rename) {
|
|
|
130
110
|
agents: { ...binding.agents },
|
|
131
111
|
// A rename moves the whole subtree: the harness path uses the new part, not run.id.
|
|
132
112
|
nodes: remapNodeKeys({ ...binding.nodes }, run.id, id),
|
|
133
|
-
sandboxSpecs: sandboxSpecsFromAgents(binding.agents),
|
|
134
113
|
isReshapingSlot: false,
|
|
135
114
|
};
|
|
136
115
|
}
|
|
@@ -143,7 +122,6 @@ function resolveRunnable(run, rename) {
|
|
|
143
122
|
agents: {},
|
|
144
123
|
nodes: { [id]: { kind: "tool", tool: bound } },
|
|
145
124
|
tool: bound,
|
|
146
|
-
sandboxSpecs: [],
|
|
147
125
|
isReshapingSlot: false,
|
|
148
126
|
};
|
|
149
127
|
}
|
|
@@ -157,22 +135,11 @@ function resolveRunnable(run, rename) {
|
|
|
157
135
|
node: resolved.node,
|
|
158
136
|
agents: resolved.agents,
|
|
159
137
|
nodes: {},
|
|
160
|
-
sandboxSpecs: resolved.sandboxSpecs,
|
|
161
138
|
isReshapingSlot: false,
|
|
162
139
|
};
|
|
163
140
|
}
|
|
164
141
|
failOne("workflow.invalid-runnable", "Unsupported workflow runnable");
|
|
165
142
|
}
|
|
166
|
-
function sandboxSpecsFromAgents(agents) {
|
|
167
|
-
const specs = [];
|
|
168
|
-
for (const binding of Object.values(agents)) {
|
|
169
|
-
for (const cap of binding.manifest.capabilities) {
|
|
170
|
-
if (cap.sandbox)
|
|
171
|
-
specs.push(cap.sandbox);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
return specs;
|
|
175
|
-
}
|
|
176
143
|
/**
|
|
177
144
|
* Resolve a runnable or slot into a manifest node plus local bindings.
|
|
178
145
|
* When the slot has `input`, the node is wrapped as `{ slot: { id?, input, run } }`.
|
|
@@ -206,7 +173,6 @@ export function resolveChild(child, defaultRename) {
|
|
|
206
173
|
agents: resolved.agents,
|
|
207
174
|
nodes: remapped,
|
|
208
175
|
tool: resolved.tool,
|
|
209
|
-
sandboxSpecs: resolved.sandboxSpecs,
|
|
210
176
|
isReshapingSlot: hasInput,
|
|
211
177
|
};
|
|
212
178
|
}
|
|
@@ -30,7 +30,6 @@ export function Switch(options) {
|
|
|
30
30
|
mergeChild(acc, {
|
|
31
31
|
agents: resolved.agents,
|
|
32
32
|
nodes: remapNodeKeys(resolved.nodes, resolved.id, casePath),
|
|
33
|
-
sandboxSpecs: resolved.sandboxSpecs,
|
|
34
33
|
});
|
|
35
34
|
caseNodes[key] = resolved.node;
|
|
36
35
|
}
|
|
@@ -41,7 +40,6 @@ export function Switch(options) {
|
|
|
41
40
|
mergeChild(acc, {
|
|
42
41
|
agents: resolved.agents,
|
|
43
42
|
nodes: remapNodeKeys(resolved.nodes, resolved.id, defaultPath),
|
|
44
|
-
sandboxSpecs: resolved.sandboxSpecs,
|
|
45
43
|
});
|
|
46
44
|
defaultNode = resolved.node;
|
|
47
45
|
}
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/** Machine-readable error raised by Harness-owned code. */
|
|
2
|
-
export type HarnessErrorCode = "execution.invalid-state" | "execution.invalid-input" | "execution.incompatible" | "execution.record-failed" | "tool.unregistered" | "tool.invalid-binding" | "agent.build-failed" | "agent.lifecycle-sealed" | "context.invalid-item" | "context.invalid-item-type" | "context.invalid-order" | "context.invalid-reason" | "context.invalid-slot" | "interaction.invalid" | "interaction.missing-resume" | "interaction.uncorrelated-resume" | "input.invalid-content" | "output.invalid" | "output.invalid-schema" | "json.invalid-data" | "json.invalid-object" | "middleware.next-after-return" | "middleware.next-called-twice" | "middleware.request-mutators-revoked" | "model.candidate-missing" | "model.adapter-invalid-options" | "model.adapter-invalid-response" | "model.invalid-candidate" | "model.invalid-directive" | "model.unsupported-content" | "model.unsupported-output-schema"
|
|
2
|
+
export type HarnessErrorCode = "execution.invalid-state" | "execution.invalid-input" | "execution.incompatible" | "execution.record-failed" | "tool.unregistered" | "tool.invalid-binding" | "agent.build-failed" | "agent.lifecycle-sealed" | "context.invalid-item" | "context.invalid-item-type" | "context.invalid-order" | "context.invalid-reason" | "context.invalid-slot" | "interaction.invalid" | "interaction.missing-resume" | "interaction.uncorrelated-resume" | "input.invalid-content" | "output.invalid" | "output.invalid-schema" | "json.invalid-data" | "json.invalid-object" | "middleware.next-after-return" | "middleware.next-called-twice" | "middleware.request-mutators-revoked" | "model.candidate-missing" | "model.adapter-invalid-options" | "model.adapter-invalid-response" | "model.invalid-candidate" | "model.invalid-directive" | "model.unsupported-content" | "model.unsupported-output-schema"
|
|
3
|
+
/** A provider failure the model adapter classified (Model Calls §6.1). */
|
|
4
|
+
| `model.${import("./types/model.js").ModelFailureCode}` | "configuration.duplicate-tool-name" | "configuration.invalid" | "configuration.invalid-instructions" | "configuration.invalid-order" | "configuration.invalid-reason" | "configuration.invalid-slot" | "configuration.invalid-tools" | "configuration.model-selection-conflict" | "response.invalid-replacement" | "tool.invalid" | "tool.invalid-arguments" | "tool.invalid-output" | "tool.invalid-name" | "tool.invalid-schema" | "tool.invalid-tool-result";
|
|
3
5
|
export type HarnessErrorDetails = Readonly<Record<string, string | number | boolean>>;
|
|
4
6
|
export interface HarnessErrorOptions {
|
|
5
7
|
readonly cause?: unknown;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ContextMutationOptions, ModelCandidate, ModelDirective, ModelToolCall, ModelConfigurationMutationOptions } from "./model.js";
|
|
2
2
|
import type { InputEvent, TranscriptEntry } from "./transcript.js";
|
|
3
3
|
import type { AgentTool } from "./agent.js";
|
|
4
|
-
import type { McpServerManifest,
|
|
4
|
+
import type { McpServerManifest, SkillManifest } from "./manifest.js";
|
|
5
5
|
/** Skill files held on the developer binding. Not a manifest field. */
|
|
6
6
|
export interface SkillRecord {
|
|
7
7
|
readonly name: string;
|
|
@@ -70,8 +70,6 @@ export interface CapabilityDeclaration<Info = unknown> {
|
|
|
70
70
|
/** File contents for load_skill. Omitted from the manifest. */
|
|
71
71
|
readonly skillRecords?: Readonly<Record<string, SkillRecord>>;
|
|
72
72
|
readonly mcpServers?: Readonly<Record<string, McpServerManifest>>;
|
|
73
|
-
/** Runtime-owned sandbox requirements. Declare the built-in sandbox tools alongside it. */
|
|
74
|
-
readonly sandbox?: SandboxManifest;
|
|
75
73
|
/** Resolved package directory. Not projected into the manifest. */
|
|
76
74
|
readonly pluginRoot?: string;
|
|
77
75
|
/**
|
package/dist/types/model.d.ts
CHANGED
|
@@ -30,6 +30,8 @@ export interface ModelUsage {
|
|
|
30
30
|
readonly outputTokens?: number;
|
|
31
31
|
readonly totalTokens?: number;
|
|
32
32
|
readonly cachedTokens?: number;
|
|
33
|
+
/** Input tokens written to the provider's prompt cache. */
|
|
34
|
+
readonly cacheWriteTokens?: number;
|
|
33
35
|
readonly reasoningTokens?: number;
|
|
34
36
|
readonly costUsd?: number;
|
|
35
37
|
}
|
|
@@ -46,6 +48,19 @@ export interface ModelCandidate {
|
|
|
46
48
|
readonly usage?: ModelUsage;
|
|
47
49
|
readonly evidence?: ModelEvidence;
|
|
48
50
|
}
|
|
51
|
+
/** Why a model call failed in a known way (Model Calls §6.1). */
|
|
52
|
+
export type ModelFailureCode = "context_overflow" | "rate_limited" | "overloaded" | "timeout" | "transient" | "content_policy" | "auth" | "invalid_request" | "invalid_output";
|
|
53
|
+
/**
|
|
54
|
+
* A model call that failed in a known way. It is a completed outcome, not a lost one:
|
|
55
|
+
* the engine fails the step with `model.<code>` instead of treating the call as uncertain.
|
|
56
|
+
*/
|
|
57
|
+
export interface ModelFailureOutcome {
|
|
58
|
+
readonly kind: "failed";
|
|
59
|
+
readonly code: ModelFailureCode;
|
|
60
|
+
readonly message: string;
|
|
61
|
+
readonly retryable: boolean;
|
|
62
|
+
readonly retryAfterMs?: number;
|
|
63
|
+
}
|
|
49
64
|
export interface ModelControls {
|
|
50
65
|
readonly temperature?: number;
|
|
51
66
|
readonly maxOutputTokens?: number;
|
|
@@ -138,6 +153,12 @@ export type PromptContentPart = {
|
|
|
138
153
|
readonly name: string;
|
|
139
154
|
readonly args: JsonObject;
|
|
140
155
|
};
|
|
156
|
+
/** The provider, API and model that produced an assistant message (Model Calls §12). */
|
|
157
|
+
export interface ModelProducer {
|
|
158
|
+
readonly provider: string;
|
|
159
|
+
readonly api?: string;
|
|
160
|
+
readonly model: string;
|
|
161
|
+
}
|
|
141
162
|
export type PromptItem = {
|
|
142
163
|
readonly kind: "instructions";
|
|
143
164
|
readonly role: "system";
|
|
@@ -146,6 +167,8 @@ export type PromptItem = {
|
|
|
146
167
|
readonly kind: "message";
|
|
147
168
|
readonly role: "user" | "assistant";
|
|
148
169
|
readonly content: readonly PromptContentPart[];
|
|
170
|
+
/** For assistant messages: the model that produced it, when the adapter recorded one. */
|
|
171
|
+
readonly producer?: ModelProducer;
|
|
149
172
|
} | {
|
|
150
173
|
readonly kind: "tool-result";
|
|
151
174
|
readonly toolCallId: string;
|
|
@@ -174,6 +197,14 @@ export interface ModelAdapterContext {
|
|
|
174
197
|
readonly request: ModelRequest;
|
|
175
198
|
readonly invocationId: string;
|
|
176
199
|
readonly signal: AbortSignal;
|
|
200
|
+
/** Set when the engine calls the model to summarize history (Model Calls §8). */
|
|
201
|
+
readonly compaction?: {
|
|
202
|
+
readonly trigger: "threshold" | "overflow";
|
|
203
|
+
readonly tokensBefore: number;
|
|
204
|
+
readonly keptTokens: number;
|
|
205
|
+
/** An intermediate call of a chunked summary; only the last one completes the compaction. */
|
|
206
|
+
readonly partial?: boolean;
|
|
207
|
+
};
|
|
177
208
|
/** Publishes one JSON-safe provider request derived from the canonical ModelCall. */
|
|
178
209
|
reportPreparedCall(prepared: ModelPreparedCall): void;
|
|
179
210
|
}
|
|
@@ -181,4 +212,4 @@ export interface ModelPreparedCall {
|
|
|
181
212
|
readonly adapter: string;
|
|
182
213
|
readonly call: JsonValue;
|
|
183
214
|
}
|
|
184
|
-
export type ModelAdapter = (call: ModelCall, context: ModelAdapterContext) => Promise<ModelCandidate | string>;
|
|
215
|
+
export type ModelAdapter = (call: ModelCall, context: ModelAdapterContext) => Promise<ModelCandidate | ModelFailureOutcome | string>;
|
package/dist/types/observe.d.ts
CHANGED
|
@@ -8,7 +8,13 @@ export interface WorkflowErrorPayload {
|
|
|
8
8
|
readonly message: string;
|
|
9
9
|
readonly path?: string;
|
|
10
10
|
}
|
|
11
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* LiveEvent payloads for workflow sessions (`workflows.md` §11, `loops.md` §4.6).
|
|
13
|
+
*
|
|
14
|
+
* @deprecated Use the event catalog (`EVENT_CATALOG`, `SessionEventOf<"node.agent">` in
|
|
15
|
+
* `@nylorun/core/contracts`): it types the events the Runtime writes. `node.completed`,
|
|
16
|
+
* `node.failed`, `switch.selected`, `map.items` and `loop.waiting` are not written.
|
|
17
|
+
*/
|
|
12
18
|
export type WorkflowLiveEventPayload = {
|
|
13
19
|
readonly type: "node.started";
|
|
14
20
|
readonly path: string;
|
package/dist/types/tool.d.ts
CHANGED
|
@@ -79,6 +79,12 @@ export interface ToolDefinition<InputSchema extends ToolInputSchema = ToolInputS
|
|
|
79
79
|
readonly approval?: ToolApproval<InputSchema>;
|
|
80
80
|
/** Code-only metadata — never serialized into the manifest. */
|
|
81
81
|
readonly effects?: ToolEffects;
|
|
82
|
+
/**
|
|
83
|
+
* Runs in the background when an Action endpoint serves it: the endpoint answers the delivery
|
|
84
|
+
* at once (`202`), heartbeats while the tool runs, and posts its outcome when it ends. For
|
|
85
|
+
* tools that run longer than an endpoint's timeout. Code-only metadata — never serialized.
|
|
86
|
+
*/
|
|
87
|
+
readonly background?: boolean;
|
|
82
88
|
}
|
|
83
89
|
/** A plain return value is treated as `{ kind: "completed", output }`. Legacy tagged outcomes remain valid. */
|
|
84
90
|
export type ToolRunResult<Output = ToolContent> = Output | ToolOutcome<Output>;
|
|
@@ -62,4 +62,17 @@ export interface TranscriptFinalEntry {
|
|
|
62
62
|
readonly stepId: string;
|
|
63
63
|
readonly output: JsonValue;
|
|
64
64
|
}
|
|
65
|
-
|
|
65
|
+
/**
|
|
66
|
+
* A summary that replaced the older part of the transcript (Model Calls §8). It is always
|
|
67
|
+
* the first entry; the entries after it are the kept tail, sent verbatim.
|
|
68
|
+
*/
|
|
69
|
+
export interface TranscriptCompactionEntry {
|
|
70
|
+
readonly kind: "compaction";
|
|
71
|
+
readonly turnId: string;
|
|
72
|
+
readonly stepId: string;
|
|
73
|
+
readonly summary: string;
|
|
74
|
+
readonly trigger: "threshold" | "overflow";
|
|
75
|
+
readonly tokensBefore: number;
|
|
76
|
+
readonly tokensAfter: number;
|
|
77
|
+
}
|
|
78
|
+
export type TranscriptEntry = TranscriptInputEntry | TranscriptCandidateEntry | TranscriptToolsEntry | TranscriptFinalEntry | TranscriptCompactionEntry;
|
package/dist/utils/sandbox.d.ts
CHANGED
|
@@ -4,10 +4,6 @@ export declare const SANDBOX_TOOL_NAMES: readonly ["bash", "read", "write", "edi
|
|
|
4
4
|
export type SandboxToolName = (typeof SANDBOX_TOOL_NAMES)[number];
|
|
5
5
|
export declare const SANDBOX_WORKSPACE = "/workspace";
|
|
6
6
|
export declare const SANDBOX_NETWORK_PRESETS: readonly ["none", "dev", "open"];
|
|
7
|
-
/** Manifest fields this version implements. Anything else is rejected with a teaching error. */
|
|
8
|
-
export declare const SANDBOX_FIELDS: readonly ["image", "network", "resources", "idle"];
|
|
9
|
-
/** Fields from the sandbox design that later versions will accept. */
|
|
10
|
-
export declare const SANDBOX_DEFERRED_FIELDS: readonly ["setup", "setupKey", "files", "secrets", "mount", "onStart", "scope", "isolation", "own", "tools", "snapshot"];
|
|
11
7
|
export declare function isSandboxToolName(name: string): name is SandboxToolName;
|
|
12
8
|
/** Parse "15m", "30s", "1h" into milliseconds. Returns undefined when invalid. */
|
|
13
9
|
export declare function parseSandboxDuration(value: string): number | undefined;
|
package/dist/utils/sandbox.js
CHANGED
|
@@ -10,22 +10,6 @@ export const SANDBOX_TOOL_NAMES = Object.freeze([
|
|
|
10
10
|
]);
|
|
11
11
|
export const SANDBOX_WORKSPACE = "/workspace";
|
|
12
12
|
export const SANDBOX_NETWORK_PRESETS = Object.freeze(["none", "dev", "open"]);
|
|
13
|
-
/** Manifest fields this version implements. Anything else is rejected with a teaching error. */
|
|
14
|
-
export const SANDBOX_FIELDS = Object.freeze(["image", "network", "resources", "idle"]);
|
|
15
|
-
/** Fields from the sandbox design that later versions will accept. */
|
|
16
|
-
export const SANDBOX_DEFERRED_FIELDS = Object.freeze([
|
|
17
|
-
"setup",
|
|
18
|
-
"setupKey",
|
|
19
|
-
"files",
|
|
20
|
-
"secrets",
|
|
21
|
-
"mount",
|
|
22
|
-
"onStart",
|
|
23
|
-
"scope",
|
|
24
|
-
"isolation",
|
|
25
|
-
"own",
|
|
26
|
-
"tools",
|
|
27
|
-
"snapshot",
|
|
28
|
-
]);
|
|
29
13
|
export function isSandboxToolName(name) {
|
|
30
14
|
return SANDBOX_TOOL_NAMES.includes(name);
|
|
31
15
|
}
|
package/package.json
CHANGED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { CapabilityDeclaration } from "../types/middleware.js";
|
|
2
|
-
import type { SandboxManifest } from "../types/manifest.js";
|
|
3
|
-
import type { ToolDefinition } from "../types/tool.js";
|
|
4
|
-
/** What computer the agent needs. The Runtime decides where it runs. */
|
|
5
|
-
export type SandboxOptions = SandboxManifest;
|
|
6
|
-
export interface SandboxCapabilityOptions {
|
|
7
|
-
/** Capability id. Defaults to `"sandbox"`. */
|
|
8
|
-
readonly id?: string;
|
|
9
|
-
}
|
|
10
|
-
export interface SandboxCapability extends CapabilityDeclaration {
|
|
11
|
-
readonly sandbox: SandboxManifest;
|
|
12
|
-
readonly tools: readonly ToolDefinition[];
|
|
13
|
-
}
|
|
14
|
-
export declare class SandboxError extends Error {
|
|
15
|
-
readonly code: string;
|
|
16
|
-
constructor(code: string, message: string);
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Give the agent an isolated Linux machine with a persistent `/workspace`.
|
|
20
|
-
*
|
|
21
|
-
* Adds the built-in `bash`, `read`, `write`, `edit`, `grep` and `glob` tools. They run in the
|
|
22
|
-
* Nylorun Runtime, which picks the backend. Agent code declares requirements only.
|
|
23
|
-
*
|
|
24
|
-
* ```ts
|
|
25
|
-
* Agent({ id: "analyst" }).instructions("...").sandbox()
|
|
26
|
-
* Agent({ id: "analyst" }).sandbox({ image: "node:24", network: { allow: ["api.github.com"] } })
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
export declare function sandbox(options?: SandboxOptions, capability?: SandboxCapabilityOptions): SandboxCapability;
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { SANDBOX_DEFERRED_FIELDS, SANDBOX_FIELDS, SANDBOX_NETWORK_PRESETS, isSandboxHostPattern, parseSandboxDuration, parseSandboxSize, } from "../utils/sandbox.js";
|
|
2
|
-
import { SANDBOX_INSTRUCTIONS, createSandboxTools } from "./sandbox-tools.js";
|
|
3
|
-
export class SandboxError extends Error {
|
|
4
|
-
code;
|
|
5
|
-
constructor(code, message) {
|
|
6
|
-
super(message);
|
|
7
|
-
this.name = "SandboxError";
|
|
8
|
-
this.code = code;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Give the agent an isolated Linux machine with a persistent `/workspace`.
|
|
13
|
-
*
|
|
14
|
-
* Adds the built-in `bash`, `read`, `write`, `edit`, `grep` and `glob` tools. They run in the
|
|
15
|
-
* Nylorun Runtime, which picks the backend. Agent code declares requirements only.
|
|
16
|
-
*
|
|
17
|
-
* ```ts
|
|
18
|
-
* Agent({ id: "analyst" }).instructions("...").sandbox()
|
|
19
|
-
* Agent({ id: "analyst" }).sandbox({ image: "node:24", network: { allow: ["api.github.com"] } })
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
export function sandbox(options = {}, capability = {}) {
|
|
23
|
-
const id = capability.id ?? "sandbox";
|
|
24
|
-
if (!isRecord(options))
|
|
25
|
-
throw new SandboxError("sandbox.invalid", "sandbox() expects an options object");
|
|
26
|
-
for (const key of Object.keys(options)) {
|
|
27
|
-
if (SANDBOX_DEFERRED_FIELDS.includes(key))
|
|
28
|
-
throw new SandboxError("sandbox.unsupported", `sandbox({ ${key} }) is not supported in this version. ` +
|
|
29
|
-
`Supported options: ${SANDBOX_FIELDS.join(", ")}. See docs/design/sandboxes.md for what is planned.`);
|
|
30
|
-
if (!SANDBOX_FIELDS.includes(key))
|
|
31
|
-
throw new SandboxError("sandbox.unknown-option", `Unknown sandbox option '${key}'. Supported options: ${SANDBOX_FIELDS.join(", ")}`);
|
|
32
|
-
}
|
|
33
|
-
return Object.freeze({
|
|
34
|
-
id,
|
|
35
|
-
instructions: Object.freeze([SANDBOX_INSTRUCTIONS]),
|
|
36
|
-
tools: createSandboxTools(),
|
|
37
|
-
sandbox: freezeOptions(options),
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
function freezeOptions(options) {
|
|
41
|
-
const result = {};
|
|
42
|
-
if (options.image !== undefined) {
|
|
43
|
-
if (typeof options.image !== "string" || options.image.trim().length === 0)
|
|
44
|
-
throw new SandboxError("sandbox.invalid-image", "sandbox({ image }) must be a non-empty OCI image reference");
|
|
45
|
-
result.image = options.image;
|
|
46
|
-
}
|
|
47
|
-
if (options.network !== undefined) {
|
|
48
|
-
if (!isRecord(options.network))
|
|
49
|
-
throw new SandboxError("sandbox.invalid-network", "sandbox({ network }) must be an object");
|
|
50
|
-
for (const key of Object.keys(options.network))
|
|
51
|
-
if (key !== "preset" && key !== "allow")
|
|
52
|
-
throw new SandboxError("sandbox.invalid-network", `Unknown network option '${key}'. Use preset and allow.`);
|
|
53
|
-
const { preset, allow } = options.network;
|
|
54
|
-
if (preset !== undefined && !SANDBOX_NETWORK_PRESETS.includes(preset))
|
|
55
|
-
throw new SandboxError("sandbox.invalid-network", `network.preset must be one of ${SANDBOX_NETWORK_PRESETS.join(", ")}`);
|
|
56
|
-
if (allow !== undefined) {
|
|
57
|
-
if (!Array.isArray(allow))
|
|
58
|
-
throw new SandboxError("sandbox.invalid-network", "network.allow must be an array of host names");
|
|
59
|
-
for (const host of allow)
|
|
60
|
-
if (typeof host !== "string" || !isSandboxHostPattern(host))
|
|
61
|
-
throw new SandboxError("sandbox.invalid-network", `network.allow entry '${String(host)}' must be a host name such as api.github.com or *.example.com (no scheme, port or path)`);
|
|
62
|
-
}
|
|
63
|
-
result.network = Object.freeze({
|
|
64
|
-
...(preset === undefined ? {} : { preset: preset }),
|
|
65
|
-
...(allow === undefined ? {} : { allow: Object.freeze([...allow]) }),
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
if (options.resources !== undefined) {
|
|
69
|
-
if (!isRecord(options.resources))
|
|
70
|
-
throw new SandboxError("sandbox.invalid-resources", "sandbox({ resources }) must be an object");
|
|
71
|
-
for (const key of Object.keys(options.resources))
|
|
72
|
-
if (key !== "cpus" && key !== "memory")
|
|
73
|
-
throw new SandboxError("sandbox.invalid-resources", `resources.${key} is not supported in this version. Use cpus and memory.`);
|
|
74
|
-
const { cpus, memory } = options.resources;
|
|
75
|
-
if (cpus !== undefined && (!Number.isInteger(cpus) || cpus < 1 || cpus > 64))
|
|
76
|
-
throw new SandboxError("sandbox.invalid-resources", "resources.cpus must be an integer from 1 to 64");
|
|
77
|
-
if (memory !== undefined && (typeof memory !== "string" || parseSandboxSize(memory) === undefined))
|
|
78
|
-
throw new SandboxError("sandbox.invalid-resources", "resources.memory must be a size such as 512MiB or 2GiB");
|
|
79
|
-
result.resources = Object.freeze({
|
|
80
|
-
...(cpus === undefined ? {} : { cpus }),
|
|
81
|
-
...(memory === undefined ? {} : { memory }),
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
if (options.idle !== undefined) {
|
|
85
|
-
if (typeof options.idle !== "string" || parseSandboxDuration(options.idle) === undefined)
|
|
86
|
-
throw new SandboxError("sandbox.invalid-idle", "sandbox({ idle }) must be a duration such as 30s, 15m or 1h");
|
|
87
|
-
result.idle = options.idle;
|
|
88
|
-
}
|
|
89
|
-
return Object.freeze(result);
|
|
90
|
-
}
|
|
91
|
-
function isRecord(value) {
|
|
92
|
-
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
93
|
-
}
|