@pluginfinity/engine 0.2.0 → 0.3.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/server-lib.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { LIB_DIR } from "./lib-files.js";
1
2
  import { SERVER_LIB_FILES } from "./server-lib.generated.js";
2
3
 
3
4
  //#region src/server-lib.ts
@@ -6,9 +7,9 @@ import { SERVER_LIB_FILES } from "./server-lib.generated.js";
6
7
  *
7
8
  * @public
8
9
  */
9
- const SERVER_LIB_DIR = "lib/pluginfinity";
10
+ const SERVER_LIB_DIR = LIB_DIR;
10
11
  /**
11
- * The server library, as one target's build writes it.
12
+ * The server library, as one target's build writes it. `log.sh` and `host.sh` come from `libFiles`.
12
13
  *
13
14
  * @public
14
15
  */
package/servers.js CHANGED
@@ -134,7 +134,7 @@ const mcpEntries = ({ servers, env, root, issues }, copilot) => {
134
134
  return out;
135
135
  };
136
136
  const MCP_ENCODERS = {
137
- "claude-mcp-json": (input) => ({ mcpServers: mcpEntries(input, false) }),
137
+ "claude-mcp-servers": (input) => mcpEntries(input, false),
138
138
  "agent-plugins-mcp-1.0": (input) => ({
139
139
  ...input.target.mcp.schema === void 0 ? {} : { $schema: input.target.mcp.schema },
140
140
  mcpServers: mcpEntries(input, true)
@@ -151,14 +151,23 @@ const lspEntries = ({ target, servers, env, root, issues, dropped }) => {
151
151
  return out;
152
152
  };
153
153
  const LSP_ENCODERS = {
154
- "claude-lsp-json": (input) => lspEntries(input),
154
+ "claude-lsp-servers": (input) => lspEntries(input),
155
155
  "copilot-lsp-json": (input) => ({ lspServers: lspEntries(input) })
156
156
  };
157
+ const place = (placement, value, files, manifest) => {
158
+ if (placement._tag === "file") files.push({
159
+ path: placement.path,
160
+ content: serialize(value)
161
+ });
162
+ else manifest[placement.key] = value;
163
+ };
157
164
  /**
158
- * The target's MCP and LSP config files: base servers with the target's
165
+ * The target's MCP and LSP configs: base servers with the target's
159
166
  * overrides applied by name, `${PLUGIN_ROOT}` rewritten to the target's
160
167
  * spelling, LSP fields mapped through the target's field map, and the
161
- * server-library variables added to every local server's `env`.
168
+ * server-library variables added to every local server's `env`. Each config
169
+ * goes where the target places it: a file of its own, or inline under a
170
+ * manifest key, for `renderManifest` to write.
162
171
  *
163
172
  * @public
164
173
  */
@@ -166,43 +175,41 @@ const renderServers = (target, id, config, plugin, libDir) => {
166
175
  const issues = [];
167
176
  const dropped = [];
168
177
  const files = [];
178
+ const manifest = {};
169
179
  const mcp = merged(id, config, "mcpServers");
170
180
  const lsp = merged(id, config, "lspServers");
171
181
  if (mcp.length > 0) {
172
182
  const root = rootOf(target.pluginRoot.mcp, "mcpServers", issues);
173
183
  if (root !== void 0) {
174
184
  const env = injectedEnv(id, plugin, root, libDir);
175
- files.push({
176
- path: target.mcp.path,
177
- content: serialize(MCP_ENCODERS[target.mcp.format]({
178
- target,
179
- servers: mcp,
180
- env,
181
- root,
182
- issues
183
- }))
185
+ const value = MCP_ENCODERS[target.mcp.format]({
186
+ target,
187
+ servers: mcp,
188
+ env,
189
+ root,
190
+ issues
184
191
  });
192
+ place(target.mcp.placement, value, files, manifest);
185
193
  }
186
194
  }
187
195
  if (lsp.length > 0) {
188
196
  const root = rootOf(target.pluginRoot.lsp, "lspServers", issues);
189
197
  if (root !== void 0) {
190
198
  const env = injectedEnv(id, plugin, root, libDir);
191
- files.push({
192
- path: target.lsp.path,
193
- content: serialize(LSP_ENCODERS[target.lsp.format]({
194
- target,
195
- servers: lsp,
196
- env,
197
- root,
198
- issues,
199
- dropped
200
- }))
199
+ const value = LSP_ENCODERS[target.lsp.format]({
200
+ target,
201
+ servers: lsp,
202
+ env,
203
+ root,
204
+ issues,
205
+ dropped
201
206
  });
207
+ place(target.lsp.placement, value, files, manifest);
202
208
  }
203
209
  }
204
210
  return {
205
211
  files,
212
+ manifest,
206
213
  issues,
207
214
  stdio: mcp.some(([, , server]) => isStdio(server)) || lsp.length > 0,
208
215
  notes: dropped.map((name) => ({
package/skills.js CHANGED
@@ -100,7 +100,11 @@ const renderSkill = (target, id, skill, known, tokens) => Effect.gen(function* (
100
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`));
101
101
  const body = mapHostBlocks(skill.body, id, known);
102
102
  if ("problem" in body) return yield* Effect.fail(invalid(skill.path, [issue(`line ${body.problem.line + skill.bodyOffset}`, body.problem.message)]));
103
- const rendered = renderTokens(body.text, tokens);
103
+ const own = {
104
+ ...tokens,
105
+ skill: skill.name
106
+ };
107
+ const rendered = renderTokens(body.text, own);
104
108
  if ("problems" in rendered) problems.push(...lineIssues(rendered.problems, body.lines, skill.bodyOffset));
105
109
  const failures = problems.length > 0 ? [invalid(skill.path, problems, id)] : [];
106
110
  const yaml = yield* frontmatterText({
@@ -131,7 +135,7 @@ const renderSkill = (target, id, skill, known, tokens) => Effect.gen(function* (
131
135
  failures.push(invalid(absolute, [issue(`line ${processed.problem.line}`, processed.problem.message)]));
132
136
  continue;
133
137
  }
134
- const tokened = renderTokens(processed.text, tokens);
138
+ const tokened = renderTokens(processed.text, own);
135
139
  if ("problems" in tokened) {
136
140
  failures.push(invalid(absolute, lineIssues(tokened.problems, processed.lines, 0), id));
137
141
  continue;
package/tokens.js CHANGED
@@ -6,10 +6,11 @@ const KINDS = /* @__PURE__ */ new Set([
6
6
  "tool",
7
7
  "agent",
8
8
  "skill",
9
+ "skill_dir",
9
10
  "plugin_root"
10
11
  ]);
11
- /** The first word after `{{`: up to whitespace or a brace. */
12
- const KIND = /^\s*([^\s{}]+)/;
12
+ /** The first word after `{{`: up to whitespace, a brace, a pipe or a backtick. */
13
+ const KIND = /^\s*([^\s{}`|]+)/;
13
14
  const LINK = /(!?)\[([^[\]]*)\]\(\s*<?pluginfinity:\/\/([^\s<>()]*)>?\s*\)/g;
14
15
  /** Every `pluginfinity://` occurrence, in any case. */
15
16
  const SCHEMES = /pluginfinity:\/\//gi;
@@ -49,15 +50,43 @@ const tool = (name, raw, ctx) => {
49
50
  /** Spell the inside of one `{{…}}` that opens with a known kind; `raw` is the whole token as written. */
50
51
  const token = (inner, raw, ctx) => {
51
52
  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
+ const bar = inner.indexOf("|");
54
+ const head = bar === -1 ? inner : inner.slice(0, bar);
55
+ const fallback = bar === -1 ? void 0 : inner.slice(bar + 1).trim();
56
+ const [kind = "", ...args] = head.trim().split(/\s+/).filter((part) => part.length > 0);
57
+ let code = false;
58
+ if (head.includes("`")) {
59
+ if (kind !== "tool") return { problem: `${raw}: only a tool token takes a backtick-wrapped name` };
60
+ const wrapped = /^\s*tool\s+`([^`\s]+)`\s*$/.exec(head);
61
+ if (wrapped === null) return { problem: `${raw}: wrap the tool name in exactly one pair of backticks, as {{tool \`Name\`}}` };
62
+ code = true;
63
+ args[0] = wrapped[1];
64
+ args.length = 1;
65
+ }
66
+ if (fallback !== void 0 && kind !== "tool") return { problem: `${raw}: only a tool token takes a | fallback` };
53
67
  if (kind === "plugin_root") {
54
68
  if (args.length > 0) return { problem: `${raw}: plugin_root takes no argument` };
55
69
  return spelling(ctx.target.pluginRoot.body, raw);
56
70
  }
57
71
  const [name] = args;
72
+ if (kind === "skill_dir") {
73
+ if (args.length > 1) return { problem: `${raw} takes one skill_dir name` };
74
+ if (name === void 0 && ctx.skill === void 0) return { problem: `${raw}: an agent has no skill directory; name a skill, {{skill_dir <skill>}}` };
75
+ const skill = name ?? ctx.skill;
76
+ if (!ctx.skills.has(skill)) return { problem: `${raw}: this plugin has no skill "${skill}"` };
77
+ const spellings = ctx.target.skills.dirSpelling;
78
+ const chosen = ctx.skill === void 0 ? spellings.agent : skill === ctx.skill ? spellings.own : spellings.other;
79
+ const spelled = spelling(chosen, raw);
80
+ return "problem" in spelled ? spelled : { value: fill(spelled.value, { skill }) };
81
+ }
58
82
  if (name === void 0) return { problem: `${raw} needs ${article(kind)} name` };
59
83
  if (args.length > 1) return { problem: `${raw} takes one ${kind} name` };
60
- if (kind === "tool") return tool(name, raw, ctx);
84
+ if (kind === "tool") {
85
+ if (fallback === "") return { problem: `${raw}: the fallback after | is empty` };
86
+ const spelled = tool(name, raw, ctx);
87
+ if (fallback !== void 0 && "problem" in spelled) return { value: fallback };
88
+ return code && "value" in spelled ? { value: `\`${spelled.value}\`` } : spelled;
89
+ }
61
90
  if (kind === "agent") {
62
91
  if (!ctx.agents.has(name)) return { problem: `${raw}: this plugin has no agent "${name}"` };
63
92
  return { value: fill(ctx.target.agents.id, {
@@ -189,8 +218,45 @@ const links = (line, ctx, problems) => {
189
218
  * or report every one it cannot render.
190
219
  *
191
220
  * @remarks
192
- * A token is `{{tool <name>}}`, `{{agent <name>}}`, `{{skill <name>}}` or
193
- * `{{plugin_root}}`, with whitespace allowed inside the braces, on one line.
221
+ * A token is one of the following, with whitespace allowed inside the braces,
222
+ * on one line:
223
+ *
224
+ * ```text
225
+ * {{tool <name>}}
226
+ * {{tool <name> | <fallback>}}
227
+ * {{agent <name>}}
228
+ * {{skill <name>}}
229
+ * {{skill_dir}}
230
+ * {{skill_dir <name>}}
231
+ * {{plugin_root}}
232
+ * ```
233
+ *
234
+ * `{{skill_dir}}` is the directory of the skill whose body it is written in,
235
+ * and `{{skill_dir <name>}}` that of the named skill (naming the own skill is
236
+ * the own form). Claude spells them `${CLAUDE_SKILL_DIR}` and
237
+ * `${CLAUDE_PLUGIN_ROOT}/skills/<name>`, which it expands in skill bodies;
238
+ * Copilot expands nothing, so on Copilot the token is a placeholder the model
239
+ * resolves from the "Base directory for this skill" line the host puts above
240
+ * the body (`<skill base directory>`, and `<skill base directory>/../<name>`
241
+ * for a sibling), not a literal path. An agent has no skill directory, so the
242
+ * bare form there is a problem, as is a named skill on a target with no
243
+ * spelling for it from an agent (Copilot), an unknown skill or one the target
244
+ * does not build. It takes neither a `|` fallback nor backticks.
245
+ *
246
+ * A tool token may wrap its name in exactly one pair of backticks:
247
+ *
248
+ * ```text
249
+ * {{tool `Name`}}
250
+ * {{tool `Name` | <fallback>}}
251
+ * ```
252
+ *
253
+ * It then renders as a code span of the spelling where the tool spells, and as
254
+ * the fallback in plain text where it does not;
255
+ * unbalanced or doubled backticks, and backticks on any other kind, are
256
+ * problems. Only a tool token takes
257
+ * a `|` fallback: literal prose, trimmed, that replaces the token on a target
258
+ * where the tool has no spelling (and is discarded where it has one). It may
259
+ * not be empty or contain `{` or `}`.
194
260
  * Tokens are replaced everywhere, fenced and inline code included; `\{{`
195
261
  * before a token renders it literally, and before any other `{{` the
196
262
  * backslash stays. A `{{` whose first word is not a kind is text, so
package/tool-map.js ADDED
@@ -0,0 +1,35 @@
1
+ //#region src/tool-map.ts
2
+ const singleQuote = (value) => `'${value.replaceAll("'", `'\\''`)}'`;
3
+ /**
4
+ * The generated `tools.sh` the hook library sources to spell a Claude tool
5
+ * name the way the host spells it at run time.
6
+ *
7
+ * @remarks
8
+ * `plugin` is the plugin's Claude name: Claude Code namespaces a plugin's MCP
9
+ * tools with it, so a hook sees an own tool as
10
+ * `mcp__plugin_<plugin>_<server>__<tool>` on every host, and the run-time
11
+ * template's `{plugin}` is filled from it. Names the target leaves unresolved
12
+ * are omitted, which `hook_tool_name` reports as no spelling.
13
+ *
14
+ * @internal
15
+ */
16
+ const renderToolMap = (target, plugin, servers, has) => {
17
+ const runtime = target.tools.runtime;
18
+ const pairs = Object.entries(runtime.names).flatMap(([name, spelled]) => typeof spelled === "string" ? [`${name}=${spelled}`] : []);
19
+ return [
20
+ "# Generated by pluginfinity. Do not edit.",
21
+ `_PF_TOOLS=${singleQuote(pairs.join("\n"))}`,
22
+ `_PF_TOOLS_PLUGIN=${singleQuote(plugin)}`,
23
+ `_PF_TOOLS_MCP=${singleQuote(runtime.mcp)}`,
24
+ `_PF_TOOLS_SERVERS=${singleQuote(servers.join(" "))}`,
25
+ `_PF_TOOLS_UNLISTED=${runtime.unlisted}`,
26
+ `_PF_HAS_SKILLS=${singleQuote(has.skills.join(" "))}`,
27
+ `_PF_HAS_AGENTS=${singleQuote(has.agents.join(" "))}`,
28
+ `_PF_HAS_MONITORS=${singleQuote(("path" in target.monitors ? has.monitors : []).join(" "))}`,
29
+ `_PF_HAS_SERVERS=${singleQuote(servers.join(" "))}`,
30
+ ""
31
+ ].join("\n");
32
+ };
33
+
34
+ //#endregion
35
+ export { renderToolMap };
package/version.js CHANGED
@@ -12,7 +12,7 @@
12
12
  *
13
13
  * @public
14
14
  */
15
- const ENGINE_VERSION = "0.2.0";
15
+ const ENGINE_VERSION = "0.3.0";
16
16
 
17
17
  //#endregion
18
18
  export { ENGINE_VERSION };