@termwire/cli 0.4.1 → 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 +82 -0
- package/README.md +79 -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 +131 -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,20 +1,19 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
1
|
import { readFileSync } from "node:fs";
|
|
3
|
-
import { appendFile as appendFileToDisk, mkdir as mkdirDirectory, readdir as readDirectory, readFile as readFileFromDisk, stat, unlink as unlinkFile, } from "node:fs/promises";
|
|
4
|
-
import { homedir as getHomeDirectory } from "node:os";
|
|
5
2
|
import { dirname, resolve as resolvePath } from "node:path";
|
|
6
|
-
import { createNvim } from "@termwire/nvim";
|
|
7
|
-
import { createTmux } from "@termwire/tmux";
|
|
8
3
|
import { Command, CommanderError } from "commander";
|
|
4
|
+
import { agentIds, parseAgentIds } from "./agents.js";
|
|
9
5
|
import { prepareBranch } from "./branch.js";
|
|
10
|
-
import { createConfigLoader } from "./config-loader.js";
|
|
6
|
+
import { configHome, createConfigLoader, globalConfigPath } from "./config-loader.js";
|
|
11
7
|
import { resolveLayout } from "./config-validation.js";
|
|
12
|
-
import {
|
|
8
|
+
import { writeConfigFile } from "./config-writer.js";
|
|
9
|
+
import { install, noLayout } from "./install.js";
|
|
13
10
|
import { createLayout } from "./layout.js";
|
|
14
11
|
import { open } from "./open.js";
|
|
15
12
|
import { createProcessScanner } from "./process-scan.js";
|
|
16
13
|
import { formatReapFailure, formatReapReport, parseTmuxServerPid, reapLogPath, reapSession, } from "./reap.js";
|
|
17
14
|
import { ensureReapHook, reapCommandName } from "./reap-hook.js";
|
|
15
|
+
import { skillSourcePath } from "./skill.js";
|
|
16
|
+
import { layoutTemplateIds } from "./templates.js";
|
|
18
17
|
import { up } from "./up.js";
|
|
19
18
|
import { findGitRoot, prepareWorktree } from "./worktree.js";
|
|
20
19
|
/**
|
|
@@ -24,32 +23,6 @@ import { findGitRoot, prepareWorktree } from "./worktree.js";
|
|
|
24
23
|
* install and from a checkout.
|
|
25
24
|
*/
|
|
26
25
|
const manifest = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
27
|
-
export const executeGit = async (argv, options) => {
|
|
28
|
-
const [command, ...args] = argv;
|
|
29
|
-
if (command === undefined) {
|
|
30
|
-
throw new Error("git execution failed: empty command");
|
|
31
|
-
}
|
|
32
|
-
return await new Promise((resolve, reject) => {
|
|
33
|
-
const child = spawn(command, args, {
|
|
34
|
-
cwd: options?.cwd,
|
|
35
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
36
|
-
});
|
|
37
|
-
let stdout = "";
|
|
38
|
-
let stderr = "";
|
|
39
|
-
child.stdout.setEncoding("utf8").on("data", (chunk) => {
|
|
40
|
-
stdout += chunk;
|
|
41
|
-
});
|
|
42
|
-
child.stderr.setEncoding("utf8").on("data", (chunk) => {
|
|
43
|
-
stderr += chunk;
|
|
44
|
-
});
|
|
45
|
-
child.once("error", (cause) => {
|
|
46
|
-
reject(new Error(`git execution failed: ${argv.join(" ")}`, { cause }));
|
|
47
|
-
});
|
|
48
|
-
child.once("close", (code) => {
|
|
49
|
-
resolve({ exitCode: code ?? 1, stdout, stderr });
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
};
|
|
53
26
|
export async function removeStaleSocket(path, unlink) {
|
|
54
27
|
try {
|
|
55
28
|
await unlink(path);
|
|
@@ -60,46 +33,24 @@ export async function removeStaleSocket(path, unlink) {
|
|
|
60
33
|
}
|
|
61
34
|
}
|
|
62
35
|
}
|
|
63
|
-
export function
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const mkdir = dependencies.mkdir ?? mkdirDirectory;
|
|
71
|
-
const pathExists = dependencies.pathExists ??
|
|
72
|
-
(async (path) => {
|
|
73
|
-
try {
|
|
74
|
-
await stat(path);
|
|
75
|
-
return true;
|
|
76
|
-
}
|
|
77
|
-
catch (error) {
|
|
78
|
-
if (error.code === "ENOENT")
|
|
79
|
-
return false;
|
|
80
|
-
throw error;
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
const unlink = dependencies.unlink ?? unlinkFile;
|
|
84
|
-
const execPath = dependencies.execPath ?? process.execPath;
|
|
85
|
-
const scriptPath = "scriptPath" in dependencies ? dependencies.scriptPath : process.argv[1];
|
|
86
|
-
const warn = dependencies.warn ?? ((message) => process.stderr.write(message));
|
|
87
|
-
const loader = createConfigLoader({ env, homedir, exists: pathExists, readFile });
|
|
88
|
-
const loadGlobalConfig = loader.loadGlobal;
|
|
89
|
-
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
|
+
});
|
|
90
43
|
return (request) => up(request, {
|
|
91
|
-
cwd,
|
|
92
|
-
findGitRoot: (directory) => findGitRoot(
|
|
93
|
-
prepareBranch: ({ cwd: directory, name }) => prepareBranch(
|
|
94
|
-
prepareWorktree: (options) => prepareWorktree({ ...options, exec:
|
|
95
|
-
mkdir:
|
|
96
|
-
|
|
97
|
-
},
|
|
98
|
-
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),
|
|
99
50
|
tmux,
|
|
100
|
-
installReapHook: () => installReapHook({
|
|
101
|
-
loadGlobalConfig,
|
|
102
|
-
loadProjectConfig,
|
|
51
|
+
installReapHook: () => installReapHook({ host, tmux }),
|
|
52
|
+
loadGlobalConfig: loader.loadGlobal,
|
|
53
|
+
loadProjectConfig: loader.loadProject,
|
|
103
54
|
resolveLayout,
|
|
104
55
|
createLayout,
|
|
105
56
|
});
|
|
@@ -108,90 +59,97 @@ export function createRuntimeUp(dependencies = {}) {
|
|
|
108
59
|
* Installing the cleanup hook is best effort: a workspace is still usable when the
|
|
109
60
|
* tmux server refuses the hook, so `up` only warns.
|
|
110
61
|
*/
|
|
111
|
-
async function installReapHook(
|
|
62
|
+
async function installReapHook({ host, tmux }) {
|
|
112
63
|
try {
|
|
113
|
-
if (options.scriptPath === undefined) {
|
|
114
|
-
throw new Error("the termwire script path is unknown");
|
|
115
|
-
}
|
|
116
64
|
await ensureReapHook({
|
|
117
|
-
tmux
|
|
118
|
-
execPath:
|
|
119
|
-
scriptPath:
|
|
65
|
+
tmux,
|
|
66
|
+
execPath: host.execPath,
|
|
67
|
+
scriptPath: resolveScriptPath(host.scriptPath),
|
|
120
68
|
});
|
|
121
69
|
}
|
|
122
70
|
catch (error) {
|
|
123
71
|
const message = error instanceof Error ? error.message : String(error);
|
|
124
|
-
|
|
72
|
+
host.writeError(`termwire: session cleanup hook not installed: ${message}\n`);
|
|
125
73
|
}
|
|
126
74
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return "signalled";
|
|
131
|
-
}
|
|
132
|
-
catch (error) {
|
|
133
|
-
const code = error.code;
|
|
134
|
-
if (code === "ESRCH")
|
|
135
|
-
return "gone";
|
|
136
|
-
if (code === "EPERM")
|
|
137
|
-
return "denied";
|
|
138
|
-
throw error;
|
|
75
|
+
function resolveScriptPath(scriptPath) {
|
|
76
|
+
if (scriptPath === undefined) {
|
|
77
|
+
throw new Error("the termwire script path is unknown");
|
|
139
78
|
}
|
|
79
|
+
return resolvePath(scriptPath);
|
|
140
80
|
}
|
|
141
|
-
export function
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const
|
|
146
|
-
const pid = dependencies.pid ?? process.pid;
|
|
147
|
-
const exec = dependencies.exec ?? spawnCapture;
|
|
148
|
-
const readdir = dependencies.readdir ?? ((path) => readDirectory(path));
|
|
149
|
-
const readFile = dependencies.readFile ?? ((path) => readFileFromDisk(path, "utf8"));
|
|
150
|
-
const kill = dependencies.kill ?? killProcess;
|
|
151
|
-
const wait = dependencies.wait ??
|
|
152
|
-
((milliseconds) => new Promise((resolveWait) => {
|
|
153
|
-
setTimeout(resolveWait, milliseconds);
|
|
154
|
-
}));
|
|
155
|
-
const appendFile = dependencies.appendFile ??
|
|
156
|
-
((path, contents) => appendFileToDisk(path, contents));
|
|
157
|
-
const mkdir = dependencies.mkdir ?? mkdirDirectory;
|
|
158
|
-
const now = dependencies.now ?? (() => new Date());
|
|
159
|
-
const record = async (line) => {
|
|
160
|
-
const path = reapLogPath({ env, homedir });
|
|
161
|
-
try {
|
|
162
|
-
await mkdir(dirname(path), { recursive: true });
|
|
163
|
-
await appendFile(path, line);
|
|
164
|
-
}
|
|
165
|
-
catch {
|
|
166
|
-
// A missing log must not hide the reap itself.
|
|
167
|
-
}
|
|
168
|
-
};
|
|
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);
|
|
169
86
|
return async (session) => {
|
|
170
87
|
try {
|
|
171
|
-
const serverPid = parseTmuxServerPid(env.TMUX);
|
|
88
|
+
const serverPid = parseTmuxServerPid(host.env.TMUX);
|
|
172
89
|
const report = await reapSession(session, {
|
|
173
|
-
scan: createProcessScanner({
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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,
|
|
177
100
|
// The reap inherits the tmux server's environment, TMUX included, which is
|
|
178
101
|
// the only way back to the server it must not kill.
|
|
179
102
|
...(serverPid === undefined ? {} : { serverPid }),
|
|
180
103
|
});
|
|
181
|
-
await record(formatReapReport(report, now().toISOString()));
|
|
104
|
+
await record(formatReapReport(report, host.now().toISOString()));
|
|
182
105
|
}
|
|
183
106
|
catch (error) {
|
|
184
|
-
await record(formatReapFailure(session, error, now().toISOString()));
|
|
107
|
+
await record(formatReapFailure(session, error, host.now().toISOString()));
|
|
185
108
|
throw error;
|
|
186
109
|
}
|
|
187
110
|
};
|
|
188
111
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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
|
+
};
|
|
195
153
|
}
|
|
196
154
|
export function createProgram(dependencies) {
|
|
197
155
|
const program = new Command().name("termwire").configureOutput({
|
|
@@ -246,6 +204,35 @@ Target syntax:
|
|
|
246
204
|
const suffix = result.line === undefined ? "" : ` at line ${result.line}`;
|
|
247
205
|
dependencies.writeOutput(`Opened ${result.path}${suffix}\n`);
|
|
248
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
|
+
});
|
|
249
236
|
// Part of the `up` lifecycle, not a user-facing command: the global tmux
|
|
250
237
|
// `session-closed` hook installed by `up` invokes it with the closed session.
|
|
251
238
|
program
|
|
@@ -256,15 +243,17 @@ Target syntax:
|
|
|
256
243
|
});
|
|
257
244
|
return program;
|
|
258
245
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
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);
|
|
268
257
|
try {
|
|
269
258
|
await program.parseAsync(argv, { from: "user" });
|
|
270
259
|
return 0;
|
|
@@ -274,7 +263,7 @@ export async function run(argv, dependencies = {}) {
|
|
|
274
263
|
return error.exitCode;
|
|
275
264
|
}
|
|
276
265
|
const message = error instanceof Error ? error.message : String(error);
|
|
277
|
-
writeError(`termwire: ${message}\n`);
|
|
266
|
+
commands.writeError(`termwire: ${message}\n`);
|
|
278
267
|
return 1;
|
|
279
268
|
}
|
|
280
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;
|