@yugenlab/vaayu 0.1.10 → 0.1.12
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-NQESOBLC.js} +2 -2
- package/chunks/akasha-5C5Q6NMP.js +12 -0
- package/chunks/{chunk-JGI4SDWS.js → chunk-26K6DS6N.js} +7 -5
- package/chunks/chunk-5E3ZS5SW.js +529 -0
- package/chunks/{chunk-VJHNE47S.js → chunk-ARZCIITZ.js} +104 -94
- package/chunks/{chunk-PRXQW76U.js → chunk-EG37M4QL.js} +17 -6
- package/chunks/{chunk-HIYHTWFW.js → chunk-FEDPZOZ5.js} +572 -441
- package/chunks/{chunk-O4KV7TFP.js → chunk-GWYC7R2L.js} +38 -20
- package/chunks/chunk-H46F2Y6R.js +134 -0
- package/chunks/{chunk-M7THR63C.js → chunk-HXHDP2PZ.js} +78 -65
- package/chunks/chunk-KVQH4LE7.js +396 -0
- package/chunks/{chunk-TND3MU4Z.js → chunk-LJCT7UYP.js} +86 -68
- package/chunks/{chunk-OT4G2L46.js → chunk-M2RLX5LU.js} +229 -163
- package/chunks/{chunk-IGKYKEKT.js → chunk-NAQKA54E.js} +8 -2
- package/chunks/{chunk-77725AR7.js → chunk-PZ4AQ22L.js} +151 -57
- package/chunks/{chunk-C76USAC5.js → chunk-QFGAB4XD.js} +13 -5
- package/chunks/{chunk-MJ74G5RB.js → chunk-R273KC7J.js} +276 -3
- package/chunks/{chunk-JAWZ7ANC.js → chunk-RVKTGKFD.js} +12 -8
- package/chunks/{chunk-AGK3A7R7.js → chunk-TSOQ2CT3.js} +1430 -1173
- package/chunks/{chunk-YJRXLRTE.js → chunk-VEZ2DI2M.js} +24 -12
- package/chunks/{chunk-N22M7D4P.js → chunk-XP3NIH5F.js} +91 -98
- package/chunks/{chunk-6556EKOB.js → chunk-Y6IZH6FT.js} +42 -26
- package/chunks/{chunk-AS3DJFY3.js → chunk-YRTGGYJU.js} +45 -41
- package/chunks/{consolidation-indexer-VKQ6DNU3.js → consolidation-indexer-KPXORCJ4.js} +9 -9
- package/chunks/database-BX3LVYXS.js +11 -0
- package/chunks/{day-consolidation-BH3QU2SZ.js → day-consolidation-CR3TJFAL.js} +5 -5
- package/chunks/{src-Y3TGMINC.js → dist-ESCM3CP5.js} +31 -21
- package/chunks/graphrag-73XA7LBX.js +14 -0
- package/chunks/hierarchical-temporal-search-GHKVKNZ6.js +8 -0
- package/chunks/hybrid-search-OD756RDV.js +20 -0
- package/chunks/{memory-store-A6WOWLWC.js → memory-store-4GCBR2DZ.js} +4 -4
- package/chunks/periodic-consolidation-IINCHP6L.js +11 -0
- package/chunks/postgres-YLCUNVPQ.js +8 -0
- package/chunks/recall-64RROTUC.js +21 -0
- package/chunks/search-JVCDNTAJ.js +19 -0
- package/chunks/{session-store-3BRPGC6P.js → session-store-3EDQZEDS.js} +12 -6
- package/chunks/{sqlite-DHUQGPR5.js → sqlite-4N7YH2KK.js} +3 -3
- package/chunks/{src-6GVZTUH6.js → src-OPSDZEFI.js} +2 -2
- package/chunks/{suncalc-NOHGYHDU.js → suncalc-RM7URNUR.js} +2 -2
- package/chunks/{tree-RSHKDTCR.js → tree-FIUVGJ5J.js} +2 -2
- package/chunks/{vasana-engine-MU25OQ23.js → vasana-engine-W4PYWT5H.js} +5 -5
- package/gateway.js +38859 -30650
- package/package.json +1 -1
- package/pair-cli.js +2 -2
- package/chunks/chunk-U62ABYKD.js +0 -123
- package/chunks/chunk-UZ6OIVEC.js +0 -198
- package/chunks/graphrag-D7OXWAWD.js +0 -14
- package/chunks/hierarchical-temporal-search-PVHVA3NZ.js +0 -8
- package/chunks/hybrid-search-G2NAJKJ7.js +0 -20
- package/chunks/periodic-consolidation-LMYMNS4Q.js +0 -11
- package/chunks/postgres-WLH3D5HG.js +0 -8
- package/chunks/recall-ZNL4DJ2L.js +0 -21
- package/chunks/search-35JMSGUT.js +0 -19
|
@@ -6,24 +6,28 @@ import {
|
|
|
6
6
|
STOP_WORDS,
|
|
7
7
|
cosineSimilarity,
|
|
8
8
|
textMatchScore
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-RVKTGKFD.js";
|
|
10
10
|
import {
|
|
11
|
-
initGraphSchema
|
|
12
|
-
|
|
11
|
+
initGraphSchema,
|
|
12
|
+
init_schema
|
|
13
|
+
} from "./chunk-VEZ2DI2M.js";
|
|
13
14
|
import {
|
|
14
|
-
DatabaseManager
|
|
15
|
-
|
|
15
|
+
DatabaseManager,
|
|
16
|
+
init_database
|
|
17
|
+
} from "./chunk-H46F2Y6R.js";
|
|
16
18
|
import {
|
|
17
|
-
getChitraguptaHome
|
|
18
|
-
|
|
19
|
+
getChitraguptaHome,
|
|
20
|
+
init_dist
|
|
21
|
+
} from "./chunk-KVQH4LE7.js";
|
|
19
22
|
|
|
20
|
-
// ../chitragupta/packages/smriti/
|
|
23
|
+
// ../chitragupta/packages/smriti/dist/graphrag-pagerank.js
|
|
21
24
|
var PAGERANK_DAMPING = 0.85;
|
|
22
25
|
var PAGERANK_EPSILON = 1e-4;
|
|
23
26
|
var PAGERANK_MAX_ITERATIONS = 100;
|
|
24
27
|
function computePageRank(graph) {
|
|
25
28
|
const N = graph.nodes.length;
|
|
26
|
-
if (N === 0)
|
|
29
|
+
if (N === 0)
|
|
30
|
+
return /* @__PURE__ */ new Map();
|
|
27
31
|
const ranks = /* @__PURE__ */ new Map();
|
|
28
32
|
const nodeIds = /* @__PURE__ */ new Set();
|
|
29
33
|
for (const node of graph.nodes) {
|
|
@@ -63,17 +67,19 @@ function computePageRank(graph) {
|
|
|
63
67
|
const rank = baseFactor + PAGERANK_DAMPING * (incomingRank + danglingRank / N);
|
|
64
68
|
newRanks.set(node.id, rank);
|
|
65
69
|
const delta = Math.abs(rank - (ranks.get(node.id) ?? 0));
|
|
66
|
-
if (delta > maxDelta)
|
|
70
|
+
if (delta > maxDelta)
|
|
71
|
+
maxDelta = delta;
|
|
67
72
|
}
|
|
68
73
|
for (const [id, rank] of newRanks) {
|
|
69
74
|
ranks.set(id, rank);
|
|
70
75
|
}
|
|
71
|
-
if (maxDelta < PAGERANK_EPSILON)
|
|
76
|
+
if (maxDelta < PAGERANK_EPSILON)
|
|
77
|
+
break;
|
|
72
78
|
}
|
|
73
79
|
return ranks;
|
|
74
80
|
}
|
|
75
81
|
|
|
76
|
-
// ../chitragupta/packages/smriti/
|
|
82
|
+
// ../chitragupta/packages/smriti/dist/graphrag-pagerank-incremental.js
|
|
77
83
|
var IncrementalPageRank = class {
|
|
78
84
|
ranks = /* @__PURE__ */ new Map();
|
|
79
85
|
outDegree = /* @__PURE__ */ new Map();
|
|
@@ -155,7 +161,8 @@ var IncrementalPageRank = class {
|
|
|
155
161
|
if (oldDeg > 0) {
|
|
156
162
|
const shareDelta = prU * (1 / newDeg - 1 / oldDeg);
|
|
157
163
|
for (const neighbor of this.getOutNeighbors(source)) {
|
|
158
|
-
if (neighbor === target)
|
|
164
|
+
if (neighbor === target)
|
|
165
|
+
continue;
|
|
159
166
|
const prev = residuals.get(neighbor) ?? 0;
|
|
160
167
|
residuals.set(neighbor, prev + this.damping * shareDelta);
|
|
161
168
|
}
|
|
@@ -169,11 +176,14 @@ var IncrementalPageRank = class {
|
|
|
169
176
|
* @param target - Target node ID.
|
|
170
177
|
*/
|
|
171
178
|
removeEdge(source, target) {
|
|
172
|
-
if (!this.nodeSet.has(source) || !this.nodeSet.has(target))
|
|
179
|
+
if (!this.nodeSet.has(source) || !this.nodeSet.has(target))
|
|
180
|
+
return;
|
|
173
181
|
const inSet = this.inLinks.get(target);
|
|
174
|
-
if (!inSet || !inSet.has(source))
|
|
182
|
+
if (!inSet || !inSet.has(source))
|
|
183
|
+
return;
|
|
175
184
|
const oldDeg = this.outDegree.get(source) ?? 0;
|
|
176
|
-
if (oldDeg <= 0)
|
|
185
|
+
if (oldDeg <= 0)
|
|
186
|
+
return;
|
|
177
187
|
const newDeg = oldDeg - 1;
|
|
178
188
|
this.outDegree.set(source, newDeg);
|
|
179
189
|
inSet.delete(source);
|
|
@@ -216,7 +226,8 @@ var IncrementalPageRank = class {
|
|
|
216
226
|
maxNode = node;
|
|
217
227
|
}
|
|
218
228
|
}
|
|
219
|
-
if (maxNode === null || maxResidual < this.epsilon)
|
|
229
|
+
if (maxNode === null || maxResidual < this.epsilon)
|
|
230
|
+
break;
|
|
220
231
|
const res = residuals.get(maxNode);
|
|
221
232
|
this.ranks.set(maxNode, (this.ranks.get(maxNode) ?? 0) + res);
|
|
222
233
|
residuals.delete(maxNode);
|
|
@@ -240,13 +251,14 @@ var IncrementalPageRank = class {
|
|
|
240
251
|
}
|
|
241
252
|
};
|
|
242
253
|
|
|
243
|
-
// ../chitragupta/packages/smriti/
|
|
254
|
+
// ../chitragupta/packages/smriti/dist/graphrag-pagerank-personalized.js
|
|
244
255
|
function buildTopicBias(nodeIds, nodeContents, topic) {
|
|
245
256
|
const bias = /* @__PURE__ */ new Map();
|
|
246
257
|
const n = nodeIds.length;
|
|
247
258
|
if (!topic || n === 0) {
|
|
248
259
|
const uniform = 1 / Math.max(n, 1);
|
|
249
|
-
for (const id of nodeIds)
|
|
260
|
+
for (const id of nodeIds)
|
|
261
|
+
bias.set(id, uniform);
|
|
250
262
|
return bias;
|
|
251
263
|
}
|
|
252
264
|
const queryTerms = tokenizeSimple(topic);
|
|
@@ -263,7 +275,8 @@ function buildTopicBias(nodeIds, nodeContents, topic) {
|
|
|
263
275
|
}
|
|
264
276
|
if (totalSim === 0) {
|
|
265
277
|
const uniform = 1 / n;
|
|
266
|
-
for (const id of nodeIds)
|
|
278
|
+
for (const id of nodeIds)
|
|
279
|
+
bias.set(id, uniform);
|
|
267
280
|
return bias;
|
|
268
281
|
}
|
|
269
282
|
for (let i = 0; i < n; i++) {
|
|
@@ -288,7 +301,8 @@ function tfCosineSimilarity(a, b) {
|
|
|
288
301
|
for (const [term, freqA] of a) {
|
|
289
302
|
normA += freqA * freqA;
|
|
290
303
|
const freqB = b.get(term);
|
|
291
|
-
if (freqB !== void 0)
|
|
304
|
+
if (freqB !== void 0)
|
|
305
|
+
dot += freqA * freqB;
|
|
292
306
|
}
|
|
293
307
|
for (const freqB of b.values()) {
|
|
294
308
|
normB += freqB * freqB;
|
|
@@ -302,7 +316,8 @@ function computePersonalizedPageRank(graph, topicBias, opts) {
|
|
|
302
316
|
const maxIter = opts?.maxIterations ?? 150;
|
|
303
317
|
const useGS = opts?.useGaussSeidel ?? true;
|
|
304
318
|
const N = graph.nodes.length;
|
|
305
|
-
if (N === 0)
|
|
319
|
+
if (N === 0)
|
|
320
|
+
return /* @__PURE__ */ new Map();
|
|
306
321
|
const nodeIds = graph.nodes.map((n) => n.id);
|
|
307
322
|
const nodeIdSet = new Set(nodeIds);
|
|
308
323
|
const outDegree = /* @__PURE__ */ new Map();
|
|
@@ -336,7 +351,8 @@ function computePersonalizedPageRank(graph, topicBias, opts) {
|
|
|
336
351
|
}
|
|
337
352
|
const danglingNodes = [];
|
|
338
353
|
for (const id of nodeIds) {
|
|
339
|
-
if ((outDegree.get(id) ?? 0) === 0)
|
|
354
|
+
if ((outDegree.get(id) ?? 0) === 0)
|
|
355
|
+
danglingNodes.push(id);
|
|
340
356
|
}
|
|
341
357
|
for (let iter = 0; iter < maxIter; iter++) {
|
|
342
358
|
let danglingSum = 0;
|
|
@@ -357,7 +373,8 @@ function computePersonalizedPageRank(graph, topicBias, opts) {
|
|
|
357
373
|
const newRank = (1 - damping) * biasVal + damping * incomingSum + danglingContrib;
|
|
358
374
|
const oldRank = ranks.get(id) ?? 0;
|
|
359
375
|
const delta = Math.abs(newRank - oldRank);
|
|
360
|
-
if (delta > maxDelta)
|
|
376
|
+
if (delta > maxDelta)
|
|
377
|
+
maxDelta = delta;
|
|
361
378
|
ranks.set(id, newRank);
|
|
362
379
|
}
|
|
363
380
|
} else {
|
|
@@ -373,16 +390,19 @@ function computePersonalizedPageRank(graph, topicBias, opts) {
|
|
|
373
390
|
const newRank = (1 - damping) * biasVal + damping * incomingSum + danglingContrib;
|
|
374
391
|
newRanks.set(id, newRank);
|
|
375
392
|
const delta = Math.abs(newRank - (ranks.get(id) ?? 0));
|
|
376
|
-
if (delta > maxDelta)
|
|
393
|
+
if (delta > maxDelta)
|
|
394
|
+
maxDelta = delta;
|
|
377
395
|
}
|
|
378
|
-
for (const [id, rank] of newRanks)
|
|
396
|
+
for (const [id, rank] of newRanks)
|
|
397
|
+
ranks.set(id, rank);
|
|
379
398
|
}
|
|
380
|
-
if (maxDelta < epsilon)
|
|
399
|
+
if (maxDelta < epsilon)
|
|
400
|
+
break;
|
|
381
401
|
}
|
|
382
402
|
return ranks;
|
|
383
403
|
}
|
|
384
404
|
|
|
385
|
-
// ../chitragupta/packages/smriti/
|
|
405
|
+
// ../chitragupta/packages/smriti/dist/graphrag-extraction.js
|
|
386
406
|
var CHUNK_TARGET_TOKENS = 350;
|
|
387
407
|
var CHUNK_MIN_TOKENS = 200;
|
|
388
408
|
var CHUNK_MAX_TOKENS = 500;
|
|
@@ -396,7 +416,8 @@ function estimateTokens(text) {
|
|
|
396
416
|
}
|
|
397
417
|
function semanticChunk(text) {
|
|
398
418
|
const sentences = splitSentences(text);
|
|
399
|
-
if (sentences.length === 0)
|
|
419
|
+
if (sentences.length === 0)
|
|
420
|
+
return [];
|
|
400
421
|
const totalTokens = estimateTokens(text);
|
|
401
422
|
if (totalTokens <= CHUNK_MAX_TOKENS) {
|
|
402
423
|
return [{ text, startSentence: 0, endSentence: sentences.length - 1 }];
|
|
@@ -425,7 +446,8 @@ function semanticChunk(text) {
|
|
|
425
446
|
overlapTokens += estimateTokens(sentences[overlapStart]);
|
|
426
447
|
}
|
|
427
448
|
startIdx = Math.max(overlapStart, startIdx + 1);
|
|
428
|
-
if (startIdx >= endIdx)
|
|
449
|
+
if (startIdx >= endIdx)
|
|
450
|
+
break;
|
|
429
451
|
}
|
|
430
452
|
return chunks;
|
|
431
453
|
}
|
|
@@ -483,8 +505,10 @@ function keywordExtractEntities(text) {
|
|
|
483
505
|
const entities = [];
|
|
484
506
|
const sorted = [...freq.entries()].sort((a, b) => b[1] - a[1]);
|
|
485
507
|
for (const [word, count] of sorted) {
|
|
486
|
-
if (count < 2)
|
|
487
|
-
|
|
508
|
+
if (count < 2)
|
|
509
|
+
break;
|
|
510
|
+
if (entities.length >= 20)
|
|
511
|
+
break;
|
|
488
512
|
entities.push({
|
|
489
513
|
name: word,
|
|
490
514
|
type: "concept",
|
|
@@ -494,7 +518,7 @@ function keywordExtractEntities(text) {
|
|
|
494
518
|
return entities;
|
|
495
519
|
}
|
|
496
520
|
|
|
497
|
-
// ../chitragupta/packages/smriti/
|
|
521
|
+
// ../chitragupta/packages/smriti/dist/ner-extractor.js
|
|
498
522
|
var DEFAULT_ENDPOINT = "http://localhost:8501";
|
|
499
523
|
var DEFAULT_MODEL = "gliner-large-v2.1";
|
|
500
524
|
var DEFAULT_MIN_CONFIDENCE = 0.5;
|
|
@@ -588,10 +612,7 @@ var TECHNOLOGY_WORDS = [
|
|
|
588
612
|
"yarn",
|
|
589
613
|
"zod"
|
|
590
614
|
];
|
|
591
|
-
var TECHNOLOGY_PATTERN = new RegExp(
|
|
592
|
-
`\\b(${TECHNOLOGY_WORDS.join("|")})\\b`,
|
|
593
|
-
"gi"
|
|
594
|
-
);
|
|
615
|
+
var TECHNOLOGY_PATTERN = new RegExp(`\\b(${TECHNOLOGY_WORDS.join("|")})\\b`, "gi");
|
|
595
616
|
var TOOL_WORDS = [
|
|
596
617
|
"read",
|
|
597
618
|
"write",
|
|
@@ -607,10 +628,7 @@ var TOOL_WORDS = [
|
|
|
607
628
|
"webfetch",
|
|
608
629
|
"websearch"
|
|
609
630
|
];
|
|
610
|
-
var TOOL_PATTERN = new RegExp(
|
|
611
|
-
`\\b(${TOOL_WORDS.join("|")})\\b`,
|
|
612
|
-
"gi"
|
|
613
|
-
);
|
|
631
|
+
var TOOL_PATTERN = new RegExp(`\\b(${TOOL_WORDS.join("|")})\\b`, "gi");
|
|
614
632
|
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
633
|
var DECISION_PATTERN = /\b((?:decided|chose|selected|opted|switched|migrated|moved)\s+to\s+\w[\w\s]{0,30})/gi;
|
|
616
634
|
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 +657,8 @@ var NERExtractor = class {
|
|
|
639
657
|
* and `useHeuristic` is enabled.
|
|
640
658
|
*/
|
|
641
659
|
async extract(text) {
|
|
642
|
-
if (!text || text.trim().length === 0)
|
|
660
|
+
if (!text || text.trim().length === 0)
|
|
661
|
+
return [];
|
|
643
662
|
const glinerUp = await this.isGLiNERAvailable();
|
|
644
663
|
if (glinerUp) {
|
|
645
664
|
try {
|
|
@@ -666,7 +685,8 @@ var NERExtractor = class {
|
|
|
666
685
|
* Caches the result after the first successful probe.
|
|
667
686
|
*/
|
|
668
687
|
async isGLiNERAvailable() {
|
|
669
|
-
if (this.glinerAvailable !== null)
|
|
688
|
+
if (this.glinerAvailable !== null)
|
|
689
|
+
return this.glinerAvailable;
|
|
670
690
|
try {
|
|
671
691
|
const response = await fetch(this.endpoint, {
|
|
672
692
|
method: "GET",
|
|
@@ -732,10 +752,12 @@ var NERExtractor = class {
|
|
|
732
752
|
const coveredSpans = [];
|
|
733
753
|
const addMatch = (match, type, groupIndex = 1) => {
|
|
734
754
|
const captured = match[groupIndex];
|
|
735
|
-
if (!captured)
|
|
755
|
+
if (!captured)
|
|
756
|
+
return;
|
|
736
757
|
const start = match.index + match[0].indexOf(captured);
|
|
737
758
|
const end = start + captured.length;
|
|
738
|
-
if (this.spanOverlaps([start, end], coveredSpans))
|
|
759
|
+
if (this.spanOverlaps([start, end], coveredSpans))
|
|
760
|
+
return;
|
|
739
761
|
coveredSpans.push([start, end]);
|
|
740
762
|
entities.push({
|
|
741
763
|
text: captured.trim(),
|
|
@@ -783,9 +805,7 @@ var NERExtractor = class {
|
|
|
783
805
|
*/
|
|
784
806
|
postProcess(entities) {
|
|
785
807
|
const typeSet = new Set(this.entityTypes);
|
|
786
|
-
let filtered = entities.filter(
|
|
787
|
-
(e) => typeSet.has(e.type) && e.confidence >= this.minConfidence
|
|
788
|
-
);
|
|
808
|
+
let filtered = entities.filter((e) => typeSet.has(e.type) && e.confidence >= this.minConfidence);
|
|
789
809
|
const seen = /* @__PURE__ */ new Map();
|
|
790
810
|
for (const entity of filtered) {
|
|
791
811
|
const key = `${entity.type}::${entity.text.toLowerCase().trim()}`;
|
|
@@ -796,14 +816,15 @@ var NERExtractor = class {
|
|
|
796
816
|
}
|
|
797
817
|
filtered = [...seen.values()];
|
|
798
818
|
filtered.sort((a, b) => {
|
|
799
|
-
if (b.confidence !== a.confidence)
|
|
819
|
+
if (b.confidence !== a.confidence)
|
|
820
|
+
return b.confidence - a.confidence;
|
|
800
821
|
return a.span[0] - b.span[0];
|
|
801
822
|
});
|
|
802
823
|
return filtered.slice(0, this.maxEntities);
|
|
803
824
|
}
|
|
804
825
|
};
|
|
805
826
|
|
|
806
|
-
// ../chitragupta/packages/smriti/
|
|
827
|
+
// ../chitragupta/packages/smriti/dist/bitemporal.js
|
|
807
828
|
function nowISO() {
|
|
808
829
|
return (/* @__PURE__ */ new Date()).toISOString();
|
|
809
830
|
}
|
|
@@ -844,14 +865,19 @@ function expireEdge(edge, validUntil) {
|
|
|
844
865
|
function queryEdgesAtTime(edges, asOfValid, asOfRecord) {
|
|
845
866
|
return edges.filter((e) => {
|
|
846
867
|
const vFrom = e.validFrom ?? EPOCH_ISO;
|
|
847
|
-
if (vFrom > asOfValid)
|
|
848
|
-
|
|
868
|
+
if (vFrom > asOfValid)
|
|
869
|
+
return false;
|
|
870
|
+
if (e.validUntil !== void 0 && e.validUntil <= asOfValid)
|
|
871
|
+
return false;
|
|
849
872
|
if (asOfRecord !== void 0) {
|
|
850
873
|
const rAt = e.recordedAt ?? EPOCH_ISO;
|
|
851
|
-
if (rAt > asOfRecord)
|
|
852
|
-
|
|
874
|
+
if (rAt > asOfRecord)
|
|
875
|
+
return false;
|
|
876
|
+
if (e.supersededAt !== void 0 && e.supersededAt <= asOfRecord)
|
|
877
|
+
return false;
|
|
853
878
|
} else {
|
|
854
|
-
if (e.supersededAt !== void 0)
|
|
879
|
+
if (e.supersededAt !== void 0)
|
|
880
|
+
return false;
|
|
855
881
|
}
|
|
856
882
|
return true;
|
|
857
883
|
});
|
|
@@ -862,19 +888,21 @@ function getEdgeHistory(edges, source, target) {
|
|
|
862
888
|
function temporalDecay(edge, now, halfLifeMs) {
|
|
863
889
|
const referenceTime = edge.validUntil !== void 0 ? new Date(edge.validUntil).getTime() : new Date(edge.validFrom ?? EPOCH_ISO).getTime();
|
|
864
890
|
const elapsed = now - referenceTime;
|
|
865
|
-
if (elapsed <= 0)
|
|
891
|
+
if (elapsed <= 0)
|
|
892
|
+
return edge.weight;
|
|
866
893
|
const decay = Math.exp(-Math.LN2 * elapsed / halfLifeMs);
|
|
867
894
|
return edge.weight * decay;
|
|
868
895
|
}
|
|
869
896
|
function compactEdges(edges, retentionMs) {
|
|
870
897
|
const cutoff = Date.now() - retentionMs;
|
|
871
898
|
return edges.filter((e) => {
|
|
872
|
-
if (e.supersededAt === void 0)
|
|
899
|
+
if (e.supersededAt === void 0)
|
|
900
|
+
return true;
|
|
873
901
|
return new Date(e.supersededAt).getTime() > cutoff;
|
|
874
902
|
});
|
|
875
903
|
}
|
|
876
904
|
|
|
877
|
-
// ../chitragupta/packages/smriti/
|
|
905
|
+
// ../chitragupta/packages/smriti/dist/graphrag-builder.js
|
|
878
906
|
function scopeToId(scope) {
|
|
879
907
|
switch (scope.type) {
|
|
880
908
|
case "global":
|
|
@@ -919,7 +947,8 @@ async function createSessionNode(ctx, session) {
|
|
|
919
947
|
async function extractConceptsFromNodes(ctx, nodes, edges) {
|
|
920
948
|
const entityIndex = /* @__PURE__ */ new Map();
|
|
921
949
|
for (const node of nodes) {
|
|
922
|
-
if (!node.content || node.type === "concept")
|
|
950
|
+
if (!node.content || node.type === "concept")
|
|
951
|
+
continue;
|
|
923
952
|
const entities = await ctx.extractEntities(node.content);
|
|
924
953
|
for (const entity of entities) {
|
|
925
954
|
const key = entity.name.toLowerCase();
|
|
@@ -935,9 +964,11 @@ async function extractConceptsFromNodes(ctx, nodes, edges) {
|
|
|
935
964
|
}
|
|
936
965
|
}
|
|
937
966
|
for (const [key, data] of entityIndex) {
|
|
938
|
-
if (data.sourceIds.length < 2)
|
|
967
|
+
if (data.sourceIds.length < 2)
|
|
968
|
+
continue;
|
|
939
969
|
const conceptId = `concept-${key.replace(/\s+/g, "-")}`;
|
|
940
|
-
if (nodes.some((n) => n.id === conceptId))
|
|
970
|
+
if (nodes.some((n) => n.id === conceptId))
|
|
971
|
+
continue;
|
|
941
972
|
const conceptNode = {
|
|
942
973
|
id: conceptId,
|
|
943
974
|
type: "concept",
|
|
@@ -1104,21 +1135,15 @@ function removeSessionFromGraph(graph, sessionId) {
|
|
|
1104
1135
|
}
|
|
1105
1136
|
}
|
|
1106
1137
|
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
|
-
);
|
|
1138
|
+
graph.edges = graph.edges.filter((e) => !nodeIdsToRemove.has(e.source) && !nodeIdsToRemove.has(e.target));
|
|
1110
1139
|
}
|
|
1111
1140
|
function removeMemoryFromGraph(graph, scope) {
|
|
1112
1141
|
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
|
-
);
|
|
1142
|
+
graph.nodes = graph.nodes.filter((n) => n.id !== memoryId && !n.id.startsWith(`${memoryId}-chunk-`));
|
|
1143
|
+
graph.edges = graph.edges.filter((e) => e.source !== memoryId && e.target !== memoryId && !e.source.startsWith(`${memoryId}-chunk-`) && !e.target.startsWith(`${memoryId}-chunk-`));
|
|
1119
1144
|
}
|
|
1120
1145
|
|
|
1121
|
-
// ../chitragupta/packages/smriti/
|
|
1146
|
+
// ../chitragupta/packages/smriti/dist/leiden-algorithm.js
|
|
1122
1147
|
var Xorshift32 = class {
|
|
1123
1148
|
state;
|
|
1124
1149
|
constructor(seed) {
|
|
@@ -1161,8 +1186,10 @@ var AdjacencyGraph = class {
|
|
|
1161
1186
|
for (const edge of graph.edges) {
|
|
1162
1187
|
const s = this.idToIndex.get(edge.source);
|
|
1163
1188
|
const t = this.idToIndex.get(edge.target);
|
|
1164
|
-
if (s === void 0 || t === void 0)
|
|
1165
|
-
|
|
1189
|
+
if (s === void 0 || t === void 0)
|
|
1190
|
+
continue;
|
|
1191
|
+
if (s === t)
|
|
1192
|
+
continue;
|
|
1166
1193
|
const w = edge.weight ?? 1;
|
|
1167
1194
|
this.adj[s].set(t, (this.adj[s].get(t) ?? 0) + w);
|
|
1168
1195
|
this.adj[t].set(s, (this.adj[t].get(s) ?? 0) + w);
|
|
@@ -1175,7 +1202,8 @@ var AdjacencyGraph = class {
|
|
|
1175
1202
|
};
|
|
1176
1203
|
function computeModularity(g, assignment, resolution) {
|
|
1177
1204
|
const twoM = g.totalWeight;
|
|
1178
|
-
if (twoM === 0)
|
|
1205
|
+
if (twoM === 0)
|
|
1206
|
+
return 0;
|
|
1179
1207
|
const internalWeight = /* @__PURE__ */ new Map();
|
|
1180
1208
|
const communityDegree = /* @__PURE__ */ new Map();
|
|
1181
1209
|
for (let i = 0; i < g.n; i++) {
|
|
@@ -1197,15 +1225,19 @@ function computeModularity(g, assignment, resolution) {
|
|
|
1197
1225
|
}
|
|
1198
1226
|
function modularityGain(g, i, cNew, assignment, communityDegree, resolution) {
|
|
1199
1227
|
const twoM = g.totalWeight;
|
|
1200
|
-
if (twoM === 0)
|
|
1228
|
+
if (twoM === 0)
|
|
1229
|
+
return 0;
|
|
1201
1230
|
const cOld = assignment[i];
|
|
1202
|
-
if (cOld === cNew)
|
|
1231
|
+
if (cOld === cNew)
|
|
1232
|
+
return 0;
|
|
1203
1233
|
const ki = g.degree[i];
|
|
1204
1234
|
let kInNew = 0;
|
|
1205
1235
|
let kInOld = 0;
|
|
1206
1236
|
for (const [j, w] of g.adj[i]) {
|
|
1207
|
-
if (assignment[j] === cNew)
|
|
1208
|
-
|
|
1237
|
+
if (assignment[j] === cNew)
|
|
1238
|
+
kInNew += w;
|
|
1239
|
+
if (assignment[j] === cOld)
|
|
1240
|
+
kInOld += w;
|
|
1209
1241
|
}
|
|
1210
1242
|
const sigmaTotNew = communityDegree.get(cNew) ?? 0;
|
|
1211
1243
|
const sigmaTotOld = communityDegree.get(cOld) ?? 0;
|
|
@@ -1235,7 +1267,8 @@ function localNodeMoving(g, assignment, resolution, rng) {
|
|
|
1235
1267
|
let bestCommunity = cOld;
|
|
1236
1268
|
let bestGain = 0;
|
|
1237
1269
|
for (const cNew of neighborCommunities) {
|
|
1238
|
-
if (cNew === cOld)
|
|
1270
|
+
if (cNew === cOld)
|
|
1271
|
+
continue;
|
|
1239
1272
|
const gain = modularityGain(g, i, cNew, assignment, communityDegree, resolution);
|
|
1240
1273
|
if (gain > bestGain) {
|
|
1241
1274
|
bestGain = gain;
|
|
@@ -1255,17 +1288,20 @@ function refineCommunities(g, assignment) {
|
|
|
1255
1288
|
const communityNodes = /* @__PURE__ */ new Map();
|
|
1256
1289
|
for (let i = 0; i < g.n; i++) {
|
|
1257
1290
|
const c = assignment[i];
|
|
1258
|
-
if (!communityNodes.has(c))
|
|
1291
|
+
if (!communityNodes.has(c))
|
|
1292
|
+
communityNodes.set(c, []);
|
|
1259
1293
|
communityNodes.get(c).push(i);
|
|
1260
1294
|
}
|
|
1261
1295
|
let nextCommunityId = Math.max(...assignment) + 1;
|
|
1262
1296
|
for (const [_communityId, nodes] of communityNodes) {
|
|
1263
|
-
if (nodes.length <= 1)
|
|
1297
|
+
if (nodes.length <= 1)
|
|
1298
|
+
continue;
|
|
1264
1299
|
const nodeSet = new Set(nodes);
|
|
1265
1300
|
const visited = /* @__PURE__ */ new Set();
|
|
1266
1301
|
const components = [];
|
|
1267
1302
|
for (const start of nodes) {
|
|
1268
|
-
if (visited.has(start))
|
|
1303
|
+
if (visited.has(start))
|
|
1304
|
+
continue;
|
|
1269
1305
|
const component = [];
|
|
1270
1306
|
const queue = [start];
|
|
1271
1307
|
visited.add(start);
|
|
@@ -1322,7 +1358,7 @@ function compactCommunities(assignment, minSize) {
|
|
|
1322
1358
|
return assignment.map((c) => remap.get(c) ?? 0);
|
|
1323
1359
|
}
|
|
1324
1360
|
|
|
1325
|
-
// ../chitragupta/packages/smriti/
|
|
1361
|
+
// ../chitragupta/packages/smriti/dist/graphrag-leiden.js
|
|
1326
1362
|
var DEFAULT_LEIDEN_CONFIG = {
|
|
1327
1363
|
resolution: 1,
|
|
1328
1364
|
maxIterations: 10,
|
|
@@ -1376,7 +1412,8 @@ function buildCommunityList(g, compacted, resolution) {
|
|
|
1376
1412
|
const communityNodes = /* @__PURE__ */ new Map();
|
|
1377
1413
|
for (let i = 0; i < g.n; i++) {
|
|
1378
1414
|
const c = compacted[i];
|
|
1379
|
-
if (!communityNodes.has(c))
|
|
1415
|
+
if (!communityNodes.has(c))
|
|
1416
|
+
communityNodes.set(c, []);
|
|
1380
1417
|
communityNodes.get(c).push(g.indexToId[i]);
|
|
1381
1418
|
}
|
|
1382
1419
|
const communityList = [];
|
|
@@ -1395,7 +1432,8 @@ function buildCommunityList(g, compacted, resolution) {
|
|
|
1395
1432
|
const possibleEdges = members.length * (members.length - 1) / 2;
|
|
1396
1433
|
const density = possibleEdges > 0 ? internalEdges / possibleEdges : 0;
|
|
1397
1434
|
let ac = 0;
|
|
1398
|
-
for (const mi of memberSet)
|
|
1435
|
+
for (const mi of memberSet)
|
|
1436
|
+
ac += g.degree[mi];
|
|
1399
1437
|
const twoM = g.totalWeight;
|
|
1400
1438
|
const communityMod = twoM > 0 ? 2 * internalWeight / twoM - resolution * (ac / twoM) ** 2 : 0;
|
|
1401
1439
|
communityList.push({
|
|
@@ -1417,9 +1455,7 @@ function annotateCommunities(graph, result) {
|
|
|
1417
1455
|
}
|
|
1418
1456
|
}
|
|
1419
1457
|
function communitySummary(graph, communityId, maxLabels = 5) {
|
|
1420
|
-
const members = graph.nodes.filter(
|
|
1421
|
-
(n) => n.metadata.communityId === communityId
|
|
1422
|
-
);
|
|
1458
|
+
const members = graph.nodes.filter((n) => n.metadata.communityId === communityId);
|
|
1423
1459
|
const typeCount = {};
|
|
1424
1460
|
for (const node of members) {
|
|
1425
1461
|
typeCount[node.type] = (typeCount[node.type] ?? 0) + 1;
|
|
@@ -1441,8 +1477,10 @@ function findBridgeNodes(graph, result, minCommunities = 2) {
|
|
|
1441
1477
|
for (const edge of graph.edges) {
|
|
1442
1478
|
const sc = result.communities.get(edge.source);
|
|
1443
1479
|
const tc = result.communities.get(edge.target);
|
|
1444
|
-
if (sc === void 0 || tc === void 0)
|
|
1445
|
-
|
|
1480
|
+
if (sc === void 0 || tc === void 0)
|
|
1481
|
+
continue;
|
|
1482
|
+
if (sc === tc)
|
|
1483
|
+
continue;
|
|
1446
1484
|
touchedCommunities.get(edge.source)?.add(tc);
|
|
1447
1485
|
touchedCommunities.get(edge.target)?.add(sc);
|
|
1448
1486
|
}
|
|
@@ -1456,7 +1494,10 @@ function findBridgeNodes(graph, result, minCommunities = 2) {
|
|
|
1456
1494
|
return bridges;
|
|
1457
1495
|
}
|
|
1458
1496
|
|
|
1459
|
-
// ../chitragupta/packages/smriti/
|
|
1497
|
+
// ../chitragupta/packages/smriti/dist/graphrag-persistence.js
|
|
1498
|
+
init_dist();
|
|
1499
|
+
init_database();
|
|
1500
|
+
init_schema();
|
|
1460
1501
|
import fs from "fs";
|
|
1461
1502
|
import path from "path";
|
|
1462
1503
|
function getGraphDir() {
|
|
@@ -1494,26 +1535,17 @@ function insertEdgeToDb(db, edge) {
|
|
|
1494
1535
|
INSERT OR IGNORE INTO edges (source, target, relationship, weight, pramana, viveka,
|
|
1495
1536
|
valid_from, valid_until, recorded_at, superseded_at)
|
|
1496
1537
|
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
|
-
);
|
|
1538
|
+
`).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
1539
|
}
|
|
1510
1540
|
function saveToSqlite(db, graph, pageRankScores, embeddingCache) {
|
|
1511
1541
|
db.transaction(() => {
|
|
1512
1542
|
db.prepare("DELETE FROM pagerank").run();
|
|
1513
1543
|
db.prepare("DELETE FROM edges").run();
|
|
1514
1544
|
db.prepare("DELETE FROM nodes").run();
|
|
1515
|
-
for (const node of graph.nodes)
|
|
1516
|
-
|
|
1545
|
+
for (const node of graph.nodes)
|
|
1546
|
+
upsertNodeToDb(db, node);
|
|
1547
|
+
for (const edge of graph.edges)
|
|
1548
|
+
insertEdgeToDb(db, edge);
|
|
1517
1549
|
for (const [nodeId, score] of pageRankScores) {
|
|
1518
1550
|
db.prepare("INSERT OR REPLACE INTO pagerank (node_id, score, updated_at) VALUES (?, ?, ?)").run(nodeId, score, Date.now());
|
|
1519
1551
|
}
|
|
@@ -1522,7 +1554,8 @@ function saveToSqlite(db, graph, pageRankScores, embeddingCache) {
|
|
|
1522
1554
|
}
|
|
1523
1555
|
function loadFromSqlite(db) {
|
|
1524
1556
|
const nodeCount = db.prepare("SELECT COUNT(*) as cnt FROM nodes").get().cnt;
|
|
1525
|
-
if (nodeCount === 0)
|
|
1557
|
+
if (nodeCount === 0)
|
|
1558
|
+
return null;
|
|
1526
1559
|
const nodeRows = db.prepare("SELECT * FROM nodes").all();
|
|
1527
1560
|
const nodes = nodeRows.map((row) => ({
|
|
1528
1561
|
id: row.id,
|
|
@@ -1557,8 +1590,10 @@ function loadFromSqlite(db) {
|
|
|
1557
1590
|
}
|
|
1558
1591
|
function migrateInMemoryToSqlite(db, graph, pageRankScores) {
|
|
1559
1592
|
db.transaction(() => {
|
|
1560
|
-
for (const node of graph.nodes)
|
|
1561
|
-
|
|
1593
|
+
for (const node of graph.nodes)
|
|
1594
|
+
upsertNodeToDb(db, node);
|
|
1595
|
+
for (const edge of graph.edges)
|
|
1596
|
+
insertEdgeToDb(db, edge);
|
|
1562
1597
|
for (const [nodeId, score] of pageRankScores) {
|
|
1563
1598
|
db.prepare("INSERT OR REPLACE INTO pagerank (node_id, score, updated_at) VALUES (?, ?, ?)").run(nodeId, score, Date.now());
|
|
1564
1599
|
}
|
|
@@ -1571,7 +1606,8 @@ function saveToJson(graph, pageRankScores, embeddingCache) {
|
|
|
1571
1606
|
fs.writeFileSync(getGraphPath(), JSON.stringify(graph, null, " "), "utf-8");
|
|
1572
1607
|
saveEmbeddingsJson(embeddingCache);
|
|
1573
1608
|
const pageRankObj = {};
|
|
1574
|
-
for (const [key, value] of pageRankScores)
|
|
1609
|
+
for (const [key, value] of pageRankScores)
|
|
1610
|
+
pageRankObj[key] = value;
|
|
1575
1611
|
fs.writeFileSync(getPageRankPath(), JSON.stringify(pageRankObj, null, " "), "utf-8");
|
|
1576
1612
|
} catch {
|
|
1577
1613
|
}
|
|
@@ -1591,7 +1627,8 @@ function loadFromJson() {
|
|
|
1591
1627
|
const prPath = getPageRankPath();
|
|
1592
1628
|
if (fs.existsSync(prPath)) {
|
|
1593
1629
|
const ranks = JSON.parse(fs.readFileSync(prPath, "utf-8"));
|
|
1594
|
-
for (const [key, value] of Object.entries(ranks))
|
|
1630
|
+
for (const [key, value] of Object.entries(ranks))
|
|
1631
|
+
pageRankScores.set(key, value);
|
|
1595
1632
|
}
|
|
1596
1633
|
} catch {
|
|
1597
1634
|
}
|
|
@@ -1602,7 +1639,8 @@ function saveEmbeddingsJson(embeddingCache) {
|
|
|
1602
1639
|
const dir = getGraphDir();
|
|
1603
1640
|
fs.mkdirSync(dir, { recursive: true });
|
|
1604
1641
|
const embeddings = {};
|
|
1605
|
-
for (const [key, value] of embeddingCache)
|
|
1642
|
+
for (const [key, value] of embeddingCache)
|
|
1643
|
+
embeddings[key] = value;
|
|
1606
1644
|
fs.writeFileSync(getEmbeddingsPath(), JSON.stringify(embeddings, null, " "), "utf-8");
|
|
1607
1645
|
} catch {
|
|
1608
1646
|
}
|
|
@@ -1613,10 +1651,12 @@ function loadEmbeddingsJson(maxCacheSize) {
|
|
|
1613
1651
|
const embPath = getEmbeddingsPath();
|
|
1614
1652
|
if (fs.existsSync(embPath)) {
|
|
1615
1653
|
const embeddings = JSON.parse(fs.readFileSync(embPath, "utf-8"));
|
|
1616
|
-
for (const [key, value] of Object.entries(embeddings))
|
|
1654
|
+
for (const [key, value] of Object.entries(embeddings))
|
|
1655
|
+
cache.set(key, value);
|
|
1617
1656
|
while (cache.size > maxCacheSize) {
|
|
1618
1657
|
const oldest = cache.keys().next().value;
|
|
1619
|
-
if (oldest !== void 0)
|
|
1658
|
+
if (oldest !== void 0)
|
|
1659
|
+
cache.delete(oldest);
|
|
1620
1660
|
}
|
|
1621
1661
|
}
|
|
1622
1662
|
} catch {
|
|
@@ -1624,23 +1664,27 @@ function loadEmbeddingsJson(maxCacheSize) {
|
|
|
1624
1664
|
return cache;
|
|
1625
1665
|
}
|
|
1626
1666
|
function lookupPramana(db, nodeId) {
|
|
1627
|
-
if (!db)
|
|
1667
|
+
if (!db)
|
|
1668
|
+
return "shabda";
|
|
1628
1669
|
try {
|
|
1629
1670
|
const row = db.prepare(`
|
|
1630
1671
|
SELECT pramana, COUNT(*) as cnt FROM edges
|
|
1631
1672
|
WHERE (source = ? OR target = ?) AND pramana IS NOT NULL
|
|
1632
1673
|
GROUP BY pramana ORDER BY cnt DESC LIMIT 1
|
|
1633
1674
|
`).get(nodeId, nodeId);
|
|
1634
|
-
if (row?.pramana)
|
|
1675
|
+
if (row?.pramana)
|
|
1676
|
+
return row.pramana;
|
|
1635
1677
|
} catch {
|
|
1636
1678
|
}
|
|
1637
1679
|
return "shabda";
|
|
1638
1680
|
}
|
|
1639
1681
|
function lookupPramanaBatch(db, nodeIds) {
|
|
1640
1682
|
const result = /* @__PURE__ */ new Map();
|
|
1641
|
-
if (nodeIds.length === 0)
|
|
1683
|
+
if (nodeIds.length === 0)
|
|
1684
|
+
return result;
|
|
1642
1685
|
if (!db) {
|
|
1643
|
-
for (const id of nodeIds)
|
|
1686
|
+
for (const id of nodeIds)
|
|
1687
|
+
result.set(id, "shabda");
|
|
1644
1688
|
return result;
|
|
1645
1689
|
}
|
|
1646
1690
|
try {
|
|
@@ -1656,26 +1700,31 @@ function lookupPramanaBatch(db, nodeIds) {
|
|
|
1656
1700
|
GROUP BY target, pramana
|
|
1657
1701
|
) GROUP BY node_id HAVING cnt = MAX(cnt)
|
|
1658
1702
|
`).all(...nodeIds, ...nodeIds);
|
|
1659
|
-
for (const row of rows)
|
|
1703
|
+
for (const row of rows)
|
|
1704
|
+
result.set(row.node_id, row.pramana);
|
|
1660
1705
|
} catch {
|
|
1661
1706
|
}
|
|
1662
1707
|
for (const id of nodeIds) {
|
|
1663
|
-
if (!result.has(id))
|
|
1708
|
+
if (!result.has(id))
|
|
1709
|
+
result.set(id, "shabda");
|
|
1664
1710
|
}
|
|
1665
1711
|
return result;
|
|
1666
1712
|
}
|
|
1667
1713
|
function migrateGraphJson() {
|
|
1668
1714
|
const graphPath = getGraphPath();
|
|
1669
1715
|
const prPath = getPageRankPath();
|
|
1670
|
-
if (!fs.existsSync(graphPath))
|
|
1716
|
+
if (!fs.existsSync(graphPath))
|
|
1717
|
+
return { nodes: 0, edges: 0 };
|
|
1671
1718
|
try {
|
|
1672
1719
|
const graph = JSON.parse(fs.readFileSync(graphPath, "utf-8"));
|
|
1673
|
-
if (!graph.nodes || graph.nodes.length === 0)
|
|
1720
|
+
if (!graph.nodes || graph.nodes.length === 0)
|
|
1721
|
+
return { nodes: 0, edges: 0 };
|
|
1674
1722
|
const pageRankScores = /* @__PURE__ */ new Map();
|
|
1675
1723
|
try {
|
|
1676
1724
|
if (fs.existsSync(prPath)) {
|
|
1677
1725
|
const ranks = JSON.parse(fs.readFileSync(prPath, "utf-8"));
|
|
1678
|
-
for (const [key, value] of Object.entries(ranks))
|
|
1726
|
+
for (const [key, value] of Object.entries(ranks))
|
|
1727
|
+
pageRankScores.set(key, value);
|
|
1679
1728
|
}
|
|
1680
1729
|
} catch {
|
|
1681
1730
|
}
|
|
@@ -1692,35 +1741,24 @@ function migrateGraphJson() {
|
|
|
1692
1741
|
valid_from, valid_until, recorded_at, superseded_at)
|
|
1693
1742
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1694
1743
|
`);
|
|
1695
|
-
const prStmt = db.prepare(
|
|
1696
|
-
"INSERT OR REPLACE INTO pagerank (node_id, score, updated_at) VALUES (?, ?, ?)"
|
|
1697
|
-
);
|
|
1744
|
+
const prStmt = db.prepare("INSERT OR REPLACE INTO pagerank (node_id, score, updated_at) VALUES (?, ?, ?)");
|
|
1698
1745
|
const now = Date.now();
|
|
1699
1746
|
for (const node of graph.nodes) {
|
|
1700
1747
|
upsertStmt.run(node.id, node.type, node.label, node.content, JSON.stringify(node.metadata), now, now);
|
|
1701
1748
|
}
|
|
1702
1749
|
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
|
-
);
|
|
1750
|
+
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
1751
|
}
|
|
1716
|
-
for (const [nodeId, score] of pageRankScores)
|
|
1752
|
+
for (const [nodeId, score] of pageRankScores)
|
|
1753
|
+
prStmt.run(nodeId, score, now);
|
|
1717
1754
|
})();
|
|
1718
1755
|
try {
|
|
1719
1756
|
fs.renameSync(graphPath, graphPath + ".bak");
|
|
1720
1757
|
} catch {
|
|
1721
1758
|
}
|
|
1722
1759
|
try {
|
|
1723
|
-
if (fs.existsSync(prPath))
|
|
1760
|
+
if (fs.existsSync(prPath))
|
|
1761
|
+
fs.renameSync(prPath, prPath + ".bak");
|
|
1724
1762
|
} catch {
|
|
1725
1763
|
}
|
|
1726
1764
|
return { nodes: graph.nodes.length, edges: graph.edges.length };
|
|
@@ -1729,7 +1767,7 @@ function migrateGraphJson() {
|
|
|
1729
1767
|
}
|
|
1730
1768
|
}
|
|
1731
1769
|
|
|
1732
|
-
// ../chitragupta/packages/smriti/
|
|
1770
|
+
// ../chitragupta/packages/smriti/dist/graphrag.js
|
|
1733
1771
|
var DEFAULT_CONFIG = {
|
|
1734
1772
|
endpoint: process.env.OLLAMA_HOST ?? "http://localhost:11434",
|
|
1735
1773
|
model: "nomic-embed-text",
|
|
@@ -1757,7 +1795,8 @@ var GraphRAGEngine = class {
|
|
|
1757
1795
|
this.loadFromDisk();
|
|
1758
1796
|
}
|
|
1759
1797
|
async checkOllamaAvailability() {
|
|
1760
|
-
if (this.ollamaAvailable !== null)
|
|
1798
|
+
if (this.ollamaAvailable !== null)
|
|
1799
|
+
return this.ollamaAvailable;
|
|
1761
1800
|
try {
|
|
1762
1801
|
const response = await fetch(`${this.config.endpoint}/api/version`, {
|
|
1763
1802
|
method: "GET",
|
|
@@ -1772,12 +1811,14 @@ var GraphRAGEngine = class {
|
|
|
1772
1811
|
/** Get (or compute and cache) an embedding vector for the given text. */
|
|
1773
1812
|
async getEmbedding(text) {
|
|
1774
1813
|
const cached = this.embeddingCache.get(text);
|
|
1775
|
-
if (cached)
|
|
1814
|
+
if (cached)
|
|
1815
|
+
return cached;
|
|
1776
1816
|
const vector = await this.embeddingService.getEmbedding(text);
|
|
1777
1817
|
this.embeddingCache.set(text, vector);
|
|
1778
1818
|
if (this.embeddingCache.size > this.maxEmbeddingCacheSize) {
|
|
1779
1819
|
const oldest = this.embeddingCache.keys().next().value;
|
|
1780
|
-
if (oldest !== void 0)
|
|
1820
|
+
if (oldest !== void 0)
|
|
1821
|
+
this.embeddingCache.delete(oldest);
|
|
1781
1822
|
}
|
|
1782
1823
|
return vector;
|
|
1783
1824
|
}
|
|
@@ -1792,7 +1833,9 @@ var GraphRAGEngine = class {
|
|
|
1792
1833
|
if (isAvailable) {
|
|
1793
1834
|
try {
|
|
1794
1835
|
baseEntities = await llmExtractEntities(text, this.config.endpoint, this.config.generationModel);
|
|
1795
|
-
} catch {
|
|
1836
|
+
} catch (err) {
|
|
1837
|
+
process.stderr.write(`[smriti:graphrag] LLM entity extraction failed, falling back to keywords: ${err instanceof Error ? err.message : String(err)}
|
|
1838
|
+
`);
|
|
1796
1839
|
baseEntities = keywordExtractEntities(text);
|
|
1797
1840
|
}
|
|
1798
1841
|
} else {
|
|
@@ -1825,7 +1868,9 @@ var GraphRAGEngine = class {
|
|
|
1825
1868
|
seen.add(normalized);
|
|
1826
1869
|
}
|
|
1827
1870
|
}
|
|
1828
|
-
} catch {
|
|
1871
|
+
} catch (err) {
|
|
1872
|
+
process.stderr.write(`[smriti:graphrag] NER extraction failed: ${err instanceof Error ? err.message : String(err)}
|
|
1873
|
+
`);
|
|
1829
1874
|
}
|
|
1830
1875
|
return baseEntities;
|
|
1831
1876
|
}
|
|
@@ -1838,14 +1883,18 @@ var GraphRAGEngine = class {
|
|
|
1838
1883
|
}
|
|
1839
1884
|
/** Feed removed edges into incremental PR engine. */
|
|
1840
1885
|
applyEdgeRemovals(edges) {
|
|
1841
|
-
if (!this.incrementalPR || edges.length === 0)
|
|
1842
|
-
|
|
1886
|
+
if (!this.incrementalPR || edges.length === 0)
|
|
1887
|
+
return;
|
|
1888
|
+
for (const edge of edges)
|
|
1889
|
+
this.incrementalPR.removeEdge(edge.source, edge.target);
|
|
1843
1890
|
this.pageRankScores = this.incrementalPR.getRanks();
|
|
1844
1891
|
}
|
|
1845
1892
|
/** Feed newly added edges into incremental PR engine. */
|
|
1846
1893
|
applyEdgeAdditions(edges) {
|
|
1847
|
-
if (!this.incrementalPR || edges.length === 0)
|
|
1848
|
-
|
|
1894
|
+
if (!this.incrementalPR || edges.length === 0)
|
|
1895
|
+
return;
|
|
1896
|
+
for (const edge of edges)
|
|
1897
|
+
this.incrementalPR.addEdge(edge.source, edge.target);
|
|
1849
1898
|
this.pageRankScores = this.incrementalPR.getRanks();
|
|
1850
1899
|
}
|
|
1851
1900
|
/**
|
|
@@ -1878,7 +1927,8 @@ var GraphRAGEngine = class {
|
|
|
1878
1927
|
nodes.push(await createSessionNode(this, session));
|
|
1879
1928
|
await indexSessionTurns(this, session, nodes, edges);
|
|
1880
1929
|
}
|
|
1881
|
-
for (const memory of memories)
|
|
1930
|
+
for (const memory of memories)
|
|
1931
|
+
await buildMemoryNodes(this, memory, nodes, edges);
|
|
1882
1932
|
await extractConceptsFromNodes(this, nodes, edges);
|
|
1883
1933
|
this.graph = { nodes, edges };
|
|
1884
1934
|
if (this.graph.nodes.length > 0 && this.graph.edges.length > 0) {
|
|
@@ -1892,13 +1942,16 @@ var GraphRAGEngine = class {
|
|
|
1892
1942
|
/** Search the knowledge graph using hybrid scoring (cosine + PageRank + BM25). */
|
|
1893
1943
|
async search(query, graph, topK = 10) {
|
|
1894
1944
|
const searchGraph = graph ?? this.graph;
|
|
1895
|
-
if (searchGraph.nodes.length === 0)
|
|
1945
|
+
if (searchGraph.nodes.length === 0)
|
|
1946
|
+
return [];
|
|
1896
1947
|
const queryEmbedding = await this.getEmbedding(query);
|
|
1897
|
-
if (this.pageRankScores.size === 0)
|
|
1948
|
+
if (this.pageRankScores.size === 0)
|
|
1949
|
+
this.computePageRank(searchGraph);
|
|
1898
1950
|
let maxPageRank = 0;
|
|
1899
1951
|
for (const node of searchGraph.nodes) {
|
|
1900
1952
|
const pr = this.pageRankScores.get(node.id) ?? 0;
|
|
1901
|
-
if (pr > maxPageRank)
|
|
1953
|
+
if (pr > maxPageRank)
|
|
1954
|
+
maxPageRank = pr;
|
|
1902
1955
|
}
|
|
1903
1956
|
const scored = [];
|
|
1904
1957
|
for (const node of searchGraph.nodes) {
|
|
@@ -1910,7 +1963,8 @@ var GraphRAGEngine = class {
|
|
|
1910
1963
|
const normPR = maxPageRank > 0 ? rawPR / maxPageRank : 0;
|
|
1911
1964
|
const textScore = textMatchScore(query, node.content + " " + node.label);
|
|
1912
1965
|
const finalScore = ALPHA * cosineSim + BETA * normPR + GAMMA * textScore;
|
|
1913
|
-
if (finalScore > 0)
|
|
1966
|
+
if (finalScore > 0)
|
|
1967
|
+
scored.push({ node, score: finalScore });
|
|
1914
1968
|
}
|
|
1915
1969
|
scored.sort((a, b) => b.score - a.score);
|
|
1916
1970
|
return scored.slice(0, topK).map((s) => s.node);
|
|
@@ -1975,7 +2029,9 @@ var GraphRAGEngine = class {
|
|
|
1975
2029
|
saveToSqlite(db, this.graph, this.pageRankScores, this.embeddingCache);
|
|
1976
2030
|
return;
|
|
1977
2031
|
}
|
|
1978
|
-
} catch {
|
|
2032
|
+
} catch (err) {
|
|
2033
|
+
process.stderr.write(`[smriti:graphrag] SQLite save failed, falling back to JSON: ${err instanceof Error ? err.message : String(err)}
|
|
2034
|
+
`);
|
|
1979
2035
|
}
|
|
1980
2036
|
saveToJson(this.graph, this.pageRankScores, this.embeddingCache);
|
|
1981
2037
|
}
|
|
@@ -1996,13 +2052,17 @@ var GraphRAGEngine = class {
|
|
|
1996
2052
|
if (this.graph.nodes.length > 0) {
|
|
1997
2053
|
try {
|
|
1998
2054
|
migrateInMemoryToSqlite(db, this.graph, this.pageRankScores);
|
|
1999
|
-
} catch {
|
|
2055
|
+
} catch (err) {
|
|
2056
|
+
process.stderr.write(`[smriti:graphrag] in-memory to SQLite migration failed: ${err instanceof Error ? err.message : String(err)}
|
|
2057
|
+
`);
|
|
2000
2058
|
}
|
|
2001
2059
|
}
|
|
2002
2060
|
}
|
|
2003
2061
|
loaded = true;
|
|
2004
2062
|
}
|
|
2005
|
-
} catch {
|
|
2063
|
+
} catch (err) {
|
|
2064
|
+
process.stderr.write(`[smriti:graphrag] SQLite load failed: ${err instanceof Error ? err.message : String(err)}
|
|
2065
|
+
`);
|
|
2006
2066
|
}
|
|
2007
2067
|
if (!loaded) {
|
|
2008
2068
|
const jsonData = loadFromJson();
|
|
@@ -2012,7 +2072,8 @@ var GraphRAGEngine = class {
|
|
|
2012
2072
|
for (const [k, v] of loadEmbeddingsJson(this.maxEmbeddingCacheSize)) {
|
|
2013
2073
|
this.embeddingCache.set(k, v);
|
|
2014
2074
|
}
|
|
2015
|
-
if (this.graph.edges.length > 0)
|
|
2075
|
+
if (this.graph.edges.length > 0)
|
|
2076
|
+
this.initIncrementalPR();
|
|
2016
2077
|
}
|
|
2017
2078
|
/** Get the current in-memory knowledge graph. */
|
|
2018
2079
|
getGraph() {
|
|
@@ -2026,8 +2087,11 @@ var GraphRAGEngine = class {
|
|
|
2026
2087
|
this.incrementalPR = null;
|
|
2027
2088
|
try {
|
|
2028
2089
|
const db = this.getGraphDbHandle();
|
|
2029
|
-
if (db)
|
|
2030
|
-
|
|
2090
|
+
if (db)
|
|
2091
|
+
db.exec("DELETE FROM pagerank; DELETE FROM edges; DELETE FROM nodes;");
|
|
2092
|
+
} catch (err) {
|
|
2093
|
+
process.stderr.write(`[smriti:graphrag] clear DB failed: ${err instanceof Error ? err.message : String(err)}
|
|
2094
|
+
`);
|
|
2031
2095
|
}
|
|
2032
2096
|
this.saveToDisk();
|
|
2033
2097
|
}
|
|
@@ -2042,8 +2106,10 @@ var GraphRAGEngine = class {
|
|
|
2042
2106
|
*/
|
|
2043
2107
|
getNeighbors(nodeId, direction = "both") {
|
|
2044
2108
|
return this.graph.edges.filter((edge) => {
|
|
2045
|
-
if (direction === "out")
|
|
2046
|
-
|
|
2109
|
+
if (direction === "out")
|
|
2110
|
+
return edge.source === nodeId;
|
|
2111
|
+
if (direction === "in")
|
|
2112
|
+
return edge.target === nodeId;
|
|
2047
2113
|
return edge.source === nodeId || edge.target === nodeId;
|
|
2048
2114
|
});
|
|
2049
2115
|
}
|
|
@@ -2068,4 +2134,4 @@ export {
|
|
|
2068
2134
|
migrateGraphJson,
|
|
2069
2135
|
GraphRAGEngine
|
|
2070
2136
|
};
|
|
2071
|
-
//# sourceMappingURL=chunk-
|
|
2137
|
+
//# sourceMappingURL=chunk-M2RLX5LU.js.map
|