A platform for community discussion. Free, open, simple.
FEATURE: Add human-in-the-loop approval queue for AI agent tool actions (#38446)
Rafael dos Santos Silvafe5e4a27e989
diff --git a/frontend/discourse/app/helpers/reviewable-status.js b/frontend/discourse/app/helpers/reviewable-status.jsindex 95d722ea935..6f085a83462 100644--- a/frontend/discourse/app/helpers/reviewable-status.js+++ b/frontend/discourse/app/helpers/reviewable-status.js@@ -9,6 +9,31 @@ import {} from "discourse/models/reviewable";import { i18n } from "discourse-i18n";+const STATUS_NAMES = {+ approved: {},+ rejected: {},+};++/**+ * Register a custom status name for a reviewable type.+ *+ * This allows plugins to customize the status badge text shown in the+ * review queue after an action is performed (e.g. "Tool approved" instead+ * of the default "Flag approved").+ *+ * @param {string} reviewableType - The reviewable class name (e.g. "ReviewableAiToolAction")+ * @param {string} approvedName - i18n key suffix for the approved status (looked up as `review.statuses.{name}.title`)+ * @param {string} rejectedName - i18n key suffix for the rejected status (looked up as `review.statuses.{name}.title`)+ */+export function registerReviewableStatusName(+ reviewableType,+ approvedName,+ rejectedName+) {+ STATUS_NAMES.approved[reviewableType] = approvedName;+ STATUS_NAMES.rejected[reviewableType] = rejectedName;+}+function dataFor(status, type) {switch (status) {case PENDING:@@ -30,7 +55,7 @@ function dataFor(status, type) {default:return {icon: "check",- name: "approved_flag",+ name: STATUS_NAMES.approved[type] || "approved_flag",cssClass: "approved",};}@@ -51,7 +76,7 @@ function dataFor(status, type) {default:return {icon: "xmark",- name: "rejected_flag",+ name: STATUS_NAMES.rejected[type] || "rejected_flag",cssClass: "rejected",};}diff --git a/frontend/discourse/app/lib/plugin-api.gjs b/frontend/discourse/app/lib/plugin-api.gjsindex ce483e0b262..9b000e381ed 100644--- a/frontend/discourse/app/lib/plugin-api.gjs+++ b/frontend/discourse/app/lib/plugin-api.gjs@@ -51,6 +51,7 @@ import {replaceCategoryLinkRenderer,} from "discourse/helpers/category-link";import { addUsernameSelectorDecorator } from "discourse/helpers/decorate-username-selector";+import { registerReviewableStatusName } from "discourse/helpers/reviewable-status";import { registerCustomAvatarHelper } from "discourse/helpers/user-avatar";import { addBeforeAuthCompleteCallback } from "discourse/instance-initializers/auth-complete";import { registerAdminPluginConfigNav } from "discourse/lib/admin-plugin-config-nav";@@ -2268,6 +2269,25 @@ class _PluginApi {registerReviewableActionModal(reviewableType, modalClass);}+ /**+ * Register custom status names for a reviewable type, used in the+ * review queue status badge (e.g. "Tool approved" instead of "Flag approved").+ *+ * The names are i18n key suffixes looked up as `review.statuses.{name}.title`.+ *+ * @param {String} reviewableType - The reviewable class name (e.g. "ReviewableAiToolAction")+ * @param {String} approvedName - Status name for approved items+ * @param {String} rejectedName - Status name for rejected items+ *+ * @example+ * ```+ * api.registerReviewableStatusName("ReviewableAiToolAction", "approved_tool_action", "rejected_tool_action");+ * ```+ **/+ registerReviewableStatusName(reviewableType, approvedName, rejectedName) {+ registerReviewableStatusName(reviewableType, approvedName, rejectedName);+ }+/*** Change the default category background and text colors in the* category creation modal.diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjsindex 38894e8df3d..984b678ee63 100644--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs@@ -831,6 +831,17 @@ export default class AgentEditor extends Component {><field.Control /></form.Field>++ <form.Field+ @name="require_approval"+ @title={{i18n "discourse_ai.ai_agent.require_approval"}}+ @tooltip={{i18n "discourse_ai.ai_agent.require_approval_help"}}+ @showTitle={{false}}+ @format="large"+ as |field|+ >+ <field.Checkbox />+ </form.Field></form.Section>{{#if this.siteSettings.ai_embeddings_enabled}}diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rbindex 030bfc0fe82..f2c8c201857 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb@@ -438,6 +438,7 @@ module DiscourseAi:execution_mode,:max_turn_tokens,:compression_threshold,+ :require_approval,allowed_group_ids: [],rag_uploads: [:id],)@@ -561,6 +562,8 @@ module DiscourseAi},compression_threshold: {},+ require_approval: {+ },# JSON fieldsjson_fields: %i[tools response_format examples allowed_group_ids],}diff --git a/plugins/discourse-ai/app/models/ai_agent.rb b/plugins/discourse-ai/app/models/ai_agent.rbindex 66a6a792ba3..76d6608f72b 100644--- a/plugins/discourse-ai/app/models/ai_agent.rb+++ b/plugins/discourse-ai/app/models/ai_agent.rb@@ -222,6 +222,7 @@ class AiAgent < ActiveRecord::Baseexecution_modemax_turn_tokenscompression_threshold+ require_approval]instance_attributes = {}@@ -452,6 +453,7 @@ end# rag_chunk_overlap_tokens :integer default(10), not null# rag_chunk_tokens :integer default(374), not null# rag_conversation_chunks :integer default(10), not null+# require_approval :boolean default(FALSE), not null# response_format :jsonb# show_thinking :boolean default(TRUE), not null# system :boolean default(FALSE), not nulldiff --git a/plugins/discourse-ai/app/models/ai_tool_action.rb b/plugins/discourse-ai/app/models/ai_tool_action.rbnew file mode 100644index 00000000000..5df6c8aedcc--- /dev/null+++ b/plugins/discourse-ai/app/models/ai_tool_action.rb@@ -0,0 +1,29 @@+# 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)+#diff --git a/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rbnew file mode 100644index 00000000000..0ed194dbaf2--- /dev/null+++ b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb@@ -0,0 +1,126 @@+# frozen_string_literal: true++require_dependency "reviewable"++class ReviewableAiToolAction < Reviewable+ def build_actions(actions, guardian, args)+ return actions if !pending?++ approve =+ actions.add_bundle(+ "#{id}-approve",+ icon: "check",+ label: "discourse_ai.reviewables.ai_tool_action.approve.title",+ )++ build_action(actions, :approve, icon: "check", bundle: approve)++ reject =+ actions.add_bundle(+ "#{id}-reject",+ icon: "xmark",+ label: "discourse_ai.reviewables.ai_tool_action.reject.title",+ )++ build_action(actions, :reject, icon: "xmark", bundle: reject)+ end++ def perform_approve(performed_by, args)+ tool_action = target+ if tool_action.blank?+ raise Discourse::InvalidAccess.new(+ I18n.t("discourse_ai.reviewables.ai_tool_action.target_missing"),+ )+ end++ tool_class =+ DiscourseAi::Agents::Agent.all_available_tools.find { |t| t.name == tool_action.tool_name }++ if tool_class.blank?+ raise Discourse::InvalidAccess.new(+ I18n.t(+ "discourse_ai.reviewables.ai_tool_action.tool_not_found",+ tool_name: tool_action.tool_name,+ ),+ )+ end++ bot_user = User.find_by(id: tool_action.bot_user_id)+ if bot_user.blank?+ raise Discourse::InvalidAccess.new(+ I18n.t("discourse_ai.reviewables.ai_tool_action.bot_user_missing"),+ )+ end++ tool =+ tool_class.new(+ tool_action.tool_parameters.symbolize_keys,+ bot_user: bot_user,+ llm: nil,+ context: DiscourseAi::Agents::BotContext.new(messages: []),+ )++ # Suppress automation re-triggers caused by the tool's side effects+ # (e.g. edit_tags → topic_tags_changed → automation fires again → loop).+ # Setting an active automation makes trigger!() return early.+ begin+ DiscourseAutomation.set_active_automation(id) if defined?(DiscourseAutomation)+ tool.invoke+ ensure+ DiscourseAutomation.set_active_automation(nil) if defined?(DiscourseAutomation)+ end++ create_result(:success, :approved)+ end++ def perform_reject(performed_by, args)+ create_result(:success, :rejected)+ end++ private++ def build_action(actions, id, icon:, bundle: nil)+ actions.add(id, bundle: bundle) do |action|+ action.icon = icon+ action.label = "discourse_ai.reviewables.ai_tool_action.#{id}.title"+ end+ end+end++# == Schema Information+#+# Table name: reviewables+#+# id :bigint not null, primary key+# type :string not null+# status :integer default("pending"), not null+# created_by_id :integer not null+# reviewable_by_moderator :boolean default(FALSE), not null+# category_id :integer+# topic_id :integer+# score :float default(0.0), not null+# potential_spam :boolean default(FALSE), not null+# target_id :integer+# target_type :string+# target_created_by_id :integer+# payload :json+# version :integer default(0), not null+# latest_score :datetime+# created_at :datetime not null+# updated_at :datetime not null+# force_review :boolean default(FALSE), not null+# reject_reason :text+# potentially_illegal :boolean default(FALSE)+# type_source :string default("unknown"), not null+#+# Indexes+#+# idx_reviewables_score_desc_created_at_desc (score,created_at)+# index_reviewables_on_reviewable_by_group_id (reviewable_by_group_id)+# index_reviewables_on_status_and_created_at (status,created_at)+# index_reviewables_on_status_and_score (status,score)+# index_reviewables_on_status_and_type (status,type)+# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)+# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)+# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE+#diff --git a/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rb b/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rbindex 84c81698e8c..8568ba76906 100644--- a/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rb+++ b/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rb@@ -34,6 +34,7 @@ class LocalizedAiAgentSerializer < ApplicationSerializer:execution_mode,:max_turn_tokens,:compression_threshold,+ :require_approval,:response_format,:examples,:featuresdiff --git a/plugins/discourse-ai/app/serializers/reviewable_ai_tool_action_serializer.rb b/plugins/discourse-ai/app/serializers/reviewable_ai_tool_action_serializer.rbnew file mode 100644index 00000000000..83c4f8cb608--- /dev/null+++ b/plugins/discourse-ai/app/serializers/reviewable_ai_tool_action_serializer.rb@@ -0,0 +1,13 @@+# frozen_string_literal: true++class ReviewableAiToolActionSerializer < ReviewableSerializer+ target_attributes :tool_name, :tool_parameters, :post_id, :bot_user_id, :ai_agent_id+ payload_attributes :agent_name, :reason, :llm_model_id++ attributes :cooked++ def cooked+ post_id = object.target&.post_id+ Post.find_by(id: post_id)&.cooked if post_id+ end+enddiff --git a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.js b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.jsindex e057b26920c..78696373bba 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.js@@ -37,6 +37,7 @@ const CREATE_ATTRIBUTES = ["max_turn_tokens","compression_threshold",+ "require_approval","response_format","examples",];@@ -70,6 +71,7 @@ const SYSTEM_ATTRIBUTES = ["max_turn_tokens","compression_threshold",+ "require_approval",];export default class AiAgent extends RestModel {diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/reviewable/ai-tool-action.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/reviewable/ai-tool-action.gjsnew file mode 100644index 00000000000..4c6fe0cd417--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/reviewable/ai-tool-action.gjs@@ -0,0 +1,60 @@+import Component from "@glimmer/component";+import { trustHTML } from "@ember/template";+import DecoratedHtml from "discourse/components/decorated-html";+import { i18n } from "discourse-i18n";++export default class ReviewableAiToolAction extends Component {+ get toolParameters() {+ const params = this.args.reviewable.tool_parameters;+ if (!params || typeof params !== "object") {+ return [];+ }+ return Object.entries(params).map(([key, value]) => ({+ key,+ value: typeof value === "object" ? JSON.stringify(value) : String(value),+ }));+ }++ <template>+ <div class="review-item__meta-content">+ <div class="review-item__meta-label">{{i18n+ "discourse_ai.reviewables.ai_tool_action.agent"+ }}</div>+ <div+ class="review-item__meta-value"+ >{{@reviewable.payload.agent_name}}</div>++ <div class="review-item__meta-label">{{i18n+ "discourse_ai.reviewables.ai_tool_action.tool"+ }}</div>+ <div class="review-item__meta-value">{{@reviewable.tool_name}}</div>++ {{#if this.toolParameters.length}}+ <div class="review-item__meta-label">{{i18n+ "discourse_ai.reviewables.ai_tool_action.parameters"+ }}</div>+ <div class="review-item__meta-value">+ <ul>+ {{#each this.toolParameters as |param|}}+ <li><strong>{{param.key}}</strong>: {{param.value}}</li>+ {{/each}}+ </ul>+ </div>+ {{/if}}+ </div>++ {{#if @reviewable.cooked}}+ <div class="review-item__post">+ <div class="review-item__post-content-wrapper">+ <DecoratedHtml+ @className="review-item__post-content"+ @html={{trustHTML @reviewable.cooked}}+ @model={{@reviewable}}+ />+ </div>+ </div>+ {{/if}}++ {{yield}}+ </template>+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-reviewable-status.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-reviewable-status.jsnew file mode 100644index 00000000000..97411672085--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-reviewable-status.js@@ -0,0 +1,15 @@+import { withPluginApi } from "discourse/lib/plugin-api";++export default {+ name: "ai-reviewable-status",++ initialize() {+ withPluginApi((api) => {+ api.registerReviewableStatusName(+ "ReviewableAiToolAction",+ "approved_tool_action",+ "rejected_tool_action"+ );+ });+ },+};diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex 27894cb67e7..0d3a42e70c8 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -561,6 +561,8 @@ en:allow_personal_messages_help: "If enabled, users in allowed groups can send personal messages to this agent."allow_topic_mentions: "Allow topic mentions"allow_topic_mentions_help: "If enabled, users in allowed groups can mention this agent in topics."+ require_approval: "Require approval for moderation tools"+ require_approval_help: "When enabled, moderation actions (close, delete, edit, etc.) will be sent to the review queue for moderator approval before execution."force_default_llm: "Always use default language model"save: "Save"saved: "Agent saved"@@ -951,6 +953,11 @@ en:reviewables:model_used: "Model used:"accuracy: "Accuracy:"+ ai_tool_action:+ agent: "Agent"+ tool: "Tool"+ parameters: "Parameters"+ post: "Triggering Post"embeddings:short_title: "Embeddings"@@ -1178,8 +1185,15 @@ en:user_preferences:empty: "There are no relevant settings available at this time"review:+ statuses:+ approved_tool_action:+ title: "Tool approved"+ rejected_tool_action:+ title: "Tool rejected"types:reviewable_ai_post:title: "AI-Flagged post"reviewable_ai_chat_message:title: "AI-Flagged chat message"+ reviewable_ai_tool_action:+ title: "AI Tool Action"diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex e5b00f68afe..e008a26bfd8 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -350,6 +350,7 @@ en:reply_error: "Sorry, it looks like our system encountered an unexpected issue while trying to reply.\n\n[details='Error details']\n%{details}\n[/details]"default_pm_prefix: "[Untitled AI bot PM]"thinking: "Thinking"+ tool_pending_approval: "This action requires moderator approval before it can be executed."agents:default_llm_required: "Default LLM model is required prior to enabling Chat"cannot_delete_system_agent: "System agents cannot be deleted, please disable it instead"@@ -778,6 +779,18 @@ en:action_failed: "Failed to update the solved status."success: "Solved status updated successfully."+ reviewables:+ ai_tool_action:+ title: "AI Tool Action"+ approve:+ title: "Approve"+ reject:+ title: "Reject"+ target_missing: "The tool action target no longer exists."+ tool_not_found: "Tool '%{tool_name}' is no longer available."+ bot_user_missing: "The bot user no longer exists."+ execution_error: "Tool execution failed: %{error}"+summarization:configuration_hint:one: "Configure the `%{setting}` setting first."diff --git a/plugins/discourse-ai/db/migrate/20260310121612_create_ai_tool_actions.rb b/plugins/discourse-ai/db/migrate/20260310121612_create_ai_tool_actions.rbnew file mode 100644index 00000000000..3bc67c7f35c--- /dev/null+++ b/plugins/discourse-ai/db/migrate/20260310121612_create_ai_tool_actions.rb@@ -0,0 +1,14 @@+# frozen_string_literal: true++class CreateAiToolActions < ActiveRecord::Migration[7.2]+ def change+ create_table :ai_tool_actions do |t|+ t.string :tool_name, null: false+ t.jsonb :tool_parameters, default: {}, null: false+ t.references :ai_agent, null: false, foreign_key: true+ t.integer :bot_user_id, null: false+ t.integer :post_id+ t.timestamps+ end+ end+enddiff --git a/plugins/discourse-ai/db/migrate/20260310121613_add_require_approval_to_ai_agents.rb b/plugins/discourse-ai/db/migrate/20260310121613_add_require_approval_to_ai_agents.rbnew file mode 100644index 00000000000..f31a1331950--- /dev/null+++ b/plugins/discourse-ai/db/migrate/20260310121613_add_require_approval_to_ai_agents.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++class AddRequireApprovalToAiAgents < ActiveRecord::Migration[7.2]+ def change+ add_column :ai_agents, :require_approval, :boolean, default: false, null: false+ end+enddiff --git a/plugins/discourse-ai/lib/agents/bot.rb b/plugins/discourse-ai/lib/agents/bot.rbindex 26357c85162..bd0bb0c0861 100644--- a/plugins/discourse-ai/lib/agents/bot.rb+++ b/plugins/discourse-ai/lib/agents/bot.rb@@ -298,6 +298,45 @@ module DiscourseAiembedded_thinkingend+ def tool_requires_approval?(tool)+ tool.class.requires_approval? && @agent.class.require_approval+ end++ def enqueue_tool_for_approval(tool, context, &update_blk)+ tool_action =+ AiToolAction.create!(+ tool_name: tool.name,+ tool_parameters: tool.parameters,+ ai_agent_id: @agent.id,+ bot_user_id: @bot_user.id,+ post_id: context.post_id,+ )++ reviewable =+ ReviewableAiToolAction.needs_review!(+ target: tool_action,+ created_by: @bot_user,+ reviewable_by_moderator: true,+ payload: {+ agent_name: @agent.class.name,+ reason: tool.parameters[:reason],+ llm_model_id: @model&.id,+ },+ )++ reviewable.add_score(+ Discourse.system_user,+ ReviewableScore.types[:needs_approval],+ force_review: true,+ )++ placeholder =+ build_placeholder(tool.summary, I18n.t("discourse_ai.ai_bot.tool_pending_approval"))+ update_blk.call(placeholder, nil, :thinking)++ { status: "pending_approval", message: I18n.t("discourse_ai.ai_bot.tool_pending_approval") }+ end+def process_tool(tool:,raw_context:,@@ -359,6 +398,10 @@ module DiscourseAienddef invoke_tool(tool, context, &update_blk)+ if tool_requires_approval?(tool)+ return enqueue_tool_for_approval(tool, context, &update_blk)+ end+show_placeholder = !context.skip_show_thinking && !tool.class.allow_partial_tool_calls?update_blk.call("", build_placeholder(tool.summary, ""), :thinking) if show_placeholderdiff --git a/plugins/discourse-ai/lib/agents/tools/assign.rb b/plugins/discourse-ai/lib/agents/tools/assign.rbindex 1aa28107613..265c1d6564c 100644--- a/plugins/discourse-ai/lib/agents/tools/assign.rb+++ b/plugins/discourse-ai/lib/agents/tools/assign.rb@@ -51,6 +51,10 @@ module DiscourseAi"assign"end+ def self.requires_approval?+ true+ end+def invokeif !defined?(::Assigner)return(error_response(I18n.t("discourse_ai.ai_bot.assign.errors.plugin_not_installed")))diff --git a/plugins/discourse-ai/lib/agents/tools/close_topic.rb b/plugins/discourse-ai/lib/agents/tools/close_topic.rbindex bc5b0a190c7..599af8587a4 100644--- a/plugins/discourse-ai/lib/agents/tools/close_topic.rb+++ b/plugins/discourse-ai/lib/agents/tools/close_topic.rb@@ -41,6 +41,10 @@ module DiscourseAi"close_topic"end+ def self.requires_approval?+ true+ end+def invoketopic = Topic.find_by(id: parameters[:topic_id])if !topicdiff --git a/plugins/discourse-ai/lib/agents/tools/delete_topic.rb b/plugins/discourse-ai/lib/agents/tools/delete_topic.rbindex ad5bec63c87..11cf1e37780 100644--- a/plugins/discourse-ai/lib/agents/tools/delete_topic.rb+++ b/plugins/discourse-ai/lib/agents/tools/delete_topic.rb@@ -35,6 +35,10 @@ module DiscourseAi"delete_topic"end+ def self.requires_approval?+ true+ end+def invoketopic = Topic.with_deleted.find_by(id: parameters[:topic_id])if !topicdiff --git a/plugins/discourse-ai/lib/agents/tools/edit_category.rb b/plugins/discourse-ai/lib/agents/tools/edit_category.rbindex 197a48caa2b..8371d757f32 100644--- a/plugins/discourse-ai/lib/agents/tools/edit_category.rb+++ b/plugins/discourse-ai/lib/agents/tools/edit_category.rb@@ -40,6 +40,10 @@ module DiscourseAi"edit_category"end+ def self.requires_approval?+ true+ end+def invoketopic = Topic.find_by(id: parameters[:topic_id])if !topicdiff --git a/plugins/discourse-ai/lib/agents/tools/edit_post.rb b/plugins/discourse-ai/lib/agents/tools/edit_post.rbindex 5212c7c00f9..e81139e7ab8 100644--- a/plugins/discourse-ai/lib/agents/tools/edit_post.rb+++ b/plugins/discourse-ai/lib/agents/tools/edit_post.rb@@ -41,6 +41,10 @@ module DiscourseAi"edit_post"end+ def self.requires_approval?+ true+ end+def invokepost = Post.find_by(id: parameters[:post_id])return error_response(I18n.t("discourse_ai.ai_bot.edit_post.errors.not_found")) if !postdiff --git a/plugins/discourse-ai/lib/agents/tools/edit_tags.rb b/plugins/discourse-ai/lib/agents/tools/edit_tags.rbindex cc77d10da83..d0ea4beac32 100644--- a/plugins/discourse-ai/lib/agents/tools/edit_tags.rb+++ b/plugins/discourse-ai/lib/agents/tools/edit_tags.rb@@ -47,6 +47,10 @@ module DiscourseAi"edit_tags"end+ def self.requires_approval?+ true+ end+def invokeif !SiteSetting.tagging_enabledreturn error_response(I18n.t("discourse_ai.ai_bot.edit_tags.errors.tagging_disabled"))diff --git a/plugins/discourse-ai/lib/agents/tools/grant_badge.rb b/plugins/discourse-ai/lib/agents/tools/grant_badge.rbindex c464d8b83bb..9471aa802c3 100644--- a/plugins/discourse-ai/lib/agents/tools/grant_badge.rb+++ b/plugins/discourse-ai/lib/agents/tools/grant_badge.rb@@ -35,6 +35,10 @@ module DiscourseAi"grant_badge"end+ def self.requires_approval?+ true+ end+def invokeif !guardian.can_grant_badges?(nil)return error_response(I18n.t("discourse_ai.ai_bot.grant_badge.errors.not_allowed"))diff --git a/plugins/discourse-ai/lib/agents/tools/lock_post.rb b/plugins/discourse-ai/lib/agents/tools/lock_post.rbindex b7deb922a87..aebf3eb6853 100644--- a/plugins/discourse-ai/lib/agents/tools/lock_post.rb+++ b/plugins/discourse-ai/lib/agents/tools/lock_post.rb@@ -35,6 +35,10 @@ module DiscourseAi"lock_post"end+ def self.requires_approval?+ true+ end+def invokepost = Post.find_by(id: parameters[:post_id])return error_response(I18n.t("discourse_ai.ai_bot.lock_post.errors.not_found")) if !postdiff --git a/plugins/discourse-ai/lib/agents/tools/mark_as_solved.rb b/plugins/discourse-ai/lib/agents/tools/mark_as_solved.rbindex 846d6e20d21..80c5ab4bbf4 100644--- a/plugins/discourse-ai/lib/agents/tools/mark_as_solved.rb+++ b/plugins/discourse-ai/lib/agents/tools/mark_as_solved.rb@@ -37,6 +37,10 @@ module DiscourseAi"mark_as_solved"end+ def self.requires_approval?+ true+ end+def invokeif !defined?(::DiscourseSolved)return(diff --git a/plugins/discourse-ai/lib/agents/tools/move_posts.rb b/plugins/discourse-ai/lib/agents/tools/move_posts.rbindex 5eb801472c0..57eb6d17677 100644--- a/plugins/discourse-ai/lib/agents/tools/move_posts.rb+++ b/plugins/discourse-ai/lib/agents/tools/move_posts.rb@@ -53,6 +53,10 @@ module DiscourseAi"move_posts"end+ def self.requires_approval?+ true+ end+def invoketopic = Topic.find_by(id: parameters[:topic_id])return error_response(I18n.t("discourse_ai.ai_bot.move_posts.errors.not_found")) if !topicdiff --git a/plugins/discourse-ai/lib/agents/tools/set_slow_mode.rb b/plugins/discourse-ai/lib/agents/tools/set_slow_mode.rbindex ed17b178b37..4f31e8e2f75 100644--- a/plugins/discourse-ai/lib/agents/tools/set_slow_mode.rb+++ b/plugins/discourse-ai/lib/agents/tools/set_slow_mode.rb@@ -42,6 +42,10 @@ module DiscourseAi"set_slow_mode"end+ def self.requires_approval?+ true+ end+def invoketopic = Topic.find_by(id: parameters[:topic_id])if !topicdiff --git a/plugins/discourse-ai/lib/agents/tools/set_topic_timer.rb b/plugins/discourse-ai/lib/agents/tools/set_topic_timer.rbindex 232091923fa..8570fa978e6 100644--- a/plugins/discourse-ai/lib/agents/tools/set_topic_timer.rb+++ b/plugins/discourse-ai/lib/agents/tools/set_topic_timer.rb@@ -44,6 +44,10 @@ module DiscourseAi"set_topic_timer"end+ def self.requires_approval?+ true+ end+def invoketopic = Topic.find_by(id: parameters[:topic_id])if !topicdiff --git a/plugins/discourse-ai/lib/agents/tools/tool.rb b/plugins/discourse-ai/lib/agents/tools/tool.rbindex e09953e263e..3f8f334ca5d 100644--- a/plugins/discourse-ai/lib/agents/tools/tool.rb+++ b/plugins/discourse-ai/lib/agents/tools/tool.rb@@ -43,6 +43,10 @@ module DiscourseAifalseend+ def requires_approval?+ false+ end+def inject_prompt(prompt:, context:, agent:)enddiff --git a/plugins/discourse-ai/lib/agents/tools/unlist_topic.rb b/plugins/discourse-ai/lib/agents/tools/unlist_topic.rbindex 1f33b2303a4..1dbe064a5bc 100644--- a/plugins/discourse-ai/lib/agents/tools/unlist_topic.rb+++ b/plugins/discourse-ai/lib/agents/tools/unlist_topic.rb@@ -41,6 +41,10 @@ module DiscourseAi"unlist_topic"end+ def self.requires_approval?+ true+ end+def invoketopic = Topic.find_by(id: parameters[:topic_id])if !topicdiff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rbindex b6afe4f5314..2c637934087 100644--- a/plugins/discourse-ai/plugin.rb+++ b/plugins/discourse-ai/plugin.rb@@ -107,6 +107,7 @@ after_initialize doregister_reviewable_type ReviewableAiChatMessageregister_reviewable_type ReviewableAiPost+ register_reviewable_type ReviewableAiToolActionon(:reviewable_transitioned_to) do |new_status, reviewable|ModelAccuracy.adjust_model_accuracy(new_status, reviewable)diff --git a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb b/plugins/discourse-ai/spec/lib/agents/bot_spec.rbindex d8d1f13fc73..cfbf61f238f 100644--- a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb+++ b/plugins/discourse-ai/spec/lib/agents/bot_spec.rb@@ -611,4 +611,73 @@ RSpec.describe DiscourseAi::Agents::Bot doendendend++ describe "#invoke_tool with require_approval" do+ fab!(:topic)++ it "creates a reviewable instead of executing when require_approval is true" do+ toggle_enabled_bots(bots: [fake])+ Group.refresh_automatic_groups!++ AiAgent.create!(+ name: "ApprovalAgent",+ system_prompt: "test",+ description: "test",+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],+ require_approval: true,+ )++ agent_class = DiscourseAi::Agents::Agent.find_by(user: admin, name: "ApprovalAgent")+ test_bot_user = DiscourseAi::AiBot::EntryPoint.find_user_from_model(fake.name)+ bot = described_class.as(test_bot_user, agent: agent_class.new)++ tool =+ DiscourseAi::Agents::Tools::CloseTopic.new(+ { topic_id: topic.id, closed: true, reason: "Off-topic" },+ bot_user: test_bot_user,+ llm: bot.llm,+ )++ context = DiscourseAi::Agents::BotContext.new(messages: [])++ result = bot.send(:invoke_tool, tool, context) { |*args| }++ expect(result[:status]).to eq("pending_approval")+ expect(topic.reload.closed).to eq(false)+ expect(AiToolAction.last.tool_name).to eq("close_topic")+ expect(ReviewableAiToolAction.count).to eq(1)+ end++ it "executes immediately when require_approval is false" do+ toggle_enabled_bots(bots: [fake])+ Group.refresh_automatic_groups!++ AiAgent.create!(+ name: "NoApprovalAgent",+ system_prompt: "test",+ description: "test",+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],+ require_approval: false,+ )++ agent_class = DiscourseAi::Agents::Agent.find_by(user: admin, name: "NoApprovalAgent")+ test_bot_user = DiscourseAi::AiBot::EntryPoint.find_user_from_model(fake.name)+ bot = described_class.as(test_bot_user, agent: agent_class.new)++ tool =+ DiscourseAi::Agents::Tools::CloseTopic.new(+ { topic_id: topic.id, closed: true, reason: "Off-topic" },+ bot_user: test_bot_user,+ llm: bot.llm,+ )++ context = DiscourseAi::Agents::BotContext.new(messages: [])++ result = bot.send(:invoke_tool, tool, context) { |*args| }++ expect(result[:status]).to eq("success")+ expect(topic.reload.closed).to eq(true)+ expect(ReviewableAiToolAction.count).to eq(0)+ end+ endenddiff --git a/plugins/discourse-ai/spec/models/ai_tool_action_spec.rb b/plugins/discourse-ai/spec/models/ai_tool_action_spec.rbnew file mode 100644index 00000000000..5f344b7f8b7--- /dev/null+++ b/plugins/discourse-ai/spec/models/ai_tool_action_spec.rb@@ -0,0 +1,37 @@+# frozen_string_literal: true++RSpec.describe AiToolAction do+ fab!(:ai_agent)++ before { enable_current_plugin }++ it "validates presence of tool_name" do+ action = AiToolAction.new(bot_user_id: -1, ai_agent: ai_agent)+ expect(action).not_to be_valid+ expect(action.errors[:tool_name]).to be_present+ end++ it "validates presence of bot_user_id" do+ action = AiToolAction.new(tool_name: "close_topic", ai_agent: ai_agent)+ expect(action).not_to be_valid+ expect(action.errors[:bot_user_id]).to be_present+ end++ it "creates a valid record" do+ action =+ AiToolAction.create!(+ tool_name: "close_topic",+ tool_parameters: {+ topic_id: 1,+ closed: true,+ reason: "test",+ },+ ai_agent: ai_agent,+ bot_user_id: -1,+ )++ expect(action).to be_persisted+ expect(action.tool_name).to eq("close_topic")+ expect(action.tool_parameters).to eq("topic_id" => 1, "closed" => true, "reason" => "test")+ end+enddiff --git a/plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rb b/plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rbnew file mode 100644index 00000000000..d1d59179225--- /dev/null+++ b/plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rb@@ -0,0 +1,120 @@+# frozen_string_literal: true++RSpec.describe ReviewableAiToolAction do+ fab!(:admin)+ fab!(:ai_agent)+ fab!(:topic)++ let(:bot_user) { Discourse.system_user }++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ end++ def create_tool_action(tool_name: "close_topic", params: nil)+ params ||= { topic_id: topic.id, closed: true, reason: "Off-topic" }+ AiToolAction.create!(+ tool_name: tool_name,+ tool_parameters: params,+ ai_agent: ai_agent,+ bot_user_id: bot_user.id,+ )+ end++ def create_reviewable(tool_action)+ reviewable =+ described_class.needs_review!(+ target: tool_action,+ created_by: bot_user,+ reviewable_by_moderator: true,+ payload: {+ agent_name: "Test Agent",+ reason: "Off-topic",+ },+ )+ reviewable.add_score(+ Discourse.system_user,+ ReviewableScore.types[:needs_approval],+ force_review: true,+ )+ reviewable+ end++ describe "#build_actions" do+ it "has approve and reject actions when pending" do+ tool_action = create_tool_action+ reviewable = create_reviewable(tool_action)++ actions = Reviewable::Actions.new(reviewable, Guardian.new(admin), {})+ reviewable.build_actions(actions, Guardian.new(admin), {})++ expect(actions.has?(:approve)).to eq(true)+ expect(actions.has?(:reject)).to eq(true)+ end++ it "returns no actions when not pending" do+ tool_action = create_tool_action+ reviewable = create_reviewable(tool_action)+ reviewable.status = Reviewable.statuses[:approved]++ actions = Reviewable::Actions.new(reviewable, Guardian.new(admin), {})+ reviewable.build_actions(actions, Guardian.new(admin), {})++ expect(actions.has?(:approve)).to eq(false)+ expect(actions.has?(:reject)).to eq(false)+ end+ end++ describe "#perform_approve" do+ it "executes the tool and transitions to approved" do+ tool_action = create_tool_action+ reviewable = create_reviewable(tool_action)++ result = reviewable.perform(admin, :approve)++ expect(result.success?).to eq(true)+ expect(result.transition_to).to eq(:approved)+ expect(topic.reload.closed).to eq(true)+ end++ it "raises error when target is missing" do+ tool_action = create_tool_action+ reviewable = create_reviewable(tool_action)+ tool_action.destroy!+ reviewable.reload++ expect { reviewable.perform(admin, :approve) }.to raise_error(Discourse::InvalidAccess)+ end++ it "raises error when tool class is not found" do+ tool_action = create_tool_action(tool_name: "nonexistent_tool")+ reviewable = create_reviewable(tool_action)++ expect { reviewable.perform(admin, :approve) }.to raise_error(Discourse::InvalidAccess)+ end++ it "approves even when tool returns an error result (e.g. stale target)" do+ tool_action = create_tool_action(params: { topic_id: -999, closed: true, reason: "test" })+ reviewable = create_reviewable(tool_action)++ result = reviewable.perform(admin, :approve)++ expect(result.success?).to eq(true)+ expect(result.transition_to).to eq(:approved)+ end+ end++ describe "#perform_reject" do+ it "transitions to rejected without executing the tool" do+ tool_action = create_tool_action+ reviewable = create_reviewable(tool_action)++ result = reviewable.perform(admin, :reject)++ expect(result.success?).to eq(true)+ expect(result.transition_to).to eq(:rejected)+ expect(topic.reload.closed).to eq(false)+ end+ end+end