@ipv9/tokentracker-cli 0.39.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/LICENSE +21 -0
- package/README.ja.md +464 -0
- package/README.ko.md +464 -0
- package/README.md +476 -0
- package/README.zh-CN.md +469 -0
- package/bin/tracker.js +52 -0
- package/dashboard/dist/app-icon.png +0 -0
- package/dashboard/dist/apple-touch-icon.png +0 -0
- package/dashboard/dist/assets/ActivityHeatmap-BxBGHY1z.js +42 -0
- package/dashboard/dist/assets/Card-GROxUc_6.js +1 -0
- package/dashboard/dist/assets/DashboardPage-BZ5VFoBk.js +19 -0
- package/dashboard/dist/assets/DevicePage-CQts7ivB.js +1 -0
- package/dashboard/dist/assets/DialogTitle-DqFsk26U.js +1 -0
- package/dashboard/dist/assets/FadeIn-HBBXW_3q.js +1 -0
- package/dashboard/dist/assets/HeaderGithubStar-COD2HfaF.js +1 -0
- package/dashboard/dist/assets/IpCheckPage-Cn0l8-BQ.js +15 -0
- package/dashboard/dist/assets/LandingPage-CmV1BMU4.js +4356 -0
- package/dashboard/dist/assets/LeaderboardAvatar-CiIpWZwb.js +1 -0
- package/dashboard/dist/assets/LeaderboardPage-BU0mlR3g.js +6 -0
- package/dashboard/dist/assets/LeaderboardProfileModal-C49_3Xm9.js +72 -0
- package/dashboard/dist/assets/LeaderboardProfilePage-Bh1n0BAM.js +1 -0
- package/dashboard/dist/assets/LimitsPage-fZAmwLWT.js +2 -0
- package/dashboard/dist/assets/LocalOnlyNotice-D3rQdA-P.js +1 -0
- package/dashboard/dist/assets/LoginPage-enO4XP-c.js +1 -0
- package/dashboard/dist/assets/PopoverPopup-pitJEpN1.js +1 -0
- package/dashboard/dist/assets/Select-tT9x4ntD.js +1 -0
- package/dashboard/dist/assets/SelectItemText-C7F7eIm0.js +1 -0
- package/dashboard/dist/assets/SettingsPage-pDasZNU2.js +1 -0
- package/dashboard/dist/assets/SkillsPage-DEu7FY-2.js +1 -0
- package/dashboard/dist/assets/WidgetsPage-DGcO7rbp.js +1 -0
- package/dashboard/dist/assets/WrappedPage-D_1ID213.js +1 -0
- package/dashboard/dist/assets/agent-logos-DXQVg1xy.js +1 -0
- package/dashboard/dist/assets/arrow-up-right-DNLo92Ba.js +1 -0
- package/dashboard/dist/assets/download-DbBBBpJF.js +1 -0
- package/dashboard/dist/assets/geist-mono-cyrillic-400-normal-BPBWmzPh.woff +0 -0
- package/dashboard/dist/assets/geist-mono-cyrillic-400-normal-Ce5q_31Z.woff2 +0 -0
- package/dashboard/dist/assets/geist-mono-cyrillic-500-normal-CJBLNVQT.woff2 +0 -0
- package/dashboard/dist/assets/geist-mono-cyrillic-500-normal-mNhfPmgl.woff +0 -0
- package/dashboard/dist/assets/geist-mono-cyrillic-700-normal-DH5Q319x.woff +0 -0
- package/dashboard/dist/assets/geist-mono-cyrillic-700-normal-VCNRadI3.woff2 +0 -0
- package/dashboard/dist/assets/geist-mono-cyrillic-900-normal-BKC3PZkM.woff +0 -0
- package/dashboard/dist/assets/geist-mono-cyrillic-900-normal-DdrQ8KBw.woff2 +0 -0
- package/dashboard/dist/assets/geist-mono-latin-400-normal-CoULgQGM.woff +0 -0
- package/dashboard/dist/assets/geist-mono-latin-400-normal-LC9RFr9I.woff2 +0 -0
- package/dashboard/dist/assets/geist-mono-latin-500-normal-D3o2eNa9.woff2 +0 -0
- package/dashboard/dist/assets/geist-mono-latin-500-normal-DOxI7kZ4.woff +0 -0
- package/dashboard/dist/assets/geist-mono-latin-700-normal-D6izGJRP.woff2 +0 -0
- package/dashboard/dist/assets/geist-mono-latin-700-normal-QGw08Lff.woff +0 -0
- package/dashboard/dist/assets/geist-mono-latin-900-normal-CmoKXrdK.woff +0 -0
- package/dashboard/dist/assets/geist-mono-latin-900-normal-Cu5MFKsu.woff2 +0 -0
- package/dashboard/dist/assets/geist-mono-latin-ext-400-normal-Cgks_Qgx.woff2 +0 -0
- package/dashboard/dist/assets/geist-mono-latin-ext-400-normal-CxNRRMGd.woff +0 -0
- package/dashboard/dist/assets/geist-mono-latin-ext-500-normal-CQcGuCNt.woff2 +0 -0
- package/dashboard/dist/assets/geist-mono-latin-ext-500-normal-diTenJ8L.woff +0 -0
- package/dashboard/dist/assets/geist-mono-latin-ext-700-normal-BX9f1BHp.woff +0 -0
- package/dashboard/dist/assets/geist-mono-latin-ext-700-normal-YOllDaLV.woff2 +0 -0
- package/dashboard/dist/assets/geist-mono-latin-ext-900-normal-Br6WNUGb.woff +0 -0
- package/dashboard/dist/assets/geist-mono-latin-ext-900-normal-CrmBTzU2.woff2 +0 -0
- package/dashboard/dist/assets/index-BeoRn2gJ.js +2 -0
- package/dashboard/dist/assets/info-Bh5qTz6k.js +1 -0
- package/dashboard/dist/assets/main-BIY3Nj6I.js +969 -0
- package/dashboard/dist/assets/main-G_F9Iu_x.css +1 -0
- package/dashboard/dist/assets/use-limits-display-prefs-B4iJ-bBc.js +1 -0
- package/dashboard/dist/assets/use-native-settings-DHjPFDv8.js +1 -0
- package/dashboard/dist/assets/use-usage-limits-odqe9OY7.js +1 -0
- package/dashboard/dist/assets/useCurrency-BsSZiZMP.js +1 -0
- package/dashboard/dist/assets/useOpenInteractionType-BQhgq4rv.js +12 -0
- package/dashboard/dist/brand-logos/antigravity.svg +1 -0
- package/dashboard/dist/brand-logos/claude-code.svg +1 -0
- package/dashboard/dist/brand-logos/codebuddy.svg +1 -0
- package/dashboard/dist/brand-logos/codex.svg +15 -0
- package/dashboard/dist/brand-logos/copilot.svg +1 -0
- package/dashboard/dist/brand-logos/cursor.svg +1 -0
- package/dashboard/dist/brand-logos/gemini.svg +1 -0
- package/dashboard/dist/brand-logos/grok.svg +1 -0
- package/dashboard/dist/brand-logos/hermes.svg +1 -0
- package/dashboard/dist/brand-logos/kilo.svg +4 -0
- package/dashboard/dist/brand-logos/kimi.svg +1 -0
- package/dashboard/dist/brand-logos/kiro.svg +1 -0
- package/dashboard/dist/brand-logos/openclaw.svg +22 -0
- package/dashboard/dist/brand-logos/opencode.svg +3 -0
- package/dashboard/dist/clawd/happy.svg +161 -0
- package/dashboard/dist/clawd/idle/collapse.svg +101 -0
- package/dashboard/dist/clawd/idle/doze.svg +72 -0
- package/dashboard/dist/clawd/idle/follow.svg +64 -0
- package/dashboard/dist/clawd/idle/living.svg +196 -0
- package/dashboard/dist/clawd/idle/look.svg +115 -0
- package/dashboard/dist/clawd/idle/yawn.svg +158 -0
- package/dashboard/dist/clawd/mini/alert.svg +129 -0
- package/dashboard/dist/clawd/mini/crabwalk.svg +76 -0
- package/dashboard/dist/clawd/mini/enter-sleep.svg +65 -0
- package/dashboard/dist/clawd/mini/enter.svg +115 -0
- package/dashboard/dist/clawd/mini/happy.svg +124 -0
- package/dashboard/dist/clawd/mini/idle-tight.svg +15 -0
- package/dashboard/dist/clawd/mini/idle.svg +83 -0
- package/dashboard/dist/clawd/mini/peek.svg +82 -0
- package/dashboard/dist/clawd/mini/sleep.svg +112 -0
- package/dashboard/dist/clawd/react/double.svg +108 -0
- package/dashboard/dist/clawd/react/drag.svg +102 -0
- package/dashboard/dist/clawd/react/left.svg +102 -0
- package/dashboard/dist/clawd/react/right.svg +102 -0
- package/dashboard/dist/clawd/sleep/collapse-sleep.svg +247 -0
- package/dashboard/dist/clawd/sleep/sleeping.svg +118 -0
- package/dashboard/dist/clawd/sleep/wake.svg +277 -0
- package/dashboard/dist/clawd/static-base.svg +21 -0
- package/dashboard/dist/clawd/status/disconnected.svg +136 -0
- package/dashboard/dist/clawd/status/error.svg +94 -0
- package/dashboard/dist/clawd/status/notification.svg +134 -0
- package/dashboard/dist/clawd/working/building.svg +231 -0
- package/dashboard/dist/clawd/working/carrying.svg +107 -0
- package/dashboard/dist/clawd/working/conducting.svg +118 -0
- package/dashboard/dist/clawd/working/confused.svg +113 -0
- package/dashboard/dist/clawd/working/debugger.svg +91 -0
- package/dashboard/dist/clawd/working/juggling.svg +82 -0
- package/dashboard/dist/clawd/working/overheated.svg +75 -0
- package/dashboard/dist/clawd/working/pushing.svg +125 -0
- package/dashboard/dist/clawd/working/sweeping.svg +79 -0
- package/dashboard/dist/clawd/working/thinking.svg +119 -0
- package/dashboard/dist/clawd/working/typing.svg +153 -0
- package/dashboard/dist/clawd/working/ultrathink.svg +166 -0
- package/dashboard/dist/clawd/working/wizard.svg +98 -0
- package/dashboard/dist/dashboard-dark.png +0 -0
- package/dashboard/dist/favicon-16.png +0 -0
- package/dashboard/dist/favicon-32.png +0 -0
- package/dashboard/dist/favicon.ico +0 -0
- package/dashboard/dist/feed.xml +34 -0
- package/dashboard/dist/icon-192.png +0 -0
- package/dashboard/dist/icon-512.png +0 -0
- package/dashboard/dist/index.html +355 -0
- package/dashboard/dist/llms.txt +53 -0
- package/dashboard/dist/neon_bg.png +0 -0
- package/dashboard/dist/robots.txt +7 -0
- package/dashboard/dist/share.html +140 -0
- package/dashboard/dist/sitemap.xml +27 -0
- package/dashboard/dist/widgets-overview.png +0 -0
- package/package.json +78 -0
- package/scripts/install-local-service.sh +140 -0
- package/scripts/uninstall-local-service.sh +21 -0
- package/src/cli.js +95 -0
- package/src/commands/device-login.js +161 -0
- package/src/commands/diagnostics.js +38 -0
- package/src/commands/doctor.js +96 -0
- package/src/commands/init.js +1195 -0
- package/src/commands/serve.js +351 -0
- package/src/commands/status.js +609 -0
- package/src/commands/sync.js +2285 -0
- package/src/commands/uninstall.js +189 -0
- package/src/commands/wrapped.js +150 -0
- package/src/lib/antigravity-paths.js +48 -0
- package/src/lib/browser-auth.js +281 -0
- package/src/lib/categorizer-utils.js +232 -0
- package/src/lib/claude-categorizer.js +1319 -0
- package/src/lib/claude-config.js +204 -0
- package/src/lib/cli-ui.js +179 -0
- package/src/lib/codex-config.js +350 -0
- package/src/lib/codex-context-breakdown.js +400 -0
- package/src/lib/codex-rollout-parser.js +623 -0
- package/src/lib/codex-token-refresh.js +112 -0
- package/src/lib/cursor-config.js +425 -0
- package/src/lib/debug-flags.js +7 -0
- package/src/lib/diagnostics.js +272 -0
- package/src/lib/doctor.js +338 -0
- package/src/lib/fs.js +91 -0
- package/src/lib/gemini-config.js +288 -0
- package/src/lib/grok-hook.js +351 -0
- package/src/lib/init-flow.js +42 -0
- package/src/lib/local-api.js +1861 -0
- package/src/lib/openclaw-hook.js +420 -0
- package/src/lib/openclaw-session-plugin.js +520 -0
- package/src/lib/opencode-config.js +99 -0
- package/src/lib/passive-mode.js +185 -0
- package/src/lib/pricing/curated-overrides.json +79 -0
- package/src/lib/pricing/index.js +143 -0
- package/src/lib/pricing/litellm-fetcher.js +172 -0
- package/src/lib/pricing/matcher.js +255 -0
- package/src/lib/pricing/seed-snapshot.json +1 -0
- package/src/lib/progress.js +76 -0
- package/src/lib/project-usage-purge.js +106 -0
- package/src/lib/prompt.js +24 -0
- package/src/lib/proxy-env.js +131 -0
- package/src/lib/rollout.js +8442 -0
- package/src/lib/runtime-config.js +122 -0
- package/src/lib/skill-usage.js +267 -0
- package/src/lib/skills-manager.js +1127 -0
- package/src/lib/source-metadata.js +48 -0
- package/src/lib/sqlite-reader.js +136 -0
- package/src/lib/static-server.js +59 -0
- package/src/lib/subscriptions.js +453 -0
- package/src/lib/tracker-paths.js +16 -0
- package/src/lib/upload-throttle.js +148 -0
- package/src/lib/usage-limits.js +1806 -0
- package/src/lib/wrapped-aggregator.js +225 -0
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
const os = require("node:os");
|
|
2
|
+
const path = require("node:path");
|
|
3
|
+
const fs = require("node:fs/promises");
|
|
4
|
+
const fssync = require("node:fs");
|
|
5
|
+
const cp = require("node:child_process");
|
|
6
|
+
|
|
7
|
+
const OPENCLAW_HOOK_NAME = "tokentracker-openclaw-sync";
|
|
8
|
+
const OPENCLAW_HOOK_DIRNAME = "openclaw-hook";
|
|
9
|
+
|
|
10
|
+
function resolveOpenclawHookPaths({ home = os.homedir(), trackerDir, env = process.env } = {}) {
|
|
11
|
+
if (!trackerDir) throw new Error("trackerDir is required");
|
|
12
|
+
|
|
13
|
+
const openclawConfigPath =
|
|
14
|
+
normalizeString(env.OPENCLAW_CONFIG_PATH) || path.join(home, ".openclaw", "openclaw.json");
|
|
15
|
+
|
|
16
|
+
const openclawHome =
|
|
17
|
+
normalizeString(env.TOKENTRACKER_OPENCLAW_HOME) ||
|
|
18
|
+
normalizeString(env.OPENCLAW_STATE_DIR) ||
|
|
19
|
+
path.join(home, ".openclaw");
|
|
20
|
+
|
|
21
|
+
const hookDir = path.join(trackerDir, OPENCLAW_HOOK_DIRNAME);
|
|
22
|
+
const hookEntryDir = path.join(hookDir, OPENCLAW_HOOK_NAME);
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
hookName: OPENCLAW_HOOK_NAME,
|
|
26
|
+
hookDir,
|
|
27
|
+
hookEntryDir,
|
|
28
|
+
openclawConfigPath,
|
|
29
|
+
openclawHome,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function installOpenclawHook({
|
|
34
|
+
home = os.homedir(),
|
|
35
|
+
trackerDir,
|
|
36
|
+
packageName = "@ipv9/tokentracker-cli",
|
|
37
|
+
env = process.env,
|
|
38
|
+
} = {}) {
|
|
39
|
+
const paths = resolveOpenclawHookPaths({ home, trackerDir, env });
|
|
40
|
+
|
|
41
|
+
await ensureOpenclawHookFiles({
|
|
42
|
+
hookDir: paths.hookDir,
|
|
43
|
+
trackerDir,
|
|
44
|
+
packageName,
|
|
45
|
+
openclawHome: paths.openclawHome,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const installResult = runOpenclawCli(["hooks", "install", "--link", paths.hookDir], env);
|
|
49
|
+
if (installResult.skippedReason) {
|
|
50
|
+
return { configured: false, ...paths, ...installResult };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const state = await probeOpenclawHookState({ home, trackerDir, env });
|
|
54
|
+
return {
|
|
55
|
+
configured: state.configured,
|
|
56
|
+
changed: /Linked hook path:/i.test(installResult.stdout || ""),
|
|
57
|
+
...paths,
|
|
58
|
+
stdout: installResult.stdout,
|
|
59
|
+
stderr: installResult.stderr,
|
|
60
|
+
code: installResult.code,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function ensureOpenclawHookFiles({
|
|
65
|
+
hookDir,
|
|
66
|
+
trackerDir,
|
|
67
|
+
packageName = "@ipv9/tokentracker-cli",
|
|
68
|
+
openclawHome,
|
|
69
|
+
} = {}) {
|
|
70
|
+
if (!hookDir || !trackerDir) throw new Error("hookDir and trackerDir are required");
|
|
71
|
+
|
|
72
|
+
const hookEntryDir = path.join(hookDir, OPENCLAW_HOOK_NAME);
|
|
73
|
+
await fs.mkdir(hookEntryDir, { recursive: true });
|
|
74
|
+
|
|
75
|
+
const hookMdPath = path.join(hookEntryDir, "HOOK.md");
|
|
76
|
+
const handlerPath = path.join(hookEntryDir, "handler.js");
|
|
77
|
+
|
|
78
|
+
await fs.writeFile(hookMdPath, buildHookMarkdown(), "utf8");
|
|
79
|
+
await fs.writeFile(
|
|
80
|
+
handlerPath,
|
|
81
|
+
buildHookHandler({
|
|
82
|
+
trackerDir,
|
|
83
|
+
packageName,
|
|
84
|
+
openclawHome: openclawHome || path.join(os.homedir(), ".openclaw"),
|
|
85
|
+
}),
|
|
86
|
+
"utf8",
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function probeOpenclawHookState({ home = os.homedir(), trackerDir, env = process.env } = {}) {
|
|
91
|
+
const paths = resolveOpenclawHookPaths({ home, trackerDir, env });
|
|
92
|
+
const { openclawConfigPath, hookDir, hookEntryDir, hookName } = paths;
|
|
93
|
+
|
|
94
|
+
const hookFilesReady =
|
|
95
|
+
fssync.existsSync(path.join(hookEntryDir, "HOOK.md")) &&
|
|
96
|
+
fssync.existsSync(path.join(hookEntryDir, "handler.js"));
|
|
97
|
+
|
|
98
|
+
let cfg = null;
|
|
99
|
+
try {
|
|
100
|
+
const raw = await fs.readFile(openclawConfigPath, "utf8");
|
|
101
|
+
cfg = JSON.parse(raw);
|
|
102
|
+
} catch (err) {
|
|
103
|
+
if (err?.code === "ENOENT" || err?.code === "ENOTDIR") {
|
|
104
|
+
return {
|
|
105
|
+
configured: false,
|
|
106
|
+
enabled: false,
|
|
107
|
+
linked: false,
|
|
108
|
+
hookFilesReady,
|
|
109
|
+
skippedReason: "openclaw-config-missing",
|
|
110
|
+
...paths,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
configured: false,
|
|
115
|
+
enabled: false,
|
|
116
|
+
linked: false,
|
|
117
|
+
hookFilesReady,
|
|
118
|
+
skippedReason: "openclaw-config-unreadable",
|
|
119
|
+
error: err?.message || String(err),
|
|
120
|
+
...paths,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const enabled = Boolean(cfg?.hooks?.internal?.entries?.[hookName]?.enabled);
|
|
125
|
+
const extraDirs = Array.isArray(cfg?.hooks?.internal?.load?.extraDirs)
|
|
126
|
+
? cfg.hooks.internal.load.extraDirs
|
|
127
|
+
: [];
|
|
128
|
+
const normalizedHookDir = path.resolve(hookDir);
|
|
129
|
+
const linked = extraDirs.some((entry) => path.resolve(String(entry || "")) === normalizedHookDir);
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
configured: enabled && linked,
|
|
133
|
+
enabled,
|
|
134
|
+
linked,
|
|
135
|
+
hookFilesReady,
|
|
136
|
+
...paths,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function removeOpenclawHookConfig({
|
|
141
|
+
home = os.homedir(),
|
|
142
|
+
trackerDir,
|
|
143
|
+
env = process.env,
|
|
144
|
+
} = {}) {
|
|
145
|
+
const paths = resolveOpenclawHookPaths({ home, trackerDir, env });
|
|
146
|
+
const { openclawConfigPath, hookDir, hookName } = paths;
|
|
147
|
+
|
|
148
|
+
let cfg;
|
|
149
|
+
try {
|
|
150
|
+
cfg = JSON.parse(await fs.readFile(openclawConfigPath, "utf8"));
|
|
151
|
+
} catch (err) {
|
|
152
|
+
if (err?.code === "ENOENT" || err?.code === "ENOTDIR") {
|
|
153
|
+
return { removed: false, skippedReason: "openclaw-config-missing", ...paths };
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
removed: false,
|
|
157
|
+
skippedReason: "openclaw-config-unreadable",
|
|
158
|
+
error: err?.message || String(err),
|
|
159
|
+
...paths,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
let changed = false;
|
|
164
|
+
const hooks = cfg?.hooks;
|
|
165
|
+
const internal = hooks?.internal;
|
|
166
|
+
|
|
167
|
+
if (internal?.entries && Object.prototype.hasOwnProperty.call(internal.entries, hookName)) {
|
|
168
|
+
delete internal.entries[hookName];
|
|
169
|
+
changed = true;
|
|
170
|
+
if (Object.keys(internal.entries).length === 0) delete internal.entries;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (internal?.load && Array.isArray(internal.load.extraDirs)) {
|
|
174
|
+
const before = internal.load.extraDirs;
|
|
175
|
+
const target = path.resolve(hookDir);
|
|
176
|
+
const after = before.filter((entry) => path.resolve(String(entry || "")) !== target);
|
|
177
|
+
if (after.length !== before.length) {
|
|
178
|
+
internal.load.extraDirs = after;
|
|
179
|
+
changed = true;
|
|
180
|
+
if (after.length === 0) delete internal.load.extraDirs;
|
|
181
|
+
if (Object.keys(internal.load).length === 0) delete internal.load;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (internal?.installs && typeof internal.installs === "object") {
|
|
186
|
+
const installs = internal.installs;
|
|
187
|
+
if (Object.prototype.hasOwnProperty.call(installs, hookName)) {
|
|
188
|
+
delete installs[hookName];
|
|
189
|
+
changed = true;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const target = path.resolve(hookDir);
|
|
193
|
+
for (const [id, entry] of Object.entries(installs)) {
|
|
194
|
+
const sourcePath = normalizeString(entry?.sourcePath);
|
|
195
|
+
const installPath = normalizeString(entry?.installPath);
|
|
196
|
+
if (
|
|
197
|
+
(sourcePath && path.resolve(sourcePath) === target) ||
|
|
198
|
+
(installPath && path.resolve(installPath) === target)
|
|
199
|
+
) {
|
|
200
|
+
delete installs[id];
|
|
201
|
+
changed = true;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (Object.keys(installs).length === 0) delete internal.installs;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (internal && Object.keys(internal).length === 0) {
|
|
209
|
+
delete hooks.internal;
|
|
210
|
+
changed = true;
|
|
211
|
+
}
|
|
212
|
+
if (hooks && Object.keys(hooks).length === 0) {
|
|
213
|
+
delete cfg.hooks;
|
|
214
|
+
changed = true;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (changed) {
|
|
218
|
+
await fs.writeFile(openclawConfigPath, `${JSON.stringify(cfg, null, 2)}\n`, "utf8");
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
await fs.rm(hookDir, { recursive: true, force: true }).catch(() => {});
|
|
222
|
+
|
|
223
|
+
return { removed: changed, ...paths };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function runOpenclawCli(args, env = process.env) {
|
|
227
|
+
let res;
|
|
228
|
+
try {
|
|
229
|
+
res = cp.spawnSync("openclaw", args, {
|
|
230
|
+
env,
|
|
231
|
+
encoding: "utf8",
|
|
232
|
+
timeout: 30_000,
|
|
233
|
+
});
|
|
234
|
+
} catch (err) {
|
|
235
|
+
return {
|
|
236
|
+
code: 1,
|
|
237
|
+
skippedReason: err?.code === "ENOENT" ? "openclaw-cli-missing" : "openclaw-cli-error",
|
|
238
|
+
error: err?.message || String(err),
|
|
239
|
+
stdout: "",
|
|
240
|
+
stderr: "",
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (res.error?.code === "ENOENT") {
|
|
245
|
+
return {
|
|
246
|
+
code: 1,
|
|
247
|
+
skippedReason: "openclaw-cli-missing",
|
|
248
|
+
error: res.error.message,
|
|
249
|
+
stdout: res.stdout || "",
|
|
250
|
+
stderr: res.stderr || "",
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if ((res.status || 0) !== 0) {
|
|
255
|
+
return {
|
|
256
|
+
code: Number(res.status || 1),
|
|
257
|
+
skippedReason: "openclaw-hooks-install-failed",
|
|
258
|
+
error: (res.stderr || res.stdout || "").trim() || "openclaw hooks install failed",
|
|
259
|
+
stdout: res.stdout || "",
|
|
260
|
+
stderr: res.stderr || "",
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return {
|
|
265
|
+
code: 0,
|
|
266
|
+
stdout: res.stdout || "",
|
|
267
|
+
stderr: res.stderr || "",
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function buildHookMarkdown() {
|
|
272
|
+
return `---
|
|
273
|
+
name: ${OPENCLAW_HOOK_NAME}
|
|
274
|
+
description: "Trigger tokentracker sync when OpenClaw sessions roll over"
|
|
275
|
+
metadata:
|
|
276
|
+
{ "openclaw": { "emoji": "📈", "events": ["command:new", "command:reset", "command:stop"], "requires": { "bins": ["node"] } } }
|
|
277
|
+
---
|
|
278
|
+
|
|
279
|
+
# TokenTracker OpenClaw Sync Hook
|
|
280
|
+
|
|
281
|
+
Triggers non-blocking 'tokentracker sync --auto --from-openclaw' runs when OpenClaw command events indicate session rollover/reset/stop.
|
|
282
|
+
`;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function buildHookHandler({ trackerDir, packageName = "@ipv9/tokentracker-cli", openclawHome }) {
|
|
286
|
+
const trackerBinPath = path.join(trackerDir, "app", "bin", "tracker.js");
|
|
287
|
+
const fallbackPkg = packageName || "@ipv9/tokentracker-cli";
|
|
288
|
+
const safeOpenclawHome = openclawHome || path.join(os.homedir(), ".openclaw");
|
|
289
|
+
|
|
290
|
+
return (
|
|
291
|
+
`'use strict';\n` +
|
|
292
|
+
`const fs = require('node:fs');\n` +
|
|
293
|
+
`const path = require('node:path');\n` +
|
|
294
|
+
`const cp = require('node:child_process');\n` +
|
|
295
|
+
`const trackerDir = ${JSON.stringify(trackerDir)};\n` +
|
|
296
|
+
`const trackerBinPath = ${JSON.stringify(trackerBinPath)};\n` +
|
|
297
|
+
`const fallbackPkg = ${JSON.stringify(fallbackPkg)};\n` +
|
|
298
|
+
`const openclawHome = ${JSON.stringify(safeOpenclawHome)};\n` +
|
|
299
|
+
`const throttlePath = path.join(trackerDir, 'openclaw.sync.throttle');\n` +
|
|
300
|
+
`const depsMarkerPath = path.join(trackerDir, 'app', 'bin', 'tracker.js');\n` +
|
|
301
|
+
`const THROTTLE_MS = 15_000;\n` +
|
|
302
|
+
`\n` +
|
|
303
|
+
`module.exports = async function handler(event) {\n` +
|
|
304
|
+
` try {\n` +
|
|
305
|
+
` if (!event || event.type !== 'command') return;\n` +
|
|
306
|
+
` if (event.action !== 'new' && event.action !== 'reset' && event.action !== 'stop') return;\n` +
|
|
307
|
+
`\n` +
|
|
308
|
+
` const sessionKey = normalize(event.sessionKey);\n` +
|
|
309
|
+
` const agentId = parseAgentId(sessionKey);\n` +
|
|
310
|
+
` if (!agentId) return;\n` +
|
|
311
|
+
`\n` +
|
|
312
|
+
` const sessionEntry = resolveSessionEntry(event);\n` +
|
|
313
|
+
` const sessionId = normalize(sessionEntry && sessionEntry.sessionId) || resolveSessionId(event);\n` +
|
|
314
|
+
` if (!sessionId) return;\n` +
|
|
315
|
+
`\n` +
|
|
316
|
+
` const now = Date.now();\n` +
|
|
317
|
+
` let last = 0;\n` +
|
|
318
|
+
` try { last = Number(fs.readFileSync(throttlePath, 'utf8')) || 0; } catch (_) {}\n` +
|
|
319
|
+
` if (now - last < THROTTLE_MS) return;\n` +
|
|
320
|
+
` try {\n` +
|
|
321
|
+
` fs.mkdirSync(trackerDir, { recursive: true });\n` +
|
|
322
|
+
` fs.writeFileSync(throttlePath, String(now), 'utf8');\n` +
|
|
323
|
+
` } catch (_) {}\n` +
|
|
324
|
+
`\n` +
|
|
325
|
+
` const env = {\n` +
|
|
326
|
+
` ...process.env,\n` +
|
|
327
|
+
` TOKENTRACKER_OPENCLAW_AGENT_ID: agentId,\n` +
|
|
328
|
+
` TOKENTRACKER_OPENCLAW_SESSION_KEY: sessionKey,\n` +
|
|
329
|
+
` TOKENTRACKER_OPENCLAW_PREV_SESSION_ID: sessionId,\n` +
|
|
330
|
+
` TOKENTRACKER_OPENCLAW_HOME: openclawHome\n` +
|
|
331
|
+
` };\n` +
|
|
332
|
+
` const prevTotalTokens = toNonNegativeInt(sessionEntry && sessionEntry.totalTokens);\n` +
|
|
333
|
+
` const prevInputTokens = toNonNegativeInt(sessionEntry && sessionEntry.inputTokens);\n` +
|
|
334
|
+
` const prevOutputTokens = toNonNegativeInt(sessionEntry && sessionEntry.outputTokens);\n` +
|
|
335
|
+
` const prevModel = normalize(sessionEntry && sessionEntry.model);\n` +
|
|
336
|
+
` const prevUpdatedAt = toIso(sessionEntry && sessionEntry.updatedAt);\n` +
|
|
337
|
+
` if (prevTotalTokens != null) env.TOKENTRACKER_OPENCLAW_PREV_TOTAL_TOKENS = String(prevTotalTokens);\n` +
|
|
338
|
+
` if (prevInputTokens != null) env.TOKENTRACKER_OPENCLAW_PREV_INPUT_TOKENS = String(prevInputTokens);\n` +
|
|
339
|
+
` if (prevOutputTokens != null) env.TOKENTRACKER_OPENCLAW_PREV_OUTPUT_TOKENS = String(prevOutputTokens);\n` +
|
|
340
|
+
` if (prevModel) env.TOKENTRACKER_OPENCLAW_PREV_MODEL = prevModel;\n` +
|
|
341
|
+
` if (prevUpdatedAt) env.TOKENTRACKER_OPENCLAW_PREV_UPDATED_AT = prevUpdatedAt;\n` +
|
|
342
|
+
`\n` +
|
|
343
|
+
` const hasLocalRuntime = fs.existsSync(trackerBinPath);\n` +
|
|
344
|
+
` const hasLocalDeps = fs.existsSync(depsMarkerPath);\n` +
|
|
345
|
+
` const cmd = hasLocalRuntime && hasLocalDeps\n` +
|
|
346
|
+
` ? [process.execPath, trackerBinPath, 'sync', '--auto', '--from-openclaw']\n` +
|
|
347
|
+
` : ['npx', '--yes', fallbackPkg, 'sync', '--auto', '--from-openclaw'];\n` +
|
|
348
|
+
`\n` +
|
|
349
|
+
` const child = cp.spawn(cmd[0], cmd.slice(1), { detached: true, stdio: 'ignore', env });\n` +
|
|
350
|
+
` child.unref();\n` +
|
|
351
|
+
` } catch (_) {}\n` +
|
|
352
|
+
`};\n` +
|
|
353
|
+
`\n` +
|
|
354
|
+
`function normalize(v) {\n` +
|
|
355
|
+
` if (typeof v !== 'string') return null;\n` +
|
|
356
|
+
` const s = v.trim();\n` +
|
|
357
|
+
` return s.length > 0 ? s : null;\n` +
|
|
358
|
+
`}\n` +
|
|
359
|
+
`\n` +
|
|
360
|
+
`function resolveSessionEntry(event) {\n` +
|
|
361
|
+
` const ctx = (event && event.context && typeof event.context === 'object') ? event.context : {};\n` +
|
|
362
|
+
` if (!event || event.type !== 'command') return null;\n` +
|
|
363
|
+
` if (event.action === 'stop') return (ctx.sessionEntry && typeof ctx.sessionEntry === 'object') ? ctx.sessionEntry : null;\n` +
|
|
364
|
+
` if (ctx.previousSessionEntry && typeof ctx.previousSessionEntry === 'object') return ctx.previousSessionEntry;\n` +
|
|
365
|
+
` if (ctx.sessionEntry && typeof ctx.sessionEntry === 'object') return ctx.sessionEntry;\n` +
|
|
366
|
+
` return null;\n` +
|
|
367
|
+
`}\n` +
|
|
368
|
+
`\n` +
|
|
369
|
+
`function toNonNegativeInt(v) {\n` +
|
|
370
|
+
` const n = Number(v);\n` +
|
|
371
|
+
` if (!Number.isFinite(n) || n < 0) return null;\n` +
|
|
372
|
+
` return Math.floor(n);\n` +
|
|
373
|
+
`}\n` +
|
|
374
|
+
`\n` +
|
|
375
|
+
`function toIso(v) {\n` +
|
|
376
|
+
` if (typeof v === 'string') {\n` +
|
|
377
|
+
` const s = normalize(v);\n` +
|
|
378
|
+
` if (s && !Number.isNaN(Date.parse(s))) return s;\n` +
|
|
379
|
+
` }\n` +
|
|
380
|
+
` const n = Number(v);\n` +
|
|
381
|
+
` if (!Number.isFinite(n) || n <= 0) return null;\n` +
|
|
382
|
+
` const ms = n < 1e12 ? Math.floor(n * 1000) : Math.floor(n);\n` +
|
|
383
|
+
` const d = new Date(ms);\n` +
|
|
384
|
+
` return Number.isNaN(d.getTime()) ? null : d.toISOString();\n` +
|
|
385
|
+
`}\n` +
|
|
386
|
+
`\n` +
|
|
387
|
+
`function parseAgentId(sessionKey) {\n` +
|
|
388
|
+
` const s = normalize(sessionKey);\n` +
|
|
389
|
+
` if (!s || !s.startsWith('agent:')) return null;\n` +
|
|
390
|
+
` const parts = s.split(':');\n` +
|
|
391
|
+
` return parts.length >= 2 ? normalize(parts[1]) : null;\n` +
|
|
392
|
+
`}\n` +
|
|
393
|
+
`\n` +
|
|
394
|
+
`function resolveSessionId(event) {\n` +
|
|
395
|
+
` const ctx = (event && event.context && typeof event.context === 'object') ? event.context : {};\n` +
|
|
396
|
+
` return (\n` +
|
|
397
|
+
` normalize(ctx.previousSessionEntry && ctx.previousSessionEntry.sessionId) ||\n` +
|
|
398
|
+
` normalize(ctx.previousSessionId) ||\n` +
|
|
399
|
+
` normalize(ctx.sessionEntry && ctx.sessionEntry.sessionId) ||\n` +
|
|
400
|
+
` normalize(ctx.sessionId)\n` +
|
|
401
|
+
` );\n` +
|
|
402
|
+
`}\n`
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function normalizeString(value) {
|
|
407
|
+
if (typeof value !== "string") return null;
|
|
408
|
+
const trimmed = value.trim();
|
|
409
|
+
return trimmed.length > 0 ? trimmed : null;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
module.exports = {
|
|
413
|
+
OPENCLAW_HOOK_NAME,
|
|
414
|
+
OPENCLAW_HOOK_DIRNAME,
|
|
415
|
+
resolveOpenclawHookPaths,
|
|
416
|
+
ensureOpenclawHookFiles,
|
|
417
|
+
installOpenclawHook,
|
|
418
|
+
probeOpenclawHookState,
|
|
419
|
+
removeOpenclawHookConfig,
|
|
420
|
+
};
|