59 lines
No EOL
1.7 KiB
JavaScript
59 lines
No EOL
1.7 KiB
JavaScript
import express from 'express';
|
|
import cors from 'cors';
|
|
|
|
const app = express();
|
|
const PORT = 3001;
|
|
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
|
|
// Proxy endpoint for Claude API
|
|
app.post('/api/claude', async (req, res) => {
|
|
try {
|
|
const { messages, apiKey, model, maxTokens } = req.body;
|
|
|
|
console.log('=== Proxy received request ===');
|
|
console.log('Model:', model);
|
|
console.log('Max tokens:', maxTokens);
|
|
console.log('Messages count:', messages?.length);
|
|
console.log('API Key present:', !!apiKey);
|
|
|
|
const requestBody = {
|
|
model: model,
|
|
max_tokens: maxTokens,
|
|
messages: messages
|
|
};
|
|
|
|
console.log('Request body:', JSON.stringify(requestBody, null, 2));
|
|
|
|
const response = await fetch('https://api.anthropic.com/v1/messages', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'x-api-key': apiKey,
|
|
'anthropic-version': '2023-06-01'
|
|
},
|
|
body: JSON.stringify(requestBody)
|
|
});
|
|
|
|
console.log('Claude API response status:', response.status);
|
|
|
|
if (!response.ok) {
|
|
const error = await response.text();
|
|
console.error('Claude API error:', error);
|
|
return res.status(response.status).json({ error });
|
|
}
|
|
|
|
const data = await response.json();
|
|
console.log('Claude API success, response content length:', data.content?.[0]?.text?.length);
|
|
res.json(data);
|
|
} catch (error) {
|
|
console.error('Proxy error:', error);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Proxy server running on http://localhost:${PORT}`);
|
|
console.log('This proxy allows the frontend to communicate with the Anthropic API');
|
|
}); |