feat(runtime): add talk mode and capture tools

This commit is contained in:
William Valentin
2026-02-16 10:17:24 -08:00
parent a9b38150c0
commit 83b8e38b11
12 changed files with 391 additions and 4 deletions
+64
View File
@@ -544,3 +544,67 @@ describe('daemon audio routing integration', () => {
expect(atts?.some(a => a.mimeType === 'image/jpeg')).toBe(true);
});
});
describe('daemon talk mode (voice wake) integration', () => {
afterEach(() => {
vi.restoreAllMocks();
});
it('ignores messages until wake phrase is used', async () => {
const processSpy = vi.spyOn(AgentOrchestrator.prototype, 'process').mockResolvedValue('ok');
const session = {
id: 'telegram:user-talk-1',
addMessage: vi.fn(),
getHistory: vi.fn(() => []),
clear: vi.fn(),
replaceHistory: vi.fn(),
getConfig: vi.fn(() => undefined),
setConfig: vi.fn(),
deleteConfig: vi.fn(),
};
const router = createMessageRouter({
sessionManager: { getSession: vi.fn(() => session) } as unknown as MessageRouterDeps['sessionManager'],
modelRouter: {
getAvailableTiers: () => ['fast', 'default', 'complex', 'local'],
getAllLabels: () => ({ fast: 'fast', default: 'default', complex: 'complex', local: 'local' }),
getLabel: (tier: string) => tier,
} as unknown as MessageRouterDeps['modelRouter'],
systemPrompt: 'test prompt',
toolRegistry: { clone() { return this; }, register: vi.fn() } as unknown as MessageRouterDeps['toolRegistry'],
toolExecutor: {} as unknown as MessageRouterDeps['toolExecutor'],
config: {
agents: {
primary_tier: 'default',
delegation: { compaction: 'fast', memory_extraction: 'fast', classification: 'fast', tool_summarisation: 'fast', complex_reasoning: 'complex' },
max_delegation_depth: 3,
max_iterations: 10,
},
compaction: { enabled: false },
models: { default: { provider: 'anthropic', model: 'claude' } },
audio: { talk_mode: { enabled: true, wake_phrase: 'hey flynn', timeout_ms: 120000, allow_manual_toggle: true } },
} as unknown as MessageRouterDeps['config'],
});
const reply = vi.fn(async (_message: OutboundMessage) => {});
await router.handler({
id: 'm-talk-1',
channel: 'telegram',
senderId: 'user-talk-1',
text: 'hello there',
timestamp: Date.now(),
} as MessageRouterInput, reply);
expect(processSpy).not.toHaveBeenCalled();
await router.handler({
id: 'm-talk-2',
channel: 'telegram',
senderId: 'user-talk-1',
text: 'hey flynn what time is it?',
timestamp: Date.now(),
} as MessageRouterInput, reply);
expect(processSpy).toHaveBeenCalledOnce();
expect(processSpy).toHaveBeenCalledWith('what time is it?', undefined);
});
});
+58 -3
View File
@@ -70,6 +70,7 @@ export function createMessageRouter(deps: {
} {
// Cache agents by session ID + agent config name to avoid recreating on every message
const agents = new Map<string, { orchestrator: AgentOrchestrator; collector: OutboundAttachmentCollector }>();
const talkModeUntil = new Map<string, number>();
function getOrCreateAgent(channel: string, senderId: string, metadata?: Record<string, unknown>, agentOverride?: string): { orchestrator: AgentOrchestrator; collector: OutboundAttachmentCollector } {
// Resolve agent config name via routing (sender → channel → default fallback)
@@ -246,10 +247,60 @@ export function createMessageRouter(deps: {
}
const handler = async (msg: InboundMessage, reply: (response: OutboundMessage) => Promise<void>): Promise<void> => {
let incomingText = msg.text;
const talkMode = deps.config.audio?.talk_mode;
if (talkMode?.enabled && incomingText.trim().length > 0) {
const key = `${msg.channel}:${msg.senderId}`;
const now = Date.now();
const timeoutMs = talkMode.timeout_ms;
const currentUntil = talkModeUntil.get(key) ?? 0;
const lower = incomingText.trim().toLowerCase();
if (talkMode.allow_manual_toggle) {
if (lower === '/talk on') {
talkModeUntil.set(key, now + timeoutMs);
await reply({ text: `Talk mode enabled for ${Math.ceil(timeoutMs / 1000)}s.`, replyTo: msg.id });
return;
}
if (lower === '/talk off') {
talkModeUntil.delete(key);
await reply({ text: 'Talk mode disabled.', replyTo: msg.id });
return;
}
if (lower === '/talk status') {
if (currentUntil <= now) {
await reply({ text: 'Talk mode is idle (wake phrase required).', replyTo: msg.id });
} else {
await reply({ text: `Talk mode active for ${Math.ceil((currentUntil - now) / 1000)}s.`, replyTo: msg.id });
}
return;
}
}
const phrase = talkMode.wake_phrase.trim();
const wakeRegex = phrase
? new RegExp(`^\\s*${escapeRegex(phrase)}(?:[\\s,:!.-]+)?`, 'i')
: null;
const wakeMatched = Boolean(wakeRegex && wakeRegex.test(incomingText));
if (wakeMatched && wakeRegex) {
talkModeUntil.set(key, now + timeoutMs);
incomingText = incomingText.replace(wakeRegex, '').trim();
if (!incomingText) {
await reply({ text: `Listening. Talk mode active for ${Math.ceil(timeoutMs / 1000)}s.`, replyTo: msg.id });
return;
}
} else if (currentUntil > now) {
talkModeUntil.set(key, now + timeoutMs);
} else {
return;
}
}
let intentAgentOverride: string | undefined;
let intentSkillOverride: string | undefined;
if (deps.config.intents?.enabled && deps.intentRegistry) {
const intentMatch = deps.intentRegistry.match(msg.text);
const intentMatch = deps.intentRegistry.match(incomingText);
if (intentMatch?.rule.target.type === 'agent') {
let confidence = intentMatch.score;
@@ -298,7 +349,7 @@ export function createMessageRouter(deps: {
const commandInput = msg.metadata?.isCommand && typeof msg.metadata.command === 'string'
? `/${msg.metadata.command}${msg.metadata.commandArgs ? ` ${msg.metadata.commandArgs}` : ''}`
: msg.text;
: incomingText;
if (deps.commandRegistry && deps.commandRegistry.isCommand(commandInput)) {
const session = deps.sessionManager.getSession(msg.channel, msg.senderId);
@@ -604,7 +655,7 @@ export function createMessageRouter(deps: {
const supportsAudioOverride = (tierConfig as Record<string, unknown> | undefined)?.supports_audio as boolean | undefined;
const nativeAudioSupported = supportsAudioInput(modelProvider, modelName, supportsAudioOverride);
let messageText = msg.text;
let messageText = incomingText;
let attachments = msg.attachments;
const audioAttachments = (msg.attachments ?? []).filter((a: Attachment) => isSupportedAudio(a));
@@ -666,3 +717,7 @@ export function createMessageRouter(deps: {
return { handler, agents };
}
function escapeRegex(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}