A platform for community discussion. Free, open, simple.
UX: Improve interaction with tools in bots (#43650)
Penar Musaraj57096e484640
diff --git a/plugins/discourse-ai/app/jobs/regular/resume_ai_tool_approval.rb b/plugins/discourse-ai/app/jobs/regular/resume_ai_tool_approval.rbnew file mode 100644index 00000000000..25f25c6996c--- /dev/null+++ b/plugins/discourse-ai/app/jobs/regular/resume_ai_tool_approval.rb@@ -0,0 +1,68 @@+# frozen_string_literal: true++module Jobs+ class ResumeAiToolApproval < ::Jobs::Base+ sidekiq_options retry: false++ def execute(args)+ return if !SiteSetting.ai_bot_enabled++ reviewable = ReviewableAiToolAction.find_by(id: args[:reviewable_id])+ return if !reviewable || !(reviewable.approved? || reviewable.rejected?)++ continuation = reviewable.payload["continuation"]+ return if continuation.blank?++ post = Post.includes(:topic).find_by(id: continuation["post_id"])+ return if !post || post.topic_id != reviewable.topic_id++ authorization_user_id =+ post.custom_fields[+ DiscourseAi::AiBot::POST_AI_AGENT_AUTHORIZATION_USER_ID_FIELD+ ].presence || reviewable.target_post&.user_id+ user = User.find_by(id: authorization_user_id)+ return if !user || user.bot? || !user.guardian.can_see?(post)++ tool_action = reviewable.target+ return if !tool_action++ agent = DiscourseAi::Agents::Agent.find_by(user: user, id: tool_action.ai_agent_id)+ bot_user = User.find_by(id: tool_action.bot_user_id)+ return if !agent || !bot_user++ model = LlmModel.find_by(id: reviewable.payload["llm_model_id"])+ bot = DiscourseAi::Agents::Bot.as(bot_user, agent: agent.new, model: model)++ reviewable.with_lock do+ return if reviewable.payload["continuation_started_at"].present?+ reviewable.payload["continuation_started_at"] = Time.current.iso8601+ reviewable.save!+ end++ decision = {+ tool: tool_action.tool_name,+ parameters: tool_action.tool_parameters,+ decision: reviewable.status,+ result: continuation["tool_result"],+ }++ DiscourseAi::AiBot::Playground.new(bot).reply_to(+ post,+ attributed_user: user,+ authorization_user_id: user.id,+ feature_name: "bot",+ auto_set_title: false,+ custom_instructions: <<~TEXT.strip,+ You are continuing after a human resolved a tool approval request.+ The final message contains the decision and the actual tool result as JSON data.+ If approved, the tool has already executed: use its result and do not execute it again.+ If rejected, the tool was not executed: acknowledge the decision and do not retry it or+ perform an equivalent action through another tool. Continue any remaining permitted work,+ or ask the user how to proceed if the rejected action was necessary.+ Subsequent actions still require their normal approvals.+ TEXT+ additional_messages: [{ type: :user, content: decision.to_json }],+ )+ end+ end+enddiff --git a/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rbindex 21aff7eeae5..35d5cfd1886 100644--- a/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb+++ b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb@@ -58,18 +58,30 @@ class ReviewableAiToolAction < Reviewableensure_tool_succeeded!(result)- create_result(:success, :approved)+ resolution_result(:approved, args, tool_result: result)enddef perform_reject(performed_by, args)ensure_inline_post_contains_approval!(args)ensure_performed_by_is_a_real_person!(performed_by)- create_result(:success, :rejected)+ resolution_result(:rejected, args)endprivate+ def resolution_result(status, args, tool_result: nil)+ result = create_result(:success, status)+ inline_post_id = args[:post_id] || args["post_id"]+ return result if inline_post_id.blank?++ payload["continuation"] = { post_id: inline_post_id.to_i, tool_result: tool_result }+ result.after_commit = -> do+ DB.after_commit { Jobs.enqueue(:resume_ai_tool_approval, reviewable_id: id) }+ end+ result+ end+# Rebuilds the tool from the persisted action. Returns [tool, tool_class,# context]; the caller sets context.user for audit attribution as needed.def build_tool!@@ -121,7 +133,7 @@ class ReviewableAiToolAction < ReviewablePost.find_by(id: inline_post_id,topic_id: topic_id,- user_id: target&.bot_user_id,+ user_id: [target&.bot_user_id, target&.ai_agent&.user_id].compact,deleted_at: nil,)approval_marker = "data-ai-tool-approval-reviewable-id='#{id}'"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 8dfa5fe6269..2a02eaf5bfd 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@@ -4,14 +4,39 @@ import { fn } from "@ember/helper";import { action } from "@ember/object";import didInsert from "@ember/render-modifiers/modifiers/did-insert";import { service } from "@ember/service";+import { capitalize } from "@ember/string";import { ajax } from "discourse/lib/ajax";import { popupAjaxError } from "discourse/lib/ajax-error";import DButton from "discourse/ui-kit/d-button";+import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";+import dIcon from "discourse/ui-kit/helpers/d-icon";import { i18n } from "discourse-i18n";const STATUSES = { pending: 0, approved: 1, rejected: 2 };+const ApprovalDetails = <template>+ <div class="ai-tool-approval__summary" ...attributes>+ <span class="ai-tool-approval__label">{{i18n+ "discourse_ai.ai_tool_approval.agent"+ }}</span>+ <span class="ai-tool-approval__value">{{@agentName}}</span>++ {{#if @username}}+ <span class="ai-tool-approval__label">{{i18n+ "discourse_ai.ai_tool_approval.user"+ }}</span>+ <span class="ai-tool-approval__value">@{{@username}}</span>+ {{/if}}++ {{#each @parameters as |param|}}+ <span class="ai-tool-approval__label">{{param.key}}</span>+ <span class="ai-tool-approval__value">{{param.value}}</span>+ {{/each}}+ </div>+</template>;+export default class AiToolApproval extends Component {+ @service a11y;@service currentUser;@tracked reviewable;@@ -19,7 +44,14 @@ export default class AiToolApproval extends Component {@tracked performing = false;@tracked loadError = false;@tracked notAuthorized = false;- @tracked expanded = false;++ get actionLabel() {+ return capitalize((this.reviewable?.tool_name || "").replaceAll("_", " "));+ }++ get isApproved() {+ return this.reviewable?.status === STATUSES.approved;+ }get isStaff() {return this.currentUser?.staff;@@ -33,12 +65,6 @@ export default class AiToolApproval extends Component {return this.reviewable && !this.isPending;}- // the summary is always visible while pending; once resolved it collapses- // behind the status header and reveals on expand- get showDetails() {- return this.isPending || this.expanded;- }-get statusLabel() {if (this.reviewable?.status === STATUSES.approved) {return i18n("discourse_ai.ai_tool_approval.approved");@@ -66,11 +92,6 @@ export default class AiToolApproval extends Component {return this.reviewable?.tool_parameters?.username;}- @action- toggleExpanded() {- this.expanded = !this.expanded;- }-@actionasync loadReviewable() {try {@@ -110,6 +131,7 @@ export default class AiToolApproval extends Component {...this.reviewable,status: actionId === "approve" ? STATUSES.approved : STATUSES.rejected,};+ this.a11y.announce(this.statusLabel, "polite");} catch (error) {popupAjaxError(error);} finally {@@ -118,7 +140,14 @@ export default class AiToolApproval extends Component {}<template>- <div class="ai-tool-approval" {{didInsert this.loadReviewable}}>+ <div+ class={{dConcatClass+ "ai-tool-approval"+ (if this.isResolved "--resolved")+ }}+ ...attributes+ {{didInsert this.loadReviewable}}+ >{{#if this.loading}}<span class="ai-tool-approval__status">{{i18n"discourse_ai.ai_tool_approval.loading"@@ -133,44 +162,39 @@ export default class AiToolApproval extends Component {}}</span>{{else}}{{#if this.isResolved}}- <DButton- class="btn-flat ai-tool-approval__toggle"- @action={{this.toggleExpanded}}- @icon={{if this.expanded "chevron-down" "chevron-right"}}- @translatedLabel={{this.statusLabel}}- />+ <details class="ai-tool-approval__details ai-details">+ <summary class="ai-tool-approval__toggle ai-details__summary">+ {{dIcon "chevron-right" class="ai-details__caret --collapsed"}}+ {{dIcon "chevron-down" class="ai-details__caret --expanded"}}+ <span class="ai-tool-approval__title">{{this.actionLabel}}</span>+ <span+ class={{dConcatClass+ "ai-tool-approval__badge"+ (if this.isApproved "--approved" "--rejected")+ }}+ >+ {{dIcon (if this.isApproved "check" "xmark")}}+ {{this.statusLabel}}+ </span>+ </summary>+ <ApprovalDetails+ @agentName={{this.reviewable.payload.agent_name}}+ @parameters={{this.toolParameters}}+ @username={{this.targetUsername}}+ />+ </details>+ {{else if this.isPending}}+ <div+ class="ai-tool-approval__title --pending"+ >{{this.actionLabel}}</div>{{/if}}- {{#if this.showDetails}}- <div class="ai-tool-approval__summary">- <span class="ai-tool-approval__label">{{i18n- "discourse_ai.ai_tool_approval.agent"- }}</span>- <span- class="ai-tool-approval__value"- >{{this.reviewable.payload.agent_name}}</span>-- <span class="ai-tool-approval__label">{{i18n- "discourse_ai.ai_tool_approval.tool"- }}</span>- <span- class="ai-tool-approval__value"- >{{this.reviewable.tool_name}}</span>-- {{#if this.targetUsername}}- <span class="ai-tool-approval__label">{{i18n- "discourse_ai.ai_tool_approval.user"- }}</span>- <span- class="ai-tool-approval__value"- >@{{this.targetUsername}}</span>- {{/if}}-- {{#each this.toolParameters as |param|}}- <span class="ai-tool-approval__label">{{param.key}}</span>- <span class="ai-tool-approval__value">{{param.value}}</span>- {{/each}}- </div>+ {{#if this.isPending}}+ <ApprovalDetails+ @agentName={{this.reviewable.payload.agent_name}}+ @parameters={{this.toolParameters}}+ @username={{this.targetUsername}}+ />{{/if}}{{#if this.isPending}}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-thinking.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-thinking.jsnew file mode 100644index 00000000000..93c3e197bf6--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-thinking.js@@ -0,0 +1,6 @@+import { apiInitializer } from "discourse/lib/api";+import decorateAiThinking from "../lib/decorate-ai-thinking";++export default apiInitializer((api) => {+ api.decorateCookedElement(decorateAiThinking, { id: "ai-thinking-carets" });+});diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/updaters/post-updater.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/updaters/post-updater.jsindex bf53e71e2e5..7bcf5ad4910 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/updaters/post-updater.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/updaters/post-updater.js@@ -1,6 +1,7 @@import loadMorphlex from "discourse/lib/load-morphlex";import { withPluginApi } from "discourse/lib/plugin-api";import { cook } from "discourse/lib/text";+import decorateAiThinking from "../../decorate-ai-thinking";import { addProgressDecoration } from "../progress-handlers";import StreamUpdater from "./stream-updater";@@ -76,6 +77,8 @@ export default class PostUpdater extends StreamUpdater {this.morphingOptions);+ decorateAiThinking(this.postElement.querySelector(".cooked"));+if (done) {this.post.set("cooked", value);withPluginApi((api) => api.preventCloak(this.postId, false));diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/decorate-ai-thinking.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/decorate-ai-thinking.jsnew file mode 100644index 00000000000..d6de75f5f78--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/decorate-ai-thinking.js@@ -0,0 +1,23 @@+import { renderIcon } from "discourse/lib/icon-library";++export default function decorateAiThinking(element) {+ element+ .querySelectorAll("details.ai-thinking > summary")+ .forEach((summary) => {+ summary.parentElement.classList.add("ai-details");+ summary.classList.add("ai-details__summary");++ if (summary.querySelector(":scope > .ai-details__caret")) {+ return;+ }++ summary.prepend(+ renderIcon("element", "chevron-right", {+ class: "ai-details__caret --collapsed",+ }),+ renderIcon("element", "chevron-down", {+ class: "ai-details__caret --expanded",+ })+ );+ });+}diff --git a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-tool-approval.scss b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-tool-approval.scssindex 60d9778dd6d..5471746281d 100644--- a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-tool-approval.scss+++ b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-tool-approval.scss@@ -3,22 +3,25 @@border-radius: var(--d-border-radius);padding: var(--space-3) var(--space-4);margin: var(--space-2) 0;- background: var(--primary-very-low);+ background: var(--secondary);&__summary {display: grid;- grid-template-columns: max-content 1fr;+ grid-template-columns: fit-content(35%) minmax(0, 1fr);gap: var(--space-1) var(--space-3);margin-bottom: var(--space-3);}&__label {- color: var(--primary-medium);+ color: var(--primary-high);font-size: var(--font-down-1);+ overflow-wrap: anywhere;}&__value {- word-break: break-word;+ min-width: 0;+ overflow-wrap: anywhere;+ white-space: pre-wrap;}&__actions {@@ -28,16 +31,67 @@}&__status {- color: var(--primary-medium);+ color: var(--primary-high);font-style: italic;}- &__toggle.btn-flat {- padding-inline: 0;+ &.--resolved {+ border: 0;+ border-radius: 0;+ padding: 0;+ background: none;- .d-button-label {- font-style: italic;- color: var(--primary-medium);+ .ai-tool-approval__toggle {+ display: flex;+ align-items: center;}}++ &__toggle {+ &:focus-visible {+ outline: 2px solid var(--tertiary);+ outline-offset: var(--space-1);+ }+ }++ &__title {+ flex: 1;+ min-width: 0;+ font-weight: 600;+ overflow-wrap: anywhere;++ &.--pending {+ margin-bottom: var(--space-3);+ }++ .--resolved & {+ font-weight: inherit;+ }+ }++ &__badge {+ display: inline-flex;+ flex-shrink: 0;+ align-items: center;+ gap: var(--space-1);+ padding: var(--space-1) var(--space-2);+ border-radius: var(--d-border-radius);+ font-size: var(--font-down-1);+ margin-inline-start: var(--space-2);++ &.--approved {+ background: color-mix(in srgb, var(--success-low) 50%, var(--secondary));+ color: var(--primary);+ }++ &.--rejected {+ background: color-mix(in srgb, var(--danger-low) 50%, var(--secondary));+ color: var(--primary);+ }+ }++ &__toggle + &__summary {+ margin-top: var(--space-3);+ margin-bottom: 0;+ }}diff --git a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/bot-replies.scss b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/bot-replies.scssindex 8c45c9611fa..9caaaa1dd29 100644--- a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/bot-replies.scss+++ b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/bot-replies.scss@@ -199,3 +199,49 @@ span.onebox-ai-llm-title {padding: 0;border-bottom: none;}++.ai-details {+ background-color: var(--primary-very-low);+ padding: 0.25rem 0.75rem;+ margin-bottom: 0.5rem;++ &:not([open]):is(:hover, :focus-within) {+ background-color: var(--d-hover);+ }++ > .ai-details__summary {+ display: block;+ cursor: pointer;++ .d-icon {+ color: var(--primary-medium);+ }++ &::before {+ content: none;+ }++ &::-webkit-details-marker {+ display: none;+ }+ }++ &__caret {+ font-size: var(--font-down-1);+ margin-inline-end: 0.25em;++ &.--expanded {+ display: none;+ }+ }++ &[open] > .ai-details__summary {+ .ai-details__caret.--collapsed {+ display: none;+ }++ .ai-details__caret.--expanded {+ display: inline-block;+ }+ }+}diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex 4b66e23ad52..00eff27d31c 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -498,7 +498,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."+ tool_pending_approval: "This action requires approval:"spawn_agent:description: "Delegate a self-contained prompt to one of the allowed agents and return its response"prompt_description: "The complete self-contained prompt sent to the selected agent and shown in the thinking block"diff --git a/plugins/discourse-ai/lib/agents/bot.rb b/plugins/discourse-ai/lib/agents/bot.rbindex f2c971e42db..a37634e0960 100644--- a/plugins/discourse-ai/lib/agents/bot.rb+++ b/plugins/discourse-ai/lib/agents/bot.rb@@ -529,12 +529,9 @@ module DiscourseAi# `.ai-tool-approval` element exists only once, on the component itself.approval_card = "<div data-ai-tool-approval-reviewable-id='#{reviewable.id}'></div>"- approval_content =- build_placeholder(- tool.summary,- I18n.t("discourse_ai.ai_bot.tool_pending_approval"),- custom_raw: approval_card,- )+ approval_notice =+ ERB::Util.html_escape(I18n.t("discourse_ai.ai_bot.tool_pending_approval"))+ approval_content = "#{approval_notice}\n\n#{approval_card}\n\n"update_blk.call(approval_content, nil, :custom_raw)enddiff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rbindex 408a6287408..88079d0465f 100644--- a/plugins/discourse-ai/lib/ai_bot/playground.rb+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb@@ -491,6 +491,7 @@ module DiscourseAidef reply_to(post,custom_instructions: nil,+ additional_messages: [],whisper: nil,context_style: nil,add_user_to_pm: true,@@ -540,6 +541,7 @@ module DiscourseAimessages:DiscourseAi::Completions::PromptMessagesBuilder.messages_from_post(post,+ guardian: (attributed_user || post.user).guardian,style: context_style,max_posts: DiscourseAi::Completions::PromptMessagesBuilder::MAX_CONTEXT_MESSAGES,context_token_budget: context_token_budget(context_llm),@@ -551,6 +553,8 @@ module DiscourseAi),)+ context.messages.concat(additional_messages)+reply_user = bot.bot_userif bot.agent.class.respond_to?(:user_id)reply_user = User.find_by(id: bot.agent.class.user_id) || reply_userdiff --git a/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb b/plugins/discourse-ai/lib/completions/prompt_messages_builder.rbindex 2f1b2bea679..dc636e197fd 100644--- a/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb+++ b/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb@@ -119,6 +119,7 @@ module DiscourseAidef self.messages_from_post(post,+ guardian: post.user.guardian,style: nil,max_posts:,context_token_budget: nil,@@ -167,7 +168,6 @@ module DiscourseAibuilder = newbuilder.topic = post.topic- guardian = Guardian.new(post.user)context.reverse_each do |raw, username, custom_prompt, upload_ids, created_at|filtered_upload_ids =diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/jobs/regular/resume_ai_tool_approval_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/jobs/regular/resume_ai_tool_approval_spec.rbnew file mode 100644index 00000000000..4c61429b081--- /dev/null+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/jobs/regular/resume_ai_tool_approval_spec.rb@@ -0,0 +1,176 @@+# frozen_string_literal: true++RSpec.describe Jobs::ResumeAiToolApproval do+ subject(:job) { described_class.new }++ fab!(:admin)+ fab!(:requester) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:llm_model) { Fabricate(:llm_model, name: "gpt-3.5-turbo") }+ fab!(:ai_agent) do+ Fabricate(+ :ai_agent,+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],+ require_approval: true,+ )+ end+ fab!(:topic) { Fabricate(:private_message_topic, user: requester, recipient: admin) }+ fab!(:source_post) { Fabricate(:post, topic: topic, user: requester) }++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ toggle_enabled_bots(bots: [llm_model])+ ai_agent.create_user!+ topic.allowed_users << ai_agent.user+ end++ let(:tool_action) do+ AiToolAction.create!(+ tool_name: "create_category",+ tool_parameters: {+ name: "Bug reports",+ reason: "Collect bug reports",+ },+ ai_agent: ai_agent,+ bot_user_id: llm_model.reload.user_id,+ post_id: source_post.id,+ )+ end++ let(:reviewable) do+ ReviewableAiToolAction.needs_review!(+ target: tool_action,+ created_by: llm_model.reload.user,+ reviewable_by_moderator: true,+ payload: {+ agent_name: ai_agent.name,+ llm_model_id: llm_model.id,+ },+ )+ end++ let(:approval_post) do+ Fabricate(+ :post,+ topic: topic,+ user: ai_agent.user,+ raw: "<div data-ai-tool-approval-reviewable-id='#{reviewable.id}'></div>",+ custom_fields: {+ DiscourseAi::AiBot::POST_AI_AGENT_AUTHORIZATION_USER_ID_FIELD => requester.id,+ },+ )+ end++ describe "#execute" do+ it "continues once with the actual result and the original user's authorization" do+ reviewable.perform(admin, :approve, post_id: approval_post.id)+ category = Category.find_by!(name: "Bug reports")+ prompts = nil++ DiscourseAi::Completions::Llm.with_prepared_responses(+ ["The category is ready."],+ ) do |_, _, captured_prompts|+ job.execute(reviewable_id: reviewable.id)+ job.execute(reviewable_id: reviewable.id)+ prompts = captured_prompts+ end++ expect(prompts.size).to eq(1)+ decision = JSON.parse(prompts.first.messages.last[:content])+ expect(decision).to include(+ "tool" => "create_category",+ "decision" => "approved",+ "result" => include("category_id" => category.id),+ )+ reply = topic.posts.order(:post_number).last+ expect(reply.raw).to eq("The category is ready.")+ expect(reply.user_id).to eq(ai_agent.user_id)+ expect(+ reply.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_AUTHORIZATION_USER_ID_FIELD].to_i,+ ).to eq(requester.id)+ expect(Category.where(name: "Bug reports").count).to eq(1)+ end++ it "continues after rejection without executing the tool" do+ reviewable.perform(admin, :reject, post_id: approval_post.id)+ prompts = nil++ DiscourseAi::Completions::Llm.with_prepared_responses(+ ["I will leave the categories unchanged."],+ ) do |_, _, captured_prompts|+ job.execute(reviewable_id: reviewable.id)+ prompts = captured_prompts+ end++ decision = JSON.parse(prompts.first.messages.last[:content])+ expect(decision).to include("decision" => "rejected", "result" => nil)+ expect(prompts.first.messages.first[:content]).to include("do not retry it")+ expect(topic.posts.order(:post_number).last.raw).to eq(+ "I will leave the categories unchanged.",+ )+ expect(Category.exists?(name: "Bug reports")).to eq(false)+ end++ it "requires another approval for a subsequent action and resumes again" do+ requester.update!(admin: true)+ ai_agent.update!(tools: ["CreateCategory"])+ reviewable.perform(admin, :approve, post_id: approval_post.id)+ next_tool_call =+ DiscourseAi::Completions::ToolCall.new(+ name: "create_category",+ id: "create-next-category",+ parameters: {+ name: "Feature requests",+ reason: "Collect feature requests",+ },+ )++ DiscourseAi::Completions::Llm.with_prepared_responses(+ [next_tool_call, "The next category is awaiting approval."],+ ) { job.execute(reviewable_id: reviewable.id) }++ next_reviewable = ReviewableAiToolAction.order(:id).last+ next_approval_post = topic.posts.order(:post_number).last+ expect(next_reviewable).to be_pending+ expect(next_approval_post.raw).to include(+ "data-ai-tool-approval-reviewable-id='#{next_reviewable.id}'",+ )+ expect(Category.exists?(name: "Feature requests")).to eq(false)++ next_reviewable.perform(admin, :approve, post_id: next_approval_post.id)+ DiscourseAi::Completions::Llm.with_prepared_responses(["Both categories are ready."]) do+ job.execute(reviewable_id: next_reviewable.id)+ end++ expect(Category.exists?(name: "Feature requests")).to eq(true)+ reply = topic.posts.order(:post_number).last+ expect(reply.raw).to eq("Both categories are ready.")+ expect(+ reply.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_AUTHORIZATION_USER_ID_FIELD].to_i,+ ).to eq(requester.id)+ end++ it "skips pending approvals" do+ approval_post++ expect { job.execute(reviewable_id: reviewable.id) }.not_to change { topic.posts.count }+ expect(reviewable.reload.payload["continuation_started_at"]).to be_nil+ end++ it "skips continuation when the requester lost access to the conversation" do+ reviewable.perform(admin, :reject, post_id: approval_post.id)+ topic.topic_allowed_users.where(user_id: requester.id).delete_all++ expect { job.execute(reviewable_id: reviewable.id) }.not_to change { topic.posts.count }+ expect(reviewable.reload.payload["continuation_started_at"]).to be_nil+ end++ it "skips continuation when the agent is no longer available to the requester" do+ reviewable.perform(admin, :reject, post_id: approval_post.id)+ ai_agent.update!(allowed_group_ids: [Group::AUTO_GROUPS[:admins]])++ expect { job.execute(reviewable_id: reviewable.id) }.not_to change { topic.posts.count }+ expect(reviewable.reload.payload["continuation_started_at"]).to be_nil+ end+ 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 61d6dd6948a..ed6d356e93c 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@@ -144,13 +144,82 @@ RSpec.describe ReviewableAiToolAction doraw: "<div data-ai-tool-approval-reviewable-id='#{reviewable.id}'></div>",)- result = reviewable.perform(admin, :approve, post_id: approval_post.id)+ result = nil+ expect { result = reviewable.perform(admin, :approve, post_id: approval_post.id) }.to change {+ Jobs::ResumeAiToolApproval.jobs.size+ }.by(1)+ expect(reviewable.reload.payload["continuation"]).to include("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 "creates a category from an approval card authored by the agent's user" do+ agent_user = ai_agent.create_user!+ source_post = Fabricate(:post, topic: topic)+ tool_action =+ create_tool_action(+ tool_name: "create_category",+ params: {+ name: "Bug reports",+ reason: "Collect bug reports",+ },+ post_id: source_post.id,+ )+ reviewable = create_reviewable(tool_action)+ approval_post =+ Fabricate(+ :post,+ topic: topic,+ user: agent_user,+ raw: "<div data-ai-tool-approval-reviewable-id='#{reviewable.id}'></div>",+ )++ reviewable.perform(admin, :approve, post_id: approval_post.id)++ expect(reviewable.reload).to be_approved+ expect(Category.find_by!(name: "Bug reports").user_id).to eq(admin.id)+ end++ it "rejects an approval card copied by an unrelated agent" do+ ai_agent.create_user!+ other_agent_user = Fabricate(:ai_agent).create_user!+ source_post = Fabricate(:post, topic: topic)+ reviewable = create_reviewable(create_tool_action(post_id: source_post.id))+ copied_post =+ Fabricate(+ :post,+ topic: topic,+ user: other_agent_user,+ raw: "<div data-ai-tool-approval-reviewable-id='#{reviewable.id}'></div>",+ )++ expect { reviewable.perform(admin, :approve, post_id: copied_post.id) }.to raise_error(+ Discourse::InvalidAccess,+ )+ expect(reviewable.reload).to be_pending+ expect(topic.reload.closed).to eq(false)+ end++ it "rejects an agent's approval card copied to another topic" do+ agent_user = ai_agent.create_user!+ source_post = Fabricate(:post, topic: topic)+ reviewable = create_reviewable(create_tool_action(post_id: source_post.id))+ copied_post =+ Fabricate(+ :post,+ user: agent_user,+ raw: "<div data-ai-tool-approval-reviewable-id='#{reviewable.id}'></div>",+ )++ expect { reviewable.perform(admin, :approve, post_id: copied_post.id) }.to raise_error(+ Discourse::InvalidAccess,+ )+ expect(reviewable.reload).to be_pending+ expect(topic.reload.closed).to eq(false)+ end+it "rejects inline approval from a bot post without its approval card", :aggregate_failures dosource_post = Fabricate(:post, topic: topic)tool_action = create_tool_action(post_id: source_post.id)@@ -201,11 +270,32 @@ RSpec.describe ReviewableAiToolAction doendit "raises and stays pending when the tool returns an error result (e.g. stale target)" do- tool_action = create_tool_action(params: { topic_id: -999, closed: true, reason: "test" })+ source_post = Fabricate(:post, topic: topic)+ tool_action =+ create_tool_action(+ params: {+ topic_id: -999,+ closed: true,+ reason: "test",+ },+ 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, :approve) }.to raise_error(Discourse::InvalidAccess)+ expect {+ expect { reviewable.perform(admin, :approve, post_id: approval_post.id) }.to raise_error(+ Discourse::InvalidAccess,+ )+ }.not_to change { Jobs::ResumeAiToolApproval.jobs.size }expect(reviewable.reload).to be_pending+ expect(reviewable.payload["continuation"]).to be_nilendit "raises and stays pending when the approver lacks permission at replay time" do@@ -361,6 +451,26 @@ RSpec.describe ReviewableAiToolAction doexpect(topic.reload.closed).to eq(false)end+ it "accepts inline rejection from the agent's user" do+ agent_user = ai_agent.create_user!+ source_post = Fabricate(:post, topic: topic)+ reviewable = create_reviewable(create_tool_action(post_id: source_post.id))+ approval_post =+ Fabricate(+ :post,+ topic: topic,+ user: agent_user,+ raw: "<div data-ai-tool-approval-reviewable-id='#{reviewable.id}'></div>",+ )++ expect { reviewable.perform(admin, :reject, post_id: approval_post.id) }.to change {+ Jobs::ResumeAiToolApproval.jobs.size+ }.by(1)++ expect(reviewable.reload).to be_rejected+ expect(topic.reload.closed).to eq(false)+ end+it "resolves multiple inline reviews independently from the same bot post" dosource_post = Fabricate(:post, topic: topic)first_reviewable = create_reviewable(create_tool_action(post_id: source_post.id))diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-thinking-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-thinking-test.gjsnew file mode 100644index 00000000000..7030b4a9716--- /dev/null+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-thinking-test.gjs@@ -0,0 +1,63 @@+import { click, find, render } from "@ember/test-helpers";+import { module, test } from "qunit";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";+import decorateAiThinking from "discourse/plugins/discourse-ai/discourse/lib/decorate-ai-thinking";++module("Integration | Component | AiThinking", function (hooks) {+ setupRenderingTest(hooks);++ test("decorates thinking with accessible SVG carets without duplicating them", async function (assert) {+ await render(+ <template>+ <div class="thinking-test">+ <details class="ai-thinking">+ <summary>Thinking</summary>+ <p>Considering the request.</p>+ </details>+ <details class="other-details"><summary>Other details</summary></details>+ </div>+ </template>+ );++ decorateAiThinking(find(".thinking-test"));+ decorateAiThinking(find(".thinking-test"));++ assert+ .dom(".ai-thinking summary")+ .hasText("Thinking", "preserves the summary's accessible name");+ assert+ .dom(".ai-thinking .ai-details__caret")+ .exists(+ { count: 2 },+ "adds one pair of icons across repeated decorations"+ );+ assert+ .dom(".ai-thinking .d-icon-chevron-right")+ .hasAttribute(+ "aria-hidden",+ "true",+ "the decorative icon is hidden from assistive technology"+ )+ .isVisible("shows the collapsed caret");+ assert+ .dom(".ai-thinking .d-icon-chevron-down")+ .isNotVisible("hides the expanded caret while closed");+ assert+ .dom(".other-details .ai-details__caret")+ .doesNotExist("leaves unrelated disclosures alone");++ await click(".ai-thinking summary");++ assert.strictEqual(+ getComputedStyle(find(".ai-thinking summary"), "::before").content,+ "none",+ "the open state does not restore the text triangle"+ );+ assert+ .dom(".ai-thinking .d-icon-chevron-right")+ .isNotVisible("hides the collapsed caret when open");+ assert+ .dom(".ai-thinking .d-icon-chevron-down")+ .isVisible("shows the expanded caret");+ });+});diff --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 17f09c3a52c..c469c9ecb04 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@@ -53,10 +53,13 @@ module("Integration | Component | AiToolApproval", function (hooks) {assert.dom(".ai-tool-approval__toggle")- .hasText("Approved", "collapses to a status toggle after approving");+ .hasText(+ "Suspend user Approved",+ "keeps the action visible after approving"+ );assert- .dom(".ai-tool-approval__summary")- .doesNotExist("hides the details once resolved");+ .dom(".ai-tool-approval__details")+ .doesNotHaveAttribute("open", "details are collapsed");await click(".ai-tool-approval__toggle");@@ -65,7 +68,7 @@ module("Integration | Component | AiToolApproval", function (hooks) {.includesText("Spam", "expands to reveal the approved action's reason");});- test("an approved action collapses to an expandable status with no revert action", async function (assert) {+ test("an approved action keeps its name visible with expandable details", async function (assert) {updateCurrentUser({ moderator: true, admin: false });const approved = { ...reviewable, status: 1 };@@ -77,7 +80,7 @@ module("Integration | Component | AiToolApproval", function (hooks) {assert.dom(".ai-tool-approval__toggle")- .hasText("Approved", "shows the collapsed approved state");+ .hasText("Suspend user Approved", "shows the action and approval status");await click(".ai-tool-approval__toggle");@@ -87,8 +90,98 @@ module("Integration | Component | AiToolApproval", function (hooks) {assert.dom(".ai-tool-approval__actions").doesNotExist("offers no revert action");+ assert+ .dom(".ai-tool-approval__details")+ .hasAttribute("open", "", "exposes the expanded state");++ await click(".ai-tool-approval__toggle");++ assert+ .dom(".ai-tool-approval__details")+ .doesNotHaveAttribute("open", "details can be collapsed again");});+ test("rejecting an action shows its name and rejected status", async function (assert) {+ updateCurrentUser({ moderator: true, admin: false });+ pretender.get("/review/42", () => response({ reviewable }));+ pretender.put("/review/42/perform/reject", () =>+ response({ reviewable_perform_result: { success: true } })+ );++ await render(+ <template><AiToolApproval @postId="123" @reviewableId="42" /></template>+ );+ await click(".ai-tool-approval__actions .btn-danger");++ assert+ .dom(".ai-tool-approval__toggle")+ .hasText("Suspend user Rejected", "identifies the rejected action");+ assert+ .dom(".ai-tool-approval__badge .d-icon-xmark")+ .exists("distinguishes rejection with an icon as well as text");+ assert+ .dom(".ai-tool-approval__actions")+ .doesNotExist("removes the approval controls");+ });++ for (const { toolName, title, parameters, detail } of [+ {+ toolName: "create_category",+ title: "Create category",+ parameters: { name: "Bugs", description: "Report bugs and issues here." },+ detail: "Report bugs and issues here.",+ },+ {+ toolName: "set_site_setting",+ title: "Set site setting",+ parameters: { setting_name: "title", value: "Our community" },+ detail: "Our community",+ },+ {+ toolName: "silence_user",+ title: "Silence user",+ parameters: { username: "noisyuser", reason: "Repeated spam" },+ detail: "@noisyuser",+ },+ {+ toolName: "custom_action",+ title: "Custom action",+ parameters: { enabled: false, count: 0, options: { groups: [1, 2] } },+ detail: '{"groups":[1,2]}',+ },+ ]) {+ test(`${toolName} uses the generic approval receipt`, async function (assert) {+ pretender.get("/review/42", () =>+ response({+ reviewable: {+ ...reviewable,+ status: 1,+ tool_name: toolName,+ tool_parameters: parameters,+ },+ })+ );++ await render(+ <template><AiToolApproval @postId="123" @reviewableId="42" /></template>+ );++ assert+ .dom(".ai-tool-approval__title")+ .hasText(title, "identifies the action without expanding");+ assert+ .dom(".ai-tool-approval__details")+ .doesNotHaveAttribute("open", "parameters are collapsed initially");++ await click(".ai-tool-approval__toggle");++ assert+ .dom(".ai-tool-approval__summary")+ .includesText(detail, "preserves the action's parameters")+ .includesText("Snorlax", "includes the agent for context");+ });+ }+test("non-staff sees a pending message without action buttons", async function (assert) {updateCurrentUser({ moderator: false, admin: false });