187 lines
6.8 KiB
TypeScript
187 lines
6.8 KiB
TypeScript
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 { sendModeratorMessage } from "./moderator-discord.js";
|
|
|
|
type DiscussionServiceDeps = {
|
|
api: OpenClawPluginApi;
|
|
moderatorBotToken?: string;
|
|
workspaceRoot?: string;
|
|
forceNoReplyForSession: (sessionKey: string) => void;
|
|
};
|
|
|
|
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;
|
|
initiatorAgentId: string;
|
|
initiatorSessionId: string;
|
|
discussGuide: string;
|
|
}): Promise<DiscussionMetadata> {
|
|
const metadata = createDiscussion({
|
|
mode: "discussion",
|
|
discussionChannelId: params.discussionChannelId,
|
|
originChannelId: params.originChannelId,
|
|
initiatorAgentId: params.initiatorAgentId,
|
|
initiatorSessionId: params.initiatorSessionId,
|
|
discussGuide: params.discussGuide,
|
|
status: "active",
|
|
createdAt: new Date().toISOString(),
|
|
});
|
|
|
|
if (deps.moderatorBotToken) {
|
|
await sendModeratorMessage(deps.moderatorBotToken, params.discussionChannelId, buildKickoffMessage(params.discussGuide), deps.api.logger);
|
|
}
|
|
|
|
return metadata;
|
|
}
|
|
|
|
async function maybeSendIdleReminder(channelId: string): Promise<void> {
|
|
const metadata = getDiscussion(channelId);
|
|
if (!metadata || metadata.status !== "active" || metadata.idleReminderSent) return;
|
|
markDiscussionIdleReminderSent(channelId);
|
|
if (deps.moderatorBotToken) {
|
|
await sendModeratorMessage(deps.moderatorBotToken, channelId, buildIdleReminderMessage(), deps.api.logger);
|
|
}
|
|
}
|
|
|
|
function validateSummaryPath(summaryPath: string): string {
|
|
if (!summaryPath || !summaryPath.trim()) throw new Error("summaryPath is required");
|
|
|
|
const resolved = path.resolve(workspaceRoot, summaryPath);
|
|
const relative = path.relative(workspaceRoot, resolved);
|
|
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
throw new Error("summaryPath must stay inside the initiator workspace");
|
|
}
|
|
|
|
const real = fs.realpathSync.native(resolved);
|
|
const realWorkspace = fs.realpathSync.native(workspaceRoot);
|
|
const realRelative = path.relative(realWorkspace, real);
|
|
if (realRelative.startsWith("..") || path.isAbsolute(realRelative)) {
|
|
throw new Error("summaryPath resolves outside the initiator workspace");
|
|
}
|
|
|
|
const stat = fs.statSync(real);
|
|
if (!stat.isFile()) throw new Error("summaryPath must point to a file");
|
|
return real;
|
|
}
|
|
|
|
async function handleCallback(params: {
|
|
channelId: string;
|
|
summaryPath: string;
|
|
callerAgentId?: string;
|
|
callerSessionKey?: string;
|
|
}): Promise<{ ok: true; summaryPath: string; discussion: DiscussionMetadata }> {
|
|
const metadata = getDiscussion(params.channelId);
|
|
if (!metadata) throw new Error("current channel is not a discussion channel");
|
|
if (metadata.status !== "active" || isDiscussionClosed(params.channelId)) throw new Error("discussion is already closed");
|
|
if (!params.callerSessionKey || params.callerSessionKey !== metadata.initiatorSessionId) {
|
|
throw new Error("only the discussion initiator session may call discuss-callback");
|
|
}
|
|
if (params.callerAgentId && params.callerAgentId !== metadata.initiatorAgentId) {
|
|
throw new Error("only the discussion initiator agent may call discuss-callback");
|
|
}
|
|
|
|
const realPath = validateSummaryPath(params.summaryPath);
|
|
const closed = closeDiscussion(params.channelId, realPath);
|
|
if (!closed) throw new Error("failed to close discussion");
|
|
|
|
deps.forceNoReplyForSession(metadata.initiatorSessionId);
|
|
|
|
if (deps.moderatorBotToken) {
|
|
await sendModeratorMessage(deps.moderatorBotToken, metadata.originChannelId, buildOriginCallbackMessage(realPath, metadata.discussionChannelId), deps.api.logger);
|
|
}
|
|
|
|
return { ok: true, summaryPath: realPath, discussion: closed };
|
|
}
|
|
|
|
async function maybeReplyClosedChannel(channelId: string, senderId?: string): Promise<boolean> {
|
|
const metadata = getDiscussion(channelId);
|
|
if (!metadata || metadata.status !== "closed") return false;
|
|
if (!deps.moderatorBotToken) return true;
|
|
await sendModeratorMessage(deps.moderatorBotToken, channelId, buildClosedMessage(), deps.api.logger);
|
|
return true;
|
|
}
|
|
|
|
return {
|
|
initDiscussion,
|
|
getDiscussion,
|
|
maybeSendIdleReminder,
|
|
maybeReplyClosedChannel,
|
|
handleCallback,
|
|
};
|
|
}
|