@inferhub/usage 0.1.9 → 0.1.11

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.
@@ -1,66 +1,71 @@
1
1
  #!/usr/bin/env node
2
- /**
3
- * Claude Code statusline — one compact line.
4
- *
5
- * Session id strategy:
6
- * - Prefer conversation-prefix hash from transcript (matches proxy sessionid.Derive)
7
- * - Also try Claude's session_id (if proxy stored user/metadata session)
8
- * - Never fall back to "latest account session" (that jumps between chats)
9
- */
10
- import { readFileSync } from "node:fs";
11
- import { dirname, join } from "node:path";
12
- import { fileURLToPath, pathToFileURL } from "node:url";
13
-
14
- const root = dirname(fileURLToPath(import.meta.url));
15
- const client = await import(pathToFileURL(join(root, "../dist/index.js")).href);
16
-
17
- function readStdin() {
18
- try {
19
- return readFileSync(0, "utf8");
20
- } catch {
21
- return "{}";
22
- }
23
- }
24
-
25
- let input = {};
26
- try {
27
- input = JSON.parse(readStdin() || "{}");
28
- } catch {
29
- input = {};
30
- }
31
-
32
- try {
33
- const sessionId = client.sessionIdFromClaudeStatuslineInput({
34
- session_id: input.session_id,
35
- transcript_path: input.transcript_path,
36
- });
37
-
38
- // Fetch with explicit session when known; otherwise omit session block rather
39
- // than showing the account's latest unrelated conversation.
40
- const { usage } = await client.fetchAccountUsageAuto({
41
- window: "day",
42
- sessionId: sessionId || undefined,
43
- });
44
-
45
- // If we couldn't derive a session id, strip session so we don't show wrong data
46
- if (!sessionId) {
47
- usage.session = null;
48
- } else if (usage.session && usage.session.id !== sessionId) {
49
- // API returned a different session (e.g. latest) — hide it
50
- usage.session = null;
51
- }
52
-
53
- const contextPct =
54
- typeof input?.context_window?.used_percentage === "number"
55
- ? input.context_window.used_percentage
56
- : null;
57
- const model = input?.model?.display_name || input?.model?.id || null;
58
-
59
- process.stdout.write(
60
- client.formatStatusLine(usage, { contextPct, model }) + "\n",
61
- );
62
- } catch (e) {
63
- const msg = e instanceof Error ? e.message : String(e);
64
- process.stdout.write("InferHub · offline (" + msg.slice(0, 40) + ")\n");
65
- process.exitCode = 0;
66
- }
2
+ /**
3
+ * Claude Code statusline — one compact line.
4
+ *
5
+ * Session id strategy:
6
+ * - Prefer conversation-prefix hash from transcript (matches proxy sessionid.Derive)
7
+ * - Also try Claude's session_id (if proxy stored user/metadata session)
8
+ * - Never fall back to "latest account session" (that jumps between chats)
9
+ */
10
+ import { readFileSync } from "node:fs";
11
+ import { dirname, join } from "node:path";
12
+ import { fileURLToPath, pathToFileURL } from "node:url";
13
+
14
+ const root = dirname(fileURLToPath(import.meta.url));
15
+ const client = await import(pathToFileURL(join(root, "../dist/index.js")).href);
16
+
17
+ function readStdin() {
18
+ try {
19
+ return readFileSync(0, "utf8");
20
+ } catch {
21
+ return "{}";
22
+ }
23
+ }
24
+
25
+ let input = {};
26
+ try {
27
+ input = JSON.parse(readStdin() || "{}");
28
+ } catch {
29
+ input = {};
30
+ }
31
+
32
+ try {
33
+ const sessionId = client.sessionIdFromClaudeStatuslineInput({
34
+ session_id: input.session_id,
35
+ transcript_path: input.transcript_path,
36
+ });
37
+
38
+ // Fetch with explicit session when known; otherwise omit session block rather
39
+ // than showing the account's latest unrelated conversation.
40
+ const { usage } = await client.fetchAccountUsageAuto({
41
+ window: "day",
42
+ sessionId: sessionId || undefined,
43
+ });
44
+
45
+ // If we couldn't derive a session id, strip session so we don't show wrong data.
46
+ // Accept exact id or legacy JSON blob that contains this session UUID.
47
+ if (!sessionId) {
48
+ usage.session = null;
49
+ } else if (usage.session) {
50
+ const got = String(usage.session.id || "");
51
+ if (got !== sessionId && !got.includes(sessionId)) {
52
+ usage.session = null;
53
+ } else {
54
+ usage.session.id = sessionId; // normalize display/id for formatters
55
+ }
56
+ }
57
+
58
+ const contextPct =
59
+ typeof input?.context_window?.used_percentage === "number"
60
+ ? input.context_window.used_percentage
61
+ : null;
62
+ const model = input?.model?.display_name || input?.model?.id || null;
63
+
64
+ process.stdout.write(
65
+ client.formatStatusLine(usage, { contextPct, model }) + "\n",
66
+ );
67
+ } catch (e) {
68
+ const msg = e instanceof Error ? e.message : String(e);
69
+ process.stdout.write("InferHub · offline (" + msg.slice(0, 40) + ")\n");
70
+ process.exitCode = 0;
71
+ }
@@ -7,7 +7,7 @@ test("normalizeBaseUrl adds /v1", () => {
7
7
  test("usageUrl", () => {
8
8
  assert.equal(usageUrl("https://api.inferhub.dev", "day", "UTC", "abc"), "https://api.inferhub.dev/v1/me/usage?window=day&tz=UTC&session_id=abc");
9
9
  });
10
- test("formatStatusLine includes tokens compactly", () => {
10
+ test("formatStatusLine has sess/day/all/bal without top model", () => {
11
11
  const usage = {
12
12
  object: "account.usage",
13
13
  currency: "USDC",
@@ -49,12 +49,9 @@ test("formatStatusLine includes tokens compactly", () => {
49
49
  cache: { cached: false, ttl_seconds: 30 },
50
50
  };
51
51
  const line = formatStatusLine(usage, { model: "grok-4.5", contextPct: 8 });
52
- assert.equal(line, "InferHub · sess 19r/6.0M · day 129r/26.4M · all $0.41/3.9kr/354.2M · bal $1.31 · top gpt-5.5");
53
- assert.ok(line.length < 100, `too long: ${line.length} ${line}`);
54
- assert.match(line, /3\.9kr/);
55
- assert.match(line, /354\.2M/);
56
- assert.doesNotMatch(line, /3933r/);
57
- // empty session omitted
52
+ assert.equal(line, "InferHub · sess 19r/6.0M · day 129r/26.4M · all $0.41/3.9kr/354.2M · bal $1.31");
53
+ assert.doesNotMatch(line, /top /);
54
+ assert.doesNotMatch(line, /gpt-5\.5/);
58
55
  const noSess = { ...usage, session: null };
59
56
  assert.doesNotMatch(formatStatusLine(noSess), /sess /);
60
57
  assert.equal(money("0"), "$0");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inferhub/usage",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "Shared InferHub account usage client for coding-agent plugins",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -14,7 +14,7 @@ test("usageUrl", () => {
14
14
  );
15
15
  });
16
16
 
17
- test("formatStatusLine includes tokens compactly", () => {
17
+ test("formatStatusLine has sess/day/all/bal without top model", () => {
18
18
  const usage: AccountUsage = {
19
19
  object: "account.usage",
20
20
  currency: "USDC",
@@ -59,14 +59,11 @@ test("formatStatusLine includes tokens compactly", () => {
59
59
  const line = formatStatusLine(usage, { model: "grok-4.5", contextPct: 8 });
60
60
  assert.equal(
61
61
  line,
62
- "InferHub · sess 19r/6.0M · day 129r/26.4M · all $0.41/3.9kr/354.2M · bal $1.31 · top gpt-5.5",
62
+ "InferHub · sess 19r/6.0M · day 129r/26.4M · all $0.41/3.9kr/354.2M · bal $1.31",
63
63
  );
64
- assert.ok(line.length < 100, `too long: ${line.length} ${line}`);
65
- assert.match(line, /3\.9kr/);
66
- assert.match(line, /354\.2M/);
67
- assert.doesNotMatch(line, /3933r/);
64
+ assert.doesNotMatch(line, /top /);
65
+ assert.doesNotMatch(line, /gpt-5\.5/);
68
66
 
69
- // empty session omitted
70
67
  const noSess = { ...usage, session: null };
71
68
  assert.doesNotMatch(formatStatusLine(noSess), /sess /);
72
69