tmux-ide 2.9.20 → 2.9.22

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 (145) hide show
  1. package/bin/cli.js +16998 -4283
  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 +3 -0
  7. package/packages/contracts/src/pane-reader.ts +121 -0
  8. package/packages/contracts/src/pane-stream.ts +18 -0
  9. package/packages/contracts/src/plain-text.ts +61 -0
  10. package/packages/contracts/src/visual-theme-presets.ts +58 -2
  11. package/packages/daemon/dist/command-center/command-catalog-routes.js +78 -0
  12. package/packages/daemon/dist/command-center/plugin-routes.js +84 -0
  13. package/packages/daemon/dist/command-center/server.js +13 -0
  14. package/packages/daemon/dist/command-center/tmux-server-reader.js +41 -0
  15. package/packages/daemon/dist/command-center/tmux-servers.js +6 -0
  16. package/packages/daemon/dist/doctor.js +106 -43
  17. package/packages/daemon/dist/lib/cli-action-bridge.js +15 -0
  18. package/packages/daemon/dist/lib/cli-command.js +13 -0
  19. package/packages/daemon/dist/lib/daemon-embed.js +39 -1
  20. package/packages/daemon/dist/lib/headless-daemon.js +1 -0
  21. package/packages/daemon/dist/lib/pane-reader-files.js +226 -0
  22. package/packages/daemon/dist/lib/remote-tmux-command.js +8 -5
  23. package/packages/daemon/dist/lib/tmux-client-execution.js +73 -7
  24. package/packages/daemon/dist/lib/tmux-pane-reader.js +105 -0
  25. package/packages/daemon/dist/lib/tmux-server-features.js +38 -0
  26. package/packages/daemon/dist/lib/tmux-server-owner.js +9 -0
  27. package/packages/daemon/dist/lib/worktree.js +18 -0
  28. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +2 -2
  29. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
  30. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
  31. package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +2 -2
  32. package/packages/daemon/dist/open-browser.js +26 -12
  33. package/packages/daemon/dist/open-reader.js +84 -0
  34. package/packages/daemon/dist/plugins/action-catalog.js +129 -0
  35. package/packages/daemon/dist/plugins/action-cli.js +212 -0
  36. package/packages/daemon/dist/plugins/api-table.js +303 -0
  37. package/packages/daemon/dist/plugins/cli.js +556 -0
  38. package/packages/daemon/dist/plugins/consent.js +107 -0
  39. package/packages/daemon/dist/plugins/daemon-plugins.js +124 -0
  40. package/packages/daemon/dist/plugins/discovery.js +142 -0
  41. package/packages/daemon/dist/plugins/examples/worktree-tasks.js +115 -0
  42. package/packages/daemon/dist/plugins/exec-sandbox.js +202 -0
  43. package/packages/daemon/dist/plugins/host/actions-prelude.js +279 -0
  44. package/packages/daemon/dist/plugins/host/entry.js +4 -0
  45. package/packages/daemon/dist/plugins/host/host-api-prelude.js +232 -0
  46. package/packages/daemon/dist/plugins/host/lua-state.js +493 -0
  47. package/packages/daemon/dist/plugins/host/main.js +322 -0
  48. package/packages/daemon/dist/plugins/host/sandbox.js +299 -0
  49. package/packages/daemon/dist/plugins/host-calls.js +279 -0
  50. package/packages/daemon/dist/plugins/manifest.js +69 -0
  51. package/packages/daemon/dist/plugins/plugin-exec.js +261 -0
  52. package/packages/daemon/dist/plugins/plugin-log.js +54 -0
  53. package/packages/daemon/dist/plugins/plugin-store.js +74 -0
  54. package/packages/daemon/dist/plugins/protocol.js +252 -0
  55. package/packages/daemon/dist/plugins/safe-files.js +97 -0
  56. package/packages/daemon/dist/plugins/supervisor.js +790 -0
  57. package/packages/daemon/dist/plugins/worktree-calls.js +154 -0
  58. package/packages/daemon/dist/restore.js +57 -40
  59. package/packages/daemon/dist/terminal/mirror/session-channel.js +4 -1
  60. package/packages/daemon/dist/terminal/pane-stream/pane-stream-websocket.js +12 -3
  61. package/packages/daemon/dist/terminal/protocol/reader-option.js +66 -0
  62. package/packages/daemon/dist/terminal/protocol/session-descriptor-discovery.js +7 -2
  63. package/packages/daemon/dist/tmux-upgrade/cli.js +106 -0
  64. package/packages/daemon/dist/tmux-upgrade/io.js +540 -0
  65. package/packages/daemon/dist/tmux-upgrade/plan.js +238 -0
  66. package/packages/daemon/dist/tmux-upgrade/run.js +199 -0
  67. package/packages/daemon/dist/tui/chrome/snapshot.js +3 -4
  68. package/packages/daemon/dist/tui/mirror/runtime/application-home-agent-navigation.js +64 -4
  69. package/packages/daemon/dist/tui/mirror/runtime/application-home-agents-owner.js +62 -5
  70. package/packages/daemon/dist/tui/mirror/runtime/application-home-experience.js +2 -0
  71. package/packages/daemon/dist/tui/mirror/runtime/application-machine-agent-navigation.js +12 -8
  72. package/packages/daemon/dist/tui/mirror/runtime/application-palette-command-owner.js +30 -2
  73. package/packages/daemon/dist/tui/mirror/runtime/application-plugin-commands.js +152 -0
  74. package/packages/daemon/dist/tui/mirror/runtime/application-plugin-form.js +251 -0
  75. package/packages/daemon/dist/tui/mirror/runtime/application-terminal-interaction-controller.js +125 -111
  76. package/packages/daemon/dist/tui/mirror/workspace/application-command-description.js +14 -0
  77. package/packages/daemon/dist/tui/mirror/workspace/palette-list-layout.js +5 -0
  78. package/packages/daemon/src/command-center/command-catalog-routes.ts +116 -0
  79. package/packages/daemon/src/command-center/plugin-routes.ts +110 -0
  80. package/packages/daemon/src/command-center/server.ts +15 -0
  81. package/packages/daemon/src/command-center/tmux-server-reader.ts +62 -0
  82. package/packages/daemon/src/command-center/tmux-servers.ts +6 -0
  83. package/packages/daemon/src/doctor.ts +118 -45
  84. package/packages/daemon/src/lib/cli-action-bridge.ts +19 -0
  85. package/packages/daemon/src/lib/cli-command.ts +16 -0
  86. package/packages/daemon/src/lib/daemon-embed.ts +59 -0
  87. package/packages/daemon/src/lib/headless-daemon.ts +1 -0
  88. package/packages/daemon/src/lib/pane-reader-files.ts +294 -0
  89. package/packages/daemon/src/lib/remote-tmux-command.ts +13 -6
  90. package/packages/daemon/src/lib/tmux-client-execution.ts +79 -7
  91. package/packages/daemon/src/lib/tmux-pane-reader.ts +144 -0
  92. package/packages/daemon/src/lib/tmux-server-features.ts +43 -0
  93. package/packages/daemon/src/lib/tmux-server-owner.ts +9 -0
  94. package/packages/daemon/src/lib/worktree.ts +16 -0
  95. package/packages/daemon/src/open-browser.ts +59 -18
  96. package/packages/daemon/src/open-reader.ts +148 -0
  97. package/packages/daemon/src/plugins/action-catalog.ts +141 -0
  98. package/packages/daemon/src/plugins/action-cli.ts +248 -0
  99. package/packages/daemon/src/plugins/api-table.ts +346 -0
  100. package/packages/daemon/src/plugins/cli.ts +630 -0
  101. package/packages/daemon/src/plugins/consent.ts +125 -0
  102. package/packages/daemon/src/plugins/daemon-plugins.ts +172 -0
  103. package/packages/daemon/src/plugins/discovery.ts +176 -0
  104. package/packages/daemon/src/plugins/examples/worktree-tasks.ts +120 -0
  105. package/packages/daemon/src/plugins/exec-sandbox.ts +264 -0
  106. package/packages/daemon/src/plugins/host/actions-prelude.ts +280 -0
  107. package/packages/daemon/src/plugins/host/entry.ts +5 -0
  108. package/packages/daemon/src/plugins/host/host-api-prelude.ts +233 -0
  109. package/packages/daemon/src/plugins/host/lua-state.ts +557 -0
  110. package/packages/daemon/src/plugins/host/main.ts +359 -0
  111. package/packages/daemon/src/plugins/host/sandbox.ts +301 -0
  112. package/packages/daemon/src/plugins/host-calls.ts +343 -0
  113. package/packages/daemon/src/plugins/manifest.ts +74 -0
  114. package/packages/daemon/src/plugins/plugin-exec.ts +326 -0
  115. package/packages/daemon/src/plugins/plugin-log.ts +63 -0
  116. package/packages/daemon/src/plugins/plugin-store.ts +80 -0
  117. package/packages/daemon/src/plugins/protocol.ts +275 -0
  118. package/packages/daemon/src/plugins/safe-files.ts +105 -0
  119. package/packages/daemon/src/plugins/supervisor.ts +954 -0
  120. package/packages/daemon/src/plugins/worktree-calls.ts +183 -0
  121. package/packages/daemon/src/restore.ts +80 -44
  122. package/packages/daemon/src/terminal/mirror/events.ts +2 -0
  123. package/packages/daemon/src/terminal/mirror/session-channel.ts +6 -1
  124. package/packages/daemon/src/terminal/pane-stream/pane-stream-websocket.ts +15 -2
  125. package/packages/daemon/src/terminal/protocol/reader-option.ts +77 -0
  126. package/packages/daemon/src/terminal/protocol/session-descriptor-discovery.ts +10 -1
  127. package/packages/daemon/src/tmux-upgrade/cli.ts +118 -0
  128. package/packages/daemon/src/tmux-upgrade/io.ts +596 -0
  129. package/packages/daemon/src/tmux-upgrade/plan.ts +353 -0
  130. package/packages/daemon/src/tmux-upgrade/run.ts +325 -0
  131. package/packages/daemon/src/tui/chrome/snapshot.ts +3 -4
  132. package/packages/daemon/src/tui/mirror/runtime/application-fleet-switcher.tsx +3 -1
  133. package/packages/daemon/src/tui/mirror/runtime/application-home-agent-navigation.ts +110 -10
  134. package/packages/daemon/src/tui/mirror/runtime/application-home-agents-owner.ts +90 -6
  135. package/packages/daemon/src/tui/mirror/runtime/application-home-experience.ts +2 -0
  136. package/packages/daemon/src/tui/mirror/runtime/application-machine-agent-navigation.ts +20 -9
  137. package/packages/daemon/src/tui/mirror/runtime/application-palette-command-owner.ts +42 -2
  138. package/packages/daemon/src/tui/mirror/runtime/application-palette-preview.tsx +7 -1
  139. package/packages/daemon/src/tui/mirror/runtime/application-plugin-commands.ts +229 -0
  140. package/packages/daemon/src/tui/mirror/runtime/application-plugin-form.ts +312 -0
  141. package/packages/daemon/src/tui/mirror/runtime/application-root-v2.tsx +2 -2
  142. package/packages/daemon/src/tui/mirror/runtime/application-terminal-interaction-controller.ts +149 -128
  143. package/packages/daemon/src/tui/mirror/workspace/application-command-description.ts +44 -2
  144. package/packages/daemon/src/tui/mirror/workspace/palette-list-layout.ts +4 -0
  145. package/packages/tmux-bridge/src/runner.ts +3 -1
@@ -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
+ }