Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/jobs/autotitle_conversation_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def usable_topic(response)
return nil
end

document = JSON.parse(response)
document = JSON.parse(unfenced(response))
topic = document.is_a?(Hash) ? document["topic"] : nil

if topic.is_a?(String) && topic.present?
Expand All @@ -64,6 +64,12 @@ def usable_topic(response)
nil
end

def unfenced(text)
return text unless text.is_a?(String)

text[/```(?:json)?\s*(.*?)\s*```/m, 1] || text
end

def system_message
<<~END
You extract a 2-4 word topic from text. I will give the text of a chat. You reply with the topic of this chat,
Expand Down
11 changes: 11 additions & 0 deletions test/jobs/autotitle_conversation_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ class AutotitleConversationJobTest < ActiveJob::TestCase
assert_equal "Claude Summary", conversation.reload.title
end

test "a topic wrapped in a markdown json fence is still used" do
conversation = conversations(:hello_claude)
conversation.update!(title: nil)

TestClient::Anthropic.stub :text, "```json\n{ \"topic\": \"Vermont's capital\" }\n```" do
AutotitleConversationJob.perform_now(conversation.id)
end

assert_equal "Vermont's capital", conversation.reload.title, "The fence should be stripped before parsing the JSON"
end

test "gemini conversations are titled through the same intent path" do
conversation = conversations(:gemini_conversation)
conversation.update!(title: nil)
Expand Down
Loading