@soulcraft/brainy 3.47.1 → 3.48.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.
@@ -0,0 +1,213 @@
1
+ /**
2
+ * Cloudflare R2 Storage Adapter (Dedicated)
3
+ * Optimized specifically for Cloudflare R2 with all latest features
4
+ *
5
+ * R2-Specific Optimizations:
6
+ * - Zero egress fees (aggressive caching)
7
+ * - Cloudflare global network (edge-aware routing)
8
+ * - Workers integration (optional edge compute)
9
+ * - High-volume mode for bulk operations
10
+ * - Smart batching and backpressure
11
+ *
12
+ * Based on latest GCS and S3 implementations with R2-specific enhancements
13
+ */
14
+ import { GraphVerb, HNSWNoun, HNSWVerb, StatisticsData } from '../../coreTypes.js';
15
+ import { BaseStorage } from '../baseStorage.js';
16
+ type HNSWNode = HNSWNoun;
17
+ type Edge = HNSWVerb;
18
+ /**
19
+ * Dedicated Cloudflare R2 storage adapter
20
+ * Optimized for R2's unique characteristics and global edge network
21
+ *
22
+ * Configuration:
23
+ * ```typescript
24
+ * const r2Storage = new R2Storage({
25
+ * bucketName: 'my-brainy-data',
26
+ * accountId: 'YOUR_CLOUDFLARE_ACCOUNT_ID',
27
+ * accessKeyId: 'YOUR_R2_ACCESS_KEY_ID',
28
+ * secretAccessKey: 'YOUR_R2_SECRET_ACCESS_KEY'
29
+ * })
30
+ * ```
31
+ */
32
+ export declare class R2Storage extends BaseStorage {
33
+ private s3Client;
34
+ private bucketName;
35
+ private accountId;
36
+ private accessKeyId;
37
+ private secretAccessKey;
38
+ private endpoint;
39
+ private nounPrefix;
40
+ private verbPrefix;
41
+ private metadataPrefix;
42
+ private verbMetadataPrefix;
43
+ private systemPrefix;
44
+ protected statisticsCache: StatisticsData | null;
45
+ private pendingOperations;
46
+ private maxConcurrentOperations;
47
+ private baseBatchSize;
48
+ private currentBatchSize;
49
+ private lastMemoryCheck;
50
+ private memoryCheckInterval;
51
+ private backpressure;
52
+ private nounWriteBuffer;
53
+ private verbWriteBuffer;
54
+ private requestCoalescer;
55
+ private highVolumeMode;
56
+ private lastVolumeCheck;
57
+ private volumeCheckInterval;
58
+ private forceHighVolumeMode;
59
+ private nounCacheManager;
60
+ private verbCacheManager;
61
+ private logger;
62
+ /**
63
+ * Initialize the R2 storage adapter
64
+ * @param options Configuration options for Cloudflare R2
65
+ */
66
+ constructor(options: {
67
+ bucketName: string;
68
+ accountId: string;
69
+ accessKeyId: string;
70
+ secretAccessKey: string;
71
+ cacheConfig?: {
72
+ hotCacheMaxSize?: number;
73
+ hotCacheEvictionThreshold?: number;
74
+ warmCacheTTL?: number;
75
+ };
76
+ readOnly?: boolean;
77
+ });
78
+ /**
79
+ * Initialize the storage adapter
80
+ */
81
+ init(): Promise<void>;
82
+ /**
83
+ * Get the R2 object key for a noun using UUID-based sharding
84
+ */
85
+ private getNounKey;
86
+ /**
87
+ * Get the R2 object key for a verb using UUID-based sharding
88
+ */
89
+ private getVerbKey;
90
+ /**
91
+ * Override base class method to detect R2-specific throttling errors
92
+ */
93
+ protected isThrottlingError(error: any): boolean;
94
+ /**
95
+ * Override base class to enable smart batching for cloud storage
96
+ * R2 is cloud storage with network latency benefits from batching
97
+ */
98
+ protected isCloudStorage(): boolean;
99
+ /**
100
+ * Apply backpressure before starting an operation
101
+ */
102
+ private applyBackpressure;
103
+ /**
104
+ * Release backpressure after completing an operation
105
+ */
106
+ private releaseBackpressure;
107
+ /**
108
+ * Check if high-volume mode should be enabled
109
+ */
110
+ private checkVolumeMode;
111
+ /**
112
+ * Flush noun buffer to R2
113
+ */
114
+ private flushNounBuffer;
115
+ /**
116
+ * Flush verb buffer to R2
117
+ */
118
+ private flushVerbBuffer;
119
+ /**
120
+ * Save a noun to storage (internal implementation)
121
+ */
122
+ protected saveNoun_internal(noun: HNSWNoun): Promise<void>;
123
+ /**
124
+ * Save a node to storage
125
+ */
126
+ protected saveNode(node: HNSWNode): Promise<void>;
127
+ /**
128
+ * Save a node directly to R2 (bypass buffer)
129
+ */
130
+ private saveNodeDirect;
131
+ /**
132
+ * Get a noun from storage (internal implementation)
133
+ */
134
+ protected getNoun_internal(id: string): Promise<HNSWNoun | null>;
135
+ /**
136
+ * Get a node from storage
137
+ */
138
+ protected getNode(id: string): Promise<HNSWNode | null>;
139
+ /**
140
+ * Delete a noun from storage (internal implementation)
141
+ */
142
+ protected deleteNoun_internal(id: string): Promise<void>;
143
+ /**
144
+ * Write an object to a specific path in R2
145
+ */
146
+ protected writeObjectToPath(path: string, data: any): Promise<void>;
147
+ /**
148
+ * Read an object from a specific path in R2
149
+ */
150
+ protected readObjectFromPath(path: string): Promise<any | null>;
151
+ /**
152
+ * Delete an object from a specific path in R2
153
+ */
154
+ protected deleteObjectFromPath(path: string): Promise<void>;
155
+ /**
156
+ * List all objects under a specific prefix in R2
157
+ */
158
+ protected listObjectsUnderPath(prefix: string): Promise<string[]>;
159
+ protected saveVerb_internal(verb: HNSWVerb): Promise<void>;
160
+ protected saveEdge(edge: Edge): Promise<void>;
161
+ private saveEdgeDirect;
162
+ protected getVerb_internal(id: string): Promise<HNSWVerb | null>;
163
+ protected getEdge(id: string): Promise<Edge | null>;
164
+ protected deleteVerb_internal(id: string): Promise<void>;
165
+ protected initializeCounts(): Promise<void>;
166
+ private initializeCountsFromScan;
167
+ protected persistCounts(): Promise<void>;
168
+ getNounVector(id: string): Promise<number[] | null>;
169
+ saveHNSWData(nounId: string, hnswData: {
170
+ level: number;
171
+ connections: Record<string, string[]>;
172
+ }): Promise<void>;
173
+ getHNSWData(nounId: string): Promise<{
174
+ level: number;
175
+ connections: Record<string, string[]>;
176
+ } | null>;
177
+ saveHNSWSystem(systemData: {
178
+ entryPointId: string | null;
179
+ maxLevel: number;
180
+ }): Promise<void>;
181
+ getHNSWSystem(): Promise<{
182
+ entryPointId: string | null;
183
+ maxLevel: number;
184
+ } | null>;
185
+ protected saveStatisticsData(statistics: StatisticsData): Promise<void>;
186
+ protected getStatisticsData(): Promise<StatisticsData | null>;
187
+ clear(): Promise<void>;
188
+ getStorageStatus(): Promise<{
189
+ type: string;
190
+ used: number;
191
+ quota: number | null;
192
+ details?: Record<string, any>;
193
+ }>;
194
+ getNounsWithPagination(options?: {
195
+ limit?: number;
196
+ cursor?: string;
197
+ filter?: {
198
+ nounType?: string | string[];
199
+ service?: string | string[];
200
+ metadata?: Record<string, any>;
201
+ };
202
+ }): Promise<{
203
+ items: HNSWNoun[];
204
+ totalCount?: number;
205
+ hasMore: boolean;
206
+ nextCursor?: string;
207
+ }>;
208
+ protected getNounsByNounType_internal(nounType: string): Promise<HNSWNoun[]>;
209
+ protected getVerbsBySource_internal(sourceId: string): Promise<GraphVerb[]>;
210
+ protected getVerbsByTarget_internal(targetId: string): Promise<GraphVerb[]>;
211
+ protected getVerbsByType_internal(type: string): Promise<GraphVerb[]>;
212
+ }
213
+ export {};