@inferhub/usage 0.1.6 → 0.1.7
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/format.d.ts +4 -2
- package/dist/format.js +24 -20
- package/dist/format.test.js +10 -19
- package/package.json +1 -1
- package/src/format.test.ts +10 -20
- package/src/format.ts +27 -18
package/dist/format.d.ts
CHANGED
|
@@ -3,10 +3,12 @@ export declare function money(usdc: string | number | null | undefined, digits?:
|
|
|
3
3
|
export declare function tokens(n: number | null | undefined): string;
|
|
4
4
|
export declare function shortModel(model: string | null | undefined): string;
|
|
5
5
|
/**
|
|
6
|
-
* One-line Claude statusline
|
|
6
|
+
* One-line Claude statusline.
|
|
7
7
|
*
|
|
8
8
|
* Example:
|
|
9
|
-
* InferHub · sess
|
|
9
|
+
* InferHub · sess 4r/1.2M · day 135r/26.4M · all $0.41/3.9k r/354M · bal $1.31 · top gpt-5.5
|
|
10
|
+
*
|
|
11
|
+
* Keep under ~90 chars when possible; tokens use k/M/B.
|
|
10
12
|
*/
|
|
11
13
|
export declare function formatStatusLine(usage: AccountUsage, extras?: {
|
|
12
14
|
sessionCostUsd?: number | null;
|
package/dist/format.js
CHANGED
|
@@ -6,12 +6,12 @@ export function money(usdc, digits = 2) {
|
|
|
6
6
|
return "$0";
|
|
7
7
|
if (Math.abs(n) < 0.01)
|
|
8
8
|
return `$${n.toFixed(4)}`;
|
|
9
|
-
if (Math.abs(n) < 1)
|
|
10
|
-
return `$${n.toFixed(2)}`;
|
|
11
9
|
return `$${n.toFixed(2)}`;
|
|
12
10
|
}
|
|
13
11
|
export function tokens(n) {
|
|
14
12
|
const v = Number(n ?? 0);
|
|
13
|
+
if (v >= 1_000_000_000)
|
|
14
|
+
return `${(v / 1_000_000_000).toFixed(1)}B`;
|
|
15
15
|
if (v >= 1_000_000)
|
|
16
16
|
return `${(v / 1_000_000).toFixed(1)}M`;
|
|
17
17
|
if (v >= 1_000)
|
|
@@ -24,36 +24,40 @@ export function shortModel(model) {
|
|
|
24
24
|
const parts = model.split("/");
|
|
25
25
|
return parts[parts.length - 1] || model;
|
|
26
26
|
}
|
|
27
|
-
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* - billed + wantReq: "$0.12/5r" when showReq=true
|
|
27
|
+
/**
|
|
28
|
+
* Compact metric: "4r/1.2M" free, or "$0.12/5r/80k" when billed.
|
|
29
|
+
* Always includes tokens when > 0.
|
|
31
30
|
*/
|
|
32
|
-
function compact(spendUsdc, requests,
|
|
31
|
+
function compact(spendUsdc, requests, totalTokens) {
|
|
33
32
|
const spend = Number(spendUsdc ?? 0);
|
|
34
33
|
const req = Number(requests ?? 0);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return `${money(spend)}/${req}r`;
|
|
38
|
-
return money(spend);
|
|
39
|
-
}
|
|
40
|
-
if (req <= 0)
|
|
34
|
+
const tok = Number(totalTokens ?? 0);
|
|
35
|
+
if (req <= 0 && tok <= 0 && spend <= 0)
|
|
41
36
|
return "—";
|
|
42
|
-
|
|
37
|
+
const bits = [];
|
|
38
|
+
if (spend > 0)
|
|
39
|
+
bits.push(money(spend));
|
|
40
|
+
if (req > 0)
|
|
41
|
+
bits.push(`${req}r`);
|
|
42
|
+
if (tok > 0)
|
|
43
|
+
bits.push(tokens(tok));
|
|
44
|
+
return bits.join("/") || "—";
|
|
43
45
|
}
|
|
44
46
|
/**
|
|
45
|
-
* One-line Claude statusline
|
|
47
|
+
* One-line Claude statusline.
|
|
46
48
|
*
|
|
47
49
|
* Example:
|
|
48
|
-
* InferHub · sess
|
|
50
|
+
* InferHub · sess 4r/1.2M · day 135r/26.4M · all $0.41/3.9k r/354M · bal $1.31 · top gpt-5.5
|
|
51
|
+
*
|
|
52
|
+
* Keep under ~90 chars when possible; tokens use k/M/B.
|
|
49
53
|
*/
|
|
50
54
|
export function formatStatusLine(usage, extras) {
|
|
51
55
|
const parts = ["InferHub"];
|
|
52
|
-
if (usage.session && (usage.session.requests > 0 || Number(usage.session.spend_usdc) > 0)) {
|
|
53
|
-
parts.push(`sess ${compact(usage.session.spend_usdc, usage.session.requests)}`);
|
|
56
|
+
if (usage.session && (usage.session.requests > 0 || Number(usage.session.spend_usdc) > 0 || usage.session.total_tokens > 0)) {
|
|
57
|
+
parts.push(`sess ${compact(usage.session.spend_usdc, usage.session.requests, usage.session.total_tokens)}`);
|
|
54
58
|
}
|
|
55
|
-
parts.push(`day ${compact(usage.window.spend_usdc, usage.window.requests)}`);
|
|
56
|
-
parts.push(`all ${compact(usage.all_time.spend_usdc, usage.all_time.requests)}`);
|
|
59
|
+
parts.push(`day ${compact(usage.window.spend_usdc, usage.window.requests, usage.window.total_tokens)}`);
|
|
60
|
+
parts.push(`all ${compact(usage.all_time.spend_usdc, usage.all_time.requests, usage.all_time.total_tokens)}`);
|
|
57
61
|
parts.push(`bal ${money(usage.balance.amount_usdc)}`);
|
|
58
62
|
const top = shortModel(usage.top_model?.model);
|
|
59
63
|
if (top && top !== "—")
|
package/dist/format.test.js
CHANGED
|
@@ -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
|
|
10
|
+
test("formatStatusLine includes tokens compactly", () => {
|
|
11
11
|
const usage = {
|
|
12
12
|
object: "account.usage",
|
|
13
13
|
currency: "USDC",
|
|
@@ -48,23 +48,14 @@ test("formatStatusLine is compact one-liner", () => {
|
|
|
48
48
|
},
|
|
49
49
|
cache: { cached: false, ttl_seconds: 30 },
|
|
50
50
|
};
|
|
51
|
-
const line = formatStatusLine(usage, {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
assert.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const paid = {
|
|
61
|
-
...usage,
|
|
62
|
-
window: { ...usage.window, spend_usdc: "0.12", requests: 5 },
|
|
63
|
-
session: null,
|
|
64
|
-
};
|
|
65
|
-
const paidLine = formatStatusLine(paid);
|
|
66
|
-
assert.match(paidLine, /day \$0\.12/);
|
|
67
|
-
assert.doesNotMatch(paidLine, /sess /);
|
|
68
|
-
assert.doesNotMatch(paidLine, /\/5r/);
|
|
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/3933r/354.2M · bal $1.31 · top gpt-5.5");
|
|
53
|
+
assert.ok(line.length < 110, `too long: ${line.length} ${line}`);
|
|
54
|
+
assert.match(line, /6\.0M/);
|
|
55
|
+
assert.match(line, /26\.4M/);
|
|
56
|
+
assert.match(line, /354\.2M/);
|
|
57
|
+
// empty session omitted
|
|
58
|
+
const noSess = { ...usage, session: null };
|
|
59
|
+
assert.doesNotMatch(formatStatusLine(noSess), /sess /);
|
|
69
60
|
assert.equal(money("0"), "$0");
|
|
70
61
|
});
|
package/package.json
CHANGED
package/src/format.test.ts
CHANGED
|
@@ -14,7 +14,7 @@ test("usageUrl", () => {
|
|
|
14
14
|
);
|
|
15
15
|
});
|
|
16
16
|
|
|
17
|
-
test("formatStatusLine
|
|
17
|
+
test("formatStatusLine includes tokens compactly", () => {
|
|
18
18
|
const usage: AccountUsage = {
|
|
19
19
|
object: "account.usage",
|
|
20
20
|
currency: "USDC",
|
|
@@ -56,29 +56,19 @@ test("formatStatusLine is compact one-liner", () => {
|
|
|
56
56
|
cache: { cached: false, ttl_seconds: 30 },
|
|
57
57
|
};
|
|
58
58
|
|
|
59
|
-
const line = formatStatusLine(usage, {
|
|
60
|
-
model: "grok-4.5",
|
|
61
|
-
contextPct: 8,
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
// Must be short enough for one Claude status row
|
|
65
|
-
assert.ok(line.length < 100, `too long: ${line.length} ${line}`);
|
|
59
|
+
const line = formatStatusLine(usage, { model: "grok-4.5", contextPct: 8 });
|
|
66
60
|
assert.equal(
|
|
67
61
|
line,
|
|
68
|
-
"InferHub · sess 19r · day 129r · all $0.41 · bal $1.31 · top gpt-5.5",
|
|
62
|
+
"InferHub · sess 19r/6.0M · day 129r/26.4M · all $0.41/3933r/354.2M · bal $1.31 · top gpt-5.5",
|
|
69
63
|
);
|
|
70
|
-
assert.ok(line.length
|
|
64
|
+
assert.ok(line.length < 110, `too long: ${line.length} ${line}`);
|
|
65
|
+
assert.match(line, /6\.0M/);
|
|
66
|
+
assert.match(line, /26\.4M/);
|
|
67
|
+
assert.match(line, /354\.2M/);
|
|
71
68
|
|
|
72
|
-
//
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
window: { ...usage.window, spend_usdc: "0.12", requests: 5 },
|
|
76
|
-
session: null,
|
|
77
|
-
};
|
|
78
|
-
const paidLine = formatStatusLine(paid);
|
|
79
|
-
assert.match(paidLine, /day \$0\.12/);
|
|
80
|
-
assert.doesNotMatch(paidLine, /sess /);
|
|
81
|
-
assert.doesNotMatch(paidLine, /\/5r/);
|
|
69
|
+
// empty session omitted
|
|
70
|
+
const noSess = { ...usage, session: null };
|
|
71
|
+
assert.doesNotMatch(formatStatusLine(noSess), /sess /);
|
|
82
72
|
|
|
83
73
|
assert.equal(money("0"), "$0");
|
|
84
74
|
});
|
package/src/format.ts
CHANGED
|
@@ -5,12 +5,12 @@ export function money(usdc: string | number | null | undefined, digits = 2): str
|
|
|
5
5
|
if (!Number.isFinite(n)) return "$0";
|
|
6
6
|
if (n === 0) return "$0";
|
|
7
7
|
if (Math.abs(n) < 0.01) return `$${n.toFixed(4)}`;
|
|
8
|
-
if (Math.abs(n) < 1) return `$${n.toFixed(2)}`;
|
|
9
8
|
return `$${n.toFixed(2)}`;
|
|
10
9
|
}
|
|
11
10
|
|
|
12
11
|
export function tokens(n: number | null | undefined): string {
|
|
13
12
|
const v = Number(n ?? 0);
|
|
13
|
+
if (v >= 1_000_000_000) return `${(v / 1_000_000_000).toFixed(1)}B`;
|
|
14
14
|
if (v >= 1_000_000) return `${(v / 1_000_000).toFixed(1)}M`;
|
|
15
15
|
if (v >= 1_000) return `${(v / 1_000).toFixed(1)}k`;
|
|
16
16
|
return String(Math.round(v));
|
|
@@ -22,31 +22,34 @@ export function shortModel(model: string | null | undefined): string {
|
|
|
22
22
|
return parts[parts.length - 1] || model;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* - billed + wantReq: "$0.12/5r" when showReq=true
|
|
25
|
+
/**
|
|
26
|
+
* Compact metric: "4r/1.2M" free, or "$0.12/5r/80k" when billed.
|
|
27
|
+
* Always includes tokens when > 0.
|
|
29
28
|
*/
|
|
30
29
|
function compact(
|
|
31
30
|
spendUsdc: string | number | null | undefined,
|
|
32
31
|
requests: number | null | undefined,
|
|
33
|
-
|
|
32
|
+
totalTokens: number | null | undefined,
|
|
34
33
|
): string {
|
|
35
34
|
const spend = Number(spendUsdc ?? 0);
|
|
36
35
|
const req = Number(requests ?? 0);
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (
|
|
42
|
-
|
|
36
|
+
const tok = Number(totalTokens ?? 0);
|
|
37
|
+
if (req <= 0 && tok <= 0 && spend <= 0) return "—";
|
|
38
|
+
|
|
39
|
+
const bits: string[] = [];
|
|
40
|
+
if (spend > 0) bits.push(money(spend));
|
|
41
|
+
if (req > 0) bits.push(`${req}r`);
|
|
42
|
+
if (tok > 0) bits.push(tokens(tok));
|
|
43
|
+
return bits.join("/") || "—";
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
/**
|
|
46
|
-
* One-line Claude statusline
|
|
47
|
+
* One-line Claude statusline.
|
|
47
48
|
*
|
|
48
49
|
* Example:
|
|
49
|
-
* InferHub · sess
|
|
50
|
+
* InferHub · sess 4r/1.2M · day 135r/26.4M · all $0.41/3.9k r/354M · bal $1.31 · top gpt-5.5
|
|
51
|
+
*
|
|
52
|
+
* Keep under ~90 chars when possible; tokens use k/M/B.
|
|
50
53
|
*/
|
|
51
54
|
export function formatStatusLine(
|
|
52
55
|
usage: AccountUsage,
|
|
@@ -59,12 +62,18 @@ export function formatStatusLine(
|
|
|
59
62
|
): string {
|
|
60
63
|
const parts: string[] = ["InferHub"];
|
|
61
64
|
|
|
62
|
-
if (usage.session && (usage.session.requests > 0 || Number(usage.session.spend_usdc) > 0)) {
|
|
63
|
-
parts.push(
|
|
65
|
+
if (usage.session && (usage.session.requests > 0 || Number(usage.session.spend_usdc) > 0 || usage.session.total_tokens > 0)) {
|
|
66
|
+
parts.push(
|
|
67
|
+
`sess ${compact(usage.session.spend_usdc, usage.session.requests, usage.session.total_tokens)}`,
|
|
68
|
+
);
|
|
64
69
|
}
|
|
65
70
|
|
|
66
|
-
parts.push(
|
|
67
|
-
|
|
71
|
+
parts.push(
|
|
72
|
+
`day ${compact(usage.window.spend_usdc, usage.window.requests, usage.window.total_tokens)}`,
|
|
73
|
+
);
|
|
74
|
+
parts.push(
|
|
75
|
+
`all ${compact(usage.all_time.spend_usdc, usage.all_time.requests, usage.all_time.total_tokens)}`,
|
|
76
|
+
);
|
|
68
77
|
parts.push(`bal ${money(usage.balance.amount_usdc)}`);
|
|
69
78
|
|
|
70
79
|
const top = shortModel(usage.top_model?.model);
|