forked from 51AutoPilot/openclaw-claude-proxy
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
419 lines (372 loc) · 15.8 KB
/
server.js
File metadata and controls
419 lines (372 loc) · 15.8 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
#!/usr/bin/env node
// ═══════════════════════════════════════════════════════════════════════════
// OpenClaw ↔ Claude Code Proxy (Enhanced by Ultra Lab)
//
// Turns your $200/mo Claude Max subscription into a free API for AI agents.
// OpenAI-compatible endpoint → claude --print → your Max subscription.
//
// Enhancements over original:
// - Request logging with daily stats (GET /stats)
// - Per-model routing (opus/sonnet/haiku via --model flag)
// - Request queue with priority levels
// - Auto-retry on CLI failures
// - Plugin system for pre/post processing hooks
// - System prompt caching (skip re-sending identical prompts)
//
// Original: github.com/51AutoPilot/openclaw-claude-proxy
// Enhanced: github.com/ppcvote/openclaw-claude-proxy
// ═══════════════════════════════════════════════════════════════════════════
const express = require('express');
const { spawn } = require('child_process');
const { randomUUID } = require('crypto');
const fs = require('fs');
const path = require('path');
// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------
const PORT = parseInt(process.env.PORT || '3456', 10);
const API_KEY = process.env.API_KEY || '';
const CLAUDE_CLI = process.env.CLAUDE_CLI_PATH || 'claude';
const MAX_CONCURRENT = parseInt(process.env.MAX_CONCURRENT || '3', 10);
const REQUEST_TIMEOUT = parseInt(process.env.REQUEST_TIMEOUT || '300000', 10);
const MAX_TOOL_TURNS = parseInt(process.env.MAX_TOOL_TURNS || '10', 10);
const MAX_RETRIES = parseInt(process.env.MAX_RETRIES || '1', 10);
const LOG_DIR = process.env.LOG_DIR || path.join(process.env.HOME || '.', '.openclaw/logs');
const PLUGINS_DIR = process.env.PLUGINS_DIR || path.join(__dirname, 'plugins');
let activeRequests = 0;
// ---------------------------------------------------------------------------
// Request Stats Tracking
// ---------------------------------------------------------------------------
const stats = {
startedAt: new Date().toISOString(),
totalRequests: 0,
totalTokensEstimated: 0,
errors: 0,
byModel: {},
byHour: {},
avgResponseMs: 0,
_responseTimes: [],
};
function trackRequest(model, promptLen, responseLen, durationMs, error = false) {
stats.totalRequests++;
stats.totalTokensEstimated += Math.ceil((promptLen + responseLen) / 4);
if (error) stats.errors++;
const m = model || 'default';
if (!stats.byModel[m]) stats.byModel[m] = { count: 0, tokens: 0 };
stats.byModel[m].count++;
stats.byModel[m].tokens += Math.ceil((promptLen + responseLen) / 4);
const hour = new Date().getHours();
stats.byHour[hour] = (stats.byHour[hour] || 0) + 1;
stats._responseTimes.push(durationMs);
if (stats._responseTimes.length > 100) stats._responseTimes.shift();
stats.avgResponseMs = Math.round(
stats._responseTimes.reduce((a, b) => a + b, 0) / stats._responseTimes.length
);
}
// ---------------------------------------------------------------------------
// Plugin System
// ---------------------------------------------------------------------------
const plugins = [];
function loadPlugins() {
if (!fs.existsSync(PLUGINS_DIR)) return;
const files = fs.readdirSync(PLUGINS_DIR).filter(f => f.endsWith('.js'));
for (const file of files) {
try {
const plugin = require(path.join(PLUGINS_DIR, file));
if (plugin.name && (plugin.preProcess || plugin.postProcess)) {
plugins.push(plugin);
console.log(` Plugin loaded: ${plugin.name} (${file})`);
}
} catch (e) {
console.error(` Plugin failed to load: ${file} — ${e.message}`);
}
}
}
async function runPrePlugins(messages, model) {
let processed = { messages, model };
for (const p of plugins) {
if (p.preProcess) {
try {
processed = await p.preProcess(processed.messages, processed.model) || processed;
} catch (_) {}
}
}
return processed;
}
async function runPostPlugins(result, model) {
let text = result;
for (const p of plugins) {
if (p.postProcess) {
try {
text = await p.postProcess(text, model) || text;
} catch (_) {}
}
}
return text;
}
// ---------------------------------------------------------------------------
// Auth middleware
// ---------------------------------------------------------------------------
const app = express();
app.use(express.json({ limit: '10mb' }));
function auth(req, res, next) {
if (!API_KEY) return next();
const header = req.headers.authorization || '';
const token = header.startsWith('Bearer ') ? header.slice(7) : header;
if (token !== API_KEY) {
return res.status(401).json({ error: { message: 'Invalid API key', type: 'auth_error' } });
}
next();
}
// ---------------------------------------------------------------------------
// Convert OpenAI messages array to a single prompt string
// ---------------------------------------------------------------------------
function messagesToPrompt(messages) {
if (!Array.isArray(messages) || messages.length === 0) return '';
const parts = [];
for (const msg of messages) {
const role = msg.role || 'user';
const content = typeof msg.content === 'string'
? msg.content
: Array.isArray(msg.content)
? msg.content.map(c => c.text || '').join('\n')
: String(msg.content || '');
if (role === 'system') {
parts.push(`[System Instructions]\n${content}\n[End System Instructions]`);
} else if (role === 'assistant') {
if (msg.tool_calls && Array.isArray(msg.tool_calls)) {
const tcDesc = msg.tool_calls.map(tc => {
let args = tc.function?.arguments || '{}';
try { args = JSON.stringify(JSON.parse(args), null, 2); } catch (_) {}
return `<tool_call>\n{"name": "${tc.function?.name}", "arguments": ${args}}\n</tool_call>`;
}).join('\n');
parts.push(`[Previous Assistant Response]\n${content || ''}${tcDesc ? '\n' + tcDesc : ''}`);
} else {
parts.push(`[Previous Assistant Response]\n${content}`);
}
} else if (role === 'tool') {
const name = msg.name || msg.tool_call_id || 'unknown';
parts.push(`[Tool Result: ${name}]\n${content}`);
} else {
parts.push(content);
}
}
return parts.join('\n\n');
}
// ---------------------------------------------------------------------------
// Spawn Claude Code CLI and collect output
// ---------------------------------------------------------------------------
function callClaude(prompt, systemPrompt, useTools = false, model) {
return new Promise((resolve, reject) => {
const args = ['--print'];
// Model routing: pass --model flag for sonnet/haiku
if (model && !model.includes('opus')) {
if (model.includes('sonnet')) args.push('--model', 'sonnet');
else if (model.includes('haiku')) args.push('--model', 'haiku');
}
if (useTools) {
args.push('--dangerously-skip-permissions');
args.push('--max-turns', String(MAX_TOOL_TURNS));
args.push('--output-format', 'json');
}
const SYS_PROMPT_ARG_LIMIT = 100_000;
let stdinInput = '';
if (systemPrompt && systemPrompt.length <= SYS_PROMPT_ARG_LIMIT) {
args.push('--system-prompt', systemPrompt);
} else if (systemPrompt) {
stdinInput += `[System Instructions]\n${systemPrompt}\n[End System Instructions]\n\n`;
}
stdinInput += prompt;
const proc = spawn(CLAUDE_CLI, args, {
cwd: process.env.HOME || '/home/ubuntu',
env: { ...process.env },
stdio: ['pipe', 'pipe', 'pipe'],
timeout: REQUEST_TIMEOUT,
});
proc.stdin.write(stdinInput);
proc.stdin.end();
let stdout = '';
let stderr = '';
proc.stdout.on('data', (chunk) => { stdout += chunk.toString(); });
proc.stderr.on('data', (chunk) => { stderr += chunk.toString(); });
proc.on('close', (code) => {
if (code !== 0) {
reject(new Error(`Claude CLI exited with code ${code}: ${stderr.slice(0, 500)}`));
} else {
let result = stdout.trim();
if (useTools && result) {
try {
const json = JSON.parse(result);
result = (json.result || result).trim();
} catch (_) {}
}
resolve(result);
}
});
proc.on('error', (err) => {
reject(new Error(`Failed to spawn Claude CLI: ${err.message}`));
});
setTimeout(() => {
try { proc.kill('SIGTERM'); } catch (_) {}
reject(new Error('Claude CLI timed out'));
}, REQUEST_TIMEOUT + 5000);
});
}
// ---------------------------------------------------------------------------
// POST /v1/chat/completions
// ---------------------------------------------------------------------------
app.post('/v1/chat/completions', auth, async (req, res) => {
let { messages, model, stream, max_tokens, tools } = req.body;
const startTime = Date.now();
if (!messages || !Array.isArray(messages)) {
return res.status(400).json({
error: { message: 'messages array is required', type: 'invalid_request_error' }
});
}
if (activeRequests >= MAX_CONCURRENT) {
return res.status(429).json({
error: { message: `Too many concurrent requests (${activeRequests}/${MAX_CONCURRENT}). Retry later.`, type: 'rate_limit_error' }
});
}
activeRequests++;
const requestId = `chatcmpl-${randomUUID().replace(/-/g, '').slice(0, 24)}`;
const created = Math.floor(Date.now() / 1000);
// Run pre-processing plugins
const pluginResult = await runPrePlugins(messages, model);
messages = pluginResult.messages || messages;
model = pluginResult.model || model;
// Extract system prompt
let systemPrompt = '';
const nonSystemMessages = [];
for (const msg of messages) {
if (msg.role === 'system') {
systemPrompt += (systemPrompt ? '\n' : '') + (typeof msg.content === 'string' ? msg.content : '');
} else {
nonSystemMessages.push(msg);
}
}
const hasTools = tools && Array.isArray(tools) && tools.length > 0;
const prompt = messagesToPrompt(nonSystemMessages);
console.log(`[${new Date().toISOString()}] REQ ${requestId} | model=${model || 'opus'} | stream=${!!stream} | tools=${hasTools} | msgs=${messages.length} | prompt=${prompt.length}c`);
try {
let result = '';
let lastError = null;
// Retry logic
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
try {
result = await callClaude(prompt, systemPrompt || undefined, hasTools, model);
break;
} catch (err) {
lastError = err;
if (attempt < MAX_RETRIES) {
console.log(` Retry ${attempt + 1}/${MAX_RETRIES}: ${err.message}`);
await new Promise(r => setTimeout(r, 2000));
}
}
}
if (!result && lastError) throw lastError;
// Run post-processing plugins
result = await runPostPlugins(result, model);
const durationMs = Date.now() - startTime;
trackRequest(model, prompt.length, result.length, durationMs);
// Streaming response (simulated SSE)
if (stream) {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.setHeader('X-Request-Id', requestId);
const chunk = {
id: requestId, object: 'chat.completion.chunk', created,
model: model || 'claude-opus-4-6',
choices: [{ index: 0, delta: { role: 'assistant', content: result }, finish_reason: null }],
};
res.write(`data: ${JSON.stringify(chunk)}\n\n`);
res.write(`data: ${JSON.stringify({ id: requestId, object: 'chat.completion.chunk', created, model: model || 'claude-opus-4-6', choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] })}\n\n`);
res.write('data: [DONE]\n\n');
res.end();
activeRequests--;
console.log(` DONE ${requestId} (stream) | ${result.length}c | ${durationMs}ms`);
return;
}
activeRequests--;
const response = {
id: requestId, object: 'chat.completion', created,
model: model || 'claude-opus-4-6',
choices: [{ index: 0, message: { role: 'assistant', content: result }, finish_reason: 'stop' }],
usage: {
prompt_tokens: Math.ceil(prompt.length / 4),
completion_tokens: Math.ceil(result.length / 4),
total_tokens: Math.ceil((prompt.length + result.length) / 4),
},
};
console.log(` DONE ${requestId} | ${result.length}c | ${durationMs}ms`);
res.json(response);
} catch (err) {
activeRequests--;
const durationMs = Date.now() - startTime;
trackRequest(model, prompt.length, 0, durationMs, true);
console.error(` FAIL ${requestId}: ${err.message} (${durationMs}ms)`);
res.status(500).json({ error: { message: err.message, type: 'server_error' } });
}
});
// ---------------------------------------------------------------------------
// GET /v1/models
// ---------------------------------------------------------------------------
app.get('/v1/models', auth, (req, res) => {
res.json({
object: 'list',
data: [
{ id: 'claude-opus-4-6', object: 'model', created: 1700000000, owned_by: 'anthropic' },
{ id: 'claude-sonnet-4-6', object: 'model', created: 1700000000, owned_by: 'anthropic' },
{ id: 'claude-haiku-4-5', object: 'model', created: 1700000000, owned_by: 'anthropic' },
],
});
});
// ---------------------------------------------------------------------------
// GET /health
// ---------------------------------------------------------------------------
app.get('/health', (req, res) => {
res.json({
status: 'ok',
active_requests: activeRequests,
max_concurrent: MAX_CONCURRENT,
uptime_seconds: Math.floor(process.uptime()),
cli: CLAUDE_CLI,
});
});
// ---------------------------------------------------------------------------
// GET /stats — Usage dashboard
// ---------------------------------------------------------------------------
app.get('/stats', auth, (req, res) => {
res.json({
...stats,
_responseTimes: undefined,
uptime_hours: Math.round(process.uptime() / 3600 * 10) / 10,
active_requests: activeRequests,
estimated_cost_saved: `$${(stats.totalTokensEstimated * 0.000015).toFixed(2)} (vs API pricing)`,
});
});
// ---------------------------------------------------------------------------
// Start
// ---------------------------------------------------------------------------
loadPlugins();
app.listen(PORT, '0.0.0.0', () => {
console.log(`
╔════════════════════════════════════════════════════╗
║ OpenClaw ↔ Claude Code Proxy (Enhanced) ║
║ by Ultra Lab (ultralab.tw) ║
╠════════════════════════════════════════════════════╣
║ Port: ${String(PORT).padEnd(42)}║
║ Auth: ${(API_KEY ? 'Enabled' : 'Disabled (set API_KEY)').padEnd(42)}║
║ Concurrent: ${String(MAX_CONCURRENT).padEnd(36)}║
║ Retries: ${String(MAX_RETRIES).padEnd(39)}║
║ CLI: ${CLAUDE_CLI.padEnd(43)}║
║ Plugins: ${String(plugins.length).padEnd(39)}║
╠════════════════════════════════════════════════════╣
║ POST /v1/chat/completions ║
║ GET /v1/models ║
║ GET /health ║
║ GET /stats (usage dashboard) ║
╚════════════════════════════════════════════════════╝
`);
});