mcard-js 2.0.0 → 2.1.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/README.md +91 -3
- package/dist/model/ContentTypeInterpreter.d.ts +16 -0
- package/dist/model/ContentTypeInterpreter.d.ts.map +1 -0
- package/dist/model/ContentTypeInterpreter.js +78 -0
- package/dist/model/ContentTypeInterpreter.js.map +1 -0
- package/dist/model/GTime.d.ts +15 -0
- package/dist/model/GTime.d.ts.map +1 -1
- package/dist/model/GTime.js +44 -2
- package/dist/model/GTime.js.map +1 -1
- package/dist/model/detectors/BaseDetector.d.ts +26 -0
- package/dist/model/detectors/BaseDetector.d.ts.map +1 -0
- package/dist/model/detectors/BaseDetector.js +5 -0
- package/dist/model/detectors/BaseDetector.js.map +1 -0
- package/dist/model/detectors/BinaryDetector.d.ts +12 -0
- package/dist/model/detectors/BinaryDetector.d.ts.map +1 -0
- package/dist/model/detectors/BinaryDetector.js +82 -0
- package/dist/model/detectors/BinaryDetector.js.map +1 -0
- package/dist/model/detectors/DataFormatDetectors.d.ts +29 -0
- package/dist/model/detectors/DataFormatDetectors.d.ts.map +1 -0
- package/dist/model/detectors/DataFormatDetectors.js +223 -0
- package/dist/model/detectors/DataFormatDetectors.js.map +1 -0
- package/dist/model/detectors/LanguageDetector.d.ts +11 -0
- package/dist/model/detectors/LanguageDetector.d.ts.map +1 -0
- package/dist/model/detectors/LanguageDetector.js +148 -0
- package/dist/model/detectors/LanguageDetector.js.map +1 -0
- package/dist/model/detectors/MarkupDetectors.d.ts +22 -0
- package/dist/model/detectors/MarkupDetectors.d.ts.map +1 -0
- package/dist/model/detectors/MarkupDetectors.js +132 -0
- package/dist/model/detectors/MarkupDetectors.js.map +1 -0
- package/dist/model/detectors/OBJDetector.d.ts +8 -0
- package/dist/model/detectors/OBJDetector.d.ts.map +1 -0
- package/dist/model/detectors/OBJDetector.js +48 -0
- package/dist/model/detectors/OBJDetector.js.map +1 -0
- package/dist/model/detectors/registry.d.ts +13 -0
- package/dist/model/detectors/registry.d.ts.map +1 -0
- package/dist/model/detectors/registry.js +67 -0
- package/dist/model/detectors/registry.js.map +1 -0
- package/dist/ptr/llm/providers/OllamaEmbeddingProvider.d.ts +20 -0
- package/dist/ptr/llm/providers/OllamaEmbeddingProvider.d.ts.map +1 -0
- package/dist/ptr/llm/providers/OllamaEmbeddingProvider.js +42 -0
- package/dist/ptr/llm/providers/OllamaEmbeddingProvider.js.map +1 -0
- package/dist/ptr/node/CLMLoader.d.ts.map +1 -1
- package/dist/ptr/node/CLMLoader.js +8 -0
- package/dist/ptr/node/CLMLoader.js.map +1 -1
- package/dist/ptr/node/CLMRunner.d.ts.map +1 -1
- package/dist/ptr/node/CLMRunner.js +4 -3
- package/dist/ptr/node/CLMRunner.js.map +1 -1
- package/dist/ptr/node/Runtimes.d.ts +7 -0
- package/dist/ptr/node/Runtimes.d.ts.map +1 -1
- package/dist/ptr/node/Runtimes.js +94 -3
- package/dist/ptr/node/Runtimes.js.map +1 -1
- package/dist/rag/HandleVectorStore.d.ts +201 -0
- package/dist/rag/HandleVectorStore.d.ts.map +1 -0
- package/dist/rag/HandleVectorStore.js +527 -0
- package/dist/rag/HandleVectorStore.js.map +1 -0
- package/dist/rag/PersistentIndexer.d.ts +144 -0
- package/dist/rag/PersistentIndexer.d.ts.map +1 -0
- package/dist/rag/PersistentIndexer.js +275 -0
- package/dist/rag/PersistentIndexer.js.map +1 -0
- package/dist/rag/embeddings/VisionEmbeddingProvider.d.ts +87 -0
- package/dist/rag/embeddings/VisionEmbeddingProvider.d.ts.map +1 -0
- package/dist/rag/embeddings/VisionEmbeddingProvider.js +164 -0
- package/dist/rag/embeddings/VisionEmbeddingProvider.js.map +1 -0
- package/dist/rag/graph/community.d.ts +56 -0
- package/dist/rag/graph/community.d.ts.map +1 -0
- package/dist/rag/graph/community.js +247 -0
- package/dist/rag/graph/community.js.map +1 -0
- package/dist/rag/graph/extractor.d.ts +99 -0
- package/dist/rag/graph/extractor.d.ts.map +1 -0
- package/dist/rag/graph/extractor.js +210 -0
- package/dist/rag/graph/extractor.js.map +1 -0
- package/dist/rag/graph/store.d.ts +176 -0
- package/dist/rag/graph/store.d.ts.map +1 -0
- package/dist/rag/graph/store.js +504 -0
- package/dist/rag/graph/store.js.map +1 -0
- package/dist/rag/index.d.ts +19 -0
- package/dist/rag/index.d.ts.map +1 -0
- package/dist/rag/index.js +24 -0
- package/dist/rag/index.js.map +1 -0
- package/dist/rag/semanticVersioning.d.ts +187 -0
- package/dist/rag/semanticVersioning.d.ts.map +1 -0
- package/dist/rag/semanticVersioning.js +253 -0
- package/dist/rag/semanticVersioning.js.map +1 -0
- package/dist/storage/IndexedDBEngine.d.ts.map +1 -1
- package/dist/storage/IndexedDBEngine.js +9 -1
- package/dist/storage/IndexedDBEngine.js.map +1 -1
- package/dist/storage/SqliteNodeEngine.d.ts.map +1 -1
- package/dist/storage/SqliteNodeEngine.js +26 -4
- package/dist/storage/SqliteNodeEngine.js.map +1 -1
- package/dist/storage/SqliteWasmEngine.d.ts.map +1 -1
- package/dist/storage/SqliteWasmEngine.js +9 -1
- package/dist/storage/SqliteWasmEngine.js.map +1 -1
- package/dist/storage/StorageAdapter.d.ts +2 -0
- package/dist/storage/StorageAdapter.d.ts.map +1 -1
- package/dist/storage/VectorStore.d.ts +28 -5
- package/dist/storage/VectorStore.d.ts.map +1 -1
- package/dist/storage/VectorStore.js +110 -50
- package/dist/storage/VectorStore.js.map +1 -1
- package/dist/storage/schema.d.ts +123 -80
- package/dist/storage/schema.d.ts.map +1 -1
- package/dist/storage/schema.js +305 -217
- package/dist/storage/schema.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Graph Extractor
|
|
3
|
+
*
|
|
4
|
+
* Extracts entities and relationships from MCard content using LLM.
|
|
5
|
+
*
|
|
6
|
+
* Mirrors Python: mcard/rag/graph/extractor.py
|
|
7
|
+
*/
|
|
8
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
9
|
+
// Factory Functions
|
|
10
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
11
|
+
/**
|
|
12
|
+
* Create an Entity object
|
|
13
|
+
*/
|
|
14
|
+
export function createEntity(name, type = 'OTHER', description = '') {
|
|
15
|
+
return { name, type, description };
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create a Relationship object
|
|
19
|
+
*/
|
|
20
|
+
export function createRelationship(source, target, relationship, description = '', weight = 1.0) {
|
|
21
|
+
return { source, target, relationship, description, weight };
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Create an ExtractionResult object
|
|
25
|
+
*/
|
|
26
|
+
export function createExtractionResult(entities = [], relationships = [], success = true, error) {
|
|
27
|
+
return { entities, relationships, success, error };
|
|
28
|
+
}
|
|
29
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
30
|
+
// Extraction Prompts
|
|
31
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
32
|
+
export const EXTRACTION_SYSTEM_PROMPT = `You are an expert at extracting structured information from text.
|
|
33
|
+
Given a text, identify:
|
|
34
|
+
1. ENTITIES: Named concepts, technologies, people, organizations, or things
|
|
35
|
+
2. RELATIONSHIPS: How entities relate to each other
|
|
36
|
+
|
|
37
|
+
Respond ONLY with valid JSON in this format:
|
|
38
|
+
{
|
|
39
|
+
"entities": [
|
|
40
|
+
{"name": "EntityName", "type": "CONCEPT|TECHNOLOGY|PERSON|ORGANIZATION|OTHER", "description": "Brief description"}
|
|
41
|
+
],
|
|
42
|
+
"relationships": [
|
|
43
|
+
{"source": "Entity1", "target": "Entity2", "relationship": "verb phrase", "description": "Optional context"}
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
Entity types:
|
|
48
|
+
- CONCEPT: Abstract ideas, methodologies, patterns (e.g., "content-addressable storage")
|
|
49
|
+
- TECHNOLOGY: Systems, libraries, frameworks (e.g., "SQLite", "Python")
|
|
50
|
+
- PERSON: People names
|
|
51
|
+
- ORGANIZATION: Companies, groups
|
|
52
|
+
- OTHER: Anything else
|
|
53
|
+
|
|
54
|
+
Keep entity names concise but unique. Use present tense for relationships.`;
|
|
55
|
+
export const EXTRACTION_USER_PROMPT = `Extract entities and relationships from this text:
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
{content}
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
Remember: Return ONLY valid JSON.`;
|
|
62
|
+
const DEFAULT_EXTRACTOR_CONFIG = {
|
|
63
|
+
model: 'gemma3:latest',
|
|
64
|
+
temperature: 0.1,
|
|
65
|
+
maxRetries: 2,
|
|
66
|
+
ollamaBaseUrl: 'http://localhost:11434',
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Extracts entities and relationships from text using LLM.
|
|
70
|
+
*
|
|
71
|
+
* Usage:
|
|
72
|
+
* const extractor = new GraphExtractor({ model: 'gemma3:latest' });
|
|
73
|
+
* const result = await extractor.extract("MCard is a TypeScript library...");
|
|
74
|
+
*
|
|
75
|
+
* for (const entity of result.entities) {
|
|
76
|
+
* console.log(`${entity.name} (${entity.type})`);
|
|
77
|
+
* }
|
|
78
|
+
*
|
|
79
|
+
* for (const rel of result.relationships) {
|
|
80
|
+
* console.log(`${rel.source} --${rel.relationship}--> ${rel.target}`);
|
|
81
|
+
* }
|
|
82
|
+
*/
|
|
83
|
+
export class GraphExtractor {
|
|
84
|
+
config;
|
|
85
|
+
constructor(config = {}) {
|
|
86
|
+
this.config = { ...DEFAULT_EXTRACTOR_CONFIG, ...config };
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Extract entities and relationships from content.
|
|
90
|
+
*
|
|
91
|
+
* @param content - Text to extract from
|
|
92
|
+
* @returns ExtractionResult with entities and relationships
|
|
93
|
+
*/
|
|
94
|
+
async extract(content) {
|
|
95
|
+
if (!content || !content.trim()) {
|
|
96
|
+
return createExtractionResult([], [], false, 'Empty content');
|
|
97
|
+
}
|
|
98
|
+
// Truncate if too long
|
|
99
|
+
const maxChars = 6000;
|
|
100
|
+
if (content.length > maxChars) {
|
|
101
|
+
content = content.slice(0, maxChars) + '\n[...truncated...]';
|
|
102
|
+
}
|
|
103
|
+
// Call LLM for extraction with retries
|
|
104
|
+
for (let attempt = 0; attempt <= this.config.maxRetries; attempt++) {
|
|
105
|
+
try {
|
|
106
|
+
const result = await this.callLLM(content);
|
|
107
|
+
const parsed = this.parseResponse(result);
|
|
108
|
+
if (parsed.entities.length > 0 || parsed.relationships.length > 0) {
|
|
109
|
+
return parsed;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
console.warn(`Extraction attempt ${attempt + 1} failed: ${error}`);
|
|
114
|
+
if (attempt === this.config.maxRetries) {
|
|
115
|
+
return createExtractionResult([], [], false, String(error));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return createExtractionResult([], [], false, 'No entities extracted');
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Call LLM for extraction
|
|
123
|
+
*/
|
|
124
|
+
async callLLM(content) {
|
|
125
|
+
const prompt = EXTRACTION_USER_PROMPT.replace('{content}', content);
|
|
126
|
+
const url = `${this.config.ollamaBaseUrl}/api/generate`;
|
|
127
|
+
const payload = {
|
|
128
|
+
model: this.config.model,
|
|
129
|
+
prompt: `${EXTRACTION_SYSTEM_PROMPT}\n\n${prompt}`,
|
|
130
|
+
stream: false,
|
|
131
|
+
options: {
|
|
132
|
+
temperature: this.config.temperature,
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
const response = await fetch(url, {
|
|
136
|
+
method: 'POST',
|
|
137
|
+
headers: { 'Content-Type': 'application/json' },
|
|
138
|
+
body: JSON.stringify(payload),
|
|
139
|
+
});
|
|
140
|
+
if (!response.ok) {
|
|
141
|
+
throw new Error(`LLM call failed: ${response.status} ${response.statusText}`);
|
|
142
|
+
}
|
|
143
|
+
const result = await response.json();
|
|
144
|
+
return result.response || '';
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Parse LLM response into structured data
|
|
148
|
+
*/
|
|
149
|
+
parseResponse(response) {
|
|
150
|
+
// Try to extract JSON from response
|
|
151
|
+
const jsonMatch = response.match(/\{[\s\S]*\}/);
|
|
152
|
+
if (!jsonMatch) {
|
|
153
|
+
throw new Error('No JSON found in response');
|
|
154
|
+
}
|
|
155
|
+
let jsonStr = jsonMatch[0];
|
|
156
|
+
let data;
|
|
157
|
+
try {
|
|
158
|
+
data = JSON.parse(jsonStr);
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
// Try to fix common issues
|
|
162
|
+
jsonStr = this.cleanJson(jsonStr);
|
|
163
|
+
data = JSON.parse(jsonStr);
|
|
164
|
+
}
|
|
165
|
+
const entities = [];
|
|
166
|
+
for (const e of (data.entities || [])) {
|
|
167
|
+
if (typeof e === 'object' && e.name) {
|
|
168
|
+
entities.push({
|
|
169
|
+
name: String(e.name).trim(),
|
|
170
|
+
type: String(e.type || 'OTHER').toUpperCase(),
|
|
171
|
+
description: String(e.description || '').trim(),
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const relationships = [];
|
|
176
|
+
for (const r of (data.relationships || [])) {
|
|
177
|
+
if (typeof r === 'object' && r.source && r.target) {
|
|
178
|
+
relationships.push({
|
|
179
|
+
source: String(r.source).trim(),
|
|
180
|
+
target: String(r.target).trim(),
|
|
181
|
+
relationship: String(r.relationship || 'relates_to').trim(),
|
|
182
|
+
description: String(r.description || '').trim(),
|
|
183
|
+
weight: 1.0,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return createExtractionResult(entities, relationships, true);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Try to clean up malformed JSON
|
|
191
|
+
*/
|
|
192
|
+
cleanJson(jsonStr) {
|
|
193
|
+
// Remove trailing commas
|
|
194
|
+
jsonStr = jsonStr.replace(/,\s*([}\]])/g, '$1');
|
|
195
|
+
// Remove control characters
|
|
196
|
+
jsonStr = jsonStr.replace(/[\x00-\x1f]/g, '');
|
|
197
|
+
return jsonStr;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Extract from multiple texts
|
|
201
|
+
*/
|
|
202
|
+
async extractBatch(contents) {
|
|
203
|
+
const results = [];
|
|
204
|
+
for (const content of contents) {
|
|
205
|
+
results.push(await this.extract(content));
|
|
206
|
+
}
|
|
207
|
+
return results;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=extractor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractor.js","sourceRoot":"","sources":["../../../src/rag/graph/extractor.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAuCH,gFAAgF;AAChF,oBAAoB;AACpB,gFAAgF;AAEhF;;GAEG;AACH,MAAM,UAAU,YAAY,CACxB,IAAY,EACZ,OAAmB,OAAO,EAC1B,cAAsB,EAAE;IAExB,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC;AACvC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAC9B,MAAc,EACd,MAAc,EACd,YAAoB,EACpB,cAAsB,EAAE,EACxB,SAAiB,GAAG;IAEpB,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,EAAE,CAAC;AACjE,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAClC,WAAqB,EAAE,EACvB,gBAAgC,EAAE,EAClC,UAAmB,IAAI,EACvB,KAAc;IAEd,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AACvD,CAAC;AAED,gFAAgF;AAChF,qBAAqB;AACrB,gFAAgF;AAEhF,MAAM,CAAC,MAAM,wBAAwB,GAAG;;;;;;;;;;;;;;;;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@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Graph Store
|
|
3
|
+
*
|
|
4
|
+
* SQLite-based knowledge graph storage with traversal capabilities.
|
|
5
|
+
*
|
|
6
|
+
* Mirrors Python: mcard/rag/graph/store.py
|
|
7
|
+
*/
|
|
8
|
+
import { Entity, ExtractionResult } from './extractor';
|
|
9
|
+
export interface EntityRecord {
|
|
10
|
+
id: number;
|
|
11
|
+
name: string;
|
|
12
|
+
type: string;
|
|
13
|
+
description: string;
|
|
14
|
+
sourceHash: string;
|
|
15
|
+
createdAt?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface RelationshipRecord {
|
|
18
|
+
relId: number;
|
|
19
|
+
relationship: string;
|
|
20
|
+
description: string;
|
|
21
|
+
weight: number;
|
|
22
|
+
targetId?: number;
|
|
23
|
+
targetName?: string;
|
|
24
|
+
targetType?: string;
|
|
25
|
+
sourceId?: number;
|
|
26
|
+
sourceName?: string;
|
|
27
|
+
sourceType?: string;
|
|
28
|
+
}
|
|
29
|
+
export interface CommunityRecord {
|
|
30
|
+
id: number;
|
|
31
|
+
title: string;
|
|
32
|
+
summary: string;
|
|
33
|
+
memberIds: number[];
|
|
34
|
+
level: number;
|
|
35
|
+
}
|
|
36
|
+
export interface GraphStats {
|
|
37
|
+
entityCount: number;
|
|
38
|
+
relationshipCount: number;
|
|
39
|
+
extractionCount: number;
|
|
40
|
+
entityTypes: Record<string, number>;
|
|
41
|
+
}
|
|
42
|
+
export interface RelatedEntity {
|
|
43
|
+
entity: EntityRecord;
|
|
44
|
+
depth: number;
|
|
45
|
+
path: string[];
|
|
46
|
+
relationship: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* SQLite-based storage for knowledge graph.
|
|
50
|
+
*
|
|
51
|
+
* Features:
|
|
52
|
+
* - Entity and relationship storage
|
|
53
|
+
* - Graph traversal (BFS)
|
|
54
|
+
* - Multi-hop pathfinding
|
|
55
|
+
* - Community management
|
|
56
|
+
*
|
|
57
|
+
* Usage:
|
|
58
|
+
* const store = new GraphStore('graph.db');
|
|
59
|
+
*
|
|
60
|
+
* // Add entities
|
|
61
|
+
* const entityId = store.addEntity(entity, sourceHash);
|
|
62
|
+
*
|
|
63
|
+
* // Add relationships
|
|
64
|
+
* store.addRelationship(sourceId, targetId, 'relates_to', sourceHash);
|
|
65
|
+
*
|
|
66
|
+
* // Find related entities
|
|
67
|
+
* const related = store.findRelated('MCard', 2);
|
|
68
|
+
*/
|
|
69
|
+
export declare class GraphStore {
|
|
70
|
+
private db;
|
|
71
|
+
readonly dbPath: string;
|
|
72
|
+
constructor(dbPath?: string);
|
|
73
|
+
private initDatabase;
|
|
74
|
+
/**
|
|
75
|
+
* Add an entity to the graph.
|
|
76
|
+
*
|
|
77
|
+
* @param entity - Entity to add
|
|
78
|
+
* @param sourceHash - Source MCard hash
|
|
79
|
+
* @param embedding - Optional entity embedding
|
|
80
|
+
* @returns Entity ID
|
|
81
|
+
*/
|
|
82
|
+
addEntity(entity: Entity, sourceHash: string, embedding?: Uint8Array): number;
|
|
83
|
+
/**
|
|
84
|
+
* Get entity by name (case-insensitive)
|
|
85
|
+
*/
|
|
86
|
+
getEntityByName(name: string): EntityRecord | null;
|
|
87
|
+
/**
|
|
88
|
+
* Get entity by ID
|
|
89
|
+
*/
|
|
90
|
+
getEntityById(entityId: number): EntityRecord | null;
|
|
91
|
+
/**
|
|
92
|
+
* Search entities by name pattern
|
|
93
|
+
*/
|
|
94
|
+
searchEntities(query: string, typeFilter?: string, limit?: number): EntityRecord[];
|
|
95
|
+
/**
|
|
96
|
+
* Get all entities from a source MCard
|
|
97
|
+
*/
|
|
98
|
+
getEntitiesBySource(sourceHash: string): EntityRecord[];
|
|
99
|
+
/**
|
|
100
|
+
* Add a relationship between entities.
|
|
101
|
+
*
|
|
102
|
+
* @param sourceEntityId - Source entity ID
|
|
103
|
+
* @param targetEntityId - Target entity ID
|
|
104
|
+
* @param relationship - Relationship type/verb
|
|
105
|
+
* @param sourceHash - Source MCard hash
|
|
106
|
+
* @param description - Optional description
|
|
107
|
+
* @param weight - Relationship weight
|
|
108
|
+
* @returns Relationship ID
|
|
109
|
+
*/
|
|
110
|
+
addRelationship(sourceEntityId: number, targetEntityId: number, relationship: string, sourceHash: string, description?: string, weight?: number): number;
|
|
111
|
+
/**
|
|
112
|
+
* Get outgoing relationships from an entity
|
|
113
|
+
*/
|
|
114
|
+
getRelationshipsFrom(entityId: number): RelationshipRecord[];
|
|
115
|
+
/**
|
|
116
|
+
* Get incoming relationships to an entity
|
|
117
|
+
*/
|
|
118
|
+
getRelationshipsTo(entityId: number): RelationshipRecord[];
|
|
119
|
+
/**
|
|
120
|
+
* Add a community summary
|
|
121
|
+
*/
|
|
122
|
+
addCommunity(title: string, summary: string, memberIds: number[], level?: number, parentId?: number, embedding?: Uint8Array): number;
|
|
123
|
+
/**
|
|
124
|
+
* Get communities by level
|
|
125
|
+
*/
|
|
126
|
+
getCommunities(level?: number): CommunityRecord[];
|
|
127
|
+
/**
|
|
128
|
+
* Find entities within N hops of a given entity.
|
|
129
|
+
*
|
|
130
|
+
* @param entityName - Starting entity name
|
|
131
|
+
* @param hops - Maximum traversal depth
|
|
132
|
+
* @param direction - 'outgoing', 'incoming', or 'both'
|
|
133
|
+
* @returns List of related entities with their paths
|
|
134
|
+
*/
|
|
135
|
+
findRelated(entityName: string, hops?: number, direction?: 'outgoing' | 'incoming' | 'both'): RelatedEntity[];
|
|
136
|
+
/**
|
|
137
|
+
* Find shortest path between two entities.
|
|
138
|
+
*
|
|
139
|
+
* @param sourceName - Starting entity name
|
|
140
|
+
* @param targetName - Target entity name
|
|
141
|
+
* @param maxDepth - Maximum path length
|
|
142
|
+
* @returns List of entity names in path, or null if no path
|
|
143
|
+
*/
|
|
144
|
+
findPath(sourceName: string, targetName: string, maxDepth?: number): string[] | null;
|
|
145
|
+
/**
|
|
146
|
+
* Mark a source as having been extracted
|
|
147
|
+
*/
|
|
148
|
+
markExtracted(sourceHash: string, entityCount: number, relCount: number): void;
|
|
149
|
+
/**
|
|
150
|
+
* Check if a source has been extracted
|
|
151
|
+
*/
|
|
152
|
+
isExtracted(sourceHash: string): boolean;
|
|
153
|
+
countEntities(): number;
|
|
154
|
+
countRelationships(): number;
|
|
155
|
+
countExtractions(): number;
|
|
156
|
+
getEntityTypes(): Record<string, number>;
|
|
157
|
+
getStats(): GraphStats;
|
|
158
|
+
/**
|
|
159
|
+
* Clear all graph data
|
|
160
|
+
*/
|
|
161
|
+
clear(): void;
|
|
162
|
+
/**
|
|
163
|
+
* Close the database connection
|
|
164
|
+
*/
|
|
165
|
+
close(): void;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Store an extraction result in the graph.
|
|
169
|
+
*
|
|
170
|
+
* @param store - GraphStore instance
|
|
171
|
+
* @param result - ExtractionResult from GraphExtractor
|
|
172
|
+
* @param sourceHash - Source MCard hash
|
|
173
|
+
* @returns Tuple of [entityCount, relationshipCount] stored
|
|
174
|
+
*/
|
|
175
|
+
export declare function storeExtractionResult(store: GraphStore, result: ExtractionResult, sourceHash: string): [number, number];
|
|
176
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../src/rag/graph/store.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,EAAE,MAAM,EAAgB,gBAAgB,EAAoC,MAAM,aAAa,CAAC;AAYvG,MAAM,WAAW,YAAY;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,kBAAkB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CA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