feat(policy): add per-channel channelPolicies with hot-reload list mode/lists
This commit is contained in:
@@ -13,6 +13,13 @@
|
|||||||
"humanList": ["561921120408698910"],
|
"humanList": ["561921120408698910"],
|
||||||
"agentList": [],
|
"agentList": [],
|
||||||
"endSymbols": ["🔚"],
|
"endSymbols": ["🔚"],
|
||||||
|
"channelPolicies": {
|
||||||
|
"1476369680632647721": {
|
||||||
|
"listMode": "agent-list",
|
||||||
|
"agentList": ["1474088632750047324"],
|
||||||
|
"endSymbols": ["🔚"]
|
||||||
|
}
|
||||||
|
},
|
||||||
"noReplyProvider": "whisper-gateway",
|
"noReplyProvider": "whisper-gateway",
|
||||||
"noReplyModel": "no-reply",
|
"noReplyModel": "no-reply",
|
||||||
"enableDiscordControlTool": true,
|
"enableDiscordControlTool": true,
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ Environment overrides:
|
|||||||
- `LIST_MODE` (`human-list` or `agent-list`)
|
- `LIST_MODE` (`human-list` or `agent-list`)
|
||||||
- `HUMAN_LIST_JSON`
|
- `HUMAN_LIST_JSON`
|
||||||
- `AGENT_LIST_JSON`
|
- `AGENT_LIST_JSON`
|
||||||
|
- `CHANNEL_POLICIES_JSON`
|
||||||
- `END_SYMBOLS_JSON`
|
- `END_SYMBOLS_JSON`
|
||||||
|
|
||||||
The script:
|
The script:
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ Optional:
|
|||||||
- `listMode` (`human-list` | `agent-list`, default `human-list`)
|
- `listMode` (`human-list` | `agent-list`, default `human-list`)
|
||||||
- `humanList` (default [])
|
- `humanList` (default [])
|
||||||
- `agentList` (default [])
|
- `agentList` (default [])
|
||||||
|
- `channelPolicies` (per-channel overrides by channelId)
|
||||||
- `bypassUserIds` (deprecated alias of `humanList`)
|
- `bypassUserIds` (deprecated alias of `humanList`)
|
||||||
- `endSymbols` (default ["🔚"])
|
- `endSymbols` (default ["🔚"])
|
||||||
- `enableDiscordControlTool` (default true)
|
- `enableDiscordControlTool` (default true)
|
||||||
|
|||||||
@@ -178,9 +178,10 @@ export default {
|
|||||||
const senderId = normalizeSender(e, c);
|
const senderId = normalizeSender(e, c);
|
||||||
const content = typeof e.content === "string" ? e.content : "";
|
const content = typeof e.content === "string" ? e.content : "";
|
||||||
const channel = normalizeChannel(c);
|
const channel = normalizeChannel(c);
|
||||||
|
const channelId = typeof c.channelId === "string" ? c.channelId : undefined;
|
||||||
|
|
||||||
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig);
|
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig);
|
||||||
const decision = evaluateDecision({ config: live, channel, senderId, content });
|
const decision = evaluateDecision({ config: live, channel, channelId, senderId, content });
|
||||||
sessionDecision.set(sessionKey, { decision, createdAt: Date.now() });
|
sessionDecision.set(sessionKey, { decision, createdAt: Date.now() });
|
||||||
pruneDecisionMap();
|
pruneDecisionMap();
|
||||||
api.logger.debug?.(
|
api.logger.debug?.(
|
||||||
|
|||||||
@@ -13,6 +13,20 @@
|
|||||||
"listMode": { "type": "string", "enum": ["human-list", "agent-list"], "default": "human-list" },
|
"listMode": { "type": "string", "enum": ["human-list", "agent-list"], "default": "human-list" },
|
||||||
"humanList": { "type": "array", "items": { "type": "string" }, "default": [] },
|
"humanList": { "type": "array", "items": { "type": "string" }, "default": [] },
|
||||||
"agentList": { "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": {}
|
||||||
|
},
|
||||||
"bypassUserIds": { "type": "array", "items": { "type": "string" }, "default": [] },
|
"bypassUserIds": { "type": "array", "items": { "type": "string" }, "default": [] },
|
||||||
"endSymbols": { "type": "array", "items": { "type": "string" }, "default": ["🔚"] },
|
"endSymbols": { "type": "array", "items": { "type": "string" }, "default": ["🔚"] },
|
||||||
"noReplyProvider": { "type": "string" },
|
"noReplyProvider": { "type": "string" },
|
||||||
|
|||||||
@@ -4,6 +4,15 @@ export type WhisperGateConfig = {
|
|||||||
listMode?: "human-list" | "agent-list";
|
listMode?: "human-list" | "agent-list";
|
||||||
humanList?: string[];
|
humanList?: string[];
|
||||||
agentList?: string[];
|
agentList?: string[];
|
||||||
|
channelPolicies?: Record<
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
listMode?: "human-list" | "agent-list";
|
||||||
|
humanList?: string[];
|
||||||
|
agentList?: string[];
|
||||||
|
endSymbols?: string[];
|
||||||
|
}
|
||||||
|
>;
|
||||||
// backward compatibility
|
// backward compatibility
|
||||||
bypassUserIds?: string[];
|
bypassUserIds?: string[];
|
||||||
endSymbols?: string[];
|
endSymbols?: string[];
|
||||||
@@ -21,9 +30,34 @@ function getLastChar(input: string): string {
|
|||||||
return t.length ? t[t.length - 1] : "";
|
return t.length ? t[t.length - 1] : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resolvePolicy(config: WhisperGateConfig, channelId?: string) {
|
||||||
|
const globalMode = config.listMode || "human-list";
|
||||||
|
const globalHuman = config.humanList || config.bypassUserIds || [];
|
||||||
|
const globalAgent = config.agentList || [];
|
||||||
|
const globalEnd = config.endSymbols || ["🔚"];
|
||||||
|
|
||||||
|
if (!channelId) {
|
||||||
|
return { listMode: globalMode, humanList: globalHuman, agentList: globalAgent, endSymbols: globalEnd };
|
||||||
|
}
|
||||||
|
|
||||||
|
const cp = config.channelPolicies || {};
|
||||||
|
const scoped = cp[channelId];
|
||||||
|
if (!scoped) {
|
||||||
|
return { listMode: globalMode, humanList: globalHuman, agentList: globalAgent, endSymbols: globalEnd };
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
listMode: scoped.listMode || globalMode,
|
||||||
|
humanList: scoped.humanList || globalHuman,
|
||||||
|
agentList: scoped.agentList || globalAgent,
|
||||||
|
endSymbols: scoped.endSymbols || globalEnd,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function evaluateDecision(params: {
|
export function evaluateDecision(params: {
|
||||||
config: WhisperGateConfig;
|
config: WhisperGateConfig;
|
||||||
channel?: string;
|
channel?: string;
|
||||||
|
channelId?: string;
|
||||||
senderId?: string;
|
senderId?: string;
|
||||||
content?: string;
|
content?: string;
|
||||||
}): Decision {
|
}): Decision {
|
||||||
@@ -38,16 +72,18 @@ export function evaluateDecision(params: {
|
|||||||
return { shouldUseNoReply: false, reason: "non_discord" };
|
return { shouldUseNoReply: false, reason: "non_discord" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const mode = config.listMode || "human-list";
|
const policy = resolvePolicy(config, params.channelId);
|
||||||
const humanList = config.humanList || config.bypassUserIds || [];
|
|
||||||
const agentList = config.agentList || [];
|
const mode = policy.listMode;
|
||||||
|
const humanList = policy.humanList;
|
||||||
|
const agentList = policy.agentList;
|
||||||
|
|
||||||
const senderId = params.senderId || "";
|
const senderId = params.senderId || "";
|
||||||
const inHumanList = !!senderId && humanList.includes(senderId);
|
const inHumanList = !!senderId && humanList.includes(senderId);
|
||||||
const inAgentList = !!senderId && agentList.includes(senderId);
|
const inAgentList = !!senderId && agentList.includes(senderId);
|
||||||
|
|
||||||
const lastChar = getLastChar(params.content || "");
|
const lastChar = getLastChar(params.content || "");
|
||||||
const hasEnd = !!lastChar && (config.endSymbols || []).includes(lastChar);
|
const hasEnd = !!lastChar && policy.endSymbols.includes(lastChar);
|
||||||
|
|
||||||
if (mode === "human-list") {
|
if (mode === "human-list") {
|
||||||
if (inHumanList) {
|
if (inHumanList) {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ NO_REPLY_API_KEY="${NO_REPLY_API_KEY:-wg-local-test-token}"
|
|||||||
LIST_MODE="${LIST_MODE:-human-list}"
|
LIST_MODE="${LIST_MODE:-human-list}"
|
||||||
HUMAN_LIST_JSON="${HUMAN_LIST_JSON:-[\"561921120408698910\",\"1474088632750047324\"]}"
|
HUMAN_LIST_JSON="${HUMAN_LIST_JSON:-[\"561921120408698910\",\"1474088632750047324\"]}"
|
||||||
AGENT_LIST_JSON="${AGENT_LIST_JSON:-[]}"
|
AGENT_LIST_JSON="${AGENT_LIST_JSON:-[]}"
|
||||||
|
CHANNEL_POLICIES_JSON="${CHANNEL_POLICIES_JSON:-{}}"
|
||||||
END_SYMBOLS_JSON="${END_SYMBOLS_JSON:-[\"🔚\"]}"
|
END_SYMBOLS_JSON="${END_SYMBOLS_JSON:-[\"🔚\"]}"
|
||||||
|
|
||||||
STATE_DIR="${STATE_DIR:-$HOME/.openclaw/whispergate-install-records}"
|
STATE_DIR="${STATE_DIR:-$HOME/.openclaw/whispergate-install-records}"
|
||||||
@@ -138,7 +139,7 @@ PY
|
|||||||
current_plugins_json='{}'
|
current_plugins_json='{}'
|
||||||
fi
|
fi
|
||||||
|
|
||||||
new_plugins_json="$(CURRENT_PLUGINS_JSON="$current_plugins_json" PLUGIN_PATH="$PLUGIN_PATH" LIST_MODE="$LIST_MODE" HUMAN_LIST_JSON="$HUMAN_LIST_JSON" AGENT_LIST_JSON="$AGENT_LIST_JSON" END_SYMBOLS_JSON="$END_SYMBOLS_JSON" NO_REPLY_PROVIDER_ID="$NO_REPLY_PROVIDER_ID" NO_REPLY_MODEL_ID="$NO_REPLY_MODEL_ID" python3 - <<'PY'
|
new_plugins_json="$(CURRENT_PLUGINS_JSON="$current_plugins_json" PLUGIN_PATH="$PLUGIN_PATH" LIST_MODE="$LIST_MODE" HUMAN_LIST_JSON="$HUMAN_LIST_JSON" AGENT_LIST_JSON="$AGENT_LIST_JSON" CHANNEL_POLICIES_JSON="$CHANNEL_POLICIES_JSON" END_SYMBOLS_JSON="$END_SYMBOLS_JSON" NO_REPLY_PROVIDER_ID="$NO_REPLY_PROVIDER_ID" NO_REPLY_MODEL_ID="$NO_REPLY_MODEL_ID" python3 - <<'PY'
|
||||||
import json, os
|
import json, os
|
||||||
plugins=json.loads(os.environ['CURRENT_PLUGINS_JSON'])
|
plugins=json.loads(os.environ['CURRENT_PLUGINS_JSON'])
|
||||||
if not isinstance(plugins,dict):
|
if not isinstance(plugins,dict):
|
||||||
@@ -162,6 +163,7 @@ entries['whispergate']={
|
|||||||
'listMode': os.environ['LIST_MODE'],
|
'listMode': os.environ['LIST_MODE'],
|
||||||
'humanList': json.loads(os.environ['HUMAN_LIST_JSON']),
|
'humanList': json.loads(os.environ['HUMAN_LIST_JSON']),
|
||||||
'agentList': json.loads(os.environ['AGENT_LIST_JSON']),
|
'agentList': json.loads(os.environ['AGENT_LIST_JSON']),
|
||||||
|
'channelPolicies': json.loads(os.environ['CHANNEL_POLICIES_JSON']),
|
||||||
'endSymbols': json.loads(os.environ['END_SYMBOLS_JSON']),
|
'endSymbols': json.loads(os.environ['END_SYMBOLS_JSON']),
|
||||||
'noReplyProvider': os.environ['NO_REPLY_PROVIDER_ID'],
|
'noReplyProvider': os.environ['NO_REPLY_PROVIDER_ID'],
|
||||||
'noReplyModel': os.environ['NO_REPLY_MODEL_ID'],
|
'noReplyModel': os.environ['NO_REPLY_MODEL_ID'],
|
||||||
|
|||||||
Reference in New Issue
Block a user