kuzco-cli/gui/kuzco-gui.js

75 lines
1.8 KiB
JavaScript
Raw Normal View History

const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const fs = require('fs');
2024-03-09 02:15:01 -05:00
const path = require('path');
const os = require('os');
2024-03-09 02:15:01 -05:00
const KuzcoCore = require('./kuzcoCore');
function createWindow() {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
},
});
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;
});
2024-03-09 02:15:01 -05:00
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
const kuzcoCore = new KuzcoCore();
ipcMain.handle('send-prompt', async (event, prompt) => {
return await kuzcoCore.sendPrompt(prompt);
});