diff --git a/js/mzta-special-commands.js b/js/mzta-special-commands.js index da63271c..95a0d14f 100644 --- a/js/mzta-special-commands.js +++ b/js/mzta-special-commands.js @@ -17,7 +17,10 @@ */ // Call the API to use a special prompt - import { prefs_default } from "../options/mzta-options-default.js"; + import { + prefs_default, + integration_options_config + } from "../options/mzta-options-default.js"; import { taLogger } from './mzta-logger.js'; export class mzta_specialCommand { @@ -45,146 +48,68 @@ 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); + + const worker_path_map = { + chatgpt_api: './workers/model-worker-openai_responses.js', + google_gemini_api: './workers/model-worker-google_gemini.js', + ollama_api: './workers/model-worker-ollama.js', + openai_comp_api: './workers/model-worker-openai_comp.js', + anthropic_api: './workers/model-worker-anthropic.js', + }; + + const worker_path = worker_path_map[this.llm]; + if (worker_path) { + this.worker = new Worker(new URL(worker_path, import.meta.url), { type: 'module' }); + } else { + 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, - }); + const integration = this.llm.replace('_api', ''); + const options_config = integration_options_config[integration]; - 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; + if (!options_config) { + this.logger.error("Invalid integration type: " + integration); + throw new Error("Invalid integration type: " + integration); + } - 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; + let prefsToGet = {}; + for (const key in options_config) { + const prefKey = `${integration}_${key}`; + prefsToGet[prefKey] = prefs_default[prefKey]; + } + + let prefs_api = await browser.storage.sync.get(prefsToGet); + + let workerInitMessage = { + type: 'init', + do_debug: this.do_debug, + i18nStrings: '' + }; + + for (const key in options_config) { + const prefKey = `${integration}_${key}`; + + const configValue = this.config[prefKey]; + + if (configValue !== undefined) { + if (typeof options_config[key] === 'boolean') { + prefs_api[prefKey] = (configValue === true || configValue === 'true' || configValue === 1); + } else { + prefs_api[prefKey] = configValue; + } } - 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; + + if (key === 'model') { + workerInitMessage[prefKey] = this.custom_model !== '' ? this.custom_model : prefs_api[prefKey]; + } else { + workerInitMessage[prefKey] = prefs_api[prefKey]; } } + + this.worker.postMessage(workerInitMessage); } sendPrompt(){