Refine discussion moderator messaging flow
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user