diff --git a/installer/client/gui/claude.mjs b/installer/client/gui/claude.mjs deleted file mode 100644 index eeccfd0..0000000 --- a/installer/client/gui/claude.mjs +++ /dev/null @@ -1,5 +0,0 @@ -import Claude from "claude-ai"; - -export function MakeClaude(apiKey) { - return new Claude({ sessionKey: apiKey }); -} diff --git a/installer/client/gui/main.js b/installer/client/gui/main.js index d0e271d..bc3ad73 100644 --- a/installer/client/gui/main.js +++ b/installer/client/gui/main.js @@ -146,13 +146,20 @@ function saveApiKeys(openAIKey, claudeKey) { } let envContent = ""; + + // Read the existing .env file if it exists + if (fs.existsSync(envFilePath)) { + envContent = fs.readFileSync(envFilePath, "utf8"); + } + + // Update the specific API key if (openAIKey) { - envContent += `OPENAI_API_KEY=${openAIKey}\n`; + envContent = updateOrAddKey(envContent, "OPENAI_API_KEY", openAIKey); process.env.OPENAI_API_KEY = openAIKey; // Set for current session openai = new OpenAI({ apiKey: openAIKey }); } if (claudeKey) { - envContent += `CLAUDE_API_KEY=${claudeKey}\n`; + envContent = updateOrAddKey(envContent, "CLAUDE_API_KEY", claudeKey); process.env.CLAUDE_API_KEY = claudeKey; // Set for current session claude = new Anthropic({ apiKey: claudeKey }); } @@ -160,6 +167,18 @@ function saveApiKeys(openAIKey, claudeKey) { fs.writeFileSync(envFilePath, envContent.trim()); } +function updateOrAddKey(envContent, keyName, keyValue) { + const keyPattern = new RegExp(`^${keyName}=.*$`, "m"); + if (keyPattern.test(envContent)) { + // Update the existing key + envContent = envContent.replace(keyPattern, `${keyName}=${keyValue}`); + } else { + // Add the new key + envContent += `\n${keyName}=${keyValue}`; + } + return envContent; +} + async function getOllamaModels() { ollama = new Ollama.Ollama(); const _models = await ollama.list(); @@ -186,11 +205,16 @@ async function getModels() { ]; allModels.claudeModels = claudeModels; } - + let gptModelsPromise = []; if (keys.openAIKey) { openai = new OpenAI({ apiKey: keys.openAIKey }); // Wrap asynchronous call with a Promise to handle it in parallel - gptModelsPromise = openai.models.list(); + try { + gptModelsPromise = openai.models.list(); + } catch (error) { + gptModelsPromise = []; + console.error("Error fetching models from OpenAI:", error); + } } // Check if ollama exists and has a list method diff --git a/installer/client/gui/static/js/index.js b/installer/client/gui/static/js/index.js index 507f48a..2e58994 100644 --- a/installer/client/gui/static/js/index.js +++ b/installer/client/gui/static/js/index.js @@ -168,6 +168,13 @@ document.addEventListener("DOMContentLoaded", async function () { submitQuery(message); }); + window.electronAPI.on; + "reload-app", + () => { + // Reload the app + loadModels(); + }; + // Submit button click handler submitButton.addEventListener("click", async () => { const userInputValue = userInput.value;