Files
alzeo/dashboard/static/js/agent.js
T
Hasan BasbunarandClaude Opus 5.5 06b3391421 cockpit on a phone: the rail answers a tap, fullscreen covers the head, the screen's edges are kept
On a phone no rail item could be tapped (found on the reference box's phone,
2026-09-30). .shell carried z-index:1, which made it a stacking context: the
drawer inside it (z-index 60) stacked as a whole at 1, under the veil its own
opening draws (body::after, 55). Every tap landed on the veil, whose click closes
the drawer, so the view never changed. The same trap held the Agent's fullscreen
frame under the head, over its own exit button, on every device; the view's entry
animation, left filling in both directions, kept a second stacking context after
it ended. Neither z-index nor fill mode is there now, the drawer sits over its
veil and both over the dock, and fullscreen drops the entry animation that would
trap the frame for half a second.

The drawer was also as tall as its items: the desktop rule's align-self:start
made the fixed drawer size to its content instead of stretching between top and
bottom, so on a short screen (a phone on its side) it never scrolled and its last
four items sat below the screen. It stretches and scrolls now.

The page asks for the whole screen (viewport-fit=cover) and kept none of what an
iPhone reserves: the head, the rail, the page, the dock, the toasts, the sheets
and the fullscreen frame now keep clear of the notch in landscape and of the home
indicator, with opencode inside the frame held to the safe area.

Smaller, found on the way with the live box on four phone sizes: the inline Agent
frame was sized on a guess of 150 px above it and left opencode's composer below
the fold on a phone, a tablet and a laptop (it now fills what is left, or takes a
screen and scrolls under the head when too little is); the request log put each
request's outcome three columns past the edge of a phone (one card per request
now), and the Library's wide tables stack into labelled cards; long facts were
cut with no way to read them on a finger (they wrap); every dense table's first
cell sat 12 px right of its header (a padding shorthand of equal specificity);
the proxy's state pill stretched across its row; empty tags drew empty pills; the
lane rack and the Lanes view called the last recorded boot's pool "this boot"
beside a live 491,136 (400,384); the drawer had no Escape and a closed dialog left
the focus on the body.

Held by: touch-check.mjs, new and in CI, drives a cockpit of its own with real
touch events on five touch screens and a laptop: 244/244, and 114/242 on the
previous revision (every rail item covered on every touch device, fullscreen on
all six). mobile-check.mjs now taps instead of calling click() from script and
checks both Agent modes: 236/236 against the live box. Page tests for the
stacking rules, the focus, the cell labels and the live pool, each failing on the
previous revision.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 17:40:53 +02:00

169 lines
11 KiB
JavaScript

