Choose an agent with a swipe at Ready (0.2.0)

Bridge: an owner-configured list of agents (HERMEVEN_AGENTS). Each extra
agent is another profile of the same Hermes gateway, reached at
/p/<profile> with that profile's own key. GET /v1/agents lists ids and
names; POST /v1/conversations takes an optional agent_id checked against
the list. A conversation belongs to one agent (migration v3) and its turns
are submitted, polled and stopped at that agent. /v1/device is unchanged so
clients built earlier keep working.

Client: at Ready a swipe moves between agents; the name is shown through
the exchange; each agent keeps its own conversation; a follow-up goes to
the agent that answered. Reopened within the conversation window it opens
on the last agent, otherwise on the default. With one agent, or a bridge
without /v1/agents, nothing changes.

Also: contract section G, server survey and key evidence, the 0.1.2
upload, and the CODY-020 correction. Not reviewed, not deployed, not
uploaded.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-21 16:08:11 -08:00
co-authored by Claude Fable 5.2
parent 15714bd848
commit 9394c249ad
20 changed files with 1117 additions and 42 deletions
+54 -14
View File
@@ -7,7 +7,7 @@ import {
type TurnResult,
type TurnState,
} from '@hermeven/contracts';
import type { BridgeConfig } from './config.ts';
import { DEFAULT_AGENT_ID, type BridgeConfig } from './config.ts';
import type { Db } from './db.ts';
import type { Devices } from './devices.ts';
import {
@@ -53,6 +53,8 @@ export interface ConversationRow {
hermes_session_id: string;
created_at: number;
closed_at: number | null;
/** The configured agent this conversation is with. NULL = the default agent. */
agent_id: string | null;
}
export interface EngineTiming {
@@ -123,6 +125,8 @@ export class TurnEngine {
config: BridgeConfig;
devices: Devices;
hermes: HermesAdapter;
/** Adapters for the other configured agents, by id. The default one is `hermes`. */
agents?: ReadonlyMap<string, HermesAdapter>;
stt: SttAdapter;
log: EngineLog;
timing?: EngineTiming;
@@ -138,8 +142,30 @@ export class TurnEngine {
// ------------------------------------------------------------ conversations
/** Start a fresh conversation for the device. Any earlier one is closed, not deleted. */
createConversation(deviceId: string): ConversationRow {
/** True when `agentId` names an agent this bridge is configured to reach. */
knowsAgent(agentId: string): boolean {
return agentId === DEFAULT_AGENT_ID || this.deps.agents?.has(agentId) === true;
}
/** The Hermes adapter for a conversation's agent, or undefined if it is no longer configured. */
private adapterFor(agentId: string | null): HermesAdapter | undefined {
if (agentId === null || agentId === DEFAULT_AGENT_ID) return this.deps.hermes;
return this.deps.agents?.get(agentId);
}
private adapterForTurn(row: TurnRow): HermesAdapter | undefined {
const conversation = this.deps.db.prepare('SELECT agent_id FROM conversations WHERE id = ?').get(row.conversation_id) as
| { agent_id: string | null }
| undefined;
return conversation ? this.adapterFor(conversation.agent_id) : undefined;
}
/**
* Start a fresh conversation for the device with one agent. Any earlier conversation with
* THAT agent is closed, not deleted; conversations with other agents are left open.
*/
createConversation(deviceId: string, agentId: string = DEFAULT_AGENT_ID): ConversationRow {
const stored = agentId === DEFAULT_AGENT_ID ? null : agentId;
const now = this.now();
const row: ConversationRow = {
id: randomUUID(),
@@ -147,20 +173,25 @@ export class TurnEngine {
hermes_session_id: `hermeven-${randomUUID()}`,
created_at: now,
closed_at: null,
agent_id: stored,
};
this.deps.db.transaction(() => {
this.deps.db.prepare('UPDATE conversations SET closed_at = ? WHERE device_id = ? AND closed_at IS NULL').run(now, deviceId);
this.deps.db
.prepare('INSERT INTO conversations (id, device_id, hermes_session_id, created_at) VALUES (@id, @device_id, @hermes_session_id, @created_at)')
.prepare('UPDATE conversations SET closed_at = ? WHERE device_id = ? AND closed_at IS NULL AND agent_id IS ?')
.run(now, deviceId, stored);
this.deps.db
.prepare(
'INSERT INTO conversations (id, device_id, hermes_session_id, created_at, agent_id) VALUES (@id, @device_id, @hermes_session_id, @created_at, @agent_id)',
)
.run(row);
})();
this.deps.log.info({ device_id: deviceId, conversation_id: row.id }, 'conversation.created');
this.deps.log.info({ device_id: deviceId, conversation_id: row.id, agent_id: agentId }, 'conversation.created');
return row;
}
activeConversation(deviceId: string): ConversationRow | undefined {
return this.deps.db
.prepare('SELECT * FROM conversations WHERE device_id = ? AND closed_at IS NULL ORDER BY created_at DESC LIMIT 1')
.prepare('SELECT * FROM conversations WHERE device_id = ? AND closed_at IS NULL AND agent_id IS NULL ORDER BY created_at DESC LIMIT 1')
.get(deviceId) as ConversationRow | undefined;
}
@@ -477,13 +508,22 @@ export class TurnEngine {
}
row = this.row(requestId)!;
const hermes = this.adapterForTurn(row);
if (!hermes) {
// The conversation's agent was removed from the configuration. If anything may already
// have been sent (a recovered turn), the outcome cannot be known from here.
this.deps.log.warn({ request_id: requestId }, 'turn.agent_not_configured');
if (recovered && (row.hermes_run_id || row.pending_body)) this.finish(requestId, 'unknown', { code: 'outcome_unknown' });
else this.finish(requestId, 'failed', { code: 'agent_unreachable' });
return;
}
if (!row.hermes_run_id) {
// A recovered turn may already have been sent by the previous process.
const runId = await this.submitToHermes(requestId, active, recovered);
const runId = await this.submitToHermes(requestId, active, recovered, hermes);
if (runId === null) return;
this.update(requestId, { hermes_run_id: runId, pending_body: null });
}
await this.poll(requestId, active);
await this.poll(requestId, active, hermes);
}
private async transcribe(requestId: string, pcm: Buffer, active: ActiveTurn, locale: string): Promise<string | null> {
@@ -519,7 +559,7 @@ export class TurnEngine {
}
/** Returns the run id, or null when the turn was settled here. */
private async submitToHermes(requestId: string, active: ActiveTurn, maybeAlreadySent: boolean): Promise<string | null> {
private async submitToHermes(requestId: string, active: ActiveTurn, maybeAlreadySent: boolean, hermes: HermesAdapter): Promise<string | null> {
const row = this.row(requestId)!;
const deadline = row.agent_deadline_at ?? (row.agent_started_at ?? this.now()) + this.timing.agentDeadlineMs;
let ambiguous = maybeAlreadySent;
@@ -528,7 +568,7 @@ export class TurnEngine {
for (;;) {
if (this.stopping) return null;
try {
const { runId } = await this.deps.hermes.submit({
const { runId } = await hermes.submit({
idempotencyKey: row.hermes_idempotency_key,
body: row.pending_body!,
});
@@ -565,7 +605,7 @@ export class TurnEngine {
}
}
private async poll(requestId: string, active: ActiveTurn): Promise<void> {
private async poll(requestId: string, active: ActiveTurn, hermes: HermesAdapter): Promise<void> {
const start = this.row(requestId)!;
const runId = start.hermes_run_id!;
// Both clocks come from the database, so a bridge restart resumes them where they were:
@@ -592,7 +632,7 @@ export class TurnEngine {
}
if (stopReason && !stopAccepted) {
try {
await this.deps.hermes.stop(runId);
await hermes.stop(runId);
stopAccepted = true;
} catch (err) {
if (!(err instanceof HermesUnavailableError)) throw err;
@@ -601,7 +641,7 @@ export class TurnEngine {
let status: RunStatus | null = null;
try {
status = await this.deps.hermes.status(runId);
status = await hermes.status(runId);
} catch (err) {
if (!(err instanceof HermesUnavailableError)) throw err;
}