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:
@@ -0,0 +1,180 @@
|
||||
import { AUDIO_FORMAT, DEFAULT_LIMITS } from '@hermeven/contracts';
|
||||
|
||||
export interface BridgeConfig {
|
||||
host: string;
|
||||
port: number;
|
||||
dbPath: string;
|
||||
/** Exact browser origins allowed by CORS. Empty = no cross-origin browser access. */
|
||||
allowedOrigins: string[];
|
||||
/** Refuse to bind a non-loopback address without this (TLS must terminate in front). */
|
||||
allowNonLoopback: boolean;
|
||||
/** Trust X-Forwarded-For from a reverse proxy (only when one is actually in front). */
|
||||
trustProxy: boolean;
|
||||
hermes: {
|
||||
/** Base URL of the Hermes API server, e.g. http://127.0.0.1:8642 or .../p/<profile>. */
|
||||
baseUrl: string;
|
||||
apiKey: string;
|
||||
/** Label of the configured profile; devices are bound to it. Not sent to Hermes. */
|
||||
profileLabel: string;
|
||||
/** Per-request ephemeral instructions layered on the agent prompt. Empty disables. */
|
||||
instructions: string;
|
||||
pollIntervalMs: number;
|
||||
requestTimeoutMs: number;
|
||||
};
|
||||
stt: {
|
||||
provider: 'none' | 'openai-compatible';
|
||||
baseUrl: string;
|
||||
apiKey: string;
|
||||
model: string;
|
||||
};
|
||||
limits: {
|
||||
captureMaxSeconds: number;
|
||||
transcribeDeadlineSeconds: number;
|
||||
agentDeadlineSeconds: number;
|
||||
approvalWaitSeconds: number;
|
||||
cancelGraceSeconds: number;
|
||||
textMaxChars: number;
|
||||
audioMaxBytes: number;
|
||||
turnsPerMinute: number;
|
||||
pairingAttemptsPerMinute: number;
|
||||
};
|
||||
credentials: {
|
||||
tokenTtlSeconds: number;
|
||||
pairingTtlSeconds: number;
|
||||
pairingCodeTtlSeconds: number;
|
||||
pairingCodeMaxFailures: number;
|
||||
};
|
||||
/** How long input/answer text stays in the bridge's result cache. Hermes is the authority. */
|
||||
resultRetentionSeconds: number;
|
||||
logLevel: string;
|
||||
}
|
||||
|
||||
const DEFAULT_INSTRUCTIONS =
|
||||
'The user reads this reply on a small monochrome heads-up display. ' +
|
||||
'Answer in plain text without Markdown, tables, code fences or links. ' +
|
||||
'Lead with the direct answer in one or two short sentences, then add detail only if it is needed.';
|
||||
|
||||
export class ConfigError extends Error {
|
||||
override name = 'ConfigError';
|
||||
}
|
||||
|
||||
function int(env: NodeJS.ProcessEnv, key: string, fallback: number, min = 1, max = Number.MAX_SAFE_INTEGER): number {
|
||||
const raw = env[key];
|
||||
if (raw === undefined || raw === '') return fallback;
|
||||
const n = Number(raw);
|
||||
if (!Number.isInteger(n) || n < min || n > max) {
|
||||
throw new ConfigError(`${key} must be an integer between ${min} and ${max}`);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
function bool(env: NodeJS.ProcessEnv, key: string, fallback = false): boolean {
|
||||
const raw = env[key];
|
||||
if (raw === undefined || raw === '') return fallback;
|
||||
if (['1', 'true', 'yes'].includes(raw.toLowerCase())) return true;
|
||||
if (['0', 'false', 'no'].includes(raw.toLowerCase())) return false;
|
||||
throw new ConfigError(`${key} must be true or false`);
|
||||
}
|
||||
|
||||
export function isLoopback(host: string): boolean {
|
||||
return host === 'localhost' || host === '::1' || /^127\./.test(host);
|
||||
}
|
||||
|
||||
function httpUrl(key: string, raw: string): string {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(raw);
|
||||
} catch {
|
||||
throw new ConfigError(`${key} is not a valid URL`);
|
||||
}
|
||||
if (url.protocol !== 'http:' && url.protocol !== 'https:') throw new ConfigError(`${key} must be http(s)`);
|
||||
if (url.protocol === 'http:' && !isLoopback(url.hostname)) {
|
||||
throw new ConfigError(`${key} must use https outside loopback`);
|
||||
}
|
||||
return raw.replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
export function loadConfig(env: NodeJS.ProcessEnv = process.env): BridgeConfig {
|
||||
const host = env.HERMEVEN_HOST || '127.0.0.1';
|
||||
const allowNonLoopback = bool(env, 'HERMEVEN_ALLOW_NON_LOOPBACK');
|
||||
if (!isLoopback(host) && !allowNonLoopback) {
|
||||
throw new ConfigError(
|
||||
'HERMEVEN_HOST is not loopback. Put a TLS-terminating proxy or private network in front and set ' +
|
||||
'HERMEVEN_ALLOW_NON_LOOPBACK=true to confirm.',
|
||||
);
|
||||
}
|
||||
|
||||
const hermesKey = env.HERMES_API_KEY || '';
|
||||
if (!hermesKey) throw new ConfigError('HERMES_API_KEY is required (the Hermes API server refuses to start without one)');
|
||||
|
||||
const allowedOrigins = (env.HERMEVEN_ALLOWED_ORIGINS || '')
|
||||
.split(',')
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean);
|
||||
for (const origin of allowedOrigins) {
|
||||
if (origin === '*' ) throw new ConfigError('HERMEVEN_ALLOWED_ORIGINS must list exact origins, not *');
|
||||
let parsed: URL;
|
||||
try {
|
||||
parsed = new URL(origin);
|
||||
} catch {
|
||||
throw new ConfigError(`HERMEVEN_ALLOWED_ORIGINS entry is not an origin: ${origin}`);
|
||||
}
|
||||
if (parsed.origin !== origin) throw new ConfigError(`HERMEVEN_ALLOWED_ORIGINS entry must be scheme://host[:port]: ${origin}`);
|
||||
}
|
||||
|
||||
const sttProvider = (env.STT_PROVIDER || 'none') as BridgeConfig['stt']['provider'];
|
||||
if (sttProvider !== 'none' && sttProvider !== 'openai-compatible') {
|
||||
throw new ConfigError('STT_PROVIDER must be "none" or "openai-compatible"');
|
||||
}
|
||||
let sttBaseUrl = '';
|
||||
if (sttProvider === 'openai-compatible') {
|
||||
if (!env.STT_BASE_URL) throw new ConfigError('STT_BASE_URL is required for STT_PROVIDER=openai-compatible');
|
||||
if (!env.STT_MODEL) throw new ConfigError('STT_MODEL is required for STT_PROVIDER=openai-compatible');
|
||||
sttBaseUrl = httpUrl('STT_BASE_URL', env.STT_BASE_URL);
|
||||
}
|
||||
|
||||
const captureMaxSeconds = int(env, 'HERMEVEN_CAPTURE_MAX_SECONDS', DEFAULT_LIMITS.captureMaxSeconds, 1, 120);
|
||||
|
||||
return {
|
||||
host,
|
||||
port: int(env, 'HERMEVEN_PORT', 8787, 0, 65535),
|
||||
dbPath: env.HERMEVEN_DB_PATH || './data/hermeven.sqlite',
|
||||
allowedOrigins,
|
||||
allowNonLoopback,
|
||||
trustProxy: bool(env, 'HERMEVEN_TRUST_PROXY'),
|
||||
hermes: {
|
||||
baseUrl: httpUrl('HERMES_BASE_URL', env.HERMES_BASE_URL || 'http://127.0.0.1:8642'),
|
||||
apiKey: hermesKey,
|
||||
profileLabel: env.HERMES_PROFILE_LABEL || 'default',
|
||||
instructions: env.HERMEVEN_INSTRUCTIONS === undefined ? DEFAULT_INSTRUCTIONS : env.HERMEVEN_INSTRUCTIONS,
|
||||
pollIntervalMs: int(env, 'HERMES_POLL_INTERVAL_MS', 500, 20, 10_000),
|
||||
requestTimeoutMs: int(env, 'HERMES_REQUEST_TIMEOUT_MS', 10_000, 50, 120_000),
|
||||
},
|
||||
stt: {
|
||||
provider: sttProvider,
|
||||
baseUrl: sttBaseUrl,
|
||||
apiKey: env.STT_API_KEY || '',
|
||||
model: env.STT_MODEL || '',
|
||||
},
|
||||
limits: {
|
||||
captureMaxSeconds,
|
||||
transcribeDeadlineSeconds: int(env, 'HERMEVEN_TRANSCRIBE_DEADLINE_SECONDS', DEFAULT_LIMITS.transcribeDeadlineSeconds, 1, 300),
|
||||
agentDeadlineSeconds: int(env, 'HERMEVEN_AGENT_DEADLINE_SECONDS', DEFAULT_LIMITS.agentDeadlineSeconds, 1, 3600),
|
||||
approvalWaitSeconds: int(env, 'HERMEVEN_APPROVAL_WAIT_SECONDS', DEFAULT_LIMITS.approvalWaitSeconds, 1, 3600),
|
||||
cancelGraceSeconds: int(env, 'HERMEVEN_CANCEL_GRACE_SECONDS', 30, 1, 600),
|
||||
textMaxChars: DEFAULT_LIMITS.textMaxChars,
|
||||
// One extra second of headroom over the capture cap for frame alignment.
|
||||
audioMaxBytes: (captureMaxSeconds + 1) * AUDIO_FORMAT.bytesPerSecond,
|
||||
turnsPerMinute: int(env, 'HERMEVEN_TURNS_PER_MINUTE', 20, 1, 600),
|
||||
pairingAttemptsPerMinute: int(env, 'HERMEVEN_PAIRING_ATTEMPTS_PER_MINUTE', 5, 1, 60),
|
||||
},
|
||||
credentials: {
|
||||
tokenTtlSeconds: int(env, 'HERMEVEN_TOKEN_TTL_SECONDS', 7 * 86_400, 60),
|
||||
pairingTtlSeconds: int(env, 'HERMEVEN_PAIRING_TTL_SECONDS', 90 * 86_400, 60),
|
||||
pairingCodeTtlSeconds: int(env, 'HERMEVEN_PAIRING_CODE_TTL_SECONDS', 300, 30, 3600),
|
||||
pairingCodeMaxFailures: int(env, 'HERMEVEN_PAIRING_CODE_MAX_FAILURES', 5, 1, 20),
|
||||
},
|
||||
resultRetentionSeconds: int(env, 'HERMEVEN_RESULT_RETENTION_SECONDS', 900, 10, 86_400),
|
||||
logLevel: env.HERMEVEN_LOG_LEVEL || 'info',
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user