fix(hooks): move first-pass decision to before_model_resolve and keep message_received for debug only
This commit is contained in:
148
plugin/index.ts
148
plugin/index.ts
@@ -54,6 +54,47 @@ function normalizeSender(event: Record<string, unknown>, ctx: Record<string, unk
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function extractUntrustedConversationInfo(text: string): Record<string, unknown> | undefined {
|
||||
const marker = "Conversation info (untrusted metadata):";
|
||||
const idx = text.indexOf(marker);
|
||||
if (idx < 0) return undefined;
|
||||
const tail = text.slice(idx + marker.length);
|
||||
const m = tail.match(/```json\s*([\s\S]*?)\s*```/i);
|
||||
if (!m) return undefined;
|
||||
try {
|
||||
const parsed = JSON.parse(m[1]);
|
||||
return parsed && typeof parsed === "object" ? (parsed as Record<string, unknown>) : undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function deriveDecisionInputFromAgentCtx(
|
||||
ctx: Record<string, unknown>,
|
||||
): { channel: string; channelId?: string; senderId?: string; content: string } {
|
||||
const channel = normalizeChannel(ctx);
|
||||
const content = typeof ctx.input === "string" ? ctx.input : "";
|
||||
const conv = extractUntrustedConversationInfo(content) || {};
|
||||
const channelIdRaw =
|
||||
(typeof ctx.channelId === "string" && ctx.channelId) ||
|
||||
(typeof conv.channel_id === "string" && conv.channel_id) ||
|
||||
(typeof conv.chat_id === "string" && conv.chat_id.startsWith("channel:")
|
||||
? conv.chat_id.slice("channel:".length)
|
||||
: undefined);
|
||||
const senderIdRaw =
|
||||
(typeof ctx.senderId === "string" && ctx.senderId) ||
|
||||
(typeof conv.sender_id === "string" && conv.sender_id) ||
|
||||
(typeof conv.sender === "string" && conv.sender) ||
|
||||
undefined;
|
||||
|
||||
return {
|
||||
channel,
|
||||
channelId: channelIdRaw,
|
||||
senderId: senderIdRaw,
|
||||
content,
|
||||
};
|
||||
}
|
||||
|
||||
function pruneDecisionMap(now = Date.now()) {
|
||||
for (const [k, v] of sessionDecision.entries()) {
|
||||
if (now - v.createdAt > DECISION_TTL_MS) sessionDecision.delete(k);
|
||||
@@ -305,36 +346,10 @@ export default {
|
||||
try {
|
||||
const c = (ctx || {}) as Record<string, unknown>;
|
||||
const e = (event || {}) as Record<string, unknown>;
|
||||
const sessionKey = typeof c.sessionKey === "string" ? c.sessionKey : undefined;
|
||||
if (!sessionKey) return;
|
||||
|
||||
const senderId = normalizeSender(e, c);
|
||||
const content = typeof e.content === "string" ? e.content : "";
|
||||
const channel = normalizeChannel(c);
|
||||
const channelId = typeof c.channelId === "string" ? c.channelId : undefined;
|
||||
|
||||
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig);
|
||||
ensurePolicyStateLoaded(api, live);
|
||||
const decision = evaluateDecision({
|
||||
config: live,
|
||||
channel,
|
||||
channelId,
|
||||
channelPolicies: policyState.channelPolicies,
|
||||
senderId,
|
||||
content,
|
||||
});
|
||||
sessionDecision.set(sessionKey, { decision, createdAt: Date.now() });
|
||||
pruneDecisionMap();
|
||||
api.logger.debug?.(
|
||||
`whispergate: session=${sessionKey} sender=${senderId ?? "unknown"} channel=${channel || "unknown"} decision=${decision.reason}`,
|
||||
);
|
||||
|
||||
if (shouldDebugLog(live as DebugConfig, channelId)) {
|
||||
const summary = debugCtxSummary(c, e);
|
||||
api.logger.info(
|
||||
`whispergate: debug message_received session=${sessionKey} decision=${decision.reason} ` +
|
||||
`shouldNoReply=${decision.shouldUseNoReply} shouldInject=${decision.shouldInjectEndMarkerPrompt} ctx=${JSON.stringify(summary)}`,
|
||||
);
|
||||
const preChannelId = typeof c.channelId === "string" ? c.channelId : undefined;
|
||||
const livePre = getLivePluginConfig(api, baseConfig as WhisperGateConfig) as WhisperGateConfig & DebugConfig;
|
||||
if (shouldDebugLog(livePre, preChannelId)) {
|
||||
api.logger.info(`whispergate: debug message_received preflight ctx=${JSON.stringify(debugCtxSummary(c, e))}`);
|
||||
}
|
||||
} catch (err) {
|
||||
api.logger.warn(`whispergate: message hook failed: ${String(err)}`);
|
||||
@@ -345,23 +360,35 @@ export default {
|
||||
const key = ctx.sessionKey;
|
||||
if (!key) return;
|
||||
|
||||
const rec = sessionDecision.get(key);
|
||||
if (!rec) {
|
||||
const liveMiss = getLivePluginConfig(api, baseConfig as WhisperGateConfig) as WhisperGateConfig & DebugConfig;
|
||||
if (shouldDebugLog(liveMiss, ctx.channelId)) {
|
||||
api.logger.info(`whispergate: debug before_model_resolve session=${key} decision=missing`);
|
||||
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig) as WhisperGateConfig & DebugConfig;
|
||||
ensurePolicyStateLoaded(api, live);
|
||||
|
||||
let rec = sessionDecision.get(key);
|
||||
if (!rec || Date.now() - rec.createdAt > DECISION_TTL_MS) {
|
||||
if (rec) sessionDecision.delete(key);
|
||||
const c = (ctx || {}) as Record<string, unknown>;
|
||||
const derived = deriveDecisionInputFromAgentCtx(c);
|
||||
const decision = evaluateDecision({
|
||||
config: live,
|
||||
channel: derived.channel,
|
||||
channelId: derived.channelId,
|
||||
channelPolicies: policyState.channelPolicies,
|
||||
senderId: derived.senderId,
|
||||
content: derived.content,
|
||||
});
|
||||
rec = { decision, createdAt: Date.now() };
|
||||
sessionDecision.set(key, rec);
|
||||
pruneDecisionMap();
|
||||
if (shouldDebugLog(live, derived.channelId ?? ctx.channelId)) {
|
||||
api.logger.info(
|
||||
`whispergate: debug before_model_resolve recompute session=${key} decision=${decision.reason} ` +
|
||||
`shouldNoReply=${decision.shouldUseNoReply} shouldInject=${decision.shouldInjectEndMarkerPrompt}`,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (Date.now() - rec.createdAt > DECISION_TTL_MS) {
|
||||
sessionDecision.delete(key);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!rec.decision.shouldUseNoReply) return;
|
||||
|
||||
sessionDecision.delete(key);
|
||||
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig);
|
||||
api.logger.info(
|
||||
`whispergate: override model for session=${key}, provider=${live.noReplyProvider}, model=${live.noReplyModel}, reason=${rec.decision.reason}`,
|
||||
);
|
||||
@@ -375,24 +402,35 @@ export default {
|
||||
api.on("before_prompt_build", async (_event, ctx) => {
|
||||
const key = ctx.sessionKey;
|
||||
if (!key) return;
|
||||
const rec = sessionDecision.get(key);
|
||||
if (!rec) {
|
||||
const liveMiss = getLivePluginConfig(api, baseConfig as WhisperGateConfig) as WhisperGateConfig & DebugConfig;
|
||||
if (shouldDebugLog(liveMiss, ctx.channelId)) {
|
||||
api.logger.info(`whispergate: debug before_prompt_build session=${key} decision=missing`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (Date.now() - rec.createdAt > DECISION_TTL_MS) {
|
||||
sessionDecision.delete(key);
|
||||
return;
|
||||
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig) as WhisperGateConfig & DebugConfig;
|
||||
ensurePolicyStateLoaded(api, live);
|
||||
|
||||
let rec = sessionDecision.get(key);
|
||||
if (!rec || Date.now() - rec.createdAt > DECISION_TTL_MS) {
|
||||
if (rec) sessionDecision.delete(key);
|
||||
const c = (ctx || {}) as Record<string, unknown>;
|
||||
const derived = deriveDecisionInputFromAgentCtx(c);
|
||||
const decision = evaluateDecision({
|
||||
config: live,
|
||||
channel: derived.channel,
|
||||
channelId: derived.channelId,
|
||||
channelPolicies: policyState.channelPolicies,
|
||||
senderId: derived.senderId,
|
||||
content: derived.content,
|
||||
});
|
||||
rec = { decision, createdAt: Date.now() };
|
||||
if (shouldDebugLog(live, derived.channelId ?? ctx.channelId)) {
|
||||
api.logger.info(
|
||||
`whispergate: debug before_prompt_build recompute session=${key} decision=${decision.reason} ` +
|
||||
`shouldNoReply=${decision.shouldUseNoReply} shouldInject=${decision.shouldInjectEndMarkerPrompt}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
sessionDecision.delete(key);
|
||||
if (!rec.decision.shouldInjectEndMarkerPrompt) {
|
||||
const liveSkip = getLivePluginConfig(api, baseConfig as WhisperGateConfig) as WhisperGateConfig & DebugConfig;
|
||||
if (shouldDebugLog(liveSkip, ctx.channelId)) {
|
||||
if (shouldDebugLog(live, ctx.channelId)) {
|
||||
api.logger.info(
|
||||
`whispergate: debug before_prompt_build session=${key} inject=false reason=${rec.decision.reason}`,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user