@soulcraft/brainy 0.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.demo.md +59 -0
- package/README.md +1257 -0
- package/brainy.png +0 -0
- package/cli-wrapper.js +56 -0
- package/dist/augmentationFactory.d.ts +87 -0
- package/dist/augmentationPipeline.d.ts +205 -0
- package/dist/augmentationRegistry.d.ts +48 -0
- package/dist/augmentationRegistryLoader.d.ts +147 -0
- package/dist/augmentations/conduitAugmentations.d.ts +173 -0
- package/dist/augmentations/memoryAugmentations.d.ts +71 -0
- package/dist/augmentations/serverSearchAugmentations.d.ts +168 -0
- package/dist/brainy.js +116929 -0
- package/dist/brainy.min.js +16107 -0
- package/dist/brainyData.d.ts +507 -0
- package/dist/cli.d.ts +7 -0
- package/dist/coreTypes.d.ts +131 -0
- package/dist/examples/basicUsage.d.ts +5 -0
- package/dist/hnsw/hnswIndex.d.ts +96 -0
- package/dist/hnsw/hnswIndexOptimized.d.ts +167 -0
- package/dist/index.d.ts +49 -0
- package/dist/mcp/brainyMCPAdapter.d.ts +69 -0
- package/dist/mcp/brainyMCPService.d.ts +99 -0
- package/dist/mcp/index.d.ts +14 -0
- package/dist/mcp/mcpAugmentationToolset.d.ts +68 -0
- package/dist/pipeline.d.ts +281 -0
- package/dist/sequentialPipeline.d.ts +114 -0
- package/dist/storage/fileSystemStorage.d.ts +123 -0
- package/dist/storage/opfsStorage.d.ts +244 -0
- package/dist/storage/s3CompatibleStorage.d.ts +158 -0
- package/dist/types/augmentations.d.ts +324 -0
- package/dist/types/augmentations.d.ts.map +1 -0
- package/dist/types/brainyDataInterface.d.ts +51 -0
- package/dist/types/brainyDataInterface.d.ts.map +1 -0
- package/dist/types/fileSystemTypes.d.ts +6 -0
- package/dist/types/fileSystemTypes.d.ts.map +1 -0
- package/dist/types/graphTypes.d.ts +134 -0
- package/dist/types/graphTypes.d.ts.map +1 -0
- package/dist/types/mcpTypes.d.ts +140 -0
- package/dist/types/mcpTypes.d.ts.map +1 -0
- package/dist/types/pipelineTypes.d.ts +27 -0
- package/dist/types/pipelineTypes.d.ts.map +1 -0
- package/dist/types/tensorflowTypes.d.ts +7 -0
- package/dist/types/tensorflowTypes.d.ts.map +1 -0
- package/dist/unified.d.ts +12 -0
- package/dist/unified.js +117122 -0
- package/dist/unified.min.js +16107 -0
- package/dist/utils/distance.d.ts +32 -0
- package/dist/utils/embedding.d.ts +55 -0
- package/dist/utils/environment.d.ts +28 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/version.d.ts +6 -0
- package/dist/utils/workerUtils.d.ts +28 -0
- package/package.json +156 -0
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BrainyData
|
|
3
|
+
* Main class that provides the vector database functionality
|
|
4
|
+
*/
|
|
5
|
+
import { HNSWOptimizedConfig } from './hnsw/hnswIndexOptimized.js';
|
|
6
|
+
import { DistanceFunction, GraphVerb, EmbeddingFunction, HNSWConfig, SearchResult, StorageAdapter, Vector, VectorDocument } from './coreTypes.js';
|
|
7
|
+
import { NounType, VerbType } from './types/graphTypes.js';
|
|
8
|
+
import { WebSocketConnection } from './types/augmentations.js';
|
|
9
|
+
import { BrainyDataInterface } from './types/brainyDataInterface.js';
|
|
10
|
+
export interface BrainyDataConfig {
|
|
11
|
+
/**
|
|
12
|
+
* HNSW index configuration
|
|
13
|
+
*/
|
|
14
|
+
hnsw?: Partial<HNSWConfig>;
|
|
15
|
+
/**
|
|
16
|
+
* Optimized HNSW index configuration
|
|
17
|
+
* If provided, will use the optimized HNSW index instead of the standard one
|
|
18
|
+
*/
|
|
19
|
+
hnswOptimized?: Partial<HNSWOptimizedConfig>;
|
|
20
|
+
/**
|
|
21
|
+
* Distance function to use for similarity calculations
|
|
22
|
+
*/
|
|
23
|
+
distanceFunction?: DistanceFunction;
|
|
24
|
+
/**
|
|
25
|
+
* Custom storage adapter (if not provided, will use OPFS or memory storage)
|
|
26
|
+
*/
|
|
27
|
+
storageAdapter?: StorageAdapter;
|
|
28
|
+
/**
|
|
29
|
+
* Storage configuration options
|
|
30
|
+
* These will be passed to createStorage if storageAdapter is not provided
|
|
31
|
+
*/
|
|
32
|
+
storage?: {
|
|
33
|
+
requestPersistentStorage?: boolean;
|
|
34
|
+
r2Storage?: {
|
|
35
|
+
bucketName?: string;
|
|
36
|
+
accountId?: string;
|
|
37
|
+
accessKeyId?: string;
|
|
38
|
+
secretAccessKey?: string;
|
|
39
|
+
};
|
|
40
|
+
s3Storage?: {
|
|
41
|
+
bucketName?: string;
|
|
42
|
+
accessKeyId?: string;
|
|
43
|
+
secretAccessKey?: string;
|
|
44
|
+
region?: string;
|
|
45
|
+
};
|
|
46
|
+
gcsStorage?: {
|
|
47
|
+
bucketName?: string;
|
|
48
|
+
accessKeyId?: string;
|
|
49
|
+
secretAccessKey?: string;
|
|
50
|
+
endpoint?: string;
|
|
51
|
+
};
|
|
52
|
+
customS3Storage?: {
|
|
53
|
+
bucketName?: string;
|
|
54
|
+
accessKeyId?: string;
|
|
55
|
+
secretAccessKey?: string;
|
|
56
|
+
endpoint?: string;
|
|
57
|
+
region?: string;
|
|
58
|
+
};
|
|
59
|
+
forceFileSystemStorage?: boolean;
|
|
60
|
+
forceMemoryStorage?: boolean;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Embedding function to convert data to vectors
|
|
64
|
+
*/
|
|
65
|
+
embeddingFunction?: EmbeddingFunction;
|
|
66
|
+
/**
|
|
67
|
+
* Set the database to read-only mode
|
|
68
|
+
* When true, all write operations will throw an error
|
|
69
|
+
*/
|
|
70
|
+
readOnly?: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Remote server configuration for search operations
|
|
73
|
+
*/
|
|
74
|
+
remoteServer?: {
|
|
75
|
+
/**
|
|
76
|
+
* WebSocket URL of the remote Brainy server
|
|
77
|
+
*/
|
|
78
|
+
url: string;
|
|
79
|
+
/**
|
|
80
|
+
* WebSocket protocols to use for the connection
|
|
81
|
+
*/
|
|
82
|
+
protocols?: string | string[];
|
|
83
|
+
/**
|
|
84
|
+
* Whether to automatically connect to the remote server on initialization
|
|
85
|
+
*/
|
|
86
|
+
autoConnect?: boolean;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export declare class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|
90
|
+
private index;
|
|
91
|
+
private storage;
|
|
92
|
+
private isInitialized;
|
|
93
|
+
private embeddingFunction;
|
|
94
|
+
private distanceFunction;
|
|
95
|
+
private requestPersistentStorage;
|
|
96
|
+
private readOnly;
|
|
97
|
+
private storageConfig;
|
|
98
|
+
private useOptimizedIndex;
|
|
99
|
+
private remoteServerConfig;
|
|
100
|
+
private serverSearchConduit;
|
|
101
|
+
private serverConnection;
|
|
102
|
+
/**
|
|
103
|
+
* Create a new vector database
|
|
104
|
+
*/
|
|
105
|
+
constructor(config?: BrainyDataConfig);
|
|
106
|
+
/**
|
|
107
|
+
* Check if the database is in read-only mode and throw an error if it is
|
|
108
|
+
* @throws Error if the database is in read-only mode
|
|
109
|
+
*/
|
|
110
|
+
private checkReadOnly;
|
|
111
|
+
/**
|
|
112
|
+
* Initialize the database
|
|
113
|
+
* Loads existing data from storage if available
|
|
114
|
+
*/
|
|
115
|
+
init(): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Connect to a remote Brainy server for search operations
|
|
118
|
+
* @param serverUrl WebSocket URL of the remote Brainy server
|
|
119
|
+
* @param protocols Optional WebSocket protocols to use
|
|
120
|
+
* @returns The connection object
|
|
121
|
+
*/
|
|
122
|
+
connectToRemoteServer(serverUrl: string, protocols?: string | string[]): Promise<WebSocketConnection>;
|
|
123
|
+
/**
|
|
124
|
+
* Add a vector or data to the database
|
|
125
|
+
* If the input is not a vector, it will be converted using the embedding function
|
|
126
|
+
* @param vectorOrData Vector or data to add
|
|
127
|
+
* @param metadata Optional metadata to associate with the vector
|
|
128
|
+
* @param options Additional options
|
|
129
|
+
* @returns The ID of the added vector
|
|
130
|
+
*/
|
|
131
|
+
add(vectorOrData: Vector | any, metadata?: T, options?: {
|
|
132
|
+
forceEmbed?: boolean;
|
|
133
|
+
addToRemote?: boolean;
|
|
134
|
+
id?: string;
|
|
135
|
+
}): Promise<string>;
|
|
136
|
+
/**
|
|
137
|
+
* Add data to both local and remote Brainy instances
|
|
138
|
+
* @param vectorOrData Vector or data to add
|
|
139
|
+
* @param metadata Optional metadata to associate with the vector
|
|
140
|
+
* @param options Additional options
|
|
141
|
+
* @returns The ID of the added vector
|
|
142
|
+
*/
|
|
143
|
+
addToBoth(vectorOrData: Vector | any, metadata?: T, options?: {
|
|
144
|
+
forceEmbed?: boolean;
|
|
145
|
+
}): Promise<string>;
|
|
146
|
+
/**
|
|
147
|
+
* Add a vector to the remote server
|
|
148
|
+
* @param id ID of the vector to add
|
|
149
|
+
* @param vector Vector to add
|
|
150
|
+
* @param metadata Optional metadata to associate with the vector
|
|
151
|
+
* @returns True if successful, false otherwise
|
|
152
|
+
* @private
|
|
153
|
+
*/
|
|
154
|
+
private addToRemote;
|
|
155
|
+
/**
|
|
156
|
+
* Add multiple vectors or data items to the database
|
|
157
|
+
* @param items Array of items to add
|
|
158
|
+
* @param options Additional options
|
|
159
|
+
* @returns Array of IDs for the added items
|
|
160
|
+
*/
|
|
161
|
+
addBatch(items: Array<{
|
|
162
|
+
vectorOrData: Vector | any;
|
|
163
|
+
metadata?: T;
|
|
164
|
+
}>, options?: {
|
|
165
|
+
forceEmbed?: boolean;
|
|
166
|
+
addToRemote?: boolean;
|
|
167
|
+
concurrency?: number;
|
|
168
|
+
}): Promise<string[]>;
|
|
169
|
+
/**
|
|
170
|
+
* Add multiple vectors or data items to both local and remote databases
|
|
171
|
+
* @param items Array of items to add
|
|
172
|
+
* @param options Additional options
|
|
173
|
+
* @returns Array of IDs for the added items
|
|
174
|
+
*/
|
|
175
|
+
addBatchToBoth(items: Array<{
|
|
176
|
+
vectorOrData: Vector | any;
|
|
177
|
+
metadata?: T;
|
|
178
|
+
}>, options?: {
|
|
179
|
+
forceEmbed?: boolean;
|
|
180
|
+
concurrency?: number;
|
|
181
|
+
}): Promise<string[]>;
|
|
182
|
+
/**
|
|
183
|
+
* Search for similar vectors within specific noun types
|
|
184
|
+
* @param queryVectorOrData Query vector or data to search for
|
|
185
|
+
* @param k Number of results to return
|
|
186
|
+
* @param nounTypes Array of noun types to search within, or null to search all
|
|
187
|
+
* @param options Additional options
|
|
188
|
+
* @returns Array of search results
|
|
189
|
+
*/
|
|
190
|
+
searchByNounTypes(queryVectorOrData: Vector | any, k?: number, nounTypes?: string[] | null, options?: {
|
|
191
|
+
forceEmbed?: boolean;
|
|
192
|
+
}): Promise<SearchResult<T>[]>;
|
|
193
|
+
/**
|
|
194
|
+
* Search for similar vectors
|
|
195
|
+
* @param queryVectorOrData Query vector or data to search for
|
|
196
|
+
* @param k Number of results to return
|
|
197
|
+
* @param options Additional options
|
|
198
|
+
* @returns Array of search results
|
|
199
|
+
*/
|
|
200
|
+
search(queryVectorOrData: Vector | any, k?: number, options?: {
|
|
201
|
+
forceEmbed?: boolean;
|
|
202
|
+
nounTypes?: string[];
|
|
203
|
+
includeVerbs?: boolean;
|
|
204
|
+
searchMode?: 'local' | 'remote' | 'combined';
|
|
205
|
+
searchVerbs?: boolean;
|
|
206
|
+
verbTypes?: string[];
|
|
207
|
+
searchConnectedNouns?: boolean;
|
|
208
|
+
verbDirection?: 'outgoing' | 'incoming' | 'both';
|
|
209
|
+
}): Promise<SearchResult<T>[]>;
|
|
210
|
+
/**
|
|
211
|
+
* Search the local database for similar vectors
|
|
212
|
+
* @param queryVectorOrData Query vector or data to search for
|
|
213
|
+
* @param k Number of results to return
|
|
214
|
+
* @param options Additional options
|
|
215
|
+
* @returns Array of search results
|
|
216
|
+
*/
|
|
217
|
+
searchLocal(queryVectorOrData: Vector | any, k?: number, options?: {
|
|
218
|
+
forceEmbed?: boolean;
|
|
219
|
+
nounTypes?: string[];
|
|
220
|
+
includeVerbs?: boolean;
|
|
221
|
+
}): Promise<SearchResult<T>[]>;
|
|
222
|
+
/**
|
|
223
|
+
* Find entities similar to a given entity ID
|
|
224
|
+
* @param id ID of the entity to find similar entities for
|
|
225
|
+
* @param options Additional options
|
|
226
|
+
* @returns Array of search results with similarity scores
|
|
227
|
+
*/
|
|
228
|
+
findSimilar(id: string, options?: {
|
|
229
|
+
limit?: number;
|
|
230
|
+
nounTypes?: string[];
|
|
231
|
+
includeVerbs?: boolean;
|
|
232
|
+
searchMode?: 'local' | 'remote' | 'combined';
|
|
233
|
+
}): Promise<SearchResult<T>[]>;
|
|
234
|
+
/**
|
|
235
|
+
* Get a vector by ID
|
|
236
|
+
*/
|
|
237
|
+
get(id: string): Promise<VectorDocument<T> | null>;
|
|
238
|
+
/**
|
|
239
|
+
* Get all nouns in the database
|
|
240
|
+
* @returns Array of vector documents
|
|
241
|
+
*/
|
|
242
|
+
getAllNouns(): Promise<VectorDocument<T>[]>;
|
|
243
|
+
/**
|
|
244
|
+
* Delete a vector by ID
|
|
245
|
+
*/
|
|
246
|
+
delete(id: string): Promise<boolean>;
|
|
247
|
+
/**
|
|
248
|
+
* Update metadata for a vector
|
|
249
|
+
*/
|
|
250
|
+
updateMetadata(id: string, metadata: T): Promise<boolean>;
|
|
251
|
+
/**
|
|
252
|
+
* Create a relationship between two entities
|
|
253
|
+
* This is a convenience wrapper around addVerb
|
|
254
|
+
*/
|
|
255
|
+
relate(sourceId: string, targetId: string, relationType: string, metadata?: any): Promise<string>;
|
|
256
|
+
/**
|
|
257
|
+
* Add a verb between two nouns
|
|
258
|
+
* If metadata is provided and vector is not, the metadata will be vectorized using the embedding function
|
|
259
|
+
*/
|
|
260
|
+
addVerb(sourceId: string, targetId: string, vector?: Vector, options?: {
|
|
261
|
+
type?: string;
|
|
262
|
+
weight?: number;
|
|
263
|
+
metadata?: any;
|
|
264
|
+
forceEmbed?: boolean;
|
|
265
|
+
id?: string;
|
|
266
|
+
}): Promise<string>;
|
|
267
|
+
/**
|
|
268
|
+
* Get a verb by ID
|
|
269
|
+
*/
|
|
270
|
+
getVerb(id: string): Promise<GraphVerb | null>;
|
|
271
|
+
/**
|
|
272
|
+
* Get all verbs
|
|
273
|
+
*/
|
|
274
|
+
getAllVerbs(): Promise<GraphVerb[]>;
|
|
275
|
+
/**
|
|
276
|
+
* Get verbs by source noun ID
|
|
277
|
+
*/
|
|
278
|
+
getVerbsBySource(sourceId: string): Promise<GraphVerb[]>;
|
|
279
|
+
/**
|
|
280
|
+
* Get verbs by target noun ID
|
|
281
|
+
*/
|
|
282
|
+
getVerbsByTarget(targetId: string): Promise<GraphVerb[]>;
|
|
283
|
+
/**
|
|
284
|
+
* Get verbs by type
|
|
285
|
+
*/
|
|
286
|
+
getVerbsByType(type: string): Promise<GraphVerb[]>;
|
|
287
|
+
/**
|
|
288
|
+
* Delete a verb
|
|
289
|
+
*/
|
|
290
|
+
deleteVerb(id: string): Promise<boolean>;
|
|
291
|
+
/**
|
|
292
|
+
* Clear the database
|
|
293
|
+
*/
|
|
294
|
+
clear(): Promise<void>;
|
|
295
|
+
/**
|
|
296
|
+
* Get the number of vectors in the database
|
|
297
|
+
*/
|
|
298
|
+
size(): number;
|
|
299
|
+
/**
|
|
300
|
+
* Check if the database is in read-only mode
|
|
301
|
+
* @returns True if the database is in read-only mode, false otherwise
|
|
302
|
+
*/
|
|
303
|
+
isReadOnly(): boolean;
|
|
304
|
+
/**
|
|
305
|
+
* Set the database to read-only mode
|
|
306
|
+
* @param readOnly True to set the database to read-only mode, false to allow writes
|
|
307
|
+
*/
|
|
308
|
+
setReadOnly(readOnly: boolean): void;
|
|
309
|
+
/**
|
|
310
|
+
* Embed text or data into a vector using the same embedding function used by this instance
|
|
311
|
+
* This allows clients to use the same TensorFlow Universal Sentence Encoder throughout their application
|
|
312
|
+
*
|
|
313
|
+
* @param data Text or data to embed
|
|
314
|
+
* @returns A promise that resolves to the embedded vector
|
|
315
|
+
*/
|
|
316
|
+
embed(data: string | string[]): Promise<Vector>;
|
|
317
|
+
/**
|
|
318
|
+
* Search for verbs by type and/or vector similarity
|
|
319
|
+
* @param queryVectorOrData Query vector or data to search for
|
|
320
|
+
* @param k Number of results to return
|
|
321
|
+
* @param options Additional options
|
|
322
|
+
* @returns Array of verbs with similarity scores
|
|
323
|
+
*/
|
|
324
|
+
searchVerbs(queryVectorOrData: Vector | any, k?: number, options?: {
|
|
325
|
+
forceEmbed?: boolean;
|
|
326
|
+
verbTypes?: string[];
|
|
327
|
+
}): Promise<Array<GraphVerb & {
|
|
328
|
+
similarity: number;
|
|
329
|
+
}>>;
|
|
330
|
+
/**
|
|
331
|
+
* Search for nouns connected by specific verb types
|
|
332
|
+
* @param queryVectorOrData Query vector or data to search for
|
|
333
|
+
* @param k Number of results to return
|
|
334
|
+
* @param options Additional options
|
|
335
|
+
* @returns Array of search results
|
|
336
|
+
*/
|
|
337
|
+
searchNounsByVerbs(queryVectorOrData: Vector | any, k?: number, options?: {
|
|
338
|
+
forceEmbed?: boolean;
|
|
339
|
+
verbTypes?: string[];
|
|
340
|
+
direction?: 'outgoing' | 'incoming' | 'both';
|
|
341
|
+
}): Promise<SearchResult<T>[]>;
|
|
342
|
+
/**
|
|
343
|
+
* Search for similar documents using a text query
|
|
344
|
+
* This is a convenience method that embeds the query text and performs a search
|
|
345
|
+
*
|
|
346
|
+
* @param query Text query to search for
|
|
347
|
+
* @param k Number of results to return
|
|
348
|
+
* @param options Additional options
|
|
349
|
+
* @returns Array of search results
|
|
350
|
+
*/
|
|
351
|
+
searchText(query: string, k?: number, options?: {
|
|
352
|
+
nounTypes?: string[];
|
|
353
|
+
includeVerbs?: boolean;
|
|
354
|
+
searchMode?: 'local' | 'remote' | 'combined';
|
|
355
|
+
}): Promise<SearchResult<T>[]>;
|
|
356
|
+
/**
|
|
357
|
+
* Search a remote Brainy server for similar vectors
|
|
358
|
+
* @param queryVectorOrData Query vector or data to search for
|
|
359
|
+
* @param k Number of results to return
|
|
360
|
+
* @param options Additional options
|
|
361
|
+
* @returns Array of search results
|
|
362
|
+
*/
|
|
363
|
+
searchRemote(queryVectorOrData: Vector | any, k?: number, options?: {
|
|
364
|
+
forceEmbed?: boolean;
|
|
365
|
+
nounTypes?: string[];
|
|
366
|
+
includeVerbs?: boolean;
|
|
367
|
+
storeResults?: boolean;
|
|
368
|
+
}): Promise<SearchResult<T>[]>;
|
|
369
|
+
/**
|
|
370
|
+
* Search both local and remote Brainy instances, combining the results
|
|
371
|
+
* @param queryVectorOrData Query vector or data to search for
|
|
372
|
+
* @param k Number of results to return
|
|
373
|
+
* @param options Additional options
|
|
374
|
+
* @returns Array of search results
|
|
375
|
+
*/
|
|
376
|
+
searchCombined(queryVectorOrData: Vector | any, k?: number, options?: {
|
|
377
|
+
forceEmbed?: boolean;
|
|
378
|
+
nounTypes?: string[];
|
|
379
|
+
includeVerbs?: boolean;
|
|
380
|
+
localFirst?: boolean;
|
|
381
|
+
}): Promise<SearchResult<T>[]>;
|
|
382
|
+
/**
|
|
383
|
+
* Check if the instance is connected to a remote server
|
|
384
|
+
* @returns True if connected to a remote server, false otherwise
|
|
385
|
+
*/
|
|
386
|
+
isConnectedToRemoteServer(): boolean;
|
|
387
|
+
/**
|
|
388
|
+
* Disconnect from the remote server
|
|
389
|
+
* @returns True if successfully disconnected, false if not connected
|
|
390
|
+
*/
|
|
391
|
+
disconnectFromRemoteServer(): Promise<boolean>;
|
|
392
|
+
/**
|
|
393
|
+
* Ensure the database is initialized
|
|
394
|
+
*/
|
|
395
|
+
private ensureInitialized;
|
|
396
|
+
/**
|
|
397
|
+
* Get information about the current storage usage and capacity
|
|
398
|
+
* @returns Object containing the storage type, used space, quota, and additional details
|
|
399
|
+
*/
|
|
400
|
+
status(): Promise<{
|
|
401
|
+
type: string;
|
|
402
|
+
used: number;
|
|
403
|
+
quota: number | null;
|
|
404
|
+
details?: Record<string, any>;
|
|
405
|
+
}>;
|
|
406
|
+
/**
|
|
407
|
+
* Shut down the database and clean up resources
|
|
408
|
+
* This should be called when the database is no longer needed
|
|
409
|
+
*/
|
|
410
|
+
shutDown(): Promise<void>;
|
|
411
|
+
/**
|
|
412
|
+
* Backup all data from the database to a JSON-serializable format
|
|
413
|
+
* @returns Object containing all nouns, verbs, noun types, verb types, HNSW index, and other related data
|
|
414
|
+
*
|
|
415
|
+
* The HNSW index data includes:
|
|
416
|
+
* - entryPointId: The ID of the entry point for the graph
|
|
417
|
+
* - maxLevel: The maximum level in the hierarchical structure
|
|
418
|
+
* - dimension: The dimension of the vectors
|
|
419
|
+
* - config: Configuration parameters for the HNSW algorithm
|
|
420
|
+
* - connections: A serialized representation of the connections between nouns
|
|
421
|
+
*/
|
|
422
|
+
backup(): Promise<{
|
|
423
|
+
nouns: VectorDocument<T>[];
|
|
424
|
+
verbs: GraphVerb[];
|
|
425
|
+
nounTypes: string[];
|
|
426
|
+
verbTypes: string[];
|
|
427
|
+
version: string;
|
|
428
|
+
hnswIndex?: {
|
|
429
|
+
entryPointId: string | null;
|
|
430
|
+
maxLevel: number;
|
|
431
|
+
dimension: number | null;
|
|
432
|
+
config: HNSWConfig;
|
|
433
|
+
connections: Record<string, Record<string, string[]>>;
|
|
434
|
+
};
|
|
435
|
+
}>;
|
|
436
|
+
/**
|
|
437
|
+
* Import sparse data into the database
|
|
438
|
+
* @param data The sparse data to import
|
|
439
|
+
* If vectors are not present for nouns, they will be created using the embedding function
|
|
440
|
+
* @param options Import options
|
|
441
|
+
* @returns Object containing counts of imported items
|
|
442
|
+
*/
|
|
443
|
+
importSparseData(data: {
|
|
444
|
+
nouns: VectorDocument<T>[];
|
|
445
|
+
verbs: GraphVerb[];
|
|
446
|
+
nounTypes?: string[];
|
|
447
|
+
verbTypes?: string[];
|
|
448
|
+
hnswIndex?: {
|
|
449
|
+
entryPointId: string | null;
|
|
450
|
+
maxLevel: number;
|
|
451
|
+
dimension: number | null;
|
|
452
|
+
config: HNSWConfig;
|
|
453
|
+
connections: Record<string, Record<string, string[]>>;
|
|
454
|
+
};
|
|
455
|
+
version: string;
|
|
456
|
+
}, options?: {
|
|
457
|
+
clearExisting?: boolean;
|
|
458
|
+
}): Promise<{
|
|
459
|
+
nounsRestored: number;
|
|
460
|
+
verbsRestored: number;
|
|
461
|
+
}>;
|
|
462
|
+
/**
|
|
463
|
+
* Restore data into the database from a previously backed up format
|
|
464
|
+
* @param data The data to restore, in the format returned by backup()
|
|
465
|
+
* This can include HNSW index data if it was included in the backup
|
|
466
|
+
* If vectors are not present for nouns, they will be created using the embedding function
|
|
467
|
+
* @param options Restore options
|
|
468
|
+
* @returns Object containing counts of restored items
|
|
469
|
+
*/
|
|
470
|
+
restore(data: {
|
|
471
|
+
nouns: VectorDocument<T>[];
|
|
472
|
+
verbs: GraphVerb[];
|
|
473
|
+
nounTypes?: string[];
|
|
474
|
+
verbTypes?: string[];
|
|
475
|
+
hnswIndex?: {
|
|
476
|
+
entryPointId: string | null;
|
|
477
|
+
maxLevel: number;
|
|
478
|
+
dimension: number | null;
|
|
479
|
+
config: HNSWConfig;
|
|
480
|
+
connections: Record<string, Record<string, string[]>>;
|
|
481
|
+
};
|
|
482
|
+
version: string;
|
|
483
|
+
}, options?: {
|
|
484
|
+
clearExisting?: boolean;
|
|
485
|
+
}): Promise<{
|
|
486
|
+
nounsRestored: number;
|
|
487
|
+
verbsRestored: number;
|
|
488
|
+
}>;
|
|
489
|
+
/**
|
|
490
|
+
* Generate a random graph of data with typed nouns and verbs for testing and experimentation
|
|
491
|
+
* @param options Configuration options for the random graph
|
|
492
|
+
* @returns Object containing the IDs of the generated nouns and verbs
|
|
493
|
+
*/
|
|
494
|
+
generateRandomGraph(options?: {
|
|
495
|
+
nounCount?: number;
|
|
496
|
+
verbCount?: number;
|
|
497
|
+
nounTypes?: NounType[];
|
|
498
|
+
verbTypes?: VerbType[];
|
|
499
|
+
clearExisting?: boolean;
|
|
500
|
+
seed?: string;
|
|
501
|
+
}): Promise<{
|
|
502
|
+
nounIds: string[];
|
|
503
|
+
verbIds: string[];
|
|
504
|
+
}>;
|
|
505
|
+
}
|
|
506
|
+
export { euclideanDistance, cosineDistance, manhattanDistance, dotProductDistance } from './utils/index.js';
|
|
507
|
+
//# sourceMappingURL=brainyData.d.ts.map
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for the Soulcraft Brainy
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Vector representation - an array of numbers
|
|
6
|
+
*/
|
|
7
|
+
export type Vector = number[];
|
|
8
|
+
/**
|
|
9
|
+
* A document with a vector embedding and optional metadata
|
|
10
|
+
*/
|
|
11
|
+
export interface VectorDocument<T = any> {
|
|
12
|
+
id: string;
|
|
13
|
+
vector: Vector;
|
|
14
|
+
metadata?: T;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Search result with similarity score
|
|
18
|
+
*/
|
|
19
|
+
export interface SearchResult<T = any> {
|
|
20
|
+
id: string;
|
|
21
|
+
score: number;
|
|
22
|
+
vector: Vector;
|
|
23
|
+
metadata?: T;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Distance function for comparing vectors
|
|
27
|
+
*/
|
|
28
|
+
export type DistanceFunction = (a: Vector, b: Vector) => number;
|
|
29
|
+
/**
|
|
30
|
+
* Embedding function for converting data to vectors
|
|
31
|
+
*/
|
|
32
|
+
export type EmbeddingFunction = (data: any) => Promise<Vector>;
|
|
33
|
+
/**
|
|
34
|
+
* Embedding model interface
|
|
35
|
+
*/
|
|
36
|
+
export interface EmbeddingModel {
|
|
37
|
+
/**
|
|
38
|
+
* Initialize the embedding model
|
|
39
|
+
*/
|
|
40
|
+
init(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Embed data into a vector
|
|
43
|
+
*/
|
|
44
|
+
embed(data: any): Promise<Vector>;
|
|
45
|
+
/**
|
|
46
|
+
* Dispose of the model resources
|
|
47
|
+
*/
|
|
48
|
+
dispose(): Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* HNSW graph noun
|
|
52
|
+
*/
|
|
53
|
+
export interface HNSWNoun {
|
|
54
|
+
id: string;
|
|
55
|
+
vector: Vector;
|
|
56
|
+
connections: Map<number, Set<string>>;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Verb representing a relationship between nouns
|
|
60
|
+
* Extends HNSWNoun to allow verbs to be first-class entities in the data model
|
|
61
|
+
*/
|
|
62
|
+
export interface GraphVerb extends HNSWNoun {
|
|
63
|
+
sourceId: string;
|
|
64
|
+
targetId: string;
|
|
65
|
+
type?: string;
|
|
66
|
+
weight?: number;
|
|
67
|
+
metadata?: any;
|
|
68
|
+
source?: string;
|
|
69
|
+
target?: string;
|
|
70
|
+
verb?: string;
|
|
71
|
+
data?: Record<string, any>;
|
|
72
|
+
embedding?: Vector;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* HNSW index configuration
|
|
76
|
+
*/
|
|
77
|
+
export interface HNSWConfig {
|
|
78
|
+
M: number;
|
|
79
|
+
efConstruction: number;
|
|
80
|
+
efSearch: number;
|
|
81
|
+
ml: number;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Storage interface for persistence
|
|
85
|
+
*/
|
|
86
|
+
export interface StorageAdapter {
|
|
87
|
+
init(): Promise<void>;
|
|
88
|
+
saveNoun(noun: HNSWNoun): Promise<void>;
|
|
89
|
+
getNoun(id: string): Promise<HNSWNoun | null>;
|
|
90
|
+
getAllNouns(): Promise<HNSWNoun[]>;
|
|
91
|
+
/**
|
|
92
|
+
* Get nouns by noun type
|
|
93
|
+
* @param nounType The noun type to filter by
|
|
94
|
+
* @returns Promise that resolves to an array of nouns of the specified noun type
|
|
95
|
+
*/
|
|
96
|
+
getNounsByNounType(nounType: string): Promise<HNSWNoun[]>;
|
|
97
|
+
deleteNoun(id: string): Promise<void>;
|
|
98
|
+
saveVerb(verb: GraphVerb): Promise<void>;
|
|
99
|
+
getVerb(id: string): Promise<GraphVerb | null>;
|
|
100
|
+
getAllVerbs(): Promise<GraphVerb[]>;
|
|
101
|
+
getVerbsBySource(sourceId: string): Promise<GraphVerb[]>;
|
|
102
|
+
getVerbsByTarget(targetId: string): Promise<GraphVerb[]>;
|
|
103
|
+
getVerbsByType(type: string): Promise<GraphVerb[]>;
|
|
104
|
+
deleteVerb(id: string): Promise<void>;
|
|
105
|
+
saveMetadata(id: string, metadata: any): Promise<void>;
|
|
106
|
+
getMetadata(id: string): Promise<any | null>;
|
|
107
|
+
clear(): Promise<void>;
|
|
108
|
+
/**
|
|
109
|
+
* Get information about storage usage and capacity
|
|
110
|
+
* @returns Promise that resolves to an object containing storage status information
|
|
111
|
+
*/
|
|
112
|
+
getStorageStatus(): Promise<{
|
|
113
|
+
/**
|
|
114
|
+
* The type of storage being used (e.g., 'filesystem', 'opfs', 'memory')
|
|
115
|
+
*/
|
|
116
|
+
type: string;
|
|
117
|
+
/**
|
|
118
|
+
* The amount of storage being used in bytes
|
|
119
|
+
*/
|
|
120
|
+
used: number;
|
|
121
|
+
/**
|
|
122
|
+
* The total amount of storage available in bytes, or null if unknown
|
|
123
|
+
*/
|
|
124
|
+
quota: number | null;
|
|
125
|
+
/**
|
|
126
|
+
* Additional storage-specific information
|
|
127
|
+
*/
|
|
128
|
+
details?: Record<string, any>;
|
|
129
|
+
}>;
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=coreTypes.d.ts.map
|