agentic-qe 2.8.0 → 2.8.1
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 +105 -0
- package/README.md +1 -1
- package/dist/agents/BaseAgent.d.ts +128 -0
- package/dist/agents/BaseAgent.d.ts.map +1 -1
- package/dist/agents/BaseAgent.js +256 -0
- package/dist/agents/BaseAgent.js.map +1 -1
- package/dist/cli/commands/supabase/index.d.ts +20 -0
- package/dist/cli/commands/supabase/index.d.ts.map +1 -0
- package/dist/cli/commands/supabase/index.js +632 -0
- package/dist/cli/commands/supabase/index.js.map +1 -0
- package/dist/cli/index.js +3 -0
- package/dist/cli/index.js.map +1 -1
- package/dist/core/memory/HNSWVectorMemory.js +1 -1
- package/dist/mcp/handlers/NewDomainToolsHandler.d.ts +8 -8
- package/dist/mcp/handlers/NewDomainToolsHandler.d.ts.map +1 -1
- package/dist/mcp/handlers/NewDomainToolsHandler.js.map +1 -1
- package/dist/mcp/handlers/ruvector/RuVectorHandler.d.ts +54 -0
- package/dist/mcp/handlers/ruvector/RuVectorHandler.d.ts.map +1 -0
- package/dist/mcp/handlers/ruvector/RuVectorHandler.js +325 -0
- package/dist/mcp/handlers/ruvector/RuVectorHandler.js.map +1 -0
- package/dist/mcp/handlers/ruvector/index.d.ts +5 -0
- package/dist/mcp/handlers/ruvector/index.d.ts.map +1 -0
- package/dist/mcp/handlers/ruvector/index.js +9 -0
- package/dist/mcp/handlers/ruvector/index.js.map +1 -0
- package/dist/mcp/server-instructions.d.ts +1 -1
- package/dist/mcp/server-instructions.js +1 -1
- package/dist/mcp/server.d.ts.map +1 -1
- package/dist/mcp/server.js +100 -22
- package/dist/mcp/server.js.map +1 -1
- package/dist/nervous-system/adapters/BTSPAdapter.d.ts +342 -0
- package/dist/nervous-system/adapters/BTSPAdapter.d.ts.map +1 -0
- package/dist/nervous-system/adapters/BTSPAdapter.js +494 -0
- package/dist/nervous-system/adapters/BTSPAdapter.js.map +1 -0
- package/dist/nervous-system/adapters/CircadianController.d.ts +560 -0
- package/dist/nervous-system/adapters/CircadianController.d.ts.map +1 -0
- package/dist/nervous-system/adapters/CircadianController.js +882 -0
- package/dist/nervous-system/adapters/CircadianController.js.map +1 -0
- package/dist/nervous-system/adapters/GlobalWorkspaceAdapter.d.ts +337 -0
- package/dist/nervous-system/adapters/GlobalWorkspaceAdapter.d.ts.map +1 -0
- package/dist/nervous-system/adapters/GlobalWorkspaceAdapter.js +532 -0
- package/dist/nervous-system/adapters/GlobalWorkspaceAdapter.js.map +1 -0
- package/dist/nervous-system/adapters/HdcMemoryAdapter.d.ts +444 -0
- package/dist/nervous-system/adapters/HdcMemoryAdapter.d.ts.map +1 -0
- package/dist/nervous-system/adapters/HdcMemoryAdapter.js +715 -0
- package/dist/nervous-system/adapters/HdcMemoryAdapter.js.map +1 -0
- package/dist/nervous-system/adapters/ReflexLayer.d.ts +231 -0
- package/dist/nervous-system/adapters/ReflexLayer.d.ts.map +1 -0
- package/dist/nervous-system/adapters/ReflexLayer.js +309 -0
- package/dist/nervous-system/adapters/ReflexLayer.js.map +1 -0
- package/dist/nervous-system/index.d.ts +25 -0
- package/dist/nervous-system/index.d.ts.map +1 -0
- package/dist/nervous-system/index.js +80 -0
- package/dist/nervous-system/index.js.map +1 -0
- package/dist/nervous-system/integration/BTSPLearningEngine.d.ts +266 -0
- package/dist/nervous-system/integration/BTSPLearningEngine.d.ts.map +1 -0
- package/dist/nervous-system/integration/BTSPLearningEngine.js +587 -0
- package/dist/nervous-system/integration/BTSPLearningEngine.js.map +1 -0
- package/dist/nervous-system/integration/CircadianAgent.d.ts +389 -0
- package/dist/nervous-system/integration/CircadianAgent.d.ts.map +1 -0
- package/dist/nervous-system/integration/CircadianAgent.js +696 -0
- package/dist/nervous-system/integration/CircadianAgent.js.map +1 -0
- package/dist/nervous-system/integration/HybridPatternStore.d.ts +244 -0
- package/dist/nervous-system/integration/HybridPatternStore.d.ts.map +1 -0
- package/dist/nervous-system/integration/HybridPatternStore.js +622 -0
- package/dist/nervous-system/integration/HybridPatternStore.js.map +1 -0
- package/dist/nervous-system/integration/NervousSystemEnhancement.d.ts +459 -0
- package/dist/nervous-system/integration/NervousSystemEnhancement.d.ts.map +1 -0
- package/dist/nervous-system/integration/NervousSystemEnhancement.js +921 -0
- package/dist/nervous-system/integration/NervousSystemEnhancement.js.map +1 -0
- package/dist/nervous-system/integration/WorkspaceAgent.d.ts +398 -0
- package/dist/nervous-system/integration/WorkspaceAgent.d.ts.map +1 -0
- package/dist/nervous-system/integration/WorkspaceAgent.js +722 -0
- package/dist/nervous-system/integration/WorkspaceAgent.js.map +1 -0
- package/dist/nervous-system/integration/index.d.ts +22 -0
- package/dist/nervous-system/integration/index.d.ts.map +1 -0
- package/dist/nervous-system/integration/index.js +44 -0
- package/dist/nervous-system/integration/index.js.map +1 -0
- package/dist/nervous-system/persistence/BTSPSerializer.d.ts +96 -0
- package/dist/nervous-system/persistence/BTSPSerializer.d.ts.map +1 -0
- package/dist/nervous-system/persistence/BTSPSerializer.js +223 -0
- package/dist/nervous-system/persistence/BTSPSerializer.js.map +1 -0
- package/dist/nervous-system/persistence/CircadianSerializer.d.ts +90 -0
- package/dist/nervous-system/persistence/CircadianSerializer.d.ts.map +1 -0
- package/dist/nervous-system/persistence/CircadianSerializer.js +239 -0
- package/dist/nervous-system/persistence/CircadianSerializer.js.map +1 -0
- package/dist/nervous-system/persistence/HdcSerializer.d.ts +100 -0
- package/dist/nervous-system/persistence/HdcSerializer.d.ts.map +1 -0
- package/dist/nervous-system/persistence/HdcSerializer.js +259 -0
- package/dist/nervous-system/persistence/HdcSerializer.js.map +1 -0
- package/dist/nervous-system/persistence/INervousSystemStore.d.ts +208 -0
- package/dist/nervous-system/persistence/INervousSystemStore.d.ts.map +1 -0
- package/dist/nervous-system/persistence/INervousSystemStore.js +11 -0
- package/dist/nervous-system/persistence/INervousSystemStore.js.map +1 -0
- package/dist/nervous-system/persistence/NervousSystemPersistenceManager.d.ts +187 -0
- package/dist/nervous-system/persistence/NervousSystemPersistenceManager.d.ts.map +1 -0
- package/dist/nervous-system/persistence/NervousSystemPersistenceManager.js +411 -0
- package/dist/nervous-system/persistence/NervousSystemPersistenceManager.js.map +1 -0
- package/dist/nervous-system/persistence/SQLiteNervousSystemStore.d.ts +98 -0
- package/dist/nervous-system/persistence/SQLiteNervousSystemStore.d.ts.map +1 -0
- package/dist/nervous-system/persistence/SQLiteNervousSystemStore.js +510 -0
- package/dist/nervous-system/persistence/SQLiteNervousSystemStore.js.map +1 -0
- package/dist/nervous-system/persistence/index.d.ts +22 -0
- package/dist/nervous-system/persistence/index.d.ts.map +1 -0
- package/dist/nervous-system/persistence/index.js +45 -0
- package/dist/nervous-system/persistence/index.js.map +1 -0
- package/dist/nervous-system/wasm-loader.d.ts +52 -0
- package/dist/nervous-system/wasm-loader.d.ts.map +1 -0
- package/dist/nervous-system/wasm-loader.js +188 -0
- package/dist/nervous-system/wasm-loader.js.map +1 -0
- package/dist/persistence/HybridPersistenceProvider.d.ts +184 -0
- package/dist/persistence/HybridPersistenceProvider.d.ts.map +1 -0
- package/dist/persistence/HybridPersistenceProvider.js +1086 -0
- package/dist/persistence/HybridPersistenceProvider.js.map +1 -0
- package/dist/persistence/IPersistenceProvider.d.ts +657 -0
- package/dist/persistence/IPersistenceProvider.d.ts.map +1 -0
- package/dist/persistence/IPersistenceProvider.js +11 -0
- package/dist/persistence/IPersistenceProvider.js.map +1 -0
- package/dist/persistence/SupabaseConfig.d.ts +176 -0
- package/dist/persistence/SupabaseConfig.d.ts.map +1 -0
- package/dist/persistence/SupabaseConfig.js +277 -0
- package/dist/persistence/SupabaseConfig.js.map +1 -0
- package/dist/persistence/SupabasePersistenceProvider.d.ts +143 -0
- package/dist/persistence/SupabasePersistenceProvider.d.ts.map +1 -0
- package/dist/persistence/SupabasePersistenceProvider.js +955 -0
- package/dist/persistence/SupabasePersistenceProvider.js.map +1 -0
- package/dist/persistence/adapters/CodeIntelligenceSyncAdapter.d.ts +213 -0
- package/dist/persistence/adapters/CodeIntelligenceSyncAdapter.d.ts.map +1 -0
- package/dist/persistence/adapters/CodeIntelligenceSyncAdapter.js +468 -0
- package/dist/persistence/adapters/CodeIntelligenceSyncAdapter.js.map +1 -0
- package/dist/persistence/adapters/MemorySyncAdapter.d.ts +115 -0
- package/dist/persistence/adapters/MemorySyncAdapter.d.ts.map +1 -0
- package/dist/persistence/adapters/MemorySyncAdapter.js +291 -0
- package/dist/persistence/adapters/MemorySyncAdapter.js.map +1 -0
- package/dist/persistence/adapters/index.d.ts +11 -0
- package/dist/persistence/adapters/index.d.ts.map +1 -0
- package/dist/persistence/adapters/index.js +20 -0
- package/dist/persistence/adapters/index.js.map +1 -0
- package/dist/persistence/index.d.ts +14 -0
- package/dist/persistence/index.d.ts.map +1 -1
- package/dist/persistence/index.js +36 -1
- package/dist/persistence/index.js.map +1 -1
- package/package.json +3 -1
|
@@ -0,0 +1,715 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* HDC Memory Adapter for RuVector Nervous System Integration
|
|
4
|
+
*
|
|
5
|
+
* Provides high-level interface to Hyperdimensional Computing (HDC) memory
|
|
6
|
+
* for QE agent pattern storage and retrieval. HDC enables:
|
|
7
|
+
* - Noise-tolerant pattern matching
|
|
8
|
+
* - One-shot learning of test patterns
|
|
9
|
+
* - Sub-microsecond similarity queries (<100ns target)
|
|
10
|
+
* - Distributed pattern representation (10,000-bit hypervectors)
|
|
11
|
+
*
|
|
12
|
+
* @module nervous-system/adapters/HdcMemoryAdapter
|
|
13
|
+
* @see https://github.com/ruvector/nervous-system-wasm
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.HdcMemoryAdapter = exports.DEFAULT_HDC_CONFIG = void 0;
|
|
17
|
+
const wasm_loader_js_1 = require("../wasm-loader.js");
|
|
18
|
+
/**
|
|
19
|
+
* Default configuration values
|
|
20
|
+
*/
|
|
21
|
+
exports.DEFAULT_HDC_CONFIG = {
|
|
22
|
+
similarityThreshold: 0.7,
|
|
23
|
+
maxRetrievalResults: 10,
|
|
24
|
+
autoInit: true,
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* HDC Memory Adapter Implementation
|
|
28
|
+
*
|
|
29
|
+
* Wraps the WASM Hypervector and HdcMemory classes to provide
|
|
30
|
+
* a high-level interface for QE agent pattern operations.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const adapter = new HdcMemoryAdapter();
|
|
35
|
+
* await adapter.initialize();
|
|
36
|
+
*
|
|
37
|
+
* // Create and store patterns
|
|
38
|
+
* const pattern: SimplePattern = {
|
|
39
|
+
* type: 'edge-case',
|
|
40
|
+
* domain: 'unit-test',
|
|
41
|
+
* content: 'null input handling'
|
|
42
|
+
* };
|
|
43
|
+
* const vector = adapter.encodePattern(pattern);
|
|
44
|
+
* adapter.store('pattern-001', vector);
|
|
45
|
+
*
|
|
46
|
+
* // Query for similar patterns
|
|
47
|
+
* const results = adapter.retrieve(vector, 0.8);
|
|
48
|
+
* console.log(`Found ${results.length} similar patterns`);
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
class HdcMemoryAdapter {
|
|
52
|
+
/**
|
|
53
|
+
* Create a new HdcMemoryAdapter
|
|
54
|
+
* @param config Optional configuration overrides
|
|
55
|
+
*/
|
|
56
|
+
constructor(config = {}) {
|
|
57
|
+
this.memory = null;
|
|
58
|
+
this.initialized = false;
|
|
59
|
+
/**
|
|
60
|
+
* Codebook for encoding pattern types to hypervectors
|
|
61
|
+
* Each type gets a unique random hypervector for binding
|
|
62
|
+
*/
|
|
63
|
+
this.typeCodebook = new Map();
|
|
64
|
+
/**
|
|
65
|
+
* Codebook for encoding domains/categories to hypervectors
|
|
66
|
+
*/
|
|
67
|
+
this.domainCodebook = new Map();
|
|
68
|
+
/**
|
|
69
|
+
* Codebook for encoding frameworks to hypervectors
|
|
70
|
+
*/
|
|
71
|
+
this.frameworkCodebook = new Map();
|
|
72
|
+
/**
|
|
73
|
+
* Base vectors for role encoding (TYPE, DOMAIN, CONTENT, FRAMEWORK)
|
|
74
|
+
*/
|
|
75
|
+
this.roleVectors = {
|
|
76
|
+
type: null,
|
|
77
|
+
domain: null,
|
|
78
|
+
content: null,
|
|
79
|
+
framework: null,
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Local tracking of stored patterns for serialization
|
|
83
|
+
* (WASM HdcMemory doesn't expose pattern iteration)
|
|
84
|
+
*/
|
|
85
|
+
this.storedPatterns = new Map();
|
|
86
|
+
this.config = { ...exports.DEFAULT_HDC_CONFIG, ...config };
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Initialize the WASM module for Node.js environment
|
|
90
|
+
*
|
|
91
|
+
* Must be called before any other operations.
|
|
92
|
+
* Loads the WASM binary and initializes all codebooks.
|
|
93
|
+
*
|
|
94
|
+
* @throws Error if WASM file cannot be loaded
|
|
95
|
+
*/
|
|
96
|
+
async initialize() {
|
|
97
|
+
if (this.initialized) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
// Use shared WASM loader
|
|
102
|
+
await (0, wasm_loader_js_1.initNervousSystem)();
|
|
103
|
+
// Create the HDC memory instance
|
|
104
|
+
this.memory = new wasm_loader_js_1.HdcMemory();
|
|
105
|
+
// Initialize role vectors (used for structured encoding)
|
|
106
|
+
this.roleVectors.type = wasm_loader_js_1.Hypervector.random();
|
|
107
|
+
this.roleVectors.domain = wasm_loader_js_1.Hypervector.random();
|
|
108
|
+
this.roleVectors.content = wasm_loader_js_1.Hypervector.random();
|
|
109
|
+
this.roleVectors.framework = wasm_loader_js_1.Hypervector.random();
|
|
110
|
+
// Initialize codebooks with known pattern types
|
|
111
|
+
this.initializeCodebooks();
|
|
112
|
+
this.initialized = true;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
throw new Error(`Failed to initialize HdcMemoryAdapter: ${error instanceof Error ? error.message : String(error)}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Initialize codebooks with predefined pattern types and domains
|
|
120
|
+
*/
|
|
121
|
+
initializeCodebooks() {
|
|
122
|
+
// Pattern types from PatternType enum
|
|
123
|
+
const patternTypes = [
|
|
124
|
+
'edge-case',
|
|
125
|
+
'boundary-condition',
|
|
126
|
+
'error-handling',
|
|
127
|
+
'integration',
|
|
128
|
+
'async-pattern',
|
|
129
|
+
'mock-pattern',
|
|
130
|
+
'assertion-pattern',
|
|
131
|
+
'setup-teardown',
|
|
132
|
+
'data-driven',
|
|
133
|
+
'parameterized',
|
|
134
|
+
];
|
|
135
|
+
for (const type of patternTypes) {
|
|
136
|
+
this.typeCodebook.set(type, wasm_loader_js_1.Hypervector.random());
|
|
137
|
+
}
|
|
138
|
+
// Pattern categories from PatternCategory enum
|
|
139
|
+
const domains = [
|
|
140
|
+
'unit-test',
|
|
141
|
+
'integration-test',
|
|
142
|
+
'e2e-test',
|
|
143
|
+
'performance-test',
|
|
144
|
+
'security-test',
|
|
145
|
+
];
|
|
146
|
+
for (const domain of domains) {
|
|
147
|
+
this.domainCodebook.set(domain, wasm_loader_js_1.Hypervector.random());
|
|
148
|
+
}
|
|
149
|
+
// Test frameworks from TestFramework enum
|
|
150
|
+
const frameworks = [
|
|
151
|
+
'jest',
|
|
152
|
+
'mocha',
|
|
153
|
+
'cypress',
|
|
154
|
+
'vitest',
|
|
155
|
+
'jasmine',
|
|
156
|
+
'ava',
|
|
157
|
+
];
|
|
158
|
+
for (const framework of frameworks) {
|
|
159
|
+
this.frameworkCodebook.set(framework, wasm_loader_js_1.Hypervector.random());
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Check if the adapter is initialized
|
|
164
|
+
*/
|
|
165
|
+
isInitialized() {
|
|
166
|
+
return this.initialized;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Ensure the adapter is initialized before operations
|
|
170
|
+
*/
|
|
171
|
+
ensureInitialized() {
|
|
172
|
+
if (!this.initialized || !this.memory) {
|
|
173
|
+
throw new Error('HdcMemoryAdapter not initialized. Call initialize() first.');
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Create a new hypervector
|
|
178
|
+
* @param seed Optional seed for reproducibility
|
|
179
|
+
* @returns New 10,000-bit hypervector
|
|
180
|
+
*/
|
|
181
|
+
createHypervector(seed) {
|
|
182
|
+
this.ensureInitialized();
|
|
183
|
+
if (seed !== undefined) {
|
|
184
|
+
return wasm_loader_js_1.Hypervector.from_seed(seed);
|
|
185
|
+
}
|
|
186
|
+
return wasm_loader_js_1.Hypervector.random();
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Bind two hypervectors using XOR
|
|
190
|
+
* @param a First hypervector
|
|
191
|
+
* @param b Second hypervector
|
|
192
|
+
* @returns Bound hypervector
|
|
193
|
+
*/
|
|
194
|
+
bind(a, b) {
|
|
195
|
+
this.ensureInitialized();
|
|
196
|
+
return a.bind(b);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Bundle multiple hypervectors using majority voting
|
|
200
|
+
*
|
|
201
|
+
* For arrays smaller than 3, uses repeated bundling with padding.
|
|
202
|
+
* For larger arrays, chains bundle_3 operations.
|
|
203
|
+
*
|
|
204
|
+
* @param vectors Array of hypervectors to bundle
|
|
205
|
+
* @returns Bundled hypervector
|
|
206
|
+
* @throws Error if vectors array is empty
|
|
207
|
+
*/
|
|
208
|
+
bundle(vectors) {
|
|
209
|
+
this.ensureInitialized();
|
|
210
|
+
if (vectors.length === 0) {
|
|
211
|
+
throw new Error('Cannot bundle empty array of vectors');
|
|
212
|
+
}
|
|
213
|
+
if (vectors.length === 1) {
|
|
214
|
+
return vectors[0];
|
|
215
|
+
}
|
|
216
|
+
if (vectors.length === 2) {
|
|
217
|
+
// For 2 vectors, add a random tiebreaker
|
|
218
|
+
const tiebreaker = wasm_loader_js_1.Hypervector.random();
|
|
219
|
+
return wasm_loader_js_1.Hypervector.bundle_3(vectors[0], vectors[1], tiebreaker);
|
|
220
|
+
}
|
|
221
|
+
if (vectors.length === 3) {
|
|
222
|
+
return wasm_loader_js_1.Hypervector.bundle_3(vectors[0], vectors[1], vectors[2]);
|
|
223
|
+
}
|
|
224
|
+
// For more than 3, chain bundle operations
|
|
225
|
+
// Bundle first 3, then iteratively bundle with pairs
|
|
226
|
+
let result = wasm_loader_js_1.Hypervector.bundle_3(vectors[0], vectors[1], vectors[2]);
|
|
227
|
+
for (let i = 3; i < vectors.length; i += 2) {
|
|
228
|
+
if (i + 1 < vectors.length) {
|
|
229
|
+
result = wasm_loader_js_1.Hypervector.bundle_3(result, vectors[i], vectors[i + 1]);
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
// Odd remaining element - use random tiebreaker
|
|
233
|
+
const tiebreaker = wasm_loader_js_1.Hypervector.random();
|
|
234
|
+
result = wasm_loader_js_1.Hypervector.bundle_3(result, vectors[i], tiebreaker);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return result;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Compute similarity between two hypervectors
|
|
241
|
+
* @param a First hypervector
|
|
242
|
+
* @param b Second hypervector
|
|
243
|
+
* @returns Similarity score in [-1.0, 1.0]
|
|
244
|
+
*/
|
|
245
|
+
similarity(a, b) {
|
|
246
|
+
this.ensureInitialized();
|
|
247
|
+
return a.similarity(b);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Store a hypervector with a key
|
|
251
|
+
*
|
|
252
|
+
* Note: The WASM store operation consumes the vector (move semantics).
|
|
253
|
+
* If you need to keep using the vector after storing, create a clone first
|
|
254
|
+
* using `Hypervector.from_bytes(vector.to_bytes())`.
|
|
255
|
+
*
|
|
256
|
+
* @param key Unique identifier
|
|
257
|
+
* @param vector Hypervector to store (will be consumed)
|
|
258
|
+
*/
|
|
259
|
+
store(key, vector) {
|
|
260
|
+
this.ensureInitialized();
|
|
261
|
+
// Clone the vector before storing to preserve the original
|
|
262
|
+
const bytes = vector.to_bytes();
|
|
263
|
+
const clone = wasm_loader_js_1.Hypervector.from_bytes(bytes);
|
|
264
|
+
this.memory.store(key, clone);
|
|
265
|
+
// Track locally for serialization (clone again for our map)
|
|
266
|
+
const trackingClone = wasm_loader_js_1.Hypervector.from_bytes(bytes);
|
|
267
|
+
this.storedPatterns.set(key, { vector: trackingClone });
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Retrieve patterns similar to query
|
|
271
|
+
* @param query Query hypervector
|
|
272
|
+
* @param threshold Minimum similarity (default from config)
|
|
273
|
+
* @returns Array of matching results
|
|
274
|
+
*/
|
|
275
|
+
retrieve(query, threshold) {
|
|
276
|
+
this.ensureInitialized();
|
|
277
|
+
const actualThreshold = threshold ?? this.config.similarityThreshold;
|
|
278
|
+
try {
|
|
279
|
+
const rawResults = this.memory.retrieve(query, actualThreshold);
|
|
280
|
+
// Convert raw results to typed array
|
|
281
|
+
if (!Array.isArray(rawResults)) {
|
|
282
|
+
return [];
|
|
283
|
+
}
|
|
284
|
+
const results = [];
|
|
285
|
+
for (const item of rawResults) {
|
|
286
|
+
if (Array.isArray(item) && item.length >= 2) {
|
|
287
|
+
results.push({
|
|
288
|
+
key: String(item[0]),
|
|
289
|
+
similarity: Number(item[1]),
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
// Sort by similarity descending and limit results
|
|
294
|
+
return results
|
|
295
|
+
.sort((a, b) => b.similarity - a.similarity)
|
|
296
|
+
.slice(0, this.config.maxRetrievalResults);
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
// Handle potential WASM errors gracefully
|
|
300
|
+
console.warn('HDC retrieve error:', error);
|
|
301
|
+
return [];
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Get a stored hypervector by key
|
|
306
|
+
* @param key Pattern key
|
|
307
|
+
* @returns Hypervector if found
|
|
308
|
+
*/
|
|
309
|
+
get(key) {
|
|
310
|
+
this.ensureInitialized();
|
|
311
|
+
return this.memory.get(key);
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Check if a key exists
|
|
315
|
+
* @param key Pattern key
|
|
316
|
+
* @returns True if exists
|
|
317
|
+
*/
|
|
318
|
+
has(key) {
|
|
319
|
+
this.ensureInitialized();
|
|
320
|
+
return this.memory.has(key);
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Clear all stored patterns
|
|
324
|
+
*/
|
|
325
|
+
clear() {
|
|
326
|
+
this.ensureInitialized();
|
|
327
|
+
this.memory.clear();
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Get number of stored patterns
|
|
331
|
+
*/
|
|
332
|
+
size() {
|
|
333
|
+
this.ensureInitialized();
|
|
334
|
+
return this.memory.size;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Find top-k most similar patterns
|
|
338
|
+
* @param query Query hypervector
|
|
339
|
+
* @param k Number of results
|
|
340
|
+
* @returns Top-k matching results
|
|
341
|
+
*/
|
|
342
|
+
topK(query, k) {
|
|
343
|
+
this.ensureInitialized();
|
|
344
|
+
try {
|
|
345
|
+
const rawResults = this.memory.top_k(query, k);
|
|
346
|
+
if (!Array.isArray(rawResults)) {
|
|
347
|
+
return [];
|
|
348
|
+
}
|
|
349
|
+
const results = [];
|
|
350
|
+
for (const item of rawResults) {
|
|
351
|
+
if (Array.isArray(item) && item.length >= 2) {
|
|
352
|
+
results.push({
|
|
353
|
+
key: String(item[0]),
|
|
354
|
+
similarity: Number(item[1]),
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return results;
|
|
359
|
+
}
|
|
360
|
+
catch (error) {
|
|
361
|
+
console.warn('HDC top_k error:', error);
|
|
362
|
+
return [];
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Encode a pattern to a hypervector
|
|
367
|
+
*
|
|
368
|
+
* Uses structured encoding with role-filler binding:
|
|
369
|
+
* - TYPE role bound to type hypervector
|
|
370
|
+
* - DOMAIN role bound to domain hypervector
|
|
371
|
+
* - CONTENT encoded via hash-based seeding
|
|
372
|
+
* - FRAMEWORK role bound to framework hypervector (if present)
|
|
373
|
+
*
|
|
374
|
+
* Final pattern is the bundle of all role-filler pairs.
|
|
375
|
+
*
|
|
376
|
+
* @param pattern Pattern to encode
|
|
377
|
+
* @returns Encoded hypervector
|
|
378
|
+
*/
|
|
379
|
+
encodePattern(pattern) {
|
|
380
|
+
this.ensureInitialized();
|
|
381
|
+
const temporaryVectors = [];
|
|
382
|
+
const components = [];
|
|
383
|
+
try {
|
|
384
|
+
// Encode type
|
|
385
|
+
const typeStr = this.getPatternType(pattern);
|
|
386
|
+
const typeVec = this.getOrCreateCodebookEntry(this.typeCodebook, typeStr);
|
|
387
|
+
const typeBound = this.roleVectors.type.bind(typeVec);
|
|
388
|
+
temporaryVectors.push(typeBound);
|
|
389
|
+
components.push(typeBound);
|
|
390
|
+
// Encode domain
|
|
391
|
+
const domainStr = this.getPatternDomain(pattern);
|
|
392
|
+
const domainVec = this.getOrCreateCodebookEntry(this.domainCodebook, domainStr);
|
|
393
|
+
const domainBound = this.roleVectors.domain.bind(domainVec);
|
|
394
|
+
temporaryVectors.push(domainBound);
|
|
395
|
+
components.push(domainBound);
|
|
396
|
+
// Encode content via hash-based seeding
|
|
397
|
+
const contentStr = this.getPatternContent(pattern);
|
|
398
|
+
const contentHash = this.hashString(contentStr);
|
|
399
|
+
const contentVec = wasm_loader_js_1.Hypervector.from_seed(contentHash);
|
|
400
|
+
temporaryVectors.push(contentVec);
|
|
401
|
+
const contentBound = this.roleVectors.content.bind(contentVec);
|
|
402
|
+
temporaryVectors.push(contentBound);
|
|
403
|
+
components.push(contentBound);
|
|
404
|
+
// Encode framework if present (for full TestPattern)
|
|
405
|
+
if (this.isTestPattern(pattern) && pattern.framework) {
|
|
406
|
+
const frameworkStr = String(pattern.framework);
|
|
407
|
+
const frameworkVec = this.getOrCreateCodebookEntry(this.frameworkCodebook, frameworkStr);
|
|
408
|
+
const frameworkBound = this.roleVectors.framework.bind(frameworkVec);
|
|
409
|
+
temporaryVectors.push(frameworkBound);
|
|
410
|
+
components.push(frameworkBound);
|
|
411
|
+
}
|
|
412
|
+
// Bundle all components
|
|
413
|
+
const result = this.bundle(components);
|
|
414
|
+
// Clone the result before freeing temporaries
|
|
415
|
+
const resultBytes = result.to_bytes();
|
|
416
|
+
const clonedResult = wasm_loader_js_1.Hypervector.from_bytes(resultBytes);
|
|
417
|
+
// Free bundle result if different from components
|
|
418
|
+
if (!components.includes(result)) {
|
|
419
|
+
temporaryVectors.push(result);
|
|
420
|
+
}
|
|
421
|
+
return clonedResult;
|
|
422
|
+
}
|
|
423
|
+
finally {
|
|
424
|
+
// Note: We don't free temporaryVectors here because they may be
|
|
425
|
+
// reused by bundle. The WASM runtime will handle cleanup.
|
|
426
|
+
// If memory issues occur, uncomment the following:
|
|
427
|
+
// for (const vec of temporaryVectors) {
|
|
428
|
+
// try { vec.free(); } catch { /* ignore */ }
|
|
429
|
+
// }
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Type guard to check if pattern is a full TestPattern
|
|
434
|
+
*/
|
|
435
|
+
isTestPattern(pattern) {
|
|
436
|
+
return 'framework' in pattern && 'id' in pattern && 'name' in pattern;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Decode a hypervector to partial pattern information
|
|
440
|
+
*
|
|
441
|
+
* Since HDC encoding is lossy, this performs best-match retrieval
|
|
442
|
+
* against the codebooks to determine the most likely type and domain.
|
|
443
|
+
*
|
|
444
|
+
* @param vector Hypervector to decode
|
|
445
|
+
* @returns Partial pattern with best-match type and domain
|
|
446
|
+
*/
|
|
447
|
+
decodePattern(vector) {
|
|
448
|
+
this.ensureInitialized();
|
|
449
|
+
const result = {};
|
|
450
|
+
const temporaryVectors = [];
|
|
451
|
+
try {
|
|
452
|
+
// Decode type by unbinding and finding best match
|
|
453
|
+
// XOR is self-inverse: vector.bind(roleType) "extracts" the type component
|
|
454
|
+
const typeUnbound = vector.bind(this.roleVectors.type);
|
|
455
|
+
temporaryVectors.push(typeUnbound);
|
|
456
|
+
let bestTypeSimilarity = -1;
|
|
457
|
+
let bestType = '';
|
|
458
|
+
this.typeCodebook.forEach((typeVec, type) => {
|
|
459
|
+
const sim = typeUnbound.similarity(typeVec);
|
|
460
|
+
if (sim > bestTypeSimilarity) {
|
|
461
|
+
bestTypeSimilarity = sim;
|
|
462
|
+
bestType = type;
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
if (bestTypeSimilarity > 0.3) {
|
|
466
|
+
result.type = bestType;
|
|
467
|
+
}
|
|
468
|
+
// Decode domain by unbinding and finding best match
|
|
469
|
+
const domainUnbound = vector.bind(this.roleVectors.domain);
|
|
470
|
+
temporaryVectors.push(domainUnbound);
|
|
471
|
+
let bestDomainSimilarity = -1;
|
|
472
|
+
let bestDomain = '';
|
|
473
|
+
this.domainCodebook.forEach((domainVec, domain) => {
|
|
474
|
+
const sim = domainUnbound.similarity(domainVec);
|
|
475
|
+
if (sim > bestDomainSimilarity) {
|
|
476
|
+
bestDomainSimilarity = sim;
|
|
477
|
+
bestDomain = domain;
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
if (bestDomainSimilarity > 0.3) {
|
|
481
|
+
result.domain = bestDomain;
|
|
482
|
+
}
|
|
483
|
+
// Content cannot be decoded (hash is one-way)
|
|
484
|
+
// but we can indicate that metadata might be reconstructable
|
|
485
|
+
result.metadata = {
|
|
486
|
+
decodedAt: new Date().toISOString(),
|
|
487
|
+
typeConfidence: bestTypeSimilarity,
|
|
488
|
+
domainConfidence: bestDomainSimilarity,
|
|
489
|
+
};
|
|
490
|
+
return result;
|
|
491
|
+
}
|
|
492
|
+
finally {
|
|
493
|
+
// Clean up temporary vectors
|
|
494
|
+
for (const vec of temporaryVectors) {
|
|
495
|
+
try {
|
|
496
|
+
vec.free();
|
|
497
|
+
}
|
|
498
|
+
catch {
|
|
499
|
+
// Ignore errors during cleanup
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Get or create a codebook entry for a value
|
|
506
|
+
*/
|
|
507
|
+
getOrCreateCodebookEntry(codebook, value) {
|
|
508
|
+
let vec = codebook.get(value);
|
|
509
|
+
if (!vec) {
|
|
510
|
+
vec = wasm_loader_js_1.Hypervector.random();
|
|
511
|
+
codebook.set(value, vec);
|
|
512
|
+
}
|
|
513
|
+
return vec;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Extract type string from pattern
|
|
517
|
+
*/
|
|
518
|
+
getPatternType(pattern) {
|
|
519
|
+
// Both TestPattern and SimplePattern have 'type' property
|
|
520
|
+
const patternType = pattern.type;
|
|
521
|
+
if (typeof patternType === 'string') {
|
|
522
|
+
return patternType;
|
|
523
|
+
}
|
|
524
|
+
// TestPattern.type is PatternType enum, SimplePattern.type is string
|
|
525
|
+
return String(patternType);
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Extract domain string from pattern
|
|
529
|
+
*/
|
|
530
|
+
getPatternDomain(pattern) {
|
|
531
|
+
if ('domain' in pattern) {
|
|
532
|
+
return pattern.domain;
|
|
533
|
+
}
|
|
534
|
+
if (this.isTestPattern(pattern) && 'category' in pattern) {
|
|
535
|
+
return typeof pattern.category === 'string'
|
|
536
|
+
? pattern.category
|
|
537
|
+
: String(pattern.category);
|
|
538
|
+
}
|
|
539
|
+
return 'unknown';
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Extract content string from pattern
|
|
543
|
+
*/
|
|
544
|
+
getPatternContent(pattern) {
|
|
545
|
+
if ('content' in pattern) {
|
|
546
|
+
return pattern.content;
|
|
547
|
+
}
|
|
548
|
+
if (this.isTestPattern(pattern) && 'name' in pattern) {
|
|
549
|
+
return pattern.name;
|
|
550
|
+
}
|
|
551
|
+
return '';
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Hash a string to a bigint seed for reproducible hypervector generation
|
|
555
|
+
* Uses FNV-1a hash algorithm for good distribution
|
|
556
|
+
*/
|
|
557
|
+
hashString(str) {
|
|
558
|
+
// FNV-1a 64-bit hash
|
|
559
|
+
const FNV_PRIME = BigInt('0x00000100000001B3');
|
|
560
|
+
const FNV_OFFSET = BigInt('0xcbf29ce484222325');
|
|
561
|
+
const MASK_64 = BigInt('0xFFFFFFFFFFFFFFFF');
|
|
562
|
+
let hash = FNV_OFFSET;
|
|
563
|
+
for (let i = 0; i < str.length; i++) {
|
|
564
|
+
hash ^= BigInt(str.charCodeAt(i));
|
|
565
|
+
hash = (hash * FNV_PRIME) & MASK_64;
|
|
566
|
+
}
|
|
567
|
+
return hash;
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Get the current configuration
|
|
571
|
+
*/
|
|
572
|
+
getConfig() {
|
|
573
|
+
return { ...this.config };
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Update configuration
|
|
577
|
+
* @param updates Partial configuration updates
|
|
578
|
+
*/
|
|
579
|
+
updateConfig(updates) {
|
|
580
|
+
Object.assign(this.config, updates);
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Get statistics about the memory state
|
|
584
|
+
*/
|
|
585
|
+
getStats() {
|
|
586
|
+
return {
|
|
587
|
+
initialized: this.initialized,
|
|
588
|
+
size: this.initialized ? this.size() : 0,
|
|
589
|
+
typeCodebookSize: this.typeCodebook.size,
|
|
590
|
+
domainCodebookSize: this.domainCodebook.size,
|
|
591
|
+
frameworkCodebookSize: this.frameworkCodebook.size,
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Dispose of WASM resources
|
|
596
|
+
* Call this when done using the adapter to free memory
|
|
597
|
+
*/
|
|
598
|
+
dispose() {
|
|
599
|
+
if (this.memory) {
|
|
600
|
+
this.memory.free();
|
|
601
|
+
this.memory = null;
|
|
602
|
+
}
|
|
603
|
+
// Free role vectors
|
|
604
|
+
const roleKeys = ['type', 'domain', 'content', 'framework'];
|
|
605
|
+
for (const key of roleKeys) {
|
|
606
|
+
if (this.roleVectors[key]) {
|
|
607
|
+
this.roleVectors[key].free();
|
|
608
|
+
this.roleVectors[key] = null;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
// Free codebook vectors
|
|
612
|
+
this.typeCodebook.forEach((vec) => vec.free());
|
|
613
|
+
this.typeCodebook.clear();
|
|
614
|
+
this.domainCodebook.forEach((vec) => vec.free());
|
|
615
|
+
this.domainCodebook.clear();
|
|
616
|
+
this.frameworkCodebook.forEach((vec) => vec.free());
|
|
617
|
+
this.frameworkCodebook.clear();
|
|
618
|
+
// Free stored pattern vectors
|
|
619
|
+
this.storedPatterns.forEach(({ vector }) => vector.free());
|
|
620
|
+
this.storedPatterns.clear();
|
|
621
|
+
this.initialized = false;
|
|
622
|
+
}
|
|
623
|
+
// ============================================
|
|
624
|
+
// Serialization Methods
|
|
625
|
+
// ============================================
|
|
626
|
+
/**
|
|
627
|
+
* Get codebooks for serialization
|
|
628
|
+
* @returns Object containing all codebooks
|
|
629
|
+
*/
|
|
630
|
+
getCodebooks() {
|
|
631
|
+
return {
|
|
632
|
+
type: this.typeCodebook,
|
|
633
|
+
domain: this.domainCodebook,
|
|
634
|
+
framework: this.frameworkCodebook,
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Get role vectors for serialization
|
|
639
|
+
* @returns Object containing all role vectors
|
|
640
|
+
*/
|
|
641
|
+
getRoleVectors() {
|
|
642
|
+
this.ensureInitialized();
|
|
643
|
+
return {
|
|
644
|
+
type: this.roleVectors.type,
|
|
645
|
+
domain: this.roleVectors.domain,
|
|
646
|
+
content: this.roleVectors.content,
|
|
647
|
+
framework: this.roleVectors.framework,
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Get all stored patterns for serialization
|
|
652
|
+
* @returns Map of pattern keys to vectors and metadata
|
|
653
|
+
*/
|
|
654
|
+
getStoredPatterns() {
|
|
655
|
+
this.ensureInitialized();
|
|
656
|
+
// Return the locally tracked patterns
|
|
657
|
+
return this.storedPatterns;
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Get the hypervector dimension
|
|
661
|
+
* @returns Dimension (default 10000)
|
|
662
|
+
*/
|
|
663
|
+
getDimension() {
|
|
664
|
+
return 10000; // Default HDC dimension
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Restore codebooks from serialized state
|
|
668
|
+
* @param codebooks Codebook maps to restore
|
|
669
|
+
*/
|
|
670
|
+
restoreCodebooks(codebooks) {
|
|
671
|
+
this.ensureInitialized();
|
|
672
|
+
// Free existing codebook vectors
|
|
673
|
+
this.typeCodebook.forEach((vec) => vec.free());
|
|
674
|
+
this.domainCodebook.forEach((vec) => vec.free());
|
|
675
|
+
this.frameworkCodebook.forEach((vec) => vec.free());
|
|
676
|
+
// Restore codebooks
|
|
677
|
+
this.typeCodebook = codebooks.type;
|
|
678
|
+
this.domainCodebook = codebooks.domain;
|
|
679
|
+
this.frameworkCodebook = codebooks.framework;
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Restore role vectors from serialized state
|
|
683
|
+
* @param roleVectors Role vectors to restore
|
|
684
|
+
*/
|
|
685
|
+
restoreRoleVectors(roleVectors) {
|
|
686
|
+
this.ensureInitialized();
|
|
687
|
+
// Free existing role vectors
|
|
688
|
+
if (this.roleVectors.type)
|
|
689
|
+
this.roleVectors.type.free();
|
|
690
|
+
if (this.roleVectors.domain)
|
|
691
|
+
this.roleVectors.domain.free();
|
|
692
|
+
if (this.roleVectors.content)
|
|
693
|
+
this.roleVectors.content.free();
|
|
694
|
+
if (this.roleVectors.framework)
|
|
695
|
+
this.roleVectors.framework.free();
|
|
696
|
+
// Restore role vectors
|
|
697
|
+
this.roleVectors.type = roleVectors.type;
|
|
698
|
+
this.roleVectors.domain = roleVectors.domain;
|
|
699
|
+
this.roleVectors.content = roleVectors.content;
|
|
700
|
+
this.roleVectors.framework = roleVectors.framework;
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Restore patterns from serialized state
|
|
704
|
+
* @param patterns Map of patterns to restore
|
|
705
|
+
*/
|
|
706
|
+
restorePatterns(patterns) {
|
|
707
|
+
this.ensureInitialized();
|
|
708
|
+
// Restore each pattern to the HDC memory
|
|
709
|
+
for (const [key, { vector }] of patterns) {
|
|
710
|
+
this.store(key, vector);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
exports.HdcMemoryAdapter = HdcMemoryAdapter;
|
|
715
|
+
//# sourceMappingURL=HdcMemoryAdapter.js.map
|