@nylorun/core 0.26.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.
@@ -0,0 +1,18 @@
1
+ import { type Located, type Position } from "./json.js";
2
+ export type ParsedMarkdown = {
3
+ readonly ok: true;
4
+ /** The frontmatter as plain data: `{}` when the file has none. */
5
+ readonly data: Record<string, unknown>;
6
+ /** Whether the file opened with a frontmatter block. */
7
+ readonly hasFrontmatter: boolean;
8
+ readonly body: string;
9
+ /** The line the body starts on. */
10
+ readonly bodyLine: number;
11
+ readonly positions: ReadonlyMap<string, Located>;
12
+ } | {
13
+ readonly ok: false;
14
+ readonly message: string;
15
+ readonly at: Position;
16
+ };
17
+ /** Splits `text` into its frontmatter and body, and parses the frontmatter. */
18
+ export declare function parseMarkdown(text: string): ParsedMarkdown;
@@ -0,0 +1,87 @@
1
+ /**
2
+ * A Markdown file's frontmatter, as an agent folder reads it: YAML 1.2 under the core schema,
3
+ * with no anchors, aliases, tags or merge keys, and a key twice in one map an error. It holds a
4
+ * few settings, so the restriction costs authors nothing, and it rules out alias bombs and
5
+ * YAML 1.1's surprises. Each value and key is found by its JSON pointer, with its line and column
6
+ * in the whole file.
7
+ */
8
+ import { isAlias, isMap, isPair, isScalar, isSeq, LineCounter, parseDocument, visit } from "yaml";
9
+ import { pointerOf } from "./json.js";
10
+ const FENCE = /^---[ \t]*$/;
11
+ /** Splits `text` into its frontmatter and body, and parses the frontmatter. */
12
+ export function parseMarkdown(text) {
13
+ const lines = text.split("\n");
14
+ if (!FENCE.test((lines[0] ?? "").replace(/\r$/, "")))
15
+ return { ok: true, data: {}, hasFrontmatter: false, body: text, bodyLine: 1, positions: new Map() };
16
+ const end = lines.findIndex((line, index) => index > 0 && FENCE.test(line.replace(/\r$/, "")));
17
+ if (end < 0)
18
+ return { ok: false, message: "The frontmatter opened with --- is not closed by a line of ---", at: { line: 1, column: 1 } };
19
+ const yamlText = lines.slice(1, end).join("\n");
20
+ const body = lines.slice(end + 1).join("\n");
21
+ // Positions in the YAML count from the line after the opening fence.
22
+ const shift = (position) => ({ line: position.line + 1, column: position.col });
23
+ const counter = new LineCounter();
24
+ const document = parseDocument(yamlText, {
25
+ lineCounter: counter,
26
+ schema: "core",
27
+ version: "1.2",
28
+ uniqueKeys: true,
29
+ merge: false,
30
+ prettyErrors: false,
31
+ });
32
+ const at = (node) => shift(counter.linePos(node?.range?.[0] ?? 0));
33
+ const first = document.errors[0];
34
+ if (first) {
35
+ const position = first.linePos?.[0] ? shift(first.linePos[0]) : { line: 2, column: 1 };
36
+ const raw = first.message.split("\n")[0].replace(/ at line \d+, column \d+:?$/, "");
37
+ const alias = first.code === "BAD_ALIAS" || /alias/i.test(raw);
38
+ const message = first.code === "DUPLICATE_KEY"
39
+ ? "A key appears twice in the frontmatter"
40
+ : alias
41
+ ? `${raw}: a value that starts with * or & must be quoted, such as schedule: "0 2 * * *"`
42
+ : raw;
43
+ return { ok: false, message: `The frontmatter is not valid YAML: ${message}`, at: position };
44
+ }
45
+ let refusal;
46
+ visit(document, {
47
+ Alias(_, node) {
48
+ refusal ??= { message: "Frontmatter takes no aliases (*name): write the value out", at: at(node) };
49
+ return visit.BREAK;
50
+ },
51
+ Node(_, node) {
52
+ if (node.anchor)
53
+ refusal ??= { message: "Frontmatter takes no anchors (&name)", at: at(node) };
54
+ else if (node.tag)
55
+ refusal ??= { message: `Frontmatter takes no tags (${node.tag})`, at: at(node) };
56
+ return refusal ? visit.BREAK : undefined;
57
+ },
58
+ Pair(_, pair) {
59
+ if (isScalar(pair.key) && pair.key.value === "<<")
60
+ refusal ??= { message: "Frontmatter takes no merge keys (<<)", at: at(pair.key) };
61
+ return refusal ? visit.BREAK : undefined;
62
+ },
63
+ });
64
+ if (refusal)
65
+ return { ok: false, ...refusal };
66
+ const contents = document.contents;
67
+ if (contents !== null && !isMap(contents))
68
+ return { ok: false, message: "The frontmatter is a map of keys to values", at: at(contents) };
69
+ const positions = new Map();
70
+ const walk = (node, path, key) => {
71
+ if (node === null || node === undefined || isAlias(node))
72
+ return;
73
+ positions.set(pointerOf(path), key ? { value: at(node), key } : { value: at(node) });
74
+ if (isMap(node))
75
+ for (const item of node.items) {
76
+ if (!isPair(item) || !isScalar(item.key))
77
+ continue;
78
+ const name = String(item.key.value);
79
+ walk(item.value ?? item.key, [...path, name], at(item.key));
80
+ }
81
+ else if (isSeq(node))
82
+ node.items.forEach((item, index) => walk(item, [...path, index]));
83
+ };
84
+ walk(contents, []);
85
+ const data = (contents === null ? {} : document.toJS({ maxAliasCount: 0 }));
86
+ return { ok: true, data: data ?? {}, hasFrontmatter: true, body, bodyLine: end + 2, positions };
87
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * `@nylorun/core/folder`: agent folders (track T6, D79). An agent written as a folder of
3
+ * Markdown and JSON, compiled by `compileAgentFolder` into the agent manifest v8 the Runtime
4
+ * runs. The Runtime compiles every folder it is sent with this code, and the CLI checks one
5
+ * offline with the same code.
6
+ */
7
+ export { AGENT_FOLDER_COMPILER, AGENT_FOLDER_LIMITS, compileAgentFolder, folderTextPaths, type AgentFolderDiagnostic, type AgentFolderEntry, type CompileAgentFolderInput, type CompiledAgentFolder, } from "./compile.js";
@@ -0,0 +1,7 @@
1
+ /**
2
+ * `@nylorun/core/folder`: agent folders (track T6, D79). An agent written as a folder of
3
+ * Markdown and JSON, compiled by `compileAgentFolder` into the agent manifest v8 the Runtime
4
+ * runs. The Runtime compiles every folder it is sent with this code, and the CLI checks one
5
+ * offline with the same code.
6
+ */
7
+ export { AGENT_FOLDER_COMPILER, AGENT_FOLDER_LIMITS, compileAgentFolder, folderTextPaths, } from "./compile.js";
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Strict JSON (RFC 8259) with positions, for an agent folder's JSON files: no comments, no
3
+ * trailing commas, and a key twice in one object is an error. Each value and object key is
4
+ * found by its JSON pointer, so a diagnostic names the line and column of what it is about.
5
+ */
6
+ /** A 1-based line and column. */
7
+ export interface Position {
8
+ readonly line: number;
9
+ readonly column: number;
10
+ }
11
+ /** Where a parsed value starts, and for an object member where its key starts. */
12
+ export interface Located {
13
+ readonly value: Position;
14
+ readonly key?: Position;
15
+ }
16
+ export type ParsedJson = {
17
+ readonly ok: true;
18
+ readonly value: unknown;
19
+ readonly positions: ReadonlyMap<string, Located>;
20
+ } | {
21
+ readonly ok: false;
22
+ readonly message: string;
23
+ readonly at: Position;
24
+ };
25
+ /** The pointer of `path` (RFC 6901): `""` for the whole document. */
26
+ export declare function pointerOf(path: readonly PropertyKey[]): string;
27
+ /** Parses `text` as strict JSON, keeping the position of every value and key. */
28
+ export declare function parseJson(text: string): ParsedJson;
@@ -0,0 +1,183 @@
1
+ /**
2
+ * Strict JSON (RFC 8259) with positions, for an agent folder's JSON files: no comments, no
3
+ * trailing commas, and a key twice in one object is an error. Each value and object key is
4
+ * found by its JSON pointer, so a diagnostic names the line and column of what it is about.
5
+ */
6
+ /** The pointer of `path` (RFC 6901): `""` for the whole document. */
7
+ export function pointerOf(path) {
8
+ return path.map((part) => `/${String(part).replace(/~/g, "~0").replace(/\//g, "~1")}`).join("");
9
+ }
10
+ class JsonError extends Error {
11
+ at;
12
+ constructor(message, at) {
13
+ super(message);
14
+ this.at = at;
15
+ }
16
+ }
17
+ /** Parses `text` as strict JSON, keeping the position of every value and key. */
18
+ export function parseJson(text) {
19
+ const positions = new Map();
20
+ let index = 0;
21
+ let line = 1;
22
+ let lineStart = 0;
23
+ const here = () => ({ line, column: index - lineStart + 1 });
24
+ const fail = (message, at = here()) => {
25
+ throw new JsonError(message, at);
26
+ };
27
+ const space = () => {
28
+ for (;;) {
29
+ const char = text[index];
30
+ if (char === "\n") {
31
+ index++;
32
+ line++;
33
+ lineStart = index;
34
+ }
35
+ else if (char === " " || char === "\t" || char === "\r")
36
+ index++;
37
+ else if (char === "/" && (text[index + 1] === "/" || text[index + 1] === "*"))
38
+ fail("Comments are not JSON: explain a value in its description field, or in README.md");
39
+ else
40
+ return;
41
+ }
42
+ };
43
+ const literal = (word, value) => {
44
+ if (text.startsWith(word, index)) {
45
+ index += word.length;
46
+ return value;
47
+ }
48
+ return fail(`Unexpected ${describe(text[index])}`);
49
+ };
50
+ const string = () => {
51
+ let out = "";
52
+ index++; // the opening quote
53
+ for (;;) {
54
+ const char = text[index];
55
+ if (char === undefined)
56
+ fail("A string is not closed");
57
+ if (char === '"') {
58
+ index++;
59
+ return out;
60
+ }
61
+ if (char === "\n" || char < " ")
62
+ fail("A string holds a control character; write it as an escape such as \\n");
63
+ if (char !== "\\") {
64
+ out += char;
65
+ index++;
66
+ continue;
67
+ }
68
+ const escape = text[index + 1];
69
+ const simple = { '"': '"', "\\": "\\", "/": "/", b: "\b", f: "\f", n: "\n", r: "\r", t: "\t" };
70
+ if (escape !== undefined && escape in simple) {
71
+ out += simple[escape];
72
+ index += 2;
73
+ }
74
+ else if (escape === "u" && /^[0-9a-fA-F]{4}$/.test(text.slice(index + 2, index + 6))) {
75
+ out += String.fromCharCode(Number.parseInt(text.slice(index + 2, index + 6), 16));
76
+ index += 6;
77
+ }
78
+ else
79
+ fail(`Unknown escape \\${escape ?? ""}`);
80
+ }
81
+ };
82
+ const number = () => {
83
+ const match = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/.exec(text.slice(index));
84
+ if (!match)
85
+ return fail(`Unexpected ${describe(text[index])}`);
86
+ index += match[0].length;
87
+ return Number(match[0]);
88
+ };
89
+ const value = (path, key) => {
90
+ space();
91
+ const start = here();
92
+ positions.set(pointerOf(path), key ? { value: start, key } : { value: start });
93
+ const char = text[index];
94
+ if (char === "{") {
95
+ index++;
96
+ const object = {};
97
+ space();
98
+ if (text[index] === "}") {
99
+ index++;
100
+ return object;
101
+ }
102
+ for (;;) {
103
+ space();
104
+ if (text[index] !== '"')
105
+ fail(text[index] === "}" ? "A trailing comma is not JSON" : "An object's key is a string in double quotes");
106
+ const keyAt = here();
107
+ const name = string();
108
+ if (Object.hasOwn(object, name))
109
+ fail(`The key "${name}" appears twice in one object`, keyAt);
110
+ space();
111
+ if (text[index] !== ":")
112
+ fail(`Expected : after the key "${name}"`);
113
+ index++;
114
+ object[name] = value([...path, name], keyAt);
115
+ space();
116
+ if (text[index] === ",") {
117
+ index++;
118
+ continue;
119
+ }
120
+ if (text[index] === "}") {
121
+ index++;
122
+ return object;
123
+ }
124
+ fail(`Expected , or } after the value of "${name}"`);
125
+ }
126
+ }
127
+ if (char === "[") {
128
+ index++;
129
+ const array = [];
130
+ space();
131
+ if (text[index] === "]") {
132
+ index++;
133
+ return array;
134
+ }
135
+ for (;;) {
136
+ array.push(value([...path, array.length]));
137
+ space();
138
+ if (text[index] === ",") {
139
+ index++;
140
+ space();
141
+ if (text[index] === "]")
142
+ fail("A trailing comma is not JSON");
143
+ continue;
144
+ }
145
+ if (text[index] === "]") {
146
+ index++;
147
+ return array;
148
+ }
149
+ fail("Expected , or ] in an array");
150
+ }
151
+ }
152
+ if (char === '"')
153
+ return string();
154
+ if (char === "t")
155
+ return literal("true", true);
156
+ if (char === "f")
157
+ return literal("false", false);
158
+ if (char === "n")
159
+ return literal("null", null);
160
+ if (char === "-" || (char !== undefined && char >= "0" && char <= "9"))
161
+ return number();
162
+ return fail(char === undefined ? "The file ends before a value" : `Unexpected ${describe(char)}`);
163
+ };
164
+ try {
165
+ const parsed = value([]);
166
+ space();
167
+ if (index < text.length)
168
+ fail(`Unexpected ${describe(text[index])} after the value`);
169
+ return { ok: true, value: parsed, positions };
170
+ }
171
+ catch (error) {
172
+ if (error instanceof JsonError)
173
+ return { ok: false, message: error.message, at: error.at };
174
+ throw error;
175
+ }
176
+ }
177
+ function describe(char) {
178
+ if (char === undefined)
179
+ return "end of file";
180
+ if (char === "'")
181
+ return "' (JSON strings use double quotes)";
182
+ return JSON.stringify(char);
183
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nylorun/core",
3
- "version": "0.26.0-beta",
3
+ "version": "0.27.0-beta",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "description": "Shared agent definitions and wire contracts",
@@ -30,7 +30,8 @@
30
30
  "prepack": "npm run build"
31
31
  },
32
32
  "dependencies": {
33
- "@noble/hashes": "^2.4.0"
33
+ "@noble/hashes": "^2.4.0",
34
+ "yaml": "^2.9.1"
34
35
  },
35
36
  "devDependencies": {
36
37
  "@types/node": "^26.6.5",
@@ -71,6 +72,10 @@
71
72
  "types": "./dist/manifest-schemas.d.ts",
72
73
  "import": "./dist/manifest-schemas.js"
73
74
  },
75
+ "./folder": {
76
+ "types": "./dist/folder/index.d.ts",
77
+ "import": "./dist/folder/index.js"
78
+ },
74
79
  "./schemas/agent-manifest.json": "./dist/schemas/agent-manifest.schema.json",
75
80
  "./schemas/harness-manifest.json": "./dist/schemas/harness-manifest.schema.json",
76
81
  "./schemas/runtime-manifest.json": "./dist/schemas/runtime-manifest.schema.json"