-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyilotCore.js
More file actions
320 lines (256 loc) Β· 9.13 KB
/
pyilotCore.js
File metadata and controls
320 lines (256 loc) Β· 9.13 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const { spawn } = require("child_process");
const fs = require("fs");
const os = require("os");
const API_KEY = "YOUR_GEMINI_API_KEY"; // Replace with your actual key
const memoryPath = "memory.json";
const sessionPath = "chatSessions.json";
const logTxtPath = "logs.txt";
const pyFolder = "python/";
const backupPath = `${pyFolder}last_code_backup.py`;
if (!fs.existsSync(pyFolder)) fs.mkdirSync(pyFolder);
if (!fs.existsSync(sessionPath)) fs.writeFileSync(sessionPath, "{}");
if (!fs.existsSync(logTxtPath)) fs.writeFileSync(logTxtPath, "");
let allChats = {};
try {
if (fs.existsSync(sessionPath)) {
const content = fs.readFileSync(sessionPath, "utf-8").trim();
if (content) {
allChats = JSON.parse(content);
}
}
} catch (err) {
console.error("β οΈ Failed to load sessions:", err);
allChats = {};
}
function saveChatSessions() {
fs.writeFileSync(sessionPath, JSON.stringify(allChats, null, 2));
}
function saveMessage(sessionId, role, content) {
if (!allChats[sessionId]) {
allChats[sessionId] = {
title: "", // We'll set this below
messages: [],
};
}
const safeContent = typeof content === "string" ? content : JSON.stringify(content);
// β
Set title if it's the first user message
if (role === "user" && allChats[sessionId].title === "") {
const clean = safeContent.trim().replace(/\s+/g, " ");
allChats[sessionId].title = clean.slice(0, 35) + (clean.length > 35 ? "..." : "");
}
allChats[sessionId].messages.push({ role, content: safeContent });
saveChatSessions();
}
function extractCode(text = "") {
const match = text.match(/```(?:python)?\s*([\s\S]*?)```/i);
return match ? match[1].trim() : null;
}
function isPythonCode(code) {
return /import|def |print|os\.|pathlib|open\(/.test(code);
}
function resolveFolder(text) {
const home = os.homedir().replace(/\\/g, "/");
const folders = {
documents: `${home}/Documents`,
downloads: `${home}/Downloads`,
desktop: `${home}/Desktop`,
pictures: `${home}/Pictures`,
videos: `${home}/Videos`
};
for (const key in folders) {
if (text.toLowerCase().includes(key)) return folders[key];
}
return "."; // fallback
}
function extractUrl(text) {
// Match full URLs
const fullUrlMatch = text.match(/https?:\/\/[^\s'"`]+/);
if (fullUrlMatch) return fullUrlMatch[0];
// Match domain names like "google.com"
const domainMatch = text.match(/\b(?:[a-z0-9-]+\.)+[a-z]{2,}\b/);
if (domainMatch) return "https://" + domainMatch[0];
return "";
}
function fetchGeminiResponse(prompt, isCodeTask, sessionId) {
const sessionMessages = allChats[sessionId]?.messages || [];
const systemPrompt = isCodeTask
? `You are a Python coding assistant. When the user describes a task (like "list files in downloads"), generate a clean Python script.
π RULES:
- DO NOT hardcode folder paths (like "Documents" or "Downloads").
- Use sys.argv[1] for folder paths.
- Use sys.argv[2] for URLs or external input.
- DO NOT print "URL to open:" if the value is empty.
- ALWAYS wrap code in a single \`\`\`python block.
- Handle errors cleanly, don't crash.
- Don't ask questions like "what would you like me to do?"
- Even if unclear, try to help with code.
`
: `You are a helpful, friendly AI assistant.
Guidelines:
- Respond naturally, like you're chatting with a human.
- Do NOT say things like "there was a technical issue."
- Only give Python code if clearly asked.
- Never say "I'm not sure what to do with that."
`;
const safeMessages = sessionMessages
.filter((m) => typeof m.content === "string")
.slice(-10) // last 10 messages for memory context
.map((m) => ({
role: m.role === "ai" ? "model" : "user",
parts: [{ text: m.content }],
}));
const payload = {
contents: [
{ role: "user", parts: [{ text: systemPrompt }] },
...safeMessages,
{ role: "user", parts: [{ text: prompt }] },
],
};
console.log("π€ Gemini Request Payload:", JSON.stringify(payload, null, 2));
return fetch(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent?key=${API_KEY}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
}
)
.then((res) => res.json())
.then((data) => {
console.log("π Gemini API raw response:", JSON.stringify(data, null, 2));
return data;
});
}
// β
UPDATED: now includes automatic package checking and pip install
function runPythonCode(
code,
promptText,
folder,
onOutput,
onError,
onDone,
onInstalling,
secondArg = ""
) {
secondArg = secondArg || extractUrl(promptText);
if (fs.existsSync(`${pyFolder}run_code.py`)) {
fs.copyFileSync(`${pyFolder}run_code.py`, backupPath);
}
fs.writeFileSync(`${pyFolder}run_code.py`, code);
const timestamp = new Date().toISOString();
fs.appendFileSync(logTxtPath, `\n=== ${timestamp} ===\nPrompt: ${promptText}\n\n${code}\n`);
const imports = [...code.matchAll(/^import\s+(\w+)|^from\s+(\w+)/gm)]
.map((match) => match[1] || match[2])
.filter((mod, idx, arr) => mod && arr.indexOf(mod) === idx);
const skipList = [
"os", "sys", "time", "math", "json", "re", "random", "datetime",
"pathlib", "webbrowser", "subprocess", "shutil", "string"
];
const checkAndInstall = (i = 0) => {
if (i >= imports.length) return runFinal();
const mod = imports[i];
if (skipList.includes(mod)) return checkAndInstall(i + 1);
const test = spawn("python", ["-c", `import ${mod}`]);
test.on("exit", (code) => {
if (code === 0) {
checkAndInstall(i + 1); // Already installed
} else {
onInstalling?.(mod);
const install = spawn("pip", ["install", mod]);
install.stdout.on("data", (d) => {
onOutput?.(`π¦ Installing ${mod}: ` + d.toString());
});
install.stderr.on("data", (e) => {
onOutput?.(`β οΈ pip error: ${e.toString()}`);
});
install.on("close", () => checkAndInstall(i + 1));
}
});
};
const runFinal = () => {
console.log("π Executing:", "python", [`${pyFolder}run_code.py`, folder, secondArg]);
onOutput?.("π Running script...\n");
const proc = spawn("python", [`${pyFolder}run_code.py`, folder, secondArg]);
proc.stdout.on("data", (d) => onOutput?.(d.toString()));
proc.stderr.on("data", (e) => onError?.(e.toString()));
proc.on("close", (code) => onDone?.(code));
};
checkAndInstall();
}
async function runAI(prompt, sessionId = Date.now().toString()) {
const isCodeTask = /(?:python|code|script|open|list files|folder|terminal|command)/i.test(prompt);
const sessionMessages = allChats[sessionId]?.messages || [];
// Only send valid Gemini messages
const safeMessages = sessionMessages
.filter(msg => typeof msg.content === "string")
.map(msg => ({
role: msg.role === "ai" ? "model" : "user",
parts: [{ text: msg.content }],
}));
// System prompt must also use "user" role in Gemini
const systemPrompt = {
role: "user",
parts: [
{
text: isCodeTask
? `Dont be dumb, always read the rules. You are a Python coding assistant, DO NOT HARD CODE ANY FOLDER PATH, FORCE THEM FOR EXAMPLE IF USER ASK TO LIST FILE IN DOCUUMENT, DO YOUR BEST TO SHOW IT.
Rules:
- Dont be dumb, always read the rules.
- You MUST always try to generate Python code if a task can be solved with it.
- DO NOT say βIβm not sureβ or βI cannot help.β Instead, assume what the user likely means.
- DO NOT hardcode folder paths like "Downloads".
- Use sys.argv[1] for folder paths.
- Use sys.argv[2] for URLs.
- ALWAYS wrap your code in a single \`\`\`python block.
- If you Cannot do what user asked, try to help with code.
- Be helpful, polite, and concise.`
: `You are a helpful, friendly AI assistant.
Guidelines:
- Respond naturally, like you're chatting with a human.
- DO NOT say things like "there was a technical issue."
- If you Cannot do what user asked, try to help with code.
- You MUST always try to generate Python code if a task can be solved with it.
- DO NOT say "I'm not sure what to do with that."
- Only provide Python code if clearly asked.`,
},
],
};
const payload = {
contents: [
systemPrompt,
...safeMessages,
{
role: "user",
parts: [{ text: prompt }],
},
],
};
console.log("π€ Gemini Request Payload:", JSON.stringify(payload, null, 2));
const res = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${API_KEY}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
}
);
const data = await res.json();
console.log("π Gemini API raw response:", JSON.stringify(data, null, 2));
const reply = data?.candidates?.[0]?.content?.parts?.[0]?.text || "β No response.";
// Save the messages to memory
saveMessage(sessionId, "user", prompt);
saveMessage(sessionId, "ai", reply);
return reply;
}
module.exports = {
fetchGeminiResponse,
saveMessage,
extractCode,
isPythonCode,
resolveFolder,
runPythonCode,
saveChatSessions,
extractUrl,
allChats,
runAI,
};