patch all API calls: translate and intro generation now use custom API when configured

This commit is contained in:
Russell Ballestrini 2025-07-10 23:50:20 -04:00
parent 4259e6774c
commit 83c6ac9a2c
2 changed files with 48 additions and 18 deletions

View file

@ -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,

View file

@ -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,