@pluginfinity/engine 0.1.1 → 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 +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 +70 -4
- 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 +340 -96
- package/index.js +6 -3
- package/manifest.js +1 -1
- package/notes.js +29 -0
- package/operations.js +192 -21
- package/package.json +5 -5
- package/server-lib.generated.js +9 -0
- package/server-lib.js +21 -0
- package/servers.js +249 -0
- package/skills.js +45 -14
- package/tokens.js +238 -0
- package/version.js +1 -1
package/operations.js
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
import { BuildStale, ComponentsInvalid, HookEventUnsupported, HookScriptInvalid, HostRejected,
|
|
1
|
+
import { BuildStale, ComponentInvalid, ComponentsInvalid, HookEventUnsupported, HookScriptInvalid, HostRejected, PackageVersionMissing, PathConflict, ShippedFileInvalid, TargetDrift } from "./errors.js";
|
|
2
2
|
import { isJunk } from "./component.js";
|
|
3
3
|
import { readAgents, renderAgent } from "./agents.js";
|
|
4
4
|
import { preparePlugins } from "./selection.js";
|
|
5
|
+
import { ENGINE_VERSION } from "./version.js";
|
|
5
6
|
import { applyEmit, planEmit } from "./emit.js";
|
|
6
7
|
import { hookCommandFiles, hookScripts, renderHooks, targetHooks } from "./hooks.js";
|
|
7
|
-
import { renderManifest, serializeManifest } from "./manifest.js";
|
|
8
|
+
import { pluginName, renderManifest, serializeManifest } from "./manifest.js";
|
|
9
|
+
import { CONFIG_NOTE_PATH, sortNotes } from "./notes.js";
|
|
10
|
+
import { HOOK_LIB_DIR, hookLibFiles } from "./hook-lib.js";
|
|
11
|
+
import { SERVER_LIB_DIR, serverLibFiles } from "./server-lib.js";
|
|
12
|
+
import { mcpServerNames, renderServers, serverFiles } from "./servers.js";
|
|
8
13
|
import { readSkills, renderSkill } from "./skills.js";
|
|
9
14
|
import { Effect, FileSystem, Path, Schema } from "effect";
|
|
10
|
-
import { Run } from "@effected/commands";
|
|
11
15
|
import { KNOWN_TARGET_IDS, TARGETS } from "@pluginfinity/targets";
|
|
16
|
+
import { Run } from "@effected/commands";
|
|
12
17
|
import { ChildProcess } from "effect/process";
|
|
13
18
|
|
|
14
19
|
//#region src/operations.ts
|
|
@@ -66,6 +71,115 @@ const checkScript = (config, script, invoke) => Effect.gen(function* () {
|
|
|
66
71
|
problem: "not-executable"
|
|
67
72
|
}));
|
|
68
73
|
});
|
|
74
|
+
/** Whether `real` is `root` or under it; both are real paths. */
|
|
75
|
+
const isInside = (root, real) => real === root || real.startsWith(`${root}/`);
|
|
76
|
+
/** Whether a plugin-relative path has no empty, `.` or `..` segment. */
|
|
77
|
+
const isNormal = (file) => file.split("/").every((segment) => segment !== "" && segment !== "." && segment !== "..");
|
|
78
|
+
/**
|
|
79
|
+
* A plugin-relative path with its empty, `.` and `..` segments resolved, or
|
|
80
|
+
* `undefined` when it climbs out of the plugin root.
|
|
81
|
+
*/
|
|
82
|
+
const canonical = (file) => {
|
|
83
|
+
const segments = [];
|
|
84
|
+
for (const segment of file.split("/")) {
|
|
85
|
+
if (segment === "" || segment === ".") continue;
|
|
86
|
+
if (segment !== "..") segments.push(segment);
|
|
87
|
+
else if (segments.pop() === void 0) return void 0;
|
|
88
|
+
}
|
|
89
|
+
return segments.join("/");
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* The files one canonical, plugin-relative path ships: the file, or every
|
|
93
|
+
* file under the directory. `spelled` is the path as written, which is what
|
|
94
|
+
* is checked for existence, so a trailing `/` after a file fails as missing.
|
|
95
|
+
* Every shipped file must resolve inside the plugin, through any symlink.
|
|
96
|
+
*/
|
|
97
|
+
const expandShipped = (config, file, spelled, referencedBy) => Effect.gen(function* () {
|
|
98
|
+
const fs = yield* FileSystem.FileSystem;
|
|
99
|
+
const path = yield* Path.Path;
|
|
100
|
+
const fail = (named, problem) => Effect.fail(new ShippedFileInvalid({
|
|
101
|
+
path: config.path,
|
|
102
|
+
file: named,
|
|
103
|
+
referencedBy,
|
|
104
|
+
problem
|
|
105
|
+
}));
|
|
106
|
+
const info = yield* fs.stat(path.join(config.root, spelled)).pipe(Effect.option);
|
|
107
|
+
if (info._tag === "None" || info.value.type !== "File" && info.value.type !== "Directory") return yield* fail(spelled, "missing");
|
|
108
|
+
const files = info.value.type === "Directory" ? yield* sourceFiles(config.root, file) : [file];
|
|
109
|
+
const root = yield* fs.realPath(config.root);
|
|
110
|
+
for (const listed of files) if (!isInside(root, yield* fs.realPath(path.join(config.root, listed)))) return yield* fail(listed, "outside-root");
|
|
111
|
+
return files;
|
|
112
|
+
});
|
|
113
|
+
/**
|
|
114
|
+
* The files `entries` name, each canonical: each file, or every file under
|
|
115
|
+
* each directory. Every one must exist and resolve inside the plugin.
|
|
116
|
+
*/
|
|
117
|
+
const listedFiles = (config, entries) => Effect.gen(function* () {
|
|
118
|
+
const fail = (file, problem) => Effect.fail(new ShippedFileInvalid({
|
|
119
|
+
path: config.path,
|
|
120
|
+
file,
|
|
121
|
+
referencedBy: "files",
|
|
122
|
+
problem
|
|
123
|
+
}));
|
|
124
|
+
const out = [];
|
|
125
|
+
for (const entry of entries) {
|
|
126
|
+
const file = canonical(entry);
|
|
127
|
+
if (file === void 0) return yield* fail(entry, "outside-root");
|
|
128
|
+
if (file === "") return yield* fail(entry, "not-normal");
|
|
129
|
+
out.push(...yield* expandShipped(config, file, file, "files"));
|
|
130
|
+
}
|
|
131
|
+
return out;
|
|
132
|
+
});
|
|
133
|
+
/**
|
|
134
|
+
* The files a server reference ships, failing with `ShippedFileInvalid`
|
|
135
|
+
* unless it is written without `.`, `..` or empty segments (a directory may
|
|
136
|
+
* end in one `/`) and exists inside the plugin. A file ships alone and a
|
|
137
|
+
* directory ships every file under it, like a `files` entry. A whole
|
|
138
|
+
* `command` must be a file, and executable.
|
|
139
|
+
*/
|
|
140
|
+
const serverShipped = (config, spelled, referencedBy, command) => Effect.gen(function* () {
|
|
141
|
+
const fs = yield* FileSystem.FileSystem;
|
|
142
|
+
const path = yield* Path.Path;
|
|
143
|
+
const fail = (problem) => Effect.fail(new ShippedFileInvalid({
|
|
144
|
+
path: config.path,
|
|
145
|
+
file: spelled,
|
|
146
|
+
referencedBy,
|
|
147
|
+
problem
|
|
148
|
+
}));
|
|
149
|
+
const file = spelled.endsWith("/") ? spelled.slice(0, -1) : spelled;
|
|
150
|
+
if (!isNormal(file)) return yield* fail("not-normal");
|
|
151
|
+
if (command) {
|
|
152
|
+
const info = yield* fs.stat(path.join(config.root, file)).pipe(Effect.option);
|
|
153
|
+
if (info._tag === "Some" && info.value.type === "Directory") return yield* fail("directory");
|
|
154
|
+
}
|
|
155
|
+
const files = yield* expandShipped(config, file, spelled, referencedBy);
|
|
156
|
+
if (command) {
|
|
157
|
+
if (((yield* fs.stat(path.join(config.root, file))).mode & 73) === 0) return yield* fail("not-executable");
|
|
158
|
+
}
|
|
159
|
+
return files;
|
|
160
|
+
});
|
|
161
|
+
/**
|
|
162
|
+
* What a target's skill and agent bodies may name: the skills and agents it
|
|
163
|
+
* builds, every file of those skills, and the plugin's own MCP servers. Agent
|
|
164
|
+
* ids and skill commands use the plugin's name on this target; own MCP tools
|
|
165
|
+
* are named, and their run-time `{plugin}` filled, by its Claude name, which
|
|
166
|
+
* Claude Code namespaces MCP tools with.
|
|
167
|
+
*/
|
|
168
|
+
const tokenContext = (target, id, config, skills, agents) => {
|
|
169
|
+
const plugin = pluginName(config, id);
|
|
170
|
+
const built = skills.filter((skill) => skill.frontmatter.targets?.[id] !== false);
|
|
171
|
+
return {
|
|
172
|
+
target,
|
|
173
|
+
plugin,
|
|
174
|
+
skills: new Set(built.map((skill) => skill.name)),
|
|
175
|
+
agents: new Set(agents.filter((agent) => agent.frontmatter.targets?.[id] !== false).map((agent) => agent.name)),
|
|
176
|
+
skillFiles: new Set(built.flatMap((skill) => [`${skill.name}/SKILL.md`, ...skill.files.map((file) => `${skill.name}/${file}`)])),
|
|
177
|
+
own: {
|
|
178
|
+
plugin: pluginName(config, "claude"),
|
|
179
|
+
servers: mcpServerNames(id, config)
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
};
|
|
69
183
|
/**
|
|
70
184
|
* Render every requested target of one plugin and compare each with its build
|
|
71
185
|
* directory.
|
|
@@ -73,14 +187,22 @@ const checkScript = (config, script, invoke) => Effect.gen(function* () {
|
|
|
73
187
|
* @remarks
|
|
74
188
|
* Each target ships the source `hooks/` directory whole, so a script can
|
|
75
189
|
* source its own helpers, except scripts only another target's hooks run; a
|
|
76
|
-
* script outside `hooks/` ships to the targets that run it.
|
|
190
|
+
* script outside `hooks/` ships to the targets that run it. A target with
|
|
191
|
+
* hooks also gets the hook library under `hooks/lib/pluginfinity/`. That path
|
|
192
|
+
* is always reserved, whether or not the target has hooks: no source file may
|
|
193
|
+
* be at it or under it.
|
|
194
|
+
*
|
|
195
|
+
* Each target also ships the files its local MCP and LSP servers name after
|
|
196
|
+
* `${PLUGIN_ROOT}/` (written without `.` or `..` segments, inside the plugin,
|
|
197
|
+
* and an executable file when a whole `command`; a named directory ships every
|
|
198
|
+
* file under it), and every file the `files` key lists, each once. A target
|
|
199
|
+
* with a local server gets the server library under `lib/pluginfinity/`,
|
|
200
|
+
* which is reserved the same way.
|
|
77
201
|
*/
|
|
78
202
|
const planPlugin = (prepared) => Effect.gen(function* () {
|
|
79
203
|
const path = yield* Path.Path;
|
|
80
204
|
const config = prepared.config;
|
|
81
205
|
const version = yield* readVersion(config.root);
|
|
82
|
-
const settings = prepared.targets.map((id) => config.config[id]);
|
|
83
|
-
if (config.config.mcpServers !== void 0 || settings.some((setting) => typeof setting === "object" && setting.mcpServers !== void 0)) return yield* Effect.fail(new NotImplemented({ operation: "build of mcpServers" }));
|
|
84
206
|
const invoke = config.config.scripts?.invoke ?? "bash";
|
|
85
207
|
const hooks = prepared.targets.map((id) => {
|
|
86
208
|
const target = targetOf(id);
|
|
@@ -96,22 +218,39 @@ const planPlugin = (prepared) => Effect.gen(function* () {
|
|
|
96
218
|
events: unsupported.map((u) => u.event)
|
|
97
219
|
}));
|
|
98
220
|
const filesOf = (events) => [...hookScripts(events), ...hookCommandFiles(events)];
|
|
99
|
-
const everyScript = new Set(
|
|
221
|
+
const everyScript = new Set(config.targets.flatMap((id) => filesOf(targetHooks(targetOf(id), id, config.config).events)));
|
|
100
222
|
for (const { events } of hooks) {
|
|
101
223
|
for (const script of hookScripts(events)) yield* checkScript(config, script, invoke);
|
|
102
224
|
for (const file of hookCommandFiles(events)) yield* checkScript(config, file, "bash");
|
|
103
225
|
}
|
|
104
226
|
const hooksDir = yield* sourceFiles(config.root, "hooks");
|
|
227
|
+
const listed = yield* listedFiles(config, config.config.files ?? []);
|
|
105
228
|
const { skills, failures: skillFailures } = yield* readSkills(config.root, KNOWN_TARGET_IDS);
|
|
106
229
|
const { agents, failures: agentFailures } = yield* readAgents(config.root, KNOWN_TARGET_IDS);
|
|
107
230
|
const failures = [...skillFailures, ...agentFailures];
|
|
108
|
-
const
|
|
231
|
+
const keep = (error) => {
|
|
109
232
|
if (!failures.some((seen) => seen.message === error.message)) failures.push(error);
|
|
110
|
-
}
|
|
233
|
+
};
|
|
234
|
+
const collect = (effect) => effect.pipe(Effect.catchTag("ComponentInvalid", (error) => Effect.sync(() => keep(error))), Effect.catchTag("ComponentsInvalid", (error) => Effect.sync(() => error.components.forEach(keep))));
|
|
111
235
|
const planned = [];
|
|
112
|
-
for (const { id, target, events } of hooks) {
|
|
236
|
+
for (const { id, target, events, omitted } of hooks) {
|
|
237
|
+
const notes = omitted.map((event) => ({
|
|
238
|
+
target: id,
|
|
239
|
+
path: CONFIG_NOTE_PATH,
|
|
240
|
+
kind: "hook-omitted",
|
|
241
|
+
name: event
|
|
242
|
+
}));
|
|
113
243
|
const own = new Set(filesOf(events));
|
|
114
|
-
const
|
|
244
|
+
const servers = serverFiles(target, id, config.config);
|
|
245
|
+
const serverFilesShipped = [];
|
|
246
|
+
for (const file of servers.commands) serverFilesShipped.push(...yield* serverShipped(config, file, servers.owners.get(file) ?? "mcpServers", true));
|
|
247
|
+
for (const file of servers.others) serverFilesShipped.push(...yield* serverShipped(config, file, servers.owners.get(file) ?? "mcpServers", false));
|
|
248
|
+
const shipped = [.../* @__PURE__ */ new Set([
|
|
249
|
+
...hooksDir.filter((file) => own.has(file) || !everyScript.has(file)),
|
|
250
|
+
...[...own].filter((script) => !script.startsWith("hooks/")),
|
|
251
|
+
...serverFilesShipped,
|
|
252
|
+
...listed
|
|
253
|
+
])];
|
|
115
254
|
const copied = [];
|
|
116
255
|
for (const file of shipped) copied.push(yield* copyFile(config.root, file));
|
|
117
256
|
const manifest = renderManifest(target, id, config.config, version);
|
|
@@ -120,15 +259,44 @@ const planPlugin = (prepared) => Effect.gen(function* () {
|
|
|
120
259
|
content: serializeManifest(manifest)
|
|
121
260
|
}];
|
|
122
261
|
const hooksFile = renderHooks(target, events, invoke);
|
|
123
|
-
if (hooksFile !== void 0)
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
262
|
+
if (hooksFile !== void 0) {
|
|
263
|
+
generated.push({
|
|
264
|
+
path: target.hooks.path,
|
|
265
|
+
content: hooksFile
|
|
266
|
+
});
|
|
267
|
+
generated.push(...hookLibFiles(id, String(manifest.name), ENGINE_VERSION));
|
|
268
|
+
}
|
|
269
|
+
const rendered = renderServers(target, id, config.config, String(manifest.name), SERVER_LIB_DIR);
|
|
270
|
+
if (rendered.issues.length > 0) {
|
|
271
|
+
const issue = new ComponentInvalid({
|
|
272
|
+
path: config.path,
|
|
273
|
+
target: id,
|
|
274
|
+
issues: rendered.issues
|
|
275
|
+
});
|
|
276
|
+
if (!failures.some((seen) => seen.message === issue.message)) failures.push(issue);
|
|
277
|
+
}
|
|
278
|
+
generated.push(...rendered.files);
|
|
279
|
+
notes.push(...rendered.notes);
|
|
280
|
+
if (rendered.stdio) generated.push(...serverLibFiles());
|
|
281
|
+
const tokens = tokenContext(target, id, config.config, skills, agents);
|
|
282
|
+
for (const skill of skills) {
|
|
283
|
+
const skillRender = yield* collect(renderSkill(target, id, skill, KNOWN_TARGET_IDS, tokens));
|
|
284
|
+
if (skillRender === void 0) continue;
|
|
285
|
+
generated.push(...skillRender.files);
|
|
286
|
+
notes.push(...skillRender.notes);
|
|
287
|
+
}
|
|
128
288
|
for (const agent of agents) {
|
|
129
|
-
const
|
|
130
|
-
if (
|
|
289
|
+
const agentRender = yield* collect(renderAgent(target, id, agent, KNOWN_TARGET_IDS, tokens));
|
|
290
|
+
if (agentRender === void 0) continue;
|
|
291
|
+
generated.push(agentRender.file);
|
|
292
|
+
notes.push(...agentRender.notes);
|
|
131
293
|
}
|
|
294
|
+
const reserved = copied.find((file) => [HOOK_LIB_DIR, SERVER_LIB_DIR].some((dir) => file.path === dir || file.path.startsWith(`${dir}/`)));
|
|
295
|
+
if (reserved !== void 0) return yield* Effect.fail(new PathConflict({
|
|
296
|
+
path: config.path,
|
|
297
|
+
target: id,
|
|
298
|
+
file: reserved.path
|
|
299
|
+
}));
|
|
132
300
|
const copiedPaths = new Set(copied.map((file) => file.path));
|
|
133
301
|
const conflict = generated.find((file) => copiedPaths.has(file.path));
|
|
134
302
|
if (conflict !== void 0) return yield* Effect.fail(new PathConflict({
|
|
@@ -144,6 +312,7 @@ const planPlugin = (prepared) => Effect.gen(function* () {
|
|
|
144
312
|
target: id,
|
|
145
313
|
out,
|
|
146
314
|
plan,
|
|
315
|
+
notes: sortNotes(notes),
|
|
147
316
|
files,
|
|
148
317
|
name: String(manifest.name),
|
|
149
318
|
version
|
|
@@ -181,11 +350,12 @@ const build = (input) => Effect.gen(function* () {
|
|
|
181
350
|
const planned = yield* planPlugin(prepared);
|
|
182
351
|
if (input.check) yield* requireClean(prepared.config.path, planned);
|
|
183
352
|
else for (const target of planned) yield* applyEmit(target.out, target.files, target.plan);
|
|
184
|
-
for (const { config, target, out, plan } of planned) builds.push({
|
|
353
|
+
for (const { config, target, out, plan, notes } of planned) builds.push({
|
|
185
354
|
config,
|
|
186
355
|
target,
|
|
187
356
|
out,
|
|
188
|
-
plan
|
|
357
|
+
plan,
|
|
358
|
+
notes
|
|
189
359
|
});
|
|
190
360
|
}
|
|
191
361
|
return builds;
|
|
@@ -257,7 +427,8 @@ const validate = (input) => Effect.gen(function* () {
|
|
|
257
427
|
config: target.config,
|
|
258
428
|
target: target.target,
|
|
259
429
|
out: target.out,
|
|
260
|
-
host: input.skipHosts ? "skipped" : "passed"
|
|
430
|
+
host: input.skipHosts ? "skipped" : "passed",
|
|
431
|
+
notes: target.notes
|
|
261
432
|
});
|
|
262
433
|
}
|
|
263
434
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluginfinity/engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "The pluginfinity build pipeline shared by every pluginfinity front end: config discovery, read, transform per target, emit and check.",
|
|
6
6
|
"keywords": [
|
|
@@ -35,13 +35,13 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@effected/commands": "^0.11.0",
|
|
38
|
-
"@effected/engine": "^0.
|
|
38
|
+
"@effected/engine": "^0.4.0",
|
|
39
39
|
"@effected/glob": "^0.10.0",
|
|
40
40
|
"@effected/semver": "^0.11.0",
|
|
41
41
|
"@effected/walker": "^0.15.0",
|
|
42
|
-
"@effected/yaml": "^0.19.
|
|
43
|
-
"@pluginfinity/core": "0.
|
|
44
|
-
"@pluginfinity/targets": "0.
|
|
42
|
+
"@effected/yaml": "^0.19.1",
|
|
43
|
+
"@pluginfinity/core": "0.2.0",
|
|
44
|
+
"@pluginfinity/targets": "0.2.0",
|
|
45
45
|
"jiti": "^2.6.1"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/server-lib.generated.ts
|
|
2
|
+
/** The server library's files, embedded so a build needs no asset on disk. */
|
|
3
|
+
const SERVER_LIB_FILES = [{
|
|
4
|
+
name: "server.sh",
|
|
5
|
+
content: "# shellcheck shell=sh\n# pluginfinity server library. `pluginfinity build` writes this file into every\n# target that has a local MCP or LSP server; do not edit a copy under builds/,\n# the next build overwrites it.\n#\n# Source it first in a launcher, and keep `set -u` (or write\n# ${PLUGINFINITY_LIB:?run by the host}) so a launcher run outside the host fails loudly:\n# . \"$PLUGINFINITY_LIB/server.sh\"\n#\n# POSIX sh. Writes nothing to stdout, which carries the server's protocol.\n\n_pf_server_lib_dir=$(cd \"$(dirname \"$PLUGINFINITY_LIB/server.sh\")\" && pwd -P)\n\n# claude or copilot.\nserver_host() { printf '%s\\n' \"${PLUGINFINITY_HOST:-unknown}\"; }\n\n# The build root the launcher runs from.\nserver_plugin_root() { (cd \"$_pf_server_lib_dir/../..\" && pwd -P); }\n\n# Append a line to the plugin's server error log.\nserver_log() {\n _pf_dir=\"${XDG_STATE_HOME:-${HOME:-/nonexistent}/.local/state}/pluginfinity/${PLUGINFINITY_PLUGIN:-unknown}\"\n mkdir -p \"$_pf_dir\" 2>/dev/null || return 0\n printf '%s [%s] %s\\n' \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\" \"$(server_host)\" \"$*\" >>\"$_pf_dir/server-error.log\" 2>/dev/null || return 0\n}\n\n# The project the session works in. Prints it and returns 0, or prints nothing\n# and returns 1 when there is none to report:\n# - Claude with CLAUDE_PROJECT_DIR set: that directory.\n# - Otherwise, when the working directory is the plugin root or inside it:\n# nothing, status 1. Copilot starts an MCP server with its cwd at the plugin\n# root and gives it no project directory, so that cwd says nothing about the\n# project. On Copilot, MCP servers get no project directory; a server should\n# ask its MCP client for roots.\n# - Otherwise: the closest directory above $PWD holding .git, else $PWD\n# (Copilot starts LSP servers at the git root).\nserver_project_dir() {\n if [ \"${PLUGINFINITY_HOST:-}\" = claude ] && [ -n \"${CLAUDE_PROJECT_DIR:-}\" ]; then\n printf '%s\\n' \"$CLAUDE_PROJECT_DIR\"\n return 0\n fi\n _pf_root=$(server_plugin_root)\n _pf_cwd=$(pwd -P)\n case \"$_pf_cwd\" in\n \"$_pf_root\" | \"$_pf_root\"/*) return 1 ;;\n esac\n _pf_probe=$PWD\n while [ -n \"$_pf_probe\" ] && [ \"$_pf_probe\" != / ]; do\n if [ -e \"$_pf_probe/.git\" ]; then\n printf '%s\\n' \"$_pf_probe\"\n return 0\n fi\n _pf_probe=$(dirname \"$_pf_probe\")\n done\n printf '%s\\n' \"$PWD\"\n}\n\n# npm, pnpm, yarn or bun: package.json's packageManager first, then lockfiles.\n_pf_detect_pm() { # project\n _pf_pm=\"\"\n if [ -f \"$1/package.json\" ]; then\n _pf_pm=$(grep -o '\"packageManager\"[[:space:]]*:[[:space:]]*\"[^\"]*\"' \"$1/package.json\" 2>/dev/null |\n sed -E 's/.*:[[:space:]]*\"([a-z]+)@.*/\\1/')\n fi\n case \"$_pf_pm\" in npm | pnpm | yarn | bun)\n printf '%s\\n' \"$_pf_pm\"\n return 0\n ;;\n esac\n if [ -f \"$1/pnpm-lock.yaml\" ]; then\n printf 'pnpm\\n'\n elif [ -f \"$1/bun.lock\" ] || [ -f \"$1/bun.lockb\" ]; then\n printf 'bun\\n'\n elif [ -f \"$1/yarn.lock\" ]; then\n printf 'yarn\\n'\n else\n printf 'npm\\n'\n fi\n}\n\n_pf_install_line() { # pm package\n case \"$1\" in\n pnpm) printf ' pnpm add -D %s\\n' \"$2\" ;;\n yarn) printf ' yarn add -D %s\\n' \"$2\" ;;\n bun) printf ' bun add -d %s\\n' \"$2\" ;;\n *) printf ' npm install --save-dev %s\\n' \"$2\" ;;\n esac\n}\n\n# Exec the project's node_modules/.bin/<bin>, else npx --yes <package>. With no\n# project directory (see server_project_dir) it skips the lookup and the\n# install hint, and goes straight to npx. An optional `--install <package>`\n# straight after the two positionals names a different package in the install\n# hint only; npx still runs <package>. Later args pass through untouched.\nserver_exec_bin() { # bin package [--install install-package] [args...]\n _pf_bin=$1\n _pf_pkg=$2\n _pf_install=$2\n shift 2\n if [ \"${1:-}\" = --install ] && [ $# -ge 2 ]; then\n _pf_install=$2\n shift 2\n fi\n if _pf_project=$(server_project_dir); then\n if [ -x \"$_pf_project/node_modules/.bin/$_pf_bin\" ]; then\n exec \"$_pf_project/node_modules/.bin/$_pf_bin\" \"$@\"\n fi\n _pf_pm=$(_pf_detect_pm \"$_pf_project\")\n {\n printf '%s: %s is not installed in %s.\\n' \"${PLUGINFINITY_PLUGIN:-plugin}\" \"$_pf_bin\" \"$_pf_project\"\n printf 'Install it with:\\n'\n _pf_install_line \"$_pf_pm\" \"$_pf_install\"\n } >&2\n else\n printf '%s: no project directory is known, so %s cannot be looked up in node_modules.\\n' \\\n \"${PLUGINFINITY_PLUGIN:-plugin}\" \"$_pf_bin\" >&2\n fi\n printf 'Falling back to \"npx --yes %s\".\\n' \"$_pf_pkg\" >&2\n exec npx --yes \"$_pf_pkg\" \"$@\"\n}\n"
|
|
6
|
+
}];
|
|
7
|
+
|
|
8
|
+
//#endregion
|
|
9
|
+
export { SERVER_LIB_FILES };
|
package/server-lib.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { SERVER_LIB_FILES } from "./server-lib.generated.js";
|
|
2
|
+
|
|
3
|
+
//#region src/server-lib.ts
|
|
4
|
+
/**
|
|
5
|
+
* Where every build that has a local server gets the server library, relative to the build root.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
const SERVER_LIB_DIR = "lib/pluginfinity";
|
|
10
|
+
/**
|
|
11
|
+
* The server library, as one target's build writes it.
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
const serverLibFiles = () => SERVER_LIB_FILES.map((file) => ({
|
|
16
|
+
path: `${SERVER_LIB_DIR}/${file.name}`,
|
|
17
|
+
content: file.content
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
export { SERVER_LIB_DIR, serverLibFiles };
|
package/servers.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { ConfigIssue } from "./errors.js";
|
|
2
|
+
import { CONFIG_NOTE_PATH } from "./notes.js";
|
|
3
|
+
|
|
4
|
+
//#region src/servers.ts
|
|
5
|
+
const PLACEHOLDER = `\${PLUGIN_ROOT}`;
|
|
6
|
+
const ROOT_FILE = /\$\{PLUGIN_ROOT\}\/([^\s"'`;|&<>()$:,]+)/g;
|
|
7
|
+
const HOST_ROOT = /\$\{(?:CLAUDE|COPILOT)_PLUGIN_ROOT\}|\$(?:CLAUDE_|COPILOT_)?PLUGIN_ROOT(?![A-Za-z0-9_])/;
|
|
8
|
+
const merged = (id, config, key) => {
|
|
9
|
+
const setting = config[id];
|
|
10
|
+
const base = config[key] ?? {};
|
|
11
|
+
const own = typeof setting === "object" ? setting[key] ?? {} : {};
|
|
12
|
+
const out = /* @__PURE__ */ new Map();
|
|
13
|
+
for (const [name, server] of Object.entries(base)) out.set(name, [
|
|
14
|
+
name,
|
|
15
|
+
key,
|
|
16
|
+
server
|
|
17
|
+
]);
|
|
18
|
+
for (const [name, server] of Object.entries(own)) out.set(name, [
|
|
19
|
+
name,
|
|
20
|
+
`${id}.${key}`,
|
|
21
|
+
server
|
|
22
|
+
]);
|
|
23
|
+
return [...out.values()];
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The names of every MCP server the target builds: the base servers and the
|
|
27
|
+
* target's own.
|
|
28
|
+
*/
|
|
29
|
+
const mcpServerNames = (id, config) => new Set(merged(id, config, "mcpServers").map(([name]) => name));
|
|
30
|
+
const isStdio = (server) => server.type === void 0 || server.type === "stdio";
|
|
31
|
+
const rewrite = (value, root) => {
|
|
32
|
+
if (typeof value === "string") return value.replaceAll(PLACEHOLDER, root);
|
|
33
|
+
if (Array.isArray(value)) return value.map((item) => rewrite(item, root));
|
|
34
|
+
if (typeof value === "object" && value !== null) return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, rewrite(v, root)]));
|
|
35
|
+
return value;
|
|
36
|
+
};
|
|
37
|
+
const MCP_ROOT_FIELDS = [
|
|
38
|
+
"command",
|
|
39
|
+
"args",
|
|
40
|
+
"env",
|
|
41
|
+
"cwd"
|
|
42
|
+
];
|
|
43
|
+
const LSP_ROOT_FIELDS = [
|
|
44
|
+
"command",
|
|
45
|
+
"args",
|
|
46
|
+
"env",
|
|
47
|
+
"workspaceFolder"
|
|
48
|
+
];
|
|
49
|
+
const strings = (value) => {
|
|
50
|
+
if (typeof value === "string") return [value];
|
|
51
|
+
if (Array.isArray(value)) return value.flatMap(strings);
|
|
52
|
+
if (typeof value === "object" && value !== null) return Object.values(value).flatMap(strings);
|
|
53
|
+
return [];
|
|
54
|
+
};
|
|
55
|
+
const hostSpellings = (server, fields, key, issues) => {
|
|
56
|
+
for (const field of fields) {
|
|
57
|
+
const spelled = strings(server[field]).find((value) => HOST_ROOT.test(value))?.match(HOST_ROOT)?.[0];
|
|
58
|
+
if (spelled === void 0) continue;
|
|
59
|
+
issues.push(ConfigIssue.make({
|
|
60
|
+
key: `${key}.${field}`,
|
|
61
|
+
message: `${spelled} is one host's spelling of the plugin root; write \${PLUGIN_ROOT} and the build rewrites it for each target`
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
const only = (server, fields) => Object.fromEntries(Object.entries(server).filter(([field]) => fields.includes(field)));
|
|
66
|
+
const rewriteFields = (server, fields, root) => Object.fromEntries(Object.entries(server).map(([field, value]) => [field, fields.includes(field) ? rewrite(value, root) : value]));
|
|
67
|
+
const serialize = (value) => `${JSON.stringify(value, null, " ")}\n`;
|
|
68
|
+
const injectedEnv = (id, plugin, root, libDir) => ({
|
|
69
|
+
PLUGINFINITY_HOST: id,
|
|
70
|
+
PLUGINFINITY_PLUGIN: plugin,
|
|
71
|
+
PLUGINFINITY_LIB: `${root}/${libDir}`
|
|
72
|
+
});
|
|
73
|
+
const withEnv = (server, env) => ({
|
|
74
|
+
...server,
|
|
75
|
+
env: {
|
|
76
|
+
...server.env ?? {},
|
|
77
|
+
...env
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
const rootOf = (spelling, key, issues) => {
|
|
81
|
+
if (typeof spelling === "string") return spelling;
|
|
82
|
+
issues.push(ConfigIssue.make({
|
|
83
|
+
key,
|
|
84
|
+
message: spelling.note
|
|
85
|
+
}));
|
|
86
|
+
};
|
|
87
|
+
const mapField = (entry, field, value, out, key, issues, dropped) => {
|
|
88
|
+
switch (entry?._tag) {
|
|
89
|
+
case "keep":
|
|
90
|
+
out[field] = value;
|
|
91
|
+
return;
|
|
92
|
+
case "rename":
|
|
93
|
+
out[entry.to] = value;
|
|
94
|
+
return;
|
|
95
|
+
case "drop":
|
|
96
|
+
dropped.push(key);
|
|
97
|
+
return;
|
|
98
|
+
case "unresolved":
|
|
99
|
+
issues.push(ConfigIssue.make({
|
|
100
|
+
key,
|
|
101
|
+
message: entry.note
|
|
102
|
+
}));
|
|
103
|
+
return;
|
|
104
|
+
default: issues.push(ConfigIssue.make({
|
|
105
|
+
key,
|
|
106
|
+
message: `no server rule for field ${field}`
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const mcpEntries = ({ servers, env, root, issues }, copilot) => {
|
|
111
|
+
const out = {};
|
|
112
|
+
for (const [name, origin, server] of servers) {
|
|
113
|
+
if (!isStdio(server)) {
|
|
114
|
+
out[name] = copilot && server.type === "http" ? {
|
|
115
|
+
...server,
|
|
116
|
+
type: "streamable-http"
|
|
117
|
+
} : { ...server };
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
hostSpellings(server, MCP_ROOT_FIELDS, `${origin}.${name}`, issues);
|
|
121
|
+
const rewritten = rewriteFields(server, MCP_ROOT_FIELDS, root);
|
|
122
|
+
if (!copilot && "cwd" in rewritten) {
|
|
123
|
+
issues.push(ConfigIssue.make({
|
|
124
|
+
key: `${origin}.${name}.cwd`,
|
|
125
|
+
message: "Claude Code ignores an MCP server's cwd (it runs in the project directory); set cwd under copilot.mcpServers or cd in the launcher instead"
|
|
126
|
+
}));
|
|
127
|
+
delete rewritten.cwd;
|
|
128
|
+
}
|
|
129
|
+
out[name] = copilot ? {
|
|
130
|
+
type: "stdio",
|
|
131
|
+
...withEnv(rewritten, env)
|
|
132
|
+
} : withEnv(rewritten, env);
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
};
|
|
136
|
+
const MCP_ENCODERS = {
|
|
137
|
+
"claude-mcp-json": (input) => ({ mcpServers: mcpEntries(input, false) }),
|
|
138
|
+
"agent-plugins-mcp-1.0": (input) => ({
|
|
139
|
+
...input.target.mcp.schema === void 0 ? {} : { $schema: input.target.mcp.schema },
|
|
140
|
+
mcpServers: mcpEntries(input, true)
|
|
141
|
+
})
|
|
142
|
+
};
|
|
143
|
+
const lspEntries = ({ target, servers, env, root, issues, dropped }) => {
|
|
144
|
+
const out = {};
|
|
145
|
+
for (const [name, origin, server] of servers) {
|
|
146
|
+
const entry = {};
|
|
147
|
+
hostSpellings(server, LSP_ROOT_FIELDS, `${origin}.${name}`, issues);
|
|
148
|
+
for (const [field, value] of Object.entries(rewriteFields(server, LSP_ROOT_FIELDS, root))) mapField(target.lsp.fields[field], field, value, entry, `${origin}.${name}.${field}`, issues, dropped);
|
|
149
|
+
out[name] = withEnv(entry, env);
|
|
150
|
+
}
|
|
151
|
+
return out;
|
|
152
|
+
};
|
|
153
|
+
const LSP_ENCODERS = {
|
|
154
|
+
"claude-lsp-json": (input) => lspEntries(input),
|
|
155
|
+
"copilot-lsp-json": (input) => ({ lspServers: lspEntries(input) })
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* The target's MCP and LSP config files: base servers with the target's
|
|
159
|
+
* overrides applied by name, `${PLUGIN_ROOT}` rewritten to the target's
|
|
160
|
+
* spelling, LSP fields mapped through the target's field map, and the
|
|
161
|
+
* server-library variables added to every local server's `env`.
|
|
162
|
+
*
|
|
163
|
+
* @public
|
|
164
|
+
*/
|
|
165
|
+
const renderServers = (target, id, config, plugin, libDir) => {
|
|
166
|
+
const issues = [];
|
|
167
|
+
const dropped = [];
|
|
168
|
+
const files = [];
|
|
169
|
+
const mcp = merged(id, config, "mcpServers");
|
|
170
|
+
const lsp = merged(id, config, "lspServers");
|
|
171
|
+
if (mcp.length > 0) {
|
|
172
|
+
const root = rootOf(target.pluginRoot.mcp, "mcpServers", issues);
|
|
173
|
+
if (root !== void 0) {
|
|
174
|
+
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
|
+
}))
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (lsp.length > 0) {
|
|
188
|
+
const root = rootOf(target.pluginRoot.lsp, "lspServers", issues);
|
|
189
|
+
if (root !== void 0) {
|
|
190
|
+
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
|
+
}))
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
files,
|
|
206
|
+
issues,
|
|
207
|
+
stdio: mcp.some(([, , server]) => isStdio(server)) || lsp.length > 0,
|
|
208
|
+
notes: dropped.map((name) => ({
|
|
209
|
+
target: id,
|
|
210
|
+
path: CONFIG_NOTE_PATH,
|
|
211
|
+
kind: "dropped",
|
|
212
|
+
name
|
|
213
|
+
}))
|
|
214
|
+
};
|
|
215
|
+
};
|
|
216
|
+
const filesIn = (value) => strings(value).flatMap((text) => [...text.matchAll(ROOT_FILE)].map((match) => match[1] ?? ""));
|
|
217
|
+
/**
|
|
218
|
+
* The plugin files a target's servers name after `${PLUGIN_ROOT}/`: those a
|
|
219
|
+
* whole `command` names, which must be executable, and every other one.
|
|
220
|
+
* Only the fields the root placeholder is documented in are scanned.
|
|
221
|
+
*
|
|
222
|
+
* @public
|
|
223
|
+
*/
|
|
224
|
+
const serverFiles = (target, id, config) => {
|
|
225
|
+
const mcp = merged(id, config, "mcpServers").flatMap(([name, origin, server]) => isStdio(server) ? [[`${origin}.${name}`, only(server, MCP_ROOT_FIELDS)]] : []);
|
|
226
|
+
const lsp = merged(id, config, "lspServers").map(([name, origin, server]) => [`${origin}.${name}`, only(server, LSP_ROOT_FIELDS.filter((field) => target.lsp.fields[field]?._tag !== "unresolved"))]);
|
|
227
|
+
const servers = [...mcp, ...lsp];
|
|
228
|
+
const commands = /* @__PURE__ */ new Set();
|
|
229
|
+
const others = /* @__PURE__ */ new Set();
|
|
230
|
+
const owners = /* @__PURE__ */ new Map();
|
|
231
|
+
for (const [owner, server] of servers) {
|
|
232
|
+
const command = typeof server.command === "string" ? server.command : "";
|
|
233
|
+
const whole = command.startsWith(`${PLACEHOLDER}/`) && filesIn(command)[0] === command.slice(PLACEHOLDER.length + 1);
|
|
234
|
+
if (whole) commands.add(command.slice(PLACEHOLDER.length + 1));
|
|
235
|
+
for (const file of filesIn({
|
|
236
|
+
...server,
|
|
237
|
+
command: whole ? "" : command
|
|
238
|
+
})) others.add(file);
|
|
239
|
+
for (const file of filesIn(server)) if (!owners.has(file)) owners.set(file, owner);
|
|
240
|
+
}
|
|
241
|
+
return {
|
|
242
|
+
commands: [...commands],
|
|
243
|
+
others: [...others].filter((file) => !commands.has(file)),
|
|
244
|
+
owners
|
|
245
|
+
};
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
//#endregion
|
|
249
|
+
export { mcpServerNames, renderServers, serverFiles };
|