Spaces:
Sleeping
Sleeping
File size: 6,831 Bytes
1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 1063df3 0c03a43 | 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 | // ==================== NAVIGATION ====================
function showSection(sectionId) {
document.querySelectorAll('.section').forEach(el => el.classList.remove('active'));
document.querySelectorAll('.nav-links li').forEach(el => el.classList.remove('active'));
document.getElementById(sectionId).classList.add('active');
event.target.classList.add('active');
if(sectionId === 'dashboard') {
loadStats();
loadLogs();
}
if(sectionId === 'list') loadApis();
if(sectionId === 'keys') loadKeys();
}
// ==================== STATS ====================
async function loadStats() {
const res = await fetch('/api/cms/stats', { credentials: 'include' });
const data = await res.json();
document.getElementById('stat-calls').innerText = data.total_calls;
document.getElementById('stat-tokens').innerText = data.total_tokens;
document.getElementById('stat-cost').innerText = '$' + data.total_cost.toFixed(4);
document.getElementById('stat-apis').innerText = data.active_apis;
}
// ==================== CREATE API ====================
document.getElementById('create-form').addEventListener('submit', async (e) => {
e.preventDefault();
const payload = {
route: document.getElementById('api-route').value,
model: document.getElementById('api-model').value,
prompt: document.getElementById('api-prompt').value
};
const res = await fetch('/api/cms/create', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(payload),
credentials: 'include'
});
if(res.ok) {
alert('API Created! You can now POST to: /user-api/' + payload.route);
document.getElementById('create-form').reset();
showSection('list');
} else {
const data = await res.json();
alert('Error: ' + data.message);
}
});
// ==================== LOGS ====================
async function loadLogs() {
const res = await fetch('/api/cms/logs', { credentials: 'include' });
const logs = await res.json();
const container = document.getElementById('logs-container');
if (logs.length === 0) {
container.innerHTML = '<p class="sub-text">No API calls yet</p>';
return;
}
let html = '<table style="width:100%; font-size:0.85rem;">';
html += '<tr><th>Time</th><th>Tokens</th><th>Latency</th><th>Cost</th></tr>';
logs.slice(0, 10).forEach(log => {
html += `
<tr>
<td>${log.timestamp.split(' ')[1]}</td>
<td>${log.tokens_used}</td>
<td>${log.latency}s</td>
<td>$${log.cost}</td>
</tr>
`;
});
html += '</table>';
container.innerHTML = html;
}
// ==================== API LIST ====================
async function loadApis() {
const res = await fetch('/api/cms/list', { credentials: 'include' });
const apis = await res.json();
const tbody = document.getElementById('api-table-body');
tbody.innerHTML = '';
apis.forEach(api => {
const row = `
<tr>
<td><span class="endpoint-url">/user-api/${api.route}</span></td>
<td>${api.model}</td>
<td>${api.total_calls}</td>
<td>
<button onclick="testApi('${api.route}')" style="padding:5px; cursor:pointer;">Test</button>
<button onclick="deleteApi(${api.id})" style="padding:5px; cursor:pointer; color:red;">Delete</button>
</td>
</tr>
`;
tbody.innerHTML += row;
});
}
async function deleteApi(id) {
if(confirm('Are you sure you want to delete this API?')) {
await fetch(`/api/cms/delete/${id}`, {
method: 'DELETE',
credentials: 'include'
});
loadApis();
loadStats();
}
}
// ==================== API KEYS ====================
async function loadKeys() {
const res = await fetch('/api/cms/keys', { credentials: 'include' });
const keys = await res.json();
const tbody = document.getElementById('keys-table-body');
tbody.innerHTML = '';
keys.forEach(key => {
const row = `
<tr>
<td>${key.name}</td>
<td><code style="background:#eee; padding:2px 6px; font-size:0.75rem;">${key.key}</code></td>
<td>${key.usage_count}</td>
<td><span style="color:${key.is_active ? 'green' : 'red'}">● ${key.is_active ? 'Active' : 'Inactive'}</span></td>
<td>
<button onclick="toggleKey(${key.id})" style="padding:5px; cursor:pointer;">${key.is_active ? 'Deactivate' : 'Activate'}</button>
<button onclick="deleteKey(${key.id})" style="padding:5px; cursor:pointer; color:red;">Delete</button>
</td>
</tr>
`;
tbody.innerHTML += row;
});
}
async function createKey() {
const name = document.getElementById('key-name').value || 'Unnamed Key';
const res = await fetch('/api/cms/keys', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name}),
credentials: 'include'
});
const data = await res.json();
if (res.ok) {
alert('New API Key Generated:\n\n' + data.key + '\n\nSave this key! It won\'t be shown again.');
document.getElementById('key-name').value = '';
loadKeys();
} else {
alert('Error: ' + data.message);
}
}
async function toggleKey(id) {
await fetch(`/api/cms/keys/${id}/toggle`, {
method: 'POST',
credentials: 'include'
});
loadKeys();
}
async function deleteKey(id) {
if(confirm('Delete this API Key?')) {
await fetch(`/api/cms/keys/${id}`, {
method: 'DELETE',
credentials: 'include'
});
loadKeys();
}
}
// ==================== EXPORT ====================
function exportLogs() {
window.open('/api/cms/export', '_blank');
}
// ==================== TEST API ====================
async function testApi(route) {
const res = await fetch(`/user-api/${route}`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({test: "data"}),
credentials: 'include'
});
const data = await res.json();
alert("Response:\n" + JSON.stringify(data, null, 2));
}
// ==================== INITIAL LOAD ====================
loadStats(); |