mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-26 21:18:34 +08:00
## Summary AI agents have 13 moderation tools (close_topic, delete_topic, edit_tags, edit_post, etc.) that currently execute immediately without human oversight. This adds an optional approval queue that routes these tool actions through Discourse's review queue for moderator approval before execution. - **New `require_approval` toggle** on AI agents — when enabled, moderation tool calls are intercepted and sent to the review queue instead of executing immediately - **Review queue integration** — moderators see the agent name, tool name, parameters, and a rendered snippet of the triggering post, then approve or reject - **Loop prevention** — approved tool execution is wrapped in `DiscourseAutomation.set_active_automation` to prevent automation re-trigger loops (e.g., `edit_tags` → `topic_tags_changed` → automation fires again) ### New files - `AiToolAction` model — stores tool name, parameters (JSONB), agent/bot user refs, and triggering post ID - `ReviewableAiToolAction` — Reviewable subclass with approve (executes tool) and reject (discards) actions - `ReviewableAiToolActionSerializer` — serializes target tool data and payload context - Review queue frontend component — displays tool action details and post snippet - Two migrations: `ai_tool_actions` table and `require_approval` column on `ai_agents` ### Modified files - `Tool` base class gains `requires_approval?` (default `false`), overridden to `true` on all 13 moderation tools - `Bot#invoke_tool` — intercepts tools when both tool and agent opt in to approval - Agent admin editor — new "Require approval" checkbox - Agent REST model — `require_approval` added to attribute whitelists for save payloads - Serializer, controller, plugin.rb — wired up for the new field and reviewable type
111 lines
3.6 KiB
Ruby
Vendored
111 lines
3.6 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
module DiscourseAi
|
|
module Agents
|
|
module Tools
|
|
class MovePosts < Tool
|
|
def self.signature
|
|
{
|
|
name: name,
|
|
description: "Moves posts from one topic to another existing topic, or to a new topic.",
|
|
parameters: [
|
|
{
|
|
name: "topic_id",
|
|
description: "The ID of the source topic containing the posts to move",
|
|
type: "integer",
|
|
required: true,
|
|
},
|
|
{
|
|
name: "post_ids",
|
|
description: "Array of post IDs to move",
|
|
type: "array",
|
|
item_type: "integer",
|
|
required: true,
|
|
},
|
|
{
|
|
name: "destination_topic_id",
|
|
description:
|
|
"The ID of the existing topic to move posts to. Either this or new_title must be provided.",
|
|
type: "integer",
|
|
},
|
|
{
|
|
name: "new_title",
|
|
description:
|
|
"Title for a new topic to create and move posts into. Either this or destination_topic_id must be provided.",
|
|
type: "string",
|
|
},
|
|
{
|
|
name: "category_id",
|
|
description: "Category ID for the new topic (only used with new_title)",
|
|
type: "integer",
|
|
},
|
|
{
|
|
name: "reason",
|
|
description: "Short explanation of why the posts are being moved",
|
|
type: "string",
|
|
required: true,
|
|
},
|
|
],
|
|
}
|
|
end
|
|
|
|
def self.name
|
|
"move_posts"
|
|
end
|
|
|
|
def self.requires_approval?
|
|
true
|
|
end
|
|
|
|
def invoke
|
|
topic = Topic.find_by(id: parameters[:topic_id])
|
|
return error_response(I18n.t("discourse_ai.ai_bot.move_posts.errors.not_found")) if !topic
|
|
|
|
if !guardian.can_move_posts?(topic)
|
|
return error_response(I18n.t("discourse_ai.ai_bot.move_posts.errors.not_allowed"))
|
|
end
|
|
|
|
if reason.blank?
|
|
return error_response(I18n.t("discourse_ai.ai_bot.move_posts.errors.no_reason"))
|
|
end
|
|
|
|
post_ids = parameters[:post_ids]
|
|
if post_ids.blank?
|
|
return error_response(I18n.t("discourse_ai.ai_bot.move_posts.errors.no_posts"))
|
|
end
|
|
|
|
destination_topic_id = parameters[:destination_topic_id]
|
|
new_title = parameters[:new_title]
|
|
|
|
if destination_topic_id.blank? && new_title.blank?
|
|
return(error_response(I18n.t("discourse_ai.ai_bot.move_posts.errors.no_destination")))
|
|
end
|
|
|
|
opts = {}
|
|
if destination_topic_id.present?
|
|
opts[:destination_topic_id] = destination_topic_id
|
|
else
|
|
opts[:title] = new_title
|
|
opts[:category_id] = parameters[:category_id] if parameters[:category_id].present?
|
|
end
|
|
|
|
destination_topic = topic.move_posts(acting_user, post_ids, opts)
|
|
|
|
if destination_topic.present?
|
|
{
|
|
status: "success",
|
|
message: I18n.t("discourse_ai.ai_bot.move_posts.success"),
|
|
destination_topic_id: destination_topic.id,
|
|
}
|
|
else
|
|
error_response(I18n.t("discourse_ai.ai_bot.move_posts.errors.move_failed"))
|
|
end
|
|
end
|
|
|
|
def description_args
|
|
{ topic_id: parameters[:topic_id], post_ids: (parameters[:post_ids] || []).join(", ") }
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|