@pluginfinity/engine 0.1.1 → 0.2.1
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/agents.js +24 -11
- package/body.js +116 -40
- package/component.js +14 -1
- package/doctor.js +1 -1
- package/emit.js +18 -3
- package/errors.js +90 -8
- package/frontmatter.js +72 -19
- package/hook-lib.generated.js +9 -0
- package/hook-lib.js +25 -0
- package/hooks.js +10 -5
- package/index.d.ts +356 -101
- package/index.js +6 -3
- package/manifest.js +7 -5
- package/notes.js +29 -0
- package/operations.js +200 -23
- package/package.json +5 -5
- package/server-lib.generated.js +9 -0
- package/server-lib.js +21 -0
- package/servers.js +256 -0
- package/skills.js +45 -14
- package/tokens.js +238 -0
- package/version.js +1 -1
package/skills.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mapHostBlocks } from "./body.js";
|
|
2
|
+
import { ComponentsInvalid } from "./errors.js";
|
|
2
3
|
import { appendSections, mapFrontmatter } from "./frontmatter.js";
|
|
3
|
-
import { decodeComponent, frontmatterText, invalid, isJunk, issue, overlayIssues, toLf, unknownTargets } from "./component.js";
|
|
4
|
+
import { decodeComponent, frontmatterText, invalid, isJunk, issue, lineIssues, overlayIssues, toLf, unknownTargets } from "./component.js";
|
|
5
|
+
import { renderTokens } from "./tokens.js";
|
|
4
6
|
import { ComponentName, SKILL_FIELDS, SkillFrontmatter } from "@pluginfinity/core";
|
|
5
7
|
import { Effect, FileSystem, Path, Schema } from "effect";
|
|
6
8
|
|
|
@@ -72,18 +74,23 @@ const readSkills = (root, known) => Effect.gen(function* () {
|
|
|
72
74
|
});
|
|
73
75
|
/**
|
|
74
76
|
* Render one skill for a target, or `undefined` when its `targets` block
|
|
75
|
-
* excludes it: `SKILL.md` with the target's frontmatter
|
|
76
|
-
* applied, `.md` support files with host blocks
|
|
77
|
-
* file copied. Every file keeps its source mode.
|
|
77
|
+
* excludes it: `SKILL.md` with the target's frontmatter, then host blocks
|
|
78
|
+
* and body tokens applied, `.md` support files with host blocks and tokens
|
|
79
|
+
* applied, and every other file copied. Every file keeps its source mode.
|
|
80
|
+
*
|
|
81
|
+
* @remarks
|
|
82
|
+
* Fails with one `ComponentInvalid` when one file has problems, and with a
|
|
83
|
+
* `ComponentsInvalid` holding one per file when several do. A token problem
|
|
84
|
+
* is keyed by its file line and names the target.
|
|
78
85
|
*
|
|
79
86
|
* @public
|
|
80
87
|
*/
|
|
81
|
-
const renderSkill = (target, id, skill, known) => Effect.gen(function* () {
|
|
88
|
+
const renderSkill = (target, id, skill, known, tokens) => Effect.gen(function* () {
|
|
82
89
|
const fs = yield* FileSystem.FileSystem;
|
|
83
90
|
const path = yield* Path.Path;
|
|
84
91
|
const block = skill.frontmatter.targets?.[id];
|
|
85
92
|
if (block === false) return void 0;
|
|
86
|
-
const mapped = mapFrontmatter(target, target.skills.fields, target.skills.hostFields, skill.frontmatter, block ?? {});
|
|
93
|
+
const mapped = mapFrontmatter(target, target.skills.fields, target.skills.hostFields, skill.frontmatter, block ?? {}, tokens.own);
|
|
87
94
|
const problems = [
|
|
88
95
|
...yield* overlayIssues(SkillFrontmatter, SKILL_FIELDS, skill.frontmatter, block ?? {}, id),
|
|
89
96
|
...mapped.unresolved.map((field) => issue(field.field, `${id} leaves this field unresolved (${field.note})`)),
|
|
@@ -91,9 +98,11 @@ const renderSkill = (target, id, skill, known) => Effect.gen(function* () {
|
|
|
91
98
|
];
|
|
92
99
|
const { name: _name, description, ...rest } = mapped.fields;
|
|
93
100
|
if (typeof description === "string" && description.length > 1024) problems.push(issue("description", `is ${description.length} characters on ${id}, over the ${SKILL_DESCRIPTION_MAX} limit; set a shorter targets.${id}.description`));
|
|
94
|
-
const body =
|
|
101
|
+
const body = mapHostBlocks(skill.body, id, known);
|
|
95
102
|
if ("problem" in body) return yield* Effect.fail(invalid(skill.path, [issue(`line ${body.problem.line + skill.bodyOffset}`, body.problem.message)]));
|
|
96
|
-
|
|
103
|
+
const rendered = renderTokens(body.text, tokens);
|
|
104
|
+
if ("problems" in rendered) problems.push(...lineIssues(rendered.problems, body.lines, skill.bodyOffset));
|
|
105
|
+
const failures = problems.length > 0 ? [invalid(skill.path, problems, id)] : [];
|
|
97
106
|
const yaml = yield* frontmatterText({
|
|
98
107
|
name: skill.name,
|
|
99
108
|
description,
|
|
@@ -103,7 +112,7 @@ const renderSkill = (target, id, skill, known) => Effect.gen(function* () {
|
|
|
103
112
|
const out = `${target.skills.dir}/${skill.name}`;
|
|
104
113
|
const files = [{
|
|
105
114
|
path: `${out}/SKILL.md`,
|
|
106
|
-
content: `---\n${yaml}---\n${appendSections("text" in
|
|
115
|
+
content: `---\n${yaml}---\n${appendSections("text" in rendered ? rendered.text : "", mapped.sections)}`,
|
|
107
116
|
mode: (yield* fs.stat(skill.path)).mode & 511
|
|
108
117
|
}];
|
|
109
118
|
for (const file of skill.files) {
|
|
@@ -117,15 +126,37 @@ const renderSkill = (target, id, skill, known) => Effect.gen(function* () {
|
|
|
117
126
|
});
|
|
118
127
|
continue;
|
|
119
128
|
}
|
|
120
|
-
const processed =
|
|
121
|
-
if ("problem" in processed)
|
|
129
|
+
const processed = mapHostBlocks(toLf(yield* fs.readFileString(absolute)), id, known);
|
|
130
|
+
if ("problem" in processed) {
|
|
131
|
+
failures.push(invalid(absolute, [issue(`line ${processed.problem.line}`, processed.problem.message)]));
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const tokened = renderTokens(processed.text, tokens);
|
|
135
|
+
if ("problems" in tokened) {
|
|
136
|
+
failures.push(invalid(absolute, lineIssues(tokened.problems, processed.lines, 0), id));
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
122
139
|
files.push({
|
|
123
140
|
path: `${out}/${file}`,
|
|
124
|
-
content:
|
|
141
|
+
content: tokened.text,
|
|
125
142
|
mode
|
|
126
143
|
});
|
|
127
144
|
}
|
|
128
|
-
|
|
145
|
+
const [only, ...more] = failures;
|
|
146
|
+
if (only !== void 0) return yield* Effect.fail(more.length === 0 ? only : new ComponentsInvalid({
|
|
147
|
+
path: dir,
|
|
148
|
+
components: failures
|
|
149
|
+
}));
|
|
150
|
+
const source = `skills/${skill.name}/SKILL.md`;
|
|
151
|
+
return {
|
|
152
|
+
files,
|
|
153
|
+
notes: mapped.drops.map(({ field, kind }) => ({
|
|
154
|
+
target: id,
|
|
155
|
+
path: source,
|
|
156
|
+
kind,
|
|
157
|
+
name: field
|
|
158
|
+
}))
|
|
159
|
+
};
|
|
129
160
|
});
|
|
130
161
|
|
|
131
162
|
//#endregion
|
package/tokens.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { fencedLines, inlineCodeSpans } from "./body.js";
|
|
2
|
+
import { splitOwnMcp } from "./frontmatter.js";
|
|
3
|
+
|
|
4
|
+
//#region src/tokens.ts
|
|
5
|
+
const KINDS = /* @__PURE__ */ new Set([
|
|
6
|
+
"tool",
|
|
7
|
+
"agent",
|
|
8
|
+
"skill",
|
|
9
|
+
"plugin_root"
|
|
10
|
+
]);
|
|
11
|
+
/** The first word after `{{`: up to whitespace or a brace. */
|
|
12
|
+
const KIND = /^\s*([^\s{}]+)/;
|
|
13
|
+
const LINK = /(!?)\[([^[\]]*)\]\(\s*<?pluginfinity:\/\/([^\s<>()]*)>?\s*\)/g;
|
|
14
|
+
/** Every `pluginfinity://` occurrence, in any case. */
|
|
15
|
+
const SCHEMES = /pluginfinity:\/\//gi;
|
|
16
|
+
const SCHEME = /pluginfinity:\/\//i;
|
|
17
|
+
/** How far a quoted stray occurrence reaches either side of its start, so each problem is cheap to build. */
|
|
18
|
+
const WINDOW = 80;
|
|
19
|
+
/** How many stray occurrences on one line are reported individually. */
|
|
20
|
+
const STRAY_LIMIT = 3;
|
|
21
|
+
const isSpace = (char) => char === void 0 || /\s/.test(char);
|
|
22
|
+
/** The run of non-whitespace around position `at`, clipped to a window, to quote a stray occurrence. */
|
|
23
|
+
const around = (line, at) => {
|
|
24
|
+
const floor = Math.max(0, at - WINDOW);
|
|
25
|
+
let start = at;
|
|
26
|
+
while (start > floor && !isSpace(line[start - 1])) start -= 1;
|
|
27
|
+
const ceiling = Math.min(line.length, at + WINDOW);
|
|
28
|
+
let end = at;
|
|
29
|
+
while (end < ceiling && !isSpace(line[end])) end += 1;
|
|
30
|
+
const clippedStart = start === floor && start > 0 && !isSpace(line[start - 1]);
|
|
31
|
+
const clippedEnd = end === ceiling && end < line.length && !isSpace(line[end]);
|
|
32
|
+
return `${clippedStart ? "…" : ""}${line.slice(start, end)}${clippedEnd ? "…" : ""}`;
|
|
33
|
+
};
|
|
34
|
+
const article = (word) => /^[aeiou]/.test(word) ? `an ${word}` : `a ${word}`;
|
|
35
|
+
const fill = (template, values) => template.replace(/\{(\w+)\}/g, (whole, key) => values[key] ?? whole);
|
|
36
|
+
const spelling = (value, what) => typeof value === "string" ? { value } : { problem: `${what} has no spelling on this target: ${value.note}` };
|
|
37
|
+
const tool = (name, raw, ctx) => {
|
|
38
|
+
const runtime = ctx.target.tools.runtime;
|
|
39
|
+
const listed = Object.hasOwn(runtime.names, name) ? runtime.names[name] : void 0;
|
|
40
|
+
if (listed !== void 0) return spelling(listed, raw);
|
|
41
|
+
const split = ctx.own === void 0 ? void 0 : splitOwnMcp(name, ctx.own);
|
|
42
|
+
if (split !== void 0 && ctx.own !== void 0) return { value: fill(runtime.mcp, {
|
|
43
|
+
plugin: ctx.own.plugin,
|
|
44
|
+
...split
|
|
45
|
+
}) };
|
|
46
|
+
if (runtime.unlisted === "keep") return { value: name };
|
|
47
|
+
return { problem: name.startsWith("mcp__") ? `${raw} has no run-time name on this target: only this plugin's own declared MCP servers can be spelled` : `${raw} has no run-time name on this target` };
|
|
48
|
+
};
|
|
49
|
+
/** Spell the inside of one `{{…}}` that opens with a known kind; `raw` is the whole token as written. */
|
|
50
|
+
const token = (inner, raw, ctx) => {
|
|
51
|
+
if (/[{}]/.test(inner)) return { problem: `malformed token ${raw}; write \\{{ for a literal {{` };
|
|
52
|
+
const [kind = "", ...args] = inner.trim().split(/\s+/).filter((part) => part.length > 0);
|
|
53
|
+
if (kind === "plugin_root") {
|
|
54
|
+
if (args.length > 0) return { problem: `${raw}: plugin_root takes no argument` };
|
|
55
|
+
return spelling(ctx.target.pluginRoot.body, raw);
|
|
56
|
+
}
|
|
57
|
+
const [name] = args;
|
|
58
|
+
if (name === void 0) return { problem: `${raw} needs ${article(kind)} name` };
|
|
59
|
+
if (args.length > 1) return { problem: `${raw} takes one ${kind} name` };
|
|
60
|
+
if (kind === "tool") return tool(name, raw, ctx);
|
|
61
|
+
if (kind === "agent") {
|
|
62
|
+
if (!ctx.agents.has(name)) return { problem: `${raw}: this plugin has no agent "${name}"` };
|
|
63
|
+
return { value: fill(ctx.target.agents.id, {
|
|
64
|
+
plugin: ctx.plugin,
|
|
65
|
+
agent: name
|
|
66
|
+
}) };
|
|
67
|
+
}
|
|
68
|
+
if (!ctx.skills.has(name)) return { problem: `${raw}: this plugin has no skill "${name}"` };
|
|
69
|
+
const invoke = spelling(ctx.target.skills.invoke, raw);
|
|
70
|
+
return "value" in invoke ? { value: fill(invoke.value, {
|
|
71
|
+
plugin: ctx.plugin,
|
|
72
|
+
skill: name
|
|
73
|
+
}) } : invoke;
|
|
74
|
+
};
|
|
75
|
+
/** Replace every token on one line, honouring the `\{{` escape. */
|
|
76
|
+
const tokens = (line, ctx, problems) => {
|
|
77
|
+
if (!line.includes("{{")) return line;
|
|
78
|
+
let out = "";
|
|
79
|
+
let at = 0;
|
|
80
|
+
while (at < line.length) {
|
|
81
|
+
const open = line.indexOf("{{", at);
|
|
82
|
+
if (open === -1) {
|
|
83
|
+
out += line.slice(at);
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
const kind = KIND.exec(line.slice(open + 2))?.[1];
|
|
87
|
+
if (kind === void 0 || !KINDS.has(kind)) {
|
|
88
|
+
out += line.slice(at, open + 1);
|
|
89
|
+
at = open + 1;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (open - 1 >= at && line[open - 1] === "\\") {
|
|
93
|
+
out += `${line.slice(at, open - 1)}{{`;
|
|
94
|
+
at = open + 2;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
out += line.slice(at, open);
|
|
98
|
+
const close = line.indexOf("}}", open + 2);
|
|
99
|
+
if (close === -1) {
|
|
100
|
+
problems.push(`a {{${kind} token is never closed on its line; write \\{{ for a literal {{`);
|
|
101
|
+
out += line.slice(open);
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
const raw = line.slice(open, close + 2);
|
|
105
|
+
const spelled = token(line.slice(open + 2, close), raw, ctx);
|
|
106
|
+
if ("value" in spelled) out += spelled.value;
|
|
107
|
+
else {
|
|
108
|
+
problems.push(spelled.problem);
|
|
109
|
+
out += raw;
|
|
110
|
+
}
|
|
111
|
+
at = close + 2;
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
};
|
|
115
|
+
const link = (text, destination, raw, ctx) => {
|
|
116
|
+
const hash = destination.indexOf("#");
|
|
117
|
+
const target = hash === -1 ? destination : destination.slice(0, hash);
|
|
118
|
+
const anchor = hash === -1 ? "" : destination.slice(hash);
|
|
119
|
+
const [kind = "", name = "", ...rest] = target.split("/");
|
|
120
|
+
const path = rest.join("/").replace(/\/+$/, "");
|
|
121
|
+
if (kind === "agent") {
|
|
122
|
+
if (name === "" || path !== "" || anchor !== "") return { problem: `${raw}: an agent link is pluginfinity://agent/<agent>, with no path or anchor` };
|
|
123
|
+
if (!ctx.agents.has(name)) return { problem: `${raw}: this plugin has no agent "${name}"` };
|
|
124
|
+
return { value: `${text} (\`${fill(ctx.target.agents.id, {
|
|
125
|
+
plugin: ctx.plugin,
|
|
126
|
+
agent: name
|
|
127
|
+
})}\`)` };
|
|
128
|
+
}
|
|
129
|
+
if (kind !== "skill") return { problem: `${raw}: a link is pluginfinity://skill/<skill>[/<path>] or pluginfinity://agent/<agent>` };
|
|
130
|
+
if (name === "") return { problem: `${raw}: a skill link names a skill, pluginfinity://skill/<skill>[/<path>]` };
|
|
131
|
+
if (!ctx.skills.has(name)) return { problem: `${raw}: this plugin has no skill "${name}"` };
|
|
132
|
+
if (path !== "" && !ctx.skillFiles.has(`${name}/${path}`)) return { problem: `${raw}: skill "${name}" has no file "${path}" (looked for ${name}/${path})` };
|
|
133
|
+
if (ctx.target.references.style === "prose") return { value: path === "" ? `${text} (the \`${name}\` skill)` : `${text} (the \`${name}\` skill's \`${path}\`)` };
|
|
134
|
+
const root = spelling(ctx.target.pluginRoot.body, raw);
|
|
135
|
+
if (!("value" in root)) return root;
|
|
136
|
+
const file = path === "" ? "SKILL.md" : path;
|
|
137
|
+
return { value: `[${text}](${root.value}/${ctx.target.skills.dir}/${name}/${file}${anchor})` };
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Rewrite the inline `pluginfinity://` links on one line outside fenced code,
|
|
141
|
+
* skipping inline code spans; any other `pluginfinity://` outside code, in any
|
|
142
|
+
* case, is a problem, so nothing unbuilt ships.
|
|
143
|
+
*/
|
|
144
|
+
const links = (line, ctx, problems) => {
|
|
145
|
+
if (!SCHEME.test(line)) return line;
|
|
146
|
+
const code = inlineCodeSpans(line);
|
|
147
|
+
const inCode = (at) => {
|
|
148
|
+
let lo = 0;
|
|
149
|
+
let hi = code.length - 1;
|
|
150
|
+
while (lo <= hi) {
|
|
151
|
+
const mid = lo + hi >> 1;
|
|
152
|
+
const span = code[mid];
|
|
153
|
+
if (at < span.start) hi = mid - 1;
|
|
154
|
+
else if (at >= span.end) lo = mid + 1;
|
|
155
|
+
else return true;
|
|
156
|
+
}
|
|
157
|
+
return false;
|
|
158
|
+
};
|
|
159
|
+
const handled = [];
|
|
160
|
+
const out = line.replace(LINK, (raw, bang, text, destination, offset) => {
|
|
161
|
+
if (inCode(offset + bang.length)) return raw;
|
|
162
|
+
handled.push([offset, offset + raw.length]);
|
|
163
|
+
if (bang !== "") {
|
|
164
|
+
problems.push(`${raw}: an image cannot link to a pluginfinity:// reference`);
|
|
165
|
+
return raw;
|
|
166
|
+
}
|
|
167
|
+
const spelled = link(text, destination, raw, ctx);
|
|
168
|
+
if ("value" in spelled) return spelled.value;
|
|
169
|
+
problems.push(spelled.problem);
|
|
170
|
+
return raw;
|
|
171
|
+
});
|
|
172
|
+
let range = 0;
|
|
173
|
+
let strays = 0;
|
|
174
|
+
for (const match of line.matchAll(SCHEMES)) {
|
|
175
|
+
const at = match.index;
|
|
176
|
+
while (range < handled.length && handled[range][1] <= at) range += 1;
|
|
177
|
+
const current = handled[range];
|
|
178
|
+
if (current !== void 0 && current[0] <= at) continue;
|
|
179
|
+
if (inCode(at)) continue;
|
|
180
|
+
strays += 1;
|
|
181
|
+
if (strays > STRAY_LIMIT) continue;
|
|
182
|
+
problems.push(`${around(line, at)}: only inline links [text](pluginfinity://skill/<skill>[/<path>][#anchor]) and [text](pluginfinity://agent/<agent>) are built, with no title`);
|
|
183
|
+
}
|
|
184
|
+
if (strays > STRAY_LIMIT) problems.push(`and ${strays - STRAY_LIMIT} more pluginfinity:// occurrences on this line that are not built links`);
|
|
185
|
+
return out;
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* Render a markdown body's tokens and `pluginfinity://` links for one target,
|
|
189
|
+
* or report every one it cannot render.
|
|
190
|
+
*
|
|
191
|
+
* @remarks
|
|
192
|
+
* A token is `{{tool <name>}}`, `{{agent <name>}}`, `{{skill <name>}}` or
|
|
193
|
+
* `{{plugin_root}}`, with whitespace allowed inside the braces, on one line.
|
|
194
|
+
* Tokens are replaced everywhere, fenced and inline code included; `\{{`
|
|
195
|
+
* before a token renders it literally, and before any other `{{` the
|
|
196
|
+
* backslash stays. A `{{` whose first word is not a kind is text, so
|
|
197
|
+
* GitHub Actions expressions, Jinja and Handlebars pass through;
|
|
198
|
+
* a token starts at the `{{` right before its kind, so `{{{tool Read}}}`
|
|
199
|
+
* keeps the outer braces. A known kind that cannot be spelled is a problem:
|
|
200
|
+
* a missing or extra argument, a token never closed on its line, or a brace
|
|
201
|
+
* inside one.
|
|
202
|
+
*
|
|
203
|
+
* An inline markdown link to `pluginfinity://skill/<skill>[/<path>][#anchor]`
|
|
204
|
+
* or `pluginfinity://agent/<agent>` outside fenced and inline code is built in
|
|
205
|
+
* the target's reference style; inside code it stays text. A path style keeps
|
|
206
|
+
* the anchor; a prose style drops it. Any other `pluginfinity://` outside
|
|
207
|
+
* code, in any case (a title, an image, a reference definition, an autolink),
|
|
208
|
+
* is a problem rather than shipped.
|
|
209
|
+
*
|
|
210
|
+
* Known limits: an indented (four-space) code block is not treated as code,
|
|
211
|
+
* an inline code span across two lines is not recognised, and an escaped
|
|
212
|
+
* `\[text](pluginfinity://…)` is still built and keeps its backslash.
|
|
213
|
+
*
|
|
214
|
+
* Run it after host blocks are applied, so a token in another target's block
|
|
215
|
+
* is never evaluated. The result is a pure function of its inputs.
|
|
216
|
+
*
|
|
217
|
+
* @public
|
|
218
|
+
*/
|
|
219
|
+
const renderTokens = (text, ctx) => {
|
|
220
|
+
if (!text.includes("{{") && !SCHEME.test(text)) return { text };
|
|
221
|
+
const lines = text.split("\n");
|
|
222
|
+
const fenced = fencedLines(lines);
|
|
223
|
+
const problems = [];
|
|
224
|
+
const rendered = lines.map((line, index) => {
|
|
225
|
+
const found = [];
|
|
226
|
+
const linked = fenced[index] === true ? line : links(line, ctx, found);
|
|
227
|
+
const out = tokens(linked, ctx, found);
|
|
228
|
+
for (const message of found) problems.push({
|
|
229
|
+
line: index + 1,
|
|
230
|
+
message
|
|
231
|
+
});
|
|
232
|
+
return out;
|
|
233
|
+
});
|
|
234
|
+
return problems.length > 0 ? { problems } : { text: rendered.join("\n") };
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
//#endregion
|
|
238
|
+
export { renderTokens };
|