From 422f238f9f8441c1aacc182011b42a967b2667cd Mon Sep 17 00:00:00 2001 From: Keegan George Date: Mon, 7 Jul 2025 13:49:27 -0700 Subject: [PATCH] FIX: title suggestions should return 5 unique titles This update fixes a regression from https://github.com/discourse/discourse-ai/pull/1484, which caused AI helper title suggestions to begin suggesting numerous non-unique titles because it was looping through structured responses incorrectly. --- lib/ai_helper/assistant.rb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/ai_helper/assistant.rb b/lib/ai_helper/assistant.rb index eaa87834..be61e415 100644 --- a/lib/ai_helper/assistant.rb +++ b/lib/ai_helper/assistant.rb @@ -142,14 +142,21 @@ module DiscourseAi Proc.new do |partial, _, type| if type == :structured_output && schema_type helper_chunk = partial.read_buffered_property(schema_key) - if !helper_chunk.nil? && !helper_chunk.empty? - if schema_type == "string" || schema_type == "array" - helper_response << helper_chunk - else - helper_response = helper_chunk + next if helper_chunk.nil? || helper_chunk.empty? + + if schema_type == "array" + if helper_chunk.is_a?(Array) + helper_chunk.each do |item| + helper_response << item if helper_response.exclude?(item) + end end - block.call(helper_chunk) if block && !bad_json + elsif schema_type == "string" + helper_response << helper_chunk + else + helper_response = helper_chunk end + + block.call(helper_chunk) if block && !bad_json elsif type.blank? # Assume response is a regular completion. helper_response << partial