benignswarm 0.4.0 → 0.4.1
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/dist/client.js +4 -1
- package/dist/commands/admin.js +2 -1
- package/dist/commands/join.js +2 -1
- package/dist/index.js +2 -1
- package/dist/update.js +43 -0
- package/dist/version.js +1 -0
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { CliError } from "./errors.js";
|
|
2
2
|
import { collectAuto } from "./git.js";
|
|
3
|
+
import { CLI_VERSION } from "./version.js";
|
|
4
|
+
import { noteServerVersions } from "./update.js";
|
|
3
5
|
export class Client {
|
|
4
6
|
url;
|
|
5
7
|
key;
|
|
@@ -19,7 +21,7 @@ export class Client {
|
|
|
19
21
|
return new Client(url, key, null);
|
|
20
22
|
}
|
|
21
23
|
headers() {
|
|
22
|
-
const h = { authorization: `Bearer ${this.key}` };
|
|
24
|
+
const h = { authorization: `Bearer ${this.key}`, "user-agent": `bschat-cli/${CLI_VERSION}` };
|
|
23
25
|
if (this.auto)
|
|
24
26
|
h["x-bschat-auto"] = JSON.stringify(this.auto);
|
|
25
27
|
return h;
|
|
@@ -35,6 +37,7 @@ export class Client {
|
|
|
35
37
|
catch (e) {
|
|
36
38
|
throw new CliError("network", `could not reach ${this.url}: ${e.message}`);
|
|
37
39
|
}
|
|
40
|
+
noteServerVersions(res.headers);
|
|
38
41
|
const text = await res.text();
|
|
39
42
|
let json;
|
|
40
43
|
try {
|
package/dist/commands/admin.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CLI_VERSION } from "../version.js";
|
|
1
2
|
import { bool, need, str } from "../args.js";
|
|
2
3
|
import { Client } from "../client.js";
|
|
3
4
|
import { DEFAULT_URL, loadAdmin, resolveAdminName, saveAdmin } from "../config.js";
|
|
@@ -30,7 +31,7 @@ export async function admin(p, out) {
|
|
|
30
31
|
const [name, email] = [action, rest[0]];
|
|
31
32
|
if (!name || !email)
|
|
32
33
|
throw new CliError("usage", "admin signup --url <api> --key <signup-code> <account-name> <email>");
|
|
33
|
-
const res = await fetch(`${url}/accounts`, { method: "POST", headers: { "content-type": "application/json", "x-signup-code": code }, body: JSON.stringify({ name, email }) });
|
|
34
|
+
const res = await fetch(`${url}/accounts`, { method: "POST", headers: { "content-type": "application/json", "x-signup-code": code, "user-agent": `bschat-cli/${CLI_VERSION}` }, body: JSON.stringify({ name, email }) });
|
|
34
35
|
const json = await res.json();
|
|
35
36
|
if (!res.ok)
|
|
36
37
|
throw new CliError(json.error, json.message);
|
package/dist/commands/join.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CLI_VERSION } from "../version.js";
|
|
1
2
|
import { spawnSync } from "node:child_process";
|
|
2
3
|
import { need, str, bool } from "../args.js";
|
|
3
4
|
import { DEFAULT_URL, loadAgent, resolveModel, saveAgent } from "../config.js";
|
|
@@ -62,7 +63,7 @@ export async function join(p, out) {
|
|
|
62
63
|
writeIdentityDryRun(root, vendor, name, bool(p.flags, "replace"));
|
|
63
64
|
let res;
|
|
64
65
|
try {
|
|
65
|
-
res = await fetch(`${url}/enroll`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ token, name, vendor, machine }) });
|
|
66
|
+
res = await fetch(`${url}/enroll`, { method: "POST", headers: { "content-type": "application/json", "user-agent": `bschat-cli/${CLI_VERSION}` }, body: JSON.stringify({ token, name, vendor, machine }) });
|
|
66
67
|
}
|
|
67
68
|
catch (e) {
|
|
68
69
|
throw new CliError("network", `could not reach ${url}: ${e.message}`);
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,7 @@ import { join, setup } from "./commands/join.js";
|
|
|
7
7
|
import { hook } from "./commands/hook.js";
|
|
8
8
|
import { CliError } from "./errors.js";
|
|
9
9
|
import { HELP } from "./help.js";
|
|
10
|
+
import { CLI_VERSION } from "./version.js";
|
|
10
11
|
import { makeOut } from "./output.js";
|
|
11
12
|
const COMMANDS = {
|
|
12
13
|
join,
|
|
@@ -33,7 +34,7 @@ async function main(argv) {
|
|
|
33
34
|
const p = parse(argv);
|
|
34
35
|
const out = makeOut(bool(p.flags, "json"));
|
|
35
36
|
if (bool(p.flags, "version")) {
|
|
36
|
-
process.stdout.write(
|
|
37
|
+
process.stdout.write(`bschat ${CLI_VERSION}\n`);
|
|
37
38
|
return 0;
|
|
38
39
|
}
|
|
39
40
|
const cmd = p.positionals[0];
|
package/dist/update.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { configRoot } from "./config.js";
|
|
4
|
+
import { CLI_VERSION } from "./version.js";
|
|
5
|
+
const NOTICE_INTERVAL_MS = 24 * 3600_000;
|
|
6
|
+
function cmp(a, b) {
|
|
7
|
+
const pa = a.split(".").map(Number);
|
|
8
|
+
const pb = b.split(".").map(Number);
|
|
9
|
+
for (let i = 0; i < 3; i++) {
|
|
10
|
+
const d = (pa[i] ?? 0) - (pb[i] ?? 0);
|
|
11
|
+
if (d)
|
|
12
|
+
return d;
|
|
13
|
+
}
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Reads the version headers the API attaches to every CLI response. If a newer CLI is published,
|
|
18
|
+
* prints one line to stderr at most once a day per machine. Never affects stdout or exit codes.
|
|
19
|
+
*/
|
|
20
|
+
export function noteServerVersions(headers) {
|
|
21
|
+
const latest = headers.get("x-bschat-latest-cli");
|
|
22
|
+
if (!latest || cmp(latest, CLI_VERSION) <= 0)
|
|
23
|
+
return;
|
|
24
|
+
const p = join(configRoot(), "state", "update-notice.json");
|
|
25
|
+
let last = 0;
|
|
26
|
+
try {
|
|
27
|
+
if (existsSync(p))
|
|
28
|
+
last = JSON.parse(readFileSync(p, "utf8")).at;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
last = 0;
|
|
32
|
+
}
|
|
33
|
+
if (Date.now() - last < NOTICE_INTERVAL_MS)
|
|
34
|
+
return;
|
|
35
|
+
try {
|
|
36
|
+
mkdirSync(join(p, ".."), { recursive: true });
|
|
37
|
+
writeFileSync(p, JSON.stringify({ at: Date.now(), latest }));
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
/* unwritable state dir: notice simply repeats next time */
|
|
41
|
+
}
|
|
42
|
+
process.stderr.write(`[bschat] CLI ${CLI_VERSION} is installed, ${latest} is available: npm i -g benignswarm@latest (npx -y benignswarm already uses the latest)\n`);
|
|
43
|
+
}
|
package/dist/version.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const CLI_VERSION = "0.4.1";
|