Files
hermeven/services/bridge/test/wake.test.ts
DanandClaude Fable 5.2 e1d183b54d 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>
2026-11-28 10:52:06 -08:00

71 lines
3.9 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { ConfigError, loadConfig } from '../src/config.ts';
import { matchWake, wakeWordsFor } from '../src/wake.ts';
const agents = [
{ id: 'default', wakeWords: ['pepper'] },
{ id: 'wendy', wakeWords: ['wendy'] },
{ id: 'cody', wakeWords: ['cody', 'kody', 'code e'] },
];
describe('wake names', () => {
it('a name said first addresses that agent, and what follows is the question', () => {
expect(matchWake(' Wendy, what is on my calendar?', agents)).toEqual({ matched: true, agentId: 'wendy', text: 'What is on my calendar?' });
expect(matchWake('Pepper. Send Dan a message.', agents)).toEqual({ matched: true, agentId: 'default', text: 'Send Dan a message.' });
expect(matchWake('cody are the tests passing', agents)).toEqual({ matched: true, agentId: 'cody', text: 'Are the tests passing' });
});
it('a greeting before the name is allowed, and nothing else is', () => {
expect(matchWake('Hey Wendy, hello', agents)).toMatchObject({ matched: true, agentId: 'wendy', text: 'Hello' });
expect(matchWake('OK, Pepper: lights off', agents)).toMatchObject({ matched: true, agentId: 'default', text: 'Lights off' });
expect(matchWake('So Wendy, hello', agents).matched).toBe(false);
expect(matchWake('Hey', agents).matched).toBe(false);
});
it('the name alone matches with an empty question', () => {
expect(matchWake('Wendy.', agents)).toEqual({ matched: true, agentId: 'wendy', text: '' });
expect(matchWake('Hey Cody!', agents)).toEqual({ matched: true, agentId: 'cody', text: '' });
});
it('a name anywhere but first never matches', () => {
for (const said of ['I told Wendy about it', 'ask Pepper later', 'is that Cody', '', ' ', '[BLANK_AUDIO]', '(wind blowing)']) {
expect(matchWake(said, agents).matched, said).toBe(false);
}
});
it('aliases match, including one of two words, and the longest wins', () => {
expect(matchWake('Kody, status', agents)).toMatchObject({ matched: true, agentId: 'cody', text: 'Status' });
expect(matchWake('Code E, status', agents)).toMatchObject({ matched: true, agentId: 'cody', text: 'Status' });
});
it('a word that is only close to a name is reported as a near miss and does not match', () => {
expect(matchWake('Wendi, what time is it', agents)).toEqual({ matched: false, nearMiss: 'wendi' });
expect(matchWake('Copy that', agents)).toEqual({ matched: false, nearMiss: 'copy' });
expect(matchWake('Paper is on the desk', agents)).toEqual({ matched: false, nearMiss: 'paper' });
expect(matchWake('Wednesday works', agents)).toEqual({ matched: false, nearMiss: null });
});
it('wake words come from the first word of the name plus aliases', () => {
expect(wakeWordsFor('Cody R.', ['Kody', ' code-e '])).toEqual(['cody', 'kody', 'code e']);
expect(wakeWordsFor('R2', [])).toEqual([]);
expect(wakeWordsFor('', ['Friday'])).toEqual(['friday']);
});
});
describe('wake names in configuration', () => {
const base = { HERMES_API_KEY: 'k'.repeat(24), STT_PROVIDER: 'none' };
it('each agent answers to its name; the unnamed default agent answers to nothing', () => {
const config = loadConfig({ ...base, HERMEVEN_AGENTS: 'wendy', HERMEVEN_AGENT_WENDY_API_KEY: 'w'.repeat(24), HERMEVEN_AGENT_WENDY_WAKE: 'wendi, windy' });
expect(config.agents.map((a) => a.wakeWords)).toEqual([[], ['wendy', 'wendi', 'windy']]);
expect(loadConfig({ ...base, HERMEVEN_AGENT_NAME: 'Pepper' }).agents[0]!.wakeWords).toEqual(['pepper']);
expect(loadConfig({ ...base, HERMEVEN_AGENT_WAKE: 'friday' }).agents[0]!.wakeWords).toEqual(['friday']);
});
it('two agents may not answer to the same word', () => {
expect(() =>
loadConfig({ ...base, HERMEVEN_AGENT_NAME: 'Pepper', HERMEVEN_AGENTS: 'wendy', HERMEVEN_AGENT_WENDY_API_KEY: 'w'.repeat(24), HERMEVEN_AGENT_WENDY_WAKE: 'pepper' }),
).toThrow(ConfigError);
});
});