First vertical slice: Even client, bridge, contract v1, tests, docs

Client (apps/even-client): pure interaction reducer with generation-based
stale-result rejection, HUD renderer, measured pagination, Even Hub SDK
0.0.16 adapter, bridge HTTP client, phone pairing/status/diagnostics page.

Bridge (services/bridge): device pairing and revocable expiring credentials,
turn engine with idempotent submit, cancel, deadlines and restart recovery,
Hermes Runs API adapter, OpenAI-compatible STT adapter, operator CLI.

Contract (packages/contracts): versioned schemas, types and error codes.

Tests: 122 unit/integration (fakes), 6 simulator scenarios (Even Hub
simulator 0.9.5), 9 scenarios against a real Hermes v0.23.1 gateway with a
stub model. No real STT, real model or physical device has been exercised;
see docs/test-evidence.md and docs/device-test-plan.md.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-18 18:21:49 -08:00
co-authored by Claude Fable 5.2
commit 4687cc0114
79 changed files with 11685 additions and 0 deletions
+178
View File
@@ -0,0 +1,178 @@
import {
AudioInputSource,
CreateStartUpPageContainer,
DeviceConnectType,
StartUpPageCreateResult,
TextContainerProperty,
TextContainerUpgrade,
type EvenAppBridge,
} from '@evenrealities/even_hub_sdk';
import { BODY_BOX, HUD, STATUS_BOX, type HudFrame } from '../core/hud.ts';
import { MENU_ITEM, normalizeEvent, type DeviceEvent, type RawEvenHubEvent } from './events.ts';
import type { DevicePort } from './port.ts';
const BODY = { id: 1, name: 'body' } as const;
const STATUS = { id: 2, name: 'status' } as const;
/** A flaky BLE hop can hang a bridge call for a long time; never let one block the UI forever. */
const CALL_TIMEOUT_MS = 8000;
function withTimeout<T>(promise: Promise<T>, fallback: T): Promise<T> {
return new Promise((resolve) => {
const timer = setTimeout(() => resolve(fallback), CALL_TIMEOUT_MS);
promise.then(
(value) => {
clearTimeout(timer);
resolve(value);
},
() => {
clearTimeout(timer);
resolve(fallback);
},
);
});
}
/**
* Adapter over the public Even Hub SDK (0.0.16). Only documented SDK calls are
* used: createStartUpPageContainer, textContainerUpgrade, audioControl,
* shutDownPageContainer, set/getLocalStorage, onEvenHubEvent, onDeviceStatusChanged.
*/
export class EvenDevice implements DevicePort {
private shown: HudFrame = { body: '', status: '' };
private pending: HudFrame | null = null;
private flushing = false;
private ready = false;
/** All host calls go through one queue: concurrent bridge calls are documented as unsafe. */
private queue: Promise<unknown> = Promise.resolve();
constructor(private readonly bridge: EvenAppBridge) {}
private enqueue<T>(call: () => Promise<T>, fallback: T): Promise<T> {
const next = this.queue.then(() => withTimeout(call(), fallback));
this.queue = next.catch(() => undefined);
return next;
}
async init(frame: HudFrame): Promise<boolean> {
const page = new CreateStartUpPageContainer({
containerTotalNum: 2,
textObject: [
new TextContainerProperty({
xPosition: BODY_BOX.x,
yPosition: BODY_BOX.y,
width: BODY_BOX.width,
height: BODY_BOX.height,
borderWidth: 0,
paddingLength: HUD.padding,
containerID: BODY.id,
containerName: BODY.name,
content: frame.body || ' ',
isEventCapture: 1,
}),
new TextContainerProperty({
xPosition: STATUS_BOX.x,
yPosition: STATUS_BOX.y,
width: STATUS_BOX.width,
height: STATUS_BOX.height,
borderWidth: 0,
paddingLength: HUD.padding,
containerID: STATUS.id,
containerName: STATUS.name,
content: frame.status || ' ',
isEventCapture: 0,
}),
],
// Extra entries in the OS contextual menu (tap, then long press). The OS adds its
// own "Close" entry, so no exit item is declared here.
menuObject: {
menuItems: [
{ itemName: 'New conversation', itemID: MENU_ITEM.NEW_CONVERSATION },
{ itemName: 'Cancel request', itemID: MENU_ITEM.CANCEL },
],
} as never,
});
const result = await this.enqueue(() => this.bridge.createStartUpPageContainer(page), StartUpPageCreateResult.invalid);
this.ready = result === StartUpPageCreateResult.success;
if (this.ready) this.shown = { body: frame.body || ' ', status: frame.status || ' ' };
return this.ready;
}
show(frame: HudFrame): void {
this.pending = { body: frame.body || ' ', status: frame.status || ' ' };
if (!this.flushing) void this.flush();
}
private async flush(): Promise<void> {
this.flushing = true;
try {
while (this.pending && this.ready) {
const frame = this.pending;
this.pending = null;
// In-place text updates are flicker-free; a page rebuild is not needed for text changes.
if (frame.body !== this.shown.body) {
const ok = await this.enqueue(
() =>
this.bridge.textContainerUpgrade(
new TextContainerUpgrade({ containerID: BODY.id, containerName: BODY.name, content: frame.body }),
),
false,
);
if (ok) this.shown.body = frame.body;
}
if (frame.status !== this.shown.status) {
const ok = await this.enqueue(
() =>
this.bridge.textContainerUpgrade(
new TextContainerUpgrade({ containerID: STATUS.id, containerName: STATUS.name, content: frame.status }),
),
false,
);
if (ok) this.shown.status = frame.status;
}
}
} finally {
this.flushing = false;
}
}
startMic(): Promise<boolean> {
return this.enqueue(() => this.bridge.audioControl(true, AudioInputSource.Glasses), false);
}
async stopMic(): Promise<void> {
await this.enqueue(() => this.bridge.audioControl(false), false);
}
onEvent(listener: (event: DeviceEvent) => void): () => void {
return this.bridge.onEvenHubEvent((event) => {
for (const normalized of normalizeEvent(event as RawEvenHubEvent)) listener(normalized);
});
}
onConnection(listener: (connected: boolean) => void): () => void {
return this.bridge.onDeviceStatusChanged((status) => {
if (status.connectType === DeviceConnectType.Connected) listener(true);
else if (
status.connectType === DeviceConnectType.Disconnected ||
status.connectType === DeviceConnectType.ConnectionFailed
) {
listener(false);
}
});
}
async requestExit(): Promise<void> {
// Mode 1: the OS shows its own confirmation. Cleanup happens on SYSTEM_EXIT, not here,
// because the user can still decline.
await this.enqueue(() => this.bridge.shutDownPageContainer(1), false);
}
storageGet(key: string): Promise<string> {
return this.enqueue(() => this.bridge.getLocalStorage(key), '');
}
async storageSet(key: string, value: string): Promise<void> {
await this.enqueue(() => this.bridge.setLocalStorage(key, value), false);
}
}
+100
View File
@@ -0,0 +1,100 @@
/**
* Normalise raw Even Hub events into Hermeven's device-neutral vocabulary.
*
* Kept free of SDK imports (numbers below are the SDK's `OsEventTypeList`
* values, asserted against the real enum in the tests) so the mapping is a
* pure function over plain objects.
*/
export const OS_EVENT = {
CLICK: 0,
SCROLL_TOP: 1,
SCROLL_BOTTOM: 2,
DOUBLE_CLICK: 3,
FOREGROUND_ENTER: 4,
FOREGROUND_EXIT: 5,
ABNORMAL_EXIT: 6,
SYSTEM_EXIT: 7,
IMU_DATA_REPORT: 8,
LONG_PRESS: 9,
LONG_PRESS_RELEASE: 10,
} as const;
export const MENU_ITEM = { NEW_CONVERSATION: 1, CANCEL: 2 } as const;
export type DeviceEvent =
| { type: 'tap' }
| { type: 'double_tap' }
| { type: 'swipe_up' }
| { type: 'swipe_down' }
| { type: 'menu'; itemId: number }
| { type: 'audio'; pcm: Uint8Array }
| { type: 'foreground_enter' }
| { type: 'foreground_exit' }
| { type: 'system_exit' }
| { type: 'abnormal_exit' };
interface Envelope {
eventType?: number;
}
export interface RawEvenHubEvent {
listEvent?: Envelope;
textEvent?: Envelope;
sysEvent?: Envelope;
audioEvent?: { audioPcm?: Uint8Array };
menuItemClickEvent?: { itemID?: number };
}
/**
* CLICK_EVENT is 0 and protobuf omits zero-valued fields, so a tap arrives as
* an envelope with no `eventType` at all. The default must be applied per
* envelope: an event with no gesture envelope (audio, menu) is not a tap.
*/
function typeOf(envelope: Envelope | undefined): number | null {
if (!envelope) return null;
return envelope.eventType ?? OS_EVENT.CLICK;
}
export function normalizeEvent(event: RawEvenHubEvent): DeviceEvent[] {
const out: DeviceEvent[] = [];
const pcm = event.audioEvent?.audioPcm;
if (pcm && pcm.length > 0) out.push({ type: 'audio', pcm });
const itemId = event.menuItemClickEvent?.itemID;
if (typeof itemId === 'number') out.push({ type: 'menu', itemId });
// Official sources disagree on whether gestures arrive on textEvent or sysEvent,
// so both are read. One raw event carries one gesture; the first envelope wins.
const gesture = typeOf(event.sysEvent) ?? typeOf(event.textEvent) ?? typeOf(event.listEvent);
switch (gesture) {
case OS_EVENT.DOUBLE_CLICK:
out.push({ type: 'double_tap' });
break;
case OS_EVENT.SCROLL_TOP:
out.push({ type: 'swipe_up' });
break;
case OS_EVENT.SCROLL_BOTTOM:
out.push({ type: 'swipe_down' });
break;
case OS_EVENT.FOREGROUND_ENTER:
out.push({ type: 'foreground_enter' });
break;
case OS_EVENT.FOREGROUND_EXIT:
out.push({ type: 'foreground_exit' });
break;
case OS_EVENT.ABNORMAL_EXIT:
out.push({ type: 'abnormal_exit' });
break;
case OS_EVENT.SYSTEM_EXIT:
out.push({ type: 'system_exit' });
break;
case OS_EVENT.CLICK:
out.push({ type: 'tap' });
break;
default:
// IMU reports and long press are not part of the interaction contract.
break;
}
return out;
}
+19
View File
@@ -0,0 +1,19 @@
import type { HudFrame } from '../core/hud.ts';
import type { DeviceEvent } from './events.ts';
/** Everything Hermeven needs from the glasses runtime. Implemented by the Even adapter and by test fakes. */
export interface DevicePort {
/** Create the HUD page. Resolves false when the host refused it. */
init(frame: HudFrame): Promise<boolean>;
/** Show a frame. Calls are serialised and coalesced; only the latest pending frame is sent. */
show(frame: HudFrame): void;
/** Open the glasses microphone. Resolves false when the host refused (for example no permission). */
startMic(): Promise<boolean>;
stopMic(): Promise<void>;
onEvent(listener: (event: DeviceEvent) => void): () => void;
onConnection(listener: (connected: boolean) => void): () => void;
/** Ask the OS to show its own exit confirmation. */
requestExit(): Promise<void>;
storageGet(key: string): Promise<string>;
storageSet(key: string, value: string): Promise<void>;
}
@@ -0,0 +1,87 @@
import type { HudFrame } from '../core/hud.ts';
import type { DeviceEvent } from './events.ts';
import type { DevicePort } from './port.ts';
/**
* Stand-in for the glasses when the page runs in a plain browser (no Even host).
* Draws the two HUD text areas in the page and offers buttons for the gestures.
* It has NO microphone: a tap that would start recording reports the mic as
* unavailable. Nothing shown here is evidence about real hardware.
*/
export class BrowserPreviewDevice implements DevicePort {
private readonly body = document.createElement('pre');
private readonly status = document.createElement('pre');
private readonly listeners = new Set<(event: DeviceEvent) => void>();
constructor(container: HTMLElement) {
this.body.className = 'hud-body';
this.status.className = 'hud-status';
const label = document.createElement('p');
label.className = 'hint';
label.textContent = 'Browser preview of the HUD text. Not the glasses, not the simulator; fonts and wrapping differ.';
const controls = document.createElement('div');
controls.className = 'row';
const gestures: Array<[string, DeviceEvent]> = [
['Tap', { type: 'tap' }],
['Double-tap', { type: 'double_tap' }],
['Swipe up', { type: 'swipe_up' }],
['Swipe down', { type: 'swipe_down' }],
];
for (const [text, event] of gestures) {
const button = document.createElement('button');
button.type = 'button';
button.className = 'secondary';
button.textContent = text;
button.addEventListener('click', () => this.listeners.forEach((l) => l(event)));
controls.append(button);
}
const screen = document.createElement('div');
screen.className = 'hud';
screen.append(this.body, this.status);
container.append(label, screen, controls);
}
async init(frame: HudFrame): Promise<boolean> {
this.show(frame);
return true;
}
show(frame: HudFrame): void {
this.body.textContent = frame.body;
this.status.textContent = frame.status;
}
async startMic(): Promise<boolean> {
return false;
}
async stopMic(): Promise<void> {}
onEvent(listener: (event: DeviceEvent) => void): () => void {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
}
onConnection(): () => void {
return () => undefined;
}
async requestExit(): Promise<void> {}
async storageGet(key: string): Promise<string> {
try {
return localStorage.getItem(key) ?? '';
} catch {
return '';
}
}
async storageSet(key: string, value: string): Promise<void> {
try {
if (value) localStorage.setItem(key, value);
else localStorage.removeItem(key);
} catch {
/* preview only */
}
}
}