@nylorun/core 0.27.0-beta → 0.28.0-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +47 -0
- package/README.md +3 -1
- package/dist/contracts.d.ts +153 -1
- package/dist/contracts.js +62 -7
- package/dist/define.d.ts +2 -1
- package/dist/define.js +2 -1
- package/dist/definition/cron.d.ts +15 -0
- package/dist/definition/cron.js +155 -0
- package/dist/definition/sections.d.ts +11 -0
- package/dist/definition/sections.js +29 -20
- package/dist/folder/compile.d.ts +43 -5
- package/dist/folder/compile.js +499 -88
- package/dist/folder/context.d.ts +97 -0
- package/dist/folder/context.js +79 -0
- package/dist/folder/decompile.d.ts +18 -0
- package/dist/folder/decompile.js +660 -0
- package/dist/folder/fix.d.ts +22 -0
- package/dist/folder/fix.js +191 -0
- package/dist/folder/flow.d.ts +42 -0
- package/dist/folder/flow.js +215 -0
- package/dist/folder/frontmatter.d.ts +5 -2
- package/dist/folder/frontmatter.js +8 -4
- package/dist/folder/index.d.ts +7 -2
- package/dist/folder/index.js +6 -1
- package/dist/folder/json-schema.d.ts +15 -0
- package/dist/folder/json-schema.js +212 -0
- package/dist/folder/nested.d.ts +24 -0
- package/dist/folder/nested.js +378 -0
- package/dist/folder/plugins.d.ts +23 -0
- package/dist/folder/plugins.js +155 -0
- package/dist/folder/schemas.d.ts +8 -0
- package/dist/folder/schemas.js +238 -0
- package/dist/folder/suggest.d.ts +13 -0
- package/dist/folder/suggest.js +63 -0
- package/dist/schemas/agent-folder/agent-md.schema.json +143 -0
- package/dist/schemas/agent-folder/flow.schema.json +466 -0
- package/dist/schemas/agent-folder/mcp.schema.json +59 -0
- package/dist/schemas/agent-folder/runtime.schema.json +447 -0
- package/dist/schemas/agent-folder/tool.schema.json +77 -0
- package/dist/schemas/agent-folder/trigger-md.schema.json +65 -0
- package/dist/utils/runtime-limits.d.ts +8 -0
- package/dist/utils/runtime-limits.js +19 -0
- package/package.json +8 -2
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { AgentFolderFix } from "../contracts.js";
|
|
2
|
+
/** What applying a folder's fix-its did: the new text of each file changed, and each change. */
|
|
3
|
+
export interface AppliedFolderFixes {
|
|
4
|
+
/** The new text of each file a fix changed, by path. */
|
|
5
|
+
readonly files: ReadonlyMap<string, string>;
|
|
6
|
+
/** Each change made, by file, as a person reads it. */
|
|
7
|
+
readonly changes: readonly {
|
|
8
|
+
readonly file: string;
|
|
9
|
+
readonly change: string;
|
|
10
|
+
}[];
|
|
11
|
+
/** Each fix not made, and why. */
|
|
12
|
+
readonly skipped: readonly {
|
|
13
|
+
readonly fix: AgentFolderFix;
|
|
14
|
+
readonly reason: string;
|
|
15
|
+
}[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Applies `fixes` to `files`, the text of each file they name (with `AGENT.md` for a
|
|
19
|
+
* `move_skill_secrets`). Line ends first, then renames, then moved secrets, so every position is
|
|
20
|
+
* still the one the compiler gave. A fix given twice is made once.
|
|
21
|
+
*/
|
|
22
|
+
export declare function applyFolderFixes(files: ReadonlyMap<string, string>, fixes: readonly AgentFolderFix[]): AppliedFolderFixes;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The compiler's fix-its, applied to text (`nylorun check --fix`, track T7): each diagnostic's
|
|
3
|
+
* `fix` (`AgentFolderFix`) is one edit, and this makes it, touching nothing else. Like the
|
|
4
|
+
* compiler it is pure: it is given the text of each file a fix names, and answers the new text
|
|
5
|
+
* of each it changed.
|
|
6
|
+
*
|
|
7
|
+
* - `crlf`: every CRLF line end becomes LF.
|
|
8
|
+
* - `rename_key`: the key token at the line and column, a JSON string or a YAML key (plain or
|
|
9
|
+
* quoted), is renamed; every other byte stays, quotes and spacing included.
|
|
10
|
+
* - `move_skill_secrets`: the `secrets` lines leave the `SKILL.md`'s frontmatter, and
|
|
11
|
+
* `AGENT.md`'s frontmatter gets `skills: { <skill>: { secrets } }` written as `migrate folder`
|
|
12
|
+
* writes it, in the `skills` block (or the skill's) that is there, else as a new one at its end.
|
|
13
|
+
*
|
|
14
|
+
* A fix whose text is not where it says (the file changed since it was checked), or that would
|
|
15
|
+
* need more than its own lines rewritten (a frontmatter in flow style), is skipped, with why.
|
|
16
|
+
*/
|
|
17
|
+
import { isMap, isPair, isScalar, LineCounter, parseDocument, stringify } from "yaml";
|
|
18
|
+
const AGENT_FILE = "AGENT.md";
|
|
19
|
+
const FENCE = /^---[ \t]*$/;
|
|
20
|
+
const BOM = "";
|
|
21
|
+
/** How `migrate folder` writes frontmatter (`decompile.ts`). */
|
|
22
|
+
const YAML_OUT = { version: "1.2", schema: "core", aliasDuplicateObjects: false, lineWidth: 0 };
|
|
23
|
+
/**
|
|
24
|
+
* Applies `fixes` to `files`, the text of each file they name (with `AGENT.md` for a
|
|
25
|
+
* `move_skill_secrets`). Line ends first, then renames, then moved secrets, so every position is
|
|
26
|
+
* still the one the compiler gave. A fix given twice is made once.
|
|
27
|
+
*/
|
|
28
|
+
export function applyFolderFixes(files, fixes) {
|
|
29
|
+
const texts = new Map();
|
|
30
|
+
const changes = [];
|
|
31
|
+
const skipped = [];
|
|
32
|
+
const seen = new Set();
|
|
33
|
+
const unique = fixes.filter((fix) => {
|
|
34
|
+
const key = JSON.stringify(fix);
|
|
35
|
+
return seen.has(key) ? false : (seen.add(key), true);
|
|
36
|
+
});
|
|
37
|
+
const textOf = (path) => texts.get(path) ?? files.get(path);
|
|
38
|
+
const order = { crlf: 0, rename_key: 1, move_skill_secrets: 2 };
|
|
39
|
+
// Renames on one line from its end, so a rename leaves the columns before it as they were.
|
|
40
|
+
const sorted = [...unique].sort((a, b) => order[a.kind] - order[b.kind] ||
|
|
41
|
+
(a.kind === "rename_key" && b.kind === "rename_key" ? (a.file < b.file ? -1 : a.file > b.file ? 1 : b.line - a.line || b.column - a.column) : 0));
|
|
42
|
+
for (const fix of sorted) {
|
|
43
|
+
const text = textOf(fix.file);
|
|
44
|
+
if (text === undefined) {
|
|
45
|
+
skipped.push({ fix, reason: `${fix.file} was not read` });
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (fix.kind === "crlf") {
|
|
49
|
+
if (!text.includes("\r\n"))
|
|
50
|
+
continue;
|
|
51
|
+
texts.set(fix.file, text.replace(/\r\n/g, "\n"));
|
|
52
|
+
changes.push({ file: fix.file, change: "CRLF line ends made LF" });
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (fix.kind === "rename_key") {
|
|
56
|
+
const renamed = renameKey(text, fix);
|
|
57
|
+
if (typeof renamed !== "string")
|
|
58
|
+
skipped.push({ fix, reason: renamed.reason });
|
|
59
|
+
else {
|
|
60
|
+
texts.set(fix.file, renamed);
|
|
61
|
+
changes.push({ file: fix.file, change: `renamed '${fix.from}' to '${fix.to}' at line ${fix.line}` });
|
|
62
|
+
}
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const agent = textOf(AGENT_FILE);
|
|
66
|
+
if (agent === undefined) {
|
|
67
|
+
skipped.push({ fix, reason: `${AGENT_FILE} was not read` });
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const moved = moveSkillSecrets(text, agent, fix);
|
|
71
|
+
if ("reason" in moved) {
|
|
72
|
+
skipped.push({ fix, reason: moved.reason });
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
texts.set(fix.file, moved.skill);
|
|
76
|
+
changes.push({ file: fix.file, change: `secrets moved to ${AGENT_FILE}'s skills block` });
|
|
77
|
+
if (moved.agent !== agent) {
|
|
78
|
+
texts.set(AGENT_FILE, moved.agent);
|
|
79
|
+
changes.push({ file: AGENT_FILE, change: `skills.${fix.skill}.secrets: ${fix.secrets.join(", ")}, from ${fix.file}` });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const changed = new Map([...texts].filter(([path, text]) => text !== files.get(path)));
|
|
83
|
+
return { files: changed, changes, skipped };
|
|
84
|
+
}
|
|
85
|
+
/** `text` with the key at the fix's line and column renamed, or why it cannot be. */
|
|
86
|
+
function renameKey(source, fix) {
|
|
87
|
+
const bom = source.startsWith(BOM) ? BOM : "";
|
|
88
|
+
const lines = source.slice(bom.length).split("\n");
|
|
89
|
+
const line = lines[fix.line - 1];
|
|
90
|
+
const at = fix.column - 1;
|
|
91
|
+
const stale = { reason: `'${fix.from}' is no longer at line ${fix.line}, column ${fix.column}: check again` };
|
|
92
|
+
if (line === undefined)
|
|
93
|
+
return stale;
|
|
94
|
+
const tokens = fix.file.endsWith(".json")
|
|
95
|
+
? [[JSON.stringify(fix.from), JSON.stringify(fix.to)]]
|
|
96
|
+
: [
|
|
97
|
+
[JSON.stringify(fix.from), JSON.stringify(fix.to)],
|
|
98
|
+
[`'${fix.from.replace(/'/g, "''")}'`, `'${fix.to.replace(/'/g, "''")}'`],
|
|
99
|
+
[fix.from, fix.to],
|
|
100
|
+
];
|
|
101
|
+
for (const [from, to] of tokens) {
|
|
102
|
+
if (!line.startsWith(from, at))
|
|
103
|
+
continue;
|
|
104
|
+
// A plain YAML key ends at its colon, or the space before it.
|
|
105
|
+
if (from === fix.from && !/^[ \t]*:/.test(line.slice(at + from.length)))
|
|
106
|
+
continue;
|
|
107
|
+
lines[fix.line - 1] = line.slice(0, at) + to + line.slice(at + from.length);
|
|
108
|
+
return bom + lines.join("\n");
|
|
109
|
+
}
|
|
110
|
+
return stale;
|
|
111
|
+
}
|
|
112
|
+
function frontmatterOf(source, file) {
|
|
113
|
+
const bom = source.startsWith(BOM) ? BOM : "";
|
|
114
|
+
const lines = source.slice(bom.length).split("\n");
|
|
115
|
+
const end = FENCE.test(lines[0]?.replace(/\r$/, "") ?? "") ? lines.findIndex((line, index) => index > 0 && FENCE.test(line.replace(/\r$/, ""))) : -1;
|
|
116
|
+
if (end < 0)
|
|
117
|
+
return { reason: `${file} has no frontmatter between --- lines` };
|
|
118
|
+
const yaml = lines.slice(1, end).join("\n");
|
|
119
|
+
const counter = new LineCounter();
|
|
120
|
+
const document = parseDocument(yaml, { lineCounter: counter, version: "1.2", schema: "core", uniqueKeys: true, merge: false });
|
|
121
|
+
if (document.errors.length > 0)
|
|
122
|
+
return { reason: `${file}'s frontmatter is not valid YAML` };
|
|
123
|
+
const contents = document.contents;
|
|
124
|
+
if (contents !== null && (!isMap(contents) || contents.flow))
|
|
125
|
+
return { reason: `${file}'s frontmatter is not a block of key: value lines` };
|
|
126
|
+
return { bom, lines, end, yaml, map: contents, counter };
|
|
127
|
+
}
|
|
128
|
+
/** The pair of `map` with key `name`. */
|
|
129
|
+
function pairOf(map, name) {
|
|
130
|
+
return map?.items.find((item) => isPair(item) && isScalar(item.key) && item.key.value === name);
|
|
131
|
+
}
|
|
132
|
+
/** The 0-based line, in the frontmatter's YAML, of the last character a pair's value holds. */
|
|
133
|
+
function lastLine(front, pair) {
|
|
134
|
+
const key = pair.key;
|
|
135
|
+
const value = pair.value?.range;
|
|
136
|
+
const end = Math.max(key.range[1], front.yaml.slice(0, value?.[1] ?? 0).trimEnd().length);
|
|
137
|
+
return front.counter.linePos(end - 1).line - 1;
|
|
138
|
+
}
|
|
139
|
+
/** A pair's 0-based line and column in the frontmatter's YAML. */
|
|
140
|
+
function startOf(front, node) {
|
|
141
|
+
const position = front.counter.linePos(node.range[0]);
|
|
142
|
+
return { line: position.line - 1, column: position.col - 1 };
|
|
143
|
+
}
|
|
144
|
+
/** `value` as `migrate folder` writes it, each line indented by `indent`. */
|
|
145
|
+
const block = (value, indent) => stringify(value, YAML_OUT)
|
|
146
|
+
.replace(/\n$/, "")
|
|
147
|
+
.split("\n")
|
|
148
|
+
.map((line) => indent + line);
|
|
149
|
+
/** The `SKILL.md` without its `secrets`, and `AGENT.md` with them, or why they cannot move. */
|
|
150
|
+
function moveSkillSecrets(skillText, agentText, fix) {
|
|
151
|
+
const skill = frontmatterOf(skillText, fix.file);
|
|
152
|
+
if ("reason" in skill)
|
|
153
|
+
return skill;
|
|
154
|
+
const secrets = pairOf(skill.map, "secrets");
|
|
155
|
+
const listed = secrets?.value?.toJSON?.();
|
|
156
|
+
if (!secrets || !Array.isArray(listed) || listed.length !== fix.secrets.length || !fix.secrets.every((name) => listed.includes(name)))
|
|
157
|
+
return { reason: `${fix.file}'s secrets are not the ones checked: check again` };
|
|
158
|
+
const from = startOf(skill, secrets.key);
|
|
159
|
+
if (from.column !== 0)
|
|
160
|
+
return { reason: `${fix.file}'s secrets is not a line of its own` };
|
|
161
|
+
const skillLines = [...skill.lines];
|
|
162
|
+
skillLines.splice(from.line + 1, lastLine(skill, secrets) - from.line + 1);
|
|
163
|
+
const agent = frontmatterOf(agentText, AGENT_FILE);
|
|
164
|
+
if ("reason" in agent)
|
|
165
|
+
return agent;
|
|
166
|
+
const agentLines = [...agent.lines];
|
|
167
|
+
const done = (lines) => ({ skill: skill.bom + skillLines.join("\n"), agent: agent.bom + lines.join("\n") });
|
|
168
|
+
const insert = (after, lines) => {
|
|
169
|
+
agentLines.splice(after + 1, 0, ...lines);
|
|
170
|
+
return done(agentLines);
|
|
171
|
+
};
|
|
172
|
+
const settings = { secrets: fix.secrets };
|
|
173
|
+
const skills = pairOf(agent.map, "skills");
|
|
174
|
+
if (!skills)
|
|
175
|
+
return insert(agent.end - 1, block({ skills: { [fix.skill]: settings } }, ""));
|
|
176
|
+
const where = { reason: `${AGENT_FILE}'s skills is not a block of key: value lines: add skills: { ${fix.skill}: { secrets: [...] } } by hand` };
|
|
177
|
+
if (!isMap(skills.value) || skills.value.flow || skills.value.items.length === 0)
|
|
178
|
+
return where;
|
|
179
|
+
const own = pairOf(skills.value, fix.skill);
|
|
180
|
+
if (!own) {
|
|
181
|
+
const indent = " ".repeat(startOf(agent, skills.value.items[0].key).column);
|
|
182
|
+
return insert(lastLine(agent, skills) + 1, block({ [fix.skill]: settings }, indent));
|
|
183
|
+
}
|
|
184
|
+
if (!isMap(own.value) || own.value.flow || own.value.items.length === 0)
|
|
185
|
+
return where;
|
|
186
|
+
const present = pairOf(own.value, "secrets")?.value?.toJSON?.();
|
|
187
|
+
if (Array.isArray(present))
|
|
188
|
+
return fix.secrets.every((name) => present.includes(name)) ? done(agentLines) : where;
|
|
189
|
+
const indent = " ".repeat(startOf(agent, own.value.items[0].key).column);
|
|
190
|
+
return insert(lastLine(agent, own) + 1, block(settings, indent));
|
|
191
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `flow.json` (track T7, design §3.8): workflow manifest v3's node tree, as a folder writes it.
|
|
3
|
+
* The node names are v3's (`agent`, `tool`, `chain`, `switch`, `parallel`, `map`, `loop`, and
|
|
4
|
+
* `id` on any node), so a v3 tree pastes in. Two leaves differ, because a folder names things
|
|
5
|
+
* instead of embedding them:
|
|
6
|
+
*
|
|
7
|
+
* - `{ "agent": "<id>" }` names a folder of the flow root's `agents/`. The compiler embeds its
|
|
8
|
+
* manifest where v3 wants it: a sub-flow's agents in the sub-flow, not in the root.
|
|
9
|
+
* - `{ "tool": "<name>" }` names `tools/<name>.json` beside that `flow.json`, and a loop's
|
|
10
|
+
* `verify` takes `{ "agent" }` or `{ "tool" }`, the folder form of `{ "http" }`.
|
|
11
|
+
*
|
|
12
|
+
* Every problem is reported with its place in `flow.json`; translating goes on past it, so one
|
|
13
|
+
* pass names them all.
|
|
14
|
+
*/
|
|
15
|
+
import { type EmbeddedAgent, type ReportOptions } from "./context.js";
|
|
16
|
+
export declare const NODE_KINDS: readonly ["agent", "tool", "chain", "switch", "parallel", "map", "loop"];
|
|
17
|
+
/** The keys of `switch`, `map`, `loop` and a loop's `verify`: `schemas.ts` builds `flow.json`'s JSON Schema from them. */
|
|
18
|
+
export declare const SWITCH_KEYS: readonly ["cases", "default"];
|
|
19
|
+
export declare const MAP_KEYS: readonly ["each"];
|
|
20
|
+
export declare const LOOP_KEYS: readonly ["run", "verify", "max"];
|
|
21
|
+
export declare const VERIFY_KEYS: readonly ["agent", "tool", "id"];
|
|
22
|
+
/** What a flow's leaves name: the flow root's agents and the tools beside this `flow.json`. */
|
|
23
|
+
export interface FlowNames {
|
|
24
|
+
readonly agentIds: readonly string[];
|
|
25
|
+
agent(id: string): EmbeddedAgent | "unknown" | "cycle" | "failed";
|
|
26
|
+
/** The agents used, by id: unused ones are a warning at the flow root. */
|
|
27
|
+
readonly used: Set<string>;
|
|
28
|
+
readonly tools: ReadonlyMap<string, Record<string, unknown>>;
|
|
29
|
+
}
|
|
30
|
+
export interface TranslatedFlow {
|
|
31
|
+
/** The v3 node tree; undefined when a node has an error. */
|
|
32
|
+
readonly root?: Record<string, unknown>;
|
|
33
|
+
/** The agents it names, embedded in the order the tree first names them. */
|
|
34
|
+
readonly agents: Record<string, EmbeddedAgent>;
|
|
35
|
+
readonly toolsUsed: ReadonlySet<string>;
|
|
36
|
+
/** False when an agent it names did not compile: its errors are reported where it is. */
|
|
37
|
+
readonly complete: boolean;
|
|
38
|
+
}
|
|
39
|
+
type FlowReport = (code: string, message: string, file: string, path: readonly PropertyKey[], options?: ReportOptions) => void;
|
|
40
|
+
/** Translates `flow.json`'s node tree into v3's, embedding the agents and tools it names. */
|
|
41
|
+
export declare function translateFlow(flow: Record<string, unknown>, names: FlowNames, report: FlowReport, suggest: (name: string, known: readonly string[]) => string | undefined): TranslatedFlow;
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `flow.json` (track T7, design §3.8): workflow manifest v3's node tree, as a folder writes it.
|
|
3
|
+
* The node names are v3's (`agent`, `tool`, `chain`, `switch`, `parallel`, `map`, `loop`, and
|
|
4
|
+
* `id` on any node), so a v3 tree pastes in. Two leaves differ, because a folder names things
|
|
5
|
+
* instead of embedding them:
|
|
6
|
+
*
|
|
7
|
+
* - `{ "agent": "<id>" }` names a folder of the flow root's `agents/`. The compiler embeds its
|
|
8
|
+
* manifest where v3 wants it: a sub-flow's agents in the sub-flow, not in the root.
|
|
9
|
+
* - `{ "tool": "<name>" }` names `tools/<name>.json` beside that `flow.json`, and a loop's
|
|
10
|
+
* `verify` takes `{ "agent" }` or `{ "tool" }`, the folder form of `{ "http" }`.
|
|
11
|
+
*
|
|
12
|
+
* Every problem is reported with its place in `flow.json`; translating goes on past it, so one
|
|
13
|
+
* pass names them all.
|
|
14
|
+
*/
|
|
15
|
+
import { isObject } from "./context.js";
|
|
16
|
+
export const NODE_KINDS = ["agent", "tool", "chain", "switch", "parallel", "map", "loop"];
|
|
17
|
+
const NODE_KEYS = [...NODE_KINDS, "id"];
|
|
18
|
+
/** The keys of `switch`, `map`, `loop` and a loop's `verify`: `schemas.ts` builds `flow.json`'s JSON Schema from them. */
|
|
19
|
+
export const SWITCH_KEYS = ["cases", "default"];
|
|
20
|
+
export const MAP_KEYS = ["each"];
|
|
21
|
+
export const LOOP_KEYS = ["run", "verify", "max"];
|
|
22
|
+
export const VERIFY_KEYS = ["agent", "tool", "id"];
|
|
23
|
+
const FLOW_FILE = "flow.json";
|
|
24
|
+
/** Translates `flow.json`'s node tree into v3's, embedding the agents and tools it names. */
|
|
25
|
+
export function translateFlow(flow, names, report, suggest) {
|
|
26
|
+
const agents = {};
|
|
27
|
+
const toolsUsed = new Set();
|
|
28
|
+
let complete = true;
|
|
29
|
+
let valid = true;
|
|
30
|
+
const fail = (code, message, path, options) => {
|
|
31
|
+
valid = false;
|
|
32
|
+
report(code, message, FLOW_FILE, path, options);
|
|
33
|
+
};
|
|
34
|
+
const close = (name, known) => {
|
|
35
|
+
const found = suggest(name, known);
|
|
36
|
+
return found ? ` Did you mean '${found}'?` : "";
|
|
37
|
+
};
|
|
38
|
+
const checkKeys = (value, known, path) => {
|
|
39
|
+
for (const key of Object.keys(value))
|
|
40
|
+
if (!known.includes(key)) {
|
|
41
|
+
const found = suggest(key, known);
|
|
42
|
+
fail("unknown_key", `Unknown key '${key}'.${found ? ` Did you mean '${found}'?` : ` Known keys: ${known.join(", ")}.`}`, [...path, key], { key: true, rename: known });
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
/** `{ agent }`: the agent's id, with its manifest embedded. */
|
|
46
|
+
const agentLeaf = (value, path) => {
|
|
47
|
+
if (typeof value !== "string" || value === "") {
|
|
48
|
+
fail("flow_invalid", "agent names a folder of the flow root's agents/ by its id", path);
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
names.used.add(value);
|
|
52
|
+
const found = names.agent(value);
|
|
53
|
+
if (found === "unknown") {
|
|
54
|
+
fail("agent_unknown", `No agent ${value} is in the flow root's agents/.${close(value, names.agentIds)}`, path);
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
if (found === "cycle") {
|
|
58
|
+
fail("flow_cycle", `${value} is a sub-flow that runs itself through this node: a flow cannot contain itself`, path);
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
if (found === "failed") {
|
|
62
|
+
complete = false;
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
agents[value] ??= found;
|
|
66
|
+
return value;
|
|
67
|
+
};
|
|
68
|
+
/** `{ tool }`: the tool file beside this flow.json. */
|
|
69
|
+
const toolLeaf = (value, path) => {
|
|
70
|
+
if (isObject(value)) {
|
|
71
|
+
fail("flow_invalid", 'A flow names a tool by its file: write "tool": "<name>" and keep the tool in tools/<name>.json beside this flow.json', path);
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
if (typeof value !== "string" || value === "") {
|
|
75
|
+
fail("flow_invalid", "tool names a file of tools/ beside this flow.json, without .json", path);
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
const tool = names.tools.get(value);
|
|
79
|
+
if (!tool) {
|
|
80
|
+
fail("tool_unknown", `No tool ${value} is in tools/ beside this flow.json (tools/${value}.json).${close(value, [...names.tools.keys()])}`, path);
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
toolsUsed.add(value);
|
|
84
|
+
if (tool.approval !== undefined) {
|
|
85
|
+
valid = false;
|
|
86
|
+
report("flow_invalid", "A flow's tool takes no approval: approvals are the Tenant's tool policy", `tools/${value}.json`, ["approval"], { key: true });
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
return tool;
|
|
90
|
+
};
|
|
91
|
+
const node = (value, path) => {
|
|
92
|
+
if (!isObject(value)) {
|
|
93
|
+
fail("flow_invalid", `A node is an object with one of ${NODE_KINDS.join(", ")}, and an optional id`, path);
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
checkKeys(value, NODE_KEYS, path);
|
|
97
|
+
const kinds = NODE_KINDS.filter((kind) => kind in value);
|
|
98
|
+
if (kinds.length !== 1) {
|
|
99
|
+
fail("flow_invalid", kinds.length === 0
|
|
100
|
+
? `A node has one of ${NODE_KINDS.join(", ")}`
|
|
101
|
+
: `A node has one of ${NODE_KINDS.join(", ")}; this one has ${kinds.join(" and ")}: nest one inside the other, as in a chain`, path);
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
const id = value.id;
|
|
105
|
+
if (id !== undefined && (typeof id !== "string" || id === ""))
|
|
106
|
+
fail("flow_invalid", "id is a non-empty string", [...path, "id"]);
|
|
107
|
+
const withId = (built) => built === undefined ? undefined : { ...built, ...(typeof id === "string" && id !== "" ? { id } : {}) };
|
|
108
|
+
const kind = kinds[0];
|
|
109
|
+
const at = [...path, kind];
|
|
110
|
+
const body = value[kind];
|
|
111
|
+
switch (kind) {
|
|
112
|
+
case "agent": {
|
|
113
|
+
const agent = agentLeaf(body, at);
|
|
114
|
+
return withId(agent === undefined ? undefined : { agent });
|
|
115
|
+
}
|
|
116
|
+
case "tool": {
|
|
117
|
+
const tool = toolLeaf(body, at);
|
|
118
|
+
return withId(tool === undefined ? undefined : { tool: stageTool(tool) });
|
|
119
|
+
}
|
|
120
|
+
case "chain": {
|
|
121
|
+
if (!Array.isArray(body) || body.length === 0) {
|
|
122
|
+
fail("flow_invalid", "chain is a list of one or more nodes, run in order", at);
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
const chain = body.map((item, index) => node(item, [...at, index]));
|
|
126
|
+
return withId(chain.every((item) => item !== undefined) ? { chain } : undefined);
|
|
127
|
+
}
|
|
128
|
+
case "switch": {
|
|
129
|
+
if (!isObject(body)) {
|
|
130
|
+
fail("flow_invalid", "switch is { cases: { <route>: <node> }, default?: <node> }", at);
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
checkKeys(body, SWITCH_KEYS, at);
|
|
134
|
+
if (!isObject(body.cases)) {
|
|
135
|
+
fail("flow_invalid", "cases maps each route to the node it runs", body.cases === undefined ? at : [...at, "cases"]);
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
const cases = Object.fromEntries(Object.entries(body.cases).map(([key, item]) => [key, node(item, [...at, "cases", key])]));
|
|
139
|
+
const fallback = body.default === undefined ? undefined : node(body.default, [...at, "default"]);
|
|
140
|
+
if (Object.values(cases).some((item) => item === undefined) || (body.default !== undefined && fallback === undefined))
|
|
141
|
+
return undefined;
|
|
142
|
+
return withId({ switch: { cases, ...(fallback === undefined ? {} : { default: fallback }) } });
|
|
143
|
+
}
|
|
144
|
+
case "parallel": {
|
|
145
|
+
if (!isObject(body) || Object.keys(body).length === 0) {
|
|
146
|
+
fail("flow_invalid", "parallel maps each branch's name to the node it runs, with at least one branch", at);
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
const branches = Object.fromEntries(Object.entries(body).map(([key, item]) => [key, node(item, [...at, key])]));
|
|
150
|
+
return withId(Object.values(branches).every((item) => item !== undefined) ? { parallel: branches } : undefined);
|
|
151
|
+
}
|
|
152
|
+
case "map": {
|
|
153
|
+
if (!isObject(body) || body.each === undefined) {
|
|
154
|
+
fail("flow_invalid", "map is { each: <node> }: the node runs once per item of its input", at);
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
checkKeys(body, MAP_KEYS, at);
|
|
158
|
+
const each = node(body.each, [...at, "each"]);
|
|
159
|
+
return withId(each === undefined ? undefined : { map: { each } });
|
|
160
|
+
}
|
|
161
|
+
case "loop": {
|
|
162
|
+
if (!isObject(body)) {
|
|
163
|
+
fail("flow_invalid", "loop is { run: <node>, verify: { agent } or { tool }, max: <attempts> }", at);
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
checkKeys(body, LOOP_KEYS, at);
|
|
167
|
+
const run = body.run === undefined ? undefined : node(body.run, [...at, "run"]);
|
|
168
|
+
if (body.run === undefined)
|
|
169
|
+
fail("flow_invalid", "loop needs run: the node each attempt runs", at);
|
|
170
|
+
const verify = verifier(body.verify, [...at, "verify"]);
|
|
171
|
+
if (typeof body.max !== "number" || !Number.isInteger(body.max) || body.max < 1) {
|
|
172
|
+
fail("flow_invalid", "max is the most attempts the loop makes: a whole number, 1 or more", body.max === undefined ? at : [...at, "max"]);
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
if (run === undefined || verify === undefined)
|
|
176
|
+
return undefined;
|
|
177
|
+
return withId({ loop: { run, verify, max: body.max } });
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
/** A loop's `verify`: `{ agent, id? }`, or `{ tool }` for an HTTP verifier (`{ http }` in v3). */
|
|
182
|
+
const verifier = (value, path) => {
|
|
183
|
+
if (!isObject(value)) {
|
|
184
|
+
fail("flow_invalid", "verify is { agent: <id> } or { tool: <name> }: what judges each attempt", path);
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
if ("http" in value) {
|
|
188
|
+
fail("flow_invalid", 'An HTTP verifier is a tool file: write "verify": { "tool": "<name>" } with tools/<name>.json beside this flow.json', [...path, "http"], { key: true });
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
191
|
+
checkKeys(value, VERIFY_KEYS, path);
|
|
192
|
+
if (("agent" in value) === ("tool" in value)) {
|
|
193
|
+
fail("flow_invalid", "verify has exactly one of agent and tool", path);
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
if ("tool" in value) {
|
|
197
|
+
if (value.id !== undefined)
|
|
198
|
+
fail("flow_invalid", "An HTTP verifier takes no id", [...path, "id"], { key: true });
|
|
199
|
+
const tool = toolLeaf(value.tool, [...path, "tool"]);
|
|
200
|
+
return tool === undefined || value.id !== undefined ? undefined : { http: tool.http };
|
|
201
|
+
}
|
|
202
|
+
const id = value.id;
|
|
203
|
+
if (id !== undefined && (typeof id !== "string" || id === ""))
|
|
204
|
+
fail("flow_invalid", "id is a non-empty string", [...path, "id"]);
|
|
205
|
+
const agent = agentLeaf(value.agent, [...path, "agent"]);
|
|
206
|
+
return agent === undefined ? undefined : { agent, ...(typeof id === "string" && id !== "" ? { id } : {}) };
|
|
207
|
+
};
|
|
208
|
+
const root = node(flow, []);
|
|
209
|
+
return { ...(valid && root !== undefined ? { root } : {}), agents, toolsUsed, complete };
|
|
210
|
+
}
|
|
211
|
+
/** An HTTP tool, compiled from its file, as a v3 stage: its name, schemas and request. */
|
|
212
|
+
function stageTool(tool) {
|
|
213
|
+
const { approval: _, ...stage } = tool;
|
|
214
|
+
return stage;
|
|
215
|
+
}
|
|
@@ -14,5 +14,8 @@ export type ParsedMarkdown = {
|
|
|
14
14
|
readonly message: string;
|
|
15
15
|
readonly at: Position;
|
|
16
16
|
};
|
|
17
|
-
/**
|
|
18
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Splits `text` into its frontmatter and body, and parses the frontmatter. CRLF line ends (a
|
|
19
|
+
* Windows checkout) are read as LF first, so lines and columns are the same either way.
|
|
20
|
+
*/
|
|
21
|
+
export declare function parseMarkdown(source: string): ParsedMarkdown;
|
|
@@ -8,12 +8,16 @@
|
|
|
8
8
|
import { isAlias, isMap, isPair, isScalar, isSeq, LineCounter, parseDocument, visit } from "yaml";
|
|
9
9
|
import { pointerOf } from "./json.js";
|
|
10
10
|
const FENCE = /^---[ \t]*$/;
|
|
11
|
-
/**
|
|
12
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Splits `text` into its frontmatter and body, and parses the frontmatter. CRLF line ends (a
|
|
13
|
+
* Windows checkout) are read as LF first, so lines and columns are the same either way.
|
|
14
|
+
*/
|
|
15
|
+
export function parseMarkdown(source) {
|
|
16
|
+
const text = source.replace(/\r\n/g, "\n");
|
|
13
17
|
const lines = text.split("\n");
|
|
14
|
-
if (!FENCE.test(
|
|
18
|
+
if (!FENCE.test(lines[0] ?? ""))
|
|
15
19
|
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
|
|
20
|
+
const end = lines.findIndex((line, index) => index > 0 && FENCE.test(line));
|
|
17
21
|
if (end < 0)
|
|
18
22
|
return { ok: false, message: "The frontmatter opened with --- is not closed by a line of ---", at: { line: 1, column: 1 } };
|
|
19
23
|
const yamlText = lines.slice(1, end).join("\n");
|
package/dist/folder/index.d.ts
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
* `@nylorun/core/folder`: agent folders (track T6, D79). An agent written as a folder of
|
|
3
3
|
* Markdown and JSON, compiled by `compileAgentFolder` into the agent manifest v8 the Runtime
|
|
4
4
|
* runs. The Runtime compiles every folder it is sent with this code, and the CLI checks one
|
|
5
|
-
* offline with the same code.
|
|
5
|
+
* offline with the same code. `folderOfManifest` writes the folder of a saved manifest,
|
|
6
|
+
* `agentFolderJsonSchemas` gives the JSON Schema of each kind of file in a folder, for editors, and
|
|
7
|
+
* `applyFolderFixes` makes the fix-its its diagnostics carry (track T7).
|
|
6
8
|
*/
|
|
7
|
-
export { AGENT_FOLDER_COMPILER, AGENT_FOLDER_LIMITS, compileAgentFolder, folderTextPaths, type AgentFolderDiagnostic, type AgentFolderEntry, type CompileAgentFolderInput, type CompiledAgentFolder, } from "./compile.js";
|
|
9
|
+
export { AGENT_FOLDER_COMPILER, AGENT_FOLDER_LIMITS, compileAgentFolder, folderTextPaths, type AgentFolderDiagnostic, type AgentFolderEntry, type AgentFolderFix, type AgentFolderLimits, type CompileAgentFolderInput, type CompiledAgentFolder, } from "./compile.js";
|
|
10
|
+
export { folderOfManifest, type FolderOfManifest, type FolderOfManifestOptions } from "./decompile.js";
|
|
11
|
+
export { AGENT_FOLDER_SCHEMA_KINDS, agentFolderJsonSchemas, type AgentFolderSchemaKind } from "./schemas.js";
|
|
12
|
+
export { applyFolderFixes, type AppliedFolderFixes } from "./fix.js";
|
package/dist/folder/index.js
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
* `@nylorun/core/folder`: agent folders (track T6, D79). An agent written as a folder of
|
|
3
3
|
* Markdown and JSON, compiled by `compileAgentFolder` into the agent manifest v8 the Runtime
|
|
4
4
|
* runs. The Runtime compiles every folder it is sent with this code, and the CLI checks one
|
|
5
|
-
* offline with the same code.
|
|
5
|
+
* offline with the same code. `folderOfManifest` writes the folder of a saved manifest,
|
|
6
|
+
* `agentFolderJsonSchemas` gives the JSON Schema of each kind of file in a folder, for editors, and
|
|
7
|
+
* `applyFolderFixes` makes the fix-its its diagnostics carry (track T7).
|
|
6
8
|
*/
|
|
7
9
|
export { AGENT_FOLDER_COMPILER, AGENT_FOLDER_LIMITS, compileAgentFolder, folderTextPaths, } from "./compile.js";
|
|
10
|
+
export { folderOfManifest } from "./decompile.js";
|
|
11
|
+
export { AGENT_FOLDER_SCHEMA_KINDS, agentFolderJsonSchemas } from "./schemas.js";
|
|
12
|
+
export { applyFolderFixes } from "./fix.js";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One problem in a schema: `path` is the pointer in the schema, `key` positions it at the key, and
|
|
3
|
+
* `known` is what an unknown keyword may be meant as, for the compiler's rename fix-it.
|
|
4
|
+
*/
|
|
5
|
+
export interface JsonSchemaIssue {
|
|
6
|
+
readonly severity: "error" | "warning";
|
|
7
|
+
readonly path: readonly PropertyKey[];
|
|
8
|
+
readonly message: string;
|
|
9
|
+
readonly key?: boolean;
|
|
10
|
+
readonly known?: readonly string[];
|
|
11
|
+
}
|
|
12
|
+
/** JSON Schema's seven types. */
|
|
13
|
+
export declare const JSON_SCHEMA_TYPES: readonly ["null", "boolean", "object", "array", "number", "string", "integer"];
|
|
14
|
+
/** Every problem in `schema`, a JSON Schema at the root of a field or file. */
|
|
15
|
+
export declare function jsonSchemaIssues(schema: unknown): JsonSchemaIssue[];
|