Norsec93 commited on
Commit
0bd0f9c
·
verified ·
1 Parent(s): 435aa06

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +33 -91
server.js CHANGED
@@ -3,98 +3,38 @@ const axios = require('axios');
3
  const cors = require('cors');
4
 
5
  const app = express();
6
- // Hardcoded to 8000 to remain strictly isolated from your other spaces
7
- const PORT = 8000;
8
 
9
- const LOGFARE_API_URL = 'https://logfare.ai/v1/chat/completions';
10
 
11
- // JANITOR COMPATIBILITY MAP: Janitor models on the left -> Logfare target on the right
12
  const MODEL_MAPPING = {
13
- 'gpt-4': 'deepseek-v4-pro',
14
- 'gpt-4o': 'deepseek-v4-flash',
15
- 'o3': 'mimo-v2.5-pro',
16
- 'o1': 'mimi-v2.5'
 
 
 
17
  };
18
 
19
  app.use(cors());
20
- app.use(express.json({ limit: '100mb' }));
21
- app.use(express.urlencoded({ limit: '100mb', extended: true }));
22
-
23
- // --- UNIVERSAL GET ROUTER ---
24
- // Catches any baseline connection/status checks from Janitor AI on any path
25
- app.get('*', (req, res) => {
26
- console.log(`[Validation Ping] GET request received at path: ${req.path}`);
27
- res.status(200).json({ status: "alive", message: "Port 8000 Logfare Proxy is fully operational." });
28
- });
29
-
30
- // --- UNIVERSAL POST ROUTER ---
31
- // Captures ALL incoming data regardless of how Janitor structures or misaligns the URL path
32
- app.post('*', async (req, res) => {
33
- console.log(`[Incoming Traffic] POST request received at path: ${req.path}`);
34
-
35
- // If Janitor is just sending an empty POST request to test the endpoint validity
36
- if (!req.body || !req.body.messages) {
37
- console.log(`[Validation Ping] Empty POST detected. Returning health check success.`);
38
- return res.status(200).json({ status: "alive", message: "Port 8000 endpoint reached." });
39
- }
40
-
41
- if (!process.env.LOGFARE_API_KEY) {
42
- return res.status(500).json({
43
- error: { message: "Configuration Error: LOGFARE_API_KEY secret is missing in HF Settings." }
44
- });
45
- }
46
 
 
47
  try {
48
- // Enforce Janitor GPT compatibility shift
49
- const inboundModel = req.body.model || 'gpt-4';
50
- const targetModel = MODEL_MAPPING[inboundModel] || 'deepseek-v4-pro';
51
 
52
- console.log(`[Model Resolver] Routing: ${inboundModel} -> ${targetModel}`);
53
-
54
- // Clean and combine chat array structure
55
- const sanitizedMessages = [];
56
- let combinedSystemContent = "";
57
-
58
- (req.body.messages || []).forEach(msg => {
59
- let role = msg.role;
60
- if (role === 'example_user') role = 'user';
61
- if (role === 'example_assistant') role = 'assistant';
62
-
63
- const content = typeof msg.content === 'string' ? msg.content.trim() : "";
64
- if (!content) return;
65
-
66
- if (role === 'system') {
67
- combinedSystemContent += (combinedSystemContent ? "\n" : "") + content;
68
- } else if (role === 'user' || role === 'assistant') {
69
- if (sanitizedMessages.length > 0 && sanitizedMessages[sanitizedMessages.length - 1].role === role) {
70
- sanitizedMessages[sanitizedMessages.length - 1].content += "\n" + content;
71
- } else {
72
- sanitizedMessages.push({ role, content });
73
- }
74
- }
75
- });
76
-
77
- if (combinedSystemContent) {
78
- sanitizedMessages.unshift({ role: 'system', content: combinedSystemContent });
79
- }
80
-
81
- const pristineBody = {
82
- model: targetModel,
83
- messages: sanitizedMessages,
84
- stream: req.body.stream ?? true
85
- };
86
-
87
- if (typeof req.body.temperature === 'number') pristineBody.temperature = req.body.temperature;
88
- if (typeof req.body.top_p === 'number') pristineBody.top_p = req.body.top_p;
89
- if (typeof req.body.max_tokens === 'number') pristineBody.max_tokens = req.body.max_tokens;
90
 
91
  const response = await axios({
92
  method: 'post',
93
- url: LOGFARE_API_URL,
94
- data: pristineBody,
95
  headers: {
96
  'Content-Type': 'application/json',
97
- 'Authorization': `Bearer ${process.env.LOGFARE_API_KEY.trim()}`
98
  },
99
  responseType: 'stream'
100
  });
@@ -104,20 +44,22 @@ app.post('*', async (req, res) => {
104
 
105
  } catch (error) {
106
  const statusCode = error.response ? error.response.status : 500;
107
- console.error(`[Pipeline Outage] Logfare downstream returned status: ${statusCode}`);
108
-
109
- if (!res.headersSent) {
110
- return res.status(statusCode).json({
111
- error: {
112
- message: `Logfare pipeline rejected request with status ${statusCode}. Verify your account balance.`,
113
- type: "logfare_proxy_failure",
114
- code: statusCode
115
- }
116
- });
117
- }
118
  }
119
  });
120
 
121
- app.listen(PORT, '0.0.0.0', () => {
122
- console.log(`Bulletproof Logfare Proxy listening explicitly on isolated port ${PORT}`);
 
 
 
 
123
  });
 
3
  const cors = require('cors');
4
 
5
  const app = express();
6
+ const PORT = process.env.PORT || 3000;
 
7
 
8
+ const TARGET_URL = 'https://api.us-west-2.modal.direct/v1/chat/completions';
9
 
 
10
  const MODEL_MAPPING = {
11
+ 'gpt-4': 'zai-org/GLM-5.2-FP8',
12
+ 'gpt-4o': 'zai-org/GLM-5.2-FP8',
13
+ 'gpt-3.5-turbo': 'zai-org/GLM-5.2-FP8',
14
+ 'claude-3-opus': 'zai-org/GLM-5.2-FP8',
15
+ 'glm-5': 'zai-org/GLM-5.2-FP8',
16
+ 'glm-5.2': 'zai-org/GLM-5.2-FP8',
17
+ 'zai-org/GLM-5.2': 'zai-org/GLM-5.2-FP8'
18
  };
19
 
20
  app.use(cors());
21
+ app.use(express.json({ limit: '50mb' }));
22
+ app.use(express.urlencoded({ limit: '50mb', extended: true }));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ app.post('/v1/chat/completions', async (req, res) => {
25
  try {
26
+ const requestedModel = req.body.model;
27
+ const targetModel = MODEL_MAPPING[requestedModel] || 'zai-org/GLM-5.2-FP8';
 
28
 
29
+ const modifiedBody = { ...req.body, model: targetModel };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  const response = await axios({
32
  method: 'post',
33
+ url: TARGET_URL,
34
+ data: modifiedBody,
35
  headers: {
36
  'Content-Type': 'application/json',
37
+ 'Authorization': req.headers['authorization'] || `Bearer ${process.env.MODAL_API_KEY || ''}`
38
  },
39
  responseType: 'stream'
40
  });
 
44
 
45
  } catch (error) {
46
  const statusCode = error.response ? error.response.status : 500;
47
+ console.error(`[Proxy Error] Status Code: ${statusCode} | Message: ${error.message}`);
48
+ if (res.headersSent) return res.end();
49
+ return res.status(statusCode).json({
50
+ error: {
51
+ message: `Upstream error (${statusCode}): ${error.message}`,
52
+ type: "proxy_internal_error",
53
+ code: statusCode
54
+ }
55
+ });
 
 
56
  }
57
  });
58
 
59
+ app.all('*', (req, res) => {
60
+ res.status(404).json({ error: { message: `Endpoint ${req.path} not found`, code: 404 } });
61
+ });
62
+
63
+ app.listen(PORT, () => {
64
+ console.log(`OpenAI to Modal GLM-5.2 Proxy live on port ${PORT}`);
65
  });