@nylorun/core 0.26.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 +54 -0
- package/README.md +6 -0
- package/dist/compatibility.d.ts +11 -2
- package/dist/compatibility.js +10 -0
- package/dist/contracts.d.ts +362 -0
- package/dist/contracts.js +131 -5
- 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 +87 -0
- package/dist/folder/compile.js +1280 -0
- 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 +21 -0
- package/dist/folder/frontmatter.js +91 -0
- package/dist/folder/index.d.ts +12 -0
- package/dist/folder/index.js +12 -0
- package/dist/folder/json-schema.d.ts +15 -0
- package/dist/folder/json-schema.js +212 -0
- package/dist/folder/json.d.ts +28 -0
- package/dist/folder/json.js +183 -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 +14 -3
|
@@ -0,0 +1,660 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The agent folder of a manifest (track T7, D80): `folderOfManifest` is `compileAgentFolder` run
|
|
3
|
+
* backwards. It writes the folder that compiles back to the same agent manifest v8, for an agent
|
|
4
|
+
* saved as JSON or from TypeScript (`nylorun export --folder`) and for the three-file format
|
|
5
|
+
* (`nylorun migrate folder`). Like the compiler it is pure: no file system, clock or network.
|
|
6
|
+
*
|
|
7
|
+
* Each manifest field goes where the compiler reads it:
|
|
8
|
+
*
|
|
9
|
+
* - `name`, `description`, `metadata`, `harness.id`, `memory`, `loop`, and this agent's server and
|
|
10
|
+
* skill settings: `AGENT.md` frontmatter. `harness.outputSchema`: `output.schema.json`, named by
|
|
11
|
+
* `outputSchema`.
|
|
12
|
+
* - `harness.instructions`: the first as `AGENT.md`'s body, each other as
|
|
13
|
+
* `instructions/NN-<slug>.md`, zero-padded so path order and natural order agree.
|
|
14
|
+
* - HTTP tools: `tools/<name>.json`, their schemas inline.
|
|
15
|
+
* - Subagents (a tool with `agent`): `agents/<name>/`, an agent folder of the embedded agent
|
|
16
|
+
* manifest v7 (or a flow, workflow manifest v3), as the compiler reads one (design §3.3).
|
|
17
|
+
* - Flows (`kind: "workflow"`, design §3.8): `AGENT.md` with no body and `inputSchema`
|
|
18
|
+
* (`input.schema.json`), `flow.json` with v3's node tree naming agents by id and tools by file
|
|
19
|
+
* name, and one flat `agents/<id>/` namespace at the flow root for every agent of the flow and
|
|
20
|
+
* of its sub-flows. A sub-flow is an `agents/<id>/` with its own `flow.json` over the same
|
|
21
|
+
* namespace. A tool stage, and an HTTP verifier (`{ http }`, `{ tool }` in a folder), is
|
|
22
|
+
* `tools/<name>.json` beside the `flow.json` that names it. Schemas are written inline or
|
|
23
|
+
* beside the agent that uses them: shared schema files are not recovered.
|
|
24
|
+
* - MCP servers: `mcp.json` in Agent Plugins syntax; a server's `credential` is
|
|
25
|
+
* `Authorization: Bearer ${SLOT}` for a `bearer` slot, `<header>: ${SLOT}` for a `headers` one.
|
|
26
|
+
* - `runtime`: `runtime.json`, without what the folder gives on its own: each trigger that is a
|
|
27
|
+
* `triggers/<id>.md`, and each secret slot the compiler derives identically from a credential,
|
|
28
|
+
* in the folder or in any folder inside it. With nothing left but kind `none`, there is no
|
|
29
|
+
* `runtime.json`.
|
|
30
|
+
* - `runtime.files` stay in `runtime.json` by hash: the Runtime holds the bytes. Given the bytes
|
|
31
|
+
* (`options.files`), a last `/workspace` entry becomes `files/`.
|
|
32
|
+
* - Skills: the manifest has their files by hash only. Given the bytes (`options.files`, by
|
|
33
|
+
* `sha256:<hex>`), each skill is written as its folder; a `SKILL.md` that still says `secrets`
|
|
34
|
+
* loses that key, which `AGENT.md`'s `skills` block holds now (D80), so its hash changes.
|
|
35
|
+
* Without them, a placeholder `SKILL.md` stands in and a note lists the files to copy in.
|
|
36
|
+
*
|
|
37
|
+
* What a folder cannot say is left out or written another way, with a note: an embedded agent's
|
|
38
|
+
* display `name` (in a folder an agent's name is its id), plugin labels, a tool's approval, two
|
|
39
|
+
* different agents of one id in a flow's namespace. Every note says what differs, and `exact`
|
|
40
|
+
* says whether the folder, compiled, gives the same manifest (`hashManifest`): it is checked by
|
|
41
|
+
* compiling it.
|
|
42
|
+
*/
|
|
43
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
44
|
+
import { bytesToHex } from "@noble/hashes/utils.js";
|
|
45
|
+
import { stringify } from "yaml";
|
|
46
|
+
import { DELEGATE_INPUT_SCHEMA } from "../definition/delegate.js";
|
|
47
|
+
import { canonical } from "../utils/canonical.js";
|
|
48
|
+
import { hashManifest } from "../utils/hash.js";
|
|
49
|
+
import { compileAgentFolder, folderTextPaths } from "./compile.js";
|
|
50
|
+
import { parseMarkdown } from "./frontmatter.js";
|
|
51
|
+
const AGENT_FILE = "AGENT.md";
|
|
52
|
+
const FLOW_FILE = "flow.json";
|
|
53
|
+
const OUTPUT_SCHEMA_FILE = "output.schema.json";
|
|
54
|
+
const INPUT_SCHEMA_FILE = "input.schema.json";
|
|
55
|
+
const WORKSPACE = "/workspace";
|
|
56
|
+
const TRIGGER_FILE_KEYS = ["schedule", "timezone", "at", "webhook", "session", "prompt"];
|
|
57
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
58
|
+
const json = (value) => `${JSON.stringify(value, null, 2)}\n`;
|
|
59
|
+
const same = (a, b) => canonical(a) === canonical(b);
|
|
60
|
+
const compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
61
|
+
/** Frontmatter as `parseMarkdown` reads it back: YAML 1.2, core schema, no anchors or aliases. */
|
|
62
|
+
function frontmatter(data) {
|
|
63
|
+
return stringify(data, { version: "1.2", schema: "core", aliasDuplicateObjects: false, lineWidth: 0 });
|
|
64
|
+
}
|
|
65
|
+
/** `text` as the compiler keeps a Markdown file's prose: without leading and trailing blank lines. */
|
|
66
|
+
function trimBlankLines(text) {
|
|
67
|
+
const lines = text.replace(/\r\n/g, "\n").split("\n");
|
|
68
|
+
while (lines.length > 0 && lines[0].trim() === "")
|
|
69
|
+
lines.shift();
|
|
70
|
+
while (lines.length > 0 && lines.at(-1).trim() === "")
|
|
71
|
+
lines.pop();
|
|
72
|
+
return lines.join("\n");
|
|
73
|
+
}
|
|
74
|
+
/** Whether the compiler reads `text` back as written from a Markdown body. */
|
|
75
|
+
const keepsProse = (text) => text !== "" && !text.startsWith("") && trimBlankLines(text) === text;
|
|
76
|
+
/** A short file name part from an instruction's first line: `## Tone of voice` → `tone-of-voice`. */
|
|
77
|
+
function slugOf(text) {
|
|
78
|
+
const line = text.split("\n").find((item) => item.trim() !== "") ?? "";
|
|
79
|
+
const words = line
|
|
80
|
+
.replace(/^#+\s*/, "")
|
|
81
|
+
.toLowerCase()
|
|
82
|
+
.split(/[^a-z0-9]+/)
|
|
83
|
+
.filter(Boolean);
|
|
84
|
+
let slug = "";
|
|
85
|
+
for (const word of words.slice(0, 5)) {
|
|
86
|
+
if (slug.length + word.length + 1 > 40)
|
|
87
|
+
break;
|
|
88
|
+
slug = slug ? `${slug}-${word}` : word;
|
|
89
|
+
}
|
|
90
|
+
return slug;
|
|
91
|
+
}
|
|
92
|
+
/** The value of a `headers` slot's description the compiler writes: the header the value sets. */
|
|
93
|
+
const headerOfSlot = (slot) => isObject(slot) && typeof slot.description === "string" ? /^Sets the (\S+) header$/.exec(slot.description)?.[1] : undefined;
|
|
94
|
+
const derivedSlot = (use) => ({
|
|
95
|
+
type: use.type,
|
|
96
|
+
url: use.url,
|
|
97
|
+
required: true,
|
|
98
|
+
...(use.type === "headers" && use.header ? { description: `Sets the ${use.header} header` } : {}),
|
|
99
|
+
});
|
|
100
|
+
/** Writes the agent folder of `manifest`; see the module comment for what it covers. */
|
|
101
|
+
export function folderOfManifest(manifest, options = {}) {
|
|
102
|
+
const writer = {
|
|
103
|
+
files: new Map(),
|
|
104
|
+
notes: [],
|
|
105
|
+
uses: new Map(),
|
|
106
|
+
secrets: isObject(manifest.runtime?.secrets) ? manifest.runtime.secrets : {},
|
|
107
|
+
bytesOf: (hash) => options.files?.get(hash),
|
|
108
|
+
};
|
|
109
|
+
const identity = { name: manifest.name, description: manifest.description, metadata: manifest.metadata };
|
|
110
|
+
const root = { prefix: "", role: "root" };
|
|
111
|
+
if (isWorkflow(manifest.harness))
|
|
112
|
+
writeFlow(writer, root, identity, manifest.harness);
|
|
113
|
+
else
|
|
114
|
+
writeAgent(writer, root, identity, manifest.harness);
|
|
115
|
+
writeRuntime(writer, manifest);
|
|
116
|
+
const sorted = new Map([...writer.files].sort(([a], [b]) => compare(a, b)));
|
|
117
|
+
const exact = verify(manifest, sorted, writer.notes);
|
|
118
|
+
return { files: sorted, notes: writer.notes, exact };
|
|
119
|
+
}
|
|
120
|
+
/** A note about a folder: one inside the root names its path first. */
|
|
121
|
+
const noteAt = (writer, place, text) => writer.notes.push(place.prefix === "" ? text : `${place.prefix}: ${text}`);
|
|
122
|
+
const useCredential = (writer, credential, use) => writer.uses.set(credential, [...(writer.uses.get(credential) ?? []), use]);
|
|
123
|
+
/** `AGENT.md`'s name and description, with a placeholder for a missing description. */
|
|
124
|
+
function identityFront(writer, place, identity) {
|
|
125
|
+
const front = { name: identity.name };
|
|
126
|
+
const description = identity.description;
|
|
127
|
+
if (description === undefined || description.trim() === "") {
|
|
128
|
+
front.description = `What ${identity.name} does, in a sentence.`;
|
|
129
|
+
noteAt(writer, place, `The ${place.role === "root" ? "manifest" : "agent"} has no description, which AGENT.md needs: write one there (a placeholder stands in).`);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
if (description.trim() !== description)
|
|
133
|
+
noteAt(writer, place, "The description loses its leading and trailing spaces: AGENT.md's is trimmed.");
|
|
134
|
+
front.description = description;
|
|
135
|
+
}
|
|
136
|
+
return front;
|
|
137
|
+
}
|
|
138
|
+
/** A JSON Schema field as a file beside `AGENT.md`, named in its frontmatter. */
|
|
139
|
+
function schemaFile(writer, place, front, key, file, schema, field) {
|
|
140
|
+
if (schema === undefined)
|
|
141
|
+
return;
|
|
142
|
+
if (!isObject(schema)) {
|
|
143
|
+
noteAt(writer, place, `${field} is not a JSON Schema object, which a folder names by file: it is left out.`);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
front[key] = `./${file}`;
|
|
147
|
+
writer.files.set(place.prefix + file, json(schema));
|
|
148
|
+
}
|
|
149
|
+
/** An agent folder: `AGENT.md`, `mcp.json`, `skills/`, `tools/`, `agents/` and `instructions/`. */
|
|
150
|
+
function writeAgent(writer, place, identity, harness) {
|
|
151
|
+
const { files } = writer;
|
|
152
|
+
const at = place.prefix;
|
|
153
|
+
const root = place.role === "root";
|
|
154
|
+
const note = (text) => noteAt(writer, place, text);
|
|
155
|
+
const front = identityFront(writer, place, identity);
|
|
156
|
+
if (harness.id !== undefined)
|
|
157
|
+
front.harness = harness.id;
|
|
158
|
+
schemaFile(writer, place, front, "outputSchema", OUTPUT_SCHEMA_FILE, harness.outputSchema, "harness.outputSchema");
|
|
159
|
+
// --- MCP servers -------------------------------------------------------------------------
|
|
160
|
+
const secrets = writer.secrets;
|
|
161
|
+
const servers = {};
|
|
162
|
+
const serverSettings = {};
|
|
163
|
+
for (const [key, server] of Object.entries(harness.mcpServers ?? {})) {
|
|
164
|
+
const url = String(server.url);
|
|
165
|
+
const headers = { ...(server.headers ?? {}) };
|
|
166
|
+
if (Object.values(headers).some((value) => value.includes("${"))) {
|
|
167
|
+
note(`MCP server ${key}: a header holds \${…}, which mcp.json reads as a credential: it is left out.`);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (url.includes("${")) {
|
|
171
|
+
note(`MCP server ${key}: its url holds \${…}, which mcp.json refuses: it is left out.`);
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const credential = typeof server.credential === "string" ? server.credential : undefined;
|
|
175
|
+
if (credential !== undefined && !root)
|
|
176
|
+
note(`MCP server ${key} authenticates with ${credential}, and only the root agent's servers take a credential: it is left out.`);
|
|
177
|
+
else if (credential !== undefined) {
|
|
178
|
+
const slot = secrets[credential];
|
|
179
|
+
const taken = (name) => Object.keys(headers).some((header) => header.toLowerCase() === name.toLowerCase());
|
|
180
|
+
const wanted = headerOfSlot(slot);
|
|
181
|
+
if (isObject(slot) && slot.type === "headers") {
|
|
182
|
+
const header = [wanted, "X-Api-Key", "X-Credential"].find((name) => name !== undefined && !taken(name)) ?? `X-${credential}`;
|
|
183
|
+
headers[header] = `\${${credential}}`;
|
|
184
|
+
useCredential(writer, credential, { type: "headers", url, header });
|
|
185
|
+
}
|
|
186
|
+
else if (!taken("Authorization")) {
|
|
187
|
+
headers.Authorization = `Bearer \${${credential}}`;
|
|
188
|
+
useCredential(writer, credential, { type: "bearer", url });
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
const header = ["X-Credential", "X-Api-Key"].find((name) => !taken(name)) ?? `X-${credential}`;
|
|
192
|
+
headers[header] = `\${${credential}}`;
|
|
193
|
+
useCredential(writer, credential, { type: "headers", url, header });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
servers[key] = {
|
|
197
|
+
type: server.type,
|
|
198
|
+
url,
|
|
199
|
+
...(Object.keys(headers).length === 0 ? {} : { headers }),
|
|
200
|
+
};
|
|
201
|
+
const settings = {};
|
|
202
|
+
if (server.deferred !== undefined)
|
|
203
|
+
settings.deferred = server.deferred;
|
|
204
|
+
if (server.tools !== undefined)
|
|
205
|
+
settings.tools = server.tools;
|
|
206
|
+
if (Object.keys(settings).length > 0)
|
|
207
|
+
serverSettings[key] = settings;
|
|
208
|
+
if (isObject(server.plugin))
|
|
209
|
+
note(`MCP server ${key} came from plugin ${String(server.plugin.name)}; the manifest does not hold the plugin, so it is written as the folder's own server.`);
|
|
210
|
+
if (isObject(server.headers) && Object.keys(server.headers).length === 0)
|
|
211
|
+
note(`MCP server ${key}: its empty headers map is left out.`);
|
|
212
|
+
}
|
|
213
|
+
if (Object.keys(servers).length > 0)
|
|
214
|
+
files.set(`${at}mcp.json`, json({ mcpServers: servers }));
|
|
215
|
+
if (Object.keys(serverSettings).length > 0)
|
|
216
|
+
front.mcpServers = serverSettings;
|
|
217
|
+
// --- Skills ------------------------------------------------------------------------------
|
|
218
|
+
const skillSettings = {};
|
|
219
|
+
for (const [key, skill] of Object.entries(harness.skills ?? {})) {
|
|
220
|
+
if (isObject(skill.plugin))
|
|
221
|
+
note(`Skill ${key} came from plugin ${String(skill.plugin.name)}; the manifest does not hold the plugin, so it is written as the folder's own skill.`);
|
|
222
|
+
if (Array.isArray(skill.secrets)) {
|
|
223
|
+
const listed = skill.secrets;
|
|
224
|
+
if (!root)
|
|
225
|
+
note(`Skill ${key}: only the root agent's skills get secrets, so its secrets (${listed.join(", ")}) are left out.`);
|
|
226
|
+
else {
|
|
227
|
+
skillSettings[key] = { secrets: [...listed] };
|
|
228
|
+
const sorted = [...new Set(listed)].sort();
|
|
229
|
+
if (!same(sorted, listed))
|
|
230
|
+
note(`Skill ${key}: its secrets are sorted, each once${sorted.length === 0 ? ", and an empty list is left out" : ""}.`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
const missing = [];
|
|
234
|
+
for (const [path, hash] of Object.entries(skill.files ?? {})) {
|
|
235
|
+
const bytes = writer.bytesOf(hash);
|
|
236
|
+
if (!bytes) {
|
|
237
|
+
missing.push(`${path} (${hash})`);
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
const file = `${at}skills/${key}/${path}`;
|
|
241
|
+
if (path !== "SKILL.md") {
|
|
242
|
+
files.set(file, bytes);
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
const moved = withoutSkillSecrets(bytes);
|
|
246
|
+
if (moved === undefined)
|
|
247
|
+
files.set(file, bytes);
|
|
248
|
+
else {
|
|
249
|
+
files.set(file, moved);
|
|
250
|
+
note(root
|
|
251
|
+
? `skills/${key}/SKILL.md: secrets moved to AGENT.md's skills block (D80), so the file and its hash change.`
|
|
252
|
+
: `skills/${key}/SKILL.md: its secrets are left out, as only the root agent's skills get secrets, so the file and its hash change.`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (missing.length > 0) {
|
|
256
|
+
if (!files.has(`${at}skills/${key}/SKILL.md`))
|
|
257
|
+
files.set(`${at}skills/${key}/SKILL.md`, `---\n${frontmatter({ name: skill.name, description: skill.description })}---\nA placeholder: the saved manifest names this skill's files by hash only. Copy the skill's folder over this one.\n`);
|
|
258
|
+
note(`Skill ${key}: the manifest has its files by hash only, so copy the skill's folder into ${at}skills/${key}/. Missing: ${missing.join(", ")}.`);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (Object.keys(skillSettings).length > 0)
|
|
262
|
+
front.skills = skillSettings;
|
|
263
|
+
if (harness.skills !== undefined && Object.keys(harness.skills).length === 0)
|
|
264
|
+
note("harness.skills is an empty map: it is left out.");
|
|
265
|
+
if (harness.memory !== undefined)
|
|
266
|
+
front.memory = harness.memory;
|
|
267
|
+
if (harness.loop !== undefined) {
|
|
268
|
+
if (isObject(harness.loop))
|
|
269
|
+
front.loop = harness.loop;
|
|
270
|
+
else
|
|
271
|
+
note("harness.loop is not a map, which AGENT.md needs: it is left out.");
|
|
272
|
+
}
|
|
273
|
+
if (identity.metadata !== undefined) {
|
|
274
|
+
if (isObject(identity.metadata))
|
|
275
|
+
front.metadata = identity.metadata;
|
|
276
|
+
else
|
|
277
|
+
note("metadata is not a map, which AGENT.md needs: it is left out.");
|
|
278
|
+
}
|
|
279
|
+
if (harness.plugins !== undefined && harness.plugins.length > 0)
|
|
280
|
+
note(`harness.plugins (${harness.plugins.map((plugin) => plugin.name).join(", ")}) is left out: the manifest names the plugins only, so their skills and servers are written as the folder's own.`);
|
|
281
|
+
// --- Tools and subagents -----------------------------------------------------------------
|
|
282
|
+
const tools = harness.tools ?? [];
|
|
283
|
+
const listed = [];
|
|
284
|
+
const http = [];
|
|
285
|
+
const agents = [];
|
|
286
|
+
for (const tool of tools) {
|
|
287
|
+
const name = String(tool.name);
|
|
288
|
+
if (tool.agent !== undefined) {
|
|
289
|
+
if (place.role === "subagent") {
|
|
290
|
+
note(`Tool ${name} is a subagent of a subagent, and agents used as tools nest one level deep: it is left out.`);
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
writeSubagent(writer, place, tool);
|
|
294
|
+
listed.push(name);
|
|
295
|
+
agents.push(name);
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (!isObject(tool.http)) {
|
|
299
|
+
note(`Tool ${name} is neither an HTTP tool nor a subagent, which a folder cannot write: it is left out.`);
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
const file = {};
|
|
303
|
+
if (tool.description !== undefined)
|
|
304
|
+
file.description = tool.description;
|
|
305
|
+
file.inputSchema = tool.inputSchema;
|
|
306
|
+
if (tool.outputSchema !== undefined)
|
|
307
|
+
file.outputSchema = tool.outputSchema;
|
|
308
|
+
file.http = tool.http;
|
|
309
|
+
if (tool.approval !== undefined)
|
|
310
|
+
note(`Tool ${name} has approval "${String(tool.approval)}", which a folder does not take: a person approves a call through the Tenant's tool policy (a rule with "effect": "ask"). It is left out.`);
|
|
311
|
+
files.set(`${at}tools/${name}.json`, json(file));
|
|
312
|
+
listed.push(name);
|
|
313
|
+
http.push(name);
|
|
314
|
+
if (typeof tool.http.credential === "string")
|
|
315
|
+
useCredential(writer, tool.http.credential, { type: "bearer", url: String(tool.http.url) });
|
|
316
|
+
}
|
|
317
|
+
// The compiler lists tools/ by file name, then agents/ by folder name.
|
|
318
|
+
const order = [...[...http].sort((a, b) => compare(`tools/${a}.json`, `tools/${b}.json`)), ...[...agents].sort(compare)];
|
|
319
|
+
if (!same(order, listed))
|
|
320
|
+
note(agents.length === 0
|
|
321
|
+
? `harness.tools are listed in file name order (${order.join(", ")}), as tools/ gives them.`
|
|
322
|
+
: `harness.tools are listed as a folder gives them (${order.join(", ")}): tools/ by file name, then agents/ by folder name.`);
|
|
323
|
+
if (harness.tools !== undefined && harness.tools.length === 0)
|
|
324
|
+
note("harness.tools is an empty list: it is left out.");
|
|
325
|
+
// --- Instructions ------------------------------------------------------------------------
|
|
326
|
+
const instructions = harness.instructions ?? [];
|
|
327
|
+
if (harness.instructions !== undefined && instructions.length === 0)
|
|
328
|
+
note("harness.instructions is an empty list: it is left out.");
|
|
329
|
+
let body = "";
|
|
330
|
+
const width = Math.max(2, String(instructions.length - 1).length);
|
|
331
|
+
instructions.forEach((text, index) => {
|
|
332
|
+
const prose = trimBlankLines(text.replace(/^/, ""));
|
|
333
|
+
if (!keepsProse(text))
|
|
334
|
+
note(prose === ""
|
|
335
|
+
? `Instruction ${index + 1} is blank, and a blank file gives no instruction: it is left out.`
|
|
336
|
+
: `Instruction ${index + 1} loses its leading and trailing blank lines${text.includes("\r\n") ? " and its CRLF line ends" : ""}, as Markdown files are read.`);
|
|
337
|
+
if (prose === "")
|
|
338
|
+
return;
|
|
339
|
+
if (index === 0 && body === "") {
|
|
340
|
+
body = prose;
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
const slug = slugOf(prose);
|
|
344
|
+
files.set(`${at}instructions/${String(index).padStart(width, "0")}${slug ? `-${slug}` : ""}.md`, `${prose}\n`);
|
|
345
|
+
});
|
|
346
|
+
files.set(`${at}${AGENT_FILE}`, `---\n${frontmatter(front)}---\n${body === "" ? "" : `${body}\n`}`);
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* A tool with `agent`: `agents/<name>/`, offered back as the tool the compiler builds from it
|
|
350
|
+
* (named by its folder, with the agent's description and `{ task }` input).
|
|
351
|
+
*/
|
|
352
|
+
function writeSubagent(writer, place, tool) {
|
|
353
|
+
const name = String(tool.name);
|
|
354
|
+
const agent = tool.agent;
|
|
355
|
+
const note = (text) => noteAt(writer, place, text);
|
|
356
|
+
if (tool.description !== agent.description)
|
|
357
|
+
note(`Tool ${name} is a subagent, whose description in a folder is its agent's${tool.description === undefined ? "" : `: the tool's own is left out`}.`);
|
|
358
|
+
if (!same(tool.inputSchema, DELEGATE_INPUT_SCHEMA))
|
|
359
|
+
note(`Tool ${name} is a subagent, which takes { task } in a folder: its inputSchema is left out.`);
|
|
360
|
+
if (tool.outputSchema !== undefined)
|
|
361
|
+
note(`Tool ${name} is a subagent, whose output is its agent's outputSchema in a folder: the tool's is left out.`);
|
|
362
|
+
if (tool.approval !== undefined)
|
|
363
|
+
note(`Tool ${name} has approval "${String(tool.approval)}", which a folder does not take: a person approves a call through the Tenant's tool policy (a rule with "effect": "ask"). It is left out.`);
|
|
364
|
+
writeEmbedded(writer, { prefix: `${place.prefix}agents/${name}/`, role: "subagent" }, name, agent);
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* An agent embedded in a manifest (a subagent's, or a stage of a flow): v7 as an agent folder,
|
|
368
|
+
* v3 as a flow folder. `id` is its folder's name, which its `AGENT.md` name must be.
|
|
369
|
+
*/
|
|
370
|
+
function writeEmbedded(writer, place, id, agent, namespace) {
|
|
371
|
+
const note = (text) => noteAt(writer, place, text);
|
|
372
|
+
if (agent.id !== id)
|
|
373
|
+
note(`The embedded agent's id is ${agent.id}, and in a folder it is its folder's name, ${id}.`);
|
|
374
|
+
if (agent.name !== undefined)
|
|
375
|
+
note(`The agent's display name "${agent.name}" is left out: in a folder an agent's name is its id (design §3.2).`);
|
|
376
|
+
const identity = { name: id, description: agent.description, metadata: agent.metadata };
|
|
377
|
+
// Its `id` is the agent's, not the harness's: `AGENT.md`'s name holds it.
|
|
378
|
+
if (isWorkflow(agent)) {
|
|
379
|
+
const { id: _, ...flow } = agent;
|
|
380
|
+
if (flow.sandbox !== undefined)
|
|
381
|
+
note("The flow's sandbox is left out: a folder's agents run in the root's runtime.json.");
|
|
382
|
+
writeFlow(writer, place, identity, flow, namespace);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
const { id: _, ...fields } = agent;
|
|
386
|
+
if (fields.requires !== undefined)
|
|
387
|
+
note("requires is left out: a folder's agents need what the root's runtime.json gives.");
|
|
388
|
+
writeAgent(writer, place, identity, fields);
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* A flow folder: `AGENT.md` with no body, `flow.json`, the tools its nodes name, and its agents
|
|
392
|
+
* in the flow root's `agents/` (`namespace`, which a sub-flow shares with the flow root).
|
|
393
|
+
*/
|
|
394
|
+
function writeFlow(writer, place, identity, harness, namespace) {
|
|
395
|
+
const at = place.prefix;
|
|
396
|
+
const note = (text) => noteAt(writer, place, text);
|
|
397
|
+
const front = identityFront(writer, place, identity);
|
|
398
|
+
if (harness.id !== undefined)
|
|
399
|
+
front.harness = harness.id;
|
|
400
|
+
schemaFile(writer, place, front, "inputSchema", INPUT_SCHEMA_FILE, harness.inputSchema, "harness.inputSchema");
|
|
401
|
+
schemaFile(writer, place, front, "outputSchema", OUTPUT_SCHEMA_FILE, harness.outputSchema, "harness.outputSchema");
|
|
402
|
+
if (harness.loop !== undefined) {
|
|
403
|
+
if (isObject(harness.loop))
|
|
404
|
+
front.loop = harness.loop;
|
|
405
|
+
else
|
|
406
|
+
note("harness.loop is not a map, which AGENT.md needs: it is left out.");
|
|
407
|
+
}
|
|
408
|
+
if (identity.metadata !== undefined) {
|
|
409
|
+
if (isObject(identity.metadata))
|
|
410
|
+
front.metadata = identity.metadata;
|
|
411
|
+
else
|
|
412
|
+
note("metadata is not a map, which AGENT.md needs: it is left out.");
|
|
413
|
+
}
|
|
414
|
+
if (harness.plugins !== undefined && harness.plugins.length > 0)
|
|
415
|
+
note(`harness.plugins (${harness.plugins.map((plugin) => plugin.name).join(", ")}) is left out: a flow takes no plugins.`);
|
|
416
|
+
const names = namespace ?? { dir: `${at}agents/`, members: new Map() };
|
|
417
|
+
// Tool files beside this flow.json: a stage's by its name, an HTTP verifier's by a new one.
|
|
418
|
+
const taken = new Set();
|
|
419
|
+
const collect = (node) => {
|
|
420
|
+
if ("tool" in node)
|
|
421
|
+
taken.add(node.tool.name);
|
|
422
|
+
for (const child of childrenOf(node))
|
|
423
|
+
collect(child);
|
|
424
|
+
};
|
|
425
|
+
collect(harness.root);
|
|
426
|
+
const tools = new Map();
|
|
427
|
+
const writeTool = (name, file) => {
|
|
428
|
+
const known = tools.get(name);
|
|
429
|
+
if (known !== undefined) {
|
|
430
|
+
if (known !== canonical(file))
|
|
431
|
+
note(`Two tool stages are named ${name}, and tools/${name}.json holds one: the first is written for both.`);
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
tools.set(name, canonical(file));
|
|
435
|
+
writer.files.set(`${at}tools/${name}.json`, json(file));
|
|
436
|
+
const http = file.http;
|
|
437
|
+
if (isObject(http) && typeof http.credential === "string")
|
|
438
|
+
useCredential(writer, http.credential, { type: "bearer", url: String(http.url) });
|
|
439
|
+
};
|
|
440
|
+
const verifierName = () => {
|
|
441
|
+
let name = "verify";
|
|
442
|
+
for (let index = 2; taken.has(name); index++)
|
|
443
|
+
name = `verify_${index}`;
|
|
444
|
+
taken.add(name);
|
|
445
|
+
return name;
|
|
446
|
+
};
|
|
447
|
+
const used = [];
|
|
448
|
+
const named = (id) => {
|
|
449
|
+
if (!used.includes(id))
|
|
450
|
+
used.push(id);
|
|
451
|
+
};
|
|
452
|
+
const withId = (node, built) => (node.id === undefined ? built : { id: node.id, ...built });
|
|
453
|
+
const tree = (node) => {
|
|
454
|
+
if ("agent" in node) {
|
|
455
|
+
named(node.agent);
|
|
456
|
+
return withId(node, { agent: node.agent });
|
|
457
|
+
}
|
|
458
|
+
if ("tool" in node) {
|
|
459
|
+
const { name, ...stage } = node.tool;
|
|
460
|
+
const file = {};
|
|
461
|
+
if (stage.description !== undefined)
|
|
462
|
+
file.description = stage.description;
|
|
463
|
+
if (stage.inputSchema === undefined)
|
|
464
|
+
note(`Tool stage ${name} has no inputSchema, which tools/${name}.json needs: it is written as { "type": "object" }.`);
|
|
465
|
+
file.inputSchema = stage.inputSchema ?? { type: "object" };
|
|
466
|
+
if (stage.outputSchema !== undefined)
|
|
467
|
+
file.outputSchema = stage.outputSchema;
|
|
468
|
+
if (stage.http === undefined)
|
|
469
|
+
note(`Tool stage ${name} has no http, which a folder's tool needs: tools/${name}.json does not compile until it has one.`);
|
|
470
|
+
else
|
|
471
|
+
file.http = stage.http;
|
|
472
|
+
writeTool(name, file);
|
|
473
|
+
return withId(node, { tool: name });
|
|
474
|
+
}
|
|
475
|
+
if ("chain" in node)
|
|
476
|
+
return withId(node, { chain: node.chain.map(tree) });
|
|
477
|
+
if ("switch" in node) {
|
|
478
|
+
const cases = Object.fromEntries(Object.entries(node.switch.cases).map(([key, item]) => [key, tree(item)]));
|
|
479
|
+
return withId(node, { switch: { cases, ...(node.switch.default === undefined ? {} : { default: tree(node.switch.default) }) } });
|
|
480
|
+
}
|
|
481
|
+
if ("parallel" in node)
|
|
482
|
+
return withId(node, { parallel: Object.fromEntries(Object.entries(node.parallel).map(([key, item]) => [key, tree(item)])) });
|
|
483
|
+
if ("map" in node)
|
|
484
|
+
return withId(node, { map: { each: tree(node.map.each) } });
|
|
485
|
+
const { verify, max } = node.loop;
|
|
486
|
+
const run = tree(node.loop.run);
|
|
487
|
+
let verifier;
|
|
488
|
+
if ("agent" in verify) {
|
|
489
|
+
named(verify.agent);
|
|
490
|
+
verifier = { agent: verify.agent, ...(verify.id === undefined ? {} : { id: verify.id }) };
|
|
491
|
+
}
|
|
492
|
+
else {
|
|
493
|
+
const name = verifierName();
|
|
494
|
+
writeTool(name, { description: "Judges each attempt of the loop.", inputSchema: { type: "object" }, http: verify.http });
|
|
495
|
+
verifier = { tool: name };
|
|
496
|
+
}
|
|
497
|
+
return withId(node, { loop: { run, verify: verifier, max } });
|
|
498
|
+
};
|
|
499
|
+
writer.files.set(`${at}${FLOW_FILE}`, json(tree(harness.root)));
|
|
500
|
+
writer.files.set(`${at}${AGENT_FILE}`, `---\n${frontmatter(front)}---\n`);
|
|
501
|
+
for (const id of Object.keys(harness.agents))
|
|
502
|
+
if (!used.includes(id))
|
|
503
|
+
note(`Agent ${id} is embedded, and no node names it: a folder holds only the agents its flows name, so it is left out.`);
|
|
504
|
+
for (const id of used) {
|
|
505
|
+
const agent = harness.agents[id];
|
|
506
|
+
if (agent === undefined) {
|
|
507
|
+
note(`A node names agent ${id}, which the flow does not embed: ${names.dir}${id}/ is not written.`);
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
const known = names.members.get(id);
|
|
511
|
+
if (known !== undefined) {
|
|
512
|
+
if (known !== canonical(agent))
|
|
513
|
+
note(`It embeds an agent ${id} that differs from the one in ${names.dir}${id}/, and a flow root's agents/ holds one agent of an id: the one there is used.`);
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
names.members.set(id, canonical(agent));
|
|
517
|
+
writeEmbedded(writer, { prefix: `${names.dir}${id}/`, role: "stage" }, id, agent, names);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
/** The nodes directly inside `node`, with a loop's verifier agent. */
|
|
521
|
+
function childrenOf(node) {
|
|
522
|
+
if ("chain" in node)
|
|
523
|
+
return [...node.chain];
|
|
524
|
+
if ("switch" in node)
|
|
525
|
+
return [...Object.values(node.switch.cases), ...(node.switch.default ? [node.switch.default] : [])];
|
|
526
|
+
if ("parallel" in node)
|
|
527
|
+
return Object.values(node.parallel);
|
|
528
|
+
if ("map" in node)
|
|
529
|
+
return [node.map.each];
|
|
530
|
+
if ("loop" in node)
|
|
531
|
+
return [node.loop.run];
|
|
532
|
+
return [];
|
|
533
|
+
}
|
|
534
|
+
/** `runtime.json`, `triggers/` and `files/`: the uploaded folder's, without what it derives. */
|
|
535
|
+
function writeRuntime(writer, manifest) {
|
|
536
|
+
const { files, notes, secrets } = writer;
|
|
537
|
+
const runtime = manifest.runtime;
|
|
538
|
+
const rest = runtime === undefined ? undefined : { ...runtime };
|
|
539
|
+
let moved = false;
|
|
540
|
+
// --- Triggers ----------------------------------------------------------------------------
|
|
541
|
+
if (rest && isObject(runtime.triggers)) {
|
|
542
|
+
const kept = {};
|
|
543
|
+
const folded = new Set();
|
|
544
|
+
for (const [id, trigger] of Object.entries(runtime.triggers)) {
|
|
545
|
+
const asFile = Object.keys(trigger).every((key) => TRIGGER_FILE_KEYS.includes(key)) &&
|
|
546
|
+
keepsProse(trigger.prompt) &&
|
|
547
|
+
!folded.has(id.toLowerCase());
|
|
548
|
+
if (!asFile) {
|
|
549
|
+
kept[id] = trigger;
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
folded.add(id.toLowerCase());
|
|
553
|
+
const { prompt, ...settings } = trigger;
|
|
554
|
+
files.set(`triggers/${id}.md`, `---\n${frontmatter(settings)}---\n${prompt}\n`);
|
|
555
|
+
moved = true;
|
|
556
|
+
}
|
|
557
|
+
if (Object.keys(kept).length > 0)
|
|
558
|
+
rest.triggers = kept;
|
|
559
|
+
else if (moved)
|
|
560
|
+
delete rest.triggers;
|
|
561
|
+
}
|
|
562
|
+
// --- Secret slots the compiler derives ---------------------------------------------------
|
|
563
|
+
for (const [credential, places] of writer.uses) {
|
|
564
|
+
const slot = secrets[credential];
|
|
565
|
+
if (slot === undefined) {
|
|
566
|
+
notes.push(`${credential} has no slot in runtime.secrets: the folder derives one for ${places[0].url}.`);
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
const first = places[0];
|
|
570
|
+
const agree = places.every((place) => place.type === first.type && place.url === first.url);
|
|
571
|
+
if (agree && same(slot, derivedSlot(first)) && rest && isObject(rest.secrets)) {
|
|
572
|
+
const { [credential]: _, ...others } = rest.secrets;
|
|
573
|
+
rest.secrets = others;
|
|
574
|
+
moved = true;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
if (rest && isObject(rest.secrets) && Object.keys(rest.secrets).length === 0 && Object.keys(secrets).length > 0)
|
|
578
|
+
delete rest.secrets;
|
|
579
|
+
// --- files/ ------------------------------------------------------------------------------
|
|
580
|
+
if (rest && Array.isArray(rest.files) && rest.files.length > 0) {
|
|
581
|
+
const last = rest.files.at(-1);
|
|
582
|
+
const seeded = isObject(last.files) ? last.files : undefined;
|
|
583
|
+
if (last.path === WORKSPACE &&
|
|
584
|
+
Object.keys(last).length === 2 &&
|
|
585
|
+
seeded &&
|
|
586
|
+
Object.values(seeded).every((hash) => writer.bytesOf(hash) !== undefined)) {
|
|
587
|
+
for (const [path, hash] of Object.entries(seeded))
|
|
588
|
+
files.set(`files/${path}`, writer.bytesOf(hash));
|
|
589
|
+
rest.files = rest.files.slice(0, -1);
|
|
590
|
+
if (rest.files.length === 0)
|
|
591
|
+
delete rest.files;
|
|
592
|
+
moved = true;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
// --- runtime.json ------------------------------------------------------------------------
|
|
596
|
+
// The compiler makes kind `none` itself for a folder with derived slots, triggers or files/.
|
|
597
|
+
if (rest && !(moved && same(rest, { kind: "none" })))
|
|
598
|
+
files.set("runtime.json", json(rest));
|
|
599
|
+
}
|
|
600
|
+
function isWorkflow(value) {
|
|
601
|
+
return isObject(value) && value.kind === "workflow";
|
|
602
|
+
}
|
|
603
|
+
/** A `SKILL.md` without its frontmatter `secrets` key, or undefined when it has none. */
|
|
604
|
+
function withoutSkillSecrets(bytes) {
|
|
605
|
+
let text;
|
|
606
|
+
try {
|
|
607
|
+
text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
608
|
+
}
|
|
609
|
+
catch {
|
|
610
|
+
return undefined;
|
|
611
|
+
}
|
|
612
|
+
const parsed = parseMarkdown(text);
|
|
613
|
+
if (!parsed.ok || !parsed.hasFrontmatter || !("secrets" in parsed.data))
|
|
614
|
+
return undefined;
|
|
615
|
+
const { secrets: _, ...data } = parsed.data;
|
|
616
|
+
return `---\n${frontmatter(data)}---\n${parsed.body}`;
|
|
617
|
+
}
|
|
618
|
+
/** Compiles the folder and compares: a note for each way it differs that no note explains yet. */
|
|
619
|
+
function verify(manifest, files, notes) {
|
|
620
|
+
const encoder = new TextEncoder();
|
|
621
|
+
const entries = [];
|
|
622
|
+
const bytes = new Map();
|
|
623
|
+
for (const [path, content] of files) {
|
|
624
|
+
const data = typeof content === "string" ? encoder.encode(content) : content;
|
|
625
|
+
entries.push({ path, size: data.byteLength, sha256: bytesToHex(sha256(data)) });
|
|
626
|
+
bytes.set(path, data);
|
|
627
|
+
}
|
|
628
|
+
const compiled = compileAgentFolder({
|
|
629
|
+
entries,
|
|
630
|
+
files: new Map(folderTextPaths(entries).map((path) => [path, bytes.get(path)])),
|
|
631
|
+
name: manifest.name,
|
|
632
|
+
});
|
|
633
|
+
if (!compiled.manifest) {
|
|
634
|
+
for (const item of compiled.diagnostics.filter((diagnostic) => diagnostic.severity === "error"))
|
|
635
|
+
notes.push(`The folder does not compile: ${item.file ? `${item.file}: ` : ""}${item.message}`);
|
|
636
|
+
return false;
|
|
637
|
+
}
|
|
638
|
+
if (hashManifest(compiled.manifest) === hashManifest(manifest))
|
|
639
|
+
return true;
|
|
640
|
+
if (notes.length === 0) {
|
|
641
|
+
const where = firstDifference(manifest, compiled.manifest);
|
|
642
|
+
notes.push(`The folder compiles to a different manifest${where ? `, first at ${where}` : ""}.`);
|
|
643
|
+
}
|
|
644
|
+
return false;
|
|
645
|
+
}
|
|
646
|
+
/** The first path where two JSON values differ, as `harness.tools.0.http`. */
|
|
647
|
+
function firstDifference(a, b, path = []) {
|
|
648
|
+
if (same(a, b))
|
|
649
|
+
return undefined;
|
|
650
|
+
if (isObject(a) && isObject(b)) {
|
|
651
|
+
for (const key of [...new Set([...Object.keys(a), ...Object.keys(b)])].sort())
|
|
652
|
+
if (!same(a[key], b[key]))
|
|
653
|
+
return firstDifference(a[key], b[key], [...path, key]) ?? [...path, key].join(".");
|
|
654
|
+
}
|
|
655
|
+
if (Array.isArray(a) && Array.isArray(b) && a.length === b.length)
|
|
656
|
+
for (let index = 0; index < a.length; index++)
|
|
657
|
+
if (!same(a[index], b[index]))
|
|
658
|
+
return firstDifference(a[index], b[index], [...path, String(index)]);
|
|
659
|
+
return path.join(".") || undefined;
|
|
660
|
+
}
|