@pluginfinity/engine 0.1.0 → 0.2.0

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 CHANGED
@@ -1,6 +1,7 @@
1
- import { applyHostBlocks } from "./body.js";
1
+ import { mapHostBlocks } from "./body.js";
2
2
  import { appendSections, mapFrontmatter } from "./frontmatter.js";
3
- import { decodeComponent, frontmatterText, invalid, issue, overlayIssues, unknownTargets } from "./component.js";
3
+ import { decodeComponent, frontmatterText, invalid, issue, lineIssues, overlayIssues, unknownTargets } from "./component.js";
4
+ import { renderTokens } from "./tokens.js";
4
5
  import { AGENT_FIELDS, AgentFrontmatter } from "@pluginfinity/core";
5
6
  import { Effect, FileSystem, Path } from "effect";
6
7
 
@@ -53,34 +54,46 @@ const readAgents = (root, known) => Effect.gen(function* () {
53
54
  /**
54
55
  * Render one agent for a target, or `undefined` when its `targets` block
55
56
  * excludes it: the file at `<agents.dir>/<name><agents.suffix>` with the
56
- * target's frontmatter, degraded fields appended as body sections, and host
57
- * blocks applied. It keeps its source mode.
57
+ * target's frontmatter, host blocks and then body tokens applied, and
58
+ * degraded fields appended as body sections. It keeps its source mode. A
59
+ * token problem is keyed by its file line and names the target.
58
60
  *
59
61
  * @public
60
62
  */
61
- const renderAgent = (target, id, agent, known) => Effect.gen(function* () {
63
+ const renderAgent = (target, id, agent, known, tokens) => Effect.gen(function* () {
62
64
  const fs = yield* FileSystem.FileSystem;
63
65
  const block = agent.frontmatter.targets?.[id];
64
66
  if (block === false) return void 0;
65
- const mapped = mapFrontmatter(target, target.agents.fields, target.agents.hostFields, agent.frontmatter, block ?? {});
67
+ const mapped = mapFrontmatter(target, target.agents.fields, target.agents.hostFields, agent.frontmatter, block ?? {}, tokens.own);
66
68
  const problems = [
67
69
  ...yield* overlayIssues(AgentFrontmatter, AGENT_FIELDS, agent.frontmatter, block ?? {}, id),
68
70
  ...mapped.unresolved.map((field) => issue(field.field, `${id} leaves this field unresolved (${field.note})`)),
69
71
  ...mapped.unknown.map((key) => issue(`targets.${id}.${key}`, `not an agent field or a ${id} agent field`))
70
72
  ];
71
- const body = applyHostBlocks(agent.body, id, known);
73
+ const body = mapHostBlocks(agent.body, id, known);
72
74
  if ("problem" in body) return yield* Effect.fail(invalid(agent.path, [issue(`line ${body.problem.line + agent.bodyOffset}`, body.problem.message)]));
73
- if (problems.length > 0) return yield* Effect.fail(invalid(agent.path, problems, id));
75
+ const rendered = renderTokens(body.text, tokens);
76
+ if ("problems" in rendered) problems.push(...lineIssues(rendered.problems, body.lines, agent.bodyOffset));
77
+ if (problems.length > 0 || !("text" in rendered)) return yield* Effect.fail(invalid(agent.path, problems, id));
74
78
  const { name: _name, description, ...rest } = mapped.fields;
75
79
  const yaml = yield* frontmatterText({
76
80
  name: agent.name,
77
81
  description,
78
82
  ...rest
79
83
  }, agent);
84
+ const source = `agents/${agent.name}.md`;
80
85
  return {
81
- path: `${target.agents.dir}/${agent.name}${target.agents.suffix}`,
82
- content: `---\n${yaml}---\n${appendSections(body.text, mapped.sections)}`,
83
- mode: (yield* fs.stat(agent.path)).mode & 511
86
+ file: {
87
+ path: `${target.agents.dir}/${agent.name}${target.agents.suffix}`,
88
+ content: `---\n${yaml}---\n${appendSections(rendered.text, mapped.sections)}`,
89
+ mode: (yield* fs.stat(agent.path)).mode & 511
90
+ },
91
+ notes: mapped.drops.map(({ field, kind }) => ({
92
+ target: id,
93
+ path: source,
94
+ kind,
95
+ name: field
96
+ }))
84
97
  };
85
98
  });
86
99
 
package/body.js CHANGED
@@ -5,31 +5,98 @@
5
5
  * `<!-- /pluginfinity:only -->` closes it. A marker inside fenced code or an
6
6
  * inline code span is text, so a body can show one.
7
7
  */
8
- const OPEN = /^\s*<!--\s*pluginfinity:only\s+([^>]*?)\s*-->\s*$/;
8
+ const OPEN_PREFIX = /^\s*<!--\s*pluginfinity:only\s+/;
9
9
  const CLOSE = /^\s*<!--\s*\/pluginfinity:only\s*-->\s*$/;
10
10
  const MARKER = /<!--\s*\/?pluginfinity:only/;
11
+ /**
12
+ * The target ids of a host block's opening line, or `undefined` when the line
13
+ * is not an opener. A prefix match, then string operations on the rest, so no
14
+ * pattern has overlapping quantifiers to backtrack on.
15
+ */
16
+ const openingIds = (line) => {
17
+ const prefix = OPEN_PREFIX.exec(line);
18
+ if (prefix === null) return void 0;
19
+ const rest = line.slice(prefix[0].length).trimEnd();
20
+ if (!rest.endsWith("-->")) return void 0;
21
+ const inner = rest.slice(0, -3);
22
+ if (inner.includes(">")) return void 0;
23
+ return inner.split(/\s+/).filter((id) => id.length > 0);
24
+ };
11
25
  const FENCE = /^ {0,3}(`{3,}|~{3,})/;
12
- const INLINE_CODE = /(`+)[\s\S]*?\1/g;
13
26
  /**
14
- * Keep the passages of a body's host blocks that list `target`, drop the
15
- * rest, and remove every marker line. Blocks do not nest; an unclosed block,
16
- * a stray close, a marker that is not on a line of its own, or an id outside
17
- * `known` is a problem, and the body is not rewritten. So, until references
18
- * are built, is a `pluginfinity://` link; see {@link referenceLines}.
27
+ * The inline code spans of one line, as sorted, disjoint `[start, end)` ranges
28
+ * covering the backticks too. A span opens at a run of backticks and closes at
29
+ * the next run of exactly the same length; a run with no such partner is
30
+ * literal text. One pass over the line: runs are indexed, then each is paired
31
+ * with the next run of its length, so no input backtracks.
32
+ */
33
+ const inlineCodeSpans = (line) => {
34
+ const starts = [];
35
+ const lengths = [];
36
+ for (let i = 0; i < line.length;) {
37
+ if (line[i] !== "`") {
38
+ i++;
39
+ continue;
40
+ }
41
+ let j = i;
42
+ while (line[j] === "`") j++;
43
+ starts.push(i);
44
+ lengths.push(j - i);
45
+ i = j;
46
+ }
47
+ const partner = new Array(starts.length).fill(-1);
48
+ const nextOfLength = /* @__PURE__ */ new Map();
49
+ for (let k = starts.length - 1; k >= 0; k--) {
50
+ const len = lengths[k];
51
+ partner[k] = nextOfLength.get(len) ?? -1;
52
+ nextOfLength.set(len, k);
53
+ }
54
+ const spans = [];
55
+ for (let k = 0; k < starts.length;) {
56
+ const close = partner[k];
57
+ if (close === -1) {
58
+ k++;
59
+ continue;
60
+ }
61
+ spans.push({
62
+ start: starts[k],
63
+ end: starts[close] + lengths[close]
64
+ });
65
+ k = close + 1;
66
+ }
67
+ return spans;
68
+ };
69
+ /** The line with its inline code spans removed. */
70
+ const stripInlineCode = (line) => {
71
+ let out = "";
72
+ let at = 0;
73
+ for (const { start, end } of inlineCodeSpans(line)) {
74
+ out += line.slice(at, start);
75
+ at = end;
76
+ }
77
+ return out + line.slice(at);
78
+ };
79
+ /**
80
+ * {@link applyHostBlocks}, also returning the 1-based source line of each
81
+ * line of the result, so a problem found in the result can be reported where
82
+ * the author wrote it.
19
83
  *
20
- * @public
84
+ * @internal
21
85
  */
22
- const applyHostBlocks = (text, target, known) => {
23
- const [reference] = referenceLines(text);
24
- if (reference !== void 0) return { problem: {
25
- line: reference,
26
- message: "pluginfinity:// references are not built yet; link with a relative path instead"
27
- } };
28
- if (!MARKER.test(text)) return { text };
86
+ const mapHostBlocks = (text, target, known) => {
87
+ const lines = text.split("\n");
88
+ if (!MARKER.test(text)) return {
89
+ text,
90
+ lines: lines.map((_, index) => index + 1)
91
+ };
29
92
  const kept = [];
93
+ const sources = [];
94
+ const push = (line, number) => {
95
+ kept.push(line);
96
+ sources.push(number);
97
+ };
30
98
  let open;
31
99
  let fence;
32
- const lines = text.split("\n");
33
100
  for (const [index, line] of lines.entries()) {
34
101
  const number = index + 1;
35
102
  const keep = open === void 0 || open.keep;
@@ -37,16 +104,15 @@ const applyHostBlocks = (text, target, known) => {
37
104
  if (fence !== void 0 || fenceMark !== void 0) {
38
105
  if (fence === void 0) fence = fenceMark;
39
106
  else if (fenceMark !== void 0 && fenceMark[0] === fence[0] && fenceMark.length >= fence.length) fence = void 0;
40
- if (keep) kept.push(line);
107
+ if (keep) push(line, number);
41
108
  continue;
42
109
  }
43
- const opening = OPEN.exec(line);
44
- if (opening !== null) {
110
+ const ids = openingIds(line);
111
+ if (ids !== void 0) {
45
112
  if (open !== void 0) return { problem: {
46
113
  line: number,
47
114
  message: "host blocks do not nest"
48
115
  } };
49
- const ids = (opening[1] ?? "").split(/\s+/).filter((id) => id.length > 0);
50
116
  const unknown = ids.filter((id) => !known.includes(id));
51
117
  if (ids.length === 0) return { problem: {
52
118
  line: number,
@@ -70,41 +136,51 @@ const applyHostBlocks = (text, target, known) => {
70
136
  open = void 0;
71
137
  continue;
72
138
  }
73
- if (MARKER.test(line.replace(INLINE_CODE, ""))) return { problem: {
139
+ if (MARKER.test(stripInlineCode(line))) return { problem: {
74
140
  line: number,
75
141
  message: "a host block marker must be on a line of its own"
76
142
  } };
77
- if (keep) kept.push(line);
143
+ if (keep) push(line, number);
78
144
  }
79
145
  if (open !== void 0) return { problem: {
80
146
  line: open.line,
81
147
  message: "a host block is never closed"
82
148
  } };
83
- return { text: kept.join("\n") };
149
+ return {
150
+ text: kept.join("\n"),
151
+ lines: sources
152
+ };
84
153
  };
85
- const REFERENCE = /\]\(\s*<?pluginfinity:\/\//;
86
154
  /**
87
- * The 1-based lines of a body that link to a `pluginfinity://` reference,
88
- * outside fenced code and inline code spans. References are not built yet,
89
- * and an unbuilt reference must never ship as text, so each is a problem.
155
+ * Keep the passages of a body's host blocks that list `target`, drop the
156
+ * rest, and remove every marker line. Blocks do not nest; an unclosed block,
157
+ * a stray close, a marker that is not on a line of its own, or an id outside
158
+ * `known` is a problem, and the body is not rewritten. Tokens and
159
+ * `pluginfinity://` links are left for {@link renderTokens}, which runs after.
90
160
  *
91
161
  * @public
92
162
  */
93
- const referenceLines = (text) => {
94
- if (!text.includes("pluginfinity://")) return [];
95
- const found = [];
163
+ const applyHostBlocks = (text, target, known) => {
164
+ const mapped = mapHostBlocks(text, target, known);
165
+ return "problem" in mapped ? mapped : { text: mapped.text };
166
+ };
167
+ /**
168
+ * For each line of `text`, whether it belongs to fenced code: a fence's
169
+ * opening and closing lines and everything between. A fence closes on the
170
+ * same character, at least as long; an unclosed fence runs to the end.
171
+ *
172
+ * @internal
173
+ */
174
+ const fencedLines = (lines) => {
96
175
  let fence;
97
- for (const [index, line] of text.split("\n").entries()) {
176
+ return lines.map((line) => {
98
177
  const fenceMark = FENCE.exec(line)?.[1];
99
- if (fence !== void 0 || fenceMark !== void 0) {
100
- if (fence === void 0) fence = fenceMark;
101
- else if (fenceMark !== void 0 && fenceMark[0] === fence[0] && fenceMark.length >= fence.length) fence = void 0;
102
- continue;
103
- }
104
- if (REFERENCE.test(line.replace(INLINE_CODE, ""))) found.push(index + 1);
105
- }
106
- return found;
178
+ if (fence === void 0 && fenceMark === void 0) return false;
179
+ if (fence === void 0) fence = fenceMark;
180
+ else if (fenceMark !== void 0 && fenceMark[0] === fence[0] && fenceMark.length >= fence.length) fence = void 0;
181
+ return true;
182
+ });
107
183
  };
108
184
 
109
185
  //#endregion
110
- export { applyHostBlocks, referenceLines };
186
+ export { FENCE, applyHostBlocks, fencedLines, inlineCodeSpans, mapHostBlocks, stripInlineCode };
package/component.js CHANGED
@@ -15,6 +15,19 @@ const issue = (key, message) => ConfigIssue.make({
15
15
  message
16
16
  });
17
17
  /**
18
+ * Token problems as issues keyed by their source file line: `lines` maps each
19
+ * line of the rendered text to its line in the source text (see
20
+ * `mapHostBlocks`), and the source text starts `offset` lines into the file.
21
+ * Each is marked a token issue, so its remediation names the token fixes.
22
+ *
23
+ * @internal
24
+ */
25
+ const lineIssues = (found, lines, offset) => found.map((problem) => ConfigIssue.make({
26
+ key: `line ${(lines[problem.line - 1] ?? problem.line) + offset}`,
27
+ message: problem.message,
28
+ kind: "token"
29
+ }));
30
+ /**
18
31
  * A `ComponentInvalid` for `path`, optionally specific to one target.
19
32
  *
20
33
  * @internal
@@ -154,4 +167,4 @@ const JUNK = /* @__PURE__ */ new Set([
154
167
  const toLf = (text) => text.replaceAll("\r\n", "\n");
155
168
 
156
169
  //#endregion
157
- export { STRINGIFY, decodeComponent, frontmatterText, invalid, isJunk, issue, overlayIssues, toLf, unknownTargets };
170
+ export { STRINGIFY, decodeComponent, frontmatterText, invalid, isJunk, issue, lineIssues, overlayIssues, toLf, unknownTargets };
package/doctor.js CHANGED
@@ -2,11 +2,11 @@ import { ConfigLoader } from "./loader.js";
2
2
  import { selectConfigPaths } from "./selection.js";
3
3
  import { ENGINE_VERSION } from "./version.js";
4
4
  import { Duration, Effect, FileSystem, Option, Path, Result, Schema } from "effect";
5
+ import { TARGETS } from "@pluginfinity/targets";
5
6
  import { Walker } from "@effected/walker";
6
7
  import { Tool, ToolDiscovery } from "@effected/commands";
7
8
  import { CurrentDistribution, DistributionField, Remediation } from "@effected/engine";
8
9
  import { SemVer } from "@effected/semver";
9
- import { TARGETS } from "@pluginfinity/targets";
10
10
 
11
11
  //#region src/doctor.ts
12
12
  /**
package/emit.js CHANGED
@@ -11,6 +11,14 @@ const encoder = new TextEncoder();
11
11
  const bytesOf = (file) => typeof file.content === "string" ? encoder.encode(file.content) : file.content;
12
12
  const modeOf = (file) => file.mode ?? 420;
13
13
  /**
14
+ * Whether a file's mode on disk matches what the build would write. A
15
+ * generated file's mode is never compared, and a copied file's only by its
16
+ * executable bit: git tracks nothing finer, and tools that flip permission
17
+ * bits behind a commit (a hook running `chmod +x` with `core.fileMode=false`,
18
+ * a umask of 002) would otherwise make a fresh build look stale.
19
+ */
20
+ const sameMode = (file, onDisk) => file.mode === void 0 || (file.mode & 73) !== 0 === ((onDisk & 73) !== 0);
21
+ /**
14
22
  * How an output directory differs from what a build produces. Every list is
15
23
  * sorted and holds `/`-separated paths relative to the output directory.
16
24
  *
@@ -19,11 +27,14 @@ const modeOf = (file) => file.mode ?? 420;
19
27
  var EmitPlan = class extends Schema.Class("EmitPlan")({
20
28
  /** Produced, not on disk. */
21
29
  added: Schema.Array(Schema.String),
22
- /** On disk with other bytes or another mode. */
30
+ /** On disk with other bytes, or a copied file whose executable bit differs. */
23
31
  changed: Schema.Array(Schema.String),
24
32
  /** On disk, no longer produced; an empty directory ends in `/`. */
25
33
  removed: Schema.Array(Schema.String),
26
- /** On disk with the same bytes and mode; never touched, so its mtime stays. */
34
+ /**
35
+ * On disk with the same bytes and, for a copied file, the same executable
36
+ * bit; never touched, so its mtime and mode stay.
37
+ */
27
38
  unchanged: Schema.Array(Schema.String)
28
39
  }) {
29
40
  /** Whether the output directory already matches the build. */
@@ -53,6 +64,10 @@ const inventory = (dir) => Effect.gen(function* () {
53
64
  /**
54
65
  * Compare `files` with what `dir` holds, reading only.
55
66
  *
67
+ * @remarks
68
+ * A file is compared by its bytes and, when it carries a source mode, by its
69
+ * executable bit alone; other permission bits never make a file changed.
70
+ *
56
71
  * @public
57
72
  */
58
73
  const planEmit = (dir, files) => Effect.gen(function* () {
@@ -69,7 +84,7 @@ const planEmit = (dir, files) => Effect.gen(function* () {
69
84
  }
70
85
  const target = path.join(dir, file.path);
71
86
  const info = yield* fs.stat(target);
72
- (info.type === "File" && (info.mode & 511) === modeOf(file) && sameBytes(yield* fs.readFile(target), bytesOf(file)) ? unchanged : changed).push(file.path);
87
+ (info.type === "File" && sameMode(file, info.mode) && sameBytes(yield* fs.readFile(target), bytesOf(file)) ? unchanged : changed).push(file.path);
73
88
  }
74
89
  const produced = new Set(files.map((file) => file.path));
75
90
  const removed = [...existing].filter((file) => !produced.has(file));
package/errors.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Schema } from "effect";
2
+ import { KNOWN_TARGET_IDS } from "@pluginfinity/targets";
2
3
 
3
4
  //#region src/errors.ts
4
5
  /**
@@ -9,9 +10,21 @@ import { Schema } from "effect";
9
10
  var ConfigIssue = class extends Schema.Class("ConfigIssue")({
10
11
  /** The dotted key path inside the config, `""` for the config as a whole. */
11
12
  key: Schema.String,
12
- message: Schema.String
13
+ message: Schema.String,
14
+ /** `"token"` for a body token or `pluginfinity://` link that cannot be built. */
15
+ kind: Schema.optionalKey(Schema.Literal("token"))
13
16
  }) {};
14
17
  /**
18
+ * The ways to clear a token or link problem besides correcting it: keep the
19
+ * passage from the failing target, keep the braces literal, or keep a link
20
+ * literal in inline code. A host block must name a target other than the failing one.
21
+ */
22
+ const tokenAlternatives = (failing) => {
23
+ const named = new Set(failing.filter((target) => target !== void 0));
24
+ const others = KNOWN_TARGET_IDS.filter((target) => !named.has(target));
25
+ return `${others.length > 0 ? `use a host block (<!-- pluginfinity:only ${others.join(" ")} -->) around a passage only another target can build` : "use a host block (<!-- pluginfinity:only <other target> -->) around a passage only another target can build"}, \`\\{{\` to keep a token literal, or inline code to keep a pluginfinity:// link literal`;
26
+ };
27
+ /**
15
28
  * The upward walk reached a `.git` directory or the filesystem root without
16
29
  * finding a config, or an explicit `--config` file does not exist.
17
30
  *
@@ -263,6 +276,52 @@ var HookScriptInvalid = class extends Schema.TaggedError()("HookScriptInvalid",
263
276
  }
264
277
  };
265
278
  /**
279
+ * What is wrong with a file a server or the `files` key ships.
280
+ *
281
+ * @public
282
+ */
283
+ const ShippedFileProblem = Schema.Literals([
284
+ "missing",
285
+ "not-executable",
286
+ "directory",
287
+ "outside-root",
288
+ "not-normal"
289
+ ]);
290
+ /**
291
+ * A file the build must ship for a server, or because `files` lists it, cannot be shipped.
292
+ *
293
+ * @public
294
+ */
295
+ var ShippedFileInvalid = class extends Schema.TaggedError()("ShippedFileInvalid", {
296
+ /** The config. */
297
+ path: Schema.String,
298
+ /** The file, relative to the plugin root. */
299
+ file: Schema.String,
300
+ /** What names it: `mcpServers.<name>`, `lspServers.<name>` (prefixed `<target>.` when a target override sets it) or `files`. */
301
+ referencedBy: Schema.String,
302
+ problem: ShippedFileProblem
303
+ }) {
304
+ get message() {
305
+ const why = {
306
+ missing: "does not exist",
307
+ "not-executable": "is its command but is not executable",
308
+ directory: "is its command but is a directory",
309
+ "outside-root": "resolves outside the plugin",
310
+ "not-normal": "has an empty, . or .. segment"
311
+ }[this.problem];
312
+ return `${this.file}, named by ${this.referencedBy} in ${this.path}, ${why}`;
313
+ }
314
+ get remediation() {
315
+ return { hint: {
316
+ missing: `Create ${this.file} under the plugin root, or fix the path in ${this.referencedBy}.`,
317
+ "not-executable": `Run \`chmod +x ${this.file}\`, or run it through sh: command "sh" with the path in args.`,
318
+ directory: `Name the launcher file inside ${this.file} as the command; a directory ships whole only when another server field names it.`,
319
+ "outside-root": "Keep shipped files inside the plugin directory; a path or symlink that leaves it cannot ship.",
320
+ "not-normal": `Write the path in ${this.referencedBy} without . or .. segments, as it lies under the plugin root.`
321
+ }[this.problem] };
322
+ }
323
+ };
324
+ /**
266
325
  * A copied source file and a generated file would land on the same build path.
267
326
  *
268
327
  * @public
@@ -299,7 +358,11 @@ var ComponentInvalid = class extends Schema.TaggedError()("ComponentInvalid", {
299
358
  return `${this.target === void 0 ? this.path : `${this.path} (for ${this.target})`}: ${this.issues.map((issue) => issue.key === "" ? issue.message : `${issue.key}: ${issue.message}`).join("; ")}`;
300
359
  }
301
360
  get remediation() {
302
- return this.target === void 0 ? { hint: `Correct the listed problems in ${this.path}.` } : { hint: `Correct the listed problems in ${this.path}, or set the field for ${this.target} in its \`targets.${this.target}\` block.` };
361
+ const tokens = this.issues.filter((issue) => issue.kind === "token").length;
362
+ const lead = `Correct the listed problems in ${this.path}`;
363
+ if (tokens > 0 && tokens === this.issues.length) return { hint: `${lead}; to keep a passage from this target, ${tokenAlternatives([this.target])}.` };
364
+ const field = this.target === void 0 ? lead : `${lead}, or set the field for ${this.target} in its \`targets.${this.target}\` block`;
365
+ return { hint: tokens > 0 ? `${field}; for a token or link, ${tokenAlternatives([this.target])}.` : `${field}.` };
303
366
  }
304
367
  };
305
368
  /**
@@ -318,7 +381,9 @@ var ComponentsInvalid = class extends Schema.TaggedError()("ComponentsInvalid",
318
381
  return [`${count} ${count === 1 ? "component is" : "components are"} invalid in the plugin of ${this.path}:`, ...this.components.map((component) => ` - ${component.message}`)].join("\n");
319
382
  }
320
383
  get remediation() {
321
- return { hint: "Correct each listed file; a field one host cannot take can be set in that host's `targets` block." };
384
+ const failing = this.components.filter((component) => component.issues.some((issue) => issue.kind === "token"));
385
+ const base = "Correct each listed file; a field one host cannot take can be set in that host's `targets` block";
386
+ return { hint: failing.length > 0 ? `${base}; to keep a passage from a target, ${tokenAlternatives(failing.map((component) => component.target))}.` : `${base}.` };
322
387
  }
323
388
  };
324
389
  const BUILD_ERROR_TAGS = [
@@ -327,6 +392,7 @@ const BUILD_ERROR_TAGS = [
327
392
  "HostRejected",
328
393
  "HookEventUnsupported",
329
394
  "HookScriptInvalid",
395
+ "ShippedFileInvalid",
330
396
  "PathConflict",
331
397
  "ComponentsInvalid"
332
398
  ];
@@ -338,4 +404,4 @@ const BUILD_ERROR_TAGS = [
338
404
  const isBuildError = (error) => typeof error === "object" && error !== null && "_tag" in error && typeof error._tag === "string" && BUILD_ERROR_TAGS.includes(error._tag);
339
405
 
340
406
  //#endregion
341
- export { BuildStale, ComponentInvalid, ComponentsInvalid, ConfigAmbiguous, ConfigInvalid, ConfigIssue, ConfigLoadFailed, ConfigNotFound, HookEventUnsupported, HookScriptInvalid, HookScriptProblem, HostRejected, NotImplemented, PackageVersionMissing, PathConflict, TargetDrift, TargetNotEnabled, UnknownTarget, isBuildError, isConfigError };
407
+ export { BuildStale, ComponentInvalid, ComponentsInvalid, ConfigAmbiguous, ConfigInvalid, ConfigIssue, ConfigLoadFailed, ConfigNotFound, HookEventUnsupported, HookScriptInvalid, HookScriptProblem, HostRejected, NotImplemented, PackageVersionMissing, PathConflict, ShippedFileInvalid, ShippedFileProblem, TargetDrift, TargetNotEnabled, UnknownTarget, isBuildError, isConfigError };
package/frontmatter.js CHANGED
@@ -44,14 +44,50 @@ const toolNames = (value) => {
44
44
  return names;
45
45
  };
46
46
  /**
47
- * A Claude Code MCP tool name, `mcp__<server>__<tool>`, in the target's MCP
48
- * spelling. A server named `plugin_…` belongs to another plugin, whose name on
49
- * the target is unknown, so it passes through, as does any other tool name.
47
+ * Split a Claude Code MCP tool name of this plugin's own server,
48
+ * `mcp__plugin_<plugin>_<server>__<tool>`, into its server and tool; the
49
+ * prefix is matched literally and the rest is split at the first `__` that
50
+ * leaves a server `own` declares. `undefined` when the name is not one.
51
+ *
52
+ * @internal
53
+ */
54
+ const splitOwnMcp = (name, own) => {
55
+ const prefix = `mcp__plugin_${own.plugin}_`;
56
+ if (!name.startsWith(prefix)) return void 0;
57
+ const rest = name.slice(prefix.length);
58
+ for (let at = rest.indexOf("__"); at !== -1; at = rest.indexOf("__", at + 1)) {
59
+ const server = rest.slice(0, at);
60
+ const tool = rest.slice(at + 2);
61
+ if (server.length > 0 && tool.length > 0 && own.servers.has(server)) return {
62
+ server,
63
+ tool
64
+ };
65
+ }
66
+ };
67
+ const spell = (target, server, tool) => target.tools.mcp.replace("{server}", server).replace("{tool}", tool);
68
+ /**
69
+ * A Claude Code MCP tool name in the target's MCP spelling, or `undefined`
70
+ * when it is not one the target can spell.
71
+ *
72
+ * @remarks
73
+ * `mcp__<server>__<tool>` names a server the user configures. Claude Code
74
+ * names a plugin's server `plugin_<plugin>_<server>`, so a tool of this
75
+ * plugin's own server is `mcp__plugin_<plugin>_<server>__<tool>`. The prefix
76
+ * is matched literally, so a plugin name holding `_` or `-` is safe, and the
77
+ * rest is split at the first `__` that leaves a server the target declares.
78
+ * A target that keeps unlisted names takes Claude Code's spelling as is, so
79
+ * the name passes through. Any other `plugin_…` server, and a server of this
80
+ * plugin the target does not declare, has no spelling on the target.
50
81
  */
51
- const mcpToolName = (target, name) => {
82
+ const mcpToolName = (target, name, own) => {
83
+ if (own !== void 0 && name.startsWith(`mcp__plugin_${own.plugin}_`)) {
84
+ if (target.tools.unlisted === "keep") return void 0;
85
+ const split = splitOwnMcp(name, own);
86
+ return split === void 0 ? void 0 : spell(target, split.server, split.tool);
87
+ }
52
88
  const match = /^mcp__(.+?)__(.+)$/.exec(name);
53
- if (match === null || (match[1] ?? "").startsWith("plugin_")) return name;
54
- return target.tools.mcp.replace("{server}", match[1] ?? "").replace("{tool}", match[2] ?? "");
89
+ if (match === null || (match[1] ?? "").startsWith("plugin_")) return void 0;
90
+ return spell(target, match[1] ?? "", match[2] ?? "");
55
91
  };
56
92
  /**
57
93
  * Map one component's frontmatter onto a target through the target's field
@@ -60,12 +96,14 @@ const mcpToolName = (target, name) => {
60
96
  * @remarks
61
97
  * The component's `targets` block for this target overlays the base fields:
62
98
  * a core field there replaces the base value before mapping, and a host field
63
- * (one of `hostFields`) is written as is. When the block sets `description`,
64
- * no field is degraded into it: the author wrote that host's description.
99
+ * (one of `hostFields`) is written as is. `own` lets a tool of the plugin's
100
+ * own MCP server take the target's spelling. When the block sets `description`,
101
+ * no field is degraded into it: the author wrote that host's description,
102
+ * so no such field is reported in `drops`.
65
103
  *
66
104
  * @public
67
105
  */
68
- const mapFrontmatter = (target, map, hostFields, base, block) => {
106
+ const mapFrontmatter = (target, map, hostFields, base, block, own) => {
69
107
  const coreOverlay = Object.fromEntries(Object.entries(block).filter(([key]) => key in map));
70
108
  const hostOverlay = Object.entries(block).filter(([key]) => hostFields.includes(key));
71
109
  const unknown = Object.keys(block).filter((key) => !(key in map) && !hostFields.includes(key));
@@ -78,6 +116,13 @@ const mapFrontmatter = (target, map, hostFields, base, block) => {
78
116
  const suffixes = [];
79
117
  const sections = [];
80
118
  const unresolved = [];
119
+ const drops = [];
120
+ const dropped = (field, kind) => {
121
+ if (!drops.some((drop) => drop.field === field && drop.kind === kind)) drops.push({
122
+ field,
123
+ kind
124
+ });
125
+ };
81
126
  for (const [field, value] of Object.entries(source)) {
82
127
  if (value === void 0) continue;
83
128
  const entry = map[field];
@@ -89,7 +134,9 @@ const mapFrontmatter = (target, map, hostFields, base, block) => {
89
134
  case "rename":
90
135
  fields[entry.to] = value;
91
136
  break;
92
- case "drop": break;
137
+ case "drop":
138
+ dropped(field, "dropped");
139
+ break;
93
140
  case "translate": {
94
141
  const name = entry.to ?? field;
95
142
  if (entry.table !== "tools") {
@@ -112,24 +159,31 @@ const mapFrontmatter = (target, map, hostFields, base, block) => {
112
159
  });
113
160
  continue;
114
161
  }
115
- const mapped = target.tools.names[name] ?? mcpToolName(target, name);
162
+ const mapped = target.tools.names[name] ?? mcpToolName(target, name, own) ?? (target.tools.unlisted === "keep" ? name : void 0);
116
163
  if (typeof mapped === "string") {
117
164
  if (!names.includes(mapped)) names.push(mapped);
118
- } else if (mapped._tag === "unresolved") unresolved.push({
165
+ } else if (mapped?._tag === "unresolved") unresolved.push({
119
166
  field: `${field}: ${name}`,
120
167
  note: mapped.note
121
168
  });
169
+ else dropped(name, "tool-dropped");
122
170
  }
123
171
  fields[name] = names;
124
172
  break;
125
173
  }
126
174
  case "degrade":
127
175
  if (entry.form === "description-suffix") {
128
- if (!("description" in block)) suffixes.push(`${SUFFIX_LABELS[field] ?? field}: ${asText(value)}`);
129
- } else sections.push({
130
- field,
131
- value
132
- });
176
+ if (!("description" in block)) {
177
+ suffixes.push(`${SUFFIX_LABELS[field] ?? field}: ${asText(value)}`);
178
+ dropped(field, "degraded");
179
+ }
180
+ } else {
181
+ sections.push({
182
+ field,
183
+ value
184
+ });
185
+ dropped(field, "degraded");
186
+ }
133
187
  break;
134
188
  case "unresolved": unresolved.push({
135
189
  field,
@@ -143,6 +197,7 @@ const mapFrontmatter = (target, map, hostFields, base, block) => {
143
197
  fields,
144
198
  sections,
145
199
  unresolved,
200
+ drops,
146
201
  unknown
147
202
  };
148
203
  };
@@ -164,4 +219,4 @@ const appendSections = (body, sections) => {
164
219
  };
165
220
 
166
221
  //#endregion
167
- export { appendSections, mapFrontmatter, splitFrontmatter };
222
+ export { appendSections, mapFrontmatter, splitFrontmatter, splitOwnMcp };