-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathApp.js
More file actions
145 lines (143 loc) · 5.67 KB
/
Copy pathApp.js
File metadata and controls
145 lines (143 loc) · 5.67 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
import { app, BrowserWindow, shell } from 'electron';
import * as MCPIPC from '#features/MCP/IPC/MCPIPC.js';
import { boot, startEngines, stopEngines } from '#main/Boot.js';
import { ensureDir } from '#main/Core/FileSystem.js';
import Paths from '#main/Core/Paths.js';
import { create as createWindow } from '#main/Core/Window.js';
import { BUILTIN_BROWSER_USER_AGENT } from '#main/Services/BrowserPreviewService.js';
import { initializeContentLibraries } from '#main/Services/ContentLibraryService.js';
import { initializePersonalMemoryLibrary } from '#main/Services/MemoryService.js';
import * as SystemPromptService from '#main/Services/SystemPromptService.js';
import * as UserService from '#main/Services/UserService.js';
import * as AppSettingsService from '#main/Services/AppSettingsService.js';
import * as AppLockService from '#main/Services/AppLockService.js';
import * as TrayService from '#main/Services/TrayService.js';
import { setupAutoUpdates } from '#main/Services/AutoUpdateService.js';
(app.commandLine.appendSwitch('disable-http2'),
app.commandLine.appendSwitch('lang', 'en-US'),
(app.userAgentFallback = BUILTIN_BROWSER_USER_AGENT));
let engines = null,
enginesStopped = !1;
const REQUIRED_RUNTIME_DIRS = Object.freeze([
Paths.DATA_DIR,
Paths.CHATS_DIR,
Paths.PROJECTS_DIR,
Paths.FEATURES_DATA_DIR,
Paths.MEMORIES_DIR,
Paths.USER_SKILLS_DIR,
Paths.USER_PERSONAS_DIR,
]);
function attachWindowServices(windowRef, activeEngines) {
if (!windowRef || !activeEngines) return;
const {
featureRegistry: featureRegistry,
channelEngine: channelEngine,
browserPreviewService: browserPreviewService,
} = activeEngines;
(browserPreviewService.attachToWindow(windowRef),
channelEngine.setWindow(windowRef),
featureRegistry.attachWindow(windowRef));
}
/**
* Resolves the correct start page for the main window.
*
* Priority order:
* 1. First-run → Setup page (app_lock doesn't apply yet, no account exists)
* 2. App lock → Lock page (user must authenticate before entering the app)
* 3. Default → Index page (normal launch)
*/
function resolveStartPage() {
if (UserService.isFirstRun()) return Paths.SETUP_PAGE;
if (AppLockService.isAppLockEnabled()) return Paths.LOCK_PAGE;
return Paths.INDEX_PAGE;
}
function createMainAppWindow(activeEngines, page = resolveStartPage()) {
const windowRef = createWindow(page);
return (attachWindowServices(windowRef, activeEngines), windowRef);
}
function shutdownEngines() {
if (engines && !enginesStopped)
try {
stopEngines(engines);
} catch (error) {
console.error('[App] Failed to stop engines cleanly:', error);
} finally {
((engines = null), (enginesStopped = !0));
}
}
(app.whenReady().then(async () => {
try {
app.on('web-contents-created', (_, contents) => {
contents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: 'deny' };
});
contents.on('will-navigate', (event, url) => {
try {
const appOrigin = new URL(contents.getURL()).origin;
if (new URL(url).origin !== appOrigin) {
event.preventDefault();
shell.openExternal(url);
}
} catch {
event.preventDefault();
}
});
});
(app.isPackaged && !process.argv.includes('--dev') && setupAutoUpdates(),
(function () {
for (const dir of REQUIRED_RUNTIME_DIRS) ensureDir(dir);
})(),
createMainAppWindow(null),
(async () => {
(initializeContentLibraries(),
initializePersonalMemoryLibrary(),
(engines = await boot()),
(enginesStopped = !1),
startEngines(engines));
for (const windowRef of BrowserWindow.getAllWindows())
attachWindowServices(windowRef, engines);
// Apply persisted app settings (keep_awake, tray, etc.) now that engines + windows are ready.
const _mainWin = BrowserWindow.getAllWindows()[0];
if (_mainWin) AppSettingsService.applyAll(_mainWin);
((async function (activeEngines) {
if (activeEngines?.connectorEngine && activeEngines?.featureRegistry)
try {
await SystemPromptService.get({
user: UserService.readUser(),
customInstructions: UserService.readText(Paths.CUSTOM_INSTRUCTIONS_FILE),
connectorEngine: activeEngines.connectorEngine,
featureRegistry: activeEngines.featureRegistry,
});
} catch (error) {
console.warn('[App] System prompt warm-up failed:', error.message);
}
})(engines).catch(() => {}),
(function () {
for (const windowRef of BrowserWindow.getAllWindows())
windowRef &&
!windowRef.isDestroyed() &&
windowRef.webContents?.send?.('backend-ready');
})(),
MCPIPC.autoConnect().catch((err) =>
console.warn('[App] MCP auto-connect failed:', err.message),
));
})().catch((error) => {
(console.error('[App] Startup failed:', error), shutdownEngines(), app.quit());
}),
app.on('activate', () => {
0 === BrowserWindow.getAllWindows().length && createMainAppWindow(engines);
}));
} catch (error) {
(console.error('[App] Startup failed:', error), shutdownEngines(), app.quit());
}
}),
app.on('before-quit', shutdownEngines),
app.on('window-all-closed', () => {
// macOS: conventional to keep app running with no windows open.
if ('darwin' === process.platform) return;
// Windows / Linux: if tray is active, hide to tray instead of quitting.
if (TrayService.isActive()) return;
shutdownEngines();
app.quit();
}));