Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
1054 lines (951 loc) · 46.6 KB
/
Copy pathserver.js
File metadata and controls
1054 lines (951 loc) · 46.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
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
'use strict';
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const express = require('express');
const db = require('./lib/db');
const git = require('./lib/git');
const rdf = require('./lib/rdf');
const agent = require('./lib/agent');
const V = require('./lib/validator');
const inference = require('./lib/inference');
const recommend = require('./lib/recommend');
const embeddings = require('./lib/embeddings');
const askLib = require('./lib/ask');
const updater = require('./lib/updater');
const importer = require('./lib/importer');
const bus = require('./lib/bus');
const { createMcpHandler } = require('./mcp/http');
// ---------- 图谱变更总线:db 写函数统一包装,任一写入即时广播 ----------
for (const fn of ['addEntity', 'updateEntity', 'deleteEntity', 'addRelation', 'updateRelation', 'deleteRelation', 'addAlias', 'removeAlias', 'deleteEntityImage', 'applyAgentOps', 'undoLast', 'logRestore']) {
const orig = db[fn];
if (typeof orig === 'function') {
db[fn] = function (...args) {
const r = orig.apply(this, args);
bus.emit('graph-changed', { via: fn });
return r;
};
}
}
function mcpServerVersion() {
try { return require('./package.json').version || '1.0.0'; } catch (_) { return '1.0.0'; }
}
const mcpHandler = createMcpHandler({
isEnabled: () => embeddings.loadSettings().mcp_enabled === true,
getToken: () => embeddings.loadSettings().mcp_token || '',
isReadonly: () => embeddings.loadSettings().mcp_readonly === true,
serverVersion: mcpServerVersion(),
});
const PORT = Number(process.env.PORT || 3000);
const app = express();
app.use(express.json({ limit: '30mb' }));
app.use(express.static(path.join(__dirname, 'public'), {
setHeaders: (res) => { res.setHeader('Cache-Control', 'no-cache, must-revalidate'); }
}));
app.use('/uploads', express.static(path.join(git.DATA_DIR, 'uploads')));
// 在线帮助文档(项目内 HELP.md,浏览器直接查看)
app.get('/help', (req, res) => {
res.type('text/markdown; charset=utf-8');
res.setHeader('Content-Disposition', 'inline; filename="HELP.md"');
res.sendFile(path.join(__dirname, 'HELP.md'));
});
// 轻量 SDK:说明文档(人类与 Agent 合读)与客户端脚本(零依赖 ESM)
app.get('/sdk', (req, res) => {
res.type('text/markdown; charset=utf-8');
res.setHeader('Content-Disposition', 'inline; filename="SDK.md"');
res.sendFile(path.join(__dirname, 'SDK.md'));
});
app.get('/sdk/kg-client.mjs', (req, res) => {
res.type('text/javascript; charset=utf-8');
res.sendFile(path.join(__dirname, 'sdk', 'kg-client.mjs'));
});
// CLI 工具集说明(kgctl,人类与 Agent 合读)
app.get('/cli', (req, res) => {
res.type('text/markdown; charset=utf-8');
res.setHeader('Content-Disposition', 'inline; filename="CLI.md"');
res.sendFile(path.join(__dirname, 'CLI.md'));
});
const api = express.Router();
// ---------- 元信息 ----------
api.get('/meta', (req, res) => {
res.json({
entity_categories: V.ENTITY_CATEGORIES,
relation_categories: V.RELATION_CATEGORIES,
counts: db.counts(),
version: db.getVersion(),
agent_available: agentAvailable,
agent_session: agentSessionInfo(),
git_available: gitAvailable,
sdk: { client: '/sdk/kg-client.mjs', docs: '/sdk', batch_write: 'POST /api/ops' },
});
});
// ---------- 实体详情(含关系与别名) ----------
api.get('/entities/:id', (req, res) => {
const id = Number(req.params.id);
const e = db.getEntity(id);
if (!e) return res.status(404).json({ error: `实体 id=${id} 不存在` });
const amap = db.aliasMap();
res.json({
entity: { ...e, aliases: amap[id] || [] },
relations: db.listRelations().filter((r) => r.source_id === id || r.target_id === id),
});
});
// ---------- SDK 批量原子写入(kg-ops 协议,与 MCP kg_apply_ops 同协议) ----------
api.post('/ops', (req, res) => {
const ops = (req.body || {}).ops;
if (!Array.isArray(ops) || !ops.length) return res.status(400).json({ error: 'ops必须为非空JSON数组' });
try {
const applied = db.applyAgentOps(ops);
try { git.savepoint(`SDK写入: ${applied.length} 项操作`, 'SDK'); } catch (_) { /* 数据目录无git时忽略 */ }
res.json({ applied_count: applied.length, applied });
} catch (e) {
res.status(e.status || 500).json({ error: e.message });
}
});
function agentSessionInfo() {
try {
const f = path.join(git.DATA_DIR, '.agent_session');
return fs.existsSync(f) ? { has_session: true } : { has_session: false };
} catch (_) { return { has_session: false }; }
}
// ---------- 实体 ----------
api.get('/entities', (req, res) => res.json(db.listEntities()));
api.post('/entities', (req, res) => {
try {
const body = req.body || {};
const name = body.name;
// 同名消歧:主名或别名占用时,按 merge_into / force 分流
if (typeof name === 'string' && name.trim()) {
const conflicts = db.findNameConflicts(name);
if (conflicts.length) {
const mergeInto = Number(body.merge_into);
if (body.merge_into !== undefined && Number.isInteger(mergeInto) && mergeInto > 0) {
const alias = db.addAlias(mergeInto, name.trim(), '手工');
return res.json({ merged: true, alias, entity: db.getEntity(mergeInto) });
}
if (body.force !== true && body.force !== 1 && body.force !== '1') {
return res.status(409).json({ error: `名称"${name.trim()}"已被占用,可并入现有实体、改用限定名或强制创建`, conflicts });
}
}
}
res.json(db.addEntity(body, '手工'));
} catch (e) { res.status(e.status || 500).json({ error: e.message, conflicts: e.conflicts }); }
});
api.put('/entities/:id', (req, res) => res.json(db.updateEntity(Number(req.params.id), req.body, '手工')));
api.delete('/entities/:id', (req, res) => {
const r = db.deleteEntity(Number(req.params.id), '手工');
cleanupImageFiles(r.image_files); // 级联删除图片行后移除文件本体
res.json(r);
});
// ---------- 中心层级子图(ego) ----------
api.get('/graph/ego', (req, res) => {
let centerId;
try { centerId = db.resolveKey(req.query.center); }
catch (e) { return res.status(e.status || 500).json({ error: e.message, candidates: e.candidates }); }
const raw = req.query.depth;
let depth = null;
if (raw !== undefined && String(raw).trim() !== '') {
depth = Number(raw);
if (!Number.isInteger(depth) || depth < 0) return res.status(400).json({ error: 'depth必须为非负整数(省略或0表示全部层级)' });
if (depth === 0) depth = null; // 0 = 全部层级
}
res.json(db.egoSubgraph(centerId, depth));
});
// ---------- 两实体最短路径 ----------
api.get('/graph/path', (req, res) => {
try {
const fromId = db.resolveKey(req.query.from);
const toId = db.resolveKey(req.query.to);
let maxHops = 6;
if (req.query.max !== undefined && String(req.query.max).trim() !== '') {
maxHops = Number(req.query.max);
if (!Number.isInteger(maxHops) || maxHops < 1 || maxHops > 12) return res.status(400).json({ error: 'max必须为1-12的整数' });
}
res.json(db.findPath(fromId, toId, maxHops));
} catch (e) { res.status(e.status || 500).json({ error: e.message, candidates: e.candidates }); }
});
// ---------- 撤销最近操作(快照逆向写入) ----------
api.get('/aliases', (req, res) => res.json(db.aliasMap()));
api.get('/aliases/records', (req, res) => res.json(db.aliasRecords()));
api.post('/aliases', (req, res) => {
try {
const { entity_id, alias } = req.body || {};
res.json(db.addAlias(Number(entity_id), alias, '手工'));
} catch (e) { res.status(e.status || 500).json({ error: e.message, conflicts: e.conflicts }); }
});
api.delete('/aliases/:id', (req, res) => {
try { res.json(db.removeAlias(Number(req.params.id), '手工')); }
catch (e) { res.status(e.status || 500).json({ error: e.message }); }
});
api.get('/graph/paths', (req, res) => {
try {
const fromId = db.resolveKey(req.query.from);
const toId = db.resolveKey(req.query.to);
let maxHops = 4;
if (req.query.max !== undefined && String(req.query.max).trim() !== '') {
maxHops = Number(req.query.max);
if (!Number.isInteger(maxHops)) return res.status(400).json({ error: 'max必须为整数' });
}
res.json(db.findPaths(fromId, toId, { maxHops }));
} catch (e) { res.status(e.status || 500).json({ error: e.message, candidates: e.candidates }); }
});
// ---------- 相似实体(语义Top-K,结构降级) ----------
api.get('/similar/:id', async (req, res) => {
try {
const k = req.query.k !== undefined ? Number(req.query.k) : 8;
res.json(await require('./lib/similar').similarEntities(Number(req.params.id), Number.isInteger(k) ? k : 8));
} catch (e) { res.status(e.status || 500).json({ error: e.message }); }
});
// ---------- 文档批量入图 ----------
const ingest = require('./lib/ingest');
ingest.loadPersisted();
api.post('/ingest', async (req, res) => {
if (agentBusy) return res.status(429).json({ error: '已有AI任务执行中(问答、导入或智能检索),请稍后再试' });
const { filename, content_b64, auto_commit } = req.body || {};
if (!filename || !content_b64) return res.status(400).json({ error: 'filename 与 content_b64 必填' });
let buf;
try { buf = Buffer.from(content_b64, 'base64'); } catch (_) { return res.status(400).json({ error: 'base64解码失败' }); }
agentBusy = true;
try {
res.json(await ingest.createTask(String(filename), buf, {
autoCommit: auto_commit === true || auto_commit === 1 || auto_commit === '1',
onSettled: () => { agentBusy = false; },
}));
} catch (e) {
agentBusy = false;
res.status(e.status || 500).json({ error: e.message });
}
});
api.get('/ingest/tasks', (req, res) => res.json(ingest.listTasks()));
api.get('/ingest/:id', (req, res) => {
const t = ingest.getTask(req.params.id);
if (!t) return res.status(404).json({ error: '任务不存在' });
res.json(t);
});
api.post('/ingest/:id/commit', (req, res) => {
try { res.json(ingest.commitTask(req.params.id, (req.body || {}).selected || null, '手工')); }
catch (e) { res.status(e.status || 500).json({ error: e.message }); }
});
api.delete('/ingest/:id', (req, res) => {
try { res.json(ingest.deleteTask(req.params.id)); }
catch (e) { res.status(e.status || 500).json({ error: e.message }); }
});
api.post('/undo', (req, res) => {
try { res.json({ ok: true, ...db.undoLast('手工'), counts: db.counts(), version: db.getVersion() }); }
catch (e) { res.status(e.status || 500).json({ error: e.message }); }
});
// ---------- 智能检索(LLM编译检索计划,只读执行) ----------
api.post('/ask', async (req, res) => {
if (agentBusy) return res.status(429).json({ error: '已有AI任务执行中(问答、导入或智能检索),请稍后再试' });
const question = (req.body || {}).question;
if (!question || !String(question).trim()) return res.status(400).json({ error: '问题不能为空' });
agentBusy = true;
try {
const s = embeddings.loadSettings();
const result = await askLib.ask(question, { synthesis: s.ask_synthesis !== false });
res.json(result);
} catch (e) {
res.status(e.status || 500).json({ error: e.message });
} finally {
agentBusy = false;
}
});
api.get('/ask/settings', (req, res) => {
res.json({ synthesis: embeddings.loadSettings().ask_synthesis !== false });
});
api.put('/ask/settings', (req, res) => {
embeddings.saveSettings({ ask_synthesis: !!(req.body || {}).synthesis });
res.json({ ok: true, synthesis: embeddings.loadSettings().ask_synthesis !== false });
});
// ---------- 版本与更新 ----------
api.get('/version', (req, res) => res.json({ version: updater.currentVersion() }));
api.get('/update/check', async (req, res) => {
try { res.json(await updater.checkUpdate()); }
catch (e) { res.status(500).json({ ok: false, error: e.message }); }
});
let updating = false;
api.post('/update/apply', (req, res) => {
if (updating) return res.status(429).json({ error: '更新正在进行中,请稍候' });
updating = true;
try {
const r = updater.applyUpdate();
if (!r.ok) return res.status(400).json(r);
res.json(r);
if (!r.up_to_date) scheduleRestart();
} catch (e) {
res.status(500).json({ ok: false, error: e.message });
} finally {
updating = false;
}
});
// ---------- 实体图片绑定 ----------
const IMAGE_EXTS = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp'];
const MAX_IMAGE_BYTES = 10 * 1024 * 1024;
function cleanupImageFiles(storedPaths) {
for (const p of storedPaths || []) {
try { fs.unlinkSync(path.join(git.DATA_DIR, p)); } catch (_) { /* 文件已不存在时容忍 */ }
}
}
api.get('/entities/:id/images', (req, res) => {
const id = Number(req.params.id);
if (!db.getEntity(id)) return res.status(404).json({ error: `实体id=${id} 不存在` });
const rel = (p) => (p ? '/uploads/' + p.replace(/^uploads[\\/]/, '') : null);
res.json(db.listEntityImages(id).map((r) => ({ ...r, url: rel(r.stored_path), thumb_url: rel(r.thumb_path) || rel(r.stored_path) })));
});
api.post('/entities/:id/images', (req, res) => {
const id = Number(req.params.id);
const { filename, content_b64, caption, thumb_b64 } = req.body || {};
if (!filename || !content_b64) return res.status(400).json({ error: '必须提供文件名与内容(content_b64)' });
const ext = path.extname(String(filename)).toLowerCase();
if (!IMAGE_EXTS.includes(ext)) return res.status(400).json({ error: `仅支持图片格式: ${IMAGE_EXTS.join(' ')}` });
let buf;
try { buf = Buffer.from(content_b64, 'base64'); } catch (_) { return res.status(400).json({ error: 'content_b64不是合法的base64' }); }
if (!buf.length) return res.status(400).json({ error: '文件内容为空' });
if (buf.length > MAX_IMAGE_BYTES) return res.status(400).json({ error: '单张图片不得超过10MB' });
const dir = path.join(git.DATA_DIR, 'uploads', `e${id}`);
fs.mkdirSync(dir, { recursive: true });
const safe = path.basename(String(filename)).replace(/[^\w.\-\u4e00-\u9fa5]/g, '_').slice(0, 80) || 'image';
const storedPath = path.join('uploads', `e${id}`, `${Date.now()}_${safe}`);
fs.writeFileSync(path.join(git.DATA_DIR, storedPath), buf);
// 缩略图(前端canvas生成的小图,可选):列表加载用,原图仅灯箱打开
let thumbPath = '';
if (thumb_b64) {
try {
const tbuf = Buffer.from(thumb_b64, 'base64');
if (tbuf.length > 0 && tbuf.length <= 2 * 1024 * 1024) {
thumbPath = path.join('uploads', `e${id}`, `${Date.now()}_thumb_${safe}`);
fs.writeFileSync(path.join(git.DATA_DIR, thumbPath), tbuf);
}
} catch (_) { thumbPath = ''; }
}
try {
const row = db.addEntityImage(id, { filename, stored_path: storedPath, caption, thumb_path: thumbPath });
const rel = (p) => (p ? '/uploads/' + p.replace(/^uploads[\\/]/, '') : null);
res.json({ ...row, url: rel(row.stored_path), thumb_url: rel(row.thumb_path) || rel(row.stored_path) });
} catch (e) {
cleanupImageFiles([storedPath, thumbPath]); // 入库失败时回滚文件,避免孤儿文件
throw e;
}
});
api.delete('/images/:imgId', (req, res) => {
const row = db.deleteEntityImage(Number(req.params.imgId));
cleanupImageFiles([row.stored_path, row.thumb_path].filter(Boolean));
res.json({ deleted: row });
});
// ---------- 关系图片绑定(与实体图片同模式,目录 r<id>)----------
api.get('/relations/:id/images', (req, res) => {
const id = Number(req.params.id);
if (!db.getRelation(id)) return res.status(404).json({ error: `关系id=${id} 不存在` });
const rel = (p) => (p ? '/uploads/' + p.replace(/^uploads[\\/]/, '') : null);
res.json(db.listRelationImages(id).map((r) => ({ ...r, url: rel(r.stored_path), thumb_url: rel(r.thumb_path) || rel(r.stored_path) })));
});
api.post('/relations/:id/images', (req, res) => {
const id = Number(req.params.id);
const { filename, content_b64, caption, thumb_b64 } = req.body || {};
if (!filename || !content_b64) return res.status(400).json({ error: '必须提供文件名与内容(content_b64)' });
const ext = path.extname(String(filename)).toLowerCase();
if (!IMAGE_EXTS.includes(ext)) return res.status(400).json({ error: `仅支持图片格式: ${IMAGE_EXTS.join(' ')}` });
let buf;
try { buf = Buffer.from(content_b64, 'base64'); } catch (_) { return res.status(400).json({ error: 'content_b64不是合法的base64' }); }
if (!buf.length) return res.status(400).json({ error: '文件内容为空' });
if (buf.length > MAX_IMAGE_BYTES) return res.status(400).json({ error: '单张图片不得超过10MB' });
const dir = path.join(git.DATA_DIR, 'uploads', `r${id}`);
fs.mkdirSync(dir, { recursive: true });
const safe = path.basename(String(filename)).replace(/[^\w.\-\u4e00-\u9fa5]/g, '_').slice(0, 80) || 'image';
const storedPath = path.join('uploads', `r${id}`, `${Date.now()}_${safe}`);
fs.writeFileSync(path.join(git.DATA_DIR, storedPath), buf);
let thumbPath = '';
if (thumb_b64) {
try {
const tbuf = Buffer.from(thumb_b64, 'base64');
if (tbuf.length > 0 && tbuf.length <= 2 * 1024 * 1024) {
thumbPath = path.join('uploads', `r${id}`, `${Date.now()}_thumb_${safe}`);
fs.writeFileSync(path.join(git.DATA_DIR, thumbPath), tbuf);
}
} catch (_) { thumbPath = ''; }
}
try {
const row = db.addRelationImage(id, { filename, stored_path: storedPath, caption, thumb_path: thumbPath });
const rel = (p) => (p ? '/uploads/' + p.replace(/^uploads[\\/]/, '') : null);
res.json({ ...row, url: rel(row.stored_path), thumb_url: rel(row.thumb_path) || rel(row.stored_path) });
} catch (e) {
cleanupImageFiles([storedPath, thumbPath]);
throw e;
}
});
api.delete('/relation-images/:imgId', (req, res) => {
const row = db.deleteRelationImage(Number(req.params.imgId));
cleanupImageFiles([row.stored_path, row.thumb_path].filter(Boolean));
res.json({ deleted: row });
});
// 删除关系时连带清理其图片文件(表行由外键级联清除)
api.delete('/relations/:id', (req, res) => {
const id = Number(req.params.id);
const files = db.listRelationImages(id).flatMap((r) => [r.stored_path, r.thumb_path].filter(Boolean));
const out = db.deleteRelation(id, '手工');
cleanupImageFiles(files);
res.json(out);
});
// ---------- 关系 ----------
api.get('/relations', (req, res) => res.json(db.listRelations()));
api.post('/relations', (req, res) => res.json(db.addRelation(req.body, '手工')));
api.put('/relations/:id', (req, res) => res.json(db.updateRelation(Number(req.params.id), req.body, '手工')));
// DELETE /relations/:id 已在上方"关系图片"块定义(连带清理图片文件)
// ---------- 图谱 / 日志 ----------
api.get('/graph', (req, res) => res.json(db.getGraph()));
// 推理引擎:动态推导隐性关系(传递/对称/逆),可选限定中心实体
api.get('/inference', (req, res) => {
const g = db.getGraph();
const inferred = inference.computeInferred(g);
const center = String(req.query.center || '').trim();
let result = inferred;
if (center) {
const byId = new Map(g.entities.map((e) => [e.id, e]));
let cid = null;
if (/^\d+$/.test(center)) {
if (byId.has(Number(center))) cid = Number(center);
else return res.status(404).json({ error: `实体"${center}"不存在` });
} else {
const cands = g.entities.filter((e) => e.name === center);
if (cands.length === 0) return res.status(404).json({ error: `实体"${center}"不存在` });
if (cands.length > 1) return res.status(409).json({ error: `实体名"${center}"存在${cands.length}个候选,请改用id`, candidates: cands.map((h) => ({ id: h.id, name: h.name, category: h.category })) });
cid = cands[0].id;
}
result = inferred.filter((i) => i.source_id === cid || i.target_id === cid);
}
const nodes = new Set();
for (const i of result) { nodes.add(i.source_id); nodes.add(i.target_id); }
res.json({ inferred: result, entities: g.entities.filter((e) => nodes.has(e.id)), ontology: inference.loadOntology() });
});
api.get('/ontology', (req, res) => res.json(inference.loadOntology()));
// ---------- 关系推荐(共同邻居 + LLM判断) ----------
api.get('/recommend', (req, res) => {
const g = db.getGraph();
const center = String(req.query.center || '').trim();
const opts = { limit: Number(req.query.limit) || 20 };
if (center) {
let cid = null;
if (/^\d+$/.test(center)) cid = Number(center);
else {
const cands = g.entities.filter((e) => e.name === center);
if (cands.length === 1) cid = cands[0].id;
else if (cands.length > 1) return res.status(409).json({ error: `实体名"${center}"存在${cands.length}个候选,请改用id`, candidates: cands.map((h) => ({ id: h.id, name: h.name, category: h.category })) });
}
if (cid && !g.entities.some((e) => e.id === cid)) return res.status(404).json({ error: `实体"${center}"不存在` });
if (cid) opts.centerId = cid;
}
res.json({ recommendations: recommend.computeCoNeighborRecs(g, opts) });
});
// LLM判断候选关系:只给建议,入库由前端确认后走人工接口
api.post('/recommend/ai', async (req, res) => {
if (agentBusy) return res.status(429).json({ error: '已有AI任务执行中(问答、导入或智能检索),请稍后再试' });
const body = req.body || {};
const g = db.getGraph();
const byId = new Map(g.entities.map((e) => [e.id, e]));
const a = byId.get(Number(body.source_id));
const b = byId.get(Number(body.target_id));
if (!a || !b) return res.status(404).json({ error: '待判断的实体不存在' });
agentBusy = true;
try {
const judge = await recommend.aiJudgeRelation({ a, b, common_names: body.common_names });
res.json(judge);
} catch (e) {
res.status(e.status || 500).json({ error: e.message });
} finally {
agentBusy = false;
}
});
// ---------- 向量混合检索 ----------
api.get('/embeddings/status', (req, res) => res.json(embeddings.status()));
api.get('/embeddings/settings', (req, res) => {
const s = embeddings.loadSettings();
res.json({ ...s, api_key: s.api_key ? '已配置' : '' }); // 不回传真实密钥
});
api.put('/embeddings/settings', (req, res) => {
try {
const patch = req.body || {};
// 前端传"已配置"占位时保留原密钥
if (patch.api_key === '已配置') delete patch.api_key;
const s = embeddings.saveSettings(patch);
res.json({ ...s, api_key: s.api_key ? '已配置' : '' });
} catch (e) { res.status(400).json({ error: e.message }); }
});
// 测试连接:用当前(或请求体内临时覆盖的)配置向服务方发一次真实embedding请求
api.post('/embeddings/test', async (req, res) => {
const overrides = req.body || {};
if (overrides.api_key === '已配置') delete overrides.api_key; // 占位=沿用已存密钥
const s = { ...embeddings.loadSettings(), ...overrides };
const started = Date.now();
try {
const vec = await embeddings.embed(['连接测试'], s);
const dim = Array.isArray(vec) && Array.isArray(vec[0]) ? vec[0].length : (vec && vec.length) || 0;
res.json({ ok: true, model: s.model, base_url: s.base_url, dim, ms: Date.now() - started });
} catch (e) {
res.status(400).json({ ok: false, error: e.message, ms: Date.now() - started });
}
});
api.post('/embeddings/build', async (req, res) => {
try {
const result = await embeddings.build();
res.json(result);
} catch (e) { res.status(400).json({ error: e.message }); }
});
api.post('/search', async (req, res) => {
const q = String((req.body || {}).query || '').trim();
if (!q) return res.status(400).json({ error: 'query不能为空' });
try {
res.json(await embeddings.search(q, (req.body || {}).top_k));
} catch (e) { res.status(400).json({ error: e.message }); }
});
api.post('/cypher', (req, res) => {
const q = String((req.body || {}).query || '').trim();
if (!q) return res.status(400).json({ error: 'query不能为空' });
try { res.json({ rows: db.miniCypher(q) }); }
catch (e) { res.status(400).json({ error: e.message }); }
});
api.put('/ontology', (req, res) => {
try { res.json(inference.saveOntology(req.body || {})); }
catch (e) { res.status(400).json({ error: '本体规则保存失败: ' + e.message }); }
});
api.get('/logs', (req, res) => res.json(db.getLogs(Number(req.query.limit) || 200)));
// ---------- RDF导出 ----------
api.get('/export/rdf', (req, res) => {
const ttl = rdf.exportTurtle(db.getGraph());
res.setHeader('Content-Type', 'text/turtle; charset=utf-8');
res.setHeader('Content-Disposition', 'attachment; filename="knowledge_graph.ttl"');
res.send(ttl);
});
// ---------- 文件:图谱保存/打开/另存 ----------
api.get('/export/db', (req, res) => {
const raw = String(req.query.name || '').trim().replace(/[\\/:*?"<>|]/g, '_') || 'kg.db';
const name = /\.(db|sqlite|sqlite3)$/i.test(raw) ? raw : raw.replace(/\.[^.]*$/, '') + '.db';
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(name)}`);
res.setHeader('Content-Type', 'application/octet-stream');
res.send(fs.readFileSync(db.DB_PATH)); // 写操作均经事务提交,主库文件始终处于一致状态
});
api.post('/graph/import', (req, res) => {
const { filename, content_b64 } = req.body || {};
if (!content_b64) return res.status(400).json({ error: '必须提供图谱数据库文件(content_b64)' });
let buf;
try { buf = Buffer.from(content_b64, 'base64'); } catch (_) { return res.status(400).json({ error: 'content_b64不是合法的base64' }); }
// 临时库校验:完整性 + 必需表 + 必需列(lib/importer.js)
const os = require('os');
const check = importer.validateImportBuffer(buf);
if (!check.ok) return res.status(400).json({ error: check.error });
const importedMaxLog = check.importedMaxLog;
const counts = check.counts;
const tmp = path.join(os.tmpdir(), `kg_import_${Date.now()}.db`);
fs.writeFileSync(tmp, buf);
// 当前数据自动备份保存点,再替换主库
let backup = null;
try { backup = git.savepoint(`打开图谱前自动备份 ${new Date().toLocaleString('zh-CN')}`, '系统'); } catch (_) { backup = null; }
fs.copyFileSync(tmp, db.DB_PATH);
fs.unlinkSync(tmp);
git.writeMeta({ last_saved_log: importedMaxLog }); // 日志区间绑定基准与导入库对齐
db.reopen();
const checkAfter = db.integrityCheck();
if (!checkAfter.ok) { const e = new Error('导入后完整性校验失败: ' + checkAfter.detail); e.status = 500; throw e; }
// .db 不携带 uploads/ 资产:剪除指向不存在文件的图片行,避免前端裂图
const prunedImages = db.pruneMissingImages();
res.json({ ok: true, counts: db.counts(), imported: { entities: counts.entities, relations: counts.relations }, backup_short: backup && backup.hash ? backup.hash : null, pruned_images: prunedImages.pruned });
});
// ---------- 另存为图谱网页(单文件只读查看器) ----------
api.get('/export/html', (req, res) => {
const viewer = require('./lib/viewer');
const html = viewer.buildViewerHtml(db.getGraph());
const raw = String(req.query.name || '').trim();
const name = /[\\/:*?"<>|]/.test(raw) || !raw ? 'kg-viewer.html' : raw;
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(/\.html?$/i.test(name) ? name : name + '.html')}`);
res.send(html);
});
// ---------- Git保存点与回溯 ----------
function requireGit(res) {
if (!gitAvailable) {
res.status(503).json({ error: '未检测到 Git,保存点/回溯功能已禁用(安装 Git 后重启服务即可启用)' });
return false;
}
return true;
}
api.get('/git/history', (req, res) => { if (!requireGit(res)) return; res.json(git.history(Number(req.query.limit) || 100)); });
api.post('/git/savepoint', (req, res) => {
if (!requireGit(res)) return;
const r = git.savepoint(req.body && req.body.message, '手工');
res.json(r);
});
api.post('/git/restore', (req, res) => {
if (!requireGit(res)) return;
const hash = req.body && req.body.hash;
if (!hash || typeof hash !== 'string') return res.status(400).json({ error: '必须提供要恢复的保存点hash' });
const r = git.restore(hash.trim());
db.logRestore(r.restored, r.backup);
res.json(r);
});
// ---------- MCP 外部接入设置 ----------
api.get('/mcp/settings', (req, res) => {
const s = embeddings.loadSettings();
res.json({
enabled: s.mcp_enabled === true,
readonly: s.mcp_readonly === true,
token: s.mcp_token || '',
endpoint: `/mcp`,
version: mcpServerVersion(),
});
});
api.put('/mcp/settings', (req, res) => {
try {
const patch = {};
const b = req.body || {};
if (typeof b.enabled === 'boolean') patch.mcp_enabled = b.enabled;
if (typeof b.readonly === 'boolean') patch.mcp_readonly = b.readonly;
const cur = embeddings.loadSettings();
if (patch.mcp_enabled === true && !cur.mcp_token) patch.mcp_token = crypto.randomBytes(24).toString('hex');
const s = embeddings.saveSettings(patch);
res.json({ enabled: s.mcp_enabled === true, readonly: s.mcp_readonly === true, token: s.mcp_token || '', endpoint: `/mcp`, version: mcpServerVersion() });
} catch (e) { res.status(400).json({ error: e.message }); }
});
api.post('/mcp/token/regen', (req, res) => {
const token = crypto.randomBytes(24).toString('hex');
embeddings.saveSettings({ mcp_token: token });
res.json({ token });
});
// ---------- 实时同步:SSE 事件流 ----------
api.get('/events', (req, res) => {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
'X-Accel-Buffering': 'no',
});
res.write('retry: 3000\n\n');
const send = (type, payload) => {
try { res.write(`event: ${type}\ndata: ${JSON.stringify(payload || {})}\n\n`); } catch (_) { /* 连接已断 */ }
};
send('hello', { ts: Date.now() });
const unsub = bus.on((type, payload) => send(type, payload));
const heartbeat = setInterval(() => { try { res.write(': ping\n\n'); } catch (_) { /* 忽略 */ } }, 25000);
req.on('close', () => { clearInterval(heartbeat); unsub(); });
});
// ---------- OpenCode自然语言指令 ----------
// AI任务互斥:OpenCode进程最长5分钟,防止并发请求叠加进程互抢SQLite与内存
let agentBusy = false;
api.post('/agent', async (req, res) => {
if (agentBusy) return res.status(429).json({ error: '已有AI任务执行中(问答或文档导入),请等待完成后再试' });
const instruction = req.body && req.body.instruction;
if (!instruction || !String(instruction).trim()) return res.status(400).json({ error: '指令不能为空' });
agentBusy = true;
try {
const result = await agent.runAgent(instruction);
if (!result.ok) return res.status(502).json({ error: result.error });
let applied = [];
let applyError = null;
if (result.ops && result.ops.length) {
try {
applied = db.applyAgentOps(result.ops);
// Agent批次写入后自动打保存点,与日志双向绑定
const title = `OpenCode: ${String(instruction).slice(0, 60).replace(/\n/g, ' ')}`;
let sp = null;
try { sp = git.savepoint(title, 'OpenCode'); } catch (e) { sp = { committed: false, message: e.message }; }
result.savepoint = sp;
} catch (e) {
applyError = e.message; // 违规操作被拦截,数据库保持上一个合规版本
}
}
res.json({ reply: result.reply, ops_found: (result.ops || []).length, applied, apply_error: applyError, parse_error: result.parse_error, savepoint: result.savepoint || null, retried: result.retried || false, partial_reply: result.partial_reply || null, session: result.session ? { has_session: true } : null });
} finally { agentBusy = false; }
});
// ---------- 文档导入:文本/文档 → 三元组 → 融合入库 ----------
const TEXT_LIKE_EXT = ['.txt', '.md', '.markdown', '.csv', '.json', '.html', '.htm', '.xml'];
const MAX_DOC_BYTES = 20 * 1024 * 1024;
const MAX_CHUNKS = 40;
const CHUNK_SIZE = 1400;
function safeName(name) {
const base = String(name || 'document').split(/[\\/]/).pop().replace(/[^\w.\-\u4e00-\u9fa5]/g, '_');
return base.slice(0, 80) || 'document';
}
function splitChunks(text, size = CHUNK_SIZE) {
const paras = text.split(/\n{2,}/);
const chunks = [];
let cur = '';
const pushCur = () => { if (cur.trim()) chunks.push(cur.trim()); cur = ''; };
for (const p of paras) {
if (p.length > size * 1.5) {
pushCur();
let s = '';
for (const sent of p.split(/(?<=[。!?.!?\n])/)) {
if ((s + sent).length > size && s) { chunks.push(s.trim()); s = ''; }
s += sent;
}
cur = s;
} else if ((cur + p).length > size && cur) {
pushCur();
cur = p;
} else {
cur += (cur ? '\n\n' : '') + p;
}
}
pushCur();
return chunks;
}
function docInstruction(filename, i, n, userInstruction) {
let s = `【文档导入任务】请使用 kg-triples 技能的完整流程,从文件《${filename}》的第 ${i}/${n} 块文本中抽取三元组(含属性)。`;
if (i > 1) s += '注意与"当前图谱"中已入库实体对齐:同名实体直接用其id引用,禁止重复创建。';
if (userInstruction) s += `\n用户补充要求:${userInstruction}`;
return s;
}
// 文档导入进度(内存态,供前端轮询;docId=本次导入的时间戳标识)
let docProgress = { active: false, stage: 'idle', chunk: 0, chunks: 0, mode: '', filename: '' };
api.get('/agent/doc/progress', (req, res) => res.json(docProgress));
// OpenCode 一键安装(便利功能:固定安装命令、无用户输入;本机单用户场景)
let installingAgent = false;
api.post('/agent/install', (req, res) => {
if (installingAgent) return res.status(429).json({ error: '已有安装任务进行中,请稍候' });
installingAgent = true;
let replied = false;
const reply = (payload) => { if (!replied) { replied = true; res.json(payload); } };
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const child = require('child_process').spawn(npmCmd, ['install', '-g', 'opencode-ai'], { shell: process.platform === 'win32' });
let tail = '';
const collect = (d) => { tail = (tail + d.toString()).slice(-2000); };
child.stdout.on('data', collect);
child.stderr.on('data', collect);
const timer = setTimeout(() => { try { child.kill(); } catch (_) {} }, 300000);
child.on('error', (e) => { installingAgent = false; clearTimeout(timer); reply({ ok: false, agent_available: false, message: '启动 npm 失败: ' + e.message, output: tail }); });
child.on('close', (code) => {
installingAgent = false;
clearTimeout(timer);
try { checkAgent(); } catch (_) {}
reply({
ok: code === 0 && agentAvailable,
agent_available: agentAvailable,
message: agentAvailable ? 'OpenCode 安装完成' : `安装未成功(退出码 ${code}),可手动执行 npm install -g opencode-ai 后刷新重试`,
output: tail,
});
});
});
api.post('/agent/doc', async (req, res) => {
if (agentBusy) return res.status(429).json({ error: '已有AI任务执行中(问答或文档导入),请等待完成后再试' });
const { filename, content_b64, instruction } = req.body || {};
if (!filename || !content_b64) return res.status(400).json({ error: '必须提供文件名与内容(content_b64)' });
let buf;
try { buf = Buffer.from(content_b64, 'base64'); } catch (_) { return res.status(400).json({ error: 'content_b64不是合法的base64' }); }
if (!buf.length) return res.status(400).json({ error: '文件内容为空' });
if (buf.length > MAX_DOC_BYTES) return res.status(400).json({ error: `文件超过${MAX_DOC_BYTES / 1024 / 1024}MB上限,请拆分后导入` });
agentBusy = true;
docProgress = { active: true, stage: 'preparing', chunk: 0, chunks: 0, mode: '', filename: safeName(filename) };
try {
const name = safeName(filename);
const ext = path.extname(name).toLowerCase();
const uploadsDir = path.join(git.DATA_DIR, 'uploads');
fs.mkdirSync(uploadsDir, { recursive: true });
const savedPath = path.join(uploadsDir, `${Date.now()}_${name}`);
fs.writeFileSync(savedPath, buf);
const report = { filename: name, mode: '', chunks: 0, chunks_ok: 0, entities_added: 0, relations_added: 0, others: 0, errors: [], reply: '' };
const tally = (applied) => {
for (const a of applied || []) {
if (a.op === 'add_entity') report.entities_added++;
else if (a.op === 'add_relation') report.relations_added++;
else report.others++;
}
};
const isTextLike = TEXT_LIKE_EXT.includes(ext);
const doApply = (r) => {
if (!r.ok) { report.errors.push(r.error); return r; }
if (r.ops && r.ops.length) {
try {
const applied = db.applyAgentOps(r.ops);
tally(applied);
report.chunks_ok++;
} catch (e) {
report.errors.push(`第${report.chunks_ok + 1}块操作被RDF校验拦截: ${e.message}`);
}
} else {
report.chunks_ok++;
}
if (r.reply) report.reply = r.reply;
return r;
};
try {
if (isTextLike && buf.length > 1500) {
// 文本类大文档:后端分块流水线,逐块抽取+融合,图谱摘要随每块刷新实现跨块对齐
report.mode = 'chunked';
const chunks = splitChunks(buf.toString('utf8'));
if (chunks.length > MAX_CHUNKS) {
docProgress = { ...docProgress, active: false, stage: 'error' };
return res.status(400).json({ error: `文档分块后达${chunks.length}块(上限${MAX_CHUNKS}),请拆分后导入` });
}
report.chunks = chunks.length;
docProgress = { ...docProgress, mode: 'chunked', chunks: chunks.length, stage: 'importing' };
for (let i = 0; i < chunks.length; i++) {
docProgress = { ...docProgress, chunk: i + 1 };
const inst = `${docInstruction(name, i + 1, chunks.length, instruction)}\n【文本块内容】\n<<<\n${chunks[i]}\n>>>`;
const r = doApply(await agent.runAgent(inst));
if (!r.ok) break; // agent层错误(超时/服务错误)时终止后续块
}
} else {
// 小文本或PDF/Word等二进制文档:整体作为附件交给OpenCode(kg-triples技能)
report.mode = 'attached';
report.chunks = 1;
docProgress = { ...docProgress, mode: 'attached', chunks: 1, chunk: 1, stage: 'importing' };
const inst = docInstruction(name, 1, 1, instruction) + ' 文档已作为附件挂载,请先读取再抽取。';
doApply(await agent.runAgent(inst, [savedPath]));
}
docProgress = { ...docProgress, active: true, stage: 'savepoint' };
} finally {
try { fs.unlinkSync(savedPath); } catch (_) { /* 保留亦可 */ }
}
// 汇总保存点(含导入统计,与日志双向绑定)
let savepoint = null;
try {
savepoint = git.savepoint(`文档导入: ${name}(实体+${report.entities_added} 关系+${report.relations_added})`, 'OpenCode');
} catch (e) { savepoint = { committed: false, message: e.message }; }
docProgress = { active: false, stage: report.errors.length && !report.chunks_ok ? 'error' : 'done', chunk: docProgress.chunk, chunks: docProgress.chunks, mode: report.mode, filename: name };
res.json({ report, applied_total: report.entities_added + report.relations_added + report.others, savepoint, retried: false });
} finally { agentBusy = false; }
});
// MCP 端点(置于 /api 之前;内部有开关与令牌校验)
app.use('/mcp', mcpHandler);
app.use('/api', api);
// 统一错误处理
app.use((err, req, res, next) => {
const status = err.status || 500;
res.status(status).json({ error: err.message, errors: err.errors || undefined });
});
// ---------- 启动流程:异常兜底 + 自动拉起OpenCode ----------
let agentAvailable = false;
let gitAvailable = false;
function checkAgent() {
// force=true:一键安装完成后强制重新探测(Windows 解析包内 opencode.exe 绝对路径)
agentAvailable = !!agent.resolveOpencodeCommand(true);
}
function bootstrap() {
require('./lib/paths').ensureLegacyMigration();
// Git 缺失时优雅降级:保存点/回溯禁用,其余功能正常(exe 分发场景常见)
try {
git.ensureRepo();
git.backupCopy(true); // 启动时强制留一份滚动副本,目录损坏时仍有外部备份可救
gitAvailable = true;
} catch (e) {
gitAvailable = false;
console.error('[保存点] 未检测到可用 Git:保存点/回溯功能已禁用,其余功能不受影响(' + String(e.message || e).split('\n')[0] + ')');
}
try {
db.open();
} catch (e) {
console.error('[兜底] 数据库异常:', e.message);
// 数据库出错自动保留并恢复上一个合规版本
try {
const commits = git.history(5);
const good = commits.find((c) => c.title !== '回滚前自动备份');
if (good) {
console.error('[兜底] 正在恢复最近合规版本:', good.short, good.title);
git.restore(good.hash);
db.open();
console.error('[兜底] 已恢复上一个合规版本');
} else {
throw e;
}
} catch (e2) {
console.error('[兜底] 恢复失败,请检查 data/ 目录:', e2.message);
process.exit(1);
}
}
seedIfEmpty();
checkAgent();
console.log(agentAvailable ? '[OpenCode] 进程已就绪,可在前端输入自然语言指令' : '[OpenCode] 未检测到 opencode CLI,自然语言补全不可用(其余功能不受影响)');
}
function seedIfEmpty() {
if (db.counts().entities > 0) return;
console.log('[初始化] 空库,写入示例数据…');
const beer = db.addEntity({ name: '长江', category: '物理实体', attributes: { 长度公里: 6397, 类型: '河流' } }, '手工');
const country = db.addEntity({ name: '中国', category: '物理实体', attributes: { 大洲: '亚洲' } }, '手工');
const commerce = db.addEntity({ name: '电子商务', category: '抽象实体', attributes: { 兴起年代: '1990年代' } }, '手工');
const year = db.addEntity({ name: '2026年', category: '时间实体', attributes: { 年份: 2026 } }, '手工');
const users = db.addEntity({ name: '网民规模', category: '数值实体', attributes: { 数值: 11.7, 单位: '亿' } }, '手工');
db.addRelation({ source_id: beer.id, target_id: country.id, name: '流经', category: '空间' }, '手工');
db.addRelation({ source_id: users.id, target_id: commerce.id, name: '推动发展', category: '互动' }, '手工');
db.addRelation({ source_id: commerce.id, target_id: year.id, name: '成熟于', category: '时间' }, '手工');
try { git.savepoint('初始化:数据库与示例数据', '系统'); console.log('[初始化] 完成,已创建首个Git保存点'); }
catch (_) { console.log('[初始化] 完成(未检测到 Git,跳过保存点)'); }
}
bootstrap();
const HOST = process.env.KG_HOST || '127.0.0.1';
// 自动重启(更新后):派生脱离的新进程接管,当前进程退出
function scheduleRestart() {
console.log('[更新] 3秒后自动重启服务…');
setTimeout(() => {
try {
const { spawn } = require('child_process');
const log = fs.openSync(path.join(require('./lib/paths').DATA_DIR, 'restart.log'), 'a');
const child = spawn(process.execPath, [...process.execArgv, path.join(__dirname, 'server.js')], {
detached: true, stdio: ['ignore', log, log], env: { ...process.env, KG_NO_OPEN: '1' }, cwd: __dirname,
});
child.unref();
console.log(`[更新] 新进程已启动 (pid ${child.pid}),当前进程即将退出`);
fs.writeSync(log, `\n[${new Date().toISOString()}] 更新重启:新进程 pid ${child.pid}\n`);
setTimeout(() => process.exit(0), 500);
} catch (e) {
console.error('[更新] 自动重启失败,请手动重新运行启动脚本:', e.message);
}
}, 3000);
}
// 服务就绪后自动打开浏览器(KG_NO_OPEN=1 禁止;kg CLI 自己负责打开,会预设该变量)
function openBrowserTab(url) {
try {
const { spawn } = require('child_process');
const cmds = process.platform === 'win32' ? [['cmd', ['/c', 'start', '', url]]]
: process.platform === 'darwin' ? [['open', [url]]]
: [['xdg-open', [url]]];
const [cmd, cargs] = cmds[0];
spawn(cmd, cargs, { detached: true, stdio: 'ignore' }).unref();
} catch (_) { /* 打不开就让用户手动访问窗口内地址 */ }
}