@inferhub/usage 0.1.8 → 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.
- package/bin/statusline.mjs +70 -47
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/session-id.d.ts +19 -0
- package/dist/session-id.js +103 -0
- package/package.json +1 -1
- package/src/index.ts +5 -0
- package/src/session-id.ts +106 -0
package/bin/statusline.mjs
CHANGED
|
@@ -1,48 +1,71 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
try {
|
|
19
|
-
|
|
20
|
-
} catch {
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export type { AccountUsage, FetchUsageOptions, ResolvedAuth, UsageWindowKind, } from "./types.js";
|
|
2
2
|
export { DEFAULT_BASE_URL, fetchAccountUsage, fetchAccountUsageAuto, normalizeBaseUrl, resolveAuth, usageUrl, } from "./client.js";
|
|
3
3
|
export { formatReport, formatStatusLine, money, shortModel, tokens } from "./format.js";
|
|
4
|
+
export { deriveConversationSessionId, messagesFromTranscript, sessionIdFromClaudeStatuslineInput, } from "./session-id.js";
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export { DEFAULT_BASE_URL, fetchAccountUsage, fetchAccountUsageAuto, normalizeBaseUrl, resolveAuth, usageUrl, } from "./client.js";
|
|
2
2
|
export { formatReport, formatStatusLine, money, shortModel, tokens } from "./format.js";
|
|
3
|
+
export { deriveConversationSessionId, messagesFromTranscript, sessionIdFromClaudeStatuslineInput, } from "./session-id.js";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Same algorithm as proxy sessionid.conversationPrefix + sha256[:16]. */
|
|
2
|
+
export declare function deriveConversationSessionId(messages: Array<{
|
|
3
|
+
role?: string;
|
|
4
|
+
content?: unknown;
|
|
5
|
+
type?: string;
|
|
6
|
+
}>): string | null;
|
|
7
|
+
/**
|
|
8
|
+
* Best-effort extract ordered messages from a Claude transcript jsonl.
|
|
9
|
+
* Handles common shapes: {type:"user"|"assistant", message:{role,content}} etc.
|
|
10
|
+
*/
|
|
11
|
+
export declare function messagesFromTranscript(transcriptPath: string): Array<{
|
|
12
|
+
role?: string;
|
|
13
|
+
content?: unknown;
|
|
14
|
+
type?: string;
|
|
15
|
+
}>;
|
|
16
|
+
export declare function sessionIdFromClaudeStatuslineInput(input: {
|
|
17
|
+
session_id?: string;
|
|
18
|
+
transcript_path?: string;
|
|
19
|
+
}): string | null;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Match proxy/internal/sessionid.Derive conversation-prefix hashing so the
|
|
3
|
+
* Claude statusline can query the same session_id that request_logs stores.
|
|
4
|
+
*
|
|
5
|
+
* Priority mirrors the proxy:
|
|
6
|
+
* 1. Explicit session id (Claude stdin session_id) if we ever store it server-side
|
|
7
|
+
* 2. sha256(system + first user message)[:16] hex — same as proxy
|
|
8
|
+
*/
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
11
|
+
function flattenContent(content) {
|
|
12
|
+
if (content == null)
|
|
13
|
+
return "";
|
|
14
|
+
if (typeof content === "string")
|
|
15
|
+
return content;
|
|
16
|
+
if (Array.isArray(content)) {
|
|
17
|
+
return content.map(flattenContent).join("");
|
|
18
|
+
}
|
|
19
|
+
if (typeof content === "object") {
|
|
20
|
+
const o = content;
|
|
21
|
+
if (typeof o.text === "string")
|
|
22
|
+
return o.text;
|
|
23
|
+
if (o.content != null)
|
|
24
|
+
return flattenContent(o.content);
|
|
25
|
+
}
|
|
26
|
+
return "";
|
|
27
|
+
}
|
|
28
|
+
/** Same algorithm as proxy sessionid.conversationPrefix + sha256[:16]. */
|
|
29
|
+
export function deriveConversationSessionId(messages) {
|
|
30
|
+
let system = "";
|
|
31
|
+
let firstUser = "";
|
|
32
|
+
for (const m of messages) {
|
|
33
|
+
const role = (m.role || m.type || "").toLowerCase();
|
|
34
|
+
if (role === "system") {
|
|
35
|
+
const t = flattenContent(m.content);
|
|
36
|
+
if (t)
|
|
37
|
+
system += t + "\n";
|
|
38
|
+
}
|
|
39
|
+
else if (role === "user" || role === "human") {
|
|
40
|
+
firstUser = flattenContent(m.content);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (!firstUser)
|
|
45
|
+
return null;
|
|
46
|
+
const prefix = system + firstUser;
|
|
47
|
+
const sum = createHash("sha256").update(prefix, "utf8").digest("hex");
|
|
48
|
+
return sum.slice(0, 32); // 16 bytes = 32 hex chars (proxy uses sum[:16] bytes)
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Best-effort extract ordered messages from a Claude transcript jsonl.
|
|
52
|
+
* Handles common shapes: {type:"user"|"assistant", message:{role,content}} etc.
|
|
53
|
+
*/
|
|
54
|
+
export function messagesFromTranscript(transcriptPath) {
|
|
55
|
+
if (!transcriptPath || !existsSync(transcriptPath))
|
|
56
|
+
return [];
|
|
57
|
+
let text;
|
|
58
|
+
try {
|
|
59
|
+
text = readFileSync(transcriptPath, "utf8");
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
const out = [];
|
|
65
|
+
for (const line of text.split(/\r?\n/)) {
|
|
66
|
+
if (!line.trim())
|
|
67
|
+
continue;
|
|
68
|
+
try {
|
|
69
|
+
const row = JSON.parse(line);
|
|
70
|
+
// Claude Code transcript variants
|
|
71
|
+
if (row.type === "user" || row.type === "assistant" || row.type === "system") {
|
|
72
|
+
const msg = row.message || row;
|
|
73
|
+
out.push({
|
|
74
|
+
role: String(msg.role || row.type),
|
|
75
|
+
content: msg.content ?? row.content,
|
|
76
|
+
});
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (typeof row.role === "string" && row.content != null) {
|
|
80
|
+
out.push({ role: row.role, content: row.content });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
/* skip bad lines */
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
export function sessionIdFromClaudeStatuslineInput(input) {
|
|
90
|
+
// Prefer Claude UUID — proxy now stores metadata.user_id / session_id when
|
|
91
|
+
// present, so each Claude conversation keeps its own counter.
|
|
92
|
+
if (input.session_id && input.session_id.length > 8)
|
|
93
|
+
return input.session_id;
|
|
94
|
+
// Fallback: conversation-prefix hash (matches proxy sessionid.Derive when no
|
|
95
|
+
// explicit session was sent).
|
|
96
|
+
if (input.transcript_path) {
|
|
97
|
+
const msgs = messagesFromTranscript(input.transcript_path);
|
|
98
|
+
const derived = deriveConversationSessionId(msgs);
|
|
99
|
+
if (derived)
|
|
100
|
+
return derived;
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -13,3 +13,8 @@ export {
|
|
|
13
13
|
usageUrl,
|
|
14
14
|
} from "./client.js";
|
|
15
15
|
export { formatReport, formatStatusLine, money, shortModel, tokens } from "./format.js";
|
|
16
|
+
export {
|
|
17
|
+
deriveConversationSessionId,
|
|
18
|
+
messagesFromTranscript,
|
|
19
|
+
sessionIdFromClaudeStatuslineInput,
|
|
20
|
+
} from "./session-id.js";
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Match proxy/internal/sessionid.Derive conversation-prefix hashing so the
|
|
3
|
+
* Claude statusline can query the same session_id that request_logs stores.
|
|
4
|
+
*
|
|
5
|
+
* Priority mirrors the proxy:
|
|
6
|
+
* 1. Explicit session id (Claude stdin session_id) if we ever store it server-side
|
|
7
|
+
* 2. sha256(system + first user message)[:16] hex — same as proxy
|
|
8
|
+
*/
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
11
|
+
|
|
12
|
+
function flattenContent(content: unknown): string {
|
|
13
|
+
if (content == null) return "";
|
|
14
|
+
if (typeof content === "string") return content;
|
|
15
|
+
if (Array.isArray(content)) {
|
|
16
|
+
return content.map(flattenContent).join("");
|
|
17
|
+
}
|
|
18
|
+
if (typeof content === "object") {
|
|
19
|
+
const o = content as { text?: unknown; content?: unknown };
|
|
20
|
+
if (typeof o.text === "string") return o.text;
|
|
21
|
+
if (o.content != null) return flattenContent(o.content);
|
|
22
|
+
}
|
|
23
|
+
return "";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Same algorithm as proxy sessionid.conversationPrefix + sha256[:16]. */
|
|
27
|
+
export function deriveConversationSessionId(messages: Array<{
|
|
28
|
+
role?: string;
|
|
29
|
+
content?: unknown;
|
|
30
|
+
type?: string;
|
|
31
|
+
}>): string | null {
|
|
32
|
+
let system = "";
|
|
33
|
+
let firstUser = "";
|
|
34
|
+
for (const m of messages) {
|
|
35
|
+
const role = (m.role || m.type || "").toLowerCase();
|
|
36
|
+
if (role === "system") {
|
|
37
|
+
const t = flattenContent(m.content);
|
|
38
|
+
if (t) system += t + "\n";
|
|
39
|
+
} else if (role === "user" || role === "human") {
|
|
40
|
+
firstUser = flattenContent(m.content);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (!firstUser) return null;
|
|
45
|
+
const prefix = system + firstUser;
|
|
46
|
+
const sum = createHash("sha256").update(prefix, "utf8").digest("hex");
|
|
47
|
+
return sum.slice(0, 32); // 16 bytes = 32 hex chars (proxy uses sum[:16] bytes)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Best-effort extract ordered messages from a Claude transcript jsonl.
|
|
52
|
+
* Handles common shapes: {type:"user"|"assistant", message:{role,content}} etc.
|
|
53
|
+
*/
|
|
54
|
+
export function messagesFromTranscript(transcriptPath: string): Array<{
|
|
55
|
+
role?: string;
|
|
56
|
+
content?: unknown;
|
|
57
|
+
type?: string;
|
|
58
|
+
}> {
|
|
59
|
+
if (!transcriptPath || !existsSync(transcriptPath)) return [];
|
|
60
|
+
let text: string;
|
|
61
|
+
try {
|
|
62
|
+
text = readFileSync(transcriptPath, "utf8");
|
|
63
|
+
} catch {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
const out: Array<{ role?: string; content?: unknown; type?: string }> = [];
|
|
67
|
+
for (const line of text.split(/\r?\n/)) {
|
|
68
|
+
if (!line.trim()) continue;
|
|
69
|
+
try {
|
|
70
|
+
const row = JSON.parse(line) as Record<string, unknown>;
|
|
71
|
+
// Claude Code transcript variants
|
|
72
|
+
if (row.type === "user" || row.type === "assistant" || row.type === "system") {
|
|
73
|
+
const msg = (row.message as Record<string, unknown>) || row;
|
|
74
|
+
out.push({
|
|
75
|
+
role: String(msg.role || row.type),
|
|
76
|
+
content: msg.content ?? row.content,
|
|
77
|
+
});
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (typeof row.role === "string" && row.content != null) {
|
|
81
|
+
out.push({ role: row.role, content: row.content });
|
|
82
|
+
}
|
|
83
|
+
} catch {
|
|
84
|
+
/* skip bad lines */
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function sessionIdFromClaudeStatuslineInput(input: {
|
|
91
|
+
session_id?: string;
|
|
92
|
+
transcript_path?: string;
|
|
93
|
+
}): string | null {
|
|
94
|
+
// Prefer Claude UUID — proxy now stores metadata.user_id / session_id when
|
|
95
|
+
// present, so each Claude conversation keeps its own counter.
|
|
96
|
+
if (input.session_id && input.session_id.length > 8) return input.session_id;
|
|
97
|
+
|
|
98
|
+
// Fallback: conversation-prefix hash (matches proxy sessionid.Derive when no
|
|
99
|
+
// explicit session was sent).
|
|
100
|
+
if (input.transcript_path) {
|
|
101
|
+
const msgs = messagesFromTranscript(input.transcript_path);
|
|
102
|
+
const derived = deriveConversationSessionId(msgs);
|
|
103
|
+
if (derived) return derived;
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|