File size: 6,836 Bytes
d0e7441
 
 
 
 
 
 
ce1e741
d0e7441
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad3f70a
 
 
 
 
 
 
 
 
 
0a94192
 
 
2df27c8
 
 
 
0a94192
2df27c8
 
 
 
 
0a94192
2df27c8
d992176
0a94192
 
9625765
 
 
d0e7441
0a94192
d0e7441
 
 
9625765
d0e7441
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a94192
d0e7441
 
 
 
9625765
0b60eec
d0e7441
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
const express = require('express');
const axios = require('axios');
const bodyParser = require('body-parser');
const fs = require('fs').promises;
const path = require('path');

const app = express();
const port = process.env.PORT || 7860;

app.use(bodyParser.json());

const sessionFilePath = path.join(__dirname, 'session.json');
const SESSION_TIMEOUT = 5 * 60 * 1000;
let sessions = {};

// Data untuk statistik
let totalRequests = 0;
let requestTimestamps = [];

async function loadSessions() {
    try {
        const data = await fs.readFile(sessionFilePath, 'utf-8');
        sessions = JSON.parse(data);
    } catch (error) {
        if (error.code === 'ENOENT') {
            sessions = {};
        } else {
            console.error('Error reading session file:', error);
        }
    }
}

async function saveSessions() {
    try {
        await fs.writeFile(sessionFilePath, JSON.stringify(sessions, null, 2), 'utf-8');
    } catch (error) {
        console.error('Error writing to session file:', error);
    }
}

function cleanUpSessions() {
    const now = Date.now();
    for (const username in sessions) {
        if (sessions[username].lastAccess && (now - sessions[username].lastAccess > SESSION_TIMEOUT)) {
            delete sessions[username];
        }
    }
}

async function blackboxChat(content, username = null, prompt = null, webSearchMode = true) {
    const url = "https://www.blackbox.ai/api/chat";
    const headers = {
        "Accept": "*/*",
        "Accept-Language": "id-ID,en;q=0.5",
        "Referer": "https://www.blackbox.ai/",
        "Content-Type": "application/json",
        "Origin": "https://www.blackbox.ai",
        "Alt-Used": "www.blackbox.ai"
    };

    let messages;
    if (username) {
        cleanUpSessions();
        if (!sessions[username]) {
            sessions[username] = { messages: [], lastAccess: Date.now() };
        } else {
            sessions[username].lastAccess = Date.now();
        }
        sessions[username].messages.push({ role: "user", content });
        messages = sessions[username].messages;
    } else {
        messages = [{ role: "user", content }];
    }

    const data = {
        messages,
        previewToken: null,
        codeModelMode: true,
        agentMode: {},
        trendingAgentMode: {},
        isMicMode: false,
        userSystemPrompt: prompt || " and answers questions effectively. Keep a cool but supportive tone so users feel comfortable and appreciated. Be patient, empathetic and attentive to user needs, providing thoughtful and helpful responses. Make sure your way of speaking is indistinguishable from a human's, so that your presence feels natural and authentic. Use simple language, avoid technical jargon, remain consistent in providing solutions, and always be supportive and attentive. Show a humane attitude in every interaction and use Indonesian that is easy to understand.",
        maxTokens: 1024,
        webSearchMode: webSearchMode,
        promptUrls: "",
        isChromeExt: false,
        githubToken: null
    };

    try {
        const response = await axios.post(url, data, { headers });
        let result = response.data;

        // Existing replacement
        result = result.replace(/\$@\$.*?\$@\$|v=.*?(\s|$)/g, '').trim(); // Remove content between $~~~$ markers
        result = result.replace(/\$~~~\$.*?\$~~~\$/gs, '').trim();

        const sourcesRegex = /Sources:.*?(\n|$)/s;
        const sourcesMatch = result.match(sourcesRegex);
        let sourcesText = "";

        if (sourcesMatch) {
            sourcesText = sourcesMatch[0].trim();
            result = result.replace(sourcesRegex, '').trim();
        }

        if (sourcesText) {
            result = `${result}\n\n${sourcesText}`;
        }

        if (username) {
            sessions[username].messages.push({ role: "assistant", content: result });
            await saveSessions();
        }

        return result;
    } catch (error) {
        console.error("Error fetching data:", error);
        return "Maaf, terjadi kesalahan saat memproses permintaan Anda.";
    }
}

loadSessions().catch(error => console.error("Failed to load sessions:", error));
setInterval(cleanUpSessions, SESSION_TIMEOUT);

// Middleware untuk melacak total permintaan dan RPS
app.use((req, res, next) => {
    totalRequests++;
    const now = Date.now();
    requestTimestamps.push(now);
    requestTimestamps = requestTimestamps.filter(timestamp => now - timestamp <= 1000);
    next();
});

// Middleware untuk mencatat detail request
app.use((req, res, next) => {
    console.log(`Received ${req.method} request for ${req.url}`);
    console.log('Request Headers:', JSON.stringify(req.headers, null, 2));
    if (req.method === 'POST') {
        console.log('Request Body:', JSON.stringify(req.body, null, 2));
    }
    next();
});

app.post('/', async (req, res) => {
    const { content, user, prompt, webSearchMode } = req.body;
    if (!content) {
        return res.status(400).json({ error: "Content is required" });
    }

    try {
        const result = await blackboxChat(content, user, prompt, webSearchMode);
        res.json({ result });
    } catch (error) {
        console.error("Error:", error.message);
        res.status(500).json({ error: "Internal server error" });
    }
});

app.get('/api/ai', async (req, res) => {
    const { q } = req.query; // Mengambil query dari req.query, bukan req.body
    if (!q) {
        return res.status(400).json({ error: "Content is required" });
    }

    try {
        const result = await blackboxChat(q);
        res.json({ result });
    } catch (error) {
        console.error("Error:", error.message);
        res.status(500).json({ error: "Internal server error" });
    }
});

app.get('/api/stats', (req, res) => {
    const seconds = Number(process.uptime());
    const d = Math.floor(seconds / (3600 * 24));
    const h = Math.floor(seconds % (3600 * 24) / 3600);
    const m = Math.floor(seconds % 3600 / 60);
    const s = Math.floor(seconds % 60);

    const dDisplay = d > 0 ? d + (d == 1 ? " Hari, " : " Hari, ") : "";
    const hDisplay = h > 0 ? h + (h == 1 ? " Jam, " : " Jam, ") : "";
    const mDisplay = m > 0 ? m + (m == 1 ? " Menit, " : " Menit, ") : "";
    const sDisplay = s > 0 ? s + (s == 1 ? " Detik" : " Detik") : "";

    const runtime = dDisplay + hDisplay + mDisplay + sDisplay;
    const rps = requestTimestamps.length;

    res.json({
        rps: rps, // Requests Per Second
        totalRequests: totalRequests, // Total Requests
        runtime: runtime // Runtime in the desired format
    });
});

// Middleware ini harus didefinisikan setelah semua rute lainnya
app.use((req, res) => {
    res.sendFile(path.join(__dirname, 'index.html'));
});

app.listen(port, () => {
    console.log(`Server is listening at port:${port}`);
});