prompt-genie 0.2.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/bin/install.cjs +176 -0
- package/hooks/post_read.cjs +57 -0
- package/hooks/pre_read.cjs +106 -0
- package/hooks/setup.cjs +15 -0
- package/hooks/stats.cjs +37 -0
- package/hooks/stop_flush.cjs +161 -0
- package/package.json +23 -0
package/bin/install.cjs
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const os = require("os");
|
|
5
|
+
const https = require("https");
|
|
6
|
+
const readline = require("readline");
|
|
7
|
+
|
|
8
|
+
const HOOKS_DEST = path.join(os.homedir(), ".prompt-genie", "hooks");
|
|
9
|
+
const SETTINGS_FILE = path.join(os.homedir(), ".claude", "settings.json");
|
|
10
|
+
const CONFIG_FILE = path.join(os.homedir(), ".claude", "pg_config.json");
|
|
11
|
+
const HOOKS_SRC = path.join(__dirname, "..", "hooks");
|
|
12
|
+
|
|
13
|
+
const GRAPHQL_URL = "https://ouybbvbacjd3tbbvf3jpqbiizy.appsync-api.us-east-2.amazonaws.com/graphql";
|
|
14
|
+
const API_KEY = "da2-xj6noinlgffx3ms3lgeopbhrjq";
|
|
15
|
+
|
|
16
|
+
function loadJson(file) {
|
|
17
|
+
try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return {}; }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function saveJson(file, data) {
|
|
21
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
22
|
+
fs.writeFileSync(file, JSON.stringify(data, null, 2));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function ask(question) {
|
|
26
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
27
|
+
return new Promise((resolve) => rl.question(question, (ans) => { rl.close(); resolve(ans.trim()); }));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function gqlPost(query) {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
const body = JSON.stringify({ query });
|
|
33
|
+
const url = new URL(GRAPHQL_URL);
|
|
34
|
+
const req = https.request({
|
|
35
|
+
hostname: url.hostname,
|
|
36
|
+
path: url.pathname,
|
|
37
|
+
method: "POST",
|
|
38
|
+
headers: {
|
|
39
|
+
"Content-Type": "application/json",
|
|
40
|
+
"x-api-key": API_KEY,
|
|
41
|
+
"Content-Length": Buffer.byteLength(body),
|
|
42
|
+
},
|
|
43
|
+
}, (res) => {
|
|
44
|
+
let data = "";
|
|
45
|
+
res.on("data", (chunk) => (data += chunk));
|
|
46
|
+
res.on("end", () => resolve(JSON.parse(data)));
|
|
47
|
+
});
|
|
48
|
+
req.on("error", reject);
|
|
49
|
+
req.write(body);
|
|
50
|
+
req.end();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function copyHooks() {
|
|
55
|
+
fs.mkdirSync(HOOKS_DEST, { recursive: true });
|
|
56
|
+
for (const file of fs.readdirSync(HOOKS_SRC)) {
|
|
57
|
+
if (file.endsWith(".cjs")) {
|
|
58
|
+
fs.copyFileSync(path.join(HOOKS_SRC, file), path.join(HOOKS_DEST, file));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function wireHooks() {
|
|
64
|
+
const settings = loadJson(SETTINGS_FILE);
|
|
65
|
+
settings.hooks = settings.hooks || {};
|
|
66
|
+
|
|
67
|
+
settings.hooks.PreToolUse = [
|
|
68
|
+
{
|
|
69
|
+
matcher: "Read",
|
|
70
|
+
hooks: [{ type: "command", command: `node ${path.join(HOOKS_DEST, "pre_read.cjs")}` }],
|
|
71
|
+
},
|
|
72
|
+
];
|
|
73
|
+
settings.hooks.PostToolUse = [
|
|
74
|
+
{
|
|
75
|
+
matcher: "Read",
|
|
76
|
+
hooks: [{ type: "command", command: `node ${path.join(HOOKS_DEST, "post_read.cjs")}` }],
|
|
77
|
+
},
|
|
78
|
+
];
|
|
79
|
+
settings.hooks.Stop = [
|
|
80
|
+
{
|
|
81
|
+
hooks: [{ type: "command", command: `node ${path.join(HOOKS_DEST, "stop_flush.cjs")}` }],
|
|
82
|
+
},
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
saveJson(SETTINGS_FILE, settings);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function main() {
|
|
89
|
+
console.log("\n Prompt Genie ā Token Saver for Claude Code\n");
|
|
90
|
+
|
|
91
|
+
// Step 1: get email
|
|
92
|
+
const email = await ask(" Enter your Prompt Genie email: ");
|
|
93
|
+
if (!email || !email.includes("@")) {
|
|
94
|
+
console.error(" Invalid email. Run again with a valid address.");
|
|
95
|
+
process.exit(1);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Step 2: send magic link
|
|
99
|
+
console.log("\n Sending verification code to " + email + "...");
|
|
100
|
+
try {
|
|
101
|
+
const res = await gqlPost(
|
|
102
|
+
`mutation { cliAuth(action: "send_magic_link", email: ${JSON.stringify(email)}) }`
|
|
103
|
+
);
|
|
104
|
+
const result = JSON.parse(res.data?.cliAuth || "{}");
|
|
105
|
+
if (!result.success) {
|
|
106
|
+
console.error(" Failed to send code. Check your email and try again.");
|
|
107
|
+
process.exit(1);
|
|
108
|
+
}
|
|
109
|
+
} catch (err) {
|
|
110
|
+
console.error(" Network error:", err.message);
|
|
111
|
+
process.exit(1);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Step 3: ask for code
|
|
115
|
+
console.log(" Check your email for a 6-digit code.\n");
|
|
116
|
+
const code = await ask(" Enter code: ");
|
|
117
|
+
if (!code || code.length !== 6) {
|
|
118
|
+
console.error(" Invalid code.");
|
|
119
|
+
process.exit(1);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Step 4: verify code ā get JWT
|
|
123
|
+
console.log("\n Verifying...");
|
|
124
|
+
let token, plan;
|
|
125
|
+
try {
|
|
126
|
+
const res = await gqlPost(
|
|
127
|
+
`mutation { cliAuth(action: "verify_magic_link", email: ${JSON.stringify(email)}, code: ${JSON.stringify(code)}) }`
|
|
128
|
+
);
|
|
129
|
+
const result = JSON.parse(res.data?.cliAuth || "{}");
|
|
130
|
+
if (!result.success || !result.token) {
|
|
131
|
+
console.error(" " + (result.error || "Verification failed. Try again."));
|
|
132
|
+
process.exit(1);
|
|
133
|
+
}
|
|
134
|
+
token = result.token;
|
|
135
|
+
plan = result.plan;
|
|
136
|
+
} catch (err) {
|
|
137
|
+
console.error(" Network error:", err.message);
|
|
138
|
+
process.exit(1);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Step 5: install
|
|
142
|
+
console.log(" Installing hooks...");
|
|
143
|
+
copyHooks();
|
|
144
|
+
|
|
145
|
+
console.log(" Wiring Claude Code settings...");
|
|
146
|
+
wireHooks();
|
|
147
|
+
|
|
148
|
+
console.log(" Saving config...");
|
|
149
|
+
saveJson(CONFIG_FILE, { email, plan, token });
|
|
150
|
+
|
|
151
|
+
// Step 6: result message based on plan
|
|
152
|
+
const isPaid = ["PRO", "ANNUAL_PRO", "THREE_DAY_PASS", "TEAMS", "ENTERPRISE"].includes(plan);
|
|
153
|
+
|
|
154
|
+
if (isPaid) {
|
|
155
|
+
console.log(`
|
|
156
|
+
ā
All done! Caching is active.
|
|
157
|
+
|
|
158
|
+
Restart VS Code to activate.
|
|
159
|
+
Your token savings will appear at prompt-genie.com after your first session.
|
|
160
|
+
`);
|
|
161
|
+
} else {
|
|
162
|
+
console.log(`
|
|
163
|
+
ā
Installed! You're on the Free plan.
|
|
164
|
+
|
|
165
|
+
Caching is disabled on Free ā you'll see how many tokens you would have
|
|
166
|
+
saved at the end of each session.
|
|
167
|
+
|
|
168
|
+
Upgrade to Pro to activate ā prompt-genie.com/pricing
|
|
169
|
+
`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
main().catch((err) => {
|
|
174
|
+
console.error("Install failed:", err.message);
|
|
175
|
+
process.exit(1);
|
|
176
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
|
|
5
|
+
const CACHE_FILE = path.join(process.env.HOME, ".claude/pg_read_cache.json");
|
|
6
|
+
const STATS_FILE = path.join(process.env.HOME, ".claude/pg_stats.json");
|
|
7
|
+
const SESSION_FILE = path.join(process.env.HOME, ".claude/pg_session.json");
|
|
8
|
+
|
|
9
|
+
function loadJson(file) {
|
|
10
|
+
try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return {}; }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function saveJson(file, data) {
|
|
14
|
+
fs.writeFileSync(file, JSON.stringify(data, null, 2));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function recordMiss() {
|
|
18
|
+
const stats = loadJson(STATS_FILE);
|
|
19
|
+
stats.total_misses = (stats.total_misses || 0) + 1;
|
|
20
|
+
saveJson(STATS_FILE, stats);
|
|
21
|
+
|
|
22
|
+
const session = loadJson(SESSION_FILE);
|
|
23
|
+
session.misses = (session.misses || 0) + 1;
|
|
24
|
+
session.date = new Date().toISOString().slice(0, 10);
|
|
25
|
+
saveJson(SESSION_FILE, session);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let raw = "";
|
|
29
|
+
process.stdin.on("data", (chunk) => (raw += chunk));
|
|
30
|
+
process.stdin.on("end", () => {
|
|
31
|
+
let data;
|
|
32
|
+
try { data = JSON.parse(raw); } catch { process.exit(0); }
|
|
33
|
+
|
|
34
|
+
if (data.tool_name !== "Read") process.exit(0);
|
|
35
|
+
|
|
36
|
+
const filePath = data.tool_input?.file_path;
|
|
37
|
+
let content = data.tool_response ?? "";
|
|
38
|
+
|
|
39
|
+
if (typeof content === "object") {
|
|
40
|
+
const blocks = content.content;
|
|
41
|
+
content = Array.isArray(blocks)
|
|
42
|
+
? blocks.map((b) => b.text ?? "").join("")
|
|
43
|
+
: JSON.stringify(content);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!filePath || !content || !fs.existsSync(filePath)) process.exit(0);
|
|
47
|
+
|
|
48
|
+
let mtime;
|
|
49
|
+
try { mtime = fs.statSync(filePath).mtimeMs; } catch { process.exit(0); }
|
|
50
|
+
|
|
51
|
+
const cache = loadJson(CACHE_FILE);
|
|
52
|
+
cache[filePath] = { mtime, content };
|
|
53
|
+
saveJson(CACHE_FILE, cache);
|
|
54
|
+
recordMiss();
|
|
55
|
+
|
|
56
|
+
process.exit(0);
|
|
57
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const crypto = require("crypto");
|
|
5
|
+
|
|
6
|
+
const CACHE_FILE = path.join(process.env.HOME, ".claude/pg_read_cache.json");
|
|
7
|
+
const STATS_FILE = path.join(process.env.HOME, ".claude/pg_stats.json");
|
|
8
|
+
const SESSION_FILE = path.join(process.env.HOME, ".claude/pg_session.json");
|
|
9
|
+
const CONFIG_FILE = path.join(process.env.HOME, ".claude/pg_config.json");
|
|
10
|
+
|
|
11
|
+
const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
|
|
12
|
+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArjBMJfK6K8JnPvZR3kuS
|
|
13
|
+
d80nO2gvF2AULghE6WNtD1N0+k2GPShpGBiV/6WugrP840i8MRL+fyBid7DQw6Tj
|
|
14
|
+
eqZ7lj8wHTKglNZCRgOvmV+Q9LOpUfDCV+znUlEJLlbZy73X2CNPN6D2kfKPL7yT
|
|
15
|
+
Yo9HJG2j2n0BQhrQELbSct1q4hNMwcie2X5S9mR+lwcxRFEsvLuVe33hH0Rk6CSz
|
|
16
|
+
BP0MCcqwkHCs8a5bdm2U5KveIv1pMUwwl8HKki3rjYbv7scdXPgERs27tRbpLdYj
|
|
17
|
+
2+MhWaGHrt21pfASIKPwFiZaMhOV49hT3CC3rRY4zgtExFfYIx8qHt4LDM3rSheM
|
|
18
|
+
dwIDAQAB
|
|
19
|
+
-----END PUBLIC KEY-----`;
|
|
20
|
+
|
|
21
|
+
function loadJson(file) {
|
|
22
|
+
try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return {}; }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function saveJson(file, data) {
|
|
26
|
+
fs.writeFileSync(file, JSON.stringify(data, null, 2));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function estimateTokens(text) {
|
|
30
|
+
return Math.max(1, Math.floor(text.length / 4));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Returns decoded payload or null if invalid/expired
|
|
34
|
+
function verifyJWT(token) {
|
|
35
|
+
try {
|
|
36
|
+
const [header, payload, signature] = token.split(".");
|
|
37
|
+
if (!header || !payload || !signature) return null;
|
|
38
|
+
|
|
39
|
+
const verify = crypto.createVerify("RSA-SHA256");
|
|
40
|
+
verify.update(`${header}.${payload}`);
|
|
41
|
+
const valid = verify.verify(PUBLIC_KEY, signature, "base64url");
|
|
42
|
+
if (!valid) return null;
|
|
43
|
+
|
|
44
|
+
const decoded = JSON.parse(Buffer.from(payload, "base64url").toString());
|
|
45
|
+
if (decoded.exp < Math.floor(Date.now() / 1000)) return null; // expired
|
|
46
|
+
return decoded;
|
|
47
|
+
} catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isPaidPlan(plan) {
|
|
53
|
+
return ["PRO", "ANNUAL_PRO", "THREE_DAY_PASS", "TEAMS", "ENTERPRISE"].includes(plan);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function recordHit(filePath, tokensSaved) {
|
|
57
|
+
const stats = loadJson(STATS_FILE);
|
|
58
|
+
stats.total_hits = (stats.total_hits || 0) + 1;
|
|
59
|
+
stats.total_tokens_saved = (stats.total_tokens_saved || 0) + tokensSaved;
|
|
60
|
+
stats.files = stats.files || {};
|
|
61
|
+
stats.files[filePath] = stats.files[filePath] || { hits: 0, tokens_saved: 0 };
|
|
62
|
+
stats.files[filePath].hits += 1;
|
|
63
|
+
stats.files[filePath].tokens_saved += tokensSaved;
|
|
64
|
+
saveJson(STATS_FILE, stats);
|
|
65
|
+
|
|
66
|
+
const session = loadJson(SESSION_FILE);
|
|
67
|
+
session.hits = (session.hits || 0) + 1;
|
|
68
|
+
session.tokensSaved = (session.tokensSaved || 0) + tokensSaved;
|
|
69
|
+
session.date = new Date().toISOString().slice(0, 10);
|
|
70
|
+
saveJson(SESSION_FILE, session);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let raw = "";
|
|
74
|
+
process.stdin.on("data", (chunk) => (raw += chunk));
|
|
75
|
+
process.stdin.on("end", () => {
|
|
76
|
+
let data;
|
|
77
|
+
try { data = JSON.parse(raw); } catch { process.exit(0); }
|
|
78
|
+
|
|
79
|
+
if (data.tool_name !== "Read") process.exit(0);
|
|
80
|
+
|
|
81
|
+
// Check JWT ā gate cache on paid plan
|
|
82
|
+
const config = loadJson(CONFIG_FILE);
|
|
83
|
+
const token = config.token;
|
|
84
|
+
if (!token) process.exit(0);
|
|
85
|
+
|
|
86
|
+
const jwt = verifyJWT(token);
|
|
87
|
+
if (!jwt || !isPaidPlan(jwt.plan)) process.exit(0);
|
|
88
|
+
|
|
89
|
+
const filePath = data.tool_input?.file_path;
|
|
90
|
+
if (!filePath || !fs.existsSync(filePath)) process.exit(0);
|
|
91
|
+
|
|
92
|
+
let mtime;
|
|
93
|
+
try { mtime = fs.statSync(filePath).mtimeMs; } catch { process.exit(0); }
|
|
94
|
+
|
|
95
|
+
const cache = loadJson(CACHE_FILE);
|
|
96
|
+
const entry = cache[filePath];
|
|
97
|
+
|
|
98
|
+
if (entry && entry.mtime === mtime) {
|
|
99
|
+
const tokensSaved = estimateTokens(entry.content);
|
|
100
|
+
recordHit(filePath, tokensSaved);
|
|
101
|
+
process.stdout.write(entry.content);
|
|
102
|
+
process.exit(2);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
process.exit(0);
|
|
106
|
+
});
|
package/hooks/setup.cjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
|
|
5
|
+
const CONFIG_FILE = path.join(process.env.HOME, ".claude/pg_config.json");
|
|
6
|
+
|
|
7
|
+
const email = process.argv[2];
|
|
8
|
+
if (!email || !email.includes("@")) {
|
|
9
|
+
console.log("Usage: node hooks/setup.cjs your@email.com");
|
|
10
|
+
process.exit(1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
fs.writeFileSync(CONFIG_FILE, JSON.stringify({ email }, null, 2));
|
|
14
|
+
console.log(`Prompt Genie configured for ${email}`);
|
|
15
|
+
console.log("Token savings will now sync to your dashboard after each session.");
|
package/hooks/stats.cjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
|
|
5
|
+
const STATS_FILE = path.join(process.env.HOME, ".claude/pg_stats.json");
|
|
6
|
+
|
|
7
|
+
if (!fs.existsSync(STATS_FILE)) {
|
|
8
|
+
console.log("No data yet ā stats are recorded after your first Claude Code session.");
|
|
9
|
+
process.exit(0);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const stats = JSON.parse(fs.readFileSync(STATS_FILE, "utf8"));
|
|
13
|
+
const hits = stats.total_hits || 0;
|
|
14
|
+
const misses = stats.total_misses || 0;
|
|
15
|
+
const total = hits + misses;
|
|
16
|
+
const tokensSaved = stats.total_tokens_saved || 0;
|
|
17
|
+
const hitRate = total ? ((hits / total) * 100).toFixed(1) : "0.0";
|
|
18
|
+
const costSaved = ((tokensSaved / 1_000_000) * 3).toFixed(4);
|
|
19
|
+
|
|
20
|
+
console.log("============================================");
|
|
21
|
+
console.log(" Prompt Genie ā Read Cache Stats");
|
|
22
|
+
console.log("============================================");
|
|
23
|
+
console.log(` Cache hits : ${hits.toLocaleString()}`);
|
|
24
|
+
console.log(` Cache misses : ${misses.toLocaleString()}`);
|
|
25
|
+
console.log(` Hit rate : ${hitRate}%`);
|
|
26
|
+
console.log(` Tokens saved : ~${tokensSaved.toLocaleString()}`);
|
|
27
|
+
console.log(` Cost saved : ~$${costSaved} (at $3/M input tokens)`);
|
|
28
|
+
console.log("============================================");
|
|
29
|
+
|
|
30
|
+
const files = stats.files || {};
|
|
31
|
+
const sorted = Object.entries(files).sort((a, b) => b[1].tokens_saved - a[1].tokens_saved);
|
|
32
|
+
if (sorted.length) {
|
|
33
|
+
console.log("\n Top files by tokens saved:");
|
|
34
|
+
sorted.slice(0, 10).forEach(([p, d]) => {
|
|
35
|
+
console.log(` ${String(d.tokens_saved).padStart(6)} tokens (${d.hits} hits) ${path.basename(p)}`);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const https = require("https");
|
|
5
|
+
const crypto = require("crypto");
|
|
6
|
+
|
|
7
|
+
const SESSION_FILE = path.join(process.env.HOME, ".claude/pg_session.json");
|
|
8
|
+
const CONFIG_FILE = path.join(process.env.HOME, ".claude/pg_config.json");
|
|
9
|
+
|
|
10
|
+
const GRAPHQL_URL = "https://ouybbvbacjd3tbbvf3jpqbiizy.appsync-api.us-east-2.amazonaws.com/graphql";
|
|
11
|
+
const API_KEY = "da2-xj6noinlgffx3ms3lgeopbhrjq";
|
|
12
|
+
|
|
13
|
+
const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
|
|
14
|
+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArjBMJfK6K8JnPvZR3kuS
|
|
15
|
+
d80nO2gvF2AULghE6WNtD1N0+k2GPShpGBiV/6WugrP840i8MRL+fyBid7DQw6Tj
|
|
16
|
+
eqZ7lj8wHTKglNZCRgOvmV+Q9LOpUfDCV+znUlEJLlbZy73X2CNPN6D2kfKPL7yT
|
|
17
|
+
Yo9HJG2j2n0BQhrQELbSct1q4hNMwcie2X5S9mR+lwcxRFEsvLuVe33hH0Rk6CSz
|
|
18
|
+
BP0MCcqwkHCs8a5bdm2U5KveIv1pMUwwl8HKki3rjYbv7scdXPgERs27tRbpLdYj
|
|
19
|
+
2+MhWaGHrt21pfASIKPwFiZaMhOV49hT3CC3rRY4zgtExFfYIx8qHt4LDM3rSheM
|
|
20
|
+
dwIDAQAB
|
|
21
|
+
-----END PUBLIC KEY-----`;
|
|
22
|
+
|
|
23
|
+
function loadJson(file) {
|
|
24
|
+
try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return {}; }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isPaidPlan(plan) {
|
|
28
|
+
return ["PRO", "ANNUAL_PRO", "THREE_DAY_PASS", "TEAMS", "ENTERPRISE"].includes(plan);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Returns decoded payload or null if invalid/expired
|
|
32
|
+
function verifyJWT(token) {
|
|
33
|
+
try {
|
|
34
|
+
const [header, payload, signature] = token.split(".");
|
|
35
|
+
if (!header || !payload || !signature) return null;
|
|
36
|
+
|
|
37
|
+
const verify = crypto.createVerify("RSA-SHA256");
|
|
38
|
+
verify.update(`${header}.${payload}`);
|
|
39
|
+
const valid = verify.verify(PUBLIC_KEY, signature, "base64url");
|
|
40
|
+
if (!valid) return null;
|
|
41
|
+
|
|
42
|
+
return JSON.parse(Buffer.from(payload, "base64url").toString());
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function decodeJWT(token) {
|
|
49
|
+
try {
|
|
50
|
+
const [, payload] = token.split(".");
|
|
51
|
+
return JSON.parse(Buffer.from(payload, "base64url").toString());
|
|
52
|
+
} catch { return null; }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function gqlPost(query, variables) {
|
|
56
|
+
return new Promise((resolve, reject) => {
|
|
57
|
+
const body = JSON.stringify({ query, variables });
|
|
58
|
+
const url = new URL(GRAPHQL_URL);
|
|
59
|
+
const req = https.request({
|
|
60
|
+
hostname: url.hostname,
|
|
61
|
+
path: url.pathname,
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers: {
|
|
64
|
+
"Content-Type": "application/json",
|
|
65
|
+
"x-api-key": API_KEY,
|
|
66
|
+
"Content-Length": Buffer.byteLength(body),
|
|
67
|
+
},
|
|
68
|
+
}, (res) => {
|
|
69
|
+
let data = "";
|
|
70
|
+
res.on("data", (chunk) => (data += chunk));
|
|
71
|
+
res.on("end", () => resolve(JSON.parse(data)));
|
|
72
|
+
});
|
|
73
|
+
req.on("error", reject);
|
|
74
|
+
req.write(body);
|
|
75
|
+
req.end();
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function refreshToken(oldToken) {
|
|
80
|
+
try {
|
|
81
|
+
const res = await gqlPost(
|
|
82
|
+
`mutation { cliAuth(action: "refresh_token", token: ${JSON.stringify(oldToken)}) }`,
|
|
83
|
+
{}
|
|
84
|
+
);
|
|
85
|
+
const result = JSON.parse(res.data?.cliAuth || "{}");
|
|
86
|
+
if (result.token) {
|
|
87
|
+
const config = loadJson(CONFIG_FILE);
|
|
88
|
+
config.token = result.token;
|
|
89
|
+
config.plan = result.plan;
|
|
90
|
+
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
} catch { /* silent */ }
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function main() {
|
|
98
|
+
const session = loadJson(SESSION_FILE);
|
|
99
|
+
const config = loadJson(CONFIG_FILE);
|
|
100
|
+
|
|
101
|
+
const hits = session.hits || 0;
|
|
102
|
+
const misses = session.misses || 0;
|
|
103
|
+
const tokensSaved = session.tokensSaved || 0;
|
|
104
|
+
|
|
105
|
+
if (hits === 0 && misses === 0) process.exit(0);
|
|
106
|
+
|
|
107
|
+
const token = config.token;
|
|
108
|
+
if (!token) process.exit(0);
|
|
109
|
+
|
|
110
|
+
// Verify or refresh JWT
|
|
111
|
+
let jwt = verifyJWT(token);
|
|
112
|
+
let plan = jwt?.plan;
|
|
113
|
+
|
|
114
|
+
if (!jwt) {
|
|
115
|
+
// Token invalid or expired ā try refresh
|
|
116
|
+
const refreshed = await refreshToken(token);
|
|
117
|
+
if (refreshed) {
|
|
118
|
+
plan = refreshed.plan;
|
|
119
|
+
jwt = decodeJWT(refreshed.token);
|
|
120
|
+
} else {
|
|
121
|
+
process.exit(0);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// FREE plan ā show FOMO, don't write stats
|
|
126
|
+
if (!isPaidPlan(plan)) {
|
|
127
|
+
const savings = tokensSaved.toLocaleString();
|
|
128
|
+
process.stderr.write(
|
|
129
|
+
`\nš” Prompt Genie: You would have saved ~${savings} tokens this session.\n` +
|
|
130
|
+
` Upgrade to Pro to activate caching ā prompt-genie.com\n\n`
|
|
131
|
+
);
|
|
132
|
+
fs.writeFileSync(SESSION_FILE, JSON.stringify({}));
|
|
133
|
+
process.exit(0);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// PRO/TEAMS ā write stats
|
|
137
|
+
try {
|
|
138
|
+
const decoded = jwt || decodeJWT(token);
|
|
139
|
+
await gqlPost(
|
|
140
|
+
`mutation CreateSmartContextSessionStats($input: CreateSmartContextSessionStatsInput!) {
|
|
141
|
+
createSmartContextSessionStats(input: $input) { id }
|
|
142
|
+
}`,
|
|
143
|
+
{
|
|
144
|
+
input: {
|
|
145
|
+
email: decoded.email,
|
|
146
|
+
sessionDate: session.date || new Date().toISOString().slice(0, 10),
|
|
147
|
+
hits,
|
|
148
|
+
misses,
|
|
149
|
+
tokensSaved,
|
|
150
|
+
source: "CLAUDE_CODE",
|
|
151
|
+
createdAt: new Date().toISOString(),
|
|
152
|
+
},
|
|
153
|
+
}
|
|
154
|
+
);
|
|
155
|
+
fs.writeFileSync(SESSION_FILE, JSON.stringify({}));
|
|
156
|
+
} catch { /* silent */ }
|
|
157
|
+
|
|
158
|
+
process.exit(0);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "prompt-genie",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Save tokens in Claude Code by caching file reads",
|
|
5
|
+
"bin": {
|
|
6
|
+
"prompt-genie": "./bin/install.cjs"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"bin",
|
|
10
|
+
"hooks"
|
|
11
|
+
],
|
|
12
|
+
"keywords": [
|
|
13
|
+
"claude",
|
|
14
|
+
"claude-code",
|
|
15
|
+
"tokens",
|
|
16
|
+
"ai",
|
|
17
|
+
"prompt"
|
|
18
|
+
],
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=16"
|
|
22
|
+
}
|
|
23
|
+
}
|