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
package/src/lib/function-url.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
const { DEFAULT_BASE_URL } = require("./runtime-config");
|
|
2
|
-
|
|
3
|
-
// token-usage serves the CLI contract under /api/functions/<slug> from the
|
|
4
|
-
// Next.js app itself — no separate edge-function host, no legacy fallback.
|
|
5
|
-
function functionUrlFor(baseUrl, slug) {
|
|
6
|
-
const root = String(baseUrl || "").trim().replace(/\/$/, "");
|
|
7
|
-
return `${root}/api/functions/${slug}`;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
async function fetchFunctionResponse(url, init = {}, fetchImpl = fetch) {
|
|
11
|
-
return fetchImpl(url, init);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
module.exports = { functionUrlFor, fetchFunctionResponse };
|
|
1
|
+
const { DEFAULT_BASE_URL } = require("./runtime-config");
|
|
2
|
+
|
|
3
|
+
// token-usage serves the CLI contract under /api/functions/<slug> from the
|
|
4
|
+
// Next.js app itself — no separate edge-function host, no legacy fallback.
|
|
5
|
+
function functionUrlFor(baseUrl, slug) {
|
|
6
|
+
const root = String(baseUrl || "").trim().replace(/\/$/, "");
|
|
7
|
+
return `${root}/api/functions/${slug}`;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async function fetchFunctionResponse(url, init = {}, fetchImpl = fetch) {
|
|
11
|
+
return fetchImpl(url, init);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = { functionUrlFor, fetchFunctionResponse };
|
package/src/lib/gemini-config.js
CHANGED
|
@@ -1,291 +1,291 @@
|
|
|
1
|
-
const os = require("node:os");
|
|
2
|
-
const path = require("node:path");
|
|
3
|
-
const fs = require("node:fs/promises");
|
|
4
|
-
|
|
5
|
-
const { ensureDir, readJson, writeJson } = require("./fs");
|
|
6
|
-
const { buildNotifyCommand } = require("./notify-command");
|
|
7
|
-
|
|
8
|
-
const DEFAULT_EVENT = "SessionEnd";
|
|
9
|
-
const DEFAULT_HOOK_NAME = "tokenusage";
|
|
10
|
-
const DEFAULT_MATCHER = "exit|clear|logout|prompt_input_exit|other";
|
|
11
|
-
|
|
12
|
-
function resolveGeminiConfigDir({ home = os.homedir(), env = process.env } = {}) {
|
|
13
|
-
const explicit = typeof env.GEMINI_HOME === "string" ? env.GEMINI_HOME.trim() : "";
|
|
14
|
-
if (explicit) return path.resolve(explicit);
|
|
15
|
-
return path.join(home, ".gemini");
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function resolveGeminiSettingsPath({ configDir }) {
|
|
19
|
-
return path.join(configDir, "settings.json");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async function upsertGeminiHook({
|
|
23
|
-
settingsPath,
|
|
24
|
-
hookCommand,
|
|
25
|
-
hookName = DEFAULT_HOOK_NAME,
|
|
26
|
-
matcher = DEFAULT_MATCHER,
|
|
27
|
-
event = DEFAULT_EVENT,
|
|
28
|
-
}) {
|
|
29
|
-
const existing = await readJson(settingsPath);
|
|
30
|
-
const settings = normalizeSettings(existing);
|
|
31
|
-
const enableResult = ensureHooksEnabled(settings);
|
|
32
|
-
const baseSettings = enableResult.settings;
|
|
33
|
-
const hooks = normalizeHooks(settings.hooks);
|
|
34
|
-
const entries = normalizeEntries(hooks[event]);
|
|
35
|
-
|
|
36
|
-
const normalized = normalizeEntriesForHook(entries, { hookCommand, hookName });
|
|
37
|
-
let nextEntries = normalized.entries;
|
|
38
|
-
let changed = normalized.changed || enableResult.changed;
|
|
39
|
-
|
|
40
|
-
if (!hasHook(nextEntries, { hookCommand, hookName })) {
|
|
41
|
-
nextEntries = nextEntries.concat([buildHookEntry({ hookCommand, hookName, matcher })]);
|
|
42
|
-
changed = true;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (!changed) return { changed: false, backupPath: null };
|
|
46
|
-
|
|
47
|
-
const nextHooks = { ...hooks, [event]: nextEntries };
|
|
48
|
-
const nextSettings = { ...baseSettings, hooks: nextHooks };
|
|
49
|
-
const backupPath = await writeGeminiSettings({ settingsPath, settings: nextSettings });
|
|
50
|
-
return { changed: true, backupPath };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async function removeGeminiHook({
|
|
54
|
-
settingsPath,
|
|
55
|
-
hookCommand,
|
|
56
|
-
hookName = DEFAULT_HOOK_NAME,
|
|
57
|
-
event = DEFAULT_EVENT,
|
|
58
|
-
}) {
|
|
59
|
-
const existing = await readJson(settingsPath);
|
|
60
|
-
if (!existing) return { removed: false, skippedReason: "settings-missing" };
|
|
61
|
-
|
|
62
|
-
const settings = normalizeSettings(existing);
|
|
63
|
-
const hooks = normalizeHooks(settings.hooks);
|
|
64
|
-
const entries = normalizeEntries(hooks[event]);
|
|
65
|
-
if (entries.length === 0) return { removed: false, skippedReason: "hook-missing" };
|
|
66
|
-
|
|
67
|
-
let removed = false;
|
|
68
|
-
const nextEntries = [];
|
|
69
|
-
for (const entry of entries) {
|
|
70
|
-
const res = stripHookFromEntry(entry, { hookCommand, hookName });
|
|
71
|
-
if (res.removed) removed = true;
|
|
72
|
-
if (res.entry) nextEntries.push(res.entry);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (!removed) return { removed: false, skippedReason: "hook-missing" };
|
|
76
|
-
|
|
77
|
-
const nextHooks = { ...hooks };
|
|
78
|
-
if (nextEntries.length > 0) nextHooks[event] = nextEntries;
|
|
79
|
-
else delete nextHooks[event];
|
|
80
|
-
|
|
81
|
-
const nextSettings = { ...settings };
|
|
82
|
-
if (Object.keys(nextHooks).length > 0) nextSettings.hooks = nextHooks;
|
|
83
|
-
else delete nextSettings.hooks;
|
|
84
|
-
|
|
85
|
-
const backupPath = await writeGeminiSettings({ settingsPath, settings: nextSettings });
|
|
86
|
-
return { removed: true, skippedReason: null, backupPath };
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
async function isGeminiHookConfigured({
|
|
90
|
-
settingsPath,
|
|
91
|
-
hookCommand,
|
|
92
|
-
hookName = DEFAULT_HOOK_NAME,
|
|
93
|
-
event = DEFAULT_EVENT,
|
|
94
|
-
}) {
|
|
95
|
-
const settings = await readJson(settingsPath);
|
|
96
|
-
if (!settings || typeof settings !== "object") return false;
|
|
97
|
-
const hooks = settings.hooks;
|
|
98
|
-
if (!hooks || typeof hooks !== "object") return false;
|
|
99
|
-
const entries = normalizeEntries(hooks[event]);
|
|
100
|
-
return hasHook(entries, { hookCommand, hookName });
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function buildGeminiHookCommand(notifyPath, options) {
|
|
104
|
-
const cmd = typeof notifyPath === "string" ? notifyPath : "";
|
|
105
|
-
return buildNotifyCommand(cmd, "gemini", options);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function buildHookEntry({ hookCommand, hookName, matcher }) {
|
|
109
|
-
const hook = {
|
|
110
|
-
name: hookName,
|
|
111
|
-
type: "command",
|
|
112
|
-
command: hookCommand,
|
|
113
|
-
};
|
|
114
|
-
const entry = { hooks: [hook] };
|
|
115
|
-
if (typeof matcher === "string" && matcher.length > 0) entry.matcher = matcher;
|
|
116
|
-
return entry;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function normalizeSettings(raw) {
|
|
120
|
-
return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function normalizeHooks(raw) {
|
|
124
|
-
return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function normalizeTools(raw) {
|
|
128
|
-
return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function normalizeEntries(raw) {
|
|
132
|
-
return Array.isArray(raw) ? raw.slice() : [];
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function normalizeCommand(cmd) {
|
|
136
|
-
if (Array.isArray(cmd)) return cmd.map((v) => String(v)).join("\u0000");
|
|
137
|
-
if (typeof cmd === "string") return cmd.trim();
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function normalizeName(name) {
|
|
142
|
-
if (typeof name !== "string") return null;
|
|
143
|
-
const trimmed = name.trim();
|
|
144
|
-
return trimmed.length > 0 ? trimmed : null;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function ensureHooksEnabled(settings) {
|
|
148
|
-
const tools = normalizeTools(settings.tools);
|
|
149
|
-
if (tools.enableHooks === true) return { settings, changed: false };
|
|
150
|
-
const nextTools = { ...tools, enableHooks: true };
|
|
151
|
-
return { settings: { ...settings, tools: nextTools }, changed: true };
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function hookMatches(hook, { hookCommand, hookName, requireCommand = false }) {
|
|
155
|
-
if (!hook || typeof hook !== "object") return false;
|
|
156
|
-
const name = normalizeName(hook.name);
|
|
157
|
-
const targetName = normalizeName(hookName);
|
|
158
|
-
const cmd = normalizeCommand(hook.command);
|
|
159
|
-
const targetCmd = normalizeCommand(hookCommand);
|
|
160
|
-
const commandMatches = Boolean(cmd && targetCmd && cmd === targetCmd);
|
|
161
|
-
if (requireCommand) return commandMatches;
|
|
162
|
-
const nameMatches = Boolean(name && targetName && name === targetName);
|
|
163
|
-
return Boolean(commandMatches || nameMatches);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
function entryMatches(entry, { hookCommand, hookName, requireCommand = false }) {
|
|
167
|
-
if (!entry || typeof entry !== "object") return false;
|
|
168
|
-
if (entry.command || entry.name)
|
|
169
|
-
return hookMatches(entry, { hookCommand, hookName, requireCommand });
|
|
170
|
-
if (!Array.isArray(entry.hooks)) return false;
|
|
171
|
-
return entry.hooks.some((hook) => hookMatches(hook, { hookCommand, hookName, requireCommand }));
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function hasHook(entries, { hookCommand, hookName }) {
|
|
175
|
-
return entries.some((entry) => entryMatches(entry, { hookCommand, hookName }));
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function normalizeEntriesForHook(entries, { hookCommand, hookName }) {
|
|
179
|
-
let changed = false;
|
|
180
|
-
const nextEntries = entries.map((entry) => {
|
|
181
|
-
if (!entry || typeof entry !== "object") return entry;
|
|
182
|
-
|
|
183
|
-
if (entry.command || entry.name) {
|
|
184
|
-
if (hookMatches(entry, { hookCommand, hookName })) {
|
|
185
|
-
const next = normalizeHookObject(entry, { hookCommand, hookName });
|
|
186
|
-
if (next !== entry) changed = true;
|
|
187
|
-
return next;
|
|
188
|
-
}
|
|
189
|
-
return entry;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const hooks = Array.isArray(entry.hooks) ? entry.hooks : null;
|
|
193
|
-
if (!hooks) return entry;
|
|
194
|
-
|
|
195
|
-
let hooksChanged = false;
|
|
196
|
-
const nextHooks = hooks.map((hook) => {
|
|
197
|
-
if (hookMatches(hook, { hookCommand, hookName })) {
|
|
198
|
-
const next = normalizeHookObject(hook, { hookCommand, hookName });
|
|
199
|
-
if (next !== hook) hooksChanged = true;
|
|
200
|
-
return next;
|
|
201
|
-
}
|
|
202
|
-
return hook;
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
if (!hooksChanged) return entry;
|
|
206
|
-
changed = true;
|
|
207
|
-
return { ...entry, hooks: nextHooks };
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
return { entries: nextEntries, changed };
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
function normalizeHookObject(hook, { hookCommand, hookName }) {
|
|
214
|
-
const next = { ...hook };
|
|
215
|
-
let changed = false;
|
|
216
|
-
|
|
217
|
-
if (next.type !== "command") {
|
|
218
|
-
next.type = "command";
|
|
219
|
-
changed = true;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
if (hookCommand && next.command !== hookCommand) {
|
|
223
|
-
next.command = hookCommand;
|
|
224
|
-
changed = true;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
if (hookName && next.name !== hookName) {
|
|
228
|
-
next.name = hookName;
|
|
229
|
-
changed = true;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
return changed ? next : hook;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
function stripHookFromEntry(entry, { hookCommand, hookName }) {
|
|
236
|
-
if (!entry || typeof entry !== "object") return { entry, removed: false };
|
|
237
|
-
|
|
238
|
-
if (entry.command || entry.name) {
|
|
239
|
-
if (hookMatches(entry, { hookCommand, hookName, requireCommand: true }))
|
|
240
|
-
return { entry: null, removed: true };
|
|
241
|
-
return { entry, removed: false };
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const hooks = Array.isArray(entry.hooks) ? entry.hooks : null;
|
|
245
|
-
if (!hooks) return { entry, removed: false };
|
|
246
|
-
|
|
247
|
-
const nextHooks = hooks.filter(
|
|
248
|
-
(hook) => !hookMatches(hook, { hookCommand, hookName, requireCommand: true }),
|
|
249
|
-
);
|
|
250
|
-
if (nextHooks.length === hooks.length) return { entry, removed: false };
|
|
251
|
-
if (nextHooks.length === 0) return { entry: null, removed: true };
|
|
252
|
-
|
|
253
|
-
return { entry: { ...entry, hooks: nextHooks }, removed: true };
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
function quoteArg(value) {
|
|
257
|
-
const v = typeof value === "string" ? value : "";
|
|
258
|
-
if (!v) return '""';
|
|
259
|
-
if (/^[A-Za-z0-9_\-./:@]+$/.test(v)) return v;
|
|
260
|
-
// Escape backslashes before quotes: a trailing backslash (Windows paths)
|
|
261
|
-
// would otherwise escape the closing quote and corrupt the hook command.
|
|
262
|
-
return `"${v.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
async function writeGeminiSettings({ settingsPath, settings }) {
|
|
266
|
-
await ensureDir(path.dirname(settingsPath));
|
|
267
|
-
let backupPath = null;
|
|
268
|
-
try {
|
|
269
|
-
const st = await fs.stat(settingsPath);
|
|
270
|
-
if (st && st.isFile()) {
|
|
271
|
-
backupPath = `${settingsPath}.bak.${new Date().toISOString().replace(/[:.]/g, "-")}`;
|
|
272
|
-
await fs.copyFile(settingsPath, backupPath);
|
|
273
|
-
}
|
|
274
|
-
} catch (_e) {
|
|
275
|
-
// Ignore missing file.
|
|
276
|
-
}
|
|
277
|
-
await writeJson(settingsPath, settings);
|
|
278
|
-
return backupPath;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
module.exports = {
|
|
282
|
-
DEFAULT_EVENT,
|
|
283
|
-
DEFAULT_HOOK_NAME,
|
|
284
|
-
DEFAULT_MATCHER,
|
|
285
|
-
resolveGeminiConfigDir,
|
|
286
|
-
resolveGeminiSettingsPath,
|
|
287
|
-
buildGeminiHookCommand,
|
|
288
|
-
upsertGeminiHook,
|
|
289
|
-
removeGeminiHook,
|
|
290
|
-
isGeminiHookConfigured,
|
|
291
|
-
};
|
|
1
|
+
const os = require("node:os");
|
|
2
|
+
const path = require("node:path");
|
|
3
|
+
const fs = require("node:fs/promises");
|
|
4
|
+
|
|
5
|
+
const { ensureDir, readJson, writeJson } = require("./fs");
|
|
6
|
+
const { buildNotifyCommand } = require("./notify-command");
|
|
7
|
+
|
|
8
|
+
const DEFAULT_EVENT = "SessionEnd";
|
|
9
|
+
const DEFAULT_HOOK_NAME = "tokenusage";
|
|
10
|
+
const DEFAULT_MATCHER = "exit|clear|logout|prompt_input_exit|other";
|
|
11
|
+
|
|
12
|
+
function resolveGeminiConfigDir({ home = os.homedir(), env = process.env } = {}) {
|
|
13
|
+
const explicit = typeof env.GEMINI_HOME === "string" ? env.GEMINI_HOME.trim() : "";
|
|
14
|
+
if (explicit) return path.resolve(explicit);
|
|
15
|
+
return path.join(home, ".gemini");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function resolveGeminiSettingsPath({ configDir }) {
|
|
19
|
+
return path.join(configDir, "settings.json");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function upsertGeminiHook({
|
|
23
|
+
settingsPath,
|
|
24
|
+
hookCommand,
|
|
25
|
+
hookName = DEFAULT_HOOK_NAME,
|
|
26
|
+
matcher = DEFAULT_MATCHER,
|
|
27
|
+
event = DEFAULT_EVENT,
|
|
28
|
+
}) {
|
|
29
|
+
const existing = await readJson(settingsPath);
|
|
30
|
+
const settings = normalizeSettings(existing);
|
|
31
|
+
const enableResult = ensureHooksEnabled(settings);
|
|
32
|
+
const baseSettings = enableResult.settings;
|
|
33
|
+
const hooks = normalizeHooks(settings.hooks);
|
|
34
|
+
const entries = normalizeEntries(hooks[event]);
|
|
35
|
+
|
|
36
|
+
const normalized = normalizeEntriesForHook(entries, { hookCommand, hookName });
|
|
37
|
+
let nextEntries = normalized.entries;
|
|
38
|
+
let changed = normalized.changed || enableResult.changed;
|
|
39
|
+
|
|
40
|
+
if (!hasHook(nextEntries, { hookCommand, hookName })) {
|
|
41
|
+
nextEntries = nextEntries.concat([buildHookEntry({ hookCommand, hookName, matcher })]);
|
|
42
|
+
changed = true;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!changed) return { changed: false, backupPath: null };
|
|
46
|
+
|
|
47
|
+
const nextHooks = { ...hooks, [event]: nextEntries };
|
|
48
|
+
const nextSettings = { ...baseSettings, hooks: nextHooks };
|
|
49
|
+
const backupPath = await writeGeminiSettings({ settingsPath, settings: nextSettings });
|
|
50
|
+
return { changed: true, backupPath };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function removeGeminiHook({
|
|
54
|
+
settingsPath,
|
|
55
|
+
hookCommand,
|
|
56
|
+
hookName = DEFAULT_HOOK_NAME,
|
|
57
|
+
event = DEFAULT_EVENT,
|
|
58
|
+
}) {
|
|
59
|
+
const existing = await readJson(settingsPath);
|
|
60
|
+
if (!existing) return { removed: false, skippedReason: "settings-missing" };
|
|
61
|
+
|
|
62
|
+
const settings = normalizeSettings(existing);
|
|
63
|
+
const hooks = normalizeHooks(settings.hooks);
|
|
64
|
+
const entries = normalizeEntries(hooks[event]);
|
|
65
|
+
if (entries.length === 0) return { removed: false, skippedReason: "hook-missing" };
|
|
66
|
+
|
|
67
|
+
let removed = false;
|
|
68
|
+
const nextEntries = [];
|
|
69
|
+
for (const entry of entries) {
|
|
70
|
+
const res = stripHookFromEntry(entry, { hookCommand, hookName });
|
|
71
|
+
if (res.removed) removed = true;
|
|
72
|
+
if (res.entry) nextEntries.push(res.entry);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!removed) return { removed: false, skippedReason: "hook-missing" };
|
|
76
|
+
|
|
77
|
+
const nextHooks = { ...hooks };
|
|
78
|
+
if (nextEntries.length > 0) nextHooks[event] = nextEntries;
|
|
79
|
+
else delete nextHooks[event];
|
|
80
|
+
|
|
81
|
+
const nextSettings = { ...settings };
|
|
82
|
+
if (Object.keys(nextHooks).length > 0) nextSettings.hooks = nextHooks;
|
|
83
|
+
else delete nextSettings.hooks;
|
|
84
|
+
|
|
85
|
+
const backupPath = await writeGeminiSettings({ settingsPath, settings: nextSettings });
|
|
86
|
+
return { removed: true, skippedReason: null, backupPath };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function isGeminiHookConfigured({
|
|
90
|
+
settingsPath,
|
|
91
|
+
hookCommand,
|
|
92
|
+
hookName = DEFAULT_HOOK_NAME,
|
|
93
|
+
event = DEFAULT_EVENT,
|
|
94
|
+
}) {
|
|
95
|
+
const settings = await readJson(settingsPath);
|
|
96
|
+
if (!settings || typeof settings !== "object") return false;
|
|
97
|
+
const hooks = settings.hooks;
|
|
98
|
+
if (!hooks || typeof hooks !== "object") return false;
|
|
99
|
+
const entries = normalizeEntries(hooks[event]);
|
|
100
|
+
return hasHook(entries, { hookCommand, hookName });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function buildGeminiHookCommand(notifyPath, options) {
|
|
104
|
+
const cmd = typeof notifyPath === "string" ? notifyPath : "";
|
|
105
|
+
return buildNotifyCommand(cmd, "gemini", options);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function buildHookEntry({ hookCommand, hookName, matcher }) {
|
|
109
|
+
const hook = {
|
|
110
|
+
name: hookName,
|
|
111
|
+
type: "command",
|
|
112
|
+
command: hookCommand,
|
|
113
|
+
};
|
|
114
|
+
const entry = { hooks: [hook] };
|
|
115
|
+
if (typeof matcher === "string" && matcher.length > 0) entry.matcher = matcher;
|
|
116
|
+
return entry;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function normalizeSettings(raw) {
|
|
120
|
+
return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function normalizeHooks(raw) {
|
|
124
|
+
return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function normalizeTools(raw) {
|
|
128
|
+
return raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function normalizeEntries(raw) {
|
|
132
|
+
return Array.isArray(raw) ? raw.slice() : [];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function normalizeCommand(cmd) {
|
|
136
|
+
if (Array.isArray(cmd)) return cmd.map((v) => String(v)).join("\u0000");
|
|
137
|
+
if (typeof cmd === "string") return cmd.trim();
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function normalizeName(name) {
|
|
142
|
+
if (typeof name !== "string") return null;
|
|
143
|
+
const trimmed = name.trim();
|
|
144
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function ensureHooksEnabled(settings) {
|
|
148
|
+
const tools = normalizeTools(settings.tools);
|
|
149
|
+
if (tools.enableHooks === true) return { settings, changed: false };
|
|
150
|
+
const nextTools = { ...tools, enableHooks: true };
|
|
151
|
+
return { settings: { ...settings, tools: nextTools }, changed: true };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function hookMatches(hook, { hookCommand, hookName, requireCommand = false }) {
|
|
155
|
+
if (!hook || typeof hook !== "object") return false;
|
|
156
|
+
const name = normalizeName(hook.name);
|
|
157
|
+
const targetName = normalizeName(hookName);
|
|
158
|
+
const cmd = normalizeCommand(hook.command);
|
|
159
|
+
const targetCmd = normalizeCommand(hookCommand);
|
|
160
|
+
const commandMatches = Boolean(cmd && targetCmd && cmd === targetCmd);
|
|
161
|
+
if (requireCommand) return commandMatches;
|
|
162
|
+
const nameMatches = Boolean(name && targetName && name === targetName);
|
|
163
|
+
return Boolean(commandMatches || nameMatches);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function entryMatches(entry, { hookCommand, hookName, requireCommand = false }) {
|
|
167
|
+
if (!entry || typeof entry !== "object") return false;
|
|
168
|
+
if (entry.command || entry.name)
|
|
169
|
+
return hookMatches(entry, { hookCommand, hookName, requireCommand });
|
|
170
|
+
if (!Array.isArray(entry.hooks)) return false;
|
|
171
|
+
return entry.hooks.some((hook) => hookMatches(hook, { hookCommand, hookName, requireCommand }));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function hasHook(entries, { hookCommand, hookName }) {
|
|
175
|
+
return entries.some((entry) => entryMatches(entry, { hookCommand, hookName }));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function normalizeEntriesForHook(entries, { hookCommand, hookName }) {
|
|
179
|
+
let changed = false;
|
|
180
|
+
const nextEntries = entries.map((entry) => {
|
|
181
|
+
if (!entry || typeof entry !== "object") return entry;
|
|
182
|
+
|
|
183
|
+
if (entry.command || entry.name) {
|
|
184
|
+
if (hookMatches(entry, { hookCommand, hookName })) {
|
|
185
|
+
const next = normalizeHookObject(entry, { hookCommand, hookName });
|
|
186
|
+
if (next !== entry) changed = true;
|
|
187
|
+
return next;
|
|
188
|
+
}
|
|
189
|
+
return entry;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const hooks = Array.isArray(entry.hooks) ? entry.hooks : null;
|
|
193
|
+
if (!hooks) return entry;
|
|
194
|
+
|
|
195
|
+
let hooksChanged = false;
|
|
196
|
+
const nextHooks = hooks.map((hook) => {
|
|
197
|
+
if (hookMatches(hook, { hookCommand, hookName })) {
|
|
198
|
+
const next = normalizeHookObject(hook, { hookCommand, hookName });
|
|
199
|
+
if (next !== hook) hooksChanged = true;
|
|
200
|
+
return next;
|
|
201
|
+
}
|
|
202
|
+
return hook;
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
if (!hooksChanged) return entry;
|
|
206
|
+
changed = true;
|
|
207
|
+
return { ...entry, hooks: nextHooks };
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
return { entries: nextEntries, changed };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function normalizeHookObject(hook, { hookCommand, hookName }) {
|
|
214
|
+
const next = { ...hook };
|
|
215
|
+
let changed = false;
|
|
216
|
+
|
|
217
|
+
if (next.type !== "command") {
|
|
218
|
+
next.type = "command";
|
|
219
|
+
changed = true;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (hookCommand && next.command !== hookCommand) {
|
|
223
|
+
next.command = hookCommand;
|
|
224
|
+
changed = true;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (hookName && next.name !== hookName) {
|
|
228
|
+
next.name = hookName;
|
|
229
|
+
changed = true;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return changed ? next : hook;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function stripHookFromEntry(entry, { hookCommand, hookName }) {
|
|
236
|
+
if (!entry || typeof entry !== "object") return { entry, removed: false };
|
|
237
|
+
|
|
238
|
+
if (entry.command || entry.name) {
|
|
239
|
+
if (hookMatches(entry, { hookCommand, hookName, requireCommand: true }))
|
|
240
|
+
return { entry: null, removed: true };
|
|
241
|
+
return { entry, removed: false };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const hooks = Array.isArray(entry.hooks) ? entry.hooks : null;
|
|
245
|
+
if (!hooks) return { entry, removed: false };
|
|
246
|
+
|
|
247
|
+
const nextHooks = hooks.filter(
|
|
248
|
+
(hook) => !hookMatches(hook, { hookCommand, hookName, requireCommand: true }),
|
|
249
|
+
);
|
|
250
|
+
if (nextHooks.length === hooks.length) return { entry, removed: false };
|
|
251
|
+
if (nextHooks.length === 0) return { entry: null, removed: true };
|
|
252
|
+
|
|
253
|
+
return { entry: { ...entry, hooks: nextHooks }, removed: true };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function quoteArg(value) {
|
|
257
|
+
const v = typeof value === "string" ? value : "";
|
|
258
|
+
if (!v) return '""';
|
|
259
|
+
if (/^[A-Za-z0-9_\-./:@]+$/.test(v)) return v;
|
|
260
|
+
// Escape backslashes before quotes: a trailing backslash (Windows paths)
|
|
261
|
+
// would otherwise escape the closing quote and corrupt the hook command.
|
|
262
|
+
return `"${v.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async function writeGeminiSettings({ settingsPath, settings }) {
|
|
266
|
+
await ensureDir(path.dirname(settingsPath));
|
|
267
|
+
let backupPath = null;
|
|
268
|
+
try {
|
|
269
|
+
const st = await fs.stat(settingsPath);
|
|
270
|
+
if (st && st.isFile()) {
|
|
271
|
+
backupPath = `${settingsPath}.bak.${new Date().toISOString().replace(/[:.]/g, "-")}`;
|
|
272
|
+
await fs.copyFile(settingsPath, backupPath);
|
|
273
|
+
}
|
|
274
|
+
} catch (_e) {
|
|
275
|
+
// Ignore missing file.
|
|
276
|
+
}
|
|
277
|
+
await writeJson(settingsPath, settings);
|
|
278
|
+
return backupPath;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
module.exports = {
|
|
282
|
+
DEFAULT_EVENT,
|
|
283
|
+
DEFAULT_HOOK_NAME,
|
|
284
|
+
DEFAULT_MATCHER,
|
|
285
|
+
resolveGeminiConfigDir,
|
|
286
|
+
resolveGeminiSettingsPath,
|
|
287
|
+
buildGeminiHookCommand,
|
|
288
|
+
upsertGeminiHook,
|
|
289
|
+
removeGeminiHook,
|
|
290
|
+
isGeminiHookConfigured,
|
|
291
|
+
};
|