-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
355 lines (315 loc) · 10.9 KB
/
Copy pathmain.js
File metadata and controls
355 lines (315 loc) · 10.9 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
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const {
signInWithOpenAI,
ensureOpenAIConfig,
getValidOpenAIToken,
logoutOpenAI
} = require('./src/auth/openai');
const { codexChatCompletion, codexChatCompletionStream } = require('./src/llm/codex-chat');
function loadWindowState() {
try {
const userData = app.getPath('userData');
const statePath = path.join(userData, 'window-state.json');
const raw = fs.readFileSync(statePath, 'utf-8');
const parsed = JSON.parse(raw);
return {
width: parsed.width || 1600,
height: parsed.height || 1000,
x: parsed.x,
y: parsed.y
};
} catch {
return { width: 1600, height: 1000 };
}
}
function saveWindowState(bounds) {
try {
const userData = app.getPath('userData');
const statePath = path.join(userData, 'window-state.json');
fs.writeFileSync(
statePath,
JSON.stringify(
{ width: bounds.width, height: bounds.height, x: bounds.x, y: bounds.y },
null,
2
)
);
} catch (err) {
console.error('Failed to save window state', err);
}
}
function getPdfCachePath(pdfPath) {
const hash = crypto.createHash('sha256').update(pdfPath).digest('hex');
const cacheDir = path.join(app.getPath('userData'), 'pdf-cache');
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir, { recursive: true });
}
return path.join(cacheDir, `${hash}.pdf`);
}
function createWindow() {
const state = loadWindowState();
const win = new BrowserWindow({
width: state.width,
height: state.height,
x: state.x,
y: state.y,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
});
win.loadFile(path.join(__dirname, 'renderer', 'index.html'));
win.on('close', () => saveWindowState(win.getBounds()));
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
ipcMain.handle('auth:openai:status', () => ensureOpenAIConfig());
ipcMain.handle('auth:openai:login', () => signInWithOpenAI());
ipcMain.handle('auth:openai:token', () => getValidOpenAIToken());
ipcMain.handle('auth:openai:logout', () => logoutOpenAI());
ipcMain.handle('chat:openai:completion', async (_event, { messages, instructions, model }) => {
const { token, accountId } = await getValidOpenAIToken();
const userMessage = Array.isArray(messages) ? messages.find((m) => m.role === 'user') : null;
const question = userMessage?.content || messages?.[0]?.content || '';
const result = await codexChatCompletion(token, accountId, question, instructions, model);
return result;
});
ipcMain.on('chat:openai:stream:start', async (event, { messages, instructions, streamId, model }) => {
try {
const { token, accountId } = await getValidOpenAIToken();
const userMessage = Array.isArray(messages) ? messages.find((m) => m.role === 'user') : null;
const question = userMessage?.content || messages?.[0]?.content || '';
for await (const chunk of codexChatCompletionStream(token, accountId, question, instructions, model)) {
if (!event.sender.isDestroyed()) {
event.sender.send('chat:openai:stream:chunk', { streamId, chunk });
}
}
if (!event.sender.isDestroyed()) {
event.sender.send('chat:openai:stream:done', { streamId });
}
} catch (err) {
if (!event.sender.isDestroyed()) {
event.sender.send('chat:openai:stream:error', { streamId, error: err.message });
}
}
});
// Load custom prompts for settings UI
ipcMain.handle('prompts:get', () => {
try {
const customP = path.join(app.getPath('userData'), 'custom_prompts.json');
if (!fs.existsSync(customP)) {
// Return default empty structure if file doesn't exist
return { system: "", sections: { keywords: "", brief: "", summary: "" } };
}
const raw = fs.readFileSync(customP, 'utf-8');
return JSON.parse(raw);
} catch (err) {
console.error('Failed to load custom_prompts.json', err);
return {};
}
});
// Save custom prompts
ipcMain.handle('prompts:save', (_event, data) => {
try {
const customP = path.join(app.getPath('userData'), 'custom_prompts.json');
fs.writeFileSync(customP, JSON.stringify(data, null, 2), 'utf-8');
return true;
} catch (err) {
console.error('Failed to save custom_prompts.json', err);
throw err;
}
});
// Load combined prompts for actual generation
ipcMain.handle('prompts:getCombined', () => {
try {
const baseP = path.join(__dirname, 'src', 'llm', 'prompts.json');
const customP = path.join(app.getPath('userData'), 'custom_prompts.json');
const base = JSON.parse(fs.readFileSync(baseP, 'utf-8'));
let custom = {};
if (fs.existsSync(customP)) {
try {
custom = JSON.parse(fs.readFileSync(customP, 'utf-8'));
} catch (e) {
console.warn('Failed to parse custom prompts', e);
}
}
const combine = (b, c) => {
if (!c) return b;
return b + '\n\n' + c;
};
return {
system: combine(base.system, custom.system),
sections: {
keywords: combine(base.sections?.keywords, custom.sections?.keywords),
brief: combine(base.sections?.brief, custom.sections?.brief),
summary: combine(base.sections?.summary, custom.sections?.summary)
}
};
} catch (err) {
console.error('Failed to load combined prompts', err);
// Fallback to base or empty
return {};
}
});
ipcMain.handle('file:read', (_event, filePath) => {
try {
return fs.readFileSync(filePath);
} catch (err) {
console.error(`Failed to read file: ${filePath}`, err);
throw err;
}
});
ipcMain.handle('pdf:cache:read', (_event, pdfPath) => {
if (!pdfPath) return null;
try {
const cachePath = getPdfCachePath(pdfPath);
if (!fs.existsSync(cachePath)) return null;
return fs.readFileSync(cachePath);
} catch (err) {
console.error(`Failed to read cached PDF: ${pdfPath}`, err);
return null;
}
});
ipcMain.handle('pdf:cache:write', (_event, { pdfPath, data }) => {
if (!pdfPath || !data) return false;
try {
const cachePath = getPdfCachePath(pdfPath);
const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
fs.writeFileSync(cachePath, buffer);
return true;
} catch (err) {
console.error(`Failed to write cached PDF: ${pdfPath}`, err);
return false;
}
});
ipcMain.handle('dialog:open', async () => {
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ['openFile'],
filters: [{ name: 'PDF Files', extensions: ['pdf'] }]
});
if (canceled) return null;
return filePaths[0];
});
let settingsWin = null;
ipcMain.on('window:settings:open', () => {
if (settingsWin) {
settingsWin.focus();
return;
}
settingsWin = new BrowserWindow({
width: 600,
height: 800,
title: 'Settings',
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
});
settingsWin.loadFile(path.join(__dirname, 'renderer', 'settings.html'));
settingsWin.on('closed', () => {
settingsWin = null;
});
});
ipcMain.handle('app:get-path', () => app.getPath('userData'));
ipcMain.handle('data:read-index', () => {
try {
const userDataPath = app.getPath('userData');
const indexPath = path.join(userDataPath, 'sunshade-index.json');
const docsPath = path.join(userDataPath, 'sunshade-docs.json'); // Legacy file
const contentDir = path.join(userDataPath, 'doc-contents');
// 1. Migration block (Old single file -> Split structure)
if (!fs.existsSync(indexPath) && fs.existsSync(docsPath)) {
console.log('Starting migration from single docs.json to split index/content structure...');
try {
const oldData = JSON.parse(fs.readFileSync(docsPath, 'utf-8'));
const newIndex = {};
if (!fs.existsSync(contentDir)) {
fs.mkdirSync(contentDir, { recursive: true });
}
for (const [key, doc] of Object.entries(oldData)) {
// Extract heavy content
const { extractedText, analysis, highlights, chatHistory, ...meta } = doc;
// Generate a safe hash for filename
const fileHash = crypto.createHash('sha256').update(key).digest('hex');
const contentFilePath = path.join(contentDir, `${fileHash}.json`);
// Save heavy content separately
fs.writeFileSync(contentFilePath, JSON.stringify({ extractedText, analysis, highlights, chatHistory }, null, 2), 'utf-8');
// Save lightweight meta to index
newIndex[key] = { ...meta, contentHash: fileHash };
}
// Save the new index
fs.writeFileSync(indexPath, JSON.stringify(newIndex, null, 2), 'utf-8');
console.log('Migration complete. Renaming old sunshade-docs.json to .bak');
fs.renameSync(docsPath, `${docsPath}.bak`); // Backup old file
return JSON.stringify(newIndex);
} catch (err) {
console.error('Migration failed:', err);
}
}
// 2. Normal read flow
if (!fs.existsSync(indexPath)) return null;
return fs.readFileSync(indexPath, 'utf-8');
} catch (err) {
console.error('Failed to read index file', err);
return null;
}
});
ipcMain.handle('data:write-index', (_event, content) => {
try {
const indexPath = path.join(app.getPath('userData'), 'sunshade-index.json');
fs.writeFileSync(indexPath, content, 'utf-8');
return true;
} catch (err) {
console.error('Failed to write index file', err);
throw err;
}
});
ipcMain.handle('data:read-content', (_event, hash) => {
try {
const contentPath = path.join(app.getPath('userData'), 'doc-contents', `${hash}.json`);
if (!fs.existsSync(contentPath)) return null;
return fs.readFileSync(contentPath, 'utf-8');
} catch (err) {
console.error(`Failed to read content file [${hash}]`, err);
return null;
}
});
ipcMain.handle('data:write-content', (_event, hash, content) => {
try {
const contentDir = path.join(app.getPath('userData'), 'doc-contents');
if (!fs.existsSync(contentDir)) {
fs.mkdirSync(contentDir, { recursive: true });
}
const contentPath = path.join(contentDir, `${hash}.json`);
fs.writeFileSync(contentPath, content, 'utf-8');
return true;
} catch (err) {
console.error(`Failed to write content file [${hash}]`, err);
throw err;
}
});
ipcMain.handle('data:delete-content', (_event, hash) => {
try {
const contentPath = path.join(app.getPath('userData'), 'doc-contents', `${hash}.json`);
if (fs.existsSync(contentPath)) {
fs.unlinkSync(contentPath);
}
return true;
} catch (err) {
console.error(`Failed to delete content file [${hash}]`, err);
return false;
}
});