tokenusage-cli 0.1.0 → 0.2.1
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 +46 -19
- package/LICENSE +22 -22
- package/README.md +123 -94
- package/THIRD_PARTY_NOTICES.md +140 -140
- package/bin/tokenusage.js +69 -69
- package/package.json +59 -59
- package/src/cli.js +146 -134
- package/src/commands/device-login.js +205 -205
- package/src/commands/diagnostics.js +38 -38
- package/src/commands/doctor.js +110 -110
- package/src/commands/init.js +2484 -2436
- package/src/commands/pause.js +77 -0
- package/src/commands/sessions.js +29 -29
- package/src/commands/status.js +1758 -1747
- package/src/commands/sync.js +8048 -7934
- package/src/commands/uninstall.js +322 -312
- package/src/lib/account-usage-dedup.js +267 -267
- package/src/lib/antigravity-paths.js +48 -48
- package/src/lib/ark-agent-plan-limits.js +438 -438
- package/src/lib/ark-coding-plan-limits.js +537 -537
- package/src/lib/async-queue-reader.js +138 -138
- package/src/lib/atomic-json.js +27 -27
- package/src/lib/bearer-token.js +11 -11
- package/src/lib/categorizer-utils.js +232 -232
- package/src/lib/claude-categorizer.js +1343 -1343
- package/src/lib/claude-config.js +279 -279
- package/src/lib/claude-user-identity.js +56 -56
- package/src/lib/cli-ui.js +179 -179
- package/src/lib/cloud-account.js +541 -541
- package/src/lib/cloud-device-token.js +141 -141
- package/src/lib/cloud-sync-prefs.js +14 -14
- package/src/lib/cloud-usage-reset.js +115 -115
- package/src/lib/codex-config.js +595 -595
- package/src/lib/codex-context-breakdown.js +806 -806
- package/src/lib/codex-model-attribution.js +128 -128
- package/src/lib/codex-rollout-parser.js +1471 -1471
- package/src/lib/codex-service-tier.js +49 -49
- package/src/lib/codex-token-refresh.js +140 -140
- package/src/lib/codex-token-usage.js +195 -195
- package/src/lib/codex-usage-record.js +195 -195
- package/src/lib/collection-pause.js +77 -0
- package/src/lib/command-runner.js +313 -313
- package/src/lib/commandcode-limits.js +317 -317
- package/src/lib/context-health.js +124 -124
- package/src/lib/cursor-config.js +605 -605
- package/src/lib/cursor-store.js +1918 -1918
- package/src/lib/debug-flags.js +7 -7
- package/src/lib/devin-limits.js +249 -249
- package/src/lib/devin-usage.js +212 -212
- package/src/lib/diagnostics.js +383 -383
- package/src/lib/doctor.js +385 -385
- package/src/lib/dumate.js +95 -95
- package/src/lib/fs.js +455 -455
- package/src/lib/function-url.js +14 -14
- package/src/lib/gemini-config.js +291 -291
- package/src/lib/git-outcomes.js +243 -243
- package/src/lib/grok-context-breakdown.js +890 -890
- package/src/lib/grok-hook.js +360 -360
- package/src/lib/grok-limits.js +865 -865
- package/src/lib/grok-usage.js +98 -98
- package/src/lib/heatmap-compact.js +114 -114
- package/src/lib/ingest-sanitize.js +41 -41
- package/src/lib/init-flow.js +42 -42
- package/src/lib/install-resolver.js +170 -170
- package/src/lib/jsonl-lines.js +134 -134
- package/src/lib/kimi-model-repair.js +173 -173
- package/src/lib/kimi-profile.js +61 -61
- package/src/lib/kimi-session.js +216 -216
- package/src/lib/local-api.js +3588 -3588
- package/src/lib/machine-id.js +238 -238
- package/src/lib/multi-install-parser.js +113 -113
- package/src/lib/notify-command.js +38 -38
- package/src/lib/omp-hook.js +589 -589
- package/src/lib/openclaw-cli.js +104 -104
- package/src/lib/openclaw-hook.js +375 -375
- package/src/lib/openclaw-session-plugin.js +560 -560
- package/src/lib/opencode-config.js +121 -121
- package/src/lib/opencode-go-limits.js +807 -807
- package/src/lib/outcomes-engine.js +253 -253
- package/src/lib/passive-mode.js +219 -219
- package/src/lib/pet-packages.js +722 -722
- package/src/lib/pricing/curated-overrides.json +822 -822
- package/src/lib/pricing/index.js +379 -379
- package/src/lib/pricing/litellm-fetcher.js +172 -172
- package/src/lib/pricing/matcher.js +456 -456
- package/src/lib/pricing/seed-snapshot.json +1 -1
- package/src/lib/progress.js +76 -76
- package/src/lib/project-usage-purge.js +117 -117
- package/src/lib/prompt.js +24 -24
- package/src/lib/provider-status.js +104 -104
- package/src/lib/proxy-env.js +489 -489
- package/src/lib/proxy-settings.js +163 -163
- package/src/lib/qoder-cn-cli-limits.js +119 -119
- package/src/lib/qoder-limits.js +880 -880
- package/src/lib/quiet-warnings.js +26 -26
- package/src/lib/rollout.js +26005 -25966
- package/src/lib/runtime-config.js +9 -4
- package/src/lib/scan-roots.js +277 -277
- package/src/lib/scheduled-task.js +145 -0
- package/src/lib/sentry.js +231 -231
- package/src/lib/session-analytics.js +2972 -2972
- package/src/lib/session-performance.js +237 -237
- package/src/lib/session-upload.js +379 -379
- package/src/lib/skill-usage.js +267 -267
- package/src/lib/skills-manager.js +1591 -1591
- package/src/lib/source-metadata.js +48 -48
- package/src/lib/sqlite-reader.js +215 -215
- package/src/lib/star-cta.js +73 -73
- package/src/lib/subscription-manager.js +293 -293
- package/src/lib/subscriptions.js +508 -508
- package/src/lib/sync-report.js +138 -0
- package/src/lib/telemetry.js +193 -193
- package/src/lib/tracker-paths.js +16 -16
- package/src/lib/trae-cn-config.js +548 -548
- package/src/lib/trae-db.js +127 -127
- package/src/lib/trae-snapshot.js +230 -230
- package/src/lib/trae-sqlite.js +143 -143
- package/src/lib/trae-usage.js +121 -121
- package/src/lib/upload-queue.js +134 -134
- package/src/lib/upload-throttle.js +171 -171
- package/src/lib/usage-limits.js +4552 -4552
- package/src/lib/wrapped-aggregator.js +236 -236
- package/src/lib/wsl-probe.js +281 -281
- package/src/lib/zcode-limits.js +1172 -1172
- package/src/lib/zcode-reset-credits.js +66 -66
- package/test/audit-fixes.test.js +200 -0
- package/test/cli-smoke.test.js +53 -53
- package/test/collection-controls.test.js +210 -0
- package/test/ingest-sanitize.test.js +162 -162
- package/test/quiet-warnings.test.js +53 -53
- package/test/session-upload.test.js +427 -427
|
@@ -1,170 +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
|
-
};
|
|
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
|
+
};
|
package/src/lib/jsonl-lines.js
CHANGED
|
@@ -1,134 +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
|
-
};
|
|
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
|
+
};
|