@soulcraft/brainy 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +53 -3
- package/README.md +427 -111
- package/bin/brainy.js +340 -62
- package/dist/api/ConfigAPI.d.ts +67 -0
- package/dist/api/ConfigAPI.js +166 -0
- package/dist/api/DataAPI.d.ts +123 -0
- package/dist/api/DataAPI.js +391 -0
- package/dist/api/SecurityAPI.d.ts +50 -0
- package/dist/api/SecurityAPI.js +139 -0
- package/dist/api/UniversalImportAPI.d.ts +134 -0
- package/dist/api/UniversalImportAPI.js +615 -0
- package/dist/augmentationManager.js +12 -7
- package/dist/augmentationPipeline.d.ts +0 -61
- package/dist/augmentationPipeline.js +0 -87
- package/dist/augmentationRegistry.d.ts +1 -1
- package/dist/augmentationRegistry.js +1 -1
- package/dist/augmentations/apiServerAugmentation.d.ts +27 -1
- package/dist/augmentations/apiServerAugmentation.js +290 -9
- package/dist/augmentations/auditLogAugmentation.d.ts +109 -0
- package/dist/augmentations/auditLogAugmentation.js +358 -0
- package/dist/augmentations/batchProcessingAugmentation.d.ts +3 -2
- package/dist/augmentations/batchProcessingAugmentation.js +123 -22
- package/dist/augmentations/brainyAugmentation.d.ts +142 -8
- package/dist/augmentations/brainyAugmentation.js +179 -2
- package/dist/augmentations/cacheAugmentation.d.ts +8 -5
- package/dist/augmentations/cacheAugmentation.js +116 -17
- package/dist/augmentations/conduitAugmentations.d.ts +2 -2
- package/dist/augmentations/conduitAugmentations.js +2 -2
- package/dist/augmentations/configResolver.d.ts +122 -0
- package/dist/augmentations/configResolver.js +440 -0
- package/dist/augmentations/connectionPoolAugmentation.d.ts +3 -1
- package/dist/augmentations/connectionPoolAugmentation.js +37 -12
- package/dist/augmentations/defaultAugmentations.d.ts +14 -10
- package/dist/augmentations/defaultAugmentations.js +16 -11
- package/dist/augmentations/discovery/catalogDiscovery.d.ts +142 -0
- package/dist/augmentations/discovery/catalogDiscovery.js +249 -0
- package/dist/augmentations/discovery/localDiscovery.d.ts +84 -0
- package/dist/augmentations/discovery/localDiscovery.js +246 -0
- package/dist/augmentations/discovery/runtimeLoader.d.ts +97 -0
- package/dist/augmentations/discovery/runtimeLoader.js +337 -0
- package/dist/augmentations/discovery.d.ts +152 -0
- package/dist/augmentations/discovery.js +441 -0
- package/dist/augmentations/display/cache.d.ts +130 -0
- package/dist/augmentations/display/cache.js +319 -0
- package/dist/augmentations/display/fieldPatterns.d.ts +52 -0
- package/dist/augmentations/display/fieldPatterns.js +393 -0
- package/dist/augmentations/display/iconMappings.d.ts +57 -0
- package/dist/augmentations/display/iconMappings.js +68 -0
- package/dist/augmentations/display/intelligentComputation.d.ts +109 -0
- package/dist/augmentations/display/intelligentComputation.js +462 -0
- package/dist/augmentations/display/types.d.ts +203 -0
- package/dist/augmentations/display/types.js +7 -0
- package/dist/augmentations/entityRegistryAugmentation.d.ts +3 -1
- package/dist/augmentations/entityRegistryAugmentation.js +5 -1
- package/dist/augmentations/indexAugmentation.d.ts +5 -3
- package/dist/augmentations/indexAugmentation.js +5 -2
- package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +24 -7
- package/dist/augmentations/intelligentVerbScoringAugmentation.js +111 -27
- package/dist/augmentations/manifest.d.ts +176 -0
- package/dist/augmentations/manifest.js +8 -0
- package/dist/augmentations/marketplace/AugmentationMarketplace.d.ts +168 -0
- package/dist/augmentations/marketplace/AugmentationMarketplace.js +329 -0
- package/dist/augmentations/marketplace/cli.d.ts +47 -0
- package/dist/augmentations/marketplace/cli.js +265 -0
- package/dist/augmentations/metricsAugmentation.d.ts +3 -3
- package/dist/augmentations/metricsAugmentation.js +2 -2
- package/dist/augmentations/monitoringAugmentation.d.ts +3 -3
- package/dist/augmentations/monitoringAugmentation.js +2 -2
- package/dist/augmentations/neuralImport.d.ts +1 -1
- package/dist/augmentations/neuralImport.js +4 -4
- package/dist/augmentations/rateLimitAugmentation.d.ts +82 -0
- package/dist/augmentations/rateLimitAugmentation.js +321 -0
- package/dist/augmentations/requestDeduplicatorAugmentation.d.ts +2 -2
- package/dist/augmentations/requestDeduplicatorAugmentation.js +1 -1
- package/dist/augmentations/storageAugmentation.d.ts +1 -1
- package/dist/augmentations/storageAugmentation.js +2 -2
- package/dist/augmentations/storageAugmentations.d.ts +37 -8
- package/dist/augmentations/storageAugmentations.js +204 -15
- package/dist/augmentations/synapseAugmentation.d.ts +1 -1
- package/dist/augmentations/synapseAugmentation.js +35 -16
- package/dist/augmentations/typeMatching/brainyTypes.d.ts +83 -0
- package/dist/augmentations/typeMatching/brainyTypes.js +425 -0
- package/dist/augmentations/typeMatching/intelligentTypeMatcher.d.ts +39 -59
- package/dist/augmentations/typeMatching/intelligentTypeMatcher.js +103 -389
- package/dist/augmentations/universalDisplayAugmentation.d.ts +191 -0
- package/dist/augmentations/universalDisplayAugmentation.js +371 -0
- package/dist/brainy-unified.d.ts +106 -0
- package/dist/brainy-unified.js +327 -0
- package/dist/brainy.d.ts +277 -0
- package/dist/brainy.js +1241 -0
- package/dist/brainyData.d.ts +56 -111
- package/dist/brainyData.js +912 -756
- package/dist/brainyDataV3.d.ts +186 -0
- package/dist/brainyDataV3.js +337 -0
- package/dist/config/distributedPresets-new.d.ts +118 -0
- package/dist/config/distributedPresets-new.js +318 -0
- package/dist/config/distributedPresets.d.ts +118 -0
- package/dist/config/distributedPresets.js +318 -0
- package/dist/config/extensibleConfig.d.ts +99 -0
- package/dist/config/extensibleConfig.js +268 -0
- package/dist/config/index.d.ts +17 -0
- package/dist/config/index.js +35 -0
- package/dist/config/modelAutoConfig.d.ts +32 -0
- package/dist/config/modelAutoConfig.js +139 -0
- package/dist/config/modelPrecisionManager.d.ts +42 -0
- package/dist/config/modelPrecisionManager.js +98 -0
- package/dist/config/sharedConfigManager.d.ts +67 -0
- package/dist/config/sharedConfigManager.js +215 -0
- package/dist/config/storageAutoConfig.d.ts +41 -0
- package/dist/config/storageAutoConfig.js +328 -0
- package/dist/config/zeroConfig.d.ts +68 -0
- package/dist/config/zeroConfig.js +301 -0
- package/dist/cortex/backupRestore.d.ts +2 -2
- package/dist/cortex/backupRestore.js +85 -27
- package/dist/cortex/healthCheck.d.ts +2 -2
- package/dist/cortex/neuralImport.d.ts +2 -2
- package/dist/cortex/neuralImport.js +18 -13
- package/dist/cortex/performanceMonitor.d.ts +2 -2
- package/dist/critical/model-guardian.d.ts +4 -0
- package/dist/critical/model-guardian.js +31 -11
- package/dist/demo.d.ts +4 -4
- package/dist/demo.js +7 -7
- package/dist/distributed/cacheSync.d.ts +112 -0
- package/dist/distributed/cacheSync.js +265 -0
- package/dist/distributed/coordinator.d.ts +193 -0
- package/dist/distributed/coordinator.js +548 -0
- package/dist/distributed/httpTransport.d.ts +120 -0
- package/dist/distributed/httpTransport.js +446 -0
- package/dist/distributed/index.d.ts +8 -0
- package/dist/distributed/index.js +5 -0
- package/dist/distributed/networkTransport.d.ts +132 -0
- package/dist/distributed/networkTransport.js +633 -0
- package/dist/distributed/queryPlanner.d.ts +104 -0
- package/dist/distributed/queryPlanner.js +327 -0
- package/dist/distributed/readWriteSeparation.d.ts +134 -0
- package/dist/distributed/readWriteSeparation.js +350 -0
- package/dist/distributed/shardManager.d.ts +114 -0
- package/dist/distributed/shardManager.js +357 -0
- package/dist/distributed/shardMigration.d.ts +110 -0
- package/dist/distributed/shardMigration.js +289 -0
- package/dist/distributed/storageDiscovery.d.ts +160 -0
- package/dist/distributed/storageDiscovery.js +551 -0
- package/dist/embeddings/CachedEmbeddings.d.ts +40 -0
- package/dist/embeddings/CachedEmbeddings.js +146 -0
- package/dist/embeddings/EmbeddingManager.d.ts +102 -0
- package/dist/embeddings/EmbeddingManager.js +291 -0
- package/dist/embeddings/SingletonModelManager.d.ts +95 -0
- package/dist/embeddings/SingletonModelManager.js +220 -0
- package/dist/embeddings/index.d.ts +12 -0
- package/dist/embeddings/index.js +16 -0
- package/dist/embeddings/lightweight-embedder.d.ts +0 -1
- package/dist/embeddings/lightweight-embedder.js +4 -12
- package/dist/embeddings/model-manager.d.ts +11 -0
- package/dist/embeddings/model-manager.js +43 -7
- package/dist/embeddings/universal-memory-manager.d.ts +1 -1
- package/dist/embeddings/universal-memory-manager.js +27 -67
- package/dist/embeddings/worker-embedding.js +4 -8
- package/dist/errors/brainyError.d.ts +5 -1
- package/dist/errors/brainyError.js +12 -0
- package/dist/examples/basicUsage.js +7 -4
- package/dist/graph/graphAdjacencyIndex.d.ts +96 -0
- package/dist/graph/graphAdjacencyIndex.js +288 -0
- package/dist/graph/pathfinding.js +4 -2
- package/dist/hnsw/scaledHNSWSystem.js +11 -2
- package/dist/importManager.js +8 -5
- package/dist/index.d.ts +17 -22
- package/dist/index.js +37 -23
- package/dist/mcp/brainyMCPAdapter.d.ts +4 -4
- package/dist/mcp/brainyMCPAdapter.js +5 -5
- package/dist/mcp/brainyMCPService.d.ts +3 -3
- package/dist/mcp/brainyMCPService.js +3 -11
- package/dist/mcp/mcpAugmentationToolset.js +20 -30
- package/dist/neural/embeddedPatterns.d.ts +1 -1
- package/dist/neural/embeddedPatterns.js +2 -2
- package/dist/neural/entityExtractor.d.ts +65 -0
- package/dist/neural/entityExtractor.js +316 -0
- package/dist/neural/improvedNeuralAPI.d.ts +357 -0
- package/dist/neural/improvedNeuralAPI.js +2628 -0
- package/dist/neural/naturalLanguageProcessor.d.ts +155 -10
- package/dist/neural/naturalLanguageProcessor.js +941 -66
- package/dist/neural/naturalLanguageProcessorStatic.d.ts +2 -2
- package/dist/neural/naturalLanguageProcessorStatic.js +3 -3
- package/dist/neural/neuralAPI.js +8 -2
- package/dist/neural/patternLibrary.d.ts +57 -3
- package/dist/neural/patternLibrary.js +348 -13
- package/dist/neural/staticPatternMatcher.d.ts +2 -2
- package/dist/neural/staticPatternMatcher.js +2 -2
- package/dist/neural/types.d.ts +287 -0
- package/dist/neural/types.js +24 -0
- package/dist/shared/default-augmentations.d.ts +3 -3
- package/dist/shared/default-augmentations.js +5 -5
- package/dist/storage/adapters/baseStorageAdapter.d.ts +42 -0
- package/dist/storage/adapters/fileSystemStorage.d.ts +26 -2
- package/dist/storage/adapters/fileSystemStorage.js +218 -15
- package/dist/storage/adapters/memoryStorage.d.ts +4 -4
- package/dist/storage/adapters/memoryStorage.js +17 -12
- package/dist/storage/adapters/opfsStorage.d.ts +2 -2
- package/dist/storage/adapters/opfsStorage.js +2 -2
- package/dist/storage/adapters/s3CompatibleStorage.d.ts +2 -2
- package/dist/storage/adapters/s3CompatibleStorage.js +2 -2
- package/dist/storage/backwardCompatibility.d.ts +10 -78
- package/dist/storage/backwardCompatibility.js +17 -132
- package/dist/storage/baseStorage.d.ts +18 -2
- package/dist/storage/baseStorage.js +74 -3
- package/dist/storage/cacheManager.js +2 -2
- package/dist/storage/readOnlyOptimizations.js +8 -3
- package/dist/streaming/pipeline.d.ts +154 -0
- package/dist/streaming/pipeline.js +551 -0
- package/dist/triple/TripleIntelligence.d.ts +25 -110
- package/dist/triple/TripleIntelligence.js +4 -574
- package/dist/triple/TripleIntelligenceSystem.d.ts +159 -0
- package/dist/triple/TripleIntelligenceSystem.js +519 -0
- package/dist/types/apiTypes.d.ts +278 -0
- package/dist/types/apiTypes.js +33 -0
- package/dist/types/brainy.types.d.ts +308 -0
- package/dist/types/brainy.types.js +8 -0
- package/dist/types/brainyDataInterface.d.ts +5 -8
- package/dist/types/brainyDataInterface.js +2 -2
- package/dist/types/graphTypes.js +2 -2
- package/dist/universal/crypto.d.ts +11 -1
- package/dist/universal/crypto.js +24 -93
- package/dist/universal/events.d.ts +3 -2
- package/dist/universal/events.js +6 -75
- package/dist/universal/fs.d.ts +2 -3
- package/dist/universal/fs.js +5 -211
- package/dist/universal/path.d.ts +3 -2
- package/dist/universal/path.js +22 -78
- package/dist/universal/uuid.d.ts +1 -1
- package/dist/universal/uuid.js +1 -1
- package/dist/utils/brainyTypes.d.ts +217 -0
- package/dist/utils/brainyTypes.js +261 -0
- package/dist/utils/cacheAutoConfig.d.ts +3 -3
- package/dist/utils/embedding.d.ts +9 -4
- package/dist/utils/embedding.js +89 -26
- package/dist/utils/enhancedLogger.d.ts +104 -0
- package/dist/utils/enhancedLogger.js +232 -0
- package/dist/utils/hybridModelManager.d.ts +19 -28
- package/dist/utils/hybridModelManager.js +36 -200
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/intelligentTypeMapper.d.ts +60 -0
- package/dist/utils/intelligentTypeMapper.js +349 -0
- package/dist/utils/metadataIndex.d.ts +118 -1
- package/dist/utils/metadataIndex.js +539 -16
- package/dist/utils/nodeVersionCheck.d.ts +24 -0
- package/dist/utils/nodeVersionCheck.js +65 -0
- package/dist/utils/paramValidation.d.ts +39 -0
- package/dist/utils/paramValidation.js +192 -0
- package/dist/utils/rateLimiter.d.ts +160 -0
- package/dist/utils/rateLimiter.js +271 -0
- package/dist/utils/statistics.d.ts +4 -4
- package/dist/utils/statistics.js +3 -3
- package/dist/utils/structuredLogger.d.ts +146 -0
- package/dist/utils/structuredLogger.js +394 -0
- package/dist/utils/textEncoding.js +2 -1
- package/dist/utils/typeValidation.d.ts +59 -0
- package/dist/utils/typeValidation.js +374 -0
- package/dist/utils/version.js +19 -3
- package/package.json +15 -17
- package/scripts/download-models.cjs +94 -20
- package/dist/augmentations/walAugmentation.d.ts +0 -109
- package/dist/augmentations/walAugmentation.js +0 -516
- package/dist/browserFramework.d.ts +0 -15
- package/dist/browserFramework.js +0 -31
- package/dist/browserFramework.minimal.d.ts +0 -14
- package/dist/browserFramework.minimal.js +0 -31
- package/dist/chat/BrainyChat.d.ts +0 -121
- package/dist/chat/BrainyChat.js +0 -396
- package/dist/chat/ChatCLI.d.ts +0 -61
- package/dist/chat/ChatCLI.js +0 -351
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming Pipeline System for Brainy
|
|
3
|
+
*
|
|
4
|
+
* Real implementation of streaming data pipelines with:
|
|
5
|
+
* - Async iterators for streaming
|
|
6
|
+
* - Backpressure handling
|
|
7
|
+
* - Auto-scaling workers
|
|
8
|
+
* - Checkpointing for recovery
|
|
9
|
+
* - Error boundaries
|
|
10
|
+
*/
|
|
11
|
+
import { NounType } from '../types/graphTypes.js';
|
|
12
|
+
/**
|
|
13
|
+
* Streaming Pipeline Builder
|
|
14
|
+
*/
|
|
15
|
+
export class Pipeline {
|
|
16
|
+
constructor(brainyInstance) {
|
|
17
|
+
this.brainyInstance = brainyInstance;
|
|
18
|
+
this.stages = [];
|
|
19
|
+
this.running = false;
|
|
20
|
+
this.metrics = {
|
|
21
|
+
processed: 0,
|
|
22
|
+
errors: 0,
|
|
23
|
+
startTime: 0,
|
|
24
|
+
throughput: 0
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Add a data source
|
|
29
|
+
*/
|
|
30
|
+
source(generator) {
|
|
31
|
+
const stage = {
|
|
32
|
+
type: 'source',
|
|
33
|
+
name: 'source',
|
|
34
|
+
async *process() {
|
|
35
|
+
const source = typeof generator === 'function' ? generator() : generator;
|
|
36
|
+
for await (const item of source) {
|
|
37
|
+
yield item;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
this.stages.push(stage);
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Transform data
|
|
46
|
+
*/
|
|
47
|
+
map(fn) {
|
|
48
|
+
const stage = {
|
|
49
|
+
type: 'transform',
|
|
50
|
+
name: 'map',
|
|
51
|
+
async *process(input) {
|
|
52
|
+
for await (const item of input) {
|
|
53
|
+
yield await fn(item);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
this.stages.push(stage);
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Filter data
|
|
62
|
+
*/
|
|
63
|
+
filter(predicate) {
|
|
64
|
+
const stage = {
|
|
65
|
+
type: 'filter',
|
|
66
|
+
name: 'filter',
|
|
67
|
+
async *process(input) {
|
|
68
|
+
for await (const item of input) {
|
|
69
|
+
if (await predicate(item)) {
|
|
70
|
+
yield item;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
this.stages.push(stage);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Batch items for efficiency
|
|
80
|
+
*/
|
|
81
|
+
batch(size, timeoutMs) {
|
|
82
|
+
const stage = {
|
|
83
|
+
type: 'batch',
|
|
84
|
+
name: 'batch',
|
|
85
|
+
async *process(input) {
|
|
86
|
+
let batch = [];
|
|
87
|
+
let timer = null;
|
|
88
|
+
const flush = () => {
|
|
89
|
+
if (batch.length > 0) {
|
|
90
|
+
const result = [...batch];
|
|
91
|
+
batch = [];
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
};
|
|
96
|
+
for await (const item of input) {
|
|
97
|
+
batch.push(item);
|
|
98
|
+
if (batch.length >= size) {
|
|
99
|
+
const result = flush();
|
|
100
|
+
if (result)
|
|
101
|
+
yield result;
|
|
102
|
+
}
|
|
103
|
+
else if (timeoutMs && !timer) {
|
|
104
|
+
timer = setTimeout(() => {
|
|
105
|
+
timer = null;
|
|
106
|
+
const result = flush();
|
|
107
|
+
if (result) {
|
|
108
|
+
// Note: This won't work perfectly in async iterator
|
|
109
|
+
// In production, use a proper queue
|
|
110
|
+
batch = [result];
|
|
111
|
+
}
|
|
112
|
+
}, timeoutMs);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Flush remaining
|
|
116
|
+
const result = flush();
|
|
117
|
+
if (result)
|
|
118
|
+
yield result;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
this.stages.push(stage);
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Sink data to a destination
|
|
126
|
+
*/
|
|
127
|
+
sink(handler) {
|
|
128
|
+
const stage = {
|
|
129
|
+
type: 'sink',
|
|
130
|
+
name: 'sink',
|
|
131
|
+
async *process(input) {
|
|
132
|
+
for await (const item of input) {
|
|
133
|
+
await handler(item);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
this.stages.push(stage);
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Sink data to Brainy
|
|
142
|
+
*/
|
|
143
|
+
toBrainy(options) {
|
|
144
|
+
if (!this.brainyInstance) {
|
|
145
|
+
throw new Error('Brainy instance required for toBrainy sink');
|
|
146
|
+
}
|
|
147
|
+
const brain = this.brainyInstance;
|
|
148
|
+
const batchSize = options?.batchSize || 100;
|
|
149
|
+
return this.batch(batchSize).sink(async (batch) => {
|
|
150
|
+
// Handle both Brainy 3.0 and Brainy APIs
|
|
151
|
+
if ('add' in brain) {
|
|
152
|
+
// Brainy 3.0 API
|
|
153
|
+
for (const item of batch) {
|
|
154
|
+
await brain.add({
|
|
155
|
+
data: item,
|
|
156
|
+
type: options?.type || NounType.Document,
|
|
157
|
+
metadata: options?.metadata
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
// Brainy API - use add method
|
|
163
|
+
for (const item of batch) {
|
|
164
|
+
await brain.add({
|
|
165
|
+
data: item,
|
|
166
|
+
type: (options?.type || 'document'), // Type coercion since pipeline accepts string
|
|
167
|
+
metadata: options?.metadata
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Sink with rate limiting
|
|
175
|
+
*/
|
|
176
|
+
throttledSink(handler, rateLimit) {
|
|
177
|
+
let lastTime = Date.now();
|
|
178
|
+
const minInterval = 1000 / rateLimit;
|
|
179
|
+
const stage = {
|
|
180
|
+
type: 'sink',
|
|
181
|
+
name: 'throttledSink',
|
|
182
|
+
async *process(input) {
|
|
183
|
+
for await (const item of input) {
|
|
184
|
+
const now = Date.now();
|
|
185
|
+
const elapsed = now - lastTime;
|
|
186
|
+
if (elapsed < minInterval) {
|
|
187
|
+
await new Promise(resolve => setTimeout(resolve, minInterval - elapsed));
|
|
188
|
+
}
|
|
189
|
+
await handler(item);
|
|
190
|
+
lastTime = Date.now();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
this.stages.push(stage);
|
|
195
|
+
return this;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Parallel sink with worker pool
|
|
199
|
+
*/
|
|
200
|
+
parallelSink(handler, workers = 4) {
|
|
201
|
+
const stage = {
|
|
202
|
+
type: 'sink',
|
|
203
|
+
name: 'parallelSink',
|
|
204
|
+
async *process(input) {
|
|
205
|
+
const queue = [];
|
|
206
|
+
for await (const item of input) {
|
|
207
|
+
// Add to queue
|
|
208
|
+
const promise = Promise.resolve(handler(item));
|
|
209
|
+
queue.push(promise);
|
|
210
|
+
// Maintain worker pool size
|
|
211
|
+
if (queue.length >= workers) {
|
|
212
|
+
await Promise.race(queue);
|
|
213
|
+
// Remove completed promises
|
|
214
|
+
for (let i = queue.length - 1; i >= 0; i--) {
|
|
215
|
+
if (await Promise.race([queue[i], Promise.resolve('pending')]) !== 'pending') {
|
|
216
|
+
queue.splice(i, 1);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
// Wait for remaining work
|
|
222
|
+
await Promise.all(queue);
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
this.stages.push(stage);
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Collect all results
|
|
230
|
+
*/
|
|
231
|
+
async collect() {
|
|
232
|
+
const results = [];
|
|
233
|
+
await this.sink(async (item) => {
|
|
234
|
+
results.push(item);
|
|
235
|
+
}).run();
|
|
236
|
+
return results;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Window operations for time-based processing
|
|
240
|
+
*/
|
|
241
|
+
window(size, type = 'tumbling') {
|
|
242
|
+
const stage = {
|
|
243
|
+
type: 'window',
|
|
244
|
+
name: 'window',
|
|
245
|
+
async *process(input) {
|
|
246
|
+
const window = [];
|
|
247
|
+
for await (const item of input) {
|
|
248
|
+
window.push(item);
|
|
249
|
+
if (type === 'sliding') {
|
|
250
|
+
if (window.length > size) {
|
|
251
|
+
window.shift();
|
|
252
|
+
}
|
|
253
|
+
if (window.length === size) {
|
|
254
|
+
yield [...window];
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
// Tumbling window
|
|
259
|
+
if (window.length >= size) {
|
|
260
|
+
yield [...window];
|
|
261
|
+
window.length = 0;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
// Emit remaining items
|
|
266
|
+
if (window.length > 0 && type === 'tumbling') {
|
|
267
|
+
yield window;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
this.stages.push(stage);
|
|
272
|
+
return this;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Flatmap operation - map and flatten results
|
|
276
|
+
*/
|
|
277
|
+
flatMap(fn) {
|
|
278
|
+
const stage = {
|
|
279
|
+
type: 'transform',
|
|
280
|
+
name: 'flatMap',
|
|
281
|
+
async *process(input) {
|
|
282
|
+
for await (const item of input) {
|
|
283
|
+
const results = await fn(item);
|
|
284
|
+
for (const result of results) {
|
|
285
|
+
yield result;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
this.stages.push(stage);
|
|
291
|
+
return this;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Tap into the pipeline for side effects without modifying data
|
|
295
|
+
*/
|
|
296
|
+
tap(fn) {
|
|
297
|
+
const stage = {
|
|
298
|
+
type: 'transform',
|
|
299
|
+
name: 'tap',
|
|
300
|
+
async *process(input) {
|
|
301
|
+
for await (const item of input) {
|
|
302
|
+
await fn(item);
|
|
303
|
+
yield item;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
this.stages.push(stage);
|
|
308
|
+
return this;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Retry failed operations
|
|
312
|
+
*/
|
|
313
|
+
retry(fn, maxRetries = 3, backoff = 1000) {
|
|
314
|
+
const stage = {
|
|
315
|
+
type: 'transform',
|
|
316
|
+
name: 'retry',
|
|
317
|
+
async *process(input) {
|
|
318
|
+
for await (const item of input) {
|
|
319
|
+
let retries = 0;
|
|
320
|
+
let lastError;
|
|
321
|
+
while (retries <= maxRetries) {
|
|
322
|
+
try {
|
|
323
|
+
yield await fn(item);
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
lastError = error;
|
|
328
|
+
retries++;
|
|
329
|
+
if (retries <= maxRetries) {
|
|
330
|
+
await new Promise(resolve => setTimeout(resolve, backoff * Math.pow(2, retries - 1)));
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (retries > maxRetries && lastError) {
|
|
335
|
+
throw lastError;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
this.stages.push(stage);
|
|
341
|
+
return this;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Buffer with backpressure handling
|
|
345
|
+
*/
|
|
346
|
+
buffer(size, strategy = 'block') {
|
|
347
|
+
const stage = {
|
|
348
|
+
type: 'transform',
|
|
349
|
+
name: 'buffer',
|
|
350
|
+
async *process(input) {
|
|
351
|
+
const buffer = [];
|
|
352
|
+
let consuming = false;
|
|
353
|
+
const consume = async function* () {
|
|
354
|
+
while (buffer.length > 0) {
|
|
355
|
+
yield buffer.shift();
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
for await (const item of input) {
|
|
359
|
+
if (buffer.length >= size) {
|
|
360
|
+
if (strategy === 'drop') {
|
|
361
|
+
// Drop oldest item
|
|
362
|
+
buffer.shift();
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
// Block until buffer has space
|
|
366
|
+
if (!consuming) {
|
|
367
|
+
consuming = true;
|
|
368
|
+
for await (const buffered of consume()) {
|
|
369
|
+
yield buffered;
|
|
370
|
+
if (buffer.length < size / 2)
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
consuming = false;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
buffer.push(item);
|
|
378
|
+
}
|
|
379
|
+
// Flush remaining buffer
|
|
380
|
+
for (const item of buffer) {
|
|
381
|
+
yield item;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
this.stages.push(stage);
|
|
386
|
+
return this;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Fork the pipeline into multiple branches
|
|
390
|
+
*/
|
|
391
|
+
fork(...branches) {
|
|
392
|
+
const brainyRef = this.brainyInstance;
|
|
393
|
+
const stage = {
|
|
394
|
+
type: 'branch',
|
|
395
|
+
name: 'fork',
|
|
396
|
+
async *process(input) {
|
|
397
|
+
const buffers = branches.map(() => []);
|
|
398
|
+
for await (const item of input) {
|
|
399
|
+
// Distribute items to all branches
|
|
400
|
+
for (let i = 0; i < branches.length; i++) {
|
|
401
|
+
buffers[i].push(item);
|
|
402
|
+
}
|
|
403
|
+
yield item;
|
|
404
|
+
}
|
|
405
|
+
// Process branches in parallel
|
|
406
|
+
await Promise.all(branches.map(async (branch, i) => {
|
|
407
|
+
const branchPipeline = new Pipeline(brainyRef);
|
|
408
|
+
const configured = branch(branchPipeline);
|
|
409
|
+
// Create async iterable from buffer
|
|
410
|
+
const source = async function* () {
|
|
411
|
+
for (const item of buffers[i]) {
|
|
412
|
+
yield item;
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
await configured.source(source()).run();
|
|
416
|
+
}));
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
this.stages.push(stage);
|
|
420
|
+
return this;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Reduce operation
|
|
424
|
+
*/
|
|
425
|
+
reduce(reducer, initial) {
|
|
426
|
+
const stage = {
|
|
427
|
+
type: 'reduce',
|
|
428
|
+
name: 'reduce',
|
|
429
|
+
async *process(input) {
|
|
430
|
+
let accumulator = initial;
|
|
431
|
+
for await (const item of input) {
|
|
432
|
+
accumulator = reducer(accumulator, item);
|
|
433
|
+
}
|
|
434
|
+
yield accumulator;
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
this.stages.push(stage);
|
|
438
|
+
return this;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Run the pipeline with metrics tracking
|
|
442
|
+
*/
|
|
443
|
+
async run(options = {}) {
|
|
444
|
+
if (this.running) {
|
|
445
|
+
throw new Error('Pipeline is already running');
|
|
446
|
+
}
|
|
447
|
+
this.running = true;
|
|
448
|
+
this.abortController = new AbortController();
|
|
449
|
+
this.metrics.startTime = Date.now();
|
|
450
|
+
this.metrics.processed = 0;
|
|
451
|
+
this.metrics.errors = 0;
|
|
452
|
+
const { errorHandler, bufferSize = 1000 } = options;
|
|
453
|
+
try {
|
|
454
|
+
// Build the pipeline chain
|
|
455
|
+
let stream = undefined;
|
|
456
|
+
for (const stage of this.stages) {
|
|
457
|
+
if (stage.type === 'source') {
|
|
458
|
+
stream = stage.process(undefined);
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
stream = stage.process(stream);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
// Execute the pipeline with error handling
|
|
465
|
+
if (stream) {
|
|
466
|
+
for await (const item of stream) {
|
|
467
|
+
try {
|
|
468
|
+
this.metrics.processed++;
|
|
469
|
+
// Calculate throughput
|
|
470
|
+
const elapsed = (Date.now() - this.metrics.startTime) / 1000;
|
|
471
|
+
this.metrics.throughput = this.metrics.processed / elapsed;
|
|
472
|
+
// Check abort signal
|
|
473
|
+
if (this.abortController.signal.aborted) {
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
// Backpressure handling
|
|
477
|
+
if (options.maxThroughput && this.metrics.throughput > options.maxThroughput) {
|
|
478
|
+
const delay = 1000 / options.maxThroughput;
|
|
479
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
catch (error) {
|
|
483
|
+
this.metrics.errors++;
|
|
484
|
+
if (errorHandler) {
|
|
485
|
+
errorHandler(error, item);
|
|
486
|
+
}
|
|
487
|
+
else {
|
|
488
|
+
throw error;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
finally {
|
|
495
|
+
this.running = false;
|
|
496
|
+
this.abortController = undefined;
|
|
497
|
+
// Log final metrics
|
|
498
|
+
if (options.monitoring) {
|
|
499
|
+
const elapsed = (Date.now() - this.metrics.startTime) / 1000;
|
|
500
|
+
console.log(`Pipeline completed: ${this.metrics.processed} items in ${elapsed.toFixed(2)}s`);
|
|
501
|
+
console.log(`Throughput: ${this.metrics.throughput.toFixed(2)} items/sec`);
|
|
502
|
+
if (this.metrics.errors > 0) {
|
|
503
|
+
console.log(`Errors: ${this.metrics.errors}`);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Start the pipeline (alias for run)
|
|
510
|
+
*/
|
|
511
|
+
async start(options = {}) {
|
|
512
|
+
return this.run(options);
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Stop the pipeline
|
|
516
|
+
*/
|
|
517
|
+
stop() {
|
|
518
|
+
if (this.abortController) {
|
|
519
|
+
this.abortController.abort();
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Monitor pipeline metrics
|
|
524
|
+
*/
|
|
525
|
+
monitor(dashboard) {
|
|
526
|
+
// In production, this would connect to monitoring service
|
|
527
|
+
console.log(`Monitoring enabled${dashboard ? ` with dashboard: ${dashboard}` : ''}`);
|
|
528
|
+
return this;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Pipeline factory function
|
|
533
|
+
*/
|
|
534
|
+
export function createPipeline(brain) {
|
|
535
|
+
return new Pipeline(brain);
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Backward compatibility exports
|
|
539
|
+
*/
|
|
540
|
+
export const pipeline = createPipeline();
|
|
541
|
+
// Execution modes for backward compatibility (deprecated)
|
|
542
|
+
export var ExecutionMode;
|
|
543
|
+
(function (ExecutionMode) {
|
|
544
|
+
ExecutionMode["SEQUENTIAL"] = "sequential";
|
|
545
|
+
ExecutionMode["PARALLEL"] = "parallel";
|
|
546
|
+
ExecutionMode["FIRST_SUCCESS"] = "firstSuccess";
|
|
547
|
+
ExecutionMode["FIRST_RESULT"] = "firstResult";
|
|
548
|
+
ExecutionMode["THREADED"] = "threaded";
|
|
549
|
+
})(ExecutionMode || (ExecutionMode = {}));
|
|
550
|
+
export { ExecutionMode as StreamlinedExecutionMode };
|
|
551
|
+
//# sourceMappingURL=pipeline.js.map
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Triple Intelligence
|
|
3
|
-
*
|
|
2
|
+
* Triple Intelligence Types
|
|
3
|
+
* Defines the query and result types for Triple Intelligence
|
|
4
4
|
*
|
|
5
|
-
*
|
|
5
|
+
* The actual implementation is in TripleIntelligenceSystem
|
|
6
6
|
*/
|
|
7
|
-
import { Vector
|
|
8
|
-
import { BrainyData } from '../brainyData.js';
|
|
7
|
+
import { Vector } from '../coreTypes.js';
|
|
9
8
|
export interface TripleQuery {
|
|
10
9
|
like?: string | Vector | any;
|
|
11
10
|
similar?: string | Vector | any;
|
|
@@ -20,119 +19,35 @@ export interface TripleQuery {
|
|
|
20
19
|
where?: Record<string, any>;
|
|
21
20
|
limit?: number;
|
|
22
21
|
offset?: number;
|
|
23
|
-
mode?: 'auto' | 'vector' | 'graph' | 'metadata' | 'fusion';
|
|
24
|
-
boost?: 'recent' | 'popular' | 'verified' | string;
|
|
25
22
|
explain?: boolean;
|
|
26
|
-
|
|
23
|
+
boost?: {
|
|
24
|
+
vector?: number;
|
|
25
|
+
graph?: number;
|
|
26
|
+
field?: number;
|
|
27
|
+
};
|
|
27
28
|
}
|
|
28
|
-
export interface TripleResult
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
fusionScore: number;
|
|
29
|
+
export interface TripleResult {
|
|
30
|
+
id: string;
|
|
31
|
+
score: number;
|
|
32
|
+
entity?: any;
|
|
33
33
|
explanation?: {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
vectorScore?: number;
|
|
35
|
+
graphScore?: number;
|
|
36
|
+
fieldScore?: number;
|
|
37
|
+
path?: string[];
|
|
37
38
|
};
|
|
38
39
|
}
|
|
39
40
|
export interface QueryPlan {
|
|
40
|
-
|
|
41
|
+
strategy: 'parallel' | 'progressive';
|
|
42
|
+
steps: Array<{
|
|
43
|
+
type: 'vector' | 'graph' | 'field';
|
|
44
|
+
cost: number;
|
|
45
|
+
expected: number;
|
|
46
|
+
}>;
|
|
41
47
|
canParallelize: boolean;
|
|
42
48
|
estimatedCost: number;
|
|
43
|
-
steps: QueryStep[];
|
|
44
|
-
}
|
|
45
|
-
export interface QueryStep {
|
|
46
|
-
type: 'vector' | 'graph' | 'field' | 'fusion';
|
|
47
|
-
operation: string;
|
|
48
|
-
estimated: number;
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
|
-
*
|
|
52
|
-
* Unifies vector, graph, and field search into one beautiful API
|
|
51
|
+
* @deprecated Use brain.getTripleIntelligence() directly to get TripleIntelligenceSystem
|
|
53
52
|
*/
|
|
54
|
-
export
|
|
55
|
-
private brain;
|
|
56
|
-
private planCache;
|
|
57
|
-
constructor(brain: BrainyData);
|
|
58
|
-
/**
|
|
59
|
-
* The magic happens here - one query to rule them all
|
|
60
|
-
*/
|
|
61
|
-
find(query: TripleQuery): Promise<TripleResult[]>;
|
|
62
|
-
/**
|
|
63
|
-
* Generate optimal execution plan based on query shape
|
|
64
|
-
*/
|
|
65
|
-
private optimizeQuery;
|
|
66
|
-
/**
|
|
67
|
-
* Execute searches in parallel for maximum speed
|
|
68
|
-
*/
|
|
69
|
-
private parallelSearch;
|
|
70
|
-
/**
|
|
71
|
-
* Progressive filtering for efficiency
|
|
72
|
-
*/
|
|
73
|
-
private progressiveSearch;
|
|
74
|
-
/**
|
|
75
|
-
* Vector similarity search
|
|
76
|
-
*/
|
|
77
|
-
private vectorSearch;
|
|
78
|
-
/**
|
|
79
|
-
* Graph traversal
|
|
80
|
-
*/
|
|
81
|
-
private graphTraversal;
|
|
82
|
-
/**
|
|
83
|
-
* Field-based filtering
|
|
84
|
-
*/
|
|
85
|
-
private fieldFilter;
|
|
86
|
-
/**
|
|
87
|
-
* Fallback manual metadata filtering when index is not available
|
|
88
|
-
*/
|
|
89
|
-
private manualMetadataFilter;
|
|
90
|
-
/**
|
|
91
|
-
* Fusion ranking combines all signals
|
|
92
|
-
*/
|
|
93
|
-
private fusionRank;
|
|
94
|
-
/**
|
|
95
|
-
* Check if a filter is selective enough to use first
|
|
96
|
-
*/
|
|
97
|
-
private isSelectiveFilter;
|
|
98
|
-
/**
|
|
99
|
-
* Apply field filter to existing candidates
|
|
100
|
-
*/
|
|
101
|
-
private applyFieldFilter;
|
|
102
|
-
/**
|
|
103
|
-
* Check a single condition
|
|
104
|
-
*/
|
|
105
|
-
private checkCondition;
|
|
106
|
-
/**
|
|
107
|
-
* Vector search within specific candidates
|
|
108
|
-
*/
|
|
109
|
-
private vectorSearchWithin;
|
|
110
|
-
/**
|
|
111
|
-
* Expand graph from candidates
|
|
112
|
-
*/
|
|
113
|
-
private graphExpand;
|
|
114
|
-
/**
|
|
115
|
-
* Apply boost strategies
|
|
116
|
-
*/
|
|
117
|
-
private applyBoosts;
|
|
118
|
-
/**
|
|
119
|
-
* Add query explanations for debugging
|
|
120
|
-
*/
|
|
121
|
-
private addExplanations;
|
|
122
|
-
/**
|
|
123
|
-
* Optimize plan based on historical patterns
|
|
124
|
-
*/
|
|
125
|
-
/**
|
|
126
|
-
* Execute single signal query without fusion
|
|
127
|
-
*/
|
|
128
|
-
private executeSingleSignal;
|
|
129
|
-
/**
|
|
130
|
-
* Clear query optimization cache
|
|
131
|
-
*/
|
|
132
|
-
clearCache(): void;
|
|
133
|
-
/**
|
|
134
|
-
* Get optimization statistics
|
|
135
|
-
*/
|
|
136
|
-
getStats(): any;
|
|
137
|
-
}
|
|
138
|
-
export declare function find(brain: BrainyData, query: TripleQuery): Promise<TripleResult[]>;
|
|
53
|
+
export type TripleIntelligenceEngine = any;
|