@soulcraft/brainy 6.3.0 → 6.3.2
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 +28 -0
- package/dist/brainy.d.ts +55 -0
- package/dist/brainy.js +86 -0
- package/dist/versioning/VersionIndex.d.ts +42 -47
- package/dist/versioning/VersionIndex.js +141 -166
- package/dist/versioning/VersionManager.d.ts +26 -6
- package/dist/versioning/VersionManager.js +101 -8
- package/dist/versioning/VersionStorage.d.ts +25 -15
- package/dist/versioning/VersionStorage.js +49 -65
- package/package.json +1 -1
- package/dist/augmentations/KnowledgeAugmentation.d.ts +0 -40
- package/dist/augmentations/KnowledgeAugmentation.js +0 -251
- package/dist/importManager.d.ts +0 -78
- package/dist/importManager.js +0 -267
- package/dist/query/typeInference.d.ts +0 -158
- package/dist/query/typeInference.js +0 -760
- package/dist/storage/adapters/typeAwareStorageAdapter.d.ts +0 -252
- package/dist/storage/adapters/typeAwareStorageAdapter.js +0 -814
- package/dist/types/brainyDataInterface.d.ts +0 -52
- package/dist/types/brainyDataInterface.js +0 -10
- 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/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
|
@@ -1,309 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Semantic Versioning System for VFS
|
|
3
|
-
*
|
|
4
|
-
* Only creates versions when the MEANING of content changes significantly
|
|
5
|
-
* PRODUCTION-READY: Real implementation using embeddings
|
|
6
|
-
*/
|
|
7
|
-
import { NounType, VerbType } from '../types/graphTypes.js';
|
|
8
|
-
import { cosineDistance } from '../utils/distance.js';
|
|
9
|
-
import { createHash } from 'crypto';
|
|
10
|
-
import { v4 as uuidv4 } from '../universal/uuid.js';
|
|
11
|
-
import { EntityManager } from './EntityManager.js';
|
|
12
|
-
/**
|
|
13
|
-
* Semantic Versioning System
|
|
14
|
-
*
|
|
15
|
-
* Creates versions only when content meaning changes significantly
|
|
16
|
-
* Uses vector embeddings to detect semantic changes
|
|
17
|
-
*/
|
|
18
|
-
export class SemanticVersioning extends EntityManager {
|
|
19
|
-
constructor(brain, config) {
|
|
20
|
-
super(brain, 'vfs-version');
|
|
21
|
-
this.versionCache = new Map();
|
|
22
|
-
this.config = {
|
|
23
|
-
threshold: config?.threshold ?? 0.3,
|
|
24
|
-
maxVersions: config?.maxVersions ?? 10,
|
|
25
|
-
minInterval: config?.minInterval ?? 60000, // 1 minute
|
|
26
|
-
sizeChangeThreshold: config?.sizeChangeThreshold ?? 0.5
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Check if content has changed enough to warrant a new version
|
|
31
|
-
*/
|
|
32
|
-
async shouldVersion(oldContent, newContent) {
|
|
33
|
-
// Quick hash check - if identical, no version needed
|
|
34
|
-
const oldHash = this.hashContent(oldContent);
|
|
35
|
-
const newHash = this.hashContent(newContent);
|
|
36
|
-
if (oldHash === newHash) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
// Check size change
|
|
40
|
-
const sizeChange = Math.abs(oldContent.length - newContent.length) / Math.max(oldContent.length, 1);
|
|
41
|
-
if (sizeChange > this.config.sizeChangeThreshold) {
|
|
42
|
-
return true; // Large size change warrants version
|
|
43
|
-
}
|
|
44
|
-
// For small files, any change is significant
|
|
45
|
-
if (oldContent.length < 100 || newContent.length < 100) {
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
// Check semantic change using embeddings
|
|
49
|
-
try {
|
|
50
|
-
const semanticDistance = await this.calculateSemanticDistance(oldContent, newContent);
|
|
51
|
-
return semanticDistance > this.config.threshold;
|
|
52
|
-
}
|
|
53
|
-
catch (error) {
|
|
54
|
-
// If embedding fails, fall back to size-based decision
|
|
55
|
-
console.warn('Failed to calculate semantic distance:', error);
|
|
56
|
-
return sizeChange > 0.2;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Create a new version
|
|
61
|
-
*/
|
|
62
|
-
async createVersion(path, content, metadata) {
|
|
63
|
-
const versionId = uuidv4();
|
|
64
|
-
const timestamp = Date.now();
|
|
65
|
-
const hash = this.hashContent(content);
|
|
66
|
-
// Get current version number
|
|
67
|
-
const versions = await this.getVersions(path);
|
|
68
|
-
const versionNumber = versions.length + 1;
|
|
69
|
-
const parentVersion = versions[0]?.id;
|
|
70
|
-
// Generate embedding for semantic comparison
|
|
71
|
-
let embedding;
|
|
72
|
-
let semanticHash;
|
|
73
|
-
try {
|
|
74
|
-
// Only generate embedding for reasonably sized content
|
|
75
|
-
if (content.length < 100000) {
|
|
76
|
-
embedding = await this.generateEmbedding(content);
|
|
77
|
-
if (embedding) {
|
|
78
|
-
semanticHash = this.hashEmbedding(embedding);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
catch (error) {
|
|
83
|
-
console.warn('Failed to generate embedding for version:', error);
|
|
84
|
-
}
|
|
85
|
-
// Create version entity
|
|
86
|
-
const version = {
|
|
87
|
-
id: versionId,
|
|
88
|
-
path,
|
|
89
|
-
version: versionNumber,
|
|
90
|
-
timestamp,
|
|
91
|
-
hash,
|
|
92
|
-
semanticHash,
|
|
93
|
-
size: content.length,
|
|
94
|
-
author: metadata?.author,
|
|
95
|
-
message: metadata?.message,
|
|
96
|
-
parentVersion
|
|
97
|
-
};
|
|
98
|
-
// Store version using EntityManager (with actual content as data)
|
|
99
|
-
await this.storeEntity(version, NounType.State, embedding, content);
|
|
100
|
-
// Create relationship to parent version if exists
|
|
101
|
-
if (parentVersion) {
|
|
102
|
-
try {
|
|
103
|
-
await this.createRelationship(versionId, parentVersion, VerbType.Succeeds);
|
|
104
|
-
}
|
|
105
|
-
catch (error) {
|
|
106
|
-
console.warn(`Failed to create parent relationship for version ${versionId}:`, error);
|
|
107
|
-
// Continue without relationship - non-critical for version functionality
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
// Update cache
|
|
111
|
-
if (!this.versionCache.has(path)) {
|
|
112
|
-
this.versionCache.set(path, []);
|
|
113
|
-
}
|
|
114
|
-
this.versionCache.get(path).unshift({
|
|
115
|
-
id: versionId,
|
|
116
|
-
path,
|
|
117
|
-
version: versionNumber,
|
|
118
|
-
timestamp,
|
|
119
|
-
hash,
|
|
120
|
-
size: content.length,
|
|
121
|
-
semanticHash,
|
|
122
|
-
author: metadata?.author,
|
|
123
|
-
message: metadata?.message,
|
|
124
|
-
parentVersion
|
|
125
|
-
});
|
|
126
|
-
// Prune old versions if needed
|
|
127
|
-
await this.pruneVersions(path);
|
|
128
|
-
return versionId;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Get all versions for a file
|
|
132
|
-
*/
|
|
133
|
-
async getVersions(path) {
|
|
134
|
-
// Check cache first
|
|
135
|
-
if (this.versionCache.has(path)) {
|
|
136
|
-
return this.versionCache.get(path);
|
|
137
|
-
}
|
|
138
|
-
// Query using EntityManager
|
|
139
|
-
const versions = await this.findEntities({ path }, NounType.State, this.config.maxVersions * 2 // Get extra in case some are pruned
|
|
140
|
-
);
|
|
141
|
-
// Sort by timestamp (newest first)
|
|
142
|
-
versions.sort((a, b) => b.timestamp - a.timestamp);
|
|
143
|
-
// Update cache
|
|
144
|
-
this.versionCache.set(path, versions);
|
|
145
|
-
return versions;
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Get a specific version's content
|
|
149
|
-
*/
|
|
150
|
-
async getVersion(path, versionId) {
|
|
151
|
-
// Get the version entity
|
|
152
|
-
const version = await this.getEntity(versionId);
|
|
153
|
-
if (!version || version.path !== path) {
|
|
154
|
-
return null;
|
|
155
|
-
}
|
|
156
|
-
// Get the content from Brainy using the Brainy ID
|
|
157
|
-
const brainyId = await this.getBrainyId(versionId);
|
|
158
|
-
if (!brainyId) {
|
|
159
|
-
return null;
|
|
160
|
-
}
|
|
161
|
-
const entity = await this.brain.get(brainyId);
|
|
162
|
-
return entity?.data;
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Restore a file to a specific version
|
|
166
|
-
*/
|
|
167
|
-
async restoreVersion(path, versionId) {
|
|
168
|
-
const content = await this.getVersion(path, versionId);
|
|
169
|
-
if (!content) {
|
|
170
|
-
throw new Error(`Version ${versionId} not found for ${path}`);
|
|
171
|
-
}
|
|
172
|
-
// Create a new version pointing to the restored one
|
|
173
|
-
await this.createVersion(path, content, {
|
|
174
|
-
message: `Restored to version ${versionId}`
|
|
175
|
-
});
|
|
176
|
-
return content;
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Get version history with diffs
|
|
180
|
-
*/
|
|
181
|
-
async getVersionHistory(path, limit = 10) {
|
|
182
|
-
const versions = await this.getVersions(path);
|
|
183
|
-
const history = [];
|
|
184
|
-
for (let i = 0; i < Math.min(versions.length, limit); i++) {
|
|
185
|
-
const version = versions[i];
|
|
186
|
-
let changes = undefined;
|
|
187
|
-
// Calculate changes from parent
|
|
188
|
-
if (version.parentVersion && i < versions.length - 1) {
|
|
189
|
-
const parentVersion = versions[i + 1];
|
|
190
|
-
if (parentVersion.id === version.parentVersion) {
|
|
191
|
-
// Simple size-based diff for now
|
|
192
|
-
changes = {
|
|
193
|
-
additions: Math.max(0, version.size - parentVersion.size),
|
|
194
|
-
deletions: Math.max(0, parentVersion.size - version.size),
|
|
195
|
-
semanticChange: version.semanticHash && parentVersion.semanticHash
|
|
196
|
-
? this.estimateSemanticChange(version.semanticHash, parentVersion.semanticHash)
|
|
197
|
-
: 0
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
history.push({ version, changes });
|
|
202
|
-
}
|
|
203
|
-
return history;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Prune old versions beyond the limit
|
|
207
|
-
*/
|
|
208
|
-
async pruneVersions(path) {
|
|
209
|
-
const versions = await this.getVersions(path);
|
|
210
|
-
if (versions.length <= this.config.maxVersions) {
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
// Keep important versions (first, last, and evenly distributed)
|
|
214
|
-
const toKeep = new Set();
|
|
215
|
-
const toDelete = [];
|
|
216
|
-
// Always keep first and last
|
|
217
|
-
toKeep.add(versions[0].id); // Newest
|
|
218
|
-
toKeep.add(versions[versions.length - 1].id); // Oldest
|
|
219
|
-
// Keep evenly distributed versions
|
|
220
|
-
const step = Math.floor(versions.length / this.config.maxVersions);
|
|
221
|
-
for (let i = 0; i < versions.length; i += step) {
|
|
222
|
-
toKeep.add(versions[i].id);
|
|
223
|
-
}
|
|
224
|
-
// Mark others for deletion
|
|
225
|
-
for (const version of versions) {
|
|
226
|
-
if (!toKeep.has(version.id)) {
|
|
227
|
-
toDelete.push(version.id);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
// Delete excess versions
|
|
231
|
-
for (const id of toDelete.slice(0, versions.length - this.config.maxVersions)) {
|
|
232
|
-
await this.deleteEntity(id);
|
|
233
|
-
}
|
|
234
|
-
// Update cache
|
|
235
|
-
this.versionCache.set(path, versions.filter(v => !toDelete.includes(v.id)));
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Calculate semantic distance between two pieces of content
|
|
239
|
-
*/
|
|
240
|
-
async calculateSemanticDistance(oldContent, newContent) {
|
|
241
|
-
// Generate embeddings
|
|
242
|
-
const [oldEmbedding, newEmbedding] = await Promise.all([
|
|
243
|
-
this.generateEmbedding(oldContent),
|
|
244
|
-
this.generateEmbedding(newContent)
|
|
245
|
-
]);
|
|
246
|
-
if (!oldEmbedding || !newEmbedding) {
|
|
247
|
-
throw new Error('Failed to generate embeddings');
|
|
248
|
-
}
|
|
249
|
-
// Calculate cosine distance
|
|
250
|
-
return cosineDistance(oldEmbedding, newEmbedding);
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* Generate embedding for content
|
|
254
|
-
*/
|
|
255
|
-
async generateEmbedding(content) {
|
|
256
|
-
try {
|
|
257
|
-
// For text content, use first 10KB for embedding
|
|
258
|
-
const text = content.toString('utf8', 0, Math.min(10240, content.length));
|
|
259
|
-
// Use Brainy's embedding function
|
|
260
|
-
const vector = await this.brain.embed(text);
|
|
261
|
-
return vector;
|
|
262
|
-
}
|
|
263
|
-
catch (error) {
|
|
264
|
-
console.error('Failed to generate embedding:', error);
|
|
265
|
-
return undefined;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Hash content for quick comparison
|
|
270
|
-
*/
|
|
271
|
-
hashContent(content) {
|
|
272
|
-
return createHash('sha256').update(content).digest('hex');
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* Hash embedding for quick comparison
|
|
276
|
-
*/
|
|
277
|
-
hashEmbedding(embedding) {
|
|
278
|
-
return createHash('sha256')
|
|
279
|
-
.update(Buffer.from(new Float32Array(embedding).buffer))
|
|
280
|
-
.digest('hex');
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Estimate semantic change from hashes (rough approximation)
|
|
284
|
-
*/
|
|
285
|
-
estimateSemanticChange(hash1, hash2) {
|
|
286
|
-
if (hash1 === hash2)
|
|
287
|
-
return 0;
|
|
288
|
-
// Simple hamming distance on first few characters
|
|
289
|
-
// This is a rough approximation
|
|
290
|
-
let distance = 0;
|
|
291
|
-
for (let i = 0; i < Math.min(hash1.length, hash2.length, 8); i++) {
|
|
292
|
-
if (hash1[i] !== hash2[i])
|
|
293
|
-
distance++;
|
|
294
|
-
}
|
|
295
|
-
return distance / 8;
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Clear version cache for a file
|
|
299
|
-
*/
|
|
300
|
-
clearCache(path) {
|
|
301
|
-
if (path) {
|
|
302
|
-
this.versionCache.delete(path);
|
|
303
|
-
}
|
|
304
|
-
else {
|
|
305
|
-
this.versionCache.clear();
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
//# sourceMappingURL=SemanticVersioning.js.map
|