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 = {
|
type DecisionRecord = {
|
||||||
decision: Decision;
|
decision: Decision;
|
||||||
createdAt: number;
|
createdAt: number;
|
||||||
|
needsRestore?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type PolicyState = {
|
type PolicyState = {
|
||||||
@@ -69,30 +70,29 @@ function extractUntrustedConversationInfo(text: string): Record<string, unknown>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function deriveDecisionInputFromAgentCtx(
|
function deriveDecisionInputFromPrompt(
|
||||||
ctx: Record<string, unknown>,
|
prompt: string,
|
||||||
): { channel: string; channelId?: string; senderId?: string; content: string } {
|
messageProvider?: string,
|
||||||
const channel = normalizeChannel(ctx);
|
): {
|
||||||
const content = typeof ctx.input === "string" ? ctx.input : "";
|
channel: string;
|
||||||
const conv = extractUntrustedConversationInfo(content) || {};
|
channelId?: string;
|
||||||
const channelIdRaw =
|
senderId?: string;
|
||||||
(typeof ctx.channelId === "string" && ctx.channelId) ||
|
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.channel_id === "string" && conv.channel_id) ||
|
||||||
(typeof conv.chat_id === "string" && conv.chat_id.startsWith("channel:")
|
(typeof conv.chat_id === "string" && conv.chat_id.startsWith("channel:")
|
||||||
? conv.chat_id.slice("channel:".length)
|
? conv.chat_id.slice("channel:".length)
|
||||||
: undefined);
|
: undefined);
|
||||||
const senderIdRaw =
|
const senderId =
|
||||||
(typeof ctx.senderId === "string" && ctx.senderId) ||
|
|
||||||
(typeof conv.sender_id === "string" && conv.sender_id) ||
|
(typeof conv.sender_id === "string" && conv.sender_id) ||
|
||||||
(typeof conv.sender === "string" && conv.sender) ||
|
(typeof conv.sender === "string" && conv.sender) ||
|
||||||
undefined;
|
undefined;
|
||||||
|
|
||||||
return {
|
return { channel, channelId, senderId, content: prompt, conv };
|
||||||
channel,
|
|
||||||
channelId: channelIdRaw,
|
|
||||||
senderId: senderIdRaw,
|
|
||||||
content,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function pruneDecisionMap(now = Date.now()) {
|
function pruneDecisionMap(now = Date.now()) {
|
||||||
@@ -169,7 +169,7 @@ function shouldDebugLog(cfg: DebugConfig, channelId?: string): boolean {
|
|||||||
if (!cfg.enableDebugLogs) return false;
|
if (!cfg.enableDebugLogs) return false;
|
||||||
const allow = Array.isArray(cfg.debugLogChannelIds) ? cfg.debugLogChannelIds : [];
|
const allow = Array.isArray(cfg.debugLogChannelIds) ? cfg.debugLogChannelIds : [];
|
||||||
if (allow.length === 0) return true;
|
if (allow.length === 0) return true;
|
||||||
if (!channelId) return false;
|
if (!channelId) return true; // 允许打印,方便排查 channelId 为空的场景
|
||||||
return allow.includes(channelId);
|
return allow.includes(channelId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -363,46 +363,76 @@ export default {
|
|||||||
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig) as WhisperGateConfig & DebugConfig;
|
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig) as WhisperGateConfig & DebugConfig;
|
||||||
ensurePolicyStateLoaded(api, live);
|
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 prompt = ((event as Record<string, unknown>).prompt as string) || "";
|
||||||
const conv = extractUntrustedConversationInfo(prompt) || {};
|
|
||||||
const senderId =
|
if (live.enableDebugLogs) {
|
||||||
(typeof conv.sender_id === "string" && conv.sender_id) ||
|
api.logger.info(
|
||||||
(typeof conv.sender === "string" && conv.sender) ||
|
`whispergate: DEBUG_BEFORE_MODEL_RESOLVE ctx=${JSON.stringify({ sessionKey: ctx.sessionKey, messageProvider: ctx.messageProvider, agentId: ctx.agentId })} ` +
|
||||||
undefined;
|
`promptPreview=${prompt.slice(0, 300)}`,
|
||||||
const channelId =
|
);
|
||||||
(typeof conv.channel_id === "string" && conv.channel_id) ||
|
}
|
||||||
(typeof (conv as Record<string, unknown>).conversation_label === "string"
|
|
||||||
? undefined
|
const derived = deriveDecisionInputFromPrompt(prompt, ctx.messageProvider);
|
||||||
: undefined);
|
// 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);
|
let rec = sessionDecision.get(key);
|
||||||
if (!rec || Date.now() - rec.createdAt > DECISION_TTL_MS) {
|
if (!rec || Date.now() - rec.createdAt > DECISION_TTL_MS) {
|
||||||
if (rec) sessionDecision.delete(key);
|
if (rec) sessionDecision.delete(key);
|
||||||
const decision = evaluateDecision({
|
const decision = evaluateDecision({
|
||||||
config: live,
|
config: live,
|
||||||
channel,
|
channel: derived.channel,
|
||||||
channelId,
|
channelId: derived.channelId,
|
||||||
channelPolicies: policyState.channelPolicies,
|
channelPolicies: policyState.channelPolicies,
|
||||||
senderId,
|
senderId: derived.senderId,
|
||||||
content: prompt,
|
content: derived.content,
|
||||||
});
|
});
|
||||||
rec = { decision, createdAt: Date.now() };
|
rec = { decision, createdAt: Date.now() };
|
||||||
sessionDecision.set(key, rec);
|
sessionDecision.set(key, rec);
|
||||||
pruneDecisionMap();
|
pruneDecisionMap();
|
||||||
if (shouldDebugLog(live, channelId)) {
|
if (shouldDebugLog(live, derived.channelId)) {
|
||||||
api.logger.info(
|
api.logger.info(
|
||||||
`whispergate: debug before_model_resolve recompute session=${key} senderId=${senderId} decision=${decision.reason} ` +
|
`whispergate: debug before_model_resolve recompute session=${key} ` +
|
||||||
`shouldNoReply=${decision.shouldUseNoReply} shouldInject=${decision.shouldInjectEndMarkerPrompt}`,
|
`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(
|
api.logger.info(
|
||||||
`whispergate: override model for session=${key}, provider=${live.noReplyProvider}, model=${live.noReplyModel}, reason=${rec.decision.reason}`,
|
`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 || Date.now() - rec.createdAt > DECISION_TTL_MS) {
|
||||||
if (rec) sessionDecision.delete(key);
|
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 prompt = ((event as Record<string, unknown>).prompt as string) || "";
|
||||||
const conv = extractUntrustedConversationInfo(prompt) || {};
|
const derived = deriveDecisionInputFromPrompt(prompt, ctx.messageProvider);
|
||||||
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 decision = evaluateDecision({
|
const decision = evaluateDecision({
|
||||||
config: live,
|
config: live,
|
||||||
channel,
|
channel: derived.channel,
|
||||||
channelId,
|
channelId: derived.channelId,
|
||||||
channelPolicies: policyState.channelPolicies,
|
channelPolicies: policyState.channelPolicies,
|
||||||
senderId,
|
senderId: derived.senderId,
|
||||||
content: prompt,
|
content: derived.content,
|
||||||
});
|
});
|
||||||
rec = { decision, createdAt: Date.now() };
|
rec = { decision, createdAt: Date.now() };
|
||||||
if (shouldDebugLog(live, channelId)) {
|
if (shouldDebugLog(live, derived.channelId)) {
|
||||||
api.logger.info(
|
api.logger.info(
|
||||||
`whispergate: debug before_prompt_build recompute session=${key} senderId=${senderId} decision=${decision.reason} ` +
|
`whispergate: debug before_prompt_build recompute session=${key} ` +
|
||||||
`shouldNoReply=${decision.shouldUseNoReply} shouldInject=${decision.shouldInjectEndMarkerPrompt}`,
|
`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