This adds a new Suggest backend for ML-based suggestions called `SuggestBackendMl`. Before, with the JS and Rust backends, only one backend was enabled at a time, but both the ML and Rust backends can be enabled at the same time since we will want to serve suggestions from both for the foreseeable future. Features can support ML suggestions by implementing the new `BaseFeature.mlIntent` getter and handling ML suggestions in `makeResult()`. Each feature can decide whether it supports ML suggestions and whether they should be preferred over Rust suggestions. I've updated the Yelp feature to hook into this, since Yelp suggestions are supported by the ML model that Chidam is working on. If ML is enabled, then the feature will only serve ML suggestions. I'm not sure if that's what we want long term, but for now that will make it clear to people which backend is being used while we develop this feature. The `quickSuggestMlEnabled` variable/pref determines whether the ML backend is enabled. The `yelpMlEnabled` variable/pref determines whether Yelp ML suggestions are enabled. We can create similar variable/prefs for each feature that supports ML suggestions so that they can be toggled independently of each other. Other changes: Move the `is_sponsored` logic out of the Rust backend and into the provider. Otherwise it would need to be duplicated in the ML backend too. Depends on D224523 Differential Revision: https://phabricator.services.mozilla.com/D226736
66 lines
1.9 KiB
JavaScript
66 lines
1.9 KiB
JavaScript
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
import { BaseFeature } from "resource:///modules/urlbar/private/BaseFeature.sys.mjs";
|
|
|
|
const lazy = {};
|
|
|
|
ChromeUtils.defineESModuleGetters(lazy, {
|
|
MLSuggest: "resource:///modules/urlbar/private/MLSuggest.sys.mjs",
|
|
UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
|
|
});
|
|
|
|
/**
|
|
* The Suggest ML backend. Both the ML and Rust backends can be enabled at the
|
|
* same time. Features can support both backends and decide which one to use per
|
|
* query.
|
|
*/
|
|
export class SuggestBackendMl extends BaseFeature {
|
|
get shouldEnable() {
|
|
return (
|
|
lazy.UrlbarPrefs.get("quickSuggestMlEnabled") &&
|
|
lazy.UrlbarPrefs.get("browser.ml.enable")
|
|
);
|
|
}
|
|
|
|
get enablingPreferences() {
|
|
return ["browser.ml.enable"];
|
|
}
|
|
|
|
async enable(enabled) {
|
|
if (enabled) {
|
|
this.logger.debug("Initializing MLSuggest...");
|
|
await lazy.MLSuggest.initialize();
|
|
this.logger.debug("MLSuggest is now initialized");
|
|
} else {
|
|
this.logger.debug("Shutting down MLSuggest...");
|
|
await lazy.MLSuggest.shutdown();
|
|
this.logger.debug("MLSuggest is now shut down");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Queries `MLSuggest` and returns the matching suggestion.
|
|
*
|
|
* @param {string} searchString
|
|
* The search string.
|
|
* @returns {Array}
|
|
* An array of matching suggestions. `MLSuggest` returns at most one
|
|
* suggestion.
|
|
*/
|
|
async query(searchString) {
|
|
this.logger.debug("Handling query: " + JSON.stringify(searchString));
|
|
|
|
let suggestion = await lazy.MLSuggest.makeSuggestions(searchString);
|
|
this.logger.debug("Got suggestion: " + JSON.stringify(suggestion, null, 2));
|
|
|
|
if (suggestion) {
|
|
suggestion.source = "ml";
|
|
suggestion.provider = suggestion.intent;
|
|
return [suggestion];
|
|
}
|
|
return [];
|
|
}
|
|
}
|