@danielsimonjr/memory-mcp 0.48.0 → 9.8.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 +22 -0
- package/README.md +2000 -194
- package/dist/__tests__/file-path.test.js +7 -11
- package/dist/__tests__/knowledge-graph.test.js +3 -8
- package/dist/core/EntityManager.d.ts +266 -0
- package/dist/core/EntityManager.d.ts.map +1 -0
- package/dist/core/EntityManager.js +85 -133
- package/dist/core/GraphEventEmitter.d.ts +202 -0
- package/dist/core/GraphEventEmitter.d.ts.map +1 -0
- package/dist/core/GraphEventEmitter.js +346 -0
- package/dist/core/GraphStorage.d.ts +395 -0
- package/dist/core/GraphStorage.d.ts.map +1 -0
- package/dist/core/GraphStorage.js +643 -31
- package/dist/core/GraphTraversal.d.ts +141 -0
- package/dist/core/GraphTraversal.d.ts.map +1 -0
- package/dist/core/GraphTraversal.js +573 -0
- package/dist/core/HierarchyManager.d.ts +111 -0
- package/dist/core/HierarchyManager.d.ts.map +1 -0
- package/dist/{features → core}/HierarchyManager.js +14 -9
- package/dist/core/ManagerContext.d.ts +72 -0
- package/dist/core/ManagerContext.d.ts.map +1 -0
- package/dist/core/ManagerContext.js +118 -0
- package/dist/core/ObservationManager.d.ts +85 -0
- package/dist/core/ObservationManager.d.ts.map +1 -0
- package/dist/core/ObservationManager.js +51 -57
- package/dist/core/RelationManager.d.ts +131 -0
- package/dist/core/RelationManager.d.ts.map +1 -0
- package/dist/core/RelationManager.js +31 -7
- package/dist/core/SQLiteStorage.d.ts +354 -0
- package/dist/core/SQLiteStorage.d.ts.map +1 -0
- package/dist/core/SQLiteStorage.js +917 -0
- package/dist/core/StorageFactory.d.ts +45 -0
- package/dist/core/StorageFactory.d.ts.map +1 -0
- package/dist/core/StorageFactory.js +64 -0
- package/dist/core/TransactionManager.d.ts +464 -0
- package/dist/core/TransactionManager.d.ts.map +1 -0
- package/dist/core/TransactionManager.js +490 -13
- package/dist/core/index.d.ts +17 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +12 -2
- package/dist/features/AnalyticsManager.d.ts +44 -0
- package/dist/features/AnalyticsManager.d.ts.map +1 -0
- package/dist/features/AnalyticsManager.js +3 -2
- package/dist/features/ArchiveManager.d.ts +133 -0
- package/dist/features/ArchiveManager.d.ts.map +1 -0
- package/dist/features/ArchiveManager.js +221 -14
- package/dist/features/CompressionManager.d.ts +117 -0
- package/dist/features/CompressionManager.d.ts.map +1 -0
- package/dist/features/CompressionManager.js +189 -20
- package/dist/features/IOManager.d.ts +225 -0
- package/dist/features/IOManager.d.ts.map +1 -0
- package/dist/features/IOManager.js +1041 -0
- package/dist/features/StreamingExporter.d.ts +123 -0
- package/dist/features/StreamingExporter.d.ts.map +1 -0
- package/dist/features/StreamingExporter.js +203 -0
- package/dist/features/TagManager.d.ts +147 -0
- package/dist/features/TagManager.d.ts.map +1 -0
- package/dist/features/index.d.ts +12 -0
- package/dist/features/index.d.ts.map +1 -0
- package/dist/features/index.js +5 -6
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -10
- package/dist/memory.jsonl +1 -26
- package/dist/search/BasicSearch.d.ts +51 -0
- package/dist/search/BasicSearch.d.ts.map +1 -0
- package/dist/search/BasicSearch.js +9 -3
- package/dist/search/BooleanSearch.d.ts +98 -0
- package/dist/search/BooleanSearch.d.ts.map +1 -0
- package/dist/search/BooleanSearch.js +156 -9
- package/dist/search/EmbeddingService.d.ts +178 -0
- package/dist/search/EmbeddingService.d.ts.map +1 -0
- package/dist/search/EmbeddingService.js +358 -0
- package/dist/search/FuzzySearch.d.ts +118 -0
- package/dist/search/FuzzySearch.d.ts.map +1 -0
- package/dist/search/FuzzySearch.js +241 -25
- package/dist/search/QueryCostEstimator.d.ts +111 -0
- package/dist/search/QueryCostEstimator.d.ts.map +1 -0
- package/dist/search/QueryCostEstimator.js +355 -0
- package/dist/search/RankedSearch.d.ts +71 -0
- package/dist/search/RankedSearch.d.ts.map +1 -0
- package/dist/search/RankedSearch.js +54 -6
- package/dist/search/SavedSearchManager.d.ts +79 -0
- package/dist/search/SavedSearchManager.d.ts.map +1 -0
- package/dist/search/SearchFilterChain.d.ts +120 -0
- package/dist/search/SearchFilterChain.d.ts.map +1 -0
- package/dist/search/SearchFilterChain.js +2 -4
- package/dist/search/SearchManager.d.ts +326 -0
- package/dist/search/SearchManager.d.ts.map +1 -0
- package/dist/search/SearchManager.js +148 -0
- package/dist/search/SearchSuggestions.d.ts +27 -0
- package/dist/search/SearchSuggestions.d.ts.map +1 -0
- package/dist/search/SearchSuggestions.js +1 -1
- package/dist/search/SemanticSearch.d.ts +149 -0
- package/dist/search/SemanticSearch.d.ts.map +1 -0
- package/dist/search/SemanticSearch.js +323 -0
- package/dist/search/TFIDFEventSync.d.ts +85 -0
- package/dist/search/TFIDFEventSync.d.ts.map +1 -0
- package/dist/search/TFIDFEventSync.js +133 -0
- package/dist/search/TFIDFIndexManager.d.ts +151 -0
- package/dist/search/TFIDFIndexManager.d.ts.map +1 -0
- package/dist/search/TFIDFIndexManager.js +232 -17
- package/dist/search/VectorStore.d.ts +235 -0
- package/dist/search/VectorStore.d.ts.map +1 -0
- package/dist/search/VectorStore.js +311 -0
- package/dist/search/index.d.ts +21 -0
- package/dist/search/index.d.ts.map +1 -0
- package/dist/search/index.js +12 -0
- package/dist/server/MCPServer.d.ts +21 -0
- package/dist/server/MCPServer.d.ts.map +1 -0
- package/dist/server/MCPServer.js +4 -4
- package/dist/server/responseCompressor.d.ts +94 -0
- package/dist/server/responseCompressor.d.ts.map +1 -0
- package/dist/server/responseCompressor.js +127 -0
- package/dist/server/toolDefinitions.d.ts +27 -0
- package/dist/server/toolDefinitions.d.ts.map +1 -0
- package/dist/server/toolDefinitions.js +188 -17
- package/dist/server/toolHandlers.d.ts +41 -0
- package/dist/server/toolHandlers.d.ts.map +1 -0
- package/dist/server/toolHandlers.js +467 -75
- package/dist/types/index.d.ts +13 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +1 -1
- package/dist/types/types.d.ts +1654 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/types.js +9 -0
- package/dist/utils/compressedCache.d.ts +192 -0
- package/dist/utils/compressedCache.d.ts.map +1 -0
- package/dist/utils/compressedCache.js +309 -0
- package/dist/utils/compressionUtil.d.ts +214 -0
- package/dist/utils/compressionUtil.d.ts.map +1 -0
- package/dist/utils/compressionUtil.js +247 -0
- package/dist/utils/constants.d.ts +245 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +124 -0
- package/dist/utils/entityUtils.d.ts +321 -0
- package/dist/utils/entityUtils.d.ts.map +1 -0
- package/dist/utils/entityUtils.js +434 -4
- package/dist/utils/errors.d.ts +95 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +24 -0
- package/dist/utils/formatters.d.ts +145 -0
- package/dist/utils/formatters.d.ts.map +1 -0
- package/dist/utils/{paginationUtils.js → formatters.js} +54 -3
- package/dist/utils/index.d.ts +23 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +69 -31
- package/dist/utils/indexes.d.ts +270 -0
- package/dist/utils/indexes.d.ts.map +1 -0
- package/dist/utils/indexes.js +526 -0
- package/dist/utils/logger.d.ts +24 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/operationUtils.d.ts +124 -0
- package/dist/utils/operationUtils.d.ts.map +1 -0
- package/dist/utils/operationUtils.js +175 -0
- package/dist/utils/parallelUtils.d.ts +72 -0
- package/dist/utils/parallelUtils.d.ts.map +1 -0
- package/dist/utils/parallelUtils.js +169 -0
- package/dist/utils/schemas.d.ts +374 -0
- package/dist/utils/schemas.d.ts.map +1 -0
- package/dist/utils/schemas.js +302 -2
- package/dist/utils/searchAlgorithms.d.ts +99 -0
- package/dist/utils/searchAlgorithms.d.ts.map +1 -0
- package/dist/utils/searchAlgorithms.js +167 -0
- package/dist/utils/searchCache.d.ts +108 -0
- package/dist/utils/searchCache.d.ts.map +1 -0
- package/dist/utils/taskScheduler.d.ts +290 -0
- package/dist/utils/taskScheduler.d.ts.map +1 -0
- package/dist/utils/taskScheduler.js +466 -0
- package/dist/workers/index.d.ts +12 -0
- package/dist/workers/index.d.ts.map +1 -0
- package/dist/workers/index.js +9 -0
- package/dist/workers/levenshteinWorker.d.ts +60 -0
- package/dist/workers/levenshteinWorker.d.ts.map +1 -0
- package/dist/workers/levenshteinWorker.js +98 -0
- package/package.json +17 -4
- package/dist/__tests__/edge-cases/edge-cases.test.js +0 -406
- package/dist/__tests__/integration/workflows.test.js +0 -449
- package/dist/__tests__/performance/benchmarks.test.js +0 -413
- package/dist/__tests__/unit/core/EntityManager.test.js +0 -334
- package/dist/__tests__/unit/core/GraphStorage.test.js +0 -205
- package/dist/__tests__/unit/core/RelationManager.test.js +0 -274
- package/dist/__tests__/unit/features/CompressionManager.test.js +0 -350
- package/dist/__tests__/unit/search/BasicSearch.test.js +0 -311
- package/dist/__tests__/unit/search/BooleanSearch.test.js +0 -432
- package/dist/__tests__/unit/search/FuzzySearch.test.js +0 -448
- package/dist/__tests__/unit/search/RankedSearch.test.js +0 -379
- package/dist/__tests__/unit/utils/levenshtein.test.js +0 -77
- package/dist/core/KnowledgeGraphManager.js +0 -423
- package/dist/features/BackupManager.js +0 -311
- package/dist/features/ExportManager.js +0 -305
- package/dist/features/ImportExportManager.js +0 -50
- package/dist/features/ImportManager.js +0 -328
- package/dist/memory-saved-searches.jsonl +0 -0
- package/dist/memory-tag-aliases.jsonl +0 -0
- package/dist/types/analytics.types.js +0 -6
- package/dist/types/entity.types.js +0 -7
- package/dist/types/import-export.types.js +0 -7
- package/dist/types/search.types.js +0 -7
- package/dist/types/tag.types.js +0 -6
- package/dist/utils/dateUtils.js +0 -89
- package/dist/utils/filterUtils.js +0 -155
- package/dist/utils/levenshtein.js +0 -62
- package/dist/utils/pathUtils.js +0 -115
- package/dist/utils/responseFormatter.js +0 -55
- package/dist/utils/tagUtils.js +0 -107
- package/dist/utils/tfidf.js +0 -90
- package/dist/utils/validationHelper.js +0 -99
- package/dist/utils/validationUtils.js +0 -109
|
@@ -0,0 +1,1654 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* Consolidated type definitions for the Memory MCP Server.
|
|
5
|
+
* Combines entity, relation, search, analytics, import/export, tag, and storage types.
|
|
6
|
+
*
|
|
7
|
+
* @module types
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Represents an entity in the knowledge graph.
|
|
11
|
+
*
|
|
12
|
+
* Entities are the primary nodes in the graph, containing:
|
|
13
|
+
* - Identity (name, type)
|
|
14
|
+
* - Content (observations)
|
|
15
|
+
* - Metadata (timestamps, tags, importance)
|
|
16
|
+
* - Hierarchy (optional parent reference)
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* const entity: Entity = {
|
|
21
|
+
* name: "Alice",
|
|
22
|
+
* entityType: "person",
|
|
23
|
+
* observations: ["Works at TechCorp", "Loves TypeScript"],
|
|
24
|
+
* createdAt: "2024-01-01T00:00:00Z",
|
|
25
|
+
* lastModified: "2024-01-02T00:00:00Z",
|
|
26
|
+
* tags: ["employee", "developer"],
|
|
27
|
+
* importance: 8,
|
|
28
|
+
* parentId: "TechCorp"
|
|
29
|
+
* };
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export interface Entity {
|
|
33
|
+
/** Unique name identifying the entity */
|
|
34
|
+
name: string;
|
|
35
|
+
/** Type/category of the entity (e.g., "person", "project", "concept") */
|
|
36
|
+
entityType: string;
|
|
37
|
+
/** Array of observation strings describing facts about the entity */
|
|
38
|
+
observations: string[];
|
|
39
|
+
/** ISO 8601 timestamp when entity was created */
|
|
40
|
+
createdAt?: string;
|
|
41
|
+
/** ISO 8601 timestamp when entity was last modified */
|
|
42
|
+
lastModified?: string;
|
|
43
|
+
/** Array of lowercase tags for categorization */
|
|
44
|
+
tags?: string[];
|
|
45
|
+
/** Importance level from 0 (low) to 10 (high) */
|
|
46
|
+
importance?: number;
|
|
47
|
+
/** Optional parent entity name for hierarchical nesting */
|
|
48
|
+
parentId?: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Represents a directed relation between two entities.
|
|
52
|
+
*
|
|
53
|
+
* Relations form the edges of the knowledge graph, connecting entities
|
|
54
|
+
* with semantic relationships.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const relation: Relation = {
|
|
59
|
+
* from: "Alice",
|
|
60
|
+
* to: "TechCorp",
|
|
61
|
+
* relationType: "works_at",
|
|
62
|
+
* createdAt: "2024-01-01T00:00:00Z",
|
|
63
|
+
* lastModified: "2024-01-01T00:00:00Z"
|
|
64
|
+
* };
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export interface Relation {
|
|
68
|
+
/** Source entity name */
|
|
69
|
+
from: string;
|
|
70
|
+
/** Target entity name */
|
|
71
|
+
to: string;
|
|
72
|
+
/** Type of relationship (should be in active voice, e.g., "works_at", "manages") */
|
|
73
|
+
relationType: string;
|
|
74
|
+
/** ISO 8601 timestamp when relation was created */
|
|
75
|
+
createdAt?: string;
|
|
76
|
+
/** ISO 8601 timestamp when relation was last modified */
|
|
77
|
+
lastModified?: string;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Represents the complete knowledge graph structure.
|
|
81
|
+
*
|
|
82
|
+
* The knowledge graph consists of entities (nodes) and relations (edges),
|
|
83
|
+
* forming a semantic network of interconnected information.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* const graph: KnowledgeGraph = {
|
|
88
|
+
* entities: [
|
|
89
|
+
* { name: "Alice", entityType: "person", observations: ["Developer"] },
|
|
90
|
+
* { name: "TechCorp", entityType: "company", observations: ["Tech company"] }
|
|
91
|
+
* ],
|
|
92
|
+
* relations: [
|
|
93
|
+
* { from: "Alice", to: "TechCorp", relationType: "works_at" }
|
|
94
|
+
* ]
|
|
95
|
+
* };
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export interface KnowledgeGraph {
|
|
99
|
+
/** Array of all entities in the graph */
|
|
100
|
+
entities: Entity[];
|
|
101
|
+
/** Array of all relations between entities */
|
|
102
|
+
relations: Relation[];
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Read-only version of KnowledgeGraph for safe cache access.
|
|
106
|
+
* Prevents accidental mutation of cached data.
|
|
107
|
+
*/
|
|
108
|
+
export type ReadonlyKnowledgeGraph = {
|
|
109
|
+
readonly entities: readonly Entity[];
|
|
110
|
+
readonly relations: readonly Relation[];
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Phase 4 Sprint 3: Cache key for fuzzy search results.
|
|
114
|
+
*
|
|
115
|
+
* Used to uniquely identify a fuzzy search query for caching purposes.
|
|
116
|
+
*/
|
|
117
|
+
export interface FuzzyCacheKey {
|
|
118
|
+
/** The search query */
|
|
119
|
+
query: string;
|
|
120
|
+
/** Similarity threshold */
|
|
121
|
+
threshold: number;
|
|
122
|
+
/** Stringified tags filter (or empty if no tags) */
|
|
123
|
+
tagsKey: string;
|
|
124
|
+
/** Min importance filter */
|
|
125
|
+
minImportance?: number;
|
|
126
|
+
/** Max importance filter */
|
|
127
|
+
maxImportance?: number;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Phase 4 Sprint 4: Cache entry for Boolean search results.
|
|
131
|
+
*
|
|
132
|
+
* Stores both the parsed AST and query results for caching.
|
|
133
|
+
*/
|
|
134
|
+
export interface BooleanCacheEntry {
|
|
135
|
+
/** Stringified AST for the query */
|
|
136
|
+
astKey: string;
|
|
137
|
+
/** Hash of entity names in the graph (for invalidation) */
|
|
138
|
+
entityHash: string;
|
|
139
|
+
/** Cached search results */
|
|
140
|
+
entityNames: string[];
|
|
141
|
+
/** Timestamp when cached */
|
|
142
|
+
timestamp: number;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Phase 4 Sprint 5: Cache entry for paginated search results.
|
|
146
|
+
*
|
|
147
|
+
* Stores paginated results with their query parameters.
|
|
148
|
+
*/
|
|
149
|
+
export interface PaginatedCacheEntry {
|
|
150
|
+
/** The query key (search query + filters) */
|
|
151
|
+
queryKey: string;
|
|
152
|
+
/** Total count of matching entities (before pagination) */
|
|
153
|
+
totalCount: number;
|
|
154
|
+
/** Cached paginated entity names */
|
|
155
|
+
entityNames: string[];
|
|
156
|
+
/** Offset used for this cache entry */
|
|
157
|
+
offset: number;
|
|
158
|
+
/** Limit used for this cache entry */
|
|
159
|
+
limit: number;
|
|
160
|
+
/** Timestamp when cached */
|
|
161
|
+
timestamp: number;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Phase 4 Sprint 2: Represents a pre-tokenized entity for search optimization.
|
|
165
|
+
*
|
|
166
|
+
* Used by RankedSearch fallback token cache to avoid repeated tokenization.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* const tokenized: TokenizedEntity = {
|
|
171
|
+
* entity: { name: "Alice", entityType: "person", observations: ["Developer"] },
|
|
172
|
+
* tokens: ["alice", "person", "developer"],
|
|
173
|
+
* tokenSet: new Set(["alice", "person", "developer"])
|
|
174
|
+
* };
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
export interface TokenizedEntity {
|
|
178
|
+
/** The original entity */
|
|
179
|
+
entity: Entity;
|
|
180
|
+
/** Array of tokens from the entity text */
|
|
181
|
+
tokens: string[];
|
|
182
|
+
/** Set of unique tokens for fast lookup */
|
|
183
|
+
tokenSet: Set<string>;
|
|
184
|
+
/** Combined text from entity fields (for TF calculation) */
|
|
185
|
+
text: string;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Represents a search result with relevance scoring and match details.
|
|
189
|
+
*
|
|
190
|
+
* Used by ranked search to return entities sorted by relevance
|
|
191
|
+
* with information about which fields matched the query.
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* const result: SearchResult = {
|
|
196
|
+
* entity: { name: "Alice", entityType: "person", observations: ["Developer"] },
|
|
197
|
+
* score: 0.85,
|
|
198
|
+
* matchedFields: {
|
|
199
|
+
* name: true,
|
|
200
|
+
* observations: ["Developer"]
|
|
201
|
+
* }
|
|
202
|
+
* };
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
export interface SearchResult {
|
|
206
|
+
/** The entity that matched the search */
|
|
207
|
+
entity: Entity;
|
|
208
|
+
/** Relevance score (0.0 to 1.0, higher is more relevant) */
|
|
209
|
+
score: number;
|
|
210
|
+
/** Details about which fields matched the search query */
|
|
211
|
+
matchedFields: {
|
|
212
|
+
/** True if entity name matched */
|
|
213
|
+
name?: boolean;
|
|
214
|
+
/** True if entity type matched */
|
|
215
|
+
entityType?: boolean;
|
|
216
|
+
/** Array of observations that matched the query */
|
|
217
|
+
observations?: string[];
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Represents a saved search query that can be executed repeatedly.
|
|
222
|
+
*
|
|
223
|
+
* Saved searches store frequently used queries with their filters,
|
|
224
|
+
* tracking usage statistics for analytics.
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```typescript
|
|
228
|
+
* const savedSearch: SavedSearch = {
|
|
229
|
+
* name: "high-priority-developers",
|
|
230
|
+
* description: "Find all high-priority developer entities",
|
|
231
|
+
* query: "developer",
|
|
232
|
+
* tags: ["employee"],
|
|
233
|
+
* minImportance: 7,
|
|
234
|
+
* createdAt: "2024-01-01T00:00:00Z",
|
|
235
|
+
* lastUsed: "2024-01-15T00:00:00Z",
|
|
236
|
+
* useCount: 42
|
|
237
|
+
* };
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
export interface SavedSearch {
|
|
241
|
+
/** Unique name for the saved search */
|
|
242
|
+
name: string;
|
|
243
|
+
/** Optional description of what this search does */
|
|
244
|
+
description?: string;
|
|
245
|
+
/** The search query string */
|
|
246
|
+
query: string;
|
|
247
|
+
/** Optional tags to filter by */
|
|
248
|
+
tags?: string[];
|
|
249
|
+
/** Optional minimum importance level (0-10) */
|
|
250
|
+
minImportance?: number;
|
|
251
|
+
/** Optional maximum importance level (0-10) */
|
|
252
|
+
maxImportance?: number;
|
|
253
|
+
/** Optional entity type to filter by */
|
|
254
|
+
entityType?: string;
|
|
255
|
+
/** ISO 8601 timestamp when search was created */
|
|
256
|
+
createdAt: string;
|
|
257
|
+
/** ISO 8601 timestamp when search was last executed */
|
|
258
|
+
lastUsed?: string;
|
|
259
|
+
/** Number of times this search has been executed */
|
|
260
|
+
useCount: number;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Abstract Syntax Tree node types for boolean search queries.
|
|
264
|
+
*
|
|
265
|
+
* Supports AND, OR, NOT operators and field-specific searches.
|
|
266
|
+
* Used by the boolean query parser to build and evaluate complex queries.
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```typescript
|
|
270
|
+
* // Query: "name:Alice AND (type:person OR observation:developer)"
|
|
271
|
+
* const ast: BooleanQueryNode = {
|
|
272
|
+
* type: 'AND',
|
|
273
|
+
* children: [
|
|
274
|
+
* { type: 'TERM', field: 'name', value: 'alice' },
|
|
275
|
+
* {
|
|
276
|
+
* type: 'OR',
|
|
277
|
+
* children: [
|
|
278
|
+
* { type: 'TERM', field: 'type', value: 'person' },
|
|
279
|
+
* { type: 'TERM', field: 'observation', value: 'developer' }
|
|
280
|
+
* ]
|
|
281
|
+
* }
|
|
282
|
+
* ]
|
|
283
|
+
* };
|
|
284
|
+
* ```
|
|
285
|
+
*/
|
|
286
|
+
export type BooleanQueryNode = {
|
|
287
|
+
type: 'AND';
|
|
288
|
+
children: BooleanQueryNode[];
|
|
289
|
+
} | {
|
|
290
|
+
type: 'OR';
|
|
291
|
+
children: BooleanQueryNode[];
|
|
292
|
+
} | {
|
|
293
|
+
type: 'NOT';
|
|
294
|
+
child: BooleanQueryNode;
|
|
295
|
+
} | {
|
|
296
|
+
type: 'TERM';
|
|
297
|
+
field?: string;
|
|
298
|
+
value: string;
|
|
299
|
+
};
|
|
300
|
+
/**
|
|
301
|
+
* Document vector for TF-IDF index.
|
|
302
|
+
*
|
|
303
|
+
* Stores pre-calculated term frequencies for a single entity document.
|
|
304
|
+
* Used to speed up ranked search by avoiding recalculation.
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* ```typescript
|
|
308
|
+
* const vector: DocumentVector = {
|
|
309
|
+
* entityName: "Alice",
|
|
310
|
+
* terms: { "developer": 2, "python": 1, "senior": 1 },
|
|
311
|
+
* documentText: "Alice is a senior developer who codes in Python"
|
|
312
|
+
* };
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
315
|
+
export interface DocumentVector {
|
|
316
|
+
/** Entity name this vector represents */
|
|
317
|
+
entityName: string;
|
|
318
|
+
/** Map of term to frequency in this document */
|
|
319
|
+
terms: Record<string, number>;
|
|
320
|
+
/** Original document text (for cache invalidation) */
|
|
321
|
+
documentText: string;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Pre-calculated TF-IDF index for fast ranked search.
|
|
325
|
+
*
|
|
326
|
+
* Stores document vectors and inverse document frequencies
|
|
327
|
+
* to avoid recalculating TF-IDF scores on every search.
|
|
328
|
+
*
|
|
329
|
+
* @example
|
|
330
|
+
* ```typescript
|
|
331
|
+
* const index: TFIDFIndex = {
|
|
332
|
+
* version: "1.0",
|
|
333
|
+
* lastUpdated: "2024-01-15T00:00:00Z",
|
|
334
|
+
* documents: new Map([
|
|
335
|
+
* ["Alice", { entityName: "Alice", terms: {...}, documentText: "..." }]
|
|
336
|
+
* ]),
|
|
337
|
+
* idf: new Map([
|
|
338
|
+
* ["developer", 0.693],
|
|
339
|
+
* ["python", 1.386]
|
|
340
|
+
* ])
|
|
341
|
+
* };
|
|
342
|
+
* ```
|
|
343
|
+
*/
|
|
344
|
+
export interface TFIDFIndex {
|
|
345
|
+
/** Index format version */
|
|
346
|
+
version: string;
|
|
347
|
+
/** ISO 8601 timestamp of last index update */
|
|
348
|
+
lastUpdated: string;
|
|
349
|
+
/** Document vectors for all entities */
|
|
350
|
+
documents: Map<string, DocumentVector>;
|
|
351
|
+
/** Inverse document frequency for all terms */
|
|
352
|
+
idf: Map<string, number>;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Comprehensive statistics about the knowledge graph.
|
|
356
|
+
*
|
|
357
|
+
* Provides counts, type distributions, and temporal information
|
|
358
|
+
* about entities and relations in the graph.
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* ```typescript
|
|
362
|
+
* const stats: GraphStats = {
|
|
363
|
+
* totalEntities: 150,
|
|
364
|
+
* totalRelations: 320,
|
|
365
|
+
* entityTypesCounts: { person: 50, project: 30, concept: 70 },
|
|
366
|
+
* relationTypesCounts: { works_on: 100, manages: 20, related_to: 200 },
|
|
367
|
+
* oldestEntity: { name: "Alice", date: "2024-01-01T00:00:00Z" },
|
|
368
|
+
* newestEntity: { name: "Bob", date: "2024-12-31T23:59:59Z" },
|
|
369
|
+
* entityDateRange: { earliest: "2024-01-01T00:00:00Z", latest: "2024-12-31T23:59:59Z" },
|
|
370
|
+
* cacheStats: { totalEntries: 150, compressedEntries: 50, uncompressedEntries: 100, estimatedMemorySaved: 250000 }
|
|
371
|
+
* };
|
|
372
|
+
* ```
|
|
373
|
+
*/
|
|
374
|
+
export interface GraphStats {
|
|
375
|
+
/** Total number of entities in the graph */
|
|
376
|
+
totalEntities: number;
|
|
377
|
+
/** Total number of relations in the graph */
|
|
378
|
+
totalRelations: number;
|
|
379
|
+
/** Count of entities by type */
|
|
380
|
+
entityTypesCounts: Record<string, number>;
|
|
381
|
+
/** Count of relations by type */
|
|
382
|
+
relationTypesCounts: Record<string, number>;
|
|
383
|
+
/** Information about the oldest entity (by createdAt) */
|
|
384
|
+
oldestEntity?: {
|
|
385
|
+
name: string;
|
|
386
|
+
date: string;
|
|
387
|
+
};
|
|
388
|
+
/** Information about the newest entity (by createdAt) */
|
|
389
|
+
newestEntity?: {
|
|
390
|
+
name: string;
|
|
391
|
+
date: string;
|
|
392
|
+
};
|
|
393
|
+
/** Information about the oldest relation (by createdAt) */
|
|
394
|
+
oldestRelation?: {
|
|
395
|
+
from: string;
|
|
396
|
+
to: string;
|
|
397
|
+
relationType: string;
|
|
398
|
+
date: string;
|
|
399
|
+
};
|
|
400
|
+
/** Information about the newest relation (by createdAt) */
|
|
401
|
+
newestRelation?: {
|
|
402
|
+
from: string;
|
|
403
|
+
to: string;
|
|
404
|
+
relationType: string;
|
|
405
|
+
date: string;
|
|
406
|
+
};
|
|
407
|
+
/** Date range of all entities */
|
|
408
|
+
entityDateRange?: {
|
|
409
|
+
earliest: string;
|
|
410
|
+
latest: string;
|
|
411
|
+
};
|
|
412
|
+
/** Date range of all relations */
|
|
413
|
+
relationDateRange?: {
|
|
414
|
+
earliest: string;
|
|
415
|
+
latest: string;
|
|
416
|
+
};
|
|
417
|
+
/** Cache compression statistics (optional, only when CompressedCache is used) */
|
|
418
|
+
cacheStats?: CacheCompressionStats;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Complete validation report for the knowledge graph.
|
|
422
|
+
*
|
|
423
|
+
* Contains errors (critical issues) and warnings (non-critical issues)
|
|
424
|
+
* along with summary statistics.
|
|
425
|
+
*
|
|
426
|
+
* @example
|
|
427
|
+
* ```typescript
|
|
428
|
+
* const report: ValidationReport = {
|
|
429
|
+
* isValid: false,
|
|
430
|
+
* issues: [
|
|
431
|
+
* { type: 'orphaned_relation', message: 'Relation references non-existent entity', details: {...} }
|
|
432
|
+
* ],
|
|
433
|
+
* warnings: [
|
|
434
|
+
* { type: 'isolated_entity', message: 'Entity has no relations', details: {...} }
|
|
435
|
+
* ],
|
|
436
|
+
* summary: {
|
|
437
|
+
* totalErrors: 1,
|
|
438
|
+
* totalWarnings: 1,
|
|
439
|
+
* orphanedRelationsCount: 1,
|
|
440
|
+
* entitiesWithoutRelationsCount: 1
|
|
441
|
+
* }
|
|
442
|
+
* };
|
|
443
|
+
* ```
|
|
444
|
+
*/
|
|
445
|
+
export interface ValidationReport {
|
|
446
|
+
/** True if graph has no issues (warnings are acceptable) */
|
|
447
|
+
isValid: boolean;
|
|
448
|
+
/** Array of critical issues found */
|
|
449
|
+
issues: ValidationIssue[];
|
|
450
|
+
/** Array of warnings (non-critical issues) */
|
|
451
|
+
warnings: ValidationWarning[];
|
|
452
|
+
/** Summary statistics of validation results */
|
|
453
|
+
summary: {
|
|
454
|
+
totalErrors: number;
|
|
455
|
+
totalWarnings: number;
|
|
456
|
+
orphanedRelationsCount: number;
|
|
457
|
+
entitiesWithoutRelationsCount: number;
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Represents a critical issue found during graph validation.
|
|
462
|
+
* Note: Named ValidationIssue to avoid collision with ValidationError class in utils/errors.ts
|
|
463
|
+
*/
|
|
464
|
+
export interface ValidationIssue {
|
|
465
|
+
/** Type of issue */
|
|
466
|
+
type: 'orphaned_relation' | 'duplicate_entity' | 'invalid_data';
|
|
467
|
+
/** Human-readable issue message */
|
|
468
|
+
message: string;
|
|
469
|
+
/** Additional details about the issue */
|
|
470
|
+
details?: Record<string, unknown>;
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Represents a non-critical warning found during graph validation.
|
|
474
|
+
*/
|
|
475
|
+
export interface ValidationWarning {
|
|
476
|
+
/** Type of warning */
|
|
477
|
+
type: 'isolated_entity' | 'empty_observations' | 'missing_metadata';
|
|
478
|
+
/** Human-readable warning message */
|
|
479
|
+
message: string;
|
|
480
|
+
/** Additional details about the warning */
|
|
481
|
+
details?: Record<string, unknown>;
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Export filter criteria for filtering graph exports by date, type, or tags.
|
|
485
|
+
*
|
|
486
|
+
* @example
|
|
487
|
+
* ```typescript
|
|
488
|
+
* const filter: ExportFilter = {
|
|
489
|
+
* startDate: '2024-01-01',
|
|
490
|
+
* endDate: '2024-12-31',
|
|
491
|
+
* entityType: 'Person',
|
|
492
|
+
* tags: ['important', 'reviewed']
|
|
493
|
+
* };
|
|
494
|
+
* ```
|
|
495
|
+
*/
|
|
496
|
+
export interface ExportFilter {
|
|
497
|
+
/** Start date for filtering (ISO 8601) */
|
|
498
|
+
startDate?: string;
|
|
499
|
+
/** End date for filtering (ISO 8601) */
|
|
500
|
+
endDate?: string;
|
|
501
|
+
/** Entity type filter */
|
|
502
|
+
entityType?: string;
|
|
503
|
+
/** Tags filter */
|
|
504
|
+
tags?: string[];
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Result summary from importing a knowledge graph.
|
|
508
|
+
*
|
|
509
|
+
* Provides detailed statistics about what was imported, skipped,
|
|
510
|
+
* updated, and any errors encountered.
|
|
511
|
+
*
|
|
512
|
+
* @example
|
|
513
|
+
* ```typescript
|
|
514
|
+
* const result: ImportResult = {
|
|
515
|
+
* entitiesAdded: 50,
|
|
516
|
+
* entitiesSkipped: 10,
|
|
517
|
+
* entitiesUpdated: 5,
|
|
518
|
+
* relationsAdded: 100,
|
|
519
|
+
* relationsSkipped: 20,
|
|
520
|
+
* errors: ["Invalid entity format on line 42"]
|
|
521
|
+
* };
|
|
522
|
+
* ```
|
|
523
|
+
*/
|
|
524
|
+
export interface ImportResult {
|
|
525
|
+
/** Number of new entities added */
|
|
526
|
+
entitiesAdded: number;
|
|
527
|
+
/** Number of entities skipped (duplicates or invalid) */
|
|
528
|
+
entitiesSkipped: number;
|
|
529
|
+
/** Number of existing entities updated */
|
|
530
|
+
entitiesUpdated: number;
|
|
531
|
+
/** Number of new relations added */
|
|
532
|
+
relationsAdded: number;
|
|
533
|
+
/** Number of relations skipped (duplicates or invalid) */
|
|
534
|
+
relationsSkipped: number;
|
|
535
|
+
/** Array of error messages encountered during import */
|
|
536
|
+
errors: string[];
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Result summary from graph compression operations.
|
|
540
|
+
*
|
|
541
|
+
* Provides statistics about deduplication, merging, and space savings
|
|
542
|
+
* achieved through compression.
|
|
543
|
+
*
|
|
544
|
+
* @example
|
|
545
|
+
* ```typescript
|
|
546
|
+
* const result: CompressionResult = {
|
|
547
|
+
* duplicatesFound: 15,
|
|
548
|
+
* entitiesMerged: 10,
|
|
549
|
+
* observationsCompressed: 25,
|
|
550
|
+
* relationsConsolidated: 30,
|
|
551
|
+
* spaceFreed: 5000,
|
|
552
|
+
* mergedEntities: [
|
|
553
|
+
* { kept: "Alice", merged: ["Alice_Smith", "A_Smith"] },
|
|
554
|
+
* { kept: "TechCorp", merged: ["Tech_Corp", "TechCorporation"] }
|
|
555
|
+
* ]
|
|
556
|
+
* };
|
|
557
|
+
* ```
|
|
558
|
+
*/
|
|
559
|
+
export interface CompressionResult {
|
|
560
|
+
/** Number of duplicate entities found */
|
|
561
|
+
duplicatesFound: number;
|
|
562
|
+
/** Number of entities merged into others */
|
|
563
|
+
entitiesMerged: number;
|
|
564
|
+
/** Number of observations compressed */
|
|
565
|
+
observationsCompressed: number;
|
|
566
|
+
/** Number of relations consolidated */
|
|
567
|
+
relationsConsolidated: number;
|
|
568
|
+
/** Approximate character count saved */
|
|
569
|
+
spaceFreed: number;
|
|
570
|
+
/** Details of which entities were merged */
|
|
571
|
+
mergedEntities: Array<{
|
|
572
|
+
kept: string;
|
|
573
|
+
merged: string[];
|
|
574
|
+
}>;
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Options for backup creation.
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* ```typescript
|
|
581
|
+
* const options: BackupOptions = {
|
|
582
|
+
* compress: true,
|
|
583
|
+
* description: 'Pre-migration backup'
|
|
584
|
+
* };
|
|
585
|
+
* ```
|
|
586
|
+
*/
|
|
587
|
+
export interface BackupOptions {
|
|
588
|
+
/** Whether to compress the backup with brotli (default: true) */
|
|
589
|
+
compress?: boolean;
|
|
590
|
+
/** Optional description for the backup */
|
|
591
|
+
description?: string;
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Result of a backup creation operation.
|
|
595
|
+
*
|
|
596
|
+
* Provides details about the created backup including compression statistics.
|
|
597
|
+
*
|
|
598
|
+
* @example
|
|
599
|
+
* ```typescript
|
|
600
|
+
* const result: BackupResult = {
|
|
601
|
+
* path: '/path/to/.backups/backup_2024-01-01.jsonl.br',
|
|
602
|
+
* timestamp: '2024-01-01T00:00:00.000Z',
|
|
603
|
+
* entityCount: 150,
|
|
604
|
+
* relationCount: 320,
|
|
605
|
+
* compressed: true,
|
|
606
|
+
* originalSize: 125000,
|
|
607
|
+
* compressedSize: 37500,
|
|
608
|
+
* compressionRatio: 0.3
|
|
609
|
+
* };
|
|
610
|
+
* ```
|
|
611
|
+
*/
|
|
612
|
+
export interface BackupResult {
|
|
613
|
+
/** Full path to the backup file */
|
|
614
|
+
path: string;
|
|
615
|
+
/** ISO 8601 timestamp when backup was created */
|
|
616
|
+
timestamp: string;
|
|
617
|
+
/** Number of entities in the backup */
|
|
618
|
+
entityCount: number;
|
|
619
|
+
/** Number of relations in the backup */
|
|
620
|
+
relationCount: number;
|
|
621
|
+
/** Whether the backup is compressed */
|
|
622
|
+
compressed: boolean;
|
|
623
|
+
/** Original size in bytes (before compression) */
|
|
624
|
+
originalSize: number;
|
|
625
|
+
/** Size after compression in bytes (same as original if not compressed) */
|
|
626
|
+
compressedSize: number;
|
|
627
|
+
/** Compression ratio (compressedSize / originalSize). Lower is better. */
|
|
628
|
+
compressionRatio: number;
|
|
629
|
+
/** Optional description for the backup */
|
|
630
|
+
description?: string;
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Result of a backup restoration operation.
|
|
634
|
+
*
|
|
635
|
+
* @example
|
|
636
|
+
* ```typescript
|
|
637
|
+
* const result: RestoreResult = {
|
|
638
|
+
* entityCount: 150,
|
|
639
|
+
* relationCount: 320,
|
|
640
|
+
* restoredFrom: '/path/to/.backups/backup_2024-01-01.jsonl.br',
|
|
641
|
+
* wasCompressed: true
|
|
642
|
+
* };
|
|
643
|
+
* ```
|
|
644
|
+
*/
|
|
645
|
+
export interface RestoreResult {
|
|
646
|
+
/** Number of entities restored */
|
|
647
|
+
entityCount: number;
|
|
648
|
+
/** Number of relations restored */
|
|
649
|
+
relationCount: number;
|
|
650
|
+
/** Path of the backup file that was restored */
|
|
651
|
+
restoredFrom: string;
|
|
652
|
+
/** Whether the backup was compressed */
|
|
653
|
+
wasCompressed: boolean;
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Extended backup metadata with compression information.
|
|
657
|
+
*
|
|
658
|
+
* Stored alongside backups for integrity verification and restoration.
|
|
659
|
+
*/
|
|
660
|
+
export interface BackupMetadataExtended {
|
|
661
|
+
/** Timestamp when backup was created (ISO 8601) */
|
|
662
|
+
timestamp: string;
|
|
663
|
+
/** Number of entities in the backup */
|
|
664
|
+
entityCount: number;
|
|
665
|
+
/** Number of relations in the backup */
|
|
666
|
+
relationCount: number;
|
|
667
|
+
/** File size in bytes (compressed size if compressed) */
|
|
668
|
+
fileSize: number;
|
|
669
|
+
/** Optional description/reason for backup */
|
|
670
|
+
description?: string;
|
|
671
|
+
/** Whether the backup is compressed */
|
|
672
|
+
compressed: boolean;
|
|
673
|
+
/** Original size before compression in bytes */
|
|
674
|
+
originalSize?: number;
|
|
675
|
+
/** Compression ratio achieved (compressedSize / originalSize) */
|
|
676
|
+
compressionRatio?: number;
|
|
677
|
+
/** Compression format used */
|
|
678
|
+
compressionFormat?: 'brotli' | 'none';
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Extended backup info with compression details.
|
|
682
|
+
*
|
|
683
|
+
* Used when listing backups to show compression statistics.
|
|
684
|
+
*/
|
|
685
|
+
export interface BackupInfoExtended {
|
|
686
|
+
/** Backup file name */
|
|
687
|
+
fileName: string;
|
|
688
|
+
/** Full path to backup file */
|
|
689
|
+
filePath: string;
|
|
690
|
+
/** Whether the backup is compressed */
|
|
691
|
+
compressed: boolean;
|
|
692
|
+
/** File size in bytes */
|
|
693
|
+
size: number;
|
|
694
|
+
/** Original size before compression (if available) */
|
|
695
|
+
originalSize?: number;
|
|
696
|
+
/** Compression ratio (if available) */
|
|
697
|
+
compressionRatio?: number;
|
|
698
|
+
/** Backup metadata */
|
|
699
|
+
metadata: BackupMetadataExtended;
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* Options for export operations with optional compression.
|
|
703
|
+
*
|
|
704
|
+
* @example
|
|
705
|
+
* ```typescript
|
|
706
|
+
* const options: ExportOptions = {
|
|
707
|
+
* filter: { entityType: 'person' },
|
|
708
|
+
* compress: true,
|
|
709
|
+
* compressionQuality: 11
|
|
710
|
+
* };
|
|
711
|
+
* ```
|
|
712
|
+
*/
|
|
713
|
+
export interface ExportOptions {
|
|
714
|
+
/** Optional filter criteria for the export */
|
|
715
|
+
filter?: ExportFilter;
|
|
716
|
+
/** Whether to compress the export with brotli (default: false, auto-enabled for >100KB) */
|
|
717
|
+
compress?: boolean;
|
|
718
|
+
/** Brotli quality level 0-11 (default: 6). Higher = better compression but slower. */
|
|
719
|
+
compressionQuality?: number;
|
|
720
|
+
/** Whether to use streaming mode for large exports */
|
|
721
|
+
streaming?: boolean;
|
|
722
|
+
/** Output file path for streaming mode */
|
|
723
|
+
outputPath?: string;
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* Result of an export operation with compression metadata.
|
|
727
|
+
*
|
|
728
|
+
* Provides the exported content along with compression statistics
|
|
729
|
+
* when compression is applied.
|
|
730
|
+
*
|
|
731
|
+
* @example
|
|
732
|
+
* ```typescript
|
|
733
|
+
* const result: ExportResult = {
|
|
734
|
+
* format: 'json',
|
|
735
|
+
* content: 'base64-encoded-data...',
|
|
736
|
+
* entityCount: 150,
|
|
737
|
+
* relationCount: 320,
|
|
738
|
+
* compressed: true,
|
|
739
|
+
* encoding: 'base64',
|
|
740
|
+
* originalSize: 125000,
|
|
741
|
+
* compressedSize: 37500,
|
|
742
|
+
* compressionRatio: 0.3
|
|
743
|
+
* };
|
|
744
|
+
* ```
|
|
745
|
+
*/
|
|
746
|
+
export interface ExportResult {
|
|
747
|
+
/** The export format used */
|
|
748
|
+
format: string;
|
|
749
|
+
/** The exported content (string if uncompressed, base64 if compressed) */
|
|
750
|
+
content: string;
|
|
751
|
+
/** Number of entities in the export */
|
|
752
|
+
entityCount: number;
|
|
753
|
+
/** Number of relations in the export */
|
|
754
|
+
relationCount: number;
|
|
755
|
+
/** Whether the content is compressed */
|
|
756
|
+
compressed: boolean;
|
|
757
|
+
/** Content encoding: 'utf-8' for plain text, 'base64' for compressed */
|
|
758
|
+
encoding: 'utf-8' | 'base64';
|
|
759
|
+
/** Original size in bytes before compression */
|
|
760
|
+
originalSize: number;
|
|
761
|
+
/** Size after compression in bytes (same as original if not compressed) */
|
|
762
|
+
compressedSize: number;
|
|
763
|
+
/** Compression ratio (compressedSize / originalSize). Lower is better. */
|
|
764
|
+
compressionRatio: number;
|
|
765
|
+
/** Whether the export was streamed to a file */
|
|
766
|
+
streamed?: boolean;
|
|
767
|
+
/** Output file path if streamed */
|
|
768
|
+
outputPath?: string;
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Extended archive result with compression information.
|
|
772
|
+
*
|
|
773
|
+
* Used when archiving entities to compressed storage.
|
|
774
|
+
*
|
|
775
|
+
* @example
|
|
776
|
+
* ```typescript
|
|
777
|
+
* const result: ArchiveResultExtended = {
|
|
778
|
+
* archived: 50,
|
|
779
|
+
* entityNames: ['Entity1', 'Entity2', ...],
|
|
780
|
+
* archivePath: '/path/to/.archives/archive_2024-01-01.jsonl.br',
|
|
781
|
+
* originalSize: 125000,
|
|
782
|
+
* compressedSize: 37500,
|
|
783
|
+
* compressionRatio: 0.3
|
|
784
|
+
* };
|
|
785
|
+
* ```
|
|
786
|
+
*/
|
|
787
|
+
export interface ArchiveResultExtended {
|
|
788
|
+
/** Number of entities archived */
|
|
789
|
+
archived: number;
|
|
790
|
+
/** Names of archived entities */
|
|
791
|
+
entityNames: string[];
|
|
792
|
+
/** Path to the archive file (if created) */
|
|
793
|
+
archivePath?: string;
|
|
794
|
+
/** Original size of archive data in bytes */
|
|
795
|
+
originalSize?: number;
|
|
796
|
+
/** Compressed size in bytes */
|
|
797
|
+
compressedSize?: number;
|
|
798
|
+
/** Compression ratio (compressedSize / originalSize). Lower is better. */
|
|
799
|
+
compressionRatio?: number;
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Cache compression statistics.
|
|
803
|
+
*
|
|
804
|
+
* Provides information about the compressed cache state for memory optimization.
|
|
805
|
+
*/
|
|
806
|
+
export interface CacheCompressionStats {
|
|
807
|
+
/** Total number of entries in the cache */
|
|
808
|
+
totalEntries: number;
|
|
809
|
+
/** Number of compressed entries */
|
|
810
|
+
compressedEntries: number;
|
|
811
|
+
/** Number of uncompressed (hot) entries */
|
|
812
|
+
uncompressedEntries: number;
|
|
813
|
+
/** Estimated memory saved by compression in bytes */
|
|
814
|
+
estimatedMemorySaved: number;
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Represents a tag alias (synonym) mapping.
|
|
818
|
+
*
|
|
819
|
+
* Tag aliases allow multiple tag names to map to a canonical tag,
|
|
820
|
+
* enabling synonym support and tag normalization.
|
|
821
|
+
*
|
|
822
|
+
* @example
|
|
823
|
+
* ```typescript
|
|
824
|
+
* const alias: TagAlias = {
|
|
825
|
+
* alias: "dev",
|
|
826
|
+
* canonical: "developer",
|
|
827
|
+
* description: "Short form of developer",
|
|
828
|
+
* createdAt: "2024-01-01T00:00:00Z"
|
|
829
|
+
* };
|
|
830
|
+
* ```
|
|
831
|
+
*/
|
|
832
|
+
export interface TagAlias {
|
|
833
|
+
/** The alias (synonym) tag name */
|
|
834
|
+
alias: string;
|
|
835
|
+
/** The canonical (official) tag name this maps to */
|
|
836
|
+
canonical: string;
|
|
837
|
+
/** Optional description of the alias relationship */
|
|
838
|
+
description?: string;
|
|
839
|
+
/** ISO 8601 timestamp when alias was created */
|
|
840
|
+
createdAt: string;
|
|
841
|
+
}
|
|
842
|
+
/**
|
|
843
|
+
* Pre-computed lowercase data for search optimization.
|
|
844
|
+
* Avoids repeated toLowerCase() calls during search operations.
|
|
845
|
+
*/
|
|
846
|
+
export interface LowercaseData {
|
|
847
|
+
/** Entity name in lowercase */
|
|
848
|
+
name: string;
|
|
849
|
+
/** Entity type in lowercase */
|
|
850
|
+
entityType: string;
|
|
851
|
+
/** Array of observations in lowercase */
|
|
852
|
+
observations: string[];
|
|
853
|
+
/** Array of tags in lowercase */
|
|
854
|
+
tags: string[];
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Storage configuration options.
|
|
858
|
+
*/
|
|
859
|
+
export interface StorageConfig {
|
|
860
|
+
/** Storage type: 'jsonl' or 'sqlite' */
|
|
861
|
+
type: 'jsonl' | 'sqlite';
|
|
862
|
+
/** Path to storage file */
|
|
863
|
+
path: string;
|
|
864
|
+
}
|
|
865
|
+
/**
|
|
866
|
+
* Interface for graph storage implementations.
|
|
867
|
+
*
|
|
868
|
+
* This abstraction allows for different storage backends:
|
|
869
|
+
* - JSONLStorage (current implementation)
|
|
870
|
+
* - SQLiteStorage (future implementation)
|
|
871
|
+
* - MemoryStorage (for testing)
|
|
872
|
+
*
|
|
873
|
+
* All implementations must maintain the same semantics for
|
|
874
|
+
* data persistence and retrieval.
|
|
875
|
+
*/
|
|
876
|
+
export interface IGraphStorage {
|
|
877
|
+
/**
|
|
878
|
+
* Load the knowledge graph from storage (read-only access).
|
|
879
|
+
*
|
|
880
|
+
* @returns Promise resolving to read-only knowledge graph reference
|
|
881
|
+
*/
|
|
882
|
+
loadGraph(): Promise<ReadonlyKnowledgeGraph>;
|
|
883
|
+
/**
|
|
884
|
+
* Get a mutable copy of the graph for write operations.
|
|
885
|
+
*
|
|
886
|
+
* @returns Promise resolving to mutable knowledge graph copy
|
|
887
|
+
*/
|
|
888
|
+
getGraphForMutation(): Promise<KnowledgeGraph>;
|
|
889
|
+
/**
|
|
890
|
+
* Ensure the storage is loaded/initialized.
|
|
891
|
+
*
|
|
892
|
+
* @returns Promise resolving when ready
|
|
893
|
+
*/
|
|
894
|
+
ensureLoaded(): Promise<void>;
|
|
895
|
+
/**
|
|
896
|
+
* Save the entire knowledge graph to storage.
|
|
897
|
+
*
|
|
898
|
+
* @param graph - The knowledge graph to save
|
|
899
|
+
* @returns Promise resolving when save is complete
|
|
900
|
+
*/
|
|
901
|
+
saveGraph(graph: KnowledgeGraph): Promise<void>;
|
|
902
|
+
/**
|
|
903
|
+
* Append a single entity to storage (O(1) write operation).
|
|
904
|
+
*
|
|
905
|
+
* @param entity - The entity to append
|
|
906
|
+
* @returns Promise resolving when append is complete
|
|
907
|
+
*/
|
|
908
|
+
appendEntity(entity: Entity): Promise<void>;
|
|
909
|
+
/**
|
|
910
|
+
* Append a single relation to storage (O(1) write operation).
|
|
911
|
+
*
|
|
912
|
+
* @param relation - The relation to append
|
|
913
|
+
* @returns Promise resolving when append is complete
|
|
914
|
+
*/
|
|
915
|
+
appendRelation(relation: Relation): Promise<void>;
|
|
916
|
+
/**
|
|
917
|
+
* Update an entity in storage.
|
|
918
|
+
*
|
|
919
|
+
* @param entityName - Name of the entity to update
|
|
920
|
+
* @param updates - Partial entity updates to apply
|
|
921
|
+
* @returns Promise resolving to true if found and updated
|
|
922
|
+
*/
|
|
923
|
+
updateEntity(entityName: string, updates: Partial<Entity>): Promise<boolean>;
|
|
924
|
+
/**
|
|
925
|
+
* Compact the storage by removing duplicates.
|
|
926
|
+
*
|
|
927
|
+
* @returns Promise resolving when compaction is complete
|
|
928
|
+
*/
|
|
929
|
+
compact(): Promise<void>;
|
|
930
|
+
/**
|
|
931
|
+
* Clear any in-memory cache.
|
|
932
|
+
*/
|
|
933
|
+
clearCache(): void;
|
|
934
|
+
/**
|
|
935
|
+
* Get an entity by name in O(1) time.
|
|
936
|
+
*
|
|
937
|
+
* @param name - Entity name to look up
|
|
938
|
+
* @returns Entity if found, undefined otherwise
|
|
939
|
+
*/
|
|
940
|
+
getEntityByName(name: string): Entity | undefined;
|
|
941
|
+
/**
|
|
942
|
+
* Check if an entity exists by name.
|
|
943
|
+
*
|
|
944
|
+
* @param name - Entity name to check
|
|
945
|
+
* @returns True if entity exists
|
|
946
|
+
*/
|
|
947
|
+
hasEntity(name: string): boolean;
|
|
948
|
+
/**
|
|
949
|
+
* Get all entities of a given type.
|
|
950
|
+
*
|
|
951
|
+
* @param entityType - Entity type to filter by
|
|
952
|
+
* @returns Array of entities with the given type
|
|
953
|
+
*/
|
|
954
|
+
getEntitiesByType(entityType: string): Entity[];
|
|
955
|
+
/**
|
|
956
|
+
* Get all unique entity types in the storage.
|
|
957
|
+
*
|
|
958
|
+
* @returns Array of unique entity types
|
|
959
|
+
*/
|
|
960
|
+
getEntityTypes(): string[];
|
|
961
|
+
/**
|
|
962
|
+
* Get pre-computed lowercase data for an entity.
|
|
963
|
+
*
|
|
964
|
+
* @param entityName - Entity name to get lowercase data for
|
|
965
|
+
* @returns LowercaseData if entity exists, undefined otherwise
|
|
966
|
+
*/
|
|
967
|
+
getLowercased(entityName: string): LowercaseData | undefined;
|
|
968
|
+
/**
|
|
969
|
+
* Get all relations where the entity is the source (outgoing relations) in O(1) time.
|
|
970
|
+
*
|
|
971
|
+
* @param entityName - Entity name to look up outgoing relations for
|
|
972
|
+
* @returns Array of relations where entity is the source
|
|
973
|
+
*/
|
|
974
|
+
getRelationsFrom(entityName: string): Relation[];
|
|
975
|
+
/**
|
|
976
|
+
* Get all relations where the entity is the target (incoming relations) in O(1) time.
|
|
977
|
+
*
|
|
978
|
+
* @param entityName - Entity name to look up incoming relations for
|
|
979
|
+
* @returns Array of relations where entity is the target
|
|
980
|
+
*/
|
|
981
|
+
getRelationsTo(entityName: string): Relation[];
|
|
982
|
+
/**
|
|
983
|
+
* Get all relations involving the entity (both incoming and outgoing) in O(1) time.
|
|
984
|
+
*
|
|
985
|
+
* @param entityName - Entity name to look up all relations for
|
|
986
|
+
* @returns Array of all relations involving the entity
|
|
987
|
+
*/
|
|
988
|
+
getRelationsFor(entityName: string): Relation[];
|
|
989
|
+
/**
|
|
990
|
+
* Check if an entity has any relations.
|
|
991
|
+
*
|
|
992
|
+
* @param entityName - Entity name to check
|
|
993
|
+
* @returns True if entity has any relations
|
|
994
|
+
*/
|
|
995
|
+
hasRelations(entityName: string): boolean;
|
|
996
|
+
/**
|
|
997
|
+
* Get the storage path/location.
|
|
998
|
+
*
|
|
999
|
+
* @returns The storage path
|
|
1000
|
+
*/
|
|
1001
|
+
getFilePath(): string;
|
|
1002
|
+
/**
|
|
1003
|
+
* Get the current pending appends count.
|
|
1004
|
+
*
|
|
1005
|
+
* @returns Number of pending appends since last compaction
|
|
1006
|
+
*/
|
|
1007
|
+
getPendingAppends(): number;
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* Phase 4 Sprint 6: Options for graph traversal algorithms.
|
|
1011
|
+
*/
|
|
1012
|
+
export interface TraversalOptions {
|
|
1013
|
+
/** Direction of traversal: 'outgoing' (default), 'incoming', or 'both' */
|
|
1014
|
+
direction?: 'outgoing' | 'incoming' | 'both';
|
|
1015
|
+
/** Maximum depth to traverse (default: Infinity) */
|
|
1016
|
+
maxDepth?: number;
|
|
1017
|
+
/** Optional filter for relation types to follow */
|
|
1018
|
+
relationTypes?: string[];
|
|
1019
|
+
/** Optional filter for entity types to include */
|
|
1020
|
+
entityTypes?: string[];
|
|
1021
|
+
}
|
|
1022
|
+
/**
|
|
1023
|
+
* Phase 4 Sprint 6: Result of a graph traversal operation.
|
|
1024
|
+
*/
|
|
1025
|
+
export interface TraversalResult {
|
|
1026
|
+
/** Nodes visited in traversal order */
|
|
1027
|
+
nodes: string[];
|
|
1028
|
+
/** Depth at which each node was found */
|
|
1029
|
+
depths: Map<string, number>;
|
|
1030
|
+
/** Parent node for each visited node (for path reconstruction) */
|
|
1031
|
+
parents: Map<string, string | null>;
|
|
1032
|
+
}
|
|
1033
|
+
/**
|
|
1034
|
+
* Phase 4 Sprint 7: Result of a path finding operation.
|
|
1035
|
+
*/
|
|
1036
|
+
export interface PathResult {
|
|
1037
|
+
/** Path from source to target as array of node names */
|
|
1038
|
+
path: string[];
|
|
1039
|
+
/** Total length of the path (number of hops or weighted distance) */
|
|
1040
|
+
length: number;
|
|
1041
|
+
/** Relations along the path */
|
|
1042
|
+
relations: Relation[];
|
|
1043
|
+
}
|
|
1044
|
+
/**
|
|
1045
|
+
* Phase 4 Sprint 8: Result of connected components analysis.
|
|
1046
|
+
*/
|
|
1047
|
+
export interface ConnectedComponentsResult {
|
|
1048
|
+
/** Array of components, each containing array of entity names */
|
|
1049
|
+
components: string[][];
|
|
1050
|
+
/** Number of components found */
|
|
1051
|
+
count: number;
|
|
1052
|
+
/** Size of the largest component */
|
|
1053
|
+
largestComponentSize: number;
|
|
1054
|
+
}
|
|
1055
|
+
/**
|
|
1056
|
+
* Phase 4 Sprint 8: Result of centrality analysis.
|
|
1057
|
+
*/
|
|
1058
|
+
export interface CentralityResult {
|
|
1059
|
+
/** Centrality scores for each entity */
|
|
1060
|
+
scores: Map<string, number>;
|
|
1061
|
+
/** Top N entities by centrality (name and score) */
|
|
1062
|
+
topEntities: Array<{
|
|
1063
|
+
name: string;
|
|
1064
|
+
score: number;
|
|
1065
|
+
}>;
|
|
1066
|
+
/** Centrality algorithm used */
|
|
1067
|
+
algorithm: 'degree' | 'betweenness' | 'pagerank';
|
|
1068
|
+
}
|
|
1069
|
+
/**
|
|
1070
|
+
* Phase 4 Sprint 9: Extended relation with optional weight and metadata.
|
|
1071
|
+
*/
|
|
1072
|
+
export interface WeightedRelation extends Relation {
|
|
1073
|
+
/** Optional weight for the relation (default: 1.0) */
|
|
1074
|
+
weight?: number;
|
|
1075
|
+
/** Optional metadata for the relation */
|
|
1076
|
+
metadata?: Record<string, unknown>;
|
|
1077
|
+
}
|
|
1078
|
+
/**
|
|
1079
|
+
* Phase 4 Sprint 10: Embedding service interface for vector embeddings.
|
|
1080
|
+
*
|
|
1081
|
+
* Provides abstraction over different embedding providers (OpenAI, local models).
|
|
1082
|
+
* Implementations can use cloud APIs or local transformer models.
|
|
1083
|
+
*
|
|
1084
|
+
* @example
|
|
1085
|
+
* ```typescript
|
|
1086
|
+
* const service: EmbeddingService = new OpenAIEmbeddingService(apiKey);
|
|
1087
|
+
* const embedding = await service.embed("Hello world");
|
|
1088
|
+
* console.log(`Vector dimensions: ${service.dimensions}`);
|
|
1089
|
+
* ```
|
|
1090
|
+
*/
|
|
1091
|
+
export interface EmbeddingService {
|
|
1092
|
+
/** Number of dimensions in the embedding vectors */
|
|
1093
|
+
readonly dimensions: number;
|
|
1094
|
+
/** Provider name for identification */
|
|
1095
|
+
readonly provider: string;
|
|
1096
|
+
/** Model name being used */
|
|
1097
|
+
readonly model: string;
|
|
1098
|
+
/**
|
|
1099
|
+
* Generate embedding for a single text.
|
|
1100
|
+
*
|
|
1101
|
+
* @param text - Text to embed
|
|
1102
|
+
* @returns Promise resolving to embedding vector (array of numbers)
|
|
1103
|
+
*/
|
|
1104
|
+
embed(text: string): Promise<number[]>;
|
|
1105
|
+
/**
|
|
1106
|
+
* Generate embeddings for multiple texts in batch.
|
|
1107
|
+
*
|
|
1108
|
+
* @param texts - Array of texts to embed
|
|
1109
|
+
* @returns Promise resolving to array of embedding vectors
|
|
1110
|
+
*/
|
|
1111
|
+
embedBatch(texts: string[]): Promise<number[][]>;
|
|
1112
|
+
/**
|
|
1113
|
+
* Check if the service is initialized and ready.
|
|
1114
|
+
*
|
|
1115
|
+
* @returns Promise resolving to true if ready
|
|
1116
|
+
*/
|
|
1117
|
+
isReady(): Promise<boolean>;
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
* Phase 4 Sprint 10: Result from semantic search operations.
|
|
1121
|
+
*
|
|
1122
|
+
* Contains the matched entity along with its similarity score
|
|
1123
|
+
* to the query vector.
|
|
1124
|
+
*
|
|
1125
|
+
* @example
|
|
1126
|
+
* ```typescript
|
|
1127
|
+
* const results: SemanticSearchResult[] = await semanticSearch.search("machine learning");
|
|
1128
|
+
* results.forEach(r => {
|
|
1129
|
+
* console.log(`${r.entity.name}: ${(r.similarity * 100).toFixed(1)}% similar`);
|
|
1130
|
+
* });
|
|
1131
|
+
* ```
|
|
1132
|
+
*/
|
|
1133
|
+
export interface SemanticSearchResult {
|
|
1134
|
+
/** The matched entity */
|
|
1135
|
+
entity: Entity;
|
|
1136
|
+
/** Similarity score (0.0 to 1.0, higher is more similar) */
|
|
1137
|
+
similarity: number;
|
|
1138
|
+
}
|
|
1139
|
+
/**
|
|
1140
|
+
* Phase 4 Sprint 11: Vector store interface for embedding storage and retrieval.
|
|
1141
|
+
*
|
|
1142
|
+
* Provides abstraction over different vector storage backends (in-memory, SQLite).
|
|
1143
|
+
*
|
|
1144
|
+
* @example
|
|
1145
|
+
* ```typescript
|
|
1146
|
+
* const store: IVectorStore = new InMemoryVectorStore();
|
|
1147
|
+
* await store.add("entity1", embedding);
|
|
1148
|
+
* const results = await store.search(queryVector, 10);
|
|
1149
|
+
* ```
|
|
1150
|
+
*/
|
|
1151
|
+
export interface IVectorStore {
|
|
1152
|
+
/**
|
|
1153
|
+
* Add a vector for an entity.
|
|
1154
|
+
*
|
|
1155
|
+
* @param entityName - Name of the entity
|
|
1156
|
+
* @param vector - Embedding vector
|
|
1157
|
+
*/
|
|
1158
|
+
add(entityName: string, vector: number[]): void;
|
|
1159
|
+
/**
|
|
1160
|
+
* Search for similar vectors.
|
|
1161
|
+
*
|
|
1162
|
+
* @param queryVector - Query embedding vector
|
|
1163
|
+
* @param k - Number of results to return
|
|
1164
|
+
* @returns Array of results with entity name and similarity score
|
|
1165
|
+
*/
|
|
1166
|
+
search(queryVector: number[], k: number): VectorSearchResult[];
|
|
1167
|
+
/**
|
|
1168
|
+
* Remove a vector by entity name.
|
|
1169
|
+
*
|
|
1170
|
+
* @param entityName - Name of the entity to remove
|
|
1171
|
+
* @returns True if found and removed
|
|
1172
|
+
*/
|
|
1173
|
+
remove(entityName: string): boolean;
|
|
1174
|
+
/**
|
|
1175
|
+
* Get the number of vectors stored.
|
|
1176
|
+
*
|
|
1177
|
+
* @returns Number of vectors
|
|
1178
|
+
*/
|
|
1179
|
+
size(): number;
|
|
1180
|
+
/**
|
|
1181
|
+
* Clear all vectors from the store.
|
|
1182
|
+
*/
|
|
1183
|
+
clear(): void;
|
|
1184
|
+
/**
|
|
1185
|
+
* Check if a vector exists for an entity.
|
|
1186
|
+
*
|
|
1187
|
+
* @param entityName - Name of the entity
|
|
1188
|
+
* @returns True if vector exists
|
|
1189
|
+
*/
|
|
1190
|
+
has(entityName: string): boolean;
|
|
1191
|
+
/**
|
|
1192
|
+
* Get the vector for an entity.
|
|
1193
|
+
*
|
|
1194
|
+
* @param entityName - Name of the entity
|
|
1195
|
+
* @returns Vector if found, undefined otherwise
|
|
1196
|
+
*/
|
|
1197
|
+
get(entityName: string): number[] | undefined;
|
|
1198
|
+
}
|
|
1199
|
+
/**
|
|
1200
|
+
* Phase 4 Sprint 11: Result from vector similarity search.
|
|
1201
|
+
*
|
|
1202
|
+
* Contains the entity name and similarity score from vector search.
|
|
1203
|
+
*/
|
|
1204
|
+
export interface VectorSearchResult {
|
|
1205
|
+
/** Entity name */
|
|
1206
|
+
name: string;
|
|
1207
|
+
/** Similarity score (0.0 to 1.0, cosine similarity) */
|
|
1208
|
+
score: number;
|
|
1209
|
+
}
|
|
1210
|
+
/**
|
|
1211
|
+
* Phase 4 Sprint 10: Configuration for embedding services.
|
|
1212
|
+
*/
|
|
1213
|
+
export interface EmbeddingConfig {
|
|
1214
|
+
/** Embedding provider: 'openai', 'local', or 'none' */
|
|
1215
|
+
provider: 'openai' | 'local' | 'none';
|
|
1216
|
+
/** API key for OpenAI (required when provider is 'openai') */
|
|
1217
|
+
apiKey?: string;
|
|
1218
|
+
/** Optional model override */
|
|
1219
|
+
model?: string;
|
|
1220
|
+
/** Whether to auto-index entities on creation */
|
|
1221
|
+
autoIndex?: boolean;
|
|
1222
|
+
}
|
|
1223
|
+
/**
|
|
1224
|
+
* Phase 4 Sprint 12: Options for semantic search indexing.
|
|
1225
|
+
*/
|
|
1226
|
+
export interface SemanticIndexOptions {
|
|
1227
|
+
/** Force re-indexing even if embeddings exist */
|
|
1228
|
+
forceReindex?: boolean;
|
|
1229
|
+
/** Progress callback for large graphs */
|
|
1230
|
+
onProgress?: (current: number, total: number) => void;
|
|
1231
|
+
/** Batch size for embedding API calls */
|
|
1232
|
+
batchSize?: number;
|
|
1233
|
+
/** AbortSignal for cancellation support (Phase 9B) */
|
|
1234
|
+
signal?: AbortSignal;
|
|
1235
|
+
}
|
|
1236
|
+
import type { ProgressCallback, TaskPriority } from '../utils/taskScheduler.js';
|
|
1237
|
+
/**
|
|
1238
|
+
* Phase 9B: Options for long-running operations supporting progress and cancellation.
|
|
1239
|
+
*
|
|
1240
|
+
* Used by operations that may take significant time and benefit from
|
|
1241
|
+
* user feedback and interruptibility.
|
|
1242
|
+
*
|
|
1243
|
+
* @example
|
|
1244
|
+
* ```typescript
|
|
1245
|
+
* // Basic usage with progress tracking
|
|
1246
|
+
* const entities = await manager.createEntities(data, {
|
|
1247
|
+
* onProgress: (p) => console.log(`${p.percentage}% complete`),
|
|
1248
|
+
* });
|
|
1249
|
+
*
|
|
1250
|
+
* // With cancellation support
|
|
1251
|
+
* const controller = new AbortController();
|
|
1252
|
+
* const promise = manager.importGraph('json', data, 'merge', false, {
|
|
1253
|
+
* signal: controller.signal,
|
|
1254
|
+
* onProgress: (p) => updateProgressBar(p.percentage),
|
|
1255
|
+
* });
|
|
1256
|
+
* // Later: controller.abort();
|
|
1257
|
+
* ```
|
|
1258
|
+
*/
|
|
1259
|
+
export interface LongRunningOperationOptions {
|
|
1260
|
+
/**
|
|
1261
|
+
* Progress callback for tracking operation progress.
|
|
1262
|
+
* Called periodically with completion status.
|
|
1263
|
+
*/
|
|
1264
|
+
onProgress?: ProgressCallback;
|
|
1265
|
+
/**
|
|
1266
|
+
* AbortSignal for cancellation support.
|
|
1267
|
+
* When aborted, the operation will throw OperationCancelledError.
|
|
1268
|
+
*/
|
|
1269
|
+
signal?: AbortSignal;
|
|
1270
|
+
/**
|
|
1271
|
+
* Priority for queued operations (optional).
|
|
1272
|
+
* Higher priority operations are processed first.
|
|
1273
|
+
*/
|
|
1274
|
+
priority?: TaskPriority;
|
|
1275
|
+
}
|
|
1276
|
+
/**
|
|
1277
|
+
* Phase 10 Sprint 1: Types of operations supported in a batch transaction.
|
|
1278
|
+
*
|
|
1279
|
+
* Used by BatchTransaction to categorize and execute operations atomically.
|
|
1280
|
+
*/
|
|
1281
|
+
export type BatchOperationType = 'createEntity' | 'updateEntity' | 'deleteEntity' | 'createRelation' | 'deleteRelation' | 'addObservations' | 'deleteObservations';
|
|
1282
|
+
/**
|
|
1283
|
+
* Phase 10 Sprint 1: A single operation in a batch transaction.
|
|
1284
|
+
*
|
|
1285
|
+
* Discriminated union type for type-safe batch operations.
|
|
1286
|
+
*
|
|
1287
|
+
* @example
|
|
1288
|
+
* ```typescript
|
|
1289
|
+
* const createOp: BatchOperation = {
|
|
1290
|
+
* type: 'createEntity',
|
|
1291
|
+
* data: { name: 'Alice', entityType: 'person', observations: [] }
|
|
1292
|
+
* };
|
|
1293
|
+
*
|
|
1294
|
+
* const updateOp: BatchOperation = {
|
|
1295
|
+
* type: 'updateEntity',
|
|
1296
|
+
* data: { name: 'Alice', updates: { importance: 8 } }
|
|
1297
|
+
* };
|
|
1298
|
+
* ```
|
|
1299
|
+
*/
|
|
1300
|
+
export type BatchOperation = {
|
|
1301
|
+
type: 'createEntity';
|
|
1302
|
+
data: Omit<Entity, 'createdAt' | 'lastModified'>;
|
|
1303
|
+
} | {
|
|
1304
|
+
type: 'updateEntity';
|
|
1305
|
+
data: {
|
|
1306
|
+
name: string;
|
|
1307
|
+
updates: Partial<Entity>;
|
|
1308
|
+
};
|
|
1309
|
+
} | {
|
|
1310
|
+
type: 'deleteEntity';
|
|
1311
|
+
data: {
|
|
1312
|
+
name: string;
|
|
1313
|
+
};
|
|
1314
|
+
} | {
|
|
1315
|
+
type: 'createRelation';
|
|
1316
|
+
data: Omit<Relation, 'createdAt' | 'lastModified'>;
|
|
1317
|
+
} | {
|
|
1318
|
+
type: 'deleteRelation';
|
|
1319
|
+
data: {
|
|
1320
|
+
from: string;
|
|
1321
|
+
to: string;
|
|
1322
|
+
relationType: string;
|
|
1323
|
+
};
|
|
1324
|
+
} | {
|
|
1325
|
+
type: 'addObservations';
|
|
1326
|
+
data: {
|
|
1327
|
+
name: string;
|
|
1328
|
+
observations: string[];
|
|
1329
|
+
};
|
|
1330
|
+
} | {
|
|
1331
|
+
type: 'deleteObservations';
|
|
1332
|
+
data: {
|
|
1333
|
+
name: string;
|
|
1334
|
+
observations: string[];
|
|
1335
|
+
};
|
|
1336
|
+
};
|
|
1337
|
+
/**
|
|
1338
|
+
* Phase 10 Sprint 1: Result of executing a batch transaction.
|
|
1339
|
+
*
|
|
1340
|
+
* Provides detailed statistics about what was processed and any errors.
|
|
1341
|
+
*
|
|
1342
|
+
* @example
|
|
1343
|
+
* ```typescript
|
|
1344
|
+
* const result: BatchResult = {
|
|
1345
|
+
* success: true,
|
|
1346
|
+
* operationsExecuted: 5,
|
|
1347
|
+
* entitiesCreated: 2,
|
|
1348
|
+
* entitiesUpdated: 1,
|
|
1349
|
+
* entitiesDeleted: 0,
|
|
1350
|
+
* relationsCreated: 2,
|
|
1351
|
+
* relationsDeleted: 0,
|
|
1352
|
+
* executionTimeMs: 45
|
|
1353
|
+
* };
|
|
1354
|
+
* ```
|
|
1355
|
+
*/
|
|
1356
|
+
export interface BatchResult {
|
|
1357
|
+
/** Whether the batch transaction was successful */
|
|
1358
|
+
success: boolean;
|
|
1359
|
+
/** Total number of operations executed */
|
|
1360
|
+
operationsExecuted: number;
|
|
1361
|
+
/** Number of entities created */
|
|
1362
|
+
entitiesCreated: number;
|
|
1363
|
+
/** Number of entities updated */
|
|
1364
|
+
entitiesUpdated: number;
|
|
1365
|
+
/** Number of entities deleted */
|
|
1366
|
+
entitiesDeleted: number;
|
|
1367
|
+
/** Number of relations created */
|
|
1368
|
+
relationsCreated: number;
|
|
1369
|
+
/** Number of relations deleted */
|
|
1370
|
+
relationsDeleted: number;
|
|
1371
|
+
/** Execution time in milliseconds */
|
|
1372
|
+
executionTimeMs: number;
|
|
1373
|
+
/** Error message if batch failed */
|
|
1374
|
+
error?: string;
|
|
1375
|
+
/** Index of the operation that failed (if applicable) */
|
|
1376
|
+
failedOperationIndex?: number;
|
|
1377
|
+
}
|
|
1378
|
+
/**
|
|
1379
|
+
* Phase 10 Sprint 1: Options for batch transaction execution.
|
|
1380
|
+
*
|
|
1381
|
+
* @example
|
|
1382
|
+
* ```typescript
|
|
1383
|
+
* const options: BatchOptions = {
|
|
1384
|
+
* stopOnError: true,
|
|
1385
|
+
* validateBeforeExecute: true
|
|
1386
|
+
* };
|
|
1387
|
+
* ```
|
|
1388
|
+
*/
|
|
1389
|
+
export interface BatchOptions {
|
|
1390
|
+
/**
|
|
1391
|
+
* Stop executing on first error (default: true).
|
|
1392
|
+
* If false, continues processing and collects all errors.
|
|
1393
|
+
*/
|
|
1394
|
+
stopOnError?: boolean;
|
|
1395
|
+
/**
|
|
1396
|
+
* Validate all operations before executing any (default: true).
|
|
1397
|
+
* Helps catch errors early before making any changes.
|
|
1398
|
+
*/
|
|
1399
|
+
validateBeforeExecute?: boolean;
|
|
1400
|
+
}
|
|
1401
|
+
/**
|
|
1402
|
+
* Phase 10 Sprint 2: Types of graph events that can be emitted.
|
|
1403
|
+
*
|
|
1404
|
+
* Used by GraphEventEmitter to categorize graph changes.
|
|
1405
|
+
*/
|
|
1406
|
+
export type GraphEventType = 'entity:created' | 'entity:updated' | 'entity:deleted' | 'relation:created' | 'relation:deleted' | 'observation:added' | 'observation:deleted' | 'graph:saved' | 'graph:loaded';
|
|
1407
|
+
/**
|
|
1408
|
+
* Phase 10 Sprint 2: Base interface for all graph events.
|
|
1409
|
+
*
|
|
1410
|
+
* All graph events share a common structure with timestamp and type.
|
|
1411
|
+
*/
|
|
1412
|
+
export interface GraphEventBase {
|
|
1413
|
+
/** Event type identifier */
|
|
1414
|
+
type: GraphEventType;
|
|
1415
|
+
/** ISO 8601 timestamp when the event occurred */
|
|
1416
|
+
timestamp: string;
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* Phase 10 Sprint 2: Event emitted when an entity is created.
|
|
1420
|
+
*
|
|
1421
|
+
* @example
|
|
1422
|
+
* ```typescript
|
|
1423
|
+
* emitter.on('entity:created', (event) => {
|
|
1424
|
+
* console.log(`Entity ${event.entity.name} created`);
|
|
1425
|
+
* });
|
|
1426
|
+
* ```
|
|
1427
|
+
*/
|
|
1428
|
+
export interface EntityCreatedEvent extends GraphEventBase {
|
|
1429
|
+
type: 'entity:created';
|
|
1430
|
+
entity: Entity;
|
|
1431
|
+
}
|
|
1432
|
+
/**
|
|
1433
|
+
* Phase 10 Sprint 2: Event emitted when an entity is updated.
|
|
1434
|
+
*
|
|
1435
|
+
* @example
|
|
1436
|
+
* ```typescript
|
|
1437
|
+
* emitter.on('entity:updated', (event) => {
|
|
1438
|
+
* console.log(`Entity ${event.entityName} updated`);
|
|
1439
|
+
* console.log(`Changed fields: ${Object.keys(event.changes).join(', ')}`);
|
|
1440
|
+
* });
|
|
1441
|
+
* ```
|
|
1442
|
+
*/
|
|
1443
|
+
export interface EntityUpdatedEvent extends GraphEventBase {
|
|
1444
|
+
type: 'entity:updated';
|
|
1445
|
+
entityName: string;
|
|
1446
|
+
changes: Partial<Entity>;
|
|
1447
|
+
previousValues?: Partial<Entity>;
|
|
1448
|
+
}
|
|
1449
|
+
/**
|
|
1450
|
+
* Phase 10 Sprint 2: Event emitted when an entity is deleted.
|
|
1451
|
+
*
|
|
1452
|
+
* @example
|
|
1453
|
+
* ```typescript
|
|
1454
|
+
* emitter.on('entity:deleted', (event) => {
|
|
1455
|
+
* console.log(`Entity ${event.entityName} deleted`);
|
|
1456
|
+
* });
|
|
1457
|
+
* ```
|
|
1458
|
+
*/
|
|
1459
|
+
export interface EntityDeletedEvent extends GraphEventBase {
|
|
1460
|
+
type: 'entity:deleted';
|
|
1461
|
+
entityName: string;
|
|
1462
|
+
entity?: Entity;
|
|
1463
|
+
}
|
|
1464
|
+
/**
|
|
1465
|
+
* Phase 10 Sprint 2: Event emitted when a relation is created.
|
|
1466
|
+
*
|
|
1467
|
+
* @example
|
|
1468
|
+
* ```typescript
|
|
1469
|
+
* emitter.on('relation:created', (event) => {
|
|
1470
|
+
* console.log(`Relation ${event.relation.from} -> ${event.relation.to} created`);
|
|
1471
|
+
* });
|
|
1472
|
+
* ```
|
|
1473
|
+
*/
|
|
1474
|
+
export interface RelationCreatedEvent extends GraphEventBase {
|
|
1475
|
+
type: 'relation:created';
|
|
1476
|
+
relation: Relation;
|
|
1477
|
+
}
|
|
1478
|
+
/**
|
|
1479
|
+
* Phase 10 Sprint 2: Event emitted when a relation is deleted.
|
|
1480
|
+
*
|
|
1481
|
+
* @example
|
|
1482
|
+
* ```typescript
|
|
1483
|
+
* emitter.on('relation:deleted', (event) => {
|
|
1484
|
+
* console.log(`Relation ${event.from} -> ${event.to} (${event.relationType}) deleted`);
|
|
1485
|
+
* });
|
|
1486
|
+
* ```
|
|
1487
|
+
*/
|
|
1488
|
+
export interface RelationDeletedEvent extends GraphEventBase {
|
|
1489
|
+
type: 'relation:deleted';
|
|
1490
|
+
from: string;
|
|
1491
|
+
to: string;
|
|
1492
|
+
relationType: string;
|
|
1493
|
+
}
|
|
1494
|
+
/**
|
|
1495
|
+
* Phase 10 Sprint 2: Event emitted when observations are added to an entity.
|
|
1496
|
+
*
|
|
1497
|
+
* @example
|
|
1498
|
+
* ```typescript
|
|
1499
|
+
* emitter.on('observation:added', (event) => {
|
|
1500
|
+
* console.log(`Added ${event.observations.length} observations to ${event.entityName}`);
|
|
1501
|
+
* });
|
|
1502
|
+
* ```
|
|
1503
|
+
*/
|
|
1504
|
+
export interface ObservationAddedEvent extends GraphEventBase {
|
|
1505
|
+
type: 'observation:added';
|
|
1506
|
+
entityName: string;
|
|
1507
|
+
observations: string[];
|
|
1508
|
+
}
|
|
1509
|
+
/**
|
|
1510
|
+
* Phase 10 Sprint 2: Event emitted when observations are deleted from an entity.
|
|
1511
|
+
*
|
|
1512
|
+
* @example
|
|
1513
|
+
* ```typescript
|
|
1514
|
+
* emitter.on('observation:deleted', (event) => {
|
|
1515
|
+
* console.log(`Deleted ${event.observations.length} observations from ${event.entityName}`);
|
|
1516
|
+
* });
|
|
1517
|
+
* ```
|
|
1518
|
+
*/
|
|
1519
|
+
export interface ObservationDeletedEvent extends GraphEventBase {
|
|
1520
|
+
type: 'observation:deleted';
|
|
1521
|
+
entityName: string;
|
|
1522
|
+
observations: string[];
|
|
1523
|
+
}
|
|
1524
|
+
/**
|
|
1525
|
+
* Phase 10 Sprint 2: Event emitted when the entire graph is saved.
|
|
1526
|
+
*
|
|
1527
|
+
* @example
|
|
1528
|
+
* ```typescript
|
|
1529
|
+
* emitter.on('graph:saved', (event) => {
|
|
1530
|
+
* console.log(`Graph saved with ${event.entityCount} entities`);
|
|
1531
|
+
* });
|
|
1532
|
+
* ```
|
|
1533
|
+
*/
|
|
1534
|
+
export interface GraphSavedEvent extends GraphEventBase {
|
|
1535
|
+
type: 'graph:saved';
|
|
1536
|
+
entityCount: number;
|
|
1537
|
+
relationCount: number;
|
|
1538
|
+
}
|
|
1539
|
+
/**
|
|
1540
|
+
* Phase 10 Sprint 2: Event emitted when the graph is loaded from storage.
|
|
1541
|
+
*
|
|
1542
|
+
* @example
|
|
1543
|
+
* ```typescript
|
|
1544
|
+
* emitter.on('graph:loaded', (event) => {
|
|
1545
|
+
* console.log(`Graph loaded with ${event.entityCount} entities`);
|
|
1546
|
+
* });
|
|
1547
|
+
* ```
|
|
1548
|
+
*/
|
|
1549
|
+
export interface GraphLoadedEvent extends GraphEventBase {
|
|
1550
|
+
type: 'graph:loaded';
|
|
1551
|
+
entityCount: number;
|
|
1552
|
+
relationCount: number;
|
|
1553
|
+
}
|
|
1554
|
+
/**
|
|
1555
|
+
* Phase 10 Sprint 2: Union type of all possible graph events.
|
|
1556
|
+
*
|
|
1557
|
+
* Use this when handling any type of graph event.
|
|
1558
|
+
*/
|
|
1559
|
+
export type GraphEvent = EntityCreatedEvent | EntityUpdatedEvent | EntityDeletedEvent | RelationCreatedEvent | RelationDeletedEvent | ObservationAddedEvent | ObservationDeletedEvent | GraphSavedEvent | GraphLoadedEvent;
|
|
1560
|
+
/**
|
|
1561
|
+
* Phase 10 Sprint 2: Listener function type for graph events.
|
|
1562
|
+
*
|
|
1563
|
+
* @template T - The specific event type to listen for
|
|
1564
|
+
*/
|
|
1565
|
+
export type GraphEventListener<T extends GraphEvent = GraphEvent> = (event: T) => void;
|
|
1566
|
+
/**
|
|
1567
|
+
* Phase 10 Sprint 2: Map of event types to their corresponding event interfaces.
|
|
1568
|
+
*
|
|
1569
|
+
* Used for type-safe event listener registration.
|
|
1570
|
+
*/
|
|
1571
|
+
export interface GraphEventMap {
|
|
1572
|
+
'entity:created': EntityCreatedEvent;
|
|
1573
|
+
'entity:updated': EntityUpdatedEvent;
|
|
1574
|
+
'entity:deleted': EntityDeletedEvent;
|
|
1575
|
+
'relation:created': RelationCreatedEvent;
|
|
1576
|
+
'relation:deleted': RelationDeletedEvent;
|
|
1577
|
+
'observation:added': ObservationAddedEvent;
|
|
1578
|
+
'observation:deleted': ObservationDeletedEvent;
|
|
1579
|
+
'graph:saved': GraphSavedEvent;
|
|
1580
|
+
'graph:loaded': GraphLoadedEvent;
|
|
1581
|
+
}
|
|
1582
|
+
/**
|
|
1583
|
+
* Phase 10 Sprint 4: Search method types for cost estimation.
|
|
1584
|
+
*/
|
|
1585
|
+
export type SearchMethod = 'basic' | 'ranked' | 'boolean' | 'fuzzy' | 'semantic';
|
|
1586
|
+
/**
|
|
1587
|
+
* Phase 10 Sprint 4: Result of query cost estimation.
|
|
1588
|
+
*
|
|
1589
|
+
* Provides information about the estimated cost and complexity
|
|
1590
|
+
* of a search query, helping clients choose optimal search methods.
|
|
1591
|
+
*
|
|
1592
|
+
* @example
|
|
1593
|
+
* ```typescript
|
|
1594
|
+
* const estimate: QueryCostEstimate = {
|
|
1595
|
+
* method: 'ranked',
|
|
1596
|
+
* estimatedTimeMs: 15,
|
|
1597
|
+
* complexity: 'medium',
|
|
1598
|
+
* entityCount: 1000,
|
|
1599
|
+
* recommendation: 'Use ranked search for best relevance'
|
|
1600
|
+
* };
|
|
1601
|
+
* ```
|
|
1602
|
+
*/
|
|
1603
|
+
export interface QueryCostEstimate {
|
|
1604
|
+
/** The search method being estimated */
|
|
1605
|
+
method: SearchMethod;
|
|
1606
|
+
/** Estimated execution time in milliseconds */
|
|
1607
|
+
estimatedTimeMs: number;
|
|
1608
|
+
/** Query complexity level */
|
|
1609
|
+
complexity: 'low' | 'medium' | 'high';
|
|
1610
|
+
/** Number of entities in the graph */
|
|
1611
|
+
entityCount: number;
|
|
1612
|
+
/** Human-readable recommendation */
|
|
1613
|
+
recommendation: string;
|
|
1614
|
+
/** Whether this method is recommended for the query */
|
|
1615
|
+
isRecommended: boolean;
|
|
1616
|
+
}
|
|
1617
|
+
/**
|
|
1618
|
+
* Phase 10 Sprint 4: Result of automatic search method selection.
|
|
1619
|
+
*
|
|
1620
|
+
* Returned by the search_auto tool to show which method was chosen
|
|
1621
|
+
* and why, along with the actual search results.
|
|
1622
|
+
*/
|
|
1623
|
+
export interface AutoSearchResult {
|
|
1624
|
+
/** The search method that was selected */
|
|
1625
|
+
selectedMethod: SearchMethod;
|
|
1626
|
+
/** Why this method was selected */
|
|
1627
|
+
selectionReason: string;
|
|
1628
|
+
/** Cost estimates for all considered methods */
|
|
1629
|
+
estimates: QueryCostEstimate[];
|
|
1630
|
+
/** The actual search results */
|
|
1631
|
+
results: SearchResult[];
|
|
1632
|
+
/** Actual execution time in milliseconds */
|
|
1633
|
+
executionTimeMs: number;
|
|
1634
|
+
}
|
|
1635
|
+
/**
|
|
1636
|
+
* Phase 10 Sprint 4: Options for the QueryCostEstimator.
|
|
1637
|
+
*/
|
|
1638
|
+
export interface QueryCostEstimatorOptions {
|
|
1639
|
+
/** Base time in ms for basic search per entity (default: 0.01) */
|
|
1640
|
+
basicTimePerEntity?: number;
|
|
1641
|
+
/** Base time in ms for ranked search per entity (default: 0.05) */
|
|
1642
|
+
rankedTimePerEntity?: number;
|
|
1643
|
+
/** Base time in ms for boolean search per entity (default: 0.02) */
|
|
1644
|
+
booleanTimePerEntity?: number;
|
|
1645
|
+
/** Base time in ms for fuzzy search per entity (default: 0.1) */
|
|
1646
|
+
fuzzyTimePerEntity?: number;
|
|
1647
|
+
/** Base time in ms for semantic search per entity (default: 0.5) */
|
|
1648
|
+
semanticTimePerEntity?: number;
|
|
1649
|
+
/** Threshold for "low" complexity (entity count, default: 100) */
|
|
1650
|
+
lowComplexityThreshold?: number;
|
|
1651
|
+
/** Threshold for "high" complexity (entity count, default: 1000) */
|
|
1652
|
+
highComplexityThreshold?: number;
|
|
1653
|
+
}
|
|
1654
|
+
//# sourceMappingURL=types.d.ts.map
|