feat(policy-file): move channel overrides to standalone channelPoliciesFile with hot reload
This commit is contained in:
@@ -29,7 +29,7 @@ Optional:
|
||||
- `listMode` (`human-list` | `agent-list`, default `human-list`)
|
||||
- `humanList` (default [])
|
||||
- `agentList` (default [])
|
||||
- `channelPolicies` (per-channel overrides by channelId)
|
||||
- `channelPoliciesFile` (per-channel overrides in a standalone JSON file)
|
||||
- `bypassUserIds` (deprecated alias of `humanList`)
|
||||
- `endSymbols` (default ["🔚"])
|
||||
- `enableDiscordControlTool` (default true)
|
||||
@@ -37,6 +37,8 @@ Optional:
|
||||
- `discordControlApiToken`
|
||||
- `discordControlCallerId`
|
||||
|
||||
Per-channel policy file example: `docs/channel-policies.example.json`.
|
||||
|
||||
## Optional tool: `discord_control`
|
||||
|
||||
This plugin now registers an optional tool named `discord_control`.
|
||||
|
||||
@@ -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?.(
|
||||
|
||||
@@ -13,20 +13,7 @@
|
||||
"listMode": { "type": "string", "enum": ["human-list", "agent-list"], "default": "human-list" },
|
||||
"humanList": { "type": "array", "items": { "type": "string" }, "default": [] },
|
||||
"agentList": { "type": "array", "items": { "type": "string" }, "default": [] },
|
||||
"channelPolicies": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"listMode": { "type": "string", "enum": ["human-list", "agent-list"] },
|
||||
"humanList": { "type": "array", "items": { "type": "string" } },
|
||||
"agentList": { "type": "array", "items": { "type": "string" } },
|
||||
"endSymbols": { "type": "array", "items": { "type": "string" } }
|
||||
}
|
||||
},
|
||||
"default": {}
|
||||
},
|
||||
"channelPoliciesFile": { "type": "string", "default": "~/.openclaw/whispergate-channel-policies.json" },
|
||||
"bypassUserIds": { "type": "array", "items": { "type": "string" }, "default": [] },
|
||||
"endSymbols": { "type": "array", "items": { "type": "string" }, "default": ["🔚"] },
|
||||
"noReplyProvider": { "type": "string" },
|
||||
|
||||
@@ -4,15 +4,7 @@ export type WhisperGateConfig = {
|
||||
listMode?: "human-list" | "agent-list";
|
||||
humanList?: string[];
|
||||
agentList?: string[];
|
||||
channelPolicies?: Record<
|
||||
string,
|
||||
{
|
||||
listMode?: "human-list" | "agent-list";
|
||||
humanList?: string[];
|
||||
agentList?: string[];
|
||||
endSymbols?: string[];
|
||||
}
|
||||
>;
|
||||
channelPoliciesFile?: string;
|
||||
// backward compatibility
|
||||
bypassUserIds?: string[];
|
||||
endSymbols?: string[];
|
||||
@@ -20,6 +12,13 @@ export type WhisperGateConfig = {
|
||||
noReplyModel: string;
|
||||
};
|
||||
|
||||
export type ChannelPolicy = {
|
||||
listMode?: "human-list" | "agent-list";
|
||||
humanList?: string[];
|
||||
agentList?: string[];
|
||||
endSymbols?: string[];
|
||||
};
|
||||
|
||||
export type Decision = {
|
||||
shouldUseNoReply: boolean;
|
||||
reason: string;
|
||||
@@ -30,7 +29,7 @@ function getLastChar(input: string): string {
|
||||
return t.length ? t[t.length - 1] : "";
|
||||
}
|
||||
|
||||
function resolvePolicy(config: WhisperGateConfig, channelId?: string) {
|
||||
function resolvePolicy(config: WhisperGateConfig, channelId?: string, channelPolicies?: Record<string, ChannelPolicy>) {
|
||||
const globalMode = config.listMode || "human-list";
|
||||
const globalHuman = config.humanList || config.bypassUserIds || [];
|
||||
const globalAgent = config.agentList || [];
|
||||
@@ -40,7 +39,7 @@ function resolvePolicy(config: WhisperGateConfig, channelId?: string) {
|
||||
return { listMode: globalMode, humanList: globalHuman, agentList: globalAgent, endSymbols: globalEnd };
|
||||
}
|
||||
|
||||
const cp = config.channelPolicies || {};
|
||||
const cp = channelPolicies || {};
|
||||
const scoped = cp[channelId];
|
||||
if (!scoped) {
|
||||
return { listMode: globalMode, humanList: globalHuman, agentList: globalAgent, endSymbols: globalEnd };
|
||||
@@ -58,6 +57,7 @@ export function evaluateDecision(params: {
|
||||
config: WhisperGateConfig;
|
||||
channel?: string;
|
||||
channelId?: string;
|
||||
channelPolicies?: Record<string, ChannelPolicy>;
|
||||
senderId?: string;
|
||||
content?: string;
|
||||
}): Decision {
|
||||
@@ -72,7 +72,7 @@ export function evaluateDecision(params: {
|
||||
return { shouldUseNoReply: false, reason: "non_discord" };
|
||||
}
|
||||
|
||||
const policy = resolvePolicy(config, params.channelId);
|
||||
const policy = resolvePolicy(config, params.channelId, params.channelPolicies);
|
||||
|
||||
const mode = policy.listMode;
|
||||
const humanList = policy.humanList;
|
||||
|
||||
Reference in New Issue
Block a user