Delete server.js
Browse files
server.js
DELETED
|
@@ -1,125 +0,0 @@
|
|
| 1 |
-
const express = require('express');
|
| 2 |
-
const cors = require('cors');
|
| 3 |
-
|
| 4 |
-
const app = express();
|
| 5 |
-
|
| 6 |
-
// Enable global CORS to let Janitor AI handshake effortlessly
|
| 7 |
-
app.use(cors());
|
| 8 |
-
app.use(express.json({ limit: '100mb' }));
|
| 9 |
-
app.use(express.urlencoded({ limit: '100mb', extended: true }));
|
| 10 |
-
// Target Endpoint for DaSong API
|
| 11 |
-
const DASONG_URL = 'https://api.ai1833.shop/v1/chat/completions';
|
| 12 |
-
|
| 13 |
-
// Map Janitor's incoming models to the specific New-API dashboard options
|
| 14 |
-
const MODEL_MAPPING = {
|
| 15 |
-
'gpt-4': 'deepseek-v4-pro',
|
| 16 |
-
'gpt-4o': 'deepseek-v4-pro',
|
| 17 |
-
'o1': 'deepseek-3.2',
|
| 18 |
-
'o3':'z-ai/glm-5.2'
|
| 19 |
-
};
|
| 20 |
-
|
| 21 |
-
// Health check route
|
| 22 |
-
app.get('*', (req, res) => {
|
| 23 |
-
res.json({ status: "alive", message: "DaSong API Node.js Proxy Bridge Active on 7860." });
|
| 24 |
-
});
|
| 25 |
-
|
| 26 |
-
// Main Reverse Proxy Handler
|
| 27 |
-
app.post('*', async (req, res) => {
|
| 28 |
-
try {
|
| 29 |
-
const body = req.body;
|
| 30 |
-
|
| 31 |
-
if (!body || !body.messages) {
|
| 32 |
-
return res.json({ status: "alive", message: "Connection baseline stable." });
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
// Pull the sk- token from Hugging Face Environment Secrets
|
| 36 |
-
const apiKey = (process.env.DASONG_API_KEY || '').trim();
|
| 37 |
-
if (!apiKey) {
|
| 38 |
-
return res.status(500).json({ error: "Configuration Error: DASONG_API_KEY secret is missing in Space settings." });
|
| 39 |
-
}
|
| 40 |
-
|
| 41 |
-
const inboundModel = body.model || 'gpt-4';
|
| 42 |
-
const targetModel = MODEL_MAPPING[inboundModel] || 'deepseek-v4-pro';
|
| 43 |
-
|
| 44 |
-
// --- Context Sanitization Layer ---
|
| 45 |
-
let sanitizedMessages = [];
|
| 46 |
-
let combinedSystemContent = "";
|
| 47 |
-
|
| 48 |
-
for (let msg of body.messages || []) {
|
| 49 |
-
let role = msg.role;
|
| 50 |
-
if (['example_user', 'user'].includes(role)) role = 'user';
|
| 51 |
-
if (['example_assistant', 'assistant'].includes(role)) role = 'assistant';
|
| 52 |
-
|
| 53 |
-
let content = typeof msg.content === 'string' ? msg.content.trim() : '';
|
| 54 |
-
if (!content) continue;
|
| 55 |
-
|
| 56 |
-
if (role === 'system') {
|
| 57 |
-
combinedSystemContent += combinedSystemContent ? "\n" + content : content;
|
| 58 |
-
} else if (['user', 'assistant'].includes(role)) {
|
| 59 |
-
// Squash consecutive identical roles to prevent parsing validation rejections
|
| 60 |
-
if (sanitizedMessages.length > 0 && sanitizedMessages[sanitizedMessages.length - 1].role === role) {
|
| 61 |
-
sanitizedMessages[sanitizedMessages.length - 1].content += "\n" + content;
|
| 62 |
-
} else {
|
| 63 |
-
sanitizedMessages.push({ role, content });
|
| 64 |
-
}
|
| 65 |
-
}
|
| 66 |
-
}
|
| 67 |
-
|
| 68 |
-
if (combinedSystemContent) {
|
| 69 |
-
sanitizedMessages.unshift({ role: 'system', content: combinedSystemContent });
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
// Re-assemble standard format
|
| 73 |
-
const pristineBody = {
|
| 74 |
-
model: targetModel,
|
| 75 |
-
messages: sanitizedMessages,
|
| 76 |
-
stream: body.stream !== undefined ? body.stream : true
|
| 77 |
-
};
|
| 78 |
-
|
| 79 |
-
// Forward optional safety/generation variables
|
| 80 |
-
if (typeof body.temperature === 'number') pristineBody.temperature = body.temperature;
|
| 81 |
-
if (typeof body.top_p === 'number') pristineBody.top_p = body.top_p;
|
| 82 |
-
if (typeof body.max_tokens === 'number') pristineBody.max_tokens = body.max_tokens;
|
| 83 |
-
|
| 84 |
-
console.log(`[Proxy Log] Forwarding request to DaSong API for model: ${targetModel}`);
|
| 85 |
-
|
| 86 |
-
// Upstream execution
|
| 87 |
-
const response = await fetch(DASONG_URL, {
|
| 88 |
-
method: 'POST',
|
| 89 |
-
headers: {
|
| 90 |
-
'Authorization': `Bearer ${apiKey}`,
|
| 91 |
-
'Content-Type': 'application/json'
|
| 92 |
-
},
|
| 93 |
-
body: JSON.stringify(pristineBody)
|
| 94 |
-
});
|
| 95 |
-
|
| 96 |
-
if (!response.ok) {
|
| 97 |
-
const errorText = await response.text();
|
| 98 |
-
console.error(`[Upstream Error] Status: {response.status} | Body: ${errorText.slice(0, 500)}`);
|
| 99 |
-
return res.status(response.status).send(`Upstream provider returned an error: ${response.status}`);
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
// Setup streaming response headers back to Janitor AI
|
| 103 |
-
res.setHeader('Content-Type', response.headers.get('content-type') || 'text/event-stream');
|
| 104 |
-
res.setHeader('Cache-Control', 'no-cache');
|
| 105 |
-
res.setHeader('Connection', 'keep-alive');
|
| 106 |
-
|
| 107 |
-
// Stream the incoming chunks out natively
|
| 108 |
-
for await (const chunk of response.body) {
|
| 109 |
-
res.write(chunk);
|
| 110 |
-
}
|
| 111 |
-
res.end();
|
| 112 |
-
|
| 113 |
-
} catch (err) {
|
| 114 |
-
console.error(`[Network Exception]: ${err.message}`);
|
| 115 |
-
if (!res.headersSent) {
|
| 116 |
-
res.status(500).send(`Proxy network loop error: ${err.message}`);
|
| 117 |
-
}
|
| 118 |
-
}
|
| 119 |
-
});
|
| 120 |
-
|
| 121 |
-
// Force explicit port binding for Hugging Face's container system
|
| 122 |
-
const PORT = 7860;
|
| 123 |
-
app.listen(PORT, '0.0.0.0', () => {
|
| 124 |
-
console.log(`OG Node Proxy running smoothly on native port ${PORT}`);
|
| 125 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|