Files
tubestation/browser/components/genai/chat.js
Ed Lee 816c5347ed Bug 1900940 - Render chat sidebar content for configured provider r=tarek
Support rendering providers but keep list empty for now. Show selected provider including custom and render content browser.

Differential Revision: https://phabricator.services.mozilla.com/D212836
2024-06-15 17:32:54 +00:00

98 lines
2.4 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/. */
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
GenAI: "resource:///modules/GenAI.sys.mjs",
});
const { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"providerPref",
"browser.ml.chat.provider",
null,
renderProviders
);
const node = {};
function request(url = lazy.providerPref) {
try {
node.chat.fixupAndLoadURIString(url, {
triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal(
{}
),
});
} catch (ex) {
console.error("Failed to request chat provider", ex);
}
}
function renderChat() {
const browser = document.createXULElement("browser");
browser.setAttribute("type", "content");
browser.setAttribute("remote", "true");
return document.body.appendChild(browser);
}
async function renderProviders() {
// Skip potential pref change callback when unloading
if ((await document.visibilityState) == "hidden") {
return null;
}
const select = document.getElementById("provider");
select.innerHTML = "";
let selected = false;
const addOption = (text, val) => {
const option = select.appendChild(document.createElement("option"));
option.textContent = text;
option.value = val;
return option;
};
// Add the known providers in order while looking for current selection
lazy.GenAI.chatProviders.forEach((data, url) => {
const option = addOption(data.name, url);
if (lazy.providerPref == url) {
option.selected = true;
selected = true;
}
});
// Must be a custom preference if provider wasn't found
if (!selected) {
const option = addOption(
`Custom provider (${lazy.providerPref})`,
lazy.providerPref
);
option.selected = true;
}
// Load the requested provider
request();
return select;
}
function handleChange({ target }) {
const { value } = target;
switch (target) {
case node.provider:
Services.prefs.setStringPref("browser.ml.chat.provider", value);
break;
}
}
async function handleLoad() {
node.chat = renderChat();
node.provider = await renderProviders();
}
addEventListener("change", handleChange);
addEventListener("load", handleLoad);