@soulcraft/brainy 3.20.1 โ 3.20.3
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/CHANGELOG.md +19 -0
- package/bin/brainy-interactive.js +2 -2
- package/dist/brainy.d.ts +1 -1
- package/dist/mcp/brainyMCPAdapter.d.ts +1 -1
- package/dist/mcp/brainyMCPService.d.ts +1 -1
- package/dist/neural/embeddedPatterns.d.ts +1 -1
- package/dist/neural/embeddedPatterns.js +1 -1
- package/dist/shared/default-augmentations.d.ts +1 -1
- package/dist/types/{brainyDataInterface.js โ brainyInterface.js} +1 -1
- package/dist/vfs/VirtualFileSystem.d.ts +1 -0
- package/dist/vfs/VirtualFileSystem.js +108 -69
- package/package.json +1 -1
- package/dist/augmentationFactory.d.ts +0 -86
- package/dist/augmentationFactory.js +0 -342
- package/dist/augmentationRegistry.d.ts +0 -38
- package/dist/augmentationRegistry.js +0 -54
- package/dist/augmentationRegistryLoader.d.ts +0 -146
- package/dist/augmentationRegistryLoader.js +0 -213
- package/dist/augmentations/KnowledgeAugmentation.d.ts +0 -40
- package/dist/augmentations/KnowledgeAugmentation.js +0 -251
- package/dist/augmentations/intelligentVerbScoring.d.ts +0 -158
- package/dist/augmentations/intelligentVerbScoring.js +0 -377
- package/dist/augmentations/marketplace/AugmentationMarketplace.d.ts +0 -168
- package/dist/augmentations/marketplace/AugmentationMarketplace.js +0 -329
- package/dist/augmentations/marketplace/cli.d.ts +0 -47
- package/dist/augmentations/marketplace/cli.js +0 -265
- package/dist/augmentations/memoryAugmentations.d.ts +0 -72
- package/dist/augmentations/memoryAugmentations.js +0 -280
- package/dist/augmentations/serverSearchAugmentations.d.ts +0 -190
- package/dist/augmentations/serverSearchAugmentations.js +0 -586
- package/dist/brainy-unified.d.ts +0 -106
- package/dist/brainy-unified.js +0 -327
- package/dist/brainyData.d.ts +0 -1832
- package/dist/brainyData.js +0 -6443
- package/dist/brainyDataV3.d.ts +0 -186
- package/dist/brainyDataV3.js +0 -337
- package/dist/config/distributedPresets-new.d.ts +0 -118
- package/dist/config/distributedPresets-new.js +0 -318
- package/dist/config/modelPrecisionManager.d.ts +0 -42
- package/dist/config/modelPrecisionManager.js +0 -98
- package/dist/connectors/interfaces/IConnector.d.ts +0 -143
- package/dist/connectors/interfaces/IConnector.js +0 -8
- package/dist/demo.d.ts +0 -106
- package/dist/demo.js +0 -201
- package/dist/embeddings/SingletonModelManager.d.ts +0 -95
- package/dist/embeddings/SingletonModelManager.js +0 -220
- package/dist/embeddings/lightweight-embedder.d.ts +0 -22
- package/dist/embeddings/lightweight-embedder.js +0 -128
- package/dist/embeddings/model-manager.d.ts +0 -39
- package/dist/embeddings/model-manager.js +0 -245
- package/dist/embeddings/universal-memory-manager.d.ts +0 -38
- package/dist/embeddings/universal-memory-manager.js +0 -166
- package/dist/embeddings/worker-embedding.d.ts +0 -7
- package/dist/embeddings/worker-embedding.js +0 -73
- package/dist/embeddings/worker-manager.d.ts +0 -28
- package/dist/embeddings/worker-manager.js +0 -162
- package/dist/examples/basicUsage.d.ts +0 -4
- package/dist/examples/basicUsage.js +0 -121
- package/dist/indices/fieldIndex.d.ts +0 -76
- package/dist/indices/fieldIndex.js +0 -357
- package/dist/mcp/brainyMCPBroadcast.d.ts +0 -82
- package/dist/mcp/brainyMCPBroadcast.js +0 -303
- package/dist/mcp/brainyMCPClient.d.ts +0 -92
- package/dist/mcp/brainyMCPClient.js +0 -258
- package/dist/scripts/precomputePatternEmbeddings.d.ts +0 -19
- package/dist/scripts/precomputePatternEmbeddings.js +0 -100
- package/dist/utils/cacheAutoConfig.d.ts +0 -63
- package/dist/utils/cacheAutoConfig.js +0 -261
- package/dist/utils/hybridModelManager.d.ts +0 -64
- package/dist/utils/hybridModelManager.js +0 -95
- package/dist/utils/statistics.d.ts +0 -28
- package/dist/utils/statistics.js +0 -25
- package/dist/vfs/ConceptSystem.d.ts +0 -203
- package/dist/vfs/ConceptSystem.js +0 -545
- package/dist/vfs/EntityManager.d.ts +0 -75
- package/dist/vfs/EntityManager.js +0 -216
- package/dist/vfs/EventRecorder.d.ts +0 -84
- package/dist/vfs/EventRecorder.js +0 -269
- package/dist/vfs/GitBridge.d.ts +0 -167
- package/dist/vfs/GitBridge.js +0 -537
- package/dist/vfs/KnowledgeAugmentation.d.ts +0 -104
- package/dist/vfs/KnowledgeAugmentation.js +0 -146
- package/dist/vfs/KnowledgeLayer.d.ts +0 -35
- package/dist/vfs/KnowledgeLayer.js +0 -443
- package/dist/vfs/PersistentEntitySystem.d.ts +0 -165
- package/dist/vfs/PersistentEntitySystem.js +0 -503
- package/dist/vfs/SemanticVersioning.d.ts +0 -105
- package/dist/vfs/SemanticVersioning.js +0 -309
- package/dist/vfs/VFSHealthCheck.d.ts +0 -78
- package/dist/vfs/VFSHealthCheck.js +0 -299
- /package/dist/types/{brainyDataInterface.d.ts โ brainyInterface.d.ts} +0 -0
package/dist/demo.d.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Demo-specific entry point for browser environments
|
|
3
|
-
* This excludes all Node.js-specific functionality to avoid import issues
|
|
4
|
-
*/
|
|
5
|
-
import { MemoryStorage } from './storage/adapters/memoryStorage.js';
|
|
6
|
-
import { OPFSStorage } from './storage/adapters/opfsStorage.js';
|
|
7
|
-
export interface Vector extends Array<number> {
|
|
8
|
-
}
|
|
9
|
-
export interface SearchResult {
|
|
10
|
-
id: string;
|
|
11
|
-
score: number;
|
|
12
|
-
metadata: any;
|
|
13
|
-
text?: string;
|
|
14
|
-
}
|
|
15
|
-
export interface VerbData {
|
|
16
|
-
id: string;
|
|
17
|
-
source: string;
|
|
18
|
-
target: string;
|
|
19
|
-
verb: string;
|
|
20
|
-
metadata: any;
|
|
21
|
-
timestamp: number;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Simplified Brainy class for demo purposes
|
|
25
|
-
* Only includes browser-compatible functionality
|
|
26
|
-
*/
|
|
27
|
-
export declare class DemoBrainy {
|
|
28
|
-
private storage;
|
|
29
|
-
private embedder;
|
|
30
|
-
private initialized;
|
|
31
|
-
private vectors;
|
|
32
|
-
private metadata;
|
|
33
|
-
private verbs;
|
|
34
|
-
constructor();
|
|
35
|
-
/**
|
|
36
|
-
* Initialize the database
|
|
37
|
-
*/
|
|
38
|
-
init(): Promise<void>;
|
|
39
|
-
/**
|
|
40
|
-
* Add a document to the database
|
|
41
|
-
*/
|
|
42
|
-
add(text: string, metadata?: any): Promise<string>;
|
|
43
|
-
/**
|
|
44
|
-
* Search for similar documents
|
|
45
|
-
*/
|
|
46
|
-
searchText(query: string, limit?: number): Promise<SearchResult[]>;
|
|
47
|
-
/**
|
|
48
|
-
* Add a relationship between two documents
|
|
49
|
-
*/
|
|
50
|
-
addVerb(sourceId: string, targetId: string, verb: string, metadata?: any): Promise<string>;
|
|
51
|
-
/**
|
|
52
|
-
* Get relationships from a source document
|
|
53
|
-
*/
|
|
54
|
-
getVerbsBySource(sourceId: string): Promise<VerbData[]>;
|
|
55
|
-
/**
|
|
56
|
-
* Get a document by ID
|
|
57
|
-
*/
|
|
58
|
-
get(id: string): Promise<any | null>;
|
|
59
|
-
/**
|
|
60
|
-
* Delete a document
|
|
61
|
-
*/
|
|
62
|
-
delete(id: string): Promise<boolean>;
|
|
63
|
-
/**
|
|
64
|
-
* Update document metadata
|
|
65
|
-
*/
|
|
66
|
-
updateMetadata(id: string, newMetadata: any): Promise<boolean>;
|
|
67
|
-
/**
|
|
68
|
-
* Get the number of documents
|
|
69
|
-
*/
|
|
70
|
-
size(): number;
|
|
71
|
-
/**
|
|
72
|
-
* Generate a random ID
|
|
73
|
-
*/
|
|
74
|
-
private generateId;
|
|
75
|
-
/**
|
|
76
|
-
* Get storage info
|
|
77
|
-
*/
|
|
78
|
-
getStorage(): MemoryStorage | OPFSStorage;
|
|
79
|
-
}
|
|
80
|
-
export declare const NounType: {
|
|
81
|
-
readonly Person: "Person";
|
|
82
|
-
readonly Organization: "Organization";
|
|
83
|
-
readonly Location: "Location";
|
|
84
|
-
readonly Thing: "Thing";
|
|
85
|
-
readonly Concept: "Concept";
|
|
86
|
-
readonly Event: "Event";
|
|
87
|
-
readonly Document: "Document";
|
|
88
|
-
readonly Media: "Media";
|
|
89
|
-
readonly File: "File";
|
|
90
|
-
readonly Message: "Message";
|
|
91
|
-
readonly Content: "Content";
|
|
92
|
-
};
|
|
93
|
-
export declare const VerbType: {
|
|
94
|
-
readonly RelatedTo: "related_to";
|
|
95
|
-
readonly Contains: "contains";
|
|
96
|
-
readonly PartOf: "part_of";
|
|
97
|
-
readonly LocatedAt: "located_at";
|
|
98
|
-
readonly References: "references";
|
|
99
|
-
readonly Owns: "owns";
|
|
100
|
-
readonly CreatedBy: "created_by";
|
|
101
|
-
readonly BelongsTo: "belongs_to";
|
|
102
|
-
readonly Likes: "likes";
|
|
103
|
-
readonly Follows: "follows";
|
|
104
|
-
};
|
|
105
|
-
export { DemoBrainy as Brainy };
|
|
106
|
-
export default DemoBrainy;
|
package/dist/demo.js
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Demo-specific entry point for browser environments
|
|
3
|
-
* This excludes all Node.js-specific functionality to avoid import issues
|
|
4
|
-
*/
|
|
5
|
-
// Import only browser-compatible modules
|
|
6
|
-
import { MemoryStorage } from './storage/adapters/memoryStorage.js';
|
|
7
|
-
import { TransformerEmbedding } from './utils/embedding.js';
|
|
8
|
-
import { cosineDistance } from './utils/distance.js';
|
|
9
|
-
/**
|
|
10
|
-
* Simplified Brainy class for demo purposes
|
|
11
|
-
* Only includes browser-compatible functionality
|
|
12
|
-
*/
|
|
13
|
-
export class DemoBrainy {
|
|
14
|
-
constructor() {
|
|
15
|
-
this.embedder = null;
|
|
16
|
-
this.initialized = false;
|
|
17
|
-
this.vectors = new Map();
|
|
18
|
-
this.metadata = new Map();
|
|
19
|
-
this.verbs = new Map();
|
|
20
|
-
// Always use memory storage for demo simplicity
|
|
21
|
-
this.storage = new MemoryStorage();
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Initialize the database
|
|
25
|
-
*/
|
|
26
|
-
async init() {
|
|
27
|
-
if (this.initialized)
|
|
28
|
-
return;
|
|
29
|
-
try {
|
|
30
|
-
await this.storage.init();
|
|
31
|
-
// Initialize the embedder
|
|
32
|
-
this.embedder = new TransformerEmbedding({ verbose: false });
|
|
33
|
-
await this.embedder.init();
|
|
34
|
-
this.initialized = true;
|
|
35
|
-
console.log('โ
Demo Brainy initialized successfully');
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
console.error('Failed to initialize demo Brainy:', error);
|
|
39
|
-
throw error;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Add a document to the database
|
|
44
|
-
*/
|
|
45
|
-
async add(text, metadata = {}) {
|
|
46
|
-
if (!this.initialized || !this.embedder) {
|
|
47
|
-
throw new Error('Database not initialized');
|
|
48
|
-
}
|
|
49
|
-
const id = this.generateId();
|
|
50
|
-
try {
|
|
51
|
-
// Generate embedding
|
|
52
|
-
const vector = await this.embedder.embed(text);
|
|
53
|
-
// Store data
|
|
54
|
-
this.vectors.set(id, vector);
|
|
55
|
-
this.metadata.set(id, { text, ...metadata, timestamp: Date.now() });
|
|
56
|
-
return id;
|
|
57
|
-
}
|
|
58
|
-
catch (error) {
|
|
59
|
-
console.error('Failed to add document:', error);
|
|
60
|
-
throw error;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Search for similar documents
|
|
65
|
-
*/
|
|
66
|
-
async searchText(query, limit = 10) {
|
|
67
|
-
if (!this.initialized || !this.embedder) {
|
|
68
|
-
throw new Error('Database not initialized');
|
|
69
|
-
}
|
|
70
|
-
try {
|
|
71
|
-
// Generate query embedding
|
|
72
|
-
const queryVector = await this.embedder.embed(query);
|
|
73
|
-
// Calculate similarities
|
|
74
|
-
const results = [];
|
|
75
|
-
for (const [id, vector] of this.vectors.entries()) {
|
|
76
|
-
const score = 1 - cosineDistance(queryVector, vector); // Convert distance to similarity
|
|
77
|
-
const metadata = this.metadata.get(id);
|
|
78
|
-
results.push({
|
|
79
|
-
id,
|
|
80
|
-
score,
|
|
81
|
-
metadata,
|
|
82
|
-
text: metadata?.text
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
// Sort by score (highest first) and limit
|
|
86
|
-
return results
|
|
87
|
-
.sort((a, b) => b.score - a.score)
|
|
88
|
-
.slice(0, limit);
|
|
89
|
-
}
|
|
90
|
-
catch (error) {
|
|
91
|
-
console.error('Search failed:', error);
|
|
92
|
-
throw error;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Add a relationship between two documents
|
|
97
|
-
*/
|
|
98
|
-
async addVerb(sourceId, targetId, verb, metadata = {}) {
|
|
99
|
-
const verbId = this.generateId();
|
|
100
|
-
const verbData = {
|
|
101
|
-
id: verbId,
|
|
102
|
-
source: sourceId,
|
|
103
|
-
target: targetId,
|
|
104
|
-
verb,
|
|
105
|
-
metadata,
|
|
106
|
-
timestamp: Date.now()
|
|
107
|
-
};
|
|
108
|
-
if (!this.verbs.has(sourceId)) {
|
|
109
|
-
this.verbs.set(sourceId, []);
|
|
110
|
-
}
|
|
111
|
-
this.verbs.get(sourceId).push(verbData);
|
|
112
|
-
return verbId;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Get relationships from a source document
|
|
116
|
-
*/
|
|
117
|
-
async getVerbsBySource(sourceId) {
|
|
118
|
-
return this.verbs.get(sourceId) || [];
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Get a document by ID
|
|
122
|
-
*/
|
|
123
|
-
async get(id) {
|
|
124
|
-
const metadata = this.metadata.get(id);
|
|
125
|
-
const vector = this.vectors.get(id);
|
|
126
|
-
if (!metadata || !vector)
|
|
127
|
-
return null;
|
|
128
|
-
return {
|
|
129
|
-
id,
|
|
130
|
-
vector,
|
|
131
|
-
...metadata
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Delete a document
|
|
136
|
-
*/
|
|
137
|
-
async delete(id) {
|
|
138
|
-
const deleted = this.vectors.delete(id) && this.metadata.delete(id);
|
|
139
|
-
this.verbs.delete(id);
|
|
140
|
-
return deleted;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Update document metadata
|
|
144
|
-
*/
|
|
145
|
-
async updateMetadata(id, newMetadata) {
|
|
146
|
-
const metadata = this.metadata.get(id);
|
|
147
|
-
if (!metadata)
|
|
148
|
-
return false;
|
|
149
|
-
this.metadata.set(id, { ...metadata, ...newMetadata });
|
|
150
|
-
return true;
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Get the number of documents
|
|
154
|
-
*/
|
|
155
|
-
size() {
|
|
156
|
-
return this.vectors.size;
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Generate a random ID
|
|
160
|
-
*/
|
|
161
|
-
generateId() {
|
|
162
|
-
return 'id-' + Math.random().toString(36).substr(2, 9) + '-' + Date.now();
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Get storage info
|
|
166
|
-
*/
|
|
167
|
-
getStorage() {
|
|
168
|
-
return this.storage;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
// Export noun and verb types for compatibility
|
|
172
|
-
export const NounType = {
|
|
173
|
-
Person: 'Person',
|
|
174
|
-
Organization: 'Organization',
|
|
175
|
-
Location: 'Location',
|
|
176
|
-
Thing: 'Thing',
|
|
177
|
-
Concept: 'Concept',
|
|
178
|
-
Event: 'Event',
|
|
179
|
-
Document: 'Document',
|
|
180
|
-
Media: 'Media',
|
|
181
|
-
File: 'File',
|
|
182
|
-
Message: 'Message',
|
|
183
|
-
Content: 'Content'
|
|
184
|
-
};
|
|
185
|
-
export const VerbType = {
|
|
186
|
-
RelatedTo: 'related_to',
|
|
187
|
-
Contains: 'contains',
|
|
188
|
-
PartOf: 'part_of',
|
|
189
|
-
LocatedAt: 'located_at',
|
|
190
|
-
References: 'references',
|
|
191
|
-
Owns: 'owns',
|
|
192
|
-
CreatedBy: 'created_by',
|
|
193
|
-
BelongsTo: 'belongs_to',
|
|
194
|
-
Likes: 'likes',
|
|
195
|
-
Follows: 'follows'
|
|
196
|
-
};
|
|
197
|
-
// Export the main class as Brainy for compatibility
|
|
198
|
-
export { DemoBrainy as Brainy };
|
|
199
|
-
// Default export
|
|
200
|
-
export default DemoBrainy;
|
|
201
|
-
//# sourceMappingURL=demo.js.map
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Singleton Model Manager - THE ONLY SOURCE OF EMBEDDING MODELS
|
|
3
|
-
*
|
|
4
|
-
* This is the SINGLE, UNIFIED model initialization system that ensures:
|
|
5
|
-
* - Only ONE model instance exists across the entire system
|
|
6
|
-
* - Precision is configured once and locked
|
|
7
|
-
* - All components share the same model
|
|
8
|
-
* - No possibility of mixed precisions
|
|
9
|
-
*
|
|
10
|
-
* CRITICAL: This manager is used by EVERYTHING:
|
|
11
|
-
* - Storage operations (add, update)
|
|
12
|
-
* - Search operations (search, find)
|
|
13
|
-
* - Public API (embed, cluster)
|
|
14
|
-
* - Neural API (all neural.* methods)
|
|
15
|
-
* - Internal operations (deduplication, indexing)
|
|
16
|
-
*/
|
|
17
|
-
import { TransformerEmbedding } from '../utils/embedding.js';
|
|
18
|
-
import { EmbeddingFunction, Vector } from '../coreTypes.js';
|
|
19
|
-
/**
|
|
20
|
-
* Statistics for monitoring
|
|
21
|
-
*/
|
|
22
|
-
interface ModelStats {
|
|
23
|
-
initialized: boolean;
|
|
24
|
-
precision: string;
|
|
25
|
-
initCount: number;
|
|
26
|
-
embedCount: number;
|
|
27
|
-
lastUsed: Date | null;
|
|
28
|
-
memoryFootprint?: number;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* The ONE TRUE model manager
|
|
32
|
-
*/
|
|
33
|
-
export declare class SingletonModelManager {
|
|
34
|
-
private static instance;
|
|
35
|
-
private stats;
|
|
36
|
-
private constructor();
|
|
37
|
-
/**
|
|
38
|
-
* Get the singleton instance
|
|
39
|
-
*/
|
|
40
|
-
static getInstance(): SingletonModelManager;
|
|
41
|
-
/**
|
|
42
|
-
* Get the model instance - creates if needed, reuses if exists
|
|
43
|
-
* This is THE ONLY way to get a model in the entire system
|
|
44
|
-
*/
|
|
45
|
-
getModel(): Promise<TransformerEmbedding>;
|
|
46
|
-
/**
|
|
47
|
-
* Initialize the model - happens exactly once
|
|
48
|
-
*/
|
|
49
|
-
private initializeModel;
|
|
50
|
-
/**
|
|
51
|
-
* Get embedding function that uses the singleton model
|
|
52
|
-
*/
|
|
53
|
-
getEmbeddingFunction(): Promise<EmbeddingFunction>;
|
|
54
|
-
/**
|
|
55
|
-
* Direct embed method for convenience
|
|
56
|
-
*/
|
|
57
|
-
embed(data: string | string[]): Promise<Vector>;
|
|
58
|
-
/**
|
|
59
|
-
* Check if model is initialized
|
|
60
|
-
*/
|
|
61
|
-
isInitialized(): boolean;
|
|
62
|
-
/**
|
|
63
|
-
* Get current statistics
|
|
64
|
-
*/
|
|
65
|
-
getStats(): ModelStats;
|
|
66
|
-
/**
|
|
67
|
-
* Validate precision consistency
|
|
68
|
-
* Throws error if attempting to use different precision
|
|
69
|
-
*/
|
|
70
|
-
validatePrecision(requestedPrecision?: string): void;
|
|
71
|
-
/**
|
|
72
|
-
* Force cleanup (for testing only)
|
|
73
|
-
* WARNING: This will break consistency - use only in tests
|
|
74
|
-
*/
|
|
75
|
-
_testOnlyCleanup(): Promise<void>;
|
|
76
|
-
}
|
|
77
|
-
export declare const singletonModelManager: SingletonModelManager;
|
|
78
|
-
/**
|
|
79
|
-
* THE ONLY embedding function that should be used anywhere
|
|
80
|
-
* This ensures all operations use the same model instance
|
|
81
|
-
*/
|
|
82
|
-
export declare function getUnifiedEmbeddingFunction(): Promise<EmbeddingFunction>;
|
|
83
|
-
/**
|
|
84
|
-
* Direct embed function for convenience
|
|
85
|
-
*/
|
|
86
|
-
export declare function unifiedEmbed(data: string | string[]): Promise<Vector>;
|
|
87
|
-
/**
|
|
88
|
-
* Check if model is ready
|
|
89
|
-
*/
|
|
90
|
-
export declare function isModelReady(): boolean;
|
|
91
|
-
/**
|
|
92
|
-
* Get model statistics
|
|
93
|
-
*/
|
|
94
|
-
export declare function getModelStats(): ModelStats;
|
|
95
|
-
export {};
|
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Singleton Model Manager - THE ONLY SOURCE OF EMBEDDING MODELS
|
|
3
|
-
*
|
|
4
|
-
* This is the SINGLE, UNIFIED model initialization system that ensures:
|
|
5
|
-
* - Only ONE model instance exists across the entire system
|
|
6
|
-
* - Precision is configured once and locked
|
|
7
|
-
* - All components share the same model
|
|
8
|
-
* - No possibility of mixed precisions
|
|
9
|
-
*
|
|
10
|
-
* CRITICAL: This manager is used by EVERYTHING:
|
|
11
|
-
* - Storage operations (add, update)
|
|
12
|
-
* - Search operations (search, find)
|
|
13
|
-
* - Public API (embed, cluster)
|
|
14
|
-
* - Neural API (all neural.* methods)
|
|
15
|
-
* - Internal operations (deduplication, indexing)
|
|
16
|
-
*/
|
|
17
|
-
import { TransformerEmbedding } from '../utils/embedding.js';
|
|
18
|
-
import { getModelPrecision, lockModelPrecision } from '../config/modelPrecisionManager.js';
|
|
19
|
-
// Global state - ensures true singleton across entire process
|
|
20
|
-
let globalModelInstance = null;
|
|
21
|
-
let globalInitPromise = null;
|
|
22
|
-
let globalInitialized = false;
|
|
23
|
-
/**
|
|
24
|
-
* The ONE TRUE model manager
|
|
25
|
-
*/
|
|
26
|
-
export class SingletonModelManager {
|
|
27
|
-
constructor() {
|
|
28
|
-
this.stats = {
|
|
29
|
-
initialized: false,
|
|
30
|
-
precision: 'unknown',
|
|
31
|
-
initCount: 0,
|
|
32
|
-
embedCount: 0,
|
|
33
|
-
lastUsed: null
|
|
34
|
-
};
|
|
35
|
-
// Private constructor enforces singleton
|
|
36
|
-
this.stats.precision = getModelPrecision();
|
|
37
|
-
console.log(`๐ SingletonModelManager initialized with ${this.stats.precision.toUpperCase()} precision`);
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Get the singleton instance
|
|
41
|
-
*/
|
|
42
|
-
static getInstance() {
|
|
43
|
-
if (!SingletonModelManager.instance) {
|
|
44
|
-
SingletonModelManager.instance = new SingletonModelManager();
|
|
45
|
-
}
|
|
46
|
-
return SingletonModelManager.instance;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Get the model instance - creates if needed, reuses if exists
|
|
50
|
-
* This is THE ONLY way to get a model in the entire system
|
|
51
|
-
*/
|
|
52
|
-
async getModel() {
|
|
53
|
-
// If already initialized, return immediately
|
|
54
|
-
if (globalModelInstance && globalInitialized) {
|
|
55
|
-
this.stats.lastUsed = new Date();
|
|
56
|
-
return globalModelInstance;
|
|
57
|
-
}
|
|
58
|
-
// If initialization is in progress, wait for it
|
|
59
|
-
if (globalInitPromise) {
|
|
60
|
-
console.log('โณ Model initialization already in progress, waiting...');
|
|
61
|
-
return await globalInitPromise;
|
|
62
|
-
}
|
|
63
|
-
// Start initialization (only happens once ever)
|
|
64
|
-
globalInitPromise = this.initializeModel();
|
|
65
|
-
try {
|
|
66
|
-
const model = await globalInitPromise;
|
|
67
|
-
globalInitialized = true;
|
|
68
|
-
return model;
|
|
69
|
-
}
|
|
70
|
-
catch (error) {
|
|
71
|
-
// Reset on error to allow retry
|
|
72
|
-
globalInitPromise = null;
|
|
73
|
-
throw error;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Initialize the model - happens exactly once
|
|
78
|
-
*/
|
|
79
|
-
async initializeModel() {
|
|
80
|
-
console.log('๐ Initializing singleton model instance...');
|
|
81
|
-
// Get precision from central manager
|
|
82
|
-
const precision = getModelPrecision();
|
|
83
|
-
console.log(`๐ Using ${precision.toUpperCase()} precision (${precision === 'q8' ? '23MB, 99% accuracy' : '90MB, 100% accuracy'})`);
|
|
84
|
-
// Detect environment for optimal settings
|
|
85
|
-
const isNode = typeof process !== 'undefined' && process.versions?.node;
|
|
86
|
-
const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
87
|
-
const isServerless = typeof process !== 'undefined' && (process.env.VERCEL ||
|
|
88
|
-
process.env.NETLIFY ||
|
|
89
|
-
process.env.AWS_LAMBDA_FUNCTION_NAME ||
|
|
90
|
-
process.env.FUNCTIONS_WORKER_RUNTIME);
|
|
91
|
-
const isTest = globalThis.__BRAINY_TEST_ENV__ || process.env.NODE_ENV === 'test';
|
|
92
|
-
// Create optimized options based on environment
|
|
93
|
-
const options = {
|
|
94
|
-
precision: precision,
|
|
95
|
-
verbose: !isTest && !isServerless && !isBrowser,
|
|
96
|
-
device: 'cpu', // CPU is most compatible
|
|
97
|
-
localFilesOnly: process.env.BRAINY_ALLOW_REMOTE_MODELS === 'false',
|
|
98
|
-
model: 'Xenova/all-MiniLM-L6-v2'
|
|
99
|
-
};
|
|
100
|
-
try {
|
|
101
|
-
// Create the ONE model instance
|
|
102
|
-
globalModelInstance = new TransformerEmbedding(options);
|
|
103
|
-
// Initialize it
|
|
104
|
-
await globalModelInstance.init();
|
|
105
|
-
// CRITICAL: Lock the precision after successful initialization
|
|
106
|
-
// This prevents any future changes to precision
|
|
107
|
-
lockModelPrecision();
|
|
108
|
-
console.log('๐ Model precision locked at:', precision.toUpperCase());
|
|
109
|
-
// Update stats
|
|
110
|
-
this.stats.initialized = true;
|
|
111
|
-
this.stats.initCount++;
|
|
112
|
-
this.stats.lastUsed = new Date();
|
|
113
|
-
// Log memory usage if available
|
|
114
|
-
if (isNode && process.memoryUsage) {
|
|
115
|
-
const usage = process.memoryUsage();
|
|
116
|
-
this.stats.memoryFootprint = Math.round(usage.heapUsed / 1024 / 1024);
|
|
117
|
-
console.log(`๐พ Model loaded, memory usage: ${this.stats.memoryFootprint}MB`);
|
|
118
|
-
}
|
|
119
|
-
console.log('โ
Singleton model initialized successfully');
|
|
120
|
-
return globalModelInstance;
|
|
121
|
-
}
|
|
122
|
-
catch (error) {
|
|
123
|
-
console.error('โ Failed to initialize singleton model:', error);
|
|
124
|
-
globalModelInstance = null;
|
|
125
|
-
throw new Error(`Singleton model initialization failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Get embedding function that uses the singleton model
|
|
130
|
-
*/
|
|
131
|
-
async getEmbeddingFunction() {
|
|
132
|
-
const model = await this.getModel();
|
|
133
|
-
return async (data) => {
|
|
134
|
-
this.stats.embedCount++;
|
|
135
|
-
this.stats.lastUsed = new Date();
|
|
136
|
-
return await model.embed(data);
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Direct embed method for convenience
|
|
141
|
-
*/
|
|
142
|
-
async embed(data) {
|
|
143
|
-
const model = await this.getModel();
|
|
144
|
-
this.stats.embedCount++;
|
|
145
|
-
this.stats.lastUsed = new Date();
|
|
146
|
-
return await model.embed(data);
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Check if model is initialized
|
|
150
|
-
*/
|
|
151
|
-
isInitialized() {
|
|
152
|
-
return globalInitialized && globalModelInstance !== null;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Get current statistics
|
|
156
|
-
*/
|
|
157
|
-
getStats() {
|
|
158
|
-
return {
|
|
159
|
-
...this.stats,
|
|
160
|
-
precision: getModelPrecision()
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Validate precision consistency
|
|
165
|
-
* Throws error if attempting to use different precision
|
|
166
|
-
*/
|
|
167
|
-
validatePrecision(requestedPrecision) {
|
|
168
|
-
const currentPrecision = getModelPrecision();
|
|
169
|
-
if (requestedPrecision && requestedPrecision !== currentPrecision) {
|
|
170
|
-
throw new Error(`โ Precision mismatch! System is using ${currentPrecision.toUpperCase()} ` +
|
|
171
|
-
`but ${requestedPrecision.toUpperCase()} was requested. ` +
|
|
172
|
-
`All operations must use the same precision.`);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Force cleanup (for testing only)
|
|
177
|
-
* WARNING: This will break consistency - use only in tests
|
|
178
|
-
*/
|
|
179
|
-
async _testOnlyCleanup() {
|
|
180
|
-
if (process.env.NODE_ENV !== 'test') {
|
|
181
|
-
throw new Error('Cleanup only allowed in test environment');
|
|
182
|
-
}
|
|
183
|
-
if (globalModelInstance && 'dispose' in globalModelInstance) {
|
|
184
|
-
await globalModelInstance.dispose();
|
|
185
|
-
}
|
|
186
|
-
globalModelInstance = null;
|
|
187
|
-
globalInitPromise = null;
|
|
188
|
-
globalInitialized = false;
|
|
189
|
-
this.stats.initialized = false;
|
|
190
|
-
console.log('๐งน Singleton model cleaned up (test only)');
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
// Export the singleton instance getter
|
|
194
|
-
export const singletonModelManager = SingletonModelManager.getInstance();
|
|
195
|
-
/**
|
|
196
|
-
* THE ONLY embedding function that should be used anywhere
|
|
197
|
-
* This ensures all operations use the same model instance
|
|
198
|
-
*/
|
|
199
|
-
export async function getUnifiedEmbeddingFunction() {
|
|
200
|
-
return await singletonModelManager.getEmbeddingFunction();
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Direct embed function for convenience
|
|
204
|
-
*/
|
|
205
|
-
export async function unifiedEmbed(data) {
|
|
206
|
-
return await singletonModelManager.embed(data);
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Check if model is ready
|
|
210
|
-
*/
|
|
211
|
-
export function isModelReady() {
|
|
212
|
-
return singletonModelManager.isInitialized();
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Get model statistics
|
|
216
|
-
*/
|
|
217
|
-
export function getModelStats() {
|
|
218
|
-
return singletonModelManager.getStats();
|
|
219
|
-
}
|
|
220
|
-
//# sourceMappingURL=SingletonModelManager.js.map
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Lightweight Embedding Alternative
|
|
3
|
-
*
|
|
4
|
-
* Uses pre-computed embeddings for common terms
|
|
5
|
-
* Falls back to ONNX for unknown terms
|
|
6
|
-
*
|
|
7
|
-
* This reduces memory usage by 90% for typical queries
|
|
8
|
-
*/
|
|
9
|
-
import { Vector } from '../coreTypes.js';
|
|
10
|
-
export declare class LightweightEmbedder {
|
|
11
|
-
private stats;
|
|
12
|
-
embed(text: string | string[]): Promise<Vector | Vector[]>;
|
|
13
|
-
private embedSingle;
|
|
14
|
-
getStats(): {
|
|
15
|
-
totalEmbeddings: number;
|
|
16
|
-
cacheHitRate: number;
|
|
17
|
-
precomputedHits: number;
|
|
18
|
-
simpleComputes: number;
|
|
19
|
-
onnxComputes: number;
|
|
20
|
-
};
|
|
21
|
-
loadPrecomputed(filePath?: string): Promise<void>;
|
|
22
|
-
}
|