@inferhub/usage 0.1.9 → 0.1.10

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/bin/statusline.mjs +70 -65
  2. package/package.json +1 -1
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inferhub/usage",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "Shared InferHub account usage client for coding-agent plugins",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",