/* * ThunderAI [https://micz.it/thunderbird-addon-thunderai/] * Copyright (C) 2024 - 2025 Mic (m@micz.it) * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ // Call the API to use a special prompt import { prefs_default } from "../options/mzta-options-default.js"; import { taLogger } from './mzta-logger.js'; export class mzta_specialCommand { prompt = ""; worker = null; llm = ""; custom_model = ""; full_message = ""; logger = null; do_debug = false; config = {}; constructor(args = {}) { let { prompt = '', llm = '', custom_model = '', do_debug = false, config = {} } = args; this.prompt = prompt; this.llm = llm; this.custom_model = custom_model; this.config = config; this.logger = new taLogger('mzta_specialCommand', do_debug); this.do_debug = do_debug; switch (this.llm) { case "chatgpt_api": this.worker = new Worker(new URL('./workers/model-worker-openai_responses.js', import.meta.url), { type: 'module' }); break; case "google_gemini_api": this.worker = new Worker(new URL('./workers/model-worker-google_gemini.js', import.meta.url), { type: 'module' }); break; case "ollama_api": this.worker = new Worker(new URL('./workers/model-worker-ollama.js', import.meta.url), { type: 'module' }); break; case "openai_comp_api": this.worker = new Worker(new URL('./workers/model-worker-openai_comp.js', import.meta.url), { type: 'module' }); break; case "anthropic_api": this.worker = new Worker(new URL('./workers/model-worker-anthropic.js', import.meta.url), { type: 'module' }); break; default: this.logger.log("Invalid LLM type: " + this.llm); throw new Error("Invalid LLM type: " + this.llm); } } async initWorker() { // console.log((">>>>>>>>>>>> this.custom_model: " + this.custom_model)); switch (this.llm) { case "chatgpt_api": { let prefs_api = await browser.storage.sync.get({ chatgpt_api_key: prefs_default.chatgpt_api_key, chatgpt_model: prefs_default.chatgpt_model, chatgpt_developer_messages: prefs_default.chatgpt_developer_messages, }); if (this.config.chatgpt_api_key) prefs_api.chatgpt_api_key = this.config.chatgpt_api_key; if (this.config.chatgpt_model) prefs_api.chatgpt_model = this.config.chatgpt_model; if (this.config.chatgpt_developer_messages) prefs_api.chatgpt_developer_messages = this.config.chatgpt_developer_messages; this.worker.postMessage({ type: 'init', chatgpt_api_key: prefs_api.chatgpt_api_key, chatgpt_model: this.custom_model != '' ? this.custom_model : prefs_api.chatgpt_model, chatgpt_developer_messages: prefs_api.chatgpt_developer_messages, do_debug: this.do_debug, i18nStrings: '' }); break; } case "google_gemini_api": { let prefs_api = await browser.storage.sync.get({ google_gemini_api_key: prefs_default.google_gemini_api_key, google_gemini_model: prefs_default.google_gemini_model, google_gemini_system_instruction: prefs_default.google_gemini_system_instruction, google_gemini_thinking_budget: prefs_default.google_gemini_thinking_budget, }); if (this.config.google_gemini_api_key) prefs_api.google_gemini_api_key = this.config.google_gemini_api_key; if (this.config.google_gemini_model) prefs_api.google_gemini_model = this.config.google_gemini_model; if (this.config.google_gemini_system_instruction) prefs_api.google_gemini_system_instruction = this.config.google_gemini_system_instruction; if (this.config.google_gemini_thinking_budget) prefs_api.google_gemini_thinking_budget = this.config.google_gemini_thinking_budget; this.worker.postMessage({ type: 'init', google_gemini_api_key: prefs_api.google_gemini_api_key, google_gemini_model: this.custom_model != '' ? this.custom_model : prefs_api.google_gemini_model, google_gemini_system_instruction: prefs_api.google_gemini_system_instruction, google_gemini_thinking_budget: prefs_api.google_gemini_thinking_budget, do_debug: this.do_debug, i18nStrings: '' }); break; } case "ollama_api": { let prefs_api = await browser.storage.sync.get({ ollama_host: prefs_default.ollama_host, ollama_model: prefs_default.ollama_model, }); if (this.config.ollama_host) prefs_api.ollama_host = this.config.ollama_host; if (this.config.ollama_model) prefs_api.ollama_model = this.config.ollama_model; this.worker.postMessage({ type: 'init', ollama_host: prefs_api.ollama_host, ollama_model: this.custom_model != '' ? this.custom_model : prefs_api.ollama_model, do_debug: this.do_debug, i18nStrings: '' }); break; } case "openai_comp_api": { let prefs_api = await browser.storage.sync.get({ openai_comp_host: prefs_default.openai_comp_host, openai_comp_model: prefs_default.openai_comp_model, openai_comp_api_key: prefs_default.openai_comp_api_key, openai_comp_use_v1: prefs_default.openai_comp_use_v1, openai_comp_chat_name: prefs_default.openai_comp_chat_name, do_debug: prefs_default.do_debug, }); if (this.config.openai_comp_host) prefs_api.openai_comp_host = this.config.openai_comp_host; if (this.config.openai_comp_model) prefs_api.openai_comp_model = this.config.openai_comp_model; if (this.config.openai_comp_api_key) prefs_api.openai_comp_api_key = this.config.openai_comp_api_key; if (this.config.openai_comp_use_v1 !== undefined) prefs_api.openai_comp_use_v1 = this.config.openai_comp_use_v1; if (this.config.openai_comp_chat_name) prefs_api.openai_comp_chat_name = this.config.openai_comp_chat_name; this.worker.postMessage({ type: 'init', openai_comp_host: prefs_api.openai_comp_host, openai_comp_model: this.custom_model != '' ? this.custom_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: this.do_debug, i18nStrings: '' }); break; } case "anthropic_api": { let prefs_api = await browser.storage.sync.get({ anthropic_api_key: prefs_default.anthropic_api_key, anthropic_model: prefs_default.anthropic_model, anthropic_version: prefs_default.anthropic_version, anthropic_max_tokens: prefs_default.anthropic_max_tokens, }); if (this.config.anthropic_api_key) prefs_api.anthropic_api_key = this.config.anthropic_api_key; if (this.config.anthropic_model) prefs_api.anthropic_model = this.config.anthropic_model; if (this.config.anthropic_version) prefs_api.anthropic_version = this.config.anthropic_version; if (this.config.anthropic_max_tokens) prefs_api.anthropic_max_tokens = this.config.anthropic_max_tokens; this.worker.postMessage({ type: 'init', anthropic_api_key: prefs_api.anthropic_api_key, anthropic_model: this.custom_model != '' ? this.custom_model : prefs_api.anthropic_model, anthropic_version: prefs_api.anthropic_version, anthropic_max_tokens: prefs_api.anthropic_max_tokens, do_debug: this.do_debug, i18nStrings: '' }); break; } } } sendPrompt(){ return new Promise((resolve, reject) => { // Event listeners for worker messages this.worker.onmessage = (event) => { const { type, payload } = event.data; // console.log(`>>>>>>>>>>>> [Worker Message Received] type: ${type}`, payload); switch (type) { case 'messageSent': break; case 'newToken': this.full_message += payload.token; break; case 'tokensDone': this.logger.log("tokensDone: " + this.full_message); resolve(this.full_message); // Resolve the promise with the full message break; case 'error': console.error('[ThunderAI] Error from API worker:', payload); reject(new Error(`[ThunderAI] Error from API worker: ${payload}`)); // Use a single error object break; default: console.error('[ThunderAI] Unknown event type from API worker:', type); } }; this.worker.onerror = (error) => { console.error('[ThunderAI] Worker Error Details:'); console.error('Message:', error.message); console.error('Filename:', error.filename); console.error('Line Number:', error.lineno); console.error('Column Number:', error.colno); console.error('Event:', error); reject(error); }; try { // console.log('[ThunderAI] Sending prompt to worker:', this.prompt); this.worker.postMessage({ type: 'chatMessage', message: this.prompt }); } catch (error) { console.error('[ThunderAI] Failed to send message to worker:', error); reject(error); } }); } }