From db5cbfb148d65927c6335302602fae87a77d6376 Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Fri, 30 Aug 2024 17:17:14 -0300 Subject: [PATCH] FIX: Bail earlier when a chat thread has no messages (#789) --- lib/ai_helper/chat_thread_titler.rb | 8 +++++--- spec/lib/modules/ai_helper/chat_thread_titler_spec.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/ai_helper/chat_thread_titler.rb b/lib/ai_helper/chat_thread_titler.rb index 2b2c18280..15ffc52ca 100644 --- a/lib/ai_helper/chat_thread_titler.rb +++ b/lib/ai_helper/chat_thread_titler.rb @@ -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 = "\n#{thread_content}\n" prompt = diff --git a/spec/lib/modules/ai_helper/chat_thread_titler_spec.rb b/spec/lib/modules/ai_helper/chat_thread_titler_spec.rb index 6f5ae898b..16a4a9733 100644 --- a/spec/lib/modules/ai_helper/chat_thread_titler_spec.rb +++ b/spec/lib/modules/ai_helper/chat_thread_titler_spec.rb @@ -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"