diff --git a/.gitignore b/.gitignore index 9c5818a..cf920d6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ node_modules/ -kuzco-cli* gui/dist/ *.exe + diff --git a/gui/kuzco-gui.js b/gui/kuzco-gui.js index ce5a0fd..3ae6b6c 100644 --- a/gui/kuzco-gui.js +++ b/gui/kuzco-gui.js @@ -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 os = require('os'); const KuzcoCore = require('./kuzcoCore'); function createWindow() { @@ -15,6 +17,42 @@ function createWindow() { }); 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(() => { diff --git a/gui/kuzcoCore.js b/gui/kuzcoCore.js index e0c2eef..b998aee 100644 --- a/gui/kuzcoCore.js +++ b/gui/kuzcoCore.js @@ -12,21 +12,30 @@ async function fetchData(url, options = {}) { class KuzcoCore { constructor() { + this.configPath = path.join(os.homedir(), '.kuzco-cli', 'config.json'); this.API_KEY = this.loadApiKey(); } loadApiKey() { - const configPath = path.join(os.homedir(), '.kuzco-cli', 'config.json'); try { - const configFile = fs.readFileSync(configPath); - const config = JSON.parse(configFile); - return config.API_KEY; + if (fs.existsSync(this.configPath)) { + const configFile = fs.readFileSync(this.configPath); + 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) { console.error(`An error occurred while reading the API key: ${error.message}`); return ''; } } + apiKeyExists() { + return fs.existsSync(this.configPath) && this.API_KEY !== ''; + } + async sendPrompt(prompt) { try { const response = await fetch('https://relay.kuzco.xyz/v1/chat/completions', { diff --git a/gui/preload.js b/gui/preload.js index e9e9a88..3b8b718 100644 --- a/gui/preload.js +++ b/gui/preload.js @@ -3,3 +3,7 @@ const { contextBridge, ipcRenderer } = require('electron'); contextBridge.exposeInMainWorld('electronAPI', { sendPrompt: (prompt) => ipcRenderer.invoke('send-prompt', prompt), }); + +contextBridge.exposeInMainWorld('api', { + submitApiKey: (apiKey) => ipcRenderer.send('submit-api-key', apiKey) +}); \ No newline at end of file diff --git a/gui/prompt.html b/gui/prompt.html new file mode 100644 index 0000000..f1154c3 --- /dev/null +++ b/gui/prompt.html @@ -0,0 +1,19 @@ + + +
+