tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

OnDeviceModelManager.mjs (2858B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 /**
      6 * @import { ModelHub } from  "chrome://global/content/ml/ModelHub.sys.mjs"
      7 */
      8 
      9 /**
     10 * Helpers for managing the install and uninstall of on-device AI models. This
     11 * could be a .sys.mjs module if that makes more sense, but any feature-specific
     12 * helpers could be imported here too.
     13 */
     14 
     15 const XPCOMUtils = ChromeUtils.importESModule(
     16  "resource://gre/modules/XPCOMUtils.sys.mjs"
     17 ).XPCOMUtils;
     18 const lazy = XPCOMUtils.declareLazy({
     19  ModelHub: "chrome://global/content/ml/ModelHub.sys.mjs",
     20  log: () =>
     21    console.createInstance({
     22      prefix: "OnDeviceModelManager",
     23      maxLogLevel: "Info",
     24    }),
     25 });
     26 
     27 /** @typedef {typeof OnDeviceModelFeatures[keyof typeof OnDeviceModelFeatures]} OnDeviceModelFeaturesEnum */
     28 
     29 /**
     30 * Features that support on-device AI models.
     31 */
     32 const OnDeviceModelFeatures = Object.freeze({
     33  // NOTE: Feel free to change the values here to whatever makes sense.
     34  TabGroups: "tabgroups",
     35  KeyPoints: "keypoints",
     36  PdfAltText: "pdfalttext",
     37 });
     38 
     39 export const OnDeviceModelManager = {
     40  features: OnDeviceModelFeatures,
     41 
     42  /** @type {ModelHub} */
     43  _modelHub: null,
     44  get modelHub() {
     45    if (!this._modelHub) {
     46      this._modelHub = new lazy.ModelHub();
     47    }
     48    return this._modelHub;
     49  },
     50 
     51  /**
     52   * Install the models for a specific feature. This should be used when a user
     53   * explicitly enables a feature, so it's ready when they go to use it.
     54   *
     55   * @param {OnDeviceModelFeaturesEnum} feature The feature key to install.
     56   */
     57  async install(feature) {
     58    switch (feature) {
     59      case OnDeviceModelFeatures.TabGroups:
     60        lazy.log.info("install TabGroups");
     61        return;
     62      case OnDeviceModelFeatures.KeyPoints:
     63        lazy.log.info("install KeyPoints");
     64        return;
     65      case OnDeviceModelFeatures.PdfAltText:
     66        lazy.log.info("install PdfAltText");
     67        return;
     68      default:
     69        throw new Error(`Unknown on-device model feature "${feature}"`);
     70    }
     71  },
     72 
     73  /**
     74   * Uninstall the models for a specific feature.
     75   *
     76   * @param {OnDeviceModelFeaturesEnum} feature The feature key to uninstall.
     77   */
     78  async uninstall(feature) {
     79    // TODO: Maybe something like this?
     80    // this.modelHub.deleteFilesByEngine(feature);
     81    switch (feature) {
     82      case OnDeviceModelFeatures.TabGroups:
     83        lazy.log.info("uninstall TabGroups");
     84        return;
     85      case OnDeviceModelFeatures.KeyPoints:
     86        lazy.log.info("uninstall KeyPoints");
     87        return;
     88      case OnDeviceModelFeatures.PdfAltText:
     89        lazy.log.info("uninstall PdfAltText");
     90        return;
     91      default:
     92        throw new Error(`Unknown on-device model feature "${feature}"`);
     93    }
     94  },
     95 };