A platform for community discussion. Free, open, simple.
PERF: Trim candidate metadata sent to the Ask AI synthesis model (#45006)
Rafael dos Santos Silvac1b8ed595344
diff --git a/plugins/discourse-ai/lib/discoveries/synthesis.rb b/plugins/discourse-ai/lib/discoveries/synthesis.rbindex c14f47fe9df..2f7dd46cdc4 100644--- a/plugins/discourse-ai/lib/discoveries/synthesis.rb+++ b/plugins/discourse-ai/lib/discoveries/synthesis.rb@@ -171,24 +171,27 @@ module DiscourseAi)end+ # Most of each candidate is metadata rather than content, so it is kept+ # to what the model can use: days rather than timestamps, links without+ # the slug that repeats the title, and flags only when they are set.+ # Whether a passage is the topic's first post shows in its post_number.def candidate_input(candidate)input =candidate.slice("source_ref","title",- "url","username",- "created","category","likes","topic_views","topic_likes","topic_replies","tags",- "author_is_staff",- "is_topic_op","source_name",).compact+ input["url"] = slugless_url(candidate["url"]) if candidate["url"]+ input["created"] = day(candidate["created"]) if candidate["created"]+ input["author_is_staff"] = true if candidate["author_is_staff"]if candidate["passages"].present?input["passages"] = candidate["passages"].map do |passage|passage.slice("post_number", "excerpt").compact@@ -196,9 +199,20 @@ module DiscourseAielseinput["excerpt"] = candidate["excerpt"] if candidate["excerpt"]end- last_updated_at = candidate["post_updated_at"] || candidate["published_at"]- input["last_updated_at"] = last_updated_at if last_updated_at- input+ last_updated_at = day(candidate["post_updated_at"] || candidate["published_at"])+ input["last_updated_at"] = last_updated_at if last_updated_at != input["created"]+ input.compact+ end++ def slugless_url(url)+ url.sub(%r{\A(#{Regexp.escape(Discourse.base_path)}/t/)(?!\d+(?:/|\z))[^/]+/(?=\d)}, '\\1')+ end++ def day(value)+ value = Time.iso8601(value) if value.is_a?(String)+ value&.utc&.to_date&.iso8601+ rescue ArgumentError+ nilenddef normalized_related_count(related_count)diff --git a/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb b/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rbindex f4deb98ed9b..10f53bb31f9 100644--- a/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb+++ b/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb@@ -42,6 +42,74 @@ describe DiscourseAi::Discoveries::Synthesis doexpect(source_refs["max_items"]).to eq(6)end+ it "leaves out unset flags, whether the post is the first, and a same-day last update" do+ candidate = {+ "source_ref" => "source_1",+ "title" => "Plugins",+ "url" => "/t/plugins/1/3",+ "passages" => [{ "post_number" => 3, "excerpt" => "Use the skeleton." }],+ "created" => Time.utc(2026, 8, 1, 9),+ "author_is_staff" => false,+ "is_topic_op" => false,+ "topic_replies" => 0,+ "post_updated_at" => "2026-08-01T18:00:00.000000Z",+ }++ supplied_candidates =+ DiscourseAi::Completions::Llm.with_prepared_responses(+ [{ answerable: false, source_refs: [], title: "", answer: "" }],+ ) do |_, _, prompts|+ synthesis.call(query: "plugins", candidates: [candidate])+ JSON.parse(prompts.first.messages.last[:content]).fetch("candidates")+ end++ expect(supplied_candidates).to eq(+ [+ {+ "source_ref" => "source_1",+ "title" => "Plugins",+ "url" => "/t/1/3",+ "passages" => [{ "post_number" => 3, "excerpt" => "Use the skeleton." }],+ "created" => "2026-08-01",+ "topic_replies" => 0,+ },+ ],+ )+ end++ it "sends an external candidate's publish date as a day and its link unchanged" do+ candidate = {+ "source_ref" => "source_1",+ "kind" => "external",+ "source_name" => "Help center",+ "title" => "Plugin quickstart",+ "url" => "https://kb.example.com/t/plugins/12",+ "excerpt" => "The full quickstart.",+ "published_at" => "2026-09-01T10:00:00Z",+ }++ supplied_candidates =+ DiscourseAi::Completions::Llm.with_prepared_responses(+ [{ answerable: false, source_refs: [], title: "", answer: "" }],+ ) do |_, _, prompts|+ synthesis.call(query: "plugins", candidates: [candidate])+ JSON.parse(prompts.first.messages.last[:content]).fetch("candidates")+ end++ expect(supplied_candidates).to eq(+ [+ {+ "source_ref" => "source_1",+ "source_name" => "Help center",+ "title" => "Plugin quickstart",+ "url" => "https://kb.example.com/t/plugins/12",+ "excerpt" => "The full quickstart.",+ "last_updated_at" => "2026-09-01",+ },+ ],+ )+ end+it "uses one tool-free structured call to select sources and stream an answer" doSiteSetting.ai_ask_ai_related_count = 4feature_name = nil@@ -136,13 +204,13 @@ describe DiscourseAi::Discoveries::Synthesis do{"source_ref" => "source_1","title" => "Create a Discourse plugin",- "url" => "/t/create-a-plugin/123",+ "url" => "/t/123","username" => "sam","passages" => [{ "post_number" => 1, "excerpt" => "Start with the plugin skeleton." },{ "post_number" => 10, "excerpt" => "Then register the plugin." },],- "created" => "2026-08-01T09:00:00.000000Z",+ "created" => "2026-08-01","category" => "Documentation > Developer Guides","likes" => 12,"topic_views" => 300,@@ -150,8 +218,7 @@ describe DiscourseAi::Discoveries::Synthesis do"topic_replies" => 8,"tags" => "plugin, development","author_is_staff" => true,- "is_topic_op" => true,- "last_updated_at" => "2026-08-12T10:30:00.000000Z",+ "last_updated_at" => "2026-08-12",},],)