@aiusage/cli 0.4.1 → 0.4.3

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.
Files changed (2) hide show
  1. package/dist/cli.js +100 -26
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -610,9 +610,9 @@ function processLedger(thread, events, dates, grouped) {
610
610
  }
611
611
  }
612
612
  function processMessages(thread, dates, grouped) {
613
- let msgs = thread.messages;
614
- if (!msgs) return;
615
- let entries = Array.isArray(msgs) ? msgs : Object.values(msgs);
613
+ let msgs2 = thread.messages;
614
+ if (!msgs2) return;
615
+ let entries = Array.isArray(msgs2) ? msgs2 : Object.values(msgs2);
616
616
  for (let msg of entries) {
617
617
  let usage = msg.usage;
618
618
  if (!usage) continue;
@@ -1826,48 +1826,119 @@ function escapeXml(value) {
1826
1826
  }
1827
1827
 
1828
1828
  // src/doctor.ts
1829
- import { stat as stat2 } from "node:fs/promises";
1829
+ import { readdir as readdir6, stat as stat2 } from "node:fs/promises";
1830
1830
  import { homedir as homedir14 } from "node:os";
1831
1831
  import { join as join14 } from "node:path";
1832
- async function runDoctor() {
1833
- let checks = [], config = await readConfig(), configPath = getConfigPath();
1832
+ var msgs = {
1833
+ en: {
1834
+ config: "Config",
1835
+ configMissing: "Not found, run aiusage init first",
1836
+ deviceId: "Device ID",
1837
+ deviceIdMissing: "Not configured, run aiusage init",
1838
+ targetMissing: "Not configured, run aiusage enroll",
1839
+ targets: "Targets",
1840
+ deviceToken: "Device token",
1841
+ tokenMissing: "Not registered",
1842
+ server: "Server",
1843
+ lastSync: "Last sync",
1844
+ lastSyncNone: "No record",
1845
+ notInstalled: "Not installed",
1846
+ installedNoData: "Installed, no usage data yet",
1847
+ hasData: (n) => `${n} session${n > 1 ? "s" : ""} found`,
1848
+ schedule: "Schedule",
1849
+ scheduleEvery: "every",
1850
+ scheduleEnabled: "Enabled",
1851
+ scheduleOff: "Not enabled, run aiusage schedule on"
1852
+ },
1853
+ zh: {
1854
+ config: "\u914D\u7F6E\u6587\u4EF6",
1855
+ configMissing: "\u672A\u627E\u5230\uFF0C\u8BF7\u5148\u6267\u884C aiusage init",
1856
+ deviceId: "\u8BBE\u5907 ID",
1857
+ deviceIdMissing: "\u672A\u914D\u7F6E\uFF0C\u8BF7\u6267\u884C aiusage init",
1858
+ targetMissing: "\u672A\u914D\u7F6E\uFF0C\u8BF7\u6267\u884C aiusage enroll",
1859
+ targets: "\u4E0A\u62A5\u76EE\u6807",
1860
+ deviceToken: "\u8BBE\u5907\u4EE4\u724C",
1861
+ tokenMissing: "\u672A\u6CE8\u518C",
1862
+ server: "\u670D\u52A1\u7AEF",
1863
+ lastSync: "\u4E0A\u6B21\u540C\u6B65",
1864
+ lastSyncNone: "\u6682\u65E0\u8BB0\u5F55",
1865
+ notInstalled: "\u672A\u5B89\u88C5",
1866
+ installedNoData: "\u5DF2\u5B89\u88C5\uFF0C\u6682\u65E0\u4F7F\u7528\u6570\u636E",
1867
+ hasData: (n) => `\u53D1\u73B0 ${n} \u4E2A\u4F1A\u8BDD`,
1868
+ schedule: "\u5B9A\u65F6\u540C\u6B65",
1869
+ scheduleEvery: "\u6BCF",
1870
+ scheduleEnabled: "\u5DF2\u542F\u7528",
1871
+ scheduleOff: "\u672A\u542F\u7528\uFF0C\u53EF\u6267\u884C aiusage schedule on"
1872
+ }
1873
+ };
1874
+ async function countFiles(dir, exts, cap = 1e3) {
1875
+ let count = 0, queue = [dir];
1876
+ for (; queue.length > 0 && count < cap; ) {
1877
+ let current = queue.pop(), entries;
1878
+ try {
1879
+ entries = await readdir6(current, { withFileTypes: !0 });
1880
+ } catch {
1881
+ continue;
1882
+ }
1883
+ for (let e of entries) {
1884
+ if (count >= cap) break;
1885
+ let full = join14(current, e.name);
1886
+ e.isDirectory() ? queue.push(full) : exts.some((ext) => e.name.endsWith(ext)) && count++;
1887
+ }
1888
+ }
1889
+ return count;
1890
+ }
1891
+ async function runDoctor(lang = "zh") {
1892
+ let s = msgs[lang], checks = [], config = await readConfig(), configPath = getConfigPath();
1834
1893
  try {
1835
- await stat2(configPath), checks.push({ name: "\u914D\u7F6E\u6587\u4EF6", status: "ok", message: configPath });
1894
+ await stat2(configPath), checks.push({ name: s.config, status: "ok", message: configPath });
1836
1895
  } catch {
1837
- checks.push({ name: "\u914D\u7F6E\u6587\u4EF6", status: "fail", message: "\u672A\u627E\u5230\uFF0C\u8BF7\u5148\u6267\u884C aiusage init" });
1896
+ checks.push({ name: s.config, status: "fail", message: s.configMissing });
1838
1897
  }
1839
- config.deviceId ? checks.push({ name: "\u8BBE\u5907 ID", status: "ok", message: config.deviceId }) : checks.push({ name: "\u8BBE\u5907 ID", status: "warn", message: "\u672A\u914D\u7F6E\uFF0C\u8BF7\u6267\u884C aiusage init" });
1898
+ config.deviceId ? checks.push({ name: s.deviceId, status: "ok", message: config.deviceId }) : checks.push({ name: s.deviceId, status: "warn", message: s.deviceIdMissing });
1840
1899
  let targets = config.targets ?? [];
1841
1900
  if (targets.length === 0)
1842
- checks.push({ name: "\u4E0A\u62A5\u76EE\u6807", status: "warn", message: "\u672A\u914D\u7F6E\uFF0C\u8BF7\u6267\u884C aiusage enroll" });
1901
+ checks.push({ name: s.targets, status: "warn", message: s.targetMissing });
1843
1902
  else
1844
1903
  for (let target of targets) {
1845
1904
  let prefix = `[${target.name}]`;
1846
- target.deviceToken ? checks.push({ name: `${prefix} \u8BBE\u5907\u4EE4\u724C`, status: "ok", message: `${target.deviceToken.slice(0, 12)}\u2026` }) : checks.push({ name: `${prefix} \u8BBE\u5907\u4EE4\u724C`, status: "fail", message: "\u672A\u6CE8\u518C" });
1905
+ target.deviceToken ? checks.push({ name: `${prefix} ${s.deviceToken}`, status: "ok", message: `${target.deviceToken.slice(0, 12)}\u2026` }) : checks.push({ name: `${prefix} ${s.deviceToken}`, status: "fail", message: s.tokenMissing });
1847
1906
  try {
1848
1907
  let health = await fetchHealth(target.apiBaseUrl);
1849
- checks.push({ name: `${prefix} \u670D\u52A1\u7AEF`, status: "ok", message: health.siteId });
1908
+ checks.push({ name: `${prefix} ${s.server}`, status: "ok", message: health.siteId });
1850
1909
  } catch (err) {
1851
- checks.push({ name: `${prefix} \u670D\u52A1\u7AEF`, status: "fail", message: err instanceof Error ? err.message : String(err) });
1910
+ checks.push({ name: `${prefix} ${s.server}`, status: "fail", message: err instanceof Error ? err.message : String(err) });
1852
1911
  }
1853
- target.lastSuccessfulUploadAt ? checks.push({ name: `${prefix} \u4E0A\u6B21\u540C\u6B65`, status: "ok", message: target.lastSuccessfulUploadAt }) : checks.push({ name: `${prefix} \u4E0A\u6B21\u540C\u6B65`, status: "warn", message: "\u6682\u65E0\u8BB0\u5F55" });
1912
+ target.lastSuccessfulUploadAt ? checks.push({ name: `${prefix} ${s.lastSync}`, status: "ok", message: target.lastSuccessfulUploadAt }) : checks.push({ name: `${prefix} ${s.lastSync}`, status: "warn", message: s.lastSyncNone });
1854
1913
  }
1855
- let scannerDirs = [
1856
- [join14(homedir14(), ".claude", "projects"), "Claude \u65E5\u5FD7"],
1857
- [join14(homedir14(), ".codex"), "Codex \u65E5\u5FD7"]
1914
+ let home = homedir14(), tools = [
1915
+ { dir: join14(home, ".claude", "projects"), label: "Claude Code", exts: [".jsonl"] },
1916
+ { dir: join14(home, ".codex"), label: "Codex CLI", exts: [".jsonl"] },
1917
+ { dir: join14(home, ".copilot", "session-state"), label: "Copilot CLI", exts: [".jsonl"] },
1918
+ { dir: join14(home, ".gemini", "tmp"), label: "Gemini CLI", exts: [".json"] },
1919
+ { dir: join14(home, ".qwen", "tmp"), label: "Qwen Code", exts: [".jsonl"] },
1920
+ { dir: join14(home, ".kimi", "sessions"), label: "Kimi Code", exts: [".jsonl"] },
1921
+ { dir: join14(home, ".local", "share", "amp", "threads"), label: "Amp", exts: [".json"] },
1922
+ { dir: join14(home, ".factory", "sessions"), label: "Droid", exts: [".jsonl", ".json"] },
1923
+ { dir: join14(home, ".local", "share", "opencode"), label: "OpenCode", exts: [".json"] },
1924
+ { dir: join14(home, ".pi", "agent", "sessions"), label: "Pi", exts: [".jsonl"] }
1858
1925
  ];
1859
- for (let [dir, label] of scannerDirs)
1926
+ for (let tool of tools) {
1860
1927
  try {
1861
- await stat2(dir), checks.push({ name: label, status: "ok", message: dir });
1928
+ await stat2(tool.dir);
1862
1929
  } catch {
1863
- checks.push({ name: label, status: "warn", message: "\u76EE\u5F55\u4E0D\u5B58\u5728\uFF08\u672A\u4F7F\u7528\u53EF\u5FFD\u7565\uFF09" });
1930
+ checks.push({ name: tool.label, status: "warn", message: s.notInstalled });
1931
+ continue;
1864
1932
  }
1933
+ let n = await countFiles(tool.dir, tool.exts);
1934
+ n > 0 ? checks.push({ name: tool.label, status: "ok", message: s.hasData(n) }) : checks.push({ name: tool.label, status: "warn", message: s.installedNoData });
1935
+ }
1865
1936
  let schedule = await getScheduleStatus();
1866
1937
  return schedule.enabled ? checks.push({
1867
- name: "\u5B9A\u65F6\u540C\u6B65",
1938
+ name: s.schedule,
1868
1939
  status: "ok",
1869
- message: schedule.intervalLabel ? `\u6BCF ${schedule.intervalLabel}` : "\u5DF2\u542F\u7528"
1870
- }) : checks.push({ name: "\u5B9A\u65F6\u540C\u6B65", status: "warn", message: "\u672A\u542F\u7528\uFF0C\u53EF\u6267\u884C aiusage schedule on" }), checks;
1940
+ message: schedule.intervalLabel ? `${s.scheduleEvery} ${schedule.intervalLabel}` : s.scheduleEnabled
1941
+ }) : checks.push({ name: s.schedule, status: "warn", message: s.scheduleOff }), checks;
1871
1942
  }
1872
1943
 
1873
1944
  // src/cli.ts
@@ -1906,7 +1977,10 @@ try {
1906
1977
  let parsed = parseArgs(argv.slice(1));
1907
1978
  await runSchedule("on", parsed.flags);
1908
1979
  }
1909
- } else command === "doctor" ? await runDoctorCommand() : command === "config" && argv[1] === "set" ? await runConfigSet(argv.slice(2)) : command === "setup" ? (console.log(`To deploy the server, clone the repo and run the setup wizard:
1980
+ } else if (command === "doctor") {
1981
+ let parsed = parseArgs(argv.slice(1));
1982
+ await runDoctorCommand(parsed.flags);
1983
+ } else command === "config" && argv[1] === "set" ? await runConfigSet(argv.slice(2)) : command === "setup" ? (console.log(`To deploy the server, clone the repo and run the setup wizard:
1910
1984
  `), console.log(" git clone https://github.com/ennann/aiusage.git"), console.log(" cd aiusage && pnpm install"), console.log(` pnpm setup
1911
1985
  `), console.log("See: https://github.com/ennann/aiusage#deploy-your-own-server")) : printHelp();
1912
1986
  } catch (error) {
@@ -2047,8 +2121,8 @@ async function runSchedule(sub, flags) {
2047
2121
  status.enabled ? (console.log("\u72B6\u6001: \u5DF2\u542F\u7528"), status.intervalLabel && console.log(`\u95F4\u9694: \u6BCF ${status.intervalLabel}`), console.log(`\u542B\u4ECA\u65E5: ${status.includeToday ? "\u662F" : "\u5426"}`), status.command && console.log(`\u547D\u4EE4: ${status.command}`), status.path && console.log(`\u914D\u7F6E: ${status.path}`), status.logPath && console.log(`\u65E5\u5FD7: ${status.logPath}`)) : (console.log("\u72B6\u6001: \u672A\u542F\u7528"), console.log("\u542F\u7528: aiusage schedule on [--every 5m]"));
2048
2122
  }
2049
2123
  }
2050
- async function runDoctorCommand() {
2051
- let checks = await runDoctor();
2124
+ async function runDoctorCommand(flags) {
2125
+ let config = await readConfig(), lang = (typeof flags.lang == "string" ? flags.lang : config.lang) || "en", checks = await runDoctor(lang);
2052
2126
  for (let check of checks) {
2053
2127
  let icon = check.status === "ok" ? "\u2713" : check.status === "warn" ? "\u26A0" : "\u2717";
2054
2128
  console.log(`${icon} ${check.name}: ${check.message}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiusage/cli",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "Track and sync AI tool token usage across devices.",
5
5
  "license": "MIT",
6
6
  "private": false,