Files
Dirigent/plugin/index.ts

243 lines
8.7 KiB
TypeScript

import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
import { evaluateDecision, type Decision, type WhisperGateConfig } from "./rules.js";
type DiscordControlAction = "channel-private-create" | "channel-private-update" | "member-list";
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.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>) {
const out: Record<string, unknown> = {};
for (const [k, v] of Object.entries(input)) {
if (v !== undefined) out[k] = v;
}
return out;
}
export default {
id: "whispergate",
name: "WhisperGate",
register(api: OpenClawPluginApi) {
const baseConfig = (api.pluginConfig || {}) as WhisperGateConfig & {
enableDiscordControlTool?: boolean;
discordControlApiBaseUrl?: string;
discordControlApiToken?: string;
discordControlCallerId?: string;
};
if (baseConfig.enableDiscordControlTool !== false) {
api.registerTool(
{
name: "discord_control",
description: "Discord admin extension actions: private channel create/update and member list.",
parameters: {
type: "object",
additionalProperties: false,
properties: {
action: {
type: "string",
enum: ["channel-private-create", "channel-private-update", "member-list"],
},
guildId: { type: "string" },
name: { type: "string" },
type: { type: "number" },
parentId: { type: "string" },
topic: { type: "string" },
position: { type: "number" },
nsfw: { type: "boolean" },
allowedUserIds: { type: "array", items: { type: "string" } },
allowedRoleIds: { type: "array", items: { type: "string" } },
allowMask: { type: "string" },
denyEveryoneMask: { type: "string" },
channelId: { type: "string" },
mode: { type: "string", enum: ["merge", "replace"] },
addUserIds: { type: "array", items: { type: "string" } },
addRoleIds: { type: "array", items: { type: "string" } },
removeTargetIds: { type: "array", items: { type: "string" } },
denyMask: { type: "string" },
limit: { type: "number" },
after: { type: "string" },
fields: { anyOf: [{ type: "string" }, { type: "array", items: { type: "string" } }] },
dryRun: { type: "boolean" },
},
required: ["action", "guildId"],
},
async execute(_id: string, params: Record<string, unknown>) {
const action = String(params.action || "") as DiscordControlAction;
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 (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",
headers,
body: JSON.stringify(body),
});
const text = await r.text();
if (!r.ok) {
return {
content: [
{
type: "text",
text: `discord_control failed (${r.status}): ${text}`,
},
],
isError: true,
};
}
return {
content: [
{
type: "text",
text,
},
],
};
},
},
{ optional: true },
);
}
api.on("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 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?.(
`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);
const live = getLivePluginConfig(api, baseConfig as WhisperGateConfig);
api.logger.info(
`whispergate: override model for session=${key}, provider=${live.noReplyProvider}, model=${live.noReplyModel}, reason=${rec.decision.reason}`,
);
return {
providerOverride: live.noReplyProvider,
modelOverride: live.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,
};
});
},
};