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,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Grok's ACP turn_completed.usage uses camelCase fields. In that shape,
|
|
4
|
+
// inputTokens is the whole prompt (cache reads/writes included) and
|
|
5
|
+
// outputTokens includes reasoningTokens. Token Usage stores mutually
|
|
6
|
+
// exclusive columns, so both reported totals need to be split before pricing.
|
|
7
|
+
//
|
|
8
|
+
// Grok's headless/CLI summaries may use snake_case fields whose input_tokens is
|
|
9
|
+
// already the non-cached portion. Keep the two shapes distinct instead of
|
|
10
|
+
// subtracting cache tokens twice.
|
|
11
|
+
|
|
12
|
+
const USD_TICKS_PER_USD = 10_000_000_000;
|
|
13
|
+
|
|
14
|
+
function nonNegative(value) {
|
|
15
|
+
// Floor: Grok's ACP reports have been observed with fractional JSON
|
|
16
|
+
// numbers; the ingest edge requires non-negative INTEGERS and would 400
|
|
17
|
+
// the whole upload batch on one fractional counter.
|
|
18
|
+
const n = Number(value);
|
|
19
|
+
return Number.isFinite(n) && n > 0 ? Math.floor(n) : 0;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function firstBoolean(...values) {
|
|
23
|
+
for (const value of values) {
|
|
24
|
+
if (typeof value === "boolean") return value;
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function reportedCostUsd(usage, { costIsPartial, usageIsIncomplete }) {
|
|
30
|
+
if (costIsPartial || usageIsIncomplete) return null;
|
|
31
|
+
|
|
32
|
+
const ticks = Number(
|
|
33
|
+
usage.costUsdTicks ??
|
|
34
|
+
usage.totalCostUsdTicks ??
|
|
35
|
+
usage.cost_usd_ticks ??
|
|
36
|
+
usage.total_cost_usd_ticks,
|
|
37
|
+
);
|
|
38
|
+
if (Number.isFinite(ticks) && ticks >= 0) return ticks / USD_TICKS_PER_USD;
|
|
39
|
+
|
|
40
|
+
const usd = Number(usage.costUsd ?? usage.totalCostUsd ?? usage.cost_usd ?? usage.total_cost_usd);
|
|
41
|
+
return Number.isFinite(usd) && usd >= 0 ? usd : null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function normalizeGrokUsage(usage) {
|
|
45
|
+
if (!usage || typeof usage !== "object") return null;
|
|
46
|
+
|
|
47
|
+
const camelCaseInput = usage.inputTokens != null;
|
|
48
|
+
const reportedInput = nonNegative(usage.inputTokens ?? usage.input_tokens);
|
|
49
|
+
const cachedInput = nonNegative(
|
|
50
|
+
usage.cachedReadTokens ?? usage.cacheReadInputTokens ?? usage.cache_read_input_tokens ?? usage.cached_input_tokens,
|
|
51
|
+
);
|
|
52
|
+
const cacheCreationInput = nonNegative(
|
|
53
|
+
usage.cacheCreationTokens ??
|
|
54
|
+
usage.cachedWriteTokens ??
|
|
55
|
+
usage.cacheWriteInputTokens ??
|
|
56
|
+
usage.cache_creation_input_tokens,
|
|
57
|
+
);
|
|
58
|
+
const rawOutput = nonNegative(usage.outputTokens ?? usage.output_tokens);
|
|
59
|
+
const reasoningOutput = nonNegative(usage.reasoningTokens ?? usage.reasoning_output_tokens);
|
|
60
|
+
|
|
61
|
+
const inputTokens = camelCaseInput
|
|
62
|
+
? Math.max(0, reportedInput - cachedInput - cacheCreationInput)
|
|
63
|
+
: reportedInput;
|
|
64
|
+
const outputTokens = Math.max(0, rawOutput - reasoningOutput);
|
|
65
|
+
|
|
66
|
+
let totalTokens = nonNegative(usage.totalTokens ?? usage.total_tokens);
|
|
67
|
+
if (totalTokens <= 0) {
|
|
68
|
+
totalTokens = inputTokens
|
|
69
|
+
+ cachedInput
|
|
70
|
+
+ cacheCreationInput
|
|
71
|
+
+ outputTokens
|
|
72
|
+
+ reasoningOutput;
|
|
73
|
+
}
|
|
74
|
+
if (totalTokens <= 0) return null;
|
|
75
|
+
|
|
76
|
+
const costIsPartial = firstBoolean(usage.costIsPartial, usage.cost_is_partial);
|
|
77
|
+
const usageIsIncomplete = firstBoolean(usage.usageIsIncomplete, usage.usage_is_incomplete);
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
input_tokens: inputTokens,
|
|
81
|
+
cached_input_tokens: cachedInput,
|
|
82
|
+
cache_creation_input_tokens: cacheCreationInput,
|
|
83
|
+
output_tokens: outputTokens,
|
|
84
|
+
reasoning_output_tokens: reasoningOutput,
|
|
85
|
+
total_tokens: totalTokens,
|
|
86
|
+
billable_total_tokens: totalTokens,
|
|
87
|
+
total_cost_usd: reportedCostUsd(usage, { costIsPartial, usageIsIncomplete }),
|
|
88
|
+
cost_is_partial: costIsPartial,
|
|
89
|
+
usage_is_incomplete: usageIsIncomplete,
|
|
90
|
+
model_calls: nonNegative(usage.modelCalls ?? usage.model_calls),
|
|
91
|
+
api_duration_ms: nonNegative(usage.apiDurationMs ?? usage.api_duration_ms),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = {
|
|
96
|
+
USD_TICKS_PER_USD,
|
|
97
|
+
normalizeGrokUsage,
|
|
98
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Expand the `format=compact` activity-heatmap payload back into the schema the
|
|
4
|
+
// dashboard, popover and widget already render.
|
|
5
|
+
//
|
|
6
|
+
// The heatmap is the single largest cloud read we serve: 2.55 GB/day on
|
|
7
|
+
// 2026-09-21, 31% of all egress, and 94% of it proxied through this CLI. The
|
|
8
|
+
// bytes were never the data — `account_heatmap_compact` hands the edge ~12 KB
|
|
9
|
+
// of sparse rows, and the edge blew that up to ~42 KB by materialising a
|
|
10
|
+
// 52-week × 7-day grid of five-key objects, where `billable_total_tokens`
|
|
11
|
+
// simply repeats `total_tokens` and `level` is derived from the window maximum.
|
|
12
|
+
//
|
|
13
|
+
// So the grid gets rebuilt here instead, one hop later, off the same sparse
|
|
14
|
+
// rows. The rendered payload is identical; it just stops crossing the network.
|
|
15
|
+
//
|
|
16
|
+
// Model names are the other half of it. A year of daily rows repeats the same
|
|
17
|
+
// ~160 names ~1900 times, which was 57% of even the sparse payload, so they are
|
|
18
|
+
// sent once in `model_names` and referenced by index from flat
|
|
19
|
+
// [index, tokens, index, tokens, ...] pairs.
|
|
20
|
+
//
|
|
21
|
+
// This mirrors the tail of dashboard/edge-patches/token-usage-account-heatmap.ts
|
|
22
|
+
// step for step, including the details that look incidental but are not:
|
|
23
|
+
// coercion through Number(), last-row-wins on a repeated day, and `{}` (not
|
|
24
|
+
// null) for a present row whose models map is empty.
|
|
25
|
+
|
|
26
|
+
/** Next YYYY-MM-DD, treating the key as a UTC calendar day (same as the edge). */
|
|
27
|
+
function nextDayKey(dayKey) {
|
|
28
|
+
const d = new Date(`${dayKey}T00:00:00Z`);
|
|
29
|
+
d.setUTCDate(d.getUTCDate() + 1);
|
|
30
|
+
return d.toISOString().slice(0, 10);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const DAY_KEY_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
34
|
+
|
|
35
|
+
// 104 weeks is the edge's own `weeks` clamp; the guard is here so a malformed
|
|
36
|
+
// `to` cannot spin the grid loop forever.
|
|
37
|
+
const MAX_GRID_DAYS = 104 * 7 + 7;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {unknown} payload a `token-usage-account-heatmap` response.
|
|
41
|
+
* @returns {unknown} the dense payload, or `payload` itself when it is not a
|
|
42
|
+
* well-formed compact response — which is also how a new CLI stays correct
|
|
43
|
+
* against an edge that predates the compact branch.
|
|
44
|
+
*/
|
|
45
|
+
function expandHeatmapCompact(payload) {
|
|
46
|
+
if (!payload || typeof payload !== "object") return payload;
|
|
47
|
+
if (payload.format !== "compact") return payload;
|
|
48
|
+
if (!Array.isArray(payload.days)) return payload;
|
|
49
|
+
// Without the dictionary the indices resolve to nothing and every model
|
|
50
|
+
// breakdown would render empty, so refuse the payload instead of silently
|
|
51
|
+
// dropping data.
|
|
52
|
+
if (!Array.isArray(payload.model_names)) return payload;
|
|
53
|
+
if (!DAY_KEY_RE.test(String(payload.from)) || !DAY_KEY_RE.test(String(payload.to))) return payload;
|
|
54
|
+
|
|
55
|
+
const names = payload.model_names;
|
|
56
|
+
const byDay = new Map();
|
|
57
|
+
for (const row of payload.days) {
|
|
58
|
+
if (!Array.isArray(row)) continue;
|
|
59
|
+
const [day, total, pairs] = row;
|
|
60
|
+
if (!DAY_KEY_RE.test(String(day))) continue;
|
|
61
|
+
const mdl = {};
|
|
62
|
+
if (Array.isArray(pairs)) {
|
|
63
|
+
// `i + 1 < length` drops a trailing index with no value rather than
|
|
64
|
+
// recording it as 0.
|
|
65
|
+
for (let i = 0; i + 1 < pairs.length; i += 2) {
|
|
66
|
+
const name = names[pairs[i]];
|
|
67
|
+
if (typeof name === "string") mdl[name] = Number(pairs[i + 1]) || 0;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// Last row wins, as Map.set did on the edge.
|
|
71
|
+
byDay.set(String(day), { total: Number(total) || 0, models: mdl });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const maxValue = Number(payload.max_value) || 0;
|
|
75
|
+
const levelOf = (v) => {
|
|
76
|
+
if (v <= 0) return 0;
|
|
77
|
+
if (maxValue === 0) return 1;
|
|
78
|
+
const r = v / maxValue;
|
|
79
|
+
if (r <= 0.25) return 1;
|
|
80
|
+
if (r <= 0.5) return 2;
|
|
81
|
+
if (r <= 0.75) return 3;
|
|
82
|
+
return 4;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const cells = [];
|
|
86
|
+
const to = String(payload.to);
|
|
87
|
+
let cursor = String(payload.from);
|
|
88
|
+
while (cursor <= to && cells.length < MAX_GRID_DAYS) {
|
|
89
|
+
const hit = byDay.get(cursor);
|
|
90
|
+
const total = hit ? hit.total : 0;
|
|
91
|
+
cells.push({
|
|
92
|
+
day: cursor,
|
|
93
|
+
total_tokens: total,
|
|
94
|
+
billable_total_tokens: total,
|
|
95
|
+
level: levelOf(total),
|
|
96
|
+
models: hit ? hit.models : null,
|
|
97
|
+
});
|
|
98
|
+
cursor = nextDayKey(cursor);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const weeks = [];
|
|
102
|
+
for (let i = 0; i < cells.length; i += 7) weeks.push(cells.slice(i, i + 7));
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
from: payload.from,
|
|
106
|
+
to: payload.to,
|
|
107
|
+
week_starts_on: payload.week_starts_on,
|
|
108
|
+
active_days: payload.active_days,
|
|
109
|
+
streak_days: payload.streak_days,
|
|
110
|
+
weeks,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
module.exports = { expandHeatmapCompact };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The ingest edge (docs/cli-contract.md §2.4) validates every usage bucket
|
|
4
|
+
// BEFORE writing and rejects the WHOLE batch with 400 on the first invalid
|
|
5
|
+
// row. The upload path drains queue.jsonl verbatim, where any producer bug
|
|
6
|
+
// (fractional counters from a vendor API, a mangled line) would otherwise
|
|
7
|
+
// poison the batch and stall the queue head-of-line forever: the offset only
|
|
8
|
+
// advances on success, so every retry re-reads the identical rejected batch.
|
|
9
|
+
// Rows are therefore coerced into the contract shape here, and dropped
|
|
10
|
+
// (return null) when they cannot be repaired — losing one row beats losing
|
|
11
|
+
// the whole queue behind it.
|
|
12
|
+
|
|
13
|
+
const { TOKEN_COLUMNS } = require("./account-usage-dedup");
|
|
14
|
+
|
|
15
|
+
function nonNegativeInteger(value) {
|
|
16
|
+
const n = Number(value);
|
|
17
|
+
return Number.isFinite(n) ? Math.max(0, Math.floor(n)) : 0;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function nonNegativeNumber(value) {
|
|
21
|
+
const n = Number(value);
|
|
22
|
+
return Number.isFinite(n) && n > 0 ? n : 0;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function sanitizeBucketForUpload(bucket) {
|
|
26
|
+
if (!bucket || typeof bucket !== "object" || Array.isArray(bucket)) return null;
|
|
27
|
+
if (typeof bucket.hour_start !== "string" || !Number.isFinite(Date.parse(bucket.hour_start))) return null;
|
|
28
|
+
const sanitized = { ...bucket };
|
|
29
|
+
for (const column of TOKEN_COLUMNS) {
|
|
30
|
+
sanitized[column] = nonNegativeInteger(bucket[column]);
|
|
31
|
+
}
|
|
32
|
+
sanitized.billable_total_tokens = bucket.billable_total_tokens == null
|
|
33
|
+
? null
|
|
34
|
+
: nonNegativeInteger(bucket.billable_total_tokens);
|
|
35
|
+
if (bucket.conversation_count !== undefined) sanitized.conversation_count = nonNegativeInteger(bucket.conversation_count);
|
|
36
|
+
if (bucket.conversations !== undefined) sanitized.conversations = nonNegativeInteger(bucket.conversations);
|
|
37
|
+
sanitized.total_cost_usd = bucket.total_cost_usd == null ? 0 : nonNegativeNumber(bucket.total_cost_usd);
|
|
38
|
+
return sanitized;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = { sanitizeBucketForUpload };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { formatSummaryLine, renderBox, underline } = require("./cli-ui");
|
|
4
|
+
|
|
5
|
+
const DIVIDER = "----------------------------------------------";
|
|
6
|
+
|
|
7
|
+
function renderLocalReport({ summary, isDryRun }) {
|
|
8
|
+
const header = isDryRun
|
|
9
|
+
? "Dry run complete. Preview only; no changes were applied."
|
|
10
|
+
: "Local configuration complete.";
|
|
11
|
+
const lines = [header, "", "Integration Status:"];
|
|
12
|
+
for (const item of summary || []) lines.push(formatSummaryLine(item));
|
|
13
|
+
process.stdout.write(`${lines.join("\n")}\n`);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function renderAuthTransition({ authUrl, canAutoOpen }) {
|
|
17
|
+
const lines = ["", DIVIDER, "", "Next: Registering device..."];
|
|
18
|
+
if (canAutoOpen) {
|
|
19
|
+
lines.push("Opening your browser to link account...");
|
|
20
|
+
if (authUrl) lines.push(`If it does not open, visit: ${underline(authUrl)}`);
|
|
21
|
+
} else {
|
|
22
|
+
lines.push("Open the link below to sign in.");
|
|
23
|
+
if (authUrl) lines.push(`Visit: ${underline(authUrl)}`);
|
|
24
|
+
}
|
|
25
|
+
lines.push("");
|
|
26
|
+
process.stdout.write(`${lines.join("\n")}\n`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function renderSuccessBox({ configPath, dashboardUrl }) {
|
|
30
|
+
const identityLine = "Account linked.";
|
|
31
|
+
const lines = ["You are all set!", "", identityLine, `Token saved to: ${configPath}`, ""];
|
|
32
|
+
if (dashboardUrl) lines.push(`View your stats at: ${dashboardUrl}`);
|
|
33
|
+
lines.push("You can close this terminal window.");
|
|
34
|
+
process.stdout.write(`${renderBox(lines)}\n`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = {
|
|
38
|
+
DIVIDER,
|
|
39
|
+
renderLocalReport,
|
|
40
|
+
renderAuthTransition,
|
|
41
|
+
renderSuccessBox,
|
|
42
|
+
};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
const fssync = require("node:fs");
|
|
2
|
+
const os = require("node:os");
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
const wsl = require("./wsl-probe");
|
|
5
|
+
|
|
6
|
+
// `union` (opt-in): return BOTH qualifying installs instead of a single pick.
|
|
7
|
+
// The mode knob then means priority, not exclusivity — `wsl-first` orders the
|
|
8
|
+
// roots, it does not delete the native install's usage (#27: a populated WSL
|
|
9
|
+
// ~/.codex silently evicted the Windows install from collection, so the session
|
|
10
|
+
// browser listed sessions whose tokens never reached the dashboard). The
|
|
11
|
+
// *-only modes stay exclusive for free: their excluded side is already null'd
|
|
12
|
+
// by the shouldProbe* guards below, so union returns exactly one root there.
|
|
13
|
+
//
|
|
14
|
+
// Only providers whose parser dedups by a path-independent identity may opt in
|
|
15
|
+
// — the same session reachable under two path spellings (a WSL $HOME mounted
|
|
16
|
+
// on the Windows profile) must collapse, not double-count. Codex qualifies via
|
|
17
|
+
// its sessionUUID:eventTimestamp event dedup; Claude does the same union by
|
|
18
|
+
// hand in sync.js with file-hash + message-hash dedup behind it.
|
|
19
|
+
function resolveInstallPaths({ nativeValue, wslDir, wslValue, requireAnyChild, union } = {}, env = process.env, deps = {}) {
|
|
20
|
+
const platform = deps.platform || process.platform;
|
|
21
|
+
if (platform !== "win32") {
|
|
22
|
+
return { native: nativeValue ?? null, wsl: null };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// requireAnyChild (opt-in): a candidate install must hold at least one of
|
|
26
|
+
// the named children (e.g. "sessions"/"archived_sessions") to qualify — an
|
|
27
|
+
// empty shell dir must not shadow a populated install. For wslDir discovery
|
|
28
|
+
// the check is folded into the existsSync probe so discoverWslHome skips an
|
|
29
|
+
// empty-shell distro and continues to the next one.
|
|
30
|
+
const populated = (p) => !requireAnyChild || hasAnyChild(p, requireAnyChild, deps.existsSync);
|
|
31
|
+
const wslExists = requireAnyChild
|
|
32
|
+
? (p) => Boolean(pathExists(p, deps.existsSync)) && hasAnyChild(p, requireAnyChild, deps.existsSync)
|
|
33
|
+
: deps.existsSync;
|
|
34
|
+
const wslCandidate = wslValue !== undefined
|
|
35
|
+
? (wsl.shouldProbeWsl(env) && pathExists(wslValue, deps.existsSync) && populated(wslValue) ? wslValue : null)
|
|
36
|
+
: (wslDir && wsl.shouldProbeWsl(env) ? wsl.discoverWslHome(wslDir, { ...deps, env, existsSync: wslExists }) : null);
|
|
37
|
+
const nativeCandidate = wsl.shouldProbeNative(env) && nativeValue && populated(nativeValue)
|
|
38
|
+
? pathExists(nativeValue, deps.existsSync) : null;
|
|
39
|
+
|
|
40
|
+
// Native first: the cross-root dedup keeps whichever copy is parsed first,
|
|
41
|
+
// so the native install stays primary (matching the Claude union).
|
|
42
|
+
if (union) return { native: nativeCandidate, wsl: wslCandidate };
|
|
43
|
+
|
|
44
|
+
return wsl.resolveAllWin32Paths({ nativeValue: nativeCandidate, wslValue: wslCandidate, env, platform });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function pathExists(p, existsSync) {
|
|
48
|
+
if (typeof p !== "string" || !p) return null;
|
|
49
|
+
try { return (existsSync || fssync.existsSync)(p) ? p : null; } catch (_e) { return null; }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ZCode stores its CLI DB under the user's home dir on every platform
|
|
53
|
+
// (~/.zcode/cli/db/db.sqlite), Windows included. The win32 APPDATA guess
|
|
54
|
+
// (introduced with the WSL dual-install work) silently missed the native
|
|
55
|
+
// install, so probe home first and keep APPDATA only as a fallback for
|
|
56
|
+
// installs that do live under Roaming. Falls back to the home path when
|
|
57
|
+
// neither exists so resolveInstallPaths can null it uniformly.
|
|
58
|
+
function resolveZcodeNativeDbPath({
|
|
59
|
+
home = os.homedir(),
|
|
60
|
+
env = process.env,
|
|
61
|
+
platform = process.platform,
|
|
62
|
+
deps = {},
|
|
63
|
+
} = {}) {
|
|
64
|
+
const existsSync = deps.existsSync || fssync.existsSync;
|
|
65
|
+
const zcodeHomeOverride =
|
|
66
|
+
typeof env.ZCODE_HOME === "string" ? env.ZCODE_HOME.trim() : "";
|
|
67
|
+
const zcodeHome = zcodeHomeOverride
|
|
68
|
+
? path.resolve(zcodeHomeOverride)
|
|
69
|
+
: path.join(home, ".zcode");
|
|
70
|
+
// An explicit ZCODE_HOME pins the install: when its database is missing we
|
|
71
|
+
// must not fall back to an APPDATA database, or sync could read a different
|
|
72
|
+
// ZCode installation than the one the user pointed at.
|
|
73
|
+
const candidates = [
|
|
74
|
+
path.join(zcodeHome, "cli", "db", "db.sqlite"),
|
|
75
|
+
...(!zcodeHomeOverride &&
|
|
76
|
+
platform === "win32" &&
|
|
77
|
+
typeof env.APPDATA === "string" &&
|
|
78
|
+
env.APPDATA.trim()
|
|
79
|
+
? [path.join(env.APPDATA.trim(), ".zcode", "cli", "db", "db.sqlite")]
|
|
80
|
+
: []),
|
|
81
|
+
];
|
|
82
|
+
return candidates.find((p) => existsSync(p)) || candidates[0];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// mimocode (the engine behind the Xiaomi MiMo desktop app and the mimocode CLI)
|
|
86
|
+
// is an OpenCode fork and keeps OpenCode's data-dir rule on every platform:
|
|
87
|
+
// XDG_DATA_HOME, else ~/.local/share — on Windows too, where that means
|
|
88
|
+
// %USERPROFILE%\.local\share\mimocode\mimocode.db (#629). The %APPDATA%
|
|
89
|
+
// location was our guess before a Windows install was seen; keep it as a
|
|
90
|
+
// fallback so an install that does live there is not lost.
|
|
91
|
+
function resolveMimoNativeDbPath({
|
|
92
|
+
home = os.homedir(),
|
|
93
|
+
env = process.env,
|
|
94
|
+
platform = process.platform,
|
|
95
|
+
deps = {},
|
|
96
|
+
} = {}) {
|
|
97
|
+
const existsSync = deps.existsSync || fssync.existsSync;
|
|
98
|
+
const mimoHomeOverride = typeof env.MIMO_HOME === "string" ? env.MIMO_HOME.trim() : "";
|
|
99
|
+
if (mimoHomeOverride) {
|
|
100
|
+
// An explicit MIMO_HOME pins the install, same rule as ZCODE_HOME above.
|
|
101
|
+
return path.join(path.resolve(mimoHomeOverride), "mimocode.db");
|
|
102
|
+
}
|
|
103
|
+
const xdgDataHome = typeof env.XDG_DATA_HOME === "string" && env.XDG_DATA_HOME.trim()
|
|
104
|
+
? env.XDG_DATA_HOME.trim()
|
|
105
|
+
: path.join(home, ".local", "share");
|
|
106
|
+
const candidates = [
|
|
107
|
+
path.join(xdgDataHome, "mimocode", "mimocode.db"),
|
|
108
|
+
...(platform === "win32" && typeof env.APPDATA === "string" && env.APPDATA.trim()
|
|
109
|
+
? [path.join(env.APPDATA.trim(), "mimocode", "mimocode.db")]
|
|
110
|
+
: []),
|
|
111
|
+
];
|
|
112
|
+
return candidates.find((p) => existsSync(p)) || candidates[0];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function hasAnyChild(p, children, existsSync) {
|
|
116
|
+
const ex = existsSync || fssync.existsSync;
|
|
117
|
+
return children.some((c) => { try { return ex(path.join(p, c)); } catch (_e) { return false; } });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Migrate a flat (single-install) cursor to { native, wsl } namespaces.
|
|
121
|
+
// `activeKeys` names the namespaces seeded with a copy of the flat state; the
|
|
122
|
+
// others start empty so their install's full history backfills on first parse.
|
|
123
|
+
// Leaving a namespace empty is only safe when its install was NEVER counted
|
|
124
|
+
// under the flat cursor — the flat state holds the per-session dedup maps, and
|
|
125
|
+
// an already-counted install re-parsed without them double-counts everything.
|
|
126
|
+
// Callers that cannot prove which install the flat cursor tracked must seed
|
|
127
|
+
// ALL namespaces (the default): bounded backfill loss, never a double count.
|
|
128
|
+
function ensureNamespacedCursors(cursors, providerName, activeKeys = ["native", "wsl"]) {
|
|
129
|
+
const state = cursors[providerName] && typeof cursors[providerName] === "object" ? cursors[providerName] : {};
|
|
130
|
+
|
|
131
|
+
if (state.native !== undefined || state.wsl !== undefined) {
|
|
132
|
+
return state;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const keys = Array.isArray(activeKeys) ? activeKeys : [activeKeys];
|
|
136
|
+
cursors[providerName] = { native: {}, wsl: {} };
|
|
137
|
+
if (Object.keys(state).length > 0) {
|
|
138
|
+
for (const key of keys) {
|
|
139
|
+
cursors[providerName][key] = JSON.parse(JSON.stringify(state));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return cursors[providerName];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Collapse { native, wsl } namespaces back into a flat cursor when only one
|
|
146
|
+
// install remains. `preferredKey` names the SURVIVING install's namespace —
|
|
147
|
+
// its keys must win the merge, otherwise the vanished install's cursor
|
|
148
|
+
// (lastDbId watermarks, dedup maps) is donated to the survivor and its next
|
|
149
|
+
// parse double-counts everything past the foreign cursor. Callers that don't
|
|
150
|
+
// know the survivor fall back to the mode preference (legacy behavior).
|
|
151
|
+
function ensureFlatCursor(cursors, providerName, env, preferredKey) {
|
|
152
|
+
const state = cursors[providerName];
|
|
153
|
+
if (!state || typeof state !== "object") return;
|
|
154
|
+
if (state.native === undefined && state.wsl === undefined) return;
|
|
155
|
+
|
|
156
|
+
const mode = wsl.getWslMode(env || process.env);
|
|
157
|
+
const preferWsl = preferredKey === "wsl" || preferredKey === "native"
|
|
158
|
+
? preferredKey === "wsl"
|
|
159
|
+
: mode === "wsl-first" || mode === "wsl-only";
|
|
160
|
+
const merged = preferWsl ? { ...state.native, ...state.wsl } : { ...state.wsl, ...state.native };
|
|
161
|
+
cursors[providerName] = merged;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
module.exports = {
|
|
165
|
+
resolveInstallPaths,
|
|
166
|
+
resolveZcodeNativeDbPath,
|
|
167
|
+
resolveMimoNativeDbPath,
|
|
168
|
+
ensureNamespacedCursors,
|
|
169
|
+
ensureFlatCursor,
|
|
170
|
+
};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Yield physical JSONL records. Unlike node:readline, this intentionally treats
|
|
5
|
+
* only LF as a record separator so valid U+2028/U+2029 characters inside JSON
|
|
6
|
+
* strings remain part of the record. Reading raw bytes keeps byte budgets exact
|
|
7
|
+
* and lets malformed UTF-8 fail closed.
|
|
8
|
+
* Records longer than maxRecordBytes content bytes (excluding LF) are reported
|
|
9
|
+
* as { line: null, utf8Valid: null, oversized: true } without being buffered
|
|
10
|
+
* beyond the limit or decoded.
|
|
11
|
+
*/
|
|
12
|
+
class PhysicalJsonlLimitError extends Error {
|
|
13
|
+
constructor(maxPhysicalBytes) {
|
|
14
|
+
super(`physical JSONL byte limit exceeded: ${maxPhysicalBytes}`);
|
|
15
|
+
this.name = "PhysicalJsonlLimitError";
|
|
16
|
+
this.code = "PHYSICAL_JSONL_LIMIT_EXCEEDED";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const fatalUtf8Decoder = new TextDecoder("utf-8", { fatal: true });
|
|
21
|
+
|
|
22
|
+
function decodePhysicalLine(buffer, { stripCr = false } = {}) {
|
|
23
|
+
const content = stripCr && buffer.length > 0 && buffer[buffer.length - 1] === 0x0d
|
|
24
|
+
? buffer.subarray(0, buffer.length - 1)
|
|
25
|
+
: buffer;
|
|
26
|
+
return fatalUtf8Decoder.decode(content);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function* physicalJsonlRecords(input, {
|
|
30
|
+
invalidUtf8 = "throw",
|
|
31
|
+
maxPhysicalBytes = Infinity,
|
|
32
|
+
maxRecordBytes = Infinity,
|
|
33
|
+
} = {}) {
|
|
34
|
+
if (invalidUtf8 !== "throw" && invalidUtf8 !== "record") {
|
|
35
|
+
throw new TypeError(`unsupported invalidUtf8 policy: ${invalidUtf8}`);
|
|
36
|
+
}
|
|
37
|
+
const byteLimit = Number.isFinite(maxPhysicalBytes)
|
|
38
|
+
? Math.max(0, Number(maxPhysicalBytes))
|
|
39
|
+
: Infinity;
|
|
40
|
+
const recordLimit = Number.isFinite(maxRecordBytes)
|
|
41
|
+
? Math.max(0, Number(maxRecordBytes))
|
|
42
|
+
: Infinity;
|
|
43
|
+
const fragments = [];
|
|
44
|
+
let fragmentsBytes = 0;
|
|
45
|
+
let oversized = false;
|
|
46
|
+
let physicalBytesRead = 0;
|
|
47
|
+
|
|
48
|
+
for await (const value of input) {
|
|
49
|
+
if (!Buffer.isBuffer(value) && !(value instanceof Uint8Array)) {
|
|
50
|
+
throw new TypeError("physicalJsonlRecords input must emit bytes");
|
|
51
|
+
}
|
|
52
|
+
const chunk = Buffer.isBuffer(value)
|
|
53
|
+
? value
|
|
54
|
+
: Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
55
|
+
|
|
56
|
+
let start = 0;
|
|
57
|
+
let newline = chunk.indexOf(0x0a);
|
|
58
|
+
while (newline !== -1) {
|
|
59
|
+
const fragment = chunk.subarray(start, newline);
|
|
60
|
+
const physicalBytes = fragmentsBytes + fragment.length + 1;
|
|
61
|
+
if (physicalBytesRead + physicalBytes > byteLimit) {
|
|
62
|
+
throw new PhysicalJsonlLimitError(byteLimit);
|
|
63
|
+
}
|
|
64
|
+
const contentBytes = fragmentsBytes + fragment.length;
|
|
65
|
+
if (oversized || contentBytes > recordLimit) {
|
|
66
|
+
fragments.length = 0;
|
|
67
|
+
fragmentsBytes = 0;
|
|
68
|
+
oversized = false;
|
|
69
|
+
physicalBytesRead += physicalBytes;
|
|
70
|
+
start = newline + 1;
|
|
71
|
+
newline = chunk.indexOf(0x0a, start);
|
|
72
|
+
yield { line: null, utf8Valid: null, oversized: true, physicalBytes, terminated: true };
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const lineBuffer = fragments.length === 0
|
|
76
|
+
? fragment
|
|
77
|
+
: Buffer.concat([...fragments, fragment], fragmentsBytes + fragment.length);
|
|
78
|
+
fragments.length = 0;
|
|
79
|
+
fragmentsBytes = 0;
|
|
80
|
+
physicalBytesRead += physicalBytes;
|
|
81
|
+
start = newline + 1;
|
|
82
|
+
newline = chunk.indexOf(0x0a, start);
|
|
83
|
+
let line;
|
|
84
|
+
try {
|
|
85
|
+
line = decodePhysicalLine(lineBuffer, { stripCr: true });
|
|
86
|
+
} catch (error) {
|
|
87
|
+
if (invalidUtf8 === "throw") throw error;
|
|
88
|
+
yield { line: null, utf8Valid: false, oversized: false, physicalBytes, terminated: true };
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
yield { line, utf8Valid: true, oversized: false, physicalBytes, terminated: true };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (start < chunk.length) {
|
|
95
|
+
const fragment = chunk.subarray(start);
|
|
96
|
+
if (physicalBytesRead + fragmentsBytes + fragment.length > byteLimit) {
|
|
97
|
+
throw new PhysicalJsonlLimitError(byteLimit);
|
|
98
|
+
}
|
|
99
|
+
if (!oversized) {
|
|
100
|
+
if (fragmentsBytes + fragment.length > recordLimit) {
|
|
101
|
+
fragments.length = 0;
|
|
102
|
+
oversized = true;
|
|
103
|
+
} else {
|
|
104
|
+
fragments.push(Buffer.from(fragment));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
fragmentsBytes += fragment.length;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (fragmentsBytes > 0) {
|
|
112
|
+
if (oversized) {
|
|
113
|
+
yield { line: null, utf8Valid: null, oversized: true, physicalBytes: fragmentsBytes, terminated: false };
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const lineBuffer = fragments.length === 1
|
|
117
|
+
? fragments[0]
|
|
118
|
+
: Buffer.concat(fragments, fragmentsBytes);
|
|
119
|
+
let line = null;
|
|
120
|
+
let utf8Valid = true;
|
|
121
|
+
try {
|
|
122
|
+
line = decodePhysicalLine(lineBuffer);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if (invalidUtf8 === "throw") throw error;
|
|
125
|
+
utf8Valid = false;
|
|
126
|
+
}
|
|
127
|
+
yield { line, utf8Valid, oversized: false, physicalBytes: fragmentsBytes, terminated: false };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
module.exports = {
|
|
132
|
+
PhysicalJsonlLimitError,
|
|
133
|
+
physicalJsonlRecords,
|
|
134
|
+
};
|