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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
|
// Copyright (c) 2026 Yuren Hao
// Licensed under AGPL-3.0 - see LICENSE file
import { app, BrowserWindow, ipcMain, dialog, shell, net } from 'electron'
import { join, basename, dirname, relative, extname, delimiter } from 'path'
import { copyFile, readFile, writeFile, mkdir as mkdirAsync, unlink, readdir, stat, rename as fsRename, rm, cp } from 'fs/promises'
import { existsSync } from 'fs'
import { spawn } from 'child_process'
import * as pty from 'node-pty'
import { OverleafSocket, type RootFolder, type SubFolder, type JoinDocResult } from './overleafSocket'
import { CompilationManager } from './compilationManager'
import { FileSyncBridge } from './fileSyncBridge'
// Prevent EPIPE crashes when stdout/stderr is closed (e.g. Electron launched from Finder)
process.stdout?.on('error', () => {})
process.stderr?.on('error', () => {})
let mainWindow: BrowserWindow | null = null
const ptyInstances = new Map<string, pty.IPty>()
let overleafSock: OverleafSocket | null = null
let compilationManager: CompilationManager | null = null
let fileSyncBridge: FileSyncBridge | null = null
let mcpStateDir = '' // syncDir for .lattex-mcp.json
let mcpProjectId = ''
let mcpCommentContexts: Record<string, { file: string; text: string; pos: number }> = {}
let mcpPathDocMap: Record<string, string> = {} // relPath → docId for MCP
const mcpOnlineUsers = new Map<string, { name: string; email?: string }>()
let mcpOnlineUsersWriteTimer: ReturnType<typeof setTimeout> | null = null
async function writeMcpState(): Promise<void> {
if (!mcpStateDir || !mcpProjectId) return
try {
// Read S2 API key if available
let s2Key: string | undefined
try {
const keys = JSON.parse(await readFile(apiKeysPath, 'utf-8'))
if (keys.semanticScholar) s2Key = keys.semanticScholar
} catch { /* ignore */ }
const state: Record<string, unknown> = {
projectId: mcpProjectId,
cookie: overleafSessionCookie,
csrf: overleafCsrfToken,
commentContexts: mcpCommentContexts,
pathDocMap: mcpPathDocMap
}
if (s2Key) state.semanticScholarApiKey = s2Key
await writeFile(join(mcpStateDir, '.lattex-mcp.json'), JSON.stringify(state, null, 2))
} catch { /* ignore */ }
}
async function prepareMcpServerPath(tmpDir: string): Promise<string> {
const sourcePath = app.isPackaged
? join(app.getAppPath() + '.unpacked', 'out', 'mcp', 'lattex.mjs')
: join(__dirname, '..', '..', 'src', 'mcp', 'lattex.mjs')
if (!app.isPackaged) return sourcePath
// Unsigned macOS apps can be launched from an App Translocation path. That
// path is not stable enough to persist in .mcp.json, so copy the bundled MCP
// server into the live project directory and point Claude at the copy.
const mcpDir = join(tmpDir, '.lattex')
await mkdirAsync(mcpDir, { recursive: true })
const serverPath = join(mcpDir, 'lattex-mcp.mjs')
await copyFile(sourcePath, serverPath)
return serverPath
}
async function clearDisabledLattexMcpServer(tmpDir: string): Promise<void> {
const settingsPath = join(tmpDir, '.claude', 'settings.local.json')
try {
const raw = await readFile(settingsPath, 'utf-8')
const settings = JSON.parse(raw) as Record<string, unknown>
const disabled = settings.disabledMcpjsonServers
if (!Array.isArray(disabled) || !disabled.includes('lattex')) return
const nextDisabled = disabled.filter((name) => name !== 'lattex')
if (nextDisabled.length > 0) {
settings.disabledMcpjsonServers = nextDisabled
} else {
delete settings.disabledMcpjsonServers
}
await writeFile(settingsPath, JSON.stringify(settings, null, 2))
} catch {
// No local settings yet, or not JSON. Claude can create it later.
}
}
let commentContextRefreshTimer: ReturnType<typeof setTimeout> | null = null
function scheduleCommentContextRefresh(): void {
if (commentContextRefreshTimer) clearTimeout(commentContextRefreshTimer)
commentContextRefreshTimer = setTimeout(async () => {
commentContextRefreshTimer = null
if (!overleafSock?.projectData) return
const { docPathMap: dp } = walkRootFolder(overleafSock.projectData.project.rootFolder)
const contexts: Record<string, { file: string; text: string; pos: number }> = {}
for (const [did, rp] of Object.entries(dp)) {
try {
const result = await overleafSock.joinDoc(did)
if (result.ranges?.comments) {
for (const c of result.ranges.comments) {
if (c.op?.t) contexts[c.op.t] = { file: rp, text: c.op.c || '', pos: c.op.p || 0 }
}
}
// Don't leaveDoc — bridge keeps all docs joined
} catch { /* ignore */ }
}
mcpCommentContexts = contexts
writeMcpState()
sendToRenderer('comments:initContexts', { contexts })
}, 2000) // 2s debounce
}
function writeMcpOnlineUsers(): void {
if (!mcpStateDir) return
if (mcpOnlineUsersWriteTimer) clearTimeout(mcpOnlineUsersWriteTimer)
mcpOnlineUsersWriteTimer = setTimeout(() => {
const users = Array.from(mcpOnlineUsers.entries()).map(([id, u]) => ({ id, ...u }))
writeFile(join(mcpStateDir, '.lattex-online-users.json'), JSON.stringify(users)).catch(() => {})
}, 500)
}
function createWindow(): void {
mainWindow = new BrowserWindow({
width: 1400,
height: 900,
minWidth: 900,
minHeight: 600,
// Frameless inset title bar is a macOS affordance; use the native
// frame elsewhere
...(process.platform === 'darwin'
? { titleBarStyle: 'hiddenInset' as const, trafficLightPosition: { x: 15, y: 15 } }
: {}),
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
sandbox: false,
contextIsolation: true
}
})
// Disable Electron's built-in pinch/Ctrl+wheel zoom so editor can handle it
mainWindow.webContents.setVisualZoomLevelLimits(1, 1)
if (process.env['ELECTRON_RENDERER_URL']) {
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
} else {
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
}
}
/** Safely send IPC to renderer — no-op if window is gone */
function sendToRenderer(channel: string, ...args: unknown[]) {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send(channel, ...args)
}
}
ipcMain.handle('fs:readFile', async (_e, filePath: string) => {
return readFile(filePath, 'utf-8')
})
ipcMain.handle('fs:readBinary', async (_e, filePath: string) => {
const buffer = await readFile(filePath)
return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength)
})
// ── Workspace file operations (agent scratch space browser) ─────
ipcMain.handle('fs:writeFile', async (_e, filePath: string, content: string) => {
await mkdirAsync(dirname(filePath), { recursive: true })
await writeFile(filePath, content, 'utf-8')
})
interface DiskNode {
name: string
path: string
isDir: boolean
children?: DiskNode[]
}
// List a directory tree from disk. Paths in the result are `pathPrefix` +
// path relative to rootPath (so the renderer can key tabs consistently).
ipcMain.handle('fs:listDirTree', async (_e, rootPath: string, pathPrefix: string) => {
const MAX_ENTRIES_PER_DIR = 500
const MAX_DEPTH = 10
async function walk(dir: string, rel: string, depth: number): Promise<DiskNode[]> {
if (depth > MAX_DEPTH) return []
let entries
try {
entries = await readdir(dir, { withFileTypes: true })
} catch {
return []
}
entries = entries
.filter((e) => !e.name.startsWith('.'))
.sort((a, b) =>
(b.isDirectory() ? 1 : 0) - (a.isDirectory() ? 1 : 0) || a.name.localeCompare(b.name)
)
.slice(0, MAX_ENTRIES_PER_DIR)
const nodes: DiskNode[] = []
for (const entry of entries) {
const relPath = rel ? `${rel}/${entry.name}` : entry.name
if (entry.isDirectory()) {
nodes.push({
name: entry.name,
path: pathPrefix + relPath,
isDir: true,
children: await walk(join(dir, entry.name), relPath, depth + 1)
})
} else if (entry.isFile()) {
nodes.push({ name: entry.name, path: pathPrefix + relPath, isDir: false })
}
}
return nodes
}
return walk(rootPath, '', 0)
})
ipcMain.handle('fs:mkdirp', async (_e, dirPath: string) => {
await mkdirAsync(dirPath, { recursive: true })
})
ipcMain.handle('fs:rename', async (_e, oldPath: string, newPath: string) => {
await fsRename(oldPath, newPath)
})
ipcMain.handle('fs:deletePath', async (_e, targetPath: string) => {
await rm(targetPath, { recursive: true, force: true })
})
ipcMain.handle('fs:copyPath', async (_e, src: string, dest: string) => {
await mkdirAsync(dirname(dest), { recursive: true })
await cp(src, dest, { recursive: true })
})
ipcMain.handle('fs:exists', async (_e, targetPath: string) => {
return existsSync(targetPath)
})
// ── API Key Storage ─────────────────────────────────────────────
const apiKeysPath = join(app.getPath('userData'), 'api-keys.json')
ipcMain.handle('settings:getApiKeys', async () => {
try {
return JSON.parse(await readFile(apiKeysPath, 'utf-8'))
} catch {
return {}
}
})
ipcMain.handle('settings:setApiKeys', async (_e, keys: Record<string, string>) => {
await writeFile(apiKeysPath, JSON.stringify(keys, null, 2))
})
// ── LaTeX Compilation ────────────────────────────────────────────
// Ensure TeX binaries are in PATH (GUI-launched apps may miss them)
const texPaths = process.platform === 'win32'
? [
'C:\\texlive\\2025\\bin\\windows',
'C:\\texlive\\2024\\bin\\windows',
join(process.env.LOCALAPPDATA || '', 'Programs', 'MiKTeX', 'miktex', 'bin', 'x64')
]
: ['/Library/TeX/texbin', '/usr/local/texlive/2024/bin/universal-darwin', '/usr/texbin', '/opt/homebrew/bin']
const currentPath = process.env.PATH || ''
for (const p of texPaths) {
if (!currentPath.includes(p)) {
process.env.PATH = `${p}${delimiter}${process.env.PATH}`
}
}
// SyncTeX: PDF position → source file:line (inverse search)
ipcMain.handle('synctex:editFromPdf', async (_e, pdfPath: string, page: number, x: number, y: number) => {
return new Promise<{ file: string; line: number } | null>((resolve) => {
const pdfDir = dirname(pdfPath)
console.log(`[synctex] edit -o ${page}:${x}:${y}:${pdfPath} (cwd: ${pdfDir})`)
const proc = spawn('synctex', ['edit', '-o', `${page}:${x}:${y}:${pdfPath}`], {
env: process.env,
cwd: pdfDir
})
let stdout = ''
let stderr = ''
proc.stdout?.on('data', (d) => { stdout += d.toString() })
proc.stderr?.on('data', (d) => { stderr += d.toString() })
proc.on('close', (code) => {
console.log(`[synctex] exit=${code} stdout=${stdout.slice(0, 300)} stderr=${stderr.slice(0, 200)}`)
// Parse output: Input:filename\nLine:123\n...
const fileMatch = stdout.match(/Input:(.+)/)
const lineMatch = stdout.match(/Line:(\d+)/)
if (fileMatch && lineMatch) {
let filePath = fileMatch[1].trim()
// Strip CLSI compilation prefix (server compile uses /compile/ as cwd)
if (filePath.startsWith('/compile/')) {
filePath = filePath.slice('/compile/'.length)
}
// Convert absolute path to relative (strip tmpDir prefix for local compile)
const syncDir = compilationManager?.dir
if (syncDir && filePath.startsWith(syncDir)) {
filePath = filePath.slice(syncDir.length).replace(/^\//, '')
}
// Normalize path: strip leading ./, collapse /./
filePath = filePath.replace(/\/\.\//g, '/').replace(/^\.\//, '')
console.log(`[synctex] resolved: file=${filePath} line=${lineMatch[1]}`)
resolve({ file: filePath, line: parseInt(lineMatch[1]) })
} else {
console.log('[synctex] no match in output')
resolve(null)
}
})
proc.on('error', (err) => {
console.log(`[synctex] spawn error: ${err.message}`)
resolve(null)
})
})
})
// SyncTeX: source file:line → PDF page/position (forward search)
ipcMain.handle('synctex:viewFromSource', async (_e, line: number, col: number, relPath: string) => {
const syncDir = compilationManager?.dir
if (!syncDir) return null
// Look for build dir output.pdf
const buildDir = join(syncDir, '.build')
const pdfPath = join(buildDir, 'output.pdf')
const filePath = join(syncDir, relPath)
const input = `${line}:${col}:${filePath}`
console.log(`[synctex] view -i ${input} -o ${pdfPath}`)
return new Promise<{ page: number; x: number; y: number; h: number; v: number; W: number; H: number } | null>((resolve) => {
const proc = spawn('synctex', ['view', '-i', input, '-o', pdfPath], {
env: process.env,
cwd: syncDir
})
let stdout = ''
let stderr = ''
proc.stdout?.on('data', (d) => { stdout += d.toString() })
proc.stderr?.on('data', (d) => { stderr += d.toString() })
proc.on('close', (code) => {
console.log(`[synctex] view exit=${code} stdout=${stdout.slice(0, 300)} stderr=${stderr.slice(0, 200)}`)
const pageMatch = stdout.match(/Page:(\d+)/)
const xMatch = stdout.match(/x:([0-9.]+)/)
const yMatch = stdout.match(/y:([0-9.]+)/)
const hMatch = stdout.match(/h:([0-9.]+)/)
const vMatch = stdout.match(/v:([0-9.]+)/)
const wMatch = stdout.match(/W:([0-9.]+)/)
const hMatch2 = stdout.match(/H:([0-9.]+)/)
if (pageMatch) {
resolve({
page: parseInt(pageMatch[1]),
x: xMatch ? parseFloat(xMatch[1]) : 0,
y: yMatch ? parseFloat(yMatch[1]) : 0,
h: hMatch ? parseFloat(hMatch[1]) : 0,
v: vMatch ? parseFloat(vMatch[1]) : 0,
W: wMatch ? parseFloat(wMatch[1]) : 0,
H: hMatch2 ? parseFloat(hMatch2[1]) : 0
})
} else {
resolve(null)
}
})
proc.on('error', (err) => {
console.log(`[synctex] view spawn error: ${err.message}`)
resolve(null)
})
})
})
// ── Multi-file search ────────────────────────────────────────────
const TEXT_EXTS = new Set(['.tex', '.bib', '.sty', '.cls', '.bst', '.txt', '.md', '.cfg', '.def', '.dtx', '.ins', '.ltx'])
async function walkDir(dir: string, base: string): Promise<string[]> {
const results: string[] = []
const entries = await readdir(dir, { withFileTypes: true })
for (const entry of entries) {
if (entry.name.startsWith('.')) continue
const full = join(dir, entry.name)
if (entry.isDirectory()) {
results.push(...await walkDir(full, base))
} else if (TEXT_EXTS.has(extname(entry.name).toLowerCase())) {
results.push(relative(base, full))
}
}
return results
}
ipcMain.handle('search:files', async (_e, query: string, caseSensitive: boolean) => {
const syncDir = compilationManager?.dir
if (!syncDir || !query) return []
const files = await walkDir(syncDir, syncDir)
const results: Array<{ file: string; line: number; content: string; col: number }> = []
const flags = caseSensitive ? 'g' : 'gi'
let regex: RegExp
try {
regex = new RegExp(query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), flags)
} catch {
return []
}
for (const relPath of files) {
if (results.length >= 200) break
try {
const content = await readFile(join(syncDir, relPath), 'utf-8')
const lines = content.split('\n')
for (let i = 0; i < lines.length; i++) {
if (results.length >= 200) break
const match = regex.exec(lines[i])
if (match) {
results.push({ file: relPath, line: i + 1, content: lines[i].trim().slice(0, 200), col: match.index })
regex.lastIndex = 0 // reset for next line
}
}
} catch { /* skip unreadable files */ }
}
return results
})
// ── Terminal / PTY ───────────────────────────────────────────────
ipcMain.handle('pty:spawn', async (_e, id: string, cwd: string, cmd?: string, args?: string[]) => {
const existing = ptyInstances.get(id)
if (existing) {
existing.kill()
ptyInstances.delete(id)
}
const shellPath = cmd || (process.platform === 'win32'
? process.env.COMSPEC || 'powershell.exe'
: process.env.SHELL || '/bin/zsh')
const shellArgs = args || (process.platform === 'win32' ? [] : ['-l'])
const ptyEnv: Record<string, string> = {
...(process.env as Record<string, string>),
TERM: 'xterm-256color',
COLORTERM: 'truecolor',
TERM_PROGRAM: 'LatteX',
LANG: process.env.LANG || 'en_US.UTF-8',
}
const instance = pty.spawn(shellPath, shellArgs, {
name: 'xterm-256color',
cols: 80,
rows: 24,
cwd,
env: ptyEnv
})
ptyInstances.set(id, instance)
instance.onData((data) => {
// Strip DEC 2026 synchronized output sequences — xterm.js may buffer indefinitely
// if the begin/end markers are split across PTY chunks
const cleaned = data.replace(/\x1b\[\?2026[hl]/g, '')
if (cleaned) sendToRenderer(`pty:data:${id}`, cleaned)
})
instance.onExit(() => {
// Only delete if this is still the current instance (avoid race with re-spawn)
if (ptyInstances.get(id) === instance) {
sendToRenderer(`pty:exit:${id}`)
ptyInstances.delete(id)
}
})
})
ipcMain.handle('pty:write', async (_e, id: string, data: string) => {
ptyInstances.get(id)?.write(data)
})
ipcMain.handle('pty:resize', async (_e, id: string, cols: number, rows: number) => {
try {
ptyInstances.get(id)?.resize(cols, rows)
} catch { /* ignore resize errors */ }
})
ipcMain.handle('pty:kill', async (_e, id: string) => {
const instance = ptyInstances.get(id)
if (instance) {
instance.kill()
ptyInstances.delete(id)
}
})
// ── Overleaf Web Session (for comments) ─────────────────────────
let overleafSessionCookie = ''
let overleafCsrfToken = ''
// Persist cookie to disk
const cookiePath = join(app.getPath('userData'), 'overleaf-session.json')
async function saveOverleafSession(): Promise<void> {
try {
await writeFile(cookiePath, JSON.stringify({ cookie: overleafSessionCookie, csrf: overleafCsrfToken }))
} catch { /* ignore */ }
}
let sessionLoadPromise: Promise<void> | null = null
async function loadOverleafSession(): Promise<void> {
try {
const raw = await readFile(cookiePath, 'utf-8')
const data = JSON.parse(raw)
if (data.cookie) {
overleafSessionCookie = data.cookie
overleafCsrfToken = data.csrf || ''
console.log('[overleaf] loaded saved session, verifying...')
// Verify it's still valid
const result = await overleafFetch('/user/projects')
if (!result.ok) {
console.log('[overleaf] saved session expired (status:', result.status, ')')
overleafSessionCookie = ''
overleafCsrfToken = ''
} else {
console.log('[overleaf] saved session is valid')
}
}
} catch { /* no saved session */ }
}
/**
* Re-fetch the CSRF token from the projects page (it can rotate/expire).
* Single-flight: concurrent 403s share one refresh. Validates the session
* first — when the cookie itself is dead, the /project fetch would redirect
* to the login page whose (anonymous) CSRF token must not be adopted.
*/
let csrfRefreshInFlight: Promise<boolean> | null = null
function refreshCsrfToken(): Promise<boolean> {
if (csrfRefreshInFlight) return csrfRefreshInFlight
csrfRefreshInFlight = (async () => {
try {
// Electron's net follows redirects, so an expired session may surface
// as a 200 login page rather than a 401 — require a JSON body too.
const session = await overleafFetchRaw('/user/projects')
if (!session.ok || typeof session.data !== 'object' || session.data === null) {
console.log('[overleaf] session expired — cannot refresh CSRF token')
sendToRenderer('auth:sessionExpired')
return false
}
const result = await overleafFetchRaw('/project', { raw: true })
if (!result.ok || typeof result.data !== 'string') return false
const m = (result.data as string).match(/ol-csrfToken[^>]*content="([^"]+)"/)
if (m) {
overleafCsrfToken = m[1]
saveOverleafSession()
return true
}
return false
} finally {
csrfRefreshInFlight = null
}
})()
return csrfRefreshInFlight
}
// Helper: make authenticated request to Overleaf web API.
// On 403 (stale CSRF token), refreshes the token and retries once.
async function overleafFetch(path: string, options: { method?: string; body?: string; raw?: boolean; cookie?: string } = {}): Promise<{ ok: boolean; status: number; data: unknown; setCookies: string[] }> {
const result = await overleafFetchRaw(path, options)
if (result.status === 403 && options.method && options.method !== 'GET') {
console.log(`[overleaf] 403 on ${options.method} ${path} — refreshing CSRF token and retrying`)
if (await refreshCsrfToken()) {
return overleafFetchRaw(path, options)
}
}
return result
}
async function overleafFetchRaw(path: string, options: { method?: string; body?: string; raw?: boolean; cookie?: string } = {}): Promise<{ ok: boolean; status: number; data: unknown; setCookies: string[] }> {
return new Promise((resolve) => {
const url = `https://www.overleaf.com${path}`
const request = net.request({ url, method: options.method || 'GET' })
request.setHeader('Cookie', options.cookie || overleafSessionCookie)
request.setHeader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.191 Safari/537.36')
if (!options.raw) {
request.setHeader('Accept', 'application/json')
}
if (options.body) {
request.setHeader('Content-Type', options.raw ? 'text/plain; charset=UTF-8' : 'application/json')
}
if (overleafCsrfToken && options.method && options.method !== 'GET') {
request.setHeader('x-csrf-token', overleafCsrfToken)
}
let body = ''
request.on('response', (response) => {
const sc = response.headers['set-cookie']
const setCookies = Array.isArray(sc) ? sc : sc ? [sc] : []
response.on('data', (chunk) => { body += chunk.toString() })
response.on('end', () => {
let data: unknown = body
if (!options.raw) {
try { data = JSON.parse(body) } catch { /* not json */ }
}
resolve({ ok: response.statusCode >= 200 && response.statusCode < 300, status: response.statusCode, data, setCookies })
})
})
request.on('error', (err) => {
resolve({ ok: false, status: 0, data: err.message, setCookies: [] })
})
if (options.body) request.write(options.body)
request.end()
})
}
// Login via webview — opens Overleaf login page, captures session cookie
ipcMain.handle('overleaf:webLogin', async () => {
return new Promise<{ success: boolean }>((resolve) => {
const loginWindow = new BrowserWindow({
width: 900,
height: 750,
parent: mainWindow!,
modal: true,
webPreferences: { nodeIntegration: false, contextIsolation: true }
})
loginWindow.loadURL('https://www.overleaf.com/login')
// Inject a floating back button when navigated away from overleaf.com
const injectBackButton = () => {
loginWindow.webContents.executeJavaScript(`
if (!document.getElementById('lattex-back-btn')) {
const btn = document.createElement('div');
btn.id = 'lattex-back-btn';
btn.innerHTML = '← Back';
btn.style.cssText = 'position:fixed;top:8px;left:8px;z-index:999999;padding:6px 14px;' +
'background:#333;color:#fff;border-radius:6px;cursor:pointer;font:13px -apple-system,sans-serif;' +
'box-shadow:0 2px 8px rgba(0,0,0,.3);user-select:none;-webkit-app-region:no-drag;';
btn.addEventListener('click', () => history.back());
btn.addEventListener('mouseenter', () => btn.style.background = '#555');
btn.addEventListener('mouseleave', () => btn.style.background = '#333');
document.body.appendChild(btn);
}
`).catch(() => {})
}
loginWindow.webContents.on('did-finish-load', injectBackButton)
loginWindow.webContents.on('did-navigate-in-page', injectBackButton)
// Verify cookie by calling Overleaf API — only succeed if we get 200
const verifyAndCapture = async (): Promise<boolean> => {
const cookies = await loginWindow.webContents.session.cookies.get({ domain: '.overleaf.com' })
if (!cookies.find((c) => c.name === 'overleaf_session2')) return false
const testCookie = cookies.map((c) => `${c.name}=${c.value}`).join('; ')
// Test if this cookie is actually authenticated
const ok = await new Promise<boolean>((res) => {
const req = net.request({ url: 'https://www.overleaf.com/user/projects', method: 'GET' })
req.setHeader('Cookie', testCookie)
req.setHeader('Accept', 'application/json')
req.on('response', (resp) => {
resp.on('data', () => {})
resp.on('end', () => res(resp.statusCode === 200))
})
req.on('error', () => res(false))
req.end()
})
if (!ok) return false
overleafSessionCookie = testCookie
// Get CSRF from meta tag if we're on an Overleaf page
try {
const csrf = await loginWindow.webContents.executeJavaScript(
`document.querySelector('meta[name="ol-csrfToken"]')?.content || ''`
)
if (csrf) overleafCsrfToken = csrf
} catch { /* ignore */ }
// If no CSRF from page, fetch from /project page
if (!overleafCsrfToken) {
await new Promise<void>((res) => {
const req = net.request({ url: 'https://www.overleaf.com/project', method: 'GET' })
req.setHeader('Cookie', overleafSessionCookie)
let body = ''
req.on('response', (resp) => {
resp.on('data', (chunk) => { body += chunk.toString() })
resp.on('end', () => {
const m = body.match(/ol-csrfToken[^>]*content="([^"]+)"/)
if (m) overleafCsrfToken = m[1]
res()
})
})
req.on('error', () => res())
req.end()
})
}
return true
}
let resolved = false
const tryCapture = async () => {
if (resolved) return
const ok = await verifyAndCapture()
if (ok && !resolved) {
resolved = true
saveOverleafSession()
// Push fresh credentials into a live sync bridge (re-login mid-session)
fileSyncBridge?.updateAuth(overleafSessionCookie, overleafCsrfToken)
loginWindow.close()
resolve({ success: true })
}
}
loginWindow.webContents.on('did-navigate', () => { setTimeout(tryCapture, 2000) })
loginWindow.webContents.on('did-navigate-in-page', () => { setTimeout(tryCapture, 2000) })
loginWindow.on('closed', () => {
if (!overleafSessionCookie) resolve({ success: false })
})
})
})
// Check if web session is active — wait for startup load to finish
ipcMain.handle('overleaf:hasWebSession', async () => {
if (sessionLoadPromise) await sessionLoadPromise
return { loggedIn: !!overleafSessionCookie }
})
// Fetch all comment threads for a project
ipcMain.handle('overleaf:getThreads', async (_e, projectId: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch(`/project/${projectId}/threads`)
if (!result.ok) return { success: false, message: `HTTP ${result.status}` }
return { success: true, threads: result.data }
})
// Reply to a thread
ipcMain.handle('overleaf:replyThread', async (_e, projectId: string, threadId: string, content: string) => {
if (!overleafSessionCookie) return { success: false }
const result = await overleafFetch(`/project/${projectId}/thread/${threadId}/messages`, {
method: 'POST',
body: JSON.stringify({ content })
})
return { success: result.ok, data: result.data }
})
// Resolve a thread
ipcMain.handle('overleaf:resolveThread', async (_e, projectId: string, threadId: string, docId?: string) => {
if (!overleafSessionCookie) return { success: false }
// docId is required in the URL path for resolve
const docSegment = docId ? `/doc/${docId}` : ''
const result = await overleafFetch(`/project/${projectId}${docSegment}/thread/${threadId}/resolve`, {
method: 'POST',
body: '{}'
})
if (!result.ok) console.log(`[resolveThread] failed: ${result.status}`, result.data)
return { success: result.ok }
})
// Reopen a thread
ipcMain.handle('overleaf:reopenThread', async (_e, projectId: string, threadId: string, docId?: string) => {
if (!overleafSessionCookie) return { success: false }
const docSegment = docId ? `/doc/${docId}` : ''
const result = await overleafFetch(`/project/${projectId}${docSegment}/thread/${threadId}/reopen`, {
method: 'POST',
body: '{}'
})
if (!result.ok) console.log(`[reopenThread] failed: ${result.status}`, result.data)
return { success: result.ok }
})
// Delete a comment message
ipcMain.handle('overleaf:deleteMessage', async (_e, projectId: string, threadId: string, messageId: string) => {
if (!overleafSessionCookie) return { success: false }
const result = await overleafFetch(`/project/${projectId}/thread/${threadId}/messages/${messageId}`, {
method: 'DELETE'
})
return { success: result.ok }
})
// Edit a comment message
ipcMain.handle('overleaf:editMessage', async (_e, projectId: string, threadId: string, messageId: string, content: string) => {
if (!overleafSessionCookie) return { success: false }
const result = await overleafFetch(`/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`, {
method: 'POST',
body: JSON.stringify({ content })
})
return { success: result.ok }
})
// Delete entire thread
ipcMain.handle('overleaf:deleteThread', async (_e, projectId: string, docId: string, threadId: string) => {
if (!overleafSessionCookie) return { success: false }
const result = await overleafFetch(`/project/${projectId}/doc/${docId}/thread/${threadId}`, {
method: 'DELETE'
})
return { success: result.ok }
})
// Add a new comment: create thread via REST then submit comment op via existing socket
async function addComment(
projectId: string,
docId: string,
pos: number,
text: string,
content: string
): Promise<{ success: boolean; threadId?: string; message?: string }> {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
if (!overleafSock) return { success: false, message: 'not_connected' }
// Generate a random threadId (24-char hex like Mongo ObjectId)
const threadId = Array.from({ length: 24 }, () => Math.floor(Math.random() * 16).toString(16)).join('')
// Step 1: Create the thread message via REST
const msgResult = await overleafFetch(`/project/${projectId}/thread/${threadId}/messages`, {
method: 'POST',
body: JSON.stringify({ content })
})
if (!msgResult.ok) return { success: false, message: `REST failed: ${msgResult.status}` }
// Step 2: Submit the comment op via the existing socket connection
try {
// Join doc if not already joined, to get the current version
const alreadyJoined = docEventHandlers.has(docId)
const joinResult = await overleafSock.joinDoc(docId)
const version = joinResult.version
// Send the comment op
const commentOp = { c: text, p: pos, t: threadId }
console.log('[addComment] submitting op:', JSON.stringify(commentOp), 'v:', version)
await overleafSock.applyOtUpdate(docId, [commentOp], version, '')
console.log('[addComment] op applied successfully')
// Leave doc if we joined it just for this
if (!alreadyJoined) {
await overleafSock.leaveDoc(docId)
}
return { success: true, threadId }
} catch (e) {
console.log('[addComment] error:', e)
return { success: false, message: String(e) }
}
}
ipcMain.handle('overleaf:addComment', async (_e, projectId: string, docId: string, pos: number, text: string, content: string) => {
return addComment(projectId, docId, pos, text, content)
})
// ── OT / Socket Mode IPC ─────────────────────────────────────────
interface SocketFileNode {
name: string
path: string
isDir: boolean
children?: SocketFileNode[]
docId?: string
fileRefId?: string
folderId?: string
}
function walkRootFolder(folders: RootFolder[]): {
files: SocketFileNode[]
docPathMap: Record<string, string>
pathDocMap: Record<string, string>
fileRefs: Array<{ id: string; path: string }>
rootFolderId: string
} {
const docPathMap: Record<string, string> = {}
const pathDocMap: Record<string, string> = {}
const fileRefs: Array<{ id: string; path: string }> = []
function walkFolder(f: SubFolder | RootFolder, prefix: string): SocketFileNode[] {
const nodes: SocketFileNode[] = []
for (const doc of f.docs || []) {
const relPath = prefix + doc.name
docPathMap[doc._id] = relPath
pathDocMap[relPath] = doc._id
nodes.push({
name: doc.name,
path: relPath,
isDir: false,
docId: doc._id
})
}
for (const ref of f.fileRefs || []) {
const relPath = prefix + ref.name
fileRefs.push({ id: ref._id, path: relPath })
nodes.push({
name: ref.name,
path: relPath,
isDir: false,
fileRefId: ref._id
})
}
for (const sub of f.folders || []) {
const relPath = prefix + sub.name + '/'
const children = walkFolder(sub, relPath)
nodes.push({
name: sub.name,
path: relPath,
isDir: true,
children,
folderId: sub._id
})
}
return nodes
}
const files: SocketFileNode[] = []
const rootFolderId = folders[0]?._id || ''
for (const root of folders) {
files.push(...walkFolder(root, ''))
}
return { files, docPathMap, pathDocMap, fileRefs, rootFolderId }
}
ipcMain.handle('ot:connect', async (_e, projectId: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
try {
overleafSock = new OverleafSocket()
// Relay events to renderer
overleafSock.on('connectionState', (state: string) => {
sendToRenderer('ot:connectionState', state)
})
// otUpdateApplied: server acknowledges our op with a no-op update on
// official Overleaf, but some deployments echo own-source ops instead.
overleafSock.on('serverEvent', (name: string, args: unknown[]) => {
if (name === 'otUpdateApplied') {
const update = args[0] as { doc?: string; op?: unknown[]; v?: number; meta?: { source?: string } } | undefined
const isOwnSource = update?.meta?.source && update.meta.source === overleafSock?.publicId
if (update?.doc && (!update.op || isOwnSource)) {
sendToRenderer('ot:ack', { docId: update.doc })
}
} else if (name === 'otUpdateError') {
console.log(`[ot:error] server rejected update:`, JSON.stringify(args).slice(0, 500))
}
})
overleafSock.on('docRejoined', (docId: string, result: JoinDocResult) => {
sendToRenderer('ot:docRejoined', {
docId,
content: result.docLines.join('\n'),
version: result.version
})
})
// Relay collaborator cursor updates to renderer + track for MCP
overleafSock.on('serverEvent', (name: string, args: unknown[]) => {
if (name === 'clientTracking.clientUpdated') {
const u = args[0] as { id: string; user_id?: string; name?: string; email?: string }
// Skip our own echo — the native caret already marks our position;
// colored overlay cursors are for collaborators only (web behavior)
if (!u.id || u.id !== overleafSock?.publicId) {
sendToRenderer('cursor:remoteUpdate', args[0])
}
// Track online user for MCP (includes ourselves)
if (u.id) {
mcpOnlineUsers.set(u.id, { name: u.name || u.email?.split('@')[0] || 'User', email: u.email })
writeMcpOnlineUsers()
}
} else if (name === 'clientTracking.clientDisconnected') {
sendToRenderer('cursor:remoteDisconnected', args[0])
const clientId = args[0] as string
if (clientId) {
mcpOnlineUsers.delete(clientId)
writeMcpOnlineUsers()
}
} else if (name === 'new-chat-message') {
sendToRenderer('chat:newMessage', args[0])
} else if (
name === 'new-comment' ||
name === 'resolve-thread' ||
name === 'reopen-thread' ||
name === 'delete-thread' ||
name === 'edit-message' ||
name === 'delete-message'
) {
sendToRenderer('comments:event', { type: name, args })
// Re-fetch comment contexts for MCP when comments change
if (name === 'new-comment' || name === 'delete-thread') {
scheduleCommentContextRefresh()
}
}
})
const projectResult = await overleafSock.connect(projectId, overleafSessionCookie)
const { files, docPathMap, pathDocMap, fileRefs, rootFolderId } = walkRootFolder(projectResult.project.rootFolder)
// Set up compilation manager
compilationManager = new CompilationManager(projectId, overleafSessionCookie)
// Set up file sync bridge for bidirectional sync
const tmpDir = compilationManager.dir
fileSyncBridge = new FileSyncBridge(
overleafSock, tmpDir, docPathMap, pathDocMap, fileRefs, mainWindow!,
projectId, overleafSessionCookie, overleafCsrfToken,
async () => {
// Re-fetch CSRF token (rotates over long sessions); cookie may also
// have been refreshed by a re-login in the meantime.
const ok = await refreshCsrfToken()
return ok ? { cookie: overleafSessionCookie, csrfToken: overleafCsrfToken } : null
}
)
await fileSyncBridge.start()
// Start MCP compile watcher (detects compile requests from Claude Code)
startMcpCompileWatcher(tmpDir)
// Write MCP state + config for Claude Code integration
mcpStateDir = tmpDir
mcpProjectId = projectId
mcpCommentContexts = {}
mcpPathDocMap = pathDocMap
await writeMcpState()
// Write .mcp.json so Claude Code auto-discovers the MCP server
// Dev: use source file. Packaged: copy bundled server into the project
// temp dir so .mcp.json never contains a stale App Translocation path.
let mcpServerPath = ''
try {
mcpServerPath = await prepareMcpServerPath(tmpDir)
await writeFile(join(tmpDir, '.mcp.json'), JSON.stringify({
mcpServers: {
lattex: {
type: 'stdio',
command: 'node',
args: [mcpServerPath]
}
}
}, null, 2))
await clearDisabledLattexMcpServer(tmpDir)
} catch (e) {
console.log('[mcp] failed to write MCP config:', e)
}
// Clean up old root-level CLAUDE.md (was incorrectly placed there before)
require('fs').unlink(join(tmpDir, 'CLAUDE.md'), () => {})
// Create claude-workspace/ for Claude Code scratch space (not synced to Overleaf)
mkdirAsync(join(tmpDir, 'claude-workspace'), { recursive: true }).catch(() => {})
// Write .claude/ dir with CLAUDE.md + settings (dotfile dir = excluded from sync)
mkdirAsync(join(tmpDir, '.claude'), { recursive: true }).then(async () => {
const rootDocPath = docPathMap[projectResult.project.rootDoc_id] || 'main.tex'
const texFiles = Object.values(docPathMap).filter((p: string) => p.endsWith('.tex'))
const fileListStr = texFiles.map((p: string) => `- \`${p}\``).join('\n')
// Fetch current user's name for CLAUDE.md
let currentUserName = ''
try {
const userResult = await overleafFetch('/user/settings')
if (userResult.ok && userResult.data) {
const u = userResult.data as { first_name?: string; last_name?: string; email?: string }
currentUserName = [u.first_name, u.last_name].filter(Boolean).join(' ') || u.email || ''
}
} catch { /* non-fatal */ }
const ownerName = [projectResult.project.owner.first_name, projectResult.project.owner.last_name].filter(Boolean).join(' ')
// One guide, two consumers: .claude/CLAUDE.md (Claude Code's native
// location) and AGENTS.md at the project root (the cross-tool standard
// read by Codex, Cursor, Gemini CLI, etc.). AGENTS.md is excluded from
// Overleaf sync alongside CLAUDE.md/.mcp.json.
const agentGuide = `# ${projectResult.project.name} — Overleaf Project
> **IMPORTANT — MANDATORY FIRST STEPS (do this EVERY conversation before ANY edits):**
>
> 1. **Read \`${rootDocPath}\`** to discover the paper structure — identify every \\\\input{} and \\\\include{} file.
> 2. **Read EVERY file** found in step 1, one by one. This means reading the full content of each .tex file listed below. Do NOT skip any file. Do NOT skim. You need to understand the paper's argument, notation, macro usage, and conventions before touching anything.
> 3. **Run \`get_comments\`** to check for reviewer comments, TODOs, or ongoing discussions.
> 4. Only AFTER completing steps 1–3 may you proceed with the user's request.
>
> This is a live Overleaf project — your edits appear to collaborators in real-time. Careless changes to a document you haven't fully read WILL break things and waste collaborators' time.
This is a LaTeX project synced from Overleaf via LatteX. All files here are **bidirectionally synced** — your edits appear on Overleaf in real-time, and vice versa.
${currentUserName ? `\n**You are logged in as: ${currentUserName}** — this is the name that appears on comments and edits. The project owner is ${ownerName}.` : `\n**Project owner**: ${ownerName}`}
## Project Structure
- **Main file**: \`${rootDocPath}\` (this is the root document for compilation)
${fileListStr ? `- **TeX files**:\n${fileListStr}` : ''}
## Rules
- **NEVER edit without reading first.** You must understand what you are changing. Read the relevant file(s) fully before making any modification.
- **Match existing conventions.** Follow the notation, formatting, macro usage, and sectioning style already established in the document. Do NOT impose your own style.
- **Do NOT reorganize, rename labels, or refactor macros** unless explicitly asked.
- **Make targeted edits only.** Modify the specific parts that need changing. Do not rewrite surrounding paragraphs for style.
- **One logical change at a time.** Do not mix unrelated edits in a single pass.
- **Compile after changes.** Use \`compile_latex\` after every edit. If compilation fails, use \`get_compile_errors\` and fix immediately before proceeding.
- **Respond to comments.** When you address a comment, use \`reply_to_comment\` to explain what you changed, then \`resolve_comment\`. Never delete others' comments.
## MCP Tools
You have MCP tools to interact with Overleaf. Use them proactively.
### Comments
- **get_comments**: Read comments. Pass \`file\` to filter, \`include_resolved\` for all.
- **resolve_comment**: Resolve a comment by \`thread_id\`.
- **reopen_comment**: Reopen a resolved comment.
- **reply_to_comment**: Reply to a comment thread.
- **delete_comment**: Permanently delete a comment thread.
### Chat
- **get_chat_messages**: Read project chat history.
- **send_chat_message**: Send a message to project chat.
### Project
- **list_project_files**: List all files with sizes.
- **get_online_users**: See who is currently online in this project.
### Compilation
- **compile_latex**: Trigger LaTeX compilation on Overleaf server. Returns status + error summary.
- **get_compile_errors**: Get parsed errors from last compile (file, line, message).
- **get_compile_warnings**: Get parsed warnings from last compile.
- **get_compile_log**: Get full raw log. Pass \`tail: N\` for last N lines only.
### PDF
- **read_compiled_pdf**: Get the path to the compiled PDF. After calling this, use your **Read** tool on the returned path to visually inspect the PDF. Use the \`pages\` parameter (e.g. \`"1-3"\`) to read specific pages. This lets you verify formatting, figures, tables, and layout.
### Bibliography
- **search_citation**: Search academic papers by title, topic, or author (Semantic Scholar). Returns matching papers with ready-to-use BibTeX entries that can be pasted directly into a \`.bib\` file. **Note:** Without a Semantic Scholar API key configured in LatteX settings, requests will likely be rate-limited (HTTP 429). With a key, the rate limit is 1 request/second.
- **search_openalex**: Search scholarly works via OpenAlex (broader/faster-moving coverage, citation counts, venues). **Citation policy:** OpenAlex metadata lags, so BibTeX is cross-checked against Semantic Scholar — only entries marked "Semantic Scholar ✓" are authoritative. Entries marked "OpenAlex only ⚠" must be verified with a web search (publisher page / arXiv) before citing; very recent papers may be missing from both indexes.
### Workflows
#### Comment Workflow
1. Use \`get_comments\` to see what reviewers have flagged
2. Read the relevant sections to understand context
3. Edit the .tex files to address the feedback
4. Use \`reply_to_comment\` to explain what you changed
5. Use \`resolve_comment\` to mark it as done
#### Compile-Debug Workflow
1. Edit .tex files
2. Use \`compile_latex\` to compile
3. If errors: use \`get_compile_errors\` for details, fix them, recompile
4. If warnings: use \`get_compile_warnings\` to review
5. To check visual output: use \`read_compiled_pdf\`, then Read the returned path with \`pages: "1-3"\`
#### Bibliography Workflow
1. Use \`search_citation\` (Semantic Scholar) or \`search_openalex\` (broader coverage) to find references
2. If the entry is marked "OpenAlex only ⚠", verify it with a web search before using it
3. Copy the BibTeX entry into the \`.bib\` file
4. Use \`\\cite{key}\` in the \`.tex\` file
5. Compile to verify the citation renders correctly
## Workspace
The \`claude-workspace/\` directory is your private scratch space. It is **not synced to Overleaf** — use it freely for:
- **Notes and plans** — draft outlines, track TODOs, keep analysis notes
- **Experiments** — test LaTeX snippets, try alternative formulations, prototype figures
- **Scripts** — helper scripts for data processing, bibliography management, etc.
**Important**: Always ask the user before running experiments or creating files in \`claude-workspace/\`. This directory persists across sessions for the same project.
## Agent Setup (MCP)
The tools above come from LatteX's MCP server (standard stdio MCP — works with any MCP-capable agent):
- **Claude Code**: auto-configured. \`.mcp.json\` in this directory registers the \`lattex\` server and \`.claude/settings.json\` pre-approves its tools. Just run \`claude\`.
- **Codex CLI**: register the server once for this project:
\`\`\`
codex mcp add lattex -- node "${mcpServerPath}"
\`\`\`
The path is project-specific — re-run this when switching projects. Approve \`lattex\` tool calls when Codex prompts.
- **Any other MCP client**: stdio transport, command \`node "${mcpServerPath}"\`.
`
await writeFile(join(tmpDir, '.claude', 'CLAUDE.md'), agentGuide)
await writeFile(join(tmpDir, 'AGENTS.md'), agentGuide)
await writeFile(join(tmpDir, '.claude', 'settings.json'), JSON.stringify({
permissions: {
allow: [
'mcp__lattex__get_comments',
'mcp__lattex__resolve_comment',
'mcp__lattex__reopen_comment',
'mcp__lattex__reply_to_comment',
'mcp__lattex__delete_comment',
'mcp__lattex__get_chat_messages',
'mcp__lattex__send_chat_message',
'mcp__lattex__list_project_files',
'mcp__lattex__get_online_users',
'mcp__lattex__compile_latex',
'mcp__lattex__get_compile_errors',
'mcp__lattex__get_compile_warnings',
'mcp__lattex__get_compile_log',
'mcp__lattex__read_compiled_pdf',
'mcp__lattex__search_citation',
'mcp__lattex__search_openalex'
]
}
}, null, 2))
}).catch(() => {})
// Fetch resolved thread IDs immediately (fast REST call) so editor highlights
// don't flash resolved comments while waiting for background fetch
overleafFetch(`/project/${projectId}/threads`).then((threadResult) => {
if (threadResult.ok && threadResult.data) {
const threads = threadResult.data as Record<string, { resolved?: boolean }>
const resolvedIds: string[] = []
for (const [tid, t] of Object.entries(threads)) {
if (t.resolved) resolvedIds.push(tid)
}
sendToRenderer('comments:initThreads', { threads: threadResult.data, resolvedIds })
}
}).catch(() => {})
// Fetch comment contexts from all docs in background (slower — joins each doc)
setTimeout(async () => {
if (!overleafSock?.projectData) return
const { docPathMap: dp } = walkRootFolder(overleafSock.projectData.project.rootFolder)
const contexts: Record<string, { file: string; text: string; pos: number }> = {}
for (const [did, rp] of Object.entries(dp)) {
try {
const result = await overleafSock.joinDoc(did)
if (result.ranges?.comments) {
for (const c of result.ranges.comments) {
if (c.op?.t) contexts[c.op.t] = { file: rp, text: c.op.c || '', pos: c.op.p || 0 }
}
}
// Don't leaveDoc — bridge keeps all docs joined
} catch { /* ignore */ }
}
mcpCommentContexts = contexts
writeMcpState()
sendToRenderer('comments:initContexts', { contexts })
}, 3000)
// Check for cached PDF from previous compile
const buildDir = join(tmpDir, '.build')
const cachedPdf = join(buildDir, 'output.pdf')
let cachedPdfPath: string | undefined
try {
const stat = await require('fs').promises.stat(cachedPdf)
if (stat.size > 0) cachedPdfPath = cachedPdf
} catch { /* no cached PDF */ }
return {
success: true,
files,
project: {
name: projectResult.project.name,
rootDocId: projectResult.project.rootDoc_id
},
docPathMap,
pathDocMap,
fileRefs,
rootFolderId,
syncDir: tmpDir,
cachedPdfPath
}
} catch (e) {
console.log('[ot:connect] error:', e)
return { success: false, message: String(e) }
}
})
ipcMain.handle('ot:disconnect', async () => {
// Clean up MCP state file + compile watcher
stopMcpCompileWatcher()
if (mcpStateDir) {
unlink(join(mcpStateDir, '.lattex-mcp.json')).catch(() => {})
unlink(join(mcpStateDir, '.lattex-online-users.json')).catch(() => {})
}
mcpStateDir = ''
mcpProjectId = ''
mcpCommentContexts = {}
mcpOnlineUsers.clear()
await fileSyncBridge?.stop()
fileSyncBridge = null
overleafSock?.disconnect()
overleafSock = null
await compilationManager?.cleanup()
compilationManager = null
})
// Track per-doc event handlers for cleanup on leaveDoc
const docEventHandlers = new Map<string, (name: string, args: unknown[]) => void>()
function attachRendererDoc(docId: string): void {
if (!overleafSock) return
// Notify bridge that editor is taking over this doc
fileSyncBridge?.addEditorDoc(docId)
// Remove existing handler if re-attaching
const existingHandler = docEventHandlers.get(docId)
if (existingHandler) overleafSock.removeListener('serverEvent', existingHandler)
// Set up relay for remote ops on this doc
const handler = (name: string, args: unknown[]) => {
if (name === 'otUpdateApplied') {
const update = args[0] as { doc?: string; op?: unknown[]; v?: number; meta?: { source?: string } } | undefined
const isOwnSource = update?.meta?.source && update.meta.source === overleafSock?.publicId
if (update?.doc === docId && update.op && !isOwnSource) {
sendToRenderer('ot:remoteOp', {
docId: update.doc,
ops: update.op,
version: update.v
})
}
}
}
docEventHandlers.set(docId, handler)
overleafSock.on('serverEvent', handler)
}
ipcMain.handle('ot:joinDoc', async (_e, docId: string) => {
if (!overleafSock) return { success: false, message: 'not_connected' }
try {
const result = await overleafSock.joinDoc(docId)
const content = (result.docLines || []).join('\n')
// Update compilation manager with doc content
if (compilationManager && overleafSock.projectData) {
const { docPathMap } = walkRootFolder(overleafSock.projectData.project.rootFolder)
const relPath = docPathMap[docId]
if (relPath) {
compilationManager.setDocContent(relPath, content)
}
}
attachRendererDoc(docId)
return {
success: true,
content,
version: result.version,
ranges: result.ranges
}
} catch (e) {
console.log('[ot:joinDoc] error:', e)
return { success: false, message: String(e) }
}
})
ipcMain.handle('ot:attachDoc', async (_e, docId: string) => {
attachRendererDoc(docId)
})
ipcMain.handle('ot:leaveDoc', async (_e, docId: string) => {
if (!overleafSock) return
try {
// Remove event handler for this doc
const handler = docEventHandlers.get(docId)
if (handler) {
overleafSock.removeListener('serverEvent', handler)
docEventHandlers.delete(docId)
}
// Bridge takes back OT ownership — do NOT leaveDoc on the socket,
// the bridge keeps the doc joined for sync
fileSyncBridge?.removeEditorDoc(docId)
} catch (e) {
console.log('[ot:leaveDoc] error:', e)
}
})
ipcMain.handle('ot:sendOp', async (_e, docId: string, ops: unknown[], version: number, hash: string) => {
if (!overleafSock) return
try {
await overleafSock.applyOtUpdate(docId, ops, version, hash)
} catch (e) {
console.log('[ot:sendOp] error:', e)
}
})
// Renderer → bridge: editor content changed (for disk sync)
ipcMain.handle('sync:contentChanged', async (_e, docId: string, content: string) => {
fileSyncBridge?.onEditorContentChanged(docId, content)
})
// Renderer ← bridge: all synced doc contents (for project-wide autocomplete)
ipcMain.handle('sync:getAllDocContents', async () => {
return fileSyncBridge ? fileSyncBridge.getAllDocContents() : []
})
// Official metadata endpoint: labels + package command snippets per doc
ipcMain.handle('overleaf:getMetadata', async (_e, projectId: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch(`/project/${projectId}/metadata`)
if (!result.ok) return { success: false, message: `HTTP ${result.status}` }
return { success: true, data: result.data }
})
// ── Cursor Tracking ────────────────────────────────────────────
ipcMain.handle('cursor:update', async (_e, docId: string, row: number, column: number) => {
overleafSock?.updateCursorPosition(docId, row, column)
})
ipcMain.handle('cursor:getConnectedUsers', async () => {
if (!overleafSock) return []
try {
const users = await overleafSock.getConnectedUsers()
// Seed MCP online users map (includes ourselves)
mcpOnlineUsers.clear()
for (const raw of users) {
const u = raw as { client_id?: string; first_name?: string; last_name?: string; email?: string }
if (u.client_id) {
const name = [u.first_name, u.last_name].filter(Boolean).join(' ') || u.email?.split('@')[0] || 'User'
mcpOnlineUsers.set(u.client_id, { name, email: u.email })
}
}
writeMcpOnlineUsers()
// Exclude our own client — no colored overlay cursor for ourselves
return users.filter((raw) => {
const u = raw as { client_id?: string }
return !u.client_id || u.client_id !== overleafSock?.publicId
})
} catch (e) {
console.log('[cursor:getConnectedUsers] error:', e)
return []
}
})
// ── Chat ───────────────────────────────────────────────────────
ipcMain.handle('chat:getMessages', async (_e, projectId: string, limit?: number) => {
if (!overleafSessionCookie) return { success: false, messages: [] }
const result = await overleafFetch(`/project/${projectId}/messages?limit=${limit || 50}`)
if (!result.ok) return { success: false, messages: [] }
return { success: true, messages: result.data }
})
ipcMain.handle('chat:sendMessage', async (_e, projectId: string, content: string) => {
if (!overleafSessionCookie) return { success: false }
const result = await overleafFetch(`/project/${projectId}/messages`, {
method: 'POST',
body: JSON.stringify({ content })
})
return { success: result.ok }
})
ipcMain.handle('overleaf:listProjects', async () => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
// POST /api/project returns full project data (lastUpdated, owner, etc.)
const result = await overleafFetch('/api/project', {
method: 'POST',
body: JSON.stringify({
filters: {},
page: { size: 200 },
sort: { by: 'lastUpdated', order: 'desc' }
})
})
if (!result.ok) return { success: false, message: `HTTP ${result.status}` }
const data = result.data as { totalSize?: number; projects?: unknown[] }
const projects = (data.projects || []) as Array<{
id?: string; _id?: string; name: string; lastUpdated: string
owner?: { firstName: string; lastName: string; email?: string }
lastUpdatedBy?: { firstName: string; lastName: string; email?: string } | null
accessLevel?: string
source?: string
archived?: boolean
trashed?: boolean
}>
return {
success: true,
projects: projects.map((p) => ({
id: p.id || p._id || '',
name: p.name,
lastUpdated: p.lastUpdated,
owner: p.owner ? { firstName: p.owner.firstName, lastName: p.owner.lastName, email: p.owner.email } : undefined,
lastUpdatedBy: p.lastUpdatedBy ? { firstName: p.lastUpdatedBy.firstName, lastName: p.lastUpdatedBy.lastName } : null,
accessLevel: p.accessLevel || 'unknown',
source: p.source || '',
archived: !!p.archived,
trashed: !!p.trashed
}))
}
})
ipcMain.handle('overleaf:createProject', async (_e, name: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch('/project/new', {
method: 'POST',
body: JSON.stringify({ projectName: name })
})
if (!result.ok) return { success: false, message: `HTTP ${result.status}` }
const data = result.data as { project_id?: string; _id?: string }
return { success: true, projectId: data.project_id || data._id }
})
ipcMain.handle('overleaf:uploadProject', async () => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const { canceled, filePaths } = await dialog.showOpenDialog({
title: 'Upload Project (.zip)',
filters: [{ name: 'ZIP Archives', extensions: ['zip'] }],
properties: ['openFile']
})
if (canceled || filePaths.length === 0) return { success: false, message: 'cancelled' }
const zipPath = filePaths[0]
const zipData = await readFile(zipPath)
const fileName = basename(zipPath)
// Multipart upload
const boundary = '----FormBoundary' + Math.random().toString(36).slice(2)
const header = `--${boundary}\r\nContent-Disposition: form-data; name="qqfile"; filename="${fileName}"\r\nContent-Type: application/zip\r\n\r\n`
const footer = `\r\n--${boundary}--\r\n`
const headerBuf = Buffer.from(header)
const footerBuf = Buffer.from(footer)
const body = Buffer.concat([headerBuf, zipData, footerBuf])
return new Promise((resolve) => {
const req = net.request({
method: 'POST',
url: 'https://www.overleaf.com/project/new/upload'
})
req.setHeader('Cookie', overleafSessionCookie)
req.setHeader('Content-Type', `multipart/form-data; boundary=${boundary}`)
req.setHeader('User-Agent', 'Mozilla/5.0')
if (overleafCsrfToken) req.setHeader('x-csrf-token', overleafCsrfToken)
let resBody = ''
req.on('response', (res) => {
res.on('data', (chunk) => { resBody += chunk.toString() })
res.on('end', () => {
try {
const data = JSON.parse(resBody) as { success?: boolean; project_id?: string }
if (data.success !== false && data.project_id) {
resolve({ success: true, projectId: data.project_id })
} else {
resolve({ success: false, message: 'Upload failed' })
}
} catch {
resolve({ success: false, message: 'Invalid response' })
}
})
})
req.on('error', (e) => resolve({ success: false, message: String(e) }))
req.write(body)
req.end()
})
})
// ── Project Dashboard Operations (official Overleaf endpoints) ──
//
// Endpoints mirror services/web/frontend/js/features/project-list/util/api.ts
// in the Overleaf source (the web dashboard's own API client).
ipcMain.handle('overleaf:getTags', async () => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch('/tag')
if (!result.ok) return { success: false, message: `HTTP ${result.status}` }
return { success: true, tags: result.data }
})
ipcMain.handle('overleaf:createTag', async (_e, name: string, color?: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch('/tag', {
method: 'POST',
body: JSON.stringify({ name, color })
})
if (!result.ok) return { success: false, message: `HTTP ${result.status}` }
return { success: true, tag: result.data }
})
ipcMain.handle('overleaf:editTag', async (_e, tagId: string, name: string, color?: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch(`/tag/${tagId}/edit`, {
method: 'POST',
body: JSON.stringify({ name, color })
})
return { success: result.ok, message: result.ok ? '' : `HTTP ${result.status}` }
})
ipcMain.handle('overleaf:deleteTag', async (_e, tagId: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch(`/tag/${tagId}`, { method: 'DELETE' })
return { success: result.ok, message: result.ok ? '' : `HTTP ${result.status}` }
})
ipcMain.handle('overleaf:addProjectsToTag', async (_e, tagId: string, projectIds: string[]) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch(`/tag/${tagId}/projects`, {
method: 'POST',
body: JSON.stringify({ projectIds })
})
return { success: result.ok, message: result.ok ? '' : `HTTP ${result.status}` }
})
ipcMain.handle('overleaf:removeProjectsFromTag', async (_e, tagId: string, projectIds: string[]) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch(`/tag/${tagId}/projects/remove`, {
method: 'POST',
body: JSON.stringify({ projectIds })
})
return { success: result.ok, message: result.ok ? '' : `HTTP ${result.status}` }
})
// Archive / trash state transitions. Paths match the official router
// (case-sensitive: /Project/:id/archive vs /project/:id/trash).
ipcMain.handle('overleaf:setProjectState', async (_e, projectId: string, action: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const routes: Record<string, { method: string; path: string }> = {
archive: { method: 'POST', path: `/project/${projectId}/archive` },
unarchive: { method: 'DELETE', path: `/project/${projectId}/archive` },
trash: { method: 'POST', path: `/project/${projectId}/trash` },
untrash: { method: 'DELETE', path: `/project/${projectId}/trash` },
delete: { method: 'DELETE', path: `/project/${projectId}` },
leave: { method: 'POST', path: `/project/${projectId}/leave` }
}
const route = routes[action]
if (!route) return { success: false, message: `unknown action: ${action}` }
const result = await overleafFetch(route.path, { method: route.method, body: '{}' })
return { success: result.ok, message: result.ok ? '' : `HTTP ${result.status}` }
})
ipcMain.handle('overleaf:renameProject', async (_e, projectId: string, newName: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch(`/project/${projectId}/rename`, {
method: 'POST',
body: JSON.stringify({ newProjectName: newName })
})
return { success: result.ok, message: result.ok ? '' : `HTTP ${result.status}` }
})
ipcMain.handle('overleaf:cloneProject', async (_e, projectId: string, projectName: string, tags?: string[]) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch(`/project/${projectId}/clone`, {
method: 'POST',
body: JSON.stringify({ projectName, tags: (tags || []).map((id) => ({ id })) })
})
if (!result.ok) return { success: false, message: `HTTP ${result.status}` }
const data = result.data as { project_id?: string }
return { success: true, projectId: data.project_id }
})
ipcMain.handle('overleaf:downloadProjectZip', async (_e, projectIds: string[], suggestedName: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
if (projectIds.length === 0) return { success: false, message: 'no projects' }
const { canceled, filePath } = await dialog.showSaveDialog({
title: 'Download Project',
defaultPath: `${suggestedName || 'projects'}.zip`,
filters: [{ name: 'ZIP Archives', extensions: ['zip'] }]
})
if (canceled || !filePath) return { success: false, message: 'cancelled' }
// Official download routes: single /project/:id/download/zip,
// multi /project/download/zip?project_ids=a,b
const url = projectIds.length === 1
? `https://www.overleaf.com/project/${projectIds[0]}/download/zip`
: `https://www.overleaf.com/project/download/zip?project_ids=${projectIds.join(',')}`
try {
const data = await fetchBinary(url, overleafSessionCookie)
await writeFile(filePath, Buffer.from(data))
return { success: true, path: filePath }
} catch (e) {
return { success: false, message: String(e) }
}
})
// ── File Operations via Overleaf REST API ──────────────────────
ipcMain.handle('overleaf:renameEntity', async (_e, projectId: string, entityType: string, entityId: string, newName: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch(`/project/${projectId}/${entityType}/${entityId}/rename`, {
method: 'POST',
body: JSON.stringify({ name: newName })
})
return { success: result.ok, message: result.ok ? '' : `HTTP ${result.status}` }
})
ipcMain.handle('overleaf:deleteEntity', async (_e, projectId: string, entityType: string, entityId: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch(`/project/${projectId}/${entityType}/${entityId}`, {
method: 'DELETE'
})
return { success: result.ok, message: result.ok ? '' : `HTTP ${result.status}` }
})
ipcMain.handle('overleaf:createDoc', async (_e, projectId: string, parentFolderId: string, name: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch(`/project/${projectId}/doc`, {
method: 'POST',
body: JSON.stringify({ name, parent_folder_id: parentFolderId })
})
return { success: result.ok, data: result.data, message: result.ok ? '' : `HTTP ${result.status}` }
})
ipcMain.handle('overleaf:createFolder', async (_e, projectId: string, parentFolderId: string, name: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
const result = await overleafFetch(`/project/${projectId}/folder`, {
method: 'POST',
body: JSON.stringify({ name, parent_folder_id: parentFolderId })
})
return { success: result.ok, data: result.data, message: result.ok ? '' : `HTTP ${result.status}` }
})
// ── Upload file to project (binary or text) ───────────────────
ipcMain.handle('project:uploadFile', async (_e, projectId: string, folderId: string, filePath: string, fileName: string) => {
if (!overleafSessionCookie) return { success: false, message: 'not_logged_in' }
try {
const fileData = await readFile(filePath)
const ext = fileName.split('.').pop()?.toLowerCase() || ''
const mimeMap: Record<string, string> = {
png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif',
svg: 'image/svg+xml', pdf: 'application/pdf', eps: 'application/postscript',
zip: 'application/zip', bmp: 'image/bmp', tiff: 'image/tiff',
tex: 'text/x-tex', bib: 'text/x-bibtex', txt: 'text/plain', csv: 'text/csv',
sty: 'text/x-tex', cls: 'text/x-tex', md: 'text/markdown',
}
const mime = mimeMap[ext] || 'application/octet-stream'
const boundary = '----FormBoundary' + Math.random().toString(36).slice(2)
// Build multipart body matching Overleaf's expected format:
// 1. "name" text field (required — server reads filename from req.body.name)
// 2. "type" text field
// 3. "qqfile" file field (fieldName must be "qqfile" for multer)
const parts: Buffer[] = []
// name field
parts.push(Buffer.from(`--${boundary}\r\nContent-Disposition: form-data; name="name"\r\n\r\n${fileName}\r\n`))
// type field
parts.push(Buffer.from(`--${boundary}\r\nContent-Disposition: form-data; name="type"\r\n\r\n${mime}\r\n`))
// qqfile field
parts.push(Buffer.from(`--${boundary}\r\nContent-Disposition: form-data; name="qqfile"; filename="${fileName}"\r\nContent-Type: ${mime}\r\n\r\n`))
parts.push(fileData)
parts.push(Buffer.from(`\r\n--${boundary}--\r\n`))
const body = Buffer.concat(parts)
return new Promise<{ success: boolean; message?: string }>((resolve) => {
const req = net.request({
method: 'POST',
url: `https://www.overleaf.com/project/${projectId}/upload?folder_id=${folderId}`
})
req.setHeader('Cookie', overleafSessionCookie)
req.setHeader('Content-Type', `multipart/form-data; boundary=${boundary}`)
req.setHeader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36')
req.setHeader('Accept', 'application/json')
req.setHeader('Referer', `https://www.overleaf.com/project/${projectId}`)
req.setHeader('Origin', 'https://www.overleaf.com')
if (overleafCsrfToken) req.setHeader('x-csrf-token', overleafCsrfToken)
let resBody = ''
req.on('response', (res) => {
res.on('data', (chunk: Buffer) => { resBody += chunk.toString() })
res.on('end', () => {
console.log('[upload] status:', res.statusCode, 'body:', resBody.slice(0, 300))
try {
const data = JSON.parse(resBody)
if (data.success !== false && !data.error) {
resolve({ success: true })
} else {
resolve({ success: false, message: data.error || 'Upload failed' })
}
} catch {
resolve({ success: false, message: `HTTP ${res.statusCode}: ${resBody.slice(0, 200)}` })
}
})
})
req.on('error', (e) => resolve({ success: false, message: String(e) }))
req.write(body)
req.end()
})
} catch (e) {
return { success: false, message: String(e) }
}
})
// Fetch comment ranges from ALL docs (for ReviewPanel)
ipcMain.handle('ot:fetchAllCommentContexts', async () => {
if (!overleafSock?.projectData) return { success: false }
const { docPathMap } = walkRootFolder(overleafSock.projectData.project.rootFolder)
const contexts: Record<string, { file: string; text: string; pos: number }> = {}
for (const [docId, relPath] of Object.entries(docPathMap)) {
try {
const alreadyJoined = docEventHandlers.has(docId)
const result = await overleafSock.joinDoc(docId)
if (result.ranges?.comments) {
for (const c of result.ranges.comments) {
if (c.op?.t) {
contexts[c.op.t] = { file: relPath, text: c.op.c || '', pos: c.op.p || 0 }
}
}
}
if (!alreadyJoined) {
await overleafSock.leaveDoc(docId)
}
} catch (e) {
console.log(`[fetchCommentContexts] failed for ${relPath}:`, e)
}
}
// Update MCP state with fresh comment contexts
mcpCommentContexts = contexts
writeMcpState()
return { success: true, contexts }
})
ipcMain.handle('overleaf:socketCompile', async (_e, mainTexRelPath: string) => {
if (!compilationManager || !overleafSock?.projectData) {
return { success: false, log: 'No compilation manager or not connected', pdfPath: '' }
}
// latexmk writes its output into the synced dir root (-outdir) — tell the
// bridge so the produced PDF is never uploaded to Overleaf as content.
fileSyncBridge?.addCompileOutput(basename(mainTexRelPath, '.tex') + '.pdf')
// Bridge already keeps all docs synced to disk. Sync content to compilation manager.
if (fileSyncBridge) {
for (const { path, content } of fileSyncBridge.getAllDocContents()) {
compilationManager.setDocContent(path, content)
}
} else {
// Fallback: fetch docs from socket if bridge isn't available
const { docPathMap } = walkRootFolder(overleafSock.projectData.project.rootFolder)
const allDocIds = Object.keys(docPathMap)
for (const docId of allDocIds) {
const relPath = docPathMap[docId]
if (docEventHandlers.has(docId) && compilationManager.hasDoc(relPath)) continue
try {
const alreadyJoined = docEventHandlers.has(docId)
const result = await overleafSock.joinDoc(docId)
const content = (result.docLines || []).join('\n')
compilationManager.setDocContent(relPath, content)
if (!alreadyJoined) {
await overleafSock.leaveDoc(docId)
}
} catch (e) {
console.log(`[socketCompile] failed to fetch doc ${relPath}:`, e)
}
}
}
// Download all binary files (images, .bst, etc.)
const fileRefs = fileSyncBridge
? fileSyncBridge.getFileRefs()
: walkRootFolder(overleafSock.projectData.project.rootFolder).fileRefs
await compilationManager.syncBinaries(fileRefs)
return compilationManager.compile(mainTexRelPath, (data) => {
sendToRenderer('latex:log', data)
})
})
// Server-side compile via Overleaf's CLSI (shared by IPC handler + MCP compile watcher)
let compileInProgress: Promise<{ success: boolean; log: string; pdfPath: string }> | null = null
async function doServerCompile(rootDocId?: string): Promise<{ success: boolean; log: string; pdfPath: string }> {
// Prevent concurrent compiles — wait for existing one if already in progress
if (compileInProgress) {
console.log('[compile] compile already in progress, waiting...')
return compileInProgress
}
const promise = doServerCompileImpl(rootDocId)
compileInProgress = promise
try {
return await promise
} finally {
compileInProgress = null
}
}
async function doServerCompileImpl(rootDocId?: string): Promise<{ success: boolean; log: string; pdfPath: string }> {
if (!overleafSessionCookie || !overleafSock?.projectData) {
return { success: false, log: 'Not connected', pdfPath: '' }
}
const projectId = overleafSock.projectData.project._id
const effectiveRootDocId = rootDocId || overleafSock.projectData.project.rootDoc_id || null
// Resolve rootResourcePath (file path of root doc) — matches Overleaf web client
let rootResourcePath: string | undefined
if (effectiveRootDocId) {
const { docPathMap } = walkRootFolder(overleafSock.projectData.project.rootFolder)
rootResourcePath = docPathMap[effectiveRootDocId]
}
try {
sendToRenderer('latex:log', 'Compiling on Overleaf server...\n')
// Flush in-memory OT changes to database so CLSI sees latest content
try {
await overleafFetch(`/project/${projectId}/flush`, { method: 'POST' })
} catch (e) {
console.log('[compile] flush failed (non-fatal):', e)
}
const compileBody = JSON.stringify({
rootDoc_id: effectiveRootDocId,
...(rootResourcePath && { rootResourcePath }),
draft: false,
check: 'silent',
incrementalCompilesEnabled: true,
stopOnFirstError: false
})
console.log(`[compile] starting server compile for project ${projectId}`)
const compileResult = await overleafFetch(
`/project/${projectId}/compile?auto_compile=false`,
{ method: 'POST', body: compileBody }
)
console.log(`[compile] compile response: ok=${compileResult.ok} status=${compileResult.status}`)
if (!compileResult.ok) {
sendToRenderer('latex:log', `Compile failed: HTTP ${compileResult.status}\n`)
return { success: false, log: '', pdfPath: '' }
}
const data = compileResult.data as any
// Diagnostic: log compile status and available output files
const outputPaths = (data.outputFiles || []).map((f: any) => f.path)
sendToRenderer('latex:log', `[CLSI status=${data.status}, outputFiles=[${outputPaths.join(', ')}]]\n`)
// Build query params for fetching output files (matches Overleaf web client)
const params = new URLSearchParams()
if (data.compileGroup) params.set('compileGroup', data.compileGroup)
if (data.clsiServerId) params.set('clsiserverid', data.clsiServerId)
const buildOutputUrl = (file: { url: string; build?: string }) => {
const base = (file.build && data.pdfDownloadDomain)
? `${data.pdfDownloadDomain}${file.url}`
: `https://www.overleaf.com${file.url}`
return `${base}?${params}`
}
// Build output dir — separate from synced project dir to avoid re-uploading artifacts
const syncDir = compilationManager?.dir || join(require('os').tmpdir(), `lattex-${projectId}`)
const buildDir = join(syncDir, '.build')
await mkdirAsync(buildDir, { recursive: true })
// Fetch compile log
const logFile = (data.outputFiles || []).find((f: any) => f.path === 'output.log')
if (logFile) {
try {
const logContent = await fetchBinary(buildOutputUrl(logFile), overleafSessionCookie)
const logText = Buffer.from(logContent).toString('utf-8')
sendToRenderer('latex:log', logText)
// Write log for MCP server to read (avoids redundant compile API call)
writeFile(join(syncDir, '.lattex-compile-log'), logText).catch(() => {})
} catch (e) {
sendToRenderer('latex:log', `[log fetch failed: ${e}]\n`)
}
}
// Grab synctex.gz (needed for PDF↔source navigation)
const synctexFile = (data.outputFiles || []).find((f: any) => f.path === 'output.synctex.gz')
if (synctexFile) {
// CDN returns 503 for non-PDF files; use Overleaf web proxy instead
const synctexUrl = `https://www.overleaf.com${synctexFile.url}?${params}`
try {
const d = await fetchBinary(synctexUrl, overleafSessionCookie)
await writeFile(join(buildDir, 'output.synctex.gz'), Buffer.from(d))
console.log(`[compile] synctex.gz saved (${d.byteLength} bytes)`)
} catch (e) {
console.log(`[compile] synctex.gz download failed: ${e}`)
}
} else {
console.log('[compile] no synctex.gz in compile output')
}
// Download PDF — first check outputFiles, then try direct URL from build ID
let pdfPath = ''
const pdfFile = (data.outputFiles || []).find((f: any) => f.path === 'output.pdf')
if (pdfFile) {
try {
const pdfUrl = buildOutputUrl(pdfFile)
console.log(`[compile] downloading PDF from ${pdfUrl.slice(0, 100)}...`)
const pdfData = await fetchBinary(pdfUrl, overleafSessionCookie)
console.log(`[compile] PDF downloaded (${pdfData.byteLength} bytes)`)
const pdfDest = join(buildDir, 'output.pdf')
await writeFile(pdfDest, Buffer.from(pdfData))
pdfPath = pdfDest
} catch (e) {
console.log(`[compile] PDF direct download failed: ${e}`)
sendToRenderer('latex:log', `\n[PDF download failed: ${e}]\n`)
}
}
// If output.pdf not in outputFiles, try constructing URL from another file's build ID
// (CLSI may have produced the PDF but not listed it — output.pdfxref proves this)
if (!pdfPath && data.outputFiles?.length > 0) {
const refFile = data.outputFiles.find((f: any) => f.build)
if (refFile) {
const pdfUrl = refFile.url.replace(/\/output\/[^/]+$/, '/output/output.pdf')
try {
const pdfData = await fetchBinary(buildOutputUrl({ url: pdfUrl, build: refFile.build }), overleafSessionCookie)
if (pdfData.byteLength > 0) {
const pdfDest = join(buildDir, 'output.pdf')
await writeFile(pdfDest, Buffer.from(pdfData))
pdfPath = pdfDest
sendToRenderer('latex:log', `\n[PDF retrieved via direct URL (${(pdfData.byteLength / 1024).toFixed(0)} KB)]\n`)
}
} catch {
// PDF truly not available on CLSI
}
}
}
if (!pdfPath && data.status !== 'success') {
sendToRenderer('latex:log', `\n[Compile status: ${data.status} — PDF not available]\n`)
}
return { success: data.status === 'success', log: '', pdfPath }
} catch (e) {
const msg = `Server compile error: ${e}`
sendToRenderer('latex:log', msg + '\n')
return { success: false, log: msg, pdfPath: '' }
}
}
ipcMain.handle('overleaf:serverCompile', async (_e, rootDocId?: string) => {
return doServerCompile(rootDocId)
})
// Watch for MCP compile requests (file-based signal from MCP server process)
let mcpCompileWatcher: ReturnType<typeof import('fs').watchFile> | null = null
let mcpCompileActive = false
function startMcpCompileWatcher(syncDir: string) {
const requestPath = join(syncDir, '.lattex-compile-request')
const resultPath = join(syncDir, '.lattex-compile-result')
// Poll for the request file every 300ms
const { watchFile, unwatchFile } = require('fs')
watchFile(requestPath, { interval: 300 }, async (curr: { size: number }) => {
if (curr.size === 0 || mcpCompileActive) return
mcpCompileActive = true
try {
const reqData = JSON.parse(await readFile(requestPath, 'utf-8'))
await unlink(requestPath).catch(() => {})
console.log('[mcp-compile] compile request received:', reqData.requestId)
// Notify renderer: compile started
sendToRenderer('compile:mcpStarted', null)
// Resolve main_file to rootDocId if provided
let rootDocId: string | undefined
if (reqData.mainFile && mcpPathDocMap[reqData.mainFile]) {
rootDocId = mcpPathDocMap[reqData.mainFile]
}
const result = await doServerCompile(rootDocId)
// Notify renderer: compile finished (renderer will update PDF + compiling state)
sendToRenderer('compile:mcpFinished', {
success: result.success,
pdfPath: result.pdfPath
})
// Write result for MCP server to read
await writeFile(resultPath, JSON.stringify({
requestId: reqData.requestId,
success: result.success,
pdfPath: result.pdfPath,
status: result.success ? 'success' : 'failure'
}))
console.log('[mcp-compile] compile result written:', result.success)
} catch (e) {
console.log('[mcp-compile] error handling compile request:', e)
// Write error result so MCP doesn't hang
await writeFile(resultPath, JSON.stringify({
success: false,
status: 'error',
error: String(e)
})).catch(() => {})
sendToRenderer('compile:mcpFinished', { success: false, pdfPath: '' })
} finally {
mcpCompileActive = false
}
})
mcpCompileWatcher = { requestPath } as any
console.log('[mcp-compile] watcher started for', requestPath)
}
function stopMcpCompileWatcher() {
if (mcpCompileWatcher) {
const { unwatchFile } = require('fs')
unwatchFile((mcpCompileWatcher as any).requestPath)
mcpCompileWatcher = null
}
}
/** Fetch a binary resource. Cookie is optional — CDN URLs use build ID for auth. */
function fetchBinary(url: string, cookie?: string): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => {
const req = net.request(url)
if (cookie) req.setHeader('Cookie', cookie)
const chunks: Buffer[] = []
req.on('response', (res) => {
if (res.statusCode && res.statusCode >= 400) {
reject(new Error(`HTTP ${res.statusCode}`))
return
}
res.on('data', (chunk) => chunks.push(chunk as Buffer))
res.on('end', () => resolve(Buffer.concat(chunks).buffer))
})
req.on('error', reject)
req.end()
})
}
/// ── Shell: open external ─────────────────────────────────────────
ipcMain.handle('shell:openExternal', async (_e, url: string) => {
await shell.openExternal(url)
})
ipcMain.handle('shell:openPath', async (_e, targetPath: string) => {
return shell.openPath(targetPath)
})
ipcMain.handle('shell:showInFinder', async (_e, path: string) => {
shell.showItemInFolder(path)
})
ipcMain.handle('shell:savePdf', async (_e, sourcePath: string) => {
const { canceled, filePath } = await dialog.showSaveDialog({
title: 'Save PDF',
defaultPath: basename(sourcePath),
filters: [{ name: 'PDF', extensions: ['pdf'] }]
})
if (canceled || !filePath) return { success: false }
const { copyFile } = await import('fs/promises')
await copyFile(sourcePath, filePath)
return { success: true, path: filePath }
})
// ── App Lifecycle ────────────────────────────────────────────────
app.whenReady().then(async () => {
createWindow()
sessionLoadPromise = loadOverleafSession()
})
app.on('window-all-closed', () => {
mainWindow = null
stopMcpCompileWatcher()
for (const inst of ptyInstances.values()) inst.kill()
ptyInstances.clear()
fileSyncBridge?.stop()
fileSyncBridge = null
overleafSock?.disconnect()
compilationManager?.cleanup()
app.quit()
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
|