-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpythonManager.cjs
More file actions
235 lines (197 loc) · 8.92 KB
/
Copy pathpythonManager.cjs
File metadata and controls
235 lines (197 loc) · 8.92 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
const { app } = require('electron');
const path = require('path');
const { spawn, execFile } = require('child_process');
const fs = require('fs');
const { promisify } = require('util');
const execFileAsync = promisify(execFile);
class PythonBackendManager {
constructor() {
this.userDataPath = app.getPath('userData');
this.venvPath = path.join(this.userDataPath, 'silhouette_brain_venv');
this.isDev = !app.isPackaged;
this.brainPath = this.detectBrainPath();
// Process references
this.apiProcess = null;
this.daemonProcess = null;
}
privateLog(msg) {
console.log(`[PythonManager] ${msg}`);
}
notifyWindow(win, message, percent = 0) {
if (win && !win.isDestroyed()) {
win.webContents.send('onboarding-progress', { message, percent });
}
}
detectBrainPath() {
const potentialPaths = [
path.resolve(process.cwd(), '../silhouette-brain'),
path.resolve(__dirname, '../silhouette-brain'),
path.join(process.resourcesPath, 'silhouette-brain')
];
for (const p of potentialPaths) {
if (fs.existsSync(p)) {
this.privateLog(`Detected Silhouette Brain source at: ${p}`);
return p;
}
}
return null;
}
getBasePythonExe() {
const isWin = process.platform === 'win32';
const pyName = isWin ? 'python.exe' : 'bin/python3';
const portablePath = path.join(process.resourcesPath, 'python', pyName);
if (fs.existsSync(portablePath)) {
this.privateLog(`Using portable base Python interpreter: ${portablePath}`);
return portablePath;
}
// Fallback to system Python for development environment
this.privateLog(`Portable Python not found. Falling back to system Python.`);
return isWin ? 'python' : 'python3';
}
getVenvPythonExe() {
const isWin = process.platform === 'win32';
const pyName = isWin ? 'Scripts\\python.exe' : 'bin/python';
return path.join(this.venvPath, pyName);
}
async initialize(win) {
if (!this.brainPath) {
this.privateLog('⚠️ Sibling silhouette-brain directory not detected. Skipping Python API execution.');
this.notifyWindow(win, 'Intelligent Brain not found. Running in core mode only.', 100);
return;
}
const venvPython = this.getVenvPythonExe();
const basePython = this.getBasePythonExe();
// Check if writable Virtual Environment already exists
if (!fs.existsSync(venvPython)) {
this.privateLog('Creating writable Python Virtual Environment...');
this.notifyWindow(win, 'Iniciando primer arranque: Creando entorno aislado virtual...', 15);
try {
// 1. Create venv: basePython -m venv venvPath
await execFileAsync(basePython, ['-m', 'venv', this.venvPath]);
this.privateLog(`Venv created successfully at: ${this.venvPath}`);
// 2. Install requirements: venvPython -m pip install -r requirements.txt
this.notifyWindow(win, 'Instalando dependencias de Inteligencia Artificial (pip)...', 40);
await this.installDependencies(win);
this.notifyWindow(win, 'Entorno virtual configurado con éxito.', 90);
} catch (err) {
this.privateLog(`❌ Error initializing Python venv: ${err.message}`);
this.notifyWindow(win, `Error al crear venv: ${err.message}. Intentando iniciar con Python del sistema.`, 90);
}
} else {
this.privateLog('Writable Virtual Environment already initialized.');
}
// 3. Start Python servers using the venv python
this.notifyWindow(win, 'Levantando procesos de la Colmena de Agentes...', 95);
this.startEcosystem();
this.notifyWindow(win, 'Todos los procesos iniciados con éxito.', 100);
}
async installDependencies(win) {
const venvPython = this.getVenvPythonExe();
const requirementsPath = path.join(this.brainPath, 'requirements.txt');
if (!fs.existsSync(requirementsPath)) {
this.privateLog(`requirements.txt not found at: ${requirementsPath}. Skipping pip install.`);
return;
}
this.privateLog(`Installing requirements from: ${requirementsPath}`);
return new Promise((resolve, reject) => {
// First upgrade pip
execFile(venvPython, ['-m', 'pip', 'install', '--upgrade', 'pip'], (err) => {
if (err) this.privateLog(`Warning: Failed to upgrade pip: ${err.message}`);
// Then run pip install requirements
const pipProcess = spawn(venvPython, ['-m', 'pip', 'install', '-r', requirementsPath]);
let progressPercent = 40;
pipProcess.stdout.on('data', (data) => {
const output = data.toString().trim();
if (output) {
this.privateLog(`[pip] ${output.substring(0, 100)}`);
// Mock progress update increment
progressPercent = Math.min(85, progressPercent + 0.5);
this.notifyWindow(win, `Instalando dependencias: ${output.substring(0, 50)}...`, progressPercent);
}
});
pipProcess.stderr.on('data', (data) => {
this.privateLog(`[pip warning] ${data.toString().trim()}`);
});
pipProcess.on('close', (code) => {
if (code === 0) {
this.privateLog('Pip install requirements completed successfully.');
resolve();
} else {
reject(new Error(`pip install exited with code ${code}`));
}
});
});
});
}
startEcosystem() {
const pythonExe = fs.existsSync(this.getVenvPythonExe()) ? this.getVenvPythonExe() : this.getBasePythonExe();
this.privateLog(`Spawning Python ecosystem using: ${pythonExe}`);
const pathSeparator = process.platform === 'win32' ? ';' : ':';
const pythonPath = [
path.join(this.brainPath, 'src', 'core'),
path.join(this.brainPath, 'src', 'api'),
this.brainPath
].join(pathSeparator);
const brainEnv = {
...process.env,
PYTHONPATH: pythonPath,
BRAIN_ROOT: this.brainPath,
BRAIN_SRC_DIR: path.join(this.brainPath, 'src', 'core'),
BRAIN_DATA_DIR: path.join(this.brainPath, 'data'),
PYTHONUNBUFFERED: '1',
PYTHONIOENCODING: 'utf-8'
};
// 1. Start Enhanced Memory API
const apiScript = path.join(this.brainPath, 'src', 'api', 'enhanced_memory_api.py');
if (fs.existsSync(apiScript)) {
this.privateLog(`Spawning Brain API: ${apiScript}`);
this.apiProcess = spawn(pythonExe, [apiScript], {
cwd: this.brainPath,
env: brainEnv
});
this.apiProcess.stdout.on('data', (data) => {
console.log(`[Brain API] ${data.toString().trim()}`);
});
this.apiProcess.stderr.on('data', (data) => {
console.error(`[Brain API ERROR] ${data.toString().trim()}`);
});
}
// 2. Start Unified Daemon
const daemonScript = path.join(this.brainPath, 'src', 'core', 'unified_daemon.py');
if (fs.existsSync(daemonScript)) {
this.privateLog(`Spawning Brain Daemon: ${daemonScript}`);
this.daemonProcess = spawn(pythonExe, [daemonScript], {
cwd: this.brainPath,
env: brainEnv
});
this.daemonProcess.stdout.on('data', (data) => {
console.log(`[Brain Daemon] ${data.toString().trim()}`);
});
this.daemonProcess.stderr.on('data', (data) => {
console.error(`[Brain Daemon ERROR] ${data.toString().trim()}`);
});
}
}
killProcess(proc, name) {
if (!proc) return;
this.privateLog(`Terminating ${name} (PID: ${proc.pid})...`);
try {
if (process.platform === 'win32') {
spawn('taskkill', ['/pid', proc.pid, '/f', '/t'], { stdio: 'ignore' });
} else {
proc.kill('SIGINT');
}
} catch (err) {
console.error(`❌ Failed to terminate ${name}:`, err);
}
}
shutdown() {
this.killProcess(this.apiProcess, 'Brain API');
this.killProcess(this.daemonProcess, 'Brain Daemon');
this.apiProcess = null;
this.daemonProcess = null;
}
}
module.exports = {
pythonBackendManager: new PythonBackendManager()
};