Files
modelgrid/ts_web/app.js
T
jkunz 3b2a16b151
CI / Type Check & Lint (push) Successful in 8s
CI / Build Test (Current Platform) (push) Successful in 9s
CI / Build All Platforms (push) Successful in 39s
feat(ui): add browser console served by the daemon
Introduce a minimal operations console reachable on a dedicated UI port
(default 8081), kept separate from the OpenAI-compatible API port.

- ts_web/ holds the SPA shell (index.html, app.css, vanilla app.js) with
  sidebar navigation for all views from readme.ui.md and a working
  Overview page backed by a new /_ui/overview JSON endpoint.
- scripts/bundle-ui.ts walks ts_web/ and emits ts_bundled/bundle.ts, a
  single generated module exporting every asset as base64. Mirrors the
  @stack.gallery/registry pattern so deno compile binaries embed the
  entire UI with no external filesystem dependency at runtime.
- ts/ui/server.ts (UiServer) serves assets from either the bundled map
  (default, prod) or directly from ts_web/ on disk (dev). The source is
  chosen per-config and can be overridden by UI_ASSET_SOURCE=disk|bundle.
  SPA fallback routes unknown extensionless paths to index.html.
- IModelGridConfig.ui block with enabled/port/host/assetSource defaults;
  config init writes the block, the normalizer fills in defaults on
  load, and the daemon starts/stops the UI server alongside the API.
- deno.json gains a bundle:ui task; compile:all now depends on it so
  released binaries always contain an up-to-date bundle. dev task sets
  UI_ASSET_SOURCE=disk for hot edits.
- ts_bundled/ is gitignored (generated on build).
- test/ui-server.smoke.ts exercises bundle and disk modes end to end
  (index, app.js, SPA fallback, /_ui/overview, 404).
2026-04-21 10:01:44 +00:00

162 lines
5.2 KiB
JavaScript

// ModelGrid UI — vanilla client. Bundled into ts_bundled/bundle.ts for
// the single-binary build, or served from disk in dev mode.
const VIEWS = [
'overview',
'cluster',
'gpus',
'deployments',
'models',
'access',
'logs',
'metrics',
'settings',
];
const view = document.getElementById('view');
const nodeIdent = document.getElementById('node-ident');
const nodeVersion = document.getElementById('node-version');
function parseHash() {
const raw = location.hash.replace(/^#\/?/, '');
const [top = 'overview'] = raw.split('/').filter(Boolean);
return VIEWS.includes(top) ? top : 'overview';
}
function setActive(current) {
document.querySelectorAll('.nav-items a').forEach((el) => {
el.classList.toggle('active', el.dataset.view === current);
});
}
async function fetchHealth() {
const res = await fetch('/_ui/overview', { headers: { accept: 'application/json' } });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
}
function statusDot(status) {
const ok = status === 'ok';
const warn = status === 'degraded';
const cls = ok ? 'ok' : warn ? 'warn' : 'err';
return `<span class="status-dot ${cls}"></span>`;
}
async function renderOverview() {
view.innerHTML = `<h1>Overview</h1><div id="ovstate" class="placeholder">Loading…</div>`;
try {
const data = await fetchHealth();
const health = data.health;
const containers = health.containers || 0;
const models = health.models || 0;
const gpus = health.gpus || 0;
const uptime = health.uptime || 0;
const detailEntries = Object.entries(health.details?.containers || {});
const runningContainers = detailEntries.filter(([, v]) => v === 'healthy').length;
view.innerHTML = `
<h1>Overview</h1>
<div class="cards">
<div class="card">
<div class="card-label">Fleet</div>
<div class="card-value">${statusDot(health.status)}${health.status}</div>
<div class="card-sub">v${health.version} · up ${formatUptime(uptime)}</div>
</div>
<div class="card">
<div class="card-label">Deployments</div>
<div class="card-value">${runningContainers} / ${containers}</div>
<div class="card-sub">${containers === 0 ? 'no deployments' : `${runningContainers} healthy`}</div>
</div>
<div class="card">
<div class="card-label">GPUs</div>
<div class="card-value">${gpus}</div>
<div class="card-sub">${gpus === 0 ? 'no GPU detected' : 'detected'}</div>
</div>
<div class="card">
<div class="card-label">Models</div>
<div class="card-value">${models}</div>
<div class="card-sub">served via OpenAI API</div>
</div>
</div>
<h1 style="margin-top:24px">Deployments</h1>
${renderContainerTable(detailEntries)}
`;
if (data.node) {
nodeIdent.textContent = `${data.node.name} · ${data.node.role}`;
nodeVersion.textContent = `v${data.node.version}`;
}
} catch (err) {
view.innerHTML = `<h1>Overview</h1><div class="error">Failed to load: ${escapeHtml(String(err.message || err))}</div>`;
}
}
function renderContainerTable(entries) {
if (entries.length === 0) {
return `<div class="placeholder">No deployments configured. Add one with <code>modelgrid run &lt;model&gt;</code>.</div>`;
}
const rows = entries.map(([id, state]) => `
<tr>
<td>${escapeHtml(id)}</td>
<td>${statusDot(state === 'healthy' ? 'ok' : 'err')}${escapeHtml(state)}</td>
</tr>
`).join('');
return `<table><thead><tr><th>Container</th><th>Health</th></tr></thead><tbody>${rows}</tbody></table>`;
}
function renderPlaceholder(name) {
view.innerHTML = `
<h1>${name}</h1>
<div class="placeholder">
This view is part of the UI concept (see <code>readme.ui.md</code>) but is not implemented yet.
Use the CLI for now: <code>modelgrid ${cliHint(name)}</code>.
</div>
`;
}
function cliHint(view) {
const map = {
Cluster: 'cluster status',
GPUs: 'gpu list',
Deployments: 'ps',
Models: 'model list',
Access: 'config apikey list',
Logs: 'service logs',
Metrics: 'service status',
Settings: 'config show',
};
return map[view] || '--help';
}
function formatUptime(s) {
if (s < 60) return `${s}s`;
if (s < 3600) return `${Math.floor(s / 60)}m`;
if (s < 86400) return `${Math.floor(s / 3600)}h`;
return `${Math.floor(s / 86400)}d`;
}
function escapeHtml(s) {
return s.replace(/[&<>"']/g, (c) => ({
'&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;',
}[c]));
}
function route() {
const current = parseHash();
setActive(current);
switch (current) {
case 'overview': return renderOverview();
case 'cluster': return renderPlaceholder('Cluster');
case 'gpus': return renderPlaceholder('GPUs');
case 'deployments': return renderPlaceholder('Deployments');
case 'models': return renderPlaceholder('Models');
case 'access': return renderPlaceholder('Access');
case 'logs': return renderPlaceholder('Logs');
case 'metrics': return renderPlaceholder('Metrics');
case 'settings': return renderPlaceholder('Settings');
}
}
window.addEventListener('hashchange', route);
if (!location.hash) location.hash = '#/overview';
route();