132 lines
4.3 KiB
TypeScript
132 lines
4.3 KiB
TypeScript
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
|
import { evaluateDecision, type Decision, type WhisperGateConfig } from "./rules.js";
|
|
|
|
type DecisionRecord = {
|
|
decision: Decision;
|
|
createdAt: number;
|
|
};
|
|
|
|
const sessionDecision = new Map<string, DecisionRecord>();
|
|
const MAX_SESSION_DECISIONS = 2000;
|
|
const DECISION_TTL_MS = 5 * 60 * 1000;
|
|
const END_MARKER_INSTRUCTION = "你的这次发言必须以🔚作为结尾。";
|
|
|
|
function normalizeChannel(ctx: Record<string, unknown>): string {
|
|
const candidates = [ctx.commandSource, ctx.messageProvider, ctx.channelId, ctx.channel];
|
|
for (const c of candidates) {
|
|
if (typeof c === "string" && c.trim()) return c.trim().toLowerCase();
|
|
}
|
|
return "";
|
|
}
|
|
|
|
function normalizeSender(event: Record<string, unknown>, ctx: Record<string, unknown>): string | undefined {
|
|
const direct = [ctx.senderId, ctx.from, event.from];
|
|
for (const v of direct) {
|
|
if (typeof v === "string" && v.trim()) return v.trim();
|
|
}
|
|
|
|
const meta = (event.metadata || ctx.metadata) as Record<string, unknown> | undefined;
|
|
if (!meta) return undefined;
|
|
const metaCandidates = [meta.senderId, meta.sender_id, meta.userId, meta.user_id];
|
|
for (const v of metaCandidates) {
|
|
if (typeof v === "string" && v.trim()) return v.trim();
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
function pruneDecisionMap(now = Date.now()) {
|
|
for (const [k, v] of sessionDecision.entries()) {
|
|
if (now - v.createdAt > DECISION_TTL_MS) sessionDecision.delete(k);
|
|
}
|
|
|
|
if (sessionDecision.size <= MAX_SESSION_DECISIONS) return;
|
|
const keys = sessionDecision.keys();
|
|
while (sessionDecision.size > MAX_SESSION_DECISIONS) {
|
|
const k = keys.next();
|
|
if (k.done) break;
|
|
sessionDecision.delete(k.value);
|
|
}
|
|
}
|
|
|
|
function shouldInjectEndMarker(reason: string): boolean {
|
|
return reason === "bypass_sender" || reason.startsWith("end_symbol:");
|
|
}
|
|
|
|
export default {
|
|
id: "whispergate",
|
|
name: "WhisperGate",
|
|
register(api: OpenClawPluginApi) {
|
|
const config = (api.pluginConfig || {}) as WhisperGateConfig;
|
|
|
|
api.registerHook("message:received", async (event, ctx) => {
|
|
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 decision = evaluateDecision({ config, channel, 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}`,
|
|
);
|
|
} catch (err) {
|
|
api.logger.warn(`whispergate: message hook failed: ${String(err)}`);
|
|
}
|
|
});
|
|
|
|
api.on("before_model_resolve", async (_event, ctx) => {
|
|
const key = ctx.sessionKey;
|
|
if (!key) return;
|
|
|
|
const rec = sessionDecision.get(key);
|
|
if (!rec) return;
|
|
if (Date.now() - rec.createdAt > DECISION_TTL_MS) {
|
|
sessionDecision.delete(key);
|
|
return;
|
|
}
|
|
|
|
if (!rec.decision.shouldUseNoReply) return;
|
|
|
|
// no-reply path is consumed here
|
|
sessionDecision.delete(key);
|
|
api.logger.info(
|
|
`whispergate: override model for session=${key}, provider=${config.noReplyProvider}, model=${config.noReplyModel}, reason=${rec.decision.reason}`,
|
|
);
|
|
|
|
return {
|
|
providerOverride: config.noReplyProvider,
|
|
modelOverride: config.noReplyModel,
|
|
};
|
|
});
|
|
|
|
api.on("before_prompt_build", async (_event, ctx) => {
|
|
const key = ctx.sessionKey;
|
|
if (!key) return;
|
|
const rec = sessionDecision.get(key);
|
|
if (!rec) return;
|
|
|
|
if (Date.now() - rec.createdAt > DECISION_TTL_MS) {
|
|
sessionDecision.delete(key);
|
|
return;
|
|
}
|
|
|
|
// consume non-no-reply paths here to avoid stale carry-over
|
|
sessionDecision.delete(key);
|
|
|
|
if (!shouldInjectEndMarker(rec.decision.reason)) return;
|
|
|
|
api.logger.info(`whispergate: prepend end marker instruction for session=${key}, reason=${rec.decision.reason}`);
|
|
return {
|
|
prependContext: END_MARKER_INSTRUCTION,
|
|
};
|
|
});
|
|
},
|
|
};
|