@pluginfinity/engine 0.2.1 → 0.3.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/env-lib.generated.js +12 -0
- package/env.js +128 -0
- package/errors.js +25 -6
- package/hook-lib.generated.js +1 -1
- package/hook-lib.js +15 -8
- package/hook-output.js +68 -0
- package/hooks.js +105 -12
- package/index.d.ts +369 -13
- package/index.js +6 -3
- package/lib-files.js +39 -0
- package/log-lib.generated.js +9 -0
- package/logs.js +147 -0
- package/monitor-lib.generated.js +9 -0
- package/monitors.js +88 -0
- package/notes.js +8 -1
- package/operations.js +129 -23
- package/package.json +9 -9
- package/server-lib.generated.js +1 -1
- package/server-lib.js +3 -2
- package/skills.js +6 -2
- package/tokens.js +72 -6
- package/tool-map.js +35 -0
- package/version.js +1 -1
package/lib-files.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { envLibFiles } from "./env.js";
|
|
2
|
+
import { renderHostFile } from "./hook-lib.js";
|
|
3
|
+
import { LOG_LIB_FILES } from "./log-lib.generated.js";
|
|
4
|
+
import { MONITOR_LIB_FILES } from "./monitor-lib.generated.js";
|
|
5
|
+
|
|
6
|
+
//#region src/lib-files.ts
|
|
7
|
+
/**
|
|
8
|
+
* Where every build gets the shared libraries (`log.sh`, `host.sh`, the server library when
|
|
9
|
+
* there is a local server, `monitor.sh` when a target has monitors, and `env.sh` with its
|
|
10
|
+
* runner `env-run.sh` when the config declares `env`), relative to the build root.
|
|
11
|
+
*
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
const LIB_DIR = "lib/pluginfinity";
|
|
15
|
+
/**
|
|
16
|
+
* The files every build carries under `lib/pluginfinity`: the logging library and the
|
|
17
|
+
* generated `host.sh` that names the host and plugin, plus the monitor library when the
|
|
18
|
+
* target has monitors and the session env library when the config declares `env`.
|
|
19
|
+
*
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
const libFiles = (host, plugin, version, options) => [
|
|
23
|
+
...LOG_LIB_FILES.map((file) => ({
|
|
24
|
+
path: `${LIB_DIR}/${file.name}`,
|
|
25
|
+
content: file.content
|
|
26
|
+
})),
|
|
27
|
+
{
|
|
28
|
+
path: `${LIB_DIR}/host.sh`,
|
|
29
|
+
content: renderHostFile(host, plugin, version)
|
|
30
|
+
},
|
|
31
|
+
...options.monitors ? MONITOR_LIB_FILES.map((file) => ({
|
|
32
|
+
path: `${LIB_DIR}/${file.name}`,
|
|
33
|
+
content: file.content
|
|
34
|
+
})) : [],
|
|
35
|
+
...options.env === void 0 ? [] : envLibFiles(options.env, LIB_DIR)
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { LIB_DIR, libFiles };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/log-lib.generated.ts
|
|
2
|
+
/** The logging library's files, embedded so a build needs no asset on disk. */
|
|
3
|
+
const LOG_LIB_FILES = [{
|
|
4
|
+
name: "log.sh",
|
|
5
|
+
content: "# shellcheck shell=sh\n# pluginfinity logging. `pluginfinity build` writes this file into every\n# target at lib/pluginfinity/log.sh; do not edit a copy under builds/, the next\n# build overwrites it.\n#\n# POSIX sh. Writes nothing to stdout. Set _pf_log_dir to this file's directory,\n# source it, then call pf_log / pf_debug. From a skill script:\n# _pf_log_dir=\"$(dirname \"$0\")/../../../lib/pluginfinity\"; . \"$_pf_log_dir/log.sh\"\n#\n# Lines go to ${XDG_STATE_HOME:-$HOME/.local/state}/pluginfinity/<plugin>/error.log\n# and, when PLUGINFINITY_DEBUG=1, debug.log, as\n# <ISO-8601 UTC> [<host>] <component>/<script>: <message>\n\n# POSIX sh cannot find a sourced file's own path, so the sourcer sets\n# _pf_log_dir first. host.sh there names the host and plugin.\nif [ -z \"${PLUGINFINITY_PLUGIN:-}\" ] && [ -n \"${_pf_log_dir:-}\" ] && [ -f \"$_pf_log_dir/host.sh\" ]; then\n # shellcheck source=/dev/null\n . \"$_pf_log_dir/host.sh\" 2>/dev/null || :\nfi\n\n_pf_log_write() { # file component message\n _pf_d=\"${XDG_STATE_HOME:-${HOME:-/nonexistent}/.local/state}/pluginfinity/${PLUGINFINITY_PLUGIN:-unknown}\"\n mkdir -p \"$_pf_d\" 2>/dev/null || return 0\n printf '%s [%s] %s/%s: %s\\n' \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\" \"${PLUGINFINITY_HOST:-unknown}\" \\\n \"$2\" \"$(basename \"$0\")\" \"$3\" >>\"$_pf_d/$1\" 2>/dev/null || return 0\n}\n\n# True when PLUGINFINITY_DEBUG=1.\npf_debug_on() { [ \"${PLUGINFINITY_DEBUG:-0}\" = 1 ]; }\n\n# pf_log <component> <message...>: append to error.log. Never fails.\npf_log() {\n _pf_c=${1:-}\n if [ $# -gt 0 ]; then shift; fi\n _pf_log_write error.log \"$_pf_c\" \"$*\"\n return 0\n}\n\n# pf_debug <component> <message...>: append to debug.log when PLUGINFINITY_DEBUG=1.\npf_debug() {\n pf_debug_on || return 0\n _pf_c=${1:-}\n if [ $# -gt 0 ]; then shift; fi\n _pf_log_write debug.log \"$_pf_c\" \"$*\"\n return 0\n}\n\nscript_log() { pf_log script \"$@\"; }\nscript_debug() { pf_debug script \"$@\"; }\n"
|
|
6
|
+
}];
|
|
7
|
+
|
|
8
|
+
//#endregion
|
|
9
|
+
export { LOG_LIB_FILES };
|
package/logs.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { preparePlugins } from "./selection.js";
|
|
2
|
+
import { pluginName } from "./manifest.js";
|
|
3
|
+
import { Effect, FileSystem, Path } from "effect";
|
|
4
|
+
|
|
5
|
+
//#region src/logs.ts
|
|
6
|
+
const LINE = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z) \[([^\]]*)\] (hook|server|monitor|script)\/([^:]*): (.*)$/;
|
|
7
|
+
/**
|
|
8
|
+
* Parse one log line, or carry it through as raw when it does not follow the
|
|
9
|
+
* standard.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
const parseLogLine = (line, plugin, file) => {
|
|
14
|
+
const match = LINE.exec(line);
|
|
15
|
+
if (match === null) return {
|
|
16
|
+
raw: line,
|
|
17
|
+
plugin,
|
|
18
|
+
file
|
|
19
|
+
};
|
|
20
|
+
return {
|
|
21
|
+
ts: match[1],
|
|
22
|
+
host: match[2],
|
|
23
|
+
component: match[3],
|
|
24
|
+
script: match[4],
|
|
25
|
+
message: match[5],
|
|
26
|
+
plugin,
|
|
27
|
+
file
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The directory every plugin's logs live under: `<stateHome>/pluginfinity`.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
const logRoot = (stateHome) => Effect.map(Path.Path, (path) => path.join(stateHome, "pluginfinity"));
|
|
36
|
+
/**
|
|
37
|
+
* One plugin's log directory: `<stateHome>/pluginfinity/<plugin>`.
|
|
38
|
+
*
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
const logDirectory = (stateHome, plugin) => Effect.gen(function* () {
|
|
42
|
+
return (yield* Path.Path).join(yield* logRoot(stateHome), plugin);
|
|
43
|
+
});
|
|
44
|
+
/**
|
|
45
|
+
* The plugin names that have a log directory, sorted; none when the state
|
|
46
|
+
* directory does not exist.
|
|
47
|
+
*
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
const listLogPlugins = (stateHome) => Effect.gen(function* () {
|
|
51
|
+
const fs = yield* FileSystem.FileSystem;
|
|
52
|
+
const path = yield* Path.Path;
|
|
53
|
+
const root = yield* logRoot(stateHome);
|
|
54
|
+
if (!(yield* fs.exists(root))) return [];
|
|
55
|
+
const names = [];
|
|
56
|
+
for (const entry of yield* fs.readDirectory(root)) if ((yield* fs.stat(path.join(root, entry))).type === "Directory") names.push(entry);
|
|
57
|
+
return names.sort();
|
|
58
|
+
});
|
|
59
|
+
/**
|
|
60
|
+
* The names the selected config's targets publish under: the config's `name`
|
|
61
|
+
* and each target's own `name` override, deduplicated, in first-seen order.
|
|
62
|
+
* Each target's components log under its own name.
|
|
63
|
+
*
|
|
64
|
+
* @public
|
|
65
|
+
*/
|
|
66
|
+
const configPluginNames = (selection) => Effect.map(preparePlugins({
|
|
67
|
+
selection,
|
|
68
|
+
targets: []
|
|
69
|
+
}), (prepared) => [...new Set(prepared.flatMap(({ config, targets }) => targets.map((id) => pluginName(config.config, id))))]);
|
|
70
|
+
const DECODER = new TextDecoder();
|
|
71
|
+
const NEWLINE = 10;
|
|
72
|
+
/** The complete lines in `bytes` and how many bytes they cover; a last line with no newline is held back. */
|
|
73
|
+
const completeLines = (bytes) => {
|
|
74
|
+
const end = bytes.lastIndexOf(NEWLINE) + 1;
|
|
75
|
+
if (end === 0) return {
|
|
76
|
+
lines: [],
|
|
77
|
+
length: 0
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
lines: DECODER.decode(bytes.subarray(0, end)).replace(/\r?\n$/, "").split(/\r?\n/),
|
|
81
|
+
length: end
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* The last `lines` complete lines of a plugin's log file.
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* An absent file is `present: false` with no lines, not a failure: a plugin
|
|
89
|
+
* that has logged nothing has no file.
|
|
90
|
+
*
|
|
91
|
+
* @public
|
|
92
|
+
*/
|
|
93
|
+
const readLog = (input) => Effect.gen(function* () {
|
|
94
|
+
const fs = yield* FileSystem.FileSystem;
|
|
95
|
+
const path = (yield* Path.Path).join(yield* logDirectory(input.stateHome, input.plugin), input.file);
|
|
96
|
+
if (!(yield* fs.exists(path))) return {
|
|
97
|
+
plugin: input.plugin,
|
|
98
|
+
file: input.file,
|
|
99
|
+
path,
|
|
100
|
+
present: false,
|
|
101
|
+
lines: [],
|
|
102
|
+
offset: 0
|
|
103
|
+
};
|
|
104
|
+
const { lines, length } = completeLines(yield* fs.readFile(path));
|
|
105
|
+
return {
|
|
106
|
+
plugin: input.plugin,
|
|
107
|
+
file: input.file,
|
|
108
|
+
path,
|
|
109
|
+
present: true,
|
|
110
|
+
lines: input.lines <= 0 ? [] : lines.slice(-input.lines),
|
|
111
|
+
offset: length
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
/**
|
|
115
|
+
* The complete lines appended to `path` after byte `offset`, and the offset to
|
|
116
|
+
* resume from.
|
|
117
|
+
*
|
|
118
|
+
* @remarks
|
|
119
|
+
* A file smaller than `offset` was truncated or replaced, so reading restarts
|
|
120
|
+
* from its beginning. A file that does not exist yields no lines at offset 0.
|
|
121
|
+
*
|
|
122
|
+
* @public
|
|
123
|
+
*/
|
|
124
|
+
const readLogFrom = (path, offset) => Effect.gen(function* () {
|
|
125
|
+
const fs = yield* FileSystem.FileSystem;
|
|
126
|
+
if (!(yield* fs.exists(path))) return {
|
|
127
|
+
lines: [],
|
|
128
|
+
offset: 0
|
|
129
|
+
};
|
|
130
|
+
const size = Number((yield* fs.stat(path)).size);
|
|
131
|
+
const from = size < offset ? 0 : offset;
|
|
132
|
+
if (size === from) return {
|
|
133
|
+
lines: [],
|
|
134
|
+
offset: from
|
|
135
|
+
};
|
|
136
|
+
const { lines, length } = completeLines((yield* fs.readFile(path)).subarray(from));
|
|
137
|
+
return {
|
|
138
|
+
lines,
|
|
139
|
+
offset: from + length
|
|
140
|
+
};
|
|
141
|
+
}).pipe(Effect.catch((error) => error.reason._tag === "NotFound" ? Effect.succeed({
|
|
142
|
+
lines: [],
|
|
143
|
+
offset: 0
|
|
144
|
+
}) : Effect.fail(error)));
|
|
145
|
+
|
|
146
|
+
//#endregion
|
|
147
|
+
export { configPluginNames, listLogPlugins, logDirectory, logRoot, parseLogLine, readLog, readLogFrom };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/monitor-lib.generated.ts
|
|
2
|
+
/** The monitor library's files, embedded so a build needs no asset on disk. */
|
|
3
|
+
const MONITOR_LIB_FILES = [{
|
|
4
|
+
name: "monitor.sh",
|
|
5
|
+
content: "# shellcheck shell=sh\n# pluginfinity monitor library. `pluginfinity build` writes this file into every\n# target that has monitors, at lib/pluginfinity/monitor.sh; do not edit a copy\n# under builds/, the next build overwrites it.\n#\n# POSIX sh. Writes to stdout only through monitor_notify: one line on stdout is\n# one notification. Set _pf_lib_dir to this file's directory, source it, then\n# call the functions. From a script at monitors/x.sh:\n# _pf_lib_dir=\"$(dirname \"$0\")/../lib/pluginfinity\"; . \"$_pf_lib_dir/monitor.sh\"\n#\n# A monitor never exits non-zero because of an error inside one poll: the\n# library logs it and the loop keeps going.\n#\n# monitor_notify cannot tell monitor_every that stdout closed when it runs inside\n# $(...) or a pipeline subshell; call it directly from the polled function.\n#\n# PLUGINFINITY_MONITOR_MAX_TICKS=<n> is a contract every monitor honours: stop\n# after n checks, however triggered (a poll, a startup sweep, or one handled\n# event). n is a positive integer; monitor_every logs any other value once and\n# ignores it (unbounded). monitor_every does it for you; a monitor that does not use it,\n# such as a node `command` monitor, must honour it itself, because the bats\n# helper's run_monitor sets it to bound a test. Do not set it in a plugin.\n\n# POSIX sh cannot find a sourced file's own path, so the sourcer sets _pf_lib_dir\n# (or _pf_log_dir, the log.sh convention) to this file's directory first. log.sh\n# and host.sh live beside this file. Without a readable log.sh the logging\n# functions are no-ops and the monitor still runs.\n_pf_mon_dir=\"${_pf_lib_dir:-${_pf_log_dir:-.}}\"\n_pf_log_dir=\"$_pf_mon_dir\"\nif [ -r \"$_pf_mon_dir/log.sh\" ]; then\n # shellcheck source=/dev/null\n . \"$_pf_mon_dir/log.sh\"\nelse\n pf_log() { return 0; }\n pf_debug() { return 0; }\n pf_debug_on() { return 1; }\nfi\n\n# A closed stdout must surface as a failed write, not as SIGPIPE killing the monitor.\ntrap '' PIPE\n_pf_mon_closed=0\n\nmonitor_name() { printf '%s\\n' \"${PLUGINFINITY_MONITOR:-$(basename \"$0\" .sh)}\"; }\nmonitor_host() { printf '%s\\n' \"${PLUGINFINITY_HOST:-unknown}\"; }\nmonitor_plugin_root() { (cd \"$_pf_mon_dir/../..\" 2>/dev/null && pwd -P) || printf '%s\\n' \"$_pf_mon_dir/../..\"; }\n\nmonitor_project_dir() {\n if [ -n \"${CLAUDE_PROJECT_DIR:-}\" ]; then\n printf '%s\\n' \"$CLAUDE_PROJECT_DIR\"\n return 0\n fi\n _pf_g=$(git rev-parse --show-toplevel 2>/dev/null) || _pf_g=\n printf '%s\\n' \"${_pf_g:-$PWD}\"\n}\n\nmonitor_log() { pf_log monitor \"$@\"; }\nmonitor_debug() { pf_debug monitor \"$@\"; }\n\n# monitor_notify <text...>: one line on stdout, flushed. Newlines become spaces.\n# Returns 1 when stdout is closed (and monitor_every then stops).\nmonitor_notify() {\n _pf_t=$(printf '%s' \"$*\" | tr '\\r\\n' ' ')\n if [ -z \"$_pf_t\" ]; then\n monitor_debug \"monitor_notify: empty text ignored\"\n return 0\n fi\n if ! printf '%s\\n' \"$_pf_t\" 2>/dev/null; then\n _pf_mon_closed=1\n return 1\n fi\n return 0\n}\n\n# monitor_state_dir: $XDG_STATE_HOME/pluginfinity/<plugin>/monitor/<name>/, created.\nmonitor_state_dir() {\n _pf_s=\"${XDG_STATE_HOME:-${HOME:-/nonexistent}/.local/state}/pluginfinity/${PLUGINFINITY_PLUGIN:-unknown}/monitor/$(monitor_name)/\"\n mkdir -p \"$_pf_s\" 2>/dev/null || :\n printf '%s\\n' \"$_pf_s\"\n}\n\n# monitor_once <key> <text...>: monitor_notify only the first time <key> is seen this session.\nmonitor_once() {\n _pf_k=${1:-}\n if [ $# -gt 0 ]; then shift; fi\n _pf_sd=$(monitor_state_dir)\n _pf_sess=\"${CLAUDE_CODE_SESSION_ID:-${CLAUDE_SESSION_ID:-$PPID}}\"\n # Keys are made filename-safe; the session scopes the marker.\n _pf_m=\"$_pf_sd$(printf '%s' \"$_pf_sess.$_pf_k\" | tr -c 'A-Za-z0-9._-' '_')\"\n if [ -e \"$_pf_m\" ]; then\n return 0\n fi\n monitor_notify \"$@\" || return 1\n : >\"$_pf_m\" 2>/dev/null || :\n}\n\n# monitor_every <seconds> <function>: call <function> now and every <seconds>, forever.\n# A failing call is logged and the loop continues; a closed stdout ends it with exit 0.\nmonitor_every() {\n _pf_iv=${1:-}\n case \"$_pf_iv\" in\n '' | *[!0-9]*)\n monitor_log \"monitor_every: interval '$_pf_iv' is not a number of seconds; using 60\"\n _pf_iv=60\n ;;\n esac\n _pf_fn=${2:-:}\n # PLUGINFINITY_MONITOR_MAX_TICKS is a positive integer; anything else is\n # logged once and treated as unset, so a test mistake never loops on an error.\n _pf_max=${PLUGINFINITY_MONITOR_MAX_TICKS:-}\n case \"$_pf_max\" in\n '') ;;\n *[!0-9]*)\n monitor_log \"monitor_every: PLUGINFINITY_MONITOR_MAX_TICKS '$_pf_max' is not a positive integer; ignoring it\"\n _pf_max=\n ;;\n *)\n if [ \"$_pf_max\" -le 0 ] 2>/dev/null; then\n monitor_log \"monitor_every: PLUGINFINITY_MONITOR_MAX_TICKS '$_pf_max' is not a positive integer; ignoring it\"\n _pf_max=\n fi\n ;;\n esac\n _pf_n=0\n while :; do\n if \"$_pf_fn\"; then _pf_rc=0; else _pf_rc=$?; fi\n if [ \"$_pf_mon_closed\" = 1 ]; then\n monitor_debug \"stdout closed; stopping\"\n exit 0\n fi\n if [ \"$_pf_rc\" -ne 0 ]; then\n monitor_log \"$_pf_fn failed (exit $_pf_rc)\"\n fi\n _pf_n=$((_pf_n + 1))\n if [ -n \"$_pf_max\" ] && [ \"$_pf_n\" -ge \"$_pf_max\" ]; then\n return 0\n fi\n sleep \"$_pf_iv\"\n done\n}\n"
|
|
6
|
+
}];
|
|
7
|
+
|
|
8
|
+
//#endregion
|
|
9
|
+
export { MONITOR_LIB_FILES };
|
package/monitors.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { ConfigIssue } from "./errors.js";
|
|
2
|
+
import { CONFIG_NOTE_PATH } from "./notes.js";
|
|
3
|
+
import { commandFiles, hookCommand, shellEnvPrefix } from "./hooks.js";
|
|
4
|
+
|
|
5
|
+
//#region src/monitors.ts
|
|
6
|
+
/**
|
|
7
|
+
* The monitors a target builds: the base `monitors` with the target's own
|
|
8
|
+
* replacing the base monitor of the same name.
|
|
9
|
+
*
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
const targetMonitors = (id, config) => {
|
|
13
|
+
const setting = config[id];
|
|
14
|
+
return {
|
|
15
|
+
...config.monitors,
|
|
16
|
+
...typeof setting === "object" ? setting.monitors : void 0
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
const SKILL_WHEN = "on-skill-invoke:";
|
|
20
|
+
/**
|
|
21
|
+
* Render a target's monitors file. A target with no monitors drops every
|
|
22
|
+
* monitor with a `monitor-omitted` note and ships neither file nor script.
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* Each entry's command carries `PLUGINFINITY_MONITOR` (the monitor's name,
|
|
26
|
+
* which the monitor library logs under), quoted by `shellEnvPrefix`: a prefix
|
|
27
|
+
* on a script, an `export` before a command.
|
|
28
|
+
*
|
|
29
|
+
* A `when` of `on-skill-invoke:<skill>` is written `on-skill-invoke:<plugin>:<skill>`: Claude
|
|
30
|
+
* Code matches the plugin-qualified skill name only, and a bare name never starts the monitor.
|
|
31
|
+
* A skill the target does not build is an issue, since the monitor would never start.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
const renderMonitors = (target, id, monitors, invoke, context) => {
|
|
36
|
+
const entries = Object.entries(monitors);
|
|
37
|
+
if (entries.length === 0) return {
|
|
38
|
+
notes: [],
|
|
39
|
+
issues: [],
|
|
40
|
+
scripts: [],
|
|
41
|
+
commandFiles: []
|
|
42
|
+
};
|
|
43
|
+
const placement = target.monitors;
|
|
44
|
+
if (!("path" in placement)) return {
|
|
45
|
+
notes: entries.map(([name]) => ({
|
|
46
|
+
target: id,
|
|
47
|
+
path: CONFIG_NOTE_PATH,
|
|
48
|
+
kind: "monitor-omitted",
|
|
49
|
+
name
|
|
50
|
+
})),
|
|
51
|
+
issues: [],
|
|
52
|
+
scripts: [],
|
|
53
|
+
commandFiles: []
|
|
54
|
+
};
|
|
55
|
+
const issues = [];
|
|
56
|
+
const array = entries.map(([name, entry]) => {
|
|
57
|
+
const env = shellEnvPrefix({ PLUGINFINITY_MONITOR: name });
|
|
58
|
+
const command = "script" in entry ? `${env}${hookCommand(entry, placement.root, invoke)}` : `export ${env.trimEnd()}; ${hookCommand(entry, placement.root, invoke)}`;
|
|
59
|
+
let when = entry.when;
|
|
60
|
+
if (when !== void 0 && when.startsWith(SKILL_WHEN)) {
|
|
61
|
+
const skill = when.slice(16);
|
|
62
|
+
if (!context.skills.has(skill)) issues.push(ConfigIssue.make({
|
|
63
|
+
key: `monitors.${name}.when`,
|
|
64
|
+
message: `names the skill "${skill}", which this plugin does not build for ${id}`
|
|
65
|
+
}));
|
|
66
|
+
when = `${SKILL_WHEN}${context.plugin}:${skill}`;
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
name,
|
|
70
|
+
command,
|
|
71
|
+
description: entry.description,
|
|
72
|
+
...when === void 0 ? {} : { when }
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
return {
|
|
76
|
+
file: {
|
|
77
|
+
path: placement.path,
|
|
78
|
+
content: `${JSON.stringify(array, null, " ")}\n`
|
|
79
|
+
},
|
|
80
|
+
notes: [],
|
|
81
|
+
issues,
|
|
82
|
+
scripts: [...new Set(entries.flatMap(([, entry]) => "script" in entry ? [entry.script] : []))],
|
|
83
|
+
commandFiles: [...new Set(entries.flatMap(([, entry]) => "command" in entry ? commandFiles(entry.command) : []))]
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
export { renderMonitors, targetMonitors };
|
package/notes.js
CHANGED
|
@@ -8,7 +8,14 @@ const BUILD_NOTE_KINDS = [
|
|
|
8
8
|
"dropped",
|
|
9
9
|
"degraded",
|
|
10
10
|
"tool-dropped",
|
|
11
|
-
"hook-
|
|
11
|
+
"hook-matcher-runtime",
|
|
12
|
+
"hook-output-ignored",
|
|
13
|
+
"hook-omitted",
|
|
14
|
+
"monitor-omitted",
|
|
15
|
+
"env-shell-unsupported",
|
|
16
|
+
"env-wait-timeout",
|
|
17
|
+
"hook-matcher-widened",
|
|
18
|
+
"hook-matcher-regex"
|
|
12
19
|
];
|
|
13
20
|
/** The path of notes about the config: hooks and servers. */
|
|
14
21
|
const CONFIG_NOTE_PATH = "config";
|
package/operations.js
CHANGED
|
@@ -4,13 +4,18 @@ import { readAgents, renderAgent } from "./agents.js";
|
|
|
4
4
|
import { preparePlugins } from "./selection.js";
|
|
5
5
|
import { ENGINE_VERSION } from "./version.js";
|
|
6
6
|
import { applyEmit, planEmit } from "./emit.js";
|
|
7
|
-
import { hookCommandFiles, hookScripts, renderHooks, targetHooks } from "./hooks.js";
|
|
8
|
-
import { pluginName, renderManifest, serializeManifest } from "./manifest.js";
|
|
9
7
|
import { CONFIG_NOTE_PATH, sortNotes } from "./notes.js";
|
|
8
|
+
import { envNotes, envRunnerEntry, envWaitNotes, withEnvRunner } from "./env.js";
|
|
9
|
+
import { hookCommandFiles, hookScripts, renderHooks, sessionStartMatcher, targetHooks, widensSessionStart } from "./hooks.js";
|
|
10
|
+
import { pluginName, renderManifest, serializeManifest } from "./manifest.js";
|
|
11
|
+
import { renderMonitors, targetMonitors } from "./monitors.js";
|
|
10
12
|
import { HOOK_LIB_DIR, hookLibFiles } from "./hook-lib.js";
|
|
13
|
+
import { ignoredOutput } from "./hook-output.js";
|
|
14
|
+
import { LIB_DIR, libFiles } from "./lib-files.js";
|
|
11
15
|
import { SERVER_LIB_DIR, serverLibFiles } from "./server-lib.js";
|
|
12
16
|
import { mcpServerNames, renderServers, serverFiles } from "./servers.js";
|
|
13
17
|
import { readSkills, renderSkill } from "./skills.js";
|
|
18
|
+
import { renderToolMap } from "./tool-map.js";
|
|
14
19
|
import { Effect, FileSystem, Path, Schema } from "effect";
|
|
15
20
|
import { KNOWN_TARGET_IDS, TARGETS } from "@pluginfinity/targets";
|
|
16
21
|
import { Run } from "@effected/commands";
|
|
@@ -56,19 +61,27 @@ const copyFile = (root, file) => Effect.gen(function* () {
|
|
|
56
61
|
};
|
|
57
62
|
});
|
|
58
63
|
/** Fail with `HookScriptInvalid` unless `script` exists, and is executable under `exec`. */
|
|
59
|
-
const checkScript = (config, script, invoke) => Effect.gen(function* () {
|
|
64
|
+
const checkScript = (config, script, invoke, component = "hooks") => Effect.gen(function* () {
|
|
60
65
|
const fs = yield* FileSystem.FileSystem;
|
|
61
66
|
const path = yield* Path.Path;
|
|
67
|
+
if (invoke === "exec" && component === "hooks" && script.includes("=")) return yield* Effect.fail(new HookScriptInvalid({
|
|
68
|
+
path: config.path,
|
|
69
|
+
script,
|
|
70
|
+
problem: "equals-in-path",
|
|
71
|
+
component
|
|
72
|
+
}));
|
|
62
73
|
const info = yield* fs.stat(path.join(config.root, script)).pipe(Effect.option);
|
|
63
74
|
if (info._tag === "None" || info.value.type !== "File") return yield* Effect.fail(new HookScriptInvalid({
|
|
64
75
|
path: config.path,
|
|
65
76
|
script,
|
|
66
|
-
problem: "missing"
|
|
77
|
+
problem: "missing",
|
|
78
|
+
component
|
|
67
79
|
}));
|
|
68
80
|
if (invoke === "exec" && (info.value.mode & 73) === 0) return yield* Effect.fail(new HookScriptInvalid({
|
|
69
81
|
path: config.path,
|
|
70
82
|
script,
|
|
71
|
-
problem: "not-executable"
|
|
83
|
+
problem: "not-executable",
|
|
84
|
+
component
|
|
72
85
|
}));
|
|
73
86
|
});
|
|
74
87
|
/** Whether `real` is `root` or under it; both are real paths. */
|
|
@@ -114,11 +127,11 @@ const expandShipped = (config, file, spelled, referencedBy) => Effect.gen(functi
|
|
|
114
127
|
* The files `entries` name, each canonical: each file, or every file under
|
|
115
128
|
* each directory. Every one must exist and resolve inside the plugin.
|
|
116
129
|
*/
|
|
117
|
-
const listedFiles = (config, entries) => Effect.gen(function* () {
|
|
130
|
+
const listedFiles = (config, entries, referencedBy = "files") => Effect.gen(function* () {
|
|
118
131
|
const fail = (file, problem) => Effect.fail(new ShippedFileInvalid({
|
|
119
132
|
path: config.path,
|
|
120
133
|
file,
|
|
121
|
-
referencedBy
|
|
134
|
+
referencedBy,
|
|
122
135
|
problem
|
|
123
136
|
}));
|
|
124
137
|
const out = [];
|
|
@@ -126,7 +139,7 @@ const listedFiles = (config, entries) => Effect.gen(function* () {
|
|
|
126
139
|
const file = canonical(entry);
|
|
127
140
|
if (file === void 0) return yield* fail(entry, "outside-root");
|
|
128
141
|
if (file === "") return yield* fail(entry, "not-normal");
|
|
129
|
-
out.push(...yield* expandShipped(config, file, file,
|
|
142
|
+
out.push(...yield* expandShipped(config, file, file, referencedBy));
|
|
130
143
|
}
|
|
131
144
|
return out;
|
|
132
145
|
});
|
|
@@ -158,6 +171,8 @@ const serverShipped = (config, spelled, referencedBy, command) => Effect.gen(fun
|
|
|
158
171
|
}
|
|
159
172
|
return files;
|
|
160
173
|
});
|
|
174
|
+
/** The skills or agents a target builds: those whose `targets` do not switch it off. */
|
|
175
|
+
const builtFor = (items, id) => items.filter((item) => item.frontmatter.targets?.[id] !== false);
|
|
161
176
|
/**
|
|
162
177
|
* What a target's skill and agent bodies may name: the skills and agents it
|
|
163
178
|
* builds, every file of those skills, and the plugin's own MCP servers. Agent
|
|
@@ -167,12 +182,12 @@ const serverShipped = (config, spelled, referencedBy, command) => Effect.gen(fun
|
|
|
167
182
|
*/
|
|
168
183
|
const tokenContext = (target, id, config, skills, agents) => {
|
|
169
184
|
const plugin = pluginName(config, id);
|
|
170
|
-
const built = skills
|
|
185
|
+
const built = builtFor(skills, id);
|
|
171
186
|
return {
|
|
172
187
|
target,
|
|
173
188
|
plugin,
|
|
174
189
|
skills: new Set(built.map((skill) => skill.name)),
|
|
175
|
-
agents: new Set(agents
|
|
190
|
+
agents: new Set(builtFor(agents, id).map((agent) => agent.name)),
|
|
176
191
|
skillFiles: new Set(built.flatMap((skill) => [`${skill.name}/SKILL.md`, ...skill.files.map((file) => `${skill.name}/${file}`)])),
|
|
177
192
|
own: {
|
|
178
193
|
plugin: pluginName(config, "claude"),
|
|
@@ -201,8 +216,16 @@ const tokenContext = (target, id, config, skills, agents) => {
|
|
|
201
216
|
* in its manifest also reserves the server file its host loads by default
|
|
202
217
|
* (Claude Code's `.mcp.json` and `.lsp.json`), but only when this plugin
|
|
203
218
|
* has servers of that kind inline.
|
|
219
|
+
*
|
|
220
|
+
* A target with monitors (Claude Code) writes them to `monitors/monitors.json`,
|
|
221
|
+
* which no source file may occupy, and ships each monitor `script` and each
|
|
222
|
+
* file a `command` names after `${PLUGIN_ROOT}/` (and the monitor library).
|
|
223
|
+
* Those files ship only to targets that build monitors, even from under
|
|
224
|
+
* `hooks/`; a target without monitors notes each as `monitor-omitted` and
|
|
225
|
+
* ships none.
|
|
204
226
|
*/
|
|
205
227
|
const planPlugin = (prepared) => Effect.gen(function* () {
|
|
228
|
+
const fs = yield* FileSystem.FileSystem;
|
|
206
229
|
const path = yield* Path.Path;
|
|
207
230
|
const config = prepared.config;
|
|
208
231
|
const version = yield* readVersion(config.root);
|
|
@@ -226,10 +249,33 @@ const planPlugin = (prepared) => Effect.gen(function* () {
|
|
|
226
249
|
for (const script of hookScripts(events)) yield* checkScript(config, script, invoke);
|
|
227
250
|
for (const file of hookCommandFiles(events)) yield* checkScript(config, file, "bash");
|
|
228
251
|
}
|
|
229
|
-
const hooksDir = yield* sourceFiles(config.root, "hooks");
|
|
230
|
-
const listed = yield* listedFiles(config, config.config.files ?? []);
|
|
231
252
|
const { skills, failures: skillFailures } = yield* readSkills(config.root, KNOWN_TARGET_IDS);
|
|
232
253
|
const { agents, failures: agentFailures } = yield* readAgents(config.root, KNOWN_TARGET_IDS);
|
|
254
|
+
const monitorsOf = new Map(config.targets.map((id) => {
|
|
255
|
+
const rendered = renderMonitors(targetOf(id), id, targetMonitors(id, config.config), invoke, {
|
|
256
|
+
plugin: pluginName(config.config, id),
|
|
257
|
+
skills: new Set(builtFor(skills, id).map((skill) => skill.name))
|
|
258
|
+
});
|
|
259
|
+
return [id, {
|
|
260
|
+
rendered,
|
|
261
|
+
files: [...rendered.scripts, ...rendered.commandFiles]
|
|
262
|
+
}];
|
|
263
|
+
}));
|
|
264
|
+
const monitorFilesOf = (id) => {
|
|
265
|
+
const found = monitorsOf.get(id);
|
|
266
|
+
if (found === void 0) throw new Error(`no monitors for target "${id}"`);
|
|
267
|
+
return found;
|
|
268
|
+
};
|
|
269
|
+
const everyMonitorFile = new Set([...monitorsOf.values()].flatMap((one) => one.files));
|
|
270
|
+
for (const id of prepared.targets) {
|
|
271
|
+
const { rendered } = monitorFilesOf(id);
|
|
272
|
+
for (const script of rendered.scripts) yield* checkScript(config, script, invoke, "monitors");
|
|
273
|
+
for (const file of rendered.commandFiles) yield* checkScript(config, file, "bash", "monitors");
|
|
274
|
+
}
|
|
275
|
+
const env = config.config.env;
|
|
276
|
+
if (env?.setup !== void 0) yield* checkScript(config, env.setup, "bash", "env");
|
|
277
|
+
const hooksDir = yield* sourceFiles(config.root, "hooks");
|
|
278
|
+
const baseListed = yield* listedFiles(config, config.config.files ?? []);
|
|
233
279
|
const failures = [...skillFailures, ...agentFailures];
|
|
234
280
|
const keep = (error) => {
|
|
235
281
|
if (!failures.some((seen) => seen.message === error.message)) failures.push(error);
|
|
@@ -243,16 +289,61 @@ const planPlugin = (prepared) => Effect.gen(function* () {
|
|
|
243
289
|
kind: "hook-omitted",
|
|
244
290
|
name: event
|
|
245
291
|
}));
|
|
292
|
+
for (const { event, entries } of events) {
|
|
293
|
+
if (target.hooks.matcherIgnored.includes(event) && entries.some((entry) => entry.matcher !== void 0)) notes.push({
|
|
294
|
+
target: id,
|
|
295
|
+
path: CONFIG_NOTE_PATH,
|
|
296
|
+
kind: "hook-matcher-runtime",
|
|
297
|
+
name: event
|
|
298
|
+
});
|
|
299
|
+
if (widensSessionStart(target.hooks.matcherIgnored, event)) for (const entry of entries) {
|
|
300
|
+
if (entry.matcher === void 0) continue;
|
|
301
|
+
const spelled = sessionStartMatcher(entry.matcher);
|
|
302
|
+
if (spelled.widened) notes.push({
|
|
303
|
+
target: id,
|
|
304
|
+
path: CONFIG_NOTE_PATH,
|
|
305
|
+
kind: "hook-matcher-widened",
|
|
306
|
+
name: `${event} ${entry.matcher} -> ${spelled.matcher}`
|
|
307
|
+
});
|
|
308
|
+
else if (spelled.unwidened) notes.push({
|
|
309
|
+
target: id,
|
|
310
|
+
path: CONFIG_NOTE_PATH,
|
|
311
|
+
kind: "hook-matcher-regex",
|
|
312
|
+
name: `${event} ${entry.matcher}`
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
const sources = /* @__PURE__ */ new Map();
|
|
317
|
+
for (const script of hookScripts(events)) {
|
|
318
|
+
const text = yield* fs.readFileString(path.join(config.root, script)).pipe(Effect.option);
|
|
319
|
+
if (text._tag === "Some") sources.set(script, text.value);
|
|
320
|
+
}
|
|
321
|
+
notes.push(...ignoredOutput(id, target, events, (script) => sources.get(script)));
|
|
246
322
|
const own = new Set(filesOf(events));
|
|
323
|
+
const monitored = monitorFilesOf(id);
|
|
324
|
+
notes.push(...monitored.rendered.notes);
|
|
325
|
+
if (monitored.rendered.issues.length > 0) {
|
|
326
|
+
const issue = new ComponentInvalid({
|
|
327
|
+
path: config.path,
|
|
328
|
+
target: id,
|
|
329
|
+
issues: monitored.rendered.issues
|
|
330
|
+
});
|
|
331
|
+
if (!failures.some((seen) => seen.message === issue.message)) failures.push(issue);
|
|
332
|
+
}
|
|
333
|
+
const monitorOwn = new Set(monitored.files);
|
|
247
334
|
const servers = serverFiles(target, id, config.config);
|
|
248
335
|
const serverFilesShipped = [];
|
|
249
336
|
for (const file of servers.commands) serverFilesShipped.push(...yield* serverShipped(config, file, servers.owners.get(file) ?? "mcpServers", true));
|
|
250
337
|
for (const file of servers.others) serverFilesShipped.push(...yield* serverShipped(config, file, servers.owners.get(file) ?? "mcpServers", false));
|
|
338
|
+
const setting = config.config[id];
|
|
339
|
+
const targetListed = yield* listedFiles(config, typeof setting === "object" ? setting.files ?? [] : [], `${id}.files`);
|
|
251
340
|
const shipped = [.../* @__PURE__ */ new Set([
|
|
252
|
-
...hooksDir.filter((file) => own.has(file) || !everyScript.has(file)),
|
|
253
|
-
...[...own].filter((script) => !script.startsWith("hooks/")),
|
|
341
|
+
...hooksDir.filter((file) => own.has(file) || monitorOwn.has(file) || !(everyScript.has(file) || everyMonitorFile.has(file))),
|
|
342
|
+
...[...own, ...monitorOwn].filter((script) => !script.startsWith("hooks/")),
|
|
343
|
+
...env?.setup === void 0 ? [] : [env.setup],
|
|
254
344
|
...serverFilesShipped,
|
|
255
|
-
...
|
|
345
|
+
...baseListed,
|
|
346
|
+
...targetListed
|
|
256
347
|
])];
|
|
257
348
|
const copied = [];
|
|
258
349
|
for (const file of shipped) copied.push(yield* copyFile(config.root, file));
|
|
@@ -270,16 +361,24 @@ const planPlugin = (prepared) => Effect.gen(function* () {
|
|
|
270
361
|
path: target.manifest.path,
|
|
271
362
|
content: serializeManifest(manifest)
|
|
272
363
|
}];
|
|
273
|
-
const hooksFile = renderHooks(target, events, invoke);
|
|
274
|
-
if (hooksFile !== void 0) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
364
|
+
const hooksFile = renderHooks(target, env === void 0 ? events : withEnvRunner(target, events, envRunnerEntry(LIB_DIR)), invoke);
|
|
365
|
+
if (hooksFile !== void 0) generated.push({
|
|
366
|
+
path: target.hooks.path,
|
|
367
|
+
content: hooksFile
|
|
368
|
+
});
|
|
369
|
+
if (events.length > 0) generated.push(...hookLibFiles(id, String(manifest.name), ENGINE_VERSION, renderToolMap(target, pluginName(config.config, "claude"), [...mcpServerNames(id, config.config)], {
|
|
370
|
+
skills: builtFor(skills, id).map((skill) => skill.name),
|
|
371
|
+
agents: builtFor(agents, id).map((agent) => agent.name),
|
|
372
|
+
monitors: Object.keys(targetMonitors(id, config.config))
|
|
373
|
+
})));
|
|
374
|
+
if (monitored.rendered.file !== void 0) generated.push(monitored.rendered.file);
|
|
375
|
+
generated.push(...libFiles(id, String(manifest.name), ENGINE_VERSION, {
|
|
376
|
+
monitors: monitored.rendered.file !== void 0,
|
|
377
|
+
...env === void 0 ? {} : { env }
|
|
378
|
+
}));
|
|
281
379
|
generated.push(...rendered.files);
|
|
282
380
|
notes.push(...rendered.notes);
|
|
381
|
+
if (env !== void 0) notes.push(...envNotes(target, id), ...envWaitNotes(id, events));
|
|
283
382
|
if (rendered.stdio) generated.push(...serverLibFiles());
|
|
284
383
|
const tokens = tokenContext(target, id, config.config, skills, agents);
|
|
285
384
|
for (const skill of skills) {
|
|
@@ -295,6 +394,13 @@ const planPlugin = (prepared) => Effect.gen(function* () {
|
|
|
295
394
|
notes.push(...agentRender.notes);
|
|
296
395
|
}
|
|
297
396
|
const reservedFiles = new Set([target.mcp.placement, target.lsp.placement].flatMap((placement) => placement._tag === "manifest" && placement.key in rendered.manifest ? [placement.reserves] : []));
|
|
397
|
+
const monitorsFile = monitored.rendered.file?.path;
|
|
398
|
+
if (monitorsFile !== void 0 && (yield* fs.exists(path.join(config.root, monitorsFile)))) return yield* Effect.fail(new PathConflict({
|
|
399
|
+
path: config.path,
|
|
400
|
+
target: id,
|
|
401
|
+
file: monitorsFile,
|
|
402
|
+
conflict: "reserved-monitors-file"
|
|
403
|
+
}));
|
|
298
404
|
const reserved = copied.find((file) => reservedFiles.has(file.path) || ["hooks/lib/pluginfinity", "lib/pluginfinity"].some((dir) => file.path === dir || file.path.startsWith(`${dir}/`)));
|
|
299
405
|
if (reserved !== void 0) return yield* Effect.fail(new PathConflict({
|
|
300
406
|
path: config.path,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluginfinity/engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
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": [
|
|
@@ -34,14 +34,14 @@
|
|
|
34
34
|
"./package.json": "./package.json"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@effected/commands": "^0.
|
|
38
|
-
"@effected/engine": "^0.
|
|
39
|
-
"@effected/glob": "^0.
|
|
40
|
-
"@effected/semver": "^0.
|
|
41
|
-
"@effected/walker": "^0.
|
|
42
|
-
"@effected/yaml": "^0.
|
|
43
|
-
"@pluginfinity/core": "0.
|
|
44
|
-
"@pluginfinity/targets": "0.
|
|
37
|
+
"@effected/commands": "^0.12.0",
|
|
38
|
+
"@effected/engine": "^0.5.0",
|
|
39
|
+
"@effected/glob": "^0.11.0",
|
|
40
|
+
"@effected/semver": "^0.12.0",
|
|
41
|
+
"@effected/walker": "^0.16.0",
|
|
42
|
+
"@effected/yaml": "^0.20.0",
|
|
43
|
+
"@pluginfinity/core": "0.3.0",
|
|
44
|
+
"@pluginfinity/targets": "0.3.0",
|
|
45
45
|
"jiti": "^2.6.1"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
package/server-lib.generated.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/** The server library's files, embedded so a build needs no asset on disk. */
|
|
3
3
|
const SERVER_LIB_FILES = [{
|
|
4
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
|
|
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# log.sh sits beside this file.\n_pf_log_dir=$_pf_server_lib_dir\n# shellcheck source=/dev/null\n. \"$_pf_log_dir/log.sh\"\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 error log.\nserver_log() { pf_log server \"$*\"; }\n# Append a line to the plugin's debug log when PLUGINFINITY_DEBUG=1.\nserver_debug() { pf_debug server \"$*\"; }\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. A Copilot MCP server cannot learn the project: its client offers\n# no roots and no variable names it (measured 2026-10-07).\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. With jq on PATH: package.json's devEngines.packageManager\n# (an object, or an array whose first entry is used) name, then packageManager.\n# Without jq only the top-level packageManager field is read (devEngines is\n# ignored). Then lockfiles, else npm. A name other than the four is npm.\n_pf_detect_pm() { # project\n _pf_pm=\"\"\n if [ -f \"$1/package.json\" ]; then\n if command -v jq >/dev/null 2>&1; then\n _pf_pm=$(jq -r '((try (.devEngines.packageManager | if type == \"array\" then .[0] else . end | .name) catch null) | strings) // (.packageManager | strings) // empty' \\\n \"$1/package.json\" 2>/dev/null | cut -d@ -f1)\n else\n _pf_pm=$(grep -o '\"packageManager\"[[:space:]]*:[[:space:]]*\"[^\"]*\"' \"$1/package.json\" 2>/dev/null |\n sed -E 's/.*:[[:space:]]*\"([^@\"]*).*/\\1/')\n fi\n fi\n case \"$_pf_pm\" in\n npm | pnpm | yarn | bun)\n printf '%s\\n' \"$_pf_pm\"\n return 0\n ;;\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 run <package> with the\n# project's package manager (pnpm dlx, yarn dlx, bunx, or npx --yes; see\n# _pf_detect_pm). npm 11 refuses to run in a project whose devEngines names\n# another manager, so npx is only the runner for npm projects, for a manager\n# that is not on PATH (said on stderr), and when there is no project directory\n# (see server_project_dir), which skips the lookup and the install hint. An\n# optional `--install <package>` straight after the two positionals names a\n# different package in the install hint only; the runner still runs <package>.\n# 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 _pf_runner=npx\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 case \"$_pf_pm\" in\n pnpm | yarn) _pf_runner=$_pf_pm ;;\n bun) _pf_runner=bunx ;;\n esac\n if [ \"$_pf_runner\" != npx ] && ! command -v \"$_pf_runner\" >/dev/null 2>&1; then\n printf '%s was not found on PATH.\\n' \"$_pf_runner\" >&2\n _pf_runner=npx\n fi\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 case \"$_pf_runner\" in\n pnpm | yarn)\n printf 'Falling back to \"%s dlx %s\".\\n' \"$_pf_runner\" \"$_pf_pkg\" >&2\n server_debug \"server_exec_bin: running $_pf_pkg with $_pf_runner dlx\"\n exec \"$_pf_runner\" dlx \"$_pf_pkg\" \"$@\"\n ;;\n bunx)\n printf 'Falling back to \"bunx %s\".\\n' \"$_pf_pkg\" >&2\n server_debug \"server_exec_bin: running $_pf_pkg with bunx\"\n exec bunx \"$_pf_pkg\" \"$@\"\n ;;\n esac\n printf 'Falling back to \"npx --yes %s\".\\n' \"$_pf_pkg\" >&2\n server_debug \"server_exec_bin: running $_pf_pkg with npx --yes\"\n exec npx --yes \"$_pf_pkg\" \"$@\"\n}\n"
|
|
6
6
|
}];
|
|
7
7
|
|
|
8
8
|
//#endregion
|