@nylorun/core 0.27.0-beta → 0.28.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 +47 -0
- package/README.md +3 -1
- package/dist/contracts.d.ts +153 -1
- package/dist/contracts.js +62 -7
- package/dist/define.d.ts +2 -1
- package/dist/define.js +2 -1
- package/dist/definition/cron.d.ts +15 -0
- package/dist/definition/cron.js +155 -0
- package/dist/definition/sections.d.ts +11 -0
- package/dist/definition/sections.js +29 -20
- package/dist/folder/compile.d.ts +43 -5
- package/dist/folder/compile.js +499 -88
- package/dist/folder/context.d.ts +97 -0
- package/dist/folder/context.js +79 -0
- package/dist/folder/decompile.d.ts +18 -0
- package/dist/folder/decompile.js +660 -0
- package/dist/folder/fix.d.ts +22 -0
- package/dist/folder/fix.js +191 -0
- package/dist/folder/flow.d.ts +42 -0
- package/dist/folder/flow.js +215 -0
- package/dist/folder/frontmatter.d.ts +5 -2
- package/dist/folder/frontmatter.js +8 -4
- package/dist/folder/index.d.ts +7 -2
- package/dist/folder/index.js +6 -1
- package/dist/folder/json-schema.d.ts +15 -0
- package/dist/folder/json-schema.js +212 -0
- package/dist/folder/nested.d.ts +24 -0
- package/dist/folder/nested.js +378 -0
- package/dist/folder/plugins.d.ts +23 -0
- package/dist/folder/plugins.js +155 -0
- package/dist/folder/schemas.d.ts +8 -0
- package/dist/folder/schemas.js +238 -0
- package/dist/folder/suggest.d.ts +13 -0
- package/dist/folder/suggest.js +63 -0
- package/dist/schemas/agent-folder/agent-md.schema.json +143 -0
- package/dist/schemas/agent-folder/flow.schema.json +466 -0
- package/dist/schemas/agent-folder/mcp.schema.json +59 -0
- package/dist/schemas/agent-folder/runtime.schema.json +447 -0
- package/dist/schemas/agent-folder/tool.schema.json +77 -0
- package/dist/schemas/agent-folder/trigger-md.schema.json +65 -0
- package/dist/utils/runtime-limits.d.ts +8 -0
- package/dist/utils/runtime-limits.js +19 -0
- package/package.json +8 -2
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The JSON Schemas of an agent folder (`inputSchema`, `outputSchema` and the schema files they
|
|
3
|
+
* name), checked as JSON Schema 2020-12 before the folder compiles. Two passes:
|
|
4
|
+
*
|
|
5
|
+
* 1. A check of the keywords' shapes, with the pointer of each problem in the schema, so the
|
|
6
|
+
* compiler names its file, line and column: a `type` that is no JSON Schema type, `properties`
|
|
7
|
+
* that is not a map, `required` that is not a list of names, and so on, in every subschema.
|
|
8
|
+
* 2. The engine's own reading (`z.fromJSONSchema`, as `schemaFromJSON` reads a tool's schemas in
|
|
9
|
+
* each turn), when the first pass finds no error: a schema the engine cannot use would fail
|
|
10
|
+
* every turn, so it fails the check instead.
|
|
11
|
+
*
|
|
12
|
+
* Keywords JSON Schema allows and the engine cannot use (`if`, `not`, `dependentRequired`,
|
|
13
|
+
* `unevaluatedProperties`, a `$ref` outside `$defs`) are errors with their own reasons. An
|
|
14
|
+
* unknown keyword is an annotation in JSON Schema, so it is only a warning, and only when it is
|
|
15
|
+
* close to a keyword (`proprties`).
|
|
16
|
+
*/
|
|
17
|
+
import { z } from "zod";
|
|
18
|
+
import { suggestion } from "./suggest.js";
|
|
19
|
+
/** JSON Schema's seven types. */
|
|
20
|
+
export const JSON_SCHEMA_TYPES = ["null", "boolean", "object", "array", "number", "string", "integer"];
|
|
21
|
+
const TYPES_TEXT = "null, boolean, object, array, number, string and integer";
|
|
22
|
+
/** Keywords whose value is one schema. */
|
|
23
|
+
const ONE_SCHEMA = ["additionalProperties", "items", "contains", "propertyNames", "not", "if", "then", "else", "unevaluatedProperties", "unevaluatedItems", "contentSchema", "additionalItems"];
|
|
24
|
+
/** Keywords whose value maps names to schemas. */
|
|
25
|
+
const SCHEMA_MAPS = ["properties", "patternProperties", "$defs", "definitions", "dependentSchemas"];
|
|
26
|
+
/** Keywords whose value is a non-empty list of schemas. */
|
|
27
|
+
const SCHEMA_LISTS = ["allOf", "anyOf", "oneOf", "prefixItems"];
|
|
28
|
+
const COUNTS = ["minLength", "maxLength", "minItems", "maxItems", "minProperties", "maxProperties", "minContains", "maxContains"];
|
|
29
|
+
const NUMBERS = ["minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum", "multipleOf"];
|
|
30
|
+
const STRINGS = ["title", "description", "format", "pattern", "$comment", "$id", "$schema", "$anchor", "$dynamicAnchor", "$ref", "$dynamicRef", "contentEncoding", "contentMediaType"];
|
|
31
|
+
const BOOLEANS = ["uniqueItems", "readOnly", "writeOnly", "deprecated"];
|
|
32
|
+
const OTHERS = ["type", "required", "enum", "const", "default", "examples", "dependentRequired", "$vocabulary"];
|
|
33
|
+
const KEYWORDS = [...ONE_SCHEMA, ...SCHEMA_MAPS, ...SCHEMA_LISTS, ...COUNTS, ...NUMBERS, ...STRINGS, ...BOOLEANS, ...OTHERS];
|
|
34
|
+
/** Why the engine cannot use a keyword JSON Schema allows. */
|
|
35
|
+
const UNSUPPORTED = {
|
|
36
|
+
if: "conditional schemas (if, then, else) are not supported: write the cases as anyOf",
|
|
37
|
+
then: "conditional schemas (if, then, else) are not supported: write the cases as anyOf",
|
|
38
|
+
else: "conditional schemas (if, then, else) are not supported: write the cases as anyOf",
|
|
39
|
+
dependentSchemas: "dependentSchemas is not supported",
|
|
40
|
+
dependentRequired: "dependentRequired is not supported",
|
|
41
|
+
unevaluatedProperties: "unevaluatedProperties is not supported: use additionalProperties",
|
|
42
|
+
unevaluatedItems: "unevaluatedItems is not supported: use items",
|
|
43
|
+
};
|
|
44
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
45
|
+
const show = (value) => JSON.stringify(value) ?? String(value);
|
|
46
|
+
/** Every problem in `schema`, a JSON Schema at the root of a field or file. */
|
|
47
|
+
export function jsonSchemaIssues(schema) {
|
|
48
|
+
const issues = [];
|
|
49
|
+
const error = (path, message, key = false) => issues.push({ severity: "error", path, message, ...(key ? { key } : {}) });
|
|
50
|
+
const warn = (path, message, key = false) => issues.push({ severity: "warning", path, message, ...(key ? { key } : {}) });
|
|
51
|
+
const defs = isObject(schema) && isObject(schema.$defs) ? schema.$defs : {};
|
|
52
|
+
const visit = (node, path) => {
|
|
53
|
+
if (typeof node === "boolean")
|
|
54
|
+
return;
|
|
55
|
+
if (!isObject(node)) {
|
|
56
|
+
error(path, `A schema is an object (or true or false), not ${show(node)}`);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
for (const [keyword, value] of Object.entries(node)) {
|
|
60
|
+
const at = [...path, keyword];
|
|
61
|
+
if (keyword in UNSUPPORTED) {
|
|
62
|
+
error(at, `${keyword} is JSON Schema, but the engine cannot use it: ${UNSUPPORTED[keyword]}`, true);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (keyword === "type") {
|
|
66
|
+
typeIssues(value, at);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (keyword === "not") {
|
|
70
|
+
if (!isObject(value) || Object.keys(value).length > 0)
|
|
71
|
+
error(at, "not is JSON Schema, but the engine cannot use it, except as { \"not\": {} } for a value that never matches", true);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (ONE_SCHEMA.includes(keyword)) {
|
|
75
|
+
if (keyword === "items" && Array.isArray(value)) {
|
|
76
|
+
warn(at, "items is one schema in JSON Schema 2020-12; a list of schemas is draft-07's tuple form. Fix: rename it prefixItems", true);
|
|
77
|
+
value.forEach((item, index) => visit(item, [...at, index]));
|
|
78
|
+
}
|
|
79
|
+
else
|
|
80
|
+
visit(value, at);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (SCHEMA_MAPS.includes(keyword)) {
|
|
84
|
+
if (!isObject(value)) {
|
|
85
|
+
error(at, `${keyword} is a map of ${keyword === "properties" ? "property names" : keyword === "patternProperties" ? "patterns" : "names"} to schemas, not ${show(value)}`);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
for (const [name, child] of Object.entries(value)) {
|
|
89
|
+
if (keyword === "patternProperties" && !isPattern(name))
|
|
90
|
+
error([...at, name], `${show(name)} is not a regular expression`, true);
|
|
91
|
+
visit(child, [...at, name]);
|
|
92
|
+
}
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (SCHEMA_LISTS.includes(keyword)) {
|
|
96
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
97
|
+
error(at, `${keyword} is a list of one or more schemas, not ${show(value)}`);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
value.forEach((item, index) => visit(item, [...at, index]));
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (COUNTS.includes(keyword)) {
|
|
104
|
+
if (!Number.isInteger(value) || value < 0)
|
|
105
|
+
error(at, `${keyword} is a whole number, 0 or more, not ${show(value)}`);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (NUMBERS.includes(keyword)) {
|
|
109
|
+
if (typeof value !== "number")
|
|
110
|
+
error(at, `${keyword} is a number, not ${show(value)}`);
|
|
111
|
+
else if (keyword === "multipleOf" && value <= 0)
|
|
112
|
+
error(at, `multipleOf is a number above 0, not ${show(value)}`);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (STRINGS.includes(keyword)) {
|
|
116
|
+
if (typeof value !== "string")
|
|
117
|
+
error(at, `${keyword} is a string, not ${show(value)}`);
|
|
118
|
+
else if (keyword === "pattern" && !isPattern(value))
|
|
119
|
+
error(at, `pattern ${show(value)} is not a regular expression`);
|
|
120
|
+
else if (keyword === "$ref")
|
|
121
|
+
refIssue(value, at);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (BOOLEANS.includes(keyword)) {
|
|
125
|
+
if (typeof value !== "boolean")
|
|
126
|
+
error(at, `${keyword} is true or false, not ${show(value)}`);
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (keyword === "required") {
|
|
130
|
+
if (!Array.isArray(value)) {
|
|
131
|
+
error(at, `required is a list of property names, such as ["id"], not ${show(value)}`);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const seen = new Set();
|
|
135
|
+
value.forEach((name, index) => {
|
|
136
|
+
if (typeof name !== "string")
|
|
137
|
+
error([...at, index], `required lists property names, which are strings, not ${show(name)}`);
|
|
138
|
+
else if (seen.has(name))
|
|
139
|
+
error([...at, index], `required lists ${show(name)} twice`);
|
|
140
|
+
else
|
|
141
|
+
seen.add(name);
|
|
142
|
+
});
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (keyword === "enum" || keyword === "examples") {
|
|
146
|
+
if (!Array.isArray(value))
|
|
147
|
+
error(at, `${keyword} is a list of values, not ${show(value)}`);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (keyword === "dependentRequired" || keyword === "$vocabulary" || keyword === "const" || keyword === "default")
|
|
151
|
+
continue;
|
|
152
|
+
const close = suggestion(keyword, KEYWORDS);
|
|
153
|
+
if (close)
|
|
154
|
+
issues.push({ severity: "warning", path: at, message: `Unknown keyword '${keyword}'. Did you mean '${close}'? JSON Schema ignores a keyword it does not know`, key: true, known: KEYWORDS });
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
const typeIssues = (value, at) => {
|
|
158
|
+
const one = (name, path) => {
|
|
159
|
+
if (typeof name === "string" && JSON_SCHEMA_TYPES.includes(name))
|
|
160
|
+
return;
|
|
161
|
+
const close = typeof name === "string" ? suggestion(name, JSON_SCHEMA_TYPES) : undefined;
|
|
162
|
+
error(path, `type ${show(name)} is not a JSON Schema type.${close ? ` Did you mean '${close}'?` : ""} The types are ${TYPES_TEXT}`);
|
|
163
|
+
};
|
|
164
|
+
if (!Array.isArray(value))
|
|
165
|
+
return one(value, at);
|
|
166
|
+
if (value.length === 0)
|
|
167
|
+
return error(at, `type is a type or a list of one or more types: ${TYPES_TEXT}`);
|
|
168
|
+
const seen = new Set();
|
|
169
|
+
value.forEach((name, index) => {
|
|
170
|
+
if (seen.has(name))
|
|
171
|
+
error([...at, index], `type lists ${show(name)} twice`);
|
|
172
|
+
seen.add(name);
|
|
173
|
+
one(name, [...at, index]);
|
|
174
|
+
});
|
|
175
|
+
};
|
|
176
|
+
const refIssue = (ref, at) => {
|
|
177
|
+
if (ref === "#")
|
|
178
|
+
return;
|
|
179
|
+
const local = /^#\/\$defs\/([^/]+)$/.exec(ref);
|
|
180
|
+
if (local) {
|
|
181
|
+
const name = local[1].replace(/~1/g, "/").replace(/~0/g, "~");
|
|
182
|
+
if (!(name in defs)) {
|
|
183
|
+
const close = suggestion(name, Object.keys(defs));
|
|
184
|
+
error(at, `$ref ${show(ref)} names no schema in this schema's $defs.${close ? ` Did you mean '#/$defs/${close}'?` : ""}`);
|
|
185
|
+
}
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
error(at, ref.startsWith("#")
|
|
189
|
+
? `$ref ${show(ref)}: the engine resolves a $ref to the schema itself (#) or to one of its $defs (#/$defs/<name>) only`
|
|
190
|
+
: `$ref ${show(ref)} names another document: the engine resolves a $ref inside this schema only (#/$defs/<name>)`);
|
|
191
|
+
};
|
|
192
|
+
visit(schema, []);
|
|
193
|
+
if (issues.some((issue) => issue.severity === "error"))
|
|
194
|
+
return issues;
|
|
195
|
+
try {
|
|
196
|
+
z.fromJSONSchema(schema);
|
|
197
|
+
}
|
|
198
|
+
catch (cause) {
|
|
199
|
+
error([], `The engine cannot use this schema: ${cause instanceof Error ? cause.message.split("\n")[0] : String(cause)}`);
|
|
200
|
+
}
|
|
201
|
+
return issues;
|
|
202
|
+
}
|
|
203
|
+
/** Whether `pattern` is an ECMA-262 regular expression, the dialect JSON Schema names. */
|
|
204
|
+
function isPattern(pattern) {
|
|
205
|
+
try {
|
|
206
|
+
new RegExp(pattern);
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AgentFolderEntry } from "./compile.js";
|
|
2
|
+
import { type FolderContext, type FolderScope } from "./context.js";
|
|
3
|
+
export type { FolderScope } from "./context.js";
|
|
4
|
+
/** The files of every folder under `agents/` and `plugins/` the compiler reads. */
|
|
5
|
+
export declare function nestedTextPaths(entries: readonly AgentFolderEntry[], folderTextPaths: (entries: readonly AgentFolderEntry[]) => string[]): string[];
|
|
6
|
+
/** `/x` from a folder inside another: the uploaded folder's file, or why not. */
|
|
7
|
+
export declare function scopedSchema(scope: FolderScope, value: string, fail: (code: string, message: string) => void): Record<string, unknown> | undefined;
|
|
8
|
+
/** The hooks `compileAgentFolder` calls for one folder: plugins, subagents and flows. */
|
|
9
|
+
export declare function folderParts(ctx: FolderContext): {
|
|
10
|
+
/** Adds the plugins' MCP servers, before `AGENT.md` says how the agent uses them. */
|
|
11
|
+
mergeServers(servers: Record<string, Record<string, unknown>>): void;
|
|
12
|
+
/**
|
|
13
|
+
* `credential` in `AGENT.md`'s block for a plugin's server, which cannot know the agent's slot
|
|
14
|
+
* names. True when the server is a plugin's, so the block may name it.
|
|
15
|
+
*/
|
|
16
|
+
serverCredential(servers: Record<string, Record<string, unknown>>, key: string, settings: Record<string, unknown>, at: readonly PropertyKey[]): boolean;
|
|
17
|
+
/** Adds the plugins' skills, before `AGENT.md` says which secrets each gets. */
|
|
18
|
+
mergeSkills(skills: Record<string, Record<string, unknown>>): void;
|
|
19
|
+
/**
|
|
20
|
+
* Subagents, or the flow: called once the folder's own parts are compiled. False when a part
|
|
21
|
+
* it needs did not compile and its errors are reported elsewhere (a stage of the flow root).
|
|
22
|
+
*/
|
|
23
|
+
compose(): boolean;
|
|
24
|
+
};
|
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The folders inside an agent folder (track T7, D80; design §3.3, §3.8), compiled by the same
|
|
3
|
+
* compiler, one level in:
|
|
4
|
+
*
|
|
5
|
+
* - **Subagents.** `agents/<id>/` of an agent (no `flow.json` beside it) is an agent folder
|
|
6
|
+
* without `runtime.json`, `triggers/` or `files/`, offered to the agent as a tool, exactly as
|
|
7
|
+
* the TypeScript builder offers an agent used as a tool: named `<id>`, with the agent's
|
|
8
|
+
* description, input `{ task }`, and the agent's manifest embedded. One level deep: a subagent
|
|
9
|
+
* has no `agents/` of its own, unless it is a flow (a flow offered as a tool).
|
|
10
|
+
* - **Flows.** `flow.json` makes a folder a flow (`harness.kind: "workflow"`, v3's node tree).
|
|
11
|
+
* A flow root has one flat `agents/`, and every `flow.json` under it names stages from there by
|
|
12
|
+
* id. A stage with its own `flow.json` is a sub-flow over the same agents, so it has no
|
|
13
|
+
* `agents/`. The compiler embeds each agent where v3 wants it. A flow's `AGENT.md` has no body
|
|
14
|
+
* and may name `inputSchema`; a flow has no `mcp.json`, `skills/` or plugins, and only the
|
|
15
|
+
* tools its nodes name. An agent of `agents/` that no `flow.json` names is a warning.
|
|
16
|
+
* - **Plugins.** `plugins/<name>/` (`plugins.ts`).
|
|
17
|
+
*
|
|
18
|
+
* A folder inside another reports its diagnostics with paths from the uploaded folder
|
|
19
|
+
* (`agents/research/flow.json:3:12`). `/x` in it names a file from the uploaded folder's root,
|
|
20
|
+
* and `./x` one from its own. A slot it needs is derived in the uploaded folder's runtime
|
|
21
|
+
* section.
|
|
22
|
+
*/
|
|
23
|
+
import { DELEGATE_INPUT_SCHEMA, DELEGATE_NAME_PATTERN } from "../definition/delegate.js";
|
|
24
|
+
import { loopManifestOf } from "../definition/sections.js";
|
|
25
|
+
import { TOOL_SECRET_NAME_PATTERN } from "../contracts.js";
|
|
26
|
+
import { addInner, cloneJson, foldersUnder, isObject, jsonPosition, } from "./context.js";
|
|
27
|
+
import { translateFlow } from "./flow.js";
|
|
28
|
+
import { foldPlugins, pluginTextPaths } from "./plugins.js";
|
|
29
|
+
const AGENT_FILE = "AGENT.md";
|
|
30
|
+
const FLOW_FILE = "flow.json";
|
|
31
|
+
/** The files of every folder under `agents/` and `plugins/` the compiler reads. */
|
|
32
|
+
export function nestedTextPaths(entries, folderTextPaths) {
|
|
33
|
+
const paths = [];
|
|
34
|
+
const all = entries.map((entry) => entry.path);
|
|
35
|
+
for (const id of foldersUnder("agents", all).keys()) {
|
|
36
|
+
const prefix = `agents/${id}/`;
|
|
37
|
+
const nested = entries.filter((entry) => entry.path.startsWith(prefix)).map((entry) => ({ ...entry, path: entry.path.slice(prefix.length) }));
|
|
38
|
+
paths.push(...folderTextPaths(nested).map((path) => prefix + path));
|
|
39
|
+
}
|
|
40
|
+
for (const [name, inner] of foldersUnder("plugins", all))
|
|
41
|
+
paths.push(...pluginTextPaths(inner).map((path) => `plugins/${name}/${path}`));
|
|
42
|
+
return paths;
|
|
43
|
+
}
|
|
44
|
+
/** `/x` from a folder inside another: the uploaded folder's file, or why not. */
|
|
45
|
+
export function scopedSchema(scope, value, fail) {
|
|
46
|
+
const found = scope.schema(value.slice(1));
|
|
47
|
+
if (found === undefined)
|
|
48
|
+
return undefined;
|
|
49
|
+
if ("message" in found) {
|
|
50
|
+
fail(found.code, found.message);
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
return found.schema;
|
|
54
|
+
}
|
|
55
|
+
/** The hooks `compileAgentFolder` calls for one folder: plugins, subagents and flows. */
|
|
56
|
+
export function folderParts(ctx) {
|
|
57
|
+
const isFlow = [...ctx.kinds.values()].includes("flow");
|
|
58
|
+
let plugins;
|
|
59
|
+
const folded = () => (plugins ??= isFlow ? { servers: new Map(), skills: new Map() } : foldPlugins(ctx));
|
|
60
|
+
return {
|
|
61
|
+
/** Adds the plugins' MCP servers, before `AGENT.md` says how the agent uses them. */
|
|
62
|
+
mergeServers(servers) {
|
|
63
|
+
for (const [key, { server, file }] of folded().servers) {
|
|
64
|
+
if (servers[key]) {
|
|
65
|
+
ctx.report("error", "plugin_duplicate", `MCP server ${key} is declared in mcp.json and by this plugin: an agent has one server of a name`, file, ["mcpServers", key], {
|
|
66
|
+
at: jsonPosition(ctx, file, ["mcpServers", key], true),
|
|
67
|
+
});
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
servers[key] = server;
|
|
71
|
+
ctx.origins.push([["harness", "mcpServers", key], { file, path: ["mcpServers", key] }]);
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
/**
|
|
75
|
+
* `credential` in `AGENT.md`'s block for a plugin's server, which cannot know the agent's slot
|
|
76
|
+
* names. True when the server is a plugin's, so the block may name it.
|
|
77
|
+
*/
|
|
78
|
+
serverCredential(servers, key, settings, at) {
|
|
79
|
+
const server = servers[key];
|
|
80
|
+
if (!server || server.plugin === undefined)
|
|
81
|
+
return false;
|
|
82
|
+
const credential = settings.credential;
|
|
83
|
+
if (credential === undefined)
|
|
84
|
+
return true;
|
|
85
|
+
const path = [...at, "credential"];
|
|
86
|
+
if (typeof credential !== "string" || !TOOL_SECRET_NAME_PATTERN.test(credential)) {
|
|
87
|
+
ctx.report("error", "credential_name", "credential names a secret slot: 1-64 letters, digits, '.', '_' and '-'", AGENT_FILE, path);
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
if (server.credential !== undefined) {
|
|
91
|
+
ctx.report("error", "credential_twice", `The plugin's mcp.json already names ${String(server.credential)} for ${key}: a server authenticates with one credential`, AGENT_FILE, path);
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
server.credential = credential;
|
|
95
|
+
ctx.deriveSlot(credential, "bearer", String(server.url), AGENT_FILE, path);
|
|
96
|
+
ctx.origins.push([["harness", "mcpServers", key, "credential"], { file: AGENT_FILE, path }]);
|
|
97
|
+
return true;
|
|
98
|
+
},
|
|
99
|
+
/** Adds the plugins' skills, before `AGENT.md` says which secrets each gets. */
|
|
100
|
+
mergeSkills(skills) {
|
|
101
|
+
for (const [key, { skill, file }] of folded().skills) {
|
|
102
|
+
if (skills[key]) {
|
|
103
|
+
ctx.report("error", "plugin_duplicate", `Skill ${key} is in skills/ and in this plugin: an agent has one skill of a name`, file);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
skills[key] = skill;
|
|
107
|
+
ctx.origins.push([["harness", "skills", key], { file, path: [] }]);
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
/**
|
|
111
|
+
* Subagents, or the flow: called once the folder's own parts are compiled. False when a part
|
|
112
|
+
* it needs did not compile and its errors are reported elsewhere (a stage of the flow root).
|
|
113
|
+
*/
|
|
114
|
+
compose() {
|
|
115
|
+
if (ctx.scope && ctx.scope.role !== "plugin")
|
|
116
|
+
nestedChecks(ctx);
|
|
117
|
+
if (isFlow)
|
|
118
|
+
return composeFlow(ctx);
|
|
119
|
+
composeAgent(ctx);
|
|
120
|
+
return true;
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/** The scope of a folder inside this one: its slots and `/x` schemas are the uploaded folder's. */
|
|
125
|
+
function innerScope(ctx, prefix, role, namespace) {
|
|
126
|
+
return {
|
|
127
|
+
role,
|
|
128
|
+
...(namespace === undefined ? {} : { namespace }),
|
|
129
|
+
deriveSlot: (credential, type, url, file, at, header) => ctx.deriveSlot(credential, type, url, prefix + file, [], header, at),
|
|
130
|
+
schema: ctx.scope?.schema ?? rootSchema(ctx),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/** `/x` read from the uploaded folder: a JSON Schema file anywhere in it. */
|
|
134
|
+
function rootSchema(ctx) {
|
|
135
|
+
const read = new Map();
|
|
136
|
+
return (target) => {
|
|
137
|
+
if (target.split("/").some((segment) => segment === "" || segment === "." || segment === ".."))
|
|
138
|
+
return { code: "path_outside", message: `/${target} leaves the folder: a path names a file inside it` };
|
|
139
|
+
if (!ctx.entries.has(target))
|
|
140
|
+
return { code: "schema_missing", message: `/${target} is not a file in the folder` };
|
|
141
|
+
const kind = ctx.kinds.get(target);
|
|
142
|
+
const segments = target.split("/");
|
|
143
|
+
const base = segments.at(-1);
|
|
144
|
+
const schemaFile = kind === "schema" || (kind === "nested" && (base.endsWith(".schema.json") || (segments.includes("schemas") && base.endsWith(".json"))));
|
|
145
|
+
if (!schemaFile)
|
|
146
|
+
return { code: "schema_invalid", message: `/${target} is not a JSON Schema file: name it *.schema.json or keep it under schemas/` };
|
|
147
|
+
if (!read.has(target))
|
|
148
|
+
read.set(target, ctx.readJson(target));
|
|
149
|
+
const schema = read.get(target);
|
|
150
|
+
return schema === undefined ? undefined : { schema };
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/** Compiles `prefix` (`agents/<id>/`) as a folder of its own, its diagnostics added to this one's. */
|
|
154
|
+
function compileInner(ctx, prefix, role, namespace) {
|
|
155
|
+
const entries = [];
|
|
156
|
+
for (const [path, entry] of ctx.entries)
|
|
157
|
+
if (path.startsWith(prefix))
|
|
158
|
+
entries.push({ ...entry, path: path.slice(prefix.length) });
|
|
159
|
+
const files = new Map();
|
|
160
|
+
for (const [path, bytes] of ctx.files)
|
|
161
|
+
if (path.startsWith(prefix))
|
|
162
|
+
files.set(path.slice(prefix.length), bytes);
|
|
163
|
+
const name = prefix.split("/").at(-2);
|
|
164
|
+
const result = ctx.compile({ entries, files, name }, innerScope(ctx, prefix, role, namespace));
|
|
165
|
+
addInner(ctx, prefix, result.diagnostics);
|
|
166
|
+
return result.manifest;
|
|
167
|
+
}
|
|
168
|
+
/** Why an id under `agents/` cannot name a tool or a stage, or undefined when it can. */
|
|
169
|
+
function badId(ctx, id) {
|
|
170
|
+
if (DELEGATE_NAME_PATTERN.test(id))
|
|
171
|
+
return false;
|
|
172
|
+
ctx.report("error", "agent_name", `agents/${id}/ names a tool or a stage, so its name takes only letters, digits, _ and -, up to 64 characters`, `agents/${id}/${AGENT_FILE}`);
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
/** The first file of `agents/<id>/`, for a diagnostic about the folder. */
|
|
176
|
+
const folderFile = (id, inner) => `agents/${id}/${inner.includes(AGENT_FILE) ? AGENT_FILE : inner[0]}`;
|
|
177
|
+
/** What only the uploaded folder has: a subagent or stage runs in its runtime and its harness. */
|
|
178
|
+
function nestedChecks(ctx) {
|
|
179
|
+
const what = ctx.scope.role === "subagent" ? "a subagent" : "a stage of a flow";
|
|
180
|
+
const refuse = (message, file, path) => ctx.report("error", "nested_refused", message, file, path, { key: path !== undefined && path.length > 0 });
|
|
181
|
+
for (const [path, kind] of ctx.kinds) {
|
|
182
|
+
if (kind === "runtime")
|
|
183
|
+
refuse(`runtime.json is the uploaded folder's: ${what} runs in its runtime, so move this to the root`, path);
|
|
184
|
+
else if (kind === "trigger")
|
|
185
|
+
refuse(`triggers/ is the uploaded folder's: a trigger starts the root agent, so move this to the root's triggers/`, path);
|
|
186
|
+
else if (kind === "file")
|
|
187
|
+
refuse(`files/ is the uploaded folder's: its runtime seeds the disk every agent shares, so move this to the root's files/`, path);
|
|
188
|
+
}
|
|
189
|
+
const keys = {
|
|
190
|
+
format: "format is read only in the uploaded folder's AGENT.md: one folder, one format",
|
|
191
|
+
harness: `${what} runs in the harness of the agent that holds it: set harness in the root's AGENT.md`,
|
|
192
|
+
memory: `memory is the root agent's: ${what} keeps none of its own`,
|
|
193
|
+
loop: `loop settings are the root agent's: ${what} has none of its own`,
|
|
194
|
+
};
|
|
195
|
+
for (const [key, message] of Object.entries(keys))
|
|
196
|
+
if (ctx.front[key] !== undefined)
|
|
197
|
+
refuse(message, AGENT_FILE, [key]);
|
|
198
|
+
// Agent manifest v7, which embeds an agent, has no place for a credential or a skill's secrets.
|
|
199
|
+
const settings = isObject(ctx.front.mcpServers) ? ctx.front.mcpServers : {};
|
|
200
|
+
for (const [key, server] of Object.entries(isObject(ctx.harness.mcpServers) ? ctx.harness.mcpServers : {})) {
|
|
201
|
+
if (!isObject(server) || server.credential === undefined)
|
|
202
|
+
continue;
|
|
203
|
+
const message = `${key} authenticates with ${String(server.credential)}, and ${what}'s MCP servers take no credential: only the root agent's servers do. Give the server to the root agent, or call it without one`;
|
|
204
|
+
if (isObject(settings[key]) && settings[key].credential !== undefined)
|
|
205
|
+
ctx.report("error", "credential_unsupported", message, AGENT_FILE, ["mcpServers", key, "credential"]);
|
|
206
|
+
else if (server.plugin !== undefined) {
|
|
207
|
+
const origin = ctx.origins.find(([path]) => path.length === 3 && path[1] === "mcpServers" && path[2] === key)?.[1];
|
|
208
|
+
ctx.report("error", "credential_unsupported", message, origin?.file, origin?.path, { at: origin && jsonPosition(ctx, origin.file, origin.path, true) });
|
|
209
|
+
}
|
|
210
|
+
else
|
|
211
|
+
ctx.report("error", "credential_unsupported", message, "mcp.json", ["mcpServers", key, "headers"]);
|
|
212
|
+
}
|
|
213
|
+
for (const [key, skill] of Object.entries(isObject(ctx.harness.skills) ? ctx.harness.skills : {}))
|
|
214
|
+
if (isObject(skill) && skill.secrets !== undefined)
|
|
215
|
+
ctx.report("error", "credential_unsupported", `Only the root agent's skills get secrets: ${what}'s skill ${key} gets none`, AGENT_FILE, ["skills", key, "secrets"]);
|
|
216
|
+
}
|
|
217
|
+
/** An agent: its `agents/` are tools, and its plugins are listed. */
|
|
218
|
+
function composeAgent(ctx) {
|
|
219
|
+
if (ctx.front.inputSchema !== undefined)
|
|
220
|
+
ctx.report("error", "input_schema_refused", "inputSchema is a flow's: an agent's input is the message, and a subagent's is { task }. A folder is a flow when it has a flow.json", AGENT_FILE, ["inputSchema"], { key: true });
|
|
221
|
+
const harness = ctx.harness;
|
|
222
|
+
const folders = foldersUnder("agents", ctx.entries.keys());
|
|
223
|
+
if (folders.size > 0 && ctx.scope?.role === "subagent")
|
|
224
|
+
for (const [id, inner] of folders)
|
|
225
|
+
ctx.report("error", "subagent_depth", `A subagent has no subagents: agents used as tools nest one level deep. Move agents/${id}/ to the agent that uses it`, folderFile(id, inner));
|
|
226
|
+
else if (folders.size > 0) {
|
|
227
|
+
const tools = Array.isArray(harness.tools) ? harness.tools : [];
|
|
228
|
+
const httpTools = new Set(tools.map((tool) => tool.name));
|
|
229
|
+
for (const [id, inner] of folders) {
|
|
230
|
+
if (badId(ctx, id))
|
|
231
|
+
continue;
|
|
232
|
+
if (httpTools.has(id)) {
|
|
233
|
+
ctx.report("error", "tool_twice", `agents/${id}/ and tools/${id}.json both name the tool ${id}: rename one`, folderFile(id, inner));
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
const manifest = compileInner(ctx, `agents/${id}/`, "subagent");
|
|
237
|
+
if (!manifest)
|
|
238
|
+
continue;
|
|
239
|
+
ctx.origins.push([["harness", "tools", tools.length], { file: `agents/${id}/${AGENT_FILE}`, path: [] }]);
|
|
240
|
+
// As the TypeScript builder emits an agent used as a tool (`delegateTool`).
|
|
241
|
+
tools.push({
|
|
242
|
+
name: id,
|
|
243
|
+
...(manifest.description === undefined ? {} : { description: manifest.description }),
|
|
244
|
+
inputSchema: cloneJson(DELEGATE_INPUT_SCHEMA),
|
|
245
|
+
agent: loopManifestOf(manifest),
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
if (tools.length > 0)
|
|
249
|
+
harness.tools = tools;
|
|
250
|
+
}
|
|
251
|
+
// The plugins its skills and servers came from, each once, by name (`pluginsOf`).
|
|
252
|
+
const labels = new Map();
|
|
253
|
+
for (const item of [...Object.values(isObject(harness.skills) ? harness.skills : {}), ...Object.values(isObject(harness.mcpServers) ? harness.mcpServers : {})]) {
|
|
254
|
+
const label = isObject(item) && isObject(item.plugin) ? item.plugin : undefined;
|
|
255
|
+
if (label && !labels.has(label.name))
|
|
256
|
+
labels.set(label.name, { ...label });
|
|
257
|
+
}
|
|
258
|
+
if (labels.size > 0)
|
|
259
|
+
harness.plugins = [...labels.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
260
|
+
}
|
|
261
|
+
/** A flow root's `agents/`: compiled once each, when a node first names it. */
|
|
262
|
+
class FlowNamespace {
|
|
263
|
+
ids;
|
|
264
|
+
used = new Set();
|
|
265
|
+
#members = new Map();
|
|
266
|
+
#ctx;
|
|
267
|
+
constructor(ctx, folders) {
|
|
268
|
+
this.#ctx = ctx;
|
|
269
|
+
this.ids = [...folders.keys()];
|
|
270
|
+
for (const id of this.ids)
|
|
271
|
+
if (badId(ctx, id))
|
|
272
|
+
this.#members.set(id, {});
|
|
273
|
+
}
|
|
274
|
+
resolve(id) {
|
|
275
|
+
if (!this.ids.includes(id))
|
|
276
|
+
return "unknown";
|
|
277
|
+
const member = this.#members.get(id);
|
|
278
|
+
if (member === "compiling")
|
|
279
|
+
return "cycle";
|
|
280
|
+
if (member)
|
|
281
|
+
return member.manifest ?? "failed";
|
|
282
|
+
this.#members.set(id, "compiling");
|
|
283
|
+
const manifest = compileInner(this.#ctx, `agents/${id}/`, "stage", this);
|
|
284
|
+
const embedded = manifest === undefined ? undefined : loopManifestOf(manifest);
|
|
285
|
+
this.#members.set(id, embedded === undefined ? {} : { manifest: embedded });
|
|
286
|
+
return embedded ?? "failed";
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
/** A flow: `flow.json`'s node tree over the flow root's agents and the tools beside it. */
|
|
290
|
+
function composeFlow(ctx) {
|
|
291
|
+
const { harness, front } = ctx;
|
|
292
|
+
const refuse = (message, file, path) => ctx.report("error", "flow_refused", message, file, path, { key: path !== undefined && path.length > 0 });
|
|
293
|
+
// What a flow has none of: each of its agents has its own.
|
|
294
|
+
if (ctx.agent) {
|
|
295
|
+
const lines = ctx.agent.body.split("\n");
|
|
296
|
+
const first = lines.findIndex((line) => line.trim() !== "");
|
|
297
|
+
if (first >= 0)
|
|
298
|
+
ctx.report("error", "flow_body", "A flow's AGENT.md has no body: a flow has no instructions, and each of its agents has its own", AGENT_FILE, undefined, {
|
|
299
|
+
at: { line: ctx.agent.bodyLine + first, column: 1 },
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
const noted = new Set();
|
|
303
|
+
for (const [path, kind] of ctx.kinds) {
|
|
304
|
+
const top = path.split("/").slice(0, 2).join("/");
|
|
305
|
+
if (kind === "instructions")
|
|
306
|
+
refuse("A flow has no instructions: move this to the agent under agents/ that needs it", path);
|
|
307
|
+
else if (kind === "mcp")
|
|
308
|
+
refuse("A flow has no MCP servers of its own: give mcp.json to the agents under agents/ that call them", path);
|
|
309
|
+
else if ((kind === "skill" || kind === "plugin") && !noted.has(top)) {
|
|
310
|
+
noted.add(top);
|
|
311
|
+
refuse(kind === "skill" ? `A flow has no skills of its own: move ${top}/ to the agent that uses it` : `A flow takes no plugins: vendor ${top}/ in the agent that uses it`, `${top}/`);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
for (const key of ["mcpServers", "skills", "memory"])
|
|
315
|
+
if (front[key] !== undefined)
|
|
316
|
+
refuse(`${key} is an agent's: set it in the AGENT.md of the agent under agents/ that needs it`, AGENT_FILE, [key]);
|
|
317
|
+
const inputSchema = front.inputSchema === undefined ? undefined : ctx.schemaAt(AGENT_FILE, ["inputSchema"], front.inputSchema);
|
|
318
|
+
// The agents it names: the flow root's.
|
|
319
|
+
const own = foldersUnder("agents", ctx.entries.keys());
|
|
320
|
+
let namespace;
|
|
321
|
+
let isRoot = false;
|
|
322
|
+
if (ctx.scope?.role === "stage" && ctx.scope.namespace) {
|
|
323
|
+
namespace = ctx.scope.namespace;
|
|
324
|
+
for (const [id, inner] of own)
|
|
325
|
+
ctx.report("error", "flow_namespace", `A sub-flow names its stages from the flow root's agents/, so it has none of its own: move agents/${id}/ to the flow root's agents/`, folderFile(id, inner));
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
namespace = new FlowNamespace(ctx, own);
|
|
329
|
+
isRoot = true;
|
|
330
|
+
}
|
|
331
|
+
const flow = ctx.readJson(FLOW_FILE);
|
|
332
|
+
if (!flow) {
|
|
333
|
+
// Its agents are compiled all the same, so one pass finds their errors too.
|
|
334
|
+
if (isRoot)
|
|
335
|
+
for (const id of namespace.ids)
|
|
336
|
+
namespace.resolve(id);
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
if ("$schema" in flow)
|
|
340
|
+
ctx.checkSchemaUrl(FLOW_FILE, "flow", flow.$schema);
|
|
341
|
+
const { $schema: _, ...tree } = flow;
|
|
342
|
+
const tools = new Map();
|
|
343
|
+
for (const tool of Array.isArray(harness.tools) ? harness.tools : [])
|
|
344
|
+
tools.set(String(tool.name), tool);
|
|
345
|
+
const translated = translateFlow(tree, { agentIds: namespace.ids, agent: (id) => namespace.resolve(id), used: namespace.used, tools }, (code, message, file, path, options) => ctx.report("error", code, message, file, path, options), ctx.suggest);
|
|
346
|
+
for (const name of tools.keys())
|
|
347
|
+
if (!translated.toolsUsed.has(name))
|
|
348
|
+
ctx.report("error", "tool_unused", `No node of flow.json names ${name}: a flow holds only the tools its nodes name`, `tools/${name}.json`);
|
|
349
|
+
if (isRoot) {
|
|
350
|
+
const unused = namespace.ids.filter((id) => !namespace.used.has(id));
|
|
351
|
+
for (const id of unused) {
|
|
352
|
+
// Compiled all the same, so its errors are found now rather than when it is named.
|
|
353
|
+
namespace.resolve(id);
|
|
354
|
+
ctx.report("warning", "agent_unused", `No flow.json under the flow root names ${id}, so it never runs and is left out of the manifest: name it in a node, or remove it`, folderFile(id, own.get(id)));
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
if (!translated.complete)
|
|
358
|
+
return false;
|
|
359
|
+
if (!translated.root)
|
|
360
|
+
return true;
|
|
361
|
+
// The workflow harness, in place of the agent harness built so far.
|
|
362
|
+
const { id, outputSchema, loop } = harness;
|
|
363
|
+
for (const key of Object.keys(harness))
|
|
364
|
+
delete harness[key];
|
|
365
|
+
Object.assign(harness, {
|
|
366
|
+
kind: "workflow",
|
|
367
|
+
...(id === undefined ? {} : { id }),
|
|
368
|
+
...(inputSchema === undefined ? {} : { inputSchema }),
|
|
369
|
+
...(outputSchema === undefined ? {} : { outputSchema }),
|
|
370
|
+
root: translated.root,
|
|
371
|
+
agents: translated.agents,
|
|
372
|
+
...(loop === undefined ? {} : { loop }),
|
|
373
|
+
});
|
|
374
|
+
ctx.origins.push([["harness", "root"], { file: FLOW_FILE, path: [] }], [["harness", "inputSchema"], { file: AGENT_FILE, path: ["inputSchema"] }]);
|
|
375
|
+
for (const agent of Object.keys(translated.agents))
|
|
376
|
+
ctx.origins.push([["harness", "agents", agent], { file: isRoot ? `agents/${agent}/${AGENT_FILE}` : FLOW_FILE, path: [] }]);
|
|
377
|
+
return true;
|
|
378
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type FolderContext } from "./context.js";
|
|
2
|
+
export declare const AGENT_PLUGINS_PLUGIN_SCHEMA = "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json";
|
|
3
|
+
export declare const AGENT_PLUGINS_MCP_SCHEMA = "https://agent-plugins.org/schemas/1.0.0/mcp.schema.json";
|
|
4
|
+
/** One label: the plugin a skill or server came from. */
|
|
5
|
+
export interface PluginLabel {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly version?: string;
|
|
8
|
+
}
|
|
9
|
+
/** A plugin's skills and servers, labelled, with the file each came from. */
|
|
10
|
+
export interface FoldedPlugins {
|
|
11
|
+
readonly servers: Map<string, {
|
|
12
|
+
readonly server: Record<string, unknown>;
|
|
13
|
+
readonly file: string;
|
|
14
|
+
}>;
|
|
15
|
+
readonly skills: Map<string, {
|
|
16
|
+
readonly skill: Record<string, unknown>;
|
|
17
|
+
readonly file: string;
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
/** The files of `plugins/<name>/` the compiler reads, relative to the plugin's folder. */
|
|
21
|
+
export declare function pluginTextPaths(paths: readonly string[]): string[];
|
|
22
|
+
/** Folds every plugin under `plugins/`. */
|
|
23
|
+
export declare function foldPlugins(ctx: FolderContext): FoldedPlugins;
|