-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathstandalone-server-simple.js
More file actions
559 lines (498 loc) · 20.3 KB
/
standalone-server-simple.js
File metadata and controls
559 lines (498 loc) · 20.3 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
#!/usr/bin/env node
/**
* Standalone API Server Runner (Simplified)
*
* This script runs the Claude Autopilot API server independently from the VS Code extension
* for testing and development purposes.
*/
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const { spawn } = require('child_process');
const PORT = process.env.PORT ? parseInt(process.env.PORT) : 8000;
// Simple logger
const logger = {
info: (msg, ...args) => console.log(`[INFO] ${msg}`, ...args),
error: (msg, ...args) => console.error(`[ERROR] ${msg}`, ...args),
warn: (msg, ...args) => console.warn(`[WARN] ${msg}`, ...args)
};
// Load system prompt
let systemPrompt = '';
try {
systemPrompt = fs.readFileSync('/Users/benbasha/Development/ben/calude-code-loop/system_prompt.txt', 'utf8').trim();
} catch (error) {
logger.warn('Could not load system prompt:', error.message);
}
// Real Claude CLI utility
const executeClaudeCommand = async (messages, useClaudeLoop = false) => {
return new Promise((resolve, reject) => {
const args = ['-p'];
// Only use JSON format for claudeloop-local model
if (useClaudeLoop) {
args.push('--input-format', 'stream-json');
args.push('--output-format', 'stream-json');
args.push('--verbose');
}
const claude = spawn('claude', args, {
stdio: ['pipe', 'pipe', 'pipe'],
shell: true
});
let output = '';
let errorOutput = '';
// Prepare messages array with system prompt if needed
let messagesToSend = [...messages];
// Add system prompt as first message for claudeloop-local if not already present
if (useClaudeLoop && systemPrompt) {
const hasSystemMessage = messagesToSend.some(msg => msg.role === 'system');
if (!hasSystemMessage) {
messagesToSend.unshift({
role: 'user',
content: systemPrompt
});
}
}
// Send the message to Claude's stdin with error handling
try {
if (useClaudeLoop) {
// For JSON format, flatten all content from all messages into a single content array
const allContent = [];
for (const msg of messagesToSend) {
if (Array.isArray(msg.content)) {
allContent.push(...msg.content);
} else {
allContent.push({
type: 'text',
text: msg.content
});
}
}
const jsonMessage = JSON.stringify({
type: 'user',
message: {
role: 'user',
content: allContent.map(x => x.text).join("\n\n-----------\n\n")
}
}) + '\n';
if (!claude.stdin.destroyed) {
claude.stdin.write(jsonMessage);
}
} else {
// For non-JSON format, concatenate all messages with role context
let finalMessage = '';
// Add system prompt if it exists in messages or from systemPrompt variable
const systemMessage = messagesToSend.find(m => m.role === 'system');
if (systemMessage) {
const systemContent = Array.isArray(systemMessage.content)
? systemMessage.content.filter(item => item.type === 'text').map(item => item.text).join('\n')
: systemMessage.content;
finalMessage += systemContent + '\n\n';
} else if (systemPrompt) {
finalMessage += systemPrompt + '\n\n';
}
// Add conversation history
for (const msg of messagesToSend) {
if (msg.role !== 'system') {
const content = Array.isArray(msg.content)
? msg.content.filter(item => item.type === 'text').map(item => item.text).join('\n')
: msg.content;
finalMessage += `${msg.role}: ${content}\n\n`;
}
}
if (!claude.stdin.destroyed) {
claude.stdin.write(finalMessage);
}
}
if (!claude.stdin.destroyed) {
claude.stdin.end();
}
} catch (writeError) {
logger.error('Error writing to Claude CLI stdin:', writeError);
reject(new Error(`Failed to write to Claude CLI: ${writeError.message}`));
return;
}
// Handle stdin errors
claude.stdin.on('error', (stdinError) => {
if (stdinError.code !== 'EPIPE') {
logger.error('Claude CLI stdin error:', stdinError);
}
});
// Collect stdout
claude.stdout.on('data', (data) => {
output += data.toString();
});
// Collect stderr
claude.stderr.on('data', (data) => {
errorOutput += data.toString();
});
// Handle process completion
claude.on('close', (code) => {
if (code === 0) {
// For JSON output, parse the Claude streaming response into structured format
if (useClaudeLoop && output.trim()) {
try {
const lines = output.trim().split('\n');
const parsedResponse = {
messages: [],
finalResult: '',
toolCalls: []
};
// Parse all assistant messages and tool calls from the JSON stream
for (const line of lines) {
if (line.trim()) {
try {
const parsed = JSON.parse(line);
if (parsed.type === 'assistant' && parsed.message) {
// Extract text content and tool calls
const content = parsed.message.content || [];
for (const item of content) {
if (item.type === 'text' && item.text) {
parsedResponse.messages.push({
role: 'assistant',
content: item.text
});
} else if (item.type === 'tool_use') {
parsedResponse.toolCalls.push({
id: item.id,
type: 'function',
function: {
name: item.name,
arguments: JSON.stringify(item.input || {})
}
});
}
}
} else if (parsed.type === 'result' && parsed.result) {
parsedResponse.finalResult = parsed.result;
}
} catch (lineParseError) {
logger.warn('Failed to parse JSON line:', line.substring(0, 50));
}
}
}
resolve(parsedResponse);
} catch (parseError) {
// Fallback to raw output if JSON parsing fails
logger.warn('Failed to parse JSON output, using raw output:', parseError.message);
resolve(output.trim());
}
} else {
resolve(output.trim());
}
} else {
logger.error(`Claude CLI exited with code ${code}:`, errorOutput);
reject(new Error(`Claude CLI failed with code ${code}: ${errorOutput}`));
}
});
// Handle process errors
claude.on('error', (error) => {
logger.error('Claude CLI process error:', error);
reject(new Error(`Claude CLI process error: ${error.message}`));
});
});
};
// Create Express app
const app = express();
// Middleware
app.use(cors({
origin: '*',
methods: ['GET', 'POST', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'Accept', 'Origin'],
credentials: false
}));
app.use(express.json({ limit: '10mb' }));
// Request logging
app.use((req, res, next) => {
const requestId = `req-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;
logger.info(`[${requestId}] ${req.method} ${req.path}`);
logger.info(`[${requestId}] Origin: ${req.headers.origin || 'none'}`);
res.setHeader('X-Request-ID', requestId);
next();
});
// Simple auth middleware (accept any API key)
app.use('/v1', (req, res, next) => {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({
error: {
message: 'Invalid API key provided',
type: 'invalid_request_error',
param: null,
code: 'invalid_api_key'
}
});
}
next();
});
// Routes
app.get('/health', (req, res) => {
res.json({
status: 'ok',
timestamp: new Date().toISOString(),
server: 'Claude Autopilot Standalone API'
});
});
app.get('/v1/models', (req, res) => {
res.json({
object: 'list',
data: [
{
id: 'claudeloop-local',
object: 'model',
created: Math.floor(Date.now() / 1000),
owned_by: 'claudeloop',
permission: [],
root: 'claudeloop-local',
parent: null
},
{
id: 'claude-3-5-sonnet-20241022',
object: 'model',
created: Math.floor(Date.now() / 1000),
owned_by: 'anthropic',
permission: [],
root: 'claude-3-5-sonnet-20241022',
parent: null
},
// OpenAI-compatible model names (all map to claudeloop-local)
{
id: 'gpt-4',
object: 'model',
created: Math.floor(Date.now() / 1000),
owned_by: 'claudeloop',
permission: [],
root: 'gpt-4',
parent: null
},
{
id: 'gpt-4o',
object: 'model',
created: Math.floor(Date.now() / 1000),
owned_by: 'claudeloop',
permission: [],
root: 'gpt-4o',
parent: null
},
{
id: 'gpt-3.5-turbo',
object: 'model',
created: Math.floor(Date.now() / 1000),
owned_by: 'claudeloop',
permission: [],
root: 'gpt-3.5-turbo',
parent: null
}
]
});
});
app.post('/v1/chat/completions', async (req, res) => {
try {
const { model, messages, stream = false, max_tokens = 4096, temperature = 1.0 } = req.body;
if (!messages || !Array.isArray(messages) || messages.length === 0) {
return res.status(400).json({
error: {
message: 'Messages are required',
type: 'invalid_request_error',
param: 'messages',
code: null
}
});
}
// Get the last user message
const lastMessage = messages[messages.length - 1];
if (lastMessage.role !== 'user') {
return res.status(400).json({
error: {
message: 'Last message must be from user',
type: 'invalid_request_error',
param: 'messages',
code: null
}
});
}
// Check if using claudeloop-local model
const useClaudeLoop = model === 'claudeloop-local';
logger.info('Processing chat completion:', {
model,
messageCount: messages.length,
useClaudeLoop,
lastUserMessage: typeof lastMessage.content === 'string'
? lastMessage.content.substring(0, 100) + '...'
: (Array.isArray(lastMessage.content)
? lastMessage.content.filter(item => item.type === 'text').map(item => item.text).join(' ').substring(0, 100) + '...'
: '...')
});
if (stream) {
// Streaming response
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.setHeader('Transfer-Encoding', 'chunked');
const response = await executeClaudeCommand(messages, useClaudeLoop);
// Handle structured response for streaming
let finalText = '';
if (useClaudeLoop && typeof response === 'object' && response.finalResult) {
finalText = response.finalResult;
} else {
finalText = typeof response === 'string' ? response : String(response);
}
const words = finalText.split(' ');
for (let i = 0; i < words.length; i++) {
const chunk = {
id: `chatcmpl-${Date.now()}`,
object: 'chat.completion.chunk',
created: Math.floor(Date.now() / 1000),
model: model || 'claude-3-5-sonnet-20241022',
choices: [{
index: 0,
delta: {
content: words[i] + (i < words.length - 1 ? ' ' : '')
},
finish_reason: i === words.length - 1 ? 'stop' : null
}]
};
res.write(`data: ${JSON.stringify(chunk)}\n\n`);
await new Promise(resolve => setTimeout(resolve, 50)); // Small delay between words
}
res.write('data: [DONE]\n\n');
res.end();
} else {
// Non-streaming response
const response = await executeClaudeCommand(messages, useClaudeLoop);
// Handle structured response from Claude loop or simple text response
let choices = [];
let responseContent = '';
if (useClaudeLoop && typeof response === 'object' && response.messages) {
// Create choices for each message and tool calls
let choiceIndex = 0;
// Add text messages
for (const msg of response.messages) {
choices.push({
index: choiceIndex++,
message: {
role: msg.role,
content: msg.content
},
finish_reason: 'stop'
});
}
// Add tool calls as a separate choice if any exist
if (response.toolCalls && response.toolCalls.length > 0) {
choices.push({
index: choiceIndex++,
message: {
role: 'assistant',
content: null,
tool_calls: response.toolCalls
},
finish_reason: 'tool_calls'
});
}
// Use final result as the main content
responseContent = response.finalResult || (response.messages.length > 0 ? response.messages[response.messages.length - 1].content : 'No response');
} else {
// Simple text response
responseContent = typeof response === 'string' ? response : String(response);
choices = [{
index: 0,
message: {
role: 'assistant',
content: responseContent
},
finish_reason: 'stop'
}];
}
const completion = {
id: `chatcmpl-${Date.now()}`,
object: 'chat.completion',
created: Math.floor(Date.now() / 1000),
model: model || 'claude-3-5-sonnet-20241022',
choices: choices,
usage: {
"prompt_tokens": 28,
"completion_tokens": 10,
"total_tokens": 38,
"prompt_tokens_details": {
"cached_tokens": 0,
"audio_tokens": 0
},
"completion_tokens_details": {
"reasoning_tokens": 0,
"audio_tokens": 0,
"accepted_prediction_tokens": 0,
"rejected_prediction_tokens": 0
}
},
"service_tier": "default",
"system_fingerprint": "fp_a288987b44"
};
res.json(completion);
}
} catch (error) {
logger.error('Chat completion error:', error);
res.status(500).json({
error: {
message: 'Internal server error',
type: 'server_error',
code: 'internal_error'
}
});
}
});
// Error handling
app.use((err, req, res, next) => {
logger.error('API Error:', err);
res.status(500).json({
error: {
message: 'Internal server error',
type: 'server_error',
code: 'internal_error'
}
});
});
// 404 handler
app.use((req, res) => {
res.status(404).json({
error: {
message: 'Endpoint not found',
type: 'invalid_request_error',
code: 'not_found'
}
});
});
// Start server
const server = app.listen(PORT, () => {
console.log('🚀 Starting Claude Autopilot API Server...');
console.log(`📡 Port: ${PORT}`);
console.log('🔗 This server provides OpenAI-compatible API endpoints for Claude CLI');
console.log('');
console.log('✅ Server started successfully!');
console.log('');
console.log('🔧 Available endpoints:');
console.log(` GET http://localhost:${PORT}/health`);
console.log(` GET http://localhost:${PORT}/v1/models`);
console.log(` POST http://localhost:${PORT}/v1/chat/completions`);
console.log('');
console.log('🔑 Authentication: Use any API key in Authorization header');
console.log(' Example: Authorization: Bearer your-api-key-here');
console.log('');
console.log('🛑 Press Ctrl+C to stop the server');
});
// Graceful shutdown
const shutdown = () => {
console.log('\n⏹️ Shutting down server...');
server.close(() => {
console.log('✅ Server stopped');
process.exit(0);
});
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
server.on('error', (error) => {
if (error.code === 'EADDRINUSE') {
logger.error(`Port ${PORT} is already in use`);
process.exit(1);
} else {
logger.error('Server error:', error);
process.exit(1);
}
});