Skip to content

Commit 8e83c09

Browse files
authored
DEV: Use explicit serializers for all models (#691)
1 parent b863ddc commit 8e83c09

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

app/controllers/discourse_ai/admin/ai_llms_controller.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ def create
3636
llm_model = LlmModel.new(ai_llm_params)
3737
if llm_model.save
3838
llm_model.toggle_companion_user
39-
render json: { ai_persona: llm_model }, status: :created
39+
render json: { ai_persona: LlmModelSerializer.new(llm_model) }, status: :created
4040
else
41-
render_json_error llm_model
41+
render_json_error LlmModelSerializer.new(llm_model)
4242
end
4343
end
4444

@@ -47,9 +47,9 @@ def update
4747

4848
if llm_model.update(ai_llm_params(updating: llm_model))
4949
llm_model.toggle_companion_user
50-
render json: llm_model
50+
render json: LlmModelSerializer.new(llm_model)
5151
else
52-
render_json_error llm_model
52+
render_json_error LlmModelSerializer.new(llm_model)
5353
end
5454
end
5555

app/controllers/discourse_ai/admin/ai_personas_controller.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ def create
4343
if ai_persona.save
4444
RagDocumentFragment.link_persona_and_uploads(ai_persona, attached_upload_ids)
4545

46-
render json: { ai_persona: ai_persona }, status: :created
46+
render json: {
47+
ai_persona: LocalizedAiPersonaSerializer.new(ai_persona, root: false),
48+
},
49+
status: :created
4750
else
4851
render_json_error ai_persona
4952
end
@@ -58,17 +61,17 @@ def update
5861
if @ai_persona.update(ai_persona_params.except(:rag_uploads))
5962
RagDocumentFragment.update_persona_uploads(@ai_persona, attached_upload_ids)
6063

61-
render json: @ai_persona
64+
render json: LocalizedAiPersonaSerializer.new(@ai_persona, root: false)
6265
else
63-
render_json_error @ai_persona
66+
render_json_error LocalizedAiPersonaSerializer.new(@ai_persona, root: false)
6467
end
6568
end
6669

6770
def destroy
6871
if @ai_persona.destroy
6972
head :no_content
7073
else
71-
render_json_error @ai_persona
74+
render_json_error LocalizedAiPersonaSerializer.new(@ai_persona, root: false)
7275
end
7376
end
7477

app/controllers/discourse_ai/ai_bot/bot_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def show_debug_info
1818

1919
debug_info = AiApiAuditLog.where(post: posts).order(created_at: :desc).first
2020

21-
render json: debug_info, status: 200
21+
render json: AiApiAuditLogSerializer.new(debug_info, root: false), status: 200
2222
end
2323

2424
def stop_streaming_response
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
class AiApiAuditLogSerializer < ApplicationSerializer
4+
attributes :id,
5+
:provider_id,
6+
:user_id,
7+
:request_tokens,
8+
:response_tokens,
9+
:raw_request_payload,
10+
:raw_response_payload,
11+
:topic_id,
12+
:post_id,
13+
:feature_name,
14+
:language_model,
15+
:created_at
16+
end

app/serializers/ai_chat_channel_serializer.rb

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
class AiChatChannelSerializer < ApplicationSerializer
44
attributes :id, :chatable, :chatable_type, :chatable_url, :slug
55

6+
def chatable
7+
case object.chatable_type
8+
when "Category"
9+
BasicCategorySerializer.new(object.chatable, root: false).as_json
10+
when "DirectMessage"
11+
Chat::DirectMessageSerializer.new(object.chatable, scope: scope, root: false).as_json
12+
when "Site"
13+
nil
14+
end
15+
end
16+
617
def title
718
# Display all participants for a DM.
819
# For category channels, the argument is ignored.

0 commit comments

Comments
 (0)