2023-12-12 09:28:39 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
class StreamPostHelper < ::Jobs::Base
|
|
|
|
sidekiq_options retry: false
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
return unless post = Post.includes(:topic).find_by(id: args[:post_id])
|
|
|
|
return unless user = User.find_by(id: args[:user_id])
|
2024-08-08 11:32:39 -07:00
|
|
|
return unless args[:text]
|
2023-12-12 09:28:39 -08:00
|
|
|
|
|
|
|
topic = post.topic
|
|
|
|
reply_to = post.reply_to_post
|
|
|
|
|
2024-03-12 16:51:41 +11:00
|
|
|
return unless user.guardian.can_see?(post)
|
2023-12-12 09:28:39 -08:00
|
|
|
|
2025-05-27 10:37:30 -03:00
|
|
|
helper_mode = args[:prompt]
|
2023-12-12 09:28:39 -08:00
|
|
|
|
2025-05-27 10:37:30 -03:00
|
|
|
if helper_mode == DiscourseAi::AiHelper::Assistant::EXPLAIN
|
|
|
|
input = <<~TEXT.strip
|
|
|
|
<term>#{args[:text]}</term>
|
|
|
|
<context>#{post.raw}</context>
|
2023-12-12 09:28:39 -08:00
|
|
|
<topic>#{topic.title}</topic>
|
|
|
|
#{reply_to ? "<replyTo>#{reply_to.raw}</replyTo>" : nil}
|
|
|
|
TEXT
|
2024-08-08 11:32:39 -07:00
|
|
|
else
|
|
|
|
input = args[:text]
|
|
|
|
end
|
2023-12-12 09:28:39 -08:00
|
|
|
|
|
|
|
DiscourseAi::AiHelper::Assistant.new.stream_prompt(
|
2025-05-27 10:37:30 -03:00
|
|
|
helper_mode,
|
2023-12-12 09:28:39 -08:00
|
|
|
input,
|
|
|
|
user,
|
2024-08-08 11:32:39 -07:00
|
|
|
"/discourse-ai/ai-helper/stream_suggestion/#{post.id}",
|
2025-05-27 10:37:30 -03:00
|
|
|
custom_prompt: args[:custom_prompt],
|
2023-12-12 09:28:39 -08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|