A platform for community discussion. Free, open, simple.
DEV: Add AI tool to read the value of a site setting (#41774)
Penar Musaraj17dfce13ccd9
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex daaeb8a6cac..ed4696fa651 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -659,6 +659,7 @@ en:setting_context: "Look up site setting context"schema: "Look up database schema"search_settings: "Searching site settings"+ read_site_setting: "Read site setting"change_site_setting: "Change site setting"dall_e: "Generate image"search_meta_discourse: "Search Meta Discourse"@@ -710,6 +711,7 @@ en:setting_context: "Look up site setting context"schema: "Look up database schema"search_settings: "Search site settings"+ read_site_setting: "Read the current value of a site setting"change_site_setting: "Change the value of a site setting"dall_e: "Generate image using DALL-E 3"search_meta_discourse: "Search Meta Discourse"@@ -780,6 +782,7 @@ en:search_settings:one: "Found %{count} result for '%{query}'"other: "Found %{count} results for '%{query}'"+ read_site_setting: "Reading site setting %{setting_name}"change_site_setting: "Changing site setting %{setting_name} to '%{value}'"discoveries:continue_conversation:@@ -962,6 +965,13 @@ en:invalid_value: "Invalid value: %{error}"failed: "Failed to change the site setting."success: "Site setting %{setting_name} has been changed to '%{value}'."+ read_site_setting:+ errors:+ not_allowed: "You are not allowed to read site settings."+ not_found: "Site setting '%{setting_name}' does not exist."+ hidden: "Site setting '%{setting_name}' is hidden and cannot be read here."+ secret: "Site setting '%{setting_name}' contains a secret and cannot be read here."+ themeable: "Site setting '%{setting_name}' requires a theme and cannot be read here."reviewables:ai_tool_action:diff --git a/plugins/discourse-ai/lib/agents/agent.rb b/plugins/discourse-ai/lib/agents/agent.rbindex 7a89c52d965..74cb94da14e 100644--- a/plugins/discourse-ai/lib/agents/agent.rb+++ b/plugins/discourse-ai/lib/agents/agent.rb@@ -111,6 +111,7 @@ module DiscourseAiTools::DbSchema,Tools::SearchSettings,Tools::SettingContext,+ Tools::ReadSiteSetting,Tools::ChangeSiteSetting,Tools::RandomPicker,Tools::DiscourseMetaSearch,diff --git a/plugins/discourse-ai/lib/agents/settings_explorer.rb b/plugins/discourse-ai/lib/agents/settings_explorer.rbindex 30d5e346836..e394b0ea08b 100644--- a/plugins/discourse-ai/lib/agents/settings_explorer.rb+++ b/plugins/discourse-ai/lib/agents/settings_explorer.rb@@ -4,7 +4,12 @@ module DiscourseAimodule Agentsclass SettingsExplorer < Agentdef tools- [Tools::SettingContext, Tools::SearchSettings, Tools::ChangeSiteSetting]+ [+ Tools::SettingContext,+ Tools::SearchSettings,+ Tools::ReadSiteSetting,+ Tools::ChangeSiteSetting,+ ]enddef system_prompt@@ -13,6 +18,7 @@ module DiscourseAi- You are able to find information about all the site settings.- You are able to request context for a specific setting.+ - You are able to look up the current value of a site setting.- You are able to update site settings when an administrator asks you to do so, but you must get approval from a human before making any change.- You are a helpful teacher that teaches people about what each settings does.- Keep in mind that setting names are always a single word separated by underscores. eg. 'site_description'diff --git a/plugins/discourse-ai/lib/agents/tools/read_site_setting.rb b/plugins/discourse-ai/lib/agents/tools/read_site_setting.rbnew file mode 100644index 00000000000..aae113c213c--- /dev/null+++ b/plugins/discourse-ai/lib/agents/tools/read_site_setting.rb@@ -0,0 +1,107 @@+# frozen_string_literal: true++module DiscourseAi+ module Agents+ module Tools+ class ReadSiteSetting < Tool+ class << self+ def signature+ {+ name: name,+ description:+ "Returns the current value of a site setting. Look up the exact setting name first (e.g. with the search_settings tool); never guess it.",+ parameters: [+ {+ name: "setting_name",+ description: "The exact name of the site setting to read",+ type: "string",+ required: true,+ },+ ],+ }+ end++ def name+ "read_site_setting"+ end+ end++ def invoke+ if (validation_result = validation_error)+ return validation_result+ end++ { setting_name: setting_name, value: SiteSetting.get(setting_sym) }+ end++ def validation_error+ if !guardian.is_admin?+ return(+ error_response(I18n.t("discourse_ai.ai_bot.read_site_setting.errors.not_allowed"))+ )+ end++ if !SiteSetting.has_setting?(setting_name)+ return(+ error_response(+ I18n.t(+ "discourse_ai.ai_bot.read_site_setting.errors.not_found",+ setting_name: setting_name,+ ),+ )+ )+ end++ if SiteSetting.hidden_settings.include?(setting_sym)+ return(+ error_response(+ I18n.t(+ "discourse_ai.ai_bot.read_site_setting.errors.hidden",+ setting_name: setting_name,+ ),+ )+ )+ end++ if SiteSetting.secret_settings.include?(setting_sym)+ return(+ error_response(+ I18n.t(+ "discourse_ai.ai_bot.read_site_setting.errors.secret",+ setting_name: setting_name,+ ),+ )+ )+ end++ if SiteSetting.themeable[setting_sym]+ return(+ error_response(+ I18n.t(+ "discourse_ai.ai_bot.read_site_setting.errors.themeable",+ setting_name: setting_name,+ ),+ )+ )+ end++ nil+ end++ def description_args+ { setting_name: setting_name }+ end++ private++ def setting_name+ @setting_name ||= parameters[:setting_name].to_s.strip+ end++ def setting_sym+ setting_name.to_sym+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/spec/lib/agents/settings_explorer_spec.rb b/plugins/discourse-ai/spec/lib/agents/settings_explorer_spec.rbindex 4884a2e4360..7628e668cd7 100644--- a/plugins/discourse-ai/spec/lib/agents/settings_explorer_spec.rb+++ b/plugins/discourse-ai/spec/lib/agents/settings_explorer_spec.rb@@ -5,6 +5,12 @@ RSpec.describe DiscourseAi::Agents::SettingsExplorer dobefore { enable_current_plugin }+ it "instructs the agent to look up current setting values" do+ expect(settings_explorer.system_prompt).to include(+ "You are able to look up the current value of a site setting.",+ )+ end+it "requires human approval before updating settings for administrators" doexpect(settings_explorer.system_prompt).to include("You are able to update site settings when an administrator asks you to do so, but you must get approval from a human before making any change.",@@ -23,6 +29,7 @@ RSpec.describe DiscourseAi::Agents::SettingsExplorer do[DiscourseAi::Agents::Tools::SettingContext,DiscourseAi::Agents::Tools::SearchSettings,+ DiscourseAi::Agents::Tools::ReadSiteSetting,DiscourseAi::Agents::Tools::ChangeSiteSetting,],)diff --git a/plugins/discourse-ai/spec/lib/agents/tools/read_site_setting_spec.rb b/plugins/discourse-ai/spec/lib/agents/tools/read_site_setting_spec.rbnew file mode 100644index 00000000000..dd823f8653d--- /dev/null+++ b/plugins/discourse-ai/spec/lib/agents/tools/read_site_setting_spec.rb@@ -0,0 +1,52 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Agents::Tools::ReadSiteSetting do+ fab!(:llm_model)++ let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }+ let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }++ before do+ enable_current_plugin+ SiteSetting.ai_bot_enabled = true+ end++ def tool(setting_name, context: DiscourseAi::Agents::BotContext.new)+ described_class.new(+ { setting_name: setting_name },+ bot_user: bot_user,+ llm: llm,+ context: context,+ )+ end++ it "returns the current value for an administrator" do+ SiteSetting.min_post_length = 42++ expect(tool("min_post_length").invoke).to eq(setting_name: "min_post_length", value: 42)+ end++ it "returns an error when the acting user is not an administrator" do+ context = DiscourseAi::Agents::BotContext.new(user: Fabricate(:user))++ result = tool("min_post_length", context: context).invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("not allowed")+ end++ it "returns an error for an unknown setting" do+ result = tool("definitely_not_a_setting").invoke++ expect(result[:status]).to eq("error")+ expect(result[:error]).to include("definitely_not_a_setting")+ end++ it "does not return hidden or secret setting values" do+ hidden_setting = SiteSetting.hidden_settings.first+ secret_setting = SiteSetting.secret_settings.first++ expect(tool(hidden_setting).invoke[:status]).to eq("error")+ expect(tool(secret_setting).invoke[:status]).to eq("error")+ end+end