discourse/plugins/discourse-ai/app/models/ai_tool_action.rb
Rafael dos Santos Silva fe5e4a27e9
FEATURE: Add human-in-the-loop approval queue for AI agent tool actions (#38446)
## 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
2026-03-13 12:46:59 -03:00

29 lines
737 B
Ruby
Vendored

# frozen_string_literal: true
class AiToolAction < ActiveRecord::Base
belongs_to :ai_agent
validates :tool_name, presence: true
validates :bot_user_id, presence: true
end
# == Schema Information
#
# Table name: ai_tool_actions
#
# id :bigint not null, primary key
# tool_name :string not null
# tool_parameters :jsonb not null
# created_at :datetime not null
# updated_at :datetime not null
# ai_agent_id :bigint not null
# bot_user_id :integer not null
# post_id :integer
#
# Indexes
#
# index_ai_tool_actions_on_ai_agent_id (ai_agent_id)
#
# Foreign Keys
#
# fk_rails_... (ai_agent_id => ai_agents.id)
#