discourse-ai/discourse_automation/llm_agent_triage.rb

59 lines
1.5 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
if defined?(DiscourseAutomation)
2025-05-29 15:40:46 +10:00
DiscourseAutomation::Scriptable.add("llm_agent_triage") do
version 1
run_in_background
triggerables %i[post_created_edited]
2025-05-29 15:40:46 +10:00
field :agent,
component: :choices,
required: true,
extra: {
2025-05-29 15:40:46 +10:00
content: DiscourseAi::Automation.available_agent_choices,
}
field :whisper, component: :boolean
field :silent_mode, component: :boolean
script do |context, fields|
post = context["post"]
next if post&.user&.bot?
2025-05-29 15:40:46 +10:00
agent_id = fields.dig("agent", "value")
whisper = !!fields.dig("whisper", "value")
silent_mode = !!fields.dig("silent_mode", "value")
begin
RateLimiter.new(
Discourse.system_user,
2025-05-29 15:40:46 +10:00
"llm_agent_triage_#{post.id}",
SiteSetting.ai_automation_max_triage_per_post_per_minute,
1.minute,
).performed!
RateLimiter.new(
Discourse.system_user,
2025-05-29 15:40:46 +10:00
"llm_agent_triage",
SiteSetting.ai_automation_max_triage_per_minute,
1.minute,
).performed!
2025-05-29 15:40:46 +10:00
DiscourseAi::Automation::LlmAgentTriage.handle(
post: post,
2025-05-29 15:40:46 +10:00
agent_id: agent_id,
whisper: whisper,
automation: self.automation,
silent_mode: silent_mode,
)
rescue => e
Discourse.warn_exception(
e,
2025-05-29 15:40:46 +10:00
message: "llm_agent_triage: skipped triage on post #{post.id}",
)
raise e if Rails.env.tests?
end
end
end
end