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>
102 lines
3.4 KiB
TypeScript
102 lines
3.4 KiB
TypeScript
import type { FastifyInstance } from 'fastify';
|
|
import { pino, type Logger } from 'pino';
|
|
import type { BridgeConfig } from './config.ts';
|
|
import { openDb, type Db } from './db.ts';
|
|
import { Devices } from './devices.ts';
|
|
import type { HermesAdapter } from './hermes/adapter.ts';
|
|
import { HermesRunsAdapter } from './hermes/runsAdapter.ts';
|
|
import { buildServer } from './server.ts';
|
|
import { NoSttAdapter, type SttAdapter } from './stt/adapter.ts';
|
|
import { BuiltinWhisperStt } from './stt/builtinWhisper.ts';
|
|
import { OpenAiCompatibleStt } from './stt/openaiCompatible.ts';
|
|
import { TurnEngine, type EngineLog, type EngineTiming } from './turns.ts';
|
|
|
|
export interface Bridge {
|
|
app: FastifyInstance;
|
|
stt: SttAdapter;
|
|
db: Db;
|
|
devices: Devices;
|
|
engine: TurnEngine;
|
|
close(): Promise<void>;
|
|
}
|
|
|
|
/** Pino redaction: belt and braces, since we never pass these fields to the logger anyway. */
|
|
export const LOG_REDACT = [
|
|
'req.headers.authorization',
|
|
'headers.authorization',
|
|
'token',
|
|
'code',
|
|
'text',
|
|
'input_text',
|
|
'answer_text',
|
|
'pending_body',
|
|
'body',
|
|
];
|
|
|
|
export function sttFromConfig(config: BridgeConfig): SttAdapter {
|
|
if (config.stt.provider === 'openai-compatible') {
|
|
return new OpenAiCompatibleStt({ baseUrl: config.stt.baseUrl, apiKey: config.stt.apiKey, model: config.stt.model });
|
|
}
|
|
if (config.stt.provider === 'builtin') {
|
|
return new BuiltinWhisperStt({ manifest: config.stt.manifest!, modelDir: config.stt.modelDir });
|
|
}
|
|
return new NoSttAdapter();
|
|
}
|
|
|
|
export async function createBridge(
|
|
config: BridgeConfig,
|
|
overrides: { hermes?: HermesAdapter; stt?: SttAdapter; timing?: EngineTiming; logger?: object | boolean; cancelSweepMs?: number } = {},
|
|
): Promise<Bridge> {
|
|
const db = openDb(config.dbPath);
|
|
const hermes =
|
|
overrides.hermes ??
|
|
new HermesRunsAdapter({
|
|
baseUrl: config.hermes.baseUrl,
|
|
apiKey: config.hermes.apiKey,
|
|
requestTimeoutMs: config.hermes.requestTimeoutMs,
|
|
});
|
|
// One adapter per extra agent: its own address under the same gateway, and its own key.
|
|
const agents = new Map<string, HermesAdapter>(
|
|
config.agents.slice(1).map((agent) => [
|
|
agent.id,
|
|
new HermesRunsAdapter({ baseUrl: agent.baseUrl, apiKey: agent.apiKey, requestTimeoutMs: config.hermes.requestTimeoutMs }),
|
|
]),
|
|
);
|
|
const stt = overrides.stt ?? sttFromConfig(config);
|
|
const devices = new Devices(db, config);
|
|
|
|
const given = overrides.logger;
|
|
const logger: Logger =
|
|
given && typeof given === 'object'
|
|
? typeof (given as Logger).child === 'function'
|
|
? (given as Logger)
|
|
: pino(given as object)
|
|
: pino({ level: 'silent' });
|
|
const engine = new TurnEngine({ db, config, devices, hermes, agents, stt, log: logger as unknown as EngineLog, timing: overrides.timing });
|
|
const app = await buildServer({ config, devices, hermes, stt, engine, logger });
|
|
|
|
engine.recover();
|
|
engine.purgeExpired();
|
|
const purge = setInterval(() => engine.purgeExpired(), 60_000);
|
|
purge.unref();
|
|
// Picks up revocations made by the operator CLI, which writes to the database directly.
|
|
const cancelSweep = setInterval(() => engine.applyCancelFlags(), overrides.cancelSweepMs ?? 1000);
|
|
cancelSweep.unref();
|
|
|
|
return {
|
|
app,
|
|
stt,
|
|
db,
|
|
devices,
|
|
engine,
|
|
async close() {
|
|
clearInterval(purge);
|
|
clearInterval(cancelSweep);
|
|
await app.close();
|
|
await engine.shutdown();
|
|
await stt.close?.();
|
|
db.close();
|
|
},
|
|
};
|
|
}
|