pirc-extension 0.3.14 → 0.4.0-beta.2
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.
- {pirc-extension-0.3.14 → pirc-extension-0.4.0-beta.2}/NOTICE +35 -23
- {pirc-extension-0.3.14 → pirc-extension-0.4.0-beta.2}/README.md +21 -0
- pirc-extension-0.4.0-beta.2/join.js +12808 -0
- pirc-extension-0.4.0-beta.2/launcher.js +241 -0
- {pirc-extension-0.3.14 → pirc-extension-0.4.0-beta.2}/package.json +2 -2
- {pirc-extension-0.3.14 → pirc-extension-0.4.0-beta.2}/pi-parts.js +2 -2
- pirc-extension-0.4.0-beta.2/pirc.js +17164 -0
- pirc-extension-0.4.0-beta.2/third_party/libsodium-js/LICENSE +38 -0
- pirc-extension-0.4.0-beta.2/third_party/uqr/LICENSE +22 -0
- pirc-extension-0.3.14/pirc.js +0 -10274
- {pirc-extension-0.3.14 → pirc-extension-0.4.0-beta.2}/LICENSE +0 -0
- {pirc-extension-0.3.14 → pirc-extension-0.4.0-beta.2}/loader.js +0 -0
- {pirc-extension-0.3.14 → pirc-extension-0.4.0-beta.2}/third_party/bufbuild-protobuf/LICENSE +0 -0
- {pirc-extension-0.3.14 → pirc-extension-0.4.0-beta.2}/third_party/bufbuild-protobuf/LICENSE-Apache-2.0 +0 -0
- {pirc-extension-0.3.14 → pirc-extension-0.4.0-beta.2}/third_party/ws/LICENSE +0 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// PiRC extension-launcher 0.4.0-beta.2. Third-party code in this file: @bufbuild/protobuf@2.16.0, libsodium-sumo@0.8.4, libsodium-wrappers-sumo@0.8.4, uqr@0.1.3, ws@8.22.0; see NOTICE.
|
|
2
|
+
import { createRequire as __pircCreateRequire } from "node:module";
|
|
3
|
+
const require = __pircCreateRequire(import.meta.url);
|
|
4
|
+
|
|
5
|
+
// extension/src/config.ts
|
|
6
|
+
import { homedir, hostname } from "node:os";
|
|
7
|
+
import { dirname, join } from "node:path";
|
|
8
|
+
function configDir() {
|
|
9
|
+
return join(process.env.XDG_CONFIG_HOME || join(homedir(), ".config"), "pirc");
|
|
10
|
+
}
|
|
11
|
+
function configPath() {
|
|
12
|
+
return process.env.PIRC_CONFIG || join(configDir(), "host.json");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// extension/src/copies.ts
|
|
16
|
+
import { dirname as dirname2 } from "node:path";
|
|
17
|
+
import { fileURLToPath } from "node:url";
|
|
18
|
+
function packageRoot(moduleURL) {
|
|
19
|
+
return dirname2(fileURLToPath(new URL("./x", moduleURL)));
|
|
20
|
+
}
|
|
21
|
+
function installOf(root) {
|
|
22
|
+
const git = /\/git\/([^/]+\/.+)$/.exec(root);
|
|
23
|
+
if (git) return `git:${git[1]}`;
|
|
24
|
+
const npm = /\/node_modules\/((?:@[^/]+\/)?[^/]+)$/.exec(root);
|
|
25
|
+
if (npm) return `npm:${npm[1]}`;
|
|
26
|
+
return root;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// extension/src/launcher.ts
|
|
30
|
+
import { execFile } from "node:child_process";
|
|
31
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
32
|
+
import { homedir as homedir2 } from "node:os";
|
|
33
|
+
import { dirname as dirname3, join as join2 } from "node:path";
|
|
34
|
+
var LAUNCHER_FLAG = "pirc-launcher";
|
|
35
|
+
var TMUX_SESSION = "pirc";
|
|
36
|
+
var LAUNCHER_WINDOW = "launcher";
|
|
37
|
+
var SUPERVISE_MS = 15e3;
|
|
38
|
+
var RELEASE_CHECK_MS = 6 * 60 * 60 * 1e3;
|
|
39
|
+
var DAY_MS = 24 * 60 * 60 * 1e3;
|
|
40
|
+
var LABEL = "dev.pirc.launcher";
|
|
41
|
+
var UNIT = "pirc-launcher.service";
|
|
42
|
+
var exec = (cmd, args, env = process.env) => new Promise((resolve, reject) => {
|
|
43
|
+
execFile(cmd, args, { env, timeout: 12e4, encoding: "utf8" }, (err, out, stderr) => err ? reject(new Error(`${cmd} ${args[0] ?? ""}: ${(stderr || err.message).trim()}`)) : resolve(out));
|
|
44
|
+
});
|
|
45
|
+
function settingsPath(config) {
|
|
46
|
+
return join2(dirname3(config), "launcher.json");
|
|
47
|
+
}
|
|
48
|
+
function readSettings(config) {
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse(readFileSync(settingsPath(config), "utf8"));
|
|
51
|
+
} catch {
|
|
52
|
+
return void 0;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function writeSettings(config, s) {
|
|
56
|
+
mkdirSync(dirname3(config), { recursive: true, mode: 448 });
|
|
57
|
+
writeFileSync(settingsPath(config), `${JSON.stringify(s, null, 2)}
|
|
58
|
+
`, { mode: 384 });
|
|
59
|
+
}
|
|
60
|
+
function keptEnv(env) {
|
|
61
|
+
const out = {};
|
|
62
|
+
for (const [k, v] of Object.entries(env)) {
|
|
63
|
+
if (v !== void 0 && /^(PI|PIRC)_/.test(k) && !["PIRC_TOKEN", "PIRC_LAUNCH", "PI_CODING_AGENT"].includes(k)) out[k] = v;
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
function launcherArgs(s) {
|
|
68
|
+
return [...s.pi, "--no-session", `--${LAUNCHER_FLAG}`];
|
|
69
|
+
}
|
|
70
|
+
async function ensureLauncher(s, run = exec, env = process.env) {
|
|
71
|
+
const e = { ...env, ...s.env, PATH: s.path };
|
|
72
|
+
delete e.TMUX;
|
|
73
|
+
delete e.TMUX_PANE;
|
|
74
|
+
const home = env.HOME || homedir2();
|
|
75
|
+
const target = `=${TMUX_SESSION}`;
|
|
76
|
+
let windows;
|
|
77
|
+
try {
|
|
78
|
+
windows = (await run("tmux", ["list-windows", "-t", target, "-F", "#{window_name}"], e)).split("\n");
|
|
79
|
+
} catch {
|
|
80
|
+
windows = void 0;
|
|
81
|
+
}
|
|
82
|
+
if (windows?.includes(LAUNCHER_WINDOW)) return "running";
|
|
83
|
+
const command = [...Object.entries(s.env).flatMap(([k, v]) => ["-e", `${k}=${v}`]), "--", ...launcherArgs(s)];
|
|
84
|
+
if (windows === void 0) await run("tmux", ["new-session", "-d", "-s", TMUX_SESSION, "-n", LAUNCHER_WINDOW, "-c", home, ...command], e);
|
|
85
|
+
else await run("tmux", ["new-window", "-d", "-t", `${target}:`, "-n", LAUNCHER_WINDOW, "-c", home, ...command], e);
|
|
86
|
+
return "started";
|
|
87
|
+
}
|
|
88
|
+
function updateDue(s, now = Date.now()) {
|
|
89
|
+
return s.autoUpdate && (s.updatedAt === void 0 || now - s.updatedAt >= DAY_MS);
|
|
90
|
+
}
|
|
91
|
+
function launchdPlist(command, env, log) {
|
|
92
|
+
const x = (v) => v.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
93
|
+
const args = command.map((a) => ` <string>${x(a)}</string>`).join("\n");
|
|
94
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
95
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
96
|
+
<plist version="1.0">
|
|
97
|
+
<dict>
|
|
98
|
+
<key>Label</key>
|
|
99
|
+
<string>${LABEL}</string>
|
|
100
|
+
<key>ProgramArguments</key>
|
|
101
|
+
<array>
|
|
102
|
+
${args}
|
|
103
|
+
</array>
|
|
104
|
+
<key>EnvironmentVariables</key>
|
|
105
|
+
<dict>
|
|
106
|
+
${Object.entries(env).map(([k, v]) => ` <key>${x(k)}</key>
|
|
107
|
+
<string>${x(v)}</string>`).join("\n")}
|
|
108
|
+
</dict>
|
|
109
|
+
<key>RunAtLoad</key>
|
|
110
|
+
<true/>
|
|
111
|
+
<key>KeepAlive</key>
|
|
112
|
+
<true/>
|
|
113
|
+
<key>AbandonProcessGroup</key>
|
|
114
|
+
<true/>
|
|
115
|
+
<key>StandardOutPath</key>
|
|
116
|
+
<string>${x(log)}</string>
|
|
117
|
+
<key>StandardErrorPath</key>
|
|
118
|
+
<string>${x(log)}</string>
|
|
119
|
+
</dict>
|
|
120
|
+
</plist>
|
|
121
|
+
`;
|
|
122
|
+
}
|
|
123
|
+
function systemdUnit(command, env) {
|
|
124
|
+
const q = (v) => `"${v.replaceAll("\\", "\\\\").replaceAll('"', '\\"').replaceAll("%", "%%")}"`;
|
|
125
|
+
return `[Unit]
|
|
126
|
+
Description=PiRC: starts pi agents for clients
|
|
127
|
+
|
|
128
|
+
[Service]
|
|
129
|
+
ExecStart=${command.map(q).join(" ")}
|
|
130
|
+
${Object.entries(env).map(([k, v]) => `Environment=${q(`${k}=${v}`)}`).join("\n")}
|
|
131
|
+
Restart=always
|
|
132
|
+
RestartSec=10
|
|
133
|
+
KillMode=process
|
|
134
|
+
|
|
135
|
+
[Install]
|
|
136
|
+
WantedBy=default.target
|
|
137
|
+
`;
|
|
138
|
+
}
|
|
139
|
+
function serviceFile(platform = process.platform, home = homedir2()) {
|
|
140
|
+
return platform === "darwin" ? join2(home, "Library", "LaunchAgents", `${LABEL}.plist`) : join2(home, ".config", "systemd", "user", UNIT);
|
|
141
|
+
}
|
|
142
|
+
function superviseCommand(root, node = process.execPath) {
|
|
143
|
+
const bundle = join2(root, "launcher.js");
|
|
144
|
+
if (existsSync(bundle)) return [node, bundle, "run"];
|
|
145
|
+
return [node, "--experimental-transform-types", "--no-warnings", join2(root, "launcher-cli.ts"), "run"];
|
|
146
|
+
}
|
|
147
|
+
async function enable(o) {
|
|
148
|
+
const run = o.run ?? exec;
|
|
149
|
+
const platform = o.platform ?? process.platform;
|
|
150
|
+
const home = o.home ?? homedir2();
|
|
151
|
+
writeSettings(o.config, o.settings);
|
|
152
|
+
const command = superviseCommand(o.root);
|
|
153
|
+
const file = serviceFile(platform, home);
|
|
154
|
+
mkdirSync(dirname3(file), { recursive: true });
|
|
155
|
+
const updates = o.settings.autoUpdate ? "PiRC updates itself daily" : "you update it with pi update";
|
|
156
|
+
if (platform === "darwin") {
|
|
157
|
+
const domain = `gui/${o.uid ?? process.getuid?.() ?? 0}`;
|
|
158
|
+
await run("launchctl", ["bootout", `${domain}/${LABEL}`]).catch(() => "");
|
|
159
|
+
writeFileSync(file, launchdPlist(command, { PATH: o.settings.path, ...o.settings.env }, join2(home, "Library", "Logs", "pirc-launcher.log")));
|
|
160
|
+
await run("launchctl", ["bootstrap", domain, file]);
|
|
161
|
+
return `pirc: this computer now starts agents for your devices, in tmux session "${TMUX_SESSION}" (${updates}); /pirc launcher off stops it`;
|
|
162
|
+
}
|
|
163
|
+
writeFileSync(file, systemdUnit(command, { PATH: o.settings.path, ...o.settings.env }));
|
|
164
|
+
await run("systemctl", ["--user", "daemon-reload"]);
|
|
165
|
+
await run("systemctl", ["--user", "enable", UNIT]);
|
|
166
|
+
await run("systemctl", ["--user", "restart", UNIT]);
|
|
167
|
+
return `pirc: this computer now starts agents for your devices, in tmux session "${TMUX_SESSION}" (${updates}); it runs while you are logged in, unless loginctl enable-linger is on; /pirc launcher off stops it`;
|
|
168
|
+
}
|
|
169
|
+
async function disable(o) {
|
|
170
|
+
const run = o.run ?? exec;
|
|
171
|
+
const platform = o.platform ?? process.platform;
|
|
172
|
+
const file = serviceFile(platform, o.home ?? homedir2());
|
|
173
|
+
if (platform === "darwin") await run("launchctl", ["bootout", `gui/${o.uid ?? process.getuid?.() ?? 0}/${LABEL}`]).catch(() => "");
|
|
174
|
+
else {
|
|
175
|
+
await run("systemctl", ["--user", "disable", "--now", UNIT]).catch(() => "");
|
|
176
|
+
rmSync(file, { force: true });
|
|
177
|
+
await run("systemctl", ["--user", "daemon-reload"]).catch(() => "");
|
|
178
|
+
}
|
|
179
|
+
rmSync(file, { force: true });
|
|
180
|
+
rmSync(settingsPath(o.config), { force: true });
|
|
181
|
+
const e = { ...process.env };
|
|
182
|
+
delete e.TMUX;
|
|
183
|
+
delete e.TMUX_PANE;
|
|
184
|
+
await run("tmux", ["kill-window", "-t", `=${TMUX_SESSION}:=${LAUNCHER_WINDOW}`], e).catch(() => "");
|
|
185
|
+
return "pirc: this computer no longer starts agents for your devices; the agents it started go on";
|
|
186
|
+
}
|
|
187
|
+
function enabled(config, platform = process.platform, home = homedir2()) {
|
|
188
|
+
return existsSync(serviceFile(platform, home)) && readSettings(config) !== void 0;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// extension/src/launcher-cli.ts
|
|
192
|
+
var USAGE = "usage: node launcher.js run | enable [--auto-update] | disable | status";
|
|
193
|
+
async function supervise() {
|
|
194
|
+
const config = configPath();
|
|
195
|
+
for (; ; ) {
|
|
196
|
+
const s = readSettings(config);
|
|
197
|
+
if (!s) throw new Error(`not enabled: no ${config.replace(/host\.json$/, "launcher.json")}`);
|
|
198
|
+
try {
|
|
199
|
+
if (updateDue(s)) {
|
|
200
|
+
await exec(s.pi[0], [...s.pi.slice(1), "update", s.source], { ...process.env, PATH: s.path });
|
|
201
|
+
writeSettings(config, { ...s, updatedAt: Date.now() });
|
|
202
|
+
console.log(`updated ${s.source}`);
|
|
203
|
+
}
|
|
204
|
+
} catch (e) {
|
|
205
|
+
writeSettings(config, { ...s, updatedAt: Date.now() });
|
|
206
|
+
console.error(e.message);
|
|
207
|
+
}
|
|
208
|
+
try {
|
|
209
|
+
if (await ensureLauncher(s) === "started") console.log("started the launcher");
|
|
210
|
+
} catch (e) {
|
|
211
|
+
console.error(e.message);
|
|
212
|
+
}
|
|
213
|
+
await new Promise((r) => setTimeout(r, SUPERVISE_MS));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
async function main(args) {
|
|
217
|
+
const [sub, ...rest] = args;
|
|
218
|
+
const config = configPath();
|
|
219
|
+
switch (sub) {
|
|
220
|
+
case "run":
|
|
221
|
+
return void await supervise();
|
|
222
|
+
case "enable": {
|
|
223
|
+
if (rest.some((a) => a !== "--auto-update")) throw new Error(USAGE);
|
|
224
|
+
const root = packageRoot(import.meta.url);
|
|
225
|
+
console.log(await enable({ config, root, settings: { pi: ["pi"], path: process.env.PATH ?? "/usr/bin:/bin", env: keptEnv(process.env), source: installOf(root), autoUpdate: rest.includes("--auto-update") } }));
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
case "disable":
|
|
229
|
+
console.log(await disable({ config }));
|
|
230
|
+
return;
|
|
231
|
+
case "status":
|
|
232
|
+
console.log(enabled(config) ? "on" : "off");
|
|
233
|
+
return;
|
|
234
|
+
default:
|
|
235
|
+
throw new Error(USAGE);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
main(process.argv.slice(2)).catch((e) => {
|
|
239
|
+
console.error(`pirc launcher: ${e.message}`);
|
|
240
|
+
process.exit(1);
|
|
241
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pirc-extension",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "PiRC: remote control for pi
|
|
3
|
+
"version": "0.4.0-beta.2",
|
|
4
|
+
"description": "PiRC: remote control for pi, the coding agent: every session on every machine, on your phone or in a browser",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://pirc.dev",
|
|
7
7
|
"repository": {
|
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
// cannot import of pi, left for it on globalThis (docs/design.md, section
|
|
3
3
|
// 8). pi's loader resolves pi's packages for a file with a static import of
|
|
4
4
|
// one, as here, but not for the bundle, which Node imports. These are pi-tui
|
|
5
|
-
//
|
|
5
|
+
// pi's conversion to PNG and its image resizing, the very modules pi draws with; whole modules,
|
|
6
6
|
// so that a name one lacks fails nothing. Set in the factory, which pi runs
|
|
7
7
|
// at every load, /reload included.
|
|
8
8
|
import * as piCodingAgent from "@earendil-works/pi-coding-agent";
|
|
9
9
|
import * as piTui from "@earendil-works/pi-tui";
|
|
10
10
|
|
|
11
11
|
export default function () {
|
|
12
|
-
globalThis[Symbol.for("pirc.piParts")] = { tui: piTui, convertToPng: piCodingAgent.convertToPng };
|
|
12
|
+
globalThis[Symbol.for("pirc.piParts")] = { tui: piTui, convertToPng: piCodingAgent.convertToPng, resizeImage: piCodingAgent.resizeImage };
|
|
13
13
|
}
|