Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 8f7dbc3

Browse files
committed
Refactor code for clarity
1 parent d9d1c90 commit 8f7dbc3

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

app/controllers/discourse_ai/admin/ai_personas_controller.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def thread_pool
8181
Concurrent::CachedThreadPool.new(min_threads: 0, max_threads: POOL_SIZE, idletime: 30)
8282
end
8383

84-
def queue_reply(&block)
84+
def schedule_block(&block)
8585
# think about a better way to handle cross thread connections
8686
if Rails.env.test?
8787
block.call
@@ -175,7 +175,17 @@ def stream_reply
175175

176176
user = current_user
177177

178-
self.class.queue_reply do
178+
self.class.queue_streamed_reply(io, persona, user, topic, post)
179+
end
180+
181+
private
182+
183+
AI_STREAM_CONVERSATION_UNIQUE_ID = "ai-stream-conversation-unique-id"
184+
185+
# keeping this in a static method so we don't capture ENV and other bits
186+
# this allows us to release memory earlier
187+
def self.queue_streamed_reply(io, persona, user, topic, post)
188+
schedule_block do
179189
begin
180190
io.write "HTTP/1.1 200 OK"
181191
io.write CRLF
@@ -198,7 +208,6 @@ def stream_reply
198208
DiscourseAi::AiBot::Personas::Persona.find_by(id: persona.id, user: user)
199209
bot = DiscourseAi::AiBot::Bot.as(persona.user, persona: persona_class.new)
200210

201-
topic_id = topic.id
202211
data =
203212
{ topic_id: topic.id, bot_user_id: persona.user.id, persona_id: persona.id }.to_json +
204213
"\n\n"
@@ -241,10 +250,6 @@ def stream_reply
241250
end
242251
end
243252

244-
private
245-
246-
AI_STREAM_CONVERSATION_UNIQUE_ID = "ai-stream-conversation-unique-id"
247-
248253
def stage_user
249254
unique_id = params[:user_unique_id].to_s
250255
field = UserCustomField.find_by(name: AI_STREAM_CONVERSATION_UNIQUE_ID, value: unique_id)

0 commit comments

Comments
 (0)