A platform for community discussion. Free, open, simple.
FEATURE: Add a combined AI and keyword search with `enable_ai_combined_search` (#44189)
Kris121f4209defe
diff --git a/db/structure.sql b/db/structure.sqlindex b1149b4b0a3..6672cd5cacf 100644--- a/db/structure.sql+++ b/db/structure.sql@@ -1443,7 +1443,19 @@ CREATE TABLE public.ask_ai_logs (asked_at timestamp(6) without time zone NOT NULL,time_to_first_answer_ms integer,created_at timestamp(6) without time zone NOT NULL,- updated_at timestamp(6) without time zone NOT NULL+ updated_at timestamp(6) without time zone NOT NULL,+ ask_trigger character varying DEFAULT ''::character varying NOT NULL,+ ask_trigger_reason character varying DEFAULT ''::character varying NOT NULL,+ ask_intent_heuristic character varying DEFAULT ''::character varying NOT NULL,+ ask_intent_model character varying DEFAULT ''::character varying NOT NULL,+ ask_intent_probability double precision DEFAULT 0.0 NOT NULL,+ ask_tab_opened character varying DEFAULT ''::character varying NOT NULL,+ ask_tab_opened_reason character varying DEFAULT ''::character varying NOT NULL,+ ask_tab_switched_to character varying DEFAULT ''::character varying NOT NULL,+ ask_tab_switched_after_ms integer DEFAULT 0 NOT NULL,+ ask_scope character varying DEFAULT ''::character varying NOT NULL,+ ask_scope_model character varying DEFAULT ''::character varying NOT NULL,+ ask_scope_probability double precision DEFAULT 0.0 NOT NULL);@@ -26093,6 +26105,9 @@ SET search_path TO "$user", public;INSERT INTO "schema_migrations" (version) VALUES('20261020161054'),('20261020160221'),+('20261019232249'),+('20261019213757'),+('20261019213116'),('20261019105124'),('20261019104439'),('20261018172000'),@@ -26119,6 +26134,7 @@ INSERT INTO "schema_migrations" (version) VALUES('20261006113418'),('20261005091527'),('20261005091447'),+('20261002200029'),('20261001073226'),('20260930114733'),('20260928103925'),diff --git a/frontend/discourse/app/components/search-menu.gjs b/frontend/discourse/app/components/search-menu.gjsindex 59ff91c4e3e..d9620124417 100644--- a/frontend/discourse/app/components/search-menu.gjs+++ b/frontend/discourse/app/components/search-menu.gjs@@ -26,7 +26,10 @@ import {searchTermScopesToPMs,updateRecentSearches,} from "discourse/lib/search";-import { applyValueTransformer } from "discourse/lib/transformer";+import {+ applyBehaviorTransformer,+ applyValueTransformer,+} from "discourse/lib/transformer";import DiscourseURL from "discourse/lib/url";import userSearch from "discourse/lib/user-search";import { and } from "discourse/truth-helpers";@@ -256,9 +259,15 @@ export default class SearchMenu extends Component {clearSearch(e) {e.stopPropagation();e.preventDefault();- this.search.activeGlobalSearchTerm = "";- this.search.focusSearchInput();- this.triggerSearch();+ applyBehaviorTransformer(+ "search-menu-clear-search",+ () => {+ this.search.activeGlobalSearchTerm = "";+ this.search.focusSearchInput();+ this.triggerSearch();+ },+ { location: this.args.location }+ );}@actiondiff --git a/frontend/discourse/app/lib/registry/transformers.ts b/frontend/discourse/app/lib/registry/transformers.tsindex 35a276d1ac2..c6ca07a6f0b 100644--- a/frontend/discourse/app/lib/registry/transformers.ts+++ b/frontend/discourse/app/lib/registry/transformers.ts@@ -26,6 +26,7 @@ export const BEHAVIOR_TRANSFORMERS = Object.freeze(["post-stream-suggested-topics","post-stream-update-from-json","search-menu-clear-recent-searches",+ "search-menu-clear-search","topic-controller:finished-editing","topic-list-item-click","topic-update-from-json",diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/discover/discoveries_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/discover/discoveries_controller.rbindex d80d6c4c222..87e3c9cc4c1 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/discover/discoveries_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/discover/discoveries_controller.rb@@ -5,37 +5,65 @@ module DiscourseAiclass DiscoveriesController < ::ApplicationControllerinclude AiCreditLimitHandler- MAX_QUERY_LENGTH = 1000-requires_plugin PLUGIN_NAMErequires_loginbefore_action :check_permissions!+ before_action :rate_limit_ask, only: :reply, if: -> { params[:request_id].present? }def replyreturn legacy_reply if params[:request_id].blank?- query = normalized_query- request_id = params[:request_id].to_s- if !DiscourseAi::Discoveries.valid_request_id?(request_id)- render_json_error(- I18n.t("discourse_ai.ai_bot.discoveries.errors.invalid_request_id"),- status: :bad_request,- )- return+ DiscourseAi::AskAi::Ask.call(service_params) do |result|+ on_success do+ render json: DiscourseAi::AskAi::AskSerializer.new(result, root: false).as_json+ end+ on_failed_contract do |contract|+ if contract.errors.include?(:query)+ raise Discourse::InvalidParameters.new(invalid_query_message)+ elsif contract.errors.include?(:request_id)+ render_json_error(+ I18n.t("discourse_ai.ai_bot.discoveries.errors.invalid_request_id"),+ status: :bad_request,+ )+ else+ raise Discourse::InvalidParameters.new(:scope)+ end+ end+ on_failed_policy(:can_ask_ai) { raise Discourse::InvalidAccess }+ on_failed_policy(:request_id_matches_query) do+ render_json_error(+ I18n.t("discourse_ai.ai_bot.discoveries.errors.request_conflict"),+ status: :conflict,+ )+ end+ # a step that raised, such as the answer job failing to queue+ on_failure do+ Rails.logger.error("Discourse AI ask failed\n#{result.inspect_steps}")+ render_json_error(+ I18n.t("discourse_ai.ai_bot.discoveries.errors.processing_failed"),+ status: :unprocessable_entity,+ )+ endend+ end- RateLimiter.new(current_user, "ai_discover_#{current_user.id}", 8, 1.minute).performed!-- DiscourseAi::Discoveries.enqueue_reply(user: current_user, request_id:, query:)-- DiscourseAi::Discoveries.record_recent_ask(user_id: current_user.id, query:)+ def record_tabs+ RateLimiter.new(+ current_user,+ "ai_discover_tabs_#{current_user.id}",+ 60,+ 1.minute,+ ).performed!- render json: { request_id: }, status: :ok- rescue DiscourseAi::Discoveries::RequestConflict- render_json_error(- I18n.t("discourse_ai.ai_bot.discoveries.errors.request_conflict"),- status: :conflict,+ DiscourseAi::Discoveries.record_tabs(+ user: current_user,+ ask_log_id: params.require(:ask_log_id),+ opened: params[:tab_opened].presence,+ opened_reason: params[:tab_opened_reason].to_s,+ switched_to: params[:tab_switched_to].presence,+ switched_after_ms: params[:tab_switched_after_ms],)+ head :no_contentenddef recent@@ -85,7 +113,7 @@ module DiscourseAiendquery = normalized_query- RateLimiter.new(current_user, "ai_discover_#{current_user.id}", 8, 1.minute).performed!+ rate_limit_askJobs.enqueue(:stream_discover_reply, user_id: current_user.id, query:)render json: {}, status: :okend@@ -156,22 +184,34 @@ module DiscourseAienddef ask_ai_action?- %w[recent clear_recent].include?(action_name) || params[:request_id].present?+ %w[recent clear_recent record_tabs].include?(action_name) || params[:request_id].present?+ end++ def rate_limit_ask+ RateLimiter.new(+ current_user,+ "ai_discover_#{current_user.id}",+ DiscourseAi::Discoveries::ASK_RATE_LIMIT,+ 1.minute,+ ).performed!enddef normalized_queryquery = params[:query].to_s.strip- if query.blank? || query.length > MAX_QUERY_LENGTH || query.include?("\0")- raise Discourse::InvalidParameters.new(- I18n.t(- "discourse_ai.ai_bot.discoveries.errors.invalid_query",- max: MAX_QUERY_LENGTH,- ),- )+ if query.blank? || query.length > DiscourseAi::Discoveries::MAX_QUERY_LENGTH ||+ query.include?("\0")+ raise Discourse::InvalidParameters.new(invalid_query_message)endqueryend++ def invalid_query_message+ I18n.t(+ "discourse_ai.ai_bot.discoveries.errors.invalid_query",+ max: DiscourseAi::Discoveries::MAX_QUERY_LENGTH,+ )+ endendendenddiff --git a/plugins/discourse-ai/app/jobs/regular/stream_discover_reply.rb b/plugins/discourse-ai/app/jobs/regular/stream_discover_reply.rbindex 9fb71d949de..2d827edc070 100644--- a/plugins/discourse-ai/app/jobs/regular/stream_discover_reply.rb+++ b/plugins/discourse-ai/app/jobs/regular/stream_discover_reply.rb@@ -67,19 +67,34 @@ module Jobsendreturn if !active_request?(user, args[:request_id])+ if !rewritten_queries.failed &&+ (rewritten_queries.keyword_query != query || rewritten_queries.semantic_query != query)+ publish_update(+ user,+ base.merge(+ done: false,+ phase: "rewritten",+ keyword_query: rewritten_queries.keyword_query,+ semantic_query: rewritten_queries.semantic_query,+ ),+ )+ end+stage = :retrievalretrieval = DiscourseAi::Discoveries::Retrieval.new(user:)- retrieval_result =- retrieval.call(- query,- keyword_query: rewritten_queries.keyword_query,- semantic_query: rewritten_queries.semantic_query,- )+ retrieval_options = {+ keyword_query: rewritten_queries.keyword_query,+ semantic_query: rewritten_queries.semantic_query,+ }+ scope_filter = DiscourseAi::Discoveries.scope_filter(args[:scope])+ retrieval_options[:scope_filter] = scope_filter if scope_filter+ retrieval_result = retrieval.call(query, **retrieval_options)if cancel_manager.cancelled? || Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadlinereturnendreturn if !active_request?(user, args[:request_id])ask_log.candidate_post_ids = retrieval_result.synthesis_candidates.pluck("post_id") if ask_log+ base = base.merge(scope_fallback: true) if retrieval_result.scope_fallbackif retrieval_result.synthesis_candidates.empty?ask_log.ask_outcome = :no_answer if ask_logpublish_no_answer(user, base)@@ -88,7 +103,7 @@ module Jobscandidate_topic_ids = retrieval_result.candidates.pluck("topic_id").uniqstage = :synthesis- result_settings = request_result_settings(args)+ result_settings = request_result_settings(args, user)synthesis =DiscourseAi::Discoveries::Synthesis.new(user:, ai_agent:, llm_model:, cancel_manager:)selected_sources = nil@@ -329,8 +344,8 @@ module Jobs)end- def request_result_settings(args)- configured_settings = DiscourseAi::Discoveries.result_settings+ def request_result_settings(args, user)+ configured_settings = DiscourseAi::Discoveries.result_settings(user)summary_detail = args[:summary_detail].to_s.to_symif !DiscourseAi::Discoveries::SUMMARY_DETAILS.include?(summary_detail)summary_detail = configured_settings[:summary_detail]@@ -436,6 +451,7 @@ module Jobscategory: source["category"].presence,category_id: source["category_id"],topic_replies: source["topic_replies"].to_i,+ bumped_at: source["topic_bumped_at"],username: source["username"],name: SiteSetting.enable_names? ? source["name"] : nil,avatar_template: source["avatar_template"],diff --git a/plugins/discourse-ai/app/models/ask_ai_log.rb b/plugins/discourse-ai/app/models/ask_ai_log.rbindex d607c657ecd..952e8c895a3 100644--- a/plugins/discourse-ai/app/models/ask_ai_log.rb+++ b/plugins/discourse-ai/app/models/ask_ai_log.rb@@ -10,6 +10,18 @@ class AskAiLog < ActiveRecord::Basewhere.not(user_id: GroupUser.where(group_id: group_ids).select(:user_id))end+ # The search's scope and the decision model's verdicts, kept beside the+ # heuristic's guess so the two can be compared.+ def record_judgement!(scope:, judgement:)+ update!(+ ask_scope: scope.to_s,+ ask_intent_model: judgement&.intent.to_s,+ ask_intent_probability: judgement&.probability || 0,+ ask_scope_model: judgement&.scope.to_s,+ ask_scope_probability: judgement&.scope_probability || 0,+ )+ end+enum :ask_outcome, { answered: 0, no_answer: 1, failed: 2, cancelled: 3 }, prefix: trueenum :failure_stage, { rewrite: 0, retrieval: 1, synthesis: 2 }, prefix: trueend@@ -18,23 +30,35 @@ end## Table name: ask_ai_logs#-# id :bigint not null, primary key-# answer :text-# answer_title :text-# ask_outcome :integer-# asked_at :datetime not null-# candidate_post_ids :bigint default([]), not null, is an Array-# failure_stage :integer-# keyword_query :text-# query :text not null-# query_locale :string-# semantic_query :text-# source_post_ids :bigint default([]), not null, is an Array-# suggested_follow_up :text-# time_to_first_answer_ms :integer-# created_at :datetime not null-# updated_at :datetime not null-# user_id :bigint not null+# id :bigint not null, primary key+# answer :text+# answer_title :text+# ask_intent_heuristic :string default(""), not null+# ask_intent_model :string default(""), not null+# ask_intent_probability :float default(0.0), not null+# ask_outcome :integer+# ask_scope :string default(""), not null+# ask_scope_model :string default(""), not null+# ask_scope_probability :float default(0.0), not null+# ask_tab_opened :string default(""), not null+# ask_tab_opened_reason :string default(""), not null+# ask_tab_switched_after_ms :integer default(0), not null+# ask_tab_switched_to :string default(""), not null+# ask_trigger :string default(""), not null+# ask_trigger_reason :string default(""), not null+# asked_at :datetime not null+# candidate_post_ids :bigint default([]), not null, is an Array+# failure_stage :integer+# keyword_query :text+# query :text not null+# query_locale :string+# semantic_query :text+# source_post_ids :bigint default([]), not null, is an Array+# suggested_follow_up :text+# time_to_first_answer_ms :integer+# created_at :datetime not null+# updated_at :datetime not null+# user_id :bigint not null## Indexes#diff --git a/plugins/discourse-ai/app/serializers/discourse_ai/ask_ai/ask_serializer.rb b/plugins/discourse-ai/app/serializers/discourse_ai/ask_ai/ask_serializer.rbnew file mode 100644index 00000000000..a67a5b3d7af--- /dev/null+++ b/plugins/discourse-ai/app/serializers/discourse_ai/ask_ai/ask_serializer.rb@@ -0,0 +1,39 @@+# frozen_string_literal: true++module DiscourseAi+ module AskAi+ # What the search needs from an ask to choose its tab, given the result of+ # `DiscourseAi::AskAi::Ask`.+ class AskSerializer < ApplicationSerializer+ attributes :request_id, :ask_log_id, :intent, :prefers_results++ def request_id+ object.params.request_id+ end++ def ask_log_id+ object.ask_log.id+ end++ def include_ask_log_id?+ object.ask_log.present?+ end++ def intent+ object.judgement.as_json+ end++ def include_intent?+ object.judgement.present?+ end++ def prefers_results+ true+ end++ def include_prefers_results?+ object.prefers_results.present?+ end+ end+ end+enddiff --git a/plugins/discourse-ai/app/services/discourse_ai/ask_ai/ask.rb b/plugins/discourse-ai/app/services/discourse_ai/ask_ai/ask.rbnew file mode 100644index 00000000000..50257ceefd2--- /dev/null+++ b/plugins/discourse-ai/app/services/discourse_ai/ask_ai/ask.rb@@ -0,0 +1,146 @@+# frozen_string_literal: true++module DiscourseAi+ module AskAi+ # Asks for an AI answer to a search, judging with the decision model which+ # tab the search should open on, and where the answer should look.+ class Ask+ include Service::Base++ params do+ attribute :query, :string+ attribute :request_id, :string+ attribute :scope, :string+ attribute :trigger, :string+ attribute :trigger_reason, :string+ attribute :intent_heuristic, :string+ attribute :classify_intent, :boolean, default: false++ # What started a search is only logged, so anything unexpected is+ # dropped rather than refused.++ before_validation do+ self.query = query.to_s.strip+ self.scope = scope.presence+ self.trigger = "" if Discoveries::TRIGGERS.exclude?(trigger)+ self.trigger_reason = trigger_reason.to_s+ self.trigger_reason = "" if !trigger_reason.match?(Discoveries::TRIGGER_REASON_PATTERN)+ self.intent_heuristic = "" if Discoveries::Intent::CHOICES.exclude?(intent_heuristic)+ end++ validates :query,+ presence: true,+ length: {+ maximum: Discoveries::MAX_QUERY_LENGTH,+ },+ format: {+ without: /\0/,+ }+ validates :request_id, format: { with: Discoveries::REQUEST_ID_PATTERN }+ validates :scope, format: { with: Discoveries::SCOPE_PATTERN }, allow_nil: true++ # what the answer job and its log are given+ def answer_attributes+ slice(:request_id, :query, :trigger, :trigger_reason, :intent_heuristic)+ end+ end++ policy :can_ask_ai+ model :request_status, :claim_request+ policy :request_id_matches_query++ only_if(:new_request?) do+ # A scoped search is judged before its answer is queued, so the answer+ # can leave a scope the search is not about.+ only_if(:scoped?) do+ model :judgement, :judge_scoped_search, optional: true+ model :ask_log, :queue_scoped_answer+ end++ # Otherwise it is judged once the answer is queued, so it does not hold+ # the answer up.+ only_if(:site_wide?) do+ model :ask_log, :queue_answer+ model :judgement, :judge_search, optional: true+ end++ only_if(:judgement_worth_logging?) { step :log_judgement }+ end++ step :remember_ask+ model :prefers_results, :look_up_reading_preference, optional: true++ private++ def can_ask_ai(guardian:)+ Discoveries.enabled_for_user?(guardian.user)+ end++ def claim_request(params:, guardian:)+ Discoveries.bind_request(+ user_id: guardian.user.id,+ request_id: params.request_id,+ query: params.query,+ )+ end++ def request_id_matches_query(request_status:)+ request_status != :conflict+ end++ def new_request?(request_status:)+ request_status == :created+ end++ def scoped?(params:)+ params.scope.present?+ end++ def judge_scoped_search(params:, guardian:)+ Discoveries::Intent.for(guardian.user, requested: params.classify_intent)&.call(+ params.query,+ scope: params.scope,+ )+ end++ def queue_scoped_answer(params:, guardian:, judgement:)+ Discoveries.enqueue_reply(+ user: guardian.user,+ scope: judgement&.about_site? ? nil : params.scope,+ **params.answer_attributes,+ )+ end++ def site_wide?(params:)+ params.scope.blank?+ end++ def queue_answer(params:, guardian:)+ Discoveries.enqueue_reply(user: guardian.user, **params.answer_attributes)+ end++ def judge_search(params:, guardian:)+ Discoveries::Intent.for(guardian.user, requested: params.classify_intent)&.call(+ params.query,+ )+ end++ def judgement_worth_logging?(params:, judgement:)+ params.scope.present? || judgement.present?+ end++ def log_judgement(ask_log:, params:, judgement:)+ ask_log.record_judgement!(scope: params.scope, judgement:)+ end++ def remember_ask(params:, guardian:)+ Discoveries.record_recent_ask(user_id: guardian.user.id, query: params.query)+ end++ def look_up_reading_preference(guardian:)+ Discoveries.combined_search_for?(guardian.user) &&+ Discoveries.prefers_results?(guardian.user)+ end+ end+ end+enddiff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search-discoveries.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search-discoveries.gjsindex a6853d78525..3ace79cba24 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search-discoveries.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search-discoveries.gjs@@ -23,82 +23,11 @@ import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";import dIcon from "discourse/ui-kit/helpers/d-icon";import dReplaceEmoji from "discourse/ui-kit/helpers/d-replace-emoji";import { i18n } from "discourse-i18n";-import { tagNames, tagSuggestionParams } from "../lib/ai-helper-suggestions.js";-import { showComposerAiHelper } from "../lib/show-ai-helper.js";+import { followUpAvailable } from "../lib/ai-follow-up.js";+import { openTopicFromQuery } from "../lib/open-topic-from-query.js";import AiBlinkingAnimation from "./ai-blinking-animation.gjs";import AiIndicatorWave from "./ai-indicator-wave.gjs";-async function requestSuggestion(url, data) {- try {- return await ajax(url, { type: "POST", data });- } catch {- return null;- }-}--async function enrichNewTopic({- model,- query,- suggestTitle,- suggestTaxonomy,- canTagTopics,- maxTitleLength,-}) {- const initialTitle = model.title;- const initialCategoryId = model.categoryId;- const initialTags = tagNames(model.tags);- const titleRequest = suggestTitle- ? requestSuggestion("/discourse-ai/ai-helper/suggest_title", {- text: query,- })- : null;- const categoryRequest = suggestTaxonomy- ? requestSuggestion("/discourse-ai/ai-helper/suggest_category", {- text: query,- })- : null;- const [titleResult, categoryResult] = await Promise.all([- titleRequest,- categoryRequest,- ]);-- const suggestedTitle = titleResult?.suggestions?.[0]?.trim();- if (suggestedTitle && model.title === initialTitle) {- model.set("title", suggestedTitle.slice(0, maxTitleLength));- }-- const suggestedCategory = categoryResult?.assistant?.[0];- if (suggestedCategory && model.categoryId === initialCategoryId) {- model.set("categoryId", suggestedCategory.id);- }-- if (- !suggestTaxonomy ||- !canTagTopics ||- tagNames(model.tags).join("\0") !== initialTags.join("\0")- ) {- return;- }-- const tagResult = await requestSuggestion(- "/discourse-ai/ai-helper/suggest_tags",- {- text: query,- ...tagSuggestionParams(model.categoryId, model.tags),- }- );- const suggestedTags = tagResult?.assistant- ?.map((tag) => tag.name)- .filter(Boolean);-- if (- suggestedTags?.length &&- tagNames(model.tags).join("\0") === initialTags.join("\0")- ) {- model.set("tags", suggestedTags);- }-}-export default class AiSearchDiscoveries extends Component {@service search;@service messageBus;@@ -216,28 +145,14 @@ export default class AiSearchDiscoveries extends Component {}get canContinueConversation() {- const agents = this.currentUser?.ai_enabled_agents;- if (!this.siteSettings.ai_bot_enabled || !agents) {- return false;- }-if (this.discobotDiscoveries.discoveryTimedOut) {return false;}- const followUpAgent = agents.find(- (agent) =>- agent.id === parseInt(this.siteSettings?.ai_ask_ai_follow_up_agent, 10)- );- const followUpAgentCanReceiveMessages =- followUpAgent?.allow_personal_messages &&- followUpAgent?.username &&- followUpAgent?.has_default_llm;-return ((this.discobotDiscoveries.discovery?.length > 0 || this.hasSources) &&!this.discobotDiscoveries.isStreaming &&- followUpAgentCanReceiveMessages+ followUpAvailable(this.siteSettings, this.currentUser));}@@ -252,11 +167,13 @@ export default class AiSearchDiscoveries extends Component {return this.discobotDiscoveries.suggestedFollowUp || "";}- // offered once an answer has settled, so it isn't what a screen reader meets- // while waiting for one+ // Offered once an answer has settled, so it isn't what a screen reader meets+ // while waiting for one. The combined search picks the tab itself, so there+ // is no default to set.get showAskAiDefaultToggle() {return (Boolean(this.currentUser) &&+ !this.siteSettings.enable_ai_combined_search &&!this.hasNoContent &&!this.discobotDiscoveries.loadingDiscoveries &&!this.discobotDiscoveries.isStreaming &&@@ -433,24 +350,11 @@ export default class AiSearchDiscoveries extends Component {this.args.closeSearchMenu();}- await this.composer.openNewTopic({ title: this.query });-- const model = this.composer.model;- const suggestionsEnabled = showComposerAiHelper(- model,- this.siteSettings,- this.currentUser,- "suggestions"- );-- await enrichNewTopic({- model,+ await openTopicFromQuery({+ composer: this.composer,+ currentUser: this.currentUser,query: this.query,- suggestTitle: suggestionsEnabled,- suggestTaxonomy:- suggestionsEnabled && this.siteSettings.ai_embeddings_enabled,- canTagTopics: this.currentUser.can_tag_topics,- maxTitleLength: this.siteSettings.max_topic_title_length,+ siteSettings: this.siteSettings,});}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-answer.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-answer.gjsnew file mode 100644index 00000000000..095b9246afd--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-answer.gjs@@ -0,0 +1,235 @@+import Component from "@glimmer/component";+import { tracked } from "@glimmer/tracking";+import { on } from "@ember/modifier";+import { action } from "@ember/object";+import { service } from "@ember/service";+import { popupAjaxError } from "discourse/lib/ajax-error";+import DiscourseURL from "discourse/lib/url";+import { and, not, or } from "discourse/truth-helpers";+import DButton from "discourse/ui-kit/d-button";+import DCookText from "discourse/ui-kit/d-cook-text";+import DInterpolatedTranslation from "discourse/ui-kit/d-interpolated-translation";+import DSkeleton from "discourse/ui-kit/d-skeleton";+import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";+import { i18n } from "discourse-i18n";+import { openTopicFromQuery } from "../../lib/open-topic-from-query.js";+import AiSearchTopicRow from "./ai-search-topic-row.gjs";++/**+ * The answer for the current combined search. It opens to its related topics+ * and a follow-up field, and asking a follow-up continues as a conversation+ * with the AI bot.+ */+export default class AiSearchAnswer extends Component {+ @service aiSearchSession;+ @service composer;+ @service currentUser;+ @service siteSettings;++ @tracked followUpValue = "";+ @tracked startingConversation = false;++ get session() {+ return this.aiSearchSession;+ }++ get discoveries() {+ return this.session.discoveries;+ }++ get answerFailureMessage() {+ if (this.discoveries.errorMessage) {+ return this.discoveries.errorMessage;+ }+ if (this.discoveries.discoveryTimedOut) {+ return i18n("discourse_ai.discobot_discoveries.timed_out");+ }+ return i18n("discourse_ai.discobot_discoveries.no_answer");+ }++ // nothing in the forum answered it, so the next step is asking people+ get canAskCommunity() {+ return (+ this.discoveries.answerable === false &&+ Boolean(this.currentUser?.can_create_topic)+ );+ }++ // added once the answer is complete, so they do not slide down as it streams+ get showMore() {+ return this.session.answerSettled && !this.session.answerFailed;+ }++ get suggestedFollowUp() {+ return this.discoveries.suggestedFollowUp || "";+ }++ @action+ async askCommunity(event) {+ event.preventDefault();+ this.args.onNavigate?.();+ await openTopicFromQuery({+ composer: this.composer,+ currentUser: this.currentUser,+ query: this.session.query,+ siteSettings: this.siteSettings,+ });+ }++ @action+ updateFollowUp(event) {+ this.followUpValue = event.target.value;+ }++ @action+ followLink(event) {+ if (event.target.closest("a")) {+ this.args.onNavigate?.();+ }+ }++ @action+ async startConversation(event) {+ event.preventDefault();+ const question = this.followUpValue.trim() || this.suggestedFollowUp;+ if (!question || this.startingConversation) {+ return;+ }++ this.startingConversation = true;+ try {+ const topicId = await this.session.startConversation(question);+ this.args.onNavigate?.();+ DiscourseURL.routeTo(`/t/${topicId}`);+ } catch (error) {+ popupAjaxError(error);+ } finally {+ this.startingConversation = false;+ }+ }++ <template>+ {{! eslint-disable-next-line ember/template-no-invalid-interactive }}+ <section+ aria-busy={{unless this.session.answerSettled "true"}}+ aria-label={{i18n "discourse_ai.ai_search.answer_label"}}+ class={{dConcatClass+ "ai-search-answer"+ (if @compact "--compact")+ (if this.session.answerFailed "--no-answer")+ }}+ {{on "click" this.followLink}}+ >+ <div class="ai-search-answer__body">+ <div class="ai-search-answer__content">+ {{#if+ (and+ this.session.scopeFallback+ this.session.fallbackScopeLabel+ (not this.session.answerFailed)+ )+ }}+ <p class="ai-search-answer__scope-note">+ {{i18n+ "discourse_ai.ai_search.scope.fallback"+ name=this.session.fallbackScopeLabel+ }}+ </p>+ {{/if}}+ {{#if this.discoveries.loadingDiscoveries}}+ <DSkeleton class="ai-search-answer__title-skeleton" @width="60%" />+ <DSkeleton @count={{3}} @lastLineWidth="45%" />+ {{else if this.canAskCommunity}}+ <p class="ai-search-answer__empty">+ <span>+ <DInterpolatedTranslation+ @key="discourse_ai.ai_search.no_answer"+ as |Placeholder|+ >+ <Placeholder @name="askLink">+ <a+ class="ai-search-answer__ask-community"+ href+ {{on "click" this.askCommunity}}+ >{{i18n "discourse_ai.ai_search.ask_community"}}</a>+ </Placeholder>+ </DInterpolatedTranslation>+ </span>+ </p>+ {{else if this.session.answerFailed}}+ <p class="ai-search-answer__empty">{{this.answerFailureMessage}}</p>+ {{else}}+ {{#if this.discoveries.discoveryTitle}}+ <h3 class="ai-search-answer__title">+ {{this.discoveries.discoveryTitle}}+ </h3>+ {{/if}}+ <DCookText+ class={{dConcatClass+ "cooked"+ (if this.discoveries.isStreaming "streaming")+ }}+ @rawText={{this.discoveries.streamedText}}+ />+ {{/if}}+ </div>+ </div>++ {{#if this.showMore}}+ <div class="ai-search-answer__more">+ <div class="ai-search-answer__related">+ <h3 class="ai-search-answer__related-heading">+ {{i18n "discourse_ai.ai_search.related_topics"}}+ </h3>+ <ul class="ai-search-answer__related-list">+ {{#each this.session.relatedTopics as |topic|}}+ <li class="ai-search-answer__related-item">+ <AiSearchTopicRow+ class="ai-search-answer__related-link"+ @bumpedAt={{topic.bumped_at}}+ @category={{topic.categoryModel}}+ @replyCount={{topic.topic_replies}}+ @title={{topic.title}}+ @url={{topic.url}}+ />+ </li>+ {{/each}}+ </ul>+ </div>++ {{#if this.session.canFollowUp}}+ <form+ class="ai-search-answer__follow-up"+ {{on "submit" this.startConversation}}+ >+ <input+ aria-label={{i18n "discourse_ai.ai_search.follow_up_label"}}+ class="ai-search-answer__follow-up-input"+ maxlength="1000"+ placeholder={{if+ this.suggestedFollowUp+ this.suggestedFollowUp+ (i18n "discourse_ai.ai_search.follow_up_placeholder")+ }}+ type="text"+ value={{this.followUpValue}}+ {{on "input" this.updateFollowUp}}+ />+ <DButton+ class="btn-primary ai-search-answer__follow-up-submit"+ @disabled={{or+ this.startingConversation+ (and (not this.followUpValue) (not this.suggestedFollowUp))+ }}+ @icon="paper-plane"+ @label="discourse_ai.ai_search.ask"+ @type="submit"+ />+ </form>+ {{/if}}+ </div>+ {{/if}}++ </section>+ </template>+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-conversation-references.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-conversation-references.gjsnew file mode 100644index 00000000000..b368a5c5428--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-conversation-references.gjs@@ -0,0 +1,18 @@+import Component from "@glimmer/component";+import { getOwner } from "@ember/owner";+import AiSearchCitedTopics from "../../lib/ai-search-cited-topics.js";+import AiSearchReferencesPanel from "./ai-search-references-panel.gjs";++/** A search conversation's cited topics, standing in for the timeline. */+export default class AiSearchConversationReferences extends Component {+ citedTopics = new AiSearchCitedTopics(getOwner(this), this.args.topic);++ willDestroy() {+ super.willDestroy(...arguments);+ this.citedTopics.teardown();+ }++ <template>+ <AiSearchReferencesPanel @references={{this.citedTopics.references}} />+ </template>+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-references-panel.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-references-panel.gjsnew file mode 100644index 00000000000..a3ef62aff7a--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-references-panel.gjs@@ -0,0 +1,56 @@+import Component from "@glimmer/component";+import { service } from "@ember/service";+import { gt } from "discourse/truth-helpers";+import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";+import { i18n } from "discourse-i18n";+import AiSearchTopicRow from "./ai-search-topic-row.gjs";++export default class AiSearchReferencesPanel extends Component {+ @service site;++ get references() {+ return (this.args.references || []).map((reference) => ({+ ...reference,+ category: this.site.categories?.find(+ (category) => category.id === reference.categoryId+ ),+ }));+ }++ <template>+ <aside+ aria-labelledby="ai-search-references-title"+ class="ai-search-references"+ ...attributes+ >+ <h3 class="ai-search-references__title" id="ai-search-references-title">+ {{i18n "discourse_ai.ai_search.references_title"}}+ </h3>++ <ol class="ai-search-references__list">+ {{#each this.references key="topicId" as |reference|}}+ <li+ class={{dConcatClass+ "ai-search-references__item"+ (if reference.isNew "--new")+ (if (gt reference.movement 0) "--up")+ }}+ >+ <AiSearchTopicRow+ class="ai-search-references__link"+ @bumpedAt={{reference.bumpedAt}}+ @category={{reference.category}}+ @replyCount={{reference.replyCount}}+ @title={{reference.title}}+ @url={{reference.url}}+ />+ </li>+ {{else}}+ <li class="ai-search-references__empty">+ {{i18n "discourse_ai.ai_search.references_empty"}}+ </li>+ {{/each}}+ </ol>+ </aside>+ </template>+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-tabs.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-tabs.gjsnew file mode 100644index 00000000000..3c3e27a7b5e--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-tabs.gjs@@ -0,0 +1,113 @@+import Component from "@glimmer/component";+import { concat, fn } from "@ember/helper";+import { on } from "@ember/modifier";+import { service } from "@ember/service";+import { eq } from "discourse/truth-helpers";+import DButton from "discourse/ui-kit/d-button";+import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";+import dIcon from "discourse/ui-kit/helpers/d-icon";+import dRovingFocus from "discourse/ui-kit/modifiers/d-roving-focus";+import { i18n } from "discourse-i18n";++/**+ * The kinds of result a combined search fetched, as pills that switch between+ * them. Each keyword tab says how much it found, so an empty one is plain+ * before it is opened.+ */+export default class AiSearchTabs extends Component {+ @service aiSearchSession;+ @service search;++ get session() {+ return this.aiSearchSession;+ }++ get tabs() {+ const tabs = [+ this.#keywordTab(+ "topics",+ i18n("discourse_ai.ai_search.tabs.topics"),+ this.session.topicsResults+ ),+ ];++ const scope = this.session.scope;+ if (scope) {+ tabs.push(+ this.#keywordTab("context", scope.tabLabel, this.session.contextResults)+ );+ }++ tabs.push({+ kind: "ask",+ icon: "far-discobot",+ label: i18n("discourse_ai.ai_search.tabs.ask"),+ empty: this.session.answerFailed,+ });++ return tabs.map((tab) => ({+ ...tab,+ selected: this.session.selectedTab === tab.kind,+ }));+ }++ get tabsKey() {+ return this.tabs.map((tab) => tab.kind).join();+ }++ #keywordTab(kind, label, results) {+ const found = results?.posts?.length ?? 0;+ return {+ kind,+ icon: "magnifying-glass",+ label,+ count: results+ ? `${found}${results.grouped_search_result?.more_posts ? "+" : ""}`+ : null,+ empty: Boolean(results) && found === 0,+ };+ }++ <template>+ <div+ aria-label={{i18n "discourse_ai.ai_search.tabs.label"}}+ class="ai-discoveries-search-options ai-search-tabs"+ role="tablist"+ {{on "keydown" this.search.handleArrowUpOrDown}}+ {{dRovingFocus+ orientation="horizontal"+ itemSelector=".ai-discoveries-search-options__option"+ entryFocus="first"+ itemsKey=this.tabsKey+ }}+ >+ {{#each this.tabs key="kind" as |tab|}}+ <DButton+ aria-selected={{if tab.selected "true" "false"}}+ class={{dConcatClass+ "btn-default btn-small ai-discoveries-search-options__option"+ (concat "--" tab.kind)+ (if tab.selected "is-active")+ (if tab.empty "--empty")+ }}+ data-search-menu-navigation-item+ role="tab"+ @action={{fn this.session.selectTab tab.kind}}+ @icon={{tab.icon}}+ @translatedLabel={{tab.label}}+ >+ {{#if (eq tab.kind "ask")}}+ {{#if tab.empty}}+ <span+ class="ai-search-tabs__empty"+ title={{i18n "discourse_ai.ai_search.tabs.no_answer"}}+ >{{dIcon "ban"}}</span>+ {{/if}}+ {{else if tab.count}}+ <span class="ai-search-tabs__count">{{tab.count}}</span>+ {{/if}}+ </DButton>+ {{/each}}+ </div>+ </template>+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-topic-row.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-topic-row.gjsnew file mode 100644index 00000000000..0187b897a1d--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search/ai-search-topic-row.gjs@@ -0,0 +1,41 @@+import { eq } from "discourse/truth-helpers";+import dCategoryLink from "discourse/ui-kit/helpers/d-category-link";+import dFormatDate from "discourse/ui-kit/helpers/d-format-date";+import dIcon from "discourse/ui-kit/helpers/d-icon";+import dNumber from "discourse/ui-kit/helpers/d-number";+import dReplaceEmoji from "discourse/ui-kit/helpers/d-replace-emoji";+import { i18n } from "discourse-i18n";++/**+ * A topic laid out like a row of a topic list: its title and last activity,+ * then its category and reply count.+ */+const AiSearchTopicRow = <template>+ <a class="ai-search-topic-row" href={{@url}} ...attributes>+ <span class="ai-search-topic-row__title">{{dReplaceEmoji @title}}</span>+ {{#if @bumpedAt}}+ <span class="ai-search-topic-row__date">+ {{dFormatDate @bumpedAt format="tiny"}}+ </span>+ {{/if}}+ {{#if @category}}+ <span class="ai-search-topic-row__category">+ {{dCategoryLink @category link=false}}+ </span>+ {{/if}}+ {{#unless (eq @replyCount undefined)}}+ <span+ class="ai-search-topic-row__replies"+ title={{i18n+ "discourse_ai.ai_search.related_replies"+ count=@replyCount+ }}+ >+ {{dIcon "reply"}}+ {{dNumber @replyCount}}+ </span>+ {{/unless}}+ </a>+</template>;++export default AiSearchTopicRow;diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/modal/ai-search-references-modal.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/ai-search-references-modal.gjsnew file mode 100644index 00000000000..da65ad8c2f2--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/ai-search-references-modal.gjs@@ -0,0 +1,20 @@+import DModal from "discourse/ui-kit/d-modal";+import { i18n } from "discourse-i18n";+import AiSearchReferencesPanel from "../ai-search/ai-search-references-panel.gjs";++const AiSearchReferencesModal = <template>+ <DModal+ class="ai-search-references-modal"+ @closeModal={{@closeModal}}+ @title={{i18n "discourse_ai.ai_search.references_title"}}+ >+ <:body>+ <AiSearchReferencesPanel+ class="--in-modal"+ @references={{@model.citedTopics.references}}+ />+ </:body>+ </DModal>+</template>;++export default AiSearchReferencesModal;diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-bottom/ai-quick-search.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-bottom/ai-quick-search.gjsindex 5b4bcd93dad..8a4b0283920 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-bottom/ai-quick-search.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-bottom/ai-quick-search.gjs@@ -9,12 +9,16 @@ import { MODIFIER_REGEXP } from "discourse/components/search-menu";import { ajax } from "discourse/lib/ajax";import { popupAjaxError } from "discourse/lib/ajax-error";import { isValidSearchTerm, translateResults } from "discourse/lib/search";+import { combinedSearchActive } from "../../lib/ai-search-scope.js";const MAX_RESULTS_FOR_ADDING_AI = 3;export default class AiQuickSearch extends Component {- static shouldRender(args, { siteSettings }) {- return siteSettings.ai_embeddings_semantic_quick_search_enabled;+ static shouldRender(args, { siteSettings, currentUser }) {+ return (+ siteSettings.ai_embeddings_semantic_quick_search_enabled &&+ !combinedSearchActive(siteSettings, currentUser)+ );}@service appEvents;diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-top/ai-discobot-discoveries.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-top/ai-discobot-discoveries.gjsindex 342af8219a7..99c2eaaa9a5 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-top/ai-discobot-discoveries.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-top/ai-discobot-discoveries.gjs@@ -1,9 +1,16 @@import Component from "@glimmer/component";+import { cancel, next } from "@ember/runloop";import { service } from "@ember/service";+import { modifier } from "ember-modifier";import { eq } from "discourse/truth-helpers";+import DSkeleton from "discourse/ui-kit/d-skeleton";import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";+import { i18n } from "discourse-i18n";import AiDiscoveriesSearchOptions from "../../components/ai-discoveries-search-options.gjs";+import AiSearchAnswer from "../../components/ai-search/ai-search-answer.gjs";+import AiSearchTabs from "../../components/ai-search/ai-search-tabs.gjs";import AiSearchDiscoveries from "../../components/ai-search-discoveries.gjs";+import { contextScope } from "../../lib/ai-search-scope.js";export default class AiDiscobotDiscoveries extends Component {static shouldRender(args, { siteSettings, currentUser }) {@@ -15,7 +22,61 @@ export default class AiDiscobotDiscoveries extends Component {);}+ @service aiSearchSession;@service discobotDiscoveries;+ @service search;+ @service siteSettings;++ // A search can start from somewhere other than a key press, such as a recent+ // search picked from the menu, so the session is given the menu's own way to+ // show topics.+ connectMenu = modifier(() => {+ this.aiSearchSession.showTopicsInMenu = () =>+ this.args.outletArgs.updateTypeFilter(null);+ return () => (this.aiSearchSession.showTopicsInMenu = null);+ });++ // These are deferred because what they set off updates state this render+ // has already read.+ guardResults = modifier((element, [results]) => {+ if (results) {+ const timer = next(() => this.aiSearchSession.guardMenuResults());+ return () => cancel(timer);+ }+ });++ followScope = modifier((element, [scope]) => {+ const timer = next(() => this.aiSearchSession.updateContextScope(scope));+ return () => cancel(timer);+ });++ get combinedSearch() {+ return this.siteSettings.enable_ai_combined_search;+ }++ get currentScope() {+ return contextScope(this.search, {+ inPMInboxContext: this.args.outletArgs.inPMInboxContext,+ });+ }++ get term() {+ return this.args.outletArgs.searchTerm?.trim() ?? "";+ }++ // tabs and results belong to a search that has run, not to the typing+ // before it, which shows the menu's own suggestions+ get searched() {+ return this.aiSearchSession.isActiveFor(this.term);+ }++ get tab() {+ return this.aiSearchSession.selectedTab;+ }++ get showNoKeywordMatches() {+ return this.aiSearchSession.noKeywordMatches;+ }get shouldShow() {return (@@ -33,36 +94,68 @@ export default class AiDiscobotDiscoveries extends Component {}<template>- {{! rendered from here rather than its own connector so the options always+ {{#if this.combinedSearch}}+ <span+ hidden+ {{this.connectMenu}}+ {{this.followScope this.currentScope}}+ {{this.guardResults this.search.results}}+ ></span>+ {{#if this.searched}}+ <AiSearchTabs />++ {{#if (eq this.tab "ask")}}+ <div class="ai-search-menu-answer">+ <AiSearchAnswer+ @compact={{true}}+ @onNavigate={{@outletArgs.closeSearchMenu}}+ />+ </div>+ {{else if (eq this.tab null)}}+ {{! until the keyword searches are in and a tab is chosen }}+ <div class="ai-search-menu-loading">+ <DSkeleton @count={{3}} @lastLineWidth="70%" />+ </div>+ {{/if}}++ {{#if this.showNoKeywordMatches}}+ <div class="no-results">+ {{i18n "discourse_ai.ai_search.no_keyword_matches"}}+ </div>+ {{/if}}+ {{/if}}+ {{else}}+ {{! rendered from here rather than its own connector so the options alwayslead the answer, whatever order connectors resolve in }}- <AiDiscoveriesSearchOptions- @clearPMInboxContext={{@outletArgs.clearPMInboxContext}}- @clearTopicContext={{@outletArgs.clearTopicContext}}- @inPMInboxContext={{@outletArgs.inPMInboxContext}}- @openAdvancedSearch={{@outletArgs.openAdvancedSearch}}- @searchTermChanged={{@outletArgs.searchTermChanged}}- @searchTopics={{@outletArgs.searchTopics}}- @triggerSearch={{@outletArgs.triggerSearch}}- @updateTypeFilter={{@outletArgs.updateTypeFilter}}- />-- {{#if this.shouldShow}}- <div- class={{dConcatClass- "ai-discobot-discoveries"- (if this.isGenerating "is-generating")- (if this.discobotDiscoveries.sources.length "has-sources")- (if (eq this.discobotDiscoveries.answerable false) "has-no-answer")- }}- >- <AiSearchDiscoveries- @closeSearchMenu={{@outletArgs.closeSearchMenu}}- @searchTerm={{@outletArgs.searchTerm}}- @showHeading={{true}}- @showSources={{true}}- @triggerOnInsert={{false}}- />- </div>+ <AiDiscoveriesSearchOptions+ @clearPMInboxContext={{@outletArgs.clearPMInboxContext}}+ @clearTopicContext={{@outletArgs.clearTopicContext}}+ @inPMInboxContext={{@outletArgs.inPMInboxContext}}+ @openAdvancedSearch={{@outletArgs.openAdvancedSearch}}+ @searchTermChanged={{@outletArgs.searchTermChanged}}+ @searchTopics={{@outletArgs.searchTopics}}+ @triggerSearch={{@outletArgs.triggerSearch}}+ @updateTypeFilter={{@outletArgs.updateTypeFilter}}+ />++ {{#if this.shouldShow}}+ <div+ class={{dConcatClass+ "ai-discobot-discoveries"+ (if this.isGenerating "is-generating")+ (if this.discobotDiscoveries.sources.length "has-sources")+ (if (eq this.discobotDiscoveries.answerable false) "has-no-answer")+ }}+ >+ <AiSearchDiscoveries+ @closeSearchMenu={{@outletArgs.closeSearchMenu}}+ @searchTerm={{@outletArgs.searchTerm}}+ @showHeading={{true}}+ @showSources={{true}}+ @triggerOnInsert={{false}}+ />+ </div>+ {{/if}}{{/if}}</template>}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-type-top/ai-quick-search-notice.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-type-top/ai-quick-search-notice.gjsindex a30a381ef8e..a8a1a18c770 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-type-top/ai-quick-search-notice.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/search-menu-results-type-top/ai-quick-search-notice.gjs@@ -3,11 +3,13 @@ import { tracked } from "@glimmer/tracking";import { service } from "@ember/service";import { modifier } from "ember-modifier";import { i18n } from "discourse-i18n";+import { combinedSearchActive } from "../../lib/ai-search-scope.js";export default class AiQuickSearchNotice extends Component {- static shouldRender(args, { siteSettings }) {+ static shouldRender(args, { siteSettings, currentUser }) {return (siteSettings.ai_embeddings_semantic_quick_search_enabled &&+ !combinedSearchActive(siteSettings, currentUser) &&args.resultType?.type === "topic");}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-map-participants-after/ai-search-conversation-references.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-map-participants-after/ai-search-conversation-references.gjsnew file mode 100644index 00000000000..cc46cf3eaf9--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-map-participants-after/ai-search-conversation-references.gjs@@ -0,0 +1,69 @@+import Component from "@glimmer/component";+import { action } from "@ember/object";+import { getOwner } from "@ember/owner";+import { service } from "@ember/service";+import { modifier } from "ember-modifier";+import TrackedMediaQuery from "discourse/lib/tracked-media-query";+import DButton from "discourse/ui-kit/d-button";+import AiSearchReferencesModal from "../../components/modal/ai-search-references-modal.gjs";+import AiSearchCitedTopics from "../../lib/ai-search-cited-topics.js";+import { combinedSearchActive } from "../../lib/ai-search-scope.js";++// matches the width below which core shows no timeline, and so no cited topics+const TIMELINE_MIN_WIDTH = 925;++/**+ * Opens a search conversation's cited topics where the timeline, which lists+ * them otherwise, does not fit.+ */+export default class AiSearchConversationReferencesButton extends Component {+ static shouldRender(args, { siteSettings, currentUser }) {+ return (+ Boolean(args.topic?.ai_search_conversation) &&+ combinedSearchActive(siteSettings, currentUser)+ );+ }++ @service modal;+ @service site;++ // loaded once the button shows, so the modal opens on the full list+ loadCitedTopics = modifier(() => {+ this.#citedTopics ??= new AiSearchCitedTopics(+ getOwner(this),+ this.args.outletArgs.topic+ );+ });+ #citedTopics = null;++ #wideQuery = new TrackedMediaQuery(`(min-width: ${TIMELINE_MIN_WIDTH}px)`);++ willDestroy() {+ super.willDestroy(...arguments);+ this.#wideQuery.teardown();+ this.#citedTopics?.teardown();+ }++ get timelineFits() {+ return !this.site.mobileView && this.#wideQuery.matches;+ }++ @action+ showReferences() {+ this.modal.show(AiSearchReferencesModal, {+ model: { citedTopics: this.#citedTopics },+ });+ }++ <template>+ {{#unless this.timelineFits}}+ <DButton+ class="btn-default btn-small ai-search-references-button"+ @action={{this.showReferences}}+ @icon="far-file-lines"+ @label="discourse_ai.ai_search.references_title"+ {{this.loadCitedTopics}}+ />+ {{/unless}}+ </template>+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-navigation/ai-search-conversation-references.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-navigation/ai-search-conversation-references.gjsnew file mode 100644index 00000000000..2da40bb3cec--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-navigation/ai-search-conversation-references.gjs@@ -0,0 +1,19 @@+import Component from "@glimmer/component";+import AiSearchConversationReferences from "../../components/ai-search/ai-search-conversation-references.gjs";+import { combinedSearchActive } from "../../lib/ai-search-scope.js";++// rendered only where the timeline is, which it stands in for+export default class AiSearchConversationReferencesConnector extends Component {+ static shouldRender(args, { siteSettings, currentUser }) {+ return (+ Boolean(args.topic?.ai_search_conversation) &&+ combinedSearchActive(siteSettings, currentUser)+ );+ }++ <template>+ {{#if @outletArgs.renderTimeline}}+ <AiSearchConversationReferences @topic={{@outletArgs.topic}} />+ {{/if}}+ </template>+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-search-discoveries.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-search-discoveries.jsindex 1f3d7850786..9d144a54ae5 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-search-discoveries.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-search-discoveries.js@@ -4,6 +4,7 @@ import { DEFAULT_TYPE_FILTER } from "discourse/components/search-menu";import { SEARCH_TYPE_DEFAULT } from "discourse/controllers/full-page-search";import { apiInitializer } from "discourse/lib/api";import { i18n } from "discourse-i18n";+import { contextPlace } from "../lib/ai-search-scope.js";import { SEARCH_TYPE_ASK_AI } from "../lib/full-page-search-types.js";import { isScopedSearch } from "../lib/search-discoveries-context.js";import shortcutLabel from "../lib/shortcut-label.js";@@ -39,16 +40,54 @@ export default apiInitializer((api) => {const asksByDefault = () =>Boolean(get(currentUser, "user_option.ai_ask_ai_default"));- api.addQuickSearchRandomTip({- label: shortcutLabel("shift", "enter"),- get description() {- return i18n(- asksByDefault()- ? "discourse_ai.discobot_discoveries.tip_search"- : "discourse_ai.discobot_discoveries.tip_ask"- );- },- });+ // enter fetches every kind of result at once and opens on the likeliest,+ // so nothing in the menu has to be picked before searching+ const combinedSearch = settings.enable_ai_combined_search;+ const aiSearchSession = api.container.lookup("service:ai-search-session");++ const startCombined = (query, { trigger }) => {+ // the session runs the keyword searches, so the menu is switched to+ // showing topics+ aiSearchSession.showTopicsInMenu?.();+ aiSearchSession.start(query, aiSearchSession.contextScope, {+ trigger,+ });+ };++ if (combinedSearch) {+ aiSearchSession.subscribe();++ const contextKey = () => contextPlace(search.searchContext)?.key ?? null;+ let lastContextKey = contextKey();++ api.onPageChange(() => {+ aiSearchSession.forgetSearches();++ // A search belongs to where it was made. Somewhere else it would run+ // again in the new scope the moment the menu opened, so it is cleared.+ const key = contextKey();+ if (key !== lastContextKey) {+ lastContextKey = key;+ aiSearchSession.reset();+ search.activeGlobalSearchTerm = "";+ search.noResults = false;+ search.results = {};+ }+ });+ }++ if (!combinedSearch) {+ api.addQuickSearchRandomTip({+ label: shortcutLabel("shift", "enter"),+ get description() {+ return i18n(+ asksByDefault()+ ? "discourse_ai.discobot_discoveries.tip_search"+ : "discourse_ai.discobot_discoveries.tip_ask"+ );+ },+ });+ }// Asking is offered on /search the way users and categories are: a type of// its own, which owns the results area while it is selected.@@ -97,7 +136,7 @@ export default apiInitializer((api) => {}const query = search.activeGlobalSearchTerm?.trim();- if (query && discobotDiscoveries.lastQuery === query) {+ if (!combinedSearch && query && discobotDiscoveries.lastQuery === query) {value.set("search_type", SEARCH_TYPE_ASK_AI);}@@ -149,7 +188,7 @@ export default apiInitializer((api) => {api.registerValueTransformer("search-advanced-icon-enabled",({ value, context }) =>- offersDiscoveries(context?.location) ? false : value+ !combinedSearch && offersDiscoveries(context?.location) ? false : value);// Once a term has been asked, the indexed results stay behind their option,@@ -163,6 +202,17 @@ export default apiInitializer((api) => {}const query = search.activeGlobalSearchTerm?.trim();++ // Once a search runs, the list is the keyword tab on screen, so the+ // answer tab has the menu to itself. Before then it is the menu's own+ // suggestions for what is being typed.+ if (combinedSearch) {+ if (aiSearchSession.isActiveFor(query)) {+ return ["topics", "context"].includes(aiSearchSession.selectedTab);+ }+ return value;+ }+return !query || discobotDiscoveries.lastQuery !== query;});@@ -189,6 +239,18 @@ export default apiInitializer((api) => {});+ // clearing the search is a fresh start, so the search's results go with it+ api.registerBehaviorTransformer(+ "search-menu-clear-search",+ ({ context, next }) => {+ if (combinedSearch && offersDiscoveries(context?.location)) {+ aiSearchSession.reset();+ }++ return next();+ }+ );+// clearing the history clears both lists, since they read as oneapi.registerBehaviorTransformer("search-menu-clear-recent-searches",@@ -203,6 +265,19 @@ export default apiInitializer((api) => {// each remembered item repeats as the kind of search its icon showsapi.addSearchMenuAssistantSelectCallback((args) => {+ // A remembered search is set rather than typed, so it starts the combined+ // search itself. The term is set without the menu's own search, which would+ // race it, and the scope stays as the reader left it.+ if (+ combinedSearch &&+ ["recent-search", "recent-ask"].includes(args.usage) &&+ args.updatedTerm+ ) {+ search.activeGlobalSearchTerm = args.updatedTerm;+ startCombined(args.updatedTerm, { trigger: "recent" });+ return false;+ }+if (args.usage === "recent-search") {discobotDiscoveries.dismissDiscovery();return true;@@ -225,6 +300,24 @@ export default apiInitializer((api) => {if (event.key === "Enter") {const query = search.activeGlobalSearchTerm?.trim();+ if (combinedSearch) {+ if (!event.shiftKey && query) {+ // a search already on screen for this term is not run again, unless+ // its answer broke down; returning false skips the menu's own enter+ // handling, which would otherwise treat a second enter as "open the+ // full page"+ if (+ !aiSearchSession.isActiveFor(query) ||+ aiSearchSession.answerErrored+ ) {+ startCombined(query, { trigger: "enter" });+ }+ return false;+ }+ aiSearchSession.reset();+ return true;+ }+const enterAsks = !search.inTopicContext && asksByDefault();if (enterAsks &&@@ -255,6 +348,7 @@ export default apiInitializer((api) => {// back when the same term is typed a second time, since by then the answer// it would be matched against is already gone.if (+ !combinedSearch &&discobotDiscoveries.lastQuery &&discobotDiscoveries.lastQuery !== search.activeGlobalSearchTerm?.trim()) {@@ -274,17 +368,18 @@ export default apiInitializer((api) => {api.registerValueTransformer("search-menu-search-context-enabled",({ value, context }) =>- offersDiscoveries(context?.location) ? false : value+ !combinedSearch && offersDiscoveries(context?.location) ? false : value);// advanced search is offered in the options row insteadapi.registerValueTransformer("search-menu-advanced-button-enabled",({ value, context }) =>- offersDiscoveries(context?.location) ? false : value+ !combinedSearch && offersDiscoveries(context?.location) ? false : value);- // the menu offers every way to resolve the term as options of its own+ // the menu offers every way to resolve the term as options of its own, or+ // with combined search, enter resolves it every way at onceapi.registerValueTransformer("search-menu-search-shortcuts-enabled",({ value, context }) =>diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-follow-up.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-follow-up.jsnew file mode 100644index 00000000000..e93c51a9fe4--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-follow-up.js@@ -0,0 +1,17 @@+/**+ * Whether the reader can continue an answer as a conversation with the+ * follow-up agent. Mirrors the server's checks, which refuse the conversation+ * otherwise.+ */+export function followUpAvailable(siteSettings, currentUser) {+ // only serialized for readers the AI bot is enabled for+ if (!siteSettings.ai_bot_enabled || !currentUser?.ai_enabled_chat_bots) {+ return false;+ }++ const agentId = parseInt(siteSettings.ai_ask_ai_follow_up_agent, 10);+ const agent = currentUser.ai_enabled_agents?.find(({ id }) => id === agentId);+ return Boolean(+ agent?.allow_personal_messages && agent.username && agent.has_default_llm+ );+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-cited-topics.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-cited-topics.jsnew file mode 100644index 00000000000..8ddd50f622e--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-cited-topics.js@@ -0,0 +1,135 @@+import { tracked } from "@glimmer/tracking";+import { setOwner } from "@ember/owner";+import { service } from "@ember/service";+import { ajax } from "discourse/lib/ajax";+import { bind } from "discourse/lib/decorators";+import AiSearchReferences, {+ citedTopicsFromCooked,+ citedTopicsFromRaw,+} from "./ai-search-references.js";++/**+ * The topics a conversation continued from a combined search has cited, the+ * latest citations first, kept current as replies finish.+ */+export default class AiSearchCitedTopics {+ @service messageBus;++ @tracked references = [];++ #currentTurn = 0;+ #details = new Map();+ #lookedUp = new Set();+ #rankedPostNumber = 0;+ #ranker = new AiSearchReferences();+ #topic;++ constructor(owner, topic) {+ setOwner(this, owner);+ this.#topic = topic;+ this.messageBus.subscribe(this.#channel, this.onStream);+ this.#rankCitedSoFar();+ }++ get #channel() {+ return `discourse-ai/ai-bot/topic/${this.#topic.id}`;+ }++ teardown() {+ this.messageBus.unsubscribe(this.#channel, this.onStream);+ }++ // A reply still streaming when the page loaded finishes as the last post,+ // and fills in its own turn; any later reply starts a new one.+ @bind+ onStream(data) {+ if (!data?.done || !data.post_number) {+ return;+ }+ if (data.post_number < this.#rankedPostNumber) {+ return;+ }++ if (data.post_number > this.#rankedPostNumber) {+ this.#currentTurn++;+ this.#rankedPostNumber = data.post_number;+ }+ const cited = data.cooked+ ? citedTopicsFromCooked(data.cooked)+ : citedTopicsFromRaw(data.raw);+ this.#ranker.add(this.#currentTurn, "cited", cited);+ this.#showRanked();+ }++ // The topic view brings what has been cited so far, with each topic's+ // details, so nothing is loaded until a new reply cites more.+ #rankCitedSoFar() {+ const { replies = 0, topics: cited = [] } =+ this.#topic.ai_search_cited_topics || {};+ const turns = new Map();+ cited.forEach((topic) => {+ turns.set(topic.turn, [...(turns.get(topic.turn) ?? []), topic]);+ });+ turns.forEach((topics, turn) => {+ topics.forEach((topic) => {+ this.#lookedUp.add(topic.topic_id);+ this.#details.set(topic.topic_id, topic);+ });+ this.#ranker.add(+ turn,+ "cited",+ topics.map((topic) => ({+ topicId: topic.topic_id,+ title: topic.title,+ url: topic.url,+ }))+ );+ });++ this.#ranker.exclude(this.#topic.id);+ this.#currentTurn = Math.max(replies - 1, 0);+ this.#rankedPostNumber = this.#topic.highest_post_number ?? 0;+ this.#showRanked();+ }++ // A cited link has only a title, so each topic's details are looked up once.+ // The lookup leaves out muted topics, which are still listed by their title.+ async #showRanked() {+ const ranked = this.#ranker.rank(this.#currentTurn);+ const unknown = ranked+ .map((reference) => reference.topicId)+ .filter((id) => !this.#lookedUp.has(id));++ if (unknown.length) {+ try {+ const result = await ajax("/filter.json", {+ data: { q: `topic:${unknown.join(",")}` },+ });+ unknown.forEach((id) => this.#lookedUp.add(id));+ result.topic_list.topics.forEach((topic) =>+ this.#details.set(topic.id, {+ title: topic.title,+ category_id: topic.category_id,+ bumped_at: topic.bumped_at,+ reply_count: topic.posts_count - 1,+ })+ );+ } catch {+ // listed by title until the next reply looks again+ }+ }++ this.references = ranked.map((reference) => {+ const details = this.#details.get(reference.topicId);+ return details+ ? {+ ...reference,+ title: details.title,+ categoryId: details.category_id,+ bumpedAt: details.bumped_at,+ replyCount: details.reply_count,+ }+ : reference;+ });+ }+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-intent.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-intent.jsnew file mode 100644index 00000000000..c922e89e300--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-intent.js@@ -0,0 +1,195 @@+// quoted phrases and search operators are someone after precise results+const SEARCH_SYNTAX =+ /"[^"]+"|(?:^|\s)-?(?:in|status|order|before|after|with|category|categories|tags?|user|group|badge|topic|min_\w+|max_\w+):\S|(?:^|\s)[#@]\S+/i;++const QUESTION_MARK = /[??]\s*$/;++// this many words is natural language rather than keywords+const QUESTION_LENGTH = 5;++// below this the decision model is no surer than how the query is written+const INTENT_PROBABILITY = 0.7;++// the writing systems each site language is searched in; anything else is+// left out of the search index's stemming and matches poorly+const LOCALE_SCRIPTS = {+ ar: ["Arabic"],+ be: ["Cyrillic"],+ bg: ["Cyrillic"],+ el: ["Greek"],+ fa: ["Arabic"],+ he: ["Hebrew"],+ hi: ["Devanagari"],+ ja: ["Han", "Hiragana", "Katakana"],+ kk: ["Cyrillic"],+ ko: ["Hangul", "Han"],+ mk: ["Cyrillic"],+ ru: ["Cyrillic"],+ sr: ["Cyrillic", "Latin"],+ th: ["Thai"],+ uk: ["Cyrillic"],+ ur: ["Arabic"],+ zh: ["Han"],+};+const DETECTED_SCRIPTS = [+ "Latin",+ "Cyrillic",+ "Greek",+ "Arabic",+ "Hebrew",+ "Devanagari",+ "Han",+ "Hiragana",+ "Katakana",+ "Hangul",+ "Thai",+].map((name) => [name, new RegExp(`\\p{Script=${name}}`, "u")]);+const FOREIGN_SHARE = 0.6;++/**+ * @typedef {"ask"|"topics"|"context"} SearchTab+ */++/**+ * @typedef {Object} SearchIntent+ * @property {"answer"|"lookup"} intent what a decision model judged the reader+ * is after+ * @property {number} probability the chance the model gives that intent+ * @property {"here"|"site"} [scope] for a search made in a topic or category,+ * whether it is about that place or the whole site+ * @property {number} [scope_probability] the chance the model gives that scope+ */++/**+ * Which tab a search should open on, guessed from how the query reads and+ * what the keyword searches found. Every tab is fetched, so this only decides+ * what the reader sees first.+ *+ * @param {string} query+ * @param {Object} found+ * @param {Object} [found.topics] the site-wide keyword results+ * @param {Object} [found.context] the keyword results within the context+ * @param {string} [found.scope] the context's scope key, when there is one+ * @param {string} [found.siteLocale] the language the site is searched in+ * @param {boolean} [found.searchedAgain] whether an earlier search on this+ * page seems not to have helped+ * @param {SearchIntent} [found.intent] a decision model's judgement, which+ * can open a search on the answer when it is confident, but never away from it+ * @param {boolean} [found.prefersResults] whether the reader's recent searches+ * show them leaving the answer for the results+ * @returns {{tab: SearchTab, reason: string}}+ */+export function chooseTab(+ query,+ {+ topics,+ context,+ scope,+ siteLocale,+ searchedAgain,+ intent,+ prefersResults,+ } = {}+) {+ const topicCount = topics?.posts?.length ?? 0;+ // a search about the whole site is not shown its context first+ const aboutSite =+ intent?.scope === "site" && intent.scope_probability >= INTENT_PROBABILITY;+ const contextCount = scope && !aboutSite ? (context?.posts?.length ?? 0) : 0;++ if (usesSearchSyntax(query)) {+ return {+ tab: contextCount > 0 ? "context" : "topics",+ reason: "syntax",+ };+ }++ // within a topic the reader is after a passage of it+ if (scope?.startsWith("topic:") && contextCount > 0) {+ return { tab: "context", reason: "topic_match" };+ }++ // keyword search does poorly outside the site's language+ if (inForeignScript(query, siteLocale)) {+ return { tab: "ask", reason: "language" };+ }++ // a second search here means the first one's results did not help+ if (searchedAgain) {+ return { tab: "ask", reason: "searched_again" };+ }++ if (prefersResults && (contextCount > 0 || topicCount > 0)) {+ return {+ tab: contextCount > 0 ? "context" : "topics",+ reason: "reader_prefers_results",+ };+ }++ // The model spots answers well but lookups poorly, and the answer usually+ // serves a reader better than keyword results, so it only ever adds answers.+ if (intent?.intent === "answer" && intent.probability >= INTENT_PROBABILITY) {+ return { tab: "ask", reason: "intent_answer" };+ }+ if (readsAsQuestion(query)) {+ return { tab: "ask", reason: "question" };+ }++ if (contextCount > 0) {+ return { tab: "context", reason: "context_match" };+ }++ if (topicCount > 0) {+ return { tab: "topics", reason: "topic_results" };+ }++ return { tab: "ask", reason: "no_keyword_matches" };+}++/**+ * Whether most of the query's letters are in a writing system the site's+ * language does not use.+ */+export function inForeignScript(query, siteLocale) {+ const expected = LOCALE_SCRIPTS[(siteLocale ?? "").split(/[_-]/)[0]] ?? [+ "Latin",+ ];+ const letters = [...(query ?? "")].filter((char) => /\p{L}/u.test(char));+ if (letters.length === 0) {+ return false;+ }++ // Latin letters are never counted: product names, code and borrowed terms+ // are written in them on sites in any language+ const foreign = letters.filter((char) => {+ const script = DETECTED_SCRIPTS.find(([, pattern]) => pattern.test(char));+ return script && script[0] !== "Latin" && !expected.includes(script[0]);+ });+ return foreign.length / letters.length >= FOREIGN_SHARE;+}++export function usesSearchSyntax(query) {+ return SEARCH_SYNTAX.test(query ?? "");+}++export function readsAsQuestion(query) {+ return QUESTION_MARK.test(query ?? "") || wordCount(query) >= QUESTION_LENGTH;+}++/**+ * Words rather than spaces, so a question in a language written without+ * spaces between words still counts as several.+ */+export function wordCount(query) {+ const text = query?.trim() ?? "";+ if (!text) {+ return 0;+ }++ if (typeof Intl?.Segmenter === "function") {+ const segments = new Intl.Segmenter(undefined, { granularity: "word" });+ return [...segments.segment(text)].filter((s) => s.isWordLike).length;+ }++ return text.split(/\s+/).length;+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-references.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-references.jsnew file mode 100644index 00000000000..aa824a1e8dd--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-references.js@@ -0,0 +1,136 @@+const RANK_K = 10;+const TURN_DECAY = 0.6;++export const SIGNAL_WEIGHTS = {+ cited: 3,+ semantic: 1.2,+ keyword: 1,+};++const TOPIC_URL_PATTERN = /\/t\/(?:[^/\s)?#]+\/)?(\d+)/;+const MARKDOWN_LINK_PATTERN = /\[([^\]]+)\]\(([^)\s]+)\)/g;++export function topicIdFromUrl(url) {+ const match = url?.match(TOPIC_URL_PATTERN);+ return match ? parseInt(match[1], 10) : null;+}++/**+ * Topics a reply links to, titled by their link text, in order of first mention.+ */+export function citedTopicsFromRaw(raw) {+ const seen = new Set();+ const cited = [];++ for (const [, text, url] of (raw || "").matchAll(MARKDOWN_LINK_PATTERN)) {+ const topicId = topicIdFromUrl(url);+ if (topicId && !seen.has(topicId)) {+ seen.add(topicId);+ cited.push({ topicId, title: text, url });+ }+ }++ return cited;+}++/**+ * Accumulates evidence for topics across a conversation's turns and ranks them+ * with a reciprocal-rank score, so a topic that keeps surfacing rises while one+ * only relevant to an early turn fades rather than vanishing.+ */+export default class AiSearchReferences {+ #entries = new Map();+ #previousOrder = [];+ #excludedTopicIds = new Set();++ exclude(topicId) {+ this.#excludedTopicIds.add(topicId);+ this.#entries.delete(topicId);+ }++ /**+ * @param {number} turn conversation turn the evidence belongs to+ * @param {"cited"|"semantic"|"keyword"} kind+ * @param {Array<{topicId: number, title: string, url: string, categoryId?: number, excerpt?: string}>} items best first+ */+ add(turn, kind, items) {+ items.forEach((item, index) => {+ if (!item.topicId || this.#excludedTopicIds.has(item.topicId)) {+ return;+ }++ const entry = this.#entries.get(item.topicId) || {+ topicId: item.topicId,+ signals: [],+ };+ entry.title ||= item.title;+ entry.url ||= item.url;+ entry.categoryId ??= item.categoryId;+ entry.excerpt ||= item.excerpt;+ // the same evidence counts once, so a reply can be added again as it grows+ if (+ !entry.signals.some(+ (signal) => signal.turn === turn && signal.kind === kind+ )+ ) {+ entry.signals.push({ turn, kind, rank: index + 1 });+ }+ this.#entries.set(item.topicId, entry);+ });+ }++ /**+ * Ranks every topic as of `currentTurn` and reports how each moved since the+ * previous call.+ */+ rank(currentTurn, limit = 12) {+ const ranked = [...this.#entries.values()]+ .map((entry) => ({+ ...entry,+ score: this.#score(entry, currentTurn),+ kinds: [...new Set(entry.signals.map((signal) => signal.kind))],+ }))+ .sort((a, b) => b.score - a.score || a.topicId - b.topicId)+ .slice(0, limit);++ const previous = this.#previousOrder;+ ranked.forEach((entry, index) => {+ const previousIndex = previous.indexOf(entry.topicId);+ entry.isNew = previous.length > 0 && previousIndex === -1;+ entry.movement = previousIndex === -1 ? 0 : previousIndex - index;+ });+ this.#previousOrder = ranked.map((entry) => entry.topicId);++ return ranked;+ }++ #score(entry, currentTurn) {+ return entry.signals.reduce(+ (total, signal) =>+ total ++ (SIGNAL_WEIGHTS[signal.kind] / (RANK_K + signal.rank)) *+ TURN_DECAY ** (currentTurn - signal.turn),+ 0+ );+ }+}++/**+ * Topics a cooked post links to, titled by their link text.+ */+export function citedTopicsFromCooked(cooked) {+ const doc = new DOMParser().parseFromString(cooked || "", "text/html");+ const seen = new Set();+ const cited = [];++ doc.querySelectorAll("a[href]").forEach((link) => {+ const url = link.getAttribute("href");+ const topicId = topicIdFromUrl(url);+ if (topicId && !seen.has(topicId)) {+ seen.add(topicId);+ cited.push({ topicId, title: link.textContent.trim(), url });+ }+ });++ return cited;+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-scope.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-scope.jsnew file mode 100644index 00000000000..4ce01593a32--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-scope.js@@ -0,0 +1,141 @@+import { i18n } from "discourse-i18n";++/**+ * @typedef {Object} AiSearchScope+ * @property {string} key what the server accepts, e.g. `category:12` or `messages`+ * @property {string} label how the scope is named within a sentence+ * @property {string} tabLabel how the scope's tab is named+ */++/**+ * Whether the header search answers and searches together for this user. It+ * fetches its own semantic matches, so features that add them to the menu+ * stand aside.+ */+export function combinedSearchActive(siteSettings, currentUser) {+ return Boolean(+ siteSettings.enable_ai_combined_search &&+ siteSettings.ai_ask_ai_enabled &&+ siteSettings.ai_ask_ai_agent &&+ currentUser?.can_use_ask_ai+ );+}++const FILTERS = {+ messages: () => "in:messages",+ topic: (value) => `topic:${value}`,+ category: (value) => `category:${value}`,+ tag: (value) => `tags:${value}`,+ user: (value) => `user:${value}`,+};++/**+ * The search filter a scope key stands for, mirroring the server's reading of+ * the same key. A key can join a category and tags with a space, as a tag page+ * within a category does.+ */+export function scopeFilter(key) {+ if (!key) {+ return null;+ }++ const filters = key.split(" ").map((part) => {+ const [type, value] = part.split(/:(.*)/s);+ return FILTERS[type]?.(value);+ });+ return filters.every(Boolean) ? filters.join(" ") : null;+}++/**+ * Whether semantic search can rank within a scope. Embeddings describe public+ * topics, so they cannot rank within one topic or among messages.+ */+export function semanticSearchable(key) {+ return !["topic", "messages"].includes(key?.split(":")[0]);+}++/**+ * The query as keyword search should see it, within the scope. Closing+ * punctuation is dropped: it adds nothing to the keywords, and a search within+ * a topic finds nothing when the term ends with it.+ */+export function withScope(query, key) {+ const keywords = (query ?? "").replace(/[\s??!!.。]+$/u, "");+ return [keywords, scopeFilter(key)].filter(Boolean).join(" ");+}++/**+ * The scope the reader is searching from, or null when the search is site+ * wide.+ *+ * @param {Object} search the search service+ * @param {Object} options+ * @param {boolean} options.inPMInboxContext whether the menu is scoped to messages+ * @returns {AiSearchScope|null}+ */+export function contextScope(search, { inPMInboxContext }) {+ const place = contextPlace(search.searchContext);++ // a topic is always offered as a tab of its own+ if (place?.key.startsWith("topic:")) {+ return place;+ }++ if (inPMInboxContext) {+ return {+ key: "messages",+ label: i18n("discourse_ai.ai_search.scope.messages"),+ tabLabel: i18n("discourse_ai.ai_search.tabs.messages"),+ };+ }++ return place;+}++/**+ * The place a search context stands for, whether or not the menu searches+ * within it.+ *+ * @param {Object|null} context the search service's search context+ * @returns {AiSearchScope|null}+ */+export function contextPlace(context) {+ switch (context?.type) {+ case "topic":+ return {+ key: `topic:${context.id}`,+ label: i18n("discourse_ai.ai_search.scope.this_topic"),+ tabLabel: i18n("discourse_ai.ai_search.tabs.this_topic"),+ };+ case "category": {+ const name = context.category?.name ?? context.id;+ return { key: `category:${context.id}`, label: name, tabLabel: name };+ }+ case "tag":+ case "tagIntersection": {+ const tags = [+ context.tagId ?? context.name ?? context.id,+ ...(context.additionalTags ?? []),+ ].filter(Boolean);+ if (!tags.length) {+ return null;+ }++ const tagNames = tags.map((tag) => `#${tag}`).join(" ");+ const name = context.category+ ? `${context.category.name} ${tagNames}`+ : tagNames;+ const tagKey = `tag:${tags.join("+")}`;+ const key = context.categoryId+ ? `category:${context.categoryId} ${tagKey}`+ : tagKey;+ return { key, label: name, tabLabel: name };+ }+ case "user": {+ const name = `@${context.user?.username ?? context.id}`;+ return { key: `user:${context.id}`, label: name, tabLabel: name };+ }+ }++ return null;+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/open-topic-from-query.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/open-topic-from-query.jsnew file mode 100644index 00000000000..b2a070980fb--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/open-topic-from-query.js@@ -0,0 +1,104 @@+import { ajax } from "discourse/lib/ajax";+import { tagNames, tagSuggestionParams } from "./ai-helper-suggestions.js";+import { showComposerAiHelper } from "./show-ai-helper.js";++async function requestSuggestion(url, data) {+ try {+ return await ajax(url, { type: "POST", data });+ } catch {+ return null;+ }+}++async function enrichNewTopic({+ model,+ query,+ suggestTitle,+ suggestTaxonomy,+ canTagTopics,+ maxTitleLength,+}) {+ const initialTitle = model.title;+ const initialCategoryId = model.categoryId;+ const initialTags = tagNames(model.tags);+ const titleRequest = suggestTitle+ ? requestSuggestion("/discourse-ai/ai-helper/suggest_title", {+ text: query,+ })+ : null;+ const categoryRequest = suggestTaxonomy+ ? requestSuggestion("/discourse-ai/ai-helper/suggest_category", {+ text: query,+ })+ : null;+ const [titleResult, categoryResult] = await Promise.all([+ titleRequest,+ categoryRequest,+ ]);++ const suggestedTitle = titleResult?.suggestions?.[0]?.trim();+ if (suggestedTitle && model.title === initialTitle) {+ model.set("title", suggestedTitle.slice(0, maxTitleLength));+ }++ const suggestedCategory = categoryResult?.assistant?.[0];+ if (suggestedCategory && model.categoryId === initialCategoryId) {+ model.set("categoryId", suggestedCategory.id);+ }++ if (+ !suggestTaxonomy ||+ !canTagTopics ||+ tagNames(model.tags).join("\0") !== initialTags.join("\0")+ ) {+ return;+ }++ const tagResult = await requestSuggestion(+ "/discourse-ai/ai-helper/suggest_tags",+ {+ text: query,+ ...tagSuggestionParams(model.categoryId, model.tags),+ }+ );+ const suggestedTags = tagResult?.assistant+ ?.map((tag) => tag.name)+ .filter(Boolean);++ if (+ suggestedTags?.length &&+ tagNames(model.tags).join("\0") === initialTags.join("\0")+ ) {+ model.set("tags", suggestedTags);+ }+}++/**+ * Opens the composer for a new topic asking the query, then fills in the AI+ * helper's suggested title, category and tags where they are enabled.+ */+export async function openTopicFromQuery({+ composer,+ currentUser,+ query,+ siteSettings,+}) {+ await composer.openNewTopic({ title: query });++ const model = composer.model;+ const suggestionsEnabled = showComposerAiHelper(+ model,+ siteSettings,+ currentUser,+ "suggestions"+ );++ await enrichNewTopic({+ model,+ query,+ suggestTitle: suggestionsEnabled,+ suggestTaxonomy: suggestionsEnabled && siteSettings.ai_embeddings_enabled,+ canTagTopics: currentUser.can_tag_topics,+ maxTitleLength: siteSettings.max_topic_title_length,+ });+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/services/ai-search-session.js b/plugins/discourse-ai/assets/javascripts/discourse/services/ai-search-session.jsnew file mode 100644index 00000000000..1e4fa1387b8--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/services/ai-search-session.js@@ -0,0 +1,454 @@+import { tracked } from "@glimmer/tracking";+import { action } from "@ember/object";+import Service, { service } from "@ember/service";+import { ajax } from "discourse/lib/ajax";+import { bind } from "discourse/lib/decorators";+import getURL from "discourse/lib/get-url";+import { searchForTerm } from "discourse/lib/search";+import { followUpAvailable } from "../lib/ai-follow-up.js";+import { chooseTab, readsAsQuestion } from "../lib/ai-search-intent.js";+import { withScope } from "../lib/ai-search-scope.js";++// a search this soon after the last one on the same page is still the same hunt+const SEARCH_AGAIN_WINDOW_MS = 3 * 60 * 1000;+// how long the tabs wait on the reply's judgements before judging the query alone+const INTENT_WAIT_MS = 1000;++function findsAsMuch(results, other) {+ return (+ Boolean(results) &&+ (results.posts?.length ?? 0) >= (other?.posts?.length ?? 0)+ );+}++/**+ * One combined search in the header search menu: the AI answer and keyword+ * results for the same query, fetched at once and offered as tabs, opening on+ * the one the query most likely wants.+ */+export default class AiSearchSession extends Service {+ @service appEvents;+ @service currentUser;+ @service discobotDiscoveries;+ @service messageBus;+ @service search;+ @service site;+ @service siteSettings;++ @tracked query = "";++ /** @type {import("../lib/ai-search-scope").AiSearchScope|null} */+ @tracked scope = null;+ @tracked answering = false;++ @tracked scopeFallback = false;+ @tracked fallbackScopeLabel = null;++ /** The scope the menu is currently in, which the next search starts from. */+ @tracked contextScope = null;++ /**+ * The menu's tab on screen, null until the keyword searches are in and one+ * is chosen.+ *+ * @type {import("../lib/ai-search-intent").SearchTab|null}+ */+ @tracked selectedTab = null;++ /** Each keyword tab's results, null while they load. */+ @tracked topicsResults = null;+ @tracked contextResults = null;++ /** Switches the open search menu to showing topics; set by the menu. */+ showTopicsInMenu = null;++ #subscribed = false;+ #tabChosenByReader = false;+ #lastSearch = null;+ // the menu search's tab choices, logged on its ask to judge how well the+ // opening tab was chosen+ #tabLog = null;++ willDestroy() {+ super.willDestroy(...arguments);+ if (this.#subscribed) {+ this.messageBus.unsubscribe(+ "/discourse-ai/discoveries",+ this.onDiscovery+ );+ this.appEvents.off("discourse-ai:answer-failed", this.onAnswerFailed);+ }+ }++ get discoveries() {+ return this.discobotDiscoveries;+ }++ get answerSettled() {+ const discoveries = this.discoveries;+ return (+ !discoveries.loadingDiscoveries &&+ !discoveries.isStreaming &&+ (discoveries.answerable !== null ||+ Boolean(discoveries.errorMessage) ||+ discoveries.discoveryTimedOut)+ );+ }++ get answerFailed() {+ return (+ this.answerSettled &&+ (this.discoveries.answerable === false ||+ Boolean(this.discoveries.errorMessage) ||+ this.discoveries.discoveryTimedOut)+ );+ }++ /** Whether the answer broke down, rather than finding nothing to say. */+ get answerErrored() {+ return (+ this.answerSettled &&+ (Boolean(this.discoveries.errorMessage) ||+ this.discoveries.discoveryTimedOut)+ );+ }++ get relatedTopics() {+ return (this.discoveries.sources || []).map((source) => ({+ ...source,+ categoryModel: this.site.categories?.find(+ (category) => category.id === source.category_id+ ),+ }));+ }++ get canFollowUp() {+ return (+ followUpAvailable(this.siteSettings, this.currentUser) &&+ this.answerSettled &&+ !this.answerFailed &&+ Boolean(this.discoveries.activeRequestId)+ );+ }++ /** The results of the keyword tab on screen, if one is. */+ get shownResults() {+ if (this.selectedTab === "topics") {+ return this.topicsResults;+ }+ if (this.selectedTab === "context") {+ return this.contextResults;+ }+ return null;+ }++ get noKeywordMatches() {+ return Boolean(this.shownResults) && !this.shownResults.posts?.length;+ }++ isActiveFor(query) {+ return Boolean(query) && this.query === query.trim();+ }++ /**+ * @param {string} query+ * @param {import("../lib/ai-search-scope").AiSearchScope|null} [scope]+ * @param {Object} [options]+ * @param {string} [options.trigger] what started the search, for the log+ */+ start(query, scope = null, { trigger = "" } = {}) {+ this.subscribe();++ const searchedAgain = this.#searchedAgain(query);++ this.query = query;+ this.answering = true;+ this.scope = scope;+ this.scopeFallback = false;+ this.fallbackScopeLabel = null;++ this.discoveries.dismissDiscovery();+ const asked = this.#ask(query, { trigger });++ this.#searchMenuTabs(query, { searchedAgain, asked });+ this.#lastSearch = { query, at: Date.now() };+ }++ /**+ * Listens for answers ahead of the first question, so an update published+ * soon after asking is not missed while the subscription is still reaching+ * the server.+ */+ subscribe() {+ if (!this.#subscribed) {+ this.messageBus.subscribe("/discourse-ai/discoveries", this.onDiscovery);+ this.appEvents.on("discourse-ai:answer-failed", this.onAnswerFailed);+ this.#subscribed = true;+ }+ }++ reset() {+ this.query = "";+ this.answering = false;+ this.scope = null;+ this.selectedTab = null;+ this.topicsResults = null;+ this.contextResults = null;+ this.discoveries.dismissDiscovery();+ }++ /** Records the menu's scope, which the next search starts from. */+ updateContextScope(scope) {+ this.contextScope = scope;+ }++ /** Leaving the page ends the hunt, whether or not a result was opened. */+ forgetSearches() {+ this.#lastSearch = null;+ }++ @action+ selectTab(tab) {+ const log = this.#tabLog;+ if (log && !log.switched && tab !== this.selectedTab) {+ log.switched = true;+ this.#recordTabs(log, {+ tab_switched_to: tab,+ tab_switched_after_ms: Date.now() - log.since,+ });+ }++ this.#tabChosenByReader = true;+ this.selectedTab = tab;+ this.#showSelectedResults();+ }++ async startConversation(question) {+ const result = await ajax("/discourse-ai/discoveries/continue-convo", {+ type: "POST",+ data: { request_id: this.discoveries.activeRequestId, question },+ });+ return result.topic_id;+ }++ @bind+ onAnswerFailed() {+ this.#leaveFailedAnswer();+ }++ @bind+ onDiscovery(update) {+ if (!this.query || update.query !== this.query) {+ return;+ }++ const isCurrent = update.request_id === this.discoveries.activeRequestId;+ if (isCurrent && update.phase === "rewritten") {+ this.#applyRewrite(update);+ }+ if (isCurrent && update.scope_fallback && this.scope) {+ this.scopeFallback = true;+ this.fallbackScopeLabel = this.scope.label;+ }++ this.discoveries.onDiscoveryUpdate(update);+ }++ /**+ * Puts the shown tab's results back if something else replaced them. The+ * menu's own search, started while the reader was still typing, can land+ * after this one and would otherwise swap in results for no topics.+ */+ guardMenuResults() {+ const shown = this.#menuDisplay();+ if (+ shown &&+ this.search.results !== shown &&+ this.isActiveFor(this.search.activeGlobalSearchTerm)+ ) {+ this.#showSelectedResults();+ }+ }++ // Every tab is searched at once, so switching between them is instant; the+ // one to open on is chosen once the keyword results show what there is.+ async #searchMenuTabs(query, { searchedAgain, asked }) {+ const log = { asked, since: Date.now(), switched: false };+ this.#tabLog = log;+ this.#tabChosenByReader = false;+ this.selectedTab = null;+ this.topicsResults = null;+ this.contextResults = null;+ this.search.noResults = false;++ const scopeKey = this.scope?.key;+ const replied = this.#waitForReply(asked);+ const [topics, context] = await Promise.all([+ this.#menuSearch(query),+ scopeKey ? this.#menuSearch(withScope(query, scopeKey)) : null,+ ]);+ if (this.query !== query) {+ return;+ }++ // the rewrite can land first, and stays when it found at least as much+ const empty = { posts: [], resultTypes: [] };+ if (!findsAsMuch(this.topicsResults, topics)) {+ this.topicsResults = topics ?? empty;+ }+ if (scopeKey && !findsAsMuch(this.contextResults, context)) {+ this.contextResults = context ?? empty;+ }++ const reply = await replied;+ if (this.query !== query) {+ return;+ }++ if (!this.#tabChosenByReader) {+ const { tab, reason } = chooseTab(query, {+ topics: this.topicsResults,+ context: this.contextResults,+ scope: scopeKey,+ siteLocale: this.siteSettings.default_locale,+ searchedAgain,+ intent: reply?.intent,+ prefersResults: reply?.prefers_results,+ });+ this.selectedTab = tab;+ log.since = Date.now();+ this.#recordTabs(log, { tab_opened: tab, tab_opened_reason: reason });+ if (this.answerFailed) {+ this.#leaveFailedAnswer();+ }+ }+ this.#showSelectedResults();+ }++ // a failed answer is a dead end, so the reader is moved to results that exist+ #leaveFailedAnswer() {+ if (this.selectedTab !== "ask" || this.#tabChosenByReader) {+ return;+ }++ if (this.contextResults?.posts?.length) {+ this.selectedTab = "context";+ } else if (this.topicsResults?.posts?.length) {+ this.selectedTab = "topics";+ } else {+ return;+ }+ this.#showSelectedResults();+ }++ async #recordTabs(log, data) {+ const askLogId = (await log.asked)?.ask_log_id;+ if (!askLogId) {+ return;+ }++ try {+ await ajax("/discourse-ai/discoveries/tabs", {+ type: "PUT",+ data: { ask_log_id: askLogId, ...data },+ });+ } catch {+ // only the measurement is lost+ }+ }++ async #waitForReply(asked) {+ const waited = new Promise((resolve) =>+ setTimeout(resolve, INTENT_WAIT_MS, null)+ );+ return await Promise.race([asked, waited]);+ }++ async #menuSearch(term) {+ try {+ // a tab's "more" opens the full page on the very terms it searched+ const results = await searchForTerm(term, {+ fullSearchUrl: () => getURL(`/search?q=${encodeURIComponent(term)}`),+ });+ // Topics shown through the session are listed by their posts. Left in+ // the topics list, they would have core treat the next keystroke as a+ // full search, logged to recent searches, rather than as typing.+ return results ? { ...results, topics: [] } : null;+ } catch {+ return null;+ }+ }++ // The answer tab still keeps the site-wide results in the menu, so its+ // people and places are there when the reader switches.+ #menuDisplay() {+ return this.shownResults ?? this.topicsResults;+ }++ #showSelectedResults() {+ const shown = this.#menuDisplay();+ if (!shown) {+ return;+ }+ if (this.search.activeGlobalSearchTerm?.trim() !== this.query) {+ return;+ }++ this.search.noResults = false;+ this.search.results = shown;+ }++ // the rewrite drops filler words, so it is preferred unless it finds less+ async #applyRewrite(update) {+ const query = this.query;+ const rewritten = update.keyword_query || "";+ if (!rewritten || rewritten === query) {+ return;+ }++ const scopeKey = this.scope?.key;+ const [topics, context] = await Promise.all([+ this.#menuSearch(rewritten),+ scopeKey ? this.#menuSearch(withScope(rewritten, scopeKey)) : null,+ ]);+ if (this.query !== query) {+ return;+ }++ if (findsAsMuch(topics, this.topicsResults)) {+ this.topicsResults = topics;+ }+ if (scopeKey && findsAsMuch(context, this.contextResults)) {+ this.contextResults = context;+ }+ this.#showSelectedResults();+ }++ #ask(query, { trigger }) {+ // a fresh request every time, so the rewrite is always published+ return this.discoveries.triggerDiscovery(query, {+ scope: this.scope?.key,+ trigger,+ intentHeuristic: readsAsQuestion(query) ? "answer" : "lookup",+ classifyIntent:+ !!this.siteSettings.ai_ask_ai_search_intent_decision_model,+ });+ }++ // a second search here, sharing a word with the last, suggests its results+ // did not help+ #searchedAgain(query) {+ const last = this.#lastSearch;+ if (!last || last.query === query) {+ return false;+ }+ if (Date.now() - last.at > SEARCH_AGAIN_WINDOW_MS) {+ return false;+ }++ const words = (text) =>+ new Set(text.toLowerCase().split(/\s+/).filter(Boolean));+ const earlier = words(last.query);+ return [...words(query)].some((word) => earlier.has(word));+ }+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/services/discobot-discoveries.js b/plugins/discourse-ai/assets/javascripts/discourse/services/discobot-discoveries.jsindex e7912bcb280..43b3d432a44 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/services/discobot-discoveries.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/services/discobot-discoveries.js@@ -28,6 +28,7 @@ export default class DiscobotDiscoveries extends Service {// We use this to retain state after search menu gets closed.// Similar to discourse/discourse#25504@service a11y;+ @service appEvents;@tracked discovery = "";@tracked lastQuery = "";@@ -98,6 +99,9 @@ export default class DiscobotDiscoveries extends Service {if (update.answerable !== undefined) {this.answerable = update.answerable;+ if (update.done && update.answerable === false) {+ this.appEvents.trigger("discourse-ai:answer-failed");+ }}if (update.ai_discover_follow_up !== undefined) {@@ -195,8 +199,15 @@ export default class DiscobotDiscoveries extends Service {this.lastQuery = "";}+ /**+ * @returns {Promise<Object|undefined>} the server's reply when a request was+ * made, which carries the decision model's intent when one was asked for+ */@action- async triggerDiscovery(query) {+ async triggerDiscovery(+ query,+ { scope, trigger, triggerReason, intentHeuristic, classifyIntent } = {}+ ) {const normalizedQuery = query?.trim();// an answer that failed or timed out is worth asking again, even though the@@ -231,12 +242,21 @@ export default class DiscobotDiscoveries extends Service {const response = await ajax("/discourse-ai/discoveries/reply", {type: "POST",- data: { query: normalizedQuery, request_id: requestId },+ data: {+ query: normalizedQuery,+ request_id: requestId,+ ...(scope && { scope }),+ ...(trigger && { trigger }),+ ...(triggerReason && { trigger_reason: triggerReason }),+ ...(intentHeuristic && { intent_heuristic: intentHeuristic }),+ ...(classifyIntent && { classify_intent: true }),+ },});if (response.request_id !== requestId) {throw new Error("Discovery request ID mismatch");}+ return response;} catch {if (this.activeRequestId === requestId) {this.failDiscovery(@@ -260,6 +280,7 @@ export default class DiscobotDiscoveries extends Service {this.activeRequestId = "";this.smoothStreamer.resetStreaming();this.a11y.announce(message, "assertive");+ this.appEvents.trigger("discourse-ai:answer-failed");}timeoutDiscovery() {@@ -279,6 +300,7 @@ export default class DiscobotDiscoveries extends Service {i18n("discourse_ai.discobot_discoveries.timed_out"),"assertive");+ this.appEvents.trigger("discourse-ai:answer-failed");}scheduleDiscoveryTimeout() {diff --git a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-search.scss b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-search.scssnew file mode 100644index 00000000000..6413e180c21--- /dev/null+++ b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-search.scss@@ -0,0 +1,313 @@+// The answer has its own tab, so it grows as it streams; a minimum height+// keeps the placeholder and the no-answer message in a steady box. Nothing may+// shrink: the search menu is a height-limited flex column.+.ai-search-answer {+ --ai-search-answer-height: 12rem;+ position: relative;+ display: flex;+ flex-shrink: 0;+ flex-direction: column;+ gap: var(--space-3);++ &__body {+ position: relative;+ flex-shrink: 0;+ min-height: var(--ai-search-answer-height);+ padding: var(--space-2) var(--space-4);++ .cooked > *:last-child {+ margin-bottom: 0;+ }+ }++ &__more {+ display: flex;+ flex-direction: column;+ gap: var(--space-3);+ padding: 0 var(--space-4) var(--space-2);+ }++ &__scope-note {+ margin: 0 0 var(--space-2);+ font-size: var(--font-down-1);+ color: var(--primary-medium);+ }++ &__title {+ margin: 0 0 var(--space-2);+ font-size: var(--font-up-1);+ }++ &__title-skeleton {+ margin-bottom: var(--space-3);+ font-size: var(--font-up-1);+ }++ &__empty {+ display: flex;+ align-items: center;+ justify-content: center;+ height: 100%;+ margin: 0;+ color: var(--primary-medium);+ text-align: center;+ }++ // the message fills the box to sit in its middle+ &.--no-answer &__body,+ &.--no-answer &__content {+ display: flex;+ }++ &.--no-answer &__content,+ &.--no-answer &__empty {+ flex: 1;+ }++ &__related-heading {+ margin: 0 0 var(--space-1);+ font-size: var(--font-down-1);+ font-weight: normal;+ color: var(--primary-high);+ }++ &__related-list {+ margin: 0;+ list-style: none;+ }++ &__related-item + &__related-item {+ border-top: 1px solid var(--content-border-color);+ }++ &__follow-up {+ display: flex;+ gap: var(--space-2);+ }++ &__follow-up-input[type="text"] {+ flex: 1;+ min-width: 0;+ margin: 0;+ }++ &.--compact {+ --ai-search-answer-height: 7.5rem;++ .ai-search-answer__title {+ font-size: var(--font-0);+ }+ }+}++// laid out like a row of a topic list+.ai-search-topic-row {+ display: grid;+ grid-template:+ "title date" auto+ "category replies" auto / minmax(0, 1fr) auto;+ column-gap: var(--space-3);+ align-items: baseline;+ padding: var(--space-2);+ margin-inline: calc(-1 * var(--space-2));+ line-height: var(--line-height-medium);++ &:hover {+ background: var(--d-hover);+ }++ &__title {+ grid-area: title;+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ color: var(--tertiary);+ }++ &__category {+ grid-area: category;+ min-width: 0;+ line-height: var(--line-height-medium);+ }++ // the category badge sets its own size, which these match+ &__date,+ &__replies {+ justify-self: end;+ font-size: var(--font-down-1);+ color: var(--primary-medium);+ }++ &__date {+ grid-area: date;+ }++ &__replies {+ grid-area: replies;+ display: inline-flex;+ gap: var(--space-1);+ align-items: center;++ .d-icon {+ font-size: var(--font-down-1);+ transform: scaleX(-1);+ }+ }+}++.ai-search-menu-answer {+ flex-shrink: 0;+ border-bottom: 1px solid var(--content-border-color);+}++// Results sit beside an answer that summarises them, so each one is held to a+// line per part: more of them fit in the menu.+.search-menu:has(.ai-search-tabs) .search-result-topic {+ .first-line,+ .blurb {+ display: block;+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ }+}++// A conversation continued from a combined search lists its references where+// the timeline would be; the timeline's own controls are hidden in bot+// conversations already.+.topic-navigation:has(.ai-search-references) .timeline-container {+ display: none;+}++// at the far right of the topic map, where nothing else sits in these+// conversations+.topic-map__stats:has(.ai-search-references-button) {+ .ai-search-references-button {+ order: 1;+ margin-left: auto;+ }++ .topic-map__buttons {+ margin-left: 0;+ }+}++.ai-search-references {+ box-sizing: border-box;+ width: clamp(12rem, 20vw, 18rem);+ max-height: calc(100vh - var(--header-offset, 0px) - var(--space-8));+ overflow-y: auto;+ padding: var(--space-4);+ border: 1px solid var(--content-border-color);+ border-radius: var(--d-border-radius-large);++ &__title {+ margin: 0 0 var(--space-3);+ font-size: var(--font-0);+ }++ &__list {+ margin: 0;+ list-style: none;+ }++ &__item {+ transition: background-color 1s;++ &.--new,+ &.--up {+ animation: ai-search-highlight 1.6s ease-out;+ }+ }++ &__item + &__item {+ border-top: 1px solid var(--content-border-color);+ }++ &__empty {+ color: var(--primary-medium);+ }++ // The modal has its own title and frame. Rows reach into the padding to+ // highlight, so some is kept to hold them inside the modal.+ &.--in-modal {+ width: auto;+ max-height: none;+ padding: 0 var(--space-2);+ border: 0;+ }++ &.--in-modal &__title {+ display: none;+ }+}++@keyframes ai-search-highlight {+ from {+ background: var(--highlight-low);+ }+}++// the classic search options' pills, as tabs over results already fetched+.ai-search-tabs {+ .ai-discoveries-search-options__option {+ position: relative;+ overflow: visible;++ &.--ask,+ &.--topics {+ flex: 0 0 auto;+ }++ &.is-active {+ &:hover {+ .d-icon {+ color: currentcolor;+ }+ }+ }++ // an empty tab stays offered, but reads as having nothing in it+ &.--empty:not(.is-active) {+ opacity: 0.6;+ }+ }++ // Pinned over the pill's corner like a notification badge, so a count+ // arriving does not resize the pill. The row's own padding gives it room.+ &__count,+ &__empty {+ position: absolute;+ top: -0.5em;+ right: -0.5em;+ z-index: 1;+ display: inline-flex;+ align-items: center;+ justify-content: center;+ box-sizing: border-box;+ min-width: 1.5em;+ height: 1.5em;+ padding: 0 0.35em;+ border-radius: var(--d-border-radius-pill);+ font-size: var(--font-down-2);+ font-weight: bold;+ line-height: 1;+ color: var(--secondary);+ background: var(--tertiary);+ pointer-events: none;++ .d-icon {+ margin: 0;+ }+ }++ .--empty &__count,+ &__empty {+ color: var(--primary-high);+ background: var(--primary-low-mid);+ }+}++.ai-search-menu-loading {+ padding: var(--space-2) var(--space-4);+}diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex bbdd17c537e..de95af8294f 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -1891,6 +1891,31 @@ en:expanded: "Expanded"expanded_description: "with AI summaries"+ ai_search:+ related_topics: "Related topics"+ related_replies:+ one: "%{count} reply"+ other: "%{count} replies"+ answer_label: "AI answer"+ follow_up_label: "Ask a follow-up question"+ follow_up_placeholder: "Ask a follow-up…"+ ask: "Ask"+ references_title: "Cited topics"+ references_empty: "Topics the answers cite appear here."+ tabs:+ label: "Search results"+ ask: "Ask AI"+ topics: "Topics"+ this_topic: "This topic"+ messages: "Messages"+ no_answer: "No AI answer found"+ no_keyword_matches: "No keyword matches found."+ no_answer: "AI couldn’t find a clear answer. %{askLink}"+ ask_community: "Ask the community."+ scope:+ this_topic: "this topic"+ messages: "your messages"+ fallback: "Nothing found in %{name}, so this answer is from the whole site."discobot_discoveries:main_title: "Ask AI"loading_convo: "Loading conversation"diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex 5e4f5717341..f9648f5c751 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -170,6 +170,8 @@ en:ai_ask_ai_enabled: "Enable Ask AI in search"ai_ask_ai_summary_detail: "Choose how much detail to include in Ask AI summaries."ai_ask_ai_related_count: "Set the maximum number of related discussions Ask AI selects and shows with each answer."+ enable_ai_combined_search: "Pressing enter in the header search answers with AI and shows keyword results together as tabs, opening on the one the search most likely wants. Follow-up questions continue on a full page."+ ai_ask_ai_search_intent_decision_model: "Experimental: decision model that judges whether a search wants an AI answer or keyword results, to choose which tab the combined search opens on. Search queries are sent to this model's provider. Leave blank to judge from how the query is written."ai_ask_ai_agent: "Agent that selects sources and writes Ask AI answers."ai_ask_ai_query_rewriter_agent: "Agent used to prepare separate keyword and semantic queries for Ask AI. If this agent or its model is unavailable, Ask AI searches with the user's original query."ai_ask_ai_follow_up_agent: "Agent used when a user wants to follow up an Ask AI result. The selected agent must be enabled, allow personal messages, and be available to the user."diff --git a/plugins/discourse-ai/config/routes.rb b/plugins/discourse-ai/config/routes.rbindex dbf3c430aca..7c5be50bdcd 100644--- a/plugins/discourse-ai/config/routes.rb+++ b/plugins/discourse-ai/config/routes.rb@@ -37,6 +37,7 @@ DiscourseAi::Engine.routes.draw doscope module: :discover, path: "/discoveries", defaults: { format: :json } dopost "reply" => "discoveries#reply"+ put "tabs" => "discoveries#record_tabs"get "recent" => "discoveries#recent"delete "recent" => "discoveries#clear_recent"post "continue-convo" => "discoveries#continue_convo"diff --git a/plugins/discourse-ai/config/settings.yml b/plugins/discourse-ai/config/settings.ymlindex bd8397c83d6..4553f7277bc 100644--- a/plugins/discourse-ai/config/settings.yml+++ b/plugins/discourse-ai/config/settings.yml@@ -499,6 +499,20 @@ discourse_ai:max: 6client: truearea: "ai-features/search"+ enable_ai_combined_search:+ default: false+ hidden: true+ client: true+ upcoming_change:+ status: "experimental"+ impact: "feature,all_members"+ learn_more_url: "https://meta.discourse.org/t/-/415171"+ ai_ask_ai_search_intent_decision_model:+ type: enum+ default: ""+ client: true+ enum: "DiscourseAi::Configuration::DecisionModelEnumerator"+ area: "ai-features/search"ai_ask_ai_agent:default: "-41"type: enumdiff --git a/plugins/discourse-ai/db/migrate/20261002200029_add_trigger_to_ask_ai_logs.rb b/plugins/discourse-ai/db/migrate/20261002200029_add_trigger_to_ask_ai_logs.rbnew file mode 100644index 00000000000..37ef8c9dd7d--- /dev/null+++ b/plugins/discourse-ai/db/migrate/20261002200029_add_trigger_to_ask_ai_logs.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true+class AddTriggerToAskAiLogs < ActiveRecord::Migration[8.1]+ def change+ add_column :ask_ai_logs, :ask_trigger, :string, null: false, default: ""+ add_column :ask_ai_logs, :ask_trigger_reason, :string, null: false, default: ""+ end+enddiff --git a/plugins/discourse-ai/db/migrate/20261019213116_add_intent_to_ask_ai_logs.rb b/plugins/discourse-ai/db/migrate/20261019213116_add_intent_to_ask_ai_logs.rbnew file mode 100644index 00000000000..aac6086af70--- /dev/null+++ b/plugins/discourse-ai/db/migrate/20261019213116_add_intent_to_ask_ai_logs.rb@@ -0,0 +1,8 @@+# frozen_string_literal: true+class AddIntentToAskAiLogs < ActiveRecord::Migration[8.1]+ def change+ add_column :ask_ai_logs, :ask_intent_heuristic, :string, null: false, default: ""+ add_column :ask_ai_logs, :ask_intent_model, :string, null: false, default: ""+ add_column :ask_ai_logs, :ask_intent_probability, :float, null: false, default: 0+ end+enddiff --git a/plugins/discourse-ai/db/migrate/20261019213757_add_tabs_to_ask_ai_logs.rb b/plugins/discourse-ai/db/migrate/20261019213757_add_tabs_to_ask_ai_logs.rbnew file mode 100644index 00000000000..ee42b9d5442--- /dev/null+++ b/plugins/discourse-ai/db/migrate/20261019213757_add_tabs_to_ask_ai_logs.rb@@ -0,0 +1,9 @@+# frozen_string_literal: true+class AddTabsToAskAiLogs < ActiveRecord::Migration[8.1]+ def change+ add_column :ask_ai_logs, :ask_tab_opened, :string, null: false, default: ""+ add_column :ask_ai_logs, :ask_tab_opened_reason, :string, null: false, default: ""+ add_column :ask_ai_logs, :ask_tab_switched_to, :string, null: false, default: ""+ add_column :ask_ai_logs, :ask_tab_switched_after_ms, :integer, null: false, default: 0+ end+enddiff --git a/plugins/discourse-ai/db/migrate/20261019232249_add_scope_to_ask_ai_logs.rb b/plugins/discourse-ai/db/migrate/20261019232249_add_scope_to_ask_ai_logs.rbnew file mode 100644index 00000000000..2f72d3f4d8a--- /dev/null+++ b/plugins/discourse-ai/db/migrate/20261019232249_add_scope_to_ask_ai_logs.rb@@ -0,0 +1,8 @@+# frozen_string_literal: true+class AddScopeToAskAiLogs < ActiveRecord::Migration[8.1]+ def change+ add_column :ask_ai_logs, :ask_scope, :string, null: false, default: ""+ add_column :ask_ai_logs, :ask_scope_model, :string, null: false, default: ""+ add_column :ask_ai_logs, :ask_scope_probability, :float, null: false, default: 0+ end+enddiff --git a/plugins/discourse-ai/lib/configuration/decision_model_enumerator.rb b/plugins/discourse-ai/lib/configuration/decision_model_enumerator.rbnew file mode 100644index 00000000000..84fe426cae8--- /dev/null+++ b/plugins/discourse-ai/lib/configuration/decision_model_enumerator.rb@@ -0,0 +1,21 @@+# frozen_string_literal: true++require "enum_site_setting"++module DiscourseAi+ module Configuration+ class DecisionModelEnumerator < ::EnumSiteSetting+ def self.valid_value?(val)+ true+ end++ def self.values+ DB.query_hash(<<~SQL).map(&:symbolize_keys)+ SELECT display_name AS name, id AS value+ FROM decision_models+ ORDER BY display_name+ SQL+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/discoveries.rb b/plugins/discourse-ai/lib/discoveries.rbindex ebd76cfc844..b01723499f2 100644--- a/plugins/discourse-ai/lib/discoveries.rb+++ b/plugins/discourse-ai/lib/discoveries.rb@@ -40,8 +40,24 @@ module DiscourseAireturn 1LUA- class RequestConflict < StandardError- end+ # Tag and user names follow their own configurable rules, so they are only+ # held to being one token: that is what keeps a scope from adding filters.+ # a tag page within a category scopes to both, as "category:12 tag:a+b"+ SCOPE_PATTERN =+ /\A(?:messages|topic:\d{1,12}|category:\d{1,12}|tag:\S{1,100}|user:\S{1,60}|category:\d{1,12} tag:\S{1,100})\z/++ # what set off an Ask AI request and why, recorded so the choice of when to+ # answer can be tuned against what readers do with the answers+ TRIGGERS = %w[enter pause page scope recent].freeze+ TRIGGER_REASON_PATTERN = /\A[a-z_:]{0,60}\z/+ SEARCH_TABS = %w[ask topics context].freeze+ # leaving the answer this soon means it was not what the reader came for+ QUICK_SWITCH_MS = 10_000+ PREFERENCE_ASKS = 10+ PREFERENCE_MIN_ASKS = 4++ MAX_QUERY_LENGTH = 1000+ ASK_RATE_LIMIT = 8class << selfdef enabled_for_user?(user)@@ -65,6 +81,19 @@ module DiscourseAirequest_id.to_s.match?(REQUEST_ID_PATTERN)end+ # A scope is where the question was asked from. It narrows retrieval+ # without being part of the question, so the question can still be+ # rewritten and searched semantically.+ def valid_scope?(scope)+ scope.to_s.match?(SCOPE_PATTERN)+ end++ def scope_filter(scope)+ return if scope.blank? || !valid_scope?(scope)++ scope.to_s.split(" ").map { |part| scope_part_filter(part) }.join(" ")+ end+def private_message_query?(query)query.to_s.match?(PRIVATE_MESSAGE_FILTER)end@@ -102,19 +131,58 @@ module DiscourseAi"discourse-ai:discoveries:recent-asks:2:#{user_id}"end- def result_settings+ def combined_search_for?(user)+ UpcomingChanges.enabled_for_user?(:enable_ai_combined_search, user)+ end++ def result_settings(user = nil){summary_detail: SiteSetting.ai_ask_ai_summary_detail.to_sym,- related_count: SiteSetting.ai_ask_ai_related_count,+ # the combined search lists its related topics compactly, so it can show the most+ related_count:+ (+ if combined_search_for?(user)+ MAX_RELATED_DISCUSSIONS+ else+ SiteSetting.ai_ask_ai_related_count+ end+ ),}end- def enqueue_reply(user:, request_id:, query:)- return if bind_request(user_id: user.id, request_id:, query:) != :created+ # Whether the reader's recent asks show them leaving the answer for the+ # results, unless they have lately been coming back to it. A habit moves+ # slowly, so it is worked out at most every few minutes.+ def prefers_results?(user)+ Discourse+ .cache+ .fetch("discourse-ai:discoveries:prefers-results:#{user.id}", expires_in: 5.minutes) do+ leaves_answers?(user)+ end+ end+ # Queues the answer to a request already bound with `bind_request`.+ # @return [AskAiLog] the new ask's log+ def enqueue_reply(+ user:,+ request_id:,+ query:,+ scope: nil,+ trigger: "",+ trigger_reason: "",+ intent_heuristic: ""+ )asked_at = Time.current- ask_log = AskAiLog.create!(user:, query:, asked_at:)- settings = result_settings+ ask_log =+ AskAiLog.create!(+ user:,+ query:,+ asked_at:,+ ask_trigger: trigger,+ ask_trigger_reason: trigger_reason,+ ask_intent_heuristic: intent_heuristic,+ )+ settings = result_settings(user)Jobs.enqueue(:stream_discover_reply,user_id: user.id,@@ -124,12 +192,45 @@ module DiscourseAiqueued_at: asked_at.to_f,summary_detail: settings[:summary_detail].to_s,related_count: settings[:related_count],+ scope:,)+ ask_logrescue StandardErrorask_log&.update!(ask_outcome: :failed)raiseend+ # Records which tab the search menu opened an ask on, and the first one the+ # reader switched to. Each is kept from its first report only.+ def record_tabs(+ user:,+ ask_log_id:,+ opened: nil,+ opened_reason: "",+ switched_to: nil,+ switched_after_ms: 0+ )+ ask_log = AskAiLog.find_by(id: ask_log_id, user_id: user.id)+ raise Discourse::NotFound if ask_log.nil?++ updates = {}+ if opened.present? && ask_log.ask_tab_opened.blank?+ raise Discourse::InvalidParameters.new(:tab_opened) if SEARCH_TABS.exclude?(opened)+ opened_reason = "" if !opened_reason.to_s.match?(TRIGGER_REASON_PATTERN)+ updates.merge!(ask_tab_opened: opened, ask_tab_opened_reason: opened_reason.to_s)+ end+ if switched_to.present? && ask_log.ask_tab_switched_to.blank?+ if SEARCH_TABS.exclude?(switched_to)+ raise Discourse::InvalidParameters.new(:tab_switched_to)+ end+ updates.merge!(+ ask_tab_switched_to: switched_to,+ ask_tab_switched_after_ms: switched_after_ms.to_i.clamp(0, 1.hour.in_milliseconds),+ )+ end+ ask_log.update!(updates) if updates.present?+ end+def bind_request(user_id:, request_id:, query:)normalized_query = query.to_s.unicode_normalize(:nfc).squishresult =@@ -144,8 +245,9 @@ module DiscourseAireturn :created if result == 1return :existing if result == 0+ # the id was already used for another query+ return :conflict if result == -1- raise RequestConflict if result == -1raise "Unexpected Discoveries request binding result: #{result.inspect}"end@@ -237,6 +339,48 @@ module DiscourseAiprivate+ def leaves_answers?(user)+ opened_on_answer =+ AskAiLog+ .where(user_id: user.id, ask_tab_opened: "ask")+ .order(asked_at: :desc)+ .limit(PREFERENCE_ASKS)+ .pluck(:ask_tab_switched_to, :ask_tab_switched_after_ms)+ return false if opened_on_answer.size < PREFERENCE_MIN_ASKS++ left =+ opened_on_answer.count do |switched_to, after_ms|+ switched_to.present? && after_ms < QUICK_SWITCH_MS+ end+ return false if left * 2 <= opened_on_answer.size++ returned =+ AskAiLog+ .where(user_id: user.id)+ .where.not(ask_tab_opened: ["", "ask"])+ .order(asked_at: :desc)+ .limit(PREFERENCE_ASKS)+ .pluck(:ask_tab_switched_to)+ .count("ask")+ returned < 2+ end++ def scope_part_filter(part)+ type, value = part.split(":", 2)+ case type+ when "messages"+ "in:messages"+ when "topic"+ "topic:#{value}"+ when "category"+ "category:#{value}"+ when "tag"+ "tags:#{value}"+ when "user"+ "user:#{value}"+ end+ end+def discover_agentAiAgent.find_by_id_from_cache(SiteSetting.ai_ask_ai_agent)enddiff --git a/plugins/discourse-ai/lib/discoveries/cited_topics.rb b/plugins/discourse-ai/lib/discoveries/cited_topics.rbnew file mode 100644index 00000000000..0be54d4f1a7--- /dev/null+++ b/plugins/discourse-ai/lib/discoveries/cited_topics.rb@@ -0,0 +1,86 @@+# frozen_string_literal: true++module DiscourseAi+ module Discoveries+ # The topics a conversation continued from a combined search has cited:+ # the answer's sources, then the topics each reply links to, with what a+ # topic list row shows of them. Each is numbered by the reply that cited+ # it, so later citations can rank higher.+ class CitedTopics+ TOPIC_PATH = %r{/t/(?:[^/\s?#]+/)?(\d+)}++ def initialize(topic:, guardian:)+ @topic = topic+ @guardian = guardian+ end++ # @return [Hash] how many replies there are, and each cited topic with the+ # reply it was cited in, counting the answer as the first+ def as_json(*)+ citations =+ sources ++ replies.flat_map.with_index do |topic_ids, turn|+ topic_ids.map { |topic_id| { topic_id:, turn: } }+ end+ topics =+ Topic+ .where(id: citations.map { |citation| citation[:topic_id] }.uniq)+ .select { |topic| @guardian.can_see?(topic) }+ .index_by(&:id)++ cited =+ citations.filter_map do |citation|+ topic = topics[citation[:topic_id]]+ next if topic.nil?++ {+ topic_id: topic.id,+ turn: citation[:turn],+ title: topic.title,+ url: topic.relative_url,+ category_id: topic.category_id,+ bumped_at: topic.bumped_at,+ reply_count: topic.posts_count - 1,+ }+ end++ { replies: replies.size, topics: cited }+ end++ private++ def sources+ Array(@topic.custom_fields[ContinueConversation::TOPIC_SOURCES_FIELD]).map do |source|+ { topic_id: source["topic_id"].to_i, turn: 0 }+ end+ end++ # The topics each reply links to, in order. Every post after the search+ # that its asker did not write is a reply.+ def replies+ @replies ||=+ @topic+ .posts+ .where.not(user_id: @topic.user_id)+ .where("post_number > 1")+ .order(:post_number)+ .pluck(:cooked)+ .map { |cooked| linked_topic_ids(cooked) }+ end++ def linked_topic_ids(cooked)+ Nokogiri::HTML5+ .fragment(cooked)+ .css("a[href]")+ .filter_map do |link|+ href = link["href"]+ next if !href.start_with?("/", Discourse.base_url)++ href[TOPIC_PATH, 1]&.to_i+ end+ .uniq+ .excluding(@topic.id)+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/discoveries/continue_conversation.rb b/plugins/discourse-ai/lib/discoveries/continue_conversation.rbindex 375746bbf17..5c917ed9aa6 100644--- a/plugins/discourse-ai/lib/discoveries/continue_conversation.rb+++ b/plugins/discourse-ai/lib/discoveries/continue_conversation.rb@@ -5,6 +5,8 @@ module DiscourseAiclass ContinueConversationMAX_QUESTION_LENGTH = 1000TOPIC_REQUEST_ID_FIELD = "ai_discovery_request_id"+ # the answer's sources, kept apart from it when the conversation lists them+ TOPIC_SOURCES_FIELD = "ai_discovery_sources"class ResultExpired < StandardErrorend@@ -121,7 +123,7 @@ module DiscourseAiDiscourseAi::AiBot::TOPIC_AI_AGENT_ID_FIELD => route.agent_id,DiscourseAi::AiBot::TOPIC_AI_LLM_MODEL_ID_FIELD => route.llm_model_id,TOPIC_REQUEST_ID_FIELD => request_token(request_id),- },+ }.merge(sources_field(result)),},custom_fields: {DiscourseAi::AiBot::Playground::BYPASS_AI_REPLY_CUSTOM_FIELD => true,@@ -174,7 +176,22 @@ module DiscourseAitopicend+ # The combined search lists the sources beside the conversation, so they+ # are not repeated under an answer. Without an answer they are all there is.+ def sources_listed_beside?(result)+ result["answer"].present? && DiscourseAi::Discoveries.combined_search_for?(@user)+ end++ def sources_field(result)+ return {} if !sources_listed_beside?(result)++ sources = result.fetch("sources").map { |source| source.slice("topic_id", "title", "url") }+ { TOPIC_SOURCES_FIELD => sources }+ end+def model_turn(result)+ return neutralize_mentions(result.fetch("answer")) if sources_listed_beside?(result)+context = []context << neutralize_mentions(result.fetch("answer")) if result["answer"].present?diff --git a/plugins/discourse-ai/lib/discoveries/entry_point.rb b/plugins/discourse-ai/lib/discoveries/entry_point.rbindex 7674ef2d3f9..65d0eb820bb 100644--- a/plugins/discourse-ai/lib/discoveries/entry_point.rb+++ b/plugins/discourse-ai/lib/discoveries/entry_point.rb@@ -13,6 +13,29 @@ module DiscourseAiinclude_condition: -> { SiteSetting.ai_ask_ai_enabled && scope.authenticated? },) { object.ai_ask_ai_default }end++ plugin.register_topic_custom_field_type(ContinueConversation::TOPIC_SOURCES_FIELD, :json)++ # A conversation continued from a combined search lists the topics it+ # cites in place of the timeline.+ plugin.add_to_serializer(+ :topic_view,+ :ai_search_conversation,+ include_condition: -> do+ object.topic.private_message? &&+ object.topic.custom_fields[ContinueConversation::TOPIC_REQUEST_ID_FIELD].present?+ end,+ ) { true }++ # sent with the conversation, so the list shows without loading it+ plugin.add_to_serializer(+ :topic_view,+ :ai_search_cited_topics,+ include_condition: -> do+ object.topic.private_message? &&+ object.topic.custom_fields[ContinueConversation::TOPIC_REQUEST_ID_FIELD].present?+ end,+ ) { CitedTopics.new(topic: object.topic, guardian: scope).as_json }endendenddiff --git a/plugins/discourse-ai/lib/discoveries/intent.rb b/plugins/discourse-ai/lib/discoveries/intent.rbnew file mode 100644index 00000000000..1be0a6ed94a--- /dev/null+++ b/plugins/discourse-ai/lib/discoveries/intent.rb@@ -0,0 +1,129 @@+# frozen_string_literal: true++module DiscourseAi+ module Discoveries+ # Asks a decision model whether a search wants an answer or a lookup, and,+ # when it was made from a topic or category, whether it is about that place+ # or the whole site, so the search can open on the tab that suits it.+ class Intent+ FEATURE_NAME = "ai_search_intent"+ CHOICES = %w[answer lookup].freeze+ QUESTION = {+ name: "intent",+ type: "choice",+ instructions:+ "Someone typed this into a community forum's search box. Choose answer when they " \+ "want something explained, solved or summarised, and lookup when they are finding " \+ "a particular topic, page, post, person or term.",+ choices: CHOICES,+ }.freeze+ SCOPE_CHOICES = %w[here site].freeze+ SCOPE_QUESTION = {+ name: "scope",+ type: "choice",+ instructions:+ "The search was made while viewing the forum topic or category in searching_in. " \+ "Choose here when it is about that topic or category, and site when it is about " \+ "the forum in general.",+ choices: SCOPE_CHOICES,+ }.freeze+ # below this the model is no surer than the rules it would replace+ CONFIDENT_PROBABILITY = 0.7+ # The menu waits a second on the verdict before choosing a tab. This applies+ # to each of connecting, sending and reading, not to the call as a whole.+ TIMEOUT_SECONDS = 1++ # What the model chose for a search, each verdict with its probability.+ # Either verdict can be missing when the model refused it.+ Judgement =+ Data.define(:intent, :probability, :scope, :scope_probability) do+ def initialize(intent: nil, probability: nil, scope: nil, scope_probability: nil)+ super+ end++ # a search confidently about the forum in general, not where it was made+ def about_site?+ scope == "site" && scope_probability >= CONFIDENT_PROBABILITY+ end++ def as_json(*)+ to_h.compact+ end+ end++ # Only readers of the combined search are judged, and only when they ask.+ def self.for(user, requested:)+ new(user:) if requested && Discoveries.combined_search_for?(user)+ end++ def self.decision_model+ id = SiteSetting.ai_ask_ai_search_intent_decision_model+ DecisionModel.find_by(id:) if id.present?+ end++ def initialize(user:, decision_model: self.class.decision_model)+ @user = user+ @decision_model = decision_model+ end++ # @param scope [String, nil] the scope key the search was made in+ # @return [Judgement, nil] the intent and, for a topic or category, the scope;+ # nil when nothing was decided+ def call(query, scope: nil)+ return if @decision_model.nil?++ place = searched_place(scope)+ state = { query: }+ state[:searching_in] = place if place+ questions = [QUESTION]+ questions << SCOPE_QUESTION if place++ client =+ DiscourseAi::Decisions::Client.new(+ @decision_model,+ timeout: TIMEOUT_SECONDS,+ max_attempts: 1,+ )+ result = client.decide(state, questions:, feature_name: FEATURE_NAME, user: @user)++ verdicts = {}+ if (intent = chosen(result[QUESTION[:name]]))+ verdicts.merge!(intent: intent[0], probability: intent[1])+ end+ if place && (scope_choice = chosen(result[SCOPE_QUESTION[:name]]))+ verdicts.merge!(scope: scope_choice[0], scope_probability: scope_choice[1])+ end+ Judgement.new(**verdicts) if verdicts.present?+ rescue DiscourseAi::Decisions::Error => e+ Rails.logger.warn("Discourse AI search intent failed: #{e.class}")+ nil+ end++ private++ # Some models report confidence as certainty across all choices, which is far+ # lower than the chosen one's probability, so the probability is what is kept.+ def chosen(answer)+ return if answer.refusal?++ [answer.choice, answer.probabilities.fetch(answer.choice)]+ end++ # Personal messages are left out: their titles are private to a few people.+ # a category with tags is described by its category+ def searched_place(scope)+ type, id = scope.to_s.split(" ").first.to_s.split(":", 2)+ case type+ when "topic"+ topic = Topic.find_by(id:)+ if topic && !topic.private_message? && @user.guardian.can_see?(topic)+ { type: "topic", title: topic.title }+ end+ when "category"+ category = Category.find_by(id:)+ { type: "category", name: category.name } if category && @user.guardian.can_see?(category)+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/discoveries/retrieval.rb b/plugins/discourse-ai/lib/discoveries/retrieval.rbindex 93a0d817daf..c7beb960cc1 100644--- a/plugins/discourse-ai/lib/discoveries/retrieval.rb+++ b/plugins/discourse-ai/lib/discoveries/retrieval.rb@@ -11,7 +11,7 @@ module DiscourseAiSEMANTIC_PRIVATE_MESSAGE_FILTER = /(?:\A|\s)in:(?:messages|personal)(?=\s|\z)/iResult =- Struct.new(:candidates, :private_messages, keyword_init: true) do+ Struct.new(:candidates, :private_messages, :scope_fallback, keyword_init: true) dodef synthesis_candidatescandidates.first(Retrieval::SYNTHESIS_LIMIT)end@@ -24,19 +24,48 @@ module DiscourseAi@semantic_retriever = semantic_retrieverend- def call(query, keyword_query: query, semantic_query: query)+ # A scope filter narrows both retrievals. When nothing matches inside it,+ # the whole site is searched instead and the result says so.+ def call(query, keyword_query: query, semantic_query: query, scope_filter: nil)+ if scope_filter.present?+ scoped = retrieve_candidates(query, keyword_query:, semantic_query:, scope_filter:)+ return scoped if scoped.candidates.present?++ fallback = retrieve_candidates(query, keyword_query:, semantic_query:)+ fallback.scope_fallback = true+ return fallback+ end++ retrieve_candidates(query, keyword_query:, semantic_query:)+ end++ def retrieve_candidates(query, keyword_query:, semantic_query:, scope_filter: nil)private_messages =DiscourseAi::Discoveries.private_message_query?(query) ||- DiscourseAi::Discoveries.private_message_query?(keyword_query)+ DiscourseAi::Discoveries.private_message_query?(keyword_query) ||+ DiscourseAi::Discoveries.private_message_query?(scope_filter)+ # embeddings describe whole topics, so they cannot narrow within one;+ # a topic is searched post by post instead+ topic_scope = scope_filter.to_s.start_with?("topic:")+ # a personal message is searched as one, or its posts are filtered out+ if topic_scope &&+ Topic.where(+ id: scope_filter.delete_prefix("topic:").to_i,+ archetype: Archetype.private_message,+ ).exists?+ private_messages = true+ end+ @per_post = topic_scope+ scoped = ->(search_query) { [search_query, scope_filter].compact_blank.join(" ") }rankings =if self.class.explicit_filters_except_private_messages?(query)- [retrieve(@lexical_retriever, query)]- elsif !DiscourseAi::Embeddings.enabled? || semantic_query.blank? ||+ [retrieve(@lexical_retriever, scoped.(query))]+ elsif topic_scope || !DiscourseAi::Embeddings.enabled? || semantic_query.blank? ||self.class.explicit_filters_except_private_messages?(keyword_query)- [retrieve(@lexical_retriever, keyword_query)]+ [retrieve(@lexical_retriever, scoped.(keyword_query))]else- retrieve_in_parallel(keyword_query, semantic_query, private_messages:)+ retrieve_in_parallel(scoped.(keyword_query), scoped.(semantic_query), private_messages:)endcandidates = reciprocal_rank_fusion(rankings)candidates = revalidate_and_limit(candidates, private_messages:)@@ -101,7 +130,7 @@ module DiscourseAiposts.filter_map do |post|- next if seen_topic_ids.include?(post.topic_id)+ next if !@per_post && seen_topic_ids.include?(post.topic_id)seen_topic_ids << post.topic_idsource_from_post(post, excerpt: results.blurb(post, scope: guardian))@@ -303,6 +332,7 @@ module DiscourseAi"passages" => passages,"post_updated_at" => post.updated_at.iso8601(6),"topic_updated_at" => topic.updated_at.iso8601(6),+ "topic_bumped_at" => topic.bumped_at&.iso8601,)end.first(CANDIDATE_LIMIT)diff --git a/plugins/discourse-ai/lib/discoveries/synthesis.rb b/plugins/discourse-ai/lib/discoveries/synthesis.rbindex 4e9b776dbe9..5361bce6b68 100644--- a/plugins/discourse-ai/lib/discoveries/synthesis.rb+++ b/plugins/discourse-ai/lib/discoveries/synthesis.rb@@ -58,7 +58,7 @@ module DiscourseAibot =DiscourseAi::Agents::Bot.as(Discourse.system_user,- agent: @ai_agent.class_instance.new,+ agent: agent_for(related_count),model: @llm_model,)values = { answerable: nil, source_refs: nil, title: +"", answer: +"", follow_up: +"" }@@ -110,6 +110,18 @@ module DiscourseAiprivate+ # The agent's stored format caps the sources at the shared setting, which+ # would cut short a request that asks for more, so it follows the request.+ def agent_for(related_count)+ agent = @ai_agent.class_instance.new+ format =+ agent.response_format&.map do |field|+ field["key"] == "source_refs" ? field.merge("max_items" => related_count) : field+ end+ agent.define_singleton_method(:response_format) { format } if format+ agent+ end+def valid_result?(result, candidates:, related_count:)return false if result.source_refs.empty?return false if result.source_refs.uniq.length != result.source_refs.lengthdiff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rbindex 063e9fa5e71..f9201cc0061 100644--- a/plugins/discourse-ai/plugin.rb+++ b/plugins/discourse-ai/plugin.rb@@ -41,6 +41,7 @@ register_asset "stylesheets/modules/admin-dashboard/common/admin-dashboard-highlregister_asset "stylesheets/modules/ai-bot/common/bot-replies.scss"register_asset "stylesheets/modules/ai-bot/common/ai-agent.scss"register_asset "stylesheets/modules/ai-bot/common/ai-discobot-discoveries.scss"+register_asset "stylesheets/modules/ai-bot/common/ai-search.scss"register_asset "stylesheets/modules/ai-bot/mobile/ai-agent.scss", :mobileregister_asset "stylesheets/modules/ai-bot-conversations/common.scss"diff --git a/plugins/discourse-ai/spec/jobs/regular/stream_discover_reply_spec.rb b/plugins/discourse-ai/spec/jobs/regular/stream_discover_reply_spec.rbindex 8d3e5a68626..29f2fa896dd 100644--- a/plugins/discourse-ai/spec/jobs/regular/stream_discover_reply_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/stream_discover_reply_spec.rb@@ -27,6 +27,7 @@ describe Jobs::StreamDiscoverReply do"category" => "Developer","category_id" => 3,"topic_replies" => 4,+ "topic_bumped_at" => "2026-10-01T12:00:00Z","username" => source_post.user.username,"name" => source_post.user.name,"avatar_template" => source_post.user.avatar_template,@@ -144,6 +145,7 @@ describe Jobs::StreamDiscoverReply docategory: candidate["category"],category_id: candidate["category_id"],topic_replies: candidate["topic_replies"],+ bumped_at: candidate["topic_bumped_at"],username: candidate["username"],name: candidate["name"],avatar_template: candidate["avatar_template"],@@ -252,6 +254,54 @@ describe Jobs::StreamDiscoverReply do)end+ it "publishes rewritten queries before retrieval when they differ from the query" do+ allow(query_rewriter).to receive(:call).with(query).and_return(+ DiscourseAi::Discoveries::QueryRewriter::Result.new(+ keyword_query: "create plugin",+ semantic_query: "how to create a Discourse plugin",+ original_query_locale: "en",+ ),+ )++ messages =+ MessageBus+ .track_publish("/discourse-ai/discoveries") do+ job.execute(user_id: user.id, query:, request_id:)+ end+ .map(&:data)++ expect(messages.map { |message| message[:phase] }.first(2)).to eq(%w[searching rewritten])+ expect(messages.second).to include(+ keyword_query: "create plugin",+ semantic_query: "how to create a Discourse plugin",+ )+ end++ it "retrieves within the scope and says when it had to search the whole site" do+ allow(retrieval).to receive(:call).and_return(+ DiscourseAi::Discoveries::Retrieval::Result.new(+ candidates: [candidate],+ scope_fallback: true,+ ),+ )+ allow(retrieval).to receive(:validated_sources).and_return([candidate])++ messages =+ MessageBus+ .track_publish("/discourse-ai/discoveries") do+ job.execute(user_id: user.id, query:, request_id:, scope: "category:7")+ end+ .map(&:data)++ expect(retrieval).to have_received(:call).with(+ query,+ keyword_query: query,+ semantic_query: query,+ scope_filter: "category:7",+ )+ expect(messages.last).to include(phase: "complete", scope_fallback: true)+ end+it "uses the original query when the configured rewrite agent is unavailable" doSiteSetting.ai_ask_ai_query_rewriter_agent = 99_999diff --git a/plugins/discourse-ai/spec/lib/discoveries/cited_topics_spec.rb b/plugins/discourse-ai/spec/lib/discoveries/cited_topics_spec.rbnew file mode 100644index 00000000000..1eafcd4b027--- /dev/null+++ b/plugins/discourse-ai/spec/lib/discoveries/cited_topics_spec.rb@@ -0,0 +1,53 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Discoveries::CitedTopics do+ subject(:cited_topics) do+ described_class.new(topic: conversation, guardian: user.guardian).as_json+ end++ fab!(:user)+ fab!(:bot, :user)+ fab!(:conversation) { Fabricate(:private_message_topic, user:, recipient: bot) }+ fab!(:source, :topic)+ fab!(:first_cited, :topic)+ fab!(:second_cited, :topic)+ fab!(:hidden) do+ Fabricate(:topic, category: Fabricate(:private_category, group: Fabricate(:group)))+ end++ before do+ enable_current_plugin+ conversation.custom_fields[+ DiscourseAi::Discoveries::ContinueConversation::TOPIC_SOURCES_FIELD+ ] = [{ "topic_id" => source.id, "title" => source.title, "url" => source.url }]+ conversation.save_custom_fields++ Fabricate(:post, topic: conversation, user:, raw: "How do I reset my password?")+ Fabricate(:post, topic: conversation, user: bot, raw: <<~MD)+ See [the guide](#{first_cited.url}), [a hidden one](#{hidden.url}) and+ [elsewhere](https://example.com/t/some-topic/#{second_cited.id}).+ MD+ Fabricate(:post, topic: conversation, user:, raw: "And two factor?")+ Fabricate(:post, topic: conversation, user: bot, raw: "Try [this](#{second_cited.url}).")+ end++ it "lists the answer's sources, then what each reply links to, by the reply that cited it" do+ expect(cited_topics[:replies]).to eq(2)+ expect(cited_topics[:topics].map { |topic| topic.slice(:topic_id, :turn) }).to eq(+ [+ { topic_id: source.id, turn: 0 },+ { topic_id: first_cited.id, turn: 0 },+ { topic_id: second_cited.id, turn: 1 },+ ],+ )+ end++ it "gives what a topic list row shows of each topic" do+ expect(cited_topics[:topics].first).to include(+ title: source.title,+ url: source.relative_url,+ category_id: source.category_id,+ reply_count: source.posts_count - 1,+ )+ end+enddiff --git a/plugins/discourse-ai/spec/lib/discoveries/intent_spec.rb b/plugins/discourse-ai/spec/lib/discoveries/intent_spec.rbnew file mode 100644index 00000000000..d97056a616c--- /dev/null+++ b/plugins/discourse-ai/spec/lib/discoveries/intent_spec.rb@@ -0,0 +1,123 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Discoveries::Intent do+ fab!(:user)+ fab!(:decision_model)++ before { enable_current_plugin }++ describe ".decision_model" do+ it "is the model chosen in settings" do+ SiteSetting.ai_ask_ai_search_intent_decision_model = decision_model.id++ expect(described_class.decision_model).to eq(decision_model)+ end++ it "is nil when none is chosen" do+ expect(described_class.decision_model).to be_nil+ end+ end++ describe "#call" do+ subject(:intent) { described_class.new(user:, decision_model:) }++ it "returns the model's choice and its probability" do+ DiscourseAi::Decisions.with_canned_answers(+ intent: {+ "answer" => 0.8,+ "lookup" => 0.2,+ },+ ) do |canned|+ expect(intent.call("how do I reset my password").as_json).to eq(+ intent: "answer",+ probability: 0.8,+ )+ expect(canned.calls.sole).to include(+ state: {+ query: "how do I reset my password",+ },+ feature_name: "ai_search_intent",+ user:,+ )+ end+ end++ context "when searching in a topic or category" do+ fab!(:category) { Fabricate(:category, name: "Self-hosting") }+ fab!(:topic) { Fabricate(:topic, category:, title: "Upgrading to PostgreSQL 18") }++ it "also asks whether the search is about that place, naming it" do+ DiscourseAi::Decisions.with_canned_answers(+ intent: "answer",+ scope: {+ "here" => 0.2,+ "site" => 0.8,+ },+ ) do |canned|+ expect(intent.call("install discourse", scope: "topic:#{topic.id}").as_json).to eq(+ intent: "answer",+ probability: 1.0,+ scope: "site",+ scope_probability: 0.8,+ )+ expect(canned.calls.sole[:state]).to eq(+ query: "install discourse",+ searching_in: {+ type: "topic",+ title: topic.title,+ },+ )+ end+ end++ it "names a category by its name" do+ DiscourseAi::Decisions.with_canned_answers(intent: "lookup", scope: "here") do |canned|+ intent.call("backups", scope: "category:#{category.id}")++ expect(canned.calls.sole[:state][:searching_in]).to eq(+ type: "category",+ name: "Self-hosting",+ )+ end+ end++ it "keeps one verdict when the other is refused" do+ DiscourseAi::Decisions.with_canned_answers(intent: :refusal, scope: "here") do+ expect(intent.call("backups", scope: "topic:#{topic.id}").as_json).to eq(+ scope: "here",+ scope_probability: 1.0,+ )+ end+ end++ it "leaves out personal messages and places the reader cannot see" do+ message = Fabricate(:private_message_topic, user:)+ hidden = Fabricate(:private_category, group: Fabricate(:group))++ DiscourseAi::Decisions.with_canned_answers(intent: "lookup") do |canned|+ intent.call("notes", scope: "topic:#{message.id}")+ intent.call("notes", scope: "category:#{hidden.id}")++ expect(canned.calls.map { |call| call[:state] }).to all(eq(query: "notes"))+ expect(canned.calls.map { |call| call[:questions].map(&:name) }).to all(eq(["intent"]))+ end+ end+ end++ it "returns nil when the model refuses" do+ DiscourseAi::Decisions.with_canned_answers(intent: :refusal) do+ expect(intent.call("password")).to be_nil+ end+ end++ it "returns nil without a decision model" do+ expect(described_class.new(user:, decision_model: nil).call("password")).to be_nil+ end++ it "returns nil when the request fails" do+ stub_request(:post, decision_model.url).to_return(status: 500, body: "")++ expect(intent.call("password")).to be_nil+ end+ end+enddiff --git a/plugins/discourse-ai/spec/lib/discoveries/retrieval_spec.rb b/plugins/discourse-ai/spec/lib/discoveries/retrieval_spec.rbindex d6b972df063..4294f5ac69c 100644--- a/plugins/discourse-ai/spec/lib/discoveries/retrieval_spec.rb+++ b/plugins/discourse-ai/spec/lib/discoveries/retrieval_spec.rb@@ -34,6 +34,87 @@ describe DiscourseAi::Discoveries::Retrieval doenddescribe "#call" do+ context "with a scope" do+ it "narrows both retrievals to the scope" do+ lexical_retriever = instance_spy(Proc, call: [source(post_1)])+ semantic_retriever = instance_spy(Proc, call: [source(post_2)])++ result =+ described_class.new(user:, lexical_retriever:, semantic_retriever:).call(+ "how do I reset my password",+ keyword_query: "reset password",+ semantic_query: "How can I reset a forgotten password?",+ scope_filter: "category:#{category.id}",+ )++ expect(lexical_retriever).to have_received(:call).with(+ "reset password category:#{category.id}",+ )+ expect(semantic_retriever).to have_received(:call).with(+ "How can I reset a forgotten password? category:#{category.id}",+ )+ expect(result.scope_fallback).to be_nil+ end++ it "searches the whole site when nothing matches inside the scope" do+ lexical_retriever = ->(query) { query.include?("category:") ? [] : [source(post_1)] }++ result =+ described_class.new(+ user:,+ lexical_retriever:,+ semantic_retriever: ->(_query) { [] },+ ).call("reset password", scope_filter: "category:#{category.id}")++ expect(result.candidates.map { |candidate| candidate.fetch("topic_id") }).to eq(+ [topic_1.id],+ )+ expect(result.scope_fallback).to eq(true)+ end++ it "searches a personal message the reader is in as a message" do+ message = Fabricate(:private_message_topic, user:)+ message_post = Fabricate(:post, topic: message, user:, raw: "Widget reset steps")++ result =+ described_class.new(user:, lexical_retriever: ->(_query) { [source(message_post)] }).call(+ "widget",+ scope_filter: "topic:#{message.id}",+ )++ expect(result.candidates.map { |candidate| candidate.fetch("topic_id") }).to eq(+ [message.id],+ )+ expect(result.scope_fallback).to be_nil+ end++ it "searches a topic post by post, without semantic retrieval" do+ SearchIndexer.enable+ widget_posts = [+ Fabricate(:post, topic: topic_1, raw: "Widgets need a reset after updating"),+ Fabricate(:post, topic: topic_1, raw: "Another widget tip: clear the cache"),+ ]+ widget_posts.each { |post| SearchIndexer.index(post, force: true) }+ semantic_retriever = instance_spy(Proc, call: [source(post_2)])++ result =+ described_class.new(user:, semantic_retriever:).call(+ "widget",+ scope_filter: "topic:#{topic_1.id}",+ )++ expect(semantic_retriever).not_to have_received(:call)+ expect(result.candidates.map { |candidate| candidate.fetch("topic_id") }).to eq(+ [topic_1.id],+ )+ expect(result.candidates.first.fetch("passages").pluck("post_id")).to match_array(+ widget_posts.map(&:id),+ )+ ensure+ SearchIndexer.disable+ end+ end+it "clears names from candidates when names are disabled" doSiteSetting.enable_names = falsepost_1.user.update!(name: "Hidden Author")diff --git a/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb b/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rbindex 445d32bfeae..0dc903aa96a 100644--- a/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb+++ b/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb@@ -23,6 +23,25 @@ describe DiscourseAi::Discoveries::Synthesis dobefore { enable_current_plugin }+ it "lets the model return as many sources as the request asks for" do+ agent = nil+ allow(DiscourseAi::Agents::Bot).to receive(:as) do |_user, **kwargs|+ agent = kwargs[:agent]+ raise StopIteration+ end++ expect {+ synthesis.call(+ query: "plugins",+ candidates: [{ "source_ref" => "source_1", "title" => "Plugins", "url" => "/t/plugins/1" }],+ related_count: 6,+ )+ }.to raise_error(StopIteration)++ source_refs = agent.response_format.find { |field| field["key"] == "source_refs" }+ expect(source_refs["max_items"]).to eq(6)+ end+it "uses one tool-free structured call to select sources and stream an answer" doSiteSetting.ai_ask_ai_related_count = 4feature_name = nildiff --git a/plugins/discourse-ai/spec/lib/discoveries_spec.rb b/plugins/discourse-ai/spec/lib/discoveries_spec.rbindex ebb8e8cd389..67c21544cd4 100644--- a/plugins/discourse-ai/spec/lib/discoveries_spec.rb+++ b/plugins/discourse-ai/spec/lib/discoveries_spec.rb@@ -63,6 +63,26 @@ describe DiscourseAi::Discoveries doendend+ describe ".scope_filter" do+ it "turns a scope into the search filter it stands for" do+ expect(described_class.scope_filter("messages")).to eq("in:messages")+ expect(described_class.scope_filter("topic:12")).to eq("topic:12")+ expect(described_class.scope_filter("category:3")).to eq("category:3")+ expect(described_class.scope_filter("tag:how-to")).to eq("tags:how-to")+ expect(described_class.scope_filter("tag:node.js")).to eq("tags:node.js")+ expect(described_class.scope_filter("user:sam.s")).to eq("user:sam.s")+ expect(described_class.scope_filter("tag:how-to+faq")).to eq("tags:how-to+faq")+ expect(described_class.scope_filter("category:3 tag:how-to")).to eq("category:3 tags:how-to")+ end++ it "ignores anything that is not a scope" do+ expect(described_class.scope_filter(nil)).to be_nil+ expect(described_class.scope_filter("category:3 in:all")).to be_nil+ expect(described_class.scope_filter("order:latest")).to be_nil+ expect(described_class.scope_filter("topic:abc")).to be_nil+ end+ end+describe ".enqueue_reply" doit "records a failed ask if the job cannot be queued" doallow(Jobs).to receive(:enqueue).and_raise(StandardError, "queue unavailable")@@ -87,6 +107,66 @@ describe DiscourseAi::Discoveries doexpect(described_class.result_settings).to eq(summary_detail: :detailed, related_count: 5)end++ it "asks for the most related topics for readers with the combined search" do+ SiteSetting.ai_ask_ai_related_count = 2+ SiteSetting.enable_ai_combined_search = true++ expect(described_class.result_settings(user)[:related_count]).to eq(+ described_class::MAX_RELATED_DISCUSSIONS,+ )+ end++ it "keeps the shared count for readers outside the combined search's groups" do+ SiteSetting.ai_ask_ai_related_count = 2+ SiteSetting.enable_ai_combined_search = true+ Fabricate(+ :site_setting_group,+ name: "enable_ai_combined_search",+ group_ids: Fabricate(:group).id.to_s,+ )++ expect(described_class.result_settings(user)[:related_count]).to eq(2)+ end+ end++ describe ".prefers_results?" do+ def ask(opened:, switched_to: "", after_ms: 0, at: Time.current)+ AskAiLog.create!(+ user:,+ query: "q",+ asked_at: at,+ ask_tab_opened: opened,+ ask_tab_switched_to: switched_to,+ ask_tab_switched_after_ms: after_ms,+ )+ end++ it "is true when the reader mostly leaves the answer quickly" do+ 3.times { ask(opened: "ask", switched_to: "topics", after_ms: 2000) }+ ask(opened: "ask")++ expect(described_class.prefers_results?(user)).to eq(true)+ end++ it "needs enough asks opened on the answer to judge" do+ 3.times { ask(opened: "ask", switched_to: "topics", after_ms: 2000) }++ expect(described_class.prefers_results?(user)).to eq(false)+ end++ it "ignores switches made after reading the answer for a while" do+ 4.times { ask(opened: "ask", switched_to: "topics", after_ms: 30_000) }++ expect(described_class.prefers_results?(user)).to eq(false)+ end++ it "is false once the reader keeps coming back to the answer" do+ 4.times { ask(opened: "ask", switched_to: "topics", after_ms: 2000, at: 1.day.ago) }+ 2.times { ask(opened: "topics", switched_to: "ask", after_ms: 2000) }++ expect(described_class.prefers_results?(user)).to eq(false)+ endenddescribe ".record_recent_ask" do@@ -118,9 +198,9 @@ describe DiscourseAi::Discoveries doexpect(described_class.bind_request(user_id: user.id, request_id:, query: "猫 search"),).to eq(:existing)- expect {- described_class.bind_request(user_id: user.id, request_id:, query: "dogs")- }.to raise_error(DiscourseAi::Discoveries::RequestConflict)+ expect(described_class.bind_request(user_id: user.id, request_id:, query: "dogs")).to eq(+ :conflict,+ )endit "makes the newest request active without allowing an old retry to reclaim ownership" dodiff --git a/plugins/discourse-ai/spec/models/ask_ai_log_spec.rb b/plugins/discourse-ai/spec/models/ask_ai_log_spec.rbnew file mode 100644index 00000000000..66b525de672--- /dev/null+++ b/plugins/discourse-ai/spec/models/ask_ai_log_spec.rb@@ -0,0 +1,43 @@+# frozen_string_literal: true++RSpec.describe AskAiLog do+ describe "#record_judgement!" do+ fab!(:user)++ let(:ask_log) { AskAiLog.create!(user:, query: "password reset", asked_at: Time.current) }++ before { enable_current_plugin }++ it "records the scope and the decision model's verdicts" do+ judgement =+ DiscourseAi::Discoveries::Intent::Judgement.new(+ intent: "lookup",+ probability: 0.7,+ scope: "site",+ scope_probability: 0.9,+ )++ ask_log.record_judgement!(scope: "topic:12", judgement:)++ expect(ask_log.reload).to have_attributes(+ ask_scope: "topic:12",+ ask_intent_model: "lookup",+ ask_intent_probability: 0.7,+ ask_scope_model: "site",+ ask_scope_probability: 0.9,+ )+ end++ it "records the scope alone when nothing was judged" do+ ask_log.record_judgement!(scope: "topic:12", judgement: nil)++ expect(ask_log.reload).to have_attributes(+ ask_scope: "topic:12",+ ask_intent_model: "",+ ask_intent_probability: 0,+ ask_scope_model: "",+ ask_scope_probability: 0,+ )+ end+ end+enddiff --git a/plugins/discourse-ai/spec/requests/discover/discoveries_controller_spec.rb b/plugins/discourse-ai/spec/requests/discover/discoveries_controller_spec.rbindex 47616ded209..ddbcfbff62a 100644--- a/plugins/discourse-ai/spec/requests/discover/discoveries_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/discover/discoveries_controller_spec.rb@@ -57,6 +57,66 @@ describe DiscourseAi::Discover::DiscoveriesController dogroup.add(user)end+ it "passes the scope the question was asked from to the job" do+ expect_enqueued_with(+ job: :stream_discover_reply,+ args: {+ user_id: user.id,+ request_id:,+ scope: "category:12",+ },+ ) do+ post "/discourse-ai/discoveries/reply",+ params: {+ query: "What is Discourse?",+ request_id:,+ scope: "category:12",+ }+ end++ expect(response.status).to eq(200)+ end++ it "records what set off the request" do+ post "/discourse-ai/discoveries/reply",+ params: {+ query: "What is Discourse?",+ request_id:,+ trigger: "pause",+ trigger_reason: "after_skip:places",+ }++ expect(response.status).to eq(200)+ expect(AskAiLog.last).to have_attributes(+ ask_trigger: "pause",+ ask_trigger_reason: "after_skip:places",+ )+ end++ it "ignores a trigger it does not know rather than failing the request" do+ post "/discourse-ai/discoveries/reply",+ params: {+ query: "What is Discourse?",+ request_id:,+ trigger: "telepathy",+ trigger_reason: "<script>",+ }++ expect(response.status).to eq(200)+ expect(AskAiLog.last).to have_attributes(ask_trigger: "", ask_trigger_reason: "")+ end++ it "rejects a scope that is not one" do+ post "/discourse-ai/discoveries/reply",+ params: {+ query: "What is Discourse?",+ request_id:,+ scope: "category:12 in:all",+ }++ expect(response.status).to eq(400)+ end+it "returns a 200 and queues a job to reply" doSiteSetting.ai_ask_ai_summary_detail = "detailed"SiteSetting.ai_ask_ai_related_count = 5@@ -165,6 +225,252 @@ describe DiscourseAi::Discover::DiscoveriesController doendend+ describe "#reply with search intent" do+ fab!(:decision_model)+ fab!(:llm_model)+ fab!(:ai_agent) do+ Fabricate(+ :ai_agent,+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],+ default_llm_id: llm_model.id,+ )+ end+ let(:request_id) { SecureRandom.uuid }++ before do+ Group.refresh_automatic_groups!+ SiteSetting.ai_ask_ai_agent = ai_agent.id+ SiteSetting.ai_ask_ai_search_intent_decision_model = decision_model.id+ SiteSetting.enable_ai_combined_search = true+ end++ it "returns the decision model's judgement and logs it beside the heuristic's" do+ DiscourseAi::Decisions.with_canned_answers(intent: { "answer" => 0.3, "lookup" => 0.7 }) do+ post "/discourse-ai/discoveries/reply",+ params: {+ query: "password reset",+ request_id:,+ intent_heuristic: "answer",+ classify_intent: true,+ }+ end++ expect(response.status).to eq(200)+ expect(response.parsed_body["intent"]).to eq("intent" => "lookup", "probability" => 0.7)+ expect(response.parsed_body["ask_log_id"]).to eq(AskAiLog.last.id)+ expect(AskAiLog.last).to have_attributes(+ ask_intent_heuristic: "answer",+ ask_intent_model: "lookup",+ ask_intent_probability: 0.7,+ )+ end++ it "answers a scoped search from the whole site when the model says it is about the site" do+ topic = Fabricate(:topic, title: "Upgrading to PostgreSQL 18")++ DiscourseAi::Decisions.with_canned_answers(+ intent: "answer",+ scope: {+ "here" => 0.1,+ "site" => 0.9,+ },+ ) do+ expect_enqueued_with(job: :stream_discover_reply, args: { scope: nil }) do+ post "/discourse-ai/discoveries/reply",+ params: {+ query: "install discourse",+ request_id:,+ scope: "topic:#{topic.id}",+ classify_intent: true,+ }+ end+ end++ expect(response.parsed_body["intent"]).to include(+ "scope" => "site",+ "scope_probability" => 0.9,+ )+ expect(AskAiLog.last).to have_attributes(+ ask_scope: "topic:#{topic.id}",+ ask_scope_model: "site",+ ask_scope_probability: 0.9,+ ask_intent_model: "answer",+ )+ end++ it "keeps the scope for a search about the place it was made in" do+ topic = Fabricate(:topic, title: "Upgrading to PostgreSQL 18")++ DiscourseAi::Decisions.with_canned_answers(intent: "lookup", scope: "here") do+ expect_enqueued_with(job: :stream_discover_reply, args: { scope: "topic:#{topic.id}" }) do+ post "/discourse-ai/discoveries/reply",+ params: {+ query: "disk space",+ request_id:,+ scope: "topic:#{topic.id}",+ classify_intent: true,+ }+ end+ end+ end++ it "does not judge for readers without the combined search" do+ SiteSetting.enable_ai_combined_search = false++ DiscourseAi::Decisions.with_canned_answers(intent: "answer") do+ post "/discourse-ai/discoveries/reply",+ params: {+ query: "password reset",+ request_id:,+ classify_intent: true,+ }+ end++ expect(response.status).to eq(200)+ expect(response.parsed_body).not_to have_key("intent")+ expect(AskAiLog.last).to have_attributes(ask_intent_model: "")+ end++ it "does not judge a scoped search again when its request is repeated" do+ topic = Fabricate(:topic)+ params = {+ query: "disk space",+ request_id:,+ scope: "topic:#{topic.id}",+ classify_intent: true,+ }+ DiscourseAi::Decisions.with_canned_answers(intent: "lookup", scope: "here") do+ post("/discourse-ai/discoveries/reply", params:)+ post("/discourse-ai/discoveries/reply", params:)+ end++ expect(response.status).to eq(200)+ expect(AskAiLog.where(user:).count).to eq(1)+ expect(response.parsed_body).not_to have_key("intent")+ end++ it "says the ask failed, rather than answering nothing, when its answer cannot be queued" do+ allow(Jobs).to receive(:enqueue).and_raise(StandardError, "queue unavailable")++ post "/discourse-ai/discoveries/reply", params: { query: "password reset", request_id: }++ expect(response.status).to eq(422)+ expect(response.parsed_body["errors"]).to contain_exactly(+ I18n.t("discourse_ai.ai_bot.discoveries.errors.processing_failed"),+ )+ expect(AskAiLog.last).to be_ask_outcome_failed+ end++ it "says when the reader prefers results in the combined search" do+ 4.times do+ AskAiLog.create!(+ user:,+ query: "q",+ asked_at: 1.hour.ago,+ ask_tab_opened: "ask",+ ask_tab_switched_to: "topics",+ ask_tab_switched_after_ms: 1000,+ )+ end++ post "/discourse-ai/discoveries/reply", params: { query: "password reset", request_id: }++ expect(response.parsed_body["prefers_results"]).to eq(true)+ end++ it "logs only the heuristic when no judgement is asked for" do+ post "/discourse-ai/discoveries/reply",+ params: {+ query: "password reset",+ request_id:,+ intent_heuristic: "lookup",+ }++ expect(response.status).to eq(200)+ expect(response.parsed_body).not_to have_key("intent")+ expect(AskAiLog.last).to have_attributes(ask_intent_heuristic: "lookup", ask_intent_model: "")+ end++ it "ignores an unknown heuristic and a missing decision model" do+ SiteSetting.ai_ask_ai_search_intent_decision_model = ""++ post "/discourse-ai/discoveries/reply",+ params: {+ query: "password reset",+ request_id:,+ intent_heuristic: "whatever",+ classify_intent: true,+ }++ expect(response.status).to eq(200)+ expect(response.parsed_body).not_to have_key("intent")+ expect(AskAiLog.last).to have_attributes(ask_intent_heuristic: "", ask_intent_model: "")+ end+ end++ describe "#record_tabs" do+ fab!(:llm_model)+ fab!(:ai_agent) do+ Fabricate(+ :ai_agent,+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],+ default_llm_id: llm_model.id,+ )+ end+ fab!(:ask_log) { AskAiLog.create!(user:, query: "password reset", asked_at: Time.current) }++ before do+ Group.refresh_automatic_groups!+ SiteSetting.ai_ask_ai_agent = ai_agent.id+ end++ it "records the opened tab and the first switch, keeping each first report" do+ put "/discourse-ai/discoveries/tabs",+ params: {+ ask_log_id: ask_log.id,+ tab_opened: "topics",+ tab_opened_reason: "topic_results",+ }+ expect(response.status).to eq(204)++ put "/discourse-ai/discoveries/tabs",+ params: {+ ask_log_id: ask_log.id,+ tab_switched_to: "ask",+ tab_switched_after_ms: 1200,+ }+ put "/discourse-ai/discoveries/tabs",+ params: {+ ask_log_id: ask_log.id,+ tab_switched_to: "context",+ tab_switched_after_ms: 5000,+ }++ expect(ask_log.reload).to have_attributes(+ ask_tab_opened: "topics",+ ask_tab_opened_reason: "topic_results",+ ask_tab_switched_to: "ask",+ ask_tab_switched_after_ms: 1200,+ )+ end++ it "rejects an unknown tab" do+ put "/discourse-ai/discoveries/tabs", params: { ask_log_id: ask_log.id, tab_opened: "nope" }++ expect(response.status).to eq(400)+ expect(ask_log.reload.ask_tab_opened).to eq("")+ end++ it "does not touch another user's ask" do+ other_log = AskAiLog.create!(user: Fabricate(:user), query: "x", asked_at: Time.current)++ put "/discourse-ai/discoveries/tabs", params: { ask_log_id: other_log.id, tab_opened: "ask" }++ expect(response.status).to eq(404)+ expect(other_log.reload.ask_tab_opened).to eq("")+ end+ end+describe "#continue_convo" dofab!(:group)fab!(:llm_model)@@ -211,6 +517,26 @@ describe DiscourseAi::Discover::DiscoveriesController doexpect(response.parsed_body["topic_id"]).to be_presentend+ it "marks the conversation as continued from a search for the topic view" do+ post "/discourse-ai/discoveries/continue-convo", params: { request_id: }+ get "/t/#{response.parsed_body["topic_id"]}.json"++ expect(response.parsed_body["ai_search_conversation"]).to eq(true)+ end++ it "leaves the sources to the sidebar for readers of the combined search" do+ SiteSetting.enable_ai_combined_search = true++ post "/discourse-ai/discoveries/continue-convo", params: { request_id: }+ topic = Topic.find(response.parsed_body["topic_id"])++ expect(topic.posts.find_by(post_number: 2).raw).to eq(context)+ get "/t/#{topic.id}.json"+ expect(response.parsed_body.dig("ai_search_cited_topics", "topics")).to contain_exactly(+ include("topic_id" => source_post.topic_id, "title" => source_post.topic.title),+ )+ end+it "limits the private message title to the configured maximum" dolong_query = "猫" * 1000DiscourseAi::Discoveries.store_result(diff --git a/plugins/discourse-ai/spec/services/discourse_ai/ask_ai/ask_spec.rb b/plugins/discourse-ai/spec/services/discourse_ai/ask_ai/ask_spec.rbnew file mode 100644index 00000000000..3ff3de95668--- /dev/null+++ b/plugins/discourse-ai/spec/services/discourse_ai/ask_ai/ask_spec.rb@@ -0,0 +1,373 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::AskAi::Ask do+ describe described_class::Contract, type: :model do+ it { is_expected.to validate_presence_of(:query) }++ it do+ is_expected.to validate_length_of(:query).is_at_most(+ DiscourseAi::Discoveries::MAX_QUERY_LENGTH,+ )+ end++ it { is_expected.not_to allow_value("pass\0word").for(:query) }+ it { is_expected.to allow_value(SecureRandom.uuid).for(:request_id) }+ it { is_expected.not_to allow_values(nil, "abc", "123").for(:request_id) }+ it { is_expected.to allow_values(nil, "topic:12", "category:3 tag:howto").for(:scope) }+ it { is_expected.not_to allow_values("topic:abc", "in:messages").for(:scope) }++ describe "normalisation" do+ it "strips the query and drops what is not recognised" do+ contract =+ described_class.new(+ query: " password reset ",+ scope: "",+ trigger: "shout",+ trigger_reason: "Not A Reason!",+ intent_heuristic: "maybe",+ )++ contract.valid?++ expect(contract).to have_attributes(+ query: "password reset",+ scope: nil,+ trigger: "",+ trigger_reason: "",+ intent_heuristic: "",+ )+ end++ it "keeps recognised values" do+ attributes = {+ query: "password reset",+ scope: "topic:12",+ trigger: "enter",+ trigger_reason: "typed:pause",+ intent_heuristic: "answer",+ }+ contract = described_class.new(**attributes)++ contract.valid?++ expect(contract).to have_attributes(attributes)+ end+ end+ end++ describe ".call" do+ subject(:result) { described_class.call(params:, **dependencies) }++ fab!(:group)+ fab!(:user) { Fabricate(:user).tap { |member| group.add(member) } }+ fab!(:llm_model)+ fab!(:ai_agent) do+ Fabricate(:ai_agent, allowed_group_ids: [group.id], default_llm_id: llm_model.id)+ end+ fab!(:decision_model)+ fab!(:topic)++ let(:params) do+ {+ query:,+ request_id:,+ scope:,+ classify_intent:,+ trigger: "enter",+ trigger_reason: "typed",+ intent_heuristic: "answer",+ }+ end+ let(:dependencies) { { guardian: user.guardian } }+ let(:query) { "How do I reset my password?" }+ let(:request_id) { SecureRandom.uuid }+ let(:scope) { nil }+ let(:classify_intent) { false }+ let(:ask_log) { AskAiLog.find_by(user:, query:) }++ before do+ enable_current_plugin+ SiteSetting.ai_ask_ai_enabled = true+ SiteSetting.ai_ask_ai_agent = ai_agent.id+ SiteSetting.ai_ask_ai_allowed_groups = group.id.to_s+ SiteSetting.ai_ask_ai_search_intent_decision_model = decision_model.id+ SiteSetting.enable_ai_combined_search = true+ Discourse.cache.delete("discourse-ai:discoveries:prefers-results:#{user.id}")+ end++ context "when the contract is invalid" do+ let(:query) { " " }++ it { is_expected.to fail_a_contract }+ end++ context "when the user cannot ask AI" do+ fab!(:user)++ it { is_expected.to fail_a_policy(:can_ask_ai) }+ end++ context "when the request id was already used for another query" do+ before do+ DiscourseAi::Discoveries.bind_request(+ user_id: user.id,+ request_id:,+ query: "Something else entirely",+ )+ end++ it { is_expected.to fail_a_policy(:request_id_matches_query) }+ end++ context "when the request is a repeat" do+ before { DiscourseAi::Discoveries.bind_request(user_id: user.id, request_id:, query:) }++ it { is_expected.to run_successfully }++ it "does not log another ask" do+ expect { result }.not_to change { AskAiLog.count }+ end++ it "does not queue another answer" do+ expect_not_enqueued_with(job: :stream_discover_reply) { result }+ end++ it "still remembers the ask" do+ result+ expect(DiscourseAi::Discoveries.recent_asks(user_id: user.id)).to contain_exactly(+ include(term: query),+ )+ end+ end++ context "when the scoped search is judged to be about the site" do+ let(:scope) { "topic:#{topic.id}" }+ let(:classify_intent) { true }++ around do |example|+ DiscourseAi::Decisions.with_canned_answers(+ intent: "answer",+ scope: {+ "here" => 0.1,+ "site" => 0.9,+ },+ ) { example.run }+ end++ it { is_expected.to run_successfully }++ it "answers from the whole site" do+ expect_enqueued_with(+ job: :stream_discover_reply,+ args: {+ user_id: user.id,+ request_id:,+ scope: nil,+ },+ ) { result }+ end++ it "logs the judgement beside the scope it was made in" do+ result+ expect(ask_log).to have_attributes(+ ask_scope: scope,+ ask_intent_model: "answer",+ ask_intent_probability: 1.0,+ ask_scope_model: "site",+ ask_scope_probability: 0.9,+ )+ end+ end++ context "when the scoped search is judged to be about where it was made" do+ let(:scope) { "topic:#{topic.id}" }+ let(:classify_intent) { true }++ around do |example|+ DiscourseAi::Decisions.with_canned_answers(+ intent: "lookup",+ scope: {+ "here" => 0.8,+ "site" => 0.2,+ },+ ) { example.run }+ end++ it { is_expected.to run_successfully }++ it "answers from within the scope" do+ expect_enqueued_with(+ job: :stream_discover_reply,+ args: {+ user_id: user.id,+ request_id:,+ scope:,+ },+ ) { result }+ end++ it "logs the judgement" do+ result+ expect(ask_log).to have_attributes(+ ask_scope: scope,+ ask_intent_model: "lookup",+ ask_scope_model: "here",+ ask_scope_probability: 0.8,+ )+ end+ end++ context "when the scoped search is not judged" do+ let(:scope) { "topic:#{topic.id}" }++ it { is_expected.to run_successfully }++ it "answers from within the scope" do+ expect_enqueued_with(+ job: :stream_discover_reply,+ args: {+ user_id: user.id,+ request_id:,+ scope:,+ },+ ) { result }+ end++ it "logs the scope without verdicts" do+ result+ expect(ask_log).to have_attributes(+ ask_scope: scope,+ ask_intent_model: "",+ ask_scope_model: "",+ )+ end+ end++ context "when the site-wide search is judged" do+ let(:classify_intent) { true }++ around do |example|+ DiscourseAi::Decisions.with_canned_answers(intent: { "answer" => 0.3, "lookup" => 0.7 }) do+ example.run+ end+ end++ it { is_expected.to run_successfully }++ it "answers from the whole site" do+ expect_enqueued_with(+ job: :stream_discover_reply,+ args: {+ user_id: user.id,+ request_id:,+ scope: nil,+ },+ ) { result }+ end++ it "logs the intent judgement" do+ result+ expect(ask_log).to have_attributes(+ ask_scope: "",+ ask_intent_model: "lookup",+ ask_intent_probability: 0.7,+ ask_scope_model: "",+ )+ end++ it "returns the judgement" do+ expect(result[:judgement]).to have_attributes(intent: "lookup", probability: 0.7)+ end+ end++ context "when everything's ok" do+ it { is_expected.to run_successfully }++ it "logs the ask with what set it off" do+ expect { result }.to change { AskAiLog.where(user:).count }.by(1)+ expect(ask_log).to have_attributes(+ ask_trigger: "enter",+ ask_trigger_reason: "typed",+ ask_intent_heuristic: "answer",+ )+ end++ it "queues the answer" do+ expect_enqueued_with(+ job: :stream_discover_reply,+ args: {+ user_id: user.id,+ query:,+ request_id:,+ scope: nil,+ },+ ) { result }+ expect(Jobs::StreamDiscoverReply.jobs.last["args"].first).to include(+ "ask_ai_log_id" => ask_log.id,+ )+ end++ it "does not judge the search" do+ expect(result[:judgement]).to be_nil+ end++ it "leaves the judgement unlogged" do+ result+ expect(ask_log).to have_attributes(ask_scope: "", ask_intent_model: "")+ end++ it "remembers the ask" do+ expect { result }.to change { DiscourseAi::Discoveries.recent_asks(user_id: user.id) }.from(+ [],+ ).to(contain_exactly(include(term: query)))+ end++ it "returns the ask log" do+ expect(result[:ask_log]).to eq(ask_log)+ end++ it "reports the reader has no preference for results" do+ expect(result[:prefers_results]).to eq(false)+ end+ end++ context "when the reader prefers results" do+ before do+ 4.times do+ AskAiLog.create!(+ user:,+ query: "Earlier question",+ asked_at: 1.day.ago,+ ask_tab_opened: "ask",+ ask_tab_switched_to: "topics",+ ask_tab_switched_after_ms: 2000,+ )+ end+ end++ it "reports the preference" do+ expect(result[:prefers_results]).to eq(true)+ end+ end++ context "when the reader prefers results without the combined search" do+ before do+ SiteSetting.enable_ai_combined_search = false+ 4.times do+ AskAiLog.create!(+ user:,+ query: "Earlier question",+ asked_at: 1.day.ago,+ ask_tab_opened: "ask",+ ask_tab_switched_to: "topics",+ ask_tab_switched_after_ms: 2000,+ )+ end+ end++ it "does not report the preference" do+ expect(result[:prefers_results]).to eq(false)+ end+ end+ end+enddiff --git a/plugins/discourse-ai/spec/system/ai_combined_search_spec.rb b/plugins/discourse-ai/spec/system/ai_combined_search_spec.rbnew file mode 100644index 00000000000..61c0d84b3dc--- /dev/null+++ b/plugins/discourse-ai/spec/system/ai_combined_search_spec.rb@@ -0,0 +1,879 @@+# frozen_string_literal: true++describe "AI combined search" do+ fab!(:user) { Fabricate(:admin, refresh_auto_groups: true) }+ fab!(:topic_1) { Fabricate(:topic, title: "Password resets and account recovery") }+ fab!(:topic_2) { Fabricate(:topic, title: "Login help for new members") }++ let(:query) { "how do I reset my password" }+ let(:request_ids) { [] }+ let(:scopes) { [] }+ let(:triggers) { [] }+ let(:sources) do+ [topic_1, topic_2].map do |topic|+ { title: topic.title, url: topic.url, excerpt: "An excerpt about #{topic.title}" }+ end+ end++ before do+ enable_current_plugin+ assign_fake_provider_to(:ai_default_llm_model)+ assign_agent_to(:ai_ask_ai_agent, [Group::AUTO_GROUPS[:admins]])++ SiteSetting.discourse_ai_enabled = true+ SiteSetting.ai_ask_ai_enabled = true+ SiteSetting.ai_ask_ai_allowed_groups = Group::AUTO_GROUPS[:admins].to_s+ SiteSetting.enable_ai_combined_search = true++ # the answer is streamed from the spec instead, one phase at a time+ allow(DiscourseAi::Discoveries).to receive(+ :enqueue_reply,+ ) do |user:, query:, request_id:, scope: nil, trigger: "", trigger_reason: "", intent_heuristic: "", **|+ request_ids << request_id+ scopes << scope+ triggers << [trigger, trigger_reason]+ AskAiLog.create!(+ user:,+ query:,+ asked_at: Time.current,+ ask_intent_heuristic: intent_heuristic,+ )+ end++ sign_in(user)+ end++ def publish(payload)+ MessageBus.publish(+ "/discourse-ai/discoveries",+ { query:, request_id: request_ids.last }.merge(payload),+ user_ids: [user.id],+ )+ end++ def wait_for_channel(channel)+ # updates published before the client knows its position on the channel+ # are skipped, which a real answer never outpaces+ try_until_success(timeout: 10) { expect(page.evaluate_script(<<~JS)).to eq(true) }+ MessageBus.callbacks.some(+ (callback) => callback.channel === #{channel.to_json} && callback.last_id !== -1+ )+ JS+ end++ def wait_for_discoveries_channel+ wait_for_channel("/discourse-ai/discoveries")+ end++ def ask(term, input: "#welcome-banner-search-input")+ find(input).fill_in(with: term)+ find(input).send_keys(:enter)+ try_until_success { expect(request_ids).to be_present }+ end++ def tab(kind)+ ".ai-search-tabs .ai-discoveries-search-options__option.--#{kind}"+ end++ def expect_tab(kind)+ expect(page).to have_css("#{tab(kind)}.is-active")+ end++ def select_tab(kind)+ find(tab(kind)).click+ expect_tab(kind)+ end++ def allow_follow_ups+ SiteSetting.ai_bot_enabled = true+ SiteSetting.ai_bot_allowed_groups = Group::AUTO_GROUPS[:admins].to_s+ assign_agent_to(:ai_ask_ai_follow_up_agent, [Group::AUTO_GROUPS[:admins]]).create_user!+ end++ def complete_answer+ publish(+ done: true,+ phase: "complete",+ answerable: true,+ ai_discover_title: "Resetting your password",+ ai_discover_reply: "Use the **forgot password** link on the login screen.",+ ai_discover_follow_up: "What if I no longer have access to my email?",+ sources:,+ )+ end++ it "streams the answer in full, adding its related topics once it is done" do+ allow_follow_ups+ visit "/"+ wait_for_discoveries_channel+ ask(query)++ # a question opens on the answer+ expect_tab("ask")+ expect(page).to have_css(".ai-search-answer .d-skeleton")++ publish(+ done: false,+ phase: "rewritten",+ keyword_query: "reset password",+ semantic_query: "How can I reset a forgotten password?",+ )+ publish(done: false, phase: "sources", ai_discover_title: "Resetting your password", sources:)+ publish(done: false, phase: "answering", ai_discover_reply: "Use the **forgot")++ expect(page).to have_css(".ai-search-answer__body .cooked", text: "Use the")+ # added once the answer is complete, so they do not slide down as it streams+ expect(page).to have_no_css(".ai-search-answer__more")++ publish(+ done: true,+ phase: "complete",+ answerable: true,+ ai_discover_title: "Resetting your password",+ ai_discover_reply: "Use the **forgot password** link on the login screen. " * 12,+ ai_discover_follow_up: "What if I no longer have access to my email?",+ sources:,+ )++ expect(page).to have_css(".ai-search-answer__body .cooked", text: "forgot password")+ within(".ai-search-answer__more") do+ expect(page).to have_css(".ai-search-answer__related-link", count: 2)+ expect(page).to have_css(+ ".ai-search-answer__follow-up-input[placeholder='What if I no longer have access to my email?']",+ )+ end+ expect(page).to have_no_css(".ai-search-answer__expand")+ expect(page).to have_no_text("Keywords")+ end++ it "leaves out the follow-up when the reader cannot talk to the AI bot" do+ visit "/"+ wait_for_discoveries_channel+ ask(query)+ complete_answer++ within(".ai-search-answer__more") do+ expect(page).to have_css(".ai-search-answer__related-link", count: 2)+ expect(page).to have_no_css(".ai-search-answer__follow-up")+ end+ end++ it "lists every related topic the answer used" do+ category = Fabricate(:category, name: "Account help")+ more_topics =+ Array.new(3) { |i| Fabricate(:topic, category:, title: "Account recovery guide part #{i}") }+ visit "/"+ wait_for_discoveries_channel+ ask(query)++ publish(+ done: true,+ phase: "complete",+ answerable: true,+ ai_discover_reply: "Use the forgot password link.",+ sources:+ sources ++ more_topics.map do |topic|+ {+ title: topic.title,+ url: topic.url,+ excerpt: "An excerpt about #{topic.title}",+ category_id: topic.category_id,+ topic_replies: 12,+ bumped_at: 2.days.ago.iso8601,+ }+ end,+ )++ expect(page).to have_css(".ai-search-answer__related-heading", text: "Related topics")+ last_row = find(".ai-search-answer__related-link", text: more_topics.last.title)+ expect(last_row).to have_css(".ai-search-topic-row__date", text: "2d")+ expect(last_row).to have_css(".ai-search-topic-row__replies[title='12 replies']", text: "12")+ expect(last_row).to have_css(".ai-search-topic-row__category", text: "Account help")+ expect(page).to have_css(".ai-search-answer__related-item", count: 5)+ expect(page).to have_css(".ai-search-answer__related-link", text: more_topics.last.title)+ expect(page).to have_no_css(".ai-search-answer__related-excerpt")+ end++ it "opens on the answer when nothing matches the keywords, and says so on the topics tab" do+ visit "/"+ wait_for_discoveries_channel+ ask("unheard of things")++ expect_tab("ask")+ expect(page).to have_css("#{tab("topics")}.--empty .ai-search-tabs__count", text: "0")++ select_tab("topics")+ expect(page).to have_css(".search-menu .no-results", text: "No keyword matches found.")+ expect(page).to have_no_text("No results found.")+ end++ it "marks the answer tab when no answer was found" do+ visit "/"+ wait_for_discoveries_channel+ ask(query)++ publish(done: true, phase: "complete", answerable: false, ai_discover_reply: "", sources: [])++ expect(page).to have_css("#{tab("ask")} .ai-search-tabs__empty .d-icon-ban")+ end++ it "opens on the answer for a query outside the site's language" do+ visit "/"+ wait_for_discoveries_channel+ ask("сброс")++ expect_tab("ask")+ expect(triggers.last).to eq(["enter", ""])+ end++ it "waits for enter rather than searching as the reader types" do+ visit "/"+ wait_for_discoveries_channel++ find("#welcome-banner-search-input").fill_in(with: query)+ expect(page).to have_no_css(".ai-search-tabs")+ expect(request_ids).to be_empty++ find("#welcome-banner-search-input").send_keys(:enter)+ try_until_success { expect(request_ids.size).to eq(1) }+ expect(page).to have_css(".ai-search-tabs")+ end++ context "with more keyword matches than the menu lists" do+ before do+ SearchIndexer.enable+ 8.times do |i|+ topic = Fabricate(:topic, title: "Widget maintenance guide number #{i}")+ Fabricate(:post, topic:, raw: "Keeping widgets running smoothly, part #{i}")+ SearchIndexer.index(topic, force: true)+ end+ end++ after { SearchIndexer.disable }++ it "opens the full search page on the tab's terms from its more link" do+ visit "/"+ wait_for_discoveries_channel+ ask("widget")++ expect_tab("topics")+ find(".search-menu__show-more a").click++ expect(page).to have_current_path(%r{/search\?q=widget\z})+ end+ end++ context "with keyword results" do+ let(:query) { "password" }++ before do+ Fabricate(:post, topic: topic_1, raw: "Reset your password from the login screen")+ SearchIndexer.enable+ SearchIndexer.index(topic_1, force: true)+ end++ after { SearchIndexer.disable }++ it "opens a lookup on its topics, with every tab fetched at once" do+ visit "/"+ wait_for_discoveries_channel+ ask(query)++ expect_tab("topics")+ expect(page).to have_css("#{tab("topics")} .ai-search-tabs__count", text: "1")+ expect(page).to have_css(".search-menu .search-result-topic", text: topic_1.title)++ # the answer was asked for with the rest, so switching asks nothing more+ select_tab("ask")+ expect(page).to have_css(".ai-search-answer")+ expect(page).to have_no_css(".search-menu .search-result-topic")+ publish(+ done: true,+ phase: "complete",+ answerable: true,+ ai_discover_reply: "An answer.",+ sources:,+ )+ expect(page).to have_css(".ai-search-answer__body .cooked", text: "An answer.")++ select_tab("topics")+ expect(page).to have_css(".search-menu .search-result-topic", text: topic_1.title)+ expect(request_ids.size).to eq(1)+ end++ it "logs the tab the search opened on and the first one the reader switched to" do+ visit "/"+ wait_for_discoveries_channel+ ask(query)++ expect_tab("topics")+ select_tab("ask")+ select_tab("topics")++ try_until_success do+ expect(AskAiLog.last).to have_attributes(+ ask_intent_heuristic: "lookup",+ ask_tab_opened: "topics",+ ask_tab_opened_reason: "topic_results",+ ask_tab_switched_to: "ask",+ )+ end+ end++ context "with a decision model judging intent" do+ fab!(:decision_model)++ before { SiteSetting.ai_ask_ai_search_intent_decision_model = decision_model.id }++ it "opens on the answer when the model is sure the reader wants one" do+ DiscourseAi::Decisions.with_canned_answers(intent: { "answer" => 0.9, "lookup" => 0.1 }) do+ visit "/"+ wait_for_discoveries_channel+ ask(query)++ expect_tab("ask")+ expect(page).to have_css("#{tab("topics")} .ai-search-tabs__count", text: "1")+ end++ try_until_success do+ expect(AskAiLog.last).to have_attributes(+ ask_intent_heuristic: "lookup",+ ask_intent_model: "answer",+ ask_intent_probability: 0.9,+ ask_tab_opened: "ask",+ ask_tab_opened_reason: "intent_answer",+ )+ end+ end++ it "shows the keyword results when the reader switches from the answer" do+ DiscourseAi::Decisions.with_canned_answers(intent: "answer") do+ visit "/"+ wait_for_discoveries_channel+ ask(query)+ expect_tab("ask")+ end+ publish(+ done: true,+ phase: "complete",+ answerable: true,+ ai_discover_reply: "Use the forgot password link.",+ sources:,+ )+ expect(page).to have_css(".ai-search-answer__body .cooked", text: "forgot password")++ select_tab("topics")++ expect(page).to have_css(".search-menu .search-result-topic", text: topic_1.title)+ end++ it "moves to the results when the answer it opened on finds nothing" do+ DiscourseAi::Decisions.with_canned_answers(intent: "answer") do+ visit "/"+ wait_for_discoveries_channel+ ask(query)+ expect_tab("ask")+ end++ publish(+ done: true,+ phase: "complete",+ answerable: false,+ ai_discover_reply: "",+ sources: [],+ )++ expect_tab("topics")+ expect(page).to have_css(".search-menu .search-result-topic", text: topic_1.title)+ end++ it "leaves a reader on the answer they chose, even when it finds nothing" do+ visit "/"+ wait_for_discoveries_channel+ ask(query)+ expect_tab("topics")+ select_tab("ask")++ publish(+ done: true,+ phase: "complete",+ answerable: false,+ ai_discover_reply: "",+ sources: [],+ )++ expect(page).to have_css(".ai-search-answer__empty")+ expect_tab("ask")+ end++ it "keeps rewritten keyword results that arrive while the menu waits for the model" do+ question = "does this site have any guides on that"+ slow_intent = instance_double(DiscourseAi::Discoveries::Intent)+ allow(slow_intent).to receive(:call) do+ sleep 1.5+ nil+ end+ allow(DiscourseAi::Discoveries::Intent).to receive(:new).and_return(slow_intent)++ visit "/"+ wait_for_discoveries_channel+ ask(question)+ publish(+ query: question,+ done: false,+ phase: "rewritten",+ keyword_query: "password",+ semantic_query: "Password guides",+ )++ expect(page).to have_css("#{tab("topics")} .ai-search-tabs__count", text: "1")+ select_tab("topics")+ expect(page).to have_css(".search-menu .search-result-topic", text: topic_1.title)+ end++ it "judges from the query alone when the model is too slow, still logging its verdict" do+ slow_intent = instance_double(DiscourseAi::Discoveries::Intent)+ allow(slow_intent).to receive(:call) do+ sleep 1.5+ DiscourseAi::Discoveries::Intent::Judgement.new(intent: "answer", probability: 0.95)+ end+ allow(DiscourseAi::Discoveries::Intent).to receive(:new).and_return(slow_intent)++ visit "/"+ wait_for_discoveries_channel+ ask(query)++ expect_tab("topics")+ try_until_success(timeout: 5) do+ expect(AskAiLog.last).to have_attributes(+ ask_intent_model: "answer",+ ask_tab_opened: "topics",+ ask_tab_opened_reason: "topic_results",+ )+ end+ end+ end++ it "leaves related results to the combined search, so the list holds still" do+ SiteSetting.ai_embeddings_semantic_quick_search_enabled = true++ visit "/"+ wait_for_discoveries_channel+ ask(query)+ publish(+ done: true,+ phase: "complete",+ answerable: true,+ ai_discover_reply: "Use the forgot password link.",+ sources:,+ )++ expect(page).to have_css(".search-menu .search-result-topic", text: topic_1.title)+ expect(page).to have_no_css(".ai-quick-search-notice")+ expect(page).to have_no_css(".search-menu .ai-search-result")+ end+ end++ it "offers to ask the community when no answer is found" do+ visit "/"+ wait_for_discoveries_channel+ ask(query)++ publish(done: true, phase: "complete", answerable: false, ai_discover_reply: "", sources: [])++ expect(page).to have_css(".ai-search-answer__empty", text: "AI couldn’t find a clear answer.")+ find(".ai-search-answer__ask-community", text: "Ask the community.").click++ expect(page).to have_css("#reply-control.open")+ expect(find("#reply-title").value).to eq(query)+ end++ it "centres the message when no answer is found" do+ visit "/"+ wait_for_discoveries_channel+ ask(query)++ publish(done: true, phase: "complete", answerable: false, ai_discover_reply: "", sources: [])++ expect(page).to have_css(".ai-search-answer__empty")+ offsets = page.evaluate_script(<<~JS)+ (() => {+ const box = document.querySelector(".ai-search-answer__body").getBoundingClientRect();+ const text = document.createRange();+ text.selectNodeContents(document.querySelector(".ai-search-answer__empty"));+ const rect = text.getBoundingClientRect();+ return [+ Math.round(rect.top - box.top - (box.bottom - rect.bottom)),+ Math.round(rect.left - box.left - (box.right - rect.right)),+ ];+ })()+ JS+ expect(offsets.map(&:abs)).to all(be <= 2)+ end++ it "keeps the results scrollable" do+ page.current_window.resize_to(1400, 450)++ visit "/"+ wait_for_discoveries_channel+ ask(query)+ publish(+ done: true,+ phase: "complete",+ answerable: true,+ ai_discover_reply: "A long answer that runs on. " * 40,+ sources:,+ )+ # long enough to run past the window+ expect(page).to have_css(".ai-search-answer__more")++ scroll = page.evaluate_script(<<~JS)+ (() => {+ const list = document.querySelector(".search-menu .panel-body-contents");+ const panel = document.querySelector(".search-menu .menu-panel");+ list.scrollTop = 50;+ return {+ overflows: list.scrollHeight > list.clientHeight,+ scrolled: list.scrollTop > 0,+ fits: list.getBoundingClientRect().bottom <= panel.getBoundingClientRect().bottom + 1,+ };+ })()+ JS+ expect(scroll).to eq("overflows" => true, "scrolled" => true, "fits" => true)+ ensure+ page.current_window.resize_to(1400, 1400)+ end++ context "when searching from somewhere" do+ fab!(:category)+ fab!(:inside) { Fabricate(:topic, category:, title: "Widget setup for this team") }+ fab!(:outside) { Fabricate(:topic, title: "Widget setup somewhere else entirely") }+ let(:query) { "widget" }++ before do+ Fabricate(:post, topic: inside, raw: "Widget configuration inside the category")+ Fabricate(:post, topic: outside, raw: "Widget configuration outside the category")+ SearchIndexer.enable+ [inside, outside].each { |topic| SearchIndexer.index(topic, force: true) }+ end++ after { SearchIndexer.disable }++ def open_header_search+ find("#search-button").click+ expect(page).to have_css("#icon-search-input")+ end++ it "offers the category as a tab, opening on it when it has matches" do+ visit category.url+ wait_for_discoveries_channel+ open_header_search+ page.execute_script(<<~JS)+ window.pillWidths = {};+ new MutationObserver(() => {+ document.querySelectorAll(".ai-search-tabs .ai-discoveries-search-options__option").forEach((pill) => {+ const kind = [...pill.classList].find((name) => name.startsWith("--") && name !== "--empty");+ (window.pillWidths[kind] ??= new Set()).add(Math.round(pill.getBoundingClientRect().width));+ });+ }).observe(document.body, { subtree: true, childList: true, characterData: true });+ JS+ ask(query, input: "#icon-search-input")++ expect(page).to have_css(tab("context"), text: category.name)+ expect_tab("context")+ expect(scopes.last).to eq("category:#{category.id}")+ expect(page).to have_css("#{tab("context")} .ai-search-tabs__count", text: "1")+ expect(page).to have_css("#{tab("topics")} .ai-search-tabs__count", text: "2")+ # the counts hang off the pills rather than widening them+ widths = page.evaluate_script(<<~JS)+ Object.fromEntries(Object.entries(window.pillWidths).map(([kind, set]) => [kind, set.size]))+ JS+ expect(widths.values).to all(eq(1))+ expect(page).to have_css(".search-menu .search-result-topic", text: inside.title)+ expect(page).to have_no_css(".search-menu .search-result-topic", text: outside.title)++ select_tab("topics")+ expect(page).to have_css(".search-menu .search-result-topic", text: outside.title)+ expect(request_ids.size).to eq(1)+ end++ it "says when the answer had to come from the whole site" do+ visit category.url+ wait_for_discoveries_channel+ open_header_search+ ask(query, input: "#icon-search-input")+ select_tab("ask")++ publish(+ done: true,+ phase: "complete",+ answerable: true,+ scope_fallback: true,+ ai_discover_reply: "An answer from elsewhere.",+ sources:,+ )++ expect(page).to have_css(+ ".ai-search-answer__scope-note",+ text: "Nothing found in #{category.name}",+ )+ expect(request_ids.size).to eq(1)+ end++ it "does not mention the whole site when no answer was found there either" do+ visit category.url+ wait_for_discoveries_channel+ open_header_search+ ask(query, input: "#icon-search-input")+ select_tab("ask")++ publish(+ done: true,+ phase: "complete",+ answerable: false,+ scope_fallback: true,+ ai_discover_reply: "",+ sources: [],+ )++ expect(page).to have_css(".ai-search-answer__empty")+ expect(page).to have_no_css(".ai-search-answer__scope-note")+ end++ it "opens a topic search on the topic when its posts match, and on the answer when not" do+ inside.posts.each { |post| SearchIndexer.index(post, force: true) }++ visit inside.url+ wait_for_discoveries_channel+ open_header_search+ ask("widget configuration?", input: "#icon-search-input")++ expect(page).to have_css(tab("context"), text: "This topic")+ expect_tab("context")+ expect(scopes.last).to eq("topic:#{inside.id}")++ find("#icon-search-input").fill_in(with: "something nobody wrote about here")+ find("#icon-search-input").send_keys(:enter)++ try_until_success { expect(request_ids.size).to eq(2) }+ expect_tab("ask")+ end++ it "opens a search about the whole site on its results, answering from the whole site" do+ inside.posts.each { |post| SearchIndexer.index(post, force: true) }+ SiteSetting.ai_ask_ai_search_intent_decision_model = Fabricate(:decision_model).id++ DiscourseAi::Decisions.with_canned_answers(+ intent: "lookup",+ scope: {+ "here" => 0.1,+ "site" => 0.9,+ },+ ) do+ visit inside.url+ wait_for_discoveries_channel+ open_header_search+ ask("widget configuration", input: "#icon-search-input")++ expect(page).to have_css(tab("context"), text: "This topic")+ expect_tab("topics")+ end+ expect(scopes.last).to be_nil+ end++ it "starts afresh in a new context rather than searching again" do+ visit category.url+ wait_for_discoveries_channel+ open_header_search+ ask(query, input: "#icon-search-input")+ expect(page).to have_css(".search-menu .search-result-topic", text: inside.title)++ find(".search-menu .search-result-topic a", text: inside.title).click+ expect(page).to have_current_path(%r{/t/})++ open_header_search++ expect(find("#icon-search-input").value).to be_blank+ expect(page).to have_no_css(".ai-search-tabs")+ expect(request_ids.size).to eq(1)+ end+ end++ context "with a conversation continued from a search" do+ fab!(:bot) { Fabricate(:user, username: "answerbot") }+ fab!(:conversation) do+ Fabricate(:private_message_topic, user:, recipient: bot).tap do |topic|+ # laid out as a conversation with the AI bot, which hides the progress bar+ topic.custom_fields[DiscourseAi::AiBot::TOPIC_AI_BOT_PM_FIELD] = true+ topic.custom_fields[+ DiscourseAi::Discoveries::ContinueConversation::TOPIC_REQUEST_ID_FIELD+ ] = "request"+ topic.save_custom_fields+ end+ end++ before do+ Fabricate(:post, topic: conversation, user:, raw: query)+ Fabricate(+ :post,+ topic: conversation,+ user: bot,+ raw: "See [#{topic_1.title}](#{topic_1.url}) for the steps.",+ )+ end++ it "lists the topics its answers cite in place of the timeline" do+ visit conversation.url++ cited = find(".ai-search-references__link", text: topic_1.title)+ expect(cited).to have_css(".ai-search-topic-row__date")+ expect(cited).to have_css(".ai-search-topic-row__replies")+ expect(page).to have_no_css(".timeline-container")+ expect(page).to have_no_css(".ai-search-references-button")+ end++ it "lists the answer's sources kept beside the conversation" do+ conversation.custom_fields[+ DiscourseAi::Discoveries::ContinueConversation::TOPIC_SOURCES_FIELD+ ] = [{ topic_id: topic_2.id, title: topic_2.title, url: topic_2.url }]+ conversation.save_custom_fields++ visit conversation.url++ expect(page).to have_css(".ai-search-references__link", text: topic_2.title)+ end++ it "adds the topics a new reply cites" do+ visit conversation.url+ expect(page).to have_css(".ai-search-references__link", text: topic_1.title)+ wait_for_channel("discourse-ai/ai-bot/topic/#{conversation.id}")++ reply =+ Fabricate(+ :post,+ topic: conversation,+ user: bot,+ raw: "Also try [#{topic_2.title}](#{topic_2.url}).",+ )+ MessageBus.publish(+ "discourse-ai/ai-bot/topic/#{conversation.id}",+ { post_id: reply.id, post_number: reply.post_number, cooked: reply.cooked, done: true },+ user_ids: [user.id],+ )++ expect(page).to have_css(".ai-search-references__link", text: topic_2.title)+ end++ it "opens the cited topics from the topic map where the timeline does not fit", mobile: true do+ visit conversation.url+ find(".ai-search-references-button").click++ expect(page).to have_css(+ ".ai-search-references-modal .ai-search-references__link",+ text: topic_1.title,+ )+ end++ it "keeps the timeline for readers without the combined search" do+ SiteSetting.enable_ai_combined_search = false++ visit conversation.url++ expect(page).to have_css(".timeline-container")+ expect(page).to have_no_css(".ai-search-references")+ end+ end++ context "with people and places to find" do+ fab!(:gardening) { Fabricate(:category, name: "Gardening") }+ fab!(:gardener) { Fabricate(:user, username: "plantlover", name: "Green Thumb") }++ before do+ SearchIndexer.enable+ [+ "Gardening tips for the spring season",+ "Gardening in small spaces",+ "Gardening with children",+ "Gardening on a budget",+ ].each do |title|+ topic = Fabricate(:topic, category: gardening, title:)+ Fabricate(:post, topic:, raw: "Gardening is a lovely way to spend the spring")+ SearchIndexer.index(topic, force: true)+ end+ SearchIndexer.index(gardener, force: true)+ SearchIndexer.index(gardening, force: true)+ end++ after { SearchIndexer.disable }++ it "opens a lookup on its topics" do+ visit "/"+ wait_for_discoveries_channel+ ask("gardening")++ expect_tab("topics")+ expect(page).to have_css(".search-menu .search-result-topic", text: "Gardening tips")+ end++ it "opens on the answer when only a person matches" do+ visit "/"+ wait_for_discoveries_channel+ ask("green thumb")++ expect_tab("ask")+ end++ it "opens a question on the answer even when places match" do+ visit "/"+ wait_for_discoveries_channel+ ask("gardening?")++ expect_tab("ask")+ end++ it "opens a query in search syntax on its topics" do+ visit "/"+ wait_for_discoveries_channel+ ask('"small spaces"')++ expect_tab("topics")+ expect(page).to have_css(+ ".search-menu .search-result-topic",+ text: "Gardening in small spaces",+ )+ end++ it "opens on the answer when the reader searches again on the same page" do+ visit "/"+ wait_for_discoveries_channel+ ask("gardening")+ expect_tab("topics")++ find("#welcome-banner-search-input").fill_in(with: "spring gardening")+ find("#welcome-banner-search-input").send_keys(:enter)++ try_until_success { expect(request_ids.size).to eq(2) }+ expect_tab("ask")+ end++ it "runs the combined search for a recent search picked from the menu" do+ SearchLog.log(+ term: "gardening",+ search_type: :header,+ ip_address: "127.0.0.1",+ user_id: user.id,+ )++ visit "/"+ wait_for_discoveries_channel+ find("#welcome-banner-search-input").click+ find(".search-menu-recent .search-menu-assistant-item", text: "gardening").click++ expect_tab("topics")+ expect(page).to have_css(".search-menu .search-result-topic", text: "Gardening tips")+ expect(find("#welcome-banner-search-input").value).to eq("gardening")+ expect(triggers.last).to eq(["recent", ""])+ end+ end+enddiff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-search-discoveries-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-search-discoveries-test.gjsindex 12dbdff5c95..1a12a95981f 100644--- a/plugins/discourse-ai/test/javascripts/integration/components/ai-search-discoveries-test.gjs+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-search-discoveries-test.gjs@@ -26,6 +26,8 @@ module("Integration | Component | AiSearchDiscoveries", function (hooks) {this.siteSettings.ai_ask_ai_summary_detail = "balanced";this.siteSettings.ai_ask_ai_related_count = 2;this.currentUser.can_use_ask_ai = true;+ // present only for readers the AI bot is enabled for+ this.currentUser.ai_enabled_chat_bots = [];this.closeSearchMenuCalled = false;this.closeSearchMenu = () => {@@ -255,6 +257,29 @@ module("Integration | Component | AiSearchDiscoveries", function (hooks) {);});+ test("the default toggle is left out under the combined search", async function (assert) {+ this.siteSettings.enable_ai_combined_search = true;+ this.owner.register(+ "service:discobot-discoveries",+ class extends Service {+ discovery = "A useful answer.";+ streamedText = "A useful answer.";+ loadingDiscoveries = false;+ isStreaming = false;+ discoveryTimedOut = false;++ triggerDiscovery() {}+ onDiscoveryUpdate() {}+ }+ );++ await render(+ <template><AiSearchDiscoveries @searchTerm="test search" /></template>+ );++ assert.dom(".ai-search-discoveries__default-preference").doesNotExist();+ });+test("the default toggle can be dismissed for this browser", async function (assert) {this.owner.register("service:discobot-discoveries",diff --git a/plugins/discourse-ai/test/javascripts/unit/lib/ai-search-intent-test.js b/plugins/discourse-ai/test/javascripts/unit/lib/ai-search-intent-test.jsnew file mode 100644index 00000000000..0bbb5233642--- /dev/null+++ b/plugins/discourse-ai/test/javascripts/unit/lib/ai-search-intent-test.js@@ -0,0 +1,139 @@+import { setupTest } from "ember-qunit";+import { module, test } from "qunit";+import { chooseTab } from "discourse/plugins/discourse-ai/discourse/lib/ai-search-intent";++const topics = { posts: [{ id: 1 }] };++module("Discourse AI | Unit | Lib | ai-search-intent", function (hooks) {+ setupTest(hooks);++ test("a confident answer intent opens on Ask AI", function (assert) {+ assert.deepEqual(+ chooseTab("password reset", {+ topics,+ intent: { intent: "answer", probability: 0.9 },+ }),+ { tab: "ask", reason: "intent_answer" }+ );+ });++ test("a lookup intent never moves a question off the answer", function (assert) {+ assert.deepEqual(+ chooseTab("where is the theme settings page?", {+ topics,+ intent: { intent: "lookup", probability: 0.9 },+ }),+ { tab: "ask", reason: "question" }+ );+ });++ test("an unsure intent falls back to how the query reads", function (assert) {+ assert.deepEqual(+ chooseTab("where is the theme settings page?", {+ topics,+ intent: { intent: "lookup", probability: 0.55 },+ }),+ { tab: "ask", reason: "question" }+ );+ assert.deepEqual(+ chooseTab("password reset", {+ topics,+ intent: { intent: "answer", probability: 0.55 },+ }),+ { tab: "topics", reason: "topic_results" }+ );+ });++ test("a reader who prefers results opens on them, even over a confident answer", function (assert) {+ assert.deepEqual(+ chooseTab("how do I reset my password", {+ topics,+ prefersResults: true,+ intent: { intent: "answer", probability: 0.9 },+ }),+ { tab: "topics", reason: "reader_prefers_results" }+ );+ });++ test("a preference for results needs results, and yields to a second search", function (assert) {+ assert.deepEqual(+ chooseTab("password reset", {+ topics: { posts: [] },+ prefersResults: true,+ }),+ { tab: "ask", reason: "no_keyword_matches" }+ );+ assert.deepEqual(+ chooseTab("password reset", {+ topics,+ prefersResults: true,+ searchedAgain: true,+ }),+ { tab: "ask", reason: "searched_again" }+ );+ });++ test("a search about the whole site skips its context", function (assert) {+ const context = { posts: [{ id: 2 }] };+ assert.deepEqual(+ chooseTab("install discourse", {+ topics,+ context,+ scope: "topic:5",+ intent: { scope: "site", scope_probability: 0.9 },+ }),+ { tab: "topics", reason: "topic_results" }+ );+ assert.deepEqual(+ chooseTab("install discourse", {+ topics,+ context,+ scope: "category:3",+ intent: { scope: "site", scope_probability: 0.9 },+ }),+ { tab: "topics", reason: "topic_results" }+ );+ });++ test("an unsure or local scope keeps the context first", function (assert) {+ const context = { posts: [{ id: 2 }] };+ assert.deepEqual(+ chooseTab("deadline", {+ topics,+ context,+ scope: "topic:5",+ intent: { scope: "site", scope_probability: 0.6 },+ }),+ { tab: "context", reason: "topic_match" }+ );+ assert.deepEqual(+ chooseTab("deadline", {+ topics,+ context,+ scope: "topic:5",+ intent: { scope: "here", scope_probability: 0.9 },+ }),+ { tab: "context", reason: "topic_match" }+ );+ });++ test("search syntax wins over the intent", function (assert) {+ assert.deepEqual(+ chooseTab('"reset password"', {+ topics,+ intent: { intent: "answer", probability: 0.9 },+ }),+ { tab: "topics", reason: "syntax" }+ );+ });++ test("a lookup with no keyword matches still opens on Ask AI", function (assert) {+ assert.deepEqual(+ chooseTab("password reset", {+ topics: { posts: [] },+ intent: { intent: "lookup", probability: 0.9 },+ }),+ { tab: "ask", reason: "no_keyword_matches" }+ );+ });+});diff --git a/plugins/discourse-ai/test/javascripts/unit/lib/ai-search-references-test.js b/plugins/discourse-ai/test/javascripts/unit/lib/ai-search-references-test.jsnew file mode 100644index 00000000000..a077c676cde--- /dev/null+++ b/plugins/discourse-ai/test/javascripts/unit/lib/ai-search-references-test.js@@ -0,0 +1,82 @@+import { setupTest } from "ember-qunit";+import { module, test } from "qunit";+import AiSearchReferences, {+ citedTopicsFromRaw,+ topicIdFromUrl,+} from "discourse/plugins/discourse-ai/discourse/lib/ai-search-references";++function item(topicId) {+ return { topicId, title: `Topic ${topicId}`, url: `/t/topic/${topicId}` };+}++module("Discourse AI | Unit | Lib | ai-search-references", function (hooks) {+ setupTest(hooks);++ test("topicIdFromUrl", function (assert) {+ assert.strictEqual(topicIdFromUrl("https://x.com/t/some-slug/123/4"), 123);+ assert.strictEqual(topicIdFromUrl("/t/456"), 456);+ assert.strictEqual(topicIdFromUrl("/u/someone"), null);+ });++ test("citedTopicsFromRaw keeps first mention of each topic", function (assert) {+ const raw =+ "See [Setup guide](/t/setup-guide/10) and [again](/t/setup-guide/10/3), plus [FAQ](https://x.com/t/faq/20).";++ assert.deepEqual(+ citedTopicsFromRaw(raw).map(({ topicId, title }) => [topicId, title]),+ [+ [10, "Setup guide"],+ [20, "FAQ"],+ ]+ );+ });++ test("topics seen across signals outrank single-signal topics", function (assert) {+ const references = new AiSearchReferences();+ references.add(0, "keyword", [item(1), item(2)]);+ references.add(0, "semantic", [item(2), item(3)]);++ assert.strictEqual(references.rank(0)[0].topicId, 2);+ });++ test("later turns rerank and report movement", function (assert) {+ const references = new AiSearchReferences();+ references.add(0, "keyword", [item(1), item(2)]);+ references.rank(0);++ references.add(1, "cited", [item(2), item(3)]);+ const ranked = references.rank(1);++ assert.deepEqual(+ ranked.map((entry) => entry.topicId),+ [2, 3, 1]+ );+ assert.strictEqual(ranked[0].movement, 1, "moved up one place");+ assert.true(ranked[1].isNew, "newly surfaced topic is flagged");+ assert.strictEqual(ranked[2].movement, -2);+ });++ test("a reply added again as it grows counts its topics once", function (assert) {+ const references = new AiSearchReferences();+ references.add(0, "cited", [item(1)]);+ references.add(0, "cited", [item(1), item(2)]);+ references.add(1, "cited", [item(2)]);++ assert.deepEqual(+ references.rank(1).map((entry) => entry.topicId),+ [2, 1],+ "cited again in a later reply, it rises"+ );+ });++ test("excluded topics never rank", function (assert) {+ const references = new AiSearchReferences();+ references.exclude(2);+ references.add(0, "keyword", [item(1), item(2)]);++ assert.deepEqual(+ references.rank(0).map((entry) => entry.topicId),+ [1]+ );+ });+});diff --git a/plugins/discourse-ai/test/javascripts/unit/lib/ai-search-scope-test.js b/plugins/discourse-ai/test/javascripts/unit/lib/ai-search-scope-test.jsnew file mode 100644index 00000000000..265828e1464--- /dev/null+++ b/plugins/discourse-ai/test/javascripts/unit/lib/ai-search-scope-test.js@@ -0,0 +1,42 @@+import { setupTest } from "ember-qunit";+import { module, test } from "qunit";+import {+ contextPlace,+ scopeFilter,+} from "discourse/plugins/discourse-ai/discourse/lib/ai-search-scope";++module("Discourse AI | Unit | Lib | ai-search-scope", function (hooks) {+ setupTest(hooks);++ test("a tag page within a category scopes to both", function (assert) {+ const place = contextPlace({+ type: "tagIntersection",+ tagId: "how-to",+ additionalTags: ["faq"],+ categoryId: 12,+ category: { name: "Support" },+ });++ assert.strictEqual(place.key, "category:12 tag:how-to+faq");+ assert.strictEqual(place.tabLabel, "Support #how-to #faq");+ assert.strictEqual(+ scopeFilter(place.key),+ "category:12 tags:how-to+faq",+ "and searches for topics with every tag"+ );+ });++ test("an intersection of tags alone scopes to every tag", function (assert) {+ const place = contextPlace({+ type: "tagIntersection",+ tagId: "how-to",+ additionalTags: ["faq"],+ });++ assert.strictEqual(place.key, "tag:how-to+faq");+ });++ test("a key with an unknown part has no filter", function (assert) {+ assert.strictEqual(scopeFilter("category:12 order:latest"), null);+ });+});