feat: rewrite plugin as v2 with globalThis-based turn management
Complete rewrite of the Dirigent plugin turn management system to work correctly with OpenClaw's VM-context-per-session architecture: - All turn state stored on globalThis (persists across VM context hot-reloads) - Hooks registered unconditionally on every api instance; event-level dedup (runId Set for agent_end, WeakSet for before_model_resolve) prevents double-processing - Gateway lifecycle events (gateway_start/stop) guarded once via globalThis flag - Shared initializingChannels lock prevents concurrent channel init across VM contexts in message_received and before_model_resolve - New ChannelStore and IdentityRegistry replace old policy/session-state modules - Added agent_end hook with tail-match polling for Discord delivery confirmation - Added web control page, padded-cell auto-scan, discussion tool support - Removed obsolete v1 modules: channel-resolver, channel-modes, discussion-service, session-state, turn-bootstrap, policy/store, rules, decision-input Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
13
plugin/commands/command-utils.ts
Normal file
13
plugin/commands/command-utils.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/** Extract Discord channel ID from slash command context. */
|
||||
export function parseDiscordChannelIdFromCommand(cmdCtx: Record<string, unknown>): string | undefined {
|
||||
// OpenClaw passes channel context in various ways depending on the trigger
|
||||
const sessionKey = String(cmdCtx.sessionKey ?? "");
|
||||
const m = sessionKey.match(/:discord:channel:(\d+)$/);
|
||||
if (m) return m[1];
|
||||
|
||||
// Fallback: channelId directly on context
|
||||
const cid = String(cmdCtx.channelId ?? "");
|
||||
if (/^\d+$/.test(cid)) return cid;
|
||||
|
||||
return undefined;
|
||||
}
|
||||
@@ -1,153 +0,0 @@
|
||||
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
||||
import { advanceTurn, getTurnDebugInfo, resetTurn } from "../turn-manager.js";
|
||||
import type { DirigentConfig } from "../rules.js";
|
||||
import { setChannelShuffling, getChannelShuffling } from "../core/channel-modes.js";
|
||||
|
||||
type CommandDeps = {
|
||||
api: OpenClawPluginApi;
|
||||
baseConfig: DirigentConfig;
|
||||
policyState: { filePath: string; channelPolicies: Record<string, unknown> };
|
||||
persistPolicies: (api: OpenClawPluginApi) => void;
|
||||
ensurePolicyStateLoaded: (api: OpenClawPluginApi, config: DirigentConfig) => void;
|
||||
};
|
||||
|
||||
export function registerDirigentCommand(deps: CommandDeps): void {
|
||||
const { api, baseConfig, policyState, persistPolicies, ensurePolicyStateLoaded } = deps;
|
||||
|
||||
api.registerCommand({
|
||||
name: "dirigent",
|
||||
description: "Dirigent runtime commands",
|
||||
acceptsArgs: true,
|
||||
handler: async (cmdCtx) => {
|
||||
const args = cmdCtx.args || "";
|
||||
const parts = args.trim().split(/\s+/);
|
||||
const subCmd = parts[0] || "help";
|
||||
|
||||
if (subCmd === "help") {
|
||||
return {
|
||||
text:
|
||||
`Dirigent commands:\n` +
|
||||
`/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\n` +
|
||||
`/dirigent turn-shuffling [on|off] - Enable/disable turn order shuffling\n` +
|
||||
`/dirigent_policy get <discordChannelId>\n` +
|
||||
`/dirigent_policy set <discordChannelId> <policy-json>\n` +
|
||||
`/dirigent_policy delete <discordChannelId>`,
|
||||
};
|
||||
}
|
||||
|
||||
if (subCmd === "status") {
|
||||
return { text: JSON.stringify({ policies: policyState.channelPolicies }, null, 2) };
|
||||
}
|
||||
|
||||
if (subCmd === "turn-status") {
|
||||
const channelId = cmdCtx.channelId;
|
||||
if (!channelId) return { text: "Cannot get channel ID", isError: true };
|
||||
return { text: JSON.stringify(getTurnDebugInfo(channelId), null, 2) };
|
||||
}
|
||||
|
||||
if (subCmd === "turn-advance") {
|
||||
const channelId = cmdCtx.channelId;
|
||||
if (!channelId) return { text: "Cannot get channel ID", isError: true };
|
||||
const next = advanceTurn(channelId);
|
||||
return { text: JSON.stringify({ ok: true, nextSpeaker: next }) };
|
||||
}
|
||||
|
||||
if (subCmd === "turn-reset") {
|
||||
const channelId = cmdCtx.channelId;
|
||||
if (!channelId) return { text: "Cannot get channel ID", isError: true };
|
||||
resetTurn(channelId);
|
||||
return { text: JSON.stringify({ ok: true }) };
|
||||
}
|
||||
|
||||
if (subCmd === "turn-shuffling") {
|
||||
const channelId = cmdCtx.channelId;
|
||||
if (!channelId) return { text: "Cannot get channel ID", isError: true };
|
||||
|
||||
const arg = parts[1]?.toLowerCase();
|
||||
if (arg === "on") {
|
||||
setChannelShuffling(channelId, true);
|
||||
return { text: JSON.stringify({ ok: true, channelId, shuffling: true }) };
|
||||
} else if (arg === "off") {
|
||||
setChannelShuffling(channelId, false);
|
||||
return { text: JSON.stringify({ ok: true, channelId, shuffling: false }) };
|
||||
} else if (!arg) {
|
||||
const isShuffling = getChannelShuffling(channelId);
|
||||
return { text: JSON.stringify({ ok: true, channelId, shuffling: isShuffling }) };
|
||||
} else {
|
||||
return { text: "Invalid argument. Use: /dirigent turn-shuffling [on|off]", isError: true };
|
||||
}
|
||||
}
|
||||
|
||||
return { text: `Unknown subcommand: ${subCmd}`, isError: true };
|
||||
},
|
||||
});
|
||||
|
||||
api.registerCommand({
|
||||
name: "dirigent_policy",
|
||||
description: "Dirigent channel policy CRUD",
|
||||
acceptsArgs: true,
|
||||
handler: async (cmdCtx) => {
|
||||
const live = 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 };
|
||||
},
|
||||
});
|
||||
}
|
||||
70
plugin/commands/set-channel-mode-command.ts
Normal file
70
plugin/commands/set-channel-mode-command.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
||||
import type { ChannelStore, ChannelMode } from "../core/channel-store.js";
|
||||
import { parseDiscordChannelIdFromCommand } from "./command-utils.js";
|
||||
|
||||
const SWITCHABLE_MODES = new Set<ChannelMode>(["none", "chat", "report"]);
|
||||
const LOCKED_MODES = new Set<ChannelMode>(["work", "discussion"]);
|
||||
|
||||
type Deps = {
|
||||
api: OpenClawPluginApi;
|
||||
channelStore: ChannelStore;
|
||||
};
|
||||
|
||||
export function registerSetChannelModeCommand(deps: Deps): void {
|
||||
const { api, channelStore } = deps;
|
||||
|
||||
api.registerCommand({
|
||||
name: "set-channel-mode",
|
||||
description: "Set the mode of the current Discord channel: none | chat | report",
|
||||
acceptsArgs: true,
|
||||
handler: async (cmdCtx) => {
|
||||
const raw = (cmdCtx.args || "").trim().toLowerCase() as ChannelMode;
|
||||
|
||||
if (!raw) {
|
||||
return {
|
||||
text: "Usage: /set-channel-mode <none|chat|report>\n\nModes work and discussion are locked and can only be set via creation tools.",
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
if (LOCKED_MODES.has(raw)) {
|
||||
return {
|
||||
text: `Mode "${raw}" cannot be set via command — it is locked to its creation tool (create-${raw}-channel or create-discussion-channel).`,
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
if (!SWITCHABLE_MODES.has(raw)) {
|
||||
return {
|
||||
text: `Unknown mode "${raw}". Valid values: none, chat, report`,
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
// Extract channel ID from command context
|
||||
const channelId = parseDiscordChannelIdFromCommand(cmdCtx);
|
||||
if (!channelId) {
|
||||
return {
|
||||
text: "Could not determine Discord channel ID. Run this command inside a Discord channel.",
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
const current = channelStore.getMode(channelId);
|
||||
if (LOCKED_MODES.has(current)) {
|
||||
return {
|
||||
text: `Channel ${channelId} is in locked mode "${current}" and cannot be changed.`,
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
channelStore.setMode(channelId, raw);
|
||||
} catch (err) {
|
||||
return { text: `Failed: ${String(err)}`, isError: true };
|
||||
}
|
||||
|
||||
return { text: `Channel ${channelId} mode set to "${raw}".` };
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user