tmux-ide 2.9.18 → 2.9.21
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/bin/cli.js +14703 -3882
- package/package.json +3 -2
- package/packages/contracts/src/command-catalog.ts +116 -0
- package/packages/contracts/src/commands.ts +5 -4
- package/packages/contracts/src/daemon-events.ts +2 -1
- package/packages/contracts/src/index.ts +2 -0
- package/packages/contracts/src/plain-text.ts +61 -0
- package/packages/daemon/dist/command-center/command-catalog-routes.js +78 -0
- package/packages/daemon/dist/command-center/plugin-routes.js +84 -0
- package/packages/daemon/dist/command-center/server.js +13 -0
- package/packages/daemon/dist/doctor.js +12 -5
- package/packages/daemon/dist/lib/cli-action-bridge.js +15 -0
- package/packages/daemon/dist/lib/daemon-embed.js +24 -1
- package/packages/daemon/dist/lib/headless-daemon.js +1 -0
- package/packages/daemon/dist/lib/native-split-capability.js +13 -15
- package/packages/daemon/dist/lib/worktree.js +18 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +2 -2
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +2 -2
- package/packages/daemon/dist/plugins/action-catalog.js +129 -0
- package/packages/daemon/dist/plugins/action-cli.js +212 -0
- package/packages/daemon/dist/plugins/api-table.js +303 -0
- package/packages/daemon/dist/plugins/cli.js +556 -0
- package/packages/daemon/dist/plugins/consent.js +107 -0
- package/packages/daemon/dist/plugins/daemon-plugins.js +132 -0
- package/packages/daemon/dist/plugins/discovery.js +142 -0
- package/packages/daemon/dist/plugins/examples/worktree-tasks.js +115 -0
- package/packages/daemon/dist/plugins/exec-sandbox.js +202 -0
- package/packages/daemon/dist/plugins/host/actions-prelude.js +279 -0
- package/packages/daemon/dist/plugins/host/entry.js +4 -0
- package/packages/daemon/dist/plugins/host/host-api-prelude.js +232 -0
- package/packages/daemon/dist/plugins/host/lua-state.js +493 -0
- package/packages/daemon/dist/plugins/host/main.js +322 -0
- package/packages/daemon/dist/plugins/host/sandbox.js +299 -0
- package/packages/daemon/dist/plugins/host-calls.js +279 -0
- package/packages/daemon/dist/plugins/manifest.js +69 -0
- package/packages/daemon/dist/plugins/plugin-exec.js +261 -0
- package/packages/daemon/dist/plugins/plugin-log.js +54 -0
- package/packages/daemon/dist/plugins/plugin-store.js +74 -0
- package/packages/daemon/dist/plugins/protocol.js +252 -0
- package/packages/daemon/dist/plugins/safe-files.js +97 -0
- package/packages/daemon/dist/plugins/supervisor.js +790 -0
- package/packages/daemon/dist/plugins/worktree-calls.js +154 -0
- package/packages/daemon/dist/terminal/mirror/session-channel.js +3 -2
- package/packages/daemon/dist/terminal/mirror/window-split-authority.js +1 -1
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agents-owner.js +27 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-experience.js +2 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-palette-command-owner.js +30 -2
- package/packages/daemon/dist/tui/mirror/runtime/application-plugin-commands.js +152 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-plugin-form.js +251 -0
- package/packages/daemon/dist/tui/mirror/workspace/application-command-description.js +14 -0
- package/packages/daemon/dist/tui/mirror/workspace/palette-list-layout.js +5 -0
- package/packages/daemon/src/command-center/command-catalog-routes.ts +116 -0
- package/packages/daemon/src/command-center/plugin-routes.ts +110 -0
- package/packages/daemon/src/command-center/server.ts +15 -0
- package/packages/daemon/src/doctor.ts +12 -5
- package/packages/daemon/src/lib/cli-action-bridge.ts +19 -0
- package/packages/daemon/src/lib/daemon-embed.ts +42 -0
- package/packages/daemon/src/lib/headless-daemon.ts +1 -0
- package/packages/daemon/src/lib/native-split-capability.ts +12 -13
- package/packages/daemon/src/lib/worktree.ts +16 -0
- package/packages/daemon/src/plugins/action-catalog.ts +141 -0
- package/packages/daemon/src/plugins/action-cli.ts +248 -0
- package/packages/daemon/src/plugins/api-table.ts +346 -0
- package/packages/daemon/src/plugins/cli.ts +630 -0
- package/packages/daemon/src/plugins/consent.ts +125 -0
- package/packages/daemon/src/plugins/daemon-plugins.ts +182 -0
- package/packages/daemon/src/plugins/discovery.ts +176 -0
- package/packages/daemon/src/plugins/examples/worktree-tasks.ts +120 -0
- package/packages/daemon/src/plugins/exec-sandbox.ts +264 -0
- package/packages/daemon/src/plugins/host/actions-prelude.ts +280 -0
- package/packages/daemon/src/plugins/host/entry.ts +5 -0
- package/packages/daemon/src/plugins/host/host-api-prelude.ts +233 -0
- package/packages/daemon/src/plugins/host/lua-state.ts +557 -0
- package/packages/daemon/src/plugins/host/main.ts +359 -0
- package/packages/daemon/src/plugins/host/sandbox.ts +301 -0
- package/packages/daemon/src/plugins/host-calls.ts +343 -0
- package/packages/daemon/src/plugins/manifest.ts +74 -0
- package/packages/daemon/src/plugins/plugin-exec.ts +326 -0
- package/packages/daemon/src/plugins/plugin-log.ts +63 -0
- package/packages/daemon/src/plugins/plugin-store.ts +80 -0
- package/packages/daemon/src/plugins/protocol.ts +275 -0
- package/packages/daemon/src/plugins/safe-files.ts +105 -0
- package/packages/daemon/src/plugins/supervisor.ts +954 -0
- package/packages/daemon/src/plugins/worktree-calls.ts +183 -0
- package/packages/daemon/src/terminal/mirror/session-channel.ts +3 -2
- package/packages/daemon/src/terminal/mirror/window-split-authority.ts +3 -2
- package/packages/daemon/src/tui/mirror/runtime/application-fleet-switcher.tsx +3 -1
- package/packages/daemon/src/tui/mirror/runtime/application-home-agents-owner.ts +32 -0
- package/packages/daemon/src/tui/mirror/runtime/application-home-experience.ts +2 -0
- package/packages/daemon/src/tui/mirror/runtime/application-palette-command-owner.ts +42 -2
- package/packages/daemon/src/tui/mirror/runtime/application-palette-preview.tsx +7 -1
- package/packages/daemon/src/tui/mirror/runtime/application-plugin-commands.ts +229 -0
- package/packages/daemon/src/tui/mirror/runtime/application-plugin-form.ts +312 -0
- package/packages/daemon/src/tui/mirror/workspace/application-command-description.ts +44 -2
- package/packages/daemon/src/tui/mirror/workspace/palette-list-layout.ts +4 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Consent for installed plugins.
|
|
3
|
+
*
|
|
4
|
+
* A plugin directory under `plugins/` runs only after its owner consented to
|
|
5
|
+
* that exact content: consent records a SHA-256 over every file in the plugin
|
|
6
|
+
* directory, and any change to any file (manifest included) means the plugin
|
|
7
|
+
* asks again. `init.lua` is the user's own file and needs no consent.
|
|
8
|
+
*
|
|
9
|
+
* Consent lives in `<state home>/consent/plugins.json` (mode 0600). Consent
|
|
10
|
+
* is the boundary: a plugin granted programs, a shell or `panes.send` can
|
|
11
|
+
* usually do anything its owner's account can, including change this file
|
|
12
|
+
* by other means. The OS sandbox (exec-sandbox.ts) only keeps programs from
|
|
13
|
+
* editing it directly and from reading it.
|
|
14
|
+
*/
|
|
15
|
+
import { createHash } from "node:crypto";
|
|
16
|
+
import {
|
|
17
|
+
lstatSync,
|
|
18
|
+
mkdirSync,
|
|
19
|
+
readdirSync,
|
|
20
|
+
readFileSync,
|
|
21
|
+
readlinkSync,
|
|
22
|
+
renameSync,
|
|
23
|
+
writeFileSync,
|
|
24
|
+
} from "node:fs";
|
|
25
|
+
import { dirname, join } from "node:path";
|
|
26
|
+
import { z } from "zod";
|
|
27
|
+
|
|
28
|
+
export const CONSENT_DIR = "consent";
|
|
29
|
+
export const CONSENT_FILE = "plugins.json";
|
|
30
|
+
/** Plugins larger than this are refused rather than hashed. */
|
|
31
|
+
export const MAX_PLUGIN_FILES = 2000;
|
|
32
|
+
export const MAX_PLUGIN_BYTES = 32 * 1024 * 1024;
|
|
33
|
+
|
|
34
|
+
const ConsentFileSchemaZ = z
|
|
35
|
+
.object({
|
|
36
|
+
version: z.literal(1),
|
|
37
|
+
plugins: z.record(
|
|
38
|
+
z.string(),
|
|
39
|
+
z.object({ hash: z.string().regex(/^sha256:[0-9a-f]{64}$/u), at: z.string() }).strict(),
|
|
40
|
+
),
|
|
41
|
+
})
|
|
42
|
+
.strict();
|
|
43
|
+
export type ConsentFile = z.infer<typeof ConsentFileSchemaZ>;
|
|
44
|
+
|
|
45
|
+
export function consentPath(stateHome: string): string {
|
|
46
|
+
return join(stateHome, CONSENT_DIR, CONSENT_FILE);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Hash every file under `dir`: relative path, type and content, in sorted
|
|
51
|
+
* order. Symlinks are hashed by their target text and never followed.
|
|
52
|
+
*/
|
|
53
|
+
export function hashPluginDirectory(dir: string): string {
|
|
54
|
+
const hash = createHash("sha256");
|
|
55
|
+
let files = 0;
|
|
56
|
+
let bytes = 0;
|
|
57
|
+
const walk = (relative: string) => {
|
|
58
|
+
const absolute = join(dir, relative);
|
|
59
|
+
for (const name of readdirSync(absolute).sort()) {
|
|
60
|
+
const childRelative = relative ? `${relative}/${name}` : name;
|
|
61
|
+
const child = join(dir, childRelative);
|
|
62
|
+
const stat = lstatSync(child);
|
|
63
|
+
files += 1;
|
|
64
|
+
if (files > MAX_PLUGIN_FILES)
|
|
65
|
+
throw new Error(`plugin has more than ${MAX_PLUGIN_FILES} files`);
|
|
66
|
+
if (stat.isSymbolicLink()) {
|
|
67
|
+
hash.update(`L\0${childRelative}\0${readlinkSync(child)}\0`);
|
|
68
|
+
} else if (stat.isDirectory()) {
|
|
69
|
+
hash.update(`D\0${childRelative}\0`);
|
|
70
|
+
walk(childRelative);
|
|
71
|
+
} else if (stat.isFile()) {
|
|
72
|
+
bytes += stat.size;
|
|
73
|
+
if (bytes > MAX_PLUGIN_BYTES) throw new Error("plugin is larger than 32 MiB");
|
|
74
|
+
const content = readFileSync(child);
|
|
75
|
+
hash.update(`F\0${childRelative}\0${content.byteLength}\0`);
|
|
76
|
+
hash.update(content);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
walk("");
|
|
81
|
+
return `sha256:${hash.digest("hex")}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function readConsent(stateHome: string): ConsentFile {
|
|
85
|
+
try {
|
|
86
|
+
const parsed = ConsentFileSchemaZ.safeParse(
|
|
87
|
+
JSON.parse(readFileSync(consentPath(stateHome), "utf8")),
|
|
88
|
+
);
|
|
89
|
+
if (parsed.success) return parsed.data;
|
|
90
|
+
} catch {
|
|
91
|
+
// Missing or unreadable: nothing is consented.
|
|
92
|
+
}
|
|
93
|
+
return { version: 1, plugins: {} };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type ConsentState = "consented" | "new" | "changed";
|
|
97
|
+
|
|
98
|
+
export function consentState(consent: ConsentFile, name: string, hash: string): ConsentState {
|
|
99
|
+
const recorded = consent.plugins[name];
|
|
100
|
+
if (!recorded) return "new";
|
|
101
|
+
return recorded.hash === hash ? "consented" : "changed";
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function writeConsent(stateHome: string, consent: ConsentFile): void {
|
|
105
|
+
const path = consentPath(stateHome);
|
|
106
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
107
|
+
const temporary = `${path}.${process.pid}.tmp`;
|
|
108
|
+
writeFileSync(temporary, `${JSON.stringify(consent, null, 2)}\n`, { mode: 0o600 });
|
|
109
|
+
renameSync(temporary, path);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Record consent for `name` at exactly `hash`. */
|
|
113
|
+
export function grantConsent(stateHome: string, name: string, hash: string, at = new Date()): void {
|
|
114
|
+
const consent = readConsent(stateHome);
|
|
115
|
+
consent.plugins[name] = { hash, at: at.toISOString() };
|
|
116
|
+
writeConsent(stateHome, consent);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function revokeConsent(stateHome: string, name: string): boolean {
|
|
120
|
+
const consent = readConsent(stateHome);
|
|
121
|
+
if (!consent.plugins[name]) return false;
|
|
122
|
+
delete consent.plugins[name];
|
|
123
|
+
writeConsent(stateHome, consent);
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the canonical daemon creates its plugin supervisor.
|
|
3
|
+
*/
|
|
4
|
+
import { basename, dirname, extname, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { appSettingsPath } from "../lib/app-settings.ts";
|
|
7
|
+
import { getCanonicalDaemonInfoPath } from "../lib/canonical-daemon.ts";
|
|
8
|
+
import { logger } from "../lib/log.ts";
|
|
9
|
+
import { stateHome } from "../lib/state-home.ts";
|
|
10
|
+
import { appendPluginLog, pluginLogPath } from "./plugin-log.ts";
|
|
11
|
+
import {
|
|
12
|
+
createHostCallHandler,
|
|
13
|
+
type DaemonEndpoint,
|
|
14
|
+
type HostCallDependencies,
|
|
15
|
+
type NotifyLevel,
|
|
16
|
+
} from "./host-calls.ts";
|
|
17
|
+
import { cliRunner } from "./worktree-calls.ts";
|
|
18
|
+
import { defaultControlSocketPath } from "../control/server.ts";
|
|
19
|
+
import { DEFAULT_PLUGIN_LIMITS } from "./protocol.ts";
|
|
20
|
+
import { minimalHostEnv, PluginSupervisor, secretEnvValues } from "./supervisor.ts";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* `ide.notify`: a short message in the status line of every attached tmux
|
|
24
|
+
* client (and so in the TUI, which is one), prefixed with the plugin name.
|
|
25
|
+
* `#` is doubled so tmux does not expand formats in plugin text.
|
|
26
|
+
*/
|
|
27
|
+
export async function notifyTmuxClients(
|
|
28
|
+
tmux: (args: readonly string[]) => Promise<string>,
|
|
29
|
+
plugin: string,
|
|
30
|
+
text: string,
|
|
31
|
+
level: NotifyLevel,
|
|
32
|
+
): Promise<void> {
|
|
33
|
+
const prefix = level === "info" ? "" : `${level}: `;
|
|
34
|
+
const message = `${plugin}: ${prefix}${text}`.replace(/#/gu, "##");
|
|
35
|
+
const clients = (await tmux(["list-clients", "-F", "#{client_name}"]))
|
|
36
|
+
.split("\n")
|
|
37
|
+
.map((line) => line.trim())
|
|
38
|
+
.filter(Boolean);
|
|
39
|
+
await Promise.all(
|
|
40
|
+
clients.map((client) =>
|
|
41
|
+
tmux(["display-message", "-c", client, "-d", "4000", message]).catch(() => ""),
|
|
42
|
+
),
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** `TMUX_IDE_PLUGINS=0` turns plugins off for a daemon. */
|
|
47
|
+
export function pluginsEnabled(env: NodeJS.ProcessEnv = process.env): boolean {
|
|
48
|
+
return env.TMUX_IDE_PLUGINS !== "0";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The argv that starts the plugin host. In the published CLI every module is
|
|
53
|
+
* bundled into `bin/cli.js`, which starts the host with `__plugin-host`.
|
|
54
|
+
* From source, the host entry sits next to this file and inherits the
|
|
55
|
+
* runtime's loader flags (tsx under Node; Bun runs TypeScript directly).
|
|
56
|
+
*/
|
|
57
|
+
export function resolvePluginHostCommand(
|
|
58
|
+
here: string = fileURLToPath(import.meta.url),
|
|
59
|
+
runtime: { execPath: string; execArgv: readonly string[] } = process,
|
|
60
|
+
): string[] {
|
|
61
|
+
if (basename(here) === "cli.js") return [runtime.execPath, here, "__plugin-host"];
|
|
62
|
+
const entry = join(dirname(here), "host", `entry${extname(here)}`);
|
|
63
|
+
return [runtime.execPath, ...runtime.execArgv, entry];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Where tmux keeps sockets: the default directory (under TMUX_TMPDIR or
|
|
68
|
+
* /tmp, per user) and the directory of the daemon's own tmux server.
|
|
69
|
+
*/
|
|
70
|
+
export async function tmuxSocketDirs(
|
|
71
|
+
tmux: (args: readonly string[]) => Promise<string>,
|
|
72
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
73
|
+
uid: number = process.getuid?.() ?? 0,
|
|
74
|
+
): Promise<string[]> {
|
|
75
|
+
const dirs = new Set<string>([join(env.TMUX_TMPDIR || "/tmp", `tmux-${uid}`)]);
|
|
76
|
+
if (env.TMUX_TMPDIR) dirs.add(join("/tmp", `tmux-${uid}`));
|
|
77
|
+
try {
|
|
78
|
+
const socket = (await tmux(["list-sessions", "-F", "#{socket_path}"])).split("\n")[0]?.trim();
|
|
79
|
+
// Only a directory of tmux sockets, never a broad one like /tmp itself.
|
|
80
|
+
if (socket?.startsWith("/") && basename(dirname(socket)).startsWith("tmux"))
|
|
81
|
+
dirs.add(dirname(socket));
|
|
82
|
+
} catch {
|
|
83
|
+
// No server yet: the default directories still apply.
|
|
84
|
+
}
|
|
85
|
+
return [...dirs];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The argv prefix that runs this tmux-ide's CLI: the bundled `bin/cli.js`,
|
|
90
|
+
* or `bin/cli.ts` under the runtime's loader flags when run from source.
|
|
91
|
+
*/
|
|
92
|
+
export function resolveCliCommand(
|
|
93
|
+
here: string = fileURLToPath(import.meta.url),
|
|
94
|
+
runtime: { execPath: string; execArgv: readonly string[] } = process,
|
|
95
|
+
): string[] {
|
|
96
|
+
if (basename(here) === "cli.js") return [runtime.execPath, here];
|
|
97
|
+
const cli = join(dirname(here), "..", "..", "..", "..", "bin", `cli${extname(here)}`);
|
|
98
|
+
return [runtime.execPath, ...runtime.execArgv, cli];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Files programs from plugins may not read: the daemon record (its port and
|
|
103
|
+
* owner token), the app settings (the remote-access token), the consent
|
|
104
|
+
* records and every plugin's store.
|
|
105
|
+
*/
|
|
106
|
+
export function daemonPrivateFiles(
|
|
107
|
+
home: string,
|
|
108
|
+
daemonInfoPath: string = getCanonicalDaemonInfoPath(),
|
|
109
|
+
): string[] {
|
|
110
|
+
// Prefixes: daemon.json also covers its .tmp and .retired siblings, and the
|
|
111
|
+
// record follows TMUX_IDE_DAEMON_INFO_DIR / the registry dir, wherever it is.
|
|
112
|
+
const infoDir = dirname(daemonInfoPath);
|
|
113
|
+
return [
|
|
114
|
+
daemonInfoPath,
|
|
115
|
+
join(infoDir, "daemon.claim"),
|
|
116
|
+
join(home, "consent"),
|
|
117
|
+
join(home, "plugin-store"),
|
|
118
|
+
appSettingsPath(),
|
|
119
|
+
];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* What the daemon's host functions run on: its state home, its own HTTP
|
|
124
|
+
* endpoint and tmux server, and what programs are kept from (the private
|
|
125
|
+
* files, the tmux socket directories, the control socket and, through the
|
|
126
|
+
* endpoint, the daemon's port).
|
|
127
|
+
*/
|
|
128
|
+
export function daemonHostCallDependencies(options: {
|
|
129
|
+
home: string;
|
|
130
|
+
endpoint?: (() => DaemonEndpoint | null) | undefined;
|
|
131
|
+
tmux?: ((args: readonly string[]) => Promise<string>) | undefined;
|
|
132
|
+
secretSet: () => ReadonlySet<string>;
|
|
133
|
+
}): HostCallDependencies {
|
|
134
|
+
const tmux = options.tmux ?? (async () => Promise.reject(new Error("tmux is unavailable")));
|
|
135
|
+
return {
|
|
136
|
+
stateHome: options.home,
|
|
137
|
+
endpoint: options.endpoint ?? (() => null),
|
|
138
|
+
tmux,
|
|
139
|
+
baseEnv: () => minimalHostEnv(),
|
|
140
|
+
secrets: options.secretSet,
|
|
141
|
+
notify: (plugin, text, level) => notifyTmuxClients(tmux, plugin, text, level),
|
|
142
|
+
unreadable: () => daemonPrivateFiles(options.home),
|
|
143
|
+
execMaxTimeoutMs: DEFAULT_PLUGIN_LIMITS.wallClockMs,
|
|
144
|
+
runCli: cliRunner(resolveCliCommand()),
|
|
145
|
+
tmuxSocketDirs: () => tmuxSocketDirs(tmux),
|
|
146
|
+
privateSockets: () => [defaultControlSocketPath()],
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function createDaemonPluginSupervisor(options: {
|
|
151
|
+
productVersion: string;
|
|
152
|
+
/** Daemon credentials that must never reach a plugin log or `lua run` output. */
|
|
153
|
+
secrets: readonly (string | null | undefined)[];
|
|
154
|
+
/** Called when plugin actions change, to invalidate the command catalog. */
|
|
155
|
+
onCatalogChange?: () => void;
|
|
156
|
+
/** The daemon's own HTTP endpoint, once it listens; plugins' pane calls go through it. */
|
|
157
|
+
endpoint?: () => DaemonEndpoint | null;
|
|
158
|
+
/** Run tmux against the daemon's server. */
|
|
159
|
+
tmux?: (args: readonly string[]) => Promise<string>;
|
|
160
|
+
home?: string;
|
|
161
|
+
}): PluginSupervisor {
|
|
162
|
+
const secrets = options.secrets.filter((secret): secret is string => typeof secret === "string");
|
|
163
|
+
const home = options.home ?? stateHome();
|
|
164
|
+
const logPath = pluginLogPath(home);
|
|
165
|
+
const secretSet = () =>
|
|
166
|
+
new Set([...secrets, ...secretEnvValues()].filter((secret) => secret.length >= 8));
|
|
167
|
+
const calls = createHostCallHandler(
|
|
168
|
+
daemonHostCallDependencies({ home, endpoint: options.endpoint, tmux: options.tmux, secretSet }),
|
|
169
|
+
);
|
|
170
|
+
return new PluginSupervisor({
|
|
171
|
+
calls,
|
|
172
|
+
stateHome: home,
|
|
173
|
+
productVersion: options.productVersion,
|
|
174
|
+
hostCommand: resolvePluginHostCommand(),
|
|
175
|
+
secrets: () => [...secrets, ...secretEnvValues()],
|
|
176
|
+
...(options.onCatalogChange ? { onCatalogChange: options.onCatalogChange } : {}),
|
|
177
|
+
onLog: (entry) => {
|
|
178
|
+
appendPluginLog(logPath, entry);
|
|
179
|
+
logger[entry.level](`plugin.${entry.plugin}`, entry.message, entry.fields);
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Find what to load: plugin directories under `<state home>/plugins/` in name
|
|
3
|
+
* order, then the user's `<state home>/init.lua` last, so the user's file can
|
|
4
|
+
* build on and override what plugins set up.
|
|
5
|
+
*/
|
|
6
|
+
import { existsSync, readFileSync, readdirSync, realpathSync, statSync } from "node:fs";
|
|
7
|
+
import { isAbsolute, join, relative } from "node:path";
|
|
8
|
+
import { consentState, hashPluginDirectory, readConsent } from "./consent.ts";
|
|
9
|
+
import {
|
|
10
|
+
apiSatisfies,
|
|
11
|
+
manifestDefaults,
|
|
12
|
+
PluginManifestSchemaZ,
|
|
13
|
+
type PluginManifest,
|
|
14
|
+
} from "./manifest.ts";
|
|
15
|
+
import {
|
|
16
|
+
INIT_PLUGIN_NAME,
|
|
17
|
+
PLUGIN_NAME_PATTERN,
|
|
18
|
+
RESERVED_PLUGIN_NAMES,
|
|
19
|
+
type PluginSpec,
|
|
20
|
+
} from "./protocol.ts";
|
|
21
|
+
|
|
22
|
+
export interface PluginProblem {
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly path: string;
|
|
25
|
+
readonly error: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A valid plugin that may not run yet: never consented, or changed since. */
|
|
29
|
+
export interface PluginAwaitingConsent {
|
|
30
|
+
readonly name: string;
|
|
31
|
+
readonly path: string;
|
|
32
|
+
readonly reason: "new" | "changed";
|
|
33
|
+
readonly hash: string;
|
|
34
|
+
readonly manifest: PluginManifest;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface PluginDiscovery {
|
|
38
|
+
readonly specs: readonly PluginSpec[];
|
|
39
|
+
readonly problems: readonly PluginProblem[];
|
|
40
|
+
readonly awaitingConsent: readonly PluginAwaitingConsent[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function pluginPaths(stateHome: string) {
|
|
44
|
+
return {
|
|
45
|
+
init: join(stateHome, "init.lua"),
|
|
46
|
+
plugins: join(stateHome, "plugins"),
|
|
47
|
+
lua: join(stateHome, "lua"),
|
|
48
|
+
logs: join(stateHome, "logs"),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function inside(child: string, parent: string): boolean {
|
|
53
|
+
const rel = relative(parent, child);
|
|
54
|
+
return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const MAX_MANIFEST_BYTES = 64 * 1024;
|
|
58
|
+
|
|
59
|
+
export function discoverPlugins(stateHome: string): PluginDiscovery {
|
|
60
|
+
const paths = pluginPaths(stateHome);
|
|
61
|
+
const specs: PluginSpec[] = [];
|
|
62
|
+
const problems: PluginProblem[] = [];
|
|
63
|
+
const awaitingConsent: PluginAwaitingConsent[] = [];
|
|
64
|
+
const consent = readConsent(stateHome);
|
|
65
|
+
|
|
66
|
+
let entries: string[];
|
|
67
|
+
try {
|
|
68
|
+
entries = readdirSync(paths.plugins).sort();
|
|
69
|
+
} catch {
|
|
70
|
+
entries = [];
|
|
71
|
+
}
|
|
72
|
+
for (const dirName of entries) {
|
|
73
|
+
if (dirName.startsWith(".")) continue;
|
|
74
|
+
const dir = join(paths.plugins, dirName);
|
|
75
|
+
const problem = (error: string) => problems.push({ name: dirName, path: dir, error });
|
|
76
|
+
try {
|
|
77
|
+
if (!statSync(dir).isDirectory()) continue;
|
|
78
|
+
} catch {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (!PLUGIN_NAME_PATTERN.test(dirName)) {
|
|
82
|
+
problem("directory name must be lowercase letters, digits and dashes");
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (RESERVED_PLUGIN_NAMES.has(dirName)) {
|
|
86
|
+
problem(`'${dirName}' is reserved for tmux-ide's own use; rename the directory`);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const manifestPath = join(dir, "manifest.json");
|
|
90
|
+
let raw: unknown;
|
|
91
|
+
try {
|
|
92
|
+
if (statSync(manifestPath).size > MAX_MANIFEST_BYTES) {
|
|
93
|
+
problem("manifest.json is larger than 64 KiB");
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
raw = JSON.parse(readFileSync(manifestPath, "utf8"));
|
|
97
|
+
} catch (error) {
|
|
98
|
+
problem(
|
|
99
|
+
existsSync(manifestPath)
|
|
100
|
+
? `manifest.json is not valid JSON: ${(error as Error).message}`
|
|
101
|
+
: "manifest.json is missing",
|
|
102
|
+
);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const parsed = PluginManifestSchemaZ.safeParse(raw);
|
|
106
|
+
if (!parsed.success) {
|
|
107
|
+
const issue = parsed.error.issues[0];
|
|
108
|
+
problem(
|
|
109
|
+
`manifest.json: ${issue?.path.join(".") || "(root)"}: ${issue?.message ?? "invalid"}`,
|
|
110
|
+
);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const manifest = parsed.data;
|
|
114
|
+
if (manifest.name !== dirName) {
|
|
115
|
+
problem(`manifest name '${manifest.name}' does not match its directory '${dirName}'`);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (!apiSatisfies(manifest.api)) {
|
|
119
|
+
problem(`needs Lua API ${manifest.api}, this tmux-ide provides a different version`);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const entry = join(dir, manifest.entry);
|
|
123
|
+
try {
|
|
124
|
+
const realDir = realpathSync(dir);
|
|
125
|
+
const realEntry = realpathSync(entry);
|
|
126
|
+
if (!inside(realEntry, realDir)) {
|
|
127
|
+
problem(`entry '${manifest.entry}' resolves outside the plugin directory`);
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
} catch {
|
|
131
|
+
problem(`entry '${manifest.entry}' is missing`);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
let hash: string;
|
|
135
|
+
try {
|
|
136
|
+
hash = hashPluginDirectory(dir);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
problem(`could not read the plugin: ${(error as Error).message}`);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const consented = consentState(consent, manifest.name, hash);
|
|
142
|
+
if (consented !== "consented") {
|
|
143
|
+
awaitingConsent.push({ name: manifest.name, path: dir, reason: consented, hash, manifest });
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
specs.push({
|
|
147
|
+
name: manifest.name,
|
|
148
|
+
kind: "plugin",
|
|
149
|
+
contentHash: hash,
|
|
150
|
+
entry,
|
|
151
|
+
// Only the plugin's own directory: consent hashes exactly what it can load.
|
|
152
|
+
includeRoots: [dir],
|
|
153
|
+
moduleRoots: [dir],
|
|
154
|
+
settings: manifestDefaults(manifest),
|
|
155
|
+
capabilities: {
|
|
156
|
+
exec: [...(manifest.capabilities.exec ?? [])],
|
|
157
|
+
shell: manifest.capabilities.shell === true,
|
|
158
|
+
panes: manifest.capabilities["panes.send"] === true,
|
|
159
|
+
worktree: manifest.capabilities.worktree === true,
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (existsSync(paths.init)) {
|
|
165
|
+
specs.push({
|
|
166
|
+
name: INIT_PLUGIN_NAME,
|
|
167
|
+
kind: "init",
|
|
168
|
+
entry: paths.init,
|
|
169
|
+
includeRoots: [stateHome, paths.lua],
|
|
170
|
+
moduleRoots: [paths.lua],
|
|
171
|
+
settings: {},
|
|
172
|
+
capabilities: { exec: "any", shell: true, panes: true, worktree: true },
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return { specs, problems, awaitingConsent };
|
|
176
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The "worktree tasks" example plugin: tasks as git worktrees, each with its
|
|
3
|
+
* own tmux session, through tmux-ide's own worktree code (`ide.worktree`).
|
|
4
|
+
*
|
|
5
|
+
* `tmux-ide plugin example worktree-tasks` copies these files into
|
|
6
|
+
* ~/.tmux-ide/plugins/worktree-tasks; it then runs only after
|
|
7
|
+
* `tmux-ide plugin consent worktree-tasks`.
|
|
8
|
+
*
|
|
9
|
+
* It asks only for the `worktree` capability and runs no programs: the
|
|
10
|
+
* daemon does the worktree work, for the session the command was run from
|
|
11
|
+
* (`ctx.session`). It never types into a pane, and cannot, so it works the
|
|
12
|
+
* same from the TUI, the CLI and the Mac app.
|
|
13
|
+
*/
|
|
14
|
+
export const WORKTREE_TASKS_MANIFEST = {
|
|
15
|
+
name: "worktree-tasks",
|
|
16
|
+
version: "1.0.0",
|
|
17
|
+
api: "^1.0",
|
|
18
|
+
description:
|
|
19
|
+
"Tasks as git worktrees: start, open and remove them from the palette, with tmux-ide worktree.",
|
|
20
|
+
capabilities: { worktree: true },
|
|
21
|
+
} as const;
|
|
22
|
+
|
|
23
|
+
export const WORKTREE_TASKS_INIT_LUA = String.raw`-- worktree-tasks: tasks as git worktrees, each with its own tmux session.
|
|
24
|
+
-- Every action asks the daemon (ide.worktree) to work on the repository of
|
|
25
|
+
-- the session the command was run from.
|
|
26
|
+
|
|
27
|
+
local function session_of(ctx)
|
|
28
|
+
if not ctx.session then
|
|
29
|
+
error("Run this from a tmux-ide session: it works on that session's repository " ..
|
|
30
|
+
"(from the CLI, run it inside tmux or pass --session).", 0)
|
|
31
|
+
end
|
|
32
|
+
return ctx.session
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
-- One ide.worktree operation for ctx's session. Errors from the daemon
|
|
36
|
+
-- (a refused branch name, a worktree with changes) reach the palette as is.
|
|
37
|
+
local function worktree(ctx, op, spec)
|
|
38
|
+
spec = spec or {}
|
|
39
|
+
spec.session = session_of(ctx)
|
|
40
|
+
return ide.worktree[op](spec)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
-- The task worktrees (not the main checkout), as palette choices.
|
|
44
|
+
local function task_choices(ctx)
|
|
45
|
+
local listed = worktree(ctx, "list")
|
|
46
|
+
local choices = {}
|
|
47
|
+
for _, entry in ipairs(listed.worktrees or {}) do
|
|
48
|
+
if not entry.primary and entry.branch then
|
|
49
|
+
local state = entry.running and ((entry.status or "running") .. " · " .. entry.session) or "no session"
|
|
50
|
+
choices[#choices + 1] = { value = entry.branch, label = entry.branch .. " · " .. state }
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
return choices
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
local task_param = { name = "task", label = "Task", type = "choice", choices = task_choices }
|
|
57
|
+
|
|
58
|
+
ide.action.register{
|
|
59
|
+
id = "new",
|
|
60
|
+
title = "New task",
|
|
61
|
+
category = "Tasks",
|
|
62
|
+
description = "Create a worktree on a new branch and open a session in it",
|
|
63
|
+
params = {
|
|
64
|
+
{ name = "branch", label = "Branch name", pattern = "^[%w][%w._/-]*$" },
|
|
65
|
+
{ name = "from", label = "Start from (branch or commit)", optional = true },
|
|
66
|
+
},
|
|
67
|
+
run = function(ctx, args)
|
|
68
|
+
local made = worktree(ctx, "create", { branch = args.branch, from = args.from })
|
|
69
|
+
return "Started " .. made.branch .. " in session " .. tostring(made.session)
|
|
70
|
+
end,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
ide.action.register{
|
|
74
|
+
id = "open",
|
|
75
|
+
title = "Open task",
|
|
76
|
+
category = "Tasks",
|
|
77
|
+
description = "Open (or start) the session of a task's worktree",
|
|
78
|
+
params = { task_param },
|
|
79
|
+
run = function(ctx, args)
|
|
80
|
+
local opened = worktree(ctx, "open", { branch = args.task })
|
|
81
|
+
return (opened.created and "Opened " or "Already open: ") .. opened.session
|
|
82
|
+
end,
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
ide.action.register{
|
|
86
|
+
id = "remove",
|
|
87
|
+
title = "Remove task",
|
|
88
|
+
category = "Tasks",
|
|
89
|
+
description = "Close a task's session and remove its worktree (refused if it has changes)",
|
|
90
|
+
dangerous = true,
|
|
91
|
+
params = { task_param },
|
|
92
|
+
run = function(ctx, args)
|
|
93
|
+
local removed = worktree(ctx, "remove", { branch = args.task })
|
|
94
|
+
return "Removed " .. removed.branch .. (removed.sessionKilled and " and closed its session" or "")
|
|
95
|
+
end,
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
ide.action.register{
|
|
99
|
+
id = "list",
|
|
100
|
+
title = "List tasks",
|
|
101
|
+
category = "Tasks",
|
|
102
|
+
run = function(ctx)
|
|
103
|
+
local choices = task_choices(ctx)
|
|
104
|
+
if #choices == 0 then return "No tasks yet. Start one with New task." end
|
|
105
|
+
local lines = {}
|
|
106
|
+
for _, choice in ipairs(choices) do lines[#lines + 1] = choice.label end
|
|
107
|
+
return table.concat(lines, "\n")
|
|
108
|
+
end,
|
|
109
|
+
}
|
|
110
|
+
`;
|
|
111
|
+
|
|
112
|
+
/** The examples `tmux-ide plugin example` can install, by name. */
|
|
113
|
+
export const PLUGIN_EXAMPLES: Readonly<
|
|
114
|
+
Record<string, { readonly manifest: object; readonly files: Readonly<Record<string, string>> }>
|
|
115
|
+
> = {
|
|
116
|
+
"worktree-tasks": {
|
|
117
|
+
manifest: WORKTREE_TASKS_MANIFEST,
|
|
118
|
+
files: { "init.lua": WORKTREE_TASKS_INIT_LUA },
|
|
119
|
+
},
|
|
120
|
+
};
|