Add function to alert user if their API key isnt set and open a popup for user to enter their key

This commit is contained in:
Wizzard 2024-03-09 02:42:30 -05:00
parent 61cf0bd49e
commit 97402a8b1f
5 changed files with 76 additions and 6 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
node_modules/ node_modules/
kuzco-cli*
gui/dist/ gui/dist/
*.exe *.exe

View File

@ -1,5 +1,7 @@
const { app, BrowserWindow, ipcMain } = require('electron'); const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const fs = require('fs');
const path = require('path'); const path = require('path');
const os = require('os');
const KuzcoCore = require('./kuzcoCore'); const KuzcoCore = require('./kuzcoCore');
function createWindow() { function createWindow() {
@ -15,6 +17,42 @@ function createWindow() {
}); });
mainWindow.loadFile('index.html'); mainWindow.loadFile('index.html');
if (!kuzcoCore.apiKeyExists()) {
promptForApiKey(mainWindow);
}
}
ipcMain.on('submit-api-key', (event, apiKey) => {
const configDir = path.join(os.homedir(), '.kuzco-cli');
const configPath = path.join(configDir, 'config.json');
if (!fs.existsSync(configDir)){
fs.mkdirSync(configDir, { recursive: true });
}
fs.writeFileSync(configPath, JSON.stringify({ API_KEY: apiKey }, null, 2), 'utf8');
event.sender.send('api-key-saved');
});
let inputWindow;
function promptForApiKey() {
inputWindow = new BrowserWindow({
width: 300,
height: 200,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
},
});
inputWindow.loadFile('prompt.html');
inputWindow.on('closed', () => {
inputWindow = null;
});
} }
app.whenReady().then(() => { app.whenReady().then(() => {

View File

@ -12,21 +12,30 @@ async function fetchData(url, options = {}) {
class KuzcoCore { class KuzcoCore {
constructor() { constructor() {
this.configPath = path.join(os.homedir(), '.kuzco-cli', 'config.json');
this.API_KEY = this.loadApiKey(); this.API_KEY = this.loadApiKey();
} }
loadApiKey() { loadApiKey() {
const configPath = path.join(os.homedir(), '.kuzco-cli', 'config.json');
try { try {
const configFile = fs.readFileSync(configPath); if (fs.existsSync(this.configPath)) {
const config = JSON.parse(configFile); const configFile = fs.readFileSync(this.configPath);
return config.API_KEY; const config = JSON.parse(configFile);
return config.API_KEY;
} else {
console.log('API Key config file does not exist. Please set up your API Key.');
return '';
}
} catch (error) { } catch (error) {
console.error(`An error occurred while reading the API key: ${error.message}`); console.error(`An error occurred while reading the API key: ${error.message}`);
return ''; return '';
} }
} }
apiKeyExists() {
return fs.existsSync(this.configPath) && this.API_KEY !== '';
}
async sendPrompt(prompt) { async sendPrompt(prompt) {
try { try {
const response = await fetch('https://relay.kuzco.xyz/v1/chat/completions', { const response = await fetch('https://relay.kuzco.xyz/v1/chat/completions', {

View File

@ -3,3 +3,7 @@ const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('electronAPI', { contextBridge.exposeInMainWorld('electronAPI', {
sendPrompt: (prompt) => ipcRenderer.invoke('send-prompt', prompt), sendPrompt: (prompt) => ipcRenderer.invoke('send-prompt', prompt),
}); });
contextBridge.exposeInMainWorld('api', {
submitApiKey: (apiKey) => ipcRenderer.send('submit-api-key', apiKey)
});

19
gui/prompt.html Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>Enter API Key</title>
</head>
<body>
<h1>Enter API Key</h1>
<input type="text" id="apiKeyInput" placeholder="API Key">
<button id="submitApiKey">Submit</button>
<script>
const { ipcRenderer } = require('electron');
document.getElementById('submitApiKey').addEventListener('click', () => {
const apiKey = document.getElementById('apiKeyInput').value;
ipcRenderer.send('submit-api-key', apiKey);
});
</script>
</body>
</html>