import { currentAgent, menuItems, menuLabel, type AppState } from './reducer.ts'; /** * HUD geometry. Canvas size and line height are documented by Even * (576 x 288 px; 27 px fixed line height); see docs/feasibility.md, "HUD". * Two text containers: a body that takes the gestures, and a one-line status * bar pinned underneath so the recording indicator never scrolls away. */ export const HUD = { width: 576, height: 288, lineHeight: 27, padding: 4, bodyLines: 9, /** Kept well under the firmware's 999-byte create/rebuild limit. */ pageMaxBytes: 900, } as const; export const BODY_BOX = { x: 0, y: 0, width: HUD.width, height: HUD.bodyLines * HUD.lineHeight + 2 * HUD.padding, // 251 innerWidth: HUD.width - 2 * HUD.padding, // 568 } as const; export const STATUS_BOX = { x: 0, y: BODY_BOX.height, width: HUD.width, height: HUD.height - BODY_BOX.height, // 37: one 27 px line plus padding } as const; export interface HudFrame { body: string; status: string; } function clock(seconds: number): string { const s = Math.max(0, Math.floor(seconds)); return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, '0')}`; } /** The longest question shown back while the agent works; the rest is on the phone. */ const HEARD_MAX = 160; /** Shown in the status bar for as long as the microphone is open at rest. */ export const MIC_OPEN = '● '; /** * The names to call, as they are said: the first word of each agent's name. As many as fit * comfortably; the rest are still reachable by swiping. */ function callNames(state: AppState): string { const names: string[] = []; let used = 0; for (const { name } of state.agents) { const word = name.trim().split(/\s+/)[0] ?? name; if (names.length > 0 && used + word.length > 80) return `${names.join(' · ')} …`; names.push(word); used += word.length + 3; } return names.join(' · '); } /** Pure: the complete HUD text for a state at a moment in time. */ export function renderHud(state: AppState, nowMs: number): HudFrame { const frame = renderFrame(state, nowMs); // The microphone is open and listening for a name: every resting screen says so. const resting = state.phase === 'ready' || state.phase === 'answer' || state.phase === 'error' || state.phase === 'cancelled'; return state.wakeListening && resting ? { body: frame.body, status: `${MIC_OPEN}${frame.status}` } : frame; } function renderFrame(state: AppState, nowMs: number): HudFrame { // With more than one agent on offer, every screen of an exchange says who it is with. const agent = state.agents.length > 1 ? currentAgent(state) : undefined; switch (state.phase) { case 'unpaired': if (state.storageFault) { return { body: 'Hermeven\n\nSaved settings could not be read.\nClose Hermeven and open it again.', status: 'Double-tap: exit', }; } return { body: 'Hermeven\n\nNot connected.\nOpen Hermeven on your phone to pair.', status: 'Double-tap: exit' }; case 'menu': { // Plain ASCII marker: the firmware font silently drops characters it does not have. const lines = menuItems(state).map((item, i) => `${i === state.menuIndex ? '>' : ' '} ${menuLabel(item, state)}`); return { body: `Hermeven\n\n${lines.join('\n')}`, status: lines.length > 1 ? 'Tap: open · Swipe: move · 2×: exit' : 'Tap: open · Double-tap: exit' }; } case 'ready': { // With a menu, double tap goes back to it; without one it leaves the app. const out = state.menu ? 'menu' : 'exit'; const status = agent ? `Tap: ask · Swipe: agent · 2×: ${out}` : `Tap: ask · Double-tap: ${out}`; if (state.wakeListening && !state.notice) { return agent ? { body: `${agent.name}\n\nListening. Say a name, then your question:\n${callNames(state)}`, status } : { body: 'Hermeven\n\nListening. Say your agent’s name, then your question.', status }; } if (agent) return { body: state.notice ? `${agent.name}\n\n${state.notice}.` : `${agent.name}\n\nReady.`, status }; return { body: state.notice ? `Hermeven\n\n${state.notice}.` : 'Hermeven\n\nReady.', status }; } case 'listening': { const elapsed = state.captureStartedAt === null ? 0 : (nowMs - state.captureStartedAt) / 1000; const cap = state.captureMaxSeconds; const title = agent ? `${agent.name} is listening…` : 'Listening…'; const status = state.captureAuto ? `● REC ${clock(elapsed)} / ${clock(cap)} · Tap: send · 2×: cancel` : `● REC ${clock(elapsed)} / ${clock(cap)} · Tap or release: send · 2×: cancel`; // Once there are words, they replace the instructions. if (state.preview) return { body: `${title}\n\n${state.preview}`, status }; return { body: state.captureAuto ? `${title}\n\nAsk your question. It is sent when you stop speaking.` : `${title}\n\nRecording stops by itself after ${cap} seconds.`, status, }; } case 'transcribing': return { body: state.preview ? `Transcribing…\n\n${state.preview}` : 'Transcribing…', status: 'Double-tap: cancel' }; case 'thinking': { const title = agent ? `${agent.name} is thinking…` : 'Thinking…'; if (!state.heard) return { body: title, status: 'Double-tap: cancel' }; // Asked by voice with nothing pressed: show what was understood, so a mishearing is seen // while it can still be cancelled. const heard = state.heard.length > HEARD_MAX ? `${state.heard.slice(0, HEARD_MAX - 1).trimEnd()}…` : state.heard; return { body: `${title}\n\nHeard: ${heard}`, status: 'Double-tap: cancel' }; } case 'approval': return { body: 'Approval required.\n\nThe agent is waiting for you to approve an action. Approve or deny it where you normally manage the agent. It cannot be approved from the glasses.', status: 'Waiting · Double-tap: cancel', }; case 'answer': { const total = state.pages.length; const page = state.pages[state.pageIndex] ?? ''; const position = total > 1 ? `${state.pageIndex + 1}/${total} · Swipe: page · ` : ''; const partial = state.partial ? 'PARTIAL · ' : ''; return { body: page, status: `${partial}${position}${agent ? `${agent.name} · ` : ''}Tap: ask` }; } case 'cancelled': return { body: 'Cancelled.\n\nAnything the agent had already done is not undone.', status: 'Tap: ask · Double-tap: back', }; case 'disconnected': if (state.disconnectedFrom === 'glasses') { return { body: 'Glasses disconnected.', status: 'Reconnect to continue' }; } if (state.disconnectedFrom === 'storage') { // True as stated: the request is only ever sent after its id has been saved. return { body: 'Could not save your request on the phone.\n\nNothing has been sent.', status: 'Tap: retry · Double-tap: discard', }; } return state.canResend ? // Deliberately not "has not been sent": a request can be admitted even when no reply // comes back, so the honest statement is that it is unconfirmed. { body: 'No connection to the bridge.\n\nYour request is not confirmed yet.', status: 'Tap: retry · Double-tap: discard' } : { body: 'Connection lost.\n\nStill waiting for the answer…', status: 'Reconnecting · Double-tap: cancel' }; case 'error': return { body: `${state.error?.hud ?? 'Something went wrong.'}\n\nDetails are on your phone.`, status: state.error?.retryable ? 'Tap: ask again · Double-tap: back' : 'Double-tap: back', }; } }