Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit ed907dd

Browse files
authored
FEATURE: allow to send LLM reports to groups (#1246)
* FEATURE: allow to send LLM reports to groups * spec regression
1 parent 5ed0990 commit ed907dd

File tree

6 files changed

+81
-10
lines changed

6 files changed

+81
-10
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
class MigrateUsersToEmailGroup < ActiveRecord::Migration[7.2]
3+
def up
4+
execute <<~SQL
5+
UPDATE discourse_automation_fields
6+
SET component = 'email_group_user'
7+
WHERE
8+
component = 'users' AND
9+
name = 'receivers' AND
10+
automation_id IN (SELECT id FROM discourse_automation_automations WHERE script = 'llm_report')
11+
SQL
12+
end
13+
14+
def down
15+
execute <<~SQL
16+
UPDATE discourse_automation_fields
17+
SET component = 'users'
18+
WHERE
19+
component = 'email_group_user' AND
20+
name = 'receivers' AND
21+
automation_id IN (SELECT id FROM discourse_automation_automations WHERE script = 'llm_report')
22+
SQL
23+
end
24+
end

discourse_automation/llm_report.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module DiscourseAutomation::LlmReport
99
triggerables %i[recurring]
1010

1111
field :sender, component: :user, required: true
12-
field :receivers, component: :users
12+
field :receivers, component: :email_group_user
1313
field :topic_id, component: :text
1414
field :title, component: :text
1515
field :days, component: :text, required: true, default_value: 7

lib/automation/report_context_generator.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,15 @@ def initialize(
6565
@posts = @posts.where(topic_id: topic_ids_with_tags)
6666
end
6767

68-
@solutions =
69-
DiscourseSolved::SolvedTopic
70-
.where(topic_id: @posts.select(:topic_id))
71-
.pluck(:topic_id, :answer_post_id)
72-
.to_h
68+
if defined?(::DiscourseSolved)
69+
@solutions =
70+
DiscourseSolved::SolvedTopic
71+
.where(topic_id: @posts.select(:topic_id))
72+
.pluck(:topic_id, :answer_post_id)
73+
.to_h
74+
else
75+
@solutions = {}
76+
end
7377
end
7478

7579
def format_topic(topic)

lib/automation/report_runner.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ def initialize(
5757
automation: nil
5858
)
5959
@sender = User.find_by(username: sender_username)
60-
@receivers = User.where(username: receivers)
60+
receivers_without_emails = receivers&.reject { |r| r.include? "@" }
61+
if receivers_without_emails.present?
62+
@group_receivers = Group.where(name: receivers_without_emails)
63+
receivers_without_emails -= @group_receivers.pluck(:name)
64+
@receivers = User.where(username: receivers_without_emails)
65+
else
66+
@group_receivers = []
67+
@receivers = []
68+
end
6169
@email_receivers = receivers&.filter { |r| r.include? "@" }
6270
@title =
6371
if title.present?
@@ -88,7 +96,8 @@ def initialize(
8896
@temperature = nil if temperature.to_f < 0
8997
@suppress_notifications = suppress_notifications
9098

91-
if !@topic_id && !@receivers.present? && !@email_receivers.present?
99+
if !@topic_id && !@receivers.present? && !@group_receivers.present? &&
100+
!@email_receivers.present?
92101
raise ArgumentError, "Must specify topic_id or receivers"
93102
end
94103
@automation = automation
@@ -165,6 +174,7 @@ def run!
165174
end
166175

167176
receiver_usernames = @receivers.map(&:username).join(",")
177+
receiver_groupnames = @group_receivers.map(&:name).join(",")
168178

169179
result = suppress_notifications(result) if @suppress_notifications
170180

@@ -173,14 +183,15 @@ def run!
173183
# no debug mode for topics, it is too noisy
174184
end
175185

176-
if receiver_usernames.present?
186+
if receiver_usernames.present? || receiver_groupnames.present?
177187
post =
178188
PostCreator.create!(
179189
@sender,
180190
raw: result,
181191
title: title,
182192
archetype: Archetype.private_message,
183193
target_usernames: receiver_usernames,
194+
target_group_names: receiver_groupnames,
184195
skip_validations: true,
185196
)
186197

spec/lib/discourse_automation/llm_report_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def add_automation_field(name, value, type: "text")
2323

2424
it "can trigger via automation" do
2525
add_automation_field("sender", user.username, type: "user")
26-
add_automation_field("receivers", [user.username], type: "users")
26+
add_automation_field("receivers", [user.username], type: "email_group_user")
2727
add_automation_field("model", "custom:#{llm_model.id}")
2828
add_automation_field("title", "Weekly report")
2929

spec/lib/modules/automation/report_runner_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,38 @@ module Automation
190190
expect(debugging).not_to include(post_with_tag.raw)
191191
end
192192

193+
it "can send reports to groups only" do
194+
group_for_reports = Fabricate(:group)
195+
group_member = Fabricate(:user)
196+
group_for_reports.add(group_member)
197+
198+
DiscourseAi::Completions::Llm.with_prepared_responses(["group report"]) do
199+
ReportRunner.run!(
200+
sender_username: user.username,
201+
receivers: [group_for_reports.name],
202+
title: "group report",
203+
model: "custom:#{llm_model.id}",
204+
category_ids: nil,
205+
tags: nil,
206+
allow_secure_categories: false,
207+
debug_mode: false,
208+
sample_size: 100,
209+
instructions: "make a group report",
210+
days: 7,
211+
offset: 0,
212+
priority_group_id: nil,
213+
tokens_per_post: 150,
214+
)
215+
end
216+
217+
report_topic =
218+
Topic.where(title: "group report", archetype: Archetype.private_message).first
219+
expect(report_topic).to be_present
220+
expect(report_topic.allowed_groups.map(&:id)).to eq([group_for_reports.id])
221+
expect(report_topic.allowed_users.map(&:id)).to eq([user.id])
222+
expect(report_topic.ordered_posts.first.raw).to eq("group report")
223+
end
224+
193225
it "generates correctly respects the params" do
194226
DiscourseAi::Completions::Llm.with_prepared_responses(["magical report"]) do
195227
ReportRunner.run!(

0 commit comments

Comments
 (0)