tokenusage-cli 0.5.4 → 0.5.5
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 +20 -0
- package/package.json +1 -1
- package/src/cli.js +12 -0
- package/src/commands/project-profiles.js +94 -0
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,26 @@
|
|
|
6
6
|
> 0.4.x 之前的条目为压缩摘要;细节与当时的验收记录见 `docs/cli-release.md`
|
|
7
7
|
> 与 `docs/archive/`。
|
|
8
8
|
|
|
9
|
+
## 0.5.5
|
|
10
|
+
|
|
11
|
+
2026-11-11。新增只读子命令 `tokenusage project-profiles`(契约 §2.13),供桌面端伴生工具
|
|
12
|
+
读取 web 上编辑的项目显示名 / logo / 仓库。零破坏性变更:现有子命令行为、对账基线与队列
|
|
13
|
+
格式均不变,服务端只需部署含该 series 的版本(否则 CLI 报 `Unknown account series`,
|
|
14
|
+
桌面端静默降级)。
|
|
15
|
+
|
|
16
|
+
- **新增 `tokenusage project-profiles`**:不带参数,凭证解析与 `sync` 同一条路径(运行时
|
|
17
|
+
配置 → `cloud-device-token` 文件,自定义 base 需配 anon key),把账号的项目档案以**单行
|
|
18
|
+
JSON** 打到 stdout(`{"profiles":[{project_key, display_name, repo_url, repo_source,
|
|
19
|
+
detected_repo_url, manual_repo_url, repo_hidden, logo_url, logo_upload_url}]}`)。诊断信息
|
|
20
|
+
一律走 stderr + `UsageError`(退出码 2),stdout 始终保持可解析。
|
|
21
|
+
- **桌面端消费方式**:桌面经 `run_cli` 捕获该行并解析,挂载时与每 5 分钟刷新一次,用于把
|
|
22
|
+
项目列表 / 项目详情的显示名、logo、仓库链接对齐 web;未配对、HTTP 失败或旧 CLI 均静默
|
|
23
|
+
降级为队列 key + 字母方块,界面不空。
|
|
24
|
+
|
|
25
|
+
升级说明:装完即生效(无迁移动作)。Volta 托管的机器升级要跑 `volta install
|
|
26
|
+
tokenusage-cli@0.5.5`(`npm i -g` 只更新 image-dir 副本,`volta run` 仍解析 packages
|
|
27
|
+
副本,见 `docs/cli-release.md` §十三/§十四)。
|
|
28
|
+
|
|
9
29
|
## 0.5.4
|
|
10
30
|
|
|
11
31
|
2026-11-09。tracker 目录写入被拒时的可诊断性与降级。零契约变更,服务端无需同步升级;对账
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -21,6 +21,7 @@ const { cmdDoctor } = require("./commands/doctor");
|
|
|
21
21
|
const { cmdUninstall } = require("./commands/uninstall");
|
|
22
22
|
const { cmdDeviceLogin } = require("./commands/device-login");
|
|
23
23
|
const { cmdSessions } = require("./commands/sessions");
|
|
24
|
+
const { cmdProjectProfiles } = require("./commands/project-profiles");
|
|
24
25
|
const { cmdPause, cmdResume } = require("./commands/pause");
|
|
25
26
|
|
|
26
27
|
const DEFAULT_WATCH_INTERVAL_MS = 15 * 60_000;
|
|
@@ -223,6 +224,17 @@ const COMMANDS = [
|
|
|
223
224
|
" --no-git Skip git outcome attribution",
|
|
224
225
|
],
|
|
225
226
|
},
|
|
227
|
+
{
|
|
228
|
+
name: "project-profiles",
|
|
229
|
+
summary: "Print the web-edited project profiles (display name, logo, repo)",
|
|
230
|
+
run: (argv) => cmdProjectProfiles(argv),
|
|
231
|
+
usage: [
|
|
232
|
+
"tokenusage project-profiles",
|
|
233
|
+
"",
|
|
234
|
+
"Prints the account's project profiles as a single JSON line (contract",
|
|
235
|
+
"docs/cli-contract.md §2.13). No options.",
|
|
236
|
+
],
|
|
237
|
+
},
|
|
226
238
|
];
|
|
227
239
|
|
|
228
240
|
function findCommand(name) {
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// `tokenusage project-profiles` — read-only mirror of the web-edited project
|
|
4
|
+
// profiles (docs/cli-contract.md §2.13), for the desktop companion: the
|
|
5
|
+
// desktop never reads the device token file (desktop-plan §3 boundary 1), so
|
|
6
|
+
// the CLI — which already holds the pairing credential — fetches the account
|
|
7
|
+
// payload and prints it as a single JSON line on stdout. Diagnostics travel
|
|
8
|
+
// through UsageError (stderr + exit 2) so stdout stays parseable.
|
|
9
|
+
|
|
10
|
+
const os = require("node:os");
|
|
11
|
+
const path = require("node:path");
|
|
12
|
+
|
|
13
|
+
const { readJson } = require("../lib/fs");
|
|
14
|
+
const { resolveTrackerPaths } = require("../lib/tracker-paths");
|
|
15
|
+
const { normalizeInstanceBaseUrl, resolveRuntimeConfig } = require("../lib/runtime-config");
|
|
16
|
+
const { readCloudDeviceTokenEntry } = require("../lib/cloud-device-token");
|
|
17
|
+
const { functionUrlFor, fetchFunctionResponse } = require("../lib/function-url");
|
|
18
|
+
const { UsageError, unknownOptionError } = require("../lib/cli-errors");
|
|
19
|
+
|
|
20
|
+
// Contract §2.13: the desktop parses this exact payload shape.
|
|
21
|
+
const SLUG = "token-usage-account-project-profiles";
|
|
22
|
+
|
|
23
|
+
function parseArgs(args = []) {
|
|
24
|
+
for (const a of args) throw unknownOptionError(a, "project-profiles");
|
|
25
|
+
return {};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Same resolution order as `resolveDeviceTokenPresence`: runtime config
|
|
30
|
+
* (env / config.json) first, then the cloud-device-token file — but this
|
|
31
|
+
* returns the VALUE, which the desktop's `run_cli` capture turns into data.
|
|
32
|
+
* The cloud file is only trusted when its recorded instance matches the
|
|
33
|
+
* configured one (same guard as the presence check).
|
|
34
|
+
*/
|
|
35
|
+
async function resolveCredentials({ home }) {
|
|
36
|
+
const { trackerDir } = await resolveTrackerPaths({ home });
|
|
37
|
+
const config = (await readJson(path.join(trackerDir, "config.json"))) || {};
|
|
38
|
+
const runtime = resolveRuntimeConfig({ config, env: process.env });
|
|
39
|
+
const baseUrl =
|
|
40
|
+
typeof runtime.baseUrl === "string" ? runtime.baseUrl.trim() : "";
|
|
41
|
+
let deviceToken =
|
|
42
|
+
typeof runtime.deviceToken === "string" ? runtime.deviceToken.trim() : "";
|
|
43
|
+
|
|
44
|
+
if (!deviceToken) {
|
|
45
|
+
const entry = readCloudDeviceTokenEntry(
|
|
46
|
+
path.join(trackerDir, "cloud-device-token.json"),
|
|
47
|
+
);
|
|
48
|
+
// The cache entry stores the credential as `token` (bound to userId /
|
|
49
|
+
// baseUrl / machineId), unlike config.json's `deviceToken`.
|
|
50
|
+
const entryToken =
|
|
51
|
+
entry && typeof entry.token === "string" ? entry.token.trim() : "";
|
|
52
|
+
const entryBase = normalizeInstanceBaseUrl(
|
|
53
|
+
(entry && entry.baseUrl) || "",
|
|
54
|
+
);
|
|
55
|
+
const configuredBase = normalizeInstanceBaseUrl(baseUrl);
|
|
56
|
+
|
|
57
|
+
if (entryToken && (!configuredBase || entryBase === configuredBase)) {
|
|
58
|
+
deviceToken = entryToken;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { baseUrl, deviceToken };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function cmdProjectProfiles(argv = [], context = {}) {
|
|
66
|
+
parseArgs(argv);
|
|
67
|
+
const home = context.home || os.homedir();
|
|
68
|
+
const { baseUrl, deviceToken } = await resolveCredentials({ home });
|
|
69
|
+
|
|
70
|
+
if (!baseUrl || !deviceToken) {
|
|
71
|
+
throw new UsageError("This machine is not paired yet.", {
|
|
72
|
+
code: "NOT_PAIRED",
|
|
73
|
+
hint: "Run `tokenusage device-login` first, then try again.",
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const res = await fetchFunctionResponse(functionUrlFor(baseUrl, SLUG), {
|
|
78
|
+
headers: {
|
|
79
|
+
Accept: "application/json",
|
|
80
|
+
Authorization: `Bearer ${deviceToken}`,
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
if (!res.ok) {
|
|
84
|
+
const body = (await res.text().catch(() => "")).replace(/\s+/g, " ").slice(0, 200);
|
|
85
|
+
throw new UsageError(`Request failed with HTTP ${res.status}.`, {
|
|
86
|
+
code: "HTTP_ERROR",
|
|
87
|
+
hint: body || "Check your instance URL and try again.",
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const payload = await res.json();
|
|
91
|
+
process.stdout.write(`${JSON.stringify(payload)}\n`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = { cmdProjectProfiles };
|