@nylorun/core 0.25.0-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 +15 -0
- package/README.md +4 -0
- package/dist/compatibility.d.ts +14 -2
- package/dist/compatibility.js +14 -0
- package/dist/contracts.d.ts +339 -6
- package/dist/contracts.js +148 -6
- package/dist/folder/compile.d.ts +49 -0
- package/dist/folder/compile.js +869 -0
- package/dist/folder/frontmatter.d.ts +18 -0
- package/dist/folder/frontmatter.js +87 -0
- package/dist/folder/index.d.ts +7 -0
- package/dist/folder/index.js +7 -0
- package/dist/folder/json.d.ts +28 -0
- package/dist/folder/json.js +183 -0
- package/package.json +7 -2
|
@@ -0,0 +1,869 @@
|
|
|
1
|
+
import { AGENT_MANIFEST_V8_SCHEMA_VERSION, AgentManifestV8Schema, ENVIRONMENT_NAME_PATTERN, HarnessIdSchema, RuntimeSectionSchema, TOOL_SECRET_NAME_PATTERN, TriggerSchema, isAgentManifestName, } from "../contracts.js";
|
|
2
|
+
import { DELEGATE_NAME_PATTERN } from "../definition/delegate.js";
|
|
3
|
+
import { SKILL_ENTRY, skillFilePathIssue } from "../utils/definition-files.js";
|
|
4
|
+
import { parseMarkdown } from "./frontmatter.js";
|
|
5
|
+
import { parseJson, pointerOf } from "./json.js";
|
|
6
|
+
/** The compiler format this version builds: a folder with no `format`, or `format: 1`. */
|
|
7
|
+
export const AGENT_FOLDER_COMPILER = "agent-folder/1";
|
|
8
|
+
/** What a folder may hold. Fixed in this version; Tenant settings come later. */
|
|
9
|
+
export const AGENT_FOLDER_LIMITS = Object.freeze({
|
|
10
|
+
/** Files in one folder. */
|
|
11
|
+
files: 2000,
|
|
12
|
+
/** Every file's bytes together. */
|
|
13
|
+
totalBytes: 50 * 1024 * 1024,
|
|
14
|
+
/** One Markdown or JSON file the compiler reads. */
|
|
15
|
+
textFileBytes: 1024 * 1024,
|
|
16
|
+
});
|
|
17
|
+
const AGENT_FILE = "AGENT.md";
|
|
18
|
+
const RUNTIME_FILE = "runtime.json";
|
|
19
|
+
const MCP_FILE = "mcp.json";
|
|
20
|
+
const OUTPUT_SCHEMA_FILE = "output.schema.json";
|
|
21
|
+
const WORKSPACE = "/workspace";
|
|
22
|
+
const AGENT_PLUGINS_MCP_SCHEMA = "https://agent-plugins.org/schemas/1.0.0/mcp.schema.json";
|
|
23
|
+
const SKILL_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
24
|
+
const AGENT_KEYS = ["name", "description", "format", "harness", "outputSchema", "mcpServers", "skills", "memory", "loop", "metadata"];
|
|
25
|
+
const SKILL_KEYS = ["name", "description", "license", "compatibility", "metadata", "allowed-tools"];
|
|
26
|
+
const TRIGGER_KEYS = ["schedule", "timezone", "at", "webhook", "session"];
|
|
27
|
+
const SERVER_KEYS = ["type", "url", "headers"];
|
|
28
|
+
const SERVER_SETTING_KEYS = ["deferred", "tools"];
|
|
29
|
+
const TOOL_KEYS = ["description", "inputSchema", "outputSchema", "http", "approval"];
|
|
30
|
+
const isEnvFile = (name) => name === ".env" || name.startsWith(".env.");
|
|
31
|
+
/** What a path is, or why the folder may not hold it. */
|
|
32
|
+
function classify(path) {
|
|
33
|
+
const segments = path.split("/");
|
|
34
|
+
const base = segments.at(-1);
|
|
35
|
+
if (segments.includes(".git") || segments.includes("node_modules"))
|
|
36
|
+
return { code: "path_refused", error: ".git/ and node_modules/ are not part of an agent folder: leave them out of the upload" };
|
|
37
|
+
if (isEnvFile(base))
|
|
38
|
+
return { code: "secret_file", error: "Environment files hold secrets and never go in an agent folder: a secret is a slot in runtime.json, and its value goes to the Agent's vault" };
|
|
39
|
+
const top = segments[0];
|
|
40
|
+
if ((base.endsWith(".pem") || base.endsWith(".key")) && top !== "skills" && top !== "files")
|
|
41
|
+
return { code: "secret_file", error: "Key files never go in an agent folder outside skills/ and files/: a secret is a slot in runtime.json, and its value goes to the Agent's vault" };
|
|
42
|
+
if (path === AGENT_FILE)
|
|
43
|
+
return { kind: "agent" };
|
|
44
|
+
if (path === RUNTIME_FILE)
|
|
45
|
+
return { kind: "runtime" };
|
|
46
|
+
if (path === MCP_FILE)
|
|
47
|
+
return { kind: "mcp" };
|
|
48
|
+
if (segments.length === 1) {
|
|
49
|
+
if (base.toLowerCase() === "agent.md")
|
|
50
|
+
return { code: "wrong_name", error: `The root file is named ${AGENT_FILE}, in capitals` };
|
|
51
|
+
const stem = base.replace(/\.(?:ya?ml|jsonc|json5)$/, "");
|
|
52
|
+
if (stem !== base && ["runtime", "mcp", "AGENT"].includes(stem))
|
|
53
|
+
return { code: "wrong_syntax", error: `Write this file as ${stem === "AGENT" ? AGENT_FILE : `${stem}.json`}: an agent folder has one syntax per kind of file` };
|
|
54
|
+
if (path === "flow.json")
|
|
55
|
+
return { code: "not_supported", error: `Flows (flow.json) are not in ${AGENT_FOLDER_COMPILER} yet: save the flow as JSON or with the TypeScript builder` };
|
|
56
|
+
if (base.endsWith(".schema.json"))
|
|
57
|
+
return { kind: "schema" };
|
|
58
|
+
return { kind: "other" };
|
|
59
|
+
}
|
|
60
|
+
if (top === "agents" || top === "plugins")
|
|
61
|
+
return {
|
|
62
|
+
code: "not_supported",
|
|
63
|
+
error: `${top}/ is not in ${AGENT_FOLDER_COMPILER} yet: ${top === "agents" ? "subagents and flows" : "vendored Agent Plugins"} come in a later version`,
|
|
64
|
+
};
|
|
65
|
+
if (top === "instructions")
|
|
66
|
+
return segments.length === 2 && base.endsWith(".md")
|
|
67
|
+
? { kind: "instructions" }
|
|
68
|
+
: { code: "reserved_directory", error: "instructions/ holds Markdown files only, with no folders" };
|
|
69
|
+
if (top === "tools")
|
|
70
|
+
return segments.length === 2 && base.endsWith(".json")
|
|
71
|
+
? { kind: "tool" }
|
|
72
|
+
: { code: "reserved_directory", error: "tools/ holds one JSON file per HTTP tool, named <tool>.json" };
|
|
73
|
+
if (top === "triggers")
|
|
74
|
+
return segments.length === 2 && base.endsWith(".md")
|
|
75
|
+
? { kind: "trigger" }
|
|
76
|
+
: { code: "reserved_directory", error: "triggers/ holds one Markdown file per trigger, named <id>.md" };
|
|
77
|
+
if (top === "skills")
|
|
78
|
+
return segments.length >= 3 ? { kind: "skill" } : { code: "reserved_directory", error: "skills/ holds one folder per skill, each with its SKILL.md" };
|
|
79
|
+
if (top === "files")
|
|
80
|
+
return { kind: "file" };
|
|
81
|
+
if (top === "schemas")
|
|
82
|
+
return base.endsWith(".json") ? { kind: "schema" } : { code: "reserved_directory", error: "schemas/ holds JSON Schema files (.json)" };
|
|
83
|
+
if (top === ".nylorun")
|
|
84
|
+
return { kind: "other" };
|
|
85
|
+
if (base.endsWith(".schema.json"))
|
|
86
|
+
return { kind: "schema" };
|
|
87
|
+
return { kind: "other" };
|
|
88
|
+
}
|
|
89
|
+
/** The files the compiler reads: the Runtime and the CLI pass their bytes in. */
|
|
90
|
+
export function folderTextPaths(entries) {
|
|
91
|
+
const paths = [];
|
|
92
|
+
for (const entry of entries) {
|
|
93
|
+
const kind = classify(entry.path);
|
|
94
|
+
if (!("kind" in kind))
|
|
95
|
+
continue;
|
|
96
|
+
const textual = kind.kind === "agent" ||
|
|
97
|
+
kind.kind === "instructions" ||
|
|
98
|
+
kind.kind === "runtime" ||
|
|
99
|
+
kind.kind === "mcp" ||
|
|
100
|
+
kind.kind === "tool" ||
|
|
101
|
+
kind.kind === "trigger" ||
|
|
102
|
+
kind.kind === "schema" ||
|
|
103
|
+
(kind.kind === "skill" && entry.path.split("/").length === 3 && entry.path.endsWith(`/${SKILL_ENTRY}`));
|
|
104
|
+
if (textual)
|
|
105
|
+
paths.push(entry.path);
|
|
106
|
+
}
|
|
107
|
+
return paths.sort(compare);
|
|
108
|
+
}
|
|
109
|
+
const compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
110
|
+
/** Why `path` cannot name a file in a folder, or undefined when it can. */
|
|
111
|
+
function pathIssue(path) {
|
|
112
|
+
if (path.length === 0 || path.length > 1024)
|
|
113
|
+
return "must be 1 to 1024 characters";
|
|
114
|
+
if (path.includes("\\"))
|
|
115
|
+
return "must use / as its separator, not \\";
|
|
116
|
+
if (path.startsWith("/"))
|
|
117
|
+
return "must be relative to the folder";
|
|
118
|
+
if (/[\u0000-\u001f\u007f]/.test(path))
|
|
119
|
+
return "must not contain control characters";
|
|
120
|
+
if (path !== path.normalize("NFC"))
|
|
121
|
+
return "must be in Unicode NFC";
|
|
122
|
+
for (const segment of path.split("/"))
|
|
123
|
+
if (segment === "" || segment === "." || segment === "..")
|
|
124
|
+
return "must not have empty, . or .. segments";
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
/** The closest of `known` to `name`, when one is close enough to be a typo. */
|
|
128
|
+
function suggestion(name, known) {
|
|
129
|
+
let best;
|
|
130
|
+
for (const candidate of known) {
|
|
131
|
+
const distance = editDistance(name.toLowerCase(), candidate.toLowerCase());
|
|
132
|
+
if (distance <= Math.max(2, Math.floor(candidate.length / 3)) && (!best || distance < best.distance))
|
|
133
|
+
best = { value: candidate, distance };
|
|
134
|
+
}
|
|
135
|
+
return best?.value;
|
|
136
|
+
}
|
|
137
|
+
function editDistance(a, b) {
|
|
138
|
+
const row = Array.from({ length: b.length + 1 }, (_, index) => index);
|
|
139
|
+
for (let i = 1; i <= a.length; i++) {
|
|
140
|
+
let previous = row[0];
|
|
141
|
+
row[0] = i;
|
|
142
|
+
for (let j = 1; j <= b.length; j++) {
|
|
143
|
+
const current = row[j];
|
|
144
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, previous + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
145
|
+
previous = current;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return row[b.length];
|
|
149
|
+
}
|
|
150
|
+
const unknownKeyMessage = (key, known) => {
|
|
151
|
+
const close = suggestion(key, known);
|
|
152
|
+
return `Unknown key '${key}'.${close ? ` Did you mean '${close}'?` : ` Known keys: ${known.join(", ")}.`}`;
|
|
153
|
+
};
|
|
154
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
155
|
+
/** Compiles an agent folder into agent manifest v8, with every diagnostic. */
|
|
156
|
+
export function compileAgentFolder(input) {
|
|
157
|
+
const diagnostics = [];
|
|
158
|
+
const parsed = new Map();
|
|
159
|
+
const report = (severity, code, message, file, path, options = {}) => {
|
|
160
|
+
let at = options.at;
|
|
161
|
+
if (!at && file && path) {
|
|
162
|
+
const found = locate(parsed.get(file), path);
|
|
163
|
+
at = options.key ? (found?.key ?? found?.value) : found?.value;
|
|
164
|
+
}
|
|
165
|
+
diagnostics.push({
|
|
166
|
+
severity,
|
|
167
|
+
code,
|
|
168
|
+
message,
|
|
169
|
+
...(file === undefined ? {} : { file }),
|
|
170
|
+
...(at === undefined ? {} : { line: at.line, column: at.column }),
|
|
171
|
+
...(options.pointer === undefined ? {} : { pointer: options.pointer }),
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
const error = (code, message, file, path, options = {}) => report("error", code, message, file, path, options);
|
|
175
|
+
// --- The entries ------------------------------------------------------------------------
|
|
176
|
+
const entries = [...input.entries].sort((a, b) => compare(a.path, b.path));
|
|
177
|
+
const byPath = new Map();
|
|
178
|
+
const folded = new Map();
|
|
179
|
+
let bytes = 0;
|
|
180
|
+
for (const entry of entries) {
|
|
181
|
+
const issue = pathIssue(entry.path);
|
|
182
|
+
if (issue) {
|
|
183
|
+
error("path_invalid", `The path ${JSON.stringify(entry.path.slice(0, 200))} ${issue}`, entry.path);
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (byPath.has(entry.path)) {
|
|
187
|
+
error("path_duplicate", "The folder lists this path twice", entry.path);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const lower = entry.path.toLowerCase();
|
|
191
|
+
const other = folded.get(lower);
|
|
192
|
+
if (other !== undefined) {
|
|
193
|
+
error("path_case", `This path and ${other} differ only in case, which some file systems cannot hold`, entry.path);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (!/^[0-9a-f]{64}$/.test(entry.sha256)) {
|
|
197
|
+
error("path_invalid", "The file's sha256 is not 64 lowercase hex digits", entry.path);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
folded.set(lower, entry.path);
|
|
201
|
+
byPath.set(entry.path, entry);
|
|
202
|
+
bytes += entry.size;
|
|
203
|
+
}
|
|
204
|
+
if (entries.length > AGENT_FOLDER_LIMITS.files)
|
|
205
|
+
error("limit_exceeded", `An agent folder holds at most ${AGENT_FOLDER_LIMITS.files} files; this one holds ${entries.length}`);
|
|
206
|
+
if (bytes > AGENT_FOLDER_LIMITS.totalBytes)
|
|
207
|
+
error("limit_exceeded", `An agent folder holds at most ${AGENT_FOLDER_LIMITS.totalBytes} bytes; this one holds ${bytes}`);
|
|
208
|
+
const kinds = new Map();
|
|
209
|
+
for (const path of byPath.keys()) {
|
|
210
|
+
const kind = classify(path);
|
|
211
|
+
if ("kind" in kind)
|
|
212
|
+
kinds.set(path, kind.kind);
|
|
213
|
+
else
|
|
214
|
+
error(kind.code, kind.error, path);
|
|
215
|
+
}
|
|
216
|
+
for (const [path, kind] of kinds)
|
|
217
|
+
if (kind === "other")
|
|
218
|
+
report("note", "file_not_compiled", "Kept with the source; not part of the agent", path);
|
|
219
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
220
|
+
/** The text of a file the compiler reads, or undefined after reporting why it has none. */
|
|
221
|
+
const textOf = (path) => {
|
|
222
|
+
const entry = byPath.get(path);
|
|
223
|
+
if (!entry)
|
|
224
|
+
return undefined;
|
|
225
|
+
if (entry.size > AGENT_FOLDER_LIMITS.textFileBytes) {
|
|
226
|
+
error("limit_exceeded", `A Markdown or JSON file holds at most ${AGENT_FOLDER_LIMITS.textFileBytes} bytes; this one holds ${entry.size}`, path);
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
const bytesOf = input.files.get(path);
|
|
230
|
+
if (!bytesOf) {
|
|
231
|
+
error("file_missing", "The compiler was not given this file's bytes", path);
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
try {
|
|
235
|
+
return decoder.decode(bytesOf).replace(/^/, "");
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
error("not_utf8", "The file is not UTF-8 text", path);
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
const readJson = (path) => {
|
|
243
|
+
const text = textOf(path);
|
|
244
|
+
if (text === undefined)
|
|
245
|
+
return undefined;
|
|
246
|
+
const result = parseJson(text);
|
|
247
|
+
if (!result.ok) {
|
|
248
|
+
error("json_invalid", result.message, path, undefined, { at: result.at });
|
|
249
|
+
return undefined;
|
|
250
|
+
}
|
|
251
|
+
parsed.set(path, { path, positions: result.positions });
|
|
252
|
+
if (!isObject(result.value)) {
|
|
253
|
+
error("json_invalid", "The file holds a JSON object", path, []);
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
256
|
+
return result.value;
|
|
257
|
+
};
|
|
258
|
+
const readMarkdown = (path) => {
|
|
259
|
+
const text = textOf(path);
|
|
260
|
+
if (text === undefined)
|
|
261
|
+
return undefined;
|
|
262
|
+
const result = parseMarkdown(text);
|
|
263
|
+
if (!result.ok) {
|
|
264
|
+
error("frontmatter_invalid", result.message, path, undefined, { at: result.at });
|
|
265
|
+
return undefined;
|
|
266
|
+
}
|
|
267
|
+
parsed.set(path, { path, positions: result.positions });
|
|
268
|
+
return result;
|
|
269
|
+
};
|
|
270
|
+
const checkKeys = (file, value, known, path = []) => {
|
|
271
|
+
for (const key of Object.keys(value))
|
|
272
|
+
if (!known.includes(key))
|
|
273
|
+
error("unknown_key", unknownKeyMessage(key, known), file, [...path, key], { key: true });
|
|
274
|
+
};
|
|
275
|
+
/** A JSON Schema a field names by path (`./x.json` from the root, `/x.json`), or inline. */
|
|
276
|
+
const schemaAt = (file, field, value) => {
|
|
277
|
+
if (isObject(value))
|
|
278
|
+
return value;
|
|
279
|
+
if (typeof value !== "string") {
|
|
280
|
+
error("schema_invalid", "A schema is a JSON Schema object, or a path to a JSON Schema file", file, field);
|
|
281
|
+
return undefined;
|
|
282
|
+
}
|
|
283
|
+
const target = value.startsWith("/") ? value.slice(1) : value.replace(/^\.\//, "");
|
|
284
|
+
if (target.split("/").some((segment) => segment === ".." || segment === "")) {
|
|
285
|
+
error("path_outside", `${value} leaves the folder: a path names a file inside it`, file, field);
|
|
286
|
+
return undefined;
|
|
287
|
+
}
|
|
288
|
+
if (!byPath.has(target)) {
|
|
289
|
+
error("schema_missing", `${value} is not a file in the folder`, file, field);
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
if (kinds.get(target) !== "schema") {
|
|
293
|
+
error("schema_invalid", `${value} is not a JSON Schema file: name it *.schema.json or keep it under schemas/`, file, field);
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
return readJson(target);
|
|
297
|
+
};
|
|
298
|
+
// --- AGENT.md ---------------------------------------------------------------------------
|
|
299
|
+
if (!byPath.has(AGENT_FILE))
|
|
300
|
+
error("agent_missing", `An agent folder has an ${AGENT_FILE} at its root, with name and description in its frontmatter`);
|
|
301
|
+
const agent = byPath.has(AGENT_FILE) ? readMarkdown(AGENT_FILE) : undefined;
|
|
302
|
+
const front = agent?.data ?? {};
|
|
303
|
+
let name;
|
|
304
|
+
let description;
|
|
305
|
+
const harness = {};
|
|
306
|
+
const instructions = [];
|
|
307
|
+
let metadata;
|
|
308
|
+
const origins = [];
|
|
309
|
+
if (agent) {
|
|
310
|
+
if (!agent.hasFrontmatter)
|
|
311
|
+
error("frontmatter_missing", `${AGENT_FILE} opens with frontmatter between --- lines, with name and description`, AGENT_FILE, undefined, { at: { line: 1, column: 1 } });
|
|
312
|
+
checkKeys(AGENT_FILE, front, AGENT_KEYS);
|
|
313
|
+
if (typeof front.name !== "string" || front.name === "")
|
|
314
|
+
error("name_missing", "name is required: the agent manifest's name", AGENT_FILE, front.name === undefined ? undefined : ["name"]);
|
|
315
|
+
else if (!isAgentManifestName(front.name))
|
|
316
|
+
error("name_invalid", "name is 1-64 letters, digits, '.', '_' and '-', starting with a letter or digit", AGENT_FILE, ["name"]);
|
|
317
|
+
else if (input.name !== undefined && front.name !== input.name)
|
|
318
|
+
error("name_mismatch", `name is ${front.name}, and the folder is saved as ${input.name}: they must match`, AGENT_FILE, ["name"]);
|
|
319
|
+
else
|
|
320
|
+
name = front.name;
|
|
321
|
+
if (typeof front.description !== "string" || front.description.trim() === "")
|
|
322
|
+
error("description_missing", "description is required: what the agent does, in a sentence", AGENT_FILE, front.description === undefined ? undefined : ["description"]);
|
|
323
|
+
else
|
|
324
|
+
description = front.description.trim();
|
|
325
|
+
if (front.format !== undefined && front.format !== 1)
|
|
326
|
+
error("format_unsupported", `format ${JSON.stringify(front.format)} is not supported: this compiler builds format 1 (${AGENT_FOLDER_COMPILER})`, AGENT_FILE, ["format"]);
|
|
327
|
+
if (front.harness !== undefined) {
|
|
328
|
+
if (HarnessIdSchema.safeParse(front.harness).success)
|
|
329
|
+
harness.id = front.harness;
|
|
330
|
+
else
|
|
331
|
+
error("harness_invalid", "harness is the id of the harness that runs the loop, such as nylorun", AGENT_FILE, ["harness"]);
|
|
332
|
+
}
|
|
333
|
+
if (front.outputSchema !== undefined) {
|
|
334
|
+
const schema = schemaAt(AGENT_FILE, ["outputSchema"], front.outputSchema);
|
|
335
|
+
if (schema)
|
|
336
|
+
harness.outputSchema = schema;
|
|
337
|
+
}
|
|
338
|
+
for (const key of ["memory", "loop"]) {
|
|
339
|
+
if (front[key] === undefined)
|
|
340
|
+
continue;
|
|
341
|
+
if (isObject(front[key]))
|
|
342
|
+
harness[key] = front[key];
|
|
343
|
+
else
|
|
344
|
+
error("value_invalid", `${key} is a map`, AGENT_FILE, [key]);
|
|
345
|
+
}
|
|
346
|
+
if (front.metadata !== undefined) {
|
|
347
|
+
if (isObject(front.metadata))
|
|
348
|
+
metadata = front.metadata;
|
|
349
|
+
else
|
|
350
|
+
error("value_invalid", "metadata is a map", AGENT_FILE, ["metadata"]);
|
|
351
|
+
}
|
|
352
|
+
const body = trimBlankLines(agent.body);
|
|
353
|
+
if (body !== "")
|
|
354
|
+
instructions.push(body);
|
|
355
|
+
origins.push([["harness", "instructions"], { file: AGENT_FILE, path: [] }]);
|
|
356
|
+
for (const key of ["memory", "loop", "outputSchema"])
|
|
357
|
+
origins.push([["harness", key], { file: AGENT_FILE, path: [key] }]);
|
|
358
|
+
origins.push([["harness", "id"], { file: AGENT_FILE, path: ["harness"] }]);
|
|
359
|
+
}
|
|
360
|
+
for (const path of [...kinds].filter(([, kind]) => kind === "instructions").map(([path]) => path)) {
|
|
361
|
+
const text = textOf(path);
|
|
362
|
+
if (text === undefined)
|
|
363
|
+
continue;
|
|
364
|
+
const trimmed = trimBlankLines(text);
|
|
365
|
+
if (trimmed !== "")
|
|
366
|
+
instructions.push(trimmed);
|
|
367
|
+
}
|
|
368
|
+
if (instructions.length > 0)
|
|
369
|
+
harness.instructions = instructions;
|
|
370
|
+
// --- runtime.json ----------------------------------------------------------------------
|
|
371
|
+
let runtime;
|
|
372
|
+
if (byPath.has(RUNTIME_FILE)) {
|
|
373
|
+
const value = readJson(RUNTIME_FILE);
|
|
374
|
+
if (value) {
|
|
375
|
+
if ("$schema" in value)
|
|
376
|
+
error("schema_key", "runtime.json takes no $schema: the path gives the kind, and the folder its format", RUNTIME_FILE, ["$schema"], { key: true });
|
|
377
|
+
if ("extends" in value)
|
|
378
|
+
error("not_supported", `extends is not in ${AGENT_FOLDER_COMPILER} yet: write the whole runtime section here`, RUNTIME_FILE, ["extends"], { key: true });
|
|
379
|
+
const { $schema: _, extends: __, ...rest } = value;
|
|
380
|
+
runtime = rest;
|
|
381
|
+
origins.push([["runtime"], { file: RUNTIME_FILE, path: [] }]);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
const derivedSlots = new Map();
|
|
385
|
+
/** A slot for `credential`, derived when runtime.json does not declare it. */
|
|
386
|
+
const deriveSlot = (credential, type, url, file, path, header) => {
|
|
387
|
+
const declared = isObject(runtime?.secrets) ? runtime.secrets[credential] : undefined;
|
|
388
|
+
if (declared !== undefined)
|
|
389
|
+
return;
|
|
390
|
+
const existing = derivedSlots.get(credential);
|
|
391
|
+
if (existing) {
|
|
392
|
+
if (existing.url !== url || existing.slot.type !== type) {
|
|
393
|
+
const at = locate(parsed.get(existing.file), existing.path)?.value;
|
|
394
|
+
error("credential_urls", `${credential} is the credential of ${existing.url} (${existing.file}${at ? `:${at.line}` : ""}) and of ${url}: a slot is bound to one URL, so give each URL its own slot`, file, path);
|
|
395
|
+
}
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
const slot = {
|
|
399
|
+
type,
|
|
400
|
+
url,
|
|
401
|
+
required: true,
|
|
402
|
+
...(header ? { description: `Sets the ${header} header` } : {}),
|
|
403
|
+
};
|
|
404
|
+
derivedSlots.set(credential, { slot, file, path, url });
|
|
405
|
+
report("info", "slot_derived", `A ${type} slot ${credential} is derived for ${url}`, file, path, { pointer: pointerOf(["runtime", "secrets", credential]) });
|
|
406
|
+
};
|
|
407
|
+
// --- mcp.json --------------------------------------------------------------------------
|
|
408
|
+
const servers = {};
|
|
409
|
+
let mcpRead = false;
|
|
410
|
+
if (byPath.has(MCP_FILE)) {
|
|
411
|
+
const value = readJson(MCP_FILE);
|
|
412
|
+
if (value) {
|
|
413
|
+
mcpRead = true;
|
|
414
|
+
checkKeys(MCP_FILE, value, ["$schema", "mcpServers"]);
|
|
415
|
+
if (value.$schema !== undefined && value.$schema !== AGENT_PLUGINS_MCP_SCHEMA)
|
|
416
|
+
error("schema_key", `$schema in mcp.json names the Agent Plugins 1.0.0 MCP schema (${AGENT_PLUGINS_MCP_SCHEMA}), or is left out`, MCP_FILE, ["$schema"]);
|
|
417
|
+
const listed = value.mcpServers;
|
|
418
|
+
if (!isObject(listed))
|
|
419
|
+
error("mcp_invalid", "mcp.json holds mcpServers: a map of server names to servers", MCP_FILE, listed === undefined ? [] : ["mcpServers"]);
|
|
420
|
+
else
|
|
421
|
+
for (const [key, server] of Object.entries(listed)) {
|
|
422
|
+
const at = ["mcpServers", key];
|
|
423
|
+
if (!DELEGATE_NAME_PATTERN.test(key)) {
|
|
424
|
+
error("mcp_invalid", `MCP server name '${key}' may use only letters, digits, _ and -, up to 64 characters`, MCP_FILE, at, { key: true });
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (!isObject(server)) {
|
|
428
|
+
error("mcp_invalid", "An MCP server is a map with type, url and headers", MCP_FILE, at);
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
checkKeys(MCP_FILE, server, SERVER_KEYS, at);
|
|
432
|
+
const type = server.type;
|
|
433
|
+
if (type === undefined) {
|
|
434
|
+
error("mcp_type", "type is required: streamable-http or sse (a server with no type is a stdio server, and the Runtime runs remote MCP servers only)", MCP_FILE, at, { key: true });
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (type === "http") {
|
|
438
|
+
error("mcp_type", 'type "http" is Claude Code\'s name; Agent Plugins writes "streamable-http". Fix: change it to "streamable-http"', MCP_FILE, [...at, "type"]);
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
if (type === "stdio" || type === "ws") {
|
|
442
|
+
error("mcp_type", `type ${type} is not served: the Runtime runs remote MCP servers only (streamable-http or sse)`, MCP_FILE, [...at, "type"]);
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
if (type !== "streamable-http" && type !== "sse") {
|
|
446
|
+
error("mcp_type", "type is streamable-http or sse", MCP_FILE, [...at, "type"]);
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
if (typeof server.url !== "string" || server.url === "") {
|
|
450
|
+
error("mcp_invalid", "url is required: the server's own URL", MCP_FILE, server.url === undefined ? at : [...at, "url"]);
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
if (server.url.includes("${")) {
|
|
454
|
+
error("credential_placement", "${…} names a credential in a header only: a URL is not secret", MCP_FILE, [...at, "url"]);
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
const headers = {};
|
|
458
|
+
let credential;
|
|
459
|
+
let headersValid = true;
|
|
460
|
+
if (server.headers !== undefined && !isObject(server.headers)) {
|
|
461
|
+
error("mcp_invalid", "headers is a map of header names to strings", MCP_FILE, [...at, "headers"]);
|
|
462
|
+
headersValid = false;
|
|
463
|
+
}
|
|
464
|
+
for (const [header, raw] of Object.entries(isObject(server.headers) ? server.headers : {})) {
|
|
465
|
+
const path = [...at, "headers", header];
|
|
466
|
+
if (typeof raw !== "string") {
|
|
467
|
+
error("mcp_invalid", "A header's value is a string", MCP_FILE, path);
|
|
468
|
+
headersValid = false;
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
if (!raw.includes("${")) {
|
|
472
|
+
headers[header] = raw;
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
const placeholder = /^(Bearer )?\$\{([^}]*)\}$/.exec(raw);
|
|
476
|
+
const named = placeholder?.[2];
|
|
477
|
+
if (!placeholder || named === undefined || raw.indexOf("${") !== raw.lastIndexOf("${")) {
|
|
478
|
+
error("credential_placement", "A header names a credential as its whole value, ${NAME}, or as Bearer ${NAME}", MCP_FILE, path);
|
|
479
|
+
headersValid = false;
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if (named.includes(":-") || named.includes(":")) {
|
|
483
|
+
error("credential_default", `\${${named}} has a default: a secret has none, and its value goes to the Agent's vault`, MCP_FILE, path);
|
|
484
|
+
headersValid = false;
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
if (!TOOL_SECRET_NAME_PATTERN.test(named)) {
|
|
488
|
+
error("credential_name", "A credential's name is 1-64 letters, digits, '.', '_' and '-'", MCP_FILE, path);
|
|
489
|
+
headersValid = false;
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
if (credential) {
|
|
493
|
+
error("credential_twice", `This server already names ${credential.name} in ${credential.header}: a server authenticates with one credential, in one header`, MCP_FILE, path);
|
|
494
|
+
headersValid = false;
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
const bearer = placeholder[1] !== undefined;
|
|
498
|
+
if (bearer && header.toLowerCase() !== "authorization") {
|
|
499
|
+
error("credential_placement", "Bearer ${NAME} goes in the Authorization header; another header takes ${NAME} alone", MCP_FILE, path);
|
|
500
|
+
headersValid = false;
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
credential = { name: named, header, type: bearer ? "bearer" : "headers", path };
|
|
504
|
+
}
|
|
505
|
+
if (!headersValid)
|
|
506
|
+
continue;
|
|
507
|
+
if (credential)
|
|
508
|
+
deriveSlot(credential.name, credential.type, server.url, MCP_FILE, credential.path, credential.type === "headers" ? credential.header : undefined);
|
|
509
|
+
servers[key] = {
|
|
510
|
+
name: key,
|
|
511
|
+
type,
|
|
512
|
+
url: server.url,
|
|
513
|
+
...(Object.keys(headers).length === 0 ? {} : { headers }),
|
|
514
|
+
...(credential ? { credential: credential.name } : {}),
|
|
515
|
+
};
|
|
516
|
+
origins.push([["harness", "mcpServers", key], { file: MCP_FILE, path: at }]);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
if (front.mcpServers !== undefined) {
|
|
521
|
+
if (!isObject(front.mcpServers))
|
|
522
|
+
error("value_invalid", "mcpServers is a map of server names to this agent's settings", AGENT_FILE, ["mcpServers"]);
|
|
523
|
+
else
|
|
524
|
+
for (const [key, settings] of Object.entries(front.mcpServers)) {
|
|
525
|
+
const at = ["mcpServers", key];
|
|
526
|
+
if (!servers[key]) {
|
|
527
|
+
if (byPath.has(MCP_FILE) && !mcpRead)
|
|
528
|
+
continue;
|
|
529
|
+
const close = suggestion(key, Object.keys(servers));
|
|
530
|
+
error("server_unknown", `No MCP server ${key} is declared in mcp.json.${close ? ` Did you mean '${close}'?` : ""}`, AGENT_FILE, at, { key: true });
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
if (!isObject(settings)) {
|
|
534
|
+
error("value_invalid", "A server's settings are a map: deferred and tools", AGENT_FILE, at);
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
checkKeys(AGENT_FILE, settings, SERVER_SETTING_KEYS, at);
|
|
538
|
+
if ("credential" in settings)
|
|
539
|
+
continue;
|
|
540
|
+
for (const setting of SERVER_SETTING_KEYS)
|
|
541
|
+
if (settings[setting] !== undefined) {
|
|
542
|
+
servers[key][setting] = settings[setting];
|
|
543
|
+
origins.push([["harness", "mcpServers", key, setting], { file: AGENT_FILE, path: [...at, setting] }]);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
if (Object.keys(servers).length > 0)
|
|
548
|
+
harness.mcpServers = servers;
|
|
549
|
+
// --- skills/ ---------------------------------------------------------------------------
|
|
550
|
+
const skills = {};
|
|
551
|
+
const skillFolders = new Map();
|
|
552
|
+
for (const [path, kind] of kinds)
|
|
553
|
+
if (kind === "skill") {
|
|
554
|
+
const folder = path.split("/")[1];
|
|
555
|
+
skillFolders.set(folder, [...(skillFolders.get(folder) ?? []), path]);
|
|
556
|
+
}
|
|
557
|
+
for (const [folder, paths] of skillFolders) {
|
|
558
|
+
const entryPath = `skills/${folder}/${SKILL_ENTRY}`;
|
|
559
|
+
if (!byPath.has(entryPath)) {
|
|
560
|
+
error("skill_invalid", `skills/${folder}/ has no ${SKILL_ENTRY}`, `skills/${folder}/${SKILL_ENTRY}`);
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
const markdown = readMarkdown(entryPath);
|
|
564
|
+
if (!markdown)
|
|
565
|
+
continue;
|
|
566
|
+
const data = markdown.data;
|
|
567
|
+
let valid = true;
|
|
568
|
+
for (const key of Object.keys(data)) {
|
|
569
|
+
if (key === "secrets") {
|
|
570
|
+
error("skill_secrets", `secrets is not an Agent Skills key: which secrets a skill gets is the agent's choice. Fix: move it to ${AGENT_FILE}'s skills block, as skills: { ${folder}: { secrets: [...] } }`, entryPath, ["secrets"], { key: true });
|
|
571
|
+
valid = false;
|
|
572
|
+
}
|
|
573
|
+
else if (!SKILL_KEYS.includes(key)) {
|
|
574
|
+
error("unknown_key", unknownKeyMessage(key, SKILL_KEYS), entryPath, [key], { key: true });
|
|
575
|
+
valid = false;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
const skillName = typeof data.name === "string" ? data.name.trim() : "";
|
|
579
|
+
const skillDescription = typeof data.description === "string" ? data.description.trim() : "";
|
|
580
|
+
if (!SKILL_NAME.test(skillName) || skillName.length > 64) {
|
|
581
|
+
error("skill_invalid", "name is required: 1-64 lowercase letters, digits and single hyphens", entryPath, data.name === undefined ? undefined : ["name"]);
|
|
582
|
+
valid = false;
|
|
583
|
+
}
|
|
584
|
+
else if (skillName !== folder) {
|
|
585
|
+
error("skill_invalid", `name is ${skillName}, and the skill's folder is ${folder}: they must match`, entryPath, ["name"]);
|
|
586
|
+
valid = false;
|
|
587
|
+
}
|
|
588
|
+
if (skillDescription === "" || skillDescription.length > 1024) {
|
|
589
|
+
error("skill_invalid", "description is required, at most 1024 characters", entryPath, data.description === undefined ? undefined : ["description"]);
|
|
590
|
+
valid = false;
|
|
591
|
+
}
|
|
592
|
+
const files = {};
|
|
593
|
+
for (const path of paths.sort(compare)) {
|
|
594
|
+
const relative = path.slice(`skills/${folder}/`.length);
|
|
595
|
+
const issue = skillFilePathIssue(relative);
|
|
596
|
+
if (issue) {
|
|
597
|
+
error("path_invalid", `A skill's file path ${issue}`, path);
|
|
598
|
+
valid = false;
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
files[relative] = `sha256:${byPath.get(path).sha256}`;
|
|
602
|
+
}
|
|
603
|
+
if (!valid)
|
|
604
|
+
continue;
|
|
605
|
+
skills[skillName] = { name: skillName, description: skillDescription, files };
|
|
606
|
+
origins.push([["harness", "skills", skillName], { file: entryPath, path: [] }]);
|
|
607
|
+
}
|
|
608
|
+
if (front.skills !== undefined) {
|
|
609
|
+
if (!isObject(front.skills))
|
|
610
|
+
error("value_invalid", "skills is a map of skill names to this agent's settings for them", AGENT_FILE, ["skills"]);
|
|
611
|
+
else
|
|
612
|
+
for (const [key, settings] of Object.entries(front.skills)) {
|
|
613
|
+
const at = ["skills", key];
|
|
614
|
+
if (!skills[key]) {
|
|
615
|
+
if (skillFolders.has(key))
|
|
616
|
+
continue;
|
|
617
|
+
const close = suggestion(key, [...skillFolders.keys()]);
|
|
618
|
+
error("skill_unknown", `No skill ${key} is in skills/.${close ? ` Did you mean '${close}'?` : ""}`, AGENT_FILE, at, { key: true });
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
if (!isObject(settings)) {
|
|
622
|
+
error("value_invalid", "A skill's settings are a map: secrets", AGENT_FILE, at);
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
checkKeys(AGENT_FILE, settings, ["secrets"], at);
|
|
626
|
+
const secrets = settings.secrets;
|
|
627
|
+
if (secrets === undefined)
|
|
628
|
+
continue;
|
|
629
|
+
if (!Array.isArray(secrets) || !secrets.every((item) => typeof item === "string" && ENVIRONMENT_NAME_PATTERN.test(item))) {
|
|
630
|
+
error("value_invalid", "secrets is a list of secret slot names, each an environment variable name", AGENT_FILE, [...at, "secrets"]);
|
|
631
|
+
continue;
|
|
632
|
+
}
|
|
633
|
+
if (secrets.length > 0)
|
|
634
|
+
skills[key].secrets = [...new Set(secrets)].sort();
|
|
635
|
+
origins.push([["harness", "skills", key, "secrets"], { file: AGENT_FILE, path: [...at, "secrets"] }]);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
if (Object.keys(skills).length > 0)
|
|
639
|
+
harness.skills = skills;
|
|
640
|
+
// --- tools/ ----------------------------------------------------------------------------
|
|
641
|
+
const tools = [];
|
|
642
|
+
for (const path of [...kinds].filter(([, kind]) => kind === "tool").map(([path]) => path)) {
|
|
643
|
+
const toolName = path.slice("tools/".length, -".json".length);
|
|
644
|
+
if (!DELEGATE_NAME_PATTERN.test(toolName)) {
|
|
645
|
+
error("tool_invalid", `A tool's name comes from its file name: '${toolName}' may use only letters, digits, _ and -, up to 64 characters`, path);
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
const value = readJson(path);
|
|
649
|
+
if (!value)
|
|
650
|
+
continue;
|
|
651
|
+
if ("name" in value) {
|
|
652
|
+
error("tool_invalid", "A tool file takes no name: its file name is the tool's name", path, ["name"], { key: true });
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
if ("agent" in value) {
|
|
656
|
+
error("not_supported", `A subagent as a tool is not in ${AGENT_FOLDER_COMPILER} yet`, path, ["agent"], { key: true });
|
|
657
|
+
continue;
|
|
658
|
+
}
|
|
659
|
+
if ("$schema" in value)
|
|
660
|
+
error("schema_key", "A tool file takes no $schema: the path gives the kind", path, ["$schema"], { key: true });
|
|
661
|
+
checkKeys(path, value, [...TOOL_KEYS, "$schema"]);
|
|
662
|
+
if (!isObject(value.http)) {
|
|
663
|
+
error("tool_invalid", "A tool file declares an HTTP tool: http with its url", path, value.http === undefined ? [] : ["http"]);
|
|
664
|
+
continue;
|
|
665
|
+
}
|
|
666
|
+
const tool = { name: toolName };
|
|
667
|
+
if (value.description !== undefined)
|
|
668
|
+
tool.description = value.description;
|
|
669
|
+
const input = value.inputSchema === undefined ? undefined : schemaAt(path, ["inputSchema"], value.inputSchema);
|
|
670
|
+
if (value.inputSchema === undefined)
|
|
671
|
+
error("tool_invalid", "inputSchema is required: the JSON Schema of the tool's input", path, []);
|
|
672
|
+
if (input)
|
|
673
|
+
tool.inputSchema = input;
|
|
674
|
+
if (value.outputSchema !== undefined) {
|
|
675
|
+
const output = schemaAt(path, ["outputSchema"], value.outputSchema);
|
|
676
|
+
if (output)
|
|
677
|
+
tool.outputSchema = output;
|
|
678
|
+
}
|
|
679
|
+
tool.http = value.http;
|
|
680
|
+
if (value.approval !== undefined)
|
|
681
|
+
tool.approval = value.approval;
|
|
682
|
+
const credential = value.http.credential;
|
|
683
|
+
if (typeof credential === "string" && typeof value.http.url === "string")
|
|
684
|
+
deriveSlot(credential, "bearer", value.http.url, path, ["http", "credential"]);
|
|
685
|
+
origins.push([["harness", "tools", tools.length], { file: path, path: [] }]);
|
|
686
|
+
tools.push(tool);
|
|
687
|
+
}
|
|
688
|
+
if (tools.length > 0)
|
|
689
|
+
harness.tools = tools;
|
|
690
|
+
// --- triggers/ -------------------------------------------------------------------------
|
|
691
|
+
const triggers = {};
|
|
692
|
+
for (const path of [...kinds].filter(([, kind]) => kind === "trigger").map(([path]) => path)) {
|
|
693
|
+
const id = path.slice("triggers/".length, -".md".length);
|
|
694
|
+
const markdown = readMarkdown(path);
|
|
695
|
+
if (!markdown)
|
|
696
|
+
continue;
|
|
697
|
+
if (!markdown.hasFrontmatter) {
|
|
698
|
+
error("frontmatter_missing", "A trigger opens with frontmatter: schedule, at or webhook, and session", path, undefined, { at: { line: 1, column: 1 } });
|
|
699
|
+
continue;
|
|
700
|
+
}
|
|
701
|
+
checkKeys(path, markdown.data, TRIGGER_KEYS);
|
|
702
|
+
const trigger = { ...markdown.data, prompt: trimBlankLines(markdown.body) };
|
|
703
|
+
if (isObject(runtime?.triggers) && id in runtime.triggers) {
|
|
704
|
+
error("trigger_twice", `Trigger ${id} is declared in runtime.json and in ${path}: keep one`, path);
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
triggers[id] = trigger;
|
|
708
|
+
origins.push([["runtime", "triggers", id], { file: path, path: [] }]);
|
|
709
|
+
}
|
|
710
|
+
// --- files/ ----------------------------------------------------------------------------
|
|
711
|
+
const seeded = {};
|
|
712
|
+
for (const [path, kind] of kinds)
|
|
713
|
+
if (kind === "file")
|
|
714
|
+
seeded[path.slice("files/".length)] = `sha256:${byPath.get(path).sha256}`;
|
|
715
|
+
// --- The runtime section -----------------------------------------------------------------
|
|
716
|
+
const needsRuntime = derivedSlots.size > 0 || Object.keys(triggers).length > 0 || Object.keys(seeded).length > 0;
|
|
717
|
+
if (!runtime && needsRuntime)
|
|
718
|
+
runtime = { kind: "none" };
|
|
719
|
+
if (runtime) {
|
|
720
|
+
if (derivedSlots.size > 0) {
|
|
721
|
+
const declared = isObject(runtime.secrets) ? runtime.secrets : {};
|
|
722
|
+
runtime.secrets = { ...declared, ...Object.fromEntries([...derivedSlots].map(([key, { slot }]) => [key, slot])) };
|
|
723
|
+
for (const [key, derived] of derivedSlots)
|
|
724
|
+
origins.push([["runtime", "secrets", key], { file: derived.file, path: derived.path }]);
|
|
725
|
+
}
|
|
726
|
+
if (Object.keys(triggers).length > 0)
|
|
727
|
+
runtime.triggers = { ...(isObject(runtime.triggers) ? runtime.triggers : {}), ...triggers };
|
|
728
|
+
if (Object.keys(seeded).length > 0) {
|
|
729
|
+
runtime.files = [...(Array.isArray(runtime.files) ? runtime.files : []), { path: WORKSPACE, files: seeded }];
|
|
730
|
+
origins.push([["runtime", "files"], { file: byPath.has(RUNTIME_FILE) ? RUNTIME_FILE : "files/", path: ["files"] }]);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
// --- Every field checked as the Runtime checks the manifest ------------------------------
|
|
734
|
+
if (diagnostics.some((item) => item.severity === "error") || name === undefined || description === undefined)
|
|
735
|
+
return finish(diagnostics);
|
|
736
|
+
const document = {
|
|
737
|
+
manifestSchemaVersion: AGENT_MANIFEST_V8_SCHEMA_VERSION,
|
|
738
|
+
name,
|
|
739
|
+
description,
|
|
740
|
+
...(metadata === undefined ? {} : { metadata }),
|
|
741
|
+
harness,
|
|
742
|
+
...(runtime === undefined ? {} : { runtime }),
|
|
743
|
+
};
|
|
744
|
+
origins.push([["name"], { file: AGENT_FILE, path: ["name"] }], [["description"], { file: AGENT_FILE, path: ["description"] }], [["metadata"], { file: AGENT_FILE, path: ["metadata"] }]);
|
|
745
|
+
// Triggers from triggers/ are checked against their own schema first, with the file's lines.
|
|
746
|
+
for (const [id, trigger] of Object.entries(triggers)) {
|
|
747
|
+
const result = TriggerSchema.safeParse(trigger);
|
|
748
|
+
if (result.success)
|
|
749
|
+
continue;
|
|
750
|
+
for (const issue of result.error.issues)
|
|
751
|
+
issueDiagnostic(issue, ["runtime", "triggers", id], `triggers/${id}.md`, issue.path);
|
|
752
|
+
}
|
|
753
|
+
const checked = AgentManifestV8Schema.safeParse(document);
|
|
754
|
+
if (!checked.success)
|
|
755
|
+
for (const issue of checked.error.issues) {
|
|
756
|
+
if (issue.path[0] === "runtime" && issue.path[1] === "triggers" && typeof issue.path[2] === "string" && issue.path[2] in triggers)
|
|
757
|
+
continue;
|
|
758
|
+
const origin = originOf(origins, issue.path);
|
|
759
|
+
issueDiagnostic(issue, [], origin?.file, origin ? [...origin.origin.path, ...issue.path.slice(origin.prefix)] : undefined);
|
|
760
|
+
}
|
|
761
|
+
if (diagnostics.some((item) => item.severity === "error"))
|
|
762
|
+
return finish(diagnostics);
|
|
763
|
+
return finish(diagnostics, checked.success ? checked.data : undefined);
|
|
764
|
+
function issueDiagnostic(issue, prefix, file, path) {
|
|
765
|
+
const pointer = pointerOf([...prefix, ...issue.path]);
|
|
766
|
+
if (issue.code === "unrecognized_keys") {
|
|
767
|
+
const known = knownKeys([...prefix, ...issue.path]);
|
|
768
|
+
for (const key of issue.keys)
|
|
769
|
+
error("unknown_key", known.length ? unknownKeyMessage(key, known) : `Unknown key '${key}'`, file, path ? [...path, key] : undefined, { key: true, pointer: pointerOf([...prefix, ...issue.path, key]) });
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
// The field as the file names it, or as the manifest does when no file holds it.
|
|
773
|
+
const shown = file && path ? path : [...prefix, ...issue.path];
|
|
774
|
+
const field = shown.length ? `${shown.map(String).join(".")}: ` : "";
|
|
775
|
+
error("schema_invalid", `${field}${issue.message}`, file, path, { pointer });
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
function finish(diagnostics, manifest) {
|
|
779
|
+
const order = { error: 0, warning: 1, info: 2, note: 3 };
|
|
780
|
+
const sorted = [...diagnostics].sort((a, b) => order[a.severity] - order[b.severity] ||
|
|
781
|
+
compare(a.file ?? "", b.file ?? "") ||
|
|
782
|
+
(a.line ?? 0) - (b.line ?? 0) ||
|
|
783
|
+
(a.column ?? 0) - (b.column ?? 0));
|
|
784
|
+
return { ...(manifest === undefined ? {} : { manifest }), diagnostics: sorted, compiler: AGENT_FOLDER_COMPILER };
|
|
785
|
+
}
|
|
786
|
+
/** The closest recorded position for `path` in `file`: the field, else its nearest parent. */
|
|
787
|
+
function locate(file, path) {
|
|
788
|
+
if (!file)
|
|
789
|
+
return undefined;
|
|
790
|
+
for (let length = path.length; length >= 0; length--) {
|
|
791
|
+
const found = file.positions.get(pointerOf(path.slice(0, length)));
|
|
792
|
+
if (found)
|
|
793
|
+
return found;
|
|
794
|
+
}
|
|
795
|
+
return undefined;
|
|
796
|
+
}
|
|
797
|
+
/** The origin recorded for the longest prefix of a manifest path. */
|
|
798
|
+
function originOf(origins, path) {
|
|
799
|
+
let best;
|
|
800
|
+
for (const [prefix, origin] of origins) {
|
|
801
|
+
if (prefix.length > path.length || (best && prefix.length <= best.prefix))
|
|
802
|
+
continue;
|
|
803
|
+
if (prefix.every((part, index) => String(part) === String(path[index])))
|
|
804
|
+
best = { file: origin.file, origin, prefix: prefix.length };
|
|
805
|
+
}
|
|
806
|
+
return best;
|
|
807
|
+
}
|
|
808
|
+
/** The keys an object at `path` of `value` holds, for a "did you mean". */
|
|
809
|
+
function knownKeys(path) {
|
|
810
|
+
let schema = AgentManifestV8Schema;
|
|
811
|
+
for (const part of path) {
|
|
812
|
+
schema = descend(schema, part);
|
|
813
|
+
if (!schema)
|
|
814
|
+
return [];
|
|
815
|
+
}
|
|
816
|
+
const shape = shapeOf(schema);
|
|
817
|
+
return shape ? Object.keys(shape) : [];
|
|
818
|
+
}
|
|
819
|
+
function defOf(schema) {
|
|
820
|
+
return schema?._zod?.def;
|
|
821
|
+
}
|
|
822
|
+
function unwrap(schema) {
|
|
823
|
+
for (let depth = 0; depth < 20; depth++) {
|
|
824
|
+
const def = defOf(schema);
|
|
825
|
+
if (!def)
|
|
826
|
+
return schema;
|
|
827
|
+
if (def.type === "optional" || def.type === "nullable" || def.type === "default" || def.type === "readonly" || def.type === "catch")
|
|
828
|
+
schema = def.innerType;
|
|
829
|
+
else if (def.type === "pipe")
|
|
830
|
+
schema = def.in;
|
|
831
|
+
else if (def.type === "lazy")
|
|
832
|
+
schema = def.getter();
|
|
833
|
+
else
|
|
834
|
+
return schema;
|
|
835
|
+
}
|
|
836
|
+
return schema;
|
|
837
|
+
}
|
|
838
|
+
function shapeOf(schema) {
|
|
839
|
+
const def = defOf(unwrap(schema));
|
|
840
|
+
return def?.type === "object" ? def.shape : undefined;
|
|
841
|
+
}
|
|
842
|
+
function descend(schema, part) {
|
|
843
|
+
const inner = unwrap(schema);
|
|
844
|
+
const def = defOf(inner);
|
|
845
|
+
if (!def)
|
|
846
|
+
return undefined;
|
|
847
|
+
if (def.type === "object")
|
|
848
|
+
return def.shape[String(part)];
|
|
849
|
+
if (def.type === "array")
|
|
850
|
+
return def.element;
|
|
851
|
+
if (def.type === "record")
|
|
852
|
+
return def.valueType;
|
|
853
|
+
if (def.type === "union")
|
|
854
|
+
for (const option of def.options) {
|
|
855
|
+
const found = descend(option, part);
|
|
856
|
+
if (found)
|
|
857
|
+
return found;
|
|
858
|
+
}
|
|
859
|
+
return undefined;
|
|
860
|
+
}
|
|
861
|
+
/** `text` without its leading and trailing blank lines; nothing else is touched. */
|
|
862
|
+
function trimBlankLines(text) {
|
|
863
|
+
const lines = text.replace(/\r\n/g, "\n").split("\n");
|
|
864
|
+
while (lines.length > 0 && lines[0].trim() === "")
|
|
865
|
+
lines.shift();
|
|
866
|
+
while (lines.length > 0 && lines.at(-1).trim() === "")
|
|
867
|
+
lines.pop();
|
|
868
|
+
return lines.join("\n");
|
|
869
|
+
}
|