feat(config): add hot-reload config + listMode (human-list/agent-list)

This commit is contained in:
2026-02-26 00:18:05 +00:00
parent 0f526346f4
commit 6d463a4572
8 changed files with 79 additions and 25 deletions

View File

@@ -26,7 +26,10 @@ Required:
Optional:
- `enabled` (default true)
- `discordOnly` (default true)
- `bypassUserIds` (default [])
- `listMode` (`human-list` | `agent-list`, default `human-list`)
- `humanList` (default [])
- `agentList` (default [])
- `bypassUserIds` (deprecated alias of `humanList`)
- `endSymbols` (default ["🔚"])
- `enableDiscordControlTool` (default true)
- `discordControlApiBaseUrl` (default `http://127.0.0.1:8790`)

View File

@@ -52,7 +52,17 @@ function pruneDecisionMap(now = Date.now()) {
}
function shouldInjectEndMarker(reason: string): boolean {
return reason === "bypass_sender" || reason.startsWith("end_symbol:");
return reason.startsWith("end_symbol:");
}
function getLivePluginConfig(api: OpenClawPluginApi, fallback: WhisperGateConfig): WhisperGateConfig {
const root = (api.config as Record<string, unknown>) || {};
const plugins = (root.plugins as Record<string, unknown>) || {};
const entries = (plugins.entries as Record<string, unknown>) || {};
const entry = (entries.whispergate as Record<string, unknown>) || {};
const cfg = (entry.config as Record<string, unknown>) || {};
if (Object.keys(cfg).length > 0) return cfg as unknown as WhisperGateConfig;
return fallback;
}
function pickDefined(input: Record<string, unknown>) {
@@ -67,14 +77,14 @@ export default {
id: "whispergate",
name: "WhisperGate",
register(api: OpenClawPluginApi) {
const config = (api.pluginConfig || {}) as WhisperGateConfig & {
const baseConfig = (api.pluginConfig || {}) as WhisperGateConfig & {
enableDiscordControlTool?: boolean;
discordControlApiBaseUrl?: string;
discordControlApiToken?: string;
discordControlCallerId?: string;
};
if (config.enableDiscordControlTool !== false) {
if (baseConfig.enableDiscordControlTool !== false) {
api.registerTool(
{
name: "discord_control",
@@ -113,12 +123,17 @@ export default {
},
async execute(_id: string, params: Record<string, unknown>) {
const action = String(params.action || "") as DiscordControlAction;
const baseUrl = (config.discordControlApiBaseUrl || "http://127.0.0.1:8790").replace(/\/$/, "");
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig) as WhisperGateConfig & {
discordControlApiBaseUrl?: string;
discordControlApiToken?: string;
discordControlCallerId?: string;
};
const baseUrl = (live.discordControlApiBaseUrl || "http://127.0.0.1:8790").replace(/\/$/, "");
const body = pickDefined({ ...params, action });
const headers: Record<string, string> = { "Content-Type": "application/json" };
if (config.discordControlApiToken) headers.Authorization = `Bearer ${config.discordControlApiToken}`;
if (config.discordControlCallerId) headers["X-OpenClaw-Caller-Id"] = config.discordControlCallerId;
if (live.discordControlApiToken) headers.Authorization = `Bearer ${live.discordControlApiToken}`;
if (live.discordControlCallerId) headers["X-OpenClaw-Caller-Id"] = live.discordControlCallerId;
const r = await fetch(`${baseUrl}/v1/discord/action`, {
method: "POST",
@@ -153,7 +168,7 @@ export default {
);
}
api.registerHook("message:received", async (event, ctx) => {
api.on("message_received", async (event, ctx) => {
try {
const c = (ctx || {}) as Record<string, unknown>;
const e = (event || {}) as Record<string, unknown>;
@@ -164,7 +179,8 @@ export default {
const content = typeof e.content === "string" ? e.content : "";
const channel = normalizeChannel(c);
const decision = evaluateDecision({ config, channel, senderId, content });
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig);
const decision = evaluateDecision({ config: live, channel, senderId, content });
sessionDecision.set(sessionKey, { decision, createdAt: Date.now() });
pruneDecisionMap();
api.logger.debug?.(
@@ -190,13 +206,14 @@ export default {
// no-reply path is consumed here
sessionDecision.delete(key);
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig);
api.logger.info(
`whispergate: override model for session=${key}, provider=${config.noReplyProvider}, model=${config.noReplyModel}, reason=${rec.decision.reason}`,
`whispergate: override model for session=${key}, provider=${live.noReplyProvider}, model=${live.noReplyModel}, reason=${rec.decision.reason}`,
);
return {
providerOverride: config.noReplyProvider,
modelOverride: config.noReplyModel,
providerOverride: live.noReplyProvider,
modelOverride: live.noReplyModel,
};
});

View File

@@ -10,6 +10,9 @@
"properties": {
"enabled": { "type": "boolean", "default": true },
"discordOnly": { "type": "boolean", "default": true },
"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": [] },
"bypassUserIds": { "type": "array", "items": { "type": "string" }, "default": [] },
"endSymbols": { "type": "array", "items": { "type": "string" }, "default": ["🔚"] },
"noReplyProvider": { "type": "string" },

View File

@@ -1,6 +1,10 @@
export type WhisperGateConfig = {
enabled?: boolean;
discordOnly?: boolean;
listMode?: "human-list" | "agent-list";
humanList?: string[];
agentList?: string[];
// backward compatibility
bypassUserIds?: string[];
endSymbols?: string[];
noReplyProvider: string;
@@ -34,14 +38,33 @@ export function evaluateDecision(params: {
return { shouldUseNoReply: false, reason: "non_discord" };
}
if (params.senderId && (config.bypassUserIds || []).includes(params.senderId)) {
return { shouldUseNoReply: false, reason: "bypass_sender" };
}
const mode = config.listMode || "human-list";
const humanList = config.humanList || config.bypassUserIds || [];
const agentList = config.agentList || [];
const senderId = params.senderId || "";
const inHumanList = !!senderId && humanList.includes(senderId);
const inAgentList = !!senderId && agentList.includes(senderId);
const lastChar = getLastChar(params.content || "");
if (lastChar && (config.endSymbols || []).includes(lastChar)) {
return { shouldUseNoReply: false, reason: `end_symbol:${lastChar}` };
const hasEnd = !!lastChar && (config.endSymbols || []).includes(lastChar);
if (mode === "human-list") {
if (inHumanList) {
return { shouldUseNoReply: false, reason: "human_list_sender" };
}
if (hasEnd) {
return { shouldUseNoReply: false, reason: `end_symbol:${lastChar}` };
}
return { shouldUseNoReply: true, reason: "rule_match_no_end_symbol" };
}
return { shouldUseNoReply: true, reason: "rule_match_no_end_symbol" };
// agent-list mode: listed senders require end symbol; others bypass requirement.
if (!inAgentList) {
return { shouldUseNoReply: false, reason: "non_agent_list_sender" };
}
if (hasEnd) {
return { shouldUseNoReply: false, reason: `end_symbol:${lastChar}` };
}
return { shouldUseNoReply: true, reason: "agent_list_missing_end_symbol" };
}