From 95f6c0e40fc7c2929c3ea6242af2d91e3182eb80 Mon Sep 17 00:00:00 2001 From: mic Date: Wed, 21 May 2025 21:49:33 +0200 Subject: [PATCH] Add API connection messages and refactor initialization logging --- _locales/en/messages.json | 58 +++++++++++++++++++++++---------------- api_webchat/controller.js | 11 ++++---- js/mzta-utils.js | 16 +++++++++++ 3 files changed, 57 insertions(+), 28 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index d5e2bf56..03cc3b0b 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -451,10 +451,6 @@ "message": "Using model", "description": "" }, - "chagpt_api_connecting": { - "message": "Attempting to connect to OpenAI ChatGPT using the API key provided", - "description": "" - }, "Debug": { "message": "Debug", "description": "" @@ -499,14 +495,6 @@ "message": "You've not choosen a model for the Ollama API. Please choose one in the options page.", "description": "" }, - "ollama_api_connecting": { - "message": "Attempting to connect to the Ollama Local Server using the host", - "description": "" - }, - "andModel": { - "message": "and model", - "description": "" - }, "error_connection_interrupted": { "message": "The connection to the server was unexpectedly interrupted", "description": "" @@ -547,10 +535,6 @@ "message": "You've not choosen a model for the OpenAI Compatible API. Please choose one in the options page.", "description": "" }, - "OpenAIComp_api_connecting": { - "message": "Attempting to connect to the OpenAI Compatible API Local Server using the host", - "description": "" - }, "OpenAIComp_api_request_failed": { "message": "OpenAI Comp API request failed", "description": "" @@ -955,10 +939,6 @@ "message": "Google Gemini API request failed", "description": "" }, - "google_gemini_api_connecting": { - "message": "Attempting to connect to Google Gemini using the API key provided", - "description": "" - }, "google_gemini_empty_apikey": { "message": "You've not added an API Key for the Google Gemini API. Please insert one in the options page.", "description": "" @@ -1491,8 +1471,40 @@ "message": "Anthropic API request failed", "description": "" }, - "anthropic_api_connecting": { - "message": "Attempting to connect to Anthropic API using the API key provided", - "description": "" + "_api_connecting": { + "message": "Attempting to connect to $api_string$ using the following configuration:", + "description": "", + "placeholders": { + "api_string": { + "content": "$1" + } + } + }, + "_api_connecting_model": { + "message": "Model: $model$", + "description": "", + "placeholders": { + "model": { + "content": "$1" + } + } + }, + "_api_connecting_host": { + "message": "Host: $api_host$", + "description": "", + "placeholders": { + "api_host": { + "content": "$1" + } + } + }, + "_api_connecting_version": { + "message": "Version: $api_version$", + "description": "", + "placeholders": { + "api_version": { + "content": "$1" + } + } } } \ No newline at end of file diff --git a/api_webchat/controller.js b/api_webchat/controller.js index 04b0478b..18b1bd48 100644 --- a/api_webchat/controller.js +++ b/api_webchat/controller.js @@ -21,6 +21,7 @@ */ import { placeholdersUtils } from '../js/mzta-placeholders.js'; +import { getAPIsInitMessageString } from '../js/mzta-utils.js'; // Get the LLM to be used const urlParams = new URLSearchParams(window.location.search); @@ -79,7 +80,7 @@ switch (llm) { messageInput.setModel(prefs_api.chatgpt_model); messagesArea.setLLMName("ChatGPT"); worker.postMessage({ type: 'init', chatgpt_api_key: prefs_api.chatgpt_api_key, chatgpt_model: prefs_api.chatgpt_model, chatgpt_developer_messages: prefs_api.chatgpt_developer_messages, do_debug: prefs_api.do_debug, i18nStrings: i18nStrings}); - messagesArea.appendUserMessage(browser.i18n.getMessage("chagpt_api_connecting") + " " +browser.i18n.getMessage("AndModel") + " \"" + prefs_api.chatgpt_model + "\"...", "info"); + messagesArea.appendUserMessage(getAPIsInitMessageString("ChatGPT API", prefs_api.chatgpt_model), "info"); browser.runtime.sendMessage({command: "openai_api_ready_" + call_id, window_id: (await browser.windows.getCurrent()).id}); break; } @@ -91,7 +92,7 @@ switch (llm) { messageInput.setModel(prefs_api.google_gemini_model); messagesArea.setLLMName("Google Gemini"); worker.postMessage({ type: 'init', google_gemini_api_key: prefs_api.google_gemini_api_key, google_gemini_model: prefs_api.google_gemini_model, google_gemini_system_instruction: prefs_api.google_gemini_system_instruction, do_debug: prefs_api.do_debug, i18nStrings: i18nStrings}); - messagesArea.appendUserMessage(browser.i18n.getMessage("google_gemini_api_connecting") + " " +browser.i18n.getMessage("AndModel") + " \"" + prefs_api.google_gemini_model + "\"...", "info"); + messagesArea.appendUserMessage(getAPIsInitMessageString("Google Gemini API", prefs_api.google_gemini_model), "info"); browser.runtime.sendMessage({command: "google_gemini_api_ready_" + call_id, window_id: (await browser.windows.getCurrent()).id}); break; } @@ -104,7 +105,7 @@ switch (llm) { messagesArea.setLLMName("Ollama Local"); worker.postMessage({ type: 'init', ollama_host: prefs_api.ollama_host, ollama_model: prefs_api.ollama_model, ollama_num_ctx: prefs_api.ollama_num_ctx, do_debug: prefs_api.do_debug, i18nStrings: i18nStrings}); browser.runtime.sendMessage({command: "ollama_api_ready_" + call_id, window_id: (await browser.windows.getCurrent()).id}); - messagesArea.appendUserMessage(browser.i18n.getMessage("ollama_api_connecting") + " \"" + prefs_api.ollama_host + "\" " +browser.i18n.getMessage("AndModel") + " \"" + prefs_api.ollama_model + "\"...", "info"); + messagesArea.appendUserMessage(getAPIsInitMessageString("Ollama API", prefs_api.ollama_model, prefs_api.ollama_host), "info"); break; } case "openai_comp_api": { @@ -115,7 +116,7 @@ switch (llm) { messageInput.setModel(prefs_api.openai_comp_model); messagesArea.setLLMName(prefs_api.openai_comp_chat_name); worker.postMessage({ type: 'init', openai_comp_host: prefs_api.openai_comp_host, openai_comp_model: prefs_api.openai_comp_model, openai_comp_api_key: prefs_api.openai_comp_api_key, openai_comp_use_v1: prefs_api.openai_comp_use_v1, do_debug: prefs_api.do_debug, i18nStrings: i18nStrings}); - messagesArea.appendUserMessage(browser.i18n.getMessage("OpenAIComp_api_connecting") + " \"" + prefs_api.openai_comp_host + "\" " +browser.i18n.getMessage("AndModel") + " \"" + prefs_api.openai_comp_model + "\"...", "info"); + messagesArea.appendUserMessage(getAPIsInitMessageString("OpenAI Compatible API", prefs_api.openai_comp_model, prefs_api.openai_comp_host), "info"); browser.runtime.sendMessage({command: "openai_comp_api_ready_" + call_id, window_id: (await browser.windows.getCurrent()).id}); break; } @@ -127,7 +128,7 @@ switch (llm) { messageInput.setModel(prefs_api.anthropic_model); messagesArea.setLLMName("Anthropic"); worker.postMessage({ type: 'init', anthropic_api_key: prefs_api.anthropic_api_key, anthropic_model: prefs_api.anthropic_model, anthropic_version: prefs_api.anthropic_version, anthropic_max_tokens: prefs_api.anthropic_max_tokens, do_debug: prefs_api.do_debug, i18nStrings: i18nStrings}); - messagesArea.appendUserMessage(browser.i18n.getMessage("anthropic_api_connecting") + " " +browser.i18n.getMessage("AndModel") + " \"" + prefs_api.anthropic_model + "\"...", "info"); + messagesArea.appendUserMessage(getAPIsInitMessageString("Anthropic API", prefs_api.anthropic_model, '', prefs_api.anthropic_version), "info"); browser.runtime.sendMessage({command: "anthropic_api_ready_" + call_id, window_id: (await browser.windows.getCurrent()).id}); break; } diff --git a/js/mzta-utils.js b/js/mzta-utils.js index 46adcad2..12904797 100644 --- a/js/mzta-utils.js +++ b/js/mzta-utils.js @@ -401,6 +401,21 @@ export async function transformTagsLabels(labels, tags_list) { return output; } +export function getAPIsInitMessageString(api_string, model_string = '', host_string = '', version_string = '') { + let output = browser.i18n.getMessage("_api_connecting", api_string); + if (model_string !== '') { + output += "\n" + browser.i18n.getMessage("_api_connecting_model", model_string); + } + if (host_string !== '') { + output += "\n" + browser.i18n.getMessage("_api_connecting_host", host_string); + } + if (version_string !== '') { + output += "\n" + browser.i18n.getMessage("_api_connecting_version", version_string); + } + + return output; +} + export function getActiveSpecialPromptsIDs(args = {}) { const { addtags = false, get_calendar_event = false, get_task = false, is_chatgpt_web = false } = args; let output = []; @@ -475,6 +490,7 @@ export function validateCustomData_ChatGPTWeb(event) { document.getElementById(event.target.id + '_info').style.color = is_valid ? '' : 'red'; } + // The following methods are a modified version derived from https://github.com/ali-raheem/Aify/blob/13ff87583bc520fb80f555ab90a90c5c9df797a7/plugin/content_scripts/compose.js const insertHtml = function (replyHtml, fullBody_string) {