A platform for community discussion. Free, open, simple.
FEATURE: Add {username} template param to BotContext (#41446)
RnL Dev3f4a87b13554
diff --git a/plugins/discourse-ai/lib/agents/bot_context.rb b/plugins/discourse-ai/lib/agents/bot_context.rbindex 8561fe26a21..5fa7238612a 100644--- a/plugins/discourse-ai/lib/agents/bot_context.rb+++ b/plugins/discourse-ai/lib/agents/bot_context.rb@@ -104,6 +104,7 @@ module DiscourseAisite_titlesite_descriptionparticipants+ usernameresource_urlinferred_conceptsuser_language@@ -139,6 +140,10 @@ module DiscourseAi@private_messageend+ def username+ @user&.username+ end+def top_categories@top_categories ||=Categorydiff --git a/plugins/discourse-ai/spec/lib/agents/agent_spec.rb b/plugins/discourse-ai/spec/lib/agents/agent_spec.rbindex 7903ff428f2..83c26bf0800 100644--- a/plugins/discourse-ai/spec/lib/agents/agent_spec.rb+++ b/plugins/discourse-ai/spec/lib/agents/agent_spec.rb@@ -45,6 +45,7 @@ class TestAgent < DiscourseAi::Agents::Agent{site_title}{site_description}{participants}+ {username}{time}{date}{resource_url}@@ -65,6 +66,8 @@ RSpec.describe DiscourseAi::Agents::Agent dolet(:resource_url) { "https://path-to-resource" }let(:inferred_concepts) { %w[bulbassaur charmander squirtle].join(", ") }+ let(:context_user) { User.new(username: "alice") }+let(:context) doDiscourseAi::Agents::BotContext.new(site_url: Discourse.base_url,@@ -72,6 +75,7 @@ RSpec.describe DiscourseAi::Agents::Agent dosite_description: "test site description",time: Time.zone.now,participants: topic_with_users.allowed_users.map(&:username).join(", "),+ user: context_user,resource_url: resource_url,inferred_concepts: inferred_concepts,)@@ -98,6 +102,8 @@ RSpec.describe DiscourseAi::Agents::Agent doexpect(system_message).to include("test site title")expect(system_message).to include("test site description")expect(system_message).to include("joe, jane")+ expect(system_message).to include("alice")+ expect(system_message).not_to include("{username}")expect(system_message).to include(Time.zone.now.to_s)expect(system_message).to include(resource_url)expect(system_message).to include(inferred_concepts)@@ -111,6 +117,24 @@ RSpec.describe DiscourseAi::Agents::Agent doexpect(tools.find { |t| t.name == "image" }).to be_nilend+ it "leaves {username} literal when no user is present" do+ context_without_user =+ DiscourseAi::Agents::BotContext.new(+ site_url: Discourse.base_url,+ site_title: "test site title",+ site_description: "test site description",+ time: Time.zone.now,+ participants: topic_with_users.allowed_users.map(&:username).join(", "),+ resource_url: resource_url,+ inferred_concepts: inferred_concepts,+ )++ rendered = agent.craft_prompt(context_without_user)+ system_message = rendered.messages.first[:content]++ expect(system_message).to include("{username}")+ end+it "can parse string that are wrapped in quotes" dotool_call =DiscourseAi::Completions::ToolCall.new(