longtable 0.1.17 → 0.1.19
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/bin/longtable.mjs +176 -165
- package/package.json +1 -1
- package/src/autostart.mjs +141 -115
- package/src/claude.mjs +13 -0
- package/src/codex.mjs +160 -154
- package/src/extras.mjs +48 -10
- package/src/pack.mjs +8 -4
- package/src/pointed.mjs +68 -14
- package/src/repo.mjs +24 -0
- package/src/run.mjs +57 -22
- package/src/signin.mjs +19 -5
- package/src/store.mjs +24 -2
package/bin/longtable.mjs
CHANGED
|
@@ -1,165 +1,176 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// longtable: brings your own Claude Code or Codex to your team's Longtable, quietly, on your own plan.
|
|
3
|
-
//
|
|
4
|
-
// longtable connect <code> [--ai claude|codex] [--app <url>] [--repo <folder or git address>] [--model <name>]
|
|
5
|
-
// [--no-autostart] [--no-run]
|
|
6
|
-
// longtable run
|
|
7
|
-
// longtable status
|
|
8
|
-
// longtable disconnect
|
|
9
|
-
//
|
|
10
|
-
// Nobody types these: Longtable's Connect button hands them to the person's own Claude or Codex, which runs them
|
|
11
|
-
// after the person presses Send. A client's AI gets no copy of the code.
|
|
12
|
-
|
|
13
|
-
import { spawn } from "node:child_process";
|
|
14
|
-
import { rmSync } from "node:fs";
|
|
15
|
-
import { hostname } from "node:os";
|
|
16
|
-
import { join, resolve } from "node:path";
|
|
17
|
-
import { fileURLToPath } from "node:url";
|
|
18
|
-
import { client, fetchConfig } from "../src/api.mjs";
|
|
19
|
-
import { claudeVersion, findClaude, tokenWorks } from "../src/claude.mjs";
|
|
20
|
-
import { codexSignIn, codexSignedIn, codexVersion, findCodex } from "../src/codex.mjs";
|
|
21
|
-
import { disableAutostart, enableAutostart } from "../src/autostart.mjs";
|
|
22
|
-
import { freshCopy, resolveSource } from "../src/repo.mjs";
|
|
23
|
-
import { runLoop, runningConnection } from "../src/run.mjs";
|
|
24
|
-
import { forgetSecret, home, loadSecret, log, readConfig, saveSecret, writeConfig } from "../src/store.mjs";
|
|
25
|
-
|
|
26
|
-
const binPath = fileURLToPath(import.meta.url);
|
|
27
|
-
const [command, ...args] = process.argv.slice(2);
|
|
28
|
-
const option = (name) => {
|
|
29
|
-
const i = args.indexOf(`--${name}`);
|
|
30
|
-
return i >= 0 && args[i + 1] && !args[i + 1].startsWith("--") ? args[i + 1] : undefined;
|
|
31
|
-
};
|
|
32
|
-
const has = (name) => args.includes(`--${name}`);
|
|
33
|
-
const say = (line) => process.stdout.write(`${line}\n`);
|
|
34
|
-
|
|
35
|
-
async function connect() {
|
|
36
|
-
const code = args.find((a, i) => !a.startsWith("--") && !(i > 0 && args[i - 1].startsWith("--") && ["app", "repo", "model", "ai"].includes(args[i - 1].slice(2))));
|
|
37
|
-
if (!code) throw new Error("Missing the connect code. Press Connect in Longtable again.");
|
|
38
|
-
const app = option("app") || "https://longtable.chat";
|
|
39
|
-
const ai = option("ai") || "claude";
|
|
40
|
-
if (!["claude", "codex"].includes(ai)) throw new Error("--ai is claude or codex.");
|
|
41
|
-
// Check the AI is here before the one-use code is spent.
|
|
42
|
-
const claude = ai === "claude" ? findClaude() : null;
|
|
43
|
-
const codex = ai === "codex" ? findCodex() : null;
|
|
44
|
-
if (ai === "claude" && !claude) throw new Error("Claude Code isn't installed on this computer. Install it from claude.com/claude-code, then press Connect again.");
|
|
45
|
-
if (ai === "codex" && !codex) throw new Error("Codex's command-line tool isn't installed on this computer. Install it with npm install -g @openai/codex, then press Connect again.");
|
|
46
|
-
|
|
47
|
-
// Find the code before the one-use code is spent: a repository this computer cannot open stops here, with
|
|
48
|
-
// nothing changed at the table. (A client's AI gets no code; whether this is one is only known after pairing.)
|
|
49
|
-
let source = null;
|
|
50
|
-
const repo = option("repo");
|
|
51
|
-
try {
|
|
52
|
-
source = resolveSource(repo || process.cwd());
|
|
53
|
-
} catch (e) {
|
|
54
|
-
if (repo) throw e;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const cfg = await fetchConfig(app);
|
|
58
|
-
const rpc = client(cfg);
|
|
59
|
-
const paired = await rpc("pair_connection", { code, device_label: hostname() });
|
|
60
|
-
// From here on, anything that goes wrong takes the connection off the table again, so the table never shows an AI
|
|
61
|
-
// as connected that is not running.
|
|
62
|
-
const leave = async (message) => {
|
|
63
|
-
try { await rpc("runner_leave", { token: paired.token, reason: message }); } catch {}
|
|
64
|
-
forgetSecret(`device-${paired.table_id}-${paired.kind}`);
|
|
65
|
-
throw new Error(message);
|
|
66
|
-
};
|
|
67
|
-
if (paired.kind !== ai) await leave(`That connect code is for ${paired.kind === "codex" ? "Codex" : "Claude"}. Press Connect again.`);
|
|
68
|
-
saveSecret(`device-${paired.table_id}-${paired.kind}`, paired.token);
|
|
69
|
-
if (paired.role === "client") source = null;
|
|
70
|
-
else if (!source) await leave("This folder is not the project. Open your AI in the project's own folder, then press Connect again.");
|
|
71
|
-
|
|
72
|
-
const config = readConfig();
|
|
73
|
-
config.app = app;
|
|
74
|
-
config.supabaseUrl = cfg.supabaseUrl;
|
|
75
|
-
config.publishableKey = cfg.publishableKey;
|
|
76
|
-
// One seat per person at a table: connecting a different AI takes the place of the one that was there.
|
|
77
|
-
for (const old of (config.connections || []).filter((c) => c.table_id === paired.table_id && c.kind !== paired.kind)) forgetSecret(`device-${old.table_id}-${old.kind}`);
|
|
78
|
-
config.connections = (config.connections || []).filter((c) => c.table_id !== paired.table_id);
|
|
79
|
-
config.connections.push({
|
|
80
|
-
table_id: paired.table_id,
|
|
81
|
-
kind: paired.kind,
|
|
82
|
-
table_name: paired.table_name,
|
|
83
|
-
member_name: paired.member_name,
|
|
84
|
-
role: paired.role,
|
|
85
|
-
source,
|
|
86
|
-
folder: /^(https?:|git@|ssh:)/.test(repo || "") ? null : resolve(repo || process.cwd()),
|
|
87
|
-
model: option("model") || null,
|
|
88
|
-
});
|
|
89
|
-
writeConfig(config);
|
|
90
|
-
|
|
91
|
-
if (source) {
|
|
92
|
-
try {
|
|
93
|
-
freshCopy(paired.table_id, source);
|
|
94
|
-
} catch (e) {
|
|
95
|
-
writeConfig({ ...config, connections: config.connections.filter((c) => c.table_id !== paired.table_id) });
|
|
96
|
-
await leave(`The connection could not copy the project's code: ${String(e.message).split(
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (ai === "claude") {
|
|
101
|
-
const token = loadSecret("claude-token");
|
|
102
|
-
if (!token || !tokenWorks(claude, token)) {
|
|
103
|
-
say("A page is opening in your browser so this connection can have its own Claude sign-in. Approve it there.");
|
|
104
|
-
// Loaded only here: it needs packages that the copy kept for start-up does not carry.
|
|
105
|
-
const { signIn } = await import("../src/signin.mjs");
|
|
106
|
-
if (!(await signIn(claude))) await leave("The Claude sign-in didn't finish. Press Connect again.");
|
|
107
|
-
}
|
|
108
|
-
} else if (!codexSignedIn(codex)) {
|
|
109
|
-
say("A page is opening in your browser so this connection can have its own ChatGPT sign-in for Codex. Approve it there.");
|
|
110
|
-
if (!(await codexSignIn(codex))) await leave("The ChatGPT sign-in didn't finish. Press Connect again.");
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const auto = has("no-autostart") ? { started: false } : enableAutostart(process.execPath, binPath);
|
|
114
|
-
if (!has("no-run") && !auto.started) {
|
|
115
|
-
spawn(process.execPath, [binPath, "run"], { detached: true, stdio: "ignore", windowsHide: true }).unref();
|
|
116
|
-
}
|
|
117
|
-
const word = ai === "codex" ? "Codex" : "Claude";
|
|
118
|
-
log(`connected to "${paired.table_name}" for ${paired.member_name} (${ai === "codex" ? codexVersion(codex) : claudeVersion(claude)})`);
|
|
119
|
-
say(`Connected. ${paired.member_name}'s ${word} is now at "${paired.table_name}". It answers in the background whenever this computer is on.`);
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// longtable: brings your own Claude Code or Codex to your team's Longtable, quietly, on your own plan.
|
|
3
|
+
//
|
|
4
|
+
// longtable connect <code> [--ai claude|codex] [--app <url>] [--repo <folder or git address>] [--model <name>]
|
|
5
|
+
// [--no-autostart] [--no-run]
|
|
6
|
+
// longtable run
|
|
7
|
+
// longtable status
|
|
8
|
+
// longtable disconnect
|
|
9
|
+
//
|
|
10
|
+
// Nobody types these: Longtable's Connect button hands them to the person's own Claude or Codex, which runs them
|
|
11
|
+
// after the person presses Send. A client's AI gets no copy of the code.
|
|
12
|
+
|
|
13
|
+
import { spawn } from "node:child_process";
|
|
14
|
+
import { rmSync } from "node:fs";
|
|
15
|
+
import { hostname } from "node:os";
|
|
16
|
+
import { join, resolve } from "node:path";
|
|
17
|
+
import { fileURLToPath } from "node:url";
|
|
18
|
+
import { client, fetchConfig } from "../src/api.mjs";
|
|
19
|
+
import { claudeVersion, findClaude, tokenWorks } from "../src/claude.mjs";
|
|
20
|
+
import { codexSignIn, codexSignedIn, codexVersion, findCodex } from "../src/codex.mjs";
|
|
21
|
+
import { disableAutostart, enableAutostart } from "../src/autostart.mjs";
|
|
22
|
+
import { forgetCopies, freshCopy, gitCanJoin, resolveSource } from "../src/repo.mjs";
|
|
23
|
+
import { endConnection, runLoop, runningConnection } from "../src/run.mjs";
|
|
24
|
+
import { forgetSecret, home, loadSecret, log, readConfig, saveSecret, writeConfig } from "../src/store.mjs";
|
|
25
|
+
|
|
26
|
+
const binPath = fileURLToPath(import.meta.url);
|
|
27
|
+
const [command, ...args] = process.argv.slice(2);
|
|
28
|
+
const option = (name) => {
|
|
29
|
+
const i = args.indexOf(`--${name}`);
|
|
30
|
+
return i >= 0 && args[i + 1] && !args[i + 1].startsWith("--") ? args[i + 1] : undefined;
|
|
31
|
+
};
|
|
32
|
+
const has = (name) => args.includes(`--${name}`);
|
|
33
|
+
const say = (line) => process.stdout.write(`${line}\n`);
|
|
34
|
+
|
|
35
|
+
async function connect() {
|
|
36
|
+
const code = args.find((a, i) => !a.startsWith("--") && !(i > 0 && args[i - 1].startsWith("--") && ["app", "repo", "model", "ai"].includes(args[i - 1].slice(2))));
|
|
37
|
+
if (!code) throw new Error("Missing the connect code. Press Connect in Longtable again.");
|
|
38
|
+
const app = option("app") || "https://longtable.chat";
|
|
39
|
+
const ai = option("ai") || "claude";
|
|
40
|
+
if (!["claude", "codex"].includes(ai)) throw new Error("--ai is claude or codex.");
|
|
41
|
+
// Check the AI is here before the one-use code is spent.
|
|
42
|
+
const claude = ai === "claude" ? findClaude() : null;
|
|
43
|
+
const codex = ai === "codex" ? findCodex() : null;
|
|
44
|
+
if (ai === "claude" && !claude) throw new Error("Claude Code isn't installed on this computer. Install it from claude.com/claude-code, then press Connect again.");
|
|
45
|
+
if (ai === "codex" && !codex) throw new Error("Codex's command-line tool isn't installed on this computer. Install it with npm install -g @openai/codex, then press Connect again.");
|
|
46
|
+
|
|
47
|
+
// Find the code before the one-use code is spent: a repository this computer cannot open stops here, with
|
|
48
|
+
// nothing changed at the table. (A client's AI gets no code; whether this is one is only known after pairing.)
|
|
49
|
+
let source = null;
|
|
50
|
+
const repo = option("repo");
|
|
51
|
+
try {
|
|
52
|
+
source = resolveSource(repo || process.cwd());
|
|
53
|
+
} catch (e) {
|
|
54
|
+
if (repo) throw e;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const cfg = await fetchConfig(app);
|
|
58
|
+
const rpc = client(cfg);
|
|
59
|
+
const paired = await rpc("pair_connection", { code, device_label: hostname() });
|
|
60
|
+
// From here on, anything that goes wrong takes the connection off the table again, so the table never shows an AI
|
|
61
|
+
// as connected that is not running.
|
|
62
|
+
const leave = async (message) => {
|
|
63
|
+
try { await rpc("runner_leave", { token: paired.token, reason: message }); } catch {}
|
|
64
|
+
forgetSecret(`device-${paired.table_id}-${paired.kind}`);
|
|
65
|
+
throw new Error(message);
|
|
66
|
+
};
|
|
67
|
+
if (paired.kind !== ai) await leave(`That connect code is for ${paired.kind === "codex" ? "Codex" : "Claude"}. Press Connect again.`);
|
|
68
|
+
saveSecret(`device-${paired.table_id}-${paired.kind}`, paired.token);
|
|
69
|
+
if (paired.role === "client") source = null;
|
|
70
|
+
else if (!source) await leave("This folder is not the project. Open your AI in the project's own folder, then press Connect again.");
|
|
71
|
+
|
|
72
|
+
const config = readConfig();
|
|
73
|
+
config.app = app;
|
|
74
|
+
config.supabaseUrl = cfg.supabaseUrl;
|
|
75
|
+
config.publishableKey = cfg.publishableKey;
|
|
76
|
+
// One seat per person at a table: connecting a different AI takes the place of the one that was there.
|
|
77
|
+
for (const old of (config.connections || []).filter((c) => c.table_id === paired.table_id && c.kind !== paired.kind)) forgetSecret(`device-${old.table_id}-${old.kind}`);
|
|
78
|
+
config.connections = (config.connections || []).filter((c) => c.table_id !== paired.table_id);
|
|
79
|
+
config.connections.push({
|
|
80
|
+
table_id: paired.table_id,
|
|
81
|
+
kind: paired.kind,
|
|
82
|
+
table_name: paired.table_name,
|
|
83
|
+
member_name: paired.member_name,
|
|
84
|
+
role: paired.role,
|
|
85
|
+
source,
|
|
86
|
+
folder: /^(https?:|git@|ssh:)/.test(repo || "") ? null : resolve(repo || process.cwd()),
|
|
87
|
+
model: option("model") || null,
|
|
88
|
+
});
|
|
89
|
+
writeConfig(config);
|
|
90
|
+
|
|
91
|
+
if (source) {
|
|
92
|
+
try {
|
|
93
|
+
freshCopy(paired.table_id, source);
|
|
94
|
+
} catch (e) {
|
|
95
|
+
writeConfig({ ...config, connections: config.connections.filter((c) => c.table_id !== paired.table_id) });
|
|
96
|
+
await leave(`The connection could not copy the project's code: ${String(e.message).split(/\r?\n/).map((l) => l.trim()).filter(Boolean).pop() || "git could not reach it"}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (ai === "claude") {
|
|
101
|
+
const token = loadSecret("claude-token");
|
|
102
|
+
if (!token || !tokenWorks(claude, token)) {
|
|
103
|
+
say("A page is opening in your browser so this connection can have its own Claude sign-in. Approve it there.");
|
|
104
|
+
// Loaded only here: it needs packages that the copy kept for start-up does not carry.
|
|
105
|
+
const { signIn } = await import("../src/signin.mjs");
|
|
106
|
+
if (!(await signIn(claude))) await leave("The Claude sign-in didn't finish. Press Connect again.");
|
|
107
|
+
}
|
|
108
|
+
} else if (!codexSignedIn(codex)) {
|
|
109
|
+
say("A page is opening in your browser so this connection can have its own ChatGPT sign-in for Codex. Approve it there.");
|
|
110
|
+
if (!(await codexSignIn(codex))) await leave("The ChatGPT sign-in didn't finish. Press Connect again.");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const auto = has("no-autostart") ? { started: false } : enableAutostart(process.execPath, binPath);
|
|
114
|
+
if (!has("no-run") && !auto.started) {
|
|
115
|
+
spawn(process.execPath, [binPath, "run"], { detached: true, stdio: "ignore", windowsHide: true }).unref();
|
|
116
|
+
}
|
|
117
|
+
const word = ai === "codex" ? "Codex" : "Claude";
|
|
118
|
+
log(`connected to "${paired.table_name}" for ${paired.member_name} (${ai === "codex" ? codexVersion(codex) : claudeVersion(claude)})`);
|
|
119
|
+
say(`Connected. ${paired.member_name}'s ${word} is now at "${paired.table_name}". It answers in the background whenever this computer is on.`);
|
|
120
|
+
const git = gitCanJoin();
|
|
121
|
+
if (!git.ok) {
|
|
122
|
+
log(`git cannot join two parts of work (${git.version})`);
|
|
123
|
+
say(`One thing to know: this computer's git (${git.version}) is older than 2.38, so when two AIs work on one task, this one's part goes on a branch of its own beside the other. Updating git puts them together.`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function status() {
|
|
128
|
+
const config = readConfig();
|
|
129
|
+
if (!config.connections?.length) return say("Not connected to any table.");
|
|
130
|
+
const rpc = client(config);
|
|
131
|
+
for (const c of config.connections) {
|
|
132
|
+
const token = loadSecret(`device-${c.table_id}-${c.kind}`);
|
|
133
|
+
let state = "missing its device key";
|
|
134
|
+
if (token) {
|
|
135
|
+
try {
|
|
136
|
+
await rpc("runner_hello", { token });
|
|
137
|
+
state = "connected";
|
|
138
|
+
} catch (e) {
|
|
139
|
+
state = e.message;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
say(`${c.member_name}'s ${c.kind === "claude" ? "Claude" : "Codex"} at "${c.table_name}": ${state}${c.source ? `, reading ${c.source}` : ""}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function disconnect() {
|
|
147
|
+
const config = readConfig();
|
|
148
|
+
for (const c of config.connections || []) forgetSecret(`device-${c.table_id}-${c.kind}`);
|
|
149
|
+
writeConfig({ ...config, connections: [] });
|
|
150
|
+
disableAutostart();
|
|
151
|
+
// The connection that is running right now still holds its keys in memory: end it.
|
|
152
|
+
try {
|
|
153
|
+
const pid = runningConnection();
|
|
154
|
+
if (pid && pid !== process.pid) endConnection(pid);
|
|
155
|
+
} catch {}
|
|
156
|
+
rmSync(join(home, "runner.pid"), { force: true });
|
|
157
|
+
// The project copies and work folders it kept go too (his choice, 16 Oct 2026). The Claude sign-in stays: it is
|
|
158
|
+
// the person's own, and another table on this computer may still use it.
|
|
159
|
+
forgetCopies((config.connections || []).map((c) => c.table_id));
|
|
160
|
+
say("Disconnected on this computer, and its copies of the project are removed. Remove it in Longtable's settings too if it still shows.");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
if (command === "connect") await connect();
|
|
165
|
+
else if (command === "run") {
|
|
166
|
+
if (!readConfig().connections?.length) { log("run: not connected to any table, so nothing to do"); process.exit(0); }
|
|
167
|
+
await runLoop();
|
|
168
|
+
}
|
|
169
|
+
else if (command === "status") await status();
|
|
170
|
+
else if (command === "disconnect") disconnect();
|
|
171
|
+
else say("Usage: longtable connect <code> | run | status | disconnect");
|
|
172
|
+
} catch (e) {
|
|
173
|
+
log(`error: ${e.message}`);
|
|
174
|
+
process.stderr.write(`${e.message}\n`);
|
|
175
|
+
process.exit(1);
|
|
176
|
+
}
|
package/package.json
CHANGED
package/src/autostart.mjs
CHANGED
|
@@ -1,115 +1,141 @@
|
|
|
1
|
-
// Start the connection with the computer, with no window. Only ever set up after the person pressed Send on the
|
|
2
|
-
// Connect message that says so, and removed again by `longtable disconnect`.
|
|
3
|
-
//
|
|
4
|
-
// Windows: a per-user task in Task Scheduler, not the Startup folder. Claude Desktop is a packaged app, and files
|
|
5
|
-
// written from inside it into AppData (the Startup folder included) are kept in its private folder, where Windows
|
|
6
|
-
// never looks at sign-in. Task Scheduler is a service outside the app, so the task is real, and it starts the
|
|
7
|
-
// connection outside the app too. The task runs a copy of the connection kept in ~/.longtable/app, since the copy
|
|
8
|
-
// that ran `connect` may itself live somewhere only the app can see.
|
|
9
|
-
|
|
10
|
-
import { spawnSync } from "node:child_process";
|
|
11
|
-
import { cpSync, existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
12
|
-
import { homedir } from "node:os";
|
|
13
|
-
import { dirname, join } from "node:path";
|
|
14
|
-
import { home } from "./store.mjs";
|
|
15
|
-
|
|
16
|
-
export const TASK = "Longtable connection";
|
|
17
|
-
const legacyStartup = () => join(process.env.APPDATA || "", "Microsoft", "Windows", "Start Menu", "Programs", "Startup", "Longtable.vbs");
|
|
18
|
-
const agentFile = () => join(homedir(), "Library", "LaunchAgents", "chat.longtable.connection.plist");
|
|
19
|
-
const unitFile = () => join(homedir(), ".config", "systemd", "user", "longtable.service");
|
|
20
|
-
|
|
21
|
-
function powershell(script, env = {}) {
|
|
22
|
-
const r = spawnSync("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", script], {
|
|
23
|
-
env: { ...process.env, ...env },
|
|
24
|
-
windowsHide: true,
|
|
25
|
-
encoding: "utf8",
|
|
26
|
-
});
|
|
27
|
-
if (r.status !== 0) throw new Error(`Task Scheduler said no: ${(r.stderr || r.stdout || "").trim().slice(0, 300)}`);
|
|
28
|
-
return r.stdout;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Copy the connection (its command and code; it needs nothing installed to run) to a place every program sees.
|
|
32
|
-
export function installCopy(binPath) {
|
|
33
|
-
const pkg = dirname(dirname(binPath));
|
|
34
|
-
const app = join(home, "app");
|
|
35
|
-
rmSync(app, { recursive: true, force: true });
|
|
36
|
-
mkdirSync(app, { recursive: true });
|
|
37
|
-
for (const part of ["bin", "src", "package.json"]) cpSync(join(pkg, part), join(app, part), { recursive: true });
|
|
38
|
-
return join(app, "bin", "longtable.mjs");
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
//
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
1
|
+
// Start the connection with the computer, with no window. Only ever set up after the person pressed Send on the
|
|
2
|
+
// Connect message that says so, and removed again by `longtable disconnect`.
|
|
3
|
+
//
|
|
4
|
+
// Windows: a per-user task in Task Scheduler, not the Startup folder. Claude Desktop is a packaged app, and files
|
|
5
|
+
// written from inside it into AppData (the Startup folder included) are kept in its private folder, where Windows
|
|
6
|
+
// never looks at sign-in. Task Scheduler is a service outside the app, so the task is real, and it starts the
|
|
7
|
+
// connection outside the app too. The task runs a copy of the connection kept in ~/.longtable/app, since the copy
|
|
8
|
+
// that ran `connect` may itself live somewhere only the app can see.
|
|
9
|
+
|
|
10
|
+
import { spawnSync } from "node:child_process";
|
|
11
|
+
import { cpSync, existsSync, mkdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { homedir } from "node:os";
|
|
13
|
+
import { dirname, join } from "node:path";
|
|
14
|
+
import { home } from "./store.mjs";
|
|
15
|
+
|
|
16
|
+
export const TASK = "Longtable connection";
|
|
17
|
+
const legacyStartup = () => join(process.env.APPDATA || "", "Microsoft", "Windows", "Start Menu", "Programs", "Startup", "Longtable.vbs");
|
|
18
|
+
const agentFile = () => join(homedir(), "Library", "LaunchAgents", "chat.longtable.connection.plist");
|
|
19
|
+
const unitFile = () => join(homedir(), ".config", "systemd", "user", "longtable.service");
|
|
20
|
+
|
|
21
|
+
function powershell(script, env = {}) {
|
|
22
|
+
const r = spawnSync("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", script], {
|
|
23
|
+
env: { ...process.env, ...env },
|
|
24
|
+
windowsHide: true,
|
|
25
|
+
encoding: "utf8",
|
|
26
|
+
});
|
|
27
|
+
if (r.status !== 0) throw new Error(`Task Scheduler said no: ${(r.stderr || r.stdout || "").trim().slice(0, 300)}`);
|
|
28
|
+
return r.stdout;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Copy the connection (its command and code; it needs nothing installed to run) to a place every program sees.
|
|
32
|
+
export function installCopy(binPath) {
|
|
33
|
+
const pkg = dirname(dirname(binPath));
|
|
34
|
+
const app = join(home, "app");
|
|
35
|
+
rmSync(app, { recursive: true, force: true });
|
|
36
|
+
mkdirSync(app, { recursive: true });
|
|
37
|
+
for (const part of ["bin", "src", "package.json"]) cpSync(join(pkg, part), join(app, part), { recursive: true });
|
|
38
|
+
return join(app, "bin", "longtable.mjs");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// A path as XML text, and as one double-quoted argument in a systemd line.
|
|
42
|
+
export const xml = (s) => String(s).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
43
|
+
export const unit = (s) => String(s).replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
44
|
+
|
|
45
|
+
// Whether a connection wrote ~/.longtable/runner.pid since `since`, waiting up to `ms` for it.
|
|
46
|
+
export function wroteNoteSince(since, ms) {
|
|
47
|
+
const pause = new Int32Array(new SharedArrayBuffer(4));
|
|
48
|
+
for (const end = Date.now() + ms; Date.now() < end;) {
|
|
49
|
+
try { if (statSync(join(home, "runner.pid")).mtimeMs >= since - 1000) return true; } catch {}
|
|
50
|
+
Atomics.wait(pause, 0, 0, 250);
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// The task runs this script, which starts Node with no window.
|
|
56
|
+
export function writeStarter(nodePath, binPath) {
|
|
57
|
+
const q = (s) => s.replace(/"/g, '""');
|
|
58
|
+
const file = join(home, "start.vbs");
|
|
59
|
+
// Written as UTF-16 with its mark: Windows Script Host reads any other file in the system's old code page, so a
|
|
60
|
+
// profile folder with letters outside it (an Arabic or accented name) made a path that did not exist, and the
|
|
61
|
+
// connection never started.
|
|
62
|
+
writeFileSync(file, `\ufeffCreateObject("Wscript.Shell").Run """${q(nodePath)}"" ""${q(binPath)}"" run", 0, False\r\n`, "utf16le");
|
|
63
|
+
return file;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function registerTask(name, starter) {
|
|
67
|
+
powershell(
|
|
68
|
+
"$a = New-ScheduledTaskAction -Execute 'wscript.exe' -Argument ('\"' + $env:LT_STARTER + '\"'); " +
|
|
69
|
+
"$t = New-ScheduledTaskTrigger -AtLogOn -User ([Security.Principal.WindowsIdentity]::GetCurrent().Name); " +
|
|
70
|
+
"$p = New-ScheduledTaskPrincipal -UserId ([Security.Principal.WindowsIdentity]::GetCurrent().Name) -LogonType Interactive -RunLevel Limited; " +
|
|
71
|
+
"$s = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit ([TimeSpan]::Zero) -StartWhenAvailable; " +
|
|
72
|
+
"Register-ScheduledTask -TaskName $env:LT_TASK -Action $a -Trigger $t -Principal $p -Settings $s -Force | Out-Null",
|
|
73
|
+
{ LT_TASK: name, LT_STARTER: starter },
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function startTask(name) {
|
|
78
|
+
powershell("Start-ScheduledTask -TaskName $env:LT_TASK", { LT_TASK: name });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function removeTask(name) {
|
|
82
|
+
try {
|
|
83
|
+
powershell("Unregister-ScheduledTask -TaskName $env:LT_TASK -Confirm:$false -ErrorAction Stop", { LT_TASK: name });
|
|
84
|
+
} catch {}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function taskExists(name) {
|
|
88
|
+
try {
|
|
89
|
+
return powershell("if (Get-ScheduledTask -TaskName $env:LT_TASK -ErrorAction SilentlyContinue) { 'yes' } else { 'no' }", { LT_TASK: name }).trim() === "yes";
|
|
90
|
+
} catch {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Returns how the connection will start: on Windows the task also starts it now, outside whatever ran `connect`.
|
|
96
|
+
export function enableAutostart(nodePath, binPath) {
|
|
97
|
+
if (process.platform === "win32") {
|
|
98
|
+
const copy = installCopy(binPath);
|
|
99
|
+
registerTask(TASK, writeStarter(nodePath, copy));
|
|
100
|
+
rmSync(legacyStartup(), { force: true });
|
|
101
|
+
const asked = Date.now();
|
|
102
|
+
startTask(TASK);
|
|
103
|
+
// Said started only when the connection really wrote its note; otherwise `connect` runs it itself.
|
|
104
|
+
return { started: wroteNoteSince(asked, 20000), where: `Task Scheduler: ${TASK}` };
|
|
105
|
+
}
|
|
106
|
+
if (process.platform === "darwin") {
|
|
107
|
+
mkdirSync(join(homedir(), "Library", "LaunchAgents"), { recursive: true });
|
|
108
|
+
// Restarted only when it stops in a failure: a connection with nothing to run stops cleanly, and KeepAlive true
|
|
109
|
+
// used to start it again for ever. Paths are written as XML text, so a folder with & or < in its name reads right.
|
|
110
|
+
writeFileSync(agentFile(), `<?xml version="1.0" encoding="UTF-8"?>
|
|
111
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
112
|
+
<plist version="1.0"><dict>
|
|
113
|
+
<key>Label</key><string>chat.longtable.connection</string>
|
|
114
|
+
<key>ProgramArguments</key><array><string>${xml(nodePath)}</string><string>${xml(binPath)}</string><string>run</string></array>
|
|
115
|
+
<key>RunAtLoad</key><true/><key>KeepAlive</key><dict><key>SuccessfulExit</key><false/></dict>
|
|
116
|
+
</dict></plist>
|
|
117
|
+
`);
|
|
118
|
+
// Loaded now, so it runs from the first minute and launchd knows to stop it on disconnect.
|
|
119
|
+
const loaded = spawnSync("launchctl", ["bootstrap", `gui/${process.getuid?.() ?? ""}`, agentFile()], { stdio: "ignore" });
|
|
120
|
+
return { started: loaded.status === 0 && wroteNoteSince(Date.now() - 1000, 15000), where: agentFile() };
|
|
121
|
+
}
|
|
122
|
+
mkdirSync(join(homedir(), ".config", "systemd", "user"), { recursive: true });
|
|
123
|
+
// Each path in quotes, so one with a space in it is one argument; restarted only after a failure.
|
|
124
|
+
writeFileSync(unitFile(), `[Unit]\nDescription=Longtable connection\n\n[Service]\nExecStart="${unit(nodePath)}" "${unit(binPath)}" run\nRestart=on-failure\n\n[Install]\nWantedBy=default.target\n`);
|
|
125
|
+
// Writing the unit is not enough: it has to be switched on to come back after a restart.
|
|
126
|
+
spawnSync("systemctl", ["--user", "daemon-reload"], { stdio: "ignore" });
|
|
127
|
+
const on = spawnSync("systemctl", ["--user", "enable", "--now", "longtable.service"], { stdio: "ignore" });
|
|
128
|
+
return { started: on.status === 0, where: unitFile() };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function disableAutostart() {
|
|
132
|
+
if (process.platform === "win32") {
|
|
133
|
+
removeTask(TASK);
|
|
134
|
+
rmSync(join(home, "start.vbs"), { force: true });
|
|
135
|
+
}
|
|
136
|
+
// The service is switched off before its file goes, or it would keep bringing the connection back: on Linux by
|
|
137
|
+
// systemd, on a Mac by launchd, which went on starting it from memory after the file was deleted.
|
|
138
|
+
if (process.platform === "linux" && existsSync(unitFile())) spawnSync("systemctl", ["--user", "disable", "--now", "longtable.service"], { stdio: "ignore" });
|
|
139
|
+
if (process.platform === "darwin") spawnSync("launchctl", ["bootout", `gui/${process.getuid?.() ?? ""}/chat.longtable.connection`], { stdio: "ignore" });
|
|
140
|
+
for (const f of [legacyStartup(), agentFile(), unitFile()]) if (existsSync(f)) rmSync(f, { force: true });
|
|
141
|
+
}
|