xithead 1.1.1 → 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 +165 -51
- 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));
|
|
@@ -45,22 +132,12 @@ function proxyAlive() {
|
|
|
45
132
|
}
|
|
46
133
|
|
|
47
134
|
function startProxy(py) {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
shell: false,
|
|
55
|
-
});
|
|
56
|
-
child.unref();
|
|
57
|
-
} else {
|
|
58
|
-
const child = spawn(py, ['-m', 'xithead.cli.main', 'proxy', '--port', String(PROXY_PORT)], {
|
|
59
|
-
detached: true,
|
|
60
|
-
stdio: 'ignore',
|
|
61
|
-
});
|
|
62
|
-
child.unref();
|
|
63
|
-
}
|
|
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();
|
|
64
141
|
}
|
|
65
142
|
|
|
66
143
|
function waitForProxy(timeoutMs = 20000) {
|
|
@@ -93,28 +170,32 @@ function installProxyDeps(py) {
|
|
|
93
170
|
// ── Commands ──────────────────────────────────────────────────────────────────
|
|
94
171
|
|
|
95
172
|
function showBanner(proxyOk) {
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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}/v1 ┃${R}`
|
|
183
|
+
: `${yellow} ┃ ${yellow}Proxy direkte Anthropic API (kein Proxy) ┃${R}`;
|
|
184
|
+
|
|
104
185
|
console.log('');
|
|
105
|
-
console.log(`${
|
|
106
|
-
|
|
107
|
-
console.log(`${
|
|
108
|
-
console.log(`${
|
|
109
|
-
console.log(`${
|
|
110
|
-
console.log(
|
|
111
|
-
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}Modell ${R}${green}${B}claude-sonnet-4-6${R} ${cyanD}┃${R}`);
|
|
190
|
+
console.log(`${cyanD} ┃${R} ${gray}Modus ${R}${magenta}${B}Ultra-Aggressiv (50–95% Einsparung)${R} ${cyanD}┃${R}`);
|
|
191
|
+
console.log(proxyLine);
|
|
192
|
+
console.log(`${cyanD} ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫${R}`);
|
|
112
193
|
if (proxyOk) {
|
|
113
|
-
|
|
114
|
-
} else {
|
|
115
|
-
console.log(`${c.yellow} ║ Proxy: nicht erreichbar — direkter API ║${c.reset}`);
|
|
194
|
+
console.log(`${cyanD} ┃${R} ${gray}Dashboard ${cyan}http://localhost:${PROXY_PORT}/dashboard${R} ${cyanD}┃${R}`);
|
|
116
195
|
}
|
|
117
|
-
console.log(`${
|
|
196
|
+
console.log(`${cyanD} ┃${R} ${gray}Discord ${R}\x1b[34mdiscord.gg/xtream-itsolutions${R} ${cyanD}┃${R}`);
|
|
197
|
+
console.log(`${cyanD} ┃${R} ${gray}Web ${R}\x1b[34mxtream-itsolutions.com${R} ${cyanD}┃${R}`);
|
|
198
|
+
console.log(`${cyanD} ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛${R}`);
|
|
118
199
|
console.log('');
|
|
119
200
|
}
|
|
120
201
|
|
|
@@ -127,6 +208,7 @@ function showHelp() {
|
|
|
127
208
|
console.log(`${c.green} xithead${c.reset} Claude starten (Proxy auto, 50-95% Einsparung)`);
|
|
128
209
|
console.log(`${c.green} xithead proxy${c.reset} Proxy + Dashboard starten`);
|
|
129
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)`);
|
|
130
212
|
console.log(`${c.green} xithead help${c.reset} Diese Hilfe`);
|
|
131
213
|
console.log('');
|
|
132
214
|
console.log(`${c.gray} Dashboard: http://localhost:${PROXY_PORT}/dashboard${c.reset}`);
|
|
@@ -165,6 +247,31 @@ async function showStats() {
|
|
|
165
247
|
});
|
|
166
248
|
}
|
|
167
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
|
+
|
|
168
275
|
// ── Main ──────────────────────────────────────────────────────────────────────
|
|
169
276
|
|
|
170
277
|
async function main() {
|
|
@@ -180,11 +287,17 @@ async function main() {
|
|
|
180
287
|
return;
|
|
181
288
|
}
|
|
182
289
|
|
|
290
|
+
const py = findPython();
|
|
291
|
+
|
|
292
|
+
if (cmd === 'update') {
|
|
293
|
+
await runManualUpdate(py);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
|
|
183
297
|
if (cmd === 'proxy') {
|
|
184
|
-
const py = findPython();
|
|
185
298
|
if (!py || !hasPythonXithead(py)) {
|
|
186
299
|
console.error('\x1b[31m [xithead] Python-Paket nicht installiert.\x1b[0m');
|
|
187
|
-
console.error(' Installieren: pip install "
|
|
300
|
+
console.error(' Installieren: pip install "xithead[proxy]"');
|
|
188
301
|
process.exit(1);
|
|
189
302
|
}
|
|
190
303
|
console.log(`\x1b[36m [xithead] Proxy auf Port ${PROXY_PORT} | Ctrl+C zum Stoppen\x1b[0m`);
|
|
@@ -195,11 +308,12 @@ async function main() {
|
|
|
195
308
|
}
|
|
196
309
|
|
|
197
310
|
// Default: launch Claude with proxy
|
|
198
|
-
|
|
311
|
+
// Kick off background auto-update check (non-blocking)
|
|
312
|
+
const updatePromise = autoUpdate(py);
|
|
313
|
+
|
|
199
314
|
let proxyOk = await proxyAlive();
|
|
200
315
|
|
|
201
316
|
if (!proxyOk && py && hasPythonXithead(py)) {
|
|
202
|
-
// Auto-install proxy deps if missing
|
|
203
317
|
if (!hasProxyDeps(py)) {
|
|
204
318
|
const ok = installProxyDeps(py);
|
|
205
319
|
if (!ok) {
|
|
@@ -211,7 +325,6 @@ async function main() {
|
|
|
211
325
|
startProxy(py);
|
|
212
326
|
proxyOk = await waitForProxy(20000);
|
|
213
327
|
if (!proxyOk) {
|
|
214
|
-
// Port may already be in use from a previous run — check once more
|
|
215
328
|
proxyOk = await proxyAlive();
|
|
216
329
|
}
|
|
217
330
|
process.stdout.write(proxyOk ? ' \x1b[32mbereit!\x1b[0m\n' : ' \x1b[33mTimeout — direkte API\x1b[0m\n');
|
|
@@ -219,7 +332,9 @@ async function main() {
|
|
|
219
332
|
|
|
220
333
|
showBanner(proxyOk);
|
|
221
334
|
|
|
222
|
-
//
|
|
335
|
+
// Wait for update check to finish (it's fast, already running)
|
|
336
|
+
await updatePromise;
|
|
337
|
+
|
|
223
338
|
const env = {
|
|
224
339
|
...process.env,
|
|
225
340
|
ANTHROPIC_MODEL: 'claude-sonnet-4-6',
|
|
@@ -237,7 +352,6 @@ async function main() {
|
|
|
237
352
|
ANTHROPIC_BASE_URL: proxyOk ? `${PROXY_URL}/v1` : 'https://api.anthropic.com/v1',
|
|
238
353
|
};
|
|
239
354
|
|
|
240
|
-
// Find claude binary
|
|
241
355
|
const claudeResult = spawnSync(process.platform === 'win32' ? 'where' : 'which', ['claude'], { stdio: 'pipe' });
|
|
242
356
|
if (claudeResult.status !== 0) {
|
|
243
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
|
},
|