-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.js
More file actions
413 lines (413 loc) · 14.5 KB
/
main.js
File metadata and controls
413 lines (413 loc) · 14.5 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import electron from "electron";
const { BrowserWindow, app, shell, Notification } = electron;
import { getLanguagePreference, getWindowBounds, saveWindowBounds, getAppSettings, getSidebarSettings, } from "./db/repos/settings.js";
import { registerWorkspaceIpc, stopPiRuntimes, cleanupOrphanedWorktrees, ensurePiAgentBootstrapped, } from "./ipc/workspace.js";
import { registerPiIpc } from "./ipc/pi.js";
import { registerUpdateIpc } from "./ipc/update.js";
import { initPiManager } from "./lib/pi/pi-manager.js";
import { initLogging } from "./lib/logging/log-manager.js";
import { initSentryTelemetry } from "./lib/telemetry/sentry.js";
import { fileURLToPath } from "node:url";
import { getDb } from "./db/index.js";
import path from "node:path";
import { setupStatusBar, updateLaunchAtStartup, getMainWindow, setMainWindow } from "./lib/status-bar.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Set the app name before readiness so macOS menu uses Chatons instead of Electron.
app.setName("Chatons");
// Register chatons:// as the app's custom protocol for deep links.
// On macOS the protocol is handled via open-url events; on Windows/Linux
// it falls back to the single-instance lock / command-line argv.
const PROTOCOL_PREFIX = "chatons";
if (process.defaultApp) {
// In development, register with the full path to the electron binary
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient(PROTOCOL_PREFIX, process.execPath, [
path.resolve(process.argv[1]),
]);
}
}
else {
app.setAsDefaultProtocolClient(PROTOCOL_PREFIX);
}
// Holds a pending deep-link URL that arrived before the window was ready.
let pendingDeepLinkUrl = null;
/**
* Parse a chatons:// URL and forward the relevant IPC event to the renderer.
* Supported routes:
* chatons://extensions/install/<npm-package-id>
*/
function handleDeepLink(url) {
const win = getMainWindow();
if (!win) {
// Window not ready yet; queue for later.
pendingDeepLinkUrl = url;
return;
}
// Bring the window to the foreground
if (win.isMinimized())
win.restore();
win.show();
win.focus();
try {
// Parse the URL: chatons://extensions/install/@scope/package
// URL constructor needs a valid scheme so replace chatons:// with https://
const parsed = new URL(url.replace(`${PROTOCOL_PREFIX}://`, "https://"));
const segments = parsed.pathname.split("/").filter(Boolean);
if (segments[0] === "extensions" && segments[1] === "install" && segments.length >= 3) {
// Reconstruct the full npm package id (may contain a scope like @scope/name)
const extensionId = decodeURIComponent(segments.slice(2).join("/"));
win.webContents.send("deeplink:extension-install", { extensionId });
}
}
catch (err) {
console.error("Failed to parse deep link URL:", url, err);
}
}
// Set custom userData path to use Chatons-specific directory instead of Electron
const userDataPath = path.join(app.getPath('appData'), 'Chatons');
app.setPath('userData', userDataPath);
const isDev = Boolean(process.env.VITE_DEV_SERVER_URL);
const appIconPath = path.join(__dirname, "../build/icons/icon.png");
// Variable to keep track of the main window
let mainWindow = null;
let isQuitting = false;
const isTelemetryEnabled = () => {
try {
const db = getDb();
const settings = getSidebarSettings(db);
return Boolean(settings.allowAnonymousTelemetry);
}
catch {
return false;
}
};
const telemetryClient = isDev
? null
: initSentryTelemetry({
appVersion: app.getVersion(),
isEnabled: isTelemetryEnabled,
});
function createWindow() {
const db = getDb();
const initialBounds = getWindowBounds(db);
const languagePreference = getLanguagePreference(db);
const appSettings = getAppSettings(db);
mainWindow = new BrowserWindow({
x: initialBounds.x,
y: initialBounds.y,
width: initialBounds.width,
height: initialBounds.height,
minWidth: 1200,
minHeight: 780,
frame: false,
titleBarStyle: "hidden",
backgroundColor: "#f5f5f7",
icon: appIconPath,
show: !appSettings.startMinimized, // Hide window if startMinimized is enabled
webPreferences: {
preload: path.join(__dirname, "preload.js"),
contextIsolation: true,
nodeIntegration: false,
sandbox: false,
},
});
// Set main window reference for status bar
setMainWindow(mainWindow);
if (isDev && process.env.VITE_DEV_SERVER_URL) {
mainWindow.loadURL(`${process.env.VITE_DEV_SERVER_URL}?language=${languagePreference}`);
mainWindow.webContents.openDevTools({ mode: "detach" });
}
else {
mainWindow.loadFile(path.join(__dirname, "../dist/index.html"), {
query: { language: languagePreference },
});
}
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: "deny" };
});
mainWindow.webContents.on("render-process-gone", (_event, details) => {
telemetryClient?.send({
timestamp: new Date().toISOString(),
source: "electron",
level: "error",
message: "render_process_gone",
data: details,
});
});
mainWindow.webContents.on("unresponsive", () => {
telemetryClient?.send({
timestamp: new Date().toISOString(),
source: "electron",
level: "warn",
message: "render_process_unresponsive",
});
});
let saveTimeout = null;
const scheduleBoundsSave = () => {
if (mainWindow?.isDestroyed()) {
return;
}
if (saveTimeout) {
clearTimeout(saveTimeout);
}
saveTimeout = setTimeout(() => {
if (mainWindow?.isDestroyed() || mainWindow?.isMinimized() || mainWindow?.isMaximized()) {
return;
}
saveWindowBounds(db, mainWindow.getBounds());
}, 200);
};
mainWindow.on("move", scheduleBoundsSave);
mainWindow.on("resize", scheduleBoundsSave);
mainWindow.on("close", (e) => {
if (saveTimeout) {
clearTimeout(saveTimeout);
}
if (!mainWindow.isMinimized() && !mainWindow.isMaximized()) {
saveWindowBounds(db, mainWindow.getBounds());
}
// Keep app alive when user closes the window on macOS, unless a real app quit is in progress.
if (process.platform === 'darwin' && !isQuitting) {
e.preventDefault();
mainWindow.hide();
}
});
// Expose method to check if window is focused
electron.ipcMain.handle('window:isFocused', () => {
return mainWindow?.isFocused() ?? false;
});
// Expose method to show notification
electron.ipcMain.handle('window:showNotification', (_event, title, body, conversationId) => {
if (!mainWindow)
return false;
// Only show notification if window is not focused
if (mainWindow.isFocused()) {
return false;
}
const notification = new Notification({
title: title,
body: body,
icon: appIconPath,
});
notification.on('click', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.show();
mainWindow.focus();
if (conversationId) {
mainWindow.webContents.send('desktop:notification-clicked', { conversationId });
}
}
});
notification.show();
return true;
});
// Window control handlers
electron.ipcMain.handle('window:close', () => {
if (mainWindow) {
mainWindow.close();
}
});
electron.ipcMain.handle('window:minimize', () => {
if (mainWindow) {
mainWindow.minimize();
}
});
electron.ipcMain.handle('window:maximize', () => {
if (mainWindow) {
if (mainWindow.isMaximized()) {
mainWindow.unmaximize();
}
else {
mainWindow.maximize();
}
}
});
// Setup status bar after window is created
setupStatusBar(mainWindow);
// Update launch at startup setting
updateLaunchAtStartup(appSettings.launchAtStartup);
}
// macOS: handle chatons:// links when the app is already running
app.on("open-url", (event, url) => {
event.preventDefault();
handleDeepLink(url);
});
// Windows/Linux: prevent second instance and forward deep link from argv
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
}
else {
app.on("second-instance", (_event, argv) => {
// On Windows the deep link URL is passed as the last command-line argument
const url = argv.find((arg) => arg.startsWith(`${PROTOCOL_PREFIX}://`));
if (url) {
handleDeepLink(url);
}
else {
// Just focus the existing window
const win = getMainWindow();
if (win) {
if (win.isMinimized())
win.restore();
win.show();
win.focus();
}
}
});
}
app.whenReady().then(async () => {
if (process.platform === "darwin" && app.dock) {
app.dock.setIcon(appIconPath);
}
// Ensure Chatons-owned Pi agent directory and base config files exist.
try {
ensurePiAgentBootstrapped();
}
catch (error) {
console.error("Erreur lors de l'initialisation du répertoire Pi local:", error);
}
// Initialiser le système de logging
try {
initLogging({
onLog: (entry) => telemetryClient?.send(entry),
});
console.log('Système de logging initialisé');
}
catch (error) {
console.error('Erreur lors de l\'initialisation du système de logging:', error);
}
process.on("uncaughtException", (error) => {
telemetryClient?.send({
timestamp: new Date().toISOString(),
source: "electron",
level: "error",
message: "uncaughtException",
data: {
name: error?.name,
message: error?.message,
stack: error?.stack,
},
});
});
process.on("unhandledRejection", (reason) => {
telemetryClient?.send({
timestamp: new Date().toISOString(),
source: "electron",
level: "error",
message: "unhandledRejection",
data: {
reason: reason instanceof Error
? {
name: reason.name,
message: reason.message,
stack: reason.stack,
}
: reason,
},
});
});
// Initialiser Pi Manager
try {
await initPiManager();
console.log('Pi Manager initialisé avec succès');
}
catch (error) {
console.error('Erreur lors de l\'initialisation de Pi Manager:', error);
}
// Initialiser Sandbox Manager
try {
// Import sandbox manager to ensure it's initialized
const { sandboxManager } = await import('./lib/sandbox/sandbox-manager.js');
console.log('Sandbox Manager initialisé avec succès');
}
catch (error) {
console.error('Erreur lors de l\'initialisation de Sandbox Manager:', error);
}
// Clean up orphaned worktrees at startup
try {
const cleanedCount = await cleanupOrphanedWorktrees();
if (cleanedCount > 0) {
console.log(`Nettoyage terminé: ${cleanedCount} worktrees orphelins supprimés`);
}
}
catch (error) {
console.error('Erreur lors du nettoyage des worktrees orphelins:', error);
// Ne pas bloquer le démarrage pour cette erreur
}
// Prefetch changelogs from GitHub (skip if update check already ran)
try {
const { UpdateService } = await import('./lib/update/update-service.js');
await UpdateService.prefetchAndStoreChangelogs();
console.log('Changelogs prefetched and stored successfully');
}
catch (error) {
console.error('Erreur lors de la pré-récupération des changelogs:', error);
// Ne pas bloquer le démarrage pour cette erreur
}
// Set anonymous user identity for Sentry user counting (opt-in only)
if (telemetryClient && isTelemetryEnabled()) {
try {
const db = getDb();
const settings = getSidebarSettings(db);
if (settings.anonymousInstallId) {
telemetryClient.setAnonymousUser(settings.anonymousInstallId);
}
}
catch {
// Non-critical: user counting will still work from future events
}
}
registerWorkspaceIpc();
registerPiIpc();
registerUpdateIpc();
createWindow();
// Send a single startup heartbeat so Sentry can count active users
telemetryClient?.send({
timestamp: new Date().toISOString(),
source: "electron",
level: "info",
message: "app_started",
data: { version: app.getVersion() },
});
// Flush any deep link URL that arrived before the window was ready
if (pendingDeepLinkUrl) {
const url = pendingDeepLinkUrl;
pendingDeepLinkUrl = null;
// Small delay to let the renderer finish mounting
setTimeout(() => handleDeepLink(url), 1500);
}
// On Windows/Linux, check startup argv for a deep link URL
if (process.platform !== "darwin") {
const url = process.argv.find((arg) => arg.startsWith(`${PROTOCOL_PREFIX}://`));
if (url) {
setTimeout(() => handleDeepLink(url), 1500);
}
}
app.on("activate", () => {
const mainWin = getMainWindow();
if (mainWin) {
mainWin.show();
mainWin.focus();
}
else if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
}).catch((error) => {
console.error("Erreur fatale au démarrage de l'application:", error);
});
app.on("before-quit", () => {
isQuitting = true;
void stopPiRuntimes();
});
app.on("window-all-closed", () => {
// Don't quit the app on any platform when windows are closed
// The app will continue running in the background
});
//# sourceMappingURL=main.js.map