feat(runtime): add talk mode and capture tools
This commit is contained in:
+58
-3
@@ -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, '\\$&');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user