diff --git a/plugin/hooks/agent-end.ts b/plugin/hooks/agent-end.ts index 48e0d22..12c9b5e 100644 --- a/plugin/hooks/agent-end.ts +++ b/plugin/hooks/agent-end.ts @@ -7,6 +7,7 @@ import { getAnchor, advanceSpeaker, wakeFromDormant, + isDormant, hasSpeakers, getDebugInfo, isTurnPending, @@ -175,10 +176,16 @@ export function registerAgentEndHook(deps: AgentEndDeps): InterruptFn { }); if (interrupted) { - api.logger.info(`dirigent: tail-match interrupted channel=${channelId} — wake-from-dormant`); - const first = wakeFromDormant(channelId); - if (first) await triggerNextSpeaker(channelId, first); - return; + if (isDormant(channelId)) { + // Channel is dormant: a new external message woke it — restart from first speaker + api.logger.info(`dirigent: tail-match interrupted (dormant) channel=${channelId} — waking`); + const first = wakeFromDormant(channelId); + if (first) await triggerNextSpeaker(channelId, first); + return; + } + // Not dormant: interrupt was a spurious trigger (e.g. moderator bot message). + // Fall through to normal advance so the turn cycle continues correctly. + api.logger.info(`dirigent: tail-match interrupted (non-dormant) channel=${channelId} — advancing normally`); } if (!matched) { api.logger.warn(`dirigent: tail-match timeout channel=${channelId} agentId=${agentId} — advancing anyway`); diff --git a/plugin/hooks/message-received.ts b/plugin/hooks/message-received.ts index 007b1db..0201cfb 100644 --- a/plugin/hooks/message-received.ts +++ b/plugin/hooks/message-received.ts @@ -24,13 +24,27 @@ export function registerMessageReceivedHook(deps: Deps): void { const e = event as Record; const c = ctx as Record; - // Extract Discord channel ID from session key or event metadata + // Extract Discord channel ID from ctx or event metadata let channelId: string | undefined; - if (typeof c.sessionKey === "string") { + + // ctx.channelId may be bare "1234567890" or "channel:1234567890" + if (typeof c.channelId === "string") { + const bare = c.channelId.match(/^(\d+)$/)?.[1] ?? c.channelId.match(/:(\d+)$/)?.[1]; + if (bare) channelId = bare; + } + // fallback: sessionKey (per-session api instances) + if (!channelId && typeof c.sessionKey === "string") { channelId = parseDiscordChannelId(c.sessionKey); } + // fallback: metadata.to / originatingTo = "channel:1234567890" + if (!channelId) { + const metadata = e.metadata as Record | undefined; + const to = String(metadata?.to ?? metadata?.originatingTo ?? ""); + const toMatch = to.match(/:(\d+)$/); + if (toMatch) channelId = toMatch[1]; + } + // fallback: conversation_info if (!channelId) { - // Try from event metadata (conversation_info channel_id field) const metadata = e.metadata as Record | undefined; const convInfo = metadata?.conversation_info as Record | undefined; const raw = String(convInfo?.channel_id ?? metadata?.channelId ?? "");