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.
Files changed (97) hide show
  1. package/bin/cli.js +14703 -3882
  2. package/package.json +3 -2
  3. package/packages/contracts/src/command-catalog.ts +116 -0
  4. package/packages/contracts/src/commands.ts +5 -4
  5. package/packages/contracts/src/daemon-events.ts +2 -1
  6. package/packages/contracts/src/index.ts +2 -0
  7. package/packages/contracts/src/plain-text.ts +61 -0
  8. package/packages/daemon/dist/command-center/command-catalog-routes.js +78 -0
  9. package/packages/daemon/dist/command-center/plugin-routes.js +84 -0
  10. package/packages/daemon/dist/command-center/server.js +13 -0
  11. package/packages/daemon/dist/doctor.js +12 -5
  12. package/packages/daemon/dist/lib/cli-action-bridge.js +15 -0
  13. package/packages/daemon/dist/lib/daemon-embed.js +24 -1
  14. package/packages/daemon/dist/lib/headless-daemon.js +1 -0
  15. package/packages/daemon/dist/lib/native-split-capability.js +13 -15
  16. package/packages/daemon/dist/lib/worktree.js +18 -0
  17. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +2 -2
  18. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
  19. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
  20. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +2 -2
  21. package/packages/daemon/dist/plugins/action-catalog.js +129 -0
  22. package/packages/daemon/dist/plugins/action-cli.js +212 -0
  23. package/packages/daemon/dist/plugins/api-table.js +303 -0
  24. package/packages/daemon/dist/plugins/cli.js +556 -0
  25. package/packages/daemon/dist/plugins/consent.js +107 -0
  26. package/packages/daemon/dist/plugins/daemon-plugins.js +132 -0
  27. package/packages/daemon/dist/plugins/discovery.js +142 -0
  28. package/packages/daemon/dist/plugins/examples/worktree-tasks.js +115 -0
  29. package/packages/daemon/dist/plugins/exec-sandbox.js +202 -0
  30. package/packages/daemon/dist/plugins/host/actions-prelude.js +279 -0
  31. package/packages/daemon/dist/plugins/host/entry.js +4 -0
  32. package/packages/daemon/dist/plugins/host/host-api-prelude.js +232 -0
  33. package/packages/daemon/dist/plugins/host/lua-state.js +493 -0
  34. package/packages/daemon/dist/plugins/host/main.js +322 -0
  35. package/packages/daemon/dist/plugins/host/sandbox.js +299 -0
  36. package/packages/daemon/dist/plugins/host-calls.js +279 -0
  37. package/packages/daemon/dist/plugins/manifest.js +69 -0
  38. package/packages/daemon/dist/plugins/plugin-exec.js +261 -0
  39. package/packages/daemon/dist/plugins/plugin-log.js +54 -0
  40. package/packages/daemon/dist/plugins/plugin-store.js +74 -0
  41. package/packages/daemon/dist/plugins/protocol.js +252 -0
  42. package/packages/daemon/dist/plugins/safe-files.js +97 -0
  43. package/packages/daemon/dist/plugins/supervisor.js +790 -0
  44. package/packages/daemon/dist/plugins/worktree-calls.js +154 -0
  45. package/packages/daemon/dist/terminal/mirror/session-channel.js +3 -2
  46. package/packages/daemon/dist/terminal/mirror/window-split-authority.js +1 -1
  47. package/packages/daemon/dist/tui/mirror/runtime/application-home-agents-owner.js +27 -0
  48. package/packages/daemon/dist/tui/mirror/runtime/application-home-experience.js +2 -0
  49. package/packages/daemon/dist/tui/mirror/runtime/application-palette-command-owner.js +30 -2
  50. package/packages/daemon/dist/tui/mirror/runtime/application-plugin-commands.js +152 -0
  51. package/packages/daemon/dist/tui/mirror/runtime/application-plugin-form.js +251 -0
  52. package/packages/daemon/dist/tui/mirror/workspace/application-command-description.js +14 -0
  53. package/packages/daemon/dist/tui/mirror/workspace/palette-list-layout.js +5 -0
  54. package/packages/daemon/src/command-center/command-catalog-routes.ts +116 -0
  55. package/packages/daemon/src/command-center/plugin-routes.ts +110 -0
  56. package/packages/daemon/src/command-center/server.ts +15 -0
  57. package/packages/daemon/src/doctor.ts +12 -5
  58. package/packages/daemon/src/lib/cli-action-bridge.ts +19 -0
  59. package/packages/daemon/src/lib/daemon-embed.ts +42 -0
  60. package/packages/daemon/src/lib/headless-daemon.ts +1 -0
  61. package/packages/daemon/src/lib/native-split-capability.ts +12 -13
  62. package/packages/daemon/src/lib/worktree.ts +16 -0
  63. package/packages/daemon/src/plugins/action-catalog.ts +141 -0
  64. package/packages/daemon/src/plugins/action-cli.ts +248 -0
  65. package/packages/daemon/src/plugins/api-table.ts +346 -0
  66. package/packages/daemon/src/plugins/cli.ts +630 -0
  67. package/packages/daemon/src/plugins/consent.ts +125 -0
  68. package/packages/daemon/src/plugins/daemon-plugins.ts +182 -0
  69. package/packages/daemon/src/plugins/discovery.ts +176 -0
  70. package/packages/daemon/src/plugins/examples/worktree-tasks.ts +120 -0
  71. package/packages/daemon/src/plugins/exec-sandbox.ts +264 -0
  72. package/packages/daemon/src/plugins/host/actions-prelude.ts +280 -0
  73. package/packages/daemon/src/plugins/host/entry.ts +5 -0
  74. package/packages/daemon/src/plugins/host/host-api-prelude.ts +233 -0
  75. package/packages/daemon/src/plugins/host/lua-state.ts +557 -0
  76. package/packages/daemon/src/plugins/host/main.ts +359 -0
  77. package/packages/daemon/src/plugins/host/sandbox.ts +301 -0
  78. package/packages/daemon/src/plugins/host-calls.ts +343 -0
  79. package/packages/daemon/src/plugins/manifest.ts +74 -0
  80. package/packages/daemon/src/plugins/plugin-exec.ts +326 -0
  81. package/packages/daemon/src/plugins/plugin-log.ts +63 -0
  82. package/packages/daemon/src/plugins/plugin-store.ts +80 -0
  83. package/packages/daemon/src/plugins/protocol.ts +275 -0
  84. package/packages/daemon/src/plugins/safe-files.ts +105 -0
  85. package/packages/daemon/src/plugins/supervisor.ts +954 -0
  86. package/packages/daemon/src/plugins/worktree-calls.ts +183 -0
  87. package/packages/daemon/src/terminal/mirror/session-channel.ts +3 -2
  88. package/packages/daemon/src/terminal/mirror/window-split-authority.ts +3 -2
  89. package/packages/daemon/src/tui/mirror/runtime/application-fleet-switcher.tsx +3 -1
  90. package/packages/daemon/src/tui/mirror/runtime/application-home-agents-owner.ts +32 -0
  91. package/packages/daemon/src/tui/mirror/runtime/application-home-experience.ts +2 -0
  92. package/packages/daemon/src/tui/mirror/runtime/application-palette-command-owner.ts +42 -2
  93. package/packages/daemon/src/tui/mirror/runtime/application-palette-preview.tsx +7 -1
  94. package/packages/daemon/src/tui/mirror/runtime/application-plugin-commands.ts +229 -0
  95. package/packages/daemon/src/tui/mirror/runtime/application-plugin-form.ts +312 -0
  96. package/packages/daemon/src/tui/mirror/workspace/application-command-description.ts +44 -2
  97. package/packages/daemon/src/tui/mirror/workspace/palette-list-layout.ts +4 -0
