2024-07-02 11:51:59 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Summarization
|
|
|
|
class ChatSummaryController < ::Chat::ApiController
|
|
|
|
requires_plugin ::DiscourseAi::PLUGIN_NAME
|
|
|
|
requires_plugin ::Chat::PLUGIN_NAME
|
|
|
|
|
|
|
|
VALID_SINCE_VALUES = [1, 3, 6, 12, 24, 72, 168]
|
|
|
|
|
|
|
|
def show
|
|
|
|
since = params[:since].to_i
|
|
|
|
raise Discourse::InvalidParameters.new(:since) if !VALID_SINCE_VALUES.include?(since)
|
|
|
|
|
|
|
|
channel = ::Chat::Channel.find(params[:channel_id])
|
|
|
|
guardian.ensure_can_join_chat_channel!(channel)
|
|
|
|
|
2024-07-03 20:48:18 -04:00
|
|
|
strategy = DiscourseAi::Summarization.default_strategy
|
2024-07-02 11:51:59 -04:00
|
|
|
raise Discourse::NotFound.new unless strategy
|
2024-07-03 20:48:18 -04:00
|
|
|
|
|
|
|
guardian.ensure_can_request_summary!
|
2024-07-02 11:51:59 -04:00
|
|
|
|
|
|
|
RateLimiter.new(current_user, "channel_summary", 6, 5.minutes).performed!
|
|
|
|
|
|
|
|
hijack do
|
|
|
|
content = { content_title: channel.name }
|
|
|
|
|
|
|
|
content[:contents] = channel
|
|
|
|
.chat_messages
|
|
|
|
.where("chat_messages.created_at > ?", since.hours.ago)
|
|
|
|
.includes(:user)
|
|
|
|
.order(created_at: :asc)
|
|
|
|
.pluck(:id, :username_lower, :message)
|
|
|
|
.map { { id: _1, poster: _2, text: _3 } }
|
|
|
|
|
|
|
|
summarized_text =
|
|
|
|
if content[:contents].empty?
|
|
|
|
I18n.t("discourse_ai.summarization.chat.no_targets")
|
|
|
|
else
|
|
|
|
strategy.summarize(content, current_user).dig(:summary)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: { summary: summarized_text }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|