Refine discussion moderator messaging flow

This commit is contained in:
zhi
2026-04-02 04:18:45 +00:00
parent d9bb5c2e21
commit 684f8f9ee7
7 changed files with 153 additions and 103 deletions

View File

@@ -0,0 +1,69 @@
export function buildDiscussionKickoffMessage(discussGuide: string): string {
return [
"[Discussion Started]",
"",
"This channel was created for a temporary agent discussion.",
"",
"Goal:",
discussGuide,
"",
"Instructions:",
"1. Discuss only the topic above.",
"2. Work toward a concrete conclusion.",
"3. When the initiator decides the goal has been achieved, the initiator must:",
" - write a summary document to a file",
" - call the tool: discuss-callback(summaryPath)",
" - provide the summary document path",
"",
"Completion rule:",
"Only the discussion initiator may finish this discussion.",
"",
"After callback:",
"- this channel will be closed",
"- further discussion messages will be ignored",
"- this channel will remain only for archive/reference",
"- the original work channel will be notified with the summary file path",
].join("\n");
}
export function buildDiscussionIdleReminderMessage(): string {
return [
"[Discussion Idle]",
"",
"No agent responded in the latest discussion round.",
"If the discussion goal has been achieved, the initiator should now:",
"1. write the discussion summary to a file in the workspace",
"2. call discuss-callback with the summary file path",
"",
"If more discussion is still needed, continue the discussion in this channel.",
].join("\n");
}
export function buildDiscussionClosedMessage(): string {
return [
"[Channel Closed]",
"",
"This discussion channel has been closed.",
"It is now kept for archive/reference only.",
"Further discussion in this channel is ignored.",
].join("\n");
}
export function buildDiscussionOriginCallbackMessage(summaryPath: string, discussionChannelId: string): string {
return [
"[Discussion Result Ready]",
"",
"A temporary discussion has completed.",
"",
"Summary file:",
summaryPath,
"",
"Source discussion channel:",
`<#${discussionChannelId}>`,
"",
"Status:",
"completed",
"",
"Continue the original task using the summary file above.",
].join("\n");
}

View File

