FIX: Bail earlier when a chat thread has no messages (#789)

This commit is contained in:
Roman Rizzi 2024-08-30 17:17:14 -03:00 committed by GitHub
parent ed97827f49
commit db5cbfb148
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 3 deletions

View File

@ -8,12 +8,14 @@ module DiscourseAi
end end
def suggested_title def suggested_title
@thread.then { thread_content(_1) }.then { call_llm(_1) }.then { cleanup(_1) } content = thread_content(@thread)
return nil if content.blank?
suggested_title = call_llm(content)
cleanup(suggested_title)
end end
def call_llm(thread_content) def call_llm(thread_content)
return nil if thread_content.blank?
chat = "<input>\n#{thread_content}\n</input>" chat = "<input>\n#{thread_content}\n</input>"
prompt = prompt =

View File

@ -9,6 +9,16 @@ RSpec.describe DiscourseAi::AiHelper::ChatThreadTitler do
fab!(:chat_message) { Fabricate(:chat_message, thread: thread) } fab!(:chat_message) { Fabricate(:chat_message, thread: thread) }
fab!(:user) fab!(:user)
describe "#suggested_title" do
it "bails early if thread has no content" do
empty_thread = Chat::Thread.new
result = described_class.new(empty_thread).suggested_title
expect(result).to be_nil
end
end
describe "#cleanup" do describe "#cleanup" do
it "picks the first when there are multiple" do it "picks the first when there are multiple" do
titles = "The solitary horse\nThe horse etched in gold" titles = "The solitary horse\nThe horse etched in gold"