refactor(policy): move policy CRUD to /dirigent_policy slash command with explicit channelId
This commit is contained in:
@@ -1,17 +1,22 @@
|
||||
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
||||
import { advanceTurn, getTurnDebugInfo, resetTurn } from "../turn-manager.js";
|
||||
import type { DirigentConfig } from "../rules.js";
|
||||
|
||||
type CommandDeps = {
|
||||
api: OpenClawPluginApi;
|
||||
policyState: { channelPolicies: Record<string, unknown> };
|
||||
baseConfig: DirigentConfig;
|
||||
policyState: { filePath: string; channelPolicies: Record<string, unknown> };
|
||||
persistPolicies: (api: OpenClawPluginApi) => void;
|
||||
ensurePolicyStateLoaded: (api: OpenClawPluginApi, config: DirigentConfig) => void;
|
||||
getLivePluginConfig: (api: OpenClawPluginApi, fallback: DirigentConfig) => DirigentConfig;
|
||||
};
|
||||
|
||||
export function registerDirigentCommand(deps: CommandDeps): void {
|
||||
const { api, policyState } = deps;
|
||||
const { api, baseConfig, policyState, persistPolicies, ensurePolicyStateLoaded, getLivePluginConfig } = deps;
|
||||
|
||||
api.registerCommand({
|
||||
name: "dirigent",
|
||||
description: "Dirigent channel policy management",
|
||||
description: "Dirigent runtime commands",
|
||||
acceptsArgs: true,
|
||||
handler: async (cmdCtx) => {
|
||||
const args = cmdCtx.args || "";
|
||||
@@ -25,7 +30,10 @@ export function registerDirigentCommand(deps: CommandDeps): void {
|
||||
`/dirigent status - Show current channel status\n` +
|
||||
`/dirigent turn-status - Show turn-based speaking status\n` +
|
||||
`/dirigent turn-advance - Manually advance turn\n` +
|
||||
`/dirigent turn-reset - Reset turn order`,
|
||||
`/dirigent turn-reset - Reset turn order\n` +
|
||||
`/dirigent_policy get <discordChannelId>\n` +
|
||||
`/dirigent_policy set <discordChannelId> <policy-json>\n` +
|
||||
`/dirigent_policy delete <discordChannelId>`,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -56,4 +64,70 @@ export function registerDirigentCommand(deps: CommandDeps): void {
|
||||
return { text: `Unknown subcommand: ${subCmd}`, isError: true };
|
||||
},
|
||||
});
|
||||
|
||||
api.registerCommand({
|
||||
name: "dirigent_policy",
|
||||
description: "Dirigent channel policy CRUD",
|
||||
acceptsArgs: true,
|
||||
handler: async (cmdCtx) => {
|
||||
const live = getLivePluginConfig(api, baseConfig);
|
||||
ensurePolicyStateLoaded(api, live);
|
||||
|
||||
const args = (cmdCtx.args || "").trim();
|
||||
if (!args) {
|
||||
return {
|
||||
text:
|
||||
"Usage:\n" +
|
||||
"/dirigent_policy get <discordChannelId>\n" +
|
||||
"/dirigent_policy set <discordChannelId> <policy-json>\n" +
|
||||
"/dirigent_policy delete <discordChannelId>",
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
const [opRaw, channelIdRaw, ...rest] = args.split(/\s+/);
|
||||
const op = (opRaw || "").toLowerCase();
|
||||
const channelId = (channelIdRaw || "").trim();
|
||||
|
||||
if (!channelId || !/^\d+$/.test(channelId)) {
|
||||
return { text: "channelId is required and must be numeric Discord channel id", isError: true };
|
||||
}
|
||||
|
||||
if (op === "get") {
|
||||
const policy = (policyState.channelPolicies as Record<string, unknown>)[channelId];
|
||||
return { text: JSON.stringify({ ok: true, channelId, policy: policy || null }, null, 2) };
|
||||
}
|
||||
|
||||
if (op === "delete") {
|
||||
delete (policyState.channelPolicies as Record<string, unknown>)[channelId];
|
||||
persistPolicies(api);
|
||||
return { text: JSON.stringify({ ok: true, channelId, deleted: true }) };
|
||||
}
|
||||
|
||||
if (op === "set") {
|
||||
const jsonText = rest.join(" ").trim();
|
||||
if (!jsonText) {
|
||||
return { text: "set requires <policy-json>", isError: true };
|
||||
}
|
||||
let parsed: Record<string, unknown>;
|
||||
try {
|
||||
parsed = JSON.parse(jsonText);
|
||||
} catch (e) {
|
||||
return { text: `invalid policy-json: ${String(e)}`, isError: true };
|
||||
}
|
||||
|
||||
const next: Record<string, unknown> = {};
|
||||
if (typeof parsed.listMode === "string") next.listMode = parsed.listMode;
|
||||
if (Array.isArray(parsed.humanList)) next.humanList = parsed.humanList.map(String);
|
||||
if (Array.isArray(parsed.agentList)) next.agentList = parsed.agentList.map(String);
|
||||
if (Array.isArray(parsed.endSymbols)) next.endSymbols = parsed.endSymbols.map(String);
|
||||
|
||||
(policyState.channelPolicies as Record<string, unknown>)[channelId] = next;
|
||||
persistPolicies(api);
|
||||
return { text: JSON.stringify({ ok: true, channelId, policy: next }, null, 2) };
|
||||
}
|
||||
|
||||
return { text: `unsupported op: ${op}. use get|set|delete`, isError: true };
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user