@@ -2,6 +2,12 @@ import fs from "node:fs";
import path from "node:path";
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
import { closeDiscussion, createDiscussion, getDiscussion, isDiscussionClosed, markDiscussionIdleReminderSent, type DiscussionMetadata } from "./discussion-state.js";
import {
buildDiscussionClosedMessage,
buildDiscussionIdleReminderMessage,
buildDiscussionKickoffMessage,
buildDiscussionOriginCallbackMessage,
} from "./discussion-messages.js";
import { sendModeratorMessage } from "./moderator-discord.js";
type DiscussionServiceDeps = {
@@ -15,76 +21,6 @@ type DiscussionServiceDeps = {
export function createDiscussionService(deps: DiscussionServiceDeps) {
const workspaceRoot = path.resolve(deps.workspaceRoot || process.cwd());
function buildKickoffMessage(discussGuide: string): string {
return [
"[Discussion Started]",
"",
"This channel was created for a temporary agent discussion.",
"",
"Goal:",
discussGuide,
"",
"Instructions:",
"1. Discuss only the topic above.",
"2. Work toward a concrete conclusion.",
"3. When the initiator decides the goal has been achieved, the initiator must:",
" - write a summary document to a file",
" - call the tool: discuss-callback",
" - provide the summary document path",
"",
"Completion rule:",
"Only the discussion initiator may finish this discussion.",
"",
"After callback:",
"- this channel will be closed",
"- further discussion messages will be ignored",
"- this channel will remain only for archive/reference",
"- the original work channel will be notified with the summary file path",
].join("\n");
}
function buildIdleReminderMessage(): string {
return [
"[Discussion Idle]",
"",
"No agent responded in the latest discussion round.",
"If the discussion goal has been achieved, the initiator should now:",
"1. write the discussion summary to a file in the workspace",
"2. call discuss-callback with the summary file path",
"",
"If more discussion is still needed, continue the discussion in this channel.",
].join("\n");
}
function buildClosedMessage(): string {
return [
"[Channel Closed]",
"",
"This discussion channel has been closed.",
"It is now kept for archive/reference only.",
"Further discussion in this channel is ignored.",
].join("\n");
}
function buildOriginCallbackMessage(summaryPath: string, discussionChannelId: string): string {
return [
"[Discussion Result Ready]",
"",
"A temporary discussion has completed.",
"",
"Summary file:",
summaryPath,
"",
"Source discussion channel:",
`<#${discussionChannelId}>`,
"",
"Status:",
"completed",
"",
"Continue the original task using the summary file above.",
].join("\n");
}
async function initDiscussion(params: {
discussionChannelId: string;
originChannelId: string;
@@ -104,7 +40,15 @@ export function createDiscussionService(deps: DiscussionServiceDeps) {
});
if (deps.moderatorBotToken) {
await sendModeratorMessage(deps.moderatorBotToken, params.discussionChannelId, buildKickoffMessage(params.discussGuide), deps.api.logger);
const result = await sendModeratorMessage(
deps.moderatorBotToken,
params.discussionChannelId,
buildDiscussionKickoffMessage(params.discussGuide),
deps.api.logger,
);
if (!result.ok) {
deps.api.logger.warn(`dirigent: discussion kickoff message failed channel=${params.discussionChannelId} error=${result.error}`);
}
}
return metadata;
@@ -115,7 +59,15 @@ export function createDiscussionService(deps: DiscussionServiceDeps) {
if (!metadata || metadata.status !== "active" || metadata.idleReminderSent) return;
markDiscussionIdleReminderSent(channelId);
if (deps.moderatorBotToken) {
await sendModeratorMessage(deps.moderatorBotToken, channelId, buildIdleReminderMessage(), deps.api.logger);
const result = await sendModeratorMessage(
deps.moderatorBotToken,
channelId,
buildDiscussionIdleReminderMessage(),
deps.api.logger,
);
if (!result.ok) {
deps.api.logger.warn(`dirigent: discussion idle reminder failed channel=${channelId} error=${result.error}`);
}
}
}
@@ -163,7 +115,17 @@ export function createDiscussionService(deps: DiscussionServiceDeps) {
deps.forceNoReplyForSession(metadata.initiatorSessionId);
if (deps.moderatorBotToken) {
await sendModeratorMessage(deps.moderatorBotToken, metadata.originChannelId, buildOriginCallbackMessage(realPath, metadata.discussionChannelId), deps.api.logger);
const result = await sendModeratorMessage(
deps.moderatorBotToken,
metadata.originChannelId,
buildDiscussionOriginCallbackMessage(realPath, metadata.discussionChannelId),
deps.api.logger,
);
if (!result.ok) {
deps.api.logger.warn(
`dirigent: discussion origin callback notification failed originChannel=${metadata.originChannelId} error=${result.error}`,
);
}
}
return { ok: true, summaryPath: realPath, discussion: closed };
@@ -174,7 +136,10 @@ export function createDiscussionService(deps: DiscussionServiceDeps) {
if (!metadata || metadata.status !== "closed") return false;
if (deps.moderatorUserId && senderId && senderId === deps.moderatorUserId) return true;
if (!deps.moderatorBotToken) return true;
await sendModeratorMessage(deps.moderatorBotToken, channelId, buildClosedMessage(), deps.api.logger);
const result = await sendModeratorMessage(deps.moderatorBotToken, channelId, buildDiscussionClosedMessage(), deps.api.logger);
if (!result.ok) {
deps.api.logger.warn(`dirigent: discussion closed reply failed channel=${channelId} error=${result.error}`);
}
return true;
}

View File

@@ -20,12 +20,16 @@ export function resolveDiscordUserId(api: OpenClawPluginApi, accountId: string):
return userIdFromToken(acct.token);
}
export type ModeratorMessageResult =
| { ok: true; status: number; channelId: string; messageId?: string }
| { ok: false; status?: number; channelId: string; error: string };
export async function sendModeratorMessage(
token: string,
channelId: string,
content: string,
logger: { info: (msg: string) => void; warn: (msg: string) => void },
): Promise<boolean> {
): Promise<ModeratorMessageResult> {
try {
const r = await fetch(`https://discord.com/api/v10/channels/${channelId}/messages`, {
method: "POST",
@@ -35,15 +39,27 @@ export async function sendModeratorMessage(
},
body: JSON.stringify({ content }),
});
if (!r.ok) {
const text = await r.text();
logger.warn(`dirigent: moderator send failed (${r.status}): ${text}`);
return false;
const text = await r.text();
let json: Record<string, unknown> | null = null;
try {
json = text ? (JSON.parse(text) as Record<string, unknown>) : null;
} catch {
json = null;
}
logger.info(`dirigent: moderator message sent to channel=${channelId}`);
return true;
if (!r.ok) {
const error = `discord api error (${r.status}): ${text || "<empty response>"}`;
logger.warn(`dirigent: moderator send failed channel=${channelId} ${error}`);
return { ok: false, status: r.status, channelId, error };
}
const messageId = typeof json?.id === "string" ? json.id : undefined;
logger.info(`dirigent: moderator message sent to channel=${channelId} messageId=${messageId ?? "unknown"}`);
return { ok: true, status: r.status, channelId, messageId };
} catch (err) {
logger.warn(`dirigent: moderator send error: ${String(err)}`);
return false;
const error = String(err);
logger.warn(`dirigent: moderator send error channel=${channelId}: ${error}`);
return { ok: false, channelId, error };
}
}

View File

@@ -24,7 +24,7 @@ type BeforeMessageWriteDeps = {
channelId: string,
content: string,
logger: { info: (m: string) => void; warn: (m: string) => void },
) => Promise<void>;
) => Promise<unknown>;
discussionService?: {
maybeSendIdleReminder: (channelId: string) => Promise<void>;
getDiscussion: (channelId: string) => { status: string } | undefined;

View File

@@ -22,7 +22,7 @@ type MessageSentDeps = {
channelId: string,
content: string,
logger: { info: (m: string) => void; warn: (m: string) => void },
) => Promise<void>;
) => Promise<unknown>;
};
export function registerMessageSentHook(deps: MessageSentDeps): void {