@nylorun/core 0.7.0-beta → 0.8.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.
Files changed (38) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/dist/contracts.js +150 -3
  3. package/dist/define.d.ts +10 -2
  4. package/dist/define.js +5 -1
  5. package/dist/definition/assemble.js +7 -0
  6. package/dist/definition/builder.d.ts +122 -16
  7. package/dist/definition/builder.js +354 -53
  8. package/dist/definition/capability.d.ts +55 -0
  9. package/dist/definition/capability.js +104 -0
  10. package/dist/definition/delegate.d.ts +25 -4
  11. package/dist/definition/delegate.js +34 -6
  12. package/dist/definition/flow/compile.d.ts +25 -0
  13. package/dist/definition/flow/compile.js +302 -0
  14. package/dist/definition/flow/from.d.ts +11 -0
  15. package/dist/definition/flow/from.js +78 -0
  16. package/dist/definition/flow/index.d.ts +11 -0
  17. package/dist/definition/flow/index.js +11 -0
  18. package/dist/definition/flow/paths.d.ts +60 -0
  19. package/dist/definition/flow/paths.js +90 -0
  20. package/dist/definition/flow/spec.d.ts +66 -0
  21. package/dist/definition/flow/spec.js +130 -0
  22. package/dist/definition/flow/types.d.ts +159 -0
  23. package/dist/definition/flow/types.js +1 -0
  24. package/dist/definition/from.js +5 -1
  25. package/dist/definition/helpers.d.ts +1 -16
  26. package/dist/definition/helpers.js +1 -30
  27. package/dist/definition/mcp.d.ts +37 -0
  28. package/dist/definition/mcp.js +81 -0
  29. package/dist/definition/sandbox.d.ts +29 -0
  30. package/dist/definition/sandbox.js +93 -0
  31. package/dist/definition/workflow/loop.js +2 -2
  32. package/dist/definition/workflow/runnable.d.ts +13 -1
  33. package/dist/definition/workflow/runnable.js +11 -7
  34. package/dist/types/manifest.d.ts +4 -2
  35. package/dist/types/workflow.d.ts +72 -3
  36. package/dist/utils/deprecate.d.ts +3 -0
  37. package/dist/utils/deprecate.js +25 -0
  38. package/package.json +1 -1
@@ -0,0 +1,29 @@
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;
@@ -0,0 +1,93 @@
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
+ }
@@ -67,10 +67,10 @@ export function Loop(options) {
67
67
  id: agentEntry.manifest.id,
68
68
  getBinding: () => agentEntry,
69
69
  });
70
- // Verifier agent sessions are derived separately; local fn keys (slot input) sit under verify.
70
+ // Keys follow the harness path for the verifier, `<loop>/<part>` (workflows.md §6).
71
71
  mergeChild(acc, {
72
72
  agents: verifyChild.agents,
73
- nodes: remapNodeKeys(verifyChild.nodes, verifyChild.id, `${id}/verify`),
73
+ nodes: remapNodeKeys(verifyChild.nodes, verifyChild.id, `${id}/${verifyChild.id}`),
74
74
  sandboxSpecs: verifyChild.sandboxSpecs,
75
75
  });
76
76
  const node = verifyChild.node;
@@ -1,7 +1,7 @@
1
1
  import type { AgentTool, BuiltAgent } from "../../types/agent.js";
2
2
  import type { JsonObject } from "../../types/shared.js";
3
3
  import type { ToolDefinition } from "../../types/tool.js";
4
- import type { WorkflowNode } from "../../types/workflow.js";
4
+ import type { WorkflowNode, WorkflowToolNode } from "../../types/workflow.js";
5
5
  import type { AgentBinding } from "../binding.js";
6
6
  import type { BoundToolDefinition } from "../bound.js";
7
7
  import { type BuiltWorkflow } from "./types.js";
@@ -20,8 +20,20 @@ export type ResolvedChild = {
20
20
  readonly sandboxSpecs: readonly JsonObject[];
21
21
  readonly isReshapingSlot: boolean;
22
22
  };
