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
|
@@ -168,6 +168,8 @@ import { setActivationBackend, type ProjectActivationOptions } from "./active-pr
|
|
|
168
168
|
import { readOrMintEnvironmentId } from "./environment-identity.ts";
|
|
169
169
|
import { captureDaemonProvenance } from "./daemon-provenance.ts";
|
|
170
170
|
import { registerLogSecret, setLogIdentity } from "./log.ts";
|
|
171
|
+
import { createDaemonPluginSupervisor, pluginsEnabled } from "../plugins/daemon-plugins.ts";
|
|
172
|
+
import type { PluginSupervisor } from "../plugins/supervisor.ts";
|
|
171
173
|
import {
|
|
172
174
|
canonicalDaemonUrl,
|
|
173
175
|
getCanonicalDaemonInfoPath,
|
|
@@ -249,6 +251,11 @@ export interface EmbeddedDaemonOptions {
|
|
|
249
251
|
silent?: boolean;
|
|
250
252
|
/** Diagnostic product version advertised by daemon.json. */
|
|
251
253
|
productVersion?: string;
|
|
254
|
+
/**
|
|
255
|
+
* @internal Run the plugin host for `~/.tmux-ide/init.lua` and plugins.
|
|
256
|
+
* Only the canonical headless owner sets this; `TMUX_IDE_PLUGINS=0` overrides it.
|
|
257
|
+
*/
|
|
258
|
+
plugins?: boolean;
|
|
252
259
|
/** @deprecated Use bindHostname. */
|
|
253
260
|
hostname?: string;
|
|
254
261
|
}
|
|
@@ -834,8 +841,10 @@ async function startHttpServer({
|
|
|
834
841
|
fleetPreviewCapture,
|
|
835
842
|
sessionRuntimeRegistry,
|
|
836
843
|
serverOwners,
|
|
844
|
+
pluginBackend,
|
|
837
845
|
}: {
|
|
838
846
|
serverOwners: Awaited<ReturnType<typeof createEmbeddedTmuxServerOwners>>;
|
|
847
|
+
pluginBackend: PluginSupervisor | null;
|
|
839
848
|
sessionName: string;
|
|
840
849
|
requestedPort: number;
|
|
841
850
|
bindHostname: string;
|
|
@@ -939,6 +948,7 @@ async function startHttpServer({
|
|
|
939
948
|
catalogLiveSessions,
|
|
940
949
|
catalogFleet,
|
|
941
950
|
fleetPreviewCapture,
|
|
951
|
+
pluginBackend,
|
|
942
952
|
});
|
|
943
953
|
mountTerminalNativeBackingRoute(app, {
|
|
944
954
|
ownerToken: localBypassToken ?? null,
|
|
@@ -1455,6 +1465,29 @@ async function startEmbeddedDaemonGeneration(
|
|
|
1455
1465
|
setFleetFactsObserverDiagnostics(null);
|
|
1456
1466
|
await runtimeTraceWriter?.close(incomplete);
|
|
1457
1467
|
};
|
|
1468
|
+
// Plugins reach the daemon's own routes over loopback with the owner token.
|
|
1469
|
+
const pluginEndpoint = canonicalDaemonUrl(
|
|
1470
|
+
"http",
|
|
1471
|
+
bindHostname === "0.0.0.0" || bindHostname === "::" ? "127.0.0.1" : bindHostname,
|
|
1472
|
+
port,
|
|
1473
|
+
);
|
|
1474
|
+
// Created now so its routes mount with the server; it spawns nothing until
|
|
1475
|
+
// start(), which runs once this generation is published.
|
|
1476
|
+
const pluginSupervisor =
|
|
1477
|
+
opts.plugins && pluginsEnabled()
|
|
1478
|
+
? createDaemonPluginSupervisor({
|
|
1479
|
+
productVersion,
|
|
1480
|
+
secrets: [localBypassToken, authToken ?? null],
|
|
1481
|
+
endpoint: () =>
|
|
1482
|
+
pluginEndpoint ? { baseUrl: pluginEndpoint, ownerToken: localBypassToken } : null,
|
|
1483
|
+
tmux: (args) => fleetFactsTmuxRunner(args, AbortSignal.timeout(5000)),
|
|
1484
|
+
onCatalogChange: () =>
|
|
1485
|
+
broadcastResourceChanged(
|
|
1486
|
+
{ workspaceName: null, resource: "command-catalog" },
|
|
1487
|
+
instanceId,
|
|
1488
|
+
),
|
|
1489
|
+
})
|
|
1490
|
+
: null;
|
|
1458
1491
|
let startedServer: Awaited<ReturnType<typeof startHttpServer>>;
|
|
1459
1492
|
try {
|
|
1460
1493
|
const selector = tmuxAuthority.socketSelector;
|
|
@@ -2080,6 +2113,7 @@ async function startEmbeddedDaemonGeneration(
|
|
|
2080
2113
|
setFleetFactsTmuxRunner(fleetFactsTmuxRunner);
|
|
2081
2114
|
startedServer = await startHttpServer({
|
|
2082
2115
|
serverOwners,
|
|
2116
|
+
pluginBackend: pluginSupervisor,
|
|
2083
2117
|
sessionName,
|
|
2084
2118
|
requestedPort: port,
|
|
2085
2119
|
bindHostname,
|
|
@@ -2211,6 +2245,7 @@ async function startEmbeddedDaemonGeneration(
|
|
|
2211
2245
|
);
|
|
2212
2246
|
};
|
|
2213
2247
|
const abortStartedServer = async (): Promise<void> => {
|
|
2248
|
+
await pluginSupervisor?.stop().catch(() => {});
|
|
2214
2249
|
await serverOwners?.dispose();
|
|
2215
2250
|
setFleetFactsTmuxRunner(null);
|
|
2216
2251
|
const terminalFailures = [
|
|
@@ -2315,6 +2350,12 @@ async function startEmbeddedDaemonGeneration(
|
|
|
2315
2350
|
throw error;
|
|
2316
2351
|
}
|
|
2317
2352
|
|
|
2353
|
+
// Plugins load after publication, in the background: a slow or broken
|
|
2354
|
+
// plugin never delays or fails daemon startup.
|
|
2355
|
+
void pluginSupervisor?.start().catch((error: unknown) => {
|
|
2356
|
+
console.warn(`[daemon] plugins failed to start: ${(error as Error).message}`);
|
|
2357
|
+
});
|
|
2358
|
+
|
|
2318
2359
|
let stopped = false;
|
|
2319
2360
|
let stopping: Promise<void> | null = null;
|
|
2320
2361
|
let stopSelf: (() => void) | null = null;
|
|
@@ -2474,6 +2515,7 @@ async function startEmbeddedDaemonGeneration(
|
|
|
2474
2515
|
};
|
|
2475
2516
|
try {
|
|
2476
2517
|
stopped = true;
|
|
2518
|
+
await capture(() => pluginSupervisor?.stop());
|
|
2477
2519
|
await capture(() => setActivationBackend(null));
|
|
2478
2520
|
await capture(() => sessionMonitor?.stop());
|
|
2479
2521
|
paneSourceCredentials.dispose();
|
|
@@ -366,6 +366,7 @@ async function runHeadlessDaemonGeneration(
|
|
|
366
366
|
try {
|
|
367
367
|
handle = await deps.startEmbeddedDaemon({
|
|
368
368
|
launcher: "headless",
|
|
369
|
+
plugins: true,
|
|
369
370
|
...(options.supervisionId
|
|
370
371
|
? { supervisionId: options.supervisionId, predecessor: lifecycle.predecessor }
|
|
371
372
|
: {}),
|
|
@@ -103,23 +103,22 @@ export function createNativeSplitReadiness(options: {
|
|
|
103
103
|
}));
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
* timeout, a lost server or any other failure says nothing, so it
|
|
106
|
+
/** True only when the selected server refuses `tmux-ide-resize-split` by name: a
|
|
107
|
+
* stock tmux, or a tmux-ide tmux built before that command, which may still have
|
|
108
|
+
* the journal. A timeout, a lost server or any other failure says nothing, so it
|
|
109
|
+
* stays false. */
|
|
109
110
|
export function createStockSplitProbe(options: {
|
|
110
111
|
runPinnedTmux: NativeSplitRunner;
|
|
111
112
|
}): () => Promise<boolean> {
|
|
113
|
+
const command = "tmux-ide-resize-split";
|
|
112
114
|
return async () => {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (!new RegExp(`^unknown command: ${command}$`, "mu").test(text)) return false;
|
|
121
|
-
}
|
|
115
|
+
try {
|
|
116
|
+
await options.runPinnedTmux([command, "-V"]);
|
|
117
|
+
return false;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
const failure = error as { stderr?: unknown; cause?: { stderr?: unknown } } | undefined;
|
|
120
|
+
const text = String(failure?.cause?.stderr ?? failure?.stderr ?? "");
|
|
121
|
+
return new RegExp(`^unknown command: ${command}$`, "mu").test(text);
|
|
122
122
|
}
|
|
123
|
-
return true;
|
|
124
123
|
};
|
|
125
124
|
}
|
|
@@ -208,6 +208,22 @@ export function mapWorktreeError(stderr: string, fallbackMessage: string): Workt
|
|
|
208
208
|
// io — git shell-outs (map stderr → WorktreeError)
|
|
209
209
|
// ---------------------------------------------------------------------------
|
|
210
210
|
|
|
211
|
+
/**
|
|
212
|
+
* `env` with git hooks off for every git command run with it: core.hooksPath
|
|
213
|
+
* points at /dev/null, through GIT_CONFIG_* (which outrank the repository's
|
|
214
|
+
* own config, a relative hooksPath that a branch carries included).
|
|
215
|
+
*/
|
|
216
|
+
export function gitHooksOff(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
|
|
217
|
+
const count = Number.parseInt(env.GIT_CONFIG_COUNT ?? "0", 10);
|
|
218
|
+
const index = Number.isInteger(count) && count >= 0 ? count : 0;
|
|
219
|
+
return {
|
|
220
|
+
...env,
|
|
221
|
+
GIT_CONFIG_COUNT: String(index + 1),
|
|
222
|
+
[`GIT_CONFIG_KEY_${index}`]: "core.hooksPath",
|
|
223
|
+
[`GIT_CONFIG_VALUE_${index}`]: "/dev/null",
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
211
227
|
/** Injectable git runner so io wrappers stay unit-testable. */
|
|
212
228
|
export type GitRunner = (repoDir: string, args: string[]) => string;
|
|
213
229
|
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plugin half of the command catalog: the actions each loaded plugin
|
|
3
|
+
* registered with `ide.action.register`, as catalog entries.
|
|
4
|
+
*
|
|
5
|
+
* A plugin's actions are replaced wholesale every time it publishes, and
|
|
6
|
+
* dropped when it unloads, is disabled, loses consent or its host exits.
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
COMMAND_PROTOCOL_VERSION,
|
|
10
|
+
CommandCatalogEntrySchemaZ,
|
|
11
|
+
CommandParamSchemaZ,
|
|
12
|
+
plainTextSchema,
|
|
13
|
+
type CommandCatalogEntry,
|
|
14
|
+
} from "@tmux-ide/contracts";
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
import { MAX_ACTIONS_PER_PLUGIN, MAX_LOCAL_ACTION_ID } from "./host/actions-prelude.ts";
|
|
17
|
+
|
|
18
|
+
/** What the host publishes for one action (plugin-local id). */
|
|
19
|
+
const PublishedActionSchemaZ = z
|
|
20
|
+
.object({
|
|
21
|
+
// "plugin." + a 63-character plugin name + "." + 80 = 151, under the
|
|
22
|
+
// catalog's 160-character id limit.
|
|
23
|
+
id: z
|
|
24
|
+
.string()
|
|
25
|
+
.regex(/^[a-z][a-z0-9-]*(?:\.[a-z0-9-]+)*$/u)
|
|
26
|
+
.max(MAX_LOCAL_ACTION_ID),
|
|
27
|
+
title: plainTextSchema(1, 160),
|
|
28
|
+
category: plainTextSchema(1, 80),
|
|
29
|
+
description: plainTextSchema(1, 500).optional(),
|
|
30
|
+
dangerous: z.boolean(),
|
|
31
|
+
params: z.array(CommandParamSchemaZ).max(32),
|
|
32
|
+
})
|
|
33
|
+
.strict();
|
|
34
|
+
const PublishedActionsSchemaZ = z.array(PublishedActionSchemaZ).max(MAX_ACTIONS_PER_PLUGIN);
|
|
35
|
+
|
|
36
|
+
/** `plugin.<plugin>.<local id>`: the catalog id of a plugin action. */
|
|
37
|
+
export function pluginCommandId(plugin: string, action: string): string {
|
|
38
|
+
return `plugin.${plugin}.${action}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Split a catalog id back into plugin and local action id, or null. */
|
|
42
|
+
export function parsePluginCommandId(id: string): { plugin: string; action: string } | null {
|
|
43
|
+
const match = /^plugin\.([a-z][a-z0-9-]{0,62})\.(.+)$/u.exec(id);
|
|
44
|
+
return match ? { plugin: match[1]!, action: match[2]! } : null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class PluginActionCatalog {
|
|
48
|
+
readonly #byPlugin = new Map<string, CommandCatalogEntry[]>();
|
|
49
|
+
readonly #onChange: () => void;
|
|
50
|
+
#revision = 0;
|
|
51
|
+
|
|
52
|
+
constructor(onChange: () => void = () => {}) {
|
|
53
|
+
this.#onChange = onChange;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
get revision(): number {
|
|
57
|
+
return this.#revision;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Replace `plugin`'s actions from the JSON text it published. Returns an
|
|
62
|
+
* error message, without changing anything, if the list is malformed.
|
|
63
|
+
*/
|
|
64
|
+
publish(plugin: string, json: string): string | null {
|
|
65
|
+
let raw: unknown;
|
|
66
|
+
try {
|
|
67
|
+
raw = JSON.parse(json);
|
|
68
|
+
} catch {
|
|
69
|
+
return "published actions are not JSON";
|
|
70
|
+
}
|
|
71
|
+
// Lua's encoder writes an empty table as [] and omits nil fields, which
|
|
72
|
+
// is exactly the shape the schema expects.
|
|
73
|
+
const parsed = PublishedActionsSchemaZ.safeParse(raw);
|
|
74
|
+
if (!parsed.success) {
|
|
75
|
+
const issue = parsed.error.issues[0];
|
|
76
|
+
return `published actions are invalid at ${issue?.path.join(".") ?? "?"}: ${issue?.message ?? ""}`;
|
|
77
|
+
}
|
|
78
|
+
const built = parsed.data.map((action) =>
|
|
79
|
+
CommandCatalogEntrySchemaZ.safeParse({
|
|
80
|
+
descriptor: {
|
|
81
|
+
version: COMMAND_PROTOCOL_VERSION,
|
|
82
|
+
id: pluginCommandId(plugin, action.id),
|
|
83
|
+
owner: "plugin",
|
|
84
|
+
label: action.title,
|
|
85
|
+
...(action.description ? { description: action.description } : {}),
|
|
86
|
+
category: action.category,
|
|
87
|
+
schemas: { input: `plugin:${plugin}` },
|
|
88
|
+
dangerous: action.dangerous,
|
|
89
|
+
confirmation: action.dangerous ? "dialog" : "none",
|
|
90
|
+
},
|
|
91
|
+
plugin,
|
|
92
|
+
params: action.params,
|
|
93
|
+
}),
|
|
94
|
+
);
|
|
95
|
+
// All or nothing: one bad entry refuses the whole list, and nothing in
|
|
96
|
+
// plugin output can throw out of here into the daemon.
|
|
97
|
+
const entries: CommandCatalogEntry[] = [];
|
|
98
|
+
for (const result of built) {
|
|
99
|
+
if (!result.success) {
|
|
100
|
+
const issue = result.error.issues[0];
|
|
101
|
+
return `published actions are invalid at ${issue?.path.join(".") ?? "?"}: ${issue?.message ?? ""}`;
|
|
102
|
+
}
|
|
103
|
+
entries.push(result.data);
|
|
104
|
+
}
|
|
105
|
+
const previous = this.#byPlugin.get(plugin) ?? [];
|
|
106
|
+
if (entries.length === 0 && previous.length === 0) return null;
|
|
107
|
+
if (JSON.stringify(previous) === JSON.stringify(entries)) return null;
|
|
108
|
+
if (entries.length === 0) this.#byPlugin.delete(plugin);
|
|
109
|
+
else this.#byPlugin.set(plugin, entries);
|
|
110
|
+
this.#changed();
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Drop every action of `plugin`. */
|
|
115
|
+
drop(plugin: string): void {
|
|
116
|
+
if (!this.#byPlugin.delete(plugin)) return;
|
|
117
|
+
this.#changed();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Drop everything, e.g. when the host exits; plugins publish again on load. */
|
|
121
|
+
clear(): void {
|
|
122
|
+
if (this.#byPlugin.size === 0) return;
|
|
123
|
+
this.#byPlugin.clear();
|
|
124
|
+
this.#changed();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
entries(): CommandCatalogEntry[] {
|
|
128
|
+
return [...this.#byPlugin.keys()].sort().flatMap((plugin) => this.#byPlugin.get(plugin) ?? []);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
find(id: string): CommandCatalogEntry | undefined {
|
|
132
|
+
const parsed = parsePluginCommandId(id);
|
|
133
|
+
if (!parsed) return undefined;
|
|
134
|
+
return this.#byPlugin.get(parsed.plugin)?.find((entry) => entry.descriptor.id === id);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
#changed(): void {
|
|
138
|
+
this.#revision += 1;
|
|
139
|
+
this.#onChange();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `tmux-ide action list` and `tmux-ide action run`: the command catalog from
|
|
3
|
+
* a shell, and the way tmux key bindings reach plugin actions.
|
|
4
|
+
*/
|
|
5
|
+
import { parseArgs } from "node:util";
|
|
6
|
+
import type {
|
|
7
|
+
CommandCatalogEntry,
|
|
8
|
+
CommandCatalogResource,
|
|
9
|
+
CommandInvokeResult,
|
|
10
|
+
CommandParam,
|
|
11
|
+
} from "@tmux-ide/contracts";
|
|
12
|
+
import {
|
|
13
|
+
callDaemon,
|
|
14
|
+
formatValue,
|
|
15
|
+
PluginCliError,
|
|
16
|
+
terminalSafeIo,
|
|
17
|
+
type PluginCliIo,
|
|
18
|
+
} from "./cli.ts";
|
|
19
|
+
|
|
20
|
+
export const ACTION_USAGE = `Usage:
|
|
21
|
+
tmux-ide action list [--json] List built-in and plugin commands
|
|
22
|
+
tmux-ide action run <id> [--arg name=value]… [--yes] [--json]
|
|
23
|
+
Run a plugin command (plugin.<plugin>.<id>)
|
|
24
|
+
tmux-ide action run <id> --input '<json>' Run a built-in command with its JSON input
|
|
25
|
+
Options for run:
|
|
26
|
+
--session <name> --pane <id> Where to act (default: this pane, from $TMUX_PANE)
|
|
27
|
+
--yes Confirm a dangerous command without a prompt`;
|
|
28
|
+
|
|
29
|
+
function describeParam(param: CommandParam): string {
|
|
30
|
+
const choices = param.choices?.length
|
|
31
|
+
? ` (${param.choices.map((choice) => choice.value).join(", ")})`
|
|
32
|
+
: param.dynamicChoices
|
|
33
|
+
? " (choices from the plugin)"
|
|
34
|
+
: "";
|
|
35
|
+
const fallback =
|
|
36
|
+
param.default !== undefined
|
|
37
|
+
? `, default ${JSON.stringify(param.default)}`
|
|
38
|
+
: param.dynamicDefault
|
|
39
|
+
? ", default from the plugin"
|
|
40
|
+
: "";
|
|
41
|
+
return `${param.name}: ${param.type}${param.required ? "" : "?"}${choices}${fallback}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function formatCatalog(catalog: CommandCatalogResource): string {
|
|
45
|
+
const plugins = catalog.entries.filter((entry) => entry.plugin);
|
|
46
|
+
const builtins = catalog.entries.filter((entry) => !entry.plugin);
|
|
47
|
+
const lines: string[] = [];
|
|
48
|
+
const section = (title: string, entries: readonly CommandCatalogEntry[]) => {
|
|
49
|
+
if (entries.length === 0) return;
|
|
50
|
+
if (lines.length > 0) lines.push("");
|
|
51
|
+
lines.push(title);
|
|
52
|
+
const width = Math.max(...entries.map((entry) => entry.descriptor.id.length));
|
|
53
|
+
for (const entry of entries) {
|
|
54
|
+
const flag = entry.descriptor.dangerous ? " [asks to confirm]" : "";
|
|
55
|
+
lines.push(` ${entry.descriptor.id.padEnd(width)} ${entry.descriptor.label}${flag}`);
|
|
56
|
+
for (const param of entry.params)
|
|
57
|
+
lines.push(` ${" ".repeat(width)} ${describeParam(param)}`);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
section("Plugin commands", plugins);
|
|
61
|
+
section("Built-in commands (take --input JSON)", builtins);
|
|
62
|
+
if (lines.length === 0) lines.push("No commands.");
|
|
63
|
+
return lines.join("\n");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Turn `--arg name=value` strings into typed arguments for `entry`. */
|
|
67
|
+
export function parseActionArgs(
|
|
68
|
+
entry: CommandCatalogEntry,
|
|
69
|
+
raw: readonly string[],
|
|
70
|
+
): Record<string, string | number | boolean> {
|
|
71
|
+
const out: Record<string, string | number | boolean> = {};
|
|
72
|
+
for (const item of raw) {
|
|
73
|
+
const eq = item.indexOf("=");
|
|
74
|
+
if (eq <= 0) throw new PluginCliError(`--arg expects name=value, got '${item}'`, 2);
|
|
75
|
+
const name = item.slice(0, eq);
|
|
76
|
+
const value = item.slice(eq + 1);
|
|
77
|
+
const param = entry.params.find((candidate) => candidate.name === name);
|
|
78
|
+
if (!param) {
|
|
79
|
+
const known = entry.params.map((candidate) => candidate.name).join(", ") || "none";
|
|
80
|
+
throw new PluginCliError(
|
|
81
|
+
`'${entry.descriptor.id}' has no parameter '${name}' (it takes: ${known})`,
|
|
82
|
+
2,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
if (name in out) throw new PluginCliError(`--arg ${name} is given twice`, 2);
|
|
86
|
+
if (param.type === "number") {
|
|
87
|
+
const number = Number(value);
|
|
88
|
+
if (value.trim() === "" || !Number.isFinite(number))
|
|
89
|
+
throw new PluginCliError(`${name} must be a number`, 2);
|
|
90
|
+
out[name] = number;
|
|
91
|
+
} else if (param.type === "boolean") {
|
|
92
|
+
if (value !== "true" && value !== "false")
|
|
93
|
+
throw new PluginCliError(`${name} must be true or false`, 2);
|
|
94
|
+
out[name] = value === "true";
|
|
95
|
+
} else {
|
|
96
|
+
out[name] = value;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export async function runActionCli(argv: readonly string[], rawIo: PluginCliIo): Promise<number> {
|
|
103
|
+
const io = terminalSafeIo(rawIo);
|
|
104
|
+
try {
|
|
105
|
+
return await actionCli(argv, io);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (error instanceof PluginCliError) {
|
|
108
|
+
io.err(error.message);
|
|
109
|
+
return error.exitCode;
|
|
110
|
+
}
|
|
111
|
+
if (
|
|
112
|
+
error instanceof TypeError &&
|
|
113
|
+
"code" in error &&
|
|
114
|
+
String(error.code).startsWith("ERR_PARSE_ARGS")
|
|
115
|
+
) {
|
|
116
|
+
io.err(`${error.message}\n${ACTION_USAGE}`);
|
|
117
|
+
return 2;
|
|
118
|
+
}
|
|
119
|
+
if (error instanceof Error && error.name === "CliActionInvocationError") {
|
|
120
|
+
io.err(error.message);
|
|
121
|
+
return 1;
|
|
122
|
+
}
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Whether `entry` may run: commands that are not dangerous always may; a
|
|
129
|
+
* dangerous one needs --yes or a "y" from the person at the terminal.
|
|
130
|
+
*/
|
|
131
|
+
async function confirmDangerous(
|
|
132
|
+
entry: CommandCatalogEntry,
|
|
133
|
+
yes: boolean,
|
|
134
|
+
io: PluginCliIo,
|
|
135
|
+
): Promise<boolean> {
|
|
136
|
+
if (!entry.descriptor.dangerous || yes) return true;
|
|
137
|
+
if (!io.interactive())
|
|
138
|
+
throw new PluginCliError(`'${entry.descriptor.label}' asks to confirm: pass --yes.`, 2);
|
|
139
|
+
const answer = (await io.ask(`${entry.descriptor.label}: are you sure? [y/N] `))
|
|
140
|
+
.trim()
|
|
141
|
+
.toLowerCase();
|
|
142
|
+
return answer === "y" || answer === "yes";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function actionCli(argv: readonly string[], io: PluginCliIo): Promise<number> {
|
|
146
|
+
const [sub, ...rest] = argv;
|
|
147
|
+
const { values, positionals } = parseArgs({
|
|
148
|
+
args: [...rest],
|
|
149
|
+
allowPositionals: true,
|
|
150
|
+
strict: true,
|
|
151
|
+
options: {
|
|
152
|
+
json: { type: "boolean" },
|
|
153
|
+
yes: { type: "boolean", short: "y" },
|
|
154
|
+
arg: { type: "string", short: "a", multiple: true },
|
|
155
|
+
input: { type: "string" },
|
|
156
|
+
session: { type: "string" },
|
|
157
|
+
pane: { type: "string" },
|
|
158
|
+
help: { type: "boolean", short: "h" },
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
if (values.help || sub === "help" || sub === "--help" || sub === "-h") {
|
|
162
|
+
io.out(ACTION_USAGE);
|
|
163
|
+
return 0;
|
|
164
|
+
}
|
|
165
|
+
if (!sub) throw new PluginCliError(ACTION_USAGE, 2);
|
|
166
|
+
if (sub === "list") {
|
|
167
|
+
const catalog = await callDaemon<CommandCatalogResource>(
|
|
168
|
+
io,
|
|
169
|
+
"GET",
|
|
170
|
+
"/api/resources/command-catalog",
|
|
171
|
+
);
|
|
172
|
+
io.out(values.json ? JSON.stringify(catalog, null, 2) : formatCatalog(catalog));
|
|
173
|
+
return 0;
|
|
174
|
+
}
|
|
175
|
+
if (sub !== "run")
|
|
176
|
+
throw new PluginCliError(`Unknown action command '${sub}'.\n${ACTION_USAGE}`, 2);
|
|
177
|
+
|
|
178
|
+
const id = positionals[0];
|
|
179
|
+
if (!id || positionals.length > 1)
|
|
180
|
+
throw new PluginCliError(`Name one command to run.\n${ACTION_USAGE}`, 2);
|
|
181
|
+
|
|
182
|
+
if (!id.startsWith("plugin.")) {
|
|
183
|
+
if (values.arg?.length)
|
|
184
|
+
throw new PluginCliError("Built-in commands take --input JSON, not --arg.", 2);
|
|
185
|
+
let input: unknown = {};
|
|
186
|
+
if (values.input !== undefined) {
|
|
187
|
+
try {
|
|
188
|
+
input = JSON.parse(values.input);
|
|
189
|
+
} catch {
|
|
190
|
+
throw new PluginCliError("--input must be JSON.", 2);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const catalog = await callDaemon<CommandCatalogResource>(
|
|
194
|
+
io,
|
|
195
|
+
"GET",
|
|
196
|
+
"/api/resources/command-catalog",
|
|
197
|
+
);
|
|
198
|
+
const entry = catalog.entries.find((candidate) => candidate.descriptor.id === id);
|
|
199
|
+
if (entry && !(await confirmDangerous(entry, values.yes === true, io))) {
|
|
200
|
+
io.out("Cancelled.");
|
|
201
|
+
return 1;
|
|
202
|
+
}
|
|
203
|
+
const result = await io.dispatchBuiltin(id, input);
|
|
204
|
+
io.out(values.json ? JSON.stringify(result, null, 2) : formatValue(result));
|
|
205
|
+
return 0;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (values.input !== undefined)
|
|
209
|
+
throw new PluginCliError("Plugin commands take --arg name=value.", 2);
|
|
210
|
+
const catalog = await callDaemon<CommandCatalogResource>(
|
|
211
|
+
io,
|
|
212
|
+
"GET",
|
|
213
|
+
"/api/resources/command-catalog",
|
|
214
|
+
);
|
|
215
|
+
const entry = catalog.entries.find((candidate) => candidate.descriptor.id === id);
|
|
216
|
+
if (!entry) throw new PluginCliError(`No command named '${id}'. See \`tmux-ide action list\`.`);
|
|
217
|
+
const args = parseActionArgs(entry, values.arg ?? []);
|
|
218
|
+
|
|
219
|
+
const confirmed = await confirmDangerous(entry, values.yes === true, io);
|
|
220
|
+
if (!confirmed) {
|
|
221
|
+
io.out("Cancelled.");
|
|
222
|
+
return 1;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const pane = values.pane ?? io.tmuxPane();
|
|
226
|
+
// Inside tmux, the session defaults to this pane's.
|
|
227
|
+
const session = values.session ?? (pane ? io.tmuxSession?.(pane) : undefined);
|
|
228
|
+
const result = await callDaemon<CommandInvokeResult>(io, "POST", "/api/v2/commands/invoke", {
|
|
229
|
+
id,
|
|
230
|
+
args,
|
|
231
|
+
context: {
|
|
232
|
+
source: "cli",
|
|
233
|
+
...(session ? { session } : {}),
|
|
234
|
+
...(pane ? { pane } : {}),
|
|
235
|
+
},
|
|
236
|
+
...(entry.descriptor.dangerous ? { confirmed: true } : {}),
|
|
237
|
+
});
|
|
238
|
+
if (values.json) {
|
|
239
|
+
io.out(JSON.stringify(result, null, 2));
|
|
240
|
+
return result.ok ? 0 : 1;
|
|
241
|
+
}
|
|
242
|
+
if (!result.ok) {
|
|
243
|
+
io.err(result.error);
|
|
244
|
+
return 1;
|
|
245
|
+
}
|
|
246
|
+
if (result.value !== undefined) io.out(formatValue(result.value));
|
|
247
|
+
return 0;
|
|
248
|
+
}
|