Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions db/post_migrate/20250404045050_migrate_users_to_email_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true
class MigrateUsersToEmailGroup < ActiveRecord::Migration[7.2]
def up
execute <<~SQL
UPDATE discourse_automation_fields
SET component = 'email_group_user'
WHERE
component = 'users' AND
name = 'receivers' AND
automation_id IN (SELECT id FROM discourse_automation_automations WHERE script = 'llm_report')
SQL
end

def down
execute <<~SQL
UPDATE discourse_automation_fields
SET component = 'users'
WHERE
component = 'email_group_user' AND
name = 'receivers' AND
automation_id IN (SELECT id FROM discourse_automation_automations WHERE script = 'llm_report')
SQL
end
end
2 changes: 1 addition & 1 deletion discourse_automation/llm_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module DiscourseAutomation::LlmReport
triggerables %i[recurring]

field :sender, component: :user, required: true
field :receivers, component: :users
field :receivers, component: :email_group_user
field :topic_id, component: :text
field :title, component: :text
field :days, component: :text, required: true, default_value: 7
Expand Down
14 changes: 9 additions & 5 deletions lib/automation/report_context_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ def initialize(
@posts = @posts.where(topic_id: topic_ids_with_tags)
end

@solutions =
DiscourseSolved::SolvedTopic
.where(topic_id: @posts.select(:topic_id))
.pluck(:topic_id, :answer_post_id)
.to_h
if defined?(::DiscourseSolved)
@solutions =
DiscourseSolved::SolvedTopic
.where(topic_id: @posts.select(:topic_id))
.pluck(:topic_id, :answer_post_id)
.to_h
else
@solutions = {}
end
end

def format_topic(topic)
Expand Down
17 changes: 14 additions & 3 deletions lib/automation/report_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ def initialize(
automation: nil
)
@sender = User.find_by(username: sender_username)
@receivers = User.where(username: receivers)
receivers_without_emails = receivers&.reject { |r| r.include? "@" }
if receivers_without_emails.present?
@group_receivers = Group.where(name: receivers_without_emails)
receivers_without_emails -= @group_receivers.pluck(:name)
@receivers = User.where(username: receivers_without_emails)
else
@group_receivers = []
@receivers = []
end
@email_receivers = receivers&.filter { |r| r.include? "@" }
@title =
if title.present?
Expand Down Expand Up @@ -88,7 +96,8 @@ def initialize(
@temperature = nil if temperature.to_f < 0
@suppress_notifications = suppress_notifications

if !@topic_id && [email protected]? && !@email_receivers.present?
if !@topic_id && [email protected]? && !@group_receivers.present? &&
!@email_receivers.present?
raise ArgumentError, "Must specify topic_id or receivers"
end
@automation = automation
Expand Down Expand Up @@ -165,6 +174,7 @@ def run!
end

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

result = suppress_notifications(result) if @suppress_notifications

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

if receiver_usernames.present?
if receiver_usernames.present? || receiver_groupnames.present?
post =
PostCreator.create!(
@sender,
raw: result,
title: title,
archetype: Archetype.private_message,
target_usernames: receiver_usernames,
target_group_names: receiver_groupnames,
skip_validations: true,
)

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/discourse_automation/llm_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def add_automation_field(name, value, type: "text")

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

Expand Down
32 changes: 32 additions & 0 deletions spec/lib/modules/automation/report_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,38 @@ module Automation
expect(debugging).not_to include(post_with_tag.raw)
end

it "can send reports to groups only" do
group_for_reports = Fabricate(:group)
group_member = Fabricate(:user)
group_for_reports.add(group_member)

DiscourseAi::Completions::Llm.with_prepared_responses(["group report"]) do
ReportRunner.run!(
sender_username: user.username,
receivers: [group_for_reports.name],
title: "group report",
model: "custom:#{llm_model.id}",
category_ids: nil,
tags: nil,
allow_secure_categories: false,
debug_mode: false,
sample_size: 100,
instructions: "make a group report",
days: 7,
offset: 0,
priority_group_id: nil,
tokens_per_post: 150,
)
end

report_topic =
Topic.where(title: "group report", archetype: Archetype.private_message).first
expect(report_topic).to be_present
expect(report_topic.allowed_groups.map(&:id)).to eq([group_for_reports.id])
expect(report_topic.allowed_users.map(&:id)).to eq([user.id])
expect(report_topic.ordered_posts.first.raw).to eq("group report")
end

it "generates correctly respects the params" do
DiscourseAi::Completions::Llm.with_prepared_responses(["magical report"]) do
ReportRunner.run!(
Expand Down