tor-browser

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

TitleGeneration.sys.mjs (2432B)


      1 /**
      2 * This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      5 */
      6 
      7 import {
      8  openAIEngine,
      9  renderPrompt,
     10  MODEL_FEATURES,
     11 } from "moz-src:///browser/components/aiwindow/models/Utils.sys.mjs";
     12 
     13 /**
     14 * Generate a default title from the first four words of a message.
     15 *
     16 * @param {string} message - The user's message
     17 * @returns {string} The default title
     18 */
     19 function generateDefaultTitle(message) {
     20  if (!message || typeof message !== "string") {
     21    return "New Chat";
     22  }
     23 
     24  const words = message
     25    .trim()
     26    .split(/\s+/)
     27    .filter(word => !!word.length);
     28 
     29  if (words.length === 0) {
     30    return "New Chat";
     31  }
     32 
     33  const titleWords = words.slice(0, 4);
     34  return titleWords.join(" ") + "...";
     35 }
     36 
     37 /**
     38 * Generate a chat title based on the user's message and current tab information.
     39 *
     40 * @param {string} message - The user's message
     41 * @param {object} current_tab - Object containing current tab information
     42 * @returns {Promise<string>} The generated chat title
     43 */
     44 export async function generateChatTitle(message, current_tab) {
     45  try {
     46    // Build the OpenAI engine
     47    const engine = await openAIEngine.build(MODEL_FEATURES.TITLE_GENERATION);
     48 
     49    const tabInfo = current_tab || { url: "", title: "", description: "" };
     50 
     51    // Load and render the prompt with actual values
     52    const rawPrompt = await engine.loadPrompt(MODEL_FEATURES.TITLE_GENERATION);
     53    const systemPrompt = await renderPrompt(rawPrompt, {
     54      current_tab: JSON.stringify(tabInfo),
     55    });
     56 
     57    // Prepare messages for the LLM
     58    const messages = [
     59      { role: "system", content: systemPrompt },
     60      { role: "user", content: message },
     61    ];
     62 
     63    // Get config for inference parameters if exists
     64    const config = engine.getConfig(engine.feature);
     65    const inferenceParams = config?.parameters || {};
     66 
     67    // Call the LLM
     68    const response = await engine.run({
     69      messages,
     70      fxAccountToken: await openAIEngine.getFxAccountToken(),
     71      ...inferenceParams,
     72    });
     73 
     74    // Extract the generated title from the response
     75    const title =
     76      response?.choices?.[0]?.message?.content?.trim() ||
     77      generateDefaultTitle(message);
     78 
     79    return title;
     80  } catch (error) {
     81    console.error("Failed to generate chat title:", error);
     82    return generateDefaultTitle(message);
     83  }
     84 }