Skip to content

A platform for community discussion. Free, open, simple.

DEV: improve AI combined search, logging (#45471)

Kris8371b43fb9d6
diff --git a/db/structure.sql b/db/structure.sql
index 3dd7bdb66e0..4f1198034c4 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -1456,7 +1456,16 @@ CREATE TABLE public.ask_ai_logs (
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
+ ask_scope_probability double precision DEFAULT 0.0 NOT NULL,
+ ai_agent_id bigint,
+ llm_id bigint,
+ no_answer_reason integer,
+ time_to_outcome_ms integer,
+ answer_requested_at timestamp(6) without time zone,
+ ask_tab_opened_after_ms integer DEFAULT 0 NOT NULL,
+ ask_answer_seen_after_ms integer,
+ ask_result_clicked_tab character varying DEFAULT ''::character varying NOT NULL,
+ ask_result_clicked_after_ms integer DEFAULT 0 NOT NULL
);
@@ -26510,6 +26519,7 @@ SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
('20261102174233'),
+('20261030215104'),
('20261030180958'),
('20261029021723'),
('20261028093149'),
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.rb
index 3741a07efe8..d4af6cc77c3 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
@@ -8,7 +8,9 @@ module DiscourseAi
requires_plugin PLUGIN_NAME
requires_login
before_action :check_permissions!
- before_action :rate_limit_ask, only: :reply, if: -> { params[:request_id].present? }
+ before_action :rate_limit_ask,
+ only: :reply,
+ if: -> { params[:request_id].present? && !requests_deferred_answer? }
def reply
return legacy_reply if params[:request_id].blank?
@@ -30,6 +32,12 @@ module DiscourseAi
end
end
on_failed_policy(:can_ask_ai) { raise Discourse::InvalidAccess }
+ on_failed_policy(:deferred_answer_available) do
+ render_json_error(
+ I18n.t("discourse_ai.ai_bot.discoveries.errors.deferred_answer_unavailable"),
+ status: :conflict,
+ )
+ end
on_failed_policy(:request_id_matches_query) do
render_json_error(
I18n.t("discourse_ai.ai_bot.discoveries.errors.request_conflict"),
@@ -60,8 +68,12 @@ module DiscourseAi
ask_log_id: params.require(:ask_log_id),
opened: params[:tab_opened].presence,
opened_reason: params[:tab_opened_reason].to_s,
+ opened_after_ms: params[:tab_opened_after_ms],
switched_to: params[:tab_switched_to].presence,
switched_after_ms: params[:tab_switched_after_ms],
+ answer_seen_after_ms: params[:answer_seen_after_ms],
+ clicked_tab: params[:result_clicked_tab].presence,
+ clicked_after_ms: params[:result_clicked_after_ms],
)
head :no_content
end
@@ -189,6 +201,16 @@ module DiscourseAi
%w[recent clear_recent record_tabs].include?(action_name) || params[:request_id].present?
end
+ # the search that deferred this answer was already counted
+ def requests_deferred_answer?
+ params[:deferred_ask_log_id].present? &&
+ AskAiLog.awaiting_answer(
+ id: params[:deferred_ask_log_id],
+ user: current_user,
+ query: params[:query],
+ ).exists?
+ end
+
def rate_limit_ask
RateLimiter.new(
current_user,
diff --git a/plugins/discourse-ai/app/jobs/regular/stream_discover_reply.rb b/plugins/discourse-ai/app/jobs/regular/stream_discover_reply.rb
index bbd88e7a117..0c4c20a37b4 100644
--- a/plugins/discourse-ai/app/jobs/regular/stream_discover_reply.rb
+++ b/plugins/discourse-ai/app/jobs/regular/stream_discover_reply.rb
@@ -27,6 +27,8 @@ module Jobs
llm_model_id = ai_agent.default_llm_id.presence || SiteSetting.ai_default_llm_model
return if (llm_model = LlmModel.find_by(id: llm_model_id)).nil?
+ ask_log&.assign_attributes(ai_agent_id: ai_agent.id, llm_id: llm_model.id)
+ feature_context = ask_log ? { ask_ai_log_id: ask_log.id } : {}
base = { query:, request_id: args[:request_id] }
queued_at = Float(args[:queued_at], exception: false)
@@ -53,7 +55,7 @@ module Jobs
publish_update(user, base.merge(done: false, phase: "searching"))
stage = :rewrite
- rewritten_queries = rewrite_queries(user:, query:, cancel_manager:)
+ rewritten_queries = rewrite_queries(user:, query:, cancel_manager:, feature_context:)
if ask_log
ask_log.assign_attributes(
keyword_query: rewritten_queries.keyword_query,
@@ -107,7 +109,7 @@ module Jobs
end
base = base.merge(scope_fallback: true) if retrieval_result.scope_fallback
if retrieval_result.synthesis_candidates.empty?
- ask_log.ask_outcome = :no_answer if ask_log
+ ask_log&.assign_attributes(ask_outcome: :no_answer, no_answer_reason: :no_candidates)
publish_no_answer(user, base)
return
end
@@ -116,16 +118,22 @@ module Jobs
stage = :synthesis
result_settings = request_result_settings(args, user)
synthesis =
- DiscourseAi::Discoveries::Synthesis.new(user:, ai_agent:, llm_model:, cancel_manager:)
+ DiscourseAi::Discoveries::Synthesis.new(
+ user:,
+ ai_agent:,
+ llm_model:,
+ cancel_manager:,
+ feature_context:,
+ )
selected_sources = nil
rejected = false
discovery_title = ""
last_published_answer = ""
last_streamed_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
reject =
- lambda do
+ lambda do |reason|
rejected = true
- ask_log.ask_outcome = :no_answer if ask_log
+ ask_log&.assign_attributes(ask_outcome: :no_answer, no_answer_reason: reason)
publish_no_answer(user, base)
cancel_manager.cancel!
end
@@ -144,7 +152,7 @@ module Jobs
) do |update|
# the cancel monitor already stops this once the request is superseded
next if rejected || cancel_manager.cancelled?
- next reject.call if update[:answerable] == false
+ next reject.call(:abstained) if update[:answerable] == false
discovery_title = update[:title].to_s.strip if update[:title].present?
@@ -155,7 +163,7 @@ module Jobs
update[:source_refs],
limit: result_settings[:related_count],
)
- next reject.call if selected_sources.empty?
+ next reject.call(:unresolved_sources) if selected_sources.empty?
publish_update(
user,
@@ -190,10 +198,7 @@ module Jobs
),
)
if ask_log && ask_log.time_to_first_answer_ms.nil?
- ask_log.time_to_first_answer_ms = [
- (Time.current - ask_log.asked_at) * 1000,
- 0,
- ].max.round
+ ask_log.time_to_first_answer_ms = ask_log.ms_since_answer_requested
end
end
@@ -244,7 +249,10 @@ module Jobs
),
)
else
- ask_log.ask_outcome = :no_answer if ask_log
+ ask_log&.assign_attributes(
+ ask_outcome: :no_answer,
+ no_answer_reason: no_answer_reason(result, final_sources),
+ )
publish_no_answer(user, base)
end
rescue LlmCreditAllocation::CreditLimitExceeded => e
@@ -267,6 +275,8 @@ module Jobs
ask_log.ask_outcome = :cancelled
end
end
+ ask_log.time_to_outcome_ms =
+ ask_log.ms_since_answer_requested if ask_log.ask_outcome_changed?
ask_log.save! if ask_log.changed?
end
end
@@ -385,7 +395,12 @@ module Jobs
agent if agent && user.in_any_groups?(agent.allowed_group_ids.to_a)
end
- def rewrite_queries(user:, query:, cancel_manager:)
+ def no_answer_reason(result, sources)
+ return result.no_answer_reason || :abstained if !result.answerable
+ sources.blank? ? :unresolved_sources : :empty_answer
+ end
+
+ def rewrite_queries(user:, query:, cancel_manager:, feature_context:)
fallback =
DiscourseAi::Discoveries::QueryRewriter::Result.new(
keyword_query: query,
@@ -407,6 +422,7 @@ module Jobs
ai_agent: agent,
llm_model:,
cancel_manager:,
+ feature_context:,
).call(query)
end
diff --git a/plugins/discourse-ai/app/models/ask_ai_log.rb b/plugins/discourse-ai/app/models/ask_ai_log.rb
index 952e8c895a3..0d366fb012e 100644
--- a/plugins/discourse-ai/app/models/ask_ai_log.rb
+++ b/plugins/discourse-ai/app/models/ask_ai_log.rb
@@ -3,11 +3,23 @@
class AskAiLog < ActiveRecord::Base
belongs_to :user
+ # searches whose answer was left for the reader to request are not asks yet
+ scope :asked, -> { where("ask_outcome IS DISTINCT FROM ?", ask_outcomes[:skipped]) }
+
+ # A search whose answer the reader can still request, once.
+ scope :awaiting_answer,
+ ->(id:, user:, query:) { ask_outcome_skipped.where(id:, user:, query: query.to_s.strip) }
+
def self.for_reports
group_ids = SiteSetting.ai_ask_ai_report_exclude_groups_map
- return all if group_ids.empty?
+ return asked if group_ids.empty?
+
+ asked.where.not(user_id: GroupUser.where(group_id: group_ids).select(:user_id))
+ end
- where.not(user_id: GroupUser.where(group_id: group_ids).select(:user_id))
+ # A requested answer's clock starts at the request, not the search before it.
+ def ms_since_answer_requested
+ [(Time.current - (answer_requested_at || asked_at)) * 1000, 0].max.round
end
# The search's scope and the decision model's verdicts, kept beside the
@@ -22,7 +34,12 @@ class AskAiLog < ActiveRecord::Base
)
end
- enum :ask_outcome, { answered: 0, no_answer: 1, failed: 2, cancelled: 3 }, prefix: true
+ enum :ask_outcome,
+ { answered: 0, no_answer: 1, failed: 2, cancelled: 3, skipped: 4 },
+ prefix: true
+ enum :no_answer_reason,
+ { no_candidates: 0, abstained: 1, unresolved_sources: 2, empty_answer: 3 },
+ prefix: true
enum :failure_stage, { rewrite: 0, retrieval: 1, synthesis: 2 }, prefix: true
end
@@ -30,35 +47,44 @@ end
#
# Table name: ask_ai_logs
#
-# 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
+# id :bigint not null, primary key
+# answer :text
+# answer_requested_at :datetime
+# answer_title :text
+# ask_answer_seen_after_ms :integer
+# 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_result_clicked_after_ms :integer default(0), not null
+# ask_result_clicked_tab :string default(""), not null
+# 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_after_ms :integer default(0), 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
+# no_answer_reason :integer
+# 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
+# time_to_outcome_ms :integer
+# created_at :datetime not null
+# updated_at :datetime not null
+# ai_agent_id :bigint
+# llm_id :bigint
+# 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.rb
index a67a5b3d7af..c96cd266fd3 100644
--- 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.rb
@@ -5,7 +5,7 @@ module DiscourseAi
# 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
+ attributes :request_id, :ask_log_id, :intent, :prefers_results, :answer_deferred
def request_id
object.params.request_id
@@ -27,6 +27,14 @@ module DiscourseAi
object.judgement.present?
end
+ def answer_deferred
+ true
+ end
+
+ def include_answer_deferred?
+ object.ask_log&.ask_outcome_skipped?
+ end
+
def prefers_results
true
end
diff --git a/plugins/discourse-ai/app/services/discourse_ai/ask_ai/ask.rb b/plugins/discourse-ai/app/services/discourse_ai/ask_ai/ask.rb
index 50257ceefd2..e3be8175504 100644
--- a/plugins/discourse-ai/app/services/discourse_ai/ask_ai/ask.rb
+++ b/plugins/discourse-ai/app/services/discourse_ai/ask_ai/ask.rb
@@ -3,7 +3,9 @@
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.
+ # tab the search should open on, and where the answer should look. A search
+ # after precise results or private messages can leave the answer for the
+ # reader to request, so nothing is generated or sent to a model until then.
class Ask
include Service::Base
@@ -15,6 +17,8 @@ module DiscourseAi
attribute :trigger_reason, :string
attribute :intent_heuristic, :string
attribute :classify_intent, :boolean, default: false
+ attribute :defer_if_precise, :boolean, default: false
+ attribute :deferred_ask_log_id, :integer
# What started a search is only logged, so anything unexpected is
# dropped rather than refused.
@@ -26,6 +30,13 @@ module DiscourseAi
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)
+
+ # Requesting a held-back answer skips the rate limit, so the request
+ # may only claim that answer: never defer again or judge first.
+ if deferred_ask_log_id.present?
+ self.defer_if_precise = false
+ self.classify_intent = false
+ end
end
validates :query,
@@ -49,25 +60,32 @@ module DiscourseAi
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
+ only_if(:answer_deferred?) { only_if(:new_request?) { model :ask_log, :log_deferred_ask } }
+
+ only_if(:answer_wanted?) do
+ only_if(:new_request?) do
+ only_if(:answer_requested_from_search?) { policy :deferred_answer_available }
+
+ # 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
+ # 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
- only_if(:judgement_worth_logging?) { step :log_judgement }
+ step :remember_ask
end
- step :remember_ask
model :prefers_results, :look_up_reading_preference, optional: true
private
@@ -92,6 +110,37 @@ module DiscourseAi
request_status == :created
end
+ def answer_deferred?(params:, guardian:)
+ return if !params.defer_if_precise
+
+ Discoveries.precise_search?(params.query) ||
+ Discoveries.private_search?(query: params.query, scope: params.scope, guardian:)
+ end
+
+ def log_deferred_ask(params:, guardian:)
+ Discoveries.log_deferred_ask(
+ user: guardian.user,
+ scope: params.scope,
+ **params.answer_attributes.except(:request_id),
+ )
+ end
+
+ def answer_wanted?(params:, guardian:)
+ !answer_deferred?(params:, guardian:)
+ end
+
+ def answer_requested_from_search?(params:)
+ params.deferred_ask_log_id.present?
+ end
+
+ def deferred_answer_available(params:, guardian:)
+ AskAiLog.awaiting_answer(
+ id: params.deferred_ask_log_id,
+ user: guardian.user,
+ query: params.query,
+ ).exists?
+ end
+
def scoped?(params:)
params.scope.present?
end
@@ -107,6 +156,7 @@ module DiscourseAi
Discoveries.enqueue_reply(
user: guardian.user,
scope: judgement&.about_site? ? nil : params.scope,
+ deferred_log_id: params.deferred_ask_log_id,
**params.answer_attributes,
)
end
@@ -116,7 +166,11 @@ module DiscourseAi
end
def queue_answer(params:, guardian:)
- Discoveries.enqueue_reply(user: guardian.user, **params.answer_attributes)
+ Discoveries.enqueue_reply(
+ user: guardian.user,
+ deferred_log_id: params.deferred_ask_log_id,
+ **params.answer_attributes,
+ )
end
def judge_search(params:, guardian:)
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.gjs
index 11ac118e191..2d330a1bdc7 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
@@ -49,6 +49,30 @@ export default class AiDiscobotDiscoveries extends Component {
}
});
+ trackResultClicks = modifier((element) => {
+ const results = element.closest(".results");
+ const onResultChosen = (event) => {
+ if (event.type === "keydown" && event.key !== "Enter") {
+ return;
+ }
+ const link = event.target.closest?.(
+ "a.search-link, .ai-search-answer a:not(.ai-search-answer__ask-community)"
+ );
+ if (link) {
+ this.aiSearchSession.recordResultClick(this.term);
+ }
+ };
+
+ // captured because a result's own handlers navigate and close the menu first
+ const options = { capture: true };
+ results?.addEventListener("click", onResultChosen, options);
+ results?.addEventListener("keydown", onResultChosen, options);
+ return () => {
+ results?.removeEventListener("click", onResultChosen, options);
+ results?.removeEventListener("keydown", onResultChosen, options);
+ };
+ });
+
followScope = modifier((element, [scope]) => {
const timer = next(() => this.aiSearchSession.updateContextScope(scope));
return () => cancel(timer);
@@ -104,6 +128,7 @@ export default class AiDiscobotDiscoveries extends Component {
{{this.connectMenu}}
{{this.followScope this.currentScope}}
{{this.guardResults this.search.results}}
+ {{this.trackResultClicks}}
></span>
{{#if this.searched}}
<AiSearchTabs />
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.js
index c922e89e300..3f397d78fb0 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-intent.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-search-intent.js
@@ -1,6 +1,7 @@
-// quoted phrases and search operators are someone after precise results
+// search operators, or a query that is only quoted phrases, are someone after
+// precise results; a question can still quote a phrase
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;
+ /^\s*(?:"[^"]+"\s*)+$|(?:^|\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*$/;
@@ -77,6 +78,8 @@ const FOREIGN_SHARE = 0.6;
* 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
+ * @param {boolean} [found.answerDeferred] whether the answer waits for the
+ * reader to request it
* @returns {{tab: SearchTab, reason: string}}
*/
export function chooseTab(
@@ -89,6 +92,7 @@ export function chooseTab(
searchedAgain,
intent,
prefersResults,
+ answerDeferred,
} = {}
) {
const topicCount = topics?.posts?.length ?? 0;
@@ -104,6 +108,13 @@ export function chooseTab(
};
}
+ if (answerDeferred) {
+ return {
+ tab: contextCount > 0 ? "context" : "topics",
+ reason: "answer_deferred",
+ };
+ }
+
// within a topic the reader is after a passage of it
if (scope?.startsWith("topic:") && contextCount > 0) {
return { tab: "context", reason: "topic_match" };
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.js
index 682eb291bf9..1373e24ba66 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/services/ai-search-session.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/services/ai-search-session.js
@@ -70,6 +70,8 @@ export default class AiSearchSession extends Service {
// the menu search's tab choices, logged on its ask to judge how well the
// opening tab was chosen
#tabLog = null;
+ #answerArrived = false;
+ #deferredAskLogId = null;
willDestroy() {
super.willDestroy(...arguments);
@@ -190,6 +192,7 @@ export default class AiSearchSession extends Service {
}
reset() {
+ this.#deferredAskLogId = null;
this.query = "";
this.answering = false;
this.scope = null;
@@ -223,6 +226,28 @@ export default class AiSearchSession extends Service {
this.#tabChosenByReader = true;
this.selectedTab = tab;
this.#showSelectedResults();
+ if (tab === "ask" && this.#deferredAskLogId) {
+ this.#requestDeferredAnswer();
+ }
+ this.#noteAnswerSeen();
+ }
+
+ /**
+ * Records the first result opened from a search still on screen.
+ *
+ * @param {string} term the search menu's current term
+ */
+ recordResultClick(term) {
+ const log = this.#tabLog;
+ if (!log || log.clicked || !this.selectedTab || !this.isActiveFor(term)) {
+ return;
+ }
+
+ log.clicked = true;
+ this.#recordTabs(log, {
+ result_clicked_tab: this.selectedTab,
+ result_clicked_after_ms: Date.now() - log.since,
+ });
}
async startConversation(question) {
@@ -254,6 +279,11 @@ export default class AiSearchSession extends Service {
}
this.discoveries.onDiscoveryUpdate(update);
+
+ if (isCurrent && update.ai_discover_reply) {
+ this.#answerArrived = true;
+ this.#noteAnswerSeen();
+ }
}
/**
@@ -275,15 +305,28 @@ export default class AiSearchSession extends Service {
// 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 };
+ const startedAt = Date.now();
+ const log = {
+ asked,
+ startedAt,
+ // the reader's events are timed from the opening tab; its own delay
+ // from the search is logged once, so the two add up
+ since: startedAt,
+ switched: false,
+ answerSeen: false,
+ clicked: false,
+ };
this.#tabLog = log;
this.#tabChosenByReader = false;
+ this.#answerArrived = false;
+ this.#deferredAskLogId = null;
this.selectedTab = null;
this.topicsResults = null;
this.contextResults = null;
this.search.noResults = false;
const scopeKey = this.scope?.key;
+ asked.then((reply) => this.#onReply(query, reply));
const replied = this.#waitForReply(asked);
const [topics, context] = await Promise.all([
this.#menuSearch(query),
@@ -316,17 +359,79 @@ export default class AiSearchSession extends Service {
searchedAgain,
intent: reply?.intent,
prefersResults: reply?.prefers_results,
+ // the reply can land after the wait for it, but before the results
+ answerDeferred:
+ reply?.answer_deferred || this.#deferredAskLogId !== null,
});
this.selectedTab = tab;
log.since = Date.now();
- this.#recordTabs(log, { tab_opened: tab, tab_opened_reason: reason });
+ this.#recordTabs(log, {
+ tab_opened: tab,
+ tab_opened_reason: reason,
+ tab_opened_after_ms: log.since - log.startedAt,
+ });
if (this.answerFailed) {
this.#leaveFailedAnswer();
}
+ this.#noteAnswerSeen();
}
this.#showSelectedResults();
}
+ // The reply can land after the tab was chosen without it, so a deferred
+ // answer is handled here rather than only when choosing.
+ #onReply(query, reply) {
+ if (this.query !== query || !reply?.answer_deferred) {
+ return;
+ }
+
+ this.#deferredAskLogId = reply.ask_log_id;
+ if (this.selectedTab !== "ask") {
+ return;
+ }
+
+ if (this.#tabChosenByReader) {
+ this.#requestDeferredAnswer();
+ } else {
+ this.selectedTab = this.contextResults?.posts?.length
+ ? "context"
+ : "topics";
+ this.#showSelectedResults();
+ }
+ }
+
+ async #requestDeferredAnswer() {
+ const log = this.#tabLog;
+ const deferredAskLogId = this.#deferredAskLogId;
+ this.#deferredAskLogId = null;
+ this.discoveries.dismissDiscovery();
+ const reply = await this.discoveries.triggerDiscovery(this.query, {
+ scope: this.scope?.key,
+ deferredAskLogId,
+ });
+ // a failed request leaves the answer to be requested again from the tab
+ if (!reply && this.#tabLog === log) {
+ this.#deferredAskLogId = deferredAskLogId;
+ }
+ }
+
+ #noteAnswerSeen() {
+ const log = this.#tabLog;
+ if (
+ !log ||
+ log.answerSeen ||
+ !this.#answerArrived ||
+ this.selectedTab !== "ask"
+ ) {
+ return;
+ }
+
+ log.answerSeen = true;
+ this.#recordTabs(log, {
+ answer_seen_after_ms: Date.now() - log.since,
+ });
+ }
+
// a failed answer is a dead end, so the reader is moved to results that exist
#leaveFailedAnswer() {
if (this.selectedTab !== "ask" || this.#tabChosenByReader) {
@@ -434,6 +539,7 @@ export default class AiSearchSession extends Service {
intentHeuristic: readsAsQuestion(query) ? "answer" : "lookup",
classifyIntent:
!!this.siteSettings.ai_ask_ai_search_intent_decision_model,
+ deferIfPrecise: true,
});
}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/services/discobot-discoveries.js b/plugins/discourse-ai/assets/javascripts/discourse/services/discobot-discoveries.js
index 43b3d432a44..7817790f48c 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/services/discobot-discoveries.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/services/discobot-discoveries.js
@@ -206,7 +206,15 @@ export default class DiscobotDiscoveries extends Service {
@action
async triggerDiscovery(
query,
- { scope, trigger, triggerReason, intentHeuristic, classifyIntent } = {}
+ {
+ scope,
+ trigger,
+ triggerReason,
+ intentHeuristic,
+ classifyIntent,
+ deferIfPrecise,
+ deferredAskLogId,
+ } = {}
) {
const normalizedQuery = query?.trim();
@@ -229,16 +237,15 @@ export default class DiscobotDiscoveries extends Service {
const requestId = buildRequestId();
this.loadingDiscoveries = true;
this.activeRequestId = requestId;
- this.a11y.announce(
- i18n("discourse_ai.discobot_discoveries.asking"),
- "polite"
- );
+ // a search that may defer its answer is not an ask until the server says so
+ if (!deferIfPrecise) {
+ this.#noteAsking(normalizedQuery);
+ }
this.scheduleDiscoveryTimeout();
try {
this.lastQuery = normalizedQuery;
- this.rememberAsk(normalizedQuery);
const response = await ajax("/discourse-ai/discoveries/reply", {
type: "POST",
@@ -250,12 +257,22 @@ export default class DiscobotDiscoveries extends Service {
...(triggerReason && { trigger_reason: triggerReason }),
...(intentHeuristic && { intent_heuristic: intentHeuristic }),
...(classifyIntent && { classify_intent: true }),
+ ...(deferIfPrecise && { defer_if_precise: true }),
+ ...(deferredAskLogId && { deferred_ask_log_id: deferredAskLogId }),
},
});
if (response.request_id !== requestId) {
throw new Error("Discovery request ID mismatch");
}
+ if (deferIfPrecise && this.activeRequestId === requestId) {
+ if (response.answer_deferred) {
+ this.cancelDiscoveryTimeout();
+ this.loadingDiscoveries = false;
+ } else {
+ this.#noteAsking(normalizedQuery);
+ }
+ }
return response;
} catch {
if (this.activeRequestId === requestId) {
@@ -318,4 +335,12 @@ export default class DiscobotDiscoveries extends Service {
this.discoveryTimeout = null;
}
}
+
+ #noteAsking(query) {
+ this.a11y.announce(
+ i18n("discourse_ai.discobot_discoveries.asking"),
+ "polite"
+ );
+ this.rememberAsk(query);
+ }
}
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index 670ae7c473d..ea16388271d 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -1099,6 +1099,7 @@ en:
invalid_query: "Enter a search query between 1 and %{max} characters."
invalid_request_id: "The discovery request ID is invalid."
request_conflict: "This discovery request ID is already being used for another query."
+ deferred_answer_unavailable: "This search's answer was already requested."
processing_failed: "Ask AI could not complete this search."
temporarily_unavailable: "Ask AI is busy right now. Try again shortly."
credits_unavailable: "Ask AI is unavailable until AI credits reset."
diff --git a/plugins/discourse-ai/db/migrate/20261030215104_add_outcome_details_to_ask_ai_logs.rb b/plugins/discourse-ai/db/migrate/20261030215104_add_outcome_details_to_ask_ai_logs.rb
new file mode 100644
index 00000000000..8aa1f99ce9a
--- /dev/null
+++ b/plugins/discourse-ai/db/migrate/20261030215104_add_outcome_details_to_ask_ai_logs.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+class AddOutcomeDetailsToAskAiLogs < ActiveRecord::Migration[8.1]
+ def change
+ add_column :ask_ai_logs, :ai_agent_id, :bigint
+ add_column :ask_ai_logs, :llm_id, :bigint
+ add_column :ask_ai_logs, :no_answer_reason, :integer
+ add_column :ask_ai_logs, :time_to_outcome_ms, :integer
+ add_column :ask_ai_logs, :answer_requested_at, :datetime
+ add_column :ask_ai_logs, :ask_tab_opened_after_ms, :integer, null: false, default: 0
+ add_column :ask_ai_logs, :ask_answer_seen_after_ms, :integer
+ add_column :ask_ai_logs, :ask_result_clicked_tab, :string, null: false, default: ""
+ add_column :ask_ai_logs, :ask_result_clicked_after_ms, :integer, null: false, default: 0
+ end
+end
diff --git a/plugins/discourse-ai/lib/admin_dashboard/ask_ai.rb b/plugins/discourse-ai/lib/admin_dashboard/ask_ai.rb
index fdc315a14c8..6f0613a7dbf 100644
--- a/plugins/discourse-ai/lib/admin_dashboard/ask_ai.rb
+++ b/plugins/discourse-ai/lib/admin_dashboard/ask_ai.rb
@@ -19,7 +19,7 @@ module DiscourseAi
end
def build
- logs = AskAiLog.where(asked_at: @start_date..@end_date)
+ logs = AskAiLog.asked.where(asked_at: @start_date..@end_date)
questions, askers, average_ms =
logs.pick(
Arel.sql("COUNT(*)"),
diff --git a/plugins/discourse-ai/lib/discoveries.rb b/plugins/discourse-ai/lib/discoveries.rb
index 00bb993bb14..76a41a202df 100644
--- a/plugins/discourse-ai/lib/discoveries.rb
+++ b/plugins/discourse-ai/lib/discoveries.rb
@@ -14,6 +14,9 @@ module DiscourseAi
/\A[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\z/i
PRIVATE_MESSAGE_FILTER =
/(?:\A|\s)(?:in:(?:messages|personal|personal-direct|all-pms)|(?:group|personal)_messages:\S+)(?=\s|\z)/i
+ # keeps to the search menu's own check for operators and quoted phrases
+ PRECISE_SEARCH =
+ /\A\s*(?:"[^"]+"\s*)+\z|(?:\A|\s)-?(?:in|status|order|before|after|with|category|categories|tags?|user|group|badge|topic|min_\w+|max_\w+):\S|(?:\A|\s)[#@]\S+/i
BIND_REQUEST_SCRIPT = <<~LUA
local existing_query = redis.call("GET", KEYS[1])
if not existing_query then
@@ -98,6 +101,21 @@ module DiscourseAi
query.to_s.match?(PRIVATE_MESSAGE_FILTER)
end
+ def precise_search?(query)
+ query.to_s.match?(PRECISE_SEARCH)
+ end
+
+ # Whether answering would send the reader's private messages to the model.
+ def private_search?(query:, scope:, guardian:)
+ return true if private_message_query?(query) || private_message_query?(scope_filter(scope))
+
+ topic_id = scope.to_s[/\Atopic:(\d+)\z/, 1]
+ return false if topic_id.nil?
+
+ # a topic they cannot see stays indistinguishable from any other
+ guardian.can_see_topic?(Topic.private_messages.find_by(id: topic_id))
+ end
+
# Asked terms are kept apart from SearchLog rather than logged into it:
# its `search_type` records where a search happened, and the reports group
# by it. The two lists are merged for display instead.
@@ -161,8 +179,16 @@ module DiscourseAi
end
end
+ # Logs a search whose answer waits for the reader to request it.
+ # @return [AskAiLog]
+ def log_deferred_ask(user:, query:, scope: nil, **search)
+ create_ask_log(user:, query:, ask_outcome: :skipped, ask_scope: scope.to_s, **search)
+ end
+
# Queues the answer to a request already bound with `bind_request`.
- # @return [AskAiLog] the new ask's log
+ # @param deferred_log_id [Integer, nil] the search this answer was requested from
+ # @return [AskAiLog, nil] the ask's log, or nil once that search's answer
+ # was already requested
def enqueue_reply(
user:,
request_id:,
@@ -170,18 +196,24 @@ module DiscourseAi
scope: nil,
trigger: "",
trigger_reason: "",
- intent_heuristic: ""
+ intent_heuristic: "",
+ deferred_log_id: nil
)
asked_at = Time.current
- ask_log =
- AskAiLog.create!(
- user:,
- query:,
- asked_at:,
- ask_trigger: trigger,
- ask_trigger_reason: trigger_reason,
- ask_intent_heuristic: intent_heuristic,
- )
+ if deferred_log_id
+ # claimed in one statement, so parallel requests cannot each answer it
+ claimed =
+ AskAiLog.awaiting_answer(id: deferred_log_id, user:, query:).update_all(
+ ask_outcome: nil,
+ answer_requested_at: asked_at,
+ updated_at: asked_at,
+ )
+ return if claimed.zero?
+ ask_log = AskAiLog.find(deferred_log_id)
+ else
+ ask_log =
+ create_ask_log(user:, query:, asked_at:, trigger:, trigger_reason:, intent_heuristic:)
+ end
settings = result_settings(user)
Jobs.enqueue(
:stream_discover_reply,
@@ -200,15 +232,19 @@ module DiscourseAi
raise
end
- # 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.
+ # Records how the reader moved through the search menu's tabs for an ask.
+ # Each event is kept from its first report only.
def record_tabs(
user:,
ask_log_id:,
opened: nil,
opened_reason: "",
+ opened_after_ms: 0,
switched_to: nil,
- switched_after_ms: 0
+ switched_after_ms: 0,
+ answer_seen_after_ms: nil,
+ clicked_tab: nil,
+ clicked_after_ms: 0
)
ask_log = AskAiLog.find_by(id: ask_log_id, user_id: user.id)
raise Discourse::NotFound if ask_log.nil?
@@ -217,7 +253,11 @@ module DiscourseAi
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)
+ updates.merge!(
+ ask_tab_opened: opened,
+ ask_tab_opened_reason: opened_reason.to_s,
+ ask_tab_opened_after_ms: reported_ms(opened_after_ms),
+ )
end
if switched_to.present? && ask_log.ask_tab_switched_to.blank?
if SEARCH_TABS.exclude?(switched_to)
@@ -225,7 +265,19 @@ module DiscourseAi
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),
+ ask_tab_switched_after_ms: reported_ms(switched_after_ms),
+ )
+ end
+ if answer_seen_after_ms.present? && ask_log.ask_answer_seen_after_ms.nil?
+ updates[:ask_answer_seen_after_ms] = reported_ms(answer_seen_after_ms)
+ end
+ if clicked_tab.present? && ask_log.ask_result_clicked_tab.blank?
+ if SEARCH_TABS.exclude?(clicked_tab)
+ raise Discourse::InvalidParameters.new(:result_clicked_tab)
+ end
+ updates.merge!(
+ ask_result_clicked_tab: clicked_tab,
+ ask_result_clicked_after_ms: reported_ms(clicked_after_ms),
)
end
ask_log.update!(updates) if updates.present?
@@ -328,6 +380,30 @@ module DiscourseAi
private
+ def create_ask_log(
+ user:,
+ query:,
+ asked_at: Time.current,
+ trigger: "",
+ trigger_reason: "",
+ intent_heuristic: "",
+ **attributes
+ )
+ AskAiLog.create!(
+ user:,
+ query:,
+ asked_at:,
+ ask_trigger: trigger,
+ ask_trigger_reason: trigger_reason,
+ ask_intent_heuristic: intent_heuristic,
+ **attributes,
+ )
+ end
+
+ def reported_ms(milliseconds)
+ milliseconds.to_i.clamp(0, 1.hour.in_milliseconds)
+ end
+
def leaves_answers?(user)
opened_on_answer =
AskAiLog
@@ -345,6 +421,7 @@ module DiscourseAi
returned =
AskAiLog
+ .asked
.where(user_id: user.id)
.where.not(ask_tab_opened: ["", "ask"])
.order(asked_at: :desc)
diff --git a/plugins/discourse-ai/lib/discoveries/query_rewriter.rb b/plugins/discourse-ai/lib/discoveries/query_rewriter.rb
index 249ad405703..ddf68e435d8 100644
--- a/plugins/discourse-ai/lib/discoveries/query_rewriter.rb
+++ b/plugins/discourse-ai/lib/discoveries/query_rewriter.rb
@@ -20,11 +20,12 @@ module DiscourseAi
keyword_init: true,
)
- def initialize(user:, ai_agent:, llm_model:, cancel_manager: nil)
+ def initialize(user:, ai_agent:, llm_model:, cancel_manager: nil, feature_context: {})
@user = user
@ai_agent = ai_agent
@llm_model = llm_model
@cancel_manager = cancel_manager
+ @feature_context = feature_context
end
def call(query)
@@ -54,7 +55,9 @@ module DiscourseAi
)
output = nil
- bot.reply(context) { |partial, _, type| output = partial if type == :structured_output }
+ bot.reply(context, llm_args: { feature_context: @feature_context }) do |partial, _, type|
+ output = partial if type == :structured_output
+ end
Result.new(
keyword_query:
diff --git a/plugins/discourse-ai/lib/discoveries/synthesis.rb b/plugins/discourse-ai/lib/discoveries/synthesis.rb
index c1379334f43..4a9e3ab2bc9 100644
--- a/plugins/discourse-ai/lib/discoveries/synthesis.rb
+++ b/plugins/discourse-ai/lib/discoveries/synthesis.rb
@@ -4,18 +4,27 @@ module DiscourseAi
module Discoveries
class Synthesis
Result =
- Struct.new(:answerable, :source_refs, :title, :answer, :follow_up, keyword_init: true)
+ Struct.new(
+ :answerable,
+ :source_refs,
+ :title,
+ :answer,
+ :follow_up,
+ :no_answer_reason,
+ keyword_init: true,
+ )
PLACEHOLDER_ANSWERS = %w[true false null undefined none].freeze
SOURCE_REFERENCE_PATTERN =
/[ \t]*(?:\[\[source_\d+\]\]|\[source_\d+\](?:\([^)]+\))?|\(source_\d+\))(?:[ \t]*,[ \t]*(?:\[\[source_\d+\]\]|\[source_\d+\](?:\([^)]+\))?|\(source_\d+\)))*/i
SOURCES_SECTION_PATTERN =
/\n{2,}(?:\#{1,6}\s*)?(?:\*\*|__)?(?:sources|references)\s*:?(?:\*\*|__)?\s*\n.*\z/im
- def initialize(user:, ai_agent:, llm_model:, cancel_manager: nil)
+ def initialize(user:, ai_agent:, llm_model:, cancel_manager: nil, feature_context: {})
@user = user
@ai_agent = ai_agent
@llm_model = llm_model
@cancel_manager = cancel_manager
+ @feature_context = feature_context
end
def call(
@@ -26,11 +35,7 @@ module DiscourseAi
summary_detail: :balanced,
related_count: DiscourseAi::Discoveries::MIN_RELATED_DISCUSSIONS
)
- if candidates.empty?
- return(
- Result.new(answerable: false, source_refs: [], title: "", answer: "", follow_up: "")
- )
- end
+ return empty_result(:no_candidates) if candidates.empty?
related_count = normalized_related_count(related_count)
original_query_locale = normalized_original_query_locale(original_query_locale)
@@ -63,7 +68,7 @@ module DiscourseAi
)
values = { answerable: nil, source_refs: nil, title: +"", answer: +"", follow_up: +"" }
- bot.reply(context) do |partial, _, type|
+ bot.reply(context, llm_args: { feature_context: @feature_context }) do |partial, _, type|
next if type != :structured_output
answerable = partial.read_buffered_property(:answerable)
@@ -99,10 +104,9 @@ module DiscourseAi
answer: answer_without_source_references(values[:answer]),
follow_up: values[:follow_up].to_s.strip,
)
- if !result.answerable || result.source_refs.empty? ||
- !self.class.meaningful_answer?(result.answer)
- return empty_result
- end
+ return empty_result(:abstained) if !result.answerable
+ return empty_result(:unresolved_sources) if result.source_refs.empty?
+ return empty_result(:empty_answer) if !self.class.meaningful_answer?(result.answer)
result
end
@@ -136,8 +140,15 @@ module DiscourseAi
.first(related_count)
end
- def empty_result
- Result.new(answerable: false, source_refs: [], title: "", answer: "", follow_up: "")
+ def empty_result(no_answer_reason)
+ Result.new(
+ answerable: false,
+ source_refs: [],
+ title: "",
+ answer: "",
+ follow_up: "",
+ no_answer_reason:,
+ )
end
def answer_without_source_references(answer)
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.rb
index c5552db5c4a..daae72772fd 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
@@ -725,6 +725,67 @@ describe Jobs::StreamDiscoverReply do
)
end
+ it "records what answered and how long the outcome took" do
+ freeze_time
+ ask_log.update!(asked_at: 3.seconds.ago)
+
+ job.execute(args)
+
+ expect(ask_log.reload).to have_attributes(
+ ask_outcome: "answered",
+ ai_agent_id: SiteSetting.ai_ask_ai_agent.to_i,
+ llm_id: be_present,
+ time_to_outcome_ms: 3000,
+ no_answer_reason: nil,
+ )
+ end
+
+ it "times a requested answer from the request rather than the search" do
+ freeze_time
+ ask_log.update!(asked_at: 1.minute.ago, answer_requested_at: 2.seconds.ago)
+
+ job.execute(args)
+
+ expect(ask_log.reload).to have_attributes(
+ time_to_first_answer_ms: 2000,
+ time_to_outcome_ms: 2000,
+ )
+ end
+
+ it "records why the model's reply gave no answer" do
+ allow(synthesis).to receive(:call) do |**_, &stream|
+ stream.call(answerable: false)
+ DiscourseAi::Discoveries::Synthesis::Result.new(
+ answerable: false,
+ source_refs: [],
+ answer: "",
+ )
+ end
+
+ job.execute(args)
+
+ expect(ask_log.reload).to have_attributes(
+ ask_outcome: "no_answer",
+ no_answer_reason: "abstained",
+ )
+ end
+
+ it "records when none of the model's sources resolve" do
+ allow(retrieval).to receive(:sources_for).and_return([])
+ allow(synthesis).to receive(:call) do |**_, &stream|
+ stream.call(answerable: true, source_refs: %w[source_9], answer: "An answer.")
+ DiscourseAi::Discoveries::Synthesis::Result.new(
+ answerable: true,
+ source_refs: %w[source_9],
+ answer: "An answer.",
+ )
+ end
+
+ job.execute(args)
+
+ expect(ask_log.reload).to have_attributes(no_answer_reason: "unresolved_sources")
+ end
+
it "records an empty retrieval without inventing an answer latency" do
allow(retrieval).to receive(:call).and_return(
DiscourseAi::Discoveries::Retrieval::Result.new(candidates: []),
@@ -734,6 +795,7 @@ describe Jobs::StreamDiscoverReply do
expect(ask_log.reload).to have_attributes(
ask_outcome: "no_answer",
+ no_answer_reason: "no_candidates",
candidate_post_ids: [],
source_post_ids: [],
answer: nil,
diff --git a/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb b/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb
index 96521f47382..9ce55298140 100644
--- a/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discoveries/synthesis_spec.rb
@@ -42,6 +42,50 @@ describe DiscourseAi::Discoveries::Synthesis do
expect(source_refs["max_items"]).to eq(6)
end
+ it "says why a reply cannot be used" do
+ cases = [
+ [{ answerable: false, source_refs: [], title: "", answer: "" }, :abstained],
+ [
+ { answerable: true, source_refs: %w[source_9], title: "", answer: "An answer" },
+ :unresolved_sources,
+ ],
+ [{ answerable: true, source_refs: %w[source_1], title: "", answer: "null" }, :empty_answer],
+ ]
+
+ reasons =
+ cases.map do |response, _reason|
+ DiscourseAi::Completions::Llm.with_prepared_responses([response]) do
+ synthesis.call(
+ query: "plugins",
+ candidates: [
+ { "source_ref" => "source_1", "title" => "Plugins", "url" => "/t/plugins/1" },
+ ],
+ ).no_answer_reason
+ end
+ end
+
+ expect(reasons).to eq(cases.map(&:last))
+ end
+
+ it "labels its model calls so they can be traced to the ask" do
+ bot = instance_double(DiscourseAi::Agents::Bot, reply: nil)
+ allow(DiscourseAi::Agents::Bot).to receive(:as).and_return(bot)
+
+ described_class.new(user:, ai_agent:, llm_model:, feature_context: { ask_ai_log_id: 5 }).call(
+ query: "plugins",
+ candidates: [{ "source_ref" => "source_1", "title" => "Plugins", "url" => "/t/plugins/1" }],
+ )
+
+ expect(bot).to have_received(:reply).with(
+ an_instance_of(DiscourseAi::Agents::BotContext),
+ llm_args: {
+ feature_context: {
+ ask_ai_log_id: 5,
+ },
+ },
+ )
+ end
+
it "leaves out unset flags, whether the post is the first, and a same-day last update" do
candidate = {
"source_ref" => "source_1",
diff --git a/plugins/discourse-ai/spec/lib/discoveries_spec.rb b/plugins/discourse-ai/spec/lib/discoveries_spec.rb
index f550b5b6b1c..3bef8646acb 100644
--- a/plugins/discourse-ai/spec/lib/discoveries_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discoveries_spec.rb
@@ -83,7 +83,59 @@ describe DiscourseAi::Discoveries do
end
end
+ describe ".precise_search?" do
+ it "is true for search operators and searches of only quoted phrases" do
+ queries = [
+ "password status:open",
+ "-in:bookmarks reset",
+ "before:2026-01-01 outage",
+ "@sam",
+ "#how-to",
+ '"slow mode"',
+ '"slow mode" "rate limit"',
+ ]
+
+ expect(queries.reject { |query| described_class.precise_search?(query) }).to be_empty
+ end
+
+ it "is false for plain searches and questions" do
+ queries = [
+ "password reset",
+ 'what does "slow mode" do?',
+ '"slow mode" settings',
+ "note: how do I reset it",
+ "[email protected] bounces",
+ nil,
+ ]
+
+ expect(queries.select { |query| described_class.precise_search?(query) }).to be_empty
+ end
+ end
+
describe ".enqueue_reply" do
+ it "answers a deferred search only once" do
+ deferred_log =
+ AskAiLog.create!(
+ user:,
+ query: "猫 status:open",
+ asked_at: 1.minute.ago,
+ ask_outcome: :skipped,
+ )
+ enqueue = -> do
+ described_class.enqueue_reply(
+ user:,
+ request_id: SecureRandom.uuid,
+ query: deferred_log.query,
+ deferred_log_id: deferred_log.id,
+ )
+ end
+
+ expect { 2.times.map { enqueue.call } }.to change { Jobs::StreamDiscoverReply.jobs.size }.by(
+ 1,
+ )
+ expect(deferred_log.reload.answer_requested_at).to be_present
+ end
+
it "records a failed ask if the job cannot be queued" do
allow(Jobs).to receive(:enqueue).and_raise(StandardError, "queue unavailable")
diff --git a/plugins/discourse-ai/spec/models/ask_ai_log_spec.rb b/plugins/discourse-ai/spec/models/ask_ai_log_spec.rb
index 66b525de672..56f5cd21c23 100644
--- a/plugins/discourse-ai/spec/models/ask_ai_log_spec.rb
+++ b/plugins/discourse-ai/spec/models/ask_ai_log_spec.rb
@@ -1,6 +1,19 @@
# frozen_string_literal: true
RSpec.describe AskAiLog do
+ describe ".asked" do
+ fab!(:user)
+
+ it "leaves out searches whose answer was never requested" do
+ pending_ask = AskAiLog.create!(user:, query: "pending", asked_at: Time.current)
+ answered =
+ AskAiLog.create!(user:, query: "done", asked_at: Time.current, ask_outcome: :answered)
+ AskAiLog.create!(user:, query: "in:messages x", asked_at: Time.current, ask_outcome: :skipped)
+
+ expect(AskAiLog.asked).to contain_exactly(pending_ask, answered)
+ end
+ end
+
describe "#record_judgement!" do
fab!(:user)
diff --git a/plugins/discourse-ai/spec/requests/discover/discoveries_controller_spec.rb b/plugins/discourse-ai/spec/requests/discover/discoveries_controller_spec.rb
index ddbcfbff62a..9f2dfc805f8 100644
--- a/plugins/discourse-ai/spec/requests/discover/discoveries_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/discover/discoveries_controller_spec.rb
@@ -193,6 +193,93 @@ describe DiscourseAi::Discover::DiscoveriesController do
expect(response.status).to eq(409)
end
+
+ it "charges a request that names a deferred search but asks to defer again once it is used" do
+ RateLimiter.enable
+ stub_const(DiscourseAi::Discoveries, :ASK_RATE_LIMIT, 1) do
+ query = "password status:open"
+ post "/discourse-ai/discoveries/reply",
+ params: {
+ query:,
+ request_id:,
+ defer_if_precise: true,
+ }
+ params = {
+ query:,
+ deferred_ask_log_id: response.parsed_body["ask_log_id"],
+ defer_if_precise: true,
+ }
+
+ expect {
+ post "/discourse-ai/discoveries/reply",
+ params: params.merge(request_id: SecureRandom.uuid)
+ }.not_to change { AskAiLog.count }
+ expect(response.status).to eq(200)
+ expect(response.parsed_body).not_to have_key("answer_deferred")
+
+ post "/discourse-ai/discoveries/reply",
+ params: params.merge(request_id: SecureRandom.uuid)
+ expect(response.status).to eq(429)
+ end
+ end
+
+ it "lets the reader request a deferred answer after using every search" do
+ RateLimiter.enable
+ stub_const(DiscourseAi::Discoveries, :ASK_RATE_LIMIT, 1) do
+ query = "password status:open"
+ post "/discourse-ai/discoveries/reply",
+ params: {
+ query:,
+ request_id:,
+ defer_if_precise: true,
+ }
+ deferred_ask_log_id = response.parsed_body["ask_log_id"]
+
+ post "/discourse-ai/discoveries/reply",
+ params: {
+ query:,
+ request_id: SecureRandom.uuid,
+ deferred_ask_log_id:,
+ }
+ expect(response.status).to eq(200)
+
+ post "/discourse-ai/discoveries/reply",
+ params: {
+ query:,
+ request_id: SecureRandom.uuid,
+ deferred_ask_log_id:,
+ }
+ expect(response.status).to eq(429)
+
+ post "/discourse-ai/discoveries/reply",
+ params: {
+ query: "What is Discourse?",
+ request_id: SecureRandom.uuid,
+ }
+ expect(response.status).to eq(429)
+ end
+ end
+
+ it "returns a 409 when a deferred answer was already requested" do
+ query = "password status:open"
+ post "/discourse-ai/discoveries/reply",
+ params: {
+ query:,
+ request_id:,
+ defer_if_precise: true,
+ }
+ deferred_ask_log_id = response.parsed_body["ask_log_id"]
+ 2.times do
+ post "/discourse-ai/discoveries/reply",
+ params: {
+ query:,
+ request_id: SecureRandom.uuid,
+ deferred_ask_log_id:,
+ }
+ end
+
+ expect(response.status).to eq(409)
+ end
end
end
@@ -408,6 +495,54 @@ describe DiscourseAi::Discover::DiscoveriesController do
end
end
+ describe "#record_tabs timings and result clicks" 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 when the tab opened, the answer was seen and a result was opened, keeping each first report" do
+ 2.times do |attempt|
+ put "/discourse-ai/discoveries/tabs",
+ params: {
+ ask_log_id: ask_log.id,
+ tab_opened: "ask",
+ tab_opened_after_ms: 900 + attempt,
+ answer_seen_after_ms: 4000 + attempt,
+ result_clicked_tab: "ask",
+ result_clicked_after_ms: 9000 + attempt,
+ }
+ end
+
+ expect(ask_log.reload).to have_attributes(
+ ask_tab_opened_after_ms: 900,
+ ask_answer_seen_after_ms: 4000,
+ ask_result_clicked_tab: "ask",
+ ask_result_clicked_after_ms: 9000,
+ )
+ end
+
+ it "rejects an unknown clicked tab" do
+ put "/discourse-ai/discoveries/tabs",
+ params: {
+ ask_log_id: ask_log.id,
+ result_clicked_tab: "nope",
+ }
+
+ expect(response.status).to eq(400)
+ end
+ end
+
describe "#record_tabs" do
fab!(:llm_model)
fab!(:ai_agent) do
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.rb
index 3ff3de95668..2f5b04559de 100644
--- 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.rb
@@ -280,6 +280,133 @@ RSpec.describe DiscourseAi::AskAi::Ask do
end
end
+ context "when a search using operators may defer its answer" do
+ let(:query) { "password reset status:open" }
+ let(:classify_intent) { true }
+ let(:params) { super().merge(defer_if_precise: true) }
+
+ it { is_expected.to run_successfully }
+
+ it "logs the search as skipped, without an answer or a judgement" do
+ expect_not_enqueued_with(job: :stream_discover_reply) { result }
+ expect(ask_log).to have_attributes(
+ ask_outcome: "skipped",
+ ask_trigger: "enter",
+ ask_intent_model: "",
+ )
+ expect(result[:judgement]).to be_nil
+ end
+
+ it "does not remember it as an ask" do
+ result
+ expect(DiscourseAi::Discoveries.recent_asks(user_id: user.id)).to be_empty
+ end
+ end
+
+ context "when a plain search may defer its answer" do
+ let(:params) { super().merge(defer_if_precise: true) }
+
+ it "queues the answer" do
+ expect_enqueued_with(job: :stream_discover_reply, args: { request_id: }) { result }
+ end
+ end
+
+ context "when a search within the reader's messages may defer its answer" do
+ let(:scope) { "messages" }
+ let(:params) { super().merge(defer_if_precise: true) }
+
+ it "logs the search as skipped with its scope" do
+ expect_not_enqueued_with(job: :stream_discover_reply) { result }
+ expect(ask_log).to have_attributes(ask_outcome: "skipped", ask_scope: "messages")
+ end
+ end
+
+ context "when a search made inside a personal message may defer its answer" do
+ fab!(:private_message) { Fabricate(:private_message_topic, user:) }
+ fab!(:other_private_message, :private_message_topic)
+
+ let(:scope) { "topic:#{private_message.id}" }
+ let(:params) { super().merge(defer_if_precise: true) }
+
+ it "logs the search as skipped" do
+ expect_not_enqueued_with(job: :stream_discover_reply) { result }
+ expect(ask_log).to have_attributes(ask_outcome: "skipped")
+ end
+
+ context "when the reader cannot see that message" do
+ let(:scope) { "topic:#{other_private_message.id}" }
+
+ it "does not reveal it by deferring" do
+ expect_enqueued_with(job: :stream_discover_reply, args: { request_id: }) { result }
+ end
+ end
+ end
+
+ context "when the reader requests an answer deferred by an earlier search" do
+ fab!(:deferred_log) do
+ AskAiLog.create!(
+ user:,
+ query: "password reset status:open",
+ asked_at: 1.minute.ago,
+ ask_outcome: :skipped,
+ )
+ end
+
+ let(:query) { deferred_log.query }
+ let(:params) { super().merge(deferred_ask_log_id: deferred_log.id) }
+
+ it "answers on the search's own log" do
+ freeze_time
+
+ expect { result }.not_to change { AskAiLog.count }
+ expect(deferred_log.reload).to have_attributes(
+ ask_outcome: nil,
+ answer_requested_at: be_within(1.second).of(Time.current),
+ )
+ expect(Jobs::StreamDiscoverReply.jobs.last["args"].first).to include(
+ "ask_ai_log_id" => deferred_log.id,
+ )
+ end
+
+ context "when the request also asks to defer and to be judged" do
+ let(:classify_intent) { true }
+ let(:params) { super().merge(defer_if_precise: true) }
+
+ it "answers on the search's own log without holding it back again" do
+ expect { result }.not_to change { AskAiLog.count }
+ expect(deferred_log.reload).to have_attributes(answer_requested_at: be_present)
+ expect(result[:judgement]).to be_nil
+ end
+ end
+
+ context "when the log belongs to someone else" do
+ before { deferred_log.update!(user: Fabricate(:user)) }
+
+ it { is_expected.to fail_a_policy(:deferred_answer_available) }
+
+ it "leaves their search alone" do
+ result
+ expect(deferred_log.reload).to have_attributes(ask_outcome: "skipped")
+ end
+ end
+
+ context "when the log is for a different search" do
+ let(:query) { "how do I reset my password" }
+
+ it { is_expected.to fail_a_policy(:deferred_answer_available) }
+ end
+
+ context "when the answer was already requested" do
+ before { deferred_log.update!(ask_outcome: nil, answer_requested_at: Time.current) }
+
+ it { is_expected.to fail_a_policy(:deferred_answer_available) }
+
+ it "does not queue another answer" do
+ expect_not_enqueued_with(job: :stream_discover_reply) { result }
+ end
+ end
+ end
+
context "when everything's ok" do
it { is_expected.to run_successfully }
diff --git a/plugins/discourse-ai/spec/system/ai_combined_search_spec.rb b/plugins/discourse-ai/spec/system/ai_combined_search_spec.rb
index 32937a09ac2..1afeee8f70b 100644
--- a/plugins/discourse-ai/spec/system/ai_combined_search_spec.rb
+++ b/plugins/discourse-ai/spec/system/ai_combined_search_spec.rb
@@ -350,6 +350,46 @@ describe "AI combined search" do
end
end
+ it "records the first result the reader opens" do
+ visit "/"
+ wait_for_discoveries_channel
+ ask(query)
+
+ expect_tab("topics")
+ find(".search-menu .search-result-topic a.search-link", text: topic_1.title).click
+
+ try_until_success do
+ expect(AskAiLog.last).to have_attributes(
+ ask_tab_opened_after_ms: be >= 0,
+ ask_result_clicked_tab: "topics",
+ ask_result_clicked_after_ms: be > 0,
+ )
+ end
+ end
+
+ it "leaves the answer to a search using operators until the reader opens it" do
+ term = "password status:open"
+ visit "/"
+ wait_for_discoveries_channel
+ find("#welcome-banner-search-input").fill_in(with: term)
+ find("#welcome-banner-search-input").send_keys(:enter)
+
+ expect_tab("topics")
+ try_until_success do
+ expect(AskAiLog.find_by(query: term)).to have_attributes(
+ ask_outcome: "skipped",
+ ask_tab_opened: "topics",
+ ask_tab_opened_reason: "syntax",
+ )
+ end
+ expect(request_ids).to be_empty
+
+ select_tab("ask")
+
+ expect(page).to have_css(".ai-search-answer")
+ try_until_success { expect(request_ids.size).to eq(1) }
+ end
+
context "with a decision model judging intent" do
fab!(:decision_model)
@@ -865,7 +905,8 @@ describe "AI combined search" do
it "opens a query in search syntax on its topics" do
visit "/"
wait_for_discoveries_channel
- ask('"small spaces"')
+ find("#welcome-banner-search-input").fill_in(with: '"small spaces"')
+ find("#welcome-banner-search-input").send_keys(:enter)
expect_tab("topics")
expect(page).to have_css(
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.js
index 0bbb5233642..1328feaf587 100644
--- 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.js
@@ -17,6 +17,25 @@ module("Discourse AI | Unit | Lib | ai-search-intent", function (hooks) {
);
});
+ test("a deferred answer opens on results, even for a question", function (assert) {
+ assert.deepEqual(
+ chooseTab("where did I put that message?", {
+ topics,
+ answerDeferred: true,
+ }),
+ { tab: "topics", reason: "answer_deferred" }
+ );
+ assert.deepEqual(
+ chooseTab("where did I put that message?", {
+ topics,
+ context: topics,
+ scope: "messages",
+ answerDeferred: true,
+ }),
+ { tab: "context", reason: "answer_deferred" }
+ );
+ });
+
test("a lookup intent never moves a question off the answer", function (assert) {
assert.deepEqual(
chooseTab("where is the theme settings page?", {
@@ -127,6 +146,16 @@ module("Discourse AI | Unit | Lib | ai-search-intent", function (hooks) {
);
});
+ test("a question quoting a phrase is not search syntax", function (assert) {
+ assert.notStrictEqual(
+ chooseTab('what does "slow mode" do?', {
+ topics,
+ intent: { intent: "answer", probability: 0.9 },
+ }).reason,
+ "syntax"
+ );
+ });
+
test("a lookup with no keyword matches still opens on Ask AI", function (assert) {
assert.deepEqual(
chooseTab("password reset", {
diff --git a/plugins/discourse-ai/test/javascripts/unit/services/ai-search-session-test.js b/plugins/discourse-ai/test/javascripts/unit/services/ai-search-session-test.js
index d3cd2d76e2c..1d1b5830c2b 100644
--- a/plugins/discourse-ai/test/javascripts/unit/services/ai-search-session-test.js
+++ b/plugins/discourse-ai/test/javascripts/unit/services/ai-search-session-test.js
@@ -1,4 +1,4 @@
-import { settled } from "@ember/test-helpers";
+import { settled, waitUntil } from "@ember/test-helpers";
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import { translateResults } from "discourse/lib/search";
@@ -15,6 +15,149 @@ function searchResults(ids) {
module("Unit | Service | ai-search-session", function (hooks) {
setupTest(hooks);
+ module("with a deferred answer", function (nestedHooks) {
+ let replies;
+ let tabReports;
+
+ nestedHooks.beforeEach(function () {
+ replies = [];
+ tabReports = [];
+ this.session = this.owner.lookup("service:ai-search-session");
+ this.owner.lookup("service:search").activeGlobalSearchTerm =
+ "in:messages";
+
+ pretender.get("/search/query", () => response(searchResults([1])));
+ pretender.post("/discourse-ai/discoveries/reply", (request) => {
+ const body = new URLSearchParams(request.requestBody);
+ replies.push(Object.fromEntries(body));
+ return response({
+ request_id: body.get("request_id"),
+ ask_log_id: 7,
+ ...(replies.length === 1 && { answer_deferred: true }),
+ });
+ });
+ pretender.put("/discourse-ai/discoveries/tabs", (request) => {
+ tabReports.push(
+ Object.fromEntries(new URLSearchParams(request.requestBody))
+ );
+ return response({});
+ });
+ });
+
+ nestedHooks.afterEach(function () {
+ this.owner
+ .lookup("service:discobot-discoveries")
+ .cancelDiscoveryTimeout();
+ });
+
+ test("opens on results and requests nothing further", async function (assert) {
+ this.session.start("in:messages");
+ await settled();
+
+ assert.strictEqual(this.session.selectedTab, "topics");
+ assert.strictEqual(replies.length, 1, "only the search itself was sent");
+ assert.strictEqual(replies[0].defer_if_precise, "true");
+ assert.false(
+ this.session.discoveries.loadingDiscoveries,
+ "nothing is shown as being generated"
+ );
+ });
+
+ test("requests the answer on the search's log when Ask is opened", async function (assert) {
+ this.session.start("in:messages");
+ await settled();
+
+ this.session.selectTab("ask");
+ assert.true(
+ this.session.discoveries.loadingDiscoveries,
+ "the answer is now on its way"
+ );
+ await waitUntil(() => replies.length === 2);
+
+ assert.strictEqual(replies[1].deferred_ask_log_id, "7");
+ assert.strictEqual(replies[1].defer_if_precise, undefined);
+
+ this.session.selectTab("topics");
+ this.session.selectTab("ask");
+ this.session.discoveries.cancelDiscoveryTimeout();
+ await settled();
+ assert.strictEqual(replies.length, 2, "it is requested only once");
+ });
+
+ test("reports when the tab opened and the first result opened", async function (assert) {
+ this.session.start("in:messages");
+ await settled();
+
+ this.session.recordResultClick("in:messages");
+ this.session.recordResultClick("in:messages");
+ await settled();
+
+ const opened = tabReports.find((report) => report.tab_opened);
+ assert.true(Number(opened.tab_opened_after_ms) >= 0);
+ assert.deepEqual(
+ tabReports
+ .filter((report) => report.result_clicked_tab)
+ .map((report) => report.result_clicked_tab),
+ ["topics"],
+ "only the first click is reported"
+ );
+ });
+
+ test("ignores a click once the term no longer matches the search", async function (assert) {
+ this.session.start("in:messages");
+ await settled();
+
+ this.session.recordResultClick("something else");
+ await settled();
+
+ assert.false(tabReports.some((report) => report.result_clicked_tab));
+ });
+ });
+
+ test("reports when an answer is first seen on the Ask tab", async function (assert) {
+ const session = this.owner.lookup("service:ai-search-session");
+ const discoveries = this.owner.lookup("service:discobot-discoveries");
+ const tabReports = [];
+ this.owner.lookup("service:search").activeGlobalSearchTerm = "password";
+
+ pretender.get("/search/query", () => response(searchResults([1])));
+ pretender.post("/discourse-ai/discoveries/reply", (request) =>
+ response({
+ request_id: new URLSearchParams(request.requestBody).get("request_id"),
+ ask_log_id: 7,
+ })
+ );
+ pretender.put("/discourse-ai/discoveries/tabs", (request) => {
+ tabReports.push(
+ Object.fromEntries(new URLSearchParams(request.requestBody))
+ );
+ return response({});
+ });
+
+ const seen = () => tabReports.filter((r) => r.answer_seen_after_ms);
+
+ session.start("password");
+ await waitUntil(() => session.selectedTab);
+ assert.strictEqual(session.selectedTab, "topics");
+
+ session.onDiscovery({
+ query: "password",
+ request_id: discoveries.activeRequestId,
+ ai_discover_reply: "Reset it from your profile.",
+ });
+ discoveries.cancelDiscoveryTimeout();
+ discoveries.smoothStreamer.resetStreaming();
+ await waitUntil(() => tabReports.some((report) => report.tab_opened));
+ assert.strictEqual(seen().length, 0, "not while another tab is showing");
+
+ session.selectTab("ask");
+ session.selectTab("topics");
+ session.selectTab("ask");
+ await waitUntil(() => seen().length > 0);
+ await settled();
+ assert.strictEqual(seen().length, 1, "once the reader is on the answer");
+ });
+
test("keeps original matches when a larger rewrite result omits one", async function (assert) {
const session = this.owner.lookup("service:ai-search-session");
const search = this.owner.lookup("service:search");
@@ -148,4 +291,69 @@ module("Unit | Service | ai-search-session", function (hooks) {
"scoped keyword results preserve original matches too"
);
});
+
+ test("opens on results when a deferred reply lands after the wait but before the results", async function (assert) {
+ const session = this.owner.lookup("service:ai-search-session");
+ const query = "how do I get a refund for my order?";
+ this.owner.lookup("service:search").activeGlobalSearchTerm = query;
+
+ // the reply outlasts the wait for it, and the results outlast the reply
+ pretender.get("/search/query", () => response(searchResults([1])), 1400);
+ pretender.post("/discourse-ai/discoveries/reply", (request) => {
+ const reply = response({
+ request_id: new URLSearchParams(request.requestBody).get("request_id"),
+ ask_log_id: 7,
+ answer_deferred: true,
+ });
+ return new Promise((resolve) => setTimeout(resolve, 1150, reply));
+ });
+ pretender.put("/discourse-ai/discoveries/tabs", () => response({}));
+
+ session.start(query);
+ await waitUntil(() => session.selectedTab, { timeout: 5000 });
+
+ assert.notStrictEqual(
+ session.selectedTab,
+ "ask",
+ "no answer was queued, so the search does not open on one"
+ );
+ });
+
+ test("asks again for a deferred answer whose request failed", async function (assert) {
+ const session = this.owner.lookup("service:ai-search-session");
+ const discoveries = this.owner.lookup("service:discobot-discoveries");
+ const replies = [];
+ this.owner.lookup("service:search").activeGlobalSearchTerm = "in:messages";
+
+ pretender.get("/search/query", () => response(searchResults([1])));
+ pretender.post("/discourse-ai/discoveries/reply", (request) => {
+ const body = new URLSearchParams(request.requestBody);
+ replies.push(Object.fromEntries(body));
+ if (replies.length === 2) {
+ return response(429, {});
+ }
+ return response({
+ request_id: body.get("request_id"),
+ ask_log_id: 7,
+ ...(replies.length === 1 && { answer_deferred: true }),
+ });
+ });
+ pretender.put("/discourse-ai/discoveries/tabs", () => response({}));
+
+ session.start("in:messages");
+ await settled();
+
+ session.selectTab("ask");
+ await waitUntil(() => discoveries.errorMessage);
+ session.selectTab("topics");
+ session.selectTab("ask");
+ await waitUntil(() => replies.length === 3);
+ discoveries.cancelDiscoveryTimeout();
+
+ assert.strictEqual(
+ replies[2].deferred_ask_log_id,
+ "7",
+ "the retry still answers on the search's log"
+ );
+ });
});
diff --git a/plugins/discourse-ai/test/javascripts/unit/services/discobot-discoveries-test.js b/plugins/discourse-ai/test/javascripts/unit/services/discobot-discoveries-test.js
index 9bbd556a5de..b48af5285c9 100644
--- a/plugins/discourse-ai/test/javascripts/unit/services/discobot-discoveries-test.js
+++ b/plugins/discourse-ai/test/javascripts/unit/services/discobot-discoveries-test.js
@@ -263,6 +263,52 @@ module("Unit | Service | discobot-discoveries", function (hooks) {
);
});
+ test("announces asking only once the server keeps the answer", async function (assert) {
+ const announcements = [];
+ this.owner.register(
+ "service:a11y",
+ class extends Service {
+ announce(message, priority) {
+ announcements.push({ message, priority });
+ }
+ }
+ );
+ pretender.post("/discourse-ai/discoveries/reply", (request) => {
+ const body = new URLSearchParams(request.requestBody);
+ return response(200, {
+ request_id: body.get("request_id"),
+ answer_deferred: body.get("query").includes("in:messages"),
+ });
+ });
+ const service = getOwner(this).lookup("service:discobot-discoveries");
+
+ await service.triggerDiscovery("refund in:messages", {
+ deferIfPrecise: true,
+ });
+
+ assert.deepEqual(announcements, [], "a deferred answer is not announced");
+ assert.deepEqual(
+ service.recentAsks,
+ [],
+ "a deferred answer is not a recent ask"
+ );
+
+ await service.triggerDiscovery("What is Discourse?", {
+ deferIfPrecise: true,
+ });
+
+ assert.deepEqual(
+ announcements,
+ [{ message: "Asking AI…", priority: "polite" }],
+ "an answer the server keeps is announced"
+ );
+ assert.deepEqual(
+ service.recentAsks.map((ask) => ask.term),
+ ["What is Discourse?"],
+ "an answer the server keeps is a recent ask"
+ );
+ });
+
test("announces a completed answer", async function (assert) {
const announcements = [];
this.owner.register(
diff --git a/plugins/discourse-data-explorer/lib/discourse_data_explorer/queries.rb b/plugins/discourse-data-explorer/lib/discourse_data_explorer/queries.rb
index 6fcfa525223..3ff1734036d 100644
--- a/plugins/discourse-data-explorer/lib/discourse_data_explorer/queries.rb
+++ b/plugins/discourse-data-explorer/lib/discourse_data_explorer/queries.rb
@@ -1692,6 +1692,7 @@ module DiscourseDataExplorer
FROM ask_ai_logs
WHERE asked_at >= :start_date::date
AND asked_at < :end_date::date + INTERVAL '1 day'
+ AND ask_outcome IS DISTINCT FROM 4 -- skipped: the reader never requested an answer
GROUP BY asked_at::date
)
SELECT days.date::date AS date, COALESCE(daily_asks.asks, 0) AS asks
@@ -1718,6 +1719,7 @@ module DiscourseDataExplorer
FROM ask_ai_logs
WHERE asked_at >= :start_date::date
AND asked_at < :end_date::date + INTERVAL '1 day'
+ AND ask_outcome IS DISTINCT FROM 4 -- skipped: the reader never requested an answer
GROUP BY ask_outcome
ORDER BY ask_outcome NULLS LAST
SQL
@@ -1769,6 +1771,7 @@ module DiscourseDataExplorer
FROM ask_ai_logs
WHERE asked_at >= :start_date::date
AND asked_at < :end_date::date + INTERVAL '1 day'
+ AND ask_outcome IS DISTINCT FROM 4 -- skipped: the reader never requested an answer
ORDER BY ask_ai_logs.asked_at DESC, id DESC
SQL