Tafita1206 commited on
Commit
caa2c12
Β·
verified Β·
1 Parent(s): 8945b97

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +53 -57
server.js CHANGED
@@ -1,80 +1,76 @@
1
  const express = require('express');
2
- const { spawn } = require('child_process');
3
- const { createProxyMiddleware } = require('http-proxy-middleware');
4
- const path = require('path');
5
 
6
- const app = express();
7
  const PORT = 7860;
8
- const PROXY_PORT = 3000;
9
 
10
- // ─── Lance le proxy free-claude-code en arriΓ¨re-plan ───────────────────────
11
- const proxy = spawn('node', ['/app/proxy/index.js'], {
12
- env: {
13
- ...process.env,
14
- PORT: String(PROXY_PORT),
15
- NVIDIA_API_KEY: process.env.NVIDIA_API_KEY || ''
16
- },
17
- stdio: 'inherit'
18
- });
19
 
20
- proxy.on('error', (err) => console.error('Proxy error:', err));
21
- proxy.on('exit', (code) => console.log('Proxy exited with code:', code));
 
 
 
 
 
22
 
23
- // Attendre que le proxy dΓ©marre
24
- setTimeout(() => {
25
- console.log(`Proxy lancΓ© sur le port ${PROXY_PORT}`);
26
- }, 2000);
 
27
 
28
- // ─── Servir l'interface web ─────────────────────────────────────────────────
29
- app.use(express.static(path.join(__dirname, 'public')));
30
- app.use(express.json());
 
 
 
 
 
31
 
32
- // ─── Route API β€” relaie vers le proxy Claude ────────────────────────────────
33
  app.post('/api/chat', async (req, res) => {
 
 
 
 
 
 
 
 
34
  try {
35
- const response = await fetch(`http://localhost:${PROXY_PORT}/v1/messages`, {
36
  method: 'POST',
37
  headers: {
38
- 'Content-Type': 'application/json',
39
- 'x-api-key': 'fake-key',
40
- 'anthropic-version': '2023-06-01'
41
  },
42
  body: JSON.stringify({
43
- model: req.body.model || 'claude-sonnet-4-20250514',
44
- max_tokens: req.body.max_tokens || 2048,
45
- messages: req.body.messages,
46
- stream: req.body.stream || false
47
- })
 
48
  });
49
 
50
- if (req.body.stream) {
51
- res.setHeader('Content-Type', 'text/event-stream');
52
- res.setHeader('Cache-Control', 'no-cache');
53
- res.setHeader('Connection', 'keep-alive');
54
- response.body.pipeTo(new WritableStream({
55
- write(chunk) { res.write(chunk); }
56
- })).finally(() => res.end());
57
- } else {
58
- const data = await response.json();
59
- res.json(data);
60
  }
 
 
 
 
 
61
  } catch (err) {
62
- console.error('API error:', err);
63
  res.status(500).json({ error: err.message });
64
  }
65
  });
66
 
67
- // ─── Status endpoint ────────────────────────────────────────────────────────
68
- app.get('/api/status', (req, res) => {
69
- const key = process.env.NVIDIA_API_KEY;
70
- res.json({
71
- status: 'ok',
72
- proxy_port: PROXY_PORT,
73
- nvidia_key_set: !!(key && key.length > 10),
74
- models: ['Kimi K2', 'GLM 4.7', 'MiniMax M2', 'Devstral']
75
- });
76
- });
77
-
78
  app.listen(PORT, '0.0.0.0', () => {
79
- console.log(`βœ… Serveur dΓ©marrΓ© sur http://0.0.0.0:${PORT}`);
80
- });
 
 
 
1
  const express = require('express');
2
+ const path = require('path');
 
 
3
 
4
+ const app = express();
5
  const PORT = 7860;
 
6
 
7
+ app.use(express.json({ limit: '10mb' }));
8
+ app.use(express.static(path.join(__dirname, 'public')));
 
 
 
 
 
 
 
9
 
10
+ const MODEL_MAP = {
11
+ 'kimi-k2': 'moonshotai/kimi-k2-instruct',
12
+ 'glm-4': 'thudm/glm-4-9b-chat',
13
+ 'minimax-m2':'minimax/minimax-m2',
14
+ 'devstral': 'mistralai/devstral-small-2505',
15
+ 'default': 'moonshotai/kimi-k2-instruct',
16
+ };
17
 
18
+ function resolveModel(name) {
19
+ if (!name) return MODEL_MAP['default'];
20
+ const key = Object.keys(MODEL_MAP).find(k => name.toLowerCase().includes(k));
21
+ return key ? MODEL_MAP[key] : MODEL_MAP['default'];
22
+ }
23
 
24
+ app.get('/api/status', (_req, res) => {
25
+ const key = process.env.NVIDIA_API_KEY || '';
26
+ res.json({
27
+ status: 'ok',
28
+ nvidia_key_set: key.length > 10,
29
+ models: Object.keys(MODEL_MAP).filter(k => k !== 'default'),
30
+ });
31
+ });
32
 
 
33
  app.post('/api/chat', async (req, res) => {
34
+ const apiKey = process.env.NVIDIA_API_KEY;
35
+ if (!apiKey || apiKey.length < 10) {
36
+ return res.status(500).json({ error: 'NVIDIA_API_KEY manquante. Ajoute-la dans Settings > Secrets.' });
37
+ }
38
+
39
+ const { messages = [], model = 'kimi-k2', max_tokens = 2048 } = req.body;
40
+ const nvidiaModel = resolveModel(model);
41
+
42
  try {
43
+ const response = await fetch('https://integrate.api.nvidia.com/v1/chat/completions', {
44
  method: 'POST',
45
  headers: {
46
+ 'Content-Type': 'application/json',
47
+ 'Authorization': `Bearer ${apiKey}`,
 
48
  },
49
  body: JSON.stringify({
50
+ model: nvidiaModel,
51
+ messages,
52
+ max_tokens,
53
+ temperature: 0.7,
54
+ stream: false,
55
+ }),
56
  });
57
 
58
+ if (!response.ok) {
59
+ const err = await response.text();
60
+ return res.status(response.status).json({ error: `NVIDIA API ${response.status}: ${err}` });
 
 
 
 
 
 
 
61
  }
62
+
63
+ const data = await response.json();
64
+ const reply = data?.choices?.[0]?.message?.content || '';
65
+ res.json({ content: [{ type: 'text', text: reply }] });
66
+
67
  } catch (err) {
 
68
  res.status(500).json({ error: err.message });
69
  }
70
  });
71
 
 
 
 
 
 
 
 
 
 
 
 
72
  app.listen(PORT, '0.0.0.0', () => {
73
+ const key = process.env.NVIDIA_API_KEY || '';
74
+ console.log(`Server on port ${PORT}`);
75
+ console.log(`NVIDIA key: ${key.length > 10 ? 'OK' : 'MISSING'}`);
76
+ });