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 = { api: OpenClawPluginApi; moderatorBotToken?: string; moderatorUserId?: string; workspaceRoot?: string; forceNoReplyForSession: (sessionKey: string) => void; getDiscussionSessionKeys?: (channelId: string) => string[]; }; export function createDiscussionService(deps: DiscussionServiceDeps) { const defaultWorkspaceRoot = path.resolve(deps.workspaceRoot || process.cwd()); async function initDiscussion(params: { discussionChannelId: string; originChannelId: string; initiatorAgentId: string; initiatorSessionId: string; initiatorWorkspaceRoot?: string; discussGuide: string; }): Promise { const metadata = createDiscussion({ mode: "discussion", discussionChannelId: params.discussionChannelId, originChannelId: params.originChannelId, initiatorAgentId: params.initiatorAgentId, initiatorSessionId: params.initiatorSessionId, initiatorWorkspaceRoot: params.initiatorWorkspaceRoot, discussGuide: params.discussGuide, status: "active", createdAt: new Date().toISOString(), }); if (deps.moderatorBotToken) { 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; } async function maybeSendIdleReminder(channelId: string): Promise { const metadata = getDiscussion(channelId); if (!metadata || metadata.status !== "active" || metadata.idleReminderSent) return; markDiscussionIdleReminderSent(channelId); if (deps.moderatorBotToken) { 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}`); } } } function validateSummaryPath(summaryPath: string, workspaceRoot?: string): string { if (!summaryPath || !summaryPath.trim()) throw new Error("summaryPath is required"); const effectiveWorkspaceRoot = path.resolve(workspaceRoot || defaultWorkspaceRoot); const resolved = path.resolve(effectiveWorkspaceRoot, summaryPath); const relative = path.relative(effectiveWorkspaceRoot, 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(effectiveWorkspaceRoot); 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, metadata.initiatorWorkspaceRoot); const closed = closeDiscussion(params.channelId, realPath); if (!closed) throw new Error("failed to close discussion"); const discussionSessionKeys = new Set([ metadata.initiatorSessionId, ...(deps.getDiscussionSessionKeys?.(metadata.discussionChannelId) || []), ]); for (const sessionKey of discussionSessionKeys) { if (sessionKey) deps.forceNoReplyForSession(sessionKey); } if (deps.moderatorBotToken) { 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 }; } async function maybeReplyClosedChannel(channelId: string, senderId?: string): Promise { const metadata = getDiscussion(channelId); if (!metadata || metadata.status !== "closed") return false; if (deps.moderatorUserId && senderId && senderId === deps.moderatorUserId) return true; if (!deps.moderatorBotToken) return true; 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; } return { initDiscussion, getDiscussion, isClosedDiscussion(channelId: string): boolean { return isDiscussionClosed(channelId); }, maybeSendIdleReminder, maybeReplyClosedChannel, handleCallback, }; }