A platform for community discussion. Free, open, simple.
FEATURE: Add Admin Assistant agent (#41782)
Penar Musaraj03b4d30475dd
diff --git a/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rbindex 4305f07b3ca..f5e7525e634 100644--- a/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb+++ b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb@@ -41,7 +41,7 @@ class ReviewableAiToolAction < Reviewableenddef perform_approve(performed_by, args)- ensure_inline_post_matches_target!(args)+ ensure_inline_post_contains_approval!(args)ensure_performed_by_is_a_real_person!(performed_by)tool, tool_class, context = build_tool!@@ -62,7 +62,7 @@ class ReviewableAiToolAction < Reviewableenddef perform_reject(performed_by, args)- ensure_inline_post_matches_target!(args)+ ensure_inline_post_contains_approval!(args)ensure_performed_by_is_a_real_person!(performed_by)create_result(:success, :rejected)@@ -113,12 +113,20 @@ class ReviewableAiToolAction < Reviewable[tool, tool_class, context]end- def ensure_inline_post_matches_target!(args)+ def ensure_inline_post_contains_approval!(args)inline_post_id = args[:post_id] || args["post_id"]return if inline_post_id.blank?- expected_post_id = target&.post_id- if expected_post_id.blank? || inline_post_id.to_i != expected_post_id+ approval_post =+ Post.find_by(+ id: inline_post_id,+ topic_id: topic_id,+ user_id: target&.bot_user_id,+ deleted_at: nil,+ )+ approval_marker = "data-ai-tool-approval-reviewable-id='#{id}'"++ if approval_post.blank? || !approval_post.raw.include?(approval_marker)raise Discourse::InvalidAccess.new(I18n.t("discourse_ai.reviewables.ai_tool_action.post_mismatch"),)diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-tool-approval.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-tool-approval.gjsindex e52c7470254..bc5ebaeea09 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-tool-approval.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-tool-approval.gjs@@ -92,7 +92,7 @@ export default class AiToolApproval extends Component {@actionasync performAction(actionId) {- if (this.performing || !this.reviewable) {+ if (this.performing || !this.reviewable || !this.args.postId) {return;}@@ -102,7 +102,7 @@ export default class AiToolApproval extends Component {await ajax(`/review/${this.args.reviewableId}/perform/${actionId}`, {type: "PUT",data: {- post_id: this.reviewable.post_id,+ post_id: this.args.postId,version: this.reviewable.version,},});diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-tool-approval.gjs b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-tool-approval.gjsindex 1440bee45e1..22b0295b81e 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-tool-approval.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-tool-approval.gjs@@ -27,7 +27,12 @@ function initializeAiToolApproval(api) {helper.renderGlimmer(cardElement,- <template><AiToolApproval @reviewableId={{reviewableId}} /></template>+ <template>+ <AiToolApproval+ @postId={{post.id}}+ @reviewableId={{reviewableId}}+ />+ </template>);});},diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex 183e4a79310..15391e0eac7 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -476,6 +476,9 @@ en:discourse_helper:name: "Discourse Helper"description: "AI Bot specialized in helping with Discourse related tasks"+ discourse_admin_assistant:+ name: "Discourse Admin Assistant"+ description: "AI Bot that answers Discourse questions and helps administrators manage site settings"web_artifact_creator:name: "Web Artifact Creator"description: "AI Bot specialized in creating interactive web artifacts"diff --git a/plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rb b/plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rbindex 19dba5138ab..638d5d1f8b9 100644--- a/plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rb+++ b/plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rb@@ -33,7 +33,10 @@ DiscourseAi::Agents::Agent.system_agents.each do |agent_class, id|if agent_class == DiscourseAi::Agents::WebArtifactCreator# this is somewhat sensitive, so we default it to staffagent.allowed_group_ids = [Group::AUTO_GROUPS[:staff]]- elsif agent_class == DiscourseAi::Agents::AdminDashboardHighlights+ elsif [+ DiscourseAi::Agents::AdminDashboardHighlights,+ DiscourseAi::Agents::DiscourseAdminAssistant,+ ].include?(agent_class)agent.allowed_group_ids = [Group::AUTO_GROUPS[:admins]]elsif external_agent_ids.include?(id)agent.allowed_group_ids = [Group::AUTO_GROUPS[:admins]]@@ -103,6 +106,7 @@ DiscourseAi::Agents::Agent.system_agents.each do |agent_class, id|agent.response_format = instance.response_formatagent.examples = instance.examples+ agent.require_approval = true if agent_class == DiscourseAi::Agents::DiscourseAdminAssistantagent.system_prompt = instance.system_promptagent.top_p = instance.top_pdiff --git a/plugins/discourse-ai/lib/agents/agent.rb b/plugins/discourse-ai/lib/agents/agent.rbindex 74cb94da14e..cf3205f6f7d 100644--- a/plugins/discourse-ai/lib/agents/agent.rb+++ b/plugins/discourse-ai/lib/agents/agent.rb@@ -242,6 +242,7 @@ module DiscourseAiSentimentClassifier => -36,EmotionClassifier => -37,AdminDashboardHighlights => -38,+ DiscourseAdminAssistant => -39,}.freezeendenddiff --git a/plugins/discourse-ai/lib/agents/discourse_admin_assistant.rb b/plugins/discourse-ai/lib/agents/discourse_admin_assistant.rbnew file mode 100644index 00000000000..0d3118eb658--- /dev/null+++ b/plugins/discourse-ai/lib/agents/discourse_admin_assistant.rb@@ -0,0 +1,58 @@+# frozen_string_literal: true++module DiscourseAi+ module Agents+ class DiscourseAdminAssistant < Agent+ def thinking_effort+ "low"+ end++ def tools+ [+ Tools::DiscourseMetaSearch,+ Tools::ListCategories,+ Tools::ListTags,+ Tools::SettingContext,+ Tools::SearchSettings,+ Tools::ReadSiteSetting,+ Tools::ChangeSiteSetting,+ Tools::ListReviewables,+ Tools::CloseTopic,+ Tools::LockPost,+ Tools::UnlistTopic,+ Tools::DeleteTopic,+ Tools::EditPost,+ Tools::EditCategory,+ Tools::EditTags,+ Tools::MovePosts,+ Tools::SuspendUser,+ Tools::SilenceUser,+ Tools::MarkAsSolved,+ ]+ end++ def system_prompt+ <<~PROMPT+ You are the Discourse Admin Assistant.++ - Answer questions about Discourse using the search function on meta.discourse.org. Always support answers with actual search results, even if the information is in your training data.+ - Search meta.discourse.org twice for every Discourse knowledge question: first with precise keywords, then with a broader query. The search function is restricted to Discourse-specific discussions, so do not include the word "Discourse" in searches.+ - Give practical, concise answers that help an administrator complete the task. Start with the direct answer and do not describe your search process.+ - For "how do I" questions, use a short numbered list of the relevant steps. Include alternative workflows only when they are materially useful.+ - When directing an administrator to an area of this site, use a descriptive Markdown link with an absolute URL based on {site_url}. For example: [Create an invite]({site_url}/new-invite). Never respond with a bare URL.+ - Use Meta search results to support factual guidance and link to the most useful source with descriptive link text. Do not overwhelm the answer with sources.+ - Mention permission requirements, trade-offs, or relevant site settings only when they affect the requested action.+ - Prefer a compact answer: a direct recommendation, two to five actionable steps, and at most one short note for an important caveat.+ - You are able to find information about site settings, request context for a specific setting, and look up the current value of a site setting.+ - Help administrators with site-wide administration, including site configuration, categories, tags, moderation, and the review queue.+ - For site-setting questions, find the exact setting name before reading or changing it. Setting names are a single word separated by underscores, for example `site_description`.+ - Only change site settings, categories, tags, reviewable content, topics, posts, or users when an administrator explicitly asks you to do so. Before requesting a change, clearly state the affected item, the proposed action, its expected effect, and the reason for the change.+ - Every change requires human approval. Never imply that a pending change has been applied.+ - Be a helpful teacher and explain the trade-offs of each setting.++ The date now is: {date}, much has changed since you were trained.+ PROMPT+ end+ end+ end+enddiff --git a/plugins/discourse-ai/spec/lib/agents/discourse_admin_assistant_spec.rb b/plugins/discourse-ai/spec/lib/agents/discourse_admin_assistant_spec.rbnew file mode 100644index 00000000000..91a4d04f7e5--- /dev/null+++ b/plugins/discourse-ai/spec/lib/agents/discourse_admin_assistant_spec.rb@@ -0,0 +1,62 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Agents::DiscourseAdminAssistant do+ subject(:assistant) { described_class.new }++ fab!(:admin)+ fab!(:regular_user, :user)++ before { enable_current_plugin }++ it "combines Discourse knowledge, general administration, and site-setting tools" do+ expect(assistant.tools).to eq(+ [+ DiscourseAi::Agents::Tools::DiscourseMetaSearch,+ DiscourseAi::Agents::Tools::ListCategories,+ DiscourseAi::Agents::Tools::ListTags,+ DiscourseAi::Agents::Tools::SettingContext,+ DiscourseAi::Agents::Tools::SearchSettings,+ DiscourseAi::Agents::Tools::ReadSiteSetting,+ DiscourseAi::Agents::Tools::ChangeSiteSetting,+ DiscourseAi::Agents::Tools::ListReviewables,+ DiscourseAi::Agents::Tools::CloseTopic,+ DiscourseAi::Agents::Tools::LockPost,+ DiscourseAi::Agents::Tools::UnlistTopic,+ DiscourseAi::Agents::Tools::DeleteTopic,+ DiscourseAi::Agents::Tools::EditPost,+ DiscourseAi::Agents::Tools::EditCategory,+ DiscourseAi::Agents::Tools::EditTags,+ DiscourseAi::Agents::Tools::MovePosts,+ DiscourseAi::Agents::Tools::SuspendUser,+ DiscourseAi::Agents::Tools::SilenceUser,+ DiscourseAi::Agents::Tools::MarkAsSolved,+ ],+ )+ end++ it "requires an administrator request and approval before changing settings" do+ expect(assistant.system_prompt).to include(+ "Only change site settings, categories, tags, reviewable content, topics, posts, or users when an administrator explicitly asks you to do so.",+ "Every change requires human approval.",+ )+ end++ it "is registered as a system agent with a deterministic id" do+ expect(DiscourseAi::Agents::Agent.system_agents[described_class]).to eq(-39)+ end++ it "is only available to administrators" do+ load Rails.root.join("plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rb") # rubocop:disable Discourse/Plugins/UseRequireRelative++ expect(+ DiscourseAi::Agents::Agent.find_by(user: admin, name: described_class.name),+ ).to be_present+ expect(+ DiscourseAi::Agents::Agent.find_by(user: regular_user, name: described_class.name),+ ).to be_nil+ agent = AiAgent.find(-39)++ expect(agent.require_approval).to eq(true)+ expect(agent.tools.map(&:first)).to eq(assistant.tools.map { it.to_s.split("::").last })+ 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.rbindex 8bcca0f6768..61d6dd6948a 100644--- a/plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rb+++ b/plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rb@@ -132,11 +132,30 @@ RSpec.describe ReviewableAiToolAction doexpect(topic.reload.closed).to eq(true)end- it "rejects inline approval from a different post", :aggregate_failures do- post = Fabricate(:post, topic: topic)- other_post = Fabricate(:post)- tool_action = create_tool_action(post_id: post.id)+ it "executes inline approval from the bot post containing its approval card" do+ source_post = Fabricate(:post, topic: topic)+ tool_action = create_tool_action(post_id: source_post.id)reviewable = create_reviewable(tool_action)+ approval_post =+ Fabricate(+ :post,+ topic: topic,+ user: bot_user,+ raw: "<div data-ai-tool-approval-reviewable-id='#{reviewable.id}'></div>",+ )++ result = reviewable.perform(admin, :approve, post_id: approval_post.id)++ expect(result.success?).to eq(true)+ expect(result.transition_to).to eq(:approved)+ expect(topic.reload.closed).to eq(true)+ end++ it "rejects inline approval from a bot post without its approval card", :aggregate_failures do+ source_post = Fabricate(:post, topic: topic)+ tool_action = create_tool_action(post_id: source_post.id)+ reviewable = create_reviewable(tool_action)+ other_post = Fabricate(:post, topic: topic, user: bot_user)expect { reviewable.perform(admin, :approve, post_id: other_post.id) }.to raise_error(Discourse::InvalidAccess,@@ -146,6 +165,25 @@ RSpec.describe ReviewableAiToolAction doexpect(topic.reload.closed).to eq(false)end+ it "rejects inline approval when another user copies its approval card", :aggregate_failures do+ source_post = Fabricate(:post, topic: topic)+ tool_action = create_tool_action(post_id: source_post.id)+ reviewable = create_reviewable(tool_action)+ copied_card_post =+ Fabricate(+ :post,+ topic: topic,+ raw: "<div data-ai-tool-approval-reviewable-id='#{reviewable.id}'></div>",+ )++ expect { reviewable.perform(admin, :approve, post_id: copied_card_post.id) }.to raise_error(+ Discourse::InvalidAccess,+ )++ expect(reviewable.reload).to be_pending+ expect(topic.reload.closed).to eq(false)+ end+it "raises error when target is missing" dotool_action = create_tool_actionreviewable = create_reviewable(tool_action)@@ -304,17 +342,44 @@ RSpec.describe ReviewableAiToolAction doexpect(topic.reload.closed).to eq(false)end- it "rejects inline rejection from a different post" do- post = Fabricate(:post, topic: topic)- other_post = Fabricate(:post)- tool_action = create_tool_action(post_id: post.id)+ it "accepts inline rejection from the bot post containing its approval card" do+ source_post = Fabricate(:post, topic: topic)+ tool_action = create_tool_action(post_id: source_post.id)reviewable = create_reviewable(tool_action)+ approval_post =+ Fabricate(+ :post,+ topic: topic,+ user: bot_user,+ raw: "<div data-ai-tool-approval-reviewable-id='#{reviewable.id}'></div>",+ )- expect { reviewable.perform(admin, :reject, post_id: other_post.id) }.to raise_error(- Discourse::InvalidAccess,- )+ result = reviewable.perform(admin, :reject, post_id: approval_post.id)- expect(reviewable.reload).to be_pending+ expect(result.success?).to eq(true)+ expect(result.transition_to).to eq(:rejected)+ expect(topic.reload.closed).to eq(false)+ end++ it "resolves multiple inline reviews independently from the same bot post" do+ source_post = Fabricate(:post, topic: topic)+ first_reviewable = create_reviewable(create_tool_action(post_id: source_post.id))+ second_reviewable = create_reviewable(create_tool_action(post_id: source_post.id))+ approval_post = Fabricate(:post, topic: topic, user: bot_user, raw: <<~RAW)+ <div data-ai-tool-approval-reviewable-id='#{first_reviewable.id}'></div>+ <div data-ai-tool-approval-reviewable-id='#{second_reviewable.id}'></div>+ RAW++ first_result = first_reviewable.perform(admin, :reject, post_id: approval_post.id)++ expect(first_result.success?).to eq(true)+ expect(first_reviewable.reload).to be_rejected+ expect(second_reviewable.reload).to be_pending++ second_result = second_reviewable.perform(admin, :reject, post_id: approval_post.id)++ expect(second_result.success?).to eq(true)+ expect(second_reviewable.reload).to be_rejectedendit "raises error when performed_by is a bot account" dodiff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-tool-approval-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-tool-approval-test.gjsindex 82f0aba3f0a..17f09c3a52c 100644--- a/plugins/discourse-ai/test/javascripts/integration/components/ai-tool-approval-test.gjs+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-tool-approval-test.gjs@@ -21,7 +21,6 @@ module("Integration | Component | AiToolApproval", function (hooks) {id: 42,version: 0,status: 0,- post_id: 456,tool_name: "suspend_user",tool_parameters: { username: "baduser", duration_days: 7, reason: "Spam" },payload: { agent_name: "Snorlax" },@@ -34,14 +33,16 @@ module("Integration | Component | AiToolApproval", function (hooks) {pretender.put("/review/42/perform/approve", (request) => {assert.strictEqual(request.requestBody,- "post_id=456&version=0",- "sends the reviewable target post id with the inline action"+ "post_id=123&version=0",+ "sends the rendered post id with the inline action");return response({ reviewable_perform_result: { success: true } });});- await render(<template><AiToolApproval @reviewableId="42" /></template>);+ await render(+ <template><AiToolApproval @postId="123" @reviewableId="42" /></template>+ );assert.dom(".ai-tool-approval__value").exists("shows the tool's details");assert@@ -70,7 +71,9 @@ module("Integration | Component | AiToolApproval", function (hooks) {const approved = { ...reviewable, status: 1 };pretender.get("/review/42", () => response({ reviewable: approved }));- await render(<template><AiToolApproval @reviewableId="42" /></template>);+ await render(+ <template><AiToolApproval @postId="123" @reviewableId="42" /></template>+ );assert.dom(".ai-tool-approval__toggle")@@ -91,7 +94,9 @@ module("Integration | Component | AiToolApproval", function (hooks) {pretender.get("/review/42", () => response({ reviewable }));- await render(<template><AiToolApproval @reviewableId="42" /></template>);+ await render(+ <template><AiToolApproval @postId="123" @reviewableId="42" /></template>+ );assert.dom(".ai-tool-approval__actions")@@ -106,7 +111,9 @@ module("Integration | Component | AiToolApproval", function (hooks) {pretender.get("/review/42", () => response(403, {}));- await render(<template><AiToolApproval @reviewableId="42" /></template>);+ await render(+ <template><AiToolApproval @postId="123" @reviewableId="42" /></template>+ );assert.dom(".ai-tool-approval__status")