882 lines
41 KiB
TypeScript
882 lines
41 KiB
TypeScript
import { renderAdminLayout } from './layout';
|
|
|
|
export function renderLiveLogsPage(): string {
|
|
const extraStyles = `
|
|
.log-row-item {
|
|
padding: 0.65rem 0.75rem;
|
|
border-radius: 8px;
|
|
border: 1px solid var(--border-dark);
|
|
background: rgba(0, 0, 0, 0.15);
|
|
cursor: pointer;
|
|
transition: all 0.15s ease;
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.3rem;
|
|
}
|
|
body.light-theme .log-row-item {
|
|
background: rgba(0, 0, 0, 0.02);
|
|
border-color: var(--border-light);
|
|
}
|
|
.log-row-item:hover {
|
|
background: rgba(187, 134, 252, 0.08);
|
|
border-color: rgba(187, 134, 252, 0.4);
|
|
}
|
|
.log-row-item.active {
|
|
border-color: var(--primary-color);
|
|
background: rgba(187, 134, 252, 0.15);
|
|
box-shadow: 0 0 10px rgba(187, 134, 252, 0.2);
|
|
}
|
|
.code-box {
|
|
background: #0d0d0d;
|
|
color: #f1f1f1;
|
|
border: 1px solid rgba(255, 255, 255, 0.1);
|
|
border-radius: 8px;
|
|
padding: 0.75rem;
|
|
font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
|
|
font-size: 0.8rem;
|
|
overflow-x: auto;
|
|
white-space: pre-wrap;
|
|
word-break: break-word;
|
|
line-height: 1.45;
|
|
}
|
|
body.light-theme .code-box {
|
|
background: #f8f9fa;
|
|
color: #212529;
|
|
border-color: #dee2e6;
|
|
}
|
|
.chat-bubble {
|
|
border-radius: 8px;
|
|
padding: 0.65rem 0.85rem;
|
|
margin-bottom: 0.6rem;
|
|
font-size: 0.82rem;
|
|
line-height: 1.45;
|
|
}
|
|
.chat-bubble.user {
|
|
background: rgba(100, 181, 246, 0.12);
|
|
border-left: 3px solid var(--accent-info);
|
|
}
|
|
.chat-bubble.assistant {
|
|
background: rgba(3, 218, 198, 0.12);
|
|
border-left: 3px solid var(--accent-success);
|
|
}
|
|
.chat-bubble.system {
|
|
background: rgba(255, 183, 77, 0.12);
|
|
border-left: 3px solid var(--accent-warning);
|
|
}
|
|
|
|
/* Dedicated Inspector Sub-tabs (isolated from page navigation) */
|
|
.insp-tabs-bar {
|
|
display: flex;
|
|
gap: 0.5rem;
|
|
border-bottom: 1px solid var(--border-dark);
|
|
margin-bottom: 0.75rem;
|
|
overflow-x: auto;
|
|
}
|
|
body.light-theme .insp-tabs-bar {
|
|
border-bottom-color: var(--border-light);
|
|
}
|
|
.insp-tab-btn {
|
|
background: transparent;
|
|
border: none;
|
|
border-bottom: 2px solid transparent;
|
|
color: var(--muted-dark);
|
|
padding: 0.6rem 1.1rem;
|
|
font-size: 0.88rem;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 0.45rem;
|
|
transition: all 0.2s ease;
|
|
white-space: nowrap;
|
|
}
|
|
body.light-theme .insp-tab-btn {
|
|
color: var(--muted-light);
|
|
}
|
|
.insp-tab-btn:hover {
|
|
color: var(--text-dark);
|
|
}
|
|
body.light-theme .insp-tab-btn:hover {
|
|
color: var(--text-light);
|
|
}
|
|
.insp-tab-btn.active {
|
|
color: var(--primary-color);
|
|
border-bottom-color: var(--primary-color);
|
|
}
|
|
`;
|
|
|
|
const bodyHtml = `
|
|
<section id="live-logs-page">
|
|
<div class="section-header">
|
|
<div>
|
|
<h2 class="section-title">
|
|
<i class="fas fa-satellite-dish"></i>
|
|
<span>Live Request Log Watcher</span>
|
|
</h2>
|
|
<p class="card-subtitle">
|
|
Inspect live incoming + served requests in RAM (input, output, metadata, debug data). Flushes from memory when toggled off.
|
|
</p>
|
|
</div>
|
|
<div style="display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap;">
|
|
<div style="display: flex; align-items: center; gap: 0.5rem; background: rgba(0,0,0,0.25); border: 1px solid var(--border-dark); padding: 0.35rem 0.75rem; border-radius: 8px;">
|
|
<span style="font-size: 0.85rem; font-weight: 500;">Watcher:</span>
|
|
<button id="watcher-toggle-btn" class="nav-btn" style="padding: 0.35rem 0.85rem; font-size: 0.8rem;">
|
|
<i class="fas fa-play"></i> <span>Start Watching</span>
|
|
</button>
|
|
</div>
|
|
<div id="ram-badge" class="badge badge-info" style="font-size: 0.8rem; padding: 0.4rem 0.75rem;">
|
|
<i class="fas fa-memory"></i> <span id="ram-count-text">0 / 250 in RAM (0 KB)</span>
|
|
</div>
|
|
<button id="flush-watcher-btn" class="nav-btn" title="Flush all buffered requests from RAM">
|
|
<i class="fas fa-trash-alt"></i> <span>Flush Memory</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Filter bar -->
|
|
<div class="card" style="margin-bottom: 1rem; padding: 0.75rem 1rem;">
|
|
<div style="display: flex; gap: 1rem; align-items: center; flex-wrap: wrap;">
|
|
<div style="flex: 1; min-width: 220px; position: relative;">
|
|
<input type="text" id="live-search-input" class="form-input" placeholder="Search prompts, responses, request IDs..." style="padding-left: 2.2rem; margin: 0; width: 100%;">
|
|
<i class="fas fa-search" style="position: absolute; left: 0.85rem; top: 50%; transform: translateY(-50%); color: var(--muted-dark); font-size: 0.85rem;"></i>
|
|
</div>
|
|
<div style="display: flex; gap: 0.5rem; align-items: center;">
|
|
<label style="font-size: 0.8rem; color: var(--muted-dark);">Status:</label>
|
|
<select id="live-status-filter" class="form-select" style="width: auto; padding: 0.4rem 0.75rem; margin: 0;">
|
|
<option value="all">All Statuses</option>
|
|
<option value="completed">Success (Direct)</option>
|
|
<option value="fallback_completed">Success (Fallback)</option>
|
|
<option value="streaming">Streaming</option>
|
|
<option value="failed">Failed</option>
|
|
<option value="cancelled">Cancelled</option>
|
|
</select>
|
|
</div>
|
|
<div style="display: flex; gap: 0.5rem; align-items: center;">
|
|
<label style="font-size: 0.8rem; color: var(--muted-dark);">Model:</label>
|
|
<select id="live-model-filter" class="form-select" style="width: auto; padding: 0.4rem 0.75rem; margin: 0;">
|
|
<option value="all">All Models</option>
|
|
</select>
|
|
</div>
|
|
<button id="live-auto-scroll-btn" class="nav-btn" style="font-size: 0.75rem; padding: 0.4rem 0.75rem;" title="Auto-scroll to latest requests">
|
|
<i class="fas fa-arrow-down"></i> <span>Auto-Scroll: ON</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Split layout: Request List (left) + Detail Inspector (right) -->
|
|
<div style="display: grid; grid-template-columns: 380px 1fr; gap: 1rem; min-height: 600px;">
|
|
<!-- Request Stream List -->
|
|
<div class="card" style="padding: 0.75rem; display: flex; flex-direction: column;">
|
|
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.5rem; padding-bottom: 0.5rem; border-bottom: 1px solid var(--border-dark);">
|
|
<span style="font-size: 0.85rem; font-weight: 600;">Captured Requests (<span id="live-displayed-count">0</span>)</span>
|
|
<span id="live-connection-status" class="badge badge-warning" style="font-size: 0.7rem;"><i class="fas fa-circle-notch fa-spin"></i> Disconnected</span>
|
|
</div>
|
|
<div id="live-requests-list" style="overflow-y: auto; max-height: 650px; display: flex; flex-direction: column; gap: 0.5rem;">
|
|
<p id="live-empty-hint" style="color: var(--muted-dark); font-size: 0.85rem; text-align: center; padding: 2rem 1rem;">
|
|
Live Watcher is currently idle. Toggle Watcher ON to begin capturing requests in RAM.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Detail Inspector -->
|
|
<div class="card" style="padding: 1rem; display: flex; flex-direction: column;">
|
|
<div id="inspector-header" style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem; padding-bottom: 0.5rem; border-bottom: 1px solid var(--border-dark);">
|
|
<div>
|
|
<h3 style="font-size: 1rem; font-weight: 600; display: flex; align-items: center; gap: 0.5rem;">
|
|
<span id="inspector-title">Request Inspector</span>
|
|
<span id="inspector-status-badge"></span>
|
|
</h3>
|
|
<span id="inspector-subtitle" style="font-size: 0.8rem; color: var(--muted-dark);">Select a request from the list to inspect full RAM data</span>
|
|
</div>
|
|
<div id="inspector-actions" style="display: none; gap: 0.5rem;">
|
|
<button id="copy-log-json-btn" class="nav-btn" style="font-size: 0.75rem; padding: 0.35rem 0.65rem;" title="Copy entire inspection payload as JSON">
|
|
<i class="fas fa-copy"></i> <span>Copy JSON</span>
|
|
</button>
|
|
<button id="export-log-json-btn" class="nav-btn" style="font-size: 0.75rem; padding: 0.35rem 0.65rem;" title="Download JSON file">
|
|
<i class="fas fa-download"></i> <span>Export</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Inspector Sub-tabs (isolated class .insp-tab-btn, no .tab-btn overlap) -->
|
|
<div id="inspector-tabs" class="insp-tabs-bar" style="display: none;">
|
|
<button type="button" class="insp-tab-btn active" data-insp-tab="insp-input">
|
|
<i class="fas fa-sign-in-alt"></i> <span>Input</span>
|
|
</button>
|
|
<button type="button" class="insp-tab-btn" data-insp-tab="insp-output">
|
|
<i class="fas fa-sign-out-alt"></i> <span>Output</span>
|
|
</button>
|
|
<button type="button" class="insp-tab-btn" data-insp-tab="insp-metadata">
|
|
<i class="fas fa-info-circle"></i> <span>Metadata</span>
|
|
</button>
|
|
<button type="button" class="insp-tab-btn" data-insp-tab="insp-debug">
|
|
<i class="fas fa-bug"></i> <span>Debug & Failover Trace</span>
|
|
</button>
|
|
</div>
|
|
|
|
<!-- Inspector Content Pane -->
|
|
<div id="inspector-content" style="flex: 1; overflow-y: auto; max-height: 560px;">
|
|
<div id="inspector-empty-state" style="text-align: center; padding: 4rem 1rem; color: var(--muted-dark);">
|
|
<i class="fas fa-microscope" style="font-size: 2.5rem; margin-bottom: 1rem; opacity: 0.5;"></i>
|
|
<p>No request selected.</p>
|
|
<p style="font-size: 0.8rem; margin-top: 0.5rem;">Toggle Watcher ON and select an incoming request from the stream to inspect.</p>
|
|
</div>
|
|
<div id="insp-input" class="insp-pane" style="display: none;"></div>
|
|
<div id="insp-output" class="insp-pane" style="display: none;"></div>
|
|
<div id="insp-metadata" class="insp-pane" style="display: none;"></div>
|
|
<div id="insp-debug" class="insp-pane" style="display: none;"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
`;
|
|
|
|
const pageScript = `
|
|
let currentConfig = { providers: [], customModels: [] };
|
|
let isWatcherActive = false;
|
|
let liveLogs = [];
|
|
let selectedLog = null;
|
|
let activeInspectorTab = 'insp-input';
|
|
let autoScroll = true;
|
|
let liveLogsEventSource = null;
|
|
let liveSearchQuery = '';
|
|
let liveStatusFilter = 'all';
|
|
let liveModelFilter = 'all';
|
|
|
|
async function initPage() {
|
|
try {
|
|
const cfgRes = await apiRequest('/api/admin/config');
|
|
if (cfgRes.ok) {
|
|
currentConfig = await cfgRes.json();
|
|
}
|
|
} catch (err) {}
|
|
await updateWatcherStatus();
|
|
connectLiveLogsSSE();
|
|
updateModelFilterOptions();
|
|
}
|
|
|
|
async function updateWatcherStatus() {
|
|
try {
|
|
const res = await apiRequest('/api/admin/live-logs/state');
|
|
if (!res.ok) return;
|
|
const data = await res.json();
|
|
isWatcherActive = !!data.active;
|
|
|
|
const navBadge = document.getElementById('nav-live-badge');
|
|
if (navBadge) {
|
|
navBadge.textContent = isWatcherActive ? 'LIVE' : 'OFF';
|
|
navBadge.style.background = isWatcherActive ? 'var(--accent-success)' : 'rgba(255,255,255,0.1)';
|
|
navBadge.style.color = isWatcherActive ? '#000' : 'inherit';
|
|
}
|
|
|
|
const ramText = document.getElementById('ram-count-text');
|
|
if (ramText) {
|
|
ramText.textContent = data.bufferCount + ' / ' + data.maxCapacity + ' in RAM (' + data.memoryEstimateKb + ' KB)';
|
|
}
|
|
|
|
const toggleBtn = document.getElementById('watcher-toggle-btn');
|
|
if (toggleBtn) {
|
|
if (isWatcherActive) {
|
|
toggleBtn.className = 'nav-btn danger';
|
|
toggleBtn.innerHTML = '<i class="fas fa-stop"></i> <span>Stop Watching</span>';
|
|
} else {
|
|
toggleBtn.className = 'nav-btn primary';
|
|
toggleBtn.innerHTML = '<i class="fas fa-play"></i> <span>Start Watching</span>';
|
|
}
|
|
}
|
|
|
|
if (isWatcherActive && liveLogs.length === 0) {
|
|
const entriesRes = await apiRequest('/api/admin/live-logs/entries?limit=100');
|
|
if (entriesRes.ok) {
|
|
const entData = await entriesRes.json();
|
|
liveLogs = entData.entries || [];
|
|
if (selectedLog) {
|
|
const refreshed = liveLogs.find(l => l.id === selectedLog.id);
|
|
if (refreshed) selectedLog = refreshed;
|
|
}
|
|
renderLiveLogsList();
|
|
renderInspector();
|
|
updateModelFilterOptions();
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.error('Failed to update watcher status:', err);
|
|
}
|
|
}
|
|
|
|
async function toggleWatcher() {
|
|
const targetState = !isWatcherActive;
|
|
try {
|
|
const res = await apiRequest('/api/admin/live-logs/toggle', {
|
|
method: 'POST',
|
|
body: { enabled: targetState }
|
|
});
|
|
if (!res.ok) throw new Error('Failed to toggle watcher');
|
|
const data = await res.json();
|
|
isWatcherActive = !!data.active;
|
|
|
|
if (!isWatcherActive) {
|
|
liveLogs = [];
|
|
selectedLog = null;
|
|
renderLiveLogsList();
|
|
renderInspector();
|
|
showToast('Live Watcher stopped. All request logs flushed from RAM.', 'info');
|
|
} else {
|
|
showToast('Live Watcher started! Now capturing requests in RAM.', 'success');
|
|
}
|
|
|
|
await updateWatcherStatus();
|
|
} catch (err) {
|
|
showToast('Error toggling watcher: ' + err.message, 'error');
|
|
}
|
|
}
|
|
|
|
async function flushWatcher() {
|
|
try {
|
|
const res = await apiRequest('/api/admin/live-logs/flush', { method: 'POST' });
|
|
if (!res.ok) throw new Error('Failed to flush watcher logs');
|
|
liveLogs = [];
|
|
selectedLog = null;
|
|
renderLiveLogsList();
|
|
renderInspector();
|
|
await updateWatcherStatus();
|
|
showToast('RAM request logs purged successfully.', 'info');
|
|
} catch (err) {
|
|
showToast('Error flushing watcher: ' + err.message, 'error');
|
|
}
|
|
}
|
|
|
|
function connectLiveLogsSSE() {
|
|
if (liveLogsEventSource) return;
|
|
try {
|
|
liveLogsEventSource = new EventSource('/api/admin/live-logs/stream?api_key=' + encodeURIComponent(activeKey));
|
|
|
|
liveLogsEventSource.onopen = function() {
|
|
const statusElem = document.getElementById('live-connection-status');
|
|
if (statusElem) {
|
|
statusElem.className = 'badge badge-success';
|
|
statusElem.innerHTML = '<i class="fas fa-circle"></i> Connected';
|
|
}
|
|
};
|
|
|
|
liveLogsEventSource.onmessage = function(e) {
|
|
try {
|
|
const msg = JSON.parse(e.data);
|
|
handleLiveLogSSEMessage(msg);
|
|
} catch (err) {
|
|
console.error('Failed to parse live log SSE:', err);
|
|
}
|
|
};
|
|
|
|
liveLogsEventSource.onerror = function() {
|
|
const statusElem = document.getElementById('live-connection-status');
|
|
if (statusElem) {
|
|
statusElem.className = 'badge badge-warning';
|
|
statusElem.innerHTML = '<i class="fas fa-exclamation-triangle"></i> Reconnecting...';
|
|
}
|
|
};
|
|
} catch (err) {
|
|
console.error('Live logs SSE error:', err);
|
|
}
|
|
}
|
|
|
|
function handleLiveLogSSEMessage(msg) {
|
|
if (msg.type === 'snapshot') {
|
|
if (msg.state) {
|
|
isWatcherActive = msg.state.active;
|
|
}
|
|
if (Array.isArray(msg.entries)) {
|
|
liveLogs = msg.entries;
|
|
if (selectedLog) {
|
|
const refreshed = liveLogs.find(l => l.id === selectedLog.id);
|
|
if (refreshed) {
|
|
selectedLog = refreshed;
|
|
renderInspector();
|
|
}
|
|
}
|
|
renderLiveLogsList();
|
|
updateModelFilterOptions();
|
|
}
|
|
updateWatcherStatus();
|
|
} else if (msg.type === 'watcher_status') {
|
|
isWatcherActive = msg.data.active;
|
|
updateWatcherStatus();
|
|
} else if (msg.type === 'watcher_flushed') {
|
|
liveLogs = [];
|
|
selectedLog = null;
|
|
renderLiveLogsList();
|
|
renderInspector();
|
|
updateWatcherStatus();
|
|
} else if (msg.type === 'request_started') {
|
|
liveLogs.unshift(msg.data);
|
|
if (liveLogs.length > 250) liveLogs.length = 250;
|
|
renderLiveLogsList();
|
|
updateModelFilterOptions();
|
|
updateWatcherStatus();
|
|
} else if (msg.type === 'stream_chunk') {
|
|
const item = liveLogs.find(l => l.id === msg.requestId);
|
|
if (item) {
|
|
item.output.completionText = (item.output.completionText || '') + msg.chunkText;
|
|
item.durationMs = msg.durationMs || item.durationMs;
|
|
if (selectedLog && selectedLog.id === msg.requestId) {
|
|
selectedLog = item;
|
|
const outText = document.getElementById('insp-output-text');
|
|
if (outText) {
|
|
outText.textContent = item.output.completionText;
|
|
} else {
|
|
renderInspectorOutputPane(selectedLog);
|
|
}
|
|
}
|
|
}
|
|
} else if (msg.type === 'candidate_step') {
|
|
const item = liveLogs.find(l => l.id === msg.requestId);
|
|
if (item) {
|
|
item.debug.stepTraces.push(msg.data);
|
|
item.metadata.totalCandidatesAttempted = item.debug.stepTraces.length;
|
|
if (item.debug.stepTraces.length > 1) item.metadata.fallbackOccurred = true;
|
|
if (selectedLog && selectedLog.id === msg.requestId) {
|
|
selectedLog = item;
|
|
renderInspectorMetadataPane(item);
|
|
renderInspectorDebugPane(item);
|
|
}
|
|
}
|
|
} else if (msg.type === 'request_completed' || msg.type === 'request_failed' || msg.type === 'request_cancelled') {
|
|
const updated = msg.data;
|
|
const idx = liveLogs.findIndex(l => l.id === updated.id);
|
|
if (idx !== -1) {
|
|
liveLogs[idx] = updated;
|
|
} else {
|
|
liveLogs.unshift(updated);
|
|
}
|
|
renderLiveLogsList();
|
|
if (selectedLog && selectedLog.id === updated.id) {
|
|
selectedLog = updated;
|
|
renderInspector();
|
|
}
|
|
updateWatcherStatus();
|
|
}
|
|
}
|
|
|
|
function updateModelFilterOptions() {
|
|
const select = document.getElementById('live-model-filter');
|
|
if (!select) return;
|
|
const currentVal = select.value;
|
|
const models = new Set();
|
|
liveLogs.forEach(l => {
|
|
if (l.input && (l.input.requestedModel || l.input.model)) {
|
|
models.add(l.input.requestedModel || l.input.model);
|
|
}
|
|
});
|
|
if (currentConfig && Array.isArray(currentConfig.customModels)) {
|
|
currentConfig.customModels.forEach(m => models.add(m.name));
|
|
}
|
|
|
|
let opts = '<option value="all">All Models</option>';
|
|
models.forEach(m => {
|
|
opts += '<option value="' + escapeHtml(m) + '">' + escapeHtml(m) + '</option>';
|
|
});
|
|
select.innerHTML = opts;
|
|
select.value = currentVal || 'all';
|
|
}
|
|
|
|
function renderLiveLogsList() {
|
|
const container = document.getElementById('live-requests-list');
|
|
const countElem = document.getElementById('live-displayed-count');
|
|
if (!container) return;
|
|
|
|
const prevScrollTop = container.scrollTop;
|
|
|
|
let filtered = [...liveLogs];
|
|
|
|
if (liveStatusFilter !== 'all') {
|
|
filtered = filtered.filter(l => l.status === liveStatusFilter);
|
|
}
|
|
|
|
if (liveModelFilter !== 'all') {
|
|
filtered = filtered.filter(l => {
|
|
const reqM = (l.input && (l.input.requestedModel || l.input.model)) || '';
|
|
return reqM.toLowerCase() === liveModelFilter.toLowerCase();
|
|
});
|
|
}
|
|
|
|
if (liveSearchQuery.trim() !== '') {
|
|
const q = liveSearchQuery.toLowerCase().trim();
|
|
filtered = filtered.filter(l => {
|
|
if (l.id.toLowerCase().includes(q)) return true;
|
|
const reqM = (l.input && (l.input.requestedModel || l.input.model)) || '';
|
|
if (reqM.toLowerCase().includes(q)) return true;
|
|
if ((l.output && l.output.completionText || '').toLowerCase().includes(q)) return true;
|
|
if (Array.isArray(l.input?.messages)) {
|
|
for (const m of l.input.messages) {
|
|
const str = typeof m.content === 'string' ? m.content : JSON.stringify(m.content || '');
|
|
if (str.toLowerCase().includes(q)) return true;
|
|
}
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if (countElem) countElem.textContent = filtered.length;
|
|
container.innerHTML = '';
|
|
|
|
if (filtered.length === 0) {
|
|
container.innerHTML = '<p style="color: var(--muted-dark); font-size: 0.82rem; text-align: center; padding: 2rem 1rem;">' +
|
|
(liveLogs.length === 0
|
|
? 'No requests captured yet. Toggle Watcher ON and send API requests.'
|
|
: 'No requests match the current search/filter criteria.') +
|
|
'</p>';
|
|
return;
|
|
}
|
|
|
|
filtered.forEach(item => {
|
|
const row = document.createElement('div');
|
|
row.className = 'log-row-item' + (selectedLog && selectedLog.id === item.id ? ' active' : '');
|
|
|
|
let statusBadge = '';
|
|
if (item.status === 'streaming') {
|
|
statusBadge = '<span class="badge badge-info"><i class="fas fa-spinner fa-spin"></i> Stream</span>';
|
|
} else if (item.status === 'completed') {
|
|
statusBadge = '<span class="badge badge-success"><i class="fas fa-check"></i> ' + (item.statusCode || 200) + '</span>';
|
|
} else if (item.status === 'fallback_completed') {
|
|
statusBadge = '<span class="badge badge-primary"><i class="fas fa-random"></i> Fallback</span>';
|
|
} else if (item.status === 'failed') {
|
|
statusBadge = '<span class="badge badge-danger"><i class="fas fa-times"></i> ' + (item.statusCode || 500) + '</span>';
|
|
} else {
|
|
statusBadge = '<span class="badge badge-warning"><i class="fas fa-ban"></i> Cancel</span>';
|
|
}
|
|
|
|
const modelName = item.input.requestedModel || item.input.model || 'unknown-model';
|
|
const tokCount = item.output?.tokens?.totalTokens ? item.output.tokens.totalTokens + ' tok' : '';
|
|
|
|
row.innerHTML = '<div style="display: flex; justify-content: space-between; align-items: center;">' +
|
|
'<span><strong><code>' + escapeHtml(item.id.slice(0, 16)) + '...</code></strong></span>' +
|
|
statusBadge +
|
|
'</div>' +
|
|
'<div style="display: flex; justify-content: space-between; align-items: center; font-size: 0.78rem; color: var(--muted-dark);">' +
|
|
'<span>' + escapeHtml(modelName) + '</span>' +
|
|
'<span>' + item.durationMs + ' ms</span>' +
|
|
'</div>' +
|
|
'<div style="display: flex; justify-content: space-between; align-items: center; font-size: 0.72rem; color: var(--muted-dark);">' +
|
|
'<span>' + new Date(item.timestamp).toLocaleTimeString() + '</span>' +
|
|
'<span>' + tokCount + '</span>' +
|
|
'</div>';
|
|
|
|
row.addEventListener('click', () => {
|
|
selectedLog = item;
|
|
document.querySelectorAll('.log-row-item').forEach(r => r.classList.remove('active'));
|
|
row.classList.add('active');
|
|
renderInspector();
|
|
});
|
|
|
|
container.appendChild(row);
|
|
});
|
|
|
|
if (autoScroll && !selectedLog) {
|
|
container.scrollTop = 0;
|
|
} else {
|
|
container.scrollTop = prevScrollTop;
|
|
}
|
|
}
|
|
|
|
function renderInspector() {
|
|
const emptyState = document.getElementById('inspector-empty-state');
|
|
const tabs = document.getElementById('inspector-tabs');
|
|
const actions = document.getElementById('inspector-actions');
|
|
const title = document.getElementById('inspector-title');
|
|
const subtitle = document.getElementById('inspector-subtitle');
|
|
const badge = document.getElementById('inspector-status-badge');
|
|
|
|
if (!selectedLog) {
|
|
if (emptyState) emptyState.style.display = 'block';
|
|
if (tabs) tabs.style.display = 'none';
|
|
if (actions) actions.style.display = 'none';
|
|
if (title) title.textContent = 'Request Inspector';
|
|
if (subtitle) subtitle.textContent = 'Select a request from the list to inspect full RAM data';
|
|
if (badge) badge.innerHTML = '';
|
|
document.querySelectorAll('.insp-pane').forEach(p => p.style.display = 'none');
|
|
return;
|
|
}
|
|
|
|
if (emptyState) emptyState.style.display = 'none';
|
|
if (tabs) tabs.style.display = 'flex';
|
|
if (actions) actions.style.display = 'flex';
|
|
|
|
if (title) title.textContent = selectedLog.method + ' ' + selectedLog.path;
|
|
if (subtitle) subtitle.textContent = selectedLog.id + ' • ' + new Date(selectedLog.timestamp).toLocaleString() + ' • ' + selectedLog.durationMs + ' ms';
|
|
|
|
let statusBadge = '';
|
|
if (selectedLog.status === 'streaming') {
|
|
statusBadge = '<span class="badge badge-info"><i class="fas fa-spinner fa-spin"></i> Streaming</span>';
|
|
} else if (selectedLog.status === 'completed') {
|
|
statusBadge = '<span class="badge badge-success"><i class="fas fa-check"></i> 200 OK</span>';
|
|
} else if (selectedLog.status === 'fallback_completed') {
|
|
statusBadge = '<span class="badge badge-primary"><i class="fas fa-random"></i> Fallback Served</span>';
|
|
} else if (selectedLog.status === 'failed') {
|
|
statusBadge = '<span class="badge badge-danger"><i class="fas fa-times"></i> Error ' + (selectedLog.statusCode || 500) + '</span>';
|
|
} else {
|
|
statusBadge = '<span class="badge badge-warning"><i class="fas fa-ban"></i> Cancelled</span>';
|
|
}
|
|
if (badge) badge.innerHTML = statusBadge;
|
|
|
|
renderInspectorInputPane(selectedLog);
|
|
renderInspectorOutputPane(selectedLog);
|
|
renderInspectorMetadataPane(selectedLog);
|
|
renderInspectorDebugPane(selectedLog);
|
|
|
|
// Activate currently selected inspector sub-tab
|
|
document.querySelectorAll('.insp-tab-btn').forEach(b => {
|
|
b.classList.toggle('active', b.getAttribute('data-insp-tab') === activeInspectorTab);
|
|
});
|
|
document.querySelectorAll('.insp-pane').forEach(p => p.style.display = 'none');
|
|
const activePane = document.getElementById(activeInspectorTab);
|
|
if (activePane) activePane.style.display = 'block';
|
|
}
|
|
|
|
function renderInspectorInputPane(item) {
|
|
const pane = document.getElementById('insp-input');
|
|
if (!pane) return;
|
|
|
|
let messagesHtml = '';
|
|
if (Array.isArray(item.input.messages) && item.input.messages.length > 0) {
|
|
item.input.messages.forEach((msg, idx) => {
|
|
const role = (msg.role || 'user').toLowerCase();
|
|
const bubbleClass = 'chat-bubble ' + (role === 'assistant' ? 'assistant' : (role === 'system' ? 'system' : 'user'));
|
|
const roleBadgeClass = role === 'assistant' ? 'badge-success' : (role === 'system' ? 'badge-warning' : 'badge-info');
|
|
const content = typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content, null, 2);
|
|
|
|
messagesHtml += '<div class="' + bubbleClass + '">' +
|
|
'<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.35rem;">' +
|
|
'<span class="badge ' + roleBadgeClass + '" style="font-size: 0.68rem;">' + escapeHtml(role.toUpperCase()) + '</span>' +
|
|
'<span style="font-size: 0.7rem; color: var(--muted-dark);">#' + (idx + 1) + '</span>' +
|
|
'</div>' +
|
|
'<div style="white-space: pre-wrap; word-break: break-word;">' + escapeHtml(content) + '</div>' +
|
|
'</div>';
|
|
});
|
|
} else {
|
|
messagesHtml = '<p style="color: var(--muted-dark);">No messages array present in request payload.</p>';
|
|
}
|
|
|
|
pane.innerHTML = '<div style="margin-bottom: 1rem; background: rgba(0,0,0,0.15); border: 1px solid var(--border-dark); border-radius: 8px; padding: 0.75rem;">' +
|
|
'<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 0.75rem; font-size: 0.8rem;">' +
|
|
'<div><span style="color: var(--muted-dark);">Model:</span> <strong>' + escapeHtml(item.input.requestedModel || item.input.model || 'N/A') + '</strong></div>' +
|
|
'<div><span style="color: var(--muted-dark);">Stream:</span> <strong>' + (item.isStreaming ? 'Yes (SSE)' : 'No (JSON)') + '</strong></div>' +
|
|
'<div><span style="color: var(--muted-dark);">Temperature:</span> <strong>' + (item.input.temperature ?? 'default') + '</strong></div>' +
|
|
'<div><span style="color: var(--muted-dark);">Max Tokens:</span> <strong>' + (item.input.maxTokens ?? 'unlimited') + '</strong></div>' +
|
|
'<div><span style="color: var(--muted-dark);">Client IP:</span> <strong>' + escapeHtml(item.clientIp || '127.0.0.1') + '</strong></div>' +
|
|
'</div>' +
|
|
'</div>' +
|
|
'<h4 style="font-size: 0.85rem; font-weight: 600; margin-bottom: 0.5rem;"><i class="fas fa-comments"></i> Prompt Messages (' + (item.input.messages ? item.input.messages.length : 0) + ')</h4>' +
|
|
'<div>' + messagesHtml + '</div>';
|
|
}
|
|
|
|
function renderInspectorOutputPane(item) {
|
|
const pane = document.getElementById('insp-output');
|
|
if (!pane) return;
|
|
|
|
let contentHtml = '';
|
|
if (item.status === 'failed' || item.output.error) {
|
|
const errObj = item.output.error || {};
|
|
contentHtml = '<div style="background: rgba(207,102,121,0.12); border: 1px solid rgba(207,102,121,0.4); border-radius: 8px; padding: 1rem; color: var(--accent-danger); margin-bottom: 1rem;">' +
|
|
'<div style="font-weight: 600; margin-bottom: 0.4rem;"><i class="fas fa-exclamation-triangle"></i> Error Response (' + (item.statusCode || 500) + ')</div>' +
|
|
'<div style="font-size: 0.85rem; white-space: pre-wrap; font-family: monospace;">' + escapeHtml(errObj.message || JSON.stringify(errObj, null, 2)) + '</div>' +
|
|
'</div>';
|
|
} else if (item.output.completionText) {
|
|
contentHtml = '<div class="chat-bubble assistant">' +
|
|
'<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.35rem;">' +
|
|
'<span class="badge badge-success"><i class="fas fa-robot"></i> Assistant Response</span>' +
|
|
'<span style="font-size: 0.72rem; color: var(--muted-dark);">' + (item.output.finishReason ? 'finish: ' + item.output.finishReason : '') + '</span>' +
|
|
'</div>' +
|
|
'<div id="insp-output-text" style="white-space: pre-wrap; word-break: break-word; font-family: inherit;">' + escapeHtml(item.output.completionText) + '</div>' +
|
|
'</div>';
|
|
} else {
|
|
contentHtml = '<p style="color: var(--muted-dark); padding: 1.5rem 0;">' +
|
|
(item.status === 'streaming' ? 'Streaming response bytes in real time...' : 'No output text returned.') +
|
|
'</p>';
|
|
}
|
|
|
|
const tok = item.output?.tokens;
|
|
const tokBar = tok ? '<div style="background: rgba(0,0,0,0.15); border: 1px solid var(--border-dark); border-radius: 8px; padding: 0.6rem 0.75rem; margin-bottom: 0.75rem; display: flex; gap: 1.5rem; font-size: 0.8rem;">' +
|
|
'<div><span style="color: var(--muted-dark);">Prompt Tokens:</span> <strong>' + tok.promptTokens + '</strong></div>' +
|
|
'<div><span style="color: var(--muted-dark);">Completion Tokens:</span> <strong>' + tok.completionTokens + '</strong></div>' +
|
|
'<div><span style="color: var(--muted-dark);">Total Tokens:</span> <strong>' + tok.totalTokens + '</strong></div>' +
|
|
'</div>' : '';
|
|
|
|
pane.innerHTML = tokBar + contentHtml;
|
|
}
|
|
|
|
function renderInspectorMetadataPane(item) {
|
|
const pane = document.getElementById('insp-metadata');
|
|
if (!pane) return;
|
|
|
|
const served = item.metadata.servedBy;
|
|
const servedStr = served
|
|
? 'Tier ' + served.tierIndex + ' (' + escapeHtml(served.tierName) + ') • ' + escapeHtml(served.providerName) + ' (' + escapeHtml(served.providerType) + ') • ' + escapeHtml(served.model) + ' [Key: ' + escapeHtml(served.maskedKey) + ']'
|
|
: 'N/A';
|
|
|
|
pane.innerHTML = '<table class="detail-table">' +
|
|
'<tbody>' +
|
|
'<tr><td>Request ID</td><td><code>' + escapeHtml(item.id) + '</code></td></tr>' +
|
|
'<tr><td>Timestamp</td><td>' + new Date(item.timestamp).toISOString() + ' (' + new Date(item.timestamp).toLocaleString() + ')</td></tr>' +
|
|
'<tr><td>Total Latency</td><td><strong>' + item.durationMs + ' ms</strong></td></tr>' +
|
|
'<tr><td>Status Code</td><td>' + (item.statusCode || 200) + ' (' + escapeHtml(item.status) + ')</td></tr>' +
|
|
'<tr><td>Method / Path</td><td><code>' + item.method + ' ' + item.path + '</code></td></tr>' +
|
|
'<tr><td>Streaming Mode</td><td>' + (item.isStreaming ? 'Yes (SSE transfer)' : 'No (standard JSON)') + '</td></tr>' +
|
|
'<tr><td>Client IP Address</td><td>' + escapeHtml(item.clientIp || '127.0.0.1') + '</td></tr>' +
|
|
'<tr><td>Client User-Agent</td><td><small style="color: var(--muted-dark); word-break: break-word;">' + escapeHtml(item.userAgent || 'None') + '</small></td></tr>' +
|
|
'<tr><td>Requested Model</td><td><strong>' + escapeHtml(item.input.requestedModel || item.input.model || 'None') + '</strong></td></tr>' +
|
|
'<tr><td>Resolved Custom Model</td><td>' + escapeHtml(item.metadata.resolvedCustomModelName || item.metadata.resolvedCustomModelId || 'Direct Fallback') + '</td></tr>' +
|
|
'<tr><td>Fallback Occurred</td><td>' + (item.metadata.fallbackOccurred ? '<span class="badge badge-warning">YES</span>' : '<span class="badge badge-success">NO (Direct)</span>') + '</td></tr>' +
|
|
'<tr><td>Total Hops Tried</td><td>' + item.metadata.totalCandidatesAttempted + '</td></tr>' +
|
|
'<tr><td>Final Serving Key</td><td>' + servedStr + '</td></tr>' +
|
|
'</tbody>' +
|
|
'</table>';
|
|
}
|
|
|
|
function renderInspectorDebugPane(item) {
|
|
const pane = document.getElementById('insp-debug');
|
|
if (!pane) return;
|
|
|
|
let stepsHtml = '';
|
|
if (Array.isArray(item.debug.stepTraces) && item.debug.stepTraces.length > 0) {
|
|
stepsHtml += '<h4 style="font-size: 0.85rem; font-weight: 600; margin-bottom: 0.5rem;"><i class="fas fa-route"></i> Candidate Attempts & Failover Cascade</h4>' +
|
|
'<table class="detail-table" style="margin-bottom: 1rem;">' +
|
|
'<thead>' +
|
|
'<tr style="color: var(--muted-dark); text-align: left; font-size: 0.72rem;">' +
|
|
'<th>Hop</th><th>Tier / Provider</th><th>Model / Key</th><th>Duration</th><th>Result</th><th>Next Action</th>' +
|
|
'</tr>' +
|
|
'</thead>' +
|
|
'<tbody>';
|
|
item.debug.stepTraces.forEach(st => {
|
|
const statusCol = st.status === 'success' ? 'var(--accent-success)' : (st.status === 'rate_limited' ? 'var(--accent-warning)' : 'var(--accent-danger)');
|
|
stepsHtml += '<tr>' +
|
|
'<td>#' + st.stepIndex + '</td>' +
|
|
'<td>Tier ' + st.tierIndex + ' (' + escapeHtml(st.tierName) + ')<br><small style="color: var(--muted-dark);">' + escapeHtml(st.providerName) + '</small></td>' +
|
|
'<td>' + escapeHtml(st.model) + '<br><small style="color: var(--muted-dark);">' + escapeHtml(st.maskedKey) + '</small></td>' +
|
|
'<td>' + st.durationMs + ' ms</td>' +
|
|
'<td style="color: ' + statusCol + ';"><strong>' + (st.statusCode ? st.statusCode + ' ' : '') + escapeHtml(st.status) + '</strong>' + (st.errorMessage ? '<br><small style="font-size: 0.7rem;">' + escapeHtml(st.errorMessage) + '</small>' : '') + '</td>' +
|
|
'<td style="font-size: 0.72rem; color: var(--primary-color);">' + escapeHtml(st.nextAction || '') + '</td>' +
|
|
'</tr>';
|
|
});
|
|
stepsHtml += '</tbody></table>';
|
|
}
|
|
|
|
let eventsHtml = '';
|
|
if (Array.isArray(item.debug.events) && item.debug.events.length > 0) {
|
|
eventsHtml += '<h4 style="font-size: 0.85rem; font-weight: 600; margin-bottom: 0.5rem;"><i class="fas fa-list-ul"></i> Server Execution Events</h4>' +
|
|
'<div class="code-box" style="max-height: 200px;">';
|
|
item.debug.events.forEach(ev => {
|
|
const lvlColor = ev.level === 'error' ? 'var(--accent-danger)' : (ev.level === 'warn' ? 'var(--accent-warning)' : 'var(--accent-info)');
|
|
eventsHtml += '<div style="margin-bottom: 0.25rem;">' +
|
|
'<span style="color: var(--muted-dark); font-size: 0.72rem;">[' + new Date(ev.time).toLocaleTimeString() + ']</span> ' +
|
|
'<span style="color: ' + lvlColor + '; font-weight: 600; font-size: 0.72rem;">[' + ev.level.toUpperCase() + ']</span> ' +
|
|
'<span>' + escapeHtml(ev.message) + '</span>' +
|
|
'</div>';
|
|
});
|
|
eventsHtml += '</div>';
|
|
}
|
|
|
|
const keepaliveSummary = '<div style="background: rgba(0,0,0,0.15); border: 1px solid var(--border-dark); border-radius: 8px; padding: 0.6rem 0.75rem; margin-bottom: 0.75rem; display: flex; gap: 1.5rem; font-size: 0.8rem;">' +
|
|
'<div><span style="color: var(--muted-dark);">SSE Keep-Alive Pings:</span> <strong>' + (item.debug.keepAlivePingsCount || 0) + '</strong></div>' +
|
|
'<div><span style="color: var(--muted-dark);">Whitespace Keep-Alives:</span> <strong>' + (item.debug.nonStreamingWhitespaceCount || 0) + '</strong></div>' +
|
|
'</div>';
|
|
|
|
pane.innerHTML = keepaliveSummary + stepsHtml + eventsHtml;
|
|
}
|
|
|
|
// Inspector Sub-tab navigation (strictly scoped to .insp-tab-btn and .insp-pane)
|
|
document.querySelectorAll('.insp-tab-btn').forEach(btn => {
|
|
btn.addEventListener('click', (e) => {
|
|
e.stopPropagation();
|
|
const targetId = btn.getAttribute('data-insp-tab');
|
|
if (!targetId) return;
|
|
activeInspectorTab = targetId;
|
|
document.querySelectorAll('.insp-tab-btn').forEach(b => b.classList.remove('active'));
|
|
document.querySelectorAll('.insp-pane').forEach(p => p.style.display = 'none');
|
|
btn.classList.add('active');
|
|
const targetPane = document.getElementById(targetId);
|
|
if (targetPane) targetPane.style.display = 'block';
|
|
});
|
|
});
|
|
|
|
// Copy / Export Handlers
|
|
const copyBtn = document.getElementById('copy-log-json-btn');
|
|
if (copyBtn) {
|
|
copyBtn.addEventListener('click', () => {
|
|
if (!selectedLog) return;
|
|
navigator.clipboard.writeText(JSON.stringify(selectedLog, null, 2))
|
|
.then(() => showToast('Full request inspection copied to clipboard!', 'success'))
|
|
.catch(() => showToast('Failed to copy to clipboard', 'error'));
|
|
});
|
|
}
|
|
|
|
const exportBtn = document.getElementById('export-log-json-btn');
|
|
if (exportBtn) {
|
|
exportBtn.addEventListener('click', () => {
|
|
if (!selectedLog) return;
|
|
const blob = new Blob([JSON.stringify(selectedLog, null, 2)], { type: 'application/json' });
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = 'request-' + selectedLog.id + '.json';
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
document.body.removeChild(a);
|
|
URL.revokeObjectURL(url);
|
|
showToast('Exported request inspection as JSON.', 'success');
|
|
});
|
|
}
|
|
|
|
const toggleBtn = document.getElementById('watcher-toggle-btn');
|
|
if (toggleBtn) {
|
|
toggleBtn.addEventListener('click', toggleWatcher);
|
|
}
|
|
|
|
const flushBtn = document.getElementById('flush-watcher-btn');
|
|
if (flushBtn) {
|
|
flushBtn.addEventListener('click', flushWatcher);
|
|
}
|
|
|
|
const searchInput = document.getElementById('live-search-input');
|
|
if (searchInput) {
|
|
searchInput.addEventListener('input', (e) => {
|
|
liveSearchQuery = e.target.value;
|
|
renderLiveLogsList();
|
|
});
|
|
}
|
|
|
|
const statusFilterSelect = document.getElementById('live-status-filter');
|
|
if (statusFilterSelect) {
|
|
statusFilterSelect.addEventListener('change', (e) => {
|
|
liveStatusFilter = e.target.value;
|
|
renderLiveLogsList();
|
|
});
|
|
}
|
|
|
|
const modelFilterSelect = document.getElementById('live-model-filter');
|
|
if (modelFilterSelect) {
|
|
modelFilterSelect.addEventListener('change', (e) => {
|
|
liveModelFilter = e.target.value;
|
|
renderLiveLogsList();
|
|
});
|
|
}
|
|
|
|
const autoScrollBtn = document.getElementById('live-auto-scroll-btn');
|
|
if (autoScrollBtn) {
|
|
autoScrollBtn.addEventListener('click', () => {
|
|
autoScroll = !autoScroll;
|
|
autoScrollBtn.innerHTML = autoScroll
|
|
? '<i class="fas fa-arrow-down"></i> <span>Auto-Scroll: ON</span>'
|
|
: '<i class="fas fa-pause"></i> <span>Auto-Scroll: OFF</span>';
|
|
autoScrollBtn.className = autoScroll ? 'nav-btn' : 'nav-btn warning';
|
|
});
|
|
}
|
|
`;
|
|
|
|
return renderAdminLayout({
|
|
activePage: 'live_logs',
|
|
pageTitle: 'Live Logs',
|
|
bodyHtml,
|
|
pageScript,
|
|
extraStyles
|
|
});
|
|
}
|