1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/contextual_cueing/zero_state_suggestions_request.h"
#include <string>
#include <vector>
#include "base/barrier_callback.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "chrome/browser/contextual_cueing/zero_state_suggestions_page_data.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "components/optimization_guide/core/optimization_guide_logger.h"
#include "content/public/browser/web_contents.h"
namespace contextual_cueing {
ZeroStateSuggestionsRequest::ZeroStateSuggestionsRequest(
OptimizationGuideKeyedService* optimization_guide_keyed_service,
const optimization_guide::proto::ZeroStateSuggestionsRequest&
pending_base_request,
const std::vector<content::WebContents*>& requested_tabs,
const content::WebContents* focused_tab)
: begin_time_(base::TimeTicks::Now()),
pending_base_request_(pending_base_request),
requested_tabs_(requested_tabs),
optimization_guide_keyed_service_(optimization_guide_keyed_service) {
OPTIMIZATION_GUIDE_LOG(
optimization_guide_common::mojom::LogSource::MODEL_EXECUTION,
optimization_guide_keyed_service_->GetOptimizationGuideLogger(),
base::StringPrintf(
"ZeroStateSuggestionsRequest: Creating new zero state suggestions "
"request for %llu tabs",
requested_tabs.size()));
auto barrier_callback = base::BarrierCallback<
std::optional<optimization_guide::proto::ZeroStatePageContext>>(
requested_tabs.size(),
base::BindOnce(&ZeroStateSuggestionsRequest::OnAllPageContextExtracted,
weak_ptr_factory_.GetWeakPtr()));
for (auto* tab : requested_tabs) {
auto* zss_data =
ZeroStateSuggestionsPageData::GetOrCreateForPage(tab->GetPrimaryPage());
// Capture the page data for the focused tab to cache suggestions into if we
// are in that mode.
if (pending_base_request_.has_page_context()) {
CHECK_EQ(requested_tabs.size(), 1u);
focused_tab_page_data_ = zss_data->AsWeakPtr();
}
// Do not fetch page context if there are already cached suggestions for the
// focused tab and we are in focused tab mode.
if (focused_tab_page_data_ &&
focused_tab_page_data_->cached_suggestions_for_focused_tab()) {
return;
}
// If we're in multitab mode, store the information about focused tab.
if (focused_tab && tab == focused_tab) {
zss_data->set_is_focused_tab(true);
} else {
zss_data->set_is_focused_tab(false);
}
// Otherwise, start grabbing the page context.
zss_data->GetPageContext(barrier_callback);
}
}
ZeroStateSuggestionsRequest::~ZeroStateSuggestionsRequest() {
OPTIMIZATION_GUIDE_LOG(
optimization_guide_common::mojom::LogSource::MODEL_EXECUTION,
optimization_guide_keyed_service_->GetOptimizationGuideLogger(),
"ZeroStateSuggestionsRequest: Destructing zero state suggestions "
"request");
}
// static
void ZeroStateSuggestionsRequest::Destroy(
std::unique_ptr<ZeroStateSuggestionsRequest> request) {
// The unique_ptr deletes automatically.
}
void ZeroStateSuggestionsRequest::AddCallback(
base::OnceCallback<void(std::vector<std::string>)> callback) {
// Check if we have cached suggestions if we are in focused tab mode.
if (focused_tab_page_data_) {
if (auto cached_suggestions =
focused_tab_page_data_->cached_suggestions_for_focused_tab()) {
// Post cached suggestions to back of UI thread.
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), *cached_suggestions));
return;
}
}
pending_callbacks_.AddUnsafe(std::move(callback));
}
std::vector<content::WebContents*>
ZeroStateSuggestionsRequest::GetRequestedTabs() const {
return requested_tabs_;
}
void ZeroStateSuggestionsRequest::OnAllPageContextExtracted(
const std::vector<
std::optional<optimization_guide::proto::ZeroStatePageContext>>&
zero_state_page_contexts) {
// Filter for page contexts that are available.
std::vector<optimization_guide::proto::ZeroStatePageContext>
filtered_page_contexts;
for (const auto& zero_state_page_context : zero_state_page_contexts) {
if (zero_state_page_context) {
filtered_page_contexts.push_back(*zero_state_page_context);
}
}
// No content to generate suggestions. Return empty.
if (filtered_page_contexts.empty()) {
OPTIMIZATION_GUIDE_LOG(
optimization_guide_common::mojom::LogSource::MODEL_EXECUTION,
optimization_guide_keyed_service_->GetOptimizationGuideLogger(),
"ZeroStateSuggestionsRequest: No page context to fetch suggestions "
"for.");
CacheFocusedTabSuggestions({});
pending_callbacks_.Notify(std::vector<std::string>({}));
return;
}
// Add page context to request.
if (pending_base_request_.has_page_context()) {
CHECK_EQ(filtered_page_contexts.size(), 1u);
*pending_base_request_.mutable_page_context() =
filtered_page_contexts.front().page_context();
} else if (pending_base_request_.has_page_context_list()) {
pending_base_request_.mutable_page_context()->Clear();
*pending_base_request_.mutable_page_context_list()
->mutable_page_contexts() = {filtered_page_contexts.begin(),
filtered_page_contexts.end()};
}
// Initiate model execution fetch.
OPTIMIZATION_GUIDE_LOG(
optimization_guide_common::mojom::LogSource::MODEL_EXECUTION,
optimization_guide_keyed_service_->GetOptimizationGuideLogger(),
base::StringPrintf(
"ZeroStateSuggestionsRequest: Starting fetch for "
"suggestions. Is-mulitab request: %s",
pending_base_request_.has_page_context_list() ? "true" : "false"));
optimization_guide_keyed_service_->ExecuteModel(
optimization_guide::ModelBasedCapabilityKey::kZeroStateSuggestions,
pending_base_request_,
/*execution_timeout=*/std::nullopt,
base::BindOnce(&ZeroStateSuggestionsRequest::OnModelExecutionResponse,
weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()));
}
void ZeroStateSuggestionsRequest::OnModelExecutionResponse(
base::TimeTicks mes_begin_time,
optimization_guide::OptimizationGuideModelExecutionResult result,
std::unique_ptr<optimization_guide::ModelQualityLogEntry> log_entry) {
base::UmaHistogramTimes("ContextualCueing.GlicSuggestions.MesFetchLatency",
base::TimeTicks::Now() - mes_begin_time);
base::TimeDelta suggestions_duration = base::TimeTicks::Now() - begin_time_;
if (!result.response.has_value()) {
OPTIMIZATION_GUIDE_LOG(
optimization_guide_common::mojom::LogSource::MODEL_EXECUTION,
optimization_guide_keyed_service_->GetOptimizationGuideLogger(),
base::StringPrintf("ZeroStateSuggestionsRequest: Failed to get "
"suggestions after %ld ms. Error: %d",
suggestions_duration.InMilliseconds(),
static_cast<int>(result.response.error().error())));
pending_callbacks_.Notify(std::vector<std::string>({}));
CacheFocusedTabSuggestions({});
return;
}
OPTIMIZATION_GUIDE_LOG(
optimization_guide_common::mojom::LogSource::MODEL_EXECUTION,
optimization_guide_keyed_service_->GetOptimizationGuideLogger(),
base::StringPrintf("ZeroStateSuggestionsRequest: Received valid "
"suggestions after %ld ms.",
suggestions_duration.InMilliseconds()));
std::optional<optimization_guide::proto::ZeroStateSuggestionsResponse>
response = optimization_guide::ParsedAnyMetadata<
optimization_guide::proto::ZeroStateSuggestionsResponse>(
result.response.value());
if (!response) {
OPTIMIZATION_GUIDE_LOG(
optimization_guide_common::mojom::LogSource::MODEL_EXECUTION,
optimization_guide_keyed_service_->GetOptimizationGuideLogger(),
"ZeroStateSuggestionsRequest: No response available.");
pending_callbacks_.Notify(std::vector<std::string>({}));
// Treat this as a transient error that server returned bad data
// momentarily. Do not cache.
return;
}
std::vector<std::string> suggestions;
for (int i = 0; i < response->suggestions_size(); ++i) {
suggestions.push_back(response->suggestions(i).label());
OPTIMIZATION_GUIDE_LOG(
optimization_guide_common::mojom::LogSource::MODEL_EXECUTION,
optimization_guide_keyed_service_->GetOptimizationGuideLogger(),
base::StringPrintf("ZeroStateSuggestionsRequest: Suggestion %d: %s",
i + 1, response->suggestions(i).label()));
}
CacheFocusedTabSuggestions(suggestions);
pending_callbacks_.Notify(suggestions);
}
void ZeroStateSuggestionsRequest::CacheFocusedTabSuggestions(
const std::vector<std::string>& suggestions_to_cache) {
if (!focused_tab_page_data_) {
return;
}
focused_tab_page_data_->set_cached_suggestions_for_focused_tab(
suggestions_to_cache);
}
} // namespace contextual_cueing
|