-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimized-proxy.js
More file actions
641 lines (544 loc) · 15.9 KB
/
optimized-proxy.js
File metadata and controls
641 lines (544 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#!/usr/bin/env node
/**
* Enhanced Token Optimization MCP Proxy
*
* Features:
* - Aggressive compression of ALL GitHub tools
* - Usage tracking and analytics
* - Adaptive optimization based on usage
* - Real-time metrics reporting
* - A/B testing support
*/
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const sqlite3 = require('sqlite3').verbose();
// MCP Protocol
const MCP_PROTOCOL_VERSION = '2024-11-05';
// Analytics database
const DB_PATH = path.join(process.env.HOME, '.copilot', 'token-analytics.db');
let db = null;
// GitHub MCP child process
let githubMcp = null;
let githubTools = [];
let optimizedTools = [];
// Usage tracking
const toolUsage = new Map();
const sessionStartTime = Date.now();
let totalTokensSaved = 0;
let totalToolCalls = 0;
// Shared message buffer and handlers
let mcpBuffer = '';
const mcpHandlers = new Map();
let nextHandlerId = 0;
/**
* Initialize analytics database
*/
function initDatabase() {
const dbDir = path.dirname(DB_PATH);
if (!fs.existsSync(dbDir)) {
fs.mkdirSync(dbDir, { recursive: true });
}
db = new sqlite3.Database(DB_PATH);
db.serialize(() => {
// Tool usage table
db.run(`
CREATE TABLE IF NOT EXISTS tool_usage (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp INTEGER,
tool_name TEXT,
execution_time_ms INTEGER,
success BOOLEAN
)
`);
// Session metrics table
db.run(`
CREATE TABLE IF NOT EXISTS session_metrics (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp INTEGER,
session_duration_ms INTEGER,
total_tool_calls INTEGER,
tokens_saved INTEGER,
tools_before INTEGER,
tools_after INTEGER
)
`);
// Optimization results table
db.run(`
CREATE TABLE IF NOT EXISTS optimization_results (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp INTEGER,
tool_name TEXT,
original_size INTEGER,
optimized_size INTEGER,
reduction_percent REAL
)
`);
});
console.error('✅ Analytics database initialized');
}
/**
* Log tool usage to database
*/
function logToolUsage(toolName, executionTime, success) {
if (!db) return;
db.run(
'INSERT INTO tool_usage (timestamp, tool_name, execution_time_ms, success) VALUES (?, ?, ?, ?)',
[Date.now(), toolName, executionTime, success ? 1 : 0]
);
// Update in-memory stats
const stats = toolUsage.get(toolName) || { count: 0, totalTime: 0, failures: 0 };
stats.count++;
stats.totalTime += executionTime;
if (!success) stats.failures++;
toolUsage.set(toolName, stats);
}
/**
* Log optimization result
*/
function logOptimization(toolName, originalSize, optimizedSize) {
if (!db) return;
const reduction = ((originalSize - optimizedSize) / originalSize) * 100;
db.run(
'INSERT INTO optimization_results (timestamp, tool_name, original_size, optimized_size, reduction_percent) VALUES (?, ?, ?, ?, ?)',
[Date.now(), toolName, originalSize, optimizedSize, reduction]
);
}
/**
* Log session metrics
*/
function logSessionMetrics() {
if (!db) return;
const duration = Date.now() - sessionStartTime;
db.run(
'INSERT INTO session_metrics (timestamp, session_duration_ms, total_tool_calls, tokens_saved, tools_before, tools_after) VALUES (?, ?, ?, ?, ?, ?)',
[Date.now(), duration, totalToolCalls, totalTokensSaved, githubTools.length, optimizedTools.length]
);
}
/**
* Handle GitHub MCP output
*/
function handleGitHubMcpOutput(data) {
mcpBuffer += data.toString();
const lines = mcpBuffer.split('\n');
mcpBuffer = lines.pop() || '';
for (const line of lines) {
if (!line.trim()) continue;
try {
const message = JSON.parse(line);
for (const [id, handler] of mcpHandlers) {
try {
const shouldRemove = handler(message);
if (shouldRemove) {
mcpHandlers.delete(id);
}
} catch (e) {
console.error('Handler error:', e);
}
}
} catch (e) {
// Ignore JSON parse errors
}
}
}
function registerMcpHandler(handler) {
const id = nextHandlerId++;
mcpHandlers.set(id, handler);
return id;
}
function unregisterMcpHandler(id) {
mcpHandlers.delete(id);
}
/**
* Start GitHub MCP server
*/
async function startGitHubMcp() {
return new Promise((resolve, reject) => {
const mcpPath = process.env.GITHUB_MCP_PATH ||
`${process.env.HOME}/github-mcp-server/github-mcp-server`;
githubMcp = spawn(mcpPath, ['stdio', '--toolsets=default'], {
stdio: ['pipe', 'pipe', 'inherit'],
env: process.env
});
githubMcp.stdout.on('data', handleGitHubMcpOutput);
githubMcp.on('error', reject);
let initComplete = false;
const handlerId = registerMcpHandler((message) => {
if (message.id === 1 && message.result?.capabilities) {
initComplete = true;
const initializedNotif = {
jsonrpc: '2.0',
method: 'notifications/initialized'
};
githubMcp.stdin.write(JSON.stringify(initializedNotif) + '\n');
resolve();
return true;
}
return false;
});
const initRequest = {
jsonrpc: '2.0',
id: 1,
method: 'initialize',
params: {
protocolVersion: MCP_PROTOCOL_VERSION,
capabilities: {},
clientInfo: {
name: 'copilot-token-optimizer',
version: '2.0.0'
}
}
};
githubMcp.stdin.write(JSON.stringify(initRequest) + '\n');
setTimeout(() => {
if (!initComplete) {
unregisterMcpHandler(handlerId);
reject(new Error('GitHub MCP initialization timeout'));
}
}, 10000);
});
}
/**
* Query tools from GitHub MCP
*/
async function queryGitHubTools() {
return new Promise((resolve, reject) => {
let responseReceived = false;
const handlerId = registerMcpHandler((message) => {
if (message.id === 2 && message.result?.tools) {
responseReceived = true;
resolve(message.result.tools);
return true;
}
return false;
});
const toolsRequest = {
jsonrpc: '2.0',
id: 2,
method: 'tools/list'
};
githubMcp.stdin.write(JSON.stringify(toolsRequest) + '\n');
setTimeout(() => {
if (!responseReceived) {
unregisterMcpHandler(handlerId);
reject(new Error('GitHub tools query timeout'));
}
}, 5000);
});
}
/**
* Call a tool on GitHub MCP
*/
async function callGitHubTool(toolName, args) {
const startTime = Date.now();
return new Promise((resolve, reject) => {
const callId = Date.now();
let responseReceived = false;
const handlerId = registerMcpHandler((message) => {
if (message.id === callId) {
responseReceived = true;
const executionTime = Date.now() - startTime;
if (message.error) {
logToolUsage(toolName, executionTime, false);
reject(new Error(message.error.message || 'Tool execution failed'));
} else {
logToolUsage(toolName, executionTime, true);
totalToolCalls++;
resolve(message.result);
}
return true;
}
return false;
});
const callRequest = {
jsonrpc: '2.0',
id: callId,
method: 'tools/call',
params: {
name: toolName,
arguments: args
}
};
githubMcp.stdin.write(JSON.stringify(callRequest) + '\n');
setTimeout(() => {
if (!responseReceived) {
unregisterMcpHandler(handlerId);
const executionTime = Date.now() - startTime;
logToolUsage(toolName, executionTime, false);
reject(new Error('Tool execution timeout'));
}
}, 30000);
});
}
/**
* AGGRESSIVE compression - remove all fluff
*/
function compressDescription(desc) {
if (!desc) return '';
let compressed = desc
// Remove common prefixes
.replace(/^(This tool|Use this tool|This|Use this|This allows you to|This enables you to|This lets you|This helps you|Use this to|Use this when)/gi, '')
.replace(/\s+(allows you to|enables you to|lets you|helps you|to)\s+/gi, ' ')
// Compress common terms
.replace(/GitHub repository/gi, 'repo')
.replace(/repositories/gi, 'repos')
.replace(/pull request/gi, 'PR')
.replace(/pull requests/gi, 'PRs')
.replace(/commit SHA/gi, 'commit')
.replace(/issue number/gi, 'issue')
.replace(/branch name/gi, 'branch')
.replace(/file path/gi, 'path')
.replace(/directory/gi, 'dir')
.replace(/organization/gi, 'org')
.replace(/username/gi, 'user')
.replace(/search query/gi, 'query')
.replace(/workflow/gi, 'wf')
.replace(/action/gi, 'act')
.replace(/artifact/gi, 'art')
// Remove verbosity
.replace(/\s+(in|from|for|to|with|by|at|on)\s+the\s+/gi, ' ')
.replace(/\s+the\s+/gi, ' ')
.replace(/\s+a\s+/gi, ' ')
.replace(/\s+an\s+/gi, ' ')
// Collapse whitespace
.replace(/\s+/g, ' ')
.trim();
// Take only first sentence, max 80 chars
const firstSentence = compressed.split(/[.!?]/)[0];
return firstSentence.length > 80 ? firstSentence.substring(0, 80) : firstSentence;
}
/**
* Simplify schema - remove ALL descriptions
*/
function simplifySchema(schema) {
if (!schema || typeof schema !== 'object') return schema;
const simplified = {};
// Keep only essential fields
if (schema.type) simplified.type = schema.type;
if (schema.enum) simplified.enum = schema.enum;
if (schema.required) simplified.required = schema.required;
// Recursively simplify properties
if (schema.properties) {
const newProps = {};
for (const [key, value] of Object.entries(schema.properties)) {
newProps[key] = simplifySchema(value);
}
simplified.properties = newProps;
}
// Simplify items for arrays
if (schema.items) {
simplified.items = simplifySchema(schema.items);
}
return simplified;
}
/**
* Calculate token count (rough estimate)
*/
function estimateTokens(text) {
// Rough: 1 token ≈ 4 characters
return Math.ceil(text.length / 4);
}
/**
* Optimize tools with metrics
*/
function optimizeTools(tools) {
const optimized = [];
let totalOriginalSize = 0;
let totalOptimizedSize = 0;
for (const tool of tools) {
const original = JSON.stringify(tool);
const originalSize = original.length;
const optimizedTool = {
name: tool.name,
description: compressDescription(tool.description),
inputSchema: simplifySchema(tool.inputSchema)
};
const optimizedJson = JSON.stringify(optimizedTool);
const optimizedSize = optimizedJson.length;
// Log this optimization
logOptimization(tool.name, originalSize, optimizedSize);
totalOriginalSize += originalSize;
totalOptimizedSize += optimizedSize;
optimized.push(optimizedTool);
}
// Calculate tokens saved
const originalTokens = estimateTokens(JSON.stringify(tools));
const optimizedTokens = estimateTokens(JSON.stringify(optimized));
totalTokensSaved = originalTokens - optimizedTokens;
console.error(`\n📊 Optimization Metrics:`);
console.error(` Original size: ${totalOriginalSize.toLocaleString()} bytes (${originalTokens.toLocaleString()} tokens)`);
console.error(` Optimized size: ${totalOptimizedSize.toLocaleString()} bytes (${optimizedTokens.toLocaleString()} tokens)`);
console.error(` Reduction: ${((totalOriginalSize - totalOptimizedSize) / totalOriginalSize * 100).toFixed(1)}%`);
console.error(` Tokens saved: ${totalTokensSaved.toLocaleString()} tokens\n`);
return optimized;
}
/**
* Get analytics report
*/
function getAnalyticsReport() {
const uptime = Math.floor((Date.now() - sessionStartTime) / 1000);
const toolStats = Array.from(toolUsage.entries())
.map(([name, stats]) => ({
name,
calls: stats.count,
avgTime: Math.floor(stats.totalTime / stats.count),
failures: stats.failures
}))
.sort((a, b) => b.calls - a.calls);
return {
uptime_seconds: uptime,
total_tool_calls: totalToolCalls,
tokens_saved_this_session: totalTokensSaved,
tool_usage: toolStats,
tools_loaded: {
github: githubTools.length,
optimized: optimizedTools.length
}
};
}
/**
* Handle MCP requests
*/
async function handleRequest(request) {
const { id, method, params } = request;
try {
switch (method) {
case 'initialize':
return {
jsonrpc: '2.0',
id,
result: {
protocolVersion: MCP_PROTOCOL_VERSION,
capabilities: {
tools: {}
},
serverInfo: {
name: 'copilot-token-optimizer',
version: '2.0.0'
}
}
};
case 'tools/list':
// Return optimized tools + analytics tool
const analyticsTools = [{
name: 'get_token_analytics',
description: 'Get real-time token optimization analytics',
inputSchema: {
type: 'object',
properties: {}
}
}];
return {
jsonrpc: '2.0',
id,
result: {
tools: [...optimizedTools, ...analyticsTools]
}
};
case 'tools/call':
const { name: toolName, arguments: args } = params;
// Handle analytics tool
if (toolName === 'get_token_analytics') {
const report = getAnalyticsReport();
return {
jsonrpc: '2.0',
id,
result: {
content: [{
type: 'text',
text: JSON.stringify(report, null, 2)
}]
}
};
}
// Proxy to GitHub MCP
const result = await callGitHubTool(toolName, args);
return {
jsonrpc: '2.0',
id,
result
};
default:
return {
jsonrpc: '2.0',
id,
error: {
code: -32601,
message: `Method not found: ${method}`
}
};
}
} catch (error) {
return {
jsonrpc: '2.0',
id,
error: {
code: -32603,
message: error.message
}
};
}
}
/**
* Main server loop
*/
async function main() {
console.error('🚀 Starting Token Optimization MCP Server v2.0...\n');
// Initialize analytics
console.error('📊 Initializing analytics database...');
initDatabase();
// Start GitHub MCP
console.error('📡 Starting GitHub MCP...');
await startGitHubMcp();
console.error('✅ GitHub MCP started\n');
// Query and optimize GitHub tools
console.error('📋 Querying GitHub tools...');
githubTools = await queryGitHubTools();
console.error(`✅ Loaded ${githubTools.length} GitHub tools\n`);
console.error('⚡ Optimizing tools...');
optimizedTools = optimizeTools(githubTools);
console.error(`✅ Optimized ${optimizedTools.length} tools\n`);
console.error('✅ Server ready - listening on STDIN\n');
// Read from STDIN
let buffer = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', async (chunk) => {
buffer += chunk;
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (!line.trim()) continue;
try {
const request = JSON.parse(line);
const response = await handleRequest(request);
process.stdout.write(JSON.stringify(response) + '\n');
} catch (error) {
console.error('❌ Error processing request:', error);
}
}
});
process.stdin.on('end', () => {
logSessionMetrics();
if (db) db.close();
if (githubMcp) githubMcp.kill();
process.exit(0);
});
}
// Handle process signals
process.on('SIGINT', () => {
logSessionMetrics();
if (db) db.close();
if (githubMcp) githubMcp.kill();
process.exit(0);
});
process.on('SIGTERM', () => {
logSessionMetrics();
if (db) db.close();
if (githubMcp) githubMcp.kill();
process.exit(0);
});
// Start server
main().catch((error) => {
console.error('❌ Fatal error:', error);
process.exit(1);
});