23
+ export declare function isToolDefinition(value: unknown): value is ToolDefinition;
24
+ export declare function isAgentLike(value: unknown): value is BuiltAgent | AgentTool;
25
+ export declare function builtAgentOf(run: BuiltAgent | AgentTool | {
26
+ build(): BuiltAgent;
27
+ }): BuiltAgent;
23
28
  /** Collect sandbox specs declared on an agent (and its delegates, one level). */
24
29
  export declare function sandboxSpecsOf(agent: BuiltAgent | AgentTool): JsonObject[];
30
+ /**
31
+ * Project a tool for use as a workflow node.
32
+ * Unlike agent tools, workflow tool nodes may take any JSON Schema root type (workflows.md §9).
33
+ * Built without `bindTool` so we skip the agent-only object-input rule in `normalizeSchema`.
34
+ */
35
+ export declare function bindToolNode(tool: ToolDefinition): BoundToolDefinition;
36
+ export declare function toolManifestNode(bound: BoundToolDefinition): WorkflowToolNode;
25
37
  /**
26
38
  * Resolve a runnable or slot into a manifest node plus local bindings.
27
39
  * When the slot has `input`, the node is wrapped as `{ slot: { id?, input, run } }`.
@@ -4,7 +4,7 @@ import { HarnessError } from "../../errors.js";
4
4
  import { isBuiltWorkflow } from "./types.js";
5
5
  import { failOne } from "./diagnostics.js";
6
6
  import { isSlot } from "./slot.js";
7
- function isToolDefinition(value) {
7
+ export function isToolDefinition(value) {
8
8
  if (!value || typeof value !== "object")
9
9
  return false;
10
10
  if (!("name" in value) || typeof value.name !== "string")
@@ -15,7 +15,7 @@ function isToolDefinition(value) {
15
15
  v.inputSchema !== undefined ||
16
16
  v.input !== undefined);
17
17
  }
18
- function isAgentLike(value) {
18
+ export function isAgentLike(value) {
19
19
  if (!value || typeof value !== "object")
20
20
  return false;
21
21
  if (isBuiltWorkflow(value))
@@ -25,7 +25,7 @@ function isAgentLike(value) {
25
25
  "manifest" in value &&
26
26
  typeof value.getBinding === "function");
27
27
  }
28
- function builtAgentOf(run) {
28
+ export function builtAgentOf(run) {
29
29
  if (run &&
30
30
  typeof run === "object" &&
31
31
  "getBinding" in run &&
@@ -61,7 +61,7 @@ export function sandboxSpecsOf(agent) {
61
61
  * Unlike agent tools, workflow tool nodes may take any JSON Schema root type (workflows.md §9).
62
62
  * Built without `bindTool` so we skip the agent-only object-input rule in `normalizeSchema`.
63
63
  */