"use strict";
/* Agent: opencode's own web interface, framed from the relay behind this login.
The relay lives on this same host (cookies ignore ports), so the frame carries the
cockpit session and opencode never asks for its own password. The frame is created
the first time the view opens and then kept: leaving the view must not lose a session. */
const AG = {mounted: false, wasReady: null};
// An explicit fullscreen choice wins on every device; with none stored, a hand-held
// viewport gets fullscreen, where the head above the frame is a third of the screen.
// The choice made on this page holds even where storage throws (private mode).
let agentMaxChoice = null;
function verCmp(a, b){
const x = String(a).split('.').map(Number), y = String(b).split('.').map(Number);
for (let i = 0; i < Math.max(x.length, y.length); i++){ const d = (x[i] || 0) - (y[i] || 0); if (d) return d; }
return 0;
}
function agentMaxDefault(){
if (agentMaxChoice !== null) return agentMaxChoice;
let pref = null;
try { pref = localStorage.getItem('cockpit.agent.max'); } catch { /* storage may be unavailable */ }
if (pref === '1') return true;
if (pref === '0') return false;
return window.matchMedia('(max-width:980px)').matches;
}
const agentUrl = () => F.agent && F.agent.relay && F.agent.relay.port ? `http://${location.hostname}:${F.agent.relay.port}/` : null;
const agentReady = () => !!(F.agent && F.agent.enabled && F.agent.relay && F.agent.relay.listening && F.agent.server && F.agent.server.healthy);
function agentMessage(text, cmd){
show('ag-msg', true); setText('ag-msg-text', text);
const c = $('ag-cmd'); c.hidden = !cmd; if (cmd) c.textContent = cmd;
}
function mountAgent(){
if (!agentReady()) return;
if (!AG.mounted){
const f = document.createElement('iframe');
f.id = 'ag-iframe'; f.title = 'opencode'; f.src = agentUrl();
f.setAttribute('allow', 'clipboard-read; clipboard-write'); f.setAttribute('referrerpolicy', 'no-referrer');
$('ag-frame').append(f); $('ag-frame').classList.add('mounted'); AG.mounted = true;
if (activeView === 'agent') fitAgentFrame(); // once, when the frame first exists: never on a refresh
}
show('ag-msg', false);
}
function reloadAgent(){ const f = $('ag-iframe'); if (f) f.src = agentUrl(); else mountAgent(); }
// Never cover the reason the panel is empty: fullscreen over an explanation is a blank screen.
function applyAgentMax(){
if (!agentMaxDefault() || document.body.classList.contains('agentmax')) return;
if (!agentReady() || !$('ag-note').hidden || !$('ag-msg').hidden) return;
setAgentMax(true, false);
}
// The inline frame fills the screen below where it really starts, so opencode's composer,
// at its foot, is on screen. It was sized on a guess of 150 px above it, which left the
// composer under the fold on a phone, a tablet and a laptop (2026-09-30). When too little
// room is left under the view's buttons (a phone on its side), the frame is a screen tall
// and the page brings it under the head instead.
function fitAgentFrame(scroll = true){
const f = $('ag-frame');
if (!f || !f.classList.contains('mounted') || document.body.classList.contains('agentmax') || activeView !== 'agent') return;
const vh = window.innerHeight, head = $('spine').getBoundingClientRect().height;
const top = f.getBoundingClientRect().top + window.scrollY, room = Math.floor(vh - top - 16);
f.classList.add('fitted');
if (room >= vh * 0.6){ f.style.setProperty('--agh', room + 'px'); return; }
f.style.setProperty('--agh', Math.max(200, Math.floor(vh - head - 32)) + 'px');
if (scroll && f.scrollIntoView) f.scrollIntoView({block: 'start'});
}
function setAgentMax(on, persist = true){
document.body.classList.toggle('agentmax', on);
const b = $('ag-max'); if (b){ setText(b, on ? 'Exit fullscreen' : 'Fullscreen'); b.setAttribute('aria-pressed', String(on)); }
if (on){ mountAgent(); agexitPlace(); } else if (activeView === 'agent') fitAgentFrame();
if (persist){ agentMaxChoice = on; try { localStorage.setItem('cockpit.agent.max', on ? '1' : '0'); } catch { /* private mode */ } }
}
on('agent', d => {
if (!d.enabled){
cap('ag-cap', 'not installed', '');
setText('ag-line', 'One install on the box adds opencode here, behind this login.');
['ag-open', 'ag-reload', 'ag-max', 'ag-restart'].forEach(id => show(id, false));
show('ag-note', false);
if (d.opencode_found === null) agentMessage(`opencode is not installed on this box, and this view runs it. On the box, re-run the installer: it installs the opencode this repo tests${d.pinned ? ` (${d.pinned})` : ''}, then this view.`,
((F.config || {}).terminal_only || {}).update_stack || 'cd ~/dgx-spark-qwen38 && ./install.sh');
else agentMessage('The Agent view is not installed on this cockpit. opencode is on the box; to add the view, run on the box:', ((F.config || {}).terminal_only || {}).install_agent || 'dashboard/install-agent.sh');
badge('agent', '', ''); return;
}
const r = d.relay || {}, sv = d.server || {}, u = d.unit || {};
const ready = agentReady(), unitOn = u.active === 'active';
const [txt, kind] = ready ? ['ready', 'ok'] : !r.listening ? ['relay waiting', 'warn'] : !unitOn ? [u.active === 'failed' ? 'server failed' : 'server stopped', 'err'] : ['server starting', 'warn'];
cap('ag-cap', txt, kind, ready ? true : 'live');
const parts = [];
if (sv.version) parts.push('opencode ' + sv.version);
if (d.binary && sv.version && d.binary !== sv.version) parts.push(`binary ${d.binary} installed, restart to serve it`);
if (d.pinned && sv.version && sv.version !== d.pinned) parts.push(verCmp(sv.version, d.pinned) < 0 ? `this repo tests ${d.pinned}: ./install.sh brings it in line` : `newer than the ${d.pinned} this repo tests`);
if (d.auto_live != null) parts.push(d.auto_live ? 'tool calls run without asking' : 'asks before risky tool calls');
if (d.auto != null && d.auto_live != null && d.auto !== d.auto_live) parts.push('permission mode changed in the unit, restart to apply');
if (!ready && unitOn && sv.error) parts.push(sv.error);
setText('ag-line', parts.join(', '));
show('ag-open', ready); show('ag-reload', ready); show('ag-max', ready); show('ag-restart', !!d.unit_installed);
if (agentUrl()) $('ag-open').href = agentUrl();
// the relay answers on one address and the session cookie lives on the host the
// browser typed: both have to be the same name for the frame to load
const note = $('ag-note');
if (r.listening && r.bind && location.hostname !== r.bind){
note.hidden = false;
note.textContent = `You reached the cockpit as ${location.hostname}; the agent relay answers on ${r.bind} only. If the panel stays blank, open http://${r.bind}:${location.port || 80}/#agent instead.`;
} else note.hidden = true;
if (ready){
if (activeView === 'agent') mountAgent();
if (AG.wasReady === false && AG.mounted) reloadAgent(); // the server came back
if (activeView === 'agent') applyAgentMax();
} else {
agentMessage(!r.listening ? `The relay is not listening yet: ${r.error || 'waiting for its address'}.`
: !unitOn ? `The opencode server is ${u.active === 'failed' ? 'failed' : 'stopped'}. Restart it from here or read its journal in Logs.`
: `The opencode server is not answering yet${sv.error ? ` (${sv.error})` : ''}. The panel reconnects by itself.`, null);
}
AG.wasReady = ready;
badge('agent', ready ? '' : 'down', 'err');
});
onShow('agent', () => { mountAgent(); applyAgentMax(); fitAgentFrame(); });
// The back button floats where the person parks it; the place is a fraction of the
// frame, so a rotation keeps it on the same side. A drag is never a click.
const AGEXIT_KEY = 'cockpit.agent.exitpos';
function agexitPlace(){
const b = $('ag-exit'), f = $('ag-frame'); if (!f) return;
let p = null; try { p = JSON.parse(localStorage.getItem(AGEXIT_KEY)); } catch { /* unparseable: the default */ }
if (!p || !(p.x >= 0 && p.x <= 1) || !(p.y >= 0 && p.y <= 1)){ b.style.left = b.style.top = b.style.right = b.style.transform = ''; return; }
const fr = f.getBoundingClientRect(), w = b.offsetWidth || 160, h = b.offsetHeight || 32;
b.style.transform = 'none'; b.style.right = 'auto';
b.style.left = Math.max(6, Math.min(fr.width - w - 6, p.x * fr.width)) + 'px';
b.style.top = Math.max(6, Math.min(fr.height - h - 6, p.y * fr.height)) + 'px';
}
function wireAgent(){
$('ag-reload').addEventListener('click', reloadAgent);
$('ag-max').addEventListener('click', () => setAgentMax(!document.body.classList.contains('agentmax')));
$('ag-restart').addEventListener('click', () => askAction('unit', {verb: 'restart', unit: AGENT_UNIT},
['A generation in flight in the agent is lost; the panel reconnects when the server is back.']));
let start = null, moved = false;
const exit = $('ag-exit');
exit.addEventListener('pointerdown', e => {
if (e.pointerType === 'mouse' && e.button !== 0) return;
const r = exit.getBoundingClientRect(), fr = $('ag-frame').getBoundingClientRect();
start = {x: e.clientX, y: e.clientY, ox: r.left - fr.left, oy: r.top - fr.top, w: r.width, h: r.height}; moved = false;
exit.setPointerCapture(e.pointerId);
});
exit.addEventListener('pointermove', e => {
if (!start) return;
const dx = e.clientX - start.x, dy = e.clientY - start.y;
if (!moved && Math.hypot(dx, dy) < 6) return;
moved = true; const fr = $('ag-frame').getBoundingClientRect();
exit.style.transform = 'none'; exit.style.right = 'auto';
exit.style.left = Math.max(6, Math.min(fr.width - start.w - 6, start.ox + dx)) + 'px';
exit.style.top = Math.max(6, Math.min(fr.height - start.h - 6, start.oy + dy)) + 'px';
});
const end = () => {
if (!start) return; start = null; if (!moved) return;
const fr = $('ag-frame').getBoundingClientRect(), r = exit.getBoundingClientRect();
try { localStorage.setItem(AGEXIT_KEY, JSON.stringify({x: (r.left - fr.left) / fr.width, y: (r.top - fr.top) / fr.height})); } catch { /* this page only */ }
};
exit.addEventListener('pointerup', end); exit.addEventListener('pointercancel', end);
exit.addEventListener('click', () => { if (moved){ moved = false; return; } setAgentMax(false); });
document.addEventListener('keydown', e => { if (e.key === 'Escape' && document.body.classList.contains('agentmax') && $('scrim').hidden) setAgentMax(false); });
window.addEventListener('resize', () => { if (document.body.classList.contains('agentmax')) agexitPlace(); else fitAgentFrame(false); });
// the visual viewport: on iOS the keyboard shrinks it without touching the layout
// viewport, so a fullscreen frame sized in dvh would keep the composer under the keyboard
const vvp = window.visualViewport;
if (vvp){
const sync = () => { const s = document.documentElement.style; s.setProperty('--vvh', Math.round(vvp.height) + 'px'); s.setProperty('--vvtop', Math.round(vvp.offsetTop) + 'px'); };
vvp.addEventListener('resize', sync); vvp.addEventListener('scroll', sync); sync();
}
}
// leaving the view always shrinks the frame back: it covers every other view
VIEWS.filter(v => v !== 'agent').forEach(v => onShow(v, () => { if (document.body.classList.contains('agentmax')) setAgentMax(false, false); }));