Skip to content

Commit

Permalink
FIX: Bail earlier when a chat thread has no messages (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
romanrizzi authored Aug 30, 2024
1 parent ed97827 commit db5cbfb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/ai_helper/chat_thread_titler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ def initialize(thread)
end

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

def call_llm(thread_content)
return nil if thread_content.blank?

chat = "<input>\n#{thread_content}\n</input>"

prompt =
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/modules/ai_helper/chat_thread_titler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
fab!(:chat_message) { Fabricate(:chat_message, thread: thread) }
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
it "picks the first when there are multiple" do
titles = "The solitary horse\nThe horse etched in gold"
Expand Down

0 comments on commit db5cbfb

Please sign in to comment.