xithead 1.1.2 → 1.1.4
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 +138 -24
- 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.4'; }
|
|
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
|
}
|
|
@@ -99,7 +186,6 @@ function showBanner(proxyOk) {
|
|
|
99
186
|
console.log(`${cyanD} ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓${R}`);
|
|
100
187
|
console.log(`${cyanD} ┃${R} ${B}${white}⚡ xithead${R} ${cyan}v${PKG_VERSION}${R} ${gray}by Xtream-ITSolutions UG${R} ${cyanD}┃${R}`);
|
|
101
188
|
console.log(`${cyanD} ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫${R}`);
|
|
102
|
-
console.log(`${cyanD} ┃${R} ${gray}Modell ${R}${green}${B}claude-sonnet-4-6${R} ${cyanD}┃${R}`);
|
|
103
189
|
console.log(`${cyanD} ┃${R} ${gray}Modus ${R}${magenta}${B}Ultra-Aggressiv (50–95% Einsparung)${R} ${cyanD}┃${R}`);
|
|
104
190
|
console.log(proxyLine);
|
|
105
191
|
console.log(`${cyanD} ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫${R}`);
|
|
@@ -121,6 +207,7 @@ function showHelp() {
|
|
|
121
207
|
console.log(`${c.green} xithead${c.reset} Claude starten (Proxy auto, 50-95% Einsparung)`);
|
|
122
208
|
console.log(`${c.green} xithead proxy${c.reset} Proxy + Dashboard starten`);
|
|
123
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)`);
|
|
124
211
|
console.log(`${c.green} xithead help${c.reset} Diese Hilfe`);
|
|
125
212
|
console.log('');
|
|
126
213
|
console.log(`${c.gray} Dashboard: http://localhost:${PROXY_PORT}/dashboard${c.reset}`);
|
|
@@ -159,6 +246,31 @@ async function showStats() {
|
|
|
159
246
|
});
|
|
160
247
|
}
|
|
161
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
|
+
|
|
162
274
|
// ── Main ──────────────────────────────────────────────────────────────────────
|
|
163
275
|
|
|
164
276
|
async function main() {
|
|
@@ -174,11 +286,17 @@ async function main() {
|
|
|
174
286
|
return;
|
|
175
287
|
}
|
|
176
288
|
|
|
289
|
+
const py = findPython();
|
|
290
|
+
|
|
291
|
+
if (cmd === 'update') {
|
|
292
|
+
await runManualUpdate(py);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
|
|
177
296
|
if (cmd === 'proxy') {
|
|
178
|
-
const py = findPython();
|
|
179
297
|
if (!py || !hasPythonXithead(py)) {
|
|
180
298
|
console.error('\x1b[31m [xithead] Python-Paket nicht installiert.\x1b[0m');
|
|
181
|
-
console.error(' Installieren: pip install "
|
|
299
|
+
console.error(' Installieren: pip install "xithead[proxy]"');
|
|
182
300
|
process.exit(1);
|
|
183
301
|
}
|
|
184
302
|
console.log(`\x1b[36m [xithead] Proxy auf Port ${PROXY_PORT} | Ctrl+C zum Stoppen\x1b[0m`);
|
|
@@ -189,11 +307,12 @@ async function main() {
|
|
|
189
307
|
}
|
|
190
308
|
|
|
191
309
|
// Default: launch Claude with proxy
|
|
192
|
-
|
|
310
|
+
// Kick off background auto-update check (non-blocking)
|
|
311
|
+
const updatePromise = autoUpdate(py);
|
|
312
|
+
|
|
193
313
|
let proxyOk = await proxyAlive();
|
|
194
314
|
|
|
195
315
|
if (!proxyOk && py && hasPythonXithead(py)) {
|
|
196
|
-
// Auto-install proxy deps if missing
|
|
197
316
|
if (!hasProxyDeps(py)) {
|
|
198
317
|
const ok = installProxyDeps(py);
|
|
199
318
|
if (!ok) {
|
|
@@ -205,7 +324,6 @@ async function main() {
|
|
|
205
324
|
startProxy(py);
|
|
206
325
|
proxyOk = await waitForProxy(20000);
|
|
207
326
|
if (!proxyOk) {
|
|
208
|
-
// Port may already be in use from a previous run — check once more
|
|
209
327
|
proxyOk = await proxyAlive();
|
|
210
328
|
}
|
|
211
329
|
process.stdout.write(proxyOk ? ' \x1b[32mbereit!\x1b[0m\n' : ' \x1b[33mTimeout — direkte API\x1b[0m\n');
|
|
@@ -213,16 +331,13 @@ async function main() {
|
|
|
213
331
|
|
|
214
332
|
showBanner(proxyOk);
|
|
215
333
|
|
|
216
|
-
//
|
|
334
|
+
// Wait for update check to finish (it's fast, already running)
|
|
335
|
+
await updatePromise;
|
|
336
|
+
|
|
217
337
|
const env = {
|
|
218
338
|
...process.env,
|
|
219
|
-
|
|
220
|
-
CLAUDE_MODEL: 'claude-sonnet-4-6',
|
|
221
|
-
CLAUDE_SMALL_FAST_MODEL: 'claude-haiku-4-5-20251001',
|
|
222
|
-
CLAUDE_CODE_MAX_OUTPUT_TOKENS: '2048',
|
|
223
|
-
CLAUDE_CODE_MAX_THINKING_TOKENS: '1000',
|
|
339
|
+
CLAUDE_CODE_MAX_OUTPUT_TOKENS: '16000',
|
|
224
340
|
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1',
|
|
225
|
-
XITHEAD_MODEL: 'claude-sonnet-4-6',
|
|
226
341
|
XITHEAD_ENABLE_CACHE_CONTROL: '1',
|
|
227
342
|
XITHEAD_ENABLE_DEDUP: '1',
|
|
228
343
|
XITHEAD_ENABLE_CONTEXT_TRIM: '1',
|
|
@@ -231,7 +346,6 @@ async function main() {
|
|
|
231
346
|
ANTHROPIC_BASE_URL: proxyOk ? `${PROXY_URL}/v1` : 'https://api.anthropic.com/v1',
|
|
232
347
|
};
|
|
233
348
|
|
|
234
|
-
// Find claude binary
|
|
235
349
|
const claudeResult = spawnSync(process.platform === 'win32' ? 'where' : 'which', ['claude'], { stdio: 'pipe' });
|
|
236
350
|
if (claudeResult.status !== 0) {
|
|
237
351
|
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.4",
|
|
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
|
},
|