tmux-ide 2.9.20 → 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 +14684 -3865
- 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/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/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/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/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/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/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,343 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a plugin's host functions do on the daemon side. The Lua side calls
|
|
3
|
+
* `_exec`, `_panes` and the rest (see host/host-api-prelude.ts); the host
|
|
4
|
+
* forwards each call here, and this answers with plain JSON.
|
|
5
|
+
*
|
|
6
|
+
* Pane, window and session calls go through the daemon's own HTTP surface
|
|
7
|
+
* with the owner credential: the action dispatcher for workspace actions and
|
|
8
|
+
* the automation API for reading and typing into panes. Plugins therefore
|
|
9
|
+
* pass exactly the gates a CLI or the app would.
|
|
10
|
+
*/
|
|
11
|
+
import { randomUUID } from "node:crypto";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import {
|
|
14
|
+
AutomationExecuteResponseSchemaZ,
|
|
15
|
+
AutomationPaneEndpointSchemaZ,
|
|
16
|
+
AutomationPanesResponseSchemaZ,
|
|
17
|
+
AutomationReserveResponseSchemaZ,
|
|
18
|
+
type AutomationOperationIntent,
|
|
19
|
+
} from "@tmux-ide/contracts";
|
|
20
|
+
import { z } from "zod";
|
|
21
|
+
import { PluginPrograms, runPluginExec, type ExecPolicy, type PaneContext } from "./plugin-exec.ts";
|
|
22
|
+
import { PluginStore, pluginStorePath } from "./plugin-store.ts";
|
|
23
|
+
import {
|
|
24
|
+
worktreeCall,
|
|
25
|
+
WorktreeCallError,
|
|
26
|
+
type WorktreeCallDependencies,
|
|
27
|
+
} from "./worktree-calls.ts";
|
|
28
|
+
import { INIT_PLUGIN_NAME, RUN_STATE_NAME } from "./protocol.ts";
|
|
29
|
+
|
|
30
|
+
/** Workspace actions a plugin may dispatch (all multiplexer verbs on panes, windows and sessions). */
|
|
31
|
+
export const PLUGIN_WORKSPACE_ACTIONS: ReadonlySet<string> = new Set([
|
|
32
|
+
"workspace.window.split",
|
|
33
|
+
"workspace.window.kill",
|
|
34
|
+
"workspace.pane.kill",
|
|
35
|
+
"workspace.session.kill",
|
|
36
|
+
"workspace.rename",
|
|
37
|
+
"workspace.pane.zoom.toggle",
|
|
38
|
+
"workspace.pane.select",
|
|
39
|
+
"workspace.pane.swap",
|
|
40
|
+
"workspace.pane.resize",
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
export const NOTIFY_MAX_CHARS = 200;
|
|
44
|
+
|
|
45
|
+
export interface DaemonEndpoint {
|
|
46
|
+
readonly baseUrl: string;
|
|
47
|
+
readonly ownerToken: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface HostCallDependencies {
|
|
51
|
+
readonly stateHome: string;
|
|
52
|
+
/** The daemon's own HTTP endpoint; null before it is listening. */
|
|
53
|
+
readonly endpoint: () => DaemonEndpoint | null;
|
|
54
|
+
readonly fetch?: typeof fetch;
|
|
55
|
+
/** Run tmux against the daemon's server and return stdout. */
|
|
56
|
+
readonly tmux: (args: readonly string[]) => Promise<string>;
|
|
57
|
+
readonly baseEnv: () => NodeJS.ProcessEnv;
|
|
58
|
+
readonly secrets: () => ReadonlySet<string>;
|
|
59
|
+
/** Show a short message to the people using tmux-ide. */
|
|
60
|
+
readonly notify: (plugin: string, text: string, level: NotifyLevel) => Promise<void>;
|
|
61
|
+
/** Tests replace the program runner. */
|
|
62
|
+
readonly exec?: typeof runPluginExec;
|
|
63
|
+
/** Files programs may not read: the daemon record and credential files. */
|
|
64
|
+
readonly unreadable?: () => readonly string[];
|
|
65
|
+
/** The longest a program may run: the callback wall clock. */
|
|
66
|
+
readonly execMaxTimeoutMs?: number;
|
|
67
|
+
/** The daemon's other sockets (its control socket): no program may connect. */
|
|
68
|
+
readonly privateSockets?: () => readonly string[];
|
|
69
|
+
/** Directories holding tmux sockets, kept from programs of plugins without panes.send. */
|
|
70
|
+
readonly tmuxSocketDirs?: () => Promise<readonly string[]>;
|
|
71
|
+
/** Runs the tmux-ide CLI for ide.worktree; without it, ide.worktree is unavailable. */
|
|
72
|
+
readonly runCli?: WorktreeCallDependencies["runCli"];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export type NotifyLevel = "info" | "warn" | "error";
|
|
76
|
+
|
|
77
|
+
export interface HostCallCaller {
|
|
78
|
+
readonly plugin: string;
|
|
79
|
+
/** The invoke or param-options request whose callback made this call. */
|
|
80
|
+
readonly origin?: string;
|
|
81
|
+
/** Whether the plugin may still act (not disabled, unloaded or revoked since). */
|
|
82
|
+
readonly active?: () => boolean;
|
|
83
|
+
readonly capabilities: {
|
|
84
|
+
readonly exec: readonly string[] | "any";
|
|
85
|
+
readonly shell: boolean;
|
|
86
|
+
/** Type into panes and run programs as if typed there (`panes.send`). */
|
|
87
|
+
readonly panes: boolean;
|
|
88
|
+
/** ide.worktree (`worktree`). */
|
|
89
|
+
readonly worktree?: boolean;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export class HostCallError extends Error {}
|
|
94
|
+
|
|
95
|
+
/** The daemon's HTTP port, which programs may not connect to on any address. */
|
|
96
|
+
function daemonPort(endpoint: DaemonEndpoint | null): number[] {
|
|
97
|
+
if (!endpoint) return [];
|
|
98
|
+
try {
|
|
99
|
+
const port = Number(new URL(endpoint.baseUrl).port);
|
|
100
|
+
return Number.isInteger(port) && port > 0 ? [port] : [];
|
|
101
|
+
} catch {
|
|
102
|
+
return [];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function fail(message: string): never {
|
|
107
|
+
throw new HostCallError(message);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const EndpointArgZ = AutomationPaneEndpointSchemaZ;
|
|
111
|
+
const PaneReadArgsZ = z.tuple([EndpointArgZ]);
|
|
112
|
+
const PaneSendArgsZ = z.tuple([
|
|
113
|
+
EndpointArgZ,
|
|
114
|
+
z
|
|
115
|
+
.string()
|
|
116
|
+
.min(1)
|
|
117
|
+
.max(16 * 1024),
|
|
118
|
+
z.boolean(),
|
|
119
|
+
]);
|
|
120
|
+
const ActionArgsZ = z.tuple([z.string(), z.record(z.string(), z.json())]);
|
|
121
|
+
const NotifyArgsZ = z.tuple([
|
|
122
|
+
z.string().min(1).max(10_000),
|
|
123
|
+
z.enum(["info", "warn", "error"]).optional(),
|
|
124
|
+
]);
|
|
125
|
+
const StoreKeyZ = z.string().min(1).max(200);
|
|
126
|
+
const ActionEnvelopeZ = z.discriminatedUnion("ok", [
|
|
127
|
+
z.object({ ok: z.literal(true), result: z.unknown().optional() }),
|
|
128
|
+
z.object({ ok: z.literal(false), error: z.object({ message: z.string() }) }),
|
|
129
|
+
]);
|
|
130
|
+
|
|
131
|
+
/** Parse `#{socket_path}\t#{pid}\t#{session_id}\t#{pane_current_path}`. */
|
|
132
|
+
export function paneContextFromTmux(pane: string, line: string): PaneContext | null {
|
|
133
|
+
const [socket, pid, session, cwd] = line.replace(/\n$/u, "").split("\t");
|
|
134
|
+
const index = /^\$(\d+)$/u.exec(session ?? "")?.[1];
|
|
135
|
+
if (!socket || !pid || !/^\d+$/u.test(pid) || index === undefined || !cwd) return null;
|
|
136
|
+
return { tmux: `${socket},${pid},${index}`, pane, cwd };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function createHostCallHandler(deps: HostCallDependencies) {
|
|
140
|
+
const programs = new PluginPrograms();
|
|
141
|
+
const request = async (path: string, init: RequestInit): Promise<unknown> => {
|
|
142
|
+
const endpoint = deps.endpoint();
|
|
143
|
+
if (!endpoint) fail("E_UNAVAILABLE: the daemon is not ready yet");
|
|
144
|
+
const response = await (deps.fetch ?? fetch)(`${endpoint.baseUrl}${path}`, {
|
|
145
|
+
...init,
|
|
146
|
+
redirect: "error",
|
|
147
|
+
headers: {
|
|
148
|
+
Authorization: `Bearer ${endpoint.ownerToken}`,
|
|
149
|
+
"Content-Type": "application/json",
|
|
150
|
+
...(init.headers as Record<string, string> | undefined),
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
const body: unknown = await response.json().catch(() => null);
|
|
154
|
+
if (!response.ok) {
|
|
155
|
+
const error = body && typeof body === "object" && "error" in body ? body.error : null;
|
|
156
|
+
const message =
|
|
157
|
+
typeof error === "string"
|
|
158
|
+
? error
|
|
159
|
+
: error && typeof error === "object" && "message" in error
|
|
160
|
+
? String((error as { message: unknown }).message)
|
|
161
|
+
: error && typeof error === "object" && "code" in error
|
|
162
|
+
? String((error as { code: unknown }).code)
|
|
163
|
+
: `HTTP ${response.status}`;
|
|
164
|
+
fail(`E_FAILED: ${message}`);
|
|
165
|
+
}
|
|
166
|
+
return body;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/** Refuse to go on for a plugin stopped (disabled, unloaded, revoked) meanwhile. */
|
|
170
|
+
const stillActive = (caller: HostCallCaller) => {
|
|
171
|
+
if (caller.active && !caller.active())
|
|
172
|
+
fail("E_UNAVAILABLE: the plugin was stopped before this finished");
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const automation = async (caller: HostCallCaller, intent: AutomationOperationIntent) => {
|
|
176
|
+
stillActive(caller);
|
|
177
|
+
const reserved = AutomationReserveResponseSchemaZ.parse(
|
|
178
|
+
await request("/api/v1/automation/reserve", {
|
|
179
|
+
method: "POST",
|
|
180
|
+
body: JSON.stringify({ version: 1, intent, origin: "sdk" }),
|
|
181
|
+
}),
|
|
182
|
+
);
|
|
183
|
+
// A send is reserved, then executed; nothing types after a stop.
|
|
184
|
+
stillActive(caller);
|
|
185
|
+
return AutomationExecuteResponseSchemaZ.parse(
|
|
186
|
+
await request("/api/v1/automation/execute", {
|
|
187
|
+
method: "POST",
|
|
188
|
+
body: JSON.stringify({ version: 1, intent, origin: "sdk", handle: reserved.handle }),
|
|
189
|
+
}),
|
|
190
|
+
);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const resolvePane = async (pane: string): Promise<PaneContext | null> => {
|
|
194
|
+
try {
|
|
195
|
+
const line = await deps.tmux([
|
|
196
|
+
"display-message",
|
|
197
|
+
"-p",
|
|
198
|
+
"-t",
|
|
199
|
+
pane,
|
|
200
|
+
"#{socket_path}\t#{pid}\t#{session_id}\t#{pane_current_path}",
|
|
201
|
+
]);
|
|
202
|
+
return paneContextFromTmux(pane, line);
|
|
203
|
+
} catch {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const store = (plugin: string) => {
|
|
209
|
+
if (plugin === RUN_STATE_NAME) fail("E_UNAVAILABLE: `lua run` has no store");
|
|
210
|
+
return new PluginStore(pluginStorePath(deps.stateHome, plugin));
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const methods: Record<string, (caller: HostCallCaller, args: unknown[]) => Promise<unknown>> = {
|
|
214
|
+
async _exec(caller, args) {
|
|
215
|
+
const policy: ExecPolicy = {
|
|
216
|
+
programs: caller.capabilities.exec,
|
|
217
|
+
shell: caller.capabilities.shell,
|
|
218
|
+
panes: caller.capabilities.panes,
|
|
219
|
+
};
|
|
220
|
+
return (deps.exec ?? runPluginExec)(args[0], {
|
|
221
|
+
policy,
|
|
222
|
+
sandbox: {
|
|
223
|
+
stateHome: deps.stateHome,
|
|
224
|
+
writable: join(deps.stateHome, "plugin-data", caller.plugin),
|
|
225
|
+
unreadable: deps.unreadable?.() ?? [],
|
|
226
|
+
tmuxSockets: caller.capabilities.panes ? [] : ((await deps.tmuxSocketDirs?.()) ?? []),
|
|
227
|
+
privateSockets: deps.privateSockets?.() ?? [],
|
|
228
|
+
daemonPorts: daemonPort(deps.endpoint()),
|
|
229
|
+
},
|
|
230
|
+
baseEnv: deps.baseEnv(),
|
|
231
|
+
resolvePane,
|
|
232
|
+
secrets: deps.secrets,
|
|
233
|
+
...(deps.execMaxTimeoutMs === undefined ? {} : { maxTimeoutMs: deps.execMaxTimeoutMs }),
|
|
234
|
+
track: (child) => programs.track(caller.plugin, child, caller.origin),
|
|
235
|
+
...(caller.active ? { stillAllowed: caller.active } : {}),
|
|
236
|
+
});
|
|
237
|
+
},
|
|
238
|
+
async _panes() {
|
|
239
|
+
const panes = AutomationPanesResponseSchemaZ.parse(
|
|
240
|
+
await request("/api/v1/automation/panes", { method: "GET" }),
|
|
241
|
+
);
|
|
242
|
+
return panes.panes.map((pane) => ({
|
|
243
|
+
endpoint: pane.endpoint,
|
|
244
|
+
title: pane.title,
|
|
245
|
+
session: pane.sessionName,
|
|
246
|
+
}));
|
|
247
|
+
},
|
|
248
|
+
async _pane_read(caller, args) {
|
|
249
|
+
const [target] = PaneReadArgsZ.parse(args);
|
|
250
|
+
const result = await automation(caller, { kind: "read", target, source: null });
|
|
251
|
+
return result.read?.availability === "available" ? result.read.text : null;
|
|
252
|
+
},
|
|
253
|
+
async _pane_send(caller, args) {
|
|
254
|
+
if (!caller.capabilities.panes)
|
|
255
|
+
fail("E_DENIED: typing into a pane needs the manifest capability 'panes.send'");
|
|
256
|
+
const [target, text, enter] = PaneSendArgsZ.parse(args);
|
|
257
|
+
await automation(caller, { kind: "send", target, source: null, text, enter });
|
|
258
|
+
return true;
|
|
259
|
+
},
|
|
260
|
+
async _action(caller, args) {
|
|
261
|
+
const [name, input] = ActionArgsZ.parse(args);
|
|
262
|
+
if (!PLUGIN_WORKSPACE_ACTIONS.has(name)) fail(`E_DENIED: plugins may not run '${name}'`);
|
|
263
|
+
stillActive(caller);
|
|
264
|
+
const body = await request(`/api/v2/action/${encodeURIComponent(name)}`, {
|
|
265
|
+
method: "POST",
|
|
266
|
+
headers: { "X-Tmux-Ide-Operation-Id": randomUUID() },
|
|
267
|
+
body: JSON.stringify(input),
|
|
268
|
+
});
|
|
269
|
+
// The dispatcher answers 200 with an { ok: false, error } envelope for
|
|
270
|
+
// refusals it reaches after the gate.
|
|
271
|
+
const envelope = ActionEnvelopeZ.safeParse(body);
|
|
272
|
+
if (!envelope.success) fail("E_FAILED: the daemon sent an unexpected answer");
|
|
273
|
+
if (!envelope.data.ok) fail(`E_FAILED: ${envelope.data.error.message}`);
|
|
274
|
+
return envelope.data.result ?? null;
|
|
275
|
+
},
|
|
276
|
+
async _worktree(caller, args) {
|
|
277
|
+
if (!caller.capabilities.worktree)
|
|
278
|
+
fail("E_DENIED: ide.worktree needs the manifest capability 'worktree'");
|
|
279
|
+
if (!deps.runCli) fail("E_UNAVAILABLE: ide.worktree is not available here");
|
|
280
|
+
try {
|
|
281
|
+
return await worktreeCall(args[0], args[1], {
|
|
282
|
+
tmux: deps.tmux,
|
|
283
|
+
runCli: deps.runCli,
|
|
284
|
+
...(caller.active ? { stillActive: caller.active } : {}),
|
|
285
|
+
});
|
|
286
|
+
} catch (error) {
|
|
287
|
+
if (error instanceof WorktreeCallError) fail(error.message);
|
|
288
|
+
throw error;
|
|
289
|
+
}
|
|
290
|
+
},
|
|
291
|
+
async _notify(caller, args) {
|
|
292
|
+
const [text, level = "info"] = NotifyArgsZ.parse(args);
|
|
293
|
+
const clipped = text.replace(/\s+/gu, " ").trim().slice(0, NOTIFY_MAX_CHARS);
|
|
294
|
+
if (!clipped) return false;
|
|
295
|
+
await deps.notify(caller.plugin, clipped, level);
|
|
296
|
+
return true;
|
|
297
|
+
},
|
|
298
|
+
async _store_get(caller, args) {
|
|
299
|
+
return store(caller.plugin).get(StoreKeyZ.parse(args[0]));
|
|
300
|
+
},
|
|
301
|
+
async _store_set(caller, args) {
|
|
302
|
+
store(caller.plugin).set(StoreKeyZ.parse(args[0]), args[1]);
|
|
303
|
+
return true;
|
|
304
|
+
},
|
|
305
|
+
async _store_keys(caller) {
|
|
306
|
+
return store(caller.plugin).keys();
|
|
307
|
+
},
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
return {
|
|
311
|
+
methods: Object.keys(methods),
|
|
312
|
+
/**
|
|
313
|
+
* Kill the programs `plugin` started (or every plugin's) that still run;
|
|
314
|
+
* with `origin`, only those started for that request.
|
|
315
|
+
*/
|
|
316
|
+
stopPrograms(plugin?: string, origin?: string): number {
|
|
317
|
+
return programs.kill(plugin, origin);
|
|
318
|
+
},
|
|
319
|
+
runningPrograms(plugin: string): number {
|
|
320
|
+
return programs.running(plugin);
|
|
321
|
+
},
|
|
322
|
+
async handle(caller: HostCallCaller, method: string, args: unknown[]): Promise<unknown> {
|
|
323
|
+
const fn = Object.hasOwn(methods, method) ? methods[method] : undefined;
|
|
324
|
+
if (!fn) fail(`E_INVALID: no host function '${method}'`);
|
|
325
|
+
try {
|
|
326
|
+
return await fn(caller, args);
|
|
327
|
+
} catch (error) {
|
|
328
|
+
if (error instanceof HostCallError) throw error;
|
|
329
|
+
if (error instanceof z.ZodError) fail(`E_INVALID: bad arguments to ${method.slice(1)}`);
|
|
330
|
+
throw new HostCallError((error as Error).message);
|
|
331
|
+
}
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export type HostCallHandler = ReturnType<typeof createHostCallHandler>;
|
|
337
|
+
|
|
338
|
+
/** What init.lua and `lua run` may do: everything a plugin could be granted. */
|
|
339
|
+
export function ownerCapabilities(plugin: string): HostCallCaller["capabilities"] | null {
|
|
340
|
+
return plugin === INIT_PLUGIN_NAME || plugin === RUN_STATE_NAME
|
|
341
|
+
? { exec: "any", shell: true, panes: true, worktree: true }
|
|
342
|
+
: null;
|
|
343
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `manifest.json` in each plugin directory. JSON, so the daemon and the
|
|
3
|
+
* installer can read what a plugin asks for without running any Lua.
|
|
4
|
+
*/
|
|
5
|
+
import { plainTextSchema } from "@tmux-ide/contracts";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { PLUGIN_API_VERSION, PluginNameSchemaZ } from "./protocol.ts";
|
|
8
|
+
|
|
9
|
+
const SettingSchemaZ = z
|
|
10
|
+
.object({
|
|
11
|
+
type: z.enum(["string", "integer", "number", "boolean"]),
|
|
12
|
+
default: z.union([z.string(), z.number(), z.boolean()]).optional(),
|
|
13
|
+
description: plainTextSchema(0, 500).optional(),
|
|
14
|
+
optional: z.boolean().optional(),
|
|
15
|
+
minimum: z.number().optional(),
|
|
16
|
+
maximum: z.number().optional(),
|
|
17
|
+
})
|
|
18
|
+
.strict();
|
|
19
|
+
|
|
20
|
+
export const PluginManifestSchemaZ = z
|
|
21
|
+
.object({
|
|
22
|
+
name: PluginNameSchemaZ,
|
|
23
|
+
version: z.string().regex(/^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/u, "version must be semver"),
|
|
24
|
+
description: plainTextSchema(0, 500).optional(),
|
|
25
|
+
/** The Lua API versions this plugin works with: `^1.0` or `1.0`. */
|
|
26
|
+
api: z.string().regex(/^\^?\d+\.\d+$/u, "api must look like ^1.0"),
|
|
27
|
+
entry: z
|
|
28
|
+
.string()
|
|
29
|
+
.regex(/^[\w-]+(?:\/[\w-]+)*\.lua$/u, "entry must be a relative .lua path")
|
|
30
|
+
.default("init.lua"),
|
|
31
|
+
capabilities: z
|
|
32
|
+
.object({
|
|
33
|
+
exec: z
|
|
34
|
+
.array(z.string().regex(/^[\w.+-]{1,64}$/u))
|
|
35
|
+
.max(32)
|
|
36
|
+
.optional(),
|
|
37
|
+
sidebar: z.boolean().optional(),
|
|
38
|
+
status: z.boolean().optional(),
|
|
39
|
+
keys: z.boolean().optional(),
|
|
40
|
+
/** Run any shell command line (ide.exec with shell = true). */
|
|
41
|
+
shell: z.boolean().optional(),
|
|
42
|
+
/** Type into panes (pane:send) and run programs in one (ide.exec context). */
|
|
43
|
+
"panes.send": z.boolean().optional(),
|
|
44
|
+
/** ide.worktree: create, open and remove git worktrees and their sessions. */
|
|
45
|
+
worktree: z.boolean().optional(),
|
|
46
|
+
})
|
|
47
|
+
.strict()
|
|
48
|
+
.default({}),
|
|
49
|
+
settings: z.record(z.string().regex(/^[a-z_][a-z0-9_]{0,63}$/u), SettingSchemaZ).default({}),
|
|
50
|
+
})
|
|
51
|
+
.strict();
|
|
52
|
+
export type PluginManifest = z.infer<typeof PluginManifestSchemaZ>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Whether this host's API version satisfies a manifest's `api` range.
|
|
56
|
+
* `^X.Y` and `X.Y` both mean: major X, minor at least Y.
|
|
57
|
+
*/
|
|
58
|
+
export function apiSatisfies(range: string, hostVersion: string = PLUGIN_API_VERSION): boolean {
|
|
59
|
+
const wanted = /^\^?(\d+)\.(\d+)$/u.exec(range);
|
|
60
|
+
const have = /^(\d+)\.(\d+)$/u.exec(hostVersion);
|
|
61
|
+
if (!wanted || !have) return false;
|
|
62
|
+
return Number(wanted[1]) === Number(have[1]) && Number(have[2]) >= Number(wanted[2]);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Default setting values declared by a manifest. */
|
|
66
|
+
export function manifestDefaults(
|
|
67
|
+
manifest: PluginManifest,
|
|
68
|
+
): Record<string, string | number | boolean> {
|
|
69
|
+
const out: Record<string, string | number | boolean> = {};
|
|
70
|
+
for (const [key, setting] of Object.entries(manifest.settings)) {
|
|
71
|
+
if (setting.default !== undefined) out[key] = setting.default;
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|