From 83c6ac9a2c326fbd0506889a8702e3013d7a7b76 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Thu, 10 Jul 2025 23:50:20 -0400 Subject: [PATCH] patch all API calls: translate and intro generation now use custom API when configured --- src/translation.js | 31 +++++++++++++++++++++++-------- src/ui.js | 35 +++++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/translation.js b/src/translation.js index bf08360..d4624ca 100644 --- a/src/translation.js +++ b/src/translation.js @@ -1,4 +1,4 @@ -import { API_KEY } from "./config.js"; +import { API_KEY, getAPIConfig } from "./config.js"; import { getSelectedModel, getSelectedModelEndpoint } from "./models.js"; import { countTokens, countTokensWithUpstream } from "./token_estimator.js"; @@ -50,19 +50,34 @@ export const NATIVE_LANGUAGE_NAMES = { // Send message with custom history (for translation with minimal system prompt) async function* sendMessageWithHistory(messageHistory) { - const apiUrl = `${getSelectedModelEndpoint()}/chat/completions`; + // Get API configuration (custom or default) + const apiConfig = getAPIConfig(); + + let apiUrl, headers, model; + + if (apiConfig.isCustom) { + // Use custom API configuration + apiUrl = `${apiConfig.endpoint}/chat/completions`; + headers = apiConfig.headers; + model = apiConfig.model; + } else { + // Use default Hermes configuration + apiUrl = `${getSelectedModelEndpoint()}/chat/completions`; + headers = { + "Content-Type": "application/json", + Authorization: `Bearer ${API_KEY}`, + }; + model = getSelectedModel(); + } console.log("Translation using endpoint:", apiUrl); - console.log("Translation using model:", getSelectedModel()); + console.log("Translation using model:", model); const response = await fetch(apiUrl, { method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${API_KEY}`, - }, + headers: headers, body: JSON.stringify({ - model: getSelectedModel(), + model: model, messages: messageHistory, temperature: 0.3, // Lower temperature for more consistent translation max_tokens: 70000, diff --git a/src/ui.js b/src/ui.js index b658e98..becf7a3 100644 --- a/src/ui.js +++ b/src/ui.js @@ -1,7 +1,7 @@ // UI creation and management functionality import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js"; import hljs from "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/es/highlight.min.js"; -import { API_KEY, setSystemMessageAppend, TTS_API_URL } from "./config.js"; +import { API_KEY, setSystemMessageAppend, TTS_API_URL, getAPIConfig } from "./config.js"; import { extractWebpageContent } from "./content.js"; import { detectPageLanguage } from "./language-detection.js"; import { speakText } from "./tts.js"; @@ -114,19 +114,34 @@ function addCodeBlockCopyButtons(element) { // Send message with custom history (for intro generation) async function* sendMessageWithCustomHistory(messageHistory) { - const { getSelectedModel, getSelectedModelEndpoint } = await import( - "./models.js" - ); - const apiUrl = `${getSelectedModelEndpoint()}/chat/completions`; + // Get API configuration (custom or default) + const apiConfig = getAPIConfig(); + + let apiUrl, headers, model; + + if (apiConfig.isCustom) { + // Use custom API configuration + apiUrl = `${apiConfig.endpoint}/chat/completions`; + headers = apiConfig.headers; + model = apiConfig.model; + } else { + // Use default Hermes configuration + const { getSelectedModel, getSelectedModelEndpoint } = await import( + "./models.js" + ); + apiUrl = `${getSelectedModelEndpoint()}/chat/completions`; + headers = { + "Content-Type": "application/json", + Authorization: `Bearer ${API_KEY}`, + }; + model = getSelectedModel(); + } const response = await fetch(apiUrl, { method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${API_KEY}`, - }, + headers: headers, body: JSON.stringify({ - model: getSelectedModel(), + model: model, messages: messageHistory, temperature: 0.3, max_tokens: 8192,