A platform for community discussion. Free, open, simple.
FIX: Replay approved AI tool actions with the options they were queued with
Régis Hanol4e814e3024cb
diff --git a/db/structure.sql b/db/structure.sqlindex 619060e911a..ba46ba6018f 100644--- a/db/structure.sql+++ b/db/structure.sql@@ -1120,7 +1120,8 @@ CREATE TABLE public.ai_tool_actions (bot_user_id integer NOT NULL,post_id integer,created_at timestamp(6) without time zone NOT NULL,- updated_at timestamp(6) without time zone NOT NULL+ updated_at timestamp(6) without time zone NOT NULL,+ tool_options jsonb DEFAULT '{}'::jsonb NOT NULL);@@ -26089,6 +26090,7 @@ ALTER TABLE ONLY public.ad_plugin_house_ads_groupsSET search_path TO "$user", public;INSERT INTO "schema_migrations" (version) VALUES+('20261019105124'),('20261018172000'),('20261015175355'),('20261015175340'),diff --git a/plugins/discourse-ai/app/models/ai_tool_action.rb b/plugins/discourse-ai/app/models/ai_tool_action.rbindex 5df6c8aedcc..15afc0175be 100644--- a/plugins/discourse-ai/app/models/ai_tool_action.rb+++ b/plugins/discourse-ai/app/models/ai_tool_action.rb@@ -12,6 +12,7 @@ end## id :bigint not null, primary key# tool_name :string not null+# tool_options :jsonb not null# tool_parameters :jsonb not null# created_at :datetime not null# updated_at :datetime not nulldiff --git a/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rbindex 5f2c616f76e..b5e948eab4c 100644--- a/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb+++ b/plugins/discourse-ai/app/models/reviewable_ai_tool_action.rb@@ -245,9 +245,10 @@ class ReviewableAiToolAction < Reviewabletool =tool_class.new(tool_action.tool_parameters.symbolize_keys,- bot_user: bot_user,+ agent_options: tool_action.tool_options,+ bot_user:,llm: nil,- context: context,+ context:,)[tool, tool_class, context]diff --git a/plugins/discourse-ai/db/migrate/20261019105124_add_tool_options_to_ai_tool_actions.rb b/plugins/discourse-ai/db/migrate/20261019105124_add_tool_options_to_ai_tool_actions.rbnew file mode 100644index 00000000000..94598f16337--- /dev/null+++ b/plugins/discourse-ai/db/migrate/20261019105124_add_tool_options_to_ai_tool_actions.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++class AddToolOptionsToAiToolActions < ActiveRecord::Migration[8.0]+ def change+ add_column :ai_tool_actions, :tool_options, :jsonb, default: {}, null: false+ end+enddiff --git a/plugins/discourse-ai/lib/agents/bot.rb b/plugins/discourse-ai/lib/agents/bot.rbindex 40a116396a5..b58e83794a0 100644--- a/plugins/discourse-ai/lib/agents/bot.rb+++ b/plugins/discourse-ai/lib/agents/bot.rb@@ -599,6 +599,7 @@ module DiscourseAiAiToolAction.create!(tool_name: tool.name,tool_parameters: tool.parameters,+ tool_options: tool.agent_options.to_h,ai_agent_id: @agent.id,bot_user_id: @bot_user.id,post_id: context.post_id,diff --git a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb b/plugins/discourse-ai/spec/lib/agents/bot_spec.rbindex bb1ea5542ed..d44bdc8c6f1 100644--- a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb+++ b/plugins/discourse-ai/spec/lib/agents/bot_spec.rb@@ -951,6 +951,9 @@ RSpec.describe DiscourseAi::Agents::Bot dotool =DiscourseAi::Agents::Tools::CloseTopic.new({ topic_id: topic.id, closed: true, reason: "Off-topic" },+ agent_options: {+ "max_closes" => "1",+ },bot_user: test_bot_user,llm: bot.llm,)@@ -961,7 +964,12 @@ RSpec.describe DiscourseAi::Agents::Bot doexpect(result[:status]).to eq("pending_approval")expect(topic.reload.closed).to eq(false)- expect(AiToolAction.last.tool_name).to eq("close_topic")+ expect(AiToolAction.last).to have_attributes(+ tool_name: "close_topic",+ tool_options: {+ "max_closes" => "1",+ },+ )expect(ReviewableAiToolAction.count).to eq(1)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 d596b2bccd5..4f745568cee 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@@ -1,5 +1,27 @@# frozen_string_literal: true+module ExternalReviewablePlugin+ class ExternalEcho < DiscourseAi::Agents::Tools::Tool+ def self.signature+ { name: name, description: "Echoes an option", parameters: [] }+ end++ def self.name+ "ExternalEcho"+ end++ def self.accepted_options+ [option(:greeting, type: :string)]+ end++ def invoke+ return error_response("no greeting") if options[:greeting] != "hello"++ { status: "success" }+ end+ end+end+RSpec.describe ReviewableAiToolAction dofab!(:admin)fab!(:ai_agent)@@ -13,11 +35,12 @@ RSpec.describe ReviewableAiToolAction doai_agent.reloadend- def create_tool_action(tool_name: "close_topic", params: nil, post_id: nil)+ def create_tool_action(tool_name: "close_topic", params: nil, post_id: nil, tool_options: {})params ||= { topic_id: topic.id, closed: true, reason: "Off-topic" }AiToolAction.create!(tool_name: tool_name,tool_parameters: params,+ tool_options: tool_options,ai_agent: ai_agent,bot_user_id: bot_user.id,post_id: post_id,@@ -122,22 +145,27 @@ RSpec.describe ReviewableAiToolAction doenddescribe "#perform_approve" do- it "rebuilds a tool supplied by a plugin for approval" do- tool_class =- Class.new(DiscourseAi::Agents::Tools::CloseTopic) do- def self.name- "extension_close_topic"- end- end- plugin = Plugin::Instance.new- modifier = ->(tools) { tools + [tool_class] }- DiscoursePluginRegistry.register_modifier(plugin, :ai_agent_available_tools, &modifier)- reviewable = create_reviewable(create_tool_action(tool_name: "extension_close_topic"))+ it "rebuilds an externally registered tool with the options queued with the action" do+ DiscourseAi.register_tools(+ [ExternalReviewablePlugin::ExternalEcho],+ plugin: Plugin::Instance.new,+ )+ tool_action =+ create_tool_action(+ tool_name: "ExternalEcho",+ params: {+ },+ tool_options: {+ greeting: "hello",+ },+ )+ reviewable = create_reviewable(tool_action)expect(reviewable.perform(admin, :approve).success?).to eq(true)- expect(topic.reload.closed).to eq(true)ensure- DiscoursePluginRegistry.unregister_modifier(plugin, :ai_agent_available_tools, &modifier)+ DiscoursePluginRegistry._raw_external_ai_tools.reject! do |entry|+ entry[:value] == ExternalReviewablePlugin::ExternalEcho+ endendit "executes the tool and transitions to approved" do