aiFeatures.mjs (6615B)
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 import { Preferences } from "chrome://global/content/preferences/Preferences.mjs"; 6 import { SettingGroupManager } from "chrome://browser/content/preferences/config/SettingGroupManager.mjs"; 7 import { OnDeviceModelManager } from "chrome://browser/content/preferences/OnDeviceModelManager.mjs"; 8 9 /** 10 * @import { OnDeviceModelFeaturesEnum } from "chrome://browser/content/preferences/OnDeviceModelManager.mjs" 11 */ 12 13 const XPCOMUtils = ChromeUtils.importESModule( 14 "resource://gre/modules/XPCOMUtils.sys.mjs" 15 ).XPCOMUtils; 16 const lazy = XPCOMUtils.declareLazy({ 17 GenAI: "resource:///modules/GenAI.sys.mjs", 18 log: () => 19 console.createInstance({ 20 prefix: "aiFeatures", 21 maxLogLevel: "Info", 22 }), 23 }); 24 25 Preferences.addAll([ 26 { id: "browser.ml.chat.provider", type: "string" }, 27 { id: "browser.aiwindow.enabled", type: "bool" }, 28 { id: "browser.aiwindow.preferences.enabled", type: "bool" }, 29 ]); 30 31 Preferences.addSetting({ id: "chatbotProviderItem" }); 32 Preferences.addSetting({ 33 id: "chatbotProvider", 34 pref: "browser.ml.chat.provider", 35 setup() { 36 lazy.GenAI.init(); 37 }, 38 getControlConfig(config, _, setting) { 39 let providerUrl = setting.value; 40 let isKnownProvider = providerUrl == ""; 41 let options = [config.options[0]]; 42 lazy.GenAI.chatProviders.forEach((provider, url) => { 43 let isSelected = url == providerUrl; 44 // @ts-expect-error provider.hidden isn't in the typing 45 if (!isSelected && provider.hidden) { 46 return; 47 } 48 isKnownProvider = isKnownProvider || isSelected; 49 options.push({ 50 value: url, 51 controlAttrs: { label: provider.name }, 52 }); 53 }); 54 if (!isKnownProvider) { 55 options.push({ 56 value: providerUrl, 57 controlAttrs: { label: providerUrl }, 58 }); 59 } 60 return { 61 ...config, 62 options, 63 }; 64 }, 65 }); 66 Preferences.addSetting( 67 /** @type {{ selected: string } & SettingConfig} */ ({ 68 id: "onDeviceModel", 69 selected: Object.values(OnDeviceModelManager.features)[0], 70 getControlConfig(config) { 71 if (!config.options) { 72 config.options = Object.entries(OnDeviceModelManager.features).map( 73 ([key, value]) => ({ 74 value, 75 controlAttrs: { label: key }, 76 }) 77 ); 78 } 79 return config; 80 }, 81 get() { 82 return this.selected; 83 }, 84 set(val) { 85 this.selected = String(val); 86 }, 87 }) 88 ); 89 Preferences.addSetting({ 90 id: "onDeviceModelInstall", 91 deps: ["onDeviceModel"], 92 async onUserClick(_, deps) { 93 let feature = /** @type {OnDeviceModelFeaturesEnum} */ ( 94 deps.onDeviceModel.value 95 ); 96 lazy.log.info("Will install: ", feature); 97 await OnDeviceModelManager.install(feature); 98 lazy.log.info("Done install: ", feature); 99 }, 100 }); 101 Preferences.addSetting({ 102 id: "onDeviceModelUninstall", 103 deps: ["onDeviceModel"], 104 async onUserClick(_, deps) { 105 let feature = /** @type {OnDeviceModelFeaturesEnum} */ ( 106 deps.onDeviceModel.value 107 ); 108 lazy.log.info("Will uninstall: ", feature); 109 await OnDeviceModelManager.uninstall(feature); 110 lazy.log.info("Done uninstall: ", feature); 111 }, 112 }); 113 Preferences.addSetting({ 114 id: "onDeviceModelUninstallAll", 115 async onUserClick() { 116 lazy.log.info("Will uninstall: ALL"); 117 await Promise.all( 118 Object.values(OnDeviceModelManager.features).map(feature => 119 OnDeviceModelManager.uninstall(feature) 120 ) 121 ); 122 lazy.log.info("Done uninstall: ALL"); 123 }, 124 }); 125 126 Preferences.addSetting({ 127 id: "AIWindowEnabled", 128 pref: "browser.aiwindow.enabled", 129 }); 130 131 Preferences.addSetting({ 132 id: "AIWindowPreferencesEnabled", 133 pref: "browser.aiwindow.preferences.enabled", 134 }); 135 136 // Only show the feature settings if the prefs are allowed to show and the 137 // feature isn't enabled. 138 Preferences.addSetting({ 139 id: "AIWindowItem", 140 deps: ["AIWindowEnabled", "AIWindowPreferencesEnabled"], 141 visible: deps => { 142 return deps.AIWindowPreferencesEnabled.value && !deps.AIWindowEnabled.value; 143 }, 144 }); 145 Preferences.addSetting({ id: "AIWindowHeader" }); 146 Preferences.addSetting({ id: "AIWindowActivateLink" }); 147 148 // Only show the AI Window features if the prefs are allowed to show and the 149 // feature is enabled. 150 // TODO: Enable when Model and Insight options are added 151 Preferences.addSetting({ 152 id: "aiFeaturesAIWindowGroup", 153 deps: ["AIWindowEnabled", "AIWindowPreferencesEnabled"], 154 visible: deps => { 155 return deps.AIWindowPreferencesEnabled.value && deps.AIWindowEnabled.value; 156 }, 157 }); 158 159 SettingGroupManager.registerGroups({ 160 debugModelManagement: { 161 l10nId: "debug-model-management-group", 162 items: [ 163 { 164 id: "onDeviceModel", 165 control: "moz-select", 166 l10nId: "debug-model-management-feature", 167 }, 168 { 169 id: "onDeviceModelInstall", 170 control: "moz-button", 171 l10nId: "debug-model-management-install", 172 }, 173 { 174 id: "onDeviceModelUninstall", 175 control: "moz-button", 176 l10nId: "debug-model-management-uninstall", 177 }, 178 { 179 id: "onDeviceModelUninstallAll", 180 control: "moz-button", 181 l10nId: "debug-model-management-uninstall-all", 182 }, 183 ], 184 }, 185 aiFeatures: { 186 l10nId: "try-ai-features-group", 187 items: [ 188 { 189 id: "chatbotProviderItem", 190 control: "moz-box-item", 191 items: [ 192 { 193 id: "chatbotProvider", 194 l10nId: "try-ai-features-chatbot-provider", 195 control: "moz-select", 196 supportPage: "ai-chatbot", 197 options: [ 198 { 199 l10nId: "try-ai-features-chatbot-choose-label", 200 value: "", 201 }, 202 ], 203 }, 204 ], 205 }, 206 { 207 id: "AIWindowItem", 208 control: "moz-box-group", 209 items: [ 210 { 211 id: "AIWindowHeader", 212 l10nId: "try-ai-features-ai-window", 213 control: "moz-box-item", 214 }, 215 { 216 id: "AIWindowActivateLink", 217 l10nId: "try-ai-features-ai-window-activate-link", 218 control: "moz-box-link", 219 }, 220 ], 221 }, 222 ], 223 }, 224 aiWindowFeatures: { 225 l10nId: "ai-window-features-group", 226 headingLevel: 2, 227 items: [ 228 { 229 id: "aiFeaturesAIWindowGroup", 230 control: "moz-box-group", 231 // TODO: Add Model and Insight list 232 // options: [ 233 // ], 234 }, 235 ], 236 }, 237 });