@yugenlab/vaayu 0.1.10 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/chunks/{agentic-tool-loop-2FZK72JO.js → agentic-tool-loop-O3NUV7KG.js} +1 -1
- package/chunks/{chunk-UZ6OIVEC.js → chunk-2OBLQJYJ.js} +1 -1
- package/chunks/{chunk-O4KV7TFP.js → chunk-3AYSJ7WB.js} +30 -18
- package/chunks/{chunk-U62ABYKD.js → chunk-67DXWEKG.js} +3 -3
- package/chunks/{chunk-6556EKOB.js → chunk-7AYYXHYZ.js} +25 -24
- package/chunks/{chunk-JGI4SDWS.js → chunk-7XV5ISV7.js} +7 -5
- package/chunks/{chunk-JAWZ7ANC.js → chunk-A3HOZBC5.js} +11 -7
- package/chunks/{chunk-VJHNE47S.js → chunk-D46QTN3G.js} +63 -82
- package/chunks/{chunk-PRXQW76U.js → chunk-EG37M4QL.js} +17 -6
- package/chunks/{chunk-77725AR7.js → chunk-F6RNEGFX.js} +82 -53
- package/chunks/{chunk-MJ74G5RB.js → chunk-G2QREGXK.js} +2 -2
- package/chunks/{chunk-AS3DJFY3.js → chunk-JZTFJE7M.js} +39 -39
- package/chunks/{chunk-OT4G2L46.js → chunk-LJUEMPLG.js} +202 -154
- package/chunks/{chunk-C76USAC5.js → chunk-QFGAB4XD.js} +13 -5
- package/chunks/{chunk-M7THR63C.js → chunk-QV4GPIPT.js} +74 -65
- package/chunks/{chunk-YJRXLRTE.js → chunk-V2ZIKDN4.js} +9 -8
- package/chunks/{chunk-AGK3A7R7.js → chunk-VCUJES75.js} +791 -677
- package/chunks/{chunk-N22M7D4P.js → chunk-W4PVGBUH.js} +86 -97
- package/chunks/{chunk-TND3MU4Z.js → chunk-Z576WVLG.js} +74 -66
- package/chunks/{chunk-HIYHTWFW.js → chunk-ZYY6N3SP.js} +90 -118
- package/chunks/{consolidation-indexer-VKQ6DNU3.js → consolidation-indexer-VIWOP6VO.js} +8 -8
- package/chunks/{day-consolidation-BH3QU2SZ.js → day-consolidation-HMHSXIOM.js} +4 -4
- package/chunks/{src-Y3TGMINC.js → dist-CY5NX2IK.js} +17 -17
- package/chunks/graphrag-T2QWNX57.js +14 -0
- package/chunks/{hierarchical-temporal-search-PVHVA3NZ.js → hierarchical-temporal-search-U6DG74IR.js} +2 -2
- package/chunks/hybrid-search-BYTXCOXP.js +20 -0
- package/chunks/{memory-store-A6WOWLWC.js → memory-store-LEERUQGL.js} +3 -3
- package/chunks/periodic-consolidation-D6SSKZ7H.js +11 -0
- package/chunks/{postgres-WLH3D5HG.js → postgres-7GZDDX77.js} +2 -2
- package/chunks/{recall-ZNL4DJ2L.js → recall-LNRQVATQ.js} +7 -7
- package/chunks/search-BIODUW2P.js +19 -0
- package/chunks/{session-store-3BRPGC6P.js → session-store-O3TS7DUY.js} +5 -5
- package/chunks/{sqlite-DHUQGPR5.js → sqlite-7BC4DJTN.js} +2 -2
- package/chunks/{vasana-engine-MU25OQ23.js → vasana-engine-BJFHJVGM.js} +4 -4
- package/gateway.js +31592 -24973
- package/package.json +1 -1
- package/pair-cli.js +1 -1
- package/chunks/graphrag-D7OXWAWD.js +0 -14
- package/chunks/hybrid-search-G2NAJKJ7.js +0 -20
- package/chunks/periodic-consolidation-LMYMNS4Q.js +0 -11
- package/chunks/search-35JMSGUT.js +0 -19
|
@@ -6,24 +6,25 @@ import {
|
|
|
6
6
|
STOP_WORDS,
|
|
7
7
|
cosineSimilarity,
|
|
8
8
|
textMatchScore
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-A3HOZBC5.js";
|
|
10
10
|
import {
|
|
11
11
|
initGraphSchema
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-V2ZIKDN4.js";
|
|
13
13
|
import {
|
|
14
14
|
DatabaseManager
|
|
15
|
-
} from "./chunk-
|
|
15
|
+
} from "./chunk-67DXWEKG.js";
|
|
16
16
|
import {
|
|
17
17
|
getChitraguptaHome
|
|
18
|
-
} from "./chunk-
|
|
18
|
+
} from "./chunk-2OBLQJYJ.js";
|
|
19
19
|
|
|
20
|
-
// ../chitragupta/packages/smriti/
|
|
20
|
+
// ../chitragupta/packages/smriti/dist/graphrag-pagerank.js
|
|
21
21
|
var PAGERANK_DAMPING = 0.85;
|
|
22
22
|
var PAGERANK_EPSILON = 1e-4;
|
|
23
23
|
var PAGERANK_MAX_ITERATIONS = 100;
|
|
24
24
|
function computePageRank(graph) {
|
|
25
25
|
const N = graph.nodes.length;
|
|
26
|
-
if (N === 0)
|
|
26
|
+
if (N === 0)
|
|
27
|
+
return /* @__PURE__ */ new Map();
|
|
27
28
|
const ranks = /* @__PURE__ */ new Map();
|
|
28
29
|
const nodeIds = /* @__PURE__ */ new Set();
|
|
29
30
|
for (const node of graph.nodes) {
|
|
@@ -63,17 +64,19 @@ function computePageRank(graph) {
|
|
|
63
64
|
const rank = baseFactor + PAGERANK_DAMPING * (incomingRank + danglingRank / N);
|
|
64
65
|
newRanks.set(node.id, rank);
|
|
65
66
|
const delta = Math.abs(rank - (ranks.get(node.id) ?? 0));
|
|
66
|
-
if (delta > maxDelta)
|
|
67
|
+
if (delta > maxDelta)
|
|
68
|
+
maxDelta = delta;
|
|
67
69
|
}
|
|
68
70
|
for (const [id, rank] of newRanks) {
|
|
69
71
|
ranks.set(id, rank);
|
|
70
72
|
}
|
|
71
|
-
if (maxDelta < PAGERANK_EPSILON)
|
|
73
|
+
if (maxDelta < PAGERANK_EPSILON)
|
|
74
|
+
break;
|
|
72
75
|
}
|
|
73
76
|
return ranks;
|
|
74
77
|
}
|
|
75
78
|
|
|
76
|
-
// ../chitragupta/packages/smriti/
|
|
79
|
+
// ../chitragupta/packages/smriti/dist/graphrag-pagerank-incremental.js
|
|
77
80
|
var IncrementalPageRank = class {
|
|
78
81
|
ranks = /* @__PURE__ */ new Map();
|
|
79
82
|
outDegree = /* @__PURE__ */ new Map();
|
|
@@ -155,7 +158,8 @@ var IncrementalPageRank = class {
|
|
|
155
158
|
if (oldDeg > 0) {
|
|
156
159
|
const shareDelta = prU * (1 / newDeg - 1 / oldDeg);
|
|
157
160
|
for (const neighbor of this.getOutNeighbors(source)) {
|
|
158
|
-
if (neighbor === target)
|
|
161
|
+
if (neighbor === target)
|
|
162
|
+
continue;
|
|
159
163
|
const prev = residuals.get(neighbor) ?? 0;
|
|
160
164
|
residuals.set(neighbor, prev + this.damping * shareDelta);
|
|
161
165
|
}
|
|
@@ -169,11 +173,14 @@ var IncrementalPageRank = class {
|
|
|
169
173
|
* @param target - Target node ID.
|
|
170
174
|
*/
|
|
171
175
|
removeEdge(source, target) {
|
|
172
|
-
if (!this.nodeSet.has(source) || !this.nodeSet.has(target))
|
|
176
|
+
if (!this.nodeSet.has(source) || !this.nodeSet.has(target))
|
|
177
|
+
return;
|
|
173
178
|
const inSet = this.inLinks.get(target);
|
|
174
|
-
if (!inSet || !inSet.has(source))
|
|
179
|
+
if (!inSet || !inSet.has(source))
|
|
180
|
+
return;
|
|
175
181
|
const oldDeg = this.outDegree.get(source) ?? 0;
|
|
176
|
-
if (oldDeg <= 0)
|
|
182
|
+
if (oldDeg <= 0)
|
|
183
|
+
return;
|
|
177
184
|
const newDeg = oldDeg - 1;
|
|
178
185
|
this.outDegree.set(source, newDeg);
|
|
179
186
|
inSet.delete(source);
|
|
@@ -216,7 +223,8 @@ var IncrementalPageRank = class {
|
|
|
216
223
|
maxNode = node;
|
|
217
224
|
}
|
|
218
225
|
}
|
|
219
|
-
if (maxNode === null || maxResidual < this.epsilon)
|
|
226
|
+
if (maxNode === null || maxResidual < this.epsilon)
|
|
227
|
+
break;
|
|
220
228
|
const res = residuals.get(maxNode);
|
|
221
229
|
this.ranks.set(maxNode, (this.ranks.get(maxNode) ?? 0) + res);
|
|
222
230
|
residuals.delete(maxNode);
|
|
@@ -240,13 +248,14 @@ var IncrementalPageRank = class {
|
|
|
240
248
|
}
|
|
241
249
|
};
|
|
242
250
|
|
|
243
|
-
// ../chitragupta/packages/smriti/
|
|
251
|
+
// ../chitragupta/packages/smriti/dist/graphrag-pagerank-personalized.js
|
|
244
252
|
function buildTopicBias(nodeIds, nodeContents, topic) {
|
|
245
253
|
const bias = /* @__PURE__ */ new Map();
|
|
246
254
|
const n = nodeIds.length;
|
|
247
255
|
if (!topic || n === 0) {
|
|
248
256
|
const uniform = 1 / Math.max(n, 1);
|
|
249
|
-
for (const id of nodeIds)
|
|
257
|
+
for (const id of nodeIds)
|
|
258
|
+
bias.set(id, uniform);
|
|
250
259
|
return bias;
|
|
251
260
|
}
|
|
252
261
|
const queryTerms = tokenizeSimple(topic);
|
|
@@ -263,7 +272,8 @@ function buildTopicBias(nodeIds, nodeContents, topic) {
|
|
|
263
272
|
}
|
|
264
273
|
if (totalSim === 0) {
|
|
265
274
|
const uniform = 1 / n;
|
|
266
|
-
for (const id of nodeIds)
|
|
275
|
+
for (const id of nodeIds)
|
|
276
|
+
bias.set(id, uniform);
|
|
267
277
|
return bias;
|
|
268
278
|
}
|
|
269
279
|
for (let i = 0; i < n; i++) {
|
|
@@ -288,7 +298,8 @@ function tfCosineSimilarity(a, b) {
|
|
|
288
298
|
for (const [term, freqA] of a) {
|
|
289
299
|
normA += freqA * freqA;
|
|
290
300
|
const freqB = b.get(term);
|
|
291
|
-
if (freqB !== void 0)
|
|
301
|
+
if (freqB !== void 0)
|
|
302
|
+
dot += freqA * freqB;
|
|
292
303
|
}
|
|
293
304
|
for (const freqB of b.values()) {
|
|
294
305
|
normB += freqB * freqB;
|
|
@@ -302,7 +313,8 @@ function computePersonalizedPageRank(graph, topicBias, opts) {
|
|
|
302
313
|
const maxIter = opts?.maxIterations ?? 150;
|
|
303
314
|
const useGS = opts?.useGaussSeidel ?? true;
|
|
304
315
|
const N = graph.nodes.length;
|
|
305
|
-
if (N === 0)
|
|
316
|
+
if (N === 0)
|
|
317
|
+
return /* @__PURE__ */ new Map();
|
|
306
318
|
const nodeIds = graph.nodes.map((n) => n.id);
|
|
307
319
|
const nodeIdSet = new Set(nodeIds);
|
|
308
320
|
const outDegree = /* @__PURE__ */ new Map();
|
|
@@ -336,7 +348,8 @@ function computePersonalizedPageRank(graph, topicBias, opts) {
|
|
|
336
348
|
}
|
|
337
349
|
const danglingNodes = [];
|
|
338
350
|
for (const id of nodeIds) {
|
|
339
|
-
if ((outDegree.get(id) ?? 0) === 0)
|
|
351
|
+
if ((outDegree.get(id) ?? 0) === 0)
|
|
352
|
+
danglingNodes.push(id);
|
|
340
353
|
}
|
|
341
354
|
for (let iter = 0; iter < maxIter; iter++) {
|
|
342
355
|
let danglingSum = 0;
|
|
@@ -357,7 +370,8 @@ function computePersonalizedPageRank(graph, topicBias, opts) {
|
|
|
357
370
|
const newRank = (1 - damping) * biasVal + damping * incomingSum + danglingContrib;
|
|
358
371
|
const oldRank = ranks.get(id) ?? 0;
|
|
359
372
|
const delta = Math.abs(newRank - oldRank);
|
|
360
|
-
if (delta > maxDelta)
|
|
373
|
+
if (delta > maxDelta)
|
|
374
|
+
maxDelta = delta;
|
|
361
375
|
ranks.set(id, newRank);
|
|
362
376
|
}
|
|
363
377
|
} else {
|
|
@@ -373,16 +387,19 @@ function computePersonalizedPageRank(graph, topicBias, opts) {
|
|
|
373
387
|
const newRank = (1 - damping) * biasVal + damping * incomingSum + danglingContrib;
|
|
374
388
|
newRanks.set(id, newRank);
|
|
375
389
|
const delta = Math.abs(newRank - (ranks.get(id) ?? 0));
|
|
376
|
-
if (delta > maxDelta)
|
|
390
|
+
if (delta > maxDelta)
|
|
391
|
+
maxDelta = delta;
|
|
377
392
|
}
|
|
378
|
-
for (const [id, rank] of newRanks)
|
|
393
|
+
for (const [id, rank] of newRanks)
|
|
394
|
+
ranks.set(id, rank);
|
|
379
395
|
}
|
|
380
|
-
if (maxDelta < epsilon)
|
|
396
|
+
if (maxDelta < epsilon)
|
|
397
|
+
break;
|
|
381
398
|
}
|
|
382
399
|
return ranks;
|
|
383
400
|
}
|
|
384
401
|
|
|
385
|
-
// ../chitragupta/packages/smriti/
|
|
402
|
+
// ../chitragupta/packages/smriti/dist/graphrag-extraction.js
|
|
386
403
|
var CHUNK_TARGET_TOKENS = 350;
|
|
387
404
|
var CHUNK_MIN_TOKENS = 200;
|
|
388
405
|
var CHUNK_MAX_TOKENS = 500;
|
|
@@ -396,7 +413,8 @@ function estimateTokens(text) {
|
|
|
396
413
|
}
|
|
397
414
|
function semanticChunk(text) {
|
|
398
415
|
const sentences = splitSentences(text);
|
|
399
|
-
if (sentences.length === 0)
|
|
416
|
+
if (sentences.length === 0)
|
|
417
|
+
return [];
|
|
400
418
|
const totalTokens = estimateTokens(text);
|
|
401
419
|
if (totalTokens <= CHUNK_MAX_TOKENS) {
|
|
402
420
|
return [{ text, startSentence: 0, endSentence: sentences.length - 1 }];
|
|
@@ -425,7 +443,8 @@ function semanticChunk(text) {
|
|
|
425
443
|
overlapTokens += estimateTokens(sentences[overlapStart]);
|
|
426
444
|
}
|
|
427
445
|
startIdx = Math.max(overlapStart, startIdx + 1);
|
|
428
|
-
if (startIdx >= endIdx)
|
|
446
|
+
if (startIdx >= endIdx)
|
|
447
|
+
break;
|
|
429
448
|
}
|
|
430
449
|
return chunks;
|
|
431
450
|
}
|
|
@@ -483,8 +502,10 @@ function keywordExtractEntities(text) {
|
|
|
483
502
|
const entities = [];
|
|
484
503
|
const sorted = [...freq.entries()].sort((a, b) => b[1] - a[1]);
|
|
485
504
|
for (const [word, count] of sorted) {
|
|
486
|
-
if (count < 2)
|
|
487
|
-
|
|
505
|
+
if (count < 2)
|
|
506
|
+
break;
|
|
507
|
+
if (entities.length >= 20)
|
|
508
|
+
break;
|
|
488
509
|
entities.push({
|
|
489
510
|
name: word,
|
|
490
511
|
type: "concept",
|
|
@@ -494,7 +515,7 @@ function keywordExtractEntities(text) {
|
|
|
494
515
|
return entities;
|
|
495
516
|
}
|
|
496
517
|
|
|
497
|
-
// ../chitragupta/packages/smriti/
|
|
518
|
+
// ../chitragupta/packages/smriti/dist/ner-extractor.js
|
|
498
519
|
var DEFAULT_ENDPOINT = "http://localhost:8501";
|
|
499
520
|
var DEFAULT_MODEL = "gliner-large-v2.1";
|
|
500
521
|
var DEFAULT_MIN_CONFIDENCE = 0.5;
|
|
@@ -588,10 +609,7 @@ var TECHNOLOGY_WORDS = [
|
|
|
588
609
|
"yarn",
|
|
589
610
|
"zod"
|
|
590
611
|
];
|
|
591
|
-
var TECHNOLOGY_PATTERN = new RegExp(
|
|
592
|
-
`\\b(${TECHNOLOGY_WORDS.join("|")})\\b`,
|
|
593
|
-
"gi"
|
|
594
|
-
);
|
|
612
|
+
var TECHNOLOGY_PATTERN = new RegExp(`\\b(${TECHNOLOGY_WORDS.join("|")})\\b`, "gi");
|
|
595
613
|
var TOOL_WORDS = [
|
|
596
614
|
"read",
|
|
597
615
|
"write",
|
|
@@ -607,10 +625,7 @@ var TOOL_WORDS = [
|
|
|
607
625
|
"webfetch",
|
|
608
626
|
"websearch"
|
|
609
627
|
];
|
|
610
|
-
var TOOL_PATTERN = new RegExp(
|
|
611
|
-
`\\b(${TOOL_WORDS.join("|")})\\b`,
|
|
612
|
-
"gi"
|
|
613
|
-
);
|
|
628
|
+
var TOOL_PATTERN = new RegExp(`\\b(${TOOL_WORDS.join("|")})\\b`, "gi");
|
|
614
629
|
var ERROR_PATTERN = /\b((?:[A-Z]\w*Error)\b|(?:ENOENT|EACCES|ECONNREFUSED|ETIMEDOUT|EPERM)\b|(?:failed\s+to\s+\w+)|(?:error:\s*\S+))/gi;
|
|
615
630
|
var DECISION_PATTERN = /\b((?:decided|chose|selected|opted|switched|migrated|moved)\s+to\s+\w[\w\s]{0,30})/gi;
|
|
616
631
|
var ACTION_PATTERN = /\b((?:created|deleted|removed|modified|updated|installed|uninstalled|deployed|fixed|refactored|renamed|merged|rebased|reverted|committed|pushed|pulled)\s+\w[\w\s]{0,30})/gi;
|
|
@@ -639,7 +654,8 @@ var NERExtractor = class {
|
|
|
639
654
|
* and `useHeuristic` is enabled.
|
|
640
655
|
*/
|
|
641
656
|
async extract(text) {
|
|
642
|
-
if (!text || text.trim().length === 0)
|
|
657
|
+
if (!text || text.trim().length === 0)
|
|
658
|
+
return [];
|
|
643
659
|
const glinerUp = await this.isGLiNERAvailable();
|
|
644
660
|
if (glinerUp) {
|
|
645
661
|
try {
|
|
@@ -666,7 +682,8 @@ var NERExtractor = class {
|
|
|
666
682
|
* Caches the result after the first successful probe.
|
|
667
683
|
*/
|
|
668
684
|
async isGLiNERAvailable() {
|
|
669
|
-
if (this.glinerAvailable !== null)
|
|
685
|
+
if (this.glinerAvailable !== null)
|
|
686
|
+
return this.glinerAvailable;
|
|
670
687
|
try {
|
|
671
688
|
const response = await fetch(this.endpoint, {
|
|
672
689
|
method: "GET",
|
|
@@ -732,10 +749,12 @@ var NERExtractor = class {
|
|
|
732
749
|
const coveredSpans = [];
|
|
733
750
|
const addMatch = (match, type, groupIndex = 1) => {
|
|
734
751
|
const captured = match[groupIndex];
|
|
735
|
-
if (!captured)
|
|
752
|
+
if (!captured)
|
|
753
|
+
return;
|
|
736
754
|
const start = match.index + match[0].indexOf(captured);
|
|
737
755
|
const end = start + captured.length;
|
|
738
|
-
if (this.spanOverlaps([start, end], coveredSpans))
|
|
756
|
+
if (this.spanOverlaps([start, end], coveredSpans))
|
|
757
|
+
return;
|
|
739
758
|
coveredSpans.push([start, end]);
|
|
740
759
|
entities.push({
|
|
741
760
|
text: captured.trim(),
|
|
@@ -783,9 +802,7 @@ var NERExtractor = class {
|
|
|
783
802
|
*/
|
|
784
803
|
postProcess(entities) {
|
|
785
804
|
const typeSet = new Set(this.entityTypes);
|
|
786
|
-
let filtered = entities.filter(
|
|
787
|
-
(e) => typeSet.has(e.type) && e.confidence >= this.minConfidence
|
|
788
|
-
);
|
|
805
|
+
let filtered = entities.filter((e) => typeSet.has(e.type) && e.confidence >= this.minConfidence);
|
|
789
806
|
const seen = /* @__PURE__ */ new Map();
|
|
790
807
|
for (const entity of filtered) {
|
|
791
808
|
const key = `${entity.type}::${entity.text.toLowerCase().trim()}`;
|
|
@@ -796,14 +813,15 @@ var NERExtractor = class {
|
|
|
796
813
|
}
|
|
797
814
|
filtered = [...seen.values()];
|
|
798
815
|
filtered.sort((a, b) => {
|
|
799
|
-
if (b.confidence !== a.confidence)
|
|
816
|
+
if (b.confidence !== a.confidence)
|
|
817
|
+
return b.confidence - a.confidence;
|
|
800
818
|
return a.span[0] - b.span[0];
|
|
801
819
|
});
|
|
802
820
|
return filtered.slice(0, this.maxEntities);
|
|
803
821
|
}
|
|
804
822
|
};
|
|
805
823
|
|
|
806
|
-
// ../chitragupta/packages/smriti/
|
|
824
|
+
// ../chitragupta/packages/smriti/dist/bitemporal.js
|
|
807
825
|
function nowISO() {
|
|
808
826
|
return (/* @__PURE__ */ new Date()).toISOString();
|
|
809
827
|
}
|
|
@@ -844,14 +862,19 @@ function expireEdge(edge, validUntil) {
|
|
|
844
862
|
function queryEdgesAtTime(edges, asOfValid, asOfRecord) {
|
|
845
863
|
return edges.filter((e) => {
|
|
846
864
|
const vFrom = e.validFrom ?? EPOCH_ISO;
|
|
847
|
-
if (vFrom > asOfValid)
|
|
848
|
-
|
|
865
|
+
if (vFrom > asOfValid)
|
|
866
|
+
return false;
|
|
867
|
+
if (e.validUntil !== void 0 && e.validUntil <= asOfValid)
|
|
868
|
+
return false;
|
|
849
869
|
if (asOfRecord !== void 0) {
|
|
850
870
|
const rAt = e.recordedAt ?? EPOCH_ISO;
|
|
851
|
-
if (rAt > asOfRecord)
|
|
852
|
-
|
|
871
|
+
if (rAt > asOfRecord)
|
|
872
|
+
return false;
|
|
873
|
+
if (e.supersededAt !== void 0 && e.supersededAt <= asOfRecord)
|
|
874
|
+
return false;
|
|
853
875
|
} else {
|
|
854
|
-
if (e.supersededAt !== void 0)
|
|
876
|
+
if (e.supersededAt !== void 0)
|
|
877
|
+
return false;
|
|
855
878
|
}
|
|
856
879
|
return true;
|
|
857
880
|
});
|
|
@@ -862,19 +885,21 @@ function getEdgeHistory(edges, source, target) {
|
|
|
862
885
|
function temporalDecay(edge, now, halfLifeMs) {
|
|
863
886
|
const referenceTime = edge.validUntil !== void 0 ? new Date(edge.validUntil).getTime() : new Date(edge.validFrom ?? EPOCH_ISO).getTime();
|
|
864
887
|
const elapsed = now - referenceTime;
|
|
865
|
-
if (elapsed <= 0)
|
|
888
|
+
if (elapsed <= 0)
|
|
889
|
+
return edge.weight;
|
|
866
890
|
const decay = Math.exp(-Math.LN2 * elapsed / halfLifeMs);
|
|
867
891
|
return edge.weight * decay;
|
|
868
892
|
}
|
|
869
893
|
function compactEdges(edges, retentionMs) {
|
|
870
894
|
const cutoff = Date.now() - retentionMs;
|
|
871
895
|
return edges.filter((e) => {
|
|
872
|
-
if (e.supersededAt === void 0)
|
|
896
|
+
if (e.supersededAt === void 0)
|
|
897
|
+
return true;
|
|
873
898
|
return new Date(e.supersededAt).getTime() > cutoff;
|
|
874
899
|
});
|
|
875
900
|
}
|
|
876
901
|
|
|
877
|
-
// ../chitragupta/packages/smriti/
|
|
902
|
+
// ../chitragupta/packages/smriti/dist/graphrag-builder.js
|
|
878
903
|
function scopeToId(scope) {
|
|
879
904
|
switch (scope.type) {
|
|
880
905
|
case "global":
|
|
@@ -919,7 +944,8 @@ async function createSessionNode(ctx, session) {
|
|
|
919
944
|
async function extractConceptsFromNodes(ctx, nodes, edges) {
|
|
920
945
|
const entityIndex = /* @__PURE__ */ new Map();
|
|
921
946
|
for (const node of nodes) {
|
|
922
|
-
if (!node.content || node.type === "concept")
|
|
947
|
+
if (!node.content || node.type === "concept")
|
|
948
|
+
continue;
|
|
923
949
|
const entities = await ctx.extractEntities(node.content);
|
|
924
950
|
for (const entity of entities) {
|
|
925
951
|
const key = entity.name.toLowerCase();
|
|
@@ -935,9 +961,11 @@ async function extractConceptsFromNodes(ctx, nodes, edges) {
|
|
|
935
961
|
}
|
|
936
962
|
}
|
|
937
963
|
for (const [key, data] of entityIndex) {
|
|
938
|
-
if (data.sourceIds.length < 2)
|
|
964
|
+
if (data.sourceIds.length < 2)
|
|
965
|
+
continue;
|
|
939
966
|
const conceptId = `concept-${key.replace(/\s+/g, "-")}`;
|
|
940
|
-
if (nodes.some((n) => n.id === conceptId))
|
|
967
|
+
if (nodes.some((n) => n.id === conceptId))
|
|
968
|
+
continue;
|
|
941
969
|
const conceptNode = {
|
|
942
970
|
id: conceptId,
|
|
943
971
|
type: "concept",
|
|
@@ -1104,21 +1132,15 @@ function removeSessionFromGraph(graph, sessionId) {
|
|
|
1104
1132
|
}
|
|
1105
1133
|
}
|
|
1106
1134
|
graph.nodes = graph.nodes.filter((n) => !nodeIdsToRemove.has(n.id));
|
|
1107
|
-
graph.edges = graph.edges.filter(
|
|
1108
|
-
(e) => !nodeIdsToRemove.has(e.source) && !nodeIdsToRemove.has(e.target)
|
|
1109
|
-
);
|
|
1135
|
+
graph.edges = graph.edges.filter((e) => !nodeIdsToRemove.has(e.source) && !nodeIdsToRemove.has(e.target));
|
|
1110
1136
|
}
|
|
1111
1137
|
function removeMemoryFromGraph(graph, scope) {
|
|
1112
1138
|
const memoryId = scopeToId(scope);
|
|
1113
|
-
graph.nodes = graph.nodes.filter(
|
|
1114
|
-
|
|
1115
|
-
);
|
|
1116
|
-
graph.edges = graph.edges.filter(
|
|
1117
|
-
(e) => e.source !== memoryId && e.target !== memoryId && !e.source.startsWith(`${memoryId}-chunk-`) && !e.target.startsWith(`${memoryId}-chunk-`)
|
|
1118
|
-
);
|
|
1139
|
+
graph.nodes = graph.nodes.filter((n) => n.id !== memoryId && !n.id.startsWith(`${memoryId}-chunk-`));
|
|
1140
|
+
graph.edges = graph.edges.filter((e) => e.source !== memoryId && e.target !== memoryId && !e.source.startsWith(`${memoryId}-chunk-`) && !e.target.startsWith(`${memoryId}-chunk-`));
|
|
1119
1141
|
}
|
|
1120
1142
|
|
|
1121
|
-
// ../chitragupta/packages/smriti/
|
|
1143
|
+
// ../chitragupta/packages/smriti/dist/leiden-algorithm.js
|
|
1122
1144
|
var Xorshift32 = class {
|
|
1123
1145
|
state;
|
|
1124
1146
|
constructor(seed) {
|
|
@@ -1161,8 +1183,10 @@ var AdjacencyGraph = class {
|
|
|
1161
1183
|
for (const edge of graph.edges) {
|
|
1162
1184
|
const s = this.idToIndex.get(edge.source);
|
|
1163
1185
|
const t = this.idToIndex.get(edge.target);
|
|
1164
|
-
if (s === void 0 || t === void 0)
|
|
1165
|
-
|
|
1186
|
+
if (s === void 0 || t === void 0)
|
|
1187
|
+
continue;
|
|
1188
|
+
if (s === t)
|
|
1189
|
+
continue;
|
|
1166
1190
|
const w = edge.weight ?? 1;
|
|
1167
1191
|
this.adj[s].set(t, (this.adj[s].get(t) ?? 0) + w);
|
|
1168
1192
|
this.adj[t].set(s, (this.adj[t].get(s) ?? 0) + w);
|
|
@@ -1175,7 +1199,8 @@ var AdjacencyGraph = class {
|
|
|
1175
1199
|
};
|
|
1176
1200
|
function computeModularity(g, assignment, resolution) {
|
|
1177
1201
|
const twoM = g.totalWeight;
|
|
1178
|
-
if (twoM === 0)
|
|
1202
|
+
if (twoM === 0)
|
|
1203
|
+
return 0;
|
|
1179
1204
|
const internalWeight = /* @__PURE__ */ new Map();
|
|
1180
1205
|
const communityDegree = /* @__PURE__ */ new Map();
|
|
1181
1206
|
for (let i = 0; i < g.n; i++) {
|
|
@@ -1197,15 +1222,19 @@ function computeModularity(g, assignment, resolution) {
|
|
|
1197
1222
|
}
|
|
1198
1223
|
function modularityGain(g, i, cNew, assignment, communityDegree, resolution) {
|
|
1199
1224
|
const twoM = g.totalWeight;
|
|
1200
|
-
if (twoM === 0)
|
|
1225
|
+
if (twoM === 0)
|
|
1226
|
+
return 0;
|
|
1201
1227
|
const cOld = assignment[i];
|
|
1202
|
-
if (cOld === cNew)
|
|
1228
|
+
if (cOld === cNew)
|
|
1229
|
+
return 0;
|
|
1203
1230
|
const ki = g.degree[i];
|
|
1204
1231
|
let kInNew = 0;
|
|
1205
1232
|
let kInOld = 0;
|
|
1206
1233
|
for (const [j, w] of g.adj[i]) {
|
|
1207
|
-
if (assignment[j] === cNew)
|
|
1208
|
-
|
|
1234
|
+
if (assignment[j] === cNew)
|
|
1235
|
+
kInNew += w;
|
|
1236
|
+
if (assignment[j] === cOld)
|
|
1237
|
+
kInOld += w;
|
|
1209
1238
|
}
|
|
1210
1239
|
const sigmaTotNew = communityDegree.get(cNew) ?? 0;
|
|
1211
1240
|
const sigmaTotOld = communityDegree.get(cOld) ?? 0;
|
|
@@ -1235,7 +1264,8 @@ function localNodeMoving(g, assignment, resolution, rng) {
|
|
|
1235
1264
|
let bestCommunity = cOld;
|
|
1236
1265
|
let bestGain = 0;
|
|
1237
1266
|
for (const cNew of neighborCommunities) {
|
|
1238
|
-
if (cNew === cOld)
|
|
1267
|
+
if (cNew === cOld)
|
|
1268
|
+
continue;
|
|
1239
1269
|
const gain = modularityGain(g, i, cNew, assignment, communityDegree, resolution);
|
|
1240
1270
|
if (gain > bestGain) {
|
|
1241
1271
|
bestGain = gain;
|
|
@@ -1255,17 +1285,20 @@ function refineCommunities(g, assignment) {
|
|
|
1255
1285
|
const communityNodes = /* @__PURE__ */ new Map();
|
|
1256
1286
|
for (let i = 0; i < g.n; i++) {
|
|
1257
1287
|
const c = assignment[i];
|
|
1258
|
-
if (!communityNodes.has(c))
|
|
1288
|
+
if (!communityNodes.has(c))
|
|
1289
|
+
communityNodes.set(c, []);
|
|
1259
1290
|
communityNodes.get(c).push(i);
|
|
1260
1291
|
}
|
|
1261
1292
|
let nextCommunityId = Math.max(...assignment) + 1;
|
|
1262
1293
|
for (const [_communityId, nodes] of communityNodes) {
|
|
1263
|
-
if (nodes.length <= 1)
|
|
1294
|
+
if (nodes.length <= 1)
|
|
1295
|
+
continue;
|
|
1264
1296
|
const nodeSet = new Set(nodes);
|
|
1265
1297
|
const visited = /* @__PURE__ */ new Set();
|
|
1266
1298
|
const components = [];
|
|
1267
1299
|
for (const start of nodes) {
|
|
1268
|
-
if (visited.has(start))
|
|
1300
|
+
if (visited.has(start))
|
|
1301
|
+
continue;
|
|
1269
1302
|
const component = [];
|
|
1270
1303
|
const queue = [start];
|
|
1271
1304
|
visited.add(start);
|
|
@@ -1322,7 +1355,7 @@ function compactCommunities(assignment, minSize) {
|
|
|
1322
1355
|
return assignment.map((c) => remap.get(c) ?? 0);
|
|
1323
1356
|
}
|
|
1324
1357
|
|
|
1325
|
-
// ../chitragupta/packages/smriti/
|
|
1358
|
+
// ../chitragupta/packages/smriti/dist/graphrag-leiden.js
|
|
1326
1359
|
var DEFAULT_LEIDEN_CONFIG = {
|
|
1327
1360
|
resolution: 1,
|
|
1328
1361
|
maxIterations: 10,
|
|
@@ -1376,7 +1409,8 @@ function buildCommunityList(g, compacted, resolution) {
|
|
|
1376
1409
|
const communityNodes = /* @__PURE__ */ new Map();
|
|
1377
1410
|
for (let i = 0; i < g.n; i++) {
|
|
1378
1411
|
const c = compacted[i];
|
|
1379
|
-
if (!communityNodes.has(c))
|
|
1412
|
+
if (!communityNodes.has(c))
|
|
1413
|
+
communityNodes.set(c, []);
|
|
1380
1414
|
communityNodes.get(c).push(g.indexToId[i]);
|
|
1381
1415
|
}
|
|
1382
1416
|
const communityList = [];
|
|
@@ -1395,7 +1429,8 @@ function buildCommunityList(g, compacted, resolution) {
|
|
|
1395
1429
|
const possibleEdges = members.length * (members.length - 1) / 2;
|
|
1396
1430
|
const density = possibleEdges > 0 ? internalEdges / possibleEdges : 0;
|
|
1397
1431
|
let ac = 0;
|
|
1398
|
-
for (const mi of memberSet)
|
|
1432
|
+
for (const mi of memberSet)
|
|
1433
|
+
ac += g.degree[mi];
|
|
1399
1434
|
const twoM = g.totalWeight;
|
|
1400
1435
|
const communityMod = twoM > 0 ? 2 * internalWeight / twoM - resolution * (ac / twoM) ** 2 : 0;
|
|
1401
1436
|
communityList.push({
|
|
@@ -1417,9 +1452,7 @@ function annotateCommunities(graph, result) {
|
|
|
1417
1452
|
}
|
|
1418
1453
|
}
|
|
1419
1454
|
function communitySummary(graph, communityId, maxLabels = 5) {
|
|
1420
|
-
const members = graph.nodes.filter(
|
|
1421
|
-
(n) => n.metadata.communityId === communityId
|
|
1422
|
-
);
|
|
1455
|
+
const members = graph.nodes.filter((n) => n.metadata.communityId === communityId);
|
|
1423
1456
|
const typeCount = {};
|
|
1424
1457
|
for (const node of members) {
|
|
1425
1458
|
typeCount[node.type] = (typeCount[node.type] ?? 0) + 1;
|
|
@@ -1441,8 +1474,10 @@ function findBridgeNodes(graph, result, minCommunities = 2) {
|
|
|
1441
1474
|
for (const edge of graph.edges) {
|
|
1442
1475
|
const sc = result.communities.get(edge.source);
|
|
1443
1476
|
const tc = result.communities.get(edge.target);
|
|
1444
|
-
if (sc === void 0 || tc === void 0)
|
|
1445
|
-
|
|
1477
|
+
if (sc === void 0 || tc === void 0)
|
|
1478
|
+
continue;
|
|
1479
|
+
if (sc === tc)
|
|
1480
|
+
continue;
|
|
1446
1481
|
touchedCommunities.get(edge.source)?.add(tc);
|
|
1447
1482
|
touchedCommunities.get(edge.target)?.add(sc);
|
|
1448
1483
|
}
|
|
@@ -1456,7 +1491,7 @@ function findBridgeNodes(graph, result, minCommunities = 2) {
|
|
|
1456
1491
|
return bridges;
|
|
1457
1492
|
}
|
|
1458
1493
|
|
|
1459
|
-
// ../chitragupta/packages/smriti/
|
|
1494
|
+
// ../chitragupta/packages/smriti/dist/graphrag-persistence.js
|
|
1460
1495
|
import fs from "fs";
|
|
1461
1496
|
import path from "path";
|
|
1462
1497
|
function getGraphDir() {
|
|
@@ -1494,26 +1529,17 @@ function insertEdgeToDb(db, edge) {
|
|
|
1494
1529
|
INSERT OR IGNORE INTO edges (source, target, relationship, weight, pramana, viveka,
|
|
1495
1530
|
valid_from, valid_until, recorded_at, superseded_at)
|
|
1496
1531
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1497
|
-
`).run(
|
|
1498
|
-
edge.source,
|
|
1499
|
-
edge.target,
|
|
1500
|
-
edge.relationship,
|
|
1501
|
-
edge.weight,
|
|
1502
|
-
null,
|
|
1503
|
-
null,
|
|
1504
|
-
edge.validFrom ? new Date(edge.validFrom).getTime() : Date.now(),
|
|
1505
|
-
edge.validUntil ? new Date(edge.validUntil).getTime() : null,
|
|
1506
|
-
edge.recordedAt ? new Date(edge.recordedAt).getTime() : Date.now(),
|
|
1507
|
-
edge.supersededAt ? new Date(edge.supersededAt).getTime() : null
|
|
1508
|
-
);
|
|
1532
|
+
`).run(edge.source, edge.target, edge.relationship, edge.weight, null, null, edge.validFrom ? new Date(edge.validFrom).getTime() : Date.now(), edge.validUntil ? new Date(edge.validUntil).getTime() : null, edge.recordedAt ? new Date(edge.recordedAt).getTime() : Date.now(), edge.supersededAt ? new Date(edge.supersededAt).getTime() : null);
|
|
1509
1533
|
}
|
|
1510
1534
|
function saveToSqlite(db, graph, pageRankScores, embeddingCache) {
|
|
1511
1535
|
db.transaction(() => {
|
|
1512
1536
|
db.prepare("DELETE FROM pagerank").run();
|
|
1513
1537
|
db.prepare("DELETE FROM edges").run();
|
|
1514
1538
|
db.prepare("DELETE FROM nodes").run();
|
|
1515
|
-
for (const node of graph.nodes)
|
|
1516
|
-
|
|
1539
|
+
for (const node of graph.nodes)
|
|
1540
|
+
upsertNodeToDb(db, node);
|
|
1541
|
+
for (const edge of graph.edges)
|
|
1542
|
+
insertEdgeToDb(db, edge);
|
|
1517
1543
|
for (const [nodeId, score] of pageRankScores) {
|
|
1518
1544
|
db.prepare("INSERT OR REPLACE INTO pagerank (node_id, score, updated_at) VALUES (?, ?, ?)").run(nodeId, score, Date.now());
|
|
1519
1545
|
}
|
|
@@ -1522,7 +1548,8 @@ function saveToSqlite(db, graph, pageRankScores, embeddingCache) {
|
|
|
1522
1548
|
}
|
|
1523
1549
|
function loadFromSqlite(db) {
|
|
1524
1550
|
const nodeCount = db.prepare("SELECT COUNT(*) as cnt FROM nodes").get().cnt;
|
|
1525
|
-
if (nodeCount === 0)
|
|
1551
|
+
if (nodeCount === 0)
|
|
1552
|
+
return null;
|
|
1526
1553
|
const nodeRows = db.prepare("SELECT * FROM nodes").all();
|
|
1527
1554
|
const nodes = nodeRows.map((row) => ({
|
|
1528
1555
|
id: row.id,
|
|
@@ -1557,8 +1584,10 @@ function loadFromSqlite(db) {
|
|
|
1557
1584
|
}
|
|
1558
1585
|
function migrateInMemoryToSqlite(db, graph, pageRankScores) {
|
|
1559
1586
|
db.transaction(() => {
|
|
1560
|
-
for (const node of graph.nodes)
|
|
1561
|
-
|
|
1587
|
+
for (const node of graph.nodes)
|
|
1588
|
+
upsertNodeToDb(db, node);
|
|
1589
|
+
for (const edge of graph.edges)
|
|
1590
|
+
insertEdgeToDb(db, edge);
|
|
1562
1591
|
for (const [nodeId, score] of pageRankScores) {
|
|
1563
1592
|
db.prepare("INSERT OR REPLACE INTO pagerank (node_id, score, updated_at) VALUES (?, ?, ?)").run(nodeId, score, Date.now());
|
|
1564
1593
|
}
|
|
@@ -1571,7 +1600,8 @@ function saveToJson(graph, pageRankScores, embeddingCache) {
|
|
|
1571
1600
|
fs.writeFileSync(getGraphPath(), JSON.stringify(graph, null, " "), "utf-8");
|
|
1572
1601
|
saveEmbeddingsJson(embeddingCache);
|
|
1573
1602
|
const pageRankObj = {};
|
|
1574
|
-
for (const [key, value] of pageRankScores)
|
|
1603
|
+
for (const [key, value] of pageRankScores)
|
|
1604
|
+
pageRankObj[key] = value;
|
|
1575
1605
|
fs.writeFileSync(getPageRankPath(), JSON.stringify(pageRankObj, null, " "), "utf-8");
|
|
1576
1606
|
} catch {
|
|
1577
1607
|
}
|
|
@@ -1591,7 +1621,8 @@ function loadFromJson() {
|
|
|
1591
1621
|
const prPath = getPageRankPath();
|
|
1592
1622
|
if (fs.existsSync(prPath)) {
|
|
1593
1623
|
const ranks = JSON.parse(fs.readFileSync(prPath, "utf-8"));
|
|
1594
|
-
for (const [key, value] of Object.entries(ranks))
|
|
1624
|
+
for (const [key, value] of Object.entries(ranks))
|
|
1625
|
+
pageRankScores.set(key, value);
|
|
1595
1626
|
}
|
|
1596
1627
|
} catch {
|
|
1597
1628
|
}
|
|
@@ -1602,7 +1633,8 @@ function saveEmbeddingsJson(embeddingCache) {
|
|
|
1602
1633
|
const dir = getGraphDir();
|
|
1603
1634
|
fs.mkdirSync(dir, { recursive: true });
|
|
1604
1635
|
const embeddings = {};
|
|
1605
|
-
for (const [key, value] of embeddingCache)
|
|
1636
|
+
for (const [key, value] of embeddingCache)
|
|
1637
|
+
embeddings[key] = value;
|
|
1606
1638
|
fs.writeFileSync(getEmbeddingsPath(), JSON.stringify(embeddings, null, " "), "utf-8");
|
|
1607
1639
|
} catch {
|
|
1608
1640
|
}
|
|
@@ -1613,10 +1645,12 @@ function loadEmbeddingsJson(maxCacheSize) {
|
|
|
1613
1645
|
const embPath = getEmbeddingsPath();
|
|
1614
1646
|
if (fs.existsSync(embPath)) {
|
|
1615
1647
|
const embeddings = JSON.parse(fs.readFileSync(embPath, "utf-8"));
|
|
1616
|
-
for (const [key, value] of Object.entries(embeddings))
|
|
1648
|
+
for (const [key, value] of Object.entries(embeddings))
|
|
1649
|
+
cache.set(key, value);
|
|
1617
1650
|
while (cache.size > maxCacheSize) {
|
|
1618
1651
|
const oldest = cache.keys().next().value;
|
|
1619
|
-
if (oldest !== void 0)
|
|
1652
|
+
if (oldest !== void 0)
|
|
1653
|
+
cache.delete(oldest);
|
|
1620
1654
|
}
|
|
1621
1655
|
}
|
|
1622
1656
|
} catch {
|
|
@@ -1624,23 +1658,27 @@ function loadEmbeddingsJson(maxCacheSize) {
|
|
|
1624
1658
|
return cache;
|
|
1625
1659
|
}
|
|
1626
1660
|
function lookupPramana(db, nodeId) {
|
|
1627
|
-
if (!db)
|
|
1661
|
+
if (!db)
|
|
1662
|
+
return "shabda";
|
|
1628
1663
|
try {
|
|
1629
1664
|
const row = db.prepare(`
|
|
1630
1665
|
SELECT pramana, COUNT(*) as cnt FROM edges
|
|
1631
1666
|
WHERE (source = ? OR target = ?) AND pramana IS NOT NULL
|
|
1632
1667
|
GROUP BY pramana ORDER BY cnt DESC LIMIT 1
|
|
1633
1668
|
`).get(nodeId, nodeId);
|
|
1634
|
-
if (row?.pramana)
|
|
1669
|
+
if (row?.pramana)
|
|
1670
|
+
return row.pramana;
|
|
1635
1671
|
} catch {
|
|
1636
1672
|
}
|
|
1637
1673
|
return "shabda";
|
|
1638
1674
|
}
|
|
1639
1675
|
function lookupPramanaBatch(db, nodeIds) {
|
|
1640
1676
|
const result = /* @__PURE__ */ new Map();
|
|
1641
|
-
if (nodeIds.length === 0)
|
|
1677
|
+
if (nodeIds.length === 0)
|
|
1678
|
+
return result;
|
|
1642
1679
|
if (!db) {
|
|
1643
|
-
for (const id of nodeIds)
|
|
1680
|
+
for (const id of nodeIds)
|
|
1681
|
+
result.set(id, "shabda");
|
|
1644
1682
|
return result;
|
|
1645
1683
|
}
|
|
1646
1684
|
try {
|
|
@@ -1656,26 +1694,31 @@ function lookupPramanaBatch(db, nodeIds) {
|
|
|
1656
1694
|
GROUP BY target, pramana
|
|
1657
1695
|
) GROUP BY node_id HAVING cnt = MAX(cnt)
|
|
1658
1696
|
`).all(...nodeIds, ...nodeIds);
|
|
1659
|
-
for (const row of rows)
|
|
1697
|
+
for (const row of rows)
|
|
1698
|
+
result.set(row.node_id, row.pramana);
|
|
1660
1699
|
} catch {
|
|
1661
1700
|
}
|
|
1662
1701
|
for (const id of nodeIds) {
|
|
1663
|
-
if (!result.has(id))
|
|
1702
|
+
if (!result.has(id))
|
|
1703
|
+
result.set(id, "shabda");
|
|
1664
1704
|
}
|
|
1665
1705
|
return result;
|
|
1666
1706
|
}
|
|
1667
1707
|
function migrateGraphJson() {
|
|
1668
1708
|
const graphPath = getGraphPath();
|
|
1669
1709
|
const prPath = getPageRankPath();
|
|
1670
|
-
if (!fs.existsSync(graphPath))
|
|
1710
|
+
if (!fs.existsSync(graphPath))
|
|
1711
|
+
return { nodes: 0, edges: 0 };
|
|
1671
1712
|
try {
|
|
1672
1713
|
const graph = JSON.parse(fs.readFileSync(graphPath, "utf-8"));
|
|
1673
|
-
if (!graph.nodes || graph.nodes.length === 0)
|
|
1714
|
+
if (!graph.nodes || graph.nodes.length === 0)
|
|
1715
|
+
return { nodes: 0, edges: 0 };
|
|
1674
1716
|
const pageRankScores = /* @__PURE__ */ new Map();
|
|
1675
1717
|
try {
|
|
1676
1718
|
if (fs.existsSync(prPath)) {
|
|
1677
1719
|
const ranks = JSON.parse(fs.readFileSync(prPath, "utf-8"));
|
|
1678
|
-
for (const [key, value] of Object.entries(ranks))
|
|
1720
|
+
for (const [key, value] of Object.entries(ranks))
|
|
1721
|
+
pageRankScores.set(key, value);
|
|
1679
1722
|
}
|
|
1680
1723
|
} catch {
|
|
1681
1724
|
}
|
|
@@ -1692,35 +1735,24 @@ function migrateGraphJson() {
|
|
|
1692
1735
|
valid_from, valid_until, recorded_at, superseded_at)
|
|
1693
1736
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1694
1737
|
`);
|
|
1695
|
-
const prStmt = db.prepare(
|
|
1696
|
-
"INSERT OR REPLACE INTO pagerank (node_id, score, updated_at) VALUES (?, ?, ?)"
|
|
1697
|
-
);
|
|
1738
|
+
const prStmt = db.prepare("INSERT OR REPLACE INTO pagerank (node_id, score, updated_at) VALUES (?, ?, ?)");
|
|
1698
1739
|
const now = Date.now();
|
|
1699
1740
|
for (const node of graph.nodes) {
|
|
1700
1741
|
upsertStmt.run(node.id, node.type, node.label, node.content, JSON.stringify(node.metadata), now, now);
|
|
1701
1742
|
}
|
|
1702
1743
|
for (const edge of graph.edges) {
|
|
1703
|
-
edgeStmt.run(
|
|
1704
|
-
edge.source,
|
|
1705
|
-
edge.target,
|
|
1706
|
-
edge.relationship,
|
|
1707
|
-
edge.weight,
|
|
1708
|
-
null,
|
|
1709
|
-
null,
|
|
1710
|
-
edge.validFrom ? new Date(edge.validFrom).getTime() : now,
|
|
1711
|
-
edge.validUntil ? new Date(edge.validUntil).getTime() : null,
|
|
1712
|
-
edge.recordedAt ? new Date(edge.recordedAt).getTime() : now,
|
|
1713
|
-
edge.supersededAt ? new Date(edge.supersededAt).getTime() : null
|
|
1714
|
-
);
|
|
1744
|
+
edgeStmt.run(edge.source, edge.target, edge.relationship, edge.weight, null, null, edge.validFrom ? new Date(edge.validFrom).getTime() : now, edge.validUntil ? new Date(edge.validUntil).getTime() : null, edge.recordedAt ? new Date(edge.recordedAt).getTime() : now, edge.supersededAt ? new Date(edge.supersededAt).getTime() : null);
|
|
1715
1745
|
}
|
|
1716
|
-
for (const [nodeId, score] of pageRankScores)
|
|
1746
|
+
for (const [nodeId, score] of pageRankScores)
|
|
1747
|
+
prStmt.run(nodeId, score, now);
|
|
1717
1748
|
})();
|
|
1718
1749
|
try {
|
|
1719
1750
|
fs.renameSync(graphPath, graphPath + ".bak");
|
|
1720
1751
|
} catch {
|
|
1721
1752
|
}
|
|
1722
1753
|
try {
|
|
1723
|
-
if (fs.existsSync(prPath))
|
|
1754
|
+
if (fs.existsSync(prPath))
|
|
1755
|
+
fs.renameSync(prPath, prPath + ".bak");
|
|
1724
1756
|
} catch {
|
|
1725
1757
|
}
|
|
1726
1758
|
return { nodes: graph.nodes.length, edges: graph.edges.length };
|
|
@@ -1729,7 +1761,7 @@ function migrateGraphJson() {
|
|
|
1729
1761
|
}
|
|
1730
1762
|
}
|
|
1731
1763
|
|
|
1732
|
-
// ../chitragupta/packages/smriti/
|
|
1764
|
+
// ../chitragupta/packages/smriti/dist/graphrag.js
|
|
1733
1765
|
var DEFAULT_CONFIG = {
|
|
1734
1766
|
endpoint: process.env.OLLAMA_HOST ?? "http://localhost:11434",
|
|
1735
1767
|
model: "nomic-embed-text",
|
|
@@ -1757,7 +1789,8 @@ var GraphRAGEngine = class {
|
|
|
1757
1789
|
this.loadFromDisk();
|
|
1758
1790
|
}
|
|
1759
1791
|
async checkOllamaAvailability() {
|
|
1760
|
-
if (this.ollamaAvailable !== null)
|
|
1792
|
+
if (this.ollamaAvailable !== null)
|
|
1793
|
+
return this.ollamaAvailable;
|
|
1761
1794
|
try {
|
|
1762
1795
|
const response = await fetch(`${this.config.endpoint}/api/version`, {
|
|
1763
1796
|
method: "GET",
|
|
@@ -1772,12 +1805,14 @@ var GraphRAGEngine = class {
|
|
|
1772
1805
|
/** Get (or compute and cache) an embedding vector for the given text. */
|
|
1773
1806
|
async getEmbedding(text) {
|
|
1774
1807
|
const cached = this.embeddingCache.get(text);
|
|
1775
|
-
if (cached)
|
|
1808
|
+
if (cached)
|
|
1809
|
+
return cached;
|
|
1776
1810
|
const vector = await this.embeddingService.getEmbedding(text);
|
|
1777
1811
|
this.embeddingCache.set(text, vector);
|
|
1778
1812
|
if (this.embeddingCache.size > this.maxEmbeddingCacheSize) {
|
|
1779
1813
|
const oldest = this.embeddingCache.keys().next().value;
|
|
1780
|
-
if (oldest !== void 0)
|
|
1814
|
+
if (oldest !== void 0)
|
|
1815
|
+
this.embeddingCache.delete(oldest);
|
|
1781
1816
|
}
|
|
1782
1817
|
return vector;
|
|
1783
1818
|
}
|
|
@@ -1838,14 +1873,18 @@ var GraphRAGEngine = class {
|
|
|
1838
1873
|
}
|
|
1839
1874
|
/** Feed removed edges into incremental PR engine. */
|
|
1840
1875
|
applyEdgeRemovals(edges) {
|
|
1841
|
-
if (!this.incrementalPR || edges.length === 0)
|
|
1842
|
-
|
|
1876
|
+
if (!this.incrementalPR || edges.length === 0)
|
|
1877
|
+
return;
|
|
1878
|
+
for (const edge of edges)
|
|
1879
|
+
this.incrementalPR.removeEdge(edge.source, edge.target);
|
|
1843
1880
|
this.pageRankScores = this.incrementalPR.getRanks();
|
|
1844
1881
|
}
|
|
1845
1882
|
/** Feed newly added edges into incremental PR engine. */
|
|
1846
1883
|
applyEdgeAdditions(edges) {
|
|
1847
|
-
if (!this.incrementalPR || edges.length === 0)
|
|
1848
|
-
|
|
1884
|
+
if (!this.incrementalPR || edges.length === 0)
|
|
1885
|
+
return;
|
|
1886
|
+
for (const edge of edges)
|
|
1887
|
+
this.incrementalPR.addEdge(edge.source, edge.target);
|
|
1849
1888
|
this.pageRankScores = this.incrementalPR.getRanks();
|
|
1850
1889
|
}
|
|
1851
1890
|
/**
|
|
@@ -1878,7 +1917,8 @@ var GraphRAGEngine = class {
|
|
|
1878
1917
|
nodes.push(await createSessionNode(this, session));
|
|
1879
1918
|
await indexSessionTurns(this, session, nodes, edges);
|
|
1880
1919
|
}
|
|
1881
|
-
for (const memory of memories)
|
|
1920
|
+
for (const memory of memories)
|
|
1921
|
+
await buildMemoryNodes(this, memory, nodes, edges);
|
|
1882
1922
|
await extractConceptsFromNodes(this, nodes, edges);
|
|
1883
1923
|
this.graph = { nodes, edges };
|
|
1884
1924
|
if (this.graph.nodes.length > 0 && this.graph.edges.length > 0) {
|
|
@@ -1892,13 +1932,16 @@ var GraphRAGEngine = class {
|
|
|
1892
1932
|
/** Search the knowledge graph using hybrid scoring (cosine + PageRank + BM25). */
|
|
1893
1933
|
async search(query, graph, topK = 10) {
|
|
1894
1934
|
const searchGraph = graph ?? this.graph;
|
|
1895
|
-
if (searchGraph.nodes.length === 0)
|
|
1935
|
+
if (searchGraph.nodes.length === 0)
|
|
1936
|
+
return [];
|
|
1896
1937
|
const queryEmbedding = await this.getEmbedding(query);
|
|
1897
|
-
if (this.pageRankScores.size === 0)
|
|
1938
|
+
if (this.pageRankScores.size === 0)
|
|
1939
|
+
this.computePageRank(searchGraph);
|
|
1898
1940
|
let maxPageRank = 0;
|
|
1899
1941
|
for (const node of searchGraph.nodes) {
|
|
1900
1942
|
const pr = this.pageRankScores.get(node.id) ?? 0;
|
|
1901
|
-
if (pr > maxPageRank)
|
|
1943
|
+
if (pr > maxPageRank)
|
|
1944
|
+
maxPageRank = pr;
|
|
1902
1945
|
}
|
|
1903
1946
|
const scored = [];
|
|
1904
1947
|
for (const node of searchGraph.nodes) {
|
|
@@ -1910,7 +1953,8 @@ var GraphRAGEngine = class {
|
|
|
1910
1953
|
const normPR = maxPageRank > 0 ? rawPR / maxPageRank : 0;
|
|
1911
1954
|
const textScore = textMatchScore(query, node.content + " " + node.label);
|
|
1912
1955
|
const finalScore = ALPHA * cosineSim + BETA * normPR + GAMMA * textScore;
|
|
1913
|
-
if (finalScore > 0)
|
|
1956
|
+
if (finalScore > 0)
|
|
1957
|
+
scored.push({ node, score: finalScore });
|
|
1914
1958
|
}
|
|
1915
1959
|
scored.sort((a, b) => b.score - a.score);
|
|
1916
1960
|
return scored.slice(0, topK).map((s) => s.node);
|
|
@@ -2012,7 +2056,8 @@ var GraphRAGEngine = class {
|
|
|
2012
2056
|
for (const [k, v] of loadEmbeddingsJson(this.maxEmbeddingCacheSize)) {
|
|
2013
2057
|
this.embeddingCache.set(k, v);
|
|
2014
2058
|
}
|
|
2015
|
-
if (this.graph.edges.length > 0)
|
|
2059
|
+
if (this.graph.edges.length > 0)
|
|
2060
|
+
this.initIncrementalPR();
|
|
2016
2061
|
}
|
|
2017
2062
|
/** Get the current in-memory knowledge graph. */
|
|
2018
2063
|
getGraph() {
|
|
@@ -2026,7 +2071,8 @@ var GraphRAGEngine = class {
|
|
|
2026
2071
|
this.incrementalPR = null;
|
|
2027
2072
|
try {
|
|
2028
2073
|
const db = this.getGraphDbHandle();
|
|
2029
|
-
if (db)
|
|
2074
|
+
if (db)
|
|
2075
|
+
db.exec("DELETE FROM pagerank; DELETE FROM edges; DELETE FROM nodes;");
|
|
2030
2076
|
} catch {
|
|
2031
2077
|
}
|
|
2032
2078
|
this.saveToDisk();
|
|
@@ -2042,8 +2088,10 @@ var GraphRAGEngine = class {
|
|
|
2042
2088
|
*/
|
|
2043
2089
|
getNeighbors(nodeId, direction = "both") {
|
|
2044
2090
|
return this.graph.edges.filter((edge) => {
|
|
2045
|
-
if (direction === "out")
|
|
2046
|
-
|
|
2091
|
+
if (direction === "out")
|
|
2092
|
+
return edge.source === nodeId;
|
|
2093
|
+
if (direction === "in")
|
|
2094
|
+
return edge.target === nodeId;
|
|
2047
2095
|
return edge.source === nodeId || edge.target === nodeId;
|
|
2048
2096
|
});
|
|
2049
2097
|
}
|
|
@@ -2068,4 +2116,4 @@ export {
|
|
|
2068
2116
|
migrateGraphJson,
|
|
2069
2117
|
GraphRAGEngine
|
|
2070
2118
|
};
|
|
2071
|
-
//# sourceMappingURL=chunk-
|
|
2119
|
+
//# sourceMappingURL=chunk-LJUEMPLG.js.map
|