DEV: Use explicit serializers for all models (#691)

This commit is contained in:
Jan Cernik 2024-06-27 10:43:00 -03:00 committed by GitHub
parent b863ddc94b
commit 8e83c091a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 39 additions and 9 deletions

View File

@ -36,9 +36,9 @@ module DiscourseAi
llm_model = LlmModel.new(ai_llm_params) llm_model = LlmModel.new(ai_llm_params)
if llm_model.save if llm_model.save
llm_model.toggle_companion_user llm_model.toggle_companion_user
render json: { ai_persona: llm_model }, status: :created render json: { ai_persona: LlmModelSerializer.new(llm_model) }, status: :created
else else
render_json_error llm_model render_json_error LlmModelSerializer.new(llm_model)
end end
end end
@ -47,9 +47,9 @@ module DiscourseAi
if llm_model.update(ai_llm_params(updating: llm_model)) if llm_model.update(ai_llm_params(updating: llm_model))
llm_model.toggle_companion_user llm_model.toggle_companion_user
render json: llm_model render json: LlmModelSerializer.new(llm_model)
else else
render_json_error llm_model render_json_error LlmModelSerializer.new(llm_model)
end end
end end

View File

@ -43,7 +43,10 @@ module DiscourseAi
if ai_persona.save if ai_persona.save
RagDocumentFragment.link_persona_and_uploads(ai_persona, attached_upload_ids) RagDocumentFragment.link_persona_and_uploads(ai_persona, attached_upload_ids)
render json: { ai_persona: ai_persona }, status: :created render json: {
ai_persona: LocalizedAiPersonaSerializer.new(ai_persona, root: false),
},
status: :created
else else
render_json_error ai_persona render_json_error ai_persona
end end
@ -58,9 +61,9 @@ module DiscourseAi
if @ai_persona.update(ai_persona_params.except(:rag_uploads)) if @ai_persona.update(ai_persona_params.except(:rag_uploads))
RagDocumentFragment.update_persona_uploads(@ai_persona, attached_upload_ids) RagDocumentFragment.update_persona_uploads(@ai_persona, attached_upload_ids)
render json: @ai_persona render json: LocalizedAiPersonaSerializer.new(@ai_persona, root: false)
else else
render_json_error @ai_persona render_json_error LocalizedAiPersonaSerializer.new(@ai_persona, root: false)
end end
end end
@ -68,7 +71,7 @@ module DiscourseAi
if @ai_persona.destroy if @ai_persona.destroy
head :no_content head :no_content
else else
render_json_error @ai_persona render_json_error LocalizedAiPersonaSerializer.new(@ai_persona, root: false)
end end
end end

View File

@ -18,7 +18,7 @@ module DiscourseAi
debug_info = AiApiAuditLog.where(post: posts).order(created_at: :desc).first debug_info = AiApiAuditLog.where(post: posts).order(created_at: :desc).first
render json: debug_info, status: 200 render json: AiApiAuditLogSerializer.new(debug_info, root: false), status: 200
end end
def stop_streaming_response def stop_streaming_response

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
class AiApiAuditLogSerializer < ApplicationSerializer
attributes :id,
:provider_id,
:user_id,
:request_tokens,
:response_tokens,
:raw_request_payload,
:raw_response_payload,
:topic_id,
:post_id,
:feature_name,
:language_model,
:created_at
end

View File

@ -3,6 +3,17 @@
class AiChatChannelSerializer < ApplicationSerializer class AiChatChannelSerializer < ApplicationSerializer
attributes :id, :chatable, :chatable_type, :chatable_url, :slug attributes :id, :chatable, :chatable_type, :chatable_url, :slug
def chatable
case object.chatable_type
when "Category"
BasicCategorySerializer.new(object.chatable, root: false).as_json
when "DirectMessage"
Chat::DirectMessageSerializer.new(object.chatable, scope: scope, root: false).as_json
when "Site"
nil
end
end
def title def title
# Display all participants for a DM. # Display all participants for a DM.
# For category channels, the argument is ignored. # For category channels, the argument is ignored.