xithead 1.1.2 → 1.1.3
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/npm/bin/xithead.js +137 -17
- package/package.json +2 -4
package/npm/bin/xithead.js
CHANGED
|
@@ -5,19 +5,107 @@
|
|
|
5
5
|
*/
|
|
6
6
|
'use strict';
|
|
7
7
|
|
|
8
|
-
const { spawnSync, spawn } = require('child_process');
|
|
9
|
-
const http
|
|
10
|
-
const
|
|
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');
|
|
11
13
|
|
|
14
|
+
const PKG_PATH = path.join(__dirname, '../../package.json');
|
|
12
15
|
const PKG_VERSION = (() => {
|
|
13
|
-
try { return require(
|
|
16
|
+
try { return require(PKG_PATH).version; } catch { return '1.1.3'; }
|
|
14
17
|
})();
|
|
15
18
|
|
|
16
19
|
const args = process.argv.slice(2);
|
|
17
20
|
const PROXY_PORT = 8787;
|
|
18
|
-
const PROXY_URL
|
|
21
|
+
const PROXY_URL = `http://localhost:${PROXY_PORT}`;
|
|
19
22
|
|
|
20
|
-
// ──
|
|
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 ───────────────────────────────────────────────────────────────────
|
|
21
109
|
|
|
22
110
|
function findPython() {
|
|
23
111
|
for (const cmd of ['python', 'python3', 'py']) {
|
|
@@ -28,10 +116,9 @@ function findPython() {
|
|
|
28
116
|
}
|
|
29
117
|
|
|
30
118
|
function proxyAlive() {
|
|
31
|
-
// Try /health first (fast), fall back to /api/stats
|
|
32
119
|
return new Promise(resolve => {
|
|
33
|
-
const tryEndpoint = (
|
|
34
|
-
const req = http.get(`${PROXY_URL}${
|
|
120
|
+
const tryEndpoint = (p, cb) => {
|
|
121
|
+
const req = http.get(`${PROXY_URL}${p}`, { timeout: 3000 }, res => {
|
|
35
122
|
cb(res.statusCode === 200);
|
|
36
123
|
});
|
|
37
124
|
req.on('error', () => cb(false));
|
|
@@ -48,7 +135,7 @@ function startProxy(py) {
|
|
|
48
135
|
const child = spawn(py, ['-m', 'xithead.cli.main', 'proxy', '--port', String(PROXY_PORT)], {
|
|
49
136
|
detached: true,
|
|
50
137
|
stdio: 'ignore',
|
|
51
|
-
windowsHide: true,
|
|
138
|
+
windowsHide: true,
|
|
52
139
|
});
|
|
53
140
|
child.unref();
|
|
54
141
|
}
|
|
@@ -121,6 +208,7 @@ function showHelp() {
|
|
|
121
208
|
console.log(`${c.green} xithead${c.reset} Claude starten (Proxy auto, 50-95% Einsparung)`);
|
|
122
209
|
console.log(`${c.green} xithead proxy${c.reset} Proxy + Dashboard starten`);
|
|
123
210
|
console.log(`${c.green} xithead stats${c.reset} Token-Einsparungen anzeigen`);
|
|
211
|
+
console.log(`${c.green} xithead update${c.reset} Manuelles Update (npm + Python)`);
|
|
124
212
|
console.log(`${c.green} xithead help${c.reset} Diese Hilfe`);
|
|
125
213
|
console.log('');
|
|
126
214
|
console.log(`${c.gray} Dashboard: http://localhost:${PROXY_PORT}/dashboard${c.reset}`);
|
|
@@ -159,6 +247,31 @@ async function showStats() {
|
|
|
159
247
|
});
|
|
160
248
|
}
|
|
161
249
|
|
|
250
|
+
async function runManualUpdate(py) {
|
|
251
|
+
const c = { cyan: '\x1b[36m', green: '\x1b[32m', yellow: '\x1b[33m', reset: '\x1b[0m' };
|
|
252
|
+
console.log('');
|
|
253
|
+
console.log(`${c.cyan} [xithead] Prüfe auf Updates...${c.reset}`);
|
|
254
|
+
|
|
255
|
+
const latest = await fetchLatestNpmVersion();
|
|
256
|
+
if (latest && versionGt(latest, PKG_VERSION)) {
|
|
257
|
+
console.log(`${c.yellow} npm: v${PKG_VERSION} → v${latest}${c.reset}`);
|
|
258
|
+
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
259
|
+
spawnSync(npmCmd, ['install', '-g', '--allow-scripts=xithead', `xithead@${latest}`], { stdio: 'inherit' });
|
|
260
|
+
console.log(`${c.green} npm xithead auf v${latest} aktualisiert ✓${c.reset}`);
|
|
261
|
+
} else {
|
|
262
|
+
console.log(`${c.green} npm xithead v${PKG_VERSION} — bereits aktuell ✓${c.reset}`);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (py) {
|
|
266
|
+
console.log(`${c.cyan} Python xithead[proxy] wird aktualisiert...${c.reset}`);
|
|
267
|
+
spawnSync(py, ['-m', 'pip', 'install', '-q', '--upgrade', 'xithead[proxy]'], { stdio: 'inherit' });
|
|
268
|
+
console.log(`${c.green} Python-Paket aktualisiert ✓${c.reset}`);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
markUpdateChecked();
|
|
272
|
+
console.log('');
|
|
273
|
+
}
|
|
274
|
+
|
|
162
275
|
// ── Main ──────────────────────────────────────────────────────────────────────
|
|
163
276
|
|
|
164
277
|
async function main() {
|
|
@@ -174,11 +287,17 @@ async function main() {
|
|
|
174
287
|
return;
|
|
175
288
|
}
|
|
176
289
|
|
|
290
|
+
const py = findPython();
|
|
291
|
+
|
|
292
|
+
if (cmd === 'update') {
|
|
293
|
+
await runManualUpdate(py);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
|
|
177
297
|
if (cmd === 'proxy') {
|
|
178
|
-
const py = findPython();
|
|
179
298
|
if (!py || !hasPythonXithead(py)) {
|
|
180
299
|
console.error('\x1b[31m [xithead] Python-Paket nicht installiert.\x1b[0m');
|
|
181
|
-
console.error(' Installieren: pip install "
|
|
300
|
+
console.error(' Installieren: pip install "xithead[proxy]"');
|
|
182
301
|
process.exit(1);
|
|
183
302
|
}
|
|
184
303
|
console.log(`\x1b[36m [xithead] Proxy auf Port ${PROXY_PORT} | Ctrl+C zum Stoppen\x1b[0m`);
|
|
@@ -189,11 +308,12 @@ async function main() {
|
|
|
189
308
|
}
|
|
190
309
|
|
|
191
310
|
// Default: launch Claude with proxy
|
|
192
|
-
|
|
311
|
+
// Kick off background auto-update check (non-blocking)
|
|
312
|
+
const updatePromise = autoUpdate(py);
|
|
313
|
+
|
|
193
314
|
let proxyOk = await proxyAlive();
|
|
194
315
|
|
|
195
316
|
if (!proxyOk && py && hasPythonXithead(py)) {
|
|
196
|
-
// Auto-install proxy deps if missing
|
|
197
317
|
if (!hasProxyDeps(py)) {
|
|
198
318
|
const ok = installProxyDeps(py);
|
|
199
319
|
if (!ok) {
|
|
@@ -205,7 +325,6 @@ async function main() {
|
|
|
205
325
|
startProxy(py);
|
|
206
326
|
proxyOk = await waitForProxy(20000);
|
|
207
327
|
if (!proxyOk) {
|
|
208
|
-
// Port may already be in use from a previous run — check once more
|
|
209
328
|
proxyOk = await proxyAlive();
|
|
210
329
|
}
|
|
211
330
|
process.stdout.write(proxyOk ? ' \x1b[32mbereit!\x1b[0m\n' : ' \x1b[33mTimeout — direkte API\x1b[0m\n');
|
|
@@ -213,7 +332,9 @@ async function main() {
|
|
|
213
332
|
|
|
214
333
|
showBanner(proxyOk);
|
|
215
334
|
|
|
216
|
-
//
|
|
335
|
+
// Wait for update check to finish (it's fast, already running)
|
|
336
|
+
await updatePromise;
|
|
337
|
+
|
|
217
338
|
const env = {
|
|
218
339
|
...process.env,
|
|
219
340
|
ANTHROPIC_MODEL: 'claude-sonnet-4-6',
|
|
@@ -231,7 +352,6 @@ async function main() {
|
|
|
231
352
|
ANTHROPIC_BASE_URL: proxyOk ? `${PROXY_URL}/v1` : 'https://api.anthropic.com/v1',
|
|
232
353
|
};
|
|
233
354
|
|
|
234
|
-
// Find claude binary
|
|
235
355
|
const claudeResult = spawnSync(process.platform === 'win32' ? 'where' : 'which', ['claude'], { stdio: 'pipe' });
|
|
236
356
|
if (claudeResult.status !== 0) {
|
|
237
357
|
console.error('\x1b[31m [xithead] Claude Code nicht gefunden. Installieren:\x1b[0m');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xithead",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "Aggressive Claude context optimizer - Cut API costs by 50-95%. By Xtream-ITSolutions UG.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"claude",
|
|
@@ -38,9 +38,7 @@
|
|
|
38
38
|
"engines": {
|
|
39
39
|
"node": ">=18.0.0"
|
|
40
40
|
},
|
|
41
|
-
"scripts": {
|
|
42
|
-
"postinstall": "node npm/postinstall.js"
|
|
43
|
-
},
|
|
41
|
+
"scripts": {},
|
|
44
42
|
"dependencies": {
|
|
45
43
|
"node-fetch": "^3.3.2"
|
|
46
44
|
},
|