64
- function bindToolNode(tool) {
64
+ export function bindToolNode(tool) {
65
65
  const normalized = normalizeToolDefinition(tool);
66
66
  if (!normalized.name)
67
67
  throw new HarnessError("tool.invalid-name", "Tool name must not be empty");
@@ -97,7 +97,7 @@ function schemaForNode(source, role) {
97
97
  // Pass role "output" so normalizeSchema does not require an object root.
98
98
  return normalizeSchema(source, role === "input" ? "output" : role);
99
99
  }
100
- function toolManifestNode(bound) {
100
+ export function toolManifestNode(bound) {
101
101
  return {
102
102
  tool: {
103
103
  name: bound.name,
@@ -120,12 +120,16 @@ function agentNode(agent) {
120
120
  function resolveRunnable(run, rename) {
121
121
  if (isBuiltWorkflow(run)) {
122
122
  const binding = run.getBinding();
123
+ if (binding.manifest.workflowSchemaVersion !== 1)
124
+ failOne("workflow.flow-agent-child", `Flow agent '${run.id}' can't be a child of Chain, Switch, Parallel, Map or Loop. Use it as a step of another flow agent: Agent({ id }).step(${run.id}).`);
125
+ const manifest = binding.manifest;
123
126
  const id = rename ?? run.id;
124
127
  return {
125
128
  id,
126
- node: binding.manifest.root,
129
+ node: manifest.root,
127
130
  agents: { ...binding.agents },
128
- nodes: { ...binding.nodes },
131
+ // A rename moves the whole subtree: the harness path uses the new part, not run.id.
132
+ nodes: remapNodeKeys({ ...binding.nodes }, run.id, id),
129
133
  sandboxSpecs: sandboxSpecsFromAgents(binding.agents),
130
134
  isReshapingSlot: false,
131
135
  };
@@ -1,5 +1,6 @@
1
1
  import type { JsonObject } from "./shared.js";
2
2
  import type { HookManifest } from "./dynamics.js";
3
+ import type { WorkflowManifestV2 } from "./workflow.js";
3
4
  /** Published manifest schema version (no top-level model — Runtime-owned). */
4
5
  export type ManifestSchemaVersion = 4;
5
6
  export interface ToolManifest {
@@ -9,9 +10,10 @@ export interface ToolManifest {
9
10
  readonly outputSchema?: JsonObject;
10
11
  /**
11
12
  * Present when this tool is another agent. The engine runs it with a fresh context and
12
- * returns its final output; its input is always `{ task: string }`. One level deep.
13
+ * returns its final output; its input is always `{ task: string }`. One level deep. A flow
14
+ * agent (workflow manifest v2) runs in its own linked session on the Runtime.
13
15
  */
14
- readonly agent?: AgentManifest;
16
+ readonly agent?: AgentManifest | WorkflowManifestV2;
15
17
  }
16
18
  export interface SkillManifest {
17
19
  readonly name: string;
@@ -1,7 +1,7 @@
1
1
  import type { AgentBinding } from "../definition/binding.js";
2
2
  import type { BoundToolDefinition } from "../definition/bound.js";
3
3
  import type { JsonObject, JsonValue } from "./shared.js";
4
- import type { SandboxManifest } from "./manifest.js";
4
+ import type { AgentManifest, SandboxManifest } from "./manifest.js";
5
5
  /** Loop verify outcome: pass, or fail with required feedback. */
6
6
  export type Verdict = {
7
7
  readonly pass: true;
@@ -72,14 +72,83 @@ export interface WorkflowSlotNode {
72
72
  };
73
73
  }
74
74
  export type WorkflowNode = WorkflowAgentNode | WorkflowToolNode | WorkflowChainNode | WorkflowSwitchNode | WorkflowParallelNode | WorkflowMapNode | WorkflowLoopNode | WorkflowSlotNode;
75
- /** Wire document for a workflow definition. Missing `kind` is never a workflow. */
76
- export interface WorkflowManifest {
75
+ /** Workflow manifest v1: control nodes carry ids, and agents are resolved from the registry. */
76
+ export interface WorkflowManifestV1 {
77
77
  readonly kind: "workflow";
78
78
  readonly workflowSchemaVersion: 1;
79
79
  readonly id: string;
80
80
  readonly root: WorkflowNode;
81
81
  readonly sandbox?: SandboxManifest;
82
82
  }
83
+ /**
84
+ * Fields every v2 node may carry. `id` names the stage: for an agent or tool it
85
+ * replaces the leaf's path part, for a control node it is the stage key and the
86
+ * `results` key. `input` marks a function that reshapes what the node receives.
87
+ */
88
+ export interface WorkflowNodeOptionsV2 {
89
+ readonly id?: string;
90
+ readonly input?: WorkflowFnRef;
91
+ }
92
+ /** Agent leaf, or a nested flow agent: a key of the enclosing manifest's `agents`. */
93
+ export interface WorkflowAgentNodeV2 extends WorkflowNodeOptionsV2 {
94
+ readonly agent: string;
95
+ }
96
+ export interface WorkflowToolNodeV2 extends WorkflowNodeOptionsV2 {
97
+ readonly tool: WorkflowToolNode["tool"];
98
+ }
99
+ export interface WorkflowChainNodeV2 extends WorkflowNodeOptionsV2 {
100
+ readonly chain: readonly WorkflowNodeV2[];
101
+ }
102
+ export interface WorkflowSwitchNodeV2 extends WorkflowNodeOptionsV2 {
103
+ readonly switch: {
104
+ readonly on: WorkflowFnRef;
105
+ readonly cases: Readonly<Record<string, WorkflowNodeV2>>;
106
+ readonly default?: WorkflowNodeV2;
107
+ };
108
+ }
109
+ export interface WorkflowParallelNodeV2 extends WorkflowNodeOptionsV2 {
110
+ readonly parallel: Readonly<Record<string, WorkflowNodeV2>>;
111
+ }
112
+ /** Runs `each` once per item of its input, which must be an array. */
113
+ export interface WorkflowMapNodeV2 extends WorkflowNodeOptionsV2 {
114
+ readonly map: {
115
+ readonly each: WorkflowNodeV2;
116
+ };
117
+ }
118
+ /**
119
+ * Runs `run`, judges it with `verify`, then retries or stops. Without `decide`, a pass
120
+ * returns the output and a fail retries with the feedback, up to `max` attempts.
121
+ */
122
+ export interface WorkflowLoopNodeV2 extends WorkflowNodeOptionsV2 {
123
+ readonly loop: {
124
+ readonly run: WorkflowNodeV2;
125
+ readonly verify: WorkflowFnRef | WorkflowAgentNodeV2;
126
+ readonly max?: number;
127
+ readonly decide?: WorkflowFnRef;
128
+ };
129
+ }
130
+ export type WorkflowNodeV2 = WorkflowAgentNodeV2 | WorkflowToolNodeV2 | WorkflowChainNodeV2 | WorkflowSwitchNodeV2 | WorkflowParallelNodeV2 | WorkflowMapNodeV2 | WorkflowLoopNodeV2;
131
+ /**
132
+ * Workflow manifest v2: a flow agent. Its leaf agents are embedded in `agents`, so one
133
+ * manifest hash covers the whole agent, and session paths come from the leaves.
134
+ */
135
+ export interface WorkflowManifestV2 {
136
+ readonly kind: "workflow";
137
+ readonly workflowSchemaVersion: 2;
138
+ readonly id: string;
139
+ readonly name?: string;
140
+ readonly description?: string;
141
+ readonly metadata?: JsonObject;
142
+ readonly inputSchema?: JsonObject;
143
+ readonly outputSchema?: JsonObject;
144
+ /** The sandbox every leaf that declares one shares. */
145
+ readonly sandbox?: SandboxManifest;
146
+ readonly root: WorkflowNodeV2;
147
+ /** Every agent the flow runs, by id: ReAct agents (v4) and nested flow agents (v2). */
148
+ readonly agents: Readonly<Record<string, AgentManifest | WorkflowManifestV2>>;
149
+ }
150
+ /** Wire document for a workflow definition. Missing `kind` is never a workflow. */
151
+ export type WorkflowManifest = WorkflowManifestV1 | WorkflowManifestV2;
83
152
  /**
84
153
  * Local implementation for a node key: a tool node, a pure `fn`, or a Loop `verify` function.
85
154
  * Functions are never serialized.
@@ -0,0 +1,3 @@
1
+ export declare function deprecate(code: string, message: string): void;
2
+ /** Test hook: forget which codes have warned. */
3
+ export declare function resetDeprecationWarnings(): void;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Warn once per code that an authoring form is deprecated.
3
+ *
4
+ * Uses `process.emitWarning` when a Node-like `process` exists, so `--no-deprecation`
5
+ * and `process.noDeprecation` silence it; otherwise falls back to `console.warn`.
6
+ * Core imports no `node:` module, so this reads `process` from `globalThis`.
7
+ */
8
+ const warned = new Set();
9
+ export function deprecate(code, message) {
10
+ if (warned.has(code))
11
+ return;
12
+ warned.add(code);
13
+ const proc = globalThis.process;
14
+ if (proc?.noDeprecation)
15
+ return;
16
+ if (typeof proc?.emitWarning === "function") {
17
+ proc.emitWarning(message, { type: "DeprecationWarning", code });
18
+ return;
19
+ }
20
+ console.warn(`DeprecationWarning [${code}]: ${message}`);
21
+ }
22
+ /** Test hook: forget which codes have warned. */
23
+ export function resetDeprecationWarnings() {
24
+ warned.clear();
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nylorun/core",
3
- "version": "0.7.0-beta",
3
+ "version": "0.8.0-beta",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "description": "Shared agent definitions and wire contracts",