tencil 0.7.0 → 0.8.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/README.md +12 -1
- package/dist/cli.mjs +19 -467
- package/dist/{cloud-jOobzTY9.d.mts → cloud-DPNrsIER.d.mts} +1 -1
- package/dist/{index-BRcZL2Ov.d.mts → index-C4SmNUQW.d.mts} +22 -1
- package/dist/index.d.mts +53 -5
- package/dist/index.mjs +1 -1
- package/dist/{loader-BevujQ44.mjs → loader-th3V-C7L.mjs} +7 -2
- package/dist/next.d.mts +6 -3
- package/dist/next.mjs +17 -3
- package/dist/overlay.global.js +44 -23
- package/dist/react.d.mts +1 -1
- package/dist/react.mjs +2 -2
- package/dist/{server-1L4Fi2wN.mjs → server-Cbvie3Om.mjs} +882 -13
- package/dist/source-attributes-Cs2NvuxG.mjs +151 -0
- package/dist/vite.d.mts +1 -1
- package/dist/vite.mjs +3 -151
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -90,7 +90,18 @@ up, and registers it with launchd on macOS, systemd on Linux or Task Scheduler o
|
|
|
90
90
|
Windows. Answer no and run `npx tencil start` after a restart instead.
|
|
91
91
|
|
|
92
92
|
`npx tencil stop` stops it until you next log in; `npx tencil service uninstall`
|
|
93
|
-
removes it.
|
|
93
|
+
removes it.
|
|
94
|
+
|
|
95
|
+
### Updates
|
|
96
|
+
|
|
97
|
+
The daemon looks for a new Tencil when it starts and about once a day. When one
|
|
98
|
+
is out, the overlay's settings show it, with an **Update** button: the daemon
|
|
99
|
+
installs the new version into its runtime copy, waits until no thread is running,
|
|
100
|
+
and restarts on it. If the new version doesn't start, the old one keeps running.
|
|
101
|
+
`npx tencil@latest setup` updates by hand.
|
|
102
|
+
|
|
103
|
+
Set `TENCIL_NO_UPDATE_CHECK=1` to stop the daemon and the CLI asking the npm
|
|
104
|
+
registry; they don't on CI either.
|
|
94
105
|
|
|
95
106
|
### Pairing
|
|
96
107
|
|
package/dist/cli.mjs
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { _ as
|
|
2
|
+
import { A as withoutNpmEnv, D as createLocalPairingCode, E as DEFAULT_T3_URL, O as exchangePairingCredential, S as defaultDataDir, T as CredentialStore, _ as detectAgents, a as readPlan, b as savedProvider, c as runtimeDir, d as uninstallService, f as startInBackground, g as PROVIDERS, h as updateNotice, i as finishUpdate, j as loadAgentSdk, k as parsePairingInput, l as isServiceInstalled, m as isNewerThanThis, n as probeDaemon, o as enableAutostart, p as stopDaemon, r as startDaemon, s as ensureDaemon, t as DEFAULT_PORT, u as stopService, v as providerLabel, w as VERSION, y as saveProvider } from "./server-Cbvie3Om.mjs";
|
|
3
3
|
import { t as DEFAULT_CLOUD_URL } from "./cloud-CiwIrP1h.mjs";
|
|
4
|
-
import {
|
|
5
|
-
import { existsSync,
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
5
|
+
import { existsSync, rmSync } from "node:fs";
|
|
6
6
|
import { readFile, readdir, writeFile } from "node:fs/promises";
|
|
7
7
|
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
8
8
|
import { parseArgs } from "node:util";
|
|
9
9
|
import { fileURLToPath } from "node:url";
|
|
10
|
-
import { homedir } from "node:os";
|
|
11
10
|
import pc from "picocolors";
|
|
12
11
|
import { createInterface } from "node:readline/promises";
|
|
13
12
|
//#region src/cli/format.ts
|
|
@@ -28,465 +27,6 @@ async function printUpdateNotice(notice, log) {
|
|
|
28
27
|
if (text) log(` ${pc.cyan("↑")} ${text}\n`);
|
|
29
28
|
}
|
|
30
29
|
//#endregion
|
|
31
|
-
//#region src/service/daemon-command.ts
|
|
32
|
-
function daemonArgs(command) {
|
|
33
|
-
return [
|
|
34
|
-
command.cli,
|
|
35
|
-
"daemon",
|
|
36
|
-
"--port",
|
|
37
|
-
String(command.port),
|
|
38
|
-
...command.args ?? []
|
|
39
|
-
];
|
|
40
|
-
}
|
|
41
|
-
//#endregion
|
|
42
|
-
//#region src/service/versions.ts
|
|
43
|
-
/** Compare dotted versions by number (pre-release tags ignored): below 0, 0 or above 0. */
|
|
44
|
-
function compareVersions(a, b) {
|
|
45
|
-
const parts = (version) => version.split("-")[0]?.split(".").map(Number) ?? [];
|
|
46
|
-
const [left, right] = [parts(a), parts(b)];
|
|
47
|
-
for (let i = 0; i < Math.max(left.length, right.length); i++) {
|
|
48
|
-
const difference = (left[i] ?? 0) - (right[i] ?? 0);
|
|
49
|
-
if (difference) return difference;
|
|
50
|
-
}
|
|
51
|
-
return 0;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* npx reuses what it cached for `npx tencil`, so an older Tencil can run after
|
|
55
|
-
* a newer one was set up. It must never replace that newer daemon.
|
|
56
|
-
*/
|
|
57
|
-
function isNewerThanThis(version) {
|
|
58
|
-
return version !== void 0 && compareVersions(version, VERSION) > 0;
|
|
59
|
-
}
|
|
60
|
-
var NewerDaemonError = class extends Error {
|
|
61
|
-
constructor(running) {
|
|
62
|
-
super(`Tencil ${running} is already running and this is ${VERSION}, which is older. Run \`npx tencil@latest\` instead.`);
|
|
63
|
-
this.name = "NewerDaemonError";
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
/** The newest published Tencil, or undefined when the registry doesn't answer quickly. */
|
|
67
|
-
async function latestVersion() {
|
|
68
|
-
const response = await fetch("https://registry.npmjs.org/tencil/latest", { signal: AbortSignal.timeout(1500) }).catch(() => void 0);
|
|
69
|
-
if (!response?.ok) return void 0;
|
|
70
|
-
return (await response.json().catch(() => void 0))?.version;
|
|
71
|
-
}
|
|
72
|
-
/** A one-line nudge when a newer Tencil is published. */
|
|
73
|
-
async function updateNotice() {
|
|
74
|
-
const latest = await latestVersion();
|
|
75
|
-
return latest && isNewerThanThis(latest) ? `Tencil ${latest} is out (this is ${VERSION}): npx tencil@latest` : void 0;
|
|
76
|
-
}
|
|
77
|
-
//#endregion
|
|
78
|
-
//#region src/service/background.ts
|
|
79
|
-
/**
|
|
80
|
-
* Start the daemon in the background unless it already runs. A daemon from an
|
|
81
|
-
* older Tencil version is replaced so pages always get the current overlay;
|
|
82
|
-
* so is one running with other flags than the ones given. Without flags of
|
|
83
|
-
* its own, a replacement keeps the running daemon's (`--listen`, `--t3-url`, …).
|
|
84
|
-
*/
|
|
85
|
-
async function startInBackground(command, dataDir) {
|
|
86
|
-
const running = await probeDaemon(command.port);
|
|
87
|
-
if (running && isNewerThanThis(running.version)) return {
|
|
88
|
-
status: "running",
|
|
89
|
-
pid: running.pid,
|
|
90
|
-
newer: running.version
|
|
91
|
-
};
|
|
92
|
-
const flags = command.args?.length ? command.args : running?.flags ?? [];
|
|
93
|
-
if (running && running.version === VERSION && sameFlags(running.flags, flags)) return {
|
|
94
|
-
status: "running",
|
|
95
|
-
pid: running.pid
|
|
96
|
-
};
|
|
97
|
-
if (running) await stopDaemon(command.port);
|
|
98
|
-
mkdirSync(dataDir, { recursive: true });
|
|
99
|
-
const logFile = openSync(join(dataDir, "daemon.log"), "a");
|
|
100
|
-
spawn(command.node, daemonArgs({
|
|
101
|
-
...command,
|
|
102
|
-
args: flags
|
|
103
|
-
}), {
|
|
104
|
-
detached: true,
|
|
105
|
-
stdio: [
|
|
106
|
-
"ignore",
|
|
107
|
-
logFile,
|
|
108
|
-
logFile
|
|
109
|
-
],
|
|
110
|
-
windowsHide: true
|
|
111
|
-
}).unref();
|
|
112
|
-
const health = await waitForDaemon(command.port, dataDir);
|
|
113
|
-
return {
|
|
114
|
-
status: running ? "restarted" : "started",
|
|
115
|
-
pid: health.pid
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
/** Wait for a daemon on `port` (optionally one of `version`) to answer. */
|
|
119
|
-
async function waitForDaemon(port, dataDir, version) {
|
|
120
|
-
for (let attempt = 0; attempt < 100; attempt++) {
|
|
121
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
122
|
-
const health = await probeDaemon(port);
|
|
123
|
-
if (health && (!version || health.version === version)) return health;
|
|
124
|
-
}
|
|
125
|
-
throw new Error(`The daemon didn't start. See ${join(dataDir, "daemon.log")}.`);
|
|
126
|
-
}
|
|
127
|
-
function sameFlags(a, b) {
|
|
128
|
-
return a.length === b.length && a.every((flag, i) => flag === b[i]);
|
|
129
|
-
}
|
|
130
|
-
/** Stop the daemon answering on `port`. Returns false when none was running. */
|
|
131
|
-
async function stopDaemon(port = DEFAULT_PORT) {
|
|
132
|
-
const running = await probeDaemon(port);
|
|
133
|
-
if (!running) return false;
|
|
134
|
-
try {
|
|
135
|
-
process.kill(running.pid, "SIGTERM");
|
|
136
|
-
} catch {
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
for (let attempt = 0; attempt < 50; attempt++) {
|
|
140
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
141
|
-
if (!await probeDaemon(port)) return true;
|
|
142
|
-
}
|
|
143
|
-
return true;
|
|
144
|
-
}
|
|
145
|
-
//#endregion
|
|
146
|
-
//#region src/service/login-service.ts
|
|
147
|
-
function serviceNames(port = DEFAULT_PORT) {
|
|
148
|
-
const own = port !== DEFAULT_PORT;
|
|
149
|
-
return {
|
|
150
|
-
label: own ? `dev.tencil.daemon.${port}` : "dev.tencil.daemon",
|
|
151
|
-
unit: own ? `tencil-${port}.service` : "tencil.service",
|
|
152
|
-
task: own ? `Tencil ${port}` : "Tencil"
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* What the daemon needs from the shell that installed it. launchd and systemd
|
|
157
|
-
* start it with a bare PATH (`/usr/bin:/bin`), where nvm's npm and npx, which
|
|
158
|
-
* it runs to pair and to install the Claude Agent SDK, can't be found; and a
|
|
159
|
-
* custom XDG_CONFIG_HOME must reach it, or it reads another data folder than
|
|
160
|
-
* the CLI. npx's and npm's own folders (anything in node_modules) are left out of PATH.
|
|
161
|
-
*/
|
|
162
|
-
function serviceEnvironment(command, env = process.env) {
|
|
163
|
-
const separator = process.platform === "win32" ? ";" : ":";
|
|
164
|
-
const entries = [dirname(command.node), ...(env.PATH ?? "").split(separator)].filter((entry) => entry && !/[\\/](?:_npx|node_modules)[\\/]/.test(entry));
|
|
165
|
-
const environment = { PATH: [...new Set(entries)].join(separator) };
|
|
166
|
-
if (env.XDG_CONFIG_HOME) environment.XDG_CONFIG_HOME = env.XDG_CONFIG_HOME;
|
|
167
|
-
return environment;
|
|
168
|
-
}
|
|
169
|
-
function launchdPlist(command, logPath, environment = {}) {
|
|
170
|
-
const args = [command.node, ...daemonArgs(command)].map((arg) => ` <string>${escapeXml(arg)}</string>`).join("\n");
|
|
171
|
-
const variables = Object.entries(environment).map(([key, value]) => ` <key>${escapeXml(key)}</key>\n <string>${escapeXml(value)}</string>`).join("\n");
|
|
172
|
-
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
173
|
-
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
174
|
-
<plist version="1.0">
|
|
175
|
-
<dict>
|
|
176
|
-
<key>Label</key>
|
|
177
|
-
<string>${serviceNames(command.port).label}</string>
|
|
178
|
-
<key>ProgramArguments</key>
|
|
179
|
-
<array>
|
|
180
|
-
${args}
|
|
181
|
-
</array>
|
|
182
|
-
<key>EnvironmentVariables</key>
|
|
183
|
-
<dict>
|
|
184
|
-
${variables}
|
|
185
|
-
</dict>
|
|
186
|
-
<key>RunAtLoad</key>
|
|
187
|
-
<true/>
|
|
188
|
-
<key>KeepAlive</key>
|
|
189
|
-
<true/>
|
|
190
|
-
<key>StandardOutPath</key>
|
|
191
|
-
<string>${escapeXml(logPath)}</string>
|
|
192
|
-
<key>StandardErrorPath</key>
|
|
193
|
-
<string>${escapeXml(logPath)}</string>
|
|
194
|
-
</dict>
|
|
195
|
-
</plist>
|
|
196
|
-
`;
|
|
197
|
-
}
|
|
198
|
-
function systemdUnit(command, environment = {}) {
|
|
199
|
-
return `[Unit]
|
|
200
|
-
Description=Tencil daemon
|
|
201
|
-
|
|
202
|
-
[Service]
|
|
203
|
-
${Object.entries(environment).map(([key, value]) => `Environment="${key}=${value}"\n`).join("")}ExecStart=${[command.node, ...daemonArgs(command)].map((arg) => `"${arg}"`).join(" ")}
|
|
204
|
-
Restart=on-failure
|
|
205
|
-
|
|
206
|
-
[Install]
|
|
207
|
-
WantedBy=default.target
|
|
208
|
-
`;
|
|
209
|
-
}
|
|
210
|
-
function windowsTaskCommand(command) {
|
|
211
|
-
return [command.node, ...daemonArgs(command)].map((arg) => `"${arg}"`).join(" ");
|
|
212
|
-
}
|
|
213
|
-
const launchAgent = (names) => join(homedir(), "Library", "LaunchAgents", `${names.label}.plist`);
|
|
214
|
-
const systemdUnitPath = (names) => join(homedir(), ".config", "systemd", "user", names.unit);
|
|
215
|
-
const launchdDomain = () => `gui/${process.getuid?.() ?? 501}`;
|
|
216
|
-
/** Whether Tencil is set up to start at login, serving `port`. */
|
|
217
|
-
function isServiceInstalled(port = DEFAULT_PORT) {
|
|
218
|
-
return servicePort(readServiceDefinition(serviceNames(port))) === port;
|
|
219
|
-
}
|
|
220
|
-
/** The daemon port in a launchd plist, systemd unit or scheduled task definition. */
|
|
221
|
-
function servicePort(definition) {
|
|
222
|
-
const match = definition?.match(/--port(?:<\/string>\s*<string>|"?\s+"?|"\s*")(\d+)/);
|
|
223
|
-
return match?.[1] ? Number(match[1]) : void 0;
|
|
224
|
-
}
|
|
225
|
-
/** The launchd plist, systemd unit or scheduled task behind `names`, if installed. */
|
|
226
|
-
function readServiceDefinition(names) {
|
|
227
|
-
try {
|
|
228
|
-
switch (process.platform) {
|
|
229
|
-
case "darwin": return readFileSync(launchAgent(names), "utf8");
|
|
230
|
-
case "linux": return readFileSync(systemdUnitPath(names), "utf8");
|
|
231
|
-
case "win32": return execFileSync("schtasks", [
|
|
232
|
-
"/Query",
|
|
233
|
-
"/TN",
|
|
234
|
-
names.task,
|
|
235
|
-
"/XML"
|
|
236
|
-
], {
|
|
237
|
-
encoding: "utf8",
|
|
238
|
-
stdio: [
|
|
239
|
-
"ignore",
|
|
240
|
-
"pipe",
|
|
241
|
-
"ignore"
|
|
242
|
-
]
|
|
243
|
-
});
|
|
244
|
-
default: return;
|
|
245
|
-
}
|
|
246
|
-
} catch {
|
|
247
|
-
return;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
/** Install (or replace) the login service and start the daemon through it. */
|
|
251
|
-
async function installService(command, dataDir) {
|
|
252
|
-
const logPath = join(dataDir, "daemon.log");
|
|
253
|
-
const names = serviceNames(command.port);
|
|
254
|
-
const { unit: unitName, task } = names;
|
|
255
|
-
mkdirSync(dataDir, { recursive: true });
|
|
256
|
-
switch (process.platform) {
|
|
257
|
-
case "darwin": {
|
|
258
|
-
const plist = launchAgent(names);
|
|
259
|
-
quietly("launchctl", [
|
|
260
|
-
"bootout",
|
|
261
|
-
launchdDomain(),
|
|
262
|
-
plist
|
|
263
|
-
]);
|
|
264
|
-
await stopDaemon(command.port);
|
|
265
|
-
mkdirSync(join(homedir(), "Library", "LaunchAgents"), { recursive: true });
|
|
266
|
-
writeFileSync(plist, launchdPlist(command, logPath, serviceEnvironment(command)));
|
|
267
|
-
execFileSync("launchctl", [
|
|
268
|
-
"bootstrap",
|
|
269
|
-
launchdDomain(),
|
|
270
|
-
plist
|
|
271
|
-
]);
|
|
272
|
-
return plist;
|
|
273
|
-
}
|
|
274
|
-
case "linux": {
|
|
275
|
-
const unit = systemdUnitPath(names);
|
|
276
|
-
quietly("systemctl", [
|
|
277
|
-
"--user",
|
|
278
|
-
"stop",
|
|
279
|
-
unitName
|
|
280
|
-
]);
|
|
281
|
-
await stopDaemon(command.port);
|
|
282
|
-
mkdirSync(join(homedir(), ".config", "systemd", "user"), { recursive: true });
|
|
283
|
-
writeFileSync(unit, systemdUnit(command, serviceEnvironment(command)));
|
|
284
|
-
execFileSync("systemctl", ["--user", "daemon-reload"]);
|
|
285
|
-
execFileSync("systemctl", [
|
|
286
|
-
"--user",
|
|
287
|
-
"enable",
|
|
288
|
-
"--now",
|
|
289
|
-
unitName
|
|
290
|
-
], { stdio: [
|
|
291
|
-
"ignore",
|
|
292
|
-
"ignore",
|
|
293
|
-
"pipe"
|
|
294
|
-
] });
|
|
295
|
-
return unit;
|
|
296
|
-
}
|
|
297
|
-
case "win32":
|
|
298
|
-
quietly("schtasks", [
|
|
299
|
-
"/End",
|
|
300
|
-
"/TN",
|
|
301
|
-
task
|
|
302
|
-
]);
|
|
303
|
-
await stopDaemon(command.port);
|
|
304
|
-
execFileSync("schtasks", [
|
|
305
|
-
"/Create",
|
|
306
|
-
"/F",
|
|
307
|
-
"/SC",
|
|
308
|
-
"ONLOGON",
|
|
309
|
-
"/TN",
|
|
310
|
-
task,
|
|
311
|
-
"/TR",
|
|
312
|
-
windowsTaskCommand(command)
|
|
313
|
-
]);
|
|
314
|
-
execFileSync("schtasks", [
|
|
315
|
-
"/Run",
|
|
316
|
-
"/TN",
|
|
317
|
-
task
|
|
318
|
-
]);
|
|
319
|
-
return `Task Scheduler: ${task}`;
|
|
320
|
-
default: throw new Error(`Login service isn't supported on ${process.platform}; use \`tencil start\`.`);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
/** Stop the service's daemon until the next login, without uninstalling it. */
|
|
324
|
-
async function stopService(port = DEFAULT_PORT) {
|
|
325
|
-
const names = serviceNames(port);
|
|
326
|
-
const { unit, task } = names;
|
|
327
|
-
switch (process.platform) {
|
|
328
|
-
case "darwin":
|
|
329
|
-
quietly("launchctl", [
|
|
330
|
-
"bootout",
|
|
331
|
-
launchdDomain(),
|
|
332
|
-
launchAgent(names)
|
|
333
|
-
]);
|
|
334
|
-
break;
|
|
335
|
-
case "linux":
|
|
336
|
-
quietly("systemctl", [
|
|
337
|
-
"--user",
|
|
338
|
-
"stop",
|
|
339
|
-
unit
|
|
340
|
-
]);
|
|
341
|
-
break;
|
|
342
|
-
case "win32": quietly("schtasks", [
|
|
343
|
-
"/End",
|
|
344
|
-
"/TN",
|
|
345
|
-
task
|
|
346
|
-
]);
|
|
347
|
-
}
|
|
348
|
-
await stopDaemon(port);
|
|
349
|
-
}
|
|
350
|
-
async function uninstallService(port = DEFAULT_PORT) {
|
|
351
|
-
removeService(serviceNames(port));
|
|
352
|
-
await stopDaemon(port);
|
|
353
|
-
}
|
|
354
|
-
/** Unload and delete the service behind `names`, wherever it is in its life. */
|
|
355
|
-
function removeService(names) {
|
|
356
|
-
switch (process.platform) {
|
|
357
|
-
case "darwin":
|
|
358
|
-
quietly("launchctl", [
|
|
359
|
-
"bootout",
|
|
360
|
-
launchdDomain(),
|
|
361
|
-
launchAgent(names)
|
|
362
|
-
]);
|
|
363
|
-
rmSync(launchAgent(names), { force: true });
|
|
364
|
-
break;
|
|
365
|
-
case "linux":
|
|
366
|
-
quietly("systemctl", [
|
|
367
|
-
"--user",
|
|
368
|
-
"disable",
|
|
369
|
-
"--now",
|
|
370
|
-
names.unit
|
|
371
|
-
]);
|
|
372
|
-
rmSync(systemdUnitPath(names), { force: true });
|
|
373
|
-
quietly("systemctl", ["--user", "daemon-reload"]);
|
|
374
|
-
break;
|
|
375
|
-
case "win32":
|
|
376
|
-
quietly("schtasks", [
|
|
377
|
-
"/End",
|
|
378
|
-
"/TN",
|
|
379
|
-
names.task
|
|
380
|
-
]);
|
|
381
|
-
quietly("schtasks", [
|
|
382
|
-
"/Delete",
|
|
383
|
-
"/F",
|
|
384
|
-
"/TN",
|
|
385
|
-
names.task
|
|
386
|
-
]);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
/** Run a command whose failure only means "nothing to do" (not loaded, already gone). */
|
|
390
|
-
function quietly(command, args) {
|
|
391
|
-
try {
|
|
392
|
-
execFileSync(command, args, { stdio: "ignore" });
|
|
393
|
-
} catch {}
|
|
394
|
-
}
|
|
395
|
-
function escapeXml(value) {
|
|
396
|
-
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
397
|
-
}
|
|
398
|
-
//#endregion
|
|
399
|
-
//#region src/service/autostart.ts
|
|
400
|
-
/**
|
|
401
|
-
* Running from a package manager's install (npx's cache, a project, a global
|
|
402
|
-
* prefix) rather than a source checkout. Those can be cleaned up or upgraded
|
|
403
|
-
* under a login service, so it gets its own copy.
|
|
404
|
-
*/
|
|
405
|
-
function isPackageInstall(cliPath) {
|
|
406
|
-
return /[\\/]node_modules[\\/]/.test(cliPath);
|
|
407
|
-
}
|
|
408
|
-
/** The login service's own copy of Tencil; one per port, like the service. */
|
|
409
|
-
function runtimeDir(dataDir, port = DEFAULT_PORT) {
|
|
410
|
-
return join(dataDir, port === 4747 ? "runtime" : `runtime-${port}`);
|
|
411
|
-
}
|
|
412
|
-
function runtimeCli(dataDir, port = DEFAULT_PORT) {
|
|
413
|
-
return join(runtimeDir(dataDir, port), "node_modules", "tencil", "dist", "cli.mjs");
|
|
414
|
-
}
|
|
415
|
-
function runtimeInstallArgs(dataDir, spec, port = DEFAULT_PORT) {
|
|
416
|
-
return [
|
|
417
|
-
"install",
|
|
418
|
-
"--prefix",
|
|
419
|
-
runtimeDir(dataDir, port),
|
|
420
|
-
spec,
|
|
421
|
-
"--no-audit",
|
|
422
|
-
"--no-fund",
|
|
423
|
-
"--loglevel=error"
|
|
424
|
-
];
|
|
425
|
-
}
|
|
426
|
-
/**
|
|
427
|
-
* Start Tencil at login and now. Keeps the running daemon's flags unless new
|
|
428
|
-
* ones are given. `from` installs another package spec (a local tarball) as
|
|
429
|
-
* the service's copy instead of this version from npm.
|
|
430
|
-
*/
|
|
431
|
-
async function enableAutostart(command, dataDir, from) {
|
|
432
|
-
const running = await probeDaemon(command.port);
|
|
433
|
-
if (running && isNewerThanThis(running.version)) throw new NewerDaemonError(running.version);
|
|
434
|
-
const args = command.args?.length ? command.args : running?.flags ?? [];
|
|
435
|
-
let cli = command.cli;
|
|
436
|
-
if (isPackageInstall(cli)) {
|
|
437
|
-
const spec = from && existsSync(from) ? resolve(from) : from ?? `tencil@${VERSION}`;
|
|
438
|
-
const result = spawnSync(process.platform === "win32" ? "npm.cmd" : "npm", runtimeInstallArgs(dataDir, spec, command.port), {
|
|
439
|
-
encoding: "utf8",
|
|
440
|
-
stdio: [
|
|
441
|
-
"ignore",
|
|
442
|
-
"ignore",
|
|
443
|
-
"pipe"
|
|
444
|
-
],
|
|
445
|
-
shell: process.platform === "win32",
|
|
446
|
-
env: withoutNpmEnv()
|
|
447
|
-
});
|
|
448
|
-
if (result.status !== 0) throw new Error(`Couldn't install Tencil for the login service (npm ${result.status ?? "failed"}): ${result.stderr?.trim() ?? ""}`);
|
|
449
|
-
cli = runtimeCli(dataDir, command.port);
|
|
450
|
-
}
|
|
451
|
-
const where = await installService({
|
|
452
|
-
...command,
|
|
453
|
-
cli,
|
|
454
|
-
args
|
|
455
|
-
}, dataDir);
|
|
456
|
-
await waitForDaemon(command.port, dataDir);
|
|
457
|
-
return where;
|
|
458
|
-
}
|
|
459
|
-
/**
|
|
460
|
-
* Make sure this version of the daemon runs. With a login service it is
|
|
461
|
-
* updated in place, since stopping its daemon would only make it restart.
|
|
462
|
-
*/
|
|
463
|
-
async function ensureDaemon(command, dataDir) {
|
|
464
|
-
if (!isServiceInstalled(command.port)) {
|
|
465
|
-
const { status, newer } = await startInBackground(command, dataDir);
|
|
466
|
-
return {
|
|
467
|
-
status,
|
|
468
|
-
autostart: false,
|
|
469
|
-
newer
|
|
470
|
-
};
|
|
471
|
-
}
|
|
472
|
-
const running = await probeDaemon(command.port);
|
|
473
|
-
if (running && isNewerThanThis(running.version)) return {
|
|
474
|
-
status: "running",
|
|
475
|
-
autostart: true,
|
|
476
|
-
newer: running.version
|
|
477
|
-
};
|
|
478
|
-
const flags = command.args?.length ? command.args : running?.flags ?? [];
|
|
479
|
-
if (running && running.version === VERSION && sameFlags(running.flags, flags)) return {
|
|
480
|
-
status: "running",
|
|
481
|
-
autostart: true
|
|
482
|
-
};
|
|
483
|
-
await enableAutostart(command, dataDir);
|
|
484
|
-
return {
|
|
485
|
-
status: running ? "restarted" : "started",
|
|
486
|
-
autostart: true
|
|
487
|
-
};
|
|
488
|
-
}
|
|
489
|
-
//#endregion
|
|
490
30
|
//#region src/service/choose-provider.ts
|
|
491
31
|
/**
|
|
492
32
|
* The agent pins go to on this machine. Asked once: kept from an earlier run
|
|
@@ -1086,7 +626,7 @@ function parseModel(value) {
|
|
|
1086
626
|
}
|
|
1087
627
|
//#endregion
|
|
1088
628
|
//#region src/cli/commands/daemon.ts
|
|
1089
|
-
async function runDaemon(values, port, provider, t3Url, runtimeMode) {
|
|
629
|
+
async function runDaemon(values, port, provider, t3Url, runtimeMode, cli) {
|
|
1090
630
|
const model = values.model;
|
|
1091
631
|
const t3Model = model?.includes(":") ? model : void 0;
|
|
1092
632
|
const daemon = await startDaemon({
|
|
@@ -1105,7 +645,8 @@ async function runDaemon(values, port, provider, t3Url, runtimeMode) {
|
|
|
1105
645
|
createProjectIfMissing: !values["no-create-project"]
|
|
1106
646
|
},
|
|
1107
647
|
log: (message) => console.log(`${pc.dim(time())} ${message}`),
|
|
1108
|
-
flags: forwardedFlags(values)
|
|
648
|
+
flags: forwardedFlags(values),
|
|
649
|
+
cli
|
|
1109
650
|
}).catch((error) => {
|
|
1110
651
|
if (error.code === "EADDRINUSE") throw new Error(`Port ${port} is in use. Is Tencil already running? Try \`tencil status\`.`);
|
|
1111
652
|
throw error;
|
|
@@ -1118,6 +659,16 @@ async function runDaemon(values, port, provider, t3Url, runtimeMode) {
|
|
|
1118
659
|
process.once("SIGTERM", shutdown);
|
|
1119
660
|
}
|
|
1120
661
|
//#endregion
|
|
662
|
+
//#region src/cli/commands/finish-update.ts
|
|
663
|
+
/** Move the daemon to a staged version (see `service/self-update.ts`). Logs to daemon.log. */
|
|
664
|
+
async function finishUpdateCommand(planPath) {
|
|
665
|
+
if (!planPath) throw new Error("finish-update needs the update plan");
|
|
666
|
+
const plan = readPlan(planPath);
|
|
667
|
+
const log = (message) => console.log(`${pc.dim(time())} ${message}`);
|
|
668
|
+
log(`Updating the daemon on port ${plan.port} to Tencil ${plan.version}`);
|
|
669
|
+
log(await finishUpdate(plan) ? `Tencil ${plan.version} is running` : `Tencil ${plan.version} didn't start; went back to the previous version`);
|
|
670
|
+
}
|
|
671
|
+
//#endregion
|
|
1121
672
|
//#region src/cli/commands/setup.ts
|
|
1122
673
|
/** `tencil setup`: once per machine. Starts the daemon (at login, by default) and pairs. */
|
|
1123
674
|
async function setup(command, options = {}) {
|
|
@@ -1366,9 +917,9 @@ async function main() {
|
|
|
1366
917
|
from: values.from,
|
|
1367
918
|
t3Url
|
|
1368
919
|
});
|
|
1369
|
-
case "daemon": return runDaemon(values, port, provider, t3Url, runtimeMode);
|
|
920
|
+
case "daemon": return runDaemon(values, port, provider, t3Url, runtimeMode, daemonCommand.cli);
|
|
1370
921
|
case "start":
|
|
1371
|
-
if (values.foreground) return runDaemon(values, port, provider, t3Url, runtimeMode);
|
|
922
|
+
if (values.foreground) return runDaemon(values, port, provider, t3Url, runtimeMode, daemonCommand.cli);
|
|
1372
923
|
return start(daemonCommand);
|
|
1373
924
|
case "setup": return setup(daemonCommand, {
|
|
1374
925
|
yes: values.yes,
|
|
@@ -1387,6 +938,7 @@ async function main() {
|
|
|
1387
938
|
case "status": return status(port);
|
|
1388
939
|
case "service": return service(rest[0], daemonCommand, values.from);
|
|
1389
940
|
case "connect": return connect(rest[0], t3Url);
|
|
941
|
+
case "finish-update": return finishUpdateCommand(rest[0]);
|
|
1390
942
|
case "init": return runInit({
|
|
1391
943
|
root: resolve(values.root ?? process.cwd()),
|
|
1392
944
|
yes: values.yes,
|
|
@@ -319,6 +319,20 @@ interface Viewer {
|
|
|
319
319
|
/** `read`: can see pins, not make or answer them (no write access to the repository). */
|
|
320
320
|
access: 'write' | 'read';
|
|
321
321
|
}
|
|
322
|
+
/**
|
|
323
|
+
* The daemon's own updates (local only; Tencil Cloud leaves it out). `latest`
|
|
324
|
+
* is set when a newer Tencil is published. `state` walks through an update
|
|
325
|
+
* started from the overlay: installing it, waiting for running threads,
|
|
326
|
+
* restarting on it, or failed (the old version keeps running).
|
|
327
|
+
*/
|
|
328
|
+
interface UpdateStatus {
|
|
329
|
+
latest?: string;
|
|
330
|
+
state: 'idle' | 'installing' | 'waiting' | 'restarting' | 'failed';
|
|
331
|
+
/** `waiting`: how many threads are still running. */
|
|
332
|
+
running?: number;
|
|
333
|
+
/** `failed`: what went wrong. */
|
|
334
|
+
error?: string;
|
|
335
|
+
}
|
|
322
336
|
declare const ProviderInfo: z.ZodObject<{
|
|
323
337
|
id: z.ZodString;
|
|
324
338
|
label: z.ZodString;
|
|
@@ -365,6 +379,13 @@ type ServerMessage = {
|
|
|
365
379
|
threads: Thread[];
|
|
366
380
|
/** Tencil Cloud only: who is signed in. */
|
|
367
381
|
viewer?: Viewer;
|
|
382
|
+
/** Local daemon only: the latest version and any update in progress. */
|
|
383
|
+
update?: UpdateStatus;
|
|
384
|
+
} |
|
|
385
|
+
/** The update status changed: a check found a newer version, or an update moved on. */
|
|
386
|
+
{
|
|
387
|
+
type: 'update';
|
|
388
|
+
update: UpdateStatus;
|
|
368
389
|
} |
|
|
369
390
|
/** Tencil Cloud: sign in first (no token, or one it no longer accepts). */
|
|
370
391
|
{
|
|
@@ -488,4 +509,4 @@ interface ThreadSink {
|
|
|
488
509
|
step?(step: ThreadStep): void;
|
|
489
510
|
}
|
|
490
511
|
//#endregion
|
|
491
|
-
export {
|
|
512
|
+
export { Viewer as a, UpdateStatus as i, ProviderOption as n, Thread as o, ServerMessage as r, ElementContext as s, Provider as t };
|