@laoyuehanni/dsh-token-usage 0.3.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/LICENSE +21 -0
- package/README.md +178 -0
- package/README.zh.md +182 -0
- package/cordis.patch.yml +7 -0
- package/lib/client/CostDeltaFlyLabel.d.ts +8 -0
- package/lib/client/CostDeltaFlyLabel.d.ts.map +1 -0
- package/lib/client/CostDeltaFlyLabel.js +24 -0
- package/lib/client/CostDeltaFlyLabel.js.map +1 -0
- package/lib/client/HitRateText.d.ts +21 -0
- package/lib/client/HitRateText.d.ts.map +1 -0
- package/lib/client/HitRateText.js +16 -0
- package/lib/client/HitRateText.js.map +1 -0
- package/lib/client/QuotaButton.d.ts +70 -0
- package/lib/client/QuotaButton.d.ts.map +1 -0
- package/lib/client/QuotaButton.js +346 -0
- package/lib/client/QuotaButton.js.map +1 -0
- package/lib/client/SessionStatsChip.d.ts +47 -0
- package/lib/client/SessionStatsChip.d.ts.map +1 -0
- package/lib/client/SessionStatsChip.js +141 -0
- package/lib/client/SessionStatsChip.js.map +1 -0
- package/lib/client/StatCard.d.ts +26 -0
- package/lib/client/StatCard.d.ts.map +1 -0
- package/lib/client/StatCard.js +10 -0
- package/lib/client/StatCard.js.map +1 -0
- package/lib/client/TokenUsageCard.d.ts +41 -0
- package/lib/client/TokenUsageCard.d.ts.map +1 -0
- package/lib/client/TokenUsageCard.js +164 -0
- package/lib/client/TokenUsageCard.js.map +1 -0
- package/lib/client/TokenUsageSection.d.ts +32 -0
- package/lib/client/TokenUsageSection.d.ts.map +1 -0
- package/lib/client/TokenUsageSection.js +266 -0
- package/lib/client/TokenUsageSection.js.map +1 -0
- package/lib/client/TrendChart.d.ts +53 -0
- package/lib/client/TrendChart.d.ts.map +1 -0
- package/lib/client/TrendChart.js +141 -0
- package/lib/client/TrendChart.js.map +1 -0
- package/lib/client/UsageView.d.ts +42 -0
- package/lib/client/UsageView.d.ts.map +1 -0
- package/lib/client/UsageView.js +158 -0
- package/lib/client/UsageView.js.map +1 -0
- package/lib/client/async-resource.d.ts +54 -0
- package/lib/client/async-resource.d.ts.map +1 -0
- package/lib/client/async-resource.js +90 -0
- package/lib/client/async-resource.js.map +1 -0
- package/lib/client/card-form.d.ts +171 -0
- package/lib/client/card-form.d.ts.map +1 -0
- package/lib/client/card-form.js +300 -0
- package/lib/client/card-form.js.map +1 -0
- package/lib/client/cost-inflate-motion.d.ts +17 -0
- package/lib/client/cost-inflate-motion.d.ts.map +1 -0
- package/lib/client/cost-inflate-motion.js +61 -0
- package/lib/client/cost-inflate-motion.js.map +1 -0
- package/lib/client/cost-inflate.d.ts +28 -0
- package/lib/client/cost-inflate.d.ts.map +1 -0
- package/lib/client/cost-inflate.js +49 -0
- package/lib/client/cost-inflate.js.map +1 -0
- package/lib/client/day.d.ts +49 -0
- package/lib/client/day.d.ts.map +1 -0
- package/lib/client/day.js +84 -0
- package/lib/client/day.js.map +1 -0
- package/lib/client/format.d.ts +120 -0
- package/lib/client/format.d.ts.map +1 -0
- package/lib/client/format.js +163 -0
- package/lib/client/format.js.map +1 -0
- package/lib/client/index.d.ts +26 -0
- package/lib/client/index.d.ts.map +1 -0
- package/lib/client/index.js +131 -0
- package/lib/client/index.js.map +1 -0
- package/lib/client/locales.d.ts +131 -0
- package/lib/client/locales.d.ts.map +1 -0
- package/lib/client/locales.js +230 -0
- package/lib/client/locales.js.map +1 -0
- package/lib/client/quota-format.d.ts +69 -0
- package/lib/client/quota-format.d.ts.map +1 -0
- package/lib/client/quota-format.js +147 -0
- package/lib/client/quota-format.js.map +1 -0
- package/lib/client/session-stats.d.ts +105 -0
- package/lib/client/session-stats.d.ts.map +1 -0
- package/lib/client/session-stats.js +142 -0
- package/lib/client/session-stats.js.map +1 -0
- package/lib/client/trend-chart/axis.d.ts +21 -0
- package/lib/client/trend-chart/axis.d.ts.map +1 -0
- package/lib/client/trend-chart/axis.js +33 -0
- package/lib/client/trend-chart/axis.js.map +1 -0
- package/lib/client/trend-chart/bucket.d.ts +24 -0
- package/lib/client/trend-chart/bucket.d.ts.map +1 -0
- package/lib/client/trend-chart/bucket.js +28 -0
- package/lib/client/trend-chart/bucket.js.map +1 -0
- package/lib/client/trend-chart/index.d.ts +12 -0
- package/lib/client/trend-chart/index.d.ts.map +1 -0
- package/lib/client/trend-chart/index.js +9 -0
- package/lib/client/trend-chart/index.js.map +1 -0
- package/lib/client/trend-chart/points.d.ts +46 -0
- package/lib/client/trend-chart/points.d.ts.map +1 -0
- package/lib/client/trend-chart/points.js +82 -0
- package/lib/client/trend-chart/points.js.map +1 -0
- package/lib/client/trend-chart/scale.d.ts +47 -0
- package/lib/client/trend-chart/scale.d.ts.map +1 -0
- package/lib/client/trend-chart/scale.js +75 -0
- package/lib/client/trend-chart/scale.js.map +1 -0
- package/lib/client/use-color-scheme.d.ts +18 -0
- package/lib/client/use-color-scheme.d.ts.map +1 -0
- package/lib/client/use-color-scheme.js +37 -0
- package/lib/client/use-color-scheme.js.map +1 -0
- package/lib/client/use-cost-inflate.d.ts +33 -0
- package/lib/client/use-cost-inflate.d.ts.map +1 -0
- package/lib/client/use-cost-inflate.js +108 -0
- package/lib/client/use-cost-inflate.js.map +1 -0
- package/lib/client.js +3892 -0
- package/lib/client.js.map +1 -0
- package/lib/index.d.ts +132 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +599 -0
- package/lib/index.js.map +1 -0
- package/lib/migrate.d.ts +60 -0
- package/lib/migrate.d.ts.map +1 -0
- package/lib/migrate.js +126 -0
- package/lib/migrate.js.map +1 -0
- package/lib/pricing.d.ts +212 -0
- package/lib/pricing.d.ts.map +1 -0
- package/lib/pricing.js +492 -0
- package/lib/pricing.js.map +1 -0
- package/lib/quota/adapters/deepseek-balance.d.ts +18 -0
- package/lib/quota/adapters/deepseek-balance.d.ts.map +1 -0
- package/lib/quota/adapters/deepseek-balance.js +76 -0
- package/lib/quota/adapters/deepseek-balance.js.map +1 -0
- package/lib/quota/adapters/kimi.d.ts +26 -0
- package/lib/quota/adapters/kimi.d.ts.map +1 -0
- package/lib/quota/adapters/kimi.js +91 -0
- package/lib/quota/adapters/kimi.js.map +1 -0
- package/lib/quota/adapters/minimax.d.ts +24 -0
- package/lib/quota/adapters/minimax.d.ts.map +1 -0
- package/lib/quota/adapters/minimax.js +136 -0
- package/lib/quota/adapters/minimax.js.map +1 -0
- package/lib/quota/adapters/opencode-go.d.ts +25 -0
- package/lib/quota/adapters/opencode-go.d.ts.map +1 -0
- package/lib/quota/adapters/opencode-go.js +105 -0
- package/lib/quota/adapters/opencode-go.js.map +1 -0
- package/lib/quota/adapters/openrouter.d.ts +15 -0
- package/lib/quota/adapters/openrouter.d.ts.map +1 -0
- package/lib/quota/adapters/openrouter.js +54 -0
- package/lib/quota/adapters/openrouter.js.map +1 -0
- package/lib/quota/adapters/zhipu.d.ts +38 -0
- package/lib/quota/adapters/zhipu.d.ts.map +1 -0
- package/lib/quota/adapters/zhipu.js +246 -0
- package/lib/quota/adapters/zhipu.js.map +1 -0
- package/lib/quota/credentials.d.ts +79 -0
- package/lib/quota/credentials.d.ts.map +1 -0
- package/lib/quota/credentials.js +130 -0
- package/lib/quota/credentials.js.map +1 -0
- package/lib/quota/http.d.ts +49 -0
- package/lib/quota/http.d.ts.map +1 -0
- package/lib/quota/http.js +114 -0
- package/lib/quota/http.js.map +1 -0
- package/lib/quota/provider-tracking.d.ts +45 -0
- package/lib/quota/provider-tracking.d.ts.map +1 -0
- package/lib/quota/provider-tracking.js +52 -0
- package/lib/quota/provider-tracking.js.map +1 -0
- package/lib/quota/quota-route.d.ts +22 -0
- package/lib/quota/quota-route.d.ts.map +1 -0
- package/lib/quota/quota-route.js +57 -0
- package/lib/quota/quota-route.js.map +1 -0
- package/lib/quota/quota-service.d.ts +54 -0
- package/lib/quota/quota-service.d.ts.map +1 -0
- package/lib/quota/quota-service.js +171 -0
- package/lib/quota/quota-service.js.map +1 -0
- package/lib/quota/registry.d.ts +24 -0
- package/lib/quota/registry.d.ts.map +1 -0
- package/lib/quota/registry.js +38 -0
- package/lib/quota/registry.js.map +1 -0
- package/lib/quota/types.d.ts +79 -0
- package/lib/quota/types.d.ts.map +1 -0
- package/lib/quota/types.js +24 -0
- package/lib/quota/types.js.map +1 -0
- package/lib/record-cache.d.ts +38 -0
- package/lib/record-cache.d.ts.map +1 -0
- package/lib/record-cache.js +143 -0
- package/lib/record-cache.js.map +1 -0
- package/lib/rollup.d.ts +52 -0
- package/lib/rollup.d.ts.map +1 -0
- package/lib/rollup.js +117 -0
- package/lib/rollup.js.map +1 -0
- package/lib/stats-route.d.ts +88 -0
- package/lib/stats-route.d.ts.map +1 -0
- package/lib/stats-route.js +313 -0
- package/lib/stats-route.js.map +1 -0
- package/lib/stats.d.ts +158 -0
- package/lib/stats.d.ts.map +1 -0
- package/lib/stats.js +426 -0
- package/lib/stats.js.map +1 -0
- package/lib/sync-state.d.ts +27 -0
- package/lib/sync-state.d.ts.map +1 -0
- package/lib/sync-state.js +56 -0
- package/lib/sync-state.js.map +1 -0
- package/lib/sync.d.ts +74 -0
- package/lib/sync.d.ts.map +1 -0
- package/lib/sync.js +64 -0
- package/lib/sync.js.map +1 -0
- package/lib/trend-bucket.d.ts +48 -0
- package/lib/trend-bucket.d.ts.map +1 -0
- package/lib/trend-bucket.js +87 -0
- package/lib/trend-bucket.js.map +1 -0
- package/lib/usage-log.d.ts +64 -0
- package/lib/usage-log.d.ts.map +1 -0
- package/lib/usage-log.js +144 -0
- package/lib/usage-log.js.map +1 -0
- package/lib/usage-record.d.ts +59 -0
- package/lib/usage-record.d.ts.map +1 -0
- package/lib/usage-record.js +115 -0
- package/lib/usage-record.js.map +1 -0
- package/lib/wire.d.ts +452 -0
- package/lib/wire.d.ts.map +1 -0
- package/lib/wire.js +144 -0
- package/lib/wire.js.map +1 -0
- package/package.json +100 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process-memory cache of parsed usage day files. Frozen (pre-today) files
|
|
3
|
+
* stay resident after the first read; today's file is re-read when its size
|
|
4
|
+
* or mtime changes. Concurrent readers of one directory share one in-flight
|
|
5
|
+
* load (singleflight), so the header chip, the usage tab, and a settings
|
|
6
|
+
* refresh cannot pile overlapping full-directory parses onto the event loop.
|
|
7
|
+
*
|
|
8
|
+
* @module token-usage/record-cache
|
|
9
|
+
*/
|
|
10
|
+
import type { UsageRecord } from './usage-record.ts';
|
|
11
|
+
/** The date part of a day-file name, or null for a foreign name. */
|
|
12
|
+
export declare function fileDay(name: string): string | null;
|
|
13
|
+
/**
|
|
14
|
+
* Drop cached parses. A missing `dir` clears every directory (test seam);
|
|
15
|
+
* a path drops that directory only (data-directory relocation).
|
|
16
|
+
*/
|
|
17
|
+
export declare function clearRecordCache(dir?: string): void;
|
|
18
|
+
/**
|
|
19
|
+
* Read one day file into records. Malformed lines are skipped silently —
|
|
20
|
+
* the stats read runs on every page refresh and must not spam the console
|
|
21
|
+
* over one bad row. An unreadable file logs once and reads as empty, so a
|
|
22
|
+
* corrupt log never blocks stats.
|
|
23
|
+
* @param dir - the plugin's data directory.
|
|
24
|
+
* @param name - the day-file name.
|
|
25
|
+
*/
|
|
26
|
+
export declare function readDayFile(dir: string, name: string): Promise<UsageRecord[]>;
|
|
27
|
+
/** List the data directory's day-file names in ascending date order ([] when absent). */
|
|
28
|
+
export declare function listDayFiles(dir: string): Promise<string[]>;
|
|
29
|
+
/**
|
|
30
|
+
* Read every day file into records, serving frozen files from memory after
|
|
31
|
+
* the first parse. An absent data directory yields an empty list.
|
|
32
|
+
* @param dir - the plugin's data directory.
|
|
33
|
+
* @param now - clock source for the frozen/today boundary (test seam).
|
|
34
|
+
*/
|
|
35
|
+
export declare function readCachedRecords(dir: string, now?: () => Date): Promise<UsageRecord[]>;
|
|
36
|
+
/** Populate the cache for one directory so the first stats poll is a memory hit. */
|
|
37
|
+
export declare function warmRecordCache(dir: string, now?: () => Date): Promise<void>;
|
|
38
|
+
//# sourceMappingURL=record-cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record-cache.d.ts","sourceRoot":"","sources":["../src/record-cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAIpD,oEAAoE;AACpE,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAEnD;AAgCD;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAGnD;AAED;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAYnF;AAED,yFAAyF;AACzF,wBAAsB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CASjE;AA4CD;;;;;GAKG;AACH,wBAAsB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,GAAE,MAAM,IAAuB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAQ/G;AAED,oFAAoF;AACpF,wBAAsB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAElF"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process-memory cache of parsed usage day files. Frozen (pre-today) files
|
|
3
|
+
* stay resident after the first read; today's file is re-read when its size
|
|
4
|
+
* or mtime changes. Concurrent readers of one directory share one in-flight
|
|
5
|
+
* load (singleflight), so the header chip, the usage tab, and a settings
|
|
6
|
+
* refresh cannot pile overlapping full-directory parses onto the event loop.
|
|
7
|
+
*
|
|
8
|
+
* @module token-usage/record-cache
|
|
9
|
+
*/
|
|
10
|
+
import { readdir, readFile, stat } from 'node:fs/promises';
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
import { parseRecord } from "./usage-record.js";
|
|
13
|
+
const DAY_FILE = /^usage-(\d{4}-\d{2}-\d{2})\.jsonl$/u;
|
|
14
|
+
/** The date part of a day-file name, or null for a foreign name. */
|
|
15
|
+
export function fileDay(name) {
|
|
16
|
+
return DAY_FILE.exec(name)?.[1] ?? null;
|
|
17
|
+
}
|
|
18
|
+
/** Local date key of a clock reading, matching the day-file naming convention. */
|
|
19
|
+
function dayKey(time) {
|
|
20
|
+
const date = new Date(time);
|
|
21
|
+
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
22
|
+
const day = String(date.getDate()).padStart(2, '0');
|
|
23
|
+
return `${date.getFullYear()}-${month}-${day}`;
|
|
24
|
+
}
|
|
25
|
+
const dirs = new Map();
|
|
26
|
+
function stateOf(dir) {
|
|
27
|
+
const existing = dirs.get(dir);
|
|
28
|
+
if (existing !== undefined)
|
|
29
|
+
return existing;
|
|
30
|
+
const created = { files: new Map(), inflight: undefined };
|
|
31
|
+
dirs.set(dir, created);
|
|
32
|
+
return created;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Drop cached parses. A missing `dir` clears every directory (test seam);
|
|
36
|
+
* a path drops that directory only (data-directory relocation).
|
|
37
|
+
*/
|
|
38
|
+
export function clearRecordCache(dir) {
|
|
39
|
+
if (dir === undefined)
|
|
40
|
+
dirs.clear();
|
|
41
|
+
else
|
|
42
|
+
dirs.delete(dir);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Read one day file into records. Malformed lines are skipped silently —
|
|
46
|
+
* the stats read runs on every page refresh and must not spam the console
|
|
47
|
+
* over one bad row. An unreadable file logs once and reads as empty, so a
|
|
48
|
+
* corrupt log never blocks stats.
|
|
49
|
+
* @param dir - the plugin's data directory.
|
|
50
|
+
* @param name - the day-file name.
|
|
51
|
+
*/
|
|
52
|
+
export async function readDayFile(dir, name) {
|
|
53
|
+
const text = await readFile(join(dir, name), 'utf8').catch((error) => {
|
|
54
|
+
console.error(`[token-usage] cannot read ${name}:`, error);
|
|
55
|
+
return '';
|
|
56
|
+
});
|
|
57
|
+
const records = [];
|
|
58
|
+
for (const line of text.split('\n')) {
|
|
59
|
+
if (line === '')
|
|
60
|
+
continue;
|
|
61
|
+
const record = parseRecord(line);
|
|
62
|
+
if (record !== null)
|
|
63
|
+
records.push(record);
|
|
64
|
+
}
|
|
65
|
+
return records;
|
|
66
|
+
}
|
|
67
|
+
/** List the data directory's day-file names in ascending date order ([] when absent). */
|
|
68
|
+
export async function listDayFiles(dir) {
|
|
69
|
+
let names;
|
|
70
|
+
try {
|
|
71
|
+
names = await readdir(dir);
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if (error.code === 'ENOENT')
|
|
75
|
+
return [];
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
return names.filter(name => fileDay(name) !== null).sort();
|
|
79
|
+
}
|
|
80
|
+
async function fileStamp(dir, name) {
|
|
81
|
+
try {
|
|
82
|
+
const info = await stat(join(dir, name));
|
|
83
|
+
return { size: info.size, mtimeMs: info.mtimeMs };
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async function loadDir(dir, cache, now) {
|
|
90
|
+
const today = dayKey(now().getTime());
|
|
91
|
+
const names = await listDayFiles(dir);
|
|
92
|
+
const live = new Set(names);
|
|
93
|
+
for (const name of cache.files.keys()) {
|
|
94
|
+
if (!live.has(name))
|
|
95
|
+
cache.files.delete(name);
|
|
96
|
+
}
|
|
97
|
+
const out = [];
|
|
98
|
+
for (const name of names) {
|
|
99
|
+
const day = fileDay(name);
|
|
100
|
+
const hot = day >= today;
|
|
101
|
+
const cached = cache.files.get(name);
|
|
102
|
+
// Frozen files are append-only history: once parsed they never change.
|
|
103
|
+
if (!hot && cached !== undefined) {
|
|
104
|
+
out.push(...cached.records);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const stamp = await fileStamp(dir, name);
|
|
108
|
+
if (stamp === null) {
|
|
109
|
+
cache.files.delete(name);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (cached !== undefined && cached.size === stamp.size && cached.mtimeMs === stamp.mtimeMs) {
|
|
113
|
+
out.push(...cached.records);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const records = await readDayFile(dir, name);
|
|
117
|
+
cache.files.set(name, { records, size: stamp.size, mtimeMs: stamp.mtimeMs });
|
|
118
|
+
out.push(...records);
|
|
119
|
+
}
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Read every day file into records, serving frozen files from memory after
|
|
124
|
+
* the first parse. An absent data directory yields an empty list.
|
|
125
|
+
* @param dir - the plugin's data directory.
|
|
126
|
+
* @param now - clock source for the frozen/today boundary (test seam).
|
|
127
|
+
*/
|
|
128
|
+
export async function readCachedRecords(dir, now = () => new Date()) {
|
|
129
|
+
const cache = stateOf(dir);
|
|
130
|
+
if (cache.inflight !== undefined)
|
|
131
|
+
return cache.inflight;
|
|
132
|
+
const pending = loadDir(dir, cache, now).finally(() => {
|
|
133
|
+
if (cache.inflight === pending)
|
|
134
|
+
cache.inflight = undefined;
|
|
135
|
+
});
|
|
136
|
+
cache.inflight = pending;
|
|
137
|
+
return pending;
|
|
138
|
+
}
|
|
139
|
+
/** Populate the cache for one directory so the first stats poll is a memory hit. */
|
|
140
|
+
export async function warmRecordCache(dir, now) {
|
|
141
|
+
await readCachedRecords(dir, now);
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=record-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record-cache.js","sourceRoot":"","sources":["../src/record-cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AAC1D,OAAO,EAAE,IAAI,EAA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AAC;AAED,oFAAoF;AACpF,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,GAAW,EAAE,GAAgB;IACjE,MAAM,iBAAiB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;AACnC,CAAC"}
|
package/lib/rollup.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The per-day rollup store of the token-usage plugin: an on-disk aggregate of
|
|
3
|
+
* every frozen (pre-today) day file. Day files are immutable once their date
|
|
4
|
+
* has passed — writes always append to the file of the current day — so an
|
|
5
|
+
* absorbed aggregate never goes stale and needs no invalidation. The stats
|
|
6
|
+
* read advances the rollup lazily: whenever unabsorbed frozen files exist,
|
|
7
|
+
* they are folded in and the rollup is rewritten atomically (temp file +
|
|
8
|
+
* rename, like the sync marker). A missing or malformed rollup simply reads
|
|
9
|
+
* as absent and is rebuilt from the day files.
|
|
10
|
+
*
|
|
11
|
+
* @module token-usage/rollup
|
|
12
|
+
*/
|
|
13
|
+
import type { UsageRecord } from './usage-record.ts';
|
|
14
|
+
import type { UsageDayRow, UsageHourRow, UsageModelRow, UsageRateRow, UsageTotals } from './wire.ts';
|
|
15
|
+
/** The on-disk rollup: the aggregate of every day file named ≤ {@link RollupFile.upto}. */
|
|
16
|
+
export interface RollupFile {
|
|
17
|
+
/** Inclusive upper date (`YYYY-MM-DD`) of the day files already absorbed. */
|
|
18
|
+
upto: string;
|
|
19
|
+
/** Totals over every absorbed record. */
|
|
20
|
+
total: UsageTotals;
|
|
21
|
+
/** Per-local-day rows of the absorbed records, ascending by day. */
|
|
22
|
+
byDay: UsageDayRow[];
|
|
23
|
+
/** Per-hour × per-model rows of the absorbed records, ascending by hour
|
|
24
|
+
* then model — feeds the single-day hourly trend chart without rereading
|
|
25
|
+
* the frozen day files. */
|
|
26
|
+
byHour: UsageHourRow[];
|
|
27
|
+
/** Per-model rows of the absorbed records, descending on request count. */
|
|
28
|
+
byModel: UsageModelRow[];
|
|
29
|
+
/** Per-(day, model, rate identity) rows of the absorbed records — rule
|
|
30
|
+
* identities, never prices, so an updated pricing table re-prices the
|
|
31
|
+
* absorbed history without a rebuild. */
|
|
32
|
+
rateRows: UsageRateRow[];
|
|
33
|
+
/** The newest absorbed records, descending by time (bounded window). */
|
|
34
|
+
recent: UsageRecord[];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Load the rollup of one data directory. A missing, malformed, or
|
|
38
|
+
* structurally invalid rollup reads as null — the caller then rebuilds it
|
|
39
|
+
* from the day files, so corruption never blocks the stats read. Invalid
|
|
40
|
+
* entries inside the recent window are dropped instead of failing the whole
|
|
41
|
+
* rollup, mirroring how malformed JSONL lines are skipped.
|
|
42
|
+
* @param dir - the plugin's data directory.
|
|
43
|
+
*/
|
|
44
|
+
export declare function readRollup(dir: string): Promise<RollupFile | null>;
|
|
45
|
+
/**
|
|
46
|
+
* Persist the rollup atomically (temp file + rename), so a crash mid-write
|
|
47
|
+
* leaves either the old or the new rollup, never a torn one.
|
|
48
|
+
* @param dir - the plugin's data directory.
|
|
49
|
+
* @param rollup - the rollup to persist.
|
|
50
|
+
*/
|
|
51
|
+
export declare function writeRollup(dir: string, rollup: RollupFile): Promise<void>;
|
|
52
|
+
//# sourceMappingURL=rollup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rollup.d.ts","sourceRoot":"","sources":["../src/rollup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAKH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AACpD,OAAO,KAAK,EAAW,WAAW,EAAE,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAK7G,2FAA2F;AAC3F,MAAM,WAAW,UAAU;IACzB,6EAA6E;IAC7E,IAAI,EAAE,MAAM,CAAA;IACZ,yCAAyC;IACzC,KAAK,EAAE,WAAW,CAAA;IAClB,oEAAoE;IACpE,KAAK,EAAE,WAAW,EAAE,CAAA;IACpB;;+BAE2B;IAC3B,MAAM,EAAE,YAAY,EAAE,CAAA;IACtB,2EAA2E;IAC3E,OAAO,EAAE,aAAa,EAAE,CAAA;IACxB;;6CAEyC;IACzC,QAAQ,EAAE,YAAY,EAAE,CAAA;IACxB,wEAAwE;IACxE,MAAM,EAAE,WAAW,EAAE,CAAA;CACtB;AAyDD;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAqBxE;AAED;;;;;GAKG;AACH,wBAAsB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAKhF"}
|
package/lib/rollup.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The per-day rollup store of the token-usage plugin: an on-disk aggregate of
|
|
3
|
+
* every frozen (pre-today) day file. Day files are immutable once their date
|
|
4
|
+
* has passed — writes always append to the file of the current day — so an
|
|
5
|
+
* absorbed aggregate never goes stale and needs no invalidation. The stats
|
|
6
|
+
* read advances the rollup lazily: whenever unabsorbed frozen files exist,
|
|
7
|
+
* they are folded in and the rollup is rewritten atomically (temp file +
|
|
8
|
+
* rename, like the sync marker). A missing or malformed rollup simply reads
|
|
9
|
+
* as absent and is rebuilt from the day files.
|
|
10
|
+
*
|
|
11
|
+
* @module token-usage/rollup
|
|
12
|
+
*/
|
|
13
|
+
import { readFile, rename, writeFile } from 'node:fs/promises';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
import { coerceRecord } from "./usage-record.js";
|
|
16
|
+
const ROLLUP_FILE = 'rollup.json';
|
|
17
|
+
const TMP_FILE = 'rollup.json.tmp';
|
|
18
|
+
const DAY_KEY = /^\d{4}-\d{2}-\d{2}$/u;
|
|
19
|
+
function isTotals(value) {
|
|
20
|
+
if (typeof value !== 'object' || value === null)
|
|
21
|
+
return false;
|
|
22
|
+
const totals = value;
|
|
23
|
+
return ['requests', 'inputTokens', 'outputTokens', 'cacheReadTokens', 'cacheWriteTokens']
|
|
24
|
+
.every(key => typeof totals[key] === 'number' && Number.isFinite(totals[key]));
|
|
25
|
+
}
|
|
26
|
+
function isDayRow(value) {
|
|
27
|
+
if (typeof value !== 'object' || value === null)
|
|
28
|
+
return false;
|
|
29
|
+
const row = value;
|
|
30
|
+
return typeof row.day === 'string' && DAY_KEY.test(row.day) && isTotals(row.totals);
|
|
31
|
+
}
|
|
32
|
+
const HOUR_KEY = /^\d{4}-\d{2}-\d{2}T\d{2}$/u;
|
|
33
|
+
function isHourRow(value) {
|
|
34
|
+
if (typeof value !== 'object' || value === null)
|
|
35
|
+
return false;
|
|
36
|
+
const row = value;
|
|
37
|
+
return typeof row.hour === 'string' && HOUR_KEY.test(row.hour)
|
|
38
|
+
&& typeof row.model === 'string' && isTotals(row.totals);
|
|
39
|
+
}
|
|
40
|
+
function isModelRow(value) {
|
|
41
|
+
if (typeof value !== 'object' || value === null)
|
|
42
|
+
return false;
|
|
43
|
+
const row = value;
|
|
44
|
+
return typeof row.model === 'string' && isTotals(row.totals);
|
|
45
|
+
}
|
|
46
|
+
function isRateKey(value) {
|
|
47
|
+
if (typeof value !== 'object' || value === null)
|
|
48
|
+
return false;
|
|
49
|
+
const key = value;
|
|
50
|
+
return ['ruleStart', 'ruleEnd', 'tier', 'slot'].every(field => typeof key[field] === 'number' && Number.isFinite(key[field]));
|
|
51
|
+
}
|
|
52
|
+
function isRateRow(value) {
|
|
53
|
+
if (typeof value !== 'object' || value === null)
|
|
54
|
+
return false;
|
|
55
|
+
const row = value;
|
|
56
|
+
return typeof row.day === 'string' && DAY_KEY.test(row.day)
|
|
57
|
+
&& typeof row.model === 'string' && isRateKey(row.rate) && isTotals(row.totals);
|
|
58
|
+
}
|
|
59
|
+
function isRollupFile(value) {
|
|
60
|
+
if (typeof value !== 'object' || value === null)
|
|
61
|
+
return false;
|
|
62
|
+
const rollup = value;
|
|
63
|
+
return typeof rollup.upto === 'string' && DAY_KEY.test(rollup.upto)
|
|
64
|
+
&& isTotals(rollup.total)
|
|
65
|
+
&& Array.isArray(rollup.byDay) && rollup.byDay.every(isDayRow)
|
|
66
|
+
&& Array.isArray(rollup.byHour) && rollup.byHour.every(isHourRow)
|
|
67
|
+
&& Array.isArray(rollup.byModel) && rollup.byModel.every(isModelRow)
|
|
68
|
+
&& Array.isArray(rollup.rateRows) && rollup.rateRows.every(isRateRow)
|
|
69
|
+
&& Array.isArray(rollup.recent);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Load the rollup of one data directory. A missing, malformed, or
|
|
73
|
+
* structurally invalid rollup reads as null — the caller then rebuilds it
|
|
74
|
+
* from the day files, so corruption never blocks the stats read. Invalid
|
|
75
|
+
* entries inside the recent window are dropped instead of failing the whole
|
|
76
|
+
* rollup, mirroring how malformed JSONL lines are skipped.
|
|
77
|
+
* @param dir - the plugin's data directory.
|
|
78
|
+
*/
|
|
79
|
+
export async function readRollup(dir) {
|
|
80
|
+
let text;
|
|
81
|
+
try {
|
|
82
|
+
text = await readFile(join(dir, ROLLUP_FILE), 'utf8');
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
if (error.code === 'ENOENT')
|
|
86
|
+
return null;
|
|
87
|
+
console.error('[token-usage] cannot read rollup:', error);
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
let value;
|
|
91
|
+
try {
|
|
92
|
+
value = JSON.parse(text);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
// A torn or hand-edited rollup rebuilds from the day files.
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
if (!isRollupFile(value))
|
|
99
|
+
return null;
|
|
100
|
+
const recent = value.recent
|
|
101
|
+
.map(coerceRecord)
|
|
102
|
+
.filter((record) => record !== null);
|
|
103
|
+
return { ...value, recent };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Persist the rollup atomically (temp file + rename), so a crash mid-write
|
|
107
|
+
* leaves either the old or the new rollup, never a torn one.
|
|
108
|
+
* @param dir - the plugin's data directory.
|
|
109
|
+
* @param rollup - the rollup to persist.
|
|
110
|
+
*/
|
|
111
|
+
export async function writeRollup(dir, rollup) {
|
|
112
|
+
const target = join(dir, ROLLUP_FILE);
|
|
113
|
+
const tmp = join(dir, TMP_FILE);
|
|
114
|
+
await writeFile(tmp, JSON.stringify(rollup), 'utf8');
|
|
115
|
+
await rename(tmp, target);
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=rollup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rollup.js","sourceRoot":"","sources":["../src/rollup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAC9D,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAIhD,MAAM,WAAW,GAAG,aAAa,CAAA;AACjC,MAAM,QAAQ,GAAG,iBAAiB,CAAA;AAwBlC,MAAM,OAAO,GAAG,sBAAsB,CAAA;AAEtC,SAAS,QAAQ,CAAC,KAAc;IAC9B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,KAAK,CAAA;IAC7D,MAAM,MAAM,GAAG,KAAgC,CAAA;IAC/C,OAAO,CAAC,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,iBAAiB,EAAE,kBAAkB,CAAC;SACtF,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;AAClF,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;I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AAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAA;IACpD,MAAM,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;AAC3B,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The stats HTTP route of the token-usage plugin: a webServer exact route
|
|
3
|
+
* serving the JSON summary at `/token-usage/stats` for the web settings page.
|
|
4
|
+
* The /api prefix is owned by the browser-transport connection plugin and its
|
|
5
|
+
* RPC method table is closed, so the plugin data channel is its own route.
|
|
6
|
+
*
|
|
7
|
+
* @module token-usage/stats-route
|
|
8
|
+
*/
|
|
9
|
+
import type { IncomingMessage } from 'node:http';
|
|
10
|
+
import type { WebRoute } from '@deepseek-ai/dsh-host-webserver';
|
|
11
|
+
import type { MigrationProgress } from './migrate.ts';
|
|
12
|
+
import type { DirectoryGuardView, DisplayCurrency, FullSyncView } from './wire.ts';
|
|
13
|
+
/** The stats endpoint path, exported for tests and the client half. */
|
|
14
|
+
export { STATS_PATH } from './wire.ts';
|
|
15
|
+
/** The migration-progress endpoint path, exported for the client half. */
|
|
16
|
+
export { MIGRATION_PATH } from './wire.ts';
|
|
17
|
+
/** The directory-guard endpoint path, exported for the client half. */
|
|
18
|
+
export { DIR_GUARD_PATH } from './wire.ts';
|
|
19
|
+
/** The full-sync endpoint path, exported for the client half. */
|
|
20
|
+
export { FULL_SYNC_PATH } from './wire.ts';
|
|
21
|
+
/**
|
|
22
|
+
* Whether a request may read the stats: same-origin browser fetches only.
|
|
23
|
+
* Browsers send `Sec-Fetch-Site` on cross-origin requests; a cross-site value
|
|
24
|
+
* (or a cross-site GET from a page on another origin) is refused. The header
|
|
25
|
+
* is absent for non-browser clients (curl, tests), which are allowed.
|
|
26
|
+
* @param req - the incoming request.
|
|
27
|
+
* @returns whether the request originates from the served page.
|
|
28
|
+
*/
|
|
29
|
+
export declare function isSameOriginFetch(req: IncomingMessage): boolean;
|
|
30
|
+
/** How the route resolves the display currency per request; a thunk so a
|
|
31
|
+
* live settings change (the region pick) lands without rebuilding the route. */
|
|
32
|
+
export interface StatsRouteOptions {
|
|
33
|
+
currency?: () => DisplayCurrency;
|
|
34
|
+
}
|
|
35
|
+
/** Live progress of a data-directory relocation; undefined when none runs. */
|
|
36
|
+
export type MigrationStatus = MigrationProgress | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Build the migration-progress route the browser card polls while a move
|
|
39
|
+
* runs. Read-only by construction: it answers the shared status object and
|
|
40
|
+
* nothing else.
|
|
41
|
+
* @param status - reads the live migration state.
|
|
42
|
+
* @returns the exact GET route answering the progress JSON.
|
|
43
|
+
*/
|
|
44
|
+
export declare function createMigrationRoute(status: () => MigrationStatus): WebRoute;
|
|
45
|
+
/**
|
|
46
|
+
* Build the directory-guard route the browser card consults before saving a
|
|
47
|
+
* staged directory edit: the settings wire never delivers a refused write's
|
|
48
|
+
* reason, so the card asks here whether the save would be refused and shows
|
|
49
|
+
* the verdict's count itself. Read-only by construction: it answers the
|
|
50
|
+
* caller's judge verdict and writes nothing.
|
|
51
|
+
* @param judge - answers the verdict for one proposed stored path
|
|
52
|
+
* (`undefined` when the save would clear the override).
|
|
53
|
+
* @returns the exact GET route answering the verdict JSON.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createDirectoryGuardRoute(judge: (proposed: string | undefined) => DirectoryGuardView): WebRoute;
|
|
56
|
+
/**
|
|
57
|
+
* Outcome of one manual full-sync trigger.
|
|
58
|
+
* - `started: true` — the run kicked off; the next poll will see the live counts.
|
|
59
|
+
* - `started: false, reason: 'already-running'` — a run is still in flight;
|
|
60
|
+
* the card surfaces this as the button's disabled state and a 409 response.
|
|
61
|
+
*/
|
|
62
|
+
export type FullSyncTrigger = {
|
|
63
|
+
started: true;
|
|
64
|
+
} | {
|
|
65
|
+
started: false;
|
|
66
|
+
reason: 'already-running';
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Build the full-sync route backing the card's manual "scan again" button.
|
|
70
|
+
* The handler is small by design: it only owns the request/response shape
|
|
71
|
+
* (GET polls the live status, POST kicks off a run). The actual scan lives
|
|
72
|
+
* on the host half and updates the shared status object, which `status()`
|
|
73
|
+
* reads. The scan is fire-and-forget on POST so the request never blocks
|
|
74
|
+
* behind a long walk; the card polls until the status leaves `running`.
|
|
75
|
+
* @param status - reads the shared status (the same view the card renders).
|
|
76
|
+
* @param trigger - kicks off a new run when the host is idle.
|
|
77
|
+
* @returns the exact route serving both methods.
|
|
78
|
+
*/
|
|
79
|
+
export declare function createFullSyncRoute(status: () => FullSyncView, trigger: () => FullSyncTrigger): WebRoute;
|
|
80
|
+
/**
|
|
81
|
+
* Build the stats route over a data directory the caller may relocate live.
|
|
82
|
+
* @param dir - reads the directory currently in force (per request, so a
|
|
83
|
+
* settings-driven move starts answering from the new location at once).
|
|
84
|
+
* @param options - the currency thunk; defaults to CNY (the domestic default).
|
|
85
|
+
* @returns the exact GET route serving the JSON summary.
|
|
86
|
+
*/
|
|
87
|
+
export declare function createStatsRoute(dir: () => string, options?: StatsRouteOptions): WebRoute;
|
|
88
|
+
//# sourceMappingURL=stats-route.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stats-route.d.ts","sourceRoot":"","sources":["../src/stats-route.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AAChD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAA;AAC/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAOrD,OAAO,KAAK,EAAgD,kBAAkB,EAAE,eAAe,EAAE,YAAY,EAAyD,MAAM,WAAW,CAAA;AAGvL,uEAAuE;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAEtC,0EAA0E;AAC1E,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAE1C,uEAAuE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