opencode-token-tracker 1.5.3 → 1.5.4
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/index.js +29 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BUILTIN_PRICING, DEFAULT_CONFIG, findModelConfigPricing, formatCost, formatTokens, getStartOfDay, getStartOfWeek, getStartOfMonth, validateConfig } from "./lib/shared.js";
|
|
2
|
-
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "fs";
|
|
2
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, statSync } from "fs";
|
|
3
3
|
import { join } from "path";
|
|
4
4
|
import { homedir } from "os";
|
|
5
5
|
const CONFIG_DIR = join(homedir(), ".config", "opencode");
|
|
@@ -11,6 +11,8 @@ const LOG_FILE = join(LOG_DIR, "tokens.jsonl");
|
|
|
11
11
|
// ============================================================================
|
|
12
12
|
let config = DEFAULT_CONFIG;
|
|
13
13
|
let configWarnings = [];
|
|
14
|
+
let lastConfigLoadTime = 0;
|
|
15
|
+
let lastConfigMtime = 0;
|
|
14
16
|
function loadConfig() {
|
|
15
17
|
try {
|
|
16
18
|
if (existsSync(CONFIG_FILE)) {
|
|
@@ -27,6 +29,26 @@ function loadConfig() {
|
|
|
27
29
|
}
|
|
28
30
|
return DEFAULT_CONFIG;
|
|
29
31
|
}
|
|
32
|
+
function ensureLatestConfig() {
|
|
33
|
+
const now = Date.now();
|
|
34
|
+
if (now - lastConfigLoadTime < 2000) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
lastConfigLoadTime = now;
|
|
38
|
+
try {
|
|
39
|
+
if (existsSync(CONFIG_FILE)) {
|
|
40
|
+
const stat = statSync(CONFIG_FILE);
|
|
41
|
+
const mtime = stat.mtimeMs;
|
|
42
|
+
if (mtime !== lastConfigMtime) {
|
|
43
|
+
config = loadConfig();
|
|
44
|
+
lastConfigMtime = mtime;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// Keep current config on error
|
|
50
|
+
}
|
|
51
|
+
}
|
|
30
52
|
// ============================================================================
|
|
31
53
|
// Pricing
|
|
32
54
|
// ============================================================================
|
|
@@ -270,6 +292,10 @@ export const TokenTrackerPlugin = async ({ directory, client }) => {
|
|
|
270
292
|
try {
|
|
271
293
|
// Load config on plugin init (with validation)
|
|
272
294
|
config = loadConfig();
|
|
295
|
+
lastConfigLoadTime = Date.now();
|
|
296
|
+
if (existsSync(CONFIG_FILE)) {
|
|
297
|
+
lastConfigMtime = statSync(CONFIG_FILE).mtimeMs;
|
|
298
|
+
}
|
|
273
299
|
// Initialize in-memory budget tracker (reads JSONL once)
|
|
274
300
|
initBudgetTracker();
|
|
275
301
|
logJson({ type: "init", directory, configLoaded: existsSync(CONFIG_FILE) });
|
|
@@ -292,6 +318,7 @@ export const TokenTrackerPlugin = async ({ directory, client }) => {
|
|
|
292
318
|
try {
|
|
293
319
|
// Handle message updates (token tracking)
|
|
294
320
|
if (event.type === "message.updated") {
|
|
321
|
+
ensureLatestConfig();
|
|
295
322
|
const props = event.properties;
|
|
296
323
|
const info = props?.info;
|
|
297
324
|
if (!info?.tokens)
|
|
@@ -376,6 +403,7 @@ export const TokenTrackerPlugin = async ({ directory, client }) => {
|
|
|
376
403
|
}
|
|
377
404
|
// Handle session idle (show summary)
|
|
378
405
|
if (event.type === "session.idle") {
|
|
406
|
+
ensureLatestConfig();
|
|
379
407
|
if (!config.toast.enabled || !config.toast.showOnIdle)
|
|
380
408
|
return;
|
|
381
409
|
const props = event.properties;
|
package/package.json
CHANGED