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/npm/index.d.ts CHANGED
@@ -1,57 +1,57 @@
1
- /**
2
- * xithead - Claude Context Optimizer
3
- * by Xtream-ITSolutions UG
4
- */
5
-
6
- export interface Message {
7
- role: 'user' | 'assistant';
8
- content: string | ContentBlock[];
9
- }
10
-
11
- export interface ContentBlock {
12
- type: 'text' | 'image' | 'tool_use' | 'tool_result';
13
- text?: string;
14
- cache_control?: { type: 'ephemeral' };
15
- [key: string]: unknown;
16
- }
17
-
18
- export interface OptimizeOptions {
19
- messages: Message[];
20
- system?: string | ContentBlock[];
21
- model?: string;
22
- enableCacheControl?: boolean;
23
- enableDedup?: boolean;
24
- enableSystemCompress?: boolean;
25
- }
26
-
27
- export interface OptimizeResult {
28
- messages: Message[];
29
- system?: string | ContentBlock[];
30
- tokensSaved: number;
31
- costSavedUsd: number;
32
- compressionRatio: number;
33
- techniquesApplied: string[];
34
- model: string;
35
- }
36
-
37
- export interface XitheadStats {
38
- requests: number;
39
- tokensSaved: number;
40
- costSavedUsd: number;
41
- avgTokensSaved: number;
42
- }
43
-
44
- export function optimize(options: OptimizeOptions): OptimizeResult;
45
- export function injectCacheControl(messages: Message[], system?: string | ContentBlock[], minTokens?: number): { messages: Message[]; system?: string | ContentBlock[] };
46
- export function dedupMessages(messages: Message[]): { messages: Message[]; changed: boolean };
47
- export function estimateTokens(text: string): number;
48
- export function createOptimizedClient(anthropicClient: unknown): {
49
- messages: {
50
- create(params: Record<string, unknown>): Promise<unknown>;
51
- stream(params: Record<string, unknown>): unknown;
52
- };
53
- getStats(): XitheadStats;
54
- };
55
-
56
- export const CLAUDE_INPUT_COST_PER_TOKEN: number;
57
- export const CLAUDE_CACHE_READ_COST: number;
1
+ /**
2
+ * xithead - Claude Context Optimizer
3
+ * by Xtream-ITSolutions UG
4
+ */
5
+
6
+ export interface Message {
7
+ role: 'user' | 'assistant';
8
+ content: string | ContentBlock[];
9
+ }
10
+
11
+ export interface ContentBlock {
12
+ type: 'text' | 'image' | 'tool_use' | 'tool_result';
13
+ text?: string;
14
+ cache_control?: { type: 'ephemeral' };
15
+ [key: string]: unknown;
16
+ }
17
+
18
+ export interface OptimizeOptions {
19
+ messages: Message[];
20
+ system?: string | ContentBlock[];
21
+ model?: string;
22
+ enableCacheControl?: boolean;
23
+ enableDedup?: boolean;
24
+ enableSystemCompress?: boolean;
25
+ }
26
+
27
+ export interface OptimizeResult {
28
+ messages: Message[];
29
+ system?: string | ContentBlock[];
30
+ tokensSaved: number;
31
+ costSavedUsd: number;
32
+ compressionRatio: number;
33
+ techniquesApplied: string[];
34
+ model: string;
35
+ }
36
+
37
+ export interface XitheadStats {
38
+ requests: number;
39
+ tokensSaved: number;
40
+ costSavedUsd: number;
41
+ avgTokensSaved: number;
42
+ }
43
+
44
+ export function optimize(options: OptimizeOptions): OptimizeResult;
45
+ export function injectCacheControl(messages: Message[], system?: string | ContentBlock[], minTokens?: number): { messages: Message[]; system?: string | ContentBlock[] };
46
+ export function dedupMessages(messages: Message[]): { messages: Message[]; changed: boolean };
47
+ export function estimateTokens(text: string): number;
48
+ export function createOptimizedClient(anthropicClient: unknown): {
49
+ messages: {
50
+ create(params: Record<string, unknown>): Promise<unknown>;
51
+ stream(params: Record<string, unknown>): unknown;
52
+ };
53
+ getStats(): XitheadStats;
54
+ };
55
+
56
+ export const CLAUDE_INPUT_COST_PER_TOKEN: number;
57
+ export const CLAUDE_CACHE_READ_COST: number;
package/npm/index.js CHANGED
@@ -1,212 +1,139 @@
1
- /**
2
- * xithead - Claude Context Optimizer
3
- * by Xtream-ITSolutions UG
4
- * discord.gg/xtream-itsolutions
5
- */
6
-
7
- 'use strict';
8
-
9
- const CLAUDE_INPUT_COST_PER_TOKEN = 3e-6;
10
- const CLAUDE_CACHE_READ_COST = 0.30e-6;
11
-
12
- /**
13
- * Estimate token count (1 token ≈ 4 chars)
14
- */
15
- function estimateTokens(text) {
16
- return Math.ceil(text.length / 4);
17
- }
18
-
19
- /**
20
- * Inject Anthropic cache_control markers for prompt caching.
21
- * This enables 90% cost reduction on cache hits.
22
- */
23
- function injectCacheControl(messages, system, minTokens = 1024) {
24
- let optimizedSystem = system;
25
- let optimizedMessages = [...messages];
26
-
27
- // Mark system prompt for caching
28
- if (typeof system === 'string' && estimateTokens(system) >= minTokens) {
29
- optimizedSystem = [{ type: 'text', text: system, cache_control: { type: 'ephemeral' } }];
30
- } else if (Array.isArray(system)) {
31
- optimizedSystem = system.map((block, i) => {
32
- if (block.type === 'text' && estimateTokens(block.text || '') >= minTokens && i === system.length - 1) {
33
- return { ...block, cache_control: { type: 'ephemeral' } };
34
- }
35
- return block;
36
- });
37
- }
38
-
39
- // Mark last large user message for caching
40
- for (let i = optimizedMessages.length - 1; i >= 0; i--) {
41
- const msg = optimizedMessages[i];
42
- if (msg.role === 'user') {
43
- const content = msg.content;
44
- if (typeof content === 'string' && estimateTokens(content) >= minTokens) {
45
- optimizedMessages[i] = {
46
- ...msg,
47
- content: [{ type: 'text', text: content, cache_control: { type: 'ephemeral' } }]
48
- };
49
- } else if (Array.isArray(content)) {
50
- const newContent = content.map((block, j) => {
51
- const text = block.text || block.content || '';
52
- if (estimateTokens(String(text)) >= minTokens && j === content.length - 1) {
53
- return { ...block, cache_control: { type: 'ephemeral' } };
54
- }
55
- return block;
56
- });
57
- optimizedMessages[i] = { ...msg, content: newContent };
58
- }
59
- break;
60
- }
61
- }
62
-
63
- return { messages: optimizedMessages, system: optimizedSystem };
64
- }
65
-
66
- /**
67
- * Remove consecutive duplicate messages
68
- */
69
- function dedupMessages(messages) {
70
- if (messages.length < 2) return { messages, changed: false };
71
- const result = [messages[0]];
72
- let changed = false;
73
- for (let i = 1; i < messages.length; i++) {
74
- const cur = messages[i];
75
- const prev = result[result.length - 1];
76
- if (cur.role === prev.role && JSON.stringify(cur.content) === JSON.stringify(prev.content)) {
77
- changed = true;
78
- continue;
79
- }
80
- result.push(cur);
81
- }
82
- return { messages: result, changed };
83
- }
84
-
85
- /**
86
- * Add verbosity reduction suffix to system prompt
87
- */
88
- function compressSystem(system) {
89
- const suffix = '\n\nIMPORTANT: Be maximally concise. Do not restate context. Skip preambles.';
90
- if (typeof system === 'string') {
91
- if (system.includes('Be maximally concise')) return { system, changed: false };
92
- return { system: system + suffix, changed: true };
93
- }
94
- if (Array.isArray(system)) {
95
- for (const block of system) {
96
- if (block.text && block.text.includes('Be maximally concise')) return { system, changed: false };
97
- }
98
- return { system: [...system, { type: 'text', text: suffix.trim() }], changed: true };
99
- }
100
- return { system, changed: false };
101
- }
102
-
103
- /**
104
- * Main optimize function - optimizes messages for Claude API calls
105
- *
106
- * @param {Object} options
107
- * @param {Array} options.messages - Claude messages array
108
- * @param {string|Array} [options.system] - System prompt
109
- * @param {string} [options.model] - Model name
110
- * @param {boolean} [options.enableCacheControl] - Inject cache_control markers (default: true)
111
- * @param {boolean} [options.enableDedup] - Remove duplicate messages (default: true)
112
- * @param {boolean} [options.enableSystemCompress] - Compress system prompt (default: true)
113
- * @returns {Object} result with optimizedMessages, optimizedSystem, tokensSaved, costSavedUsd, techniquesApplied
114
- */
115
- function optimize({ messages, system, model = 'claude-sonnet-4-6', enableCacheControl = true, enableDedup = true, enableSystemCompress = true } = {}) {
116
- const techniques = [];
117
- let msgs = [...messages];
118
- let sys = system;
119
-
120
- const originalTokens = estimateTokens(JSON.stringify(msgs) + JSON.stringify(sys || ''));
121
-
122
- if (enableDedup) {
123
- const { messages: deduped, changed } = dedupMessages(msgs);
124
- if (changed) { msgs = deduped; techniques.push('dedup'); }
125
- }
126
-
127
- if (enableSystemCompress && sys) {
128
- const { system: compressed, changed } = compressSystem(sys);
129
- if (changed) { sys = compressed; techniques.push('system_compress'); }
130
- }
131
-
132
- if (enableCacheControl) {
133
- const result = injectCacheControl(msgs, sys);
134
- msgs = result.messages;
135
- sys = result.system;
136
- techniques.push('cache_control');
137
- }
138
-
139
- const optimizedTokens = estimateTokens(JSON.stringify(msgs) + JSON.stringify(sys || ''));
140
- const tokensSaved = Math.max(0, originalTokens - optimizedTokens);
141
- const costSavedUsd = tokensSaved * CLAUDE_INPUT_COST_PER_TOKEN;
142
-
143
- return {
144
- messages: msgs,
145
- system: sys,
146
- tokensSaved,
147
- costSavedUsd,
148
- compressionRatio: originalTokens > 0 ? tokensSaved / originalTokens : 0,
149
- techniquesApplied: techniques,
150
- model,
151
- };
152
- }
153
-
154
- /**
155
- * Create an optimizing wrapper around the Anthropic SDK client.
156
- *
157
- * @param {Object} anthropicClient - Anthropic SDK client instance
158
- * @returns {Object} Wrapped client with automatic optimization
159
- */
160
- function createOptimizedClient(anthropicClient) {
161
- const stats = { requests: 0, tokensSaved: 0, costSavedUsd: 0 };
162
-
163
- return {
164
- messages: {
165
- create(params) {
166
- const result = optimize({
167
- messages: params.messages || [],
168
- system: params.system,
169
- model: params.model,
170
- });
171
- stats.requests++;
172
- stats.tokensSaved += result.tokensSaved;
173
- stats.costSavedUsd += result.costSavedUsd;
174
-
175
- return anthropicClient.messages.create({
176
- ...params,
177
- messages: result.messages,
178
- system: result.system,
179
- });
180
- },
181
- stream(params) {
182
- const result = optimize({
183
- messages: params.messages || [],
184
- system: params.system,
185
- model: params.model,
186
- });
187
- stats.requests++;
188
- stats.tokensSaved += result.tokensSaved;
189
- stats.costSavedUsd += result.costSavedUsd;
190
-
191
- return anthropicClient.messages.stream({
192
- ...params,
193
- messages: result.messages,
194
- system: result.system,
195
- });
196
- },
197
- },
198
- getStats() {
199
- return { ...stats, avgTokensSaved: stats.requests > 0 ? stats.tokensSaved / stats.requests : 0 };
200
- },
201
- };
202
- }
203
-
204
- module.exports = {
205
- optimize,
206
- createOptimizedClient,
207
- injectCacheControl,
208
- dedupMessages,
209
- estimateTokens,
210
- CLAUDE_INPUT_COST_PER_TOKEN,
211
- CLAUDE_CACHE_READ_COST,
212
- };
1
+ /**
2
+ * xtream-head — Claude Code Proxy
3
+ * by Xtream-ITSolutions UG | xtream-head.com
4
+ */
5
+ 'use strict';
6
+
7
+ const CLAUDE_INPUT_COST_PER_TOKEN = 3e-6;
8
+ const CLAUDE_CACHE_READ_COST = 0.30e-6;
9
+
10
+ function estimateTokens(text) {
11
+ return Math.ceil(text.length / 4);
12
+ }
13
+
14
+ function injectCacheControl(messages, system, minTokens = 1024) {
15
+ let optimizedSystem = system;
16
+ let optimizedMessages = [...messages];
17
+
18
+ if (typeof system === 'string' && estimateTokens(system) >= minTokens) {
19
+ optimizedSystem = [{ type: 'text', text: system, cache_control: { type: 'ephemeral' } }];
20
+ } else if (Array.isArray(system)) {
21
+ optimizedSystem = system.map((block, i) => {
22
+ if (block.type === 'text' && estimateTokens(block.text || '') >= minTokens && i === system.length - 1) {
23
+ return { ...block, cache_control: { type: 'ephemeral' } };
24
+ }
25
+ return block;
26
+ });
27
+ }
28
+
29
+ for (let i = optimizedMessages.length - 1; i >= 0; i--) {
30
+ const msg = optimizedMessages[i];
31
+ if (msg.role === 'user') {
32
+ const content = msg.content;
33
+ if (typeof content === 'string' && estimateTokens(content) >= minTokens) {
34
+ optimizedMessages[i] = {
35
+ ...msg,
36
+ content: [{ type: 'text', text: content, cache_control: { type: 'ephemeral' } }]
37
+ };
38
+ } else if (Array.isArray(content)) {
39
+ optimizedMessages[i] = {
40
+ ...msg,
41
+ content: content.map((block, j) => {
42
+ const text = block.text || block.content || '';
43
+ if (estimateTokens(String(text)) >= minTokens && j === content.length - 1) {
44
+ return { ...block, cache_control: { type: 'ephemeral' } };
45
+ }
46
+ return block;
47
+ }),
48
+ };
49
+ }
50
+ break;
51
+ }
52
+ }
53
+
54
+ return { messages: optimizedMessages, system: optimizedSystem };
55
+ }
56
+
57
+ function dedupMessages(messages) {
58
+ if (messages.length < 2) return { messages, changed: false };
59
+ const result = [messages[0]];
60
+ let changed = false;
61
+ for (let i = 1; i < messages.length; i++) {
62
+ const cur = messages[i];
63
+ const prev = result[result.length - 1];
64
+ if (cur.role === prev.role && JSON.stringify(cur.content) === JSON.stringify(prev.content)) {
65
+ changed = true;
66
+ continue;
67
+ }
68
+ result.push(cur);
69
+ }
70
+ return { messages: result, changed };
71
+ }
72
+
73
+ function optimize({ messages, system, model = 'claude-sonnet-4-6', enableCacheControl = true, enableDedup = true } = {}) {
74
+ const techniques = [];
75
+ let msgs = [...messages];
76
+ let sys = system;
77
+
78
+ const originalTokens = estimateTokens(JSON.stringify(msgs) + JSON.stringify(sys || ''));
79
+
80
+ if (enableDedup) {
81
+ const { messages: deduped, changed } = dedupMessages(msgs);
82
+ if (changed) { msgs = deduped; techniques.push('dedup'); }
83
+ }
84
+
85
+ if (enableCacheControl) {
86
+ const result = injectCacheControl(msgs, sys);
87
+ msgs = result.messages;
88
+ sys = result.system;
89
+ techniques.push('cache_control');
90
+ }
91
+
92
+ const optimizedTokens = estimateTokens(JSON.stringify(msgs) + JSON.stringify(sys || ''));
93
+ const tokensSaved = Math.max(0, originalTokens - optimizedTokens);
94
+
95
+ return {
96
+ messages: msgs,
97
+ system: sys,
98
+ tokensSaved,
99
+ costSavedUsd: tokensSaved * CLAUDE_INPUT_COST_PER_TOKEN,
100
+ compressionRatio: originalTokens > 0 ? tokensSaved / originalTokens : 0,
101
+ techniquesApplied: techniques,
102
+ model,
103
+ };
104
+ }
105
+
106
+ function createOptimizedClient(anthropicClient) {
107
+ const stats = { requests: 0, tokensSaved: 0, costSavedUsd: 0 };
108
+ return {
109
+ messages: {
110
+ create(params) {
111
+ const result = optimize({ messages: params.messages || [], system: params.system, model: params.model });
112
+ stats.requests++;
113
+ stats.tokensSaved += result.tokensSaved;
114
+ stats.costSavedUsd += result.costSavedUsd;
115
+ return anthropicClient.messages.create({ ...params, messages: result.messages, system: result.system });
116
+ },
117
+ stream(params) {
118
+ const result = optimize({ messages: params.messages || [], system: params.system, model: params.model });
119
+ stats.requests++;
120
+ stats.tokensSaved += result.tokensSaved;
121
+ stats.costSavedUsd += result.costSavedUsd;
122
+ return anthropicClient.messages.stream({ ...params, messages: result.messages, system: result.system });
123
+ },
124
+ },
125
+ getStats() {
126
+ return { ...stats, avgTokensSaved: stats.requests > 0 ? stats.tokensSaved / stats.requests : 0 };
127
+ },
128
+ };
129
+ }
130
+
131
+ module.exports = {
132
+ optimize,
133
+ createOptimizedClient,
134
+ injectCacheControl,
135
+ dedupMessages,
136
+ estimateTokens,
137
+ CLAUDE_INPUT_COST_PER_TOKEN,
138
+ CLAUDE_CACHE_READ_COST,
139
+ };
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "xithead",
3
+ "version": "1.4.0",
4
+ "description": "Lokaler Claude Code Proxy — Prompt Cache maximieren, Token-Kosten bis zu 88% reduzieren. By Xtream-ITSolutions UG.",
5
+ "keywords": [
6
+ "claude",
7
+ "anthropic",
8
+ "llm",
9
+ "token",
10
+ "optimization",
11
+ "caching",
12
+ "proxy",
13
+ "context",
14
+ "ai",
15
+ "cost-reduction",
16
+ "claude-code"
17
+ ],
18
+ "author": "Xtream-ITSolutions UG <hello@xtream-itsolutions.com>",
19
+ "license": "Apache-2.0",
20
+ "homepage": "https://xtream-head.com",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/xtream-itsolutions/xithead.git"
24
+ },
25
+ "bugs": {
26
+ "url": "https://github.com/xtream-itsolutions/xithead/issues"
27
+ },
28
+ "bin": {
29
+ "xithead": "./bin/xithead.js"
30
+ },
31
+ "files": [
32
+ "index.js",
33
+ "index.d.ts",
34
+ "bin/",
35
+ "config/",
36
+ "utils/"
37
+ ],
38
+ "type": "module",
39
+ "engines": {
40
+ "node": ">=18.0.0"
41
+ },
42
+ "dependencies": {
43
+ "chalk": "^5.3.0",
44
+ "dotenv": "^16.4.5",
45
+ "yargs": "^17.7.2"
46
+ },
47
+ "devDependencies": {
48
+ "@types/node": "^22.0.0"
49
+ },
50
+ "publishConfig": {
51
+ "access": "public",
52
+ "registry": "https://registry.npmjs.org/"
53
+ }
54
+ }
@@ -1,43 +1,43 @@
1
- #!/usr/bin/env node
2
- /**
3
- * xithead postinstall — auto-installs Python proxy dependencies
4
- */
5
- 'use strict';
6
-
7
- const { spawnSync } = require('child_process');
8
-
9
- function findPython() {
10
- for (const cmd of ['python', 'python3', 'py']) {
11
- const r = spawnSync(cmd, ['--version'], { stdio: 'pipe' });
12
- if (r.status === 0) return cmd;
13
- }
14
- return null;
15
- }
16
-
17
- const py = findPython();
18
- if (!py) {
19
- console.log('[xithead] Python nicht gefunden — überspringe Proxy-Setup.');
20
- console.log(' Installiere Python von https://python.org und führe dann aus:');
21
- console.log(' pip install "xithead[proxy]"');
22
- process.exit(0);
23
- }
24
-
25
- // Check if proxy deps already installed
26
- const check = spawnSync(py, ['-c', 'import fastapi, uvicorn, httpx'], { stdio: 'pipe' });
27
- if (check.status === 0) {
28
- console.log('[xithead] Proxy-Abhängigkeiten bereits installiert ✓');
29
- process.exit(0);
30
- }
31
-
32
- console.log('[xithead] Installiere xithead[proxy] (fastapi + uvicorn + httpx)...');
33
- const result = spawnSync(
34
- py, ['-m', 'pip', 'install', '-q', '--upgrade', 'xithead[proxy]'],
35
- { stdio: 'inherit' }
36
- );
37
-
38
- if (result.status === 0) {
39
- console.log('[xithead] Proxy-Abhängigkeiten erfolgreich installiert ✓');
40
- } else {
41
- console.log('[xithead] Hinweis: Proxy-Installation fehlgeschlagen.');
42
- console.log(' Manuell: pip install "xithead[proxy]"');
43
- }
1
+ #!/usr/bin/env node
2
+ /**
3
+ * xithead postinstall — auto-installs Python proxy dependencies
4
+ */
5
+ 'use strict';
6
+
7
+ const { spawnSync } = require('child_process');
8
+
9
+ function findPython() {
10
+ for (const cmd of ['python', 'python3', 'py']) {
11
+ const r = spawnSync(cmd, ['--version'], { stdio: 'pipe' });
12
+ if (r.status === 0) return cmd;
13
+ }
14
+ return null;
15
+ }
16
+
17
+ const py = findPython();
18
+ if (!py) {
19
+ console.log('[xithead] Python nicht gefunden — überspringe Proxy-Setup.');
20
+ console.log(' Installiere Python von https://python.org und führe dann aus:');
21
+ console.log(' pip install "xithead[proxy]"');
22
+ process.exit(0);
23
+ }
24
+
25
+ // Check if proxy deps already installed
26
+ const check = spawnSync(py, ['-c', 'import fastapi, uvicorn, httpx'], { stdio: 'pipe' });
27
+ if (check.status === 0) {
28
+ console.log('[xithead] Proxy-Abhängigkeiten bereits installiert ✓');
29
+ process.exit(0);
30
+ }
31
+
32
+ console.log('[xithead] Installiere xithead[proxy] (fastapi + uvicorn + httpx)...');
33
+ const result = spawnSync(
34
+ py, ['-m', 'pip', 'install', '-q', '--upgrade', 'xithead[proxy]'],
35
+ { stdio: 'inherit' }
36
+ );
37
+
38
+ if (result.status === 0) {
39
+ console.log('[xithead] Proxy-Abhängigkeiten erfolgreich installiert ✓');
40
+ } else {
41
+ console.log('[xithead] Hinweis: Proxy-Installation fehlgeschlagen.');
42
+ console.log(' Manuell: pip install "xithead[proxy]"');
43
+ }