@termwire/cli 0.4.0 → 0.5.0
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/CHANGELOG.md +97 -0
- package/README.md +80 -8
- package/dist/agents.d.ts +24 -0
- package/dist/agents.js +54 -0
- package/dist/bin/termwire.js +2 -2
- package/dist/config-loader.d.ts +9 -0
- package/dist/config-loader.js +6 -3
- package/dist/config-writer.d.ts +17 -0
- package/dist/config-writer.js +46 -0
- package/dist/exec.d.ts +10 -2
- package/dist/exec.js +7 -4
- package/dist/index.d.ts +7 -17
- package/dist/index.js +2 -9
- package/dist/install-prompt.d.ts +10 -0
- package/dist/install-prompt.js +100 -0
- package/dist/install.d.ts +92 -0
- package/dist/install.js +363 -0
- package/dist/layout-preview.d.ts +20 -0
- package/dist/layout-preview.js +38 -0
- package/dist/program.d.ts +13 -50
- package/dist/program.js +140 -142
- package/dist/runtime.d.ts +74 -0
- package/dist/runtime.js +113 -0
- package/dist/skill.d.ts +13 -0
- package/dist/skill.js +17 -0
- package/dist/templates.d.ts +13 -0
- package/dist/templates.js +168 -0
- package/package.json +6 -3
- package/skills/termwire-open/SKILL.md +27 -0
package/dist/program.js
CHANGED
|
@@ -1,47 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { appendFile as appendFileToDisk, mkdir as mkdirDirectory, readdir as readDirectory, readFile as readFileFromDisk, stat, unlink as unlinkFile, } from "node:fs/promises";
|
|
3
|
-
import { homedir as getHomeDirectory } from "node:os";
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
4
2
|
import { dirname, resolve as resolvePath } from "node:path";
|
|
5
|
-
import { createNvim } from "@termwire/nvim";
|
|
6
|
-
import { createTmux } from "@termwire/tmux";
|
|
7
3
|
import { Command, CommanderError } from "commander";
|
|
4
|
+
import { agentIds, parseAgentIds } from "./agents.js";
|
|
8
5
|
import { prepareBranch } from "./branch.js";
|
|
9
|
-
import { createConfigLoader } from "./config-loader.js";
|
|
6
|
+
import { configHome, createConfigLoader, globalConfigPath } from "./config-loader.js";
|
|
10
7
|
import { resolveLayout } from "./config-validation.js";
|
|
11
|
-
import {
|
|
8
|
+
import { writeConfigFile } from "./config-writer.js";
|
|
9
|
+
import { install, noLayout } from "./install.js";
|
|
12
10
|
import { createLayout } from "./layout.js";
|
|
13
11
|
import { open } from "./open.js";
|
|
14
12
|
import { createProcessScanner } from "./process-scan.js";
|
|
15
13
|
import { formatReapFailure, formatReapReport, parseTmuxServerPid, reapLogPath, reapSession, } from "./reap.js";
|
|
16
14
|
import { ensureReapHook, reapCommandName } from "./reap-hook.js";
|
|
15
|
+
import { skillSourcePath } from "./skill.js";
|
|
16
|
+
import { layoutTemplateIds } from "./templates.js";
|
|
17
17
|
import { up } from "./up.js";
|
|
18
18
|
import { findGitRoot, prepareWorktree } from "./worktree.js";
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
cwd: options?.cwd,
|
|
27
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
28
|
-
});
|
|
29
|
-
let stdout = "";
|
|
30
|
-
let stderr = "";
|
|
31
|
-
child.stdout.setEncoding("utf8").on("data", (chunk) => {
|
|
32
|
-
stdout += chunk;
|
|
33
|
-
});
|
|
34
|
-
child.stderr.setEncoding("utf8").on("data", (chunk) => {
|
|
35
|
-
stderr += chunk;
|
|
36
|
-
});
|
|
37
|
-
child.once("error", (cause) => {
|
|
38
|
-
reject(new Error(`git execution failed: ${argv.join(" ")}`, { cause }));
|
|
39
|
-
});
|
|
40
|
-
child.once("close", (code) => {
|
|
41
|
-
resolve({ exitCode: code ?? 1, stdout, stderr });
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
};
|
|
19
|
+
/**
|
|
20
|
+
* Read at runtime rather than baked in at build time, the way `@termwire/mcp`
|
|
21
|
+
* reports its own version. `dist/program.js` and `src/program.ts` both sit one
|
|
22
|
+
* level below the package root, so the same specifier resolves in a published
|
|
23
|
+
* install and from a checkout.
|
|
24
|
+
*/
|
|
25
|
+
const manifest = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
45
26
|
export async function removeStaleSocket(path, unlink) {
|
|
46
27
|
try {
|
|
47
28
|
await unlink(path);
|
|
@@ -52,46 +33,24 @@ export async function removeStaleSocket(path, unlink) {
|
|
|
52
33
|
}
|
|
53
34
|
}
|
|
54
35
|
}
|
|
55
|
-
export function
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const mkdir = dependencies.mkdir ?? mkdirDirectory;
|
|
63
|
-
const pathExists = dependencies.pathExists ??
|
|
64
|
-
(async (path) => {
|
|
65
|
-
try {
|
|
66
|
-
await stat(path);
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
catch (error) {
|
|
70
|
-
if (error.code === "ENOENT")
|
|
71
|
-
return false;
|
|
72
|
-
throw error;
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
const unlink = dependencies.unlink ?? unlinkFile;
|
|
76
|
-
const execPath = dependencies.execPath ?? process.execPath;
|
|
77
|
-
const scriptPath = "scriptPath" in dependencies ? dependencies.scriptPath : process.argv[1];
|
|
78
|
-
const warn = dependencies.warn ?? ((message) => process.stderr.write(message));
|
|
79
|
-
const loader = createConfigLoader({ env, homedir, exists: pathExists, readFile });
|
|
80
|
-
const loadGlobalConfig = loader.loadGlobal;
|
|
81
|
-
const loadProjectConfig = loader.loadProject;
|
|
36
|
+
export function createUp({ fs, host, git, tmux, }) {
|
|
37
|
+
const loader = createConfigLoader({
|
|
38
|
+
env: host.env,
|
|
39
|
+
homedir: host.homedir,
|
|
40
|
+
exists: fs.exists,
|
|
41
|
+
readFile: (path) => fs.readFile(path),
|
|
42
|
+
});
|
|
82
43
|
return (request) => up(request, {
|
|
83
|
-
cwd,
|
|
84
|
-
findGitRoot: (directory) => findGitRoot(
|
|
85
|
-
prepareBranch: ({ cwd: directory, name }) => prepareBranch(
|
|
86
|
-
prepareWorktree: (options) => prepareWorktree({ ...options, exec:
|
|
87
|
-
mkdir:
|
|
88
|
-
|
|
89
|
-
},
|
|
90
|
-
removeFile: (path) => removeStaleSocket(path, unlink),
|
|
44
|
+
cwd: host.cwd,
|
|
45
|
+
findGitRoot: (directory) => findGitRoot(git, directory),
|
|
46
|
+
prepareBranch: ({ cwd: directory, name }) => prepareBranch(git, directory, name),
|
|
47
|
+
prepareWorktree: (options) => prepareWorktree({ ...options, exec: git, pathExists: fs.exists }),
|
|
48
|
+
mkdir: fs.mkdir,
|
|
49
|
+
removeFile: (path) => removeStaleSocket(path, fs.unlink),
|
|
91
50
|
tmux,
|
|
92
|
-
installReapHook: () => installReapHook({
|
|
93
|
-
loadGlobalConfig,
|
|
94
|
-
loadProjectConfig,
|
|
51
|
+
installReapHook: () => installReapHook({ host, tmux }),
|
|
52
|
+
loadGlobalConfig: loader.loadGlobal,
|
|
53
|
+
loadProjectConfig: loader.loadProject,
|
|
95
54
|
resolveLayout,
|
|
96
55
|
createLayout,
|
|
97
56
|
});
|
|
@@ -100,90 +59,97 @@ export function createRuntimeUp(dependencies = {}) {
|
|
|
100
59
|
* Installing the cleanup hook is best effort: a workspace is still usable when the
|
|
101
60
|
* tmux server refuses the hook, so `up` only warns.
|
|
102
61
|
*/
|
|
103
|
-
async function installReapHook(
|
|
62
|
+
async function installReapHook({ host, tmux }) {
|
|
104
63
|
try {
|
|
105
|
-
if (options.scriptPath === undefined) {
|
|
106
|
-
throw new Error("the termwire script path is unknown");
|
|
107
|
-
}
|
|
108
64
|
await ensureReapHook({
|
|
109
|
-
tmux
|
|
110
|
-
execPath:
|
|
111
|
-
scriptPath:
|
|
65
|
+
tmux,
|
|
66
|
+
execPath: host.execPath,
|
|
67
|
+
scriptPath: resolveScriptPath(host.scriptPath),
|
|
112
68
|
});
|
|
113
69
|
}
|
|
114
70
|
catch (error) {
|
|
115
71
|
const message = error instanceof Error ? error.message : String(error);
|
|
116
|
-
|
|
72
|
+
host.writeError(`termwire: session cleanup hook not installed: ${message}\n`);
|
|
117
73
|
}
|
|
118
74
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return "signalled";
|
|
123
|
-
}
|
|
124
|
-
catch (error) {
|
|
125
|
-
const code = error.code;
|
|
126
|
-
if (code === "ESRCH")
|
|
127
|
-
return "gone";
|
|
128
|
-
if (code === "EPERM")
|
|
129
|
-
return "denied";
|
|
130
|
-
throw error;
|
|
75
|
+
function resolveScriptPath(scriptPath) {
|
|
76
|
+
if (scriptPath === undefined) {
|
|
77
|
+
throw new Error("the termwire script path is unknown");
|
|
131
78
|
}
|
|
79
|
+
return resolvePath(scriptPath);
|
|
132
80
|
}
|
|
133
|
-
export function
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
const pid = dependencies.pid ?? process.pid;
|
|
139
|
-
const exec = dependencies.exec ?? spawnCapture;
|
|
140
|
-
const readdir = dependencies.readdir ?? ((path) => readDirectory(path));
|
|
141
|
-
const readFile = dependencies.readFile ?? ((path) => readFileFromDisk(path, "utf8"));
|
|
142
|
-
const kill = dependencies.kill ?? killProcess;
|
|
143
|
-
const wait = dependencies.wait ??
|
|
144
|
-
((milliseconds) => new Promise((resolveWait) => {
|
|
145
|
-
setTimeout(resolveWait, milliseconds);
|
|
146
|
-
}));
|
|
147
|
-
const appendFile = dependencies.appendFile ??
|
|
148
|
-
((path, contents) => appendFileToDisk(path, contents));
|
|
149
|
-
const mkdir = dependencies.mkdir ?? mkdirDirectory;
|
|
150
|
-
const now = dependencies.now ?? (() => new Date());
|
|
151
|
-
const record = async (line) => {
|
|
152
|
-
const path = reapLogPath({ env, homedir });
|
|
153
|
-
try {
|
|
154
|
-
await mkdir(dirname(path), { recursive: true });
|
|
155
|
-
await appendFile(path, line);
|
|
156
|
-
}
|
|
157
|
-
catch {
|
|
158
|
-
// A missing log must not hide the reap itself.
|
|
159
|
-
}
|
|
160
|
-
};
|
|
81
|
+
export function createOpen({ host, nvim, tmux, }) {
|
|
82
|
+
return (request) => open(request, { cwd: host.cwd, env: host.env, nvim, tmux });
|
|
83
|
+
}
|
|
84
|
+
export function createReap({ fs, host, exec }) {
|
|
85
|
+
const record = (line) => appendQuietly(reapLogPath({ env: host.env, homedir: host.homedir }), line, fs);
|
|
161
86
|
return async (session) => {
|
|
162
87
|
try {
|
|
163
|
-
const serverPid = parseTmuxServerPid(env.TMUX);
|
|
88
|
+
const serverPid = parseTmuxServerPid(host.env.TMUX);
|
|
164
89
|
const report = await reapSession(session, {
|
|
165
|
-
scan: createProcessScanner({
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
90
|
+
scan: createProcessScanner({
|
|
91
|
+
platform: host.platform,
|
|
92
|
+
exec,
|
|
93
|
+
uid: host.uid,
|
|
94
|
+
readdir: fs.readdir,
|
|
95
|
+
readFile: fs.readFile,
|
|
96
|
+
}),
|
|
97
|
+
kill: host.kill,
|
|
98
|
+
wait: host.wait,
|
|
99
|
+
selfPid: host.pid,
|
|
169
100
|
// The reap inherits the tmux server's environment, TMUX included, which is
|
|
170
101
|
// the only way back to the server it must not kill.
|
|
171
102
|
...(serverPid === undefined ? {} : { serverPid }),
|
|
172
103
|
});
|
|
173
|
-
await record(formatReapReport(report, now().toISOString()));
|
|
104
|
+
await record(formatReapReport(report, host.now().toISOString()));
|
|
174
105
|
}
|
|
175
106
|
catch (error) {
|
|
176
|
-
await record(formatReapFailure(session, error, now().toISOString()));
|
|
107
|
+
await record(formatReapFailure(session, error, host.now().toISOString()));
|
|
177
108
|
throw error;
|
|
178
109
|
}
|
|
179
110
|
};
|
|
180
111
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
112
|
+
/** A log that cannot be written must not hide what the reap did. */
|
|
113
|
+
async function appendQuietly(path, line, fs) {
|
|
114
|
+
try {
|
|
115
|
+
await fs.mkdir(dirname(path));
|
|
116
|
+
await fs.appendFile(path, line);
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
// Nothing left to report it to.
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
export function createInstall({ fs, host, git, createPrompter, }) {
|
|
123
|
+
return async (request) => {
|
|
124
|
+
const prompt = host.isTerminal && request.yes !== true ? await createPrompter() : undefined;
|
|
125
|
+
return await install(request, {
|
|
126
|
+
cwd: host.cwd,
|
|
127
|
+
findGitRoot: (directory) => findGitRoot(git, directory),
|
|
128
|
+
globalConfigPath: () => globalConfigPath({ env: host.env, homedir: host.homedir }),
|
|
129
|
+
agentPaths: {
|
|
130
|
+
home: host.homedir(),
|
|
131
|
+
configHome: configHome({ env: host.env, homedir: host.homedir }),
|
|
132
|
+
},
|
|
133
|
+
skillSource: skillSourcePath(),
|
|
134
|
+
pathExists: fs.exists,
|
|
135
|
+
readFile: fs.readFile,
|
|
136
|
+
writeConfig: (options) => writeConfigFile(options, { ...fs, suffix: host.randomId }),
|
|
137
|
+
write: host.writeOutput,
|
|
138
|
+
isTerminal: host.isTerminal,
|
|
139
|
+
...(prompt === undefined ? {} : { prompt }),
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/** Binds every command to one runtime. The only caller that needs the real one. */
|
|
144
|
+
export function createCommands(runtime) {
|
|
145
|
+
return {
|
|
146
|
+
up: createUp(runtime),
|
|
147
|
+
open: createOpen(runtime),
|
|
148
|
+
reap: createReap(runtime),
|
|
149
|
+
install: createInstall(runtime),
|
|
150
|
+
writeError: runtime.host.writeError,
|
|
151
|
+
writeOutput: runtime.host.writeOutput,
|
|
152
|
+
};
|
|
187
153
|
}
|
|
188
154
|
export function createProgram(dependencies) {
|
|
189
155
|
const program = new Command().name("termwire").configureOutput({
|
|
@@ -192,6 +158,7 @@ export function createProgram(dependencies) {
|
|
|
192
158
|
});
|
|
193
159
|
program.exitOverride();
|
|
194
160
|
program.showHelpAfterError();
|
|
161
|
+
program.version(manifest.version, "-V, --version", "print the installed termwire version");
|
|
195
162
|
program
|
|
196
163
|
.command("up <name>")
|
|
197
164
|
.description("create or attach to the workspace tmux session <project>-<name>")
|
|
@@ -237,6 +204,35 @@ Target syntax:
|
|
|
237
204
|
const suffix = result.line === undefined ? "" : ` at line ${result.line}`;
|
|
238
205
|
dependencies.writeOutput(`Opened ${result.path}${suffix}\n`);
|
|
239
206
|
});
|
|
207
|
+
program
|
|
208
|
+
.command("install")
|
|
209
|
+
.description("write a layout config and install the file-opening skill for your agents")
|
|
210
|
+
.option("-l, --layout <name>", `layout template: ${[...layoutTemplateIds, noLayout].join(", ")}`)
|
|
211
|
+
.option("-p, --project", "write .termwire.jsonc in this repository instead of the global config")
|
|
212
|
+
.option("-a, --agents <list>", `comma-separated: ${agentIds.join(", ")}, or none`)
|
|
213
|
+
.option("-f, --force", "replace an existing config, or a skill file you own")
|
|
214
|
+
.option("-y, --yes", "never prompt: default layout, and no agents unless --agents")
|
|
215
|
+
.addHelpText("after", `
|
|
216
|
+
Run it before installing anything:
|
|
217
|
+
npx @termwire/cli install
|
|
218
|
+
|
|
219
|
+
Interactive by default, and silent once the flags leave nothing to ask: --yes, or
|
|
220
|
+
--layout together with --agents. The skill teaches an agent to run ${"`termwire open`"}, which costs
|
|
221
|
+
no process, unlike the ${"`@termwire/mcp`"} server it replaces. It only ever adds its own file
|
|
222
|
+
to an agent's skills directory and never edits another tool's configuration.
|
|
223
|
+
`)
|
|
224
|
+
.action(async (options) => {
|
|
225
|
+
const result = await dependencies.install({
|
|
226
|
+
...(options.layout === undefined ? {} : { layout: options.layout }),
|
|
227
|
+
...(options.project === undefined ? {} : { project: true }),
|
|
228
|
+
...(options.agents === undefined ? {} : { agents: parseAgentIds(options.agents) }),
|
|
229
|
+
...(options.force === undefined ? {} : { force: true }),
|
|
230
|
+
...(options.yes === undefined ? {} : { yes: true }),
|
|
231
|
+
});
|
|
232
|
+
if (result.failures.length > 0) {
|
|
233
|
+
throw new Error(`install finished with errors: ${result.failures.join("; ")}`);
|
|
234
|
+
}
|
|
235
|
+
});
|
|
240
236
|
// Part of the `up` lifecycle, not a user-facing command: the global tmux
|
|
241
237
|
// `session-closed` hook installed by `up` invokes it with the closed session.
|
|
242
238
|
program
|
|
@@ -247,15 +243,17 @@ Target syntax:
|
|
|
247
243
|
});
|
|
248
244
|
return program;
|
|
249
245
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
246
|
+
/** The composition root's entry: one runtime in, an exit code out. */
|
|
247
|
+
export async function run(argv, runtime) {
|
|
248
|
+
if (runtime === undefined) {
|
|
249
|
+
// Reachable from JavaScript, where the required parameter is only a suggestion.
|
|
250
|
+
throw new Error("run needs a runtime: pass createNodeRuntime()");
|
|
251
|
+
}
|
|
252
|
+
return await runCommands(argv, createCommands(runtime));
|
|
253
|
+
}
|
|
254
|
+
/** Parsing and exit-code mapping, indifferent to how the commands were built. */
|
|
255
|
+
export async function runCommands(argv, commands) {
|
|
256
|
+
const program = createProgram(commands);
|
|
259
257
|
try {
|
|
260
258
|
await program.parseAsync(argv, { from: "user" });
|
|
261
259
|
return 0;
|
|
@@ -265,7 +263,7 @@ export async function run(argv, dependencies = {}) {
|
|
|
265
263
|
return error.exitCode;
|
|
266
264
|
}
|
|
267
265
|
const message = error instanceof Error ? error.message : String(error);
|
|
268
|
-
writeError(`termwire: ${message}\n`);
|
|
266
|
+
commands.writeError(`termwire: ${message}\n`);
|
|
269
267
|
return 1;
|
|
270
268
|
}
|
|
271
269
|
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { createNvim } from "@termwire/nvim";
|
|
2
|
+
import { createTmux } from "@termwire/tmux";
|
|
3
|
+
import { type Exec } from "./exec.js";
|
|
4
|
+
import type { InstallPrompter } from "./install.js";
|
|
5
|
+
import type { KillOutcome, ReapSignal } from "./reap.js";
|
|
6
|
+
import type { GitExec } from "./worktree.js";
|
|
7
|
+
export interface RuntimeFileSystem {
|
|
8
|
+
readFile: (path: string) => Promise<string>;
|
|
9
|
+
readdir: (path: string) => Promise<string[]>;
|
|
10
|
+
exists: (path: string) => Promise<boolean>;
|
|
11
|
+
mkdir: (path: string) => Promise<void>;
|
|
12
|
+
/** `flag: "wx"` fails on an existing file instead of replacing it. */
|
|
13
|
+
writeFile: (path: string, contents: string, options?: {
|
|
14
|
+
flag: "wx";
|
|
15
|
+
}) => Promise<void>;
|
|
16
|
+
rename: (from: string, to: string) => Promise<void>;
|
|
17
|
+
appendFile: (path: string, contents: string) => Promise<void>;
|
|
18
|
+
unlink: (path: string) => Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
export interface RuntimeHost {
|
|
21
|
+
env: Record<string, string | undefined>;
|
|
22
|
+
cwd: () => string;
|
|
23
|
+
homedir: () => string;
|
|
24
|
+
platform: string;
|
|
25
|
+
uid: number;
|
|
26
|
+
pid: number;
|
|
27
|
+
execPath: string;
|
|
28
|
+
/** The launched script, which the tmux hook has to name by absolute path. */
|
|
29
|
+
scriptPath: string | undefined;
|
|
30
|
+
now: () => Date;
|
|
31
|
+
/** A unique name for a temporary file, so two writers cannot collide. */
|
|
32
|
+
randomId: () => string;
|
|
33
|
+
/** Whether there is a terminal to ask questions on, both ways. */
|
|
34
|
+
isTerminal: boolean;
|
|
35
|
+
kill: (pid: number, signal: ReapSignal) => KillOutcome;
|
|
36
|
+
wait: (milliseconds: number) => Promise<void>;
|
|
37
|
+
writeOutput: (message: string) => void;
|
|
38
|
+
writeError: (message: string) => void;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Everything the CLI needs from the outside world. Commands receive it whole and
|
|
42
|
+
* never reach for `node:*` or `process` themselves, so a test builds a runtime of
|
|
43
|
+
* fakes instead of overriding a dozen separate default arguments.
|
|
44
|
+
*
|
|
45
|
+
* The adapters carry their own copy of the environment, so a composed runtime should
|
|
46
|
+
* build them from the same `host.env` it passes here — `createAdapters` does that.
|
|
47
|
+
* Otherwise `attach` reads one environment to decide between `attach-session` and
|
|
48
|
+
* `switch-client` while the reap reads another to find the tmux server.
|
|
49
|
+
*/
|
|
50
|
+
export interface CliRuntime {
|
|
51
|
+
fs: RuntimeFileSystem;
|
|
52
|
+
host: RuntimeHost;
|
|
53
|
+
/** Used for anything without an adapter, currently the process scanner. */
|
|
54
|
+
exec: Exec;
|
|
55
|
+
git: GitExec;
|
|
56
|
+
tmux: ReturnType<typeof createTmux>;
|
|
57
|
+
nvim: ReturnType<typeof createNvim>;
|
|
58
|
+
/** Built on demand, so a command that never asks anything loads no terminal UI. */
|
|
59
|
+
createPrompter: () => Promise<InstallPrompter>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Binds both adapters to one exec and one environment. The exec has to forward
|
|
63
|
+
* `ExecOptions`: `attach` asks for an inherited terminal, and tmux refuses to attach
|
|
64
|
+
* without one.
|
|
65
|
+
*/
|
|
66
|
+
export declare function createAdapters({ exec, env, }: {
|
|
67
|
+
exec: Exec;
|
|
68
|
+
env: Record<string, string | undefined>;
|
|
69
|
+
}): Pick<CliRuntime, "tmux" | "nvim">;
|
|
70
|
+
/** The one place where the real world is bound. Nothing else imports `node:*`. */
|
|
71
|
+
export declare function createNodeRuntime(): CliRuntime;
|
|
72
|
+
export declare function fileExists(path: string): Promise<boolean>;
|
|
73
|
+
export declare function killProcess(pid: number, signal: ReapSignal): KillOutcome;
|
|
74
|
+
export declare const executeGit: GitExec;
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { appendFile as appendFileToDisk, mkdir as mkdirDirectory, readdir as readDirectory, readFile as readFileFromDisk, rename as renameFile, stat, unlink as unlinkFile, writeFile as writeFileToDisk, } from "node:fs/promises";
|
|
4
|
+
import { homedir as getHomeDirectory } from "node:os";
|
|
5
|
+
import { createNvim } from "@termwire/nvim";
|
|
6
|
+
import { createTmux } from "@termwire/tmux";
|
|
7
|
+
import { spawnCapture } from "./exec.js";
|
|
8
|
+
/**
|
|
9
|
+
* Binds both adapters to one exec and one environment. The exec has to forward
|
|
10
|
+
* `ExecOptions`: `attach` asks for an inherited terminal, and tmux refuses to attach
|
|
11
|
+
* without one.
|
|
12
|
+
*/
|
|
13
|
+
export function createAdapters({ exec, env, }) {
|
|
14
|
+
return { tmux: createTmux({ exec, env }), nvim: createNvim({ exec }) };
|
|
15
|
+
}
|
|
16
|
+
/** The one place where the real world is bound. Nothing else imports `node:*`. */
|
|
17
|
+
export function createNodeRuntime() {
|
|
18
|
+
const env = process.env;
|
|
19
|
+
return {
|
|
20
|
+
fs: {
|
|
21
|
+
readFile: (path) => readFileFromDisk(path, "utf8"),
|
|
22
|
+
readdir: readDirectory,
|
|
23
|
+
exists: fileExists,
|
|
24
|
+
mkdir: async (path) => {
|
|
25
|
+
await mkdirDirectory(path, { recursive: true });
|
|
26
|
+
},
|
|
27
|
+
writeFile: (path, contents, options) => writeFileToDisk(path, contents, options ? { flag: options.flag } : undefined),
|
|
28
|
+
rename: renameFile,
|
|
29
|
+
appendFile: appendFileToDisk,
|
|
30
|
+
unlink: unlinkFile,
|
|
31
|
+
},
|
|
32
|
+
host: {
|
|
33
|
+
env,
|
|
34
|
+
cwd: () => process.cwd(),
|
|
35
|
+
homedir: getHomeDirectory,
|
|
36
|
+
platform: process.platform,
|
|
37
|
+
uid: process.getuid?.() ?? 0,
|
|
38
|
+
pid: process.pid,
|
|
39
|
+
execPath: process.execPath,
|
|
40
|
+
scriptPath: process.argv[1],
|
|
41
|
+
now: () => new Date(),
|
|
42
|
+
randomId: randomUUID,
|
|
43
|
+
isTerminal: process.stdin.isTTY === true && process.stdout.isTTY === true,
|
|
44
|
+
kill: killProcess,
|
|
45
|
+
wait: waitFor,
|
|
46
|
+
writeOutput: (message) => process.stdout.write(message),
|
|
47
|
+
writeError: (message) => process.stderr.write(message),
|
|
48
|
+
},
|
|
49
|
+
exec: spawnCapture,
|
|
50
|
+
git: executeGit,
|
|
51
|
+
...createAdapters({ exec: spawnCapture, env }),
|
|
52
|
+
// Imported here and not at the top, so `up` and `open` never load the prompts.
|
|
53
|
+
createPrompter: async () => (await import("./install-prompt.js")).createClackPrompter({
|
|
54
|
+
columns: process.stdout.columns ?? 80,
|
|
55
|
+
}),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export async function fileExists(path) {
|
|
59
|
+
try {
|
|
60
|
+
await stat(path);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
if (error.code === "ENOENT")
|
|
65
|
+
return false;
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export function killProcess(pid, signal) {
|
|
70
|
+
try {
|
|
71
|
+
process.kill(pid, signal);
|
|
72
|
+
return "signalled";
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
const code = error.code;
|
|
76
|
+
if (code === "ESRCH")
|
|
77
|
+
return "gone";
|
|
78
|
+
if (code === "EPERM")
|
|
79
|
+
return "denied";
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function waitFor(milliseconds) {
|
|
84
|
+
return new Promise((resolve) => {
|
|
85
|
+
setTimeout(resolve, milliseconds);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
export const executeGit = async (argv, options) => {
|
|
89
|
+
const [command, ...args] = argv;
|
|
90
|
+
if (command === undefined) {
|
|
91
|
+
throw new Error("git execution failed: empty command");
|
|
92
|
+
}
|
|
93
|
+
return await new Promise((resolve, reject) => {
|
|
94
|
+
const child = spawn(command, args, {
|
|
95
|
+
cwd: options?.cwd,
|
|
96
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
97
|
+
});
|
|
98
|
+
let stdout = "";
|
|
99
|
+
let stderr = "";
|
|
100
|
+
child.stdout.setEncoding("utf8").on("data", (chunk) => {
|
|
101
|
+
stdout += chunk;
|
|
102
|
+
});
|
|
103
|
+
child.stderr.setEncoding("utf8").on("data", (chunk) => {
|
|
104
|
+
stderr += chunk;
|
|
105
|
+
});
|
|
106
|
+
child.once("error", (cause) => {
|
|
107
|
+
reject(new Error(`git execution failed: ${argv.join(" ")}`, { cause }));
|
|
108
|
+
});
|
|
109
|
+
child.once("close", (code) => {
|
|
110
|
+
resolve({ exitCode: code ?? 1, stdout, stderr });
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
};
|
package/dist/skill.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Marks a skill file as ours to replace. Without it we cannot tell our own older text
|
|
3
|
+
* from something the user rewrote, and an upgrade would quietly eat their edits. Deleting
|
|
4
|
+
* the line is how a user says "leave this file alone".
|
|
5
|
+
*/
|
|
6
|
+
export declare const skillMarker = "<!-- managed by `termwire install`; delete this line to keep your own version -->";
|
|
7
|
+
/**
|
|
8
|
+
* The skill lives as Markdown in the package rather than as a string in the source: it is
|
|
9
|
+
* editable, reviewable in a diff, and readable by anyone who wants to know what their agent
|
|
10
|
+
* was told. `dist/skill.js` and `src/skill.ts` both sit one level below the package root, so
|
|
11
|
+
* the same specifier resolves in a published install and from a checkout.
|
|
12
|
+
*/
|
|
13
|
+
export declare function skillSourcePath(): string;
|
package/dist/skill.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { skillName } from "./agents.js";
|
|
3
|
+
/**
|
|
4
|
+
* Marks a skill file as ours to replace. Without it we cannot tell our own older text
|
|
5
|
+
* from something the user rewrote, and an upgrade would quietly eat their edits. Deleting
|
|
6
|
+
* the line is how a user says "leave this file alone".
|
|
7
|
+
*/
|
|
8
|
+
export const skillMarker = "<!-- managed by `termwire install`; delete this line to keep your own version -->";
|
|
9
|
+
/**
|
|
10
|
+
* The skill lives as Markdown in the package rather than as a string in the source: it is
|
|
11
|
+
* editable, reviewable in a diff, and readable by anyone who wants to know what their agent
|
|
12
|
+
* was told. `dist/skill.js` and `src/skill.ts` both sit one level below the package root, so
|
|
13
|
+
* the same specifier resolves in a published install and from a checkout.
|
|
14
|
+
*/
|
|
15
|
+
export function skillSourcePath() {
|
|
16
|
+
return fileURLToPath(new URL(`../skills/${skillName}/SKILL.md`, import.meta.url));
|
|
17
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const layoutTemplateIds: readonly ["default", "three-window", "focused", "full-stack"];
|
|
2
|
+
export type LayoutTemplateId = (typeof layoutTemplateIds)[number];
|
|
3
|
+
export interface LayoutTemplate {
|
|
4
|
+
id: LayoutTemplateId;
|
|
5
|
+
title: string;
|
|
6
|
+
hint: string;
|
|
7
|
+
/** Box drawing of the windows and panes, one line per row, all the same width. */
|
|
8
|
+
preview: readonly string[];
|
|
9
|
+
contents: string;
|
|
10
|
+
}
|
|
11
|
+
/** Copies, so a caller can never mutate the registry other callers read. */
|
|
12
|
+
export declare function layoutTemplates(): LayoutTemplate[];
|
|
13
|
+
export declare function findLayoutTemplate(id: string): LayoutTemplate | undefined;
|