Compare commits
2 Commits
6b3d89634a
...
4622173787
| Author | SHA1 | Date | |
|---|---|---|---|
| 4622173787 | |||
| 211a94233f |
139
plugin/index.ts
139
plugin/index.ts
@@ -8,6 +8,7 @@ type DiscordControlAction = "channel-private-create" | "channel-private-update"
|
||||
type DecisionRecord = {
|
||||
decision: Decision;
|
||||
createdAt: number;
|
||||
needsRestore?: boolean;
|
||||
};
|
||||
|
||||
type PolicyState = {
|
||||
@@ -69,30 +70,29 @@ function extractUntrustedConversationInfo(text: string): Record<string, unknown>
|
||||
}
|
||||
}
|
||||
|
||||
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) ||
|
||||
function deriveDecisionInputFromPrompt(
|
||||
prompt: string,
|
||||
messageProvider?: string,
|
||||
): {
|
||||
channel: string;
|
||||
channelId?: string;
|
||||
senderId?: string;
|
||||
content: string;
|
||||
conv: Record<string, unknown>;
|
||||
} {
|
||||
const conv = extractUntrustedConversationInfo(prompt) || {};
|
||||
const channel = (messageProvider || "").toLowerCase();
|
||||
const 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) ||
|
||||
const senderId =
|
||||
(typeof conv.sender_id === "string" && conv.sender_id) ||
|
||||
(typeof conv.sender === "string" && conv.sender) ||
|
||||
undefined;
|
||||
|
||||
return {
|
||||
channel,
|
||||
channelId: channelIdRaw,
|
||||
senderId: senderIdRaw,
|
||||
content,
|
||||
};
|
||||
return { channel, channelId, senderId, content: prompt, conv };
|
||||
}
|
||||
|
||||
function pruneDecisionMap(now = Date.now()) {
|
||||
@@ -169,7 +169,7 @@ function shouldDebugLog(cfg: DebugConfig, channelId?: string): boolean {
|
||||
if (!cfg.enableDebugLogs) return false;
|
||||
const allow = Array.isArray(cfg.debugLogChannelIds) ? cfg.debugLogChannelIds : [];
|
||||
if (allow.length === 0) return true;
|
||||
if (!channelId) return false;
|
||||
if (!channelId) return true; // 允许打印,方便排查 channelId 为空的场景
|
||||
return allow.includes(channelId);
|
||||
}
|
||||
|
||||
@@ -363,46 +363,76 @@ export default {
|
||||
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig) as WhisperGateConfig & DebugConfig;
|
||||
ensurePolicyStateLoaded(api, live);
|
||||
|
||||
// In before_model_resolve, ctx only has: agentId, sessionKey, sessionId, workspaceDir, messageProvider.
|
||||
// senderId/channelId/input are NOT available. Use event.prompt (user message incl. untrusted metadata).
|
||||
const channel = (ctx.messageProvider || "").toLowerCase();
|
||||
if (live.discordOnly !== false && channel !== "discord") return;
|
||||
|
||||
const prompt = ((event as Record<string, unknown>).prompt as string) || "";
|
||||
const conv = extractUntrustedConversationInfo(prompt) || {};
|
||||
const senderId =
|
||||
(typeof conv.sender_id === "string" && conv.sender_id) ||
|
||||
(typeof conv.sender === "string" && conv.sender) ||
|
||||
undefined;
|
||||
const channelId =
|
||||
(typeof conv.channel_id === "string" && conv.channel_id) ||
|
||||
(typeof (conv as Record<string, unknown>).conversation_label === "string"
|
||||
? undefined
|
||||
: undefined);
|
||||
|
||||
if (live.enableDebugLogs) {
|
||||
api.logger.info(
|
||||
`whispergate: DEBUG_BEFORE_MODEL_RESOLVE ctx=${JSON.stringify({ sessionKey: ctx.sessionKey, messageProvider: ctx.messageProvider, agentId: ctx.agentId })} ` +
|
||||
`promptPreview=${prompt.slice(0, 300)}`,
|
||||
);
|
||||
}
|
||||
|
||||
const derived = deriveDecisionInputFromPrompt(prompt, ctx.messageProvider);
|
||||
// Only proceed if: discord channel AND prompt contains untrusted metadata
|
||||
const hasConvMarker = prompt.includes("Conversation info (untrusted metadata):");
|
||||
if (live.discordOnly !== false && (!hasConvMarker || derived.channel !== "discord")) return;
|
||||
|
||||
let rec = sessionDecision.get(key);
|
||||
if (!rec || Date.now() - rec.createdAt > DECISION_TTL_MS) {
|
||||
if (rec) sessionDecision.delete(key);
|
||||
const decision = evaluateDecision({
|
||||
config: live,
|
||||
channel,
|
||||
channelId,
|
||||
channel: derived.channel,
|
||||
channelId: derived.channelId,
|
||||
channelPolicies: policyState.channelPolicies,
|
||||
senderId,
|
||||
content: prompt,
|
||||
senderId: derived.senderId,
|
||||
content: derived.content,
|
||||
});
|
||||
rec = { decision, createdAt: Date.now() };
|
||||
sessionDecision.set(key, rec);
|
||||
pruneDecisionMap();
|
||||
if (shouldDebugLog(live, channelId)) {
|
||||
if (shouldDebugLog(live, derived.channelId)) {
|
||||
api.logger.info(
|
||||
`whispergate: debug before_model_resolve recompute session=${key} senderId=${senderId} decision=${decision.reason} ` +
|
||||
`shouldNoReply=${decision.shouldUseNoReply} shouldInject=${decision.shouldInjectEndMarkerPrompt}`,
|
||||
`whispergate: debug before_model_resolve recompute session=${key} ` +
|
||||
`channel=${derived.channel} channelId=${derived.channelId ?? ""} senderId=${derived.senderId ?? ""} ` +
|
||||
`convSenderId=${String((derived.conv as Record<string, unknown>).sender_id ?? "")} ` +
|
||||
`convSender=${String((derived.conv as Record<string, unknown>).sender ?? "")} ` +
|
||||
`convChannelId=${String((derived.conv as Record<string, unknown>).channel_id ?? "")} ` +
|
||||
`decision=${decision.reason} shouldNoReply=${decision.shouldUseNoReply} shouldInject=${decision.shouldInjectEndMarkerPrompt}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!rec.decision.shouldUseNoReply) return;
|
||||
if (!rec.decision.shouldUseNoReply) {
|
||||
// 如果之前有 no-reply 执行过,现在不需要了,清除 override 恢复原模型
|
||||
if (rec.needsRestore) {
|
||||
sessionDecision.delete(key);
|
||||
return {
|
||||
providerOverride: undefined,
|
||||
modelOverride: undefined,
|
||||
};
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 标记这次执行了 no-reply,下次需要恢复模型
|
||||
rec.needsRestore = true;
|
||||
sessionDecision.set(key, rec);
|
||||
|
||||
// 无论是否有缓存,只要 debug flag 开启就打印决策详情
|
||||
if (live.enableDebugLogs) {
|
||||
const prompt = ((event as Record<string, unknown>).prompt as string) || "";
|
||||
const hasConvMarker = prompt.includes("Conversation info (untrusted metadata):");
|
||||
api.logger.info(
|
||||
`whispergate: DEBUG_NO_REPLY_TRIGGER session=${key} ` +
|
||||
`channel=${derived.channel} channelId=${derived.channelId ?? ""} senderId=${derived.senderId ?? ""} ` +
|
||||
`convSenderId=${String((derived.conv as Record<string, unknown>).sender_id ?? "")} ` +
|
||||
`convSender=${String((derived.conv as Record<string, unknown>).sender ?? "")} ` +
|
||||
`decision=${rec.decision.reason} ` +
|
||||
`shouldNoReply=${rec.decision.shouldUseNoReply} shouldInject=${rec.decision.shouldInjectEndMarkerPrompt} ` +
|
||||
`hasConvMarker=${hasConvMarker} promptLen=${prompt.length}`,
|
||||
);
|
||||
}
|
||||
|
||||
api.logger.info(
|
||||
`whispergate: override model for session=${key}, provider=${live.noReplyProvider}, model=${live.noReplyModel}, reason=${rec.decision.reason}`,
|
||||
@@ -425,31 +455,26 @@ export default {
|
||||
if (!rec || Date.now() - rec.createdAt > DECISION_TTL_MS) {
|
||||
if (rec) sessionDecision.delete(key);
|
||||
|
||||
// before_prompt_build has event.prompt and event.messages available.
|
||||
const channel = (ctx.messageProvider || "").toLowerCase();
|
||||
const prompt = ((event as Record<string, unknown>).prompt as string) || "";
|
||||
const conv = extractUntrustedConversationInfo(prompt) || {};
|
||||
const senderId =
|
||||
(typeof conv.sender_id === "string" && conv.sender_id) ||
|
||||
(typeof conv.sender === "string" && conv.sender) ||
|
||||
undefined;
|
||||
const channelId =
|
||||
(typeof conv.channel_id === "string" && conv.channel_id) ||
|
||||
undefined;
|
||||
const derived = deriveDecisionInputFromPrompt(prompt, ctx.messageProvider);
|
||||
|
||||
const decision = evaluateDecision({
|
||||
config: live,
|
||||
channel,
|
||||
channelId,
|
||||
channel: derived.channel,
|
||||
channelId: derived.channelId,
|
||||
channelPolicies: policyState.channelPolicies,
|
||||
senderId,
|
||||
content: prompt,
|
||||
senderId: derived.senderId,
|
||||
content: derived.content,
|
||||
});
|
||||
rec = { decision, createdAt: Date.now() };
|
||||
if (shouldDebugLog(live, channelId)) {
|
||||
if (shouldDebugLog(live, derived.channelId)) {
|
||||
api.logger.info(
|
||||
`whispergate: debug before_prompt_build recompute session=${key} senderId=${senderId} decision=${decision.reason} ` +
|
||||
`shouldNoReply=${decision.shouldUseNoReply} shouldInject=${decision.shouldInjectEndMarkerPrompt}`,
|
||||
`whispergate: debug before_prompt_build recompute session=${key} ` +
|
||||
`channel=${derived.channel} channelId=${derived.channelId ?? ""} senderId=${derived.senderId ?? ""} ` +
|
||||
`convSenderId=${String((derived.conv as Record<string, unknown>).sender_id ?? "")} ` +
|
||||
`convSender=${String((derived.conv as Record<string, unknown>).sender ?? "")} ` +
|
||||
`convChannelId=${String((derived.conv as Record<string, unknown>).channel_id ?? "")} ` +
|
||||
`decision=${decision.reason} shouldNoReply=${decision.shouldUseNoReply} shouldInject=${decision.shouldInjectEndMarkerPrompt}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user