xithead 1.2.2 → 1.4.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/README.md +72 -187
- package/npm/bin/xithead.js +366 -366
- package/npm/index.d.ts +57 -57
- package/npm/index.js +139 -212
- package/npm/package.json +54 -0
- package/npm/postinstall.js +43 -43
- package/package.json +36 -53
package/npm/bin/xithead.js
CHANGED
|
@@ -1,366 +1,366 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* xithead CLI — Claude Context Optimizer
|
|
4
|
-
* by Xtream-ITSolutions UG | discord.gg/xtream-itsolutions
|
|
5
|
-
*/
|
|
6
|
-
'use strict';
|
|
7
|
-
|
|
8
|
-
const { spawnSync, spawn, execSync } = require('child_process');
|
|
9
|
-
const http = require('http');
|
|
10
|
-
const https = require('https');
|
|
11
|
-
const path = require('path');
|
|
12
|
-
const fs = require('fs');
|
|
13
|
-
|
|
14
|
-
const PKG_PATH = path.join(__dirname, '../../package.json');
|
|
15
|
-
const PKG_VERSION = (() => {
|
|
16
|
-
try { return require(PKG_PATH).version; } catch { return '1.2.0'; }
|
|
17
|
-
})();
|
|
18
|
-
|
|
19
|
-
const args = process.argv.slice(2);
|
|
20
|
-
const PROXY_PORT = 8787;
|
|
21
|
-
const PROXY_URL = `http://localhost:${PROXY_PORT}`;
|
|
22
|
-
|
|
23
|
-
// ── Update logic ──────────────────────────────────────────────────────────────
|
|
24
|
-
|
|
25
|
-
const UPDATE_CACHE = path.join(
|
|
26
|
-
process.env.APPDATA || process.env.HOME || require('os').homedir(),
|
|
27
|
-
'.xithead', 'last_update_check'
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
function shouldCheckUpdate() {
|
|
31
|
-
try {
|
|
32
|
-
const ts = parseInt(fs.readFileSync(UPDATE_CACHE, 'utf8').trim(), 10);
|
|
33
|
-
return Date.now() - ts > 6 * 60 * 60 * 1000; // once per 6h
|
|
34
|
-
} catch { return true; }
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function markUpdateChecked() {
|
|
38
|
-
try {
|
|
39
|
-
fs.mkdirSync(path.dirname(UPDATE_CACHE), { recursive: true });
|
|
40
|
-
fs.writeFileSync(UPDATE_CACHE, String(Date.now()));
|
|
41
|
-
} catch {}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function fetchLatestNpmVersion() {
|
|
45
|
-
return new Promise(resolve => {
|
|
46
|
-
const req = https.get('https://registry.npmjs.org/xithead/latest', { timeout: 5000 }, res => {
|
|
47
|
-
let body = '';
|
|
48
|
-
res.on('data', d => body += d);
|
|
49
|
-
res.on('end', () => {
|
|
50
|
-
try { resolve(JSON.parse(body).version || null); } catch { resolve(null); }
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
req.on('error', () => resolve(null));
|
|
54
|
-
req.on('timeout', () => { req.destroy(); resolve(null); });
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function versionGt(a, b) {
|
|
59
|
-
const pa = a.split('.').map(Number);
|
|
60
|
-
const pb = b.split('.').map(Number);
|
|
61
|
-
for (let i = 0; i < 3; i++) {
|
|
62
|
-
if ((pa[i]||0) > (pb[i]||0)) return true;
|
|
63
|
-
if ((pa[i]||0) < (pb[i]||0)) return false;
|
|
64
|
-
}
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async function autoUpdate(py) {
|
|
69
|
-
if (!shouldCheckUpdate()) return;
|
|
70
|
-
markUpdateChecked();
|
|
71
|
-
|
|
72
|
-
const latest = await fetchLatestNpmVersion();
|
|
73
|
-
if (!latest || !versionGt(latest, PKG_VERSION)) {
|
|
74
|
-
// npm is up to date — still ensure Python pkg is current (silent)
|
|
75
|
-
if (py) {
|
|
76
|
-
spawn(py, ['-m', 'pip', 'install', '-q', '--upgrade', 'xithead[proxy]'], {
|
|
77
|
-
detached: true, stdio: 'ignore',
|
|
78
|
-
}).unref();
|
|
79
|
-
}
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// New npm version available — update in background
|
|
84
|
-
process.stdout.write(
|
|
85
|
-
`\x1b[33m [xithead] Update v${PKG_VERSION} → v${latest} wird installiert...\x1b[0m\n`
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
89
|
-
const up = spawn(
|
|
90
|
-
npmCmd,
|
|
91
|
-
['install', '-g', '--allow-scripts=xithead', `xithead@${latest}`],
|
|
92
|
-
{ detached: true, stdio: 'ignore' }
|
|
93
|
-
);
|
|
94
|
-
up.unref();
|
|
95
|
-
|
|
96
|
-
// Also update Python package
|
|
97
|
-
if (py) {
|
|
98
|
-
spawn(py, ['-m', 'pip', 'install', '-q', '--upgrade', 'xithead[proxy]'], {
|
|
99
|
-
detached: true, stdio: 'ignore',
|
|
100
|
-
}).unref();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
process.stdout.write(
|
|
104
|
-
`\x1b[32m [xithead] Update läuft im Hintergrund — beim nächsten Start aktiv.\x1b[0m\n`
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
109
|
-
|
|
110
|
-
function findPython() {
|
|
111
|
-
for (const cmd of ['python', 'python3', 'py']) {
|
|
112
|
-
const r = spawnSync(cmd, ['--version'], { stdio: 'pipe' });
|
|
113
|
-
if (r.status === 0) return cmd;
|
|
114
|
-
}
|
|
115
|
-
return null;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function proxyAlive() {
|
|
119
|
-
return new Promise(resolve => {
|
|
120
|
-
const tryEndpoint = (p, cb) => {
|
|
121
|
-
const req = http.get(`${PROXY_URL}${p}`, { timeout: 3000 }, res => {
|
|
122
|
-
cb(res.statusCode === 200);
|
|
123
|
-
});
|
|
124
|
-
req.on('error', () => cb(false));
|
|
125
|
-
req.on('timeout', () => { req.destroy(); cb(false); });
|
|
126
|
-
};
|
|
127
|
-
tryEndpoint('/health', ok => {
|
|
128
|
-
if (ok) return resolve(true);
|
|
129
|
-
tryEndpoint('/api/stats', resolve);
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function startProxy(py) {
|
|
135
|
-
const child = spawn(py, ['-m', 'xithead.cli.main', 'proxy', '--port', String(PROXY_PORT)], {
|
|
136
|
-
detached: true,
|
|
137
|
-
stdio: 'ignore',
|
|
138
|
-
windowsHide: true,
|
|
139
|
-
});
|
|
140
|
-
child.unref();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function waitForProxy(timeoutMs = 20000) {
|
|
144
|
-
return new Promise(async resolve => {
|
|
145
|
-
const deadline = Date.now() + timeoutMs;
|
|
146
|
-
while (Date.now() < deadline) {
|
|
147
|
-
if (await proxyAlive()) return resolve(true);
|
|
148
|
-
await new Promise(r => setTimeout(r, 400));
|
|
149
|
-
}
|
|
150
|
-
resolve(false);
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function hasPythonXithead(py) {
|
|
155
|
-
const r = spawnSync(py, ['-c', 'import xithead'], { stdio: 'pipe' });
|
|
156
|
-
return r.status === 0;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function hasProxyDeps(py) {
|
|
160
|
-
const r = spawnSync(py, ['-c', 'import fastapi, uvicorn, httpx'], { stdio: 'pipe' });
|
|
161
|
-
return r.status === 0;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function installProxyDeps(py) {
|
|
165
|
-
process.stdout.write('\x1b[33m [xithead] Installiere Proxy-Abhängigkeiten...\x1b[0m\n');
|
|
166
|
-
const r = spawnSync(py, ['-m', 'pip', 'install', '-q', 'xithead[proxy]'], { stdio: 'inherit' });
|
|
167
|
-
return r.status === 0;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// ── Commands ──────────────────────────────────────────────────────────────────
|
|
171
|
-
|
|
172
|
-
function showBanner(proxyOk) {
|
|
173
|
-
const R = '\x1b[0m', B = '\x1b[1m';
|
|
174
|
-
const cyan = '\x1b[96m', cyanD = '\x1b[36m';
|
|
175
|
-
const green = '\x1b[92m', greenD = '\x1b[32m';
|
|
176
|
-
const yellow = '\x1b[93m';
|
|
177
|
-
const gray = '\x1b[90m';
|
|
178
|
-
const magenta = '\x1b[95m';
|
|
179
|
-
const white = '\x1b[97m';
|
|
180
|
-
|
|
181
|
-
const proxyLine = proxyOk
|
|
182
|
-
? `${green} ┃ ${greenD}Proxy ${green}${B}aktiv ✓${R}${green} http://localhost:${PROXY_PORT} ┃${R}`
|
|
183
|
-
: `${yellow} ┃ ${yellow}Proxy direkte Anthropic API (kein Proxy) ┃${R}`;
|
|
184
|
-
|
|
185
|
-
console.log('');
|
|
186
|
-
console.log(`${cyanD} ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓${R}`);
|
|
187
|
-
console.log(`${cyanD} ┃${R} ${B}${white}⚡ xithead${R} ${cyan}v${PKG_VERSION}${R} ${gray}by Xtream-ITSolutions UG${R} ${cyanD}┃${R}`);
|
|
188
|
-
console.log(`${cyanD} ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫${R}`);
|
|
189
|
-
console.log(`${cyanD} ┃${R} ${gray}Modus ${R}${magenta}${B}Ultra-Aggressiv (50–95% Einsparung)${R} ${cyanD}┃${R}`);
|
|
190
|
-
console.log(proxyLine);
|
|
191
|
-
console.log(`${cyanD} ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫${R}`);
|
|
192
|
-
if (proxyOk) {
|
|
193
|
-
console.log(`${cyanD} ┃${R} ${gray}Dashboard ${cyan}http://localhost:${PROXY_PORT}/dashboard${R} ${cyanD}┃${R}`);
|
|
194
|
-
}
|
|
195
|
-
console.log(`${cyanD} ┃${R} ${gray}Discord ${R}\x1b[34mdiscord.gg/xtream-itsolutions${R} ${cyanD}┃${R}`);
|
|
196
|
-
console.log(`${cyanD} ┃${R} ${gray}Web ${R}\x1b[34mxtream-itsolutions.com${R} ${cyanD}┃${R}`);
|
|
197
|
-
console.log(`${cyanD} ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛${R}`);
|
|
198
|
-
console.log('');
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function showHelp() {
|
|
202
|
-
const c = { cyan: '\x1b[36m', green: '\x1b[32m', gray: '\x1b[90m', reset: '\x1b[0m' };
|
|
203
|
-
console.log('');
|
|
204
|
-
console.log(`${c.cyan} xithead — Claude Context Optimizer${c.reset}`);
|
|
205
|
-
console.log(`${c.gray} by Xtream-ITSolutions UG${c.reset}`);
|
|
206
|
-
console.log('');
|
|
207
|
-
console.log(`${c.green} xithead${c.reset} Claude starten (Proxy auto, 50-95% Einsparung)`);
|
|
208
|
-
console.log(`${c.green} xithead proxy${c.reset} Proxy + Dashboard starten`);
|
|
209
|
-
console.log(`${c.green} xithead stats${c.reset} Token-Einsparungen anzeigen`);
|
|
210
|
-
console.log(`${c.green} xithead update${c.reset} Manuelles Update (npm + Python)`);
|
|
211
|
-
console.log(`${c.green} xithead help${c.reset} Diese Hilfe`);
|
|
212
|
-
console.log('');
|
|
213
|
-
console.log(`${c.gray} Dashboard: http://localhost:${PROXY_PORT}/dashboard${c.reset}`);
|
|
214
|
-
console.log(`${c.gray} Discord: discord.gg/xtream-itsolutions${c.reset}`);
|
|
215
|
-
console.log('');
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
async function showStats() {
|
|
219
|
-
return new Promise(resolve => {
|
|
220
|
-
const req = http.get(`${PROXY_URL}/api/stats`, { timeout: 3000 }, res => {
|
|
221
|
-
let body = '';
|
|
222
|
-
res.on('data', d => body += d);
|
|
223
|
-
res.on('end', () => {
|
|
224
|
-
try {
|
|
225
|
-
const s = JSON.parse(body);
|
|
226
|
-
const c = { cyan: '\x1b[36m', green: '\x1b[32m', gray: '\x1b[90m', reset: '\x1b[0m' };
|
|
227
|
-
console.log('');
|
|
228
|
-
console.log(`${c.cyan} ┌─────────────────────────────────────┐${c.reset}`);
|
|
229
|
-
console.log(`${c.cyan} │ xithead Statistiken │${c.reset}`);
|
|
230
|
-
console.log(`${c.cyan} ├─────────────────────────────────────┤${c.reset}`);
|
|
231
|
-
console.log(`${c.reset} │ Requests: ${String(s.session.requests).padEnd(19)}│${c.reset}`);
|
|
232
|
-
console.log(`${c.green} │ Tokens gespart: ${String(s.session.tokens_saved).padEnd(19)}│${c.reset}`);
|
|
233
|
-
console.log(`${c.green} │ Kosten gespart: $${String((s.session.cost_saved_usd||0).toFixed(4)).padEnd(18)}│${c.reset}`);
|
|
234
|
-
console.log(`${c.cyan} │ Cache-Hit-Rate: ${String(((s.session.cache_hit_rate||0)*100).toFixed(1)+'%').padEnd(19)}│${c.reset}`);
|
|
235
|
-
console.log(`${c.gray} │ Modell: ${String(s.model||'').padEnd(19)}│${c.reset}`);
|
|
236
|
-
console.log(`${c.cyan} └─────────────────────────────────────┘${c.reset}`);
|
|
237
|
-
} catch { console.log(' [xithead] Konnte Stats nicht parsen.'); }
|
|
238
|
-
resolve();
|
|
239
|
-
});
|
|
240
|
-
});
|
|
241
|
-
req.on('error', () => {
|
|
242
|
-
console.log('\x1b[33m [xithead] Proxy nicht erreichbar — starte mit: xithead\x1b[0m');
|
|
243
|
-
resolve();
|
|
244
|
-
});
|
|
245
|
-
req.on('timeout', () => { req.destroy(); resolve(); });
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
async function runManualUpdate(py) {
|
|
250
|
-
const c = { cyan: '\x1b[36m', green: '\x1b[32m', yellow: '\x1b[33m', reset: '\x1b[0m' };
|
|
251
|
-
console.log('');
|
|
252
|
-
console.log(`${c.cyan} [xithead] Prüfe auf Updates...${c.reset}`);
|
|
253
|
-
|
|
254
|
-
const latest = await fetchLatestNpmVersion();
|
|
255
|
-
if (latest && versionGt(latest, PKG_VERSION)) {
|
|
256
|
-
console.log(`${c.yellow} npm: v${PKG_VERSION} → v${latest}${c.reset}`);
|
|
257
|
-
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
258
|
-
spawnSync(npmCmd, ['install', '-g', '--allow-scripts=xithead', `xithead@${latest}`], { stdio: 'inherit' });
|
|
259
|
-
console.log(`${c.green} npm xithead auf v${latest} aktualisiert ✓${c.reset}`);
|
|
260
|
-
} else {
|
|
261
|
-
console.log(`${c.green} npm xithead v${PKG_VERSION} — bereits aktuell ✓${c.reset}`);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (py) {
|
|
265
|
-
console.log(`${c.cyan} Python xithead[proxy] wird aktualisiert...${c.reset}`);
|
|
266
|
-
spawnSync(py, ['-m', 'pip', 'install', '-q', '--upgrade', 'xithead[proxy]'], { stdio: 'inherit' });
|
|
267
|
-
console.log(`${c.green} Python-Paket aktualisiert ✓${c.reset}`);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
markUpdateChecked();
|
|
271
|
-
console.log('');
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// ── Main ──────────────────────────────────────────────────────────────────────
|
|
275
|
-
|
|
276
|
-
async function main() {
|
|
277
|
-
const cmd = args[0];
|
|
278
|
-
|
|
279
|
-
if (cmd === 'help' || cmd === '--help' || cmd === '-h') {
|
|
280
|
-
showHelp();
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
if (cmd === 'stats') {
|
|
285
|
-
await showStats();
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const py = findPython();
|
|
290
|
-
|
|
291
|
-
if (cmd === 'update') {
|
|
292
|
-
await runManualUpdate(py);
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
if (cmd === 'proxy') {
|
|
297
|
-
if (!py || !hasPythonXithead(py)) {
|
|
298
|
-
console.error('\x1b[31m [xithead] Python-Paket nicht installiert.\x1b[0m');
|
|
299
|
-
console.error(' Installieren: pip install "xithead[proxy]"');
|
|
300
|
-
process.exit(1);
|
|
301
|
-
}
|
|
302
|
-
console.log(`\x1b[36m [xithead] Proxy auf Port ${PROXY_PORT} | Ctrl+C zum Stoppen\x1b[0m`);
|
|
303
|
-
console.log(`\x1b[32m [xithead] Dashboard: ${PROXY_URL}/dashboard\x1b[0m`);
|
|
304
|
-
const child = spawnSync(py, ['-m', 'xithead.cli.main', 'proxy', '--port', String(PROXY_PORT), '--verbose'], { stdio: 'inherit' });
|
|
305
|
-
process.exit(child.status || 0);
|
|
306
|
-
return;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// Default: launch Claude with proxy
|
|
310
|
-
// Kick off background auto-update check (non-blocking)
|
|
311
|
-
const updatePromise = autoUpdate(py);
|
|
312
|
-
|
|
313
|
-
let proxyOk = await proxyAlive();
|
|
314
|
-
|
|
315
|
-
if (!proxyOk && py && hasPythonXithead(py)) {
|
|
316
|
-
if (!hasProxyDeps(py)) {
|
|
317
|
-
const ok = installProxyDeps(py);
|
|
318
|
-
if (!ok) {
|
|
319
|
-
console.log('\x1b[31m [xithead] Proxy-Abhängigkeiten konnten nicht installiert werden.\x1b[0m');
|
|
320
|
-
console.log(' Manuell: pip install "xithead[proxy]"');
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
process.stdout.write('\x1b[33m [xithead] Proxy startet...\x1b[0m');
|
|
324
|
-
startProxy(py);
|
|
325
|
-
proxyOk = await waitForProxy(20000);
|
|
326
|
-
if (!proxyOk) {
|
|
327
|
-
proxyOk = await proxyAlive();
|
|
328
|
-
}
|
|
329
|
-
process.stdout.write(proxyOk ? ' \x1b[32mbereit!\x1b[0m\n' : ' \x1b[33mTimeout — direkte API\x1b[0m\n');
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
showBanner(proxyOk);
|
|
333
|
-
|
|
334
|
-
// Wait for update check to finish (it's fast, already running)
|
|
335
|
-
await updatePromise;
|
|
336
|
-
|
|
337
|
-
const env = {
|
|
338
|
-
...process.env,
|
|
339
|
-
CLAUDE_CODE_MAX_OUTPUT_TOKENS: '16000',
|
|
340
|
-
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1',
|
|
341
|
-
XITHEAD_ENABLE_CACHE_CONTROL: '1',
|
|
342
|
-
XITHEAD_ENABLE_DEDUP: '1',
|
|
343
|
-
XITHEAD_ENABLE_CONTEXT_TRIM: '1',
|
|
344
|
-
HEADROOM_STRENGTH: 'aggressive',
|
|
345
|
-
HEADROOM_COMPRESSION_RATIO_TARGET: '0.3',
|
|
346
|
-
};
|
|
347
|
-
// Claude Code haengt /v1/... selbst an — Basis-URL daher OHNE /v1 (wie upstream headroom)
|
|
348
|
-
if (proxyOk) {
|
|
349
|
-
env.ANTHROPIC_BASE_URL = PROXY_URL;
|
|
350
|
-
} else {
|
|
351
|
-
delete env.ANTHROPIC_BASE_URL; // direkte Anthropic-API, Claude nutzt seinen Default
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
const claudeResult = spawnSync(process.platform === 'win32' ? 'where' : 'which', ['claude'], { stdio: 'pipe' });
|
|
355
|
-
if (claudeResult.status !== 0) {
|
|
356
|
-
console.error('\x1b[31m [xithead] Claude Code nicht gefunden. Installieren:\x1b[0m');
|
|
357
|
-
console.error(' npm install -g @anthropic-ai/claude-code');
|
|
358
|
-
process.exit(1);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
const claudeArgs = args.length > 0 ? args : [];
|
|
362
|
-
const claudeProc = spawnSync('claude', claudeArgs, { stdio: 'inherit', env });
|
|
363
|
-
process.exit(claudeProc.status || 0);
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
main().catch(e => { console.error(e); process.exit(1); });
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* xithead CLI — Claude Context Optimizer
|
|
4
|
+
* by Xtream-ITSolutions UG | discord.gg/xtream-itsolutions
|
|
5
|
+
*/
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
const { spawnSync, spawn, execSync } = require('child_process');
|
|
9
|
+
const http = require('http');
|
|
10
|
+
const https = require('https');
|
|
11
|
+
const path = require('path');
|
|
12
|
+
const fs = require('fs');
|
|
13
|
+
|
|
14
|
+
const PKG_PATH = path.join(__dirname, '../../package.json');
|
|
15
|
+
const PKG_VERSION = (() => {
|
|
16
|
+
try { return require(PKG_PATH).version; } catch { return '1.2.0'; }
|
|
17
|
+
})();
|
|
18
|
+
|
|
19
|
+
const args = process.argv.slice(2);
|
|
20
|
+
const PROXY_PORT = 8787;
|
|
21
|
+
const PROXY_URL = `http://localhost:${PROXY_PORT}`;
|
|
22
|
+
|
|
23
|
+
// ── Update logic ──────────────────────────────────────────────────────────────
|
|
24
|
+
|
|
25
|
+
const UPDATE_CACHE = path.join(
|
|
26
|
+
process.env.APPDATA || process.env.HOME || require('os').homedir(),
|
|
27
|
+
'.xithead', 'last_update_check'
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
function shouldCheckUpdate() {
|
|
31
|
+
try {
|
|
32
|
+
const ts = parseInt(fs.readFileSync(UPDATE_CACHE, 'utf8').trim(), 10);
|
|
33
|
+
return Date.now() - ts > 6 * 60 * 60 * 1000; // once per 6h
|
|
34
|
+
} catch { return true; }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function markUpdateChecked() {
|
|
38
|
+
try {
|
|
39
|
+
fs.mkdirSync(path.dirname(UPDATE_CACHE), { recursive: true });
|
|
40
|
+
fs.writeFileSync(UPDATE_CACHE, String(Date.now()));
|
|
41
|
+
} catch {}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function fetchLatestNpmVersion() {
|
|
45
|
+
return new Promise(resolve => {
|
|
46
|
+
const req = https.get('https://registry.npmjs.org/xithead/latest', { timeout: 5000 }, res => {
|
|
47
|
+
let body = '';
|
|
48
|
+
res.on('data', d => body += d);
|
|
49
|
+
res.on('end', () => {
|
|
50
|
+
try { resolve(JSON.parse(body).version || null); } catch { resolve(null); }
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
req.on('error', () => resolve(null));
|
|
54
|
+
req.on('timeout', () => { req.destroy(); resolve(null); });
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function versionGt(a, b) {
|
|
59
|
+
const pa = a.split('.').map(Number);
|
|
60
|
+
const pb = b.split('.').map(Number);
|
|
61
|
+
for (let i = 0; i < 3; i++) {
|
|
62
|
+
if ((pa[i]||0) > (pb[i]||0)) return true;
|
|
63
|
+
if ((pa[i]||0) < (pb[i]||0)) return false;
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function autoUpdate(py) {
|
|
69
|
+
if (!shouldCheckUpdate()) return;
|
|
70
|
+
markUpdateChecked();
|
|
71
|
+
|
|
72
|
+
const latest = await fetchLatestNpmVersion();
|
|
73
|
+
if (!latest || !versionGt(latest, PKG_VERSION)) {
|
|
74
|
+
// npm is up to date — still ensure Python pkg is current (silent)
|
|
75
|
+
if (py) {
|
|
76
|
+
spawn(py, ['-m', 'pip', 'install', '-q', '--upgrade', 'xithead[proxy]'], {
|
|
77
|
+
detached: true, stdio: 'ignore',
|
|
78
|
+
}).unref();
|
|
79
|
+
}
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// New npm version available — update in background
|
|
84
|
+
process.stdout.write(
|
|
85
|
+
`\x1b[33m [xithead] Update v${PKG_VERSION} → v${latest} wird installiert...\x1b[0m\n`
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
89
|
+
const up = spawn(
|
|
90
|
+
npmCmd,
|
|
91
|
+
['install', '-g', '--allow-scripts=xithead', `xithead@${latest}`],
|
|
92
|
+
{ detached: true, stdio: 'ignore' }
|
|
93
|
+
);
|
|
94
|
+
up.unref();
|
|
95
|
+
|
|
96
|
+
// Also update Python package
|
|
97
|
+
if (py) {
|
|
98
|
+
spawn(py, ['-m', 'pip', 'install', '-q', '--upgrade', 'xithead[proxy]'], {
|
|
99
|
+
detached: true, stdio: 'ignore',
|
|
100
|
+
}).unref();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
process.stdout.write(
|
|
104
|
+
`\x1b[32m [xithead] Update läuft im Hintergrund — beim nächsten Start aktiv.\x1b[0m\n`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
109
|
+
|
|
110
|
+
function findPython() {
|
|
111
|
+
for (const cmd of ['python', 'python3', 'py']) {
|
|
112
|
+
const r = spawnSync(cmd, ['--version'], { stdio: 'pipe' });
|
|
113
|
+
if (r.status === 0) return cmd;
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function proxyAlive() {
|
|
119
|
+
return new Promise(resolve => {
|
|
120
|
+
const tryEndpoint = (p, cb) => {
|
|
121
|
+
const req = http.get(`${PROXY_URL}${p}`, { timeout: 3000 }, res => {
|
|
122
|
+
cb(res.statusCode === 200);
|
|
123
|
+
});
|
|
124
|
+
req.on('error', () => cb(false));
|
|
125
|
+
req.on('timeout', () => { req.destroy(); cb(false); });
|
|
126
|
+
};
|
|
127
|
+
tryEndpoint('/health', ok => {
|
|
128
|
+
if (ok) return resolve(true);
|
|
129
|
+
tryEndpoint('/api/stats', resolve);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function startProxy(py) {
|
|
135
|
+
const child = spawn(py, ['-m', 'xithead.cli.main', 'proxy', '--port', String(PROXY_PORT)], {
|
|
136
|
+
detached: true,
|
|
137
|
+
stdio: 'ignore',
|
|
138
|
+
windowsHide: true,
|
|
139
|
+
});
|
|
140
|
+
child.unref();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function waitForProxy(timeoutMs = 20000) {
|
|
144
|
+
return new Promise(async resolve => {
|
|
145
|
+
const deadline = Date.now() + timeoutMs;
|
|
146
|
+
while (Date.now() < deadline) {
|
|
147
|
+
if (await proxyAlive()) return resolve(true);
|
|
148
|
+
await new Promise(r => setTimeout(r, 400));
|
|
149
|
+
}
|
|
150
|
+
resolve(false);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function hasPythonXithead(py) {
|
|
155
|
+
const r = spawnSync(py, ['-c', 'import xithead'], { stdio: 'pipe' });
|
|
156
|
+
return r.status === 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function hasProxyDeps(py) {
|
|
160
|
+
const r = spawnSync(py, ['-c', 'import fastapi, uvicorn, httpx'], { stdio: 'pipe' });
|
|
161
|
+
return r.status === 0;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function installProxyDeps(py) {
|
|
165
|
+
process.stdout.write('\x1b[33m [xithead] Installiere Proxy-Abhängigkeiten...\x1b[0m\n');
|
|
166
|
+
const r = spawnSync(py, ['-m', 'pip', 'install', '-q', 'xithead[proxy]'], { stdio: 'inherit' });
|
|
167
|
+
return r.status === 0;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ── Commands ──────────────────────────────────────────────────────────────────
|
|
171
|
+
|
|
172
|
+
function showBanner(proxyOk) {
|
|
173
|
+
const R = '\x1b[0m', B = '\x1b[1m';
|
|
174
|
+
const cyan = '\x1b[96m', cyanD = '\x1b[36m';
|
|
175
|
+
const green = '\x1b[92m', greenD = '\x1b[32m';
|
|
176
|
+
const yellow = '\x1b[93m';
|
|
177
|
+
const gray = '\x1b[90m';
|
|
178
|
+
const magenta = '\x1b[95m';
|
|
179
|
+
const white = '\x1b[97m';
|
|
180
|
+
|
|
181
|
+
const proxyLine = proxyOk
|
|
182
|
+
? `${green} ┃ ${greenD}Proxy ${green}${B}aktiv ✓${R}${green} http://localhost:${PROXY_PORT} ┃${R}`
|
|
183
|
+
: `${yellow} ┃ ${yellow}Proxy direkte Anthropic API (kein Proxy) ┃${R}`;
|
|
184
|
+
|
|
185
|
+
console.log('');
|
|
186
|
+
console.log(`${cyanD} ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓${R}`);
|
|
187
|
+
console.log(`${cyanD} ┃${R} ${B}${white}⚡ xithead${R} ${cyan}v${PKG_VERSION}${R} ${gray}by Xtream-ITSolutions UG${R} ${cyanD}┃${R}`);
|
|
188
|
+
console.log(`${cyanD} ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫${R}`);
|
|
189
|
+
console.log(`${cyanD} ┃${R} ${gray}Modus ${R}${magenta}${B}Ultra-Aggressiv (50–95% Einsparung)${R} ${cyanD}┃${R}`);
|
|
190
|
+
console.log(proxyLine);
|
|
191
|
+
console.log(`${cyanD} ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫${R}`);
|
|
192
|
+
if (proxyOk) {
|
|
193
|
+
console.log(`${cyanD} ┃${R} ${gray}Dashboard ${cyan}http://localhost:${PROXY_PORT}/dashboard${R} ${cyanD}┃${R}`);
|
|
194
|
+
}
|
|
195
|
+
console.log(`${cyanD} ┃${R} ${gray}Discord ${R}\x1b[34mdiscord.gg/xtream-itsolutions${R} ${cyanD}┃${R}`);
|
|
196
|
+
console.log(`${cyanD} ┃${R} ${gray}Web ${R}\x1b[34mxtream-itsolutions.com${R} ${cyanD}┃${R}`);
|
|
197
|
+
console.log(`${cyanD} ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛${R}`);
|
|
198
|
+
console.log('');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function showHelp() {
|
|
202
|
+
const c = { cyan: '\x1b[36m', green: '\x1b[32m', gray: '\x1b[90m', reset: '\x1b[0m' };
|
|
203
|
+
console.log('');
|
|
204
|
+
console.log(`${c.cyan} xithead — Claude Context Optimizer${c.reset}`);
|
|
205
|
+
console.log(`${c.gray} by Xtream-ITSolutions UG${c.reset}`);
|
|
206
|
+
console.log('');
|
|
207
|
+
console.log(`${c.green} xithead${c.reset} Claude starten (Proxy auto, 50-95% Einsparung)`);
|
|
208
|
+
console.log(`${c.green} xithead proxy${c.reset} Proxy + Dashboard starten`);
|
|
209
|
+
console.log(`${c.green} xithead stats${c.reset} Token-Einsparungen anzeigen`);
|
|
210
|
+
console.log(`${c.green} xithead update${c.reset} Manuelles Update (npm + Python)`);
|
|
211
|
+
console.log(`${c.green} xithead help${c.reset} Diese Hilfe`);
|
|
212
|
+
console.log('');
|
|
213
|
+
console.log(`${c.gray} Dashboard: http://localhost:${PROXY_PORT}/dashboard${c.reset}`);
|
|
214
|
+
console.log(`${c.gray} Discord: discord.gg/xtream-itsolutions${c.reset}`);
|
|
215
|
+
console.log('');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async function showStats() {
|
|
219
|
+
return new Promise(resolve => {
|
|
220
|
+
const req = http.get(`${PROXY_URL}/api/stats`, { timeout: 3000 }, res => {
|
|
221
|
+
let body = '';
|
|
222
|
+
res.on('data', d => body += d);
|
|
223
|
+
res.on('end', () => {
|
|
224
|
+
try {
|
|
225
|
+
const s = JSON.parse(body);
|
|
226
|
+
const c = { cyan: '\x1b[36m', green: '\x1b[32m', gray: '\x1b[90m', reset: '\x1b[0m' };
|
|
227
|
+
console.log('');
|
|
228
|
+
console.log(`${c.cyan} ┌─────────────────────────────────────┐${c.reset}`);
|
|
229
|
+
console.log(`${c.cyan} │ xithead Statistiken │${c.reset}`);
|
|
230
|
+
console.log(`${c.cyan} ├─────────────────────────────────────┤${c.reset}`);
|
|
231
|
+
console.log(`${c.reset} │ Requests: ${String(s.session.requests).padEnd(19)}│${c.reset}`);
|
|
232
|
+
console.log(`${c.green} │ Tokens gespart: ${String(s.session.tokens_saved).padEnd(19)}│${c.reset}`);
|
|
233
|
+
console.log(`${c.green} │ Kosten gespart: $${String((s.session.cost_saved_usd||0).toFixed(4)).padEnd(18)}│${c.reset}`);
|
|
234
|
+
console.log(`${c.cyan} │ Cache-Hit-Rate: ${String(((s.session.cache_hit_rate||0)*100).toFixed(1)+'%').padEnd(19)}│${c.reset}`);
|
|
235
|
+
console.log(`${c.gray} │ Modell: ${String(s.model||'').padEnd(19)}│${c.reset}`);
|
|
236
|
+
console.log(`${c.cyan} └─────────────────────────────────────┘${c.reset}`);
|
|
237
|
+
} catch { console.log(' [xithead] Konnte Stats nicht parsen.'); }
|
|
238
|
+
resolve();
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
req.on('error', () => {
|
|
242
|
+
console.log('\x1b[33m [xithead] Proxy nicht erreichbar — starte mit: xithead\x1b[0m');
|
|
243
|
+
resolve();
|
|
244
|
+
});
|
|
245
|
+
req.on('timeout', () => { req.destroy(); resolve(); });
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async function runManualUpdate(py) {
|
|
250
|
+
const c = { cyan: '\x1b[36m', green: '\x1b[32m', yellow: '\x1b[33m', reset: '\x1b[0m' };
|
|
251
|
+
console.log('');
|
|
252
|
+
console.log(`${c.cyan} [xithead] Prüfe auf Updates...${c.reset}`);
|
|
253
|
+
|
|
254
|
+
const latest = await fetchLatestNpmVersion();
|
|
255
|
+
if (latest && versionGt(latest, PKG_VERSION)) {
|
|
256
|
+
console.log(`${c.yellow} npm: v${PKG_VERSION} → v${latest}${c.reset}`);
|
|
257
|
+
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
258
|
+
spawnSync(npmCmd, ['install', '-g', '--allow-scripts=xithead', `xithead@${latest}`], { stdio: 'inherit' });
|
|
259
|
+
console.log(`${c.green} npm xithead auf v${latest} aktualisiert ✓${c.reset}`);
|
|
260
|
+
} else {
|
|
261
|
+
console.log(`${c.green} npm xithead v${PKG_VERSION} — bereits aktuell ✓${c.reset}`);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (py) {
|
|
265
|
+
console.log(`${c.cyan} Python xithead[proxy] wird aktualisiert...${c.reset}`);
|
|
266
|
+
spawnSync(py, ['-m', 'pip', 'install', '-q', '--upgrade', 'xithead[proxy]'], { stdio: 'inherit' });
|
|
267
|
+
console.log(`${c.green} Python-Paket aktualisiert ✓${c.reset}`);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
markUpdateChecked();
|
|
271
|
+
console.log('');
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// ── Main ──────────────────────────────────────────────────────────────────────
|
|
275
|
+
|
|
276
|
+
async function main() {
|
|
277
|
+
const cmd = args[0];
|
|
278
|
+
|
|
279
|
+
if (cmd === 'help' || cmd === '--help' || cmd === '-h') {
|
|
280
|
+
showHelp();
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (cmd === 'stats') {
|
|
285
|
+
await showStats();
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const py = findPython();
|
|
290
|
+
|
|
291
|
+
if (cmd === 'update') {
|
|
292
|
+
await runManualUpdate(py);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (cmd === 'proxy') {
|
|
297
|
+
if (!py || !hasPythonXithead(py)) {
|
|
298
|
+
console.error('\x1b[31m [xithead] Python-Paket nicht installiert.\x1b[0m');
|
|
299
|
+
console.error(' Installieren: pip install "xithead[proxy]"');
|
|
300
|
+
process.exit(1);
|
|
301
|
+
}
|
|
302
|
+
console.log(`\x1b[36m [xithead] Proxy auf Port ${PROXY_PORT} | Ctrl+C zum Stoppen\x1b[0m`);
|
|
303
|
+
console.log(`\x1b[32m [xithead] Dashboard: ${PROXY_URL}/dashboard\x1b[0m`);
|
|
304
|
+
const child = spawnSync(py, ['-m', 'xithead.cli.main', 'proxy', '--port', String(PROXY_PORT), '--verbose'], { stdio: 'inherit' });
|
|
305
|
+
process.exit(child.status || 0);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Default: launch Claude with proxy
|
|
310
|
+
// Kick off background auto-update check (non-blocking)
|
|
311
|
+
const updatePromise = autoUpdate(py);
|
|
312
|
+
|
|
313
|
+
let proxyOk = await proxyAlive();
|
|
314
|
+
|
|
315
|
+
if (!proxyOk && py && hasPythonXithead(py)) {
|
|
316
|
+
if (!hasProxyDeps(py)) {
|
|
317
|
+
const ok = installProxyDeps(py);
|
|
318
|
+
if (!ok) {
|
|
319
|
+
console.log('\x1b[31m [xithead] Proxy-Abhängigkeiten konnten nicht installiert werden.\x1b[0m');
|
|
320
|
+
console.log(' Manuell: pip install "xithead[proxy]"');
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
process.stdout.write('\x1b[33m [xithead] Proxy startet...\x1b[0m');
|
|
324
|
+
startProxy(py);
|
|
325
|
+
proxyOk = await waitForProxy(20000);
|
|
326
|
+
if (!proxyOk) {
|
|
327
|
+
proxyOk = await proxyAlive();
|
|
328
|
+
}
|
|
329
|
+
process.stdout.write(proxyOk ? ' \x1b[32mbereit!\x1b[0m\n' : ' \x1b[33mTimeout — direkte API\x1b[0m\n');
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
showBanner(proxyOk);
|
|
333
|
+
|
|
334
|
+
// Wait for update check to finish (it's fast, already running)
|
|
335
|
+
await updatePromise;
|
|
336
|
+
|
|
337
|
+
const env = {
|
|
338
|
+
...process.env,
|
|
339
|
+
CLAUDE_CODE_MAX_OUTPUT_TOKENS: '16000',
|
|
340
|
+
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1',
|
|
341
|
+
XITHEAD_ENABLE_CACHE_CONTROL: '1',
|
|
342
|
+
XITHEAD_ENABLE_DEDUP: '1',
|
|
343
|
+
XITHEAD_ENABLE_CONTEXT_TRIM: '1',
|
|
344
|
+
HEADROOM_STRENGTH: 'aggressive',
|
|
345
|
+
HEADROOM_COMPRESSION_RATIO_TARGET: '0.3',
|
|
346
|
+
};
|
|
347
|
+
// Claude Code haengt /v1/... selbst an — Basis-URL daher OHNE /v1 (wie upstream headroom)
|
|
348
|
+
if (proxyOk) {
|
|
349
|
+
env.ANTHROPIC_BASE_URL = PROXY_URL;
|
|
350
|
+
} else {
|
|
351
|
+
delete env.ANTHROPIC_BASE_URL; // direkte Anthropic-API, Claude nutzt seinen Default
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const claudeResult = spawnSync(process.platform === 'win32' ? 'where' : 'which', ['claude'], { stdio: 'pipe' });
|
|
355
|
+
if (claudeResult.status !== 0) {
|
|
356
|
+
console.error('\x1b[31m [xithead] Claude Code nicht gefunden. Installieren:\x1b[0m');
|
|
357
|
+
console.error(' npm install -g @anthropic-ai/claude-code');
|
|
358
|
+
process.exit(1);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const claudeArgs = args.length > 0 ? args : [];
|
|
362
|
+
const claudeProc = spawnSync('claude', claudeArgs, { stdio: 'inherit', env });
|
|
363
|
+
process.exit(claudeProc.status || 0);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
main().catch(e => { console.error(e); process.exit(1); });
|