Opening menu and hands-free mode: call an agent by name (0.3.0)
The app now opens on a menu, which is the root page: "Hands free" first, "Agents" second. Agents is the 0.2.0 app unchanged. Hands free is the same agents, conversations and question path with one flag set: the microphone listens while the app is at rest and the app does not leave by itself. The mode is never remembered; every launch starts at the menu with the microphone closed, and double tap at Ready returns to the menu and closes it. Client: a loudness-only speech segmenter (core/vad.ts) cuts the stream into utterances; each goes to the bridge for a wake check. "Wendy, <q>" becomes a text turn to Wendy, with what was heard shown while she works. A name alone starts a recording that sends itself when the speaker stops. The status line starts with a dot for exactly as long as the microphone is open at rest. In wake mode every microphone open and close goes through one serialised reconciler; without the option the old path is untouched. Bridge: POST /v1/wake transcribes one utterance in memory and reports whether it BEGAN with an agent's name (first word of the display name, or an alias from HERMEVEN_AGENT_<ID>_WAKE). Exact match only; a near miss is reported as one word and never acted on. Creates no turn, stores nothing, returns and logs nothing that was not addressed to an agent. One check at a time per device, HERMEVEN_WAKE_CHECKS_PER_MINUTE (30). Docs: decision 0004, bridge API, security gaps 3c/3d, device plan D. The manifest's microphone description changed. Not reviewed, not deployed, not uploaded, not device-tested. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -11,12 +11,14 @@ import {
|
||||
PairingClaimRequest,
|
||||
SCHEMA_VERSION,
|
||||
TextTurnRequest,
|
||||
WakeQuery,
|
||||
type ConversationResponse,
|
||||
type DeviceCredential,
|
||||
type DeviceStatusResponse,
|
||||
type ErrorCode,
|
||||
type ErrorResponse,
|
||||
type Limits,
|
||||
type WakeResponse,
|
||||
} from '@hermeven/contracts';
|
||||
import { DEFAULT_AGENT_ID, type BridgeConfig } from './config.ts';
|
||||
import type { DeviceRow, Devices } from './devices.ts';
|
||||
@@ -24,6 +26,7 @@ import type { HermesAdapter } from './hermes/adapter.ts';
|
||||
import { RateLimiter } from './ratelimit.ts';
|
||||
import type { SttAdapter } from './stt/adapter.ts';
|
||||
import type { TurnEngine } from './turns.ts';
|
||||
import { matchWake } from './wake.ts';
|
||||
|
||||
export interface ServerDeps {
|
||||
config: BridgeConfig;
|
||||
@@ -142,6 +145,9 @@ export async function buildServer(deps: ServerDeps): Promise<FastifyInstance> {
|
||||
const pairingLimiter = new RateLimiter(config.limits.pairingAttemptsPerMinute, 60_000);
|
||||
const turnLimiter = new RateLimiter(config.limits.turnsPerMinute, 60_000);
|
||||
const readLimiter = new RateLimiter(600, 60_000);
|
||||
const wakeLimiter = new RateLimiter(config.limits.wakeChecksPerMinute, 60_000);
|
||||
/** Devices with a wake check being transcribed: one at a time each. */
|
||||
const wakeBusy = new Set<string>();
|
||||
|
||||
function authenticate(req: FastifyRequest): DeviceRow {
|
||||
const header = req.headers.authorization ?? '';
|
||||
@@ -305,6 +311,48 @@ export async function buildServer(deps: ServerDeps): Promise<FastifyInstance> {
|
||||
return engine.toResult(result.turn, result.replayed);
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------- wake
|
||||
/**
|
||||
* One stretch of speech heard while the app was open: does it begin with an agent's name?
|
||||
* Transcribed in memory and dropped. No turn, no row, nothing sent to any agent; a match only
|
||||
* tells the app who was addressed and what was said after the name, and the app then asks in
|
||||
* the ordinary way. Speech that was not for an agent is not returned and never logged.
|
||||
*/
|
||||
app.post('/v1/wake', async (req): Promise<WakeResponse> => {
|
||||
const device = authenticate(req);
|
||||
const contentType = String(req.headers['content-type'] ?? '').split(';')[0]!.trim().toLowerCase();
|
||||
if (contentType !== 'application/octet-stream') throw new ApiError('unsupported_media', 'Unsupported content type.');
|
||||
const query = req.query as Record<string, unknown>;
|
||||
requireVersion(query.schema_version);
|
||||
if (!check(WakeQuery, query)) throw new ApiError('invalid_request', 'Malformed wake request.');
|
||||
const pcm = req.body as Buffer;
|
||||
if (!Buffer.isBuffer(pcm) || pcm.length === 0) throw new ApiError('invalid_request', 'Audio body is empty.');
|
||||
if (pcm.length % 2 !== 0) throw new ApiError('invalid_request', 'Audio is not whole 16-bit samples.');
|
||||
// Nobody can be called by voice (no speech-to-text, or no agent with a usable name): the
|
||||
// app is told so once and stops sending audio.
|
||||
if (!deps.stt.configured || !config.agents.some((a) => a.wakeWords.length > 0)) throw new ApiError('not_found', 'Not found.');
|
||||
if (wakeBusy.has(device.id) || !wakeLimiter.take(device.id)) throw new ApiError('rate_limited', 'Too many requests. Wait a moment.');
|
||||
|
||||
const none: WakeResponse = { schema_version: SCHEMA_VERSION, matched: false, agent_id: null, text: '', near_miss: null };
|
||||
wakeBusy.add(device.id);
|
||||
const abort = new AbortController();
|
||||
const timer = setTimeout(() => abort.abort(), config.limits.transcribeDeadlineSeconds * 1000);
|
||||
try {
|
||||
const transcript = await deps.stt.transcribe({ pcm, locale: query.locale, signal: abort.signal });
|
||||
const match = matchWake(transcript, config.agents);
|
||||
if (!match.matched) return { ...none, near_miss: match.nearMiss };
|
||||
req.log.info({ device_id: device.id, agent_id: match.agentId, name_only: match.text === '' }, 'wake.matched');
|
||||
return { ...none, matched: true, agent_id: match.agentId, text: match.text.slice(0, config.limits.textMaxChars) };
|
||||
} catch (err) {
|
||||
// A failed transcription is "nothing was addressed to anyone": there is no request to fail.
|
||||
req.log.warn({ device_id: device.id, err_name: err instanceof Error ? err.name : 'unknown' }, 'wake.transcription_failed');
|
||||
return none;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
wakeBusy.delete(device.id);
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/v1/turns/:requestId', async (req) => {
|
||||
const device = authenticate(req);
|
||||
const { requestId } = req.params as { requestId: string };
|
||||
|
||||
Reference in New Issue
Block a user