File size: 11,889 Bytes
7dbf588
38f089d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edfae64
38f089d
 
 
7dbf588
edfae64
38f089d
 
 
edfae64
 
38f089d
edfae64
 
 
 
38f089d
 
 
 
 
 
 
 
2952353
38f089d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2952353
38f089d
 
2952353
38f089d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dbf588
2952353
38f089d
 
 
 
 
edfae64
38f089d
edfae64
 
 
38f089d
 
 
2952353
38f089d
2952353
 
38f089d
 
2952353
 
 
 
 
38f089d
 
 
 
 
 
 
2952353
edfae64
38f089d
 
 
 
 
2952353
7dbf588
2952353
 
 
38f089d
2952353
38f089d
 
 
 
 
 
 
 
 
 
 
 
 
 
edfae64
7dbf588
38f089d
edfae64
38f089d
 
 
 
 
 
 
 
7dbf588
38f089d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edfae64
38f089d
 
7dbf588
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<!-- Build Trigger: 1 -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CLIProxyAPI Status</title>
    <style>
        :root {
            --bg-primary: #0f1117;
            --bg-secondary: #1e212b;
            --text-primary: #e2e8f0;
            --text-secondary: #94a3b8;
            --accent: #3b82f6;
            --border: #334155;
            --success: #22c55e;
            --warning: #f59e0b;
            --error: #ef4444;
        }
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background: var(--bg-primary);
            color: var(--text-primary);
            margin: 0;
            padding: 20px;
            line-height: 1.5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        h1, h2 { color: var(--text-primary); }
        .card {
            background: var(--bg-secondary);
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
            border: 1px solid var(--border);
        }
        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid var(--border);
        }
        th { color: var(--text-secondary); font-weight: 600; }
        .badge {
            padding: 4px 8px;
            border-radius: 9999px;
            font-size: 12px;
            font-weight: 600;
        }
        .badge-success { background: rgba(34, 197, 94, 0.1); color: var(--success); }
        .badge-warning { background: rgba(245, 158, 11, 0.1); color: var(--warning); }
        .badge-error { background: rgba(239, 68, 68, 0.1); color: var(--error); }
        .btn {
            background: var(--accent);
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 6px;
            cursor: pointer;
            font-weight: 600;
        }
        .btn:hover { opacity: 0.9; }
        .btn-secondary {
            background: transparent;
            border: 1px solid var(--border);
            color: var(--text-secondary);
        }
        .btn-secondary:hover { border-color: var(--text-primary); color: var(--text-primary); }
        .flex-between {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        
        /* Login Modal - Visible by default for immediate auth */
        .modal-overlay {
            position: fixed;
            top: 0; left: 0; right: 0; bottom: 0;
            background: rgba(0,0,0,0.9);
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 1000;
            opacity: 1;
            transition: opacity 0.3s ease-in-out;
        }
        .modal-overlay.hidden {
            opacity: 0;
            pointer-events: none;
            display: none;
        }
        .modal {
            background: var(--bg-secondary);
            padding: 30px;
            border-radius: 12px;
            width: 100%;
            max-width: 400px;
            border: 1px solid var(--border);
            box-shadow: 0 10px 25px rgba(0,0,0,0.5);
        }
        .input-group {
            margin-bottom: 20px;
        }
        .input-group label {
            display: block;
            margin-bottom: 8px;
            color: var(--text-secondary);
        }
        .input-group input {
            width: 100%;
            padding: 10px;
            background: var(--bg-primary);
            border: 1px solid var(--border);
            border-radius: 6px;
            color: var(--text-primary);
            box-sizing: border-box;
        }
        .error-msg {
            color: var(--error);
            font-size: 14px;
            margin-top: 10px;
            display: none;
        }
    </style>
</head>
<body>
    <!-- Login Modal -->
    <div id="login-modal" class="modal-overlay">
        <div class="modal">
            <h2 style="margin-top: 0">Authentication Required</h2>
            <p style="color: var(--text-secondary)">Please enter your management secret key.</p>
            <form id="login-form">
                <div class="input-group">
                    <label for="mgmt-key">Management Key</label>
                    <input type="password" id="mgmt-key" placeholder="Enter key..." required autocomplete="current-password">
                </div>
                <div id="login-error" class="error-msg">Invalid key or connection failed</div>
                <button type="submit" class="btn" style="width: 100%">Connect</button>
            </form>
        </div>
    </div>

    <div class="container">
        <div class="flex-between">
            <h1>System Status</h1>
            <div style="display: flex; gap: 10px;">
                <button class="btn" onclick="fetchData()">Refresh</button>
                <button class="btn btn-secondary" onclick="handleLogout()">Logout</button>
            </div>
        </div>

        <div class="grid">
            <div class="card">
                <h2>System Info</h2>
                <div id="system-info">Loading...</div>
            </div>
            <div class="card">
                <h2>Usage Stats</h2>
                <div id="usage-stats">Loading...</div>
            </div>
        </div>

        <div class="card">
            <h2>API Keys & Providers</h2>
            <div id="providers-list">Loading...</div>
        </div>

        <div class="card">
            <h2>Recent Activity</h2>
            <div id="activity-log">Loading...</div>
        </div>
    </div>

    <script>
        // Serving from /v0/management/status.html
        const API_BASE = "/v0/management"; 

        function getKey() {
            return localStorage.getItem("managementKey");
        }

        function init() {
            const key = getKey();
            if (key) {
                document.getElementById('login-modal').classList.add('hidden');
                fetchData();
            }
        }

        document.getElementById('login-form').addEventListener('submit', async function(e) {
            e.preventDefault();
            const keyInput = document.getElementById('mgmt-key');
            const key = keyInput.value.trim();
            if (!key) return;

            const btn = e.target.querySelector('button');
            const originalText = btn.innerText;
            btn.innerText = "Checking...";
            btn.disabled = true;

            try {
                const res = await fetch(API_BASE + "/config", {
                    headers: { "Authorization": "Bearer " + key }
                });
                
                if (res.ok) {
                    localStorage.setItem("managementKey", key);
                    localStorage.setItem("isLoggedIn", "true");
                    document.getElementById('login-modal').classList.add('hidden');
                    fetchData();
                } else {
                    showLoginError("Invalid key");
                }
            } catch (err) {
                console.error(err);
                showLoginError("Connection failed");
            } finally {
                btn.innerText = originalText;
                btn.disabled = false;
            }
        });

        function showLoginError(msg) {
            const el = document.getElementById('login-error');
            el.innerText = msg;
            el.style.display = 'block';
        }

        function handleLogout() {
            localStorage.removeItem("managementKey");
            localStorage.removeItem("isLoggedIn");
            location.reload();
        }

        async function fetchData() {
            const key = getKey();
            if (!key) return;

            const headers = { "Authorization": "Bearer " + key };

            try {
                const [configRes, usageRes] = await Promise.all([
                    fetch(API_BASE + "/config", { headers }),
                    fetch(API_BASE + "/usage", { headers })
                ]);

                if (configRes.status === 401 || usageRes.status === 401) {
                    handleLogout();
                    return;
                }

                const config = await configRes.json();
                const usage = await usageRes.json();

                renderSystemInfo(config);
                renderUsageStats(usage);
                renderProviders(config, usage);
                renderActivity(usage);
            } catch (error) {
                console.error("Error fetching data:", error);
                document.getElementById("system-info").innerHTML = `<div class="badge badge-error">Connection Error</div>`;
            }
        }

        function renderSystemInfo(config) {
            const html = `
                <p><strong>Proxy URL:</strong> ${config.proxyUrl || "Direct"}</p>
                <p><strong>Debug Mode:</strong> <span class="badge ${config.debug ? "badge-warning" : "badge-success"}">${config.debug ? "Enabled" : "Disabled"}</span></p>
                <p><strong>Routing:</strong> ${config.routingStrategy || "Round Robin"}</p>
                <p><strong>Free APIs:</strong> <span class="badge ${config.freeapis?.enabled ? "badge-success" : "badge-error"}">${config.freeapis?.enabled ? "Enabled" : "Disabled"}</span></p>
            `;
            document.getElementById("system-info").innerHTML = html;
        }

        function renderUsageStats(usage) {
            const totalRequests = usage.total_requests || 0;
            const totalTokens = usage.total_tokens || 0;
            
            const html = `
                <p><strong>Total Requests:</strong> ${totalRequests.toLocaleString()}</p>
                <p><strong>Total Tokens:</strong> ${totalTokens.toLocaleString()}</p>
            `;
            document.getElementById("usage-stats").innerHTML = html;
        }

        function renderProviders(config, usage) {
            let html = "<table><thead><tr><th>Provider / Model</th><th>Type</th><th>Key Configured</th></tr></thead><tbody>";
            
            if (config.openaiCompatibility) {
                config.openaiCompatibility.forEach(p => {
                    html += `<tr>
                        <td>${p.name}</td>
                        <td><span class="badge badge-success">OpenAI Compat</span></td>
                        <td>${p.apiKeyEntries?.length || 0} keys</td>
                    </tr>`;
                });
            }

            if (config.geminiApiKeys) {
                html += `<tr>
                    <td>Gemini (Google)</td>
                    <td><span class="badge badge-success">Native</span></td>
                    <td>${config.geminiApiKeys.length} keys</td>
                </tr>`;
            }

            if (config.freeapis && config.freeapis.enabled) {
                 html += `<tr>
                    <td>Free APIs (g4f/Cohere)</td>
                    <td><span class="badge badge-warning">Proxy</span></td>
                    <td>Automatic</td>
                </tr>`;
            }

            html += "</tbody></table>";
            document.getElementById("providers-list").innerHTML = html;
        }

        function renderActivity(usage) {
            document.getElementById("activity-log").innerHTML = "<p>Check logs for detailed activity.</p>";
        }

        document.addEventListener('DOMContentLoaded', init);
    </script>
</body>
</html>