refactor #22

Merged
hzhang merged 33 commits from refactor into main 2026-04-10 07:49:57 +00:00
2 changed files with 28 additions and 7 deletions
Showing only changes of commit 74e6d61d4d - Show all commits

View File

@@ -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`);

View File

@@ -24,13 +24,27 @@ export function registerMessageReceivedHook(deps: Deps): void {
const e = event as Record<string, unknown>;
const c = ctx as Record<string, unknown>;
// 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<string, unknown> | 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<string, unknown> | undefined;
const convInfo = metadata?.conversation_info as Record<string, unknown> | undefined;
const raw = String(convInfo?.channel_id ?? metadata?.channelId ?? "");