claude-token-saver 2.0.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Installs the Claude Code integration assets:
3
+ * - Skill: ~/.claude/skills/claude-token-saver/SKILL.md
4
+ * - Slash: ~/.claude/commands/token-monitor.md
5
+ *
6
+ * All paths are resolved with node:path so Windows backslashes and POSIX
7
+ * forward-slashes are both handled. Directories are created with
8
+ * `mkdirSync(..., { recursive: true })` which is a no-op if they already
9
+ * exist on every platform.
10
+ */
11
+
12
+ import { writeFileSync, mkdirSync, existsSync } from 'node:fs';
13
+ import { join } from 'node:path';
14
+ import { claudeUserDir } from './paths.js';
15
+
16
+ const SKILL_BODY = `---
17
+ name: claude-token-saver
18
+ description: Use when the user mentions Claude Code token usage, prompt cache hit rate, TTL/expiry, the 1M context window, cache misses, output spikes, or anything in the statusline produced by claude-token-saver (chips like "⚠ 1M ON", "⚠ Input spike", "⚠ Cache miss", "⚠ 5m TTL", "⚠ Rebuild churn", "⚠ Output heavy", "⚠ Call surge", "⏳ Cache expires", "💰 Cache saved", "🧠 Cache hit"). Also use when they ask to view token-usage history or want to understand a warning they just saw.
19
+ ---
20
+
21
+ # claude-token-saver — Claude Code Token Monitor
22
+
23
+ This skill helps users interpret and act on the \`claude-token-saver\` statusline
24
+ in Claude Code. The statusline updates every ~1s and shows cache health, TTL
25
+ countdown, savings, and (when relevant) a leading warning chip.
26
+
27
+ ## When this skill should activate
28
+
29
+ - The user references any chip wording: \`⚠ 1M ON\`, \`⚠ Input spike\`,
30
+ \`⚠ Cache miss\`, \`⚠ 5m TTL\`, \`⚠ Rebuild churn\`, \`⚠ Output heavy\`,
31
+ \`⚠ Call surge\`.
32
+ - The user asks "why is my cache hit rate low", "what does this warning mean",
33
+ "when did this start happening", or similar.
34
+ - The user wants to see the token-usage history file or asks for a summary
35
+ of recent warnings.
36
+
37
+ ## What to do
38
+
39
+ 1. **Identify the chip.** If the user pasted a statusline, pull out the leading
40
+ \`⚠ ...\` chip. That maps to a specific issue category.
41
+ 2. **Show recent history.** Run \`claude-token-saver history\` (default last 7
42
+ days) to see the chronology of warning transitions. Each entry is timestamped
43
+ and includes a short detail string.
44
+ 3. **Drill down on the live state.** Run \`claude-token-saver --days 1\` (or
45
+ another window) to render the full table view, which lists per-session
46
+ spikes and recommended actions.
47
+ 4. **Explain the warning** in plain language. Use the chip → cause table:
48
+
49
+ | Chip | Likely cause |
50
+ | ------------------ | ----------------------------------------------------- |
51
+ | \`⚠ 1M ON\` | Auto-promoted to 1M context (Opus 4.7+ Max default). |
52
+ | \`⚠ Input spike\` | One request consumed >250k or >3× the recent p95. |
53
+ | \`⚠ Cache miss\` | Cache hit rate dropped below ~70%. |
54
+ | \`⚠ 5m TTL\` | Most cache writes are 5-min ephemeral (Pro plan default). |
55
+ | \`⚠ Rebuild churn\` | Cache being re-written rapidly — prefix is unstable. |
56
+ | \`⚠ Output heavy\` | Output ratio dominates input — inspect long generations. |
57
+ | \`⚠ Call surge\` | Request count is well above baseline. |
58
+
59
+ 5. **Suggest the next action.** For 1M ON, mention
60
+ \`CLAUDE_CODE_DISABLE_1M_CONTEXT=1\`. For 5m TTL, point at the Max plan's
61
+ 1h bucket. For input spike, suggest splitting the conversation or
62
+ compacting context.
63
+
64
+ ## Useful commands
65
+
66
+ - \`claude-token-saver\` — full table report (default last 1 day).
67
+ - \`claude-token-saver --days 7\` — wider window.
68
+ - \`claude-token-saver history\` — recent warning transitions per day.
69
+ - \`claude-token-saver history --days 30\` — longer history.
70
+ - \`claude-token-saver mode\` — show statusline preferences.
71
+ - \`claude-token-saver mode icon verbose 1d\` — change preferences.
72
+
73
+ ## Storage layout (for reference)
74
+
75
+ History files live under the OS-appropriate user-data dir:
76
+ - Windows: \`%APPDATA%\\claude-token-saver\\history\\YYYY-MM-DD.md\`
77
+ - macOS: \`~/Library/Application Support/claude-token-saver/history/YYYY-MM-DD.md\`
78
+ - Linux: \`~/.config/claude-token-saver/history/YYYY-MM-DD.md\`
79
+
80
+ Each day's file is plain Markdown — safe to open in any editor.
81
+ `;
82
+
83
+ const COMMAND_BODY = `---
84
+ description: Show recent claude-token-saver warning history and a fresh report.
85
+ ---
86
+
87
+ You are responding to the \`/token-monitor\` slash command. The user wants a
88
+ quick read of their Claude Code token usage and any active warnings.
89
+
90
+ Steps:
91
+
92
+ 1. Run \`claude-token-saver history --days 7\` and capture the output. This
93
+ prints recent warning transitions (timestamps + chip + short detail).
94
+ 2. Run \`claude-token-saver --days 1\` and capture the output. This prints the
95
+ full table view: TTL breakdown, cost impact, daily trend, and any active
96
+ spikes with recommended actions.
97
+ 3. Summarize for the user:
98
+ - **Active warnings** — list the most recent unresolved chip(s) with the
99
+ time they appeared.
100
+ - **Today's pattern** — when warnings cluster in time, mention it.
101
+ - **Recommended action** — pick the highest-leverage suggestion from the
102
+ table report's "Recommended actions" section.
103
+ 4. If the history is empty, say so plainly — no warnings means the cache has
104
+ been healthy in the configured window.
105
+
106
+ Keep the summary to ~10 lines. The user can re-run the underlying commands
107
+ themselves for the full output.
108
+ `;
109
+
110
+ function writeIfNeeded(file, body, force) {
111
+ const existed = existsSync(file);
112
+ if (existed && !force) return { path: file, action: 'exists' };
113
+ writeFileSync(file, body);
114
+ return { path: file, action: existed ? 'updated' : 'created' };
115
+ }
116
+
117
+ export function installSkill({ force = false } = {}) {
118
+ const dir = join(claudeUserDir(), 'skills', 'claude-token-saver');
119
+ const file = join(dir, 'SKILL.md');
120
+ mkdirSync(dir, { recursive: true });
121
+ return writeIfNeeded(file, SKILL_BODY, force);
122
+ }
123
+
124
+ export function installCommand({ force = false } = {}) {
125
+ const dir = join(claudeUserDir(), 'commands');
126
+ const file = join(dir, 'token-monitor.md');
127
+ mkdirSync(dir, { recursive: true });
128
+ return writeIfNeeded(file, COMMAND_BODY, force);
129
+ }
130
+
131
+ export function installAll({ force = false } = {}) {
132
+ return {
133
+ skill: installSkill({ force }),
134
+ command: installCommand({ force }),
135
+ };
136
+ }
package/src/paths.js ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Cross-platform user-data path resolution.
3
+ *
4
+ * Order of precedence:
5
+ * 1. $XDG_CONFIG_HOME (explicit override, honored on every platform)
6
+ * 2. %APPDATA% on Windows (e.g. C:\Users\foo\AppData\Roaming)
7
+ * 3. ~/Library/Application Support on macOS
8
+ * 4. ~/.config on Linux / fallback
9
+ *
10
+ * All paths are joined via node:path so the OS-correct separator is used
11
+ * automatically. Callers are responsible for `mkdirSync(..., { recursive: true })`
12
+ * before writing — every helper here returns a path string only.
13
+ */
14
+
15
+ import { join } from 'node:path';
16
+ import { homedir } from 'node:os';
17
+
18
+ /**
19
+ * Returns the base directory for this tool's user-level data
20
+ * (config, history, last-chip state).
21
+ */
22
+ export function userDataDir() {
23
+ if (process.env.XDG_CONFIG_HOME) {
24
+ return join(process.env.XDG_CONFIG_HOME, 'claude-token-saver');
25
+ }
26
+ if (process.platform === 'win32' && process.env.APPDATA) {
27
+ return join(process.env.APPDATA, 'claude-token-saver');
28
+ }
29
+ if (process.platform === 'darwin') {
30
+ return join(homedir(), 'Library', 'Application Support', 'claude-token-saver');
31
+ }
32
+ return join(homedir(), '.config', 'claude-token-saver');
33
+ }
34
+
35
+ /**
36
+ * Returns the user's Claude Code config root (~/.claude on every OS Claude
37
+ * Code supports — the CLI itself uses this path on Windows and macOS too).
38
+ */
39
+ export function claudeUserDir() {
40
+ return join(homedir(), '.claude');
41
+ }