feat(csm): bootstrap discussion callback flow
This commit is contained in:
@@ -7,6 +7,21 @@ type ToolDeps = {
|
||||
api: OpenClawPluginApi;
|
||||
baseConfig: DirigentConfig;
|
||||
pickDefined: (obj: Record<string, unknown>) => Record<string, unknown>;
|
||||
discussionService?: {
|
||||
initDiscussion: (params: {
|
||||
discussionChannelId: string;
|
||||
originChannelId: string;
|
||||
initiatorAgentId: string;
|
||||
initiatorSessionId: string;
|
||||
discussGuide: string;
|
||||
}) => Promise<unknown>;
|
||||
handleCallback: (params: {
|
||||
channelId: string;
|
||||
summaryPath: string;
|
||||
callerAgentId?: string;
|
||||
callerSessionKey?: string;
|
||||
}) => Promise<unknown>;
|
||||
};
|
||||
};
|
||||
|
||||
function parseAccountToken(api: OpenClawPluginApi, accountId?: string): { accountId: string; token: string } | null {
|
||||
@@ -46,7 +61,7 @@ function roleOrMemberType(v: unknown): number {
|
||||
}
|
||||
|
||||
export function registerDirigentTools(deps: ToolDeps): void {
|
||||
const { api, baseConfig, pickDefined } = deps;
|
||||
const { api, baseConfig, pickDefined, discussionService } = deps;
|
||||
|
||||
async function executeDiscordAction(action: DiscordControlAction, params: Record<string, unknown>) {
|
||||
const live = baseConfig as DirigentConfig & {
|
||||
@@ -68,6 +83,12 @@ export function registerDirigentTools(deps: ToolDeps): void {
|
||||
const name = String(params.name || "").trim();
|
||||
if (!guildId || !name) return { content: [{ type: "text", text: "guildId and name are required" }], isError: true };
|
||||
|
||||
const callbackChannelId = typeof params.callbackChannelId === "string" ? params.callbackChannelId.trim() : "";
|
||||
const discussGuide = typeof params.discussGuide === "string" ? params.discussGuide.trim() : "";
|
||||
if (callbackChannelId && !discussGuide) {
|
||||
return { content: [{ type: "text", text: "discussGuide is required when callbackChannelId is provided" }], isError: true };
|
||||
}
|
||||
|
||||
const allowedUserIds = Array.isArray(params.allowedUserIds) ? params.allowedUserIds.map(String) : [];
|
||||
const allowedRoleIds = Array.isArray(params.allowedRoleIds) ? params.allowedRoleIds.map(String) : [];
|
||||
const allowMask = String(params.allowMask || "1024");
|
||||
@@ -91,7 +112,18 @@ export function registerDirigentTools(deps: ToolDeps): void {
|
||||
|
||||
const resp = await discordRequest(token, "POST", `/guilds/${guildId}/channels`, body);
|
||||
if (!resp.ok) return { content: [{ type: "text", text: `discord action failed (${resp.status}): ${resp.text}` }], isError: true };
|
||||
return { content: [{ type: "text", text: JSON.stringify({ ok: true, accountId: selected.accountId, channel: resp.json }, null, 2) }] };
|
||||
|
||||
if (callbackChannelId && discussGuide && discussionService) {
|
||||
await discussionService.initDiscussion({
|
||||
discussionChannelId: String(resp.json?.id || ""),
|
||||
originChannelId: callbackChannelId,
|
||||
initiatorAgentId: String((params.__agentId as string | undefined) || ""),
|
||||
initiatorSessionId: String((params.__sessionKey as string | undefined) || ""),
|
||||
discussGuide,
|
||||
});
|
||||
}
|
||||
|
||||
return { content: [{ type: "text", text: JSON.stringify({ ok: true, accountId: selected.accountId, channel: resp.json, discussionMode: !!callbackChannelId }, null, 2) }] };
|
||||
}
|
||||
|
||||
const channelId = String(params.channelId || "").trim();
|
||||
@@ -152,9 +184,47 @@ export function registerDirigentTools(deps: ToolDeps): void {
|
||||
addRoleIds: { type: "array", items: { type: "string" } },
|
||||
removeTargetIds: { type: "array", items: { type: "string" } },
|
||||
denyMask: { type: "string" },
|
||||
callbackChannelId: { type: "string" },
|
||||
discussGuide: { type: "string" },
|
||||
},
|
||||
required: ["action"],
|
||||
},
|
||||
handler: async (params) => executeDiscordAction(params.action as DiscordControlAction, params as Record<string, unknown>),
|
||||
handler: async (params, ctx) => {
|
||||
const nextParams = {
|
||||
...(params as Record<string, unknown>),
|
||||
__agentId: ctx?.agentId,
|
||||
__sessionKey: ctx?.sessionKey,
|
||||
};
|
||||
return executeDiscordAction(params.action as DiscordControlAction, nextParams);
|
||||
},
|
||||
});
|
||||
|
||||
api.registerTool({
|
||||
name: "discuss-callback",
|
||||
description: "Close a discussion channel and notify the origin work channel with the discussion summary path",
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
properties: {
|
||||
summaryPath: { type: "string" },
|
||||
},
|
||||
required: ["summaryPath"],
|
||||
},
|
||||
handler: async (params, ctx) => {
|
||||
if (!discussionService) {
|
||||
return { content: [{ type: "text", text: "discussion service is not available" }], isError: true };
|
||||
}
|
||||
try {
|
||||
const result = await discussionService.handleCallback({
|
||||
channelId: String(ctx?.channelId || ""),
|
||||
summaryPath: String((params as Record<string, unknown>).summaryPath || ""),
|
||||
callerAgentId: ctx?.agentId,
|
||||
callerSessionKey: ctx?.sessionKey,
|
||||
});
|
||||
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
||||
} catch (error) {
|
||||
return { content: [{ type: "text", text: `discuss-callback failed: ${String(error)}` }], isError: true };
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user