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,58 @@
|
||||
/**
|
||||
* Stable, machine-readable error codes for the Hermeven bridge contract.
|
||||
*
|
||||
* Codes are part of the versioned contract: never repurpose one. `retryable`
|
||||
* means "a deliberate NEW turn by the user is safe", never "replay automatically".
|
||||
*/
|
||||
export const ERROR_CODES = {
|
||||
// transport / auth
|
||||
invalid_request: { http: 400, retryable: false },
|
||||
unsupported_schema_version: { http: 400, retryable: false },
|
||||
unauthorized: { http: 401, retryable: false },
|
||||
credential_expired: { http: 401, retryable: false },
|
||||
forbidden: { http: 403, retryable: false },
|
||||
not_found: { http: 404, retryable: false },
|
||||
request_id_conflict: { http: 409, retryable: false },
|
||||
conversation_busy: { http: 409, retryable: true },
|
||||
conversation_closed: { http: 409, retryable: false },
|
||||
payload_too_large: { http: 413, retryable: false },
|
||||
unsupported_media: { http: 415, retryable: false },
|
||||
rate_limited: { http: 429, retryable: true },
|
||||
pairing_invalid: { http: 401, retryable: false },
|
||||
pairing_locked: { http: 429, retryable: false },
|
||||
internal: { http: 500, retryable: false },
|
||||
// turn outcomes (carried in TurnResult.error, HTTP status of the GET is still 200)
|
||||
stt_unavailable: { http: 200, retryable: true },
|
||||
stt_failed: { http: 200, retryable: true },
|
||||
stt_timeout: { http: 200, retryable: true },
|
||||
stt_empty: { http: 200, retryable: true },
|
||||
/** Bridge restarted before anything was sent to Hermes: nothing ran. */
|
||||
interrupted_before_agent: { http: 200, retryable: true },
|
||||
/** Hermes definitely did not admit the turn (connection refused, 4xx, 429/503). */
|
||||
agent_unreachable: { http: 200, retryable: true },
|
||||
agent_rejected: { http: 200, retryable: false },
|
||||
/** Hermes ran the turn and reported failure. Tools may already have acted. */
|
||||
agent_failed: { http: 200, retryable: false },
|
||||
/** Hermes interrupted the turn (gateway shutdown). Tools may already have acted. */
|
||||
agent_interrupted: { http: 200, retryable: false },
|
||||
/** Deadline passed; a stop was requested and Hermes confirmed it. */
|
||||
agent_timeout: { http: 200, retryable: false },
|
||||
/** Hermes waited for a human approval that never arrived on a verified surface. */
|
||||
approval_timeout: { http: 200, retryable: false },
|
||||
/** Hermes stopped short (for example iteration budget); the text is partial. */
|
||||
agent_incomplete: { http: 200, retryable: false },
|
||||
/** Downstream status could not be reconciled. NOT permission to run again. */
|
||||
outcome_unknown: { http: 200, retryable: false },
|
||||
} as const;
|
||||
|
||||
export type ErrorCode = keyof typeof ERROR_CODES;
|
||||
|
||||
export const ERROR_CODE_LIST = Object.keys(ERROR_CODES) as ErrorCode[];
|
||||
|
||||
export function isRetryable(code: ErrorCode): boolean {
|
||||
return ERROR_CODES[code].retryable;
|
||||
}
|
||||
|
||||
export function httpStatusFor(code: ErrorCode): number {
|
||||
return ERROR_CODES[code].http;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './errors.ts';
|
||||
export * from './schemas.ts';
|
||||
@@ -0,0 +1,260 @@
|
||||
import { Type, type Static, type TSchema } from '@sinclair/typebox';
|
||||
import { Value } from '@sinclair/typebox/value';
|
||||
import { ERROR_CODE_LIST, type ErrorCode } from './errors.ts';
|
||||
|
||||
/** Contract version. Bump on any breaking change; the bridge rejects unknown versions. */
|
||||
export const SCHEMA_VERSION = 1 as const;
|
||||
|
||||
/**
|
||||
* Audio the bridge accepts. Validated against what the Even Hub SDK actually
|
||||
* emits (see docs/feasibility.md, "Microphone"): raw PCM, signed 16-bit
|
||||
* little-endian, mono, 16 kHz. Nothing else is accepted until it is measured.
|
||||
*/
|
||||
export const AUDIO_FORMAT = {
|
||||
encoding: 'pcm_s16le',
|
||||
sampleRate: 16000,
|
||||
channels: 1,
|
||||
bytesPerSecond: 16000 * 2,
|
||||
} as const;
|
||||
|
||||
/** Defaults; the bridge can lower them via configuration and reports the effective values. */
|
||||
export const DEFAULT_LIMITS = {
|
||||
captureMaxSeconds: 30,
|
||||
transcribeDeadlineSeconds: 30,
|
||||
agentDeadlineSeconds: 120,
|
||||
approvalWaitSeconds: 300,
|
||||
textMaxChars: 4000,
|
||||
} as const;
|
||||
|
||||
const Uuid = Type.String({
|
||||
pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$',
|
||||
description: 'Lower-case RFC 4122 UUID',
|
||||
});
|
||||
const Locale = Type.String({ pattern: '^[A-Za-z]{2,3}(-[A-Za-z0-9]{2,8}){0,2}$', maxLength: 20 });
|
||||
const IsoTime = Type.String({ minLength: 20, maxLength: 30 });
|
||||
const SchemaVersion = Type.Literal(SCHEMA_VERSION);
|
||||
|
||||
export const ErrorBody = Type.Object(
|
||||
{
|
||||
code: Type.Union(ERROR_CODE_LIST.map((c) => Type.Literal(c))),
|
||||
message: Type.String({ maxLength: 300 }),
|
||||
retryable: Type.Boolean(),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
export type ErrorBody = { code: ErrorCode; message: string; retryable: boolean };
|
||||
|
||||
export const ErrorResponse = Type.Object(
|
||||
{ schema_version: SchemaVersion, error: ErrorBody },
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
export type ErrorResponse = { schema_version: typeof SCHEMA_VERSION; error: ErrorBody };
|
||||
|
||||
// ---------------------------------------------------------------- health
|
||||
export const HealthResponse = Type.Object(
|
||||
{ status: Type.Literal('ok'), schema_version: SchemaVersion },
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
export type HealthResponse = Static<typeof HealthResponse>;
|
||||
|
||||
// --------------------------------------------------------------- pairing
|
||||
export const PairingClaimRequest = Type.Object(
|
||||
{
|
||||
schema_version: SchemaVersion,
|
||||
code: Type.String({ minLength: 4, maxLength: 32 }),
|
||||
device_name: Type.String({ minLength: 1, maxLength: 60 }),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
export type PairingClaimRequest = Static<typeof PairingClaimRequest>;
|
||||
|
||||
export const DeviceCredential = Type.Object(
|
||||
{
|
||||
schema_version: SchemaVersion,
|
||||
device_id: Uuid,
|
||||
/** Opaque bearer credential. Shown once; the bridge stores only a hash. */
|
||||
token: Type.String({ minLength: 32, maxLength: 128 }),
|
||||
token_expires_at: IsoTime,
|
||||
/** Absolute lifetime of this pairing; after it the user must pair again. */
|
||||
pairing_expires_at: IsoTime,
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
export type DeviceCredential = Static<typeof DeviceCredential>;
|
||||
|
||||
export const Limits = Type.Object(
|
||||
{
|
||||
capture_max_seconds: Type.Integer({ minimum: 1 }),
|
||||
transcribe_deadline_seconds: Type.Integer({ minimum: 1 }),
|
||||
agent_deadline_seconds: Type.Integer({ minimum: 1 }),
|
||||
approval_wait_seconds: Type.Integer({ minimum: 1 }),
|
||||
audio_max_bytes: Type.Integer({ minimum: 1 }),
|
||||
text_max_chars: Type.Integer({ minimum: 1 }),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
export type Limits = Static<typeof Limits>;
|
||||
|
||||
export const DeviceStatusResponse = Type.Object(
|
||||
{
|
||||
schema_version: SchemaVersion,
|
||||
device_id: Uuid,
|
||||
device_name: Type.String(),
|
||||
token_expires_at: IsoTime,
|
||||
pairing_expires_at: IsoTime,
|
||||
/** Whether the bridge could reach its configured agent just now. No detail by design. */
|
||||
agent_reachable: Type.Boolean(),
|
||||
stt_configured: Type.Boolean(),
|
||||
active_conversation_id: Type.Union([Uuid, Type.Null()]),
|
||||
limits: Limits,
|
||||
audio_format: Type.Object(
|
||||
{
|
||||
encoding: Type.Literal(AUDIO_FORMAT.encoding),
|
||||
sample_rate: Type.Literal(AUDIO_FORMAT.sampleRate),
|
||||
channels: Type.Literal(AUDIO_FORMAT.channels),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
export type DeviceStatusResponse = Static<typeof DeviceStatusResponse>;
|
||||
|
||||
export const RevokeResponse = Type.Object(
|
||||
{ schema_version: SchemaVersion, revoked: Type.Literal(true) },
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
export type RevokeResponse = Static<typeof RevokeResponse>;
|
||||
|
||||
// ---------------------------------------------------------- conversations
|
||||
export const ConversationCreateRequest = Type.Object(
|
||||
{ schema_version: SchemaVersion },
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
export type ConversationCreateRequest = Static<typeof ConversationCreateRequest>;
|
||||
|
||||
export const ConversationResponse = Type.Object(
|
||||
{
|
||||
schema_version: SchemaVersion,
|
||||
/** Hermeven conversation id. The Hermes session behind it is never exposed to clients. */
|
||||
conversation_id: Uuid,
|
||||
created_at: IsoTime,
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
export type ConversationResponse = Static<typeof ConversationResponse>;
|
||||
|
||||
// ------------------------------------------------------------------ turns
|
||||
/** JSON body of POST /v1/turns for typed text (development and accessibility input). */
|
||||
export const TextTurnRequest = Type.Object(
|
||||
{
|
||||
schema_version: SchemaVersion,
|
||||
request_id: Uuid,
|
||||
conversation_id: Uuid,
|
||||
locale: Locale,
|
||||
input: Type.Object(
|
||||
{ kind: Type.Literal('text'), text: Type.String({ minLength: 1, maxLength: DEFAULT_LIMITS.textMaxChars }) },
|
||||
{ additionalProperties: false },
|
||||
),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
export type TextTurnRequest = Static<typeof TextTurnRequest>;
|
||||
|
||||
/**
|
||||
* Query string of POST /v1/turns when the body is raw audio
|
||||
* (`Content-Type: application/octet-stream`). Audio is never JSON-encoded.
|
||||
*/
|
||||
export const AudioTurnQuery = Type.Object(
|
||||
{
|
||||
schema_version: Type.Literal(String(SCHEMA_VERSION)),
|
||||
request_id: Uuid,
|
||||
conversation_id: Uuid,
|
||||
locale: Locale,
|
||||
input_kind: Type.Literal('audio'),
|
||||
encoding: Type.Literal(AUDIO_FORMAT.encoding),
|
||||
sample_rate: Type.Literal(String(AUDIO_FORMAT.sampleRate)),
|
||||
channels: Type.Literal(String(AUDIO_FORMAT.channels)),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
export type AudioTurnQuery = Static<typeof AudioTurnQuery>;
|
||||
|
||||
export const TURN_STATES = [
|
||||
'accepted',
|
||||
'transcribing',
|
||||
'thinking',
|
||||
'approval_required',
|
||||
'cancelling',
|
||||
'done',
|
||||
] as const;
|
||||
export type TurnState = (typeof TURN_STATES)[number];
|
||||
|
||||
/**
|
||||
* Terminal outcome. Only meaningful when `terminal` is true.
|
||||
* - succeeded: Hermes reported the turn completed.
|
||||
* - incomplete: Hermes stopped short; `answer_text` is a usable partial answer.
|
||||
* - failed: definite failure (see error.code for whether anything may have run).
|
||||
* - cancelled: Hermes confirmed the stop, or the turn was cancelled before reaching it.
|
||||
* Cancellation is not rollback: tools may already have acted.
|
||||
* - unknown: downstream state could not be reconciled. Never replay automatically.
|
||||
*/
|
||||
export const TURN_OUTCOMES = ['succeeded', 'incomplete', 'failed', 'cancelled', 'unknown'] as const;
|
||||
export type TurnOutcome = (typeof TURN_OUTCOMES)[number];
|
||||
|
||||
export const TurnResult = Type.Object(
|
||||
{
|
||||
schema_version: SchemaVersion,
|
||||
request_id: Uuid,
|
||||
conversation_id: Uuid,
|
||||
state: Type.Union(TURN_STATES.map((s) => Type.Literal(s))),
|
||||
terminal: Type.Boolean(),
|
||||
outcome: Type.Union([...TURN_OUTCOMES.map((s) => Type.Literal(s)), Type.Null()]),
|
||||
/** True once a cancel was requested, whatever the eventual outcome. */
|
||||
cancel_requested: Type.Boolean(),
|
||||
/** What the bridge sent to the agent (typed text or transcript), while retained. */
|
||||
input_text: Type.Union([Type.String(), Type.Null()]),
|
||||
/** Complete agent answer, never truncated by the bridge, while retained. */
|
||||
answer_text: Type.Union([Type.String(), Type.Null()]),
|
||||
error: Type.Union([ErrorBody, Type.Null()]),
|
||||
/** True when this response is for a duplicate submit of an existing request_id. */
|
||||
replayed: Type.Boolean(),
|
||||
created_at: IsoTime,
|
||||
updated_at: IsoTime,
|
||||
timings: Type.Object(
|
||||
{
|
||||
transcribe_ms: Type.Union([Type.Integer(), Type.Null()]),
|
||||
agent_ms: Type.Union([Type.Integer(), Type.Null()]),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
export type TurnResult = Omit<Static<typeof TurnResult>, 'error' | 'state' | 'outcome'> & {
|
||||
state: TurnState;
|
||||
outcome: TurnOutcome | null;
|
||||
error: ErrorBody | null;
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------- helpers
|
||||
export function check<T extends TSchema>(schema: T, value: unknown): value is Static<T> {
|
||||
return Value.Check(schema, value);
|
||||
}
|
||||
|
||||
/** First validation problem as a short path string, for diagnostics only. */
|
||||
export function firstError(schema: TSchema, value: unknown): string | null {
|
||||
const first = Value.Errors(schema, value).First();
|
||||
return first ? `${first.path || '/'}: ${first.message}` : null;
|
||||
}
|
||||
|
||||
export function parse<T extends TSchema>(schema: T, value: unknown, what: string): Static<T> {
|
||||
if (!Value.Check(schema, value)) {
|
||||
throw new ContractError(`${what} does not match contract v${SCHEMA_VERSION}: ${firstError(schema, value)}`);
|
||||
}
|
||||
return value as Static<T>;
|
||||
}
|
||||
|
||||
export class ContractError extends Error {
|
||||
override name = 'ContractError';
|
||||
}
|
||||
Reference in New Issue
Block a user