@@ -0,0 +1,264 @@
1
+ /**
2
+ * The OS sandbox every program a plugin runs goes through.
3
+ *
4
+ * It is a seat belt, not the security boundary: consent is. A program a
5
+ * plugin may run can usually do anything your account can (edit your shell
6
+ * profile, start other programs, talk to the network). What the sandbox
7
+ * adds:
8
+ *
9
+ * - no writes under the tmux-ide state home except the plugin's own data
10
+ * directory, so a program cannot simply edit consent records, init.lua or
11
+ * installed plugins in place;
12
+ * - no reads of the daemon record and credential files the daemon names;
13
+ * - on Linux, its own PID namespace and /proc, so it cannot reach the
14
+ * daemon through /proc/<pid>.
15
+ *
16
+ * macOS uses sandbox-exec. Linux uses bubblewrap, but running programs on
17
+ * Linux is off unless TMUX_IDE_PLUGIN_EXEC_LINUX=1: it has not been tested
18
+ * on a real Linux machine yet. Elsewhere, plugins cannot run programs.
19
+ */
20
+ import { accessSync, constants, existsSync, readdirSync, realpathSync, statSync } from "node:fs";
21
+ import { basename, delimiter, dirname, isAbsolute, join } from "node:path";
22
+
23
+ function isDirectory(path: string): boolean {
24
+ try {
25
+ return statSync(path).isDirectory();
26
+ } catch {
27
+ return false;
28
+ }
29
+ }
30
+
31
+ export type SandboxPlan =
32
+ | { readonly ok: true; readonly argv: readonly string[] }
33
+ | { readonly ok: false; readonly reason: string };
34
+
35
+ export interface SandboxPaths {
36
+ /** The state home; nothing under it is writable, except `writable`. */
37
+ readonly stateHome: string;
38
+ /** The plugin's own data directory, the one writable place under the state home. */
39
+ readonly writable: string;
40
+ /**
41
+ * Paths programs may not read or write, each as a prefix: `…/daemon.json`
42
+ * also covers `daemon.json.<claim>.<uuid>.tmp`, `….retired` and the like.
43
+ */
44
+ readonly unreadable?: readonly string[];
45
+ /**
46
+ * Directories whose tmux sockets programs may not connect to (nor read or
47
+ * write in): set unless the plugin has `panes.send`, so a granted program
48
+ * cannot drive your tmux server, and through it your panes.
49
+ */
50
+ readonly tmuxSockets?: readonly string[];
51
+ /** Socket files no program may connect to: the daemon's control socket. */
52
+ readonly privateSockets?: readonly string[];
53
+ /** TCP ports no program may connect to, on any address: the daemon's HTTP port. */
54
+ readonly daemonPorts?: readonly number[];
55
+ }
56
+
57
+ /** The environment variable that turns plugin programs on for Linux. */
58
+ export const LINUX_EXEC_OPT_IN = "TMUX_IDE_PLUGIN_EXEC_LINUX";
59
+
60
+ /** Resolve symlinks (macOS /tmp is /private/tmp), including for paths that do not exist yet. */
61
+ export function realPath(path: string): string {
62
+ if (existsSync(path)) return realpathSync(path);
63
+ const parent = dirname(path);
64
+ return parent === path ? path : join(realPath(parent), basename(path));
65
+ }
66
+
67
+ /** An SBPL regex literal matching every path that starts with `prefix`. */
68
+ export function sbplPrefixRegex(prefix: string): string {
69
+ const escaped = prefix.replace(/[\\^$.*+?()[\]{}|"]/gu, (char) => `\\${char}`);
70
+ return `#"^${escaped}"`;
71
+ }
72
+
73
+ /** `paths` and every directory above them, short of the root, each once. */
74
+ export function immovable(paths: readonly string[]): string[] {
75
+ const out = new Set<string>();
76
+ for (const path of paths) {
77
+ for (let current = path; current !== dirname(current); current = dirname(current))
78
+ out.add(current);
79
+ }
80
+ return [...out];
81
+ }
82
+
83
+ /** Existing paths that start with `prefix` (its directory's matching entries). */
84
+ function existingWithPrefix(prefix: string): string[] {
85
+ const dir = dirname(prefix);
86
+ const name = basename(prefix);
87
+ try {
88
+ return readdirSync(dir)
89
+ .filter((entry) => entry.startsWith(name))
90
+ .map((entry) => join(dir, entry));
91
+ } catch {
92
+ return [];
93
+ }
94
+ }
95
+
96
+ function sbplString(value: string): string {
97
+ return `"${value.replace(/\\/gu, "\\\\").replace(/"/gu, '\\"')}"`;
98
+ }
99
+
100
+ /**
101
+ * The data directory as the sandbox should name it: its parent resolved,
102
+ * its own name kept. If something replaced the directory with a symlink,
103
+ * this still names the link, so the link's target never becomes writable.
104
+ */
105
+ export function writablePath(
106
+ paths: SandboxPaths,
107
+ resolve: (path: string) => string = realPath,
108
+ ): string {
109
+ return join(resolve(dirname(paths.writable)), basename(paths.writable));
110
+ }
111
+
112
+ /** The sandbox-exec profile: allow everything, deny writes under the state home but the data dir. */
113
+ export function darwinProfile(
114
+ paths: SandboxPaths,
115
+ resolve: (path: string) => string = realPath,
116
+ ): string {
117
+ // Later rules win in SBPL, so the narrower allow comes after the deny,
118
+ // and the read denials come last.
119
+ return [
120
+ "(version 1)",
121
+ "(allow default)",
122
+ `(deny file-write* (subpath ${sbplString(resolve(paths.stateHome))}))`,
123
+ `(allow file-write* (subpath ${sbplString(writablePath(paths, resolve))}))`,
124
+ ...(paths.unreadable ?? []).map(
125
+ (path) => `(deny file-read* file-write* (regex ${sbplPrefixRegex(resolve(path))}))`,
126
+ ),
127
+ ...(paths.tmuxSockets ?? []).flatMap((dir) => {
128
+ const inside = sbplPrefixRegex(`${resolve(dir)}/`);
129
+ return [
130
+ `(deny network-outbound (remote unix-socket (path-regex ${inside})))`,
131
+ `(deny file-read* file-write* (regex ${inside}))`,
132
+ ];
133
+ }),
134
+ ...(paths.privateSockets ?? []).map(
135
+ (path) =>
136
+ `(deny network-outbound (remote unix-socket (path-regex ${sbplPrefixRegex(resolve(path))})))`,
137
+ ),
138
+ // A path rule is only as good as the path: the state home, the private
139
+ // files, the socket directories, the control socket and every directory
140
+ // above them cannot be renamed, removed or relinked, so none of them can
141
+ // be moved out from under the rules above. (Only the paths themselves:
142
+ // files inside those directories are governed by the rules above.)
143
+ ...immovable(
144
+ [
145
+ paths.stateHome,
146
+ ...(paths.unreadable ?? []),
147
+ ...(paths.tmuxSockets ?? []),
148
+ ...(paths.privateSockets ?? []),
149
+ ].map(resolve),
150
+ ).map((path) => `(deny file-write* (literal ${sbplString(path)}))`),
151
+ ...(paths.daemonPorts ?? [])
152
+ .filter((port) => Number.isInteger(port) && port > 0 && port < 65536)
153
+ .map((port) => `(deny network-outbound (remote ip "*:${port}"))`),
154
+ ].join("");
155
+ }
156
+
157
+ /**
158
+ * The first executable regular file called `name` in `path`'s directories.
159
+ * Relative entries (".", "bin", empty) are skipped: they would resolve
160
+ * against whatever directory the program starts in.
161
+ */
162
+ export function findOnPath(name: string, path = process.env.PATH ?? ""): string | null {
163
+ for (const dir of path.split(delimiter)) {
164
+ if (!dir || !isAbsolute(dir)) continue;
165
+ const candidate = join(dir, name);
166
+ try {
167
+ if (statSync(candidate).isFile()) {
168
+ accessSync(candidate, constants.X_OK);
169
+ return candidate;
170
+ }
171
+ } catch {
172
+ // Not there, or not executable: keep looking.
173
+ }
174
+ }
175
+ return null;
176
+ }
177
+
178
+ /** Wrap `command` so it runs inside the sandbox, or say why it cannot. */
179
+ export function sandboxedArgv(
180
+ command: readonly string[],
181
+ paths: SandboxPaths,
182
+ options: {
183
+ platform?: NodeJS.Platform;
184
+ find?: (name: string) => string | null;
185
+ exists?: (path: string) => boolean;
186
+ /** Resolves symlinks; the sandbox matches real paths. */
187
+ resolve?: (path: string) => string;
188
+ /** Whether the owner turned plugin programs on for Linux. */
189
+ linuxOptIn?: boolean;
190
+ } = {},
191
+ ): SandboxPlan {
192
+ const platform = options.platform ?? process.platform;
193
+ const find = options.find ?? ((name: string) => findOnPath(name));
194
+ const exists = options.exists ?? existsSync;
195
+ const resolve = options.resolve ?? realPath;
196
+ if (platform === "darwin") {
197
+ const tool = "/usr/bin/sandbox-exec";
198
+ if (!exists(tool)) return { ok: false, reason: "sandbox-exec is missing" };
199
+ return { ok: true, argv: [tool, "-p", darwinProfile(paths, resolve), ...command] };
200
+ }
201
+ if (platform === "linux") {
202
+ const optIn = options.linuxOptIn ?? process.env[LINUX_EXEC_OPT_IN] === "1";
203
+ if (!optIn)
204
+ return {
205
+ ok: false,
206
+ reason: `running programs from plugins on Linux is untested and off; set ${LINUX_EXEC_OPT_IN}=1 for the daemon to turn it on`,
207
+ };
208
+ const tool = find("bwrap");
209
+ if (!tool)
210
+ return {
211
+ ok: false,
212
+ reason: "running programs from plugins needs bubblewrap (bwrap) on Linux",
213
+ };
214
+ const home = resolve(paths.stateHome);
215
+ const writable = writablePath(paths, resolve);
216
+ // bubblewrap binds paths, not patterns: what matches a prefix now is
217
+ // hidden; a file created under that prefix later is not (Linux is off
218
+ // by default and untested).
219
+ const hidden = (paths.unreadable ?? [])
220
+ .map(resolve)
221
+ .flatMap((prefix) =>
222
+ (exists(prefix) ? [prefix] : []).concat(
223
+ existingWithPrefix(prefix).filter((path) => path !== prefix),
224
+ ),
225
+ );
226
+ return {
227
+ ok: true,
228
+ argv: [
229
+ tool,
230
+ "--dev-bind",
231
+ "/",
232
+ "/",
233
+ "--ro-bind",
234
+ home,
235
+ home,
236
+ "--bind",
237
+ writable,
238
+ writable,
239
+ // Hide each unreadable file behind an empty one, each directory
240
+ // behind an empty tmpfs.
241
+ ...hidden.flatMap((path) =>
242
+ isDirectory(path) ? ["--tmpfs", path] : ["--ro-bind", "/dev/null", path],
243
+ ),
244
+ // tmux socket directories become empty: no tmux server to reach.
245
+ ...(paths.tmuxSockets ?? [])
246
+ .map(resolve)
247
+ .filter((dir) => exists(dir) && isDirectory(dir))
248
+ .flatMap((dir) => ["--tmpfs", dir]),
249
+ // Its own PIDs and /proc: the daemon's /proc/<pid> (environ, root,
250
+ // fds) is out of sight. Its own IPC and session (no TIOCSTI into
251
+ // the daemon's terminal), and it dies with the daemon.
252
+ "--unshare-pid",
253
+ "--proc",
254
+ "/proc",
255
+ "--unshare-ipc",
256
+ "--new-session",
257
+ "--die-with-parent",
258
+ "--",
259
+ ...command,
260
+ ],
261
+ };
262
+ }
263
+ return { ok: false, reason: `plugins cannot run programs on ${platform}` };
264
+ }
@@ -0,0 +1,280 @@
1
+ /**
2
+ * `ide.action`: named functions a plugin offers to the command palette, key
3
+ * bindings and `tmux-ide action run`. Runs after SANDBOX_PRELUDE and reaches
4
+ * it only through `__ide_internal`.
5
+ *
6
+ * The plugin keeps its callbacks; the host learns only plain descriptors. A
7
+ * change calls `__ide_actions_changed()`, and when the callback finishes the
8
+ * host reads the list once through `__ide_internal.actions_json()`.
9
+ * Arguments are checked here, in Lua, so `pattern` is a Lua pattern.
10
+ */
11
+ export const MAX_ACTIONS_PER_PLUGIN = 200;
12
+ export const MAX_PARAMS_PER_ACTION = 32;
13
+ /** Local action id length, so `plugin.<name>.<id>` fits the catalog's 160. */
14
+ export const MAX_LOCAL_ACTION_ID = 80;
15
+
16
+ export const ACTIONS_PRELUDE = String.raw`
17
+ local internal = __ide_internal
18
+ local mark_dirty = __ide_actions_changed
19
+ __ide_actions_changed = nil
20
+
21
+ local arm, runners, ide, json_encode = internal.arm, internal.runners, internal.ide, internal.json_encode
22
+ local raw_error, raw_type, raw_pairs, raw_ipairs, raw_tostring =
23
+ error, type, pairs, ipairs, tostring
24
+ local s_match, s_find = string.match, string.find
25
+ local co_running = coroutine.running
26
+ local MAX_ACTIONS = ${MAX_ACTIONS_PER_PLUGIN}
27
+ local MAX_PARAMS = ${MAX_PARAMS_PER_ACTION}
28
+ local MAX_ID = ${MAX_LOCAL_ACTION_ID}
29
+
30
+ local actions = {}
31
+ local order = {}
32
+
33
+ local PARAM_TYPES = { string = true, text = true, number = true, boolean = true, choice = true }
34
+
35
+ local function fail(message) raw_error("E_INVALID: " .. message, 3) end
36
+
37
+ -- Plain text only: no C0/DEL controls, C1 controls, bidi or zero-width
38
+ -- format characters, U+FEFF, soft hyphens or U+2028/U+2029; the catalog
39
+ -- schema refuses the rarer format characters at publish. They
40
+ -- would reach terminals that show this text.
41
+ local function unsafe(text)
42
+ return s_find(text, "[%c\127]") ~= nil
43
+ or s_find(text, "\194[\128-\159]") ~= nil
44
+ or s_find(text, "\216\156") ~= nil
45
+ or s_find(text, "\226\128[\139-\143\168-\174]") ~= nil
46
+ or s_find(text, "\226\129[\160-\164\166-\175]") ~= nil
47
+ or s_find(text, "\239\187\191") ~= nil
48
+ or s_find(text, "\194\173") ~= nil
49
+ end
50
+
51
+ local function check_string(value, what, max, optional)
52
+ if value == nil and optional then return nil end
53
+ if raw_type(value) ~= "string" or #value == 0 or #value > max then
54
+ fail(what .. " must be a string of 1 to " .. max .. " characters")
55
+ end
56
+ if unsafe(value) then fail(what .. " must be plain text: no control characters or newlines") end
57
+ return value
58
+ end
59
+
60
+ local function normalize_choices(list, where)
61
+ if raw_type(list) ~= "table" then fail(where .. " must return a list") end
62
+ local out = {}
63
+ for i, item in raw_ipairs(list) do
64
+ if i > 500 then fail(where .. " has more than 500 choices") end
65
+ if raw_type(item) == "string" then
66
+ out[i] = { value = item, label = item }
67
+ elseif raw_type(item) == "table" and raw_type(item.value) == "string" then
68
+ out[i] = { value = item.value, label = raw_type(item.label) == "string" and item.label or item.value }
69
+ else
70
+ out[i] = false
71
+ end
72
+ if not out[i] or unsafe(out[i].value) or unsafe(out[i].label) or #out[i].label == 0 then
73
+ fail(where .. " entries must be strings or { value, label } tables")
74
+ end
75
+ end
76
+ return out
77
+ end
78
+
79
+ local function normalize_param(param, index)
80
+ if raw_type(param) ~= "table" then fail("params[" .. index .. "] must be a table") end
81
+ local name = param.name
82
+ if raw_type(name) ~= "string" or not s_match(name, "^[a-z_][a-z0-9_]*$") or #name > 64 then
83
+ fail("params[" .. index .. "].name must be lowercase letters, digits and _")
84
+ end
85
+ local kind = param.type or "string"
86
+ if not PARAM_TYPES[kind] then fail("param '" .. name .. "' has unknown type '" .. raw_tostring(kind) .. "'") end
87
+ if param.pattern ~= nil and raw_type(param.pattern) ~= "string" then
88
+ fail("param '" .. name .. "'.pattern must be a Lua pattern string")
89
+ end
90
+ local static_choices, dynamic_choices = nil, false
91
+ if raw_type(param.choices) == "function" then
92
+ dynamic_choices = true
93
+ elseif param.choices ~= nil then
94
+ static_choices = normalize_choices(param.choices, "param '" .. name .. "'.choices")
95
+ end
96
+ if kind == "choice" and not static_choices and not dynamic_choices then
97
+ fail("choice param '" .. name .. "' needs choices")
98
+ end
99
+ local static_default, dynamic_default = nil, false
100
+ if raw_type(param.default) == "function" then
101
+ dynamic_default = true
102
+ elseif param.default ~= nil then
103
+ local t = raw_type(param.default)
104
+ if t ~= "string" and t ~= "number" and t ~= "boolean" then
105
+ fail("param '" .. name .. "'.default must be a string, number, boolean or function")
106
+ end
107
+ if t == "string" and unsafe(param.default) then
108
+ fail("param '" .. name .. "'.default must be plain text: no control characters or newlines")
109
+ end
110
+ static_default = param.default
111
+ end
112
+ return {
113
+ name = name,
114
+ type = kind,
115
+ label = check_string(param.label, "param '" .. name .. "'.label", 120, true) or name,
116
+ description = check_string(param.description, "param '" .. name .. "'.description", 500, true),
117
+ required = param.optional ~= true,
118
+ default = static_default,
119
+ dynamicDefault = dynamic_default,
120
+ choices = static_choices,
121
+ dynamicChoices = dynamic_choices,
122
+ -- Kept for checking only; never published.
123
+ pattern = param.pattern,
124
+ choices_fn = dynamic_choices and param.choices or nil,
125
+ default_fn = dynamic_default and param.default or nil,
126
+ }
127
+ end
128
+
129
+ local function valid_id(id)
130
+ if raw_type(id) ~= "string" or #id == 0 or #id > MAX_ID then return false end
131
+ if not s_match(id, "^[a-z]") or s_find(id, "..", 1, true) or s_match(id, "%.$") then return false end
132
+ return s_match(id, "^[a-z0-9.-]+$") ~= nil
133
+ end
134
+
135
+ -- The host asks for the list once a callback finishes, so registering many
136
+ -- actions in one load costs one encoding, not one per action.
137
+ local function actions_json()
138
+ local list = {}
139
+ for i, id in raw_ipairs(order) do
140
+ local action = actions[id]
141
+ local params = {}
142
+ for j, p in raw_ipairs(action.params) do
143
+ params[j] = {
144
+ name = p.name, type = p.type, label = p.label, description = p.description,
145
+ required = p.required, default = p.default, dynamicDefault = p.dynamicDefault,
146
+ choices = p.choices, dynamicChoices = p.dynamicChoices,
147
+ }
148
+ end
149
+ list[i] = {
150
+ id = id, title = action.title, category = action.category,
151
+ description = action.description, dangerous = action.dangerous, params = params,
152
+ }
153
+ end
154
+ return json_encode(list)
155
+ end
156
+ internal.actions_json = actions_json
157
+
158
+ ide.action = {
159
+ register = function(spec)
160
+ if raw_type(spec) ~= "table" then fail("ide.action.register expects a table") end
161
+ local id = spec.id
162
+ if not valid_id(id) then
163
+ fail("action id must be dot-separated parts of lowercase letters, digits and dashes, like 'tasks.new'")
164
+ end
165
+ if raw_type(spec.run) ~= "function" then fail("action '" .. id .. "' needs a run function") end
166
+ local params = {}
167
+ local seen = {}
168
+ if spec.params ~= nil then
169
+ if raw_type(spec.params) ~= "table" then fail("params must be a list") end
170
+ for i, param in raw_ipairs(spec.params) do
171
+ if i > MAX_PARAMS then fail("an action may have at most " .. MAX_PARAMS .. " params") end
172
+ local p = normalize_param(param, i)
173
+ if seen[p.name] then fail("param '" .. p.name .. "' is declared twice") end
174
+ seen[p.name] = true
175
+ params[i] = p
176
+ end
177
+ end
178
+ -- Validate everything before changing anything.
179
+ local record = {
180
+ title = check_string(spec.title, "action '" .. id .. "'.title", 160),
181
+ category = check_string(spec.category, "action '" .. id .. "'.category", 80, true) or "Plugins",
182
+ description = check_string(spec.description, "action '" .. id .. "'.description", 500, true),
183
+ dangerous = spec.dangerous == true,
184
+ params = params,
185
+ run = spec.run,
186
+ }
187
+ if actions[id] == nil then
188
+ if #order >= MAX_ACTIONS then fail("a plugin may register at most " .. MAX_ACTIONS .. " actions") end
189
+ order[#order + 1] = id
190
+ end
191
+ actions[id] = record
192
+ mark_dirty()
193
+ end,
194
+ unregister = function(id)
195
+ if actions[id] == nil then return false end
196
+ actions[id] = nil
197
+ for i, existing in raw_ipairs(order) do
198
+ if existing == id then table.remove(order, i) break end
199
+ end
200
+ mark_dirty()
201
+ return true
202
+ end,
203
+ }
204
+ internal.features.actions = true
205
+
206
+ local function check_args(action, args)
207
+ if raw_type(args) ~= "table" then args = {} end
208
+ local known = {}
209
+ for _, p in raw_ipairs(action.params) do known[p.name] = p end
210
+ for name in raw_pairs(args) do
211
+ if not known[name] then raw_error("E_INVALID: unknown argument '" .. raw_tostring(name) .. "'", 0) end
212
+ end
213
+ local clean = {}
214
+ for _, p in raw_ipairs(action.params) do
215
+ local value = args[p.name]
216
+ if value == nil then value = p.default end
217
+ if value == nil then
218
+ if p.required then raw_error("E_INVALID: '" .. p.label .. "' is required", 0) end
219
+ else
220
+ local t = raw_type(value)
221
+ if (p.type == "number" and t ~= "number")
222
+ or (p.type == "boolean" and t ~= "boolean")
223
+ or ((p.type == "string" or p.type == "text" or p.type == "choice") and t ~= "string") then
224
+ raw_error("E_INVALID: '" .. p.label .. "' must be a " .. p.type, 0)
225
+ end
226
+ if p.pattern and not s_match(value, p.pattern) then
227
+ raw_error("E_INVALID: '" .. p.label .. "' does not match " .. p.pattern, 0)
228
+ end
229
+ if p.type == "choice" and p.choices then
230
+ local ok = false
231
+ for _, c in raw_ipairs(p.choices) do if c.value == value then ok = true break end end
232
+ if not ok then raw_error("E_INVALID: '" .. p.label .. "' must be one of its choices", 0) end
233
+ end
234
+ clean[p.name] = value
235
+ end
236
+ end
237
+ return clean
238
+ end
239
+
240
+ local function find(id)
241
+ local action = actions[id]
242
+ if not action then raw_error("E_NOT_FOUND: no action '" .. raw_tostring(id) .. "'", 0) end
243
+ return action
244
+ end
245
+
246
+ internal.invoke_action = function(id, args, context)
247
+ runners[co_running()] = true
248
+ arm()
249
+ local action = find(id)
250
+ local clean = check_args(action, args)
251
+ return json_encode({ action.run(context or {}, clean) })
252
+ end
253
+
254
+ internal.param_options = function(id, name, context)
255
+ runners[co_running()] = true
256
+ arm()
257
+ local action = find(id)
258
+ for _, p in raw_ipairs(action.params) do
259
+ if p.name == name then
260
+ local result = { choices = p.choices, default = p.default }
261
+ if p.choices_fn then
262
+ result.choices = normalize_choices(p.choices_fn(context or {}), "choices for '" .. name .. "'")
263
+ end
264
+ if p.default_fn then
265
+ local value = p.default_fn(context or {})
266
+ local t = raw_type(value)
267
+ if value ~= nil and t ~= "string" and t ~= "number" and t ~= "boolean" then
268
+ raw_error("E_INVALID: default for '" .. name .. "' must be a string, number or boolean", 0)
269
+ end
270
+ if t == "string" and unsafe(value) then
271
+ raw_error("E_INVALID: default for '" .. name .. "' must be plain text", 0)
272
+ end
273
+ result.default = value
274
+ end
275
+ return json_encode(result)
276
+ end
277
+ end
278
+ raw_error("E_NOT_FOUND: action '" .. id .. "' has no param '" .. raw_tostring(name) .. "'", 0)
279
+ end
280
+ `;
@@ -0,0 +1,5 @@
1
+ // Standalone entry for the plugin host, used when the daemon runs from source.
2
+ // The published CLI starts the same code with `tmux-ide __plugin-host`.
3
+ import { runPluginHost } from "./main.ts";
4
+
5
+ await runPluginHost();