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:
parent
61cf0bd49e
commit
97402a8b1f
|
@ -1,4 +1,4 @@
|
|||
node_modules/
|
||||
kuzco-cli*
|
||||
gui/dist/
|
||||
*.exe
|
||||
|
||||
|
|
|
@ -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(() => {
|
||||
|
|
|
@ -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);
|
||||
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', {
|
||||
|
|
|
@ -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)
|
||||
});
|
|
@ -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>
|
Loading…
Reference in New Issue