Skip to content

A platform for community discussion. Free, open, simple.

DEV: Let plugins register standalone AI tools

Régis Hanolc957f1090aa8
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.rb
index 8abe869f2eb..111074e201d 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
@@ -17,10 +17,9 @@ module DiscourseAi
.map { |agent| LocalizedAiAgentSerializer.new(agent, root: false) }
tools =
- (
- DiscourseAi::Agents::Agent.all_available_tools +
- DiscourseAi::Agents::Agent.external_tools
- ).map { |tool| AiToolSerializer.new(tool, root: false) }
+ DiscourseAi::Agents::Agent.all_available_tools.map do |tool|
+ AiToolSerializer.new(tool, root: false)
+ end
AiTool
.where(enabled: true)
diff --git a/plugins/discourse-ai/app/models/ai_agent.rb b/plugins/discourse-ai/app/models/ai_agent.rb
index 92c777f7c85..73c9c608869 100644
--- a/plugins/discourse-ai/app/models/ai_agent.rb
+++ b/plugins/discourse-ai/app/models/ai_agent.rb
@@ -363,11 +363,8 @@ class AiAgent < ActiveRecord::Base
klass = DiscourseAi::Agents::Tools::Custom.class_instance(custom_tool_id)
end
else
- inner_name = inner_name.gsub("Tool", "")
- inner_name = "List#{inner_name}" if %w[Categories Tags Users].include?(inner_name)
-
klass =
- "DiscourseAi::Agents::Tools::#{inner_name}".safe_constantize ||
+ DiscourseAi::Agents::Agent.builtin_tool_by_name(inner_name) ||
DiscourseAi::Agents::Agent.external_tool_by_name(inner_name)
options[klass] = current_options if klass && current_options
end
@@ -631,13 +628,9 @@ class AiAgent < ActiveRecord::Base
if tool_name.to_s.start_with?("custom-")
AiTool.find_by(id: tool_name.to_s.split("-", 2).last.to_i)&.function_call_name
else
- normalized_name = tool_name.to_s.gsub("Tool", "")
- normalized_name = "List#{normalized_name}" if %w[Categories Tags Users].include?(
- normalized_name,
- )
tool_class =
- "DiscourseAi::Agents::Tools::#{normalized_name}".safe_constantize ||
- DiscourseAi::Agents::Agent.external_tool_by_name(normalized_name)
+ DiscourseAi::Agents::Agent.builtin_tool_by_name(tool_name) ||
+ DiscourseAi::Agents::Agent.external_tool_by_name(tool_name.to_s)
tool_class&.signature&.[](:name)
end
end
diff --git a/plugins/discourse-ai/lib/agents/agent.rb b/plugins/discourse-ai/lib/agents/agent.rb
index 713783942de..e75e195f297 100644
--- a/plugins/discourse-ai/lib/agents/agent.rb
+++ b/plugins/discourse-ai/lib/agents/agent.rb
@@ -63,6 +63,14 @@ module DiscourseAi
@system_agents_by_id ||= system_agents.invert
end
+ def builtin_tool_by_name(name)
+ normalized_name = name.to_s.gsub("Tool", "")
+ normalized_name = "List#{normalized_name}" if %w[Categories Tags Users].include?(
+ normalized_name,
+ )
+ "DiscourseAi::Agents::Tools::#{normalized_name}".safe_constantize
+ end
+
def external_tool_by_name(name)
sync_external_registry!
@external_tools_by_name[name]
@@ -75,13 +83,13 @@ module DiscourseAi
def all(user:)
# listing tools has to be dynamic cause site settings may change
+ available_tools = all_available_tools
AiAgent.all_agents.filter do |agent|
next false if !user.in_any_groups?(agent.allowed_group_ids)
if agent.system
instance = agent.new
- instance.required_tools == [] ||
- (instance.required_tools - all_available_tools).empty?
+ instance.required_tools == [] || (instance.required_tools - available_tools).empty?
else
true
end
@@ -173,6 +181,7 @@ module DiscourseAi
tools << Tools::Assign if defined?(::Assigner)
tools << Tools::MarkAsSolved if defined?(::DiscourseSolved)
+ tools.concat(external_tools)
DiscoursePluginRegistry.apply_modifier(:ai_agent_available_tools, tools).uniq
end
@@ -185,14 +194,15 @@ module DiscourseAi
def sync_external_registry!
configs = external_feature_configs
- signature = configs.hash
+ standalone_tools = DiscoursePluginRegistry.external_ai_tools
+ signature = [configs, standalone_tools].hash
if @external_registry_signature == signature && @external_tools_by_name && @system_agents
return
end
- @external_registry_signature = signature
external_agents = {}
external_tools_by_name = {}
+ agent_tools = []
configs.each do |config|
agent_klass = config[:agent_klass]
@@ -201,18 +211,32 @@ module DiscourseAi
next if builtin_system_agents.key?(agent_klass)
external_agents[agent_klass] = config[:agent_id]
+ agent_tools.concat(agent_klass.new.tools)
+ end
- agent_klass.new.tools.each do |tool_klass|
- tool_name = tool_klass.to_s.split("::").last
- next if "DiscourseAi::Agents::Tools::#{tool_name}".safe_constantize
- external_tools_by_name[tool_name] ||= tool_klass
+ (agent_tools + standalone_tools).each do |tool_klass|
+ tool_name = tool_klass.to_s.demodulize
+ builtin = builtin_tool_by_name(tool_name)
+ next if builtin == tool_klass
+ if builtin
+ raise ArgumentError,
+ "AI tool '#{tool_name}' from #{tool_klass} clashes with built-in #{builtin}"
end
+
+ existing = external_tools_by_name[tool_name]
+ if existing && existing.to_s != tool_klass.to_s
+ raise ArgumentError,
+ "AI tool '#{tool_name}' from #{tool_klass} is already registered by #{existing}"
+ end
+
+ external_tools_by_name[tool_name] = tool_klass
end
new_system_agents = builtin_system_agents.merge(external_agents)
@system_agents_by_id = nil if @system_agents != new_system_agents
@system_agents = new_system_agents
@external_tools_by_name = external_tools_by_name
+ @external_registry_signature = signature
end
def external_feature_configs
diff --git a/plugins/discourse-ai/lib/engine.rb b/plugins/discourse-ai/lib/engine.rb
index 61aa616571d..151b2ba4544 100644
--- a/plugins/discourse-ai/lib/engine.rb
+++ b/plugins/discourse-ai/lib/engine.rb
@@ -91,4 +91,8 @@ module DiscourseAi
plugin,
)
end
+
+ def self.register_tools(tool_classes, plugin:)
+ tool_classes.each { |klass| DiscoursePluginRegistry.register_external_ai_tool(klass, plugin) }
+ end
end
diff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rb
index d02daacb30a..063e9fa5e71 100644
--- a/plugins/discourse-ai/plugin.rb
+++ b/plugins/discourse-ai/plugin.rb
@@ -84,6 +84,7 @@ require_relative "lib/mcp/oauth_flow"
# Other plugins can register features through this register.
DiscoursePluginRegistry.define_filtered_register(:external_ai_features)
+DiscoursePluginRegistry.define_filtered_register(:external_ai_tools)
DiscourseAi::Configuration::Module::NAMES.each do |module_name|
register_site_setting_area("ai-features/#{module_name}")
diff --git a/plugins/discourse-ai/spec/lib/agents/agent_spec.rb b/plugins/discourse-ai/spec/lib/agents/agent_spec.rb
index 05e211ebe64..4e781e04275 100644
--- a/plugins/discourse-ai/spec/lib/agents/agent_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/agent_spec.rb
@@ -6,10 +6,6 @@ module FakeExternalPlugin
{ name: "fake_external_tool", description: "A fake tool", parameters: [] }
end
- def self.custom?
- true
- end
-
def self.name
"fake_external_tool"
end
@@ -425,38 +421,64 @@ RSpec.describe DiscourseAi::Agents::Agent do
DiscoursePluginRegistry._raw_external_ai_features.reject! do |entry|
entry[:value][:module_name] == :test_module
end
+ DiscoursePluginRegistry._raw_external_ai_tools.reject! do |entry|
+ entry[:plugin] == fake_plugin
+ end
# remove fake entries from the registry
reset_external_registry!
end
- it "adds the external agent to system_agents" do
- register_fake_feature
+ it "registers standalone tools without an agent" do
+ DiscourseAi.register_tools([FakeExternalPlugin::FakeExternalTool], plugin: fake_plugin)
- expected_id = described_class.external_agent_id(FakeExternalAgent)
- expect(described_class.system_agents[FakeExternalAgent]).to eq(expected_id)
+ expect(described_class.external_tools).to include(FakeExternalPlugin::FakeExternalTool)
+ expect(AiAgent.new(tools: ["FakeExternalTool"]).class_instance.new.tools).to eq(
+ [FakeExternalPlugin::FakeExternalTool],
+ )
end
- it "makes the external agent discoverable by ID" do
- register_fake_feature
+ it "raises when two plugins register tools with the same class name" do
+ impostor = Class.new(DiscourseAi::Agents::Tools::Tool)
+ impostor.define_singleton_method(:to_s) { "OtherPlugin::FakeExternalTool" }
+ DiscourseAi.register_tools(
+ [FakeExternalPlugin::FakeExternalTool, impostor],
+ plugin: fake_plugin,
+ )
- expected_id = described_class.external_agent_id(FakeExternalAgent)
- expect(described_class.system_agents_by_id[expected_id]).to eq(FakeExternalAgent)
+ 2.times do
+ expect { described_class.external_tools }.to raise_error(
+ ArgumentError,
+ /FakeExternalTool.*already registered/,
+ )
+ end
end
- it "registers external tools for name-based lookup" do
- register_fake_feature
+ it "raises when a tool name normalises to a built-in tool" do
+ %w[OtherPlugin::SearchTool OtherPlugin::Tags].each do |name|
+ reset_external_registry!
+ impostor = Class.new(DiscourseAi::Agents::Tools::Tool)
+ impostor.define_singleton_method(:to_s) { name }
+ DiscourseAi.register_tools([impostor], plugin: fake_plugin)
- expect(described_class.external_tool_by_name("FakeExternalTool")).to eq(
- FakeExternalPlugin::FakeExternalTool,
- )
+ expect { described_class.external_tools }.to raise_error(
+ ArgumentError,
+ /#{name.demodulize}.*built-in/,
+ )
+
+ DiscoursePluginRegistry._raw_external_ai_tools.reject! do |entry|
+ entry[:plugin] == fake_plugin
+ end
+ end
end
- it "rebuilds external tool lookup when the cache is partially reset" do
- register_fake_feature
+ it "registers an external agent once under a stable id, with its tools" do
+ register_fake_feature(feature: :feature_one)
+ register_fake_feature(feature: :feature_two)
- expect(described_class.external_tool_by_name("FakeExternalTool")).to eq(
- FakeExternalPlugin::FakeExternalTool,
- )
+ expected_id = described_class.external_agent_id(FakeExternalAgent)
+ expect(described_class.system_agents[FakeExternalAgent]).to eq(expected_id)
+ expect(described_class.system_agents_by_id[expected_id]).to eq(FakeExternalAgent)
+ expect(FakeExternalAgent.new.available_tools).to include(FakeExternalPlugin::FakeExternalTool)
described_class.instance_variable_set(:@external_tools_by_name, nil)
@@ -465,14 +487,6 @@ RSpec.describe DiscourseAi::Agents::Agent do
)
end
- it "includes external tools in the agent's available_tools" do
- register_fake_feature
-
- instance = FakeExternalAgent.new
- tool_names = instance.available_tools.map { |t| t.signature[:name] }
- expect(tool_names).to include("fake_external_tool")
- end
-
it "lets an external agent opt into a bot user" do
register_fake_feature(feature: :bot_feature, agent_klass: FakeExternalBotAgent)
register_fake_feature
@@ -484,27 +498,10 @@ RSpec.describe DiscourseAi::Agents::Agent do
expect(default.can_have_bot_user?).to eq(false)
end
- it "produces one agent entry when two features share the same agent_klass" do
- register_fake_feature(feature: :feature_one)
- register_fake_feature(feature: :feature_two)
-
- matching = described_class.system_agents.select { |klass, _| klass == FakeExternalAgent }
- expect(matching.size).to eq(1)
- end
-
it "does not overwrite a builtin agent when registered as an external agent_klass" do
builtin_id = described_class.system_agents[DiscourseAi::Agents::SqlHelper]
- DiscoursePluginRegistry.register_external_ai_feature(
- {
- module_name: :test_module,
- feature: :sql_feature,
- agent_klass: DiscourseAi::Agents::SqlHelper,
- enabled_by_setting: nil,
- },
- fake_plugin,
- )
- reset_external_registry!
+ register_fake_feature(feature: :sql_feature, agent_klass: DiscourseAi::Agents::SqlHelper)
expect(described_class.system_agents[DiscourseAi::Agents::SqlHelper]).to eq(builtin_id)
end
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb
index d84186bcf66..c18d2cd218a 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb
@@ -31,7 +31,6 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController do
expected_tool_count =
DiscourseAi::Agents::Agent.all_available_tools.length +
- DiscourseAi::Agents::Agent.external_tools.length +
DiscourseAi::Completions::NativeTools.all.length
expect(response.parsed_body["meta"]["tools"].length).to eq(expected_tool_count)
end