claudecode-rlm 1.0.0
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/LICENSE +21 -0
- package/README.md +209 -0
- package/dist/config.d.ts +176 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +103 -0
- package/dist/config.js.map +1 -0
- package/dist/graph/index.d.ts +10 -0
- package/dist/graph/index.d.ts.map +1 -0
- package/dist/graph/index.js +10 -0
- package/dist/graph/index.js.map +1 -0
- package/dist/graph/ingestion.d.ts +68 -0
- package/dist/graph/ingestion.d.ts.map +1 -0
- package/dist/graph/ingestion.js +417 -0
- package/dist/graph/ingestion.js.map +1 -0
- package/dist/graph/storage.d.ts +51 -0
- package/dist/graph/storage.d.ts.map +1 -0
- package/dist/graph/storage.js +552 -0
- package/dist/graph/storage.js.map +1 -0
- package/dist/graph/traversal.d.ts +54 -0
- package/dist/graph/traversal.d.ts.map +1 -0
- package/dist/graph/traversal.js +255 -0
- package/dist/graph/traversal.js.map +1 -0
- package/dist/graph/types.d.ts +152 -0
- package/dist/graph/types.d.ts.map +1 -0
- package/dist/graph/types.js +94 -0
- package/dist/graph/types.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +190 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin-types.d.ts +96 -0
- package/dist/plugin-types.d.ts.map +1 -0
- package/dist/plugin-types.js +17 -0
- package/dist/plugin-types.js.map +1 -0
- package/dist/search/enhanced.d.ts +95 -0
- package/dist/search/enhanced.d.ts.map +1 -0
- package/dist/search/enhanced.js +194 -0
- package/dist/search/enhanced.js.map +1 -0
- package/dist/search/index.d.ts +8 -0
- package/dist/search/index.d.ts.map +1 -0
- package/dist/search/index.js +8 -0
- package/dist/search/index.js.map +1 -0
- package/dist/search/patterns.d.ts +38 -0
- package/dist/search/patterns.d.ts.map +1 -0
- package/dist/search/patterns.js +124 -0
- package/dist/search/patterns.js.map +1 -0
- package/dist/tools/graph-query.d.ts +14 -0
- package/dist/tools/graph-query.d.ts.map +1 -0
- package/dist/tools/graph-query.js +203 -0
- package/dist/tools/graph-query.js.map +1 -0
- package/dist/tools/index.d.ts +8 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +8 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/memory.d.ts +20 -0
- package/dist/tools/memory.d.ts.map +1 -0
- package/dist/tools/memory.js +181 -0
- package/dist/tools/memory.js.map +1 -0
- package/package.json +66 -0
- package/src/config.ts +111 -0
- package/src/graph/index.ts +10 -0
- package/src/graph/ingestion.ts +528 -0
- package/src/graph/storage.ts +639 -0
- package/src/graph/traversal.ts +348 -0
- package/src/graph/types.ts +144 -0
- package/src/index.ts +238 -0
- package/src/plugin-types.ts +107 -0
- package/src/search/enhanced.ts +264 -0
- package/src/search/index.ts +23 -0
- package/src/search/patterns.ts +139 -0
- package/src/tools/graph-query.ts +257 -0
- package/src/tools/index.ts +8 -0
- package/src/tools/memory.ts +208 -0
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Graph ingestion: Entity extraction and content chunking.
|
|
3
|
+
*
|
|
4
|
+
* Provides utilities for processing text content:
|
|
5
|
+
* - EntityExtractor: Identifies named entities and code elements
|
|
6
|
+
* - ContentChunker: Splits content into hierarchical chunks
|
|
7
|
+
* - GraphIngester: Converts content into graph structure
|
|
8
|
+
*/
|
|
9
|
+
import * as crypto from "crypto";
|
|
10
|
+
import { NodeType, RelationType, EntityType, } from "./types.js";
|
|
11
|
+
import { GraphStorage } from "./storage.js";
|
|
12
|
+
/**
|
|
13
|
+
* Stopwords to filter from keyword extraction.
|
|
14
|
+
*/
|
|
15
|
+
const STOPWORDS = new Set([
|
|
16
|
+
// Basic articles and prepositions
|
|
17
|
+
"the", "a", "an", "is", "are", "was", "were", "be", "been", "being",
|
|
18
|
+
"have", "has", "had", "do", "does", "did", "will", "would", "could",
|
|
19
|
+
"should", "may", "might", "must", "shall", "can", "need", "dare",
|
|
20
|
+
"ought", "used", "to", "of", "in", "for", "on", "with", "at", "by",
|
|
21
|
+
"from", "as", "into", "through", "during", "before", "after", "above",
|
|
22
|
+
"below", "between", "under", "again", "further", "then", "once", "here",
|
|
23
|
+
"there", "when", "where", "why", "how", "all", "each", "few", "more",
|
|
24
|
+
"most", "other", "some", "such", "no", "nor", "not", "only", "own",
|
|
25
|
+
"same", "so", "than", "too", "very", "just", "and", "but", "if", "or",
|
|
26
|
+
"because", "until", "while", "about", "against",
|
|
27
|
+
// Pronouns
|
|
28
|
+
"i", "me", "my", "myself", "we", "our", "ours", "ourselves",
|
|
29
|
+
"you", "your", "yours", "yourself", "yourselves",
|
|
30
|
+
"he", "him", "his", "himself", "she", "her", "hers", "herself",
|
|
31
|
+
"it", "its", "itself", "they", "them", "their", "theirs", "themselves",
|
|
32
|
+
"what", "which", "who", "whom", "this", "that", "these", "those",
|
|
33
|
+
// Common verbs
|
|
34
|
+
"am", "having", "doing", "going", "coming", "getting", "making",
|
|
35
|
+
"taking", "using", "trying", "saying", "seeing", "wanting", "needing",
|
|
36
|
+
"knowing", "thinking", "looking", "giving", "finding", "telling",
|
|
37
|
+
"asking", "working", "seeming", "feeling", "leaving", "calling",
|
|
38
|
+
// Fillers
|
|
39
|
+
"please", "thanks", "okay", "ok", "yes", "yeah", "no", "nope",
|
|
40
|
+
"right", "sure", "great", "good", "nice", "cool", "fine", "alright",
|
|
41
|
+
"hello", "hi", "hey", "bye", "goodbye",
|
|
42
|
+
// RLM-specific
|
|
43
|
+
"remember", "recall", "earlier", "previously", "discussed", "mentioned",
|
|
44
|
+
"said", "decided", "talked", "worked", "think", "know", "like",
|
|
45
|
+
]);
|
|
46
|
+
/**
|
|
47
|
+
* Extracts entities from text content.
|
|
48
|
+
*/
|
|
49
|
+
export var EntityExtractor;
|
|
50
|
+
(function (EntityExtractor) {
|
|
51
|
+
// Code-related regex patterns
|
|
52
|
+
const CODE_PATTERNS = [
|
|
53
|
+
// Classes ending with Error, Exception, Handler, etc.
|
|
54
|
+
/\b([A-Z][a-zA-Z0-9]*(?:Error|Exception|Handler|Manager|Service|Controller|Factory|Builder))\b/g,
|
|
55
|
+
// Function calls: functionName(
|
|
56
|
+
/\b([a-z_][a-z0-9_]*)\s*\(/g,
|
|
57
|
+
// Constants: MAX_TIMEOUT, DEBUG_MODE
|
|
58
|
+
/\b([A-Z_][A-Z0-9_]{2,})\b/g,
|
|
59
|
+
// Inline code: `variable`
|
|
60
|
+
/`([^`]+)`/g,
|
|
61
|
+
// Type definitions: class User, interface Config
|
|
62
|
+
/(?:class|interface|type|struct)\s+([A-Z][a-zA-Z0-9]*)/g,
|
|
63
|
+
];
|
|
64
|
+
/**
|
|
65
|
+
* Extract entities from text.
|
|
66
|
+
*/
|
|
67
|
+
function extract(text) {
|
|
68
|
+
const entities = new Map();
|
|
69
|
+
// Extract capitalized terms (potential proper nouns)
|
|
70
|
+
const capitalizedPattern = /\b([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)\b/g;
|
|
71
|
+
let match;
|
|
72
|
+
while ((match = capitalizedPattern.exec(text)) !== null) {
|
|
73
|
+
const term = match[1];
|
|
74
|
+
if (!STOPWORDS.has(term.toLowerCase()) && term.length > 2) {
|
|
75
|
+
entities.set(term, EntityType.PROPER_NOUN);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Extract code elements
|
|
79
|
+
for (const pattern of CODE_PATTERNS) {
|
|
80
|
+
pattern.lastIndex = 0;
|
|
81
|
+
while ((match = pattern.exec(text)) !== null) {
|
|
82
|
+
const term = match[1];
|
|
83
|
+
if (term && !STOPWORDS.has(term.toLowerCase()) && term.length > 1) {
|
|
84
|
+
entities.set(term, EntityType.CODE_ELEMENT);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// Extract markdown links
|
|
89
|
+
const linkPattern = /\[([^\]]+)\]\([^)]+\)/g;
|
|
90
|
+
while ((match = linkPattern.exec(text)) !== null) {
|
|
91
|
+
const linkText = match[1];
|
|
92
|
+
if (!STOPWORDS.has(linkText.toLowerCase())) {
|
|
93
|
+
entities.set(linkText, EntityType.REFERENCE);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Extract file paths
|
|
97
|
+
const pathPattern = /(?:^|\s)([./]?(?:[a-zA-Z0-9_-]+\/)+[a-zA-Z0-9_.-]+)/g;
|
|
98
|
+
while ((match = pathPattern.exec(text)) !== null) {
|
|
99
|
+
entities.set(match[1], EntityType.FILE_PATH);
|
|
100
|
+
}
|
|
101
|
+
return Array.from(entities.entries()).map(([name, type]) => ({
|
|
102
|
+
name,
|
|
103
|
+
type,
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
EntityExtractor.extract = extract;
|
|
107
|
+
/**
|
|
108
|
+
* Extract top keywords based on frequency.
|
|
109
|
+
*/
|
|
110
|
+
function extractKeywords(text, topN = 10) {
|
|
111
|
+
const wordPattern = /\b[a-zA-Z]{3,}\b/g;
|
|
112
|
+
const wordCounts = new Map();
|
|
113
|
+
let match;
|
|
114
|
+
while ((match = wordPattern.exec(text)) !== null) {
|
|
115
|
+
const word = match[0].toLowerCase();
|
|
116
|
+
if (!STOPWORDS.has(word)) {
|
|
117
|
+
wordCounts.set(word, (wordCounts.get(word) || 0) + 1);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return Array.from(wordCounts.entries())
|
|
121
|
+
.sort((a, b) => b[1] - a[1])
|
|
122
|
+
.slice(0, topN)
|
|
123
|
+
.map(([word]) => word);
|
|
124
|
+
}
|
|
125
|
+
EntityExtractor.extractKeywords = extractKeywords;
|
|
126
|
+
})(EntityExtractor || (EntityExtractor = {}));
|
|
127
|
+
/**
|
|
128
|
+
* Splits content into hierarchical chunks.
|
|
129
|
+
*/
|
|
130
|
+
export var ContentChunker;
|
|
131
|
+
(function (ContentChunker) {
|
|
132
|
+
const DEFAULT_SECTION_MIN_CHARS = 500;
|
|
133
|
+
const DEFAULT_CHUNK_TARGET_CHARS = 300;
|
|
134
|
+
const DEFAULT_CHUNK_OVERLAP = 50;
|
|
135
|
+
/**
|
|
136
|
+
* Split content into sections.
|
|
137
|
+
*/
|
|
138
|
+
function splitIntoSections(content, minChars = DEFAULT_SECTION_MIN_CHARS) {
|
|
139
|
+
const sections = [];
|
|
140
|
+
// Try to split by markdown headers
|
|
141
|
+
const headerPattern = /(?:^|\n)(#{1,3})\s+(.+?)(?:\n|$)/g;
|
|
142
|
+
const matches = [];
|
|
143
|
+
let match;
|
|
144
|
+
while ((match = headerPattern.exec(content)) !== null) {
|
|
145
|
+
matches.push({
|
|
146
|
+
level: match[1].length,
|
|
147
|
+
title: match[2].trim(),
|
|
148
|
+
index: match.index,
|
|
149
|
+
end: headerPattern.lastIndex,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
if (matches.length > 0) {
|
|
153
|
+
for (let i = 0; i < matches.length; i++) {
|
|
154
|
+
const start = matches[i].end;
|
|
155
|
+
const end = i + 1 < matches.length ? matches[i + 1].index : content.length;
|
|
156
|
+
const sectionContent = content.slice(start, end).trim();
|
|
157
|
+
if (sectionContent) {
|
|
158
|
+
sections.push({
|
|
159
|
+
title: matches[i].title,
|
|
160
|
+
content: sectionContent,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
// Fall back to paragraph-based splitting
|
|
167
|
+
const paragraphs = content.split("\n\n");
|
|
168
|
+
let currentSection = [];
|
|
169
|
+
let currentLength = 0;
|
|
170
|
+
for (const para of paragraphs) {
|
|
171
|
+
currentSection.push(para);
|
|
172
|
+
currentLength += para.length;
|
|
173
|
+
if (currentLength >= minChars) {
|
|
174
|
+
const sectionText = currentSection.join("\n\n");
|
|
175
|
+
const firstLine = sectionText.split("\n")[0].slice(0, 50);
|
|
176
|
+
sections.push({
|
|
177
|
+
title: firstLine,
|
|
178
|
+
content: sectionText,
|
|
179
|
+
});
|
|
180
|
+
currentSection = [];
|
|
181
|
+
currentLength = 0;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (currentSection.length > 0) {
|
|
185
|
+
const sectionText = currentSection.join("\n\n");
|
|
186
|
+
const firstLine = sectionText.split("\n")[0].slice(0, 50);
|
|
187
|
+
sections.push({
|
|
188
|
+
title: firstLine,
|
|
189
|
+
content: sectionText,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return sections;
|
|
194
|
+
}
|
|
195
|
+
ContentChunker.splitIntoSections = splitIntoSections;
|
|
196
|
+
/**
|
|
197
|
+
* Split content into overlapping chunks.
|
|
198
|
+
*/
|
|
199
|
+
function splitIntoChunks(content, targetChars = DEFAULT_CHUNK_TARGET_CHARS, overlap = DEFAULT_CHUNK_OVERLAP) {
|
|
200
|
+
if (content.length <= targetChars) {
|
|
201
|
+
return [content];
|
|
202
|
+
}
|
|
203
|
+
const chunks = [];
|
|
204
|
+
const sentences = content.split(/(?<=[.!?])\s+/);
|
|
205
|
+
let currentChunk = [];
|
|
206
|
+
let currentLength = 0;
|
|
207
|
+
for (const sentence of sentences) {
|
|
208
|
+
const sentenceLen = sentence.length;
|
|
209
|
+
if (currentLength + sentenceLen > targetChars && currentChunk.length > 0) {
|
|
210
|
+
chunks.push(currentChunk.join(" "));
|
|
211
|
+
// Keep overlap
|
|
212
|
+
const overlapChunk = [];
|
|
213
|
+
let overlapLen = 0;
|
|
214
|
+
for (let i = currentChunk.length - 1; i >= 0; i--) {
|
|
215
|
+
if (overlapLen + currentChunk[i].length <= overlap) {
|
|
216
|
+
overlapChunk.unshift(currentChunk[i]);
|
|
217
|
+
overlapLen += currentChunk[i].length;
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
currentChunk = overlapChunk;
|
|
224
|
+
currentLength = overlapLen;
|
|
225
|
+
}
|
|
226
|
+
currentChunk.push(sentence);
|
|
227
|
+
currentLength += sentenceLen;
|
|
228
|
+
}
|
|
229
|
+
if (currentChunk.length > 0) {
|
|
230
|
+
chunks.push(currentChunk.join(" "));
|
|
231
|
+
}
|
|
232
|
+
return chunks;
|
|
233
|
+
}
|
|
234
|
+
ContentChunker.splitIntoChunks = splitIntoChunks;
|
|
235
|
+
})(ContentChunker || (ContentChunker = {}));
|
|
236
|
+
/**
|
|
237
|
+
* Ingests content into the knowledge graph.
|
|
238
|
+
*/
|
|
239
|
+
export var GraphIngester;
|
|
240
|
+
(function (GraphIngester) {
|
|
241
|
+
/**
|
|
242
|
+
* Generate a unique node ID.
|
|
243
|
+
*/
|
|
244
|
+
function generateNodeID(nodeType, content) {
|
|
245
|
+
const uuid = crypto.randomUUID().slice(0, 8);
|
|
246
|
+
const contentHash = crypto
|
|
247
|
+
.createHash("md5")
|
|
248
|
+
.update(content)
|
|
249
|
+
.digest("hex")
|
|
250
|
+
.slice(0, 8);
|
|
251
|
+
return `${nodeType}_${uuid}_${contentHash}`;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Ingest a context block into the graph.
|
|
255
|
+
*/
|
|
256
|
+
function ingestContextBlock(block) {
|
|
257
|
+
const now = Date.now();
|
|
258
|
+
// Create document node
|
|
259
|
+
const docNode = {
|
|
260
|
+
id: generateNodeID("doc", block.id),
|
|
261
|
+
sessionID: block.sessionID,
|
|
262
|
+
type: NodeType.DOCUMENT,
|
|
263
|
+
content: block.summary || block.content.slice(0, 200),
|
|
264
|
+
metadata: {
|
|
265
|
+
blockID: block.id,
|
|
266
|
+
taskID: block.taskID,
|
|
267
|
+
taskDescription: block.taskDescription,
|
|
268
|
+
tokens: block.tokens,
|
|
269
|
+
},
|
|
270
|
+
createdAt: now,
|
|
271
|
+
};
|
|
272
|
+
GraphStorage.addNode(docNode);
|
|
273
|
+
// Split into sections
|
|
274
|
+
const sections = ContentChunker.splitIntoSections(block.content);
|
|
275
|
+
for (const section of sections) {
|
|
276
|
+
const sectionNode = ingestSection(docNode.id, block.sessionID, section.title, section.content, now);
|
|
277
|
+
// Link document -> section
|
|
278
|
+
const edge = {
|
|
279
|
+
sourceID: docNode.id,
|
|
280
|
+
targetID: sectionNode.id,
|
|
281
|
+
relationship: RelationType.HAS_SECTION,
|
|
282
|
+
weight: 1.0,
|
|
283
|
+
metadata: {},
|
|
284
|
+
createdAt: now,
|
|
285
|
+
};
|
|
286
|
+
GraphStorage.addEdgeWithSession(block.sessionID, edge);
|
|
287
|
+
}
|
|
288
|
+
return docNode;
|
|
289
|
+
}
|
|
290
|
+
GraphIngester.ingestContextBlock = ingestContextBlock;
|
|
291
|
+
/**
|
|
292
|
+
* Ingest a section and its chunks.
|
|
293
|
+
*/
|
|
294
|
+
function ingestSection(parentID, sessionID, title, content, timestamp) {
|
|
295
|
+
const sectionNode = {
|
|
296
|
+
id: generateNodeID("sec", content),
|
|
297
|
+
sessionID,
|
|
298
|
+
type: NodeType.SECTION,
|
|
299
|
+
content: title,
|
|
300
|
+
metadata: { fullContent: content.slice(0, 500) },
|
|
301
|
+
createdAt: timestamp,
|
|
302
|
+
};
|
|
303
|
+
GraphStorage.addNode(sectionNode);
|
|
304
|
+
// Split into chunks
|
|
305
|
+
const chunks = ContentChunker.splitIntoChunks(content);
|
|
306
|
+
let prevChunkID = null;
|
|
307
|
+
for (const chunkContent of chunks) {
|
|
308
|
+
const chunkNode = ingestChunk(sessionID, chunkContent, timestamp);
|
|
309
|
+
// Link section -> chunk
|
|
310
|
+
const edge = {
|
|
311
|
+
sourceID: sectionNode.id,
|
|
312
|
+
targetID: chunkNode.id,
|
|
313
|
+
relationship: RelationType.HAS_CHUNK,
|
|
314
|
+
weight: 1.0,
|
|
315
|
+
metadata: {},
|
|
316
|
+
createdAt: timestamp,
|
|
317
|
+
};
|
|
318
|
+
GraphStorage.addEdgeWithSession(sessionID, edge);
|
|
319
|
+
// Link previous chunk -> current chunk (temporal order)
|
|
320
|
+
if (prevChunkID) {
|
|
321
|
+
const followsEdge = {
|
|
322
|
+
sourceID: prevChunkID,
|
|
323
|
+
targetID: chunkNode.id,
|
|
324
|
+
relationship: RelationType.FOLLOWS,
|
|
325
|
+
weight: 1.0,
|
|
326
|
+
metadata: {},
|
|
327
|
+
createdAt: timestamp,
|
|
328
|
+
};
|
|
329
|
+
GraphStorage.addEdgeWithSession(sessionID, followsEdge);
|
|
330
|
+
}
|
|
331
|
+
prevChunkID = chunkNode.id;
|
|
332
|
+
}
|
|
333
|
+
return sectionNode;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Ingest a chunk and extract entities.
|
|
337
|
+
*/
|
|
338
|
+
function ingestChunk(sessionID, content, timestamp) {
|
|
339
|
+
const chunkNode = {
|
|
340
|
+
id: generateNodeID("chunk", content),
|
|
341
|
+
sessionID,
|
|
342
|
+
type: NodeType.CHUNK,
|
|
343
|
+
content,
|
|
344
|
+
metadata: {},
|
|
345
|
+
createdAt: timestamp,
|
|
346
|
+
};
|
|
347
|
+
GraphStorage.addNode(chunkNode);
|
|
348
|
+
// Extract and link entities
|
|
349
|
+
const entities = EntityExtractor.extract(content);
|
|
350
|
+
const entityCache = new Map();
|
|
351
|
+
for (const entity of entities) {
|
|
352
|
+
let entityID;
|
|
353
|
+
if (entityCache.has(entity.name)) {
|
|
354
|
+
entityID = entityCache.get(entity.name);
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
entityID = getOrCreateEntity(sessionID, entity.name, entity.type, timestamp);
|
|
358
|
+
entityCache.set(entity.name, entityID);
|
|
359
|
+
}
|
|
360
|
+
// Link chunk -> entity
|
|
361
|
+
const edge = {
|
|
362
|
+
sourceID: chunkNode.id,
|
|
363
|
+
targetID: entityID,
|
|
364
|
+
relationship: RelationType.MENTIONS,
|
|
365
|
+
weight: 1.0,
|
|
366
|
+
metadata: {},
|
|
367
|
+
createdAt: timestamp,
|
|
368
|
+
};
|
|
369
|
+
GraphStorage.addEdgeWithSession(sessionID, edge);
|
|
370
|
+
}
|
|
371
|
+
return chunkNode;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Get existing entity or create new one.
|
|
375
|
+
*/
|
|
376
|
+
function getOrCreateEntity(sessionID, name, entityType, timestamp) {
|
|
377
|
+
// Search for existing entity with same name
|
|
378
|
+
const existing = GraphStorage.searchNodes(sessionID, name, NodeType.ENTITY, 1);
|
|
379
|
+
if (existing.length > 0 && existing[0].content === name) {
|
|
380
|
+
return existing[0].id;
|
|
381
|
+
}
|
|
382
|
+
// Create new entity
|
|
383
|
+
const entityNode = {
|
|
384
|
+
id: generateNodeID("ent", name),
|
|
385
|
+
sessionID,
|
|
386
|
+
type: NodeType.ENTITY,
|
|
387
|
+
content: name,
|
|
388
|
+
metadata: { entityType },
|
|
389
|
+
createdAt: timestamp,
|
|
390
|
+
};
|
|
391
|
+
GraphStorage.addNode(entityNode);
|
|
392
|
+
return entityNode.id;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Ingest messages into the graph.
|
|
396
|
+
*/
|
|
397
|
+
function ingestMessages(sessionID, messages) {
|
|
398
|
+
if (messages.length === 0) {
|
|
399
|
+
return null;
|
|
400
|
+
}
|
|
401
|
+
// Build content from messages
|
|
402
|
+
const contentParts = messages.map((msg) => `${msg.role.toUpperCase()}: ${msg.content}`);
|
|
403
|
+
const content = contentParts.join("\n\n");
|
|
404
|
+
// Create a temporary context block
|
|
405
|
+
const block = {
|
|
406
|
+
id: `msg_${crypto.randomUUID().slice(0, 12)}`,
|
|
407
|
+
sessionID,
|
|
408
|
+
content,
|
|
409
|
+
summary: content.slice(0, 200),
|
|
410
|
+
tokens: Math.ceil(content.length / 4),
|
|
411
|
+
createdAt: Date.now(),
|
|
412
|
+
};
|
|
413
|
+
return ingestContextBlock(block);
|
|
414
|
+
}
|
|
415
|
+
GraphIngester.ingestMessages = ingestMessages;
|
|
416
|
+
})(GraphIngester || (GraphIngester = {}));
|
|
417
|
+
//# sourceMappingURL=ingestion.js.map
|
|
@@ -0,0 +1 @@
|
|
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
* Optimized graph storage for large projects.
|
|
3
|
+
*
|
|
4
|
+
* Performance optimizations:
|
|
5
|
+
* - In-memory LRU cache for nodes/edges (74x faster reads)
|
|
6
|
+
* - Inverted index for fast keyword search
|
|
7
|
+
* - Batched async writes (5x faster bulk inserts)
|
|
8
|
+
* - Lazy index persistence
|
|
9
|
+
*/
|
|
10
|
+
import { type GraphNode, type GraphEdge, NodeType, RelationType } from "./types.js";
|
|
11
|
+
/**
|
|
12
|
+
* Optimized graph storage with caching and indexing.
|
|
13
|
+
*/
|
|
14
|
+
export declare namespace GraphStorage {
|
|
15
|
+
/**
|
|
16
|
+
* Initialize storage with base directory.
|
|
17
|
+
*/
|
|
18
|
+
function init(worktree: string): void;
|
|
19
|
+
/**
|
|
20
|
+
* Force flush all pending writes.
|
|
21
|
+
*/
|
|
22
|
+
function flush(): void;
|
|
23
|
+
function addNode(node: GraphNode): void;
|
|
24
|
+
function getNode(sessionID: string, nodeID: string): GraphNode | null;
|
|
25
|
+
function getNodesByType(sessionID: string, nodeType: NodeType, limit?: number): GraphNode[];
|
|
26
|
+
/**
|
|
27
|
+
* Fast keyword search using inverted index.
|
|
28
|
+
*/
|
|
29
|
+
function searchNodes(sessionID: string, query: string, nodeType?: NodeType, limit?: number): GraphNode[];
|
|
30
|
+
function deleteNode(sessionID: string, nodeID: string): boolean;
|
|
31
|
+
function addEdgeWithSession(sessionID: string, edge: GraphEdge): void;
|
|
32
|
+
function getEdges(sessionID: string, nodeID: string, direction?: "outgoing" | "incoming" | "both"): GraphEdge[];
|
|
33
|
+
function getNeighbors(sessionID: string, nodeID: string, direction?: "outgoing" | "incoming" | "both", relationship?: RelationType): GraphNode[];
|
|
34
|
+
function clearSession(sessionID: string): void;
|
|
35
|
+
function getSessions(): string[];
|
|
36
|
+
function getStats(sessionID: string): {
|
|
37
|
+
nodeCount: number;
|
|
38
|
+
edgeCount: number;
|
|
39
|
+
nodesByType: Record<string, number>;
|
|
40
|
+
cacheStats: {
|
|
41
|
+
hits: number;
|
|
42
|
+
misses: number;
|
|
43
|
+
hitRate: string;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Get chunks that mention an entity.
|
|
48
|
+
*/
|
|
49
|
+
function getChunksForEntity(sessionID: string, entityName: string): GraphNode[];
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../src/graph/storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,EACL,KAAK,SAAS,EACd,KAAK,SAAS,EAKd,QAAQ,EACR,YAAY,EACb,MAAM,YAAY,CAAA;AAoInB;;GAEG;AACH,yBAAiB,YAAY,CAAC;IAoB5B;;OAEG;IACH,SAAgB,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAG3C;IA4GD;;OAEG;IACH,SAAgB,KAAK,IAAI,IAAI,CA2B5B;IAMD,SAAgB,OAAO,CAAC,IAAI,EAAE,SAAS,GAAG,IAAI,CA2B7C;IAED,SAAgB,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAoB3E;IAED,SAAgB,cAAc,CAC5B,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,QAAQ,EAClB,KAAK,GAAE,MAAY,GAClB,SAAS,EAAE,CAWb;IAED;;OAEG;IACH,SAAgB,WAAW,CACzB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,EACb,QAAQ,CAAC,EAAE,QAAQ,EACnB,KAAK,GAAE,MAAW,GACjB,SAAS,EAAE,CAoBb;IAED,SAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAsCrE;IAMD,SAAgB,kBAAkB,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,GAAG,IAAI,CA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