2025-04-10 08:16:31 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Admin
|
|
|
|
class AiFeaturesController < ::Admin::AdminController
|
|
|
|
requires_plugin ::DiscourseAi::PLUGIN_NAME
|
|
|
|
|
|
|
|
def index
|
2025-06-10 14:37:53 -03:00
|
|
|
render json: serialize_modules(DiscourseAi::Configuration::Module.all)
|
2025-04-10 08:16:31 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
raise Discourse::InvalidParameters.new(:id) if params[:id].blank?
|
2025-06-10 14:37:53 -03:00
|
|
|
|
|
|
|
a_module = DiscourseAi::Configuration::Module.find_by(id: params[:id].to_i)
|
|
|
|
|
|
|
|
render json: serialize_module(a_module)
|
2025-04-10 08:16:31 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2025-06-10 14:37:53 -03:00
|
|
|
def serialize_modules(modules)
|
2025-06-09 16:13:09 -03:00
|
|
|
modules.map { |a_module| serialize_module(a_module) }
|
2025-04-10 08:16:31 -07:00
|
|
|
end
|
|
|
|
|
2025-06-09 16:13:09 -03:00
|
|
|
def serialize_module(a_module)
|
|
|
|
return nil if a_module.blank?
|
2025-04-10 08:16:31 -07:00
|
|
|
|
2025-06-10 14:37:53 -03:00
|
|
|
{
|
|
|
|
id: a_module.id,
|
|
|
|
module_name: a_module.name,
|
|
|
|
module_enabled: a_module.enabled?,
|
|
|
|
features: a_module.features.map { |f| serialize_feature(f) },
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialize_feature(feature)
|
|
|
|
{
|
|
|
|
name: feature.name,
|
2025-06-27 12:35:41 +10:00
|
|
|
personas: feature.persona_ids.map { |id| serialize_persona(persona_id_obj_hash[id]) },
|
|
|
|
llm_models:
|
|
|
|
feature.llm_models.map do |llm_model|
|
|
|
|
{ id: llm_model.id, name: llm_model.display_name }
|
|
|
|
end,
|
2025-06-10 14:37:53 -03:00
|
|
|
enabled: feature.enabled?,
|
|
|
|
}
|
2025-04-10 08:16:31 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def serialize_persona(persona)
|
|
|
|
return nil if persona.blank?
|
|
|
|
|
|
|
|
serialize_data(persona, AiFeaturesPersonaSerializer, root: false)
|
|
|
|
end
|
2025-06-10 14:37:53 -03:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def persona_id_obj_hash
|
|
|
|
@persona_id_obj_hash ||=
|
|
|
|
begin
|
2025-06-27 12:35:41 +10:00
|
|
|
ids = DiscourseAi::Configuration::Feature.all.map(&:persona_ids).flatten.uniq
|
2025-06-10 14:37:53 -03:00
|
|
|
AiPersona.where(id: ids).index_by(&:id)
|
|
|
|
end
|
|
|
|
end
|
2025-04-10 08:16:31 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|