kuzco-cli/gui/kuzco-gui.js

82 lines
2.0 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');
const KuzcoCore = require('./js/kuzcoCore');
2024-03-09 02:15:01 -05:00
function createWindow() {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'js/preload.js'),
2024-03-09 02:15:01 -05:00
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
},
});
mainWindow.loadFile('html/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');
2024-03-09 02:52:10 -05:00
event.reply('api-key-saved');
app.relaunch();
app.quit();
});
let inputWindow;
function promptForApiKey() {
inputWindow = new BrowserWindow({
width: 300,
height: 300,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
},
});
inputWindow.loadFile('html/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, model }) => {
console.log("Received model in main process:", model);
return await kuzcoCore.sendPrompt(prompt, model);
2024-03-09 15:22:44 -05:00
});
ipcMain.on('abort-prompt', () => {
kuzcoCore.abortFetch();
});