tokenusage-cli 0.1.0
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/CHANGELOG.md +19 -0
- package/LICENSE +22 -0
- package/README.md +94 -0
- package/THIRD_PARTY_NOTICES.md +140 -0
- package/bin/tokenusage.js +69 -0
- package/package.json +59 -0
- package/src/cli.js +134 -0
- package/src/commands/device-login.js +205 -0
- package/src/commands/diagnostics.js +38 -0
- package/src/commands/doctor.js +110 -0
- package/src/commands/init.js +2436 -0
- package/src/commands/sessions.js +29 -0
- package/src/commands/status.js +1747 -0
- package/src/commands/sync.js +7934 -0
- package/src/commands/uninstall.js +312 -0
- package/src/lib/account-usage-dedup.js +267 -0
- package/src/lib/antigravity-paths.js +48 -0
- package/src/lib/ark-agent-plan-limits.js +438 -0
- package/src/lib/ark-coding-plan-limits.js +537 -0
- package/src/lib/async-queue-reader.js +138 -0
- package/src/lib/atomic-json.js +27 -0
- package/src/lib/bearer-token.js +11 -0
- package/src/lib/categorizer-utils.js +232 -0
- package/src/lib/claude-categorizer.js +1343 -0
- package/src/lib/claude-config.js +279 -0
- package/src/lib/claude-user-identity.js +56 -0
- package/src/lib/cli-ui.js +179 -0
- package/src/lib/cloud-account.js +541 -0
- package/src/lib/cloud-device-token.js +141 -0
- package/src/lib/cloud-sync-prefs.js +14 -0
- package/src/lib/cloud-usage-reset.js +115 -0
- package/src/lib/codex-config.js +595 -0
- package/src/lib/codex-context-breakdown.js +806 -0
- package/src/lib/codex-model-attribution.js +128 -0
- package/src/lib/codex-rollout-parser.js +1471 -0
- package/src/lib/codex-service-tier.js +49 -0
- package/src/lib/codex-token-refresh.js +140 -0
- package/src/lib/codex-token-usage.js +195 -0
- package/src/lib/codex-usage-record.js +195 -0
- package/src/lib/command-runner.js +313 -0
- package/src/lib/commandcode-limits.js +317 -0
- package/src/lib/context-health.js +124 -0
- package/src/lib/cursor-config.js +605 -0
- package/src/lib/cursor-store.js +1918 -0
- package/src/lib/debug-flags.js +7 -0
- package/src/lib/devin-limits.js +249 -0
- package/src/lib/devin-usage.js +212 -0
- package/src/lib/diagnostics.js +383 -0
- package/src/lib/doctor.js +385 -0
- package/src/lib/dumate.js +95 -0
- package/src/lib/fs.js +455 -0
- package/src/lib/function-url.js +14 -0
- package/src/lib/gemini-config.js +291 -0
- package/src/lib/git-outcomes.js +243 -0
- package/src/lib/grok-context-breakdown.js +890 -0
- package/src/lib/grok-hook.js +360 -0
- package/src/lib/grok-limits.js +865 -0
- package/src/lib/grok-usage.js +98 -0
- package/src/lib/heatmap-compact.js +114 -0
- package/src/lib/ingest-sanitize.js +41 -0
- package/src/lib/init-flow.js +42 -0
- package/src/lib/install-resolver.js +170 -0
- package/src/lib/jsonl-lines.js +134 -0
- package/src/lib/kimi-model-repair.js +173 -0
- package/src/lib/kimi-profile.js +61 -0
- package/src/lib/kimi-session.js +216 -0
- package/src/lib/local-api.js +3588 -0
- package/src/lib/machine-id.js +238 -0
- package/src/lib/multi-install-parser.js +113 -0
- package/src/lib/notify-command.js +38 -0
- package/src/lib/omp-hook.js +589 -0
- package/src/lib/openclaw-cli.js +104 -0
- package/src/lib/openclaw-hook.js +375 -0
- package/src/lib/openclaw-session-plugin.js +560 -0
- package/src/lib/opencode-config.js +121 -0
- package/src/lib/opencode-go-limits.js +807 -0
- package/src/lib/outcomes-engine.js +253 -0
- package/src/lib/passive-mode.js +219 -0
- package/src/lib/pet-packages.js +722 -0
- package/src/lib/pricing/curated-overrides.json +822 -0
- package/src/lib/pricing/index.js +379 -0
- package/src/lib/pricing/litellm-fetcher.js +172 -0
- package/src/lib/pricing/matcher.js +456 -0
- package/src/lib/pricing/seed-snapshot.json +1 -0
- package/src/lib/progress.js +76 -0
- package/src/lib/project-usage-purge.js +117 -0
- package/src/lib/prompt.js +24 -0
- package/src/lib/provider-status.js +104 -0
- package/src/lib/proxy-env.js +489 -0
- package/src/lib/proxy-settings.js +163 -0
- package/src/lib/qoder-cn-cli-limits.js +119 -0
- package/src/lib/qoder-limits.js +880 -0
- package/src/lib/quiet-warnings.js +26 -0
- package/src/lib/rollout.js +25966 -0
- package/src/lib/runtime-config.js +259 -0
- package/src/lib/scan-roots.js +277 -0
- package/src/lib/sentry.js +231 -0
- package/src/lib/session-analytics.js +2972 -0
- package/src/lib/session-performance.js +237 -0
- package/src/lib/session-upload.js +379 -0
- package/src/lib/skill-usage.js +267 -0
- package/src/lib/skills-manager.js +1591 -0
- package/src/lib/source-metadata.js +48 -0
- package/src/lib/sqlite-reader.js +215 -0
- package/src/lib/star-cta.js +73 -0
- package/src/lib/subscription-manager.js +293 -0
- package/src/lib/subscriptions.js +508 -0
- package/src/lib/telemetry.js +193 -0
- package/src/lib/tracker-paths.js +16 -0
- package/src/lib/trae-cn-config.js +548 -0
- package/src/lib/trae-db.js +127 -0
- package/src/lib/trae-snapshot.js +230 -0
- package/src/lib/trae-sqlite.js +143 -0
- package/src/lib/trae-usage.js +121 -0
- package/src/lib/upload-queue.js +134 -0
- package/src/lib/upload-throttle.js +171 -0
- package/src/lib/usage-limits.js +4552 -0
- package/src/lib/wrapped-aggregator.js +236 -0
- package/src/lib/wsl-probe.js +281 -0
- package/src/lib/zcode-limits.js +1172 -0
- package/src/lib/zcode-reset-credits.js +66 -0
- package/test/cli-smoke.test.js +53 -0
- package/test/ingest-sanitize.test.js +162 -0
- package/test/quiet-warnings.test.js +53 -0
- package/test/session-upload.test.js +427 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("node:fs/promises");
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
const crypto = require("node:crypto");
|
|
6
|
+
|
|
7
|
+
const KIMI_MODEL_REPAIR_KEY = "kimiModelAttribution_2026_10";
|
|
8
|
+
const TOKEN_FIELDS = ["input_tokens", "cached_input_tokens", "cache_creation_input_tokens", "output_tokens", "reasoning_output_tokens", "total_tokens"];
|
|
9
|
+
const keyOf = (row) => row?.source === "kimi" && typeof row.model === "string" && typeof row.hour_start === "string"
|
|
10
|
+
? `kimi|${row.model}|${row.hour_start}` : null;
|
|
11
|
+
const digest = (bytes) => crypto.createHash("sha256").update(bytes).digest("hex");
|
|
12
|
+
|
|
13
|
+
async function readBytes(file) {
|
|
14
|
+
try { return await fs.readFile(file); } catch (error) { if (error.code === "ENOENT") return Buffer.alloc(0); throw error; }
|
|
15
|
+
}
|
|
16
|
+
async function readJson(file) {
|
|
17
|
+
const bytes = await readBytes(file);
|
|
18
|
+
return bytes.length ? JSON.parse(bytes.toString("utf8")) : null;
|
|
19
|
+
}
|
|
20
|
+
function latestRows(bytes) {
|
|
21
|
+
const rows = new Map();
|
|
22
|
+
for (const line of bytes.toString("utf8").split("\n")) {
|
|
23
|
+
let row;
|
|
24
|
+
try { row = JSON.parse(line); } catch { continue; }
|
|
25
|
+
const key = keyOf(row);
|
|
26
|
+
if (key) rows.set(key, row);
|
|
27
|
+
}
|
|
28
|
+
return rows;
|
|
29
|
+
}
|
|
30
|
+
function sumsByHour(rows) {
|
|
31
|
+
const totals = new Map();
|
|
32
|
+
for (const row of rows.values()) {
|
|
33
|
+
const total = totals.get(row.hour_start) || Object.fromEntries(TOKEN_FIELDS.map((field) => [field, 0]));
|
|
34
|
+
for (const field of TOKEN_FIELDS) {
|
|
35
|
+
const count = Number(row[field] || 0);
|
|
36
|
+
if (!Number.isSafeInteger(count) || count < 0) throw new Error("Kimi repair encountered invalid token totals");
|
|
37
|
+
total[field] += count;
|
|
38
|
+
}
|
|
39
|
+
if (TOKEN_FIELDS.some((field) => total[field] !== 0)) totals.set(row.hour_start, total);
|
|
40
|
+
}
|
|
41
|
+
return totals;
|
|
42
|
+
}
|
|
43
|
+
function sameTotals(before, after) {
|
|
44
|
+
const left = sumsByHour(before), right = sumsByHour(after);
|
|
45
|
+
return left.size === right.size && [...left].every(([hour, total]) => right.has(hour)
|
|
46
|
+
&& TOKEN_FIELDS.every((field) => total[field] === right.get(hour)[field]));
|
|
47
|
+
}
|
|
48
|
+
function sameSnapshot(left, right) {
|
|
49
|
+
return left && right && [...TOKEN_FIELDS, "conversation_count", "billable_total_tokens"]
|
|
50
|
+
.every((field) => Number(left[field] || 0) === Number(right[field] || 0));
|
|
51
|
+
}
|
|
52
|
+
async function fileSignature(file) {
|
|
53
|
+
const handle = await fs.open(file, "r");
|
|
54
|
+
try {
|
|
55
|
+
const stat = await handle.stat();
|
|
56
|
+
if (!stat.isFile()) throw new Error("Kimi repair source is not a file");
|
|
57
|
+
return { size: stat.size, ino: stat.ino, mtimeMs: stat.mtimeMs };
|
|
58
|
+
} finally { await handle.close(); }
|
|
59
|
+
}
|
|
60
|
+
async function writeJournal(file, journal) {
|
|
61
|
+
const temp = `${file}.${crypto.randomUUID()}.tmp`;
|
|
62
|
+
try {
|
|
63
|
+
await fs.writeFile(temp, JSON.stringify(journal), { mode: 0o600, flag: "wx" });
|
|
64
|
+
await fs.rename(temp, file);
|
|
65
|
+
} finally { await fs.rm(temp, { force: true }); }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Caller holds sync.lock and commits cursors after this returns, before upload.
|
|
69
|
+
// Only append to queue.jsonl; the caller compacts its pending suffix with
|
|
70
|
+
// minPendingBytes:0 after cursor commit, preserving uploaded bytes and offset.
|
|
71
|
+
// Parsers are injected to keep rollout.js independent of its migration helper.
|
|
72
|
+
async function repairKimiModelAttribution({ cursors, queuePath, queueStatePath, legacyFiles = [], officialFiles = [], parseLegacy, parseOfficial, env = process.env }) {
|
|
73
|
+
const journalPath = `${queuePath}.kimi-model-repair.json`;
|
|
74
|
+
const migrations = cursors.migrations || {};
|
|
75
|
+
const applied = migrations[KIMI_MODEL_REPAIR_KEY];
|
|
76
|
+
if (applied?.status === "applied" || applied?.status === "noop") {
|
|
77
|
+
await fs.rm(journalPath, { force: true });
|
|
78
|
+
return { repaired: false, pendingCompaction: applied.pendingCompaction === true };
|
|
79
|
+
}
|
|
80
|
+
let journal = await readJson(journalPath);
|
|
81
|
+
if (!journal) {
|
|
82
|
+
const rawQueue = await readBytes(queuePath);
|
|
83
|
+
const live = latestRows(rawQueue);
|
|
84
|
+
for (const [key, bucket] of Object.entries(cursors.hourly?.buckets || {})) {
|
|
85
|
+
if (!key.startsWith("kimi|") || live.has(key)) continue;
|
|
86
|
+
const [, model, hour_start] = key.split("|");
|
|
87
|
+
live.set(key, { source: "kimi", model, hour_start, ...bucket.totals });
|
|
88
|
+
}
|
|
89
|
+
if (![...live.values()].some((row) => Number(row.total_tokens) > 0)) {
|
|
90
|
+
(cursors.migrations ||= {})[KIMI_MODEL_REPAIR_KEY] = { status: "noop", appliedAt: new Date().toISOString() };
|
|
91
|
+
return { repaired: false, pendingCompaction: false };
|
|
92
|
+
}
|
|
93
|
+
const files = [...new Set([...legacyFiles, ...officialFiles])];
|
|
94
|
+
const discovered = new Set(files);
|
|
95
|
+
const previousFiles = Object.entries(cursors.kimi?.fileOffsets || {}).concat(Object.entries(cursors.kimiCode?.fileOffsets || {}));
|
|
96
|
+
if (!officialFiles.length || previousFiles.some(([file]) => !discovered.has(file))) {
|
|
97
|
+
return { repaired: false, pendingCompaction: false, deferred: "missing-source" };
|
|
98
|
+
}
|
|
99
|
+
const signatures = new Map();
|
|
100
|
+
try {
|
|
101
|
+
for (const file of files) signatures.set(file, await fileSignature(file));
|
|
102
|
+
if (previousFiles.some(([file, prior]) => signatures.get(file).size < Number(prior.size || 0))) {
|
|
103
|
+
return { repaired: false, pendingCompaction: false, deferred: "truncated-source" };
|
|
104
|
+
}
|
|
105
|
+
} catch {
|
|
106
|
+
return { repaired: false, pendingCompaction: false, deferred: "unreadable-source" };
|
|
107
|
+
}
|
|
108
|
+
const scratch = await fs.mkdtemp(path.join(path.dirname(queuePath), ".kimi-model-repair-"));
|
|
109
|
+
const rebuilt = { version: 1, hourly: { version: 3, buckets: {}, groupQueued: {} } };
|
|
110
|
+
let corrected;
|
|
111
|
+
try {
|
|
112
|
+
const scratchQueue = path.join(scratch, "queue.jsonl");
|
|
113
|
+
await parseLegacy({ wireFiles: legacyFiles, cursors: rebuilt, queuePath: scratchQueue, env });
|
|
114
|
+
await parseOfficial({ wireFiles: officialFiles, cursors: rebuilt, queuePath: scratchQueue, env });
|
|
115
|
+
corrected = latestRows(await readBytes(scratchQueue));
|
|
116
|
+
for (const file of files) {
|
|
117
|
+
if (JSON.stringify(await fileSignature(file)) !== JSON.stringify(signatures.get(file))) {
|
|
118
|
+
return { repaired: false, pendingCompaction: false, deferred: "source-changed" };
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
} finally { await fs.rm(scratch, { recursive: true, force: true }); }
|
|
122
|
+
// Missing/corrupt history or newly appended usage must first settle through
|
|
123
|
+
// ordinary sync. A model-only repair may never change per-hour token totals.
|
|
124
|
+
if (!sameTotals(live, corrected)) return { repaired: false, pendingCompaction: false, deferred: "totals-not-conserved" };
|
|
125
|
+
const currentQueue = await readBytes(queuePath);
|
|
126
|
+
if (!currentQueue.equals(rawQueue)) return { repaired: false, pendingCompaction: false, deferred: "queue-changed" };
|
|
127
|
+
const state = await readJson(queueStatePath) || {};
|
|
128
|
+
const offset = Number(state.offset || 0);
|
|
129
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || offset > rawQueue.length || (offset > 0 && rawQueue[offset - 1] !== 10)) {
|
|
130
|
+
return { repaired: false, pendingCompaction: false, deferred: "invalid-upload-offset" };
|
|
131
|
+
}
|
|
132
|
+
const corrections = [];
|
|
133
|
+
for (const [key, old] of live) {
|
|
134
|
+
if (!corrected.has(key)) corrections.push({ source: "kimi", model: old.model, hour_start: old.hour_start,
|
|
135
|
+
...Object.fromEntries(TOKEN_FIELDS.map((field) => [field, 0])), billable_total_tokens: 0, conversation_count: 0 });
|
|
136
|
+
}
|
|
137
|
+
for (const [key, row] of corrected) if (!sameSnapshot(live.get(key), row)) corrections.push(row);
|
|
138
|
+
journal = { version: 1, uploadedOffset: offset, uploadedPrefixSha256: digest(rawQueue.subarray(0, offset)),
|
|
139
|
+
rows: corrections, kimi: rebuilt.kimi || {}, kimiCode: rebuilt.kimiCode || {}, hourly: rebuilt.hourly,
|
|
140
|
+
preparedAt: new Date().toISOString() };
|
|
141
|
+
await writeJournal(journalPath, journal);
|
|
142
|
+
}
|
|
143
|
+
const rawQueue = await readBytes(queuePath);
|
|
144
|
+
const state = await readJson(queueStatePath) || {};
|
|
145
|
+
if (journal.version !== 1 || !Number.isSafeInteger(journal.uploadedOffset) || journal.uploadedOffset < 0
|
|
146
|
+
|| rawQueue.length < journal.uploadedOffset || Number(state.offset || 0) < journal.uploadedOffset
|
|
147
|
+
|| digest(rawQueue.subarray(0, journal.uploadedOffset)) !== journal.uploadedPrefixSha256) {
|
|
148
|
+
return { repaired: false, pendingCompaction: false, deferred: "uploaded-prefix-changed" };
|
|
149
|
+
}
|
|
150
|
+
if (journal.rows.length) {
|
|
151
|
+
// A prior append can have stopped halfway through a JSON line. Preserve
|
|
152
|
+
// those bytes, terminate that incomplete record, then append complete LWW
|
|
153
|
+
// snapshots again. The journal bypasses conservation checks on partial work.
|
|
154
|
+
const separator = rawQueue.length && rawQueue.at(-1) !== 10 ? "\n" : "";
|
|
155
|
+
await fs.appendFile(queuePath, separator + journal.rows.map(JSON.stringify).join("\n") + "\n");
|
|
156
|
+
}
|
|
157
|
+
const hourly = (cursors.hourly ||= { version: 3, buckets: {}, groupQueued: {} });
|
|
158
|
+
for (const field of ["buckets", "groupQueued"]) {
|
|
159
|
+
hourly[field] ||= {};
|
|
160
|
+
for (const key of Object.keys(hourly[field])) if (key.startsWith("kimi|")) delete hourly[field][key];
|
|
161
|
+
for (const [key, value] of Object.entries(journal.hourly?.[field] || {})) if (key.startsWith("kimi|")) hourly[field][key] = value;
|
|
162
|
+
}
|
|
163
|
+
cursors.kimi = journal.kimi;
|
|
164
|
+
cursors.kimiCode = journal.kimiCode;
|
|
165
|
+
(cursors.migrations ||= {})[KIMI_MODEL_REPAIR_KEY] = {
|
|
166
|
+
status: "applied", appliedAt: new Date().toISOString(), correctedBuckets: journal.rows.length,
|
|
167
|
+
pendingCompaction: journal.rows.length > 0,
|
|
168
|
+
};
|
|
169
|
+
// Leave the journal until the next call observes the persisted marker.
|
|
170
|
+
return { repaired: true, pendingCompaction: journal.rows.length > 0 };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
module.exports = { KIMI_MODEL_REPAIR_KEY, repairKimiModelAttribution };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const os = require("node:os");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const { parse } = require("smol-toml");
|
|
7
|
+
const { expandHome } = require("./scan-roots");
|
|
8
|
+
|
|
9
|
+
const MAINLAND = { oauthHost: "https://auth.kimi.com", baseUrl: "https://api.kimi.com/coding/v1" };
|
|
10
|
+
const GLOBAL = { oauthHost: "https://auth.kimi.ai", baseUrl: "https://api.kimi.ai/coding/v1" };
|
|
11
|
+
|
|
12
|
+
function endpoint(value, fallback) {
|
|
13
|
+
return typeof value === "string" ? value.trim().replace(/\/+$/, "") : fallback;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// The two supported official endpoint pairs have stable upstream credential
|
|
17
|
+
// slots. The global suffix is SHA-256(JSON.stringify({ oauthHost, baseUrl }))
|
|
18
|
+
// truncated to 16 hex characters, matching managed-kimi-code.ts. Keeping the
|
|
19
|
+
// filenames constant also prevents remote configuration from shaping paths.
|
|
20
|
+
function credentialKey(oauthHost, baseUrl) {
|
|
21
|
+
return oauthHost === GLOBAL.oauthHost && baseUrl === GLOBAL.baseUrl
|
|
22
|
+
? "kimi-code-env-0e4f99c69cc27850" : "kimi-code";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function resolveKimiProfile({ home, env = process.env } = {}) {
|
|
26
|
+
const base = home || os.homedir();
|
|
27
|
+
const explicit = expandHome(env?.KIMI_HOME, base);
|
|
28
|
+
const explicitCode = expandHome(env?.KIMI_CODE_HOME, base);
|
|
29
|
+
const codeHome = explicit || explicitCode || path.join(base, ".kimi-code");
|
|
30
|
+
const codeConfig = path.join(codeHome, "config.toml");
|
|
31
|
+
let provider = null;
|
|
32
|
+
let error = null;
|
|
33
|
+
if (fs.existsSync(codeConfig)) {
|
|
34
|
+
try {
|
|
35
|
+
provider = parse(fs.readFileSync(codeConfig, "utf8"))?.providers?.["managed:kimi-code"] || null;
|
|
36
|
+
} catch {
|
|
37
|
+
error = "Could not parse Kimi Code config.toml";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let hasCodeLogin = false;
|
|
41
|
+
try {
|
|
42
|
+
hasCodeLogin = Boolean(JSON.parse(fs.readFileSync(path.join(codeHome, "credentials", "kimi-code.json"), "utf8"))?.access_token);
|
|
43
|
+
} catch { /* No default-slot login. */ }
|
|
44
|
+
const hasEnvOverride = env?.KIMI_CODE_BASE_URL != null || env?.KIMI_CODE_OAUTH_HOST != null || env?.KIMI_OAUTH_HOST != null;
|
|
45
|
+
const kimiHome = explicit || explicitCode || provider || error || hasCodeLogin || hasEnvOverride
|
|
46
|
+
? codeHome : path.join(base, ".kimi");
|
|
47
|
+
const oauthHost = endpoint(hasEnvOverride ? env?.KIMI_CODE_OAUTH_HOST ?? env?.KIMI_OAUTH_HOST : provider?.oauth?.oauth_host, MAINLAND.oauthHost);
|
|
48
|
+
const baseUrl = endpoint(env?.KIMI_CODE_BASE_URL ?? provider?.base_url, MAINLAND.baseUrl);
|
|
49
|
+
if (![MAINLAND, GLOBAL].some((region) => region.oauthHost === oauthHost && region.baseUrl === baseUrl)) {
|
|
50
|
+
error = "Unsupported Kimi quota endpoints; configure a matching official mainland or global profile";
|
|
51
|
+
}
|
|
52
|
+
if (provider?.oauth?.storage && provider.oauth.storage !== "file") {
|
|
53
|
+
error = "Kimi quota supports file credentials; the configured credential storage is unsupported";
|
|
54
|
+
}
|
|
55
|
+
// Derive the endpoint-scoped slot, just as upstream does when a stored key
|
|
56
|
+
// mismatches the endpoints. Never read an unrelated slot or legacy login.
|
|
57
|
+
const credsPath = path.join(kimiHome, "credentials", `${credentialKey(oauthHost, baseUrl)}.json`);
|
|
58
|
+
return { home: kimiHome, credsPath, oauthHost, baseUrl, error, configured: fs.existsSync(path.join(kimiHome, "config.toml")) };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = { resolveKimiProfile };
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const crypto = require("node:crypto");
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const { physicalJsonlRecords } = require("./jsonl-lines");
|
|
7
|
+
const { resolveKimiDefaultModel } = require("./rollout");
|
|
8
|
+
const { mergePerformance } = require("./session-performance");
|
|
9
|
+
|
|
10
|
+
function readObject(file) {
|
|
11
|
+
try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return {}; }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function kimiSessionLocation(file) {
|
|
15
|
+
const official = path.basename(path.dirname(path.dirname(file))) === "agents";
|
|
16
|
+
const directory = official ? path.dirname(path.dirname(path.dirname(file))) : path.dirname(file);
|
|
17
|
+
const id = path.basename(directory);
|
|
18
|
+
const imported = official ? readObject(path.join(directory, "state.json"))?.custom : null;
|
|
19
|
+
// The official migrator keeps the legacy UUID under ses_<UUID>. Group its
|
|
20
|
+
// message-only mirror with the original wire, retaining any new usage tail.
|
|
21
|
+
const identity = imported?.imported_from_kimi_cli && typeof imported.kimi_cli_session_id === "string"
|
|
22
|
+
? imported.kimi_cli_session_id : id.replace(/^(?:ses_|session_)(?=[0-9a-f-]{36}$)/i, "");
|
|
23
|
+
return { directory, id, identity, official, main: !official || path.basename(path.dirname(file)) === "main" };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function kimiSessionDependencies(files) {
|
|
27
|
+
return [...new Set(files.flatMap((file) => {
|
|
28
|
+
const location = kimiSessionLocation(file);
|
|
29
|
+
const root = path.dirname(path.dirname(path.dirname(location.directory)));
|
|
30
|
+
return [path.join(location.directory, "state.json"), path.join(root, "config.toml"), path.join(root, "kimi.json")];
|
|
31
|
+
}))];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function discoverKimiWireFiles(roots, incomplete) {
|
|
35
|
+
const files = new Set();
|
|
36
|
+
function walk(directory, depth = 0) {
|
|
37
|
+
if (depth > 5) return;
|
|
38
|
+
let entries;
|
|
39
|
+
try { entries = fs.readdirSync(directory, { withFileTypes: true }); } catch (error) {
|
|
40
|
+
if (error.code !== "ENOENT") incomplete.push({ path: directory, error: error.code || "unreadable" });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
for (const entry of entries) {
|
|
44
|
+
if (entry.name.startsWith(".")) continue;
|
|
45
|
+
const file = path.join(directory, entry.name);
|
|
46
|
+
if (entry.isDirectory()) walk(file, depth + 1);
|
|
47
|
+
else if (entry.isFile() && entry.name === "wire.jsonl") files.add(file);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
for (const root of roots) walk(path.join(root, "sessions"));
|
|
51
|
+
return [...files].sort();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const count = (value) => Number.isFinite(Number(value)) ? Math.max(0, Math.floor(Number(value))) : 0;
|
|
55
|
+
function kimiUsage(usage, legacy) {
|
|
56
|
+
if (!usage || typeof usage !== "object") return null;
|
|
57
|
+
let input, cached, created, output;
|
|
58
|
+
// Same column semantics as parseKimiIncremental / parseKimiCodeIncremental.
|
|
59
|
+
if (legacy || usage.inputOther != null) {
|
|
60
|
+
input = count(legacy ? usage.input_other : usage.inputOther);
|
|
61
|
+
cached = count(legacy ? usage.input_cache_read : usage.inputCacheRead);
|
|
62
|
+
created = count(legacy ? usage.input_cache_creation : usage.inputCacheCreation);
|
|
63
|
+
output = count(usage.output);
|
|
64
|
+
} else {
|
|
65
|
+
created = count(usage.cache_creation_input_tokens);
|
|
66
|
+
cached = count(usage.cache_read_input_tokens ?? usage.input_tokens_details?.cached_tokens);
|
|
67
|
+
input = Math.max(0, count(usage.input_tokens) - (usage.cache_read_input_tokens != null ? 0 : cached));
|
|
68
|
+
output = count(usage.output_tokens);
|
|
69
|
+
}
|
|
70
|
+
return { input_tokens: input, cached_input_tokens: cached, cache_creation_input_tokens: created,
|
|
71
|
+
output_tokens: output, reasoning_output_tokens: 0, total_tokens: input + cached + created + output };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const modelAlias = (value) => typeof value === "string" && value.trim() ? value.trim().split("/").pop() : null;
|
|
75
|
+
const EDIT_TOOLS = new Set(["edit", "write", "multiedit", "notebookedit", "strreplacefile", "writefile", "apply_patch"]);
|
|
76
|
+
const AGENT_TOOLS = new Set(["agent", "agentswarm", "task"]);
|
|
77
|
+
const hash = (value) => crypto.createHash("sha256").update(value).digest("hex");
|
|
78
|
+
|
|
79
|
+
// Reads both wire protocols into metadata only. A full inventory shares its
|
|
80
|
+
// usage-ID set, as sync does, so copied/forked step records count only once.
|
|
81
|
+
async function readKimiSessions(files) {
|
|
82
|
+
const groups = new Map();
|
|
83
|
+
for (const file of [...new Set(files)]) {
|
|
84
|
+
const location = kimiSessionLocation(file);
|
|
85
|
+
const group = groups.get(location.identity) || { identity: location.identity, files: [], createdAt: Infinity };
|
|
86
|
+
const state = location.official ? readObject(path.join(location.directory, "state.json")) : {};
|
|
87
|
+
group.createdAt = Math.min(group.createdAt, Number(state.createdAt) || Infinity);
|
|
88
|
+
group.files.push({ file, ...location, state });
|
|
89
|
+
groups.set(location.identity, group);
|
|
90
|
+
}
|
|
91
|
+
const seenUsage = new Set();
|
|
92
|
+
const seenSignals = new Set();
|
|
93
|
+
const rows = [];
|
|
94
|
+
for (const group of [...groups.values()].sort((a, b) => a.createdAt - b.createdAt || a.identity.localeCompare(b.identity))) {
|
|
95
|
+
const tokens = kimiUsage({}, true);
|
|
96
|
+
const modelUsage = new Map();
|
|
97
|
+
const buckets = new Map();
|
|
98
|
+
const times = new Set();
|
|
99
|
+
let turns = 0, edits = 0, retries = 0, agents = 0, errors = 0;
|
|
100
|
+
let lastPrompt = null;
|
|
101
|
+
let cwd = null;
|
|
102
|
+
let resumeId = null;
|
|
103
|
+
const modelsForEdits = new Map();
|
|
104
|
+
// Legacy first preserves migrated user turns; context.append_message in
|
|
105
|
+
// the imported wire is history, never another prompt or usage event.
|
|
106
|
+
group.files.sort((a, b) => Number(a.official) - Number(b.official) || Number(b.main) - Number(a.main) || a.file.localeCompare(b.file));
|
|
107
|
+
for (const location of group.files) {
|
|
108
|
+
const root = path.dirname(path.dirname(path.dirname(location.directory)));
|
|
109
|
+
let model = resolveKimiDefaultModel({ KIMI_HOME: root });
|
|
110
|
+
if (location.official && location.main) {
|
|
111
|
+
resumeId = location.id;
|
|
112
|
+
if (typeof location.state.workDir === "string") cwd = location.state.workDir;
|
|
113
|
+
}
|
|
114
|
+
if (!location.official && !cwd) {
|
|
115
|
+
const workspaceHash = path.basename(path.dirname(location.directory));
|
|
116
|
+
const workDirs = readObject(path.join(root, "kimi.json"))?.work_dirs;
|
|
117
|
+
const workspace = Array.isArray(workDirs) && workDirs.find((row) => typeof row?.path === "string"
|
|
118
|
+
&& crypto.createHash("md5").update(row.path).digest("hex") === workspaceHash);
|
|
119
|
+
if (workspace) cwd = workspace.path;
|
|
120
|
+
}
|
|
121
|
+
let turnOpen = false, hadEdit = false, turnModel = model;
|
|
122
|
+
const closeTurn = () => {
|
|
123
|
+
if (hadEdit) { edits++; modelsForEdits.set(turnModel, (modelsForEdits.get(turnModel) || 0) + 1); }
|
|
124
|
+
hadEdit = false; turnOpen = false;
|
|
125
|
+
};
|
|
126
|
+
const input = fs.createReadStream(location.file);
|
|
127
|
+
for await (const { line, terminated } of physicalJsonlRecords(input)) {
|
|
128
|
+
if (!terminated || !line) continue;
|
|
129
|
+
let entry;
|
|
130
|
+
try { entry = JSON.parse(line); } catch { continue; }
|
|
131
|
+
if (!entry || typeof entry !== "object") continue;
|
|
132
|
+
const message = entry.message && typeof entry.message === "object" ? entry.message : null;
|
|
133
|
+
const event = entry.type === "context.append_loop_event" ? entry.event : entry;
|
|
134
|
+
const timestampMs = Number(location.official ? entry.time ?? event?.time : entry.timestamp ?? message?.payload?.timestamp) * (location.official ? 1 : 1000);
|
|
135
|
+
const timestamp = Number.isFinite(timestampMs) && timestampMs > 0 && timestampMs <= 8640000000000000
|
|
136
|
+
? new Date(timestampMs).toISOString() : null;
|
|
137
|
+
if (timestamp) times.add(timestampMs);
|
|
138
|
+
if (["config.update", "profile.bind", "llm.request"].includes(entry.type)) {
|
|
139
|
+
model = modelAlias(entry.modelAlias || entry.model) || model;
|
|
140
|
+
if (location.main && typeof (entry.cwd || entry.environmentDisclosure?.cwd) === "string") cwd = entry.cwd || entry.environmentDisclosure.cwd;
|
|
141
|
+
}
|
|
142
|
+
const isLegacyUsage = message?.type === "StatusUpdate";
|
|
143
|
+
const usage = isLegacyUsage ? message.payload?.token_usage : event?.type === "step.end" ? event.usage : null;
|
|
144
|
+
const usageId = isLegacyUsage ? message.payload?.message_id : event?.uuid;
|
|
145
|
+
if (usage && typeof usageId === "string" && timestamp) {
|
|
146
|
+
const ids = [isLegacyUsage ? `message:${usageId}` : `step:${usageId}`];
|
|
147
|
+
if (!isLegacyUsage && typeof event.messageId === "string") ids.push(`message:${event.messageId}`);
|
|
148
|
+
const delta = kimiUsage(usage, isLegacyUsage);
|
|
149
|
+
if (delta.total_tokens > 0 && !ids.some((id) => seenUsage.has(id))) {
|
|
150
|
+
for (const id of ids) seenUsage.add(id);
|
|
151
|
+
const modelRow = modelUsage.get(model) || { model, ...kimiUsage({}, true), usage_events: 0 };
|
|
152
|
+
const bucketTime = new Date(Math.floor(timestampMs / 60000) * 60000).toISOString();
|
|
153
|
+
const key = `${model}\0${bucketTime}`;
|
|
154
|
+
const bucket = buckets.get(key) || { model, timestamp: bucketTime, ...kimiUsage({}, true), usage_events: 0 };
|
|
155
|
+
for (const field of Object.keys(delta)) { tokens[field] += delta[field]; modelRow[field] += delta[field]; bucket[field] += delta[field]; }
|
|
156
|
+
modelRow.usage_events++; bucket.usage_events++;
|
|
157
|
+
const streamMs = count(event.llmStreamDurationMs);
|
|
158
|
+
const firstMs = Number(event.llmFirstTokenLatencyMs);
|
|
159
|
+
const performance = {
|
|
160
|
+
...(delta.output_tokens >= 200 && streamMs >= 1000 && streamMs <= 3600000
|
|
161
|
+
? { estimated_output_tokens: delta.output_tokens, estimated_duration_ms: streamMs, estimated_request_count: 1 } : {}),
|
|
162
|
+
...(event.llmFirstTokenLatencyMs != null && Number.isFinite(firstMs) && firstMs >= 0
|
|
163
|
+
? { first_response_total_ms: firstMs, first_response_sample_count: 1 } : {}),
|
|
164
|
+
};
|
|
165
|
+
modelRow.performance = mergePerformance(modelRow.performance, performance);
|
|
166
|
+
modelUsage.set(model, modelRow); buckets.set(key, bucket);
|
|
167
|
+
turnModel = model;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (!location.main) continue;
|
|
171
|
+
const signalId = hash(line);
|
|
172
|
+
if (seenSignals.has(signalId)) continue;
|
|
173
|
+
seenSignals.add(signalId);
|
|
174
|
+
const prompt = message?.type === "TurnBegin" ? message.payload?.user_input
|
|
175
|
+
: entry.type === "turn.prompt" && (!entry.origin?.kind || entry.origin.kind === "user") ? entry.input : null;
|
|
176
|
+
if (prompt != null) {
|
|
177
|
+
closeTurn(); turns++; turnOpen = true; turnModel = model;
|
|
178
|
+
// Fingerprints live only for this scan; no text or hash is persisted.
|
|
179
|
+
const fingerprint = hash(JSON.stringify(prompt));
|
|
180
|
+
if (lastPrompt && fingerprint === lastPrompt) retries++;
|
|
181
|
+
lastPrompt = fingerprint;
|
|
182
|
+
}
|
|
183
|
+
const tool = message?.type === "ToolCall" ? message.payload?.function?.name : event?.type === "tool.call" ? event.name : null;
|
|
184
|
+
if (typeof tool === "string") {
|
|
185
|
+
const name = tool.toLowerCase();
|
|
186
|
+
if (turnOpen && EDIT_TOOLS.has(name)) hadEdit = true;
|
|
187
|
+
if (AGENT_TOOLS.has(name)) agents++;
|
|
188
|
+
}
|
|
189
|
+
if (entry.type === "turn.step.retrying" || message?.type === "StepInterrupted") errors++;
|
|
190
|
+
if (message?.type === "TurnEnd" || entry.type === "turn.ended") closeTurn();
|
|
191
|
+
}
|
|
192
|
+
closeTurn();
|
|
193
|
+
}
|
|
194
|
+
const orderedTimes = [...times].sort((a, b) => a - b);
|
|
195
|
+
let activeMs = 0;
|
|
196
|
+
for (let i = 1; i < orderedTimes.length; i++) {
|
|
197
|
+
const gap = orderedTimes[i] - orderedTimes[i - 1];
|
|
198
|
+
if (gap <= 30 * 60 * 1000) activeMs += gap;
|
|
199
|
+
}
|
|
200
|
+
const models = [...modelUsage.values()].map((row) => ({ ...row, edit_turns: modelsForEdits.get(row.model) || 0 }));
|
|
201
|
+
if (!tokens.total_tokens && !turns) continue;
|
|
202
|
+
rows.push({ identity: group.identity, files: group.files.map((row) => row.file), session_id: resumeId,
|
|
203
|
+
title: null, project_ref: cwd, model: models.length === 1 ? models[0].model : "unknown", model_usage: models,
|
|
204
|
+
usage_buckets: [...buckets.values()], tokens, turns, edit_turns: edits, retry_turns: retries,
|
|
205
|
+
subagent_calls: agents, subagent_types: {}, error_count: errors,
|
|
206
|
+
usage_events: models.reduce((sum, row) => sum + row.usage_events, 0),
|
|
207
|
+
started_at: orderedTimes.length ? new Date(orderedTimes[0]).toISOString() : null,
|
|
208
|
+
ended_at: orderedTimes.length ? new Date(orderedTimes.at(-1)).toISOString() : null, active_ms: activeMs,
|
|
209
|
+
performance: mergePerformance(...models.map((row) => row.performance)),
|
|
210
|
+
provenance: { source: "local-session-log", confidence: "observed", retry_confidence: "inferred", content_retained: false, usage: "kimi-wire-metadata" },
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
return rows;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
module.exports = { discoverKimiWireFiles, kimiSessionDependencies, readKimiSessions };
|