feat(policy-file): move channel overrides to standalone channelPoliciesFile with hot reload
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import fs from "node:fs";
|
||||
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
||||
import { evaluateDecision, type Decision, type WhisperGateConfig } from "./rules.js";
|
||||
import { evaluateDecision, type ChannelPolicy, type Decision, type WhisperGateConfig } from "./rules.js";
|
||||
|
||||
type DiscordControlAction = "channel-private-create" | "channel-private-update" | "member-list";
|
||||
|
||||
@@ -13,6 +14,10 @@ const MAX_SESSION_DECISIONS = 2000;
|
||||
const DECISION_TTL_MS = 5 * 60 * 1000;
|
||||
const END_MARKER_INSTRUCTION = "你的这次发言必须以🔚作为结尾。";
|
||||
|
||||
let channelPoliciesCache: Record<string, ChannelPolicy> = {};
|
||||
let channelPoliciesFilePath = "";
|
||||
let channelPoliciesMtimeMs = -1;
|
||||
|
||||
function normalizeChannel(ctx: Record<string, unknown>): string {
|
||||
const candidates = [ctx.commandSource, ctx.messageProvider, ctx.channelId, ctx.channel];
|
||||
for (const c of candidates) {
|
||||
@@ -65,6 +70,31 @@ function getLivePluginConfig(api: OpenClawPluginApi, fallback: WhisperGateConfig
|
||||
return fallback;
|
||||
}
|
||||
|
||||
function loadChannelPolicies(api: OpenClawPluginApi, config: WhisperGateConfig): Record<string, ChannelPolicy> {
|
||||
const file = config.channelPoliciesFile;
|
||||
if (!file) return {};
|
||||
|
||||
const resolved = api.resolvePath(file);
|
||||
try {
|
||||
const stat = fs.statSync(resolved);
|
||||
const mtime = Number(stat.mtimeMs || 0);
|
||||
|
||||
if (resolved === channelPoliciesFilePath && mtime === channelPoliciesMtimeMs) {
|
||||
return channelPoliciesCache;
|
||||
}
|
||||
|
||||
const raw = fs.readFileSync(resolved, "utf8");
|
||||
const parsed = JSON.parse(raw) as Record<string, ChannelPolicy>;
|
||||
channelPoliciesCache = parsed && typeof parsed === "object" ? parsed : {};
|
||||
channelPoliciesFilePath = resolved;
|
||||
channelPoliciesMtimeMs = mtime;
|
||||
return channelPoliciesCache;
|
||||
} catch (err) {
|
||||
api.logger.warn(`whispergate: failed loading channelPoliciesFile=${resolved}: ${String(err)}`);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function pickDefined(input: Record<string, unknown>) {
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const [k, v] of Object.entries(input)) {
|
||||
@@ -181,7 +211,8 @@ export default {
|
||||
const channelId = typeof c.channelId === "string" ? c.channelId : undefined;
|
||||
|
||||
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig);
|
||||
const decision = evaluateDecision({ config: live, channel, channelId, senderId, content });
|
||||
const channelPolicies = loadChannelPolicies(api, live);
|
||||
const decision = evaluateDecision({ config: live, channel, channelId, channelPolicies, senderId, content });
|
||||
sessionDecision.set(sessionKey, { decision, createdAt: Date.now() });
|
||||
pruneDecisionMap();
|
||||
api.logger.debug?.(
|
||||
|
||||
Reference in New Issue
Block a user