@tokenboard/cli 0.1.1 → 0.1.2
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/cli.js +39 -11
- package/dist/cli.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -793,6 +793,7 @@ function ccusageDailyToRecords(source, daily) {
|
|
|
793
793
|
|
|
794
794
|
// src/collectors/ccusage-source.ts
|
|
795
795
|
var SPAWN_TIMEOUT_MS = 3e4;
|
|
796
|
+
var CLAUDE_AGENT = "claude";
|
|
796
797
|
function hasNpx() {
|
|
797
798
|
try {
|
|
798
799
|
const r = spawnSync("npx", ["--version"], { stdio: "ignore" });
|
|
@@ -801,11 +802,9 @@ function hasNpx() {
|
|
|
801
802
|
return false;
|
|
802
803
|
}
|
|
803
804
|
}
|
|
804
|
-
function
|
|
805
|
+
function runCcusage(args) {
|
|
805
806
|
return new Promise((resolve, reject) => {
|
|
806
|
-
const child = spawn("npx", ["-y", "ccusage@20",
|
|
807
|
-
stdio: ["ignore", "pipe", "ignore"]
|
|
808
|
-
});
|
|
807
|
+
const child = spawn("npx", ["-y", "ccusage@20", ...args], { stdio: ["ignore", "pipe", "ignore"] });
|
|
809
808
|
let stdout = "";
|
|
810
809
|
const killer = setTimeout(() => child.kill("SIGKILL"), SPAWN_TIMEOUT_MS);
|
|
811
810
|
child.stdout.on("data", (chunk2) => {
|
|
@@ -818,22 +817,51 @@ function runSource(source) {
|
|
|
818
817
|
child.on("close", (code) => {
|
|
819
818
|
clearTimeout(killer);
|
|
820
819
|
if (code !== 0) {
|
|
821
|
-
reject(new Error(`ccusage ${
|
|
820
|
+
reject(new Error(`ccusage ${args.join(" ")} exited ${code}`));
|
|
822
821
|
return;
|
|
823
822
|
}
|
|
824
|
-
|
|
825
|
-
const parsed = JSON.parse(stdout);
|
|
826
|
-
resolve(parsed.daily ?? []);
|
|
827
|
-
} catch (err) {
|
|
828
|
-
reject(err);
|
|
829
|
-
}
|
|
823
|
+
resolve(stdout);
|
|
830
824
|
});
|
|
831
825
|
});
|
|
832
826
|
}
|
|
827
|
+
function runSource(source) {
|
|
828
|
+
return runCcusage([source, "daily", "--json", "--offline"]).then((stdout) => {
|
|
829
|
+
const parsed = JSON.parse(stdout);
|
|
830
|
+
return parsed.daily ?? [];
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
function agentsFromDailyPayload(parsed) {
|
|
834
|
+
const rows = typeof parsed === "object" && parsed !== null && Array.isArray(parsed.daily) ? parsed.daily : [];
|
|
835
|
+
const agents = /* @__PURE__ */ new Set();
|
|
836
|
+
for (const row2 of rows) {
|
|
837
|
+
const list = row2?.metadata?.agents;
|
|
838
|
+
if (Array.isArray(list)) {
|
|
839
|
+
for (const a of list) if (typeof a === "string") agents.add(a.toLowerCase());
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
if (rows.length > 0 && agents.size === 0) return null;
|
|
843
|
+
return agents;
|
|
844
|
+
}
|
|
845
|
+
async function detectAgentsWithData() {
|
|
846
|
+
try {
|
|
847
|
+
const stdout = await runCcusage(["daily", "--json", "--offline"]);
|
|
848
|
+
return agentsFromDailyPayload(JSON.parse(stdout));
|
|
849
|
+
} catch {
|
|
850
|
+
return null;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
function canSkipFanOut(agents) {
|
|
854
|
+
if (agents === null) return false;
|
|
855
|
+
return [...agents].every((a) => a === CLAUDE_AGENT);
|
|
856
|
+
}
|
|
833
857
|
async function collectCcusage() {
|
|
834
858
|
if (!hasNpx()) {
|
|
835
859
|
return { records: [], skipped: [], npxAvailable: false };
|
|
836
860
|
}
|
|
861
|
+
const agents = await detectAgentsWithData();
|
|
862
|
+
if (canSkipFanOut(agents)) {
|
|
863
|
+
return { records: [], skipped: [], npxAvailable: true };
|
|
864
|
+
}
|
|
837
865
|
const records = [];
|
|
838
866
|
const skipped = [];
|
|
839
867
|
await Promise.all(
|