-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
139 lines (107 loc) · 3.36 KB
/
main.js
File metadata and controls
139 lines (107 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// main.js
const { app, BrowserWindow, ipcMain, dialog } = require("electron");
const path = require("path");
const pyilotCore = require("./pyilotCore.js"); // ✅ Import AI backend logic
// ✅ Destructure extractUrl explicitly if needed
const { extractUrl, resolveFolder, runPythonCode } = pyilotCore;
function createWindow() {
const win = new BrowserWindow({
width: 1000,
height: 700,
title: "Pyilot",
webPreferences: {
preload: path.join(__dirname, "preload.js"),
contextIsolation: true,
nodeIntegration: false,
},
});
win.setMenuBarVisibility(false);
win.loadFile("frontend/dist/index.html")
}
app.whenReady().then(createWindow);
// ========== IPC HANDLERS ==========
ipcMain.handle("confirm-run", async (_, code) => {
return dialog.showMessageBoxSync({
type: "question",
buttons: ["Run", "Cancel"],
defaultId: 1,
cancelId: 1,
title: "Pyilot — Run Script?",
message: "Do you want to run this AI-generated script?",
detail: code,
});
});
ipcMain.handle("danger-check", async (_, dangers) => {
return dialog.showMessageBoxSync({
type: "warning",
buttons: ["Allow", "Cancel"],
defaultId: 1,
cancelId: 1,
title: "⚠️ Pyilot — Dangerous Code Detected",
message: `This script contains potentially destructive actions:\n\n${dangers.join(", ")}\n\nAre you sure you want to continue?`,
});
});
// ✅ AI Prompt → Response
ipcMain.handle("ai-response", async (_, { prompt, sessionId }) => {
return await pyilotCore.runAI(prompt, sessionId);
});
// ✅ Safe Code Execution (only runs after confirm)
ipcMain.handle("execute-code", async (_, { code, prompt }) => {
const folder = pyilotCore.resolveFolder(prompt);
const secondArg = pyilotCore.extractUrl(prompt);
return await new Promise((resolve) => {
let output = "";
pyilotCore.runPythonCode(
code,
prompt,
folder,
(msg) => output += msg, // append stdout
(err) => output += err, // append stderr
() => {
const clean = output.trim();
resolve(clean || "✅ Task completed.");
},
() => {}, // installing
secondArg
);
});
});
ipcMain.handle("get-sessions", () => {
// return { id: title }
const sessions = {};
for (const id in pyilotCore.allChats) {
sessions[id] = pyilotCore.allChats[id].title || id;
}
return sessions;
});
ipcMain.handle("delete-session", async (_, sessionId) => {
try {
delete pyilotCore.allChats[sessionId];
pyilotCore.saveChatSessions(); // Make sure this persists to disk
// Return updated session list
return Object.entries(pyilotCore.allChats).map(([id, data]) => ({
id,
title: data.title || "Untitled",
}));
} catch (err) {
console.error("Failed to delete session:", err);
return []; // fallback to empty
}
});
ipcMain.handle("load-session", async (_, sessionId) => {
const session = pyilotCore.allChats[sessionId];
return session ? session.messages : [];
});
ipcMain.handle("list-sessions", async () => {
const sessions = pyilotCore.allChats;
return Object.entries(sessions).map(([id, data]) => ({
id,
title: data.title || "Untitled",
}));
});
ipcMain.handle("save-message", (_, msg) => {
pyilotCore.saveMessage(msg.sessionId, msg.role, msg.content);
});
ipcMain.handle("get-session-title", (_, id) => {
return allChats[id]?.title || "Untitled";
});