@soulcraft/brainy 5.7.13 → 5.8.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,41 @@
1
+ /**
2
+ * Transaction System Errors
3
+ *
4
+ * Provides detailed error information for transaction failures.
5
+ */
6
+ /**
7
+ * Base class for all transaction errors
8
+ */
9
+ export declare class TransactionError extends Error {
10
+ readonly context?: Record<string, any>;
11
+ constructor(message: string, context?: Record<string, any>);
12
+ }
13
+ /**
14
+ * Error during transaction execution
15
+ */
16
+ export declare class TransactionExecutionError extends TransactionError {
17
+ readonly operationIndex: number;
18
+ readonly operationName: string | undefined;
19
+ readonly cause: Error;
20
+ constructor(message: string, operationIndex: number, operationName: string | undefined, cause: Error);
21
+ }
22
+ /**
23
+ * Error during transaction rollback
24
+ */
25
+ export declare class TransactionRollbackError extends TransactionError {
26
+ readonly originalError: Error;
27
+ readonly rollbackErrors: Error[];
28
+ constructor(message: string, originalError: Error, rollbackErrors: Error[]);
29
+ }
30
+ /**
31
+ * Error for invalid transaction state
32
+ */
33
+ export declare class InvalidTransactionStateError extends TransactionError {
34
+ constructor(currentState: string, attemptedAction: string);
35
+ }
36
+ /**
37
+ * Error for transaction timeout
38
+ */
39
+ export declare class TransactionTimeoutError extends TransactionError {
40
+ constructor(timeoutMs: number, operationIndex: number);
41
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Transaction System Errors
3
+ *
4
+ * Provides detailed error information for transaction failures.
5
+ */
6
+ /**
7
+ * Base class for all transaction errors
8
+ */
9
+ export class TransactionError extends Error {
10
+ constructor(message, context) {
11
+ super(message);
12
+ this.context = context;
13
+ this.name = 'TransactionError';
14
+ Error.captureStackTrace(this, this.constructor);
15
+ }
16
+ }
17
+ /**
18
+ * Error during transaction execution
19
+ */
20
+ export class TransactionExecutionError extends TransactionError {
21
+ constructor(message, operationIndex, operationName, cause) {
22
+ super(message, {
23
+ operationIndex,
24
+ operationName,
25
+ cause: cause.message
26
+ });
27
+ this.operationIndex = operationIndex;
28
+ this.operationName = operationName;
29
+ this.cause = cause;
30
+ this.name = 'TransactionExecutionError';
31
+ }
32
+ }
33
+ /**
34
+ * Error during transaction rollback
35
+ */
36
+ export class TransactionRollbackError extends TransactionError {
37
+ constructor(message, originalError, rollbackErrors) {
38
+ super(message, {
39
+ originalError: originalError.message,
40
+ rollbackErrorCount: rollbackErrors.length,
41
+ rollbackErrors: rollbackErrors.map(e => e.message)
42
+ });
43
+ this.originalError = originalError;
44
+ this.rollbackErrors = rollbackErrors;
45
+ this.name = 'TransactionRollbackError';
46
+ }
47
+ }
48
+ /**
49
+ * Error for invalid transaction state
50
+ */
51
+ export class InvalidTransactionStateError extends TransactionError {
52
+ constructor(currentState, attemptedAction) {
53
+ super(`Cannot ${attemptedAction}: transaction is in state '${currentState}'`, { currentState, attemptedAction });
54
+ this.name = 'InvalidTransactionStateError';
55
+ }
56
+ }
57
+ /**
58
+ * Error for transaction timeout
59
+ */
60
+ export class TransactionTimeoutError extends TransactionError {
61
+ constructor(timeoutMs, operationIndex) {
62
+ super(`Transaction timed out after ${timeoutMs}ms at operation ${operationIndex}`, { timeoutMs, operationIndex });
63
+ this.name = 'TransactionTimeoutError';
64
+ }
65
+ }
66
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Transaction System
3
+ *
4
+ * Provides atomicity for Brainy operations.
5
+ * All operations succeed or all rollback - no partial failures.
6
+ *
7
+ * @module transaction
8
+ */
9
+ export * from './types.js';
10
+ export * from './errors.js';
11
+ export { Transaction } from './Transaction.js';
12
+ export { TransactionManager, type TransactionStats } from './TransactionManager.js';
13
+ export type { Operation, RollbackAction, TransactionState, TransactionContext, TransactionFunction, TransactionResult, TransactionOptions } from './types.js';
14
+ export { TransactionError, TransactionExecutionError, TransactionRollbackError, InvalidTransactionStateError, TransactionTimeoutError } from './errors.js';
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Transaction System
3
+ *
4
+ * Provides atomicity for Brainy operations.
5
+ * All operations succeed or all rollback - no partial failures.
6
+ *
7
+ * @module transaction
8
+ */
9
+ export * from './types.js';
10
+ export * from './errors.js';
11
+ export { Transaction } from './Transaction.js';
12
+ export { TransactionManager } from './TransactionManager.js';
13
+ export { TransactionError, TransactionExecutionError, TransactionRollbackError, InvalidTransactionStateError, TransactionTimeoutError } from './errors.js';
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,172 @@
1
+ /**
2
+ * Index Operations with Rollback Support
3
+ *
4
+ * Provides transactional operations for all indexes:
5
+ * - TypeAwareHNSWIndex (per-type vector indexes)
6
+ * - HNSWIndex (legacy/fallback vector index)
7
+ * - MetadataIndexManager (roaring bitmap filtering)
8
+ * - GraphAdjacencyIndex (LSM-tree graph storage)
9
+ *
10
+ * Each operation can be executed and rolled back atomically.
11
+ */
12
+ import type { HNSWIndex } from '../../hnsw/hnswIndex.js';
13
+ import type { TypeAwareHNSWIndex } from '../../hnsw/typeAwareHNSWIndex.js';
14
+ import type { MetadataIndexManager } from '../../utils/metadataIndex.js';
15
+ import type { GraphAdjacencyIndex } from '../../graph/graphAdjacencyIndex.js';
16
+ import type { NounType } from '../../types/graphTypes.js';
17
+ import type { GraphVerb } from '../../coreTypes.js';
18
+ import type { Operation, RollbackAction } from '../types.js';
19
+ /**
20
+ * Add to HNSW index with rollback support
21
+ *
22
+ * Rollback strategy:
23
+ * - Remove item from index
24
+ *
25
+ * Note: Works with both HNSWIndex and TypeAwareHNSWIndex
26
+ */
27
+ export declare class AddToHNSWOperation implements Operation {
28
+ private readonly index;
29
+ private readonly id;
30
+ private readonly vector;
31
+ readonly name = "AddToHNSW";
32
+ constructor(index: HNSWIndex, id: string, vector: number[]);
33
+ execute(): Promise<RollbackAction>;
34
+ /**
35
+ * Check if item exists in index
36
+ */
37
+ private itemExists;
38
+ }
39
+ /**
40
+ * Add to TypeAwareHNSW index with rollback support
41
+ *
42
+ * Rollback strategy:
43
+ * - Remove item from type-specific index
44
+ */
45
+ export declare class AddToTypeAwareHNSWOperation implements Operation {
46
+ private readonly index;
47
+ private readonly id;
48
+ private readonly vector;
49
+ private readonly nounType;
50
+ readonly name = "AddToTypeAwareHNSW";
51
+ constructor(index: TypeAwareHNSWIndex, id: string, vector: number[], nounType: NounType);
52
+ execute(): Promise<RollbackAction>;
53
+ /**
54
+ * Check if item exists in type-specific index
55
+ */
56
+ private itemExists;
57
+ }
58
+ /**
59
+ * Remove from HNSW index with rollback support
60
+ *
61
+ * Rollback strategy:
62
+ * - Re-add item to index with original vector
63
+ *
64
+ * Note: Requires storing the vector for rollback
65
+ */
66
+ export declare class RemoveFromHNSWOperation implements Operation {
67
+ private readonly index;
68
+ private readonly id;
69
+ private readonly vector;
70
+ readonly name = "RemoveFromHNSW";
71
+ constructor(index: HNSWIndex, id: string, vector: number[]);
72
+ execute(): Promise<RollbackAction>;
73
+ }
74
+ /**
75
+ * Remove from TypeAwareHNSW index with rollback support
76
+ *
77
+ * Rollback strategy:
78
+ * - Re-add item to type-specific index with original vector
79
+ */
80
+ export declare class RemoveFromTypeAwareHNSWOperation implements Operation {
81
+ private readonly index;
82
+ private readonly id;
83
+ private readonly vector;
84
+ private readonly nounType;
85
+ readonly name = "RemoveFromTypeAwareHNSW";
86
+ constructor(index: TypeAwareHNSWIndex, id: string, vector: number[], // Required for rollback
87
+ nounType: NounType);
88
+ execute(): Promise<RollbackAction>;
89
+ }
90
+ /**
91
+ * Add to metadata index with rollback support
92
+ *
93
+ * Rollback strategy:
94
+ * - Remove item from index
95
+ */
96
+ export declare class AddToMetadataIndexOperation implements Operation {
97
+ private readonly index;
98
+ private readonly id;
99
+ private readonly entity;
100
+ readonly name = "AddToMetadataIndex";
101
+ constructor(index: MetadataIndexManager, id: string, entity: any);
102
+ execute(): Promise<RollbackAction>;
103
+ }
104
+ /**
105
+ * Remove from metadata index with rollback support
106
+ *
107
+ * Rollback strategy:
108
+ * - Re-add item to index with original metadata
109
+ */
110
+ export declare class RemoveFromMetadataIndexOperation implements Operation {
111
+ private readonly index;
112
+ private readonly id;
113
+ private readonly entity;
114
+ readonly name = "RemoveFromMetadataIndex";
115
+ constructor(index: MetadataIndexManager, id: string, entity: any);
116
+ execute(): Promise<RollbackAction>;
117
+ }
118
+ /**
119
+ * Add verb to graph index with rollback support
120
+ *
121
+ * Rollback strategy:
122
+ * - Remove verb from graph index
123
+ */
124
+ export declare class AddToGraphIndexOperation implements Operation {
125
+ private readonly index;
126
+ private readonly verb;
127
+ readonly name = "AddToGraphIndex";
128
+ constructor(index: GraphAdjacencyIndex, verb: GraphVerb);
129
+ execute(): Promise<RollbackAction>;
130
+ }
131
+ /**
132
+ * Remove verb from graph index with rollback support
133
+ *
134
+ * Rollback strategy:
135
+ * - Re-add verb to graph index
136
+ */
137
+ export declare class RemoveFromGraphIndexOperation implements Operation {
138
+ private readonly index;
139
+ private readonly verb;
140
+ readonly name = "RemoveFromGraphIndex";
141
+ constructor(index: GraphAdjacencyIndex, verb: GraphVerb);
142
+ execute(): Promise<RollbackAction>;
143
+ }
144
+ /**
145
+ * Batch operation: Add multiple items to HNSW index
146
+ *
147
+ * Useful for bulk imports with transaction support.
148
+ * Rolls back all items if any fail.
149
+ */
150
+ export declare class BatchAddToHNSWOperation implements Operation {
151
+ readonly name = "BatchAddToHNSW";
152
+ private operations;
153
+ constructor(index: HNSWIndex, items: Array<{
154
+ id: string;
155
+ vector: number[];
156
+ }>);
157
+ execute(): Promise<RollbackAction>;
158
+ }
159
+ /**
160
+ * Batch operation: Add multiple entities to metadata index
161
+ *
162
+ * Useful for bulk imports with transaction support.
163
+ */
164
+ export declare class BatchAddToMetadataIndexOperation implements Operation {
165
+ readonly name = "BatchAddToMetadataIndex";
166
+ private operations;
167
+ constructor(index: MetadataIndexManager, items: Array<{
168
+ id: string;
169
+ entity: any;
170
+ }>);
171
+ execute(): Promise<RollbackAction>;
172
+ }
@@ -0,0 +1,301 @@
1
+ /**
2
+ * Index Operations with Rollback Support
3
+ *
4
+ * Provides transactional operations for all indexes:
5
+ * - TypeAwareHNSWIndex (per-type vector indexes)
6
+ * - HNSWIndex (legacy/fallback vector index)
7
+ * - MetadataIndexManager (roaring bitmap filtering)
8
+ * - GraphAdjacencyIndex (LSM-tree graph storage)
9
+ *
10
+ * Each operation can be executed and rolled back atomically.
11
+ */
12
+ /**
13
+ * Add to HNSW index with rollback support
14
+ *
15
+ * Rollback strategy:
16
+ * - Remove item from index
17
+ *
18
+ * Note: Works with both HNSWIndex and TypeAwareHNSWIndex
19
+ */
20
+ export class AddToHNSWOperation {
21
+ constructor(index, id, vector) {
22
+ this.index = index;
23
+ this.id = id;
24
+ this.vector = vector;
25
+ this.name = 'AddToHNSW';
26
+ }
27
+ async execute() {
28
+ // Check if item already exists (for rollback decision)
29
+ const existed = await this.itemExists(this.id);
30
+ // Add to index
31
+ await this.index.addItem({ id: this.id, vector: this.vector });
32
+ // Return rollback action
33
+ return async () => {
34
+ if (!existed) {
35
+ // Remove newly added item
36
+ await this.index.removeItem(this.id);
37
+ }
38
+ // If item existed before, we don't rollback (update is OK)
39
+ // This prevents index corruption from removing pre-existing items
40
+ };
41
+ }
42
+ /**
43
+ * Check if item exists in index
44
+ */
45
+ async itemExists(id) {
46
+ try {
47
+ // Try to get item - if exists, no error
48
+ await this.index.getItem?.(id);
49
+ return true;
50
+ }
51
+ catch {
52
+ return false;
53
+ }
54
+ }
55
+ }
56
+ /**
57
+ * Add to TypeAwareHNSW index with rollback support
58
+ *
59
+ * Rollback strategy:
60
+ * - Remove item from type-specific index
61
+ */
62
+ export class AddToTypeAwareHNSWOperation {
63
+ constructor(index, id, vector, nounType) {
64
+ this.index = index;
65
+ this.id = id;
66
+ this.vector = vector;
67
+ this.nounType = nounType;
68
+ this.name = 'AddToTypeAwareHNSW';
69
+ }
70
+ async execute() {
71
+ // Check if item already exists
72
+ const existed = await this.itemExists(this.id, this.nounType);
73
+ // Add to type-specific index
74
+ await this.index.addItem({ id: this.id, vector: this.vector }, this.nounType);
75
+ // Return rollback action
76
+ return async () => {
77
+ if (!existed) {
78
+ // Remove newly added item from type-specific index
79
+ await this.index.removeItem(this.id, this.nounType);
80
+ }
81
+ };
82
+ }
83
+ /**
84
+ * Check if item exists in type-specific index
85
+ */
86
+ async itemExists(id, type) {
87
+ try {
88
+ const index = this.index.getIndexForType?.(type);
89
+ if (index) {
90
+ await index.getItem?.(id);
91
+ return true;
92
+ }
93
+ return false;
94
+ }
95
+ catch {
96
+ return false;
97
+ }
98
+ }
99
+ }
100
+ /**
101
+ * Remove from HNSW index with rollback support
102
+ *
103
+ * Rollback strategy:
104
+ * - Re-add item to index with original vector
105
+ *
106
+ * Note: Requires storing the vector for rollback
107
+ */
108
+ export class RemoveFromHNSWOperation {
109
+ constructor(index, id, vector // Required for rollback
110
+ ) {
111
+ this.index = index;
112
+ this.id = id;
113
+ this.vector = vector;
114
+ this.name = 'RemoveFromHNSW';
115
+ }
116
+ async execute() {
117
+ // Remove from index
118
+ await this.index.removeItem(this.id);
119
+ // Return rollback action
120
+ return async () => {
121
+ // Re-add item with original vector
122
+ await this.index.addItem({ id: this.id, vector: this.vector });
123
+ };
124
+ }
125
+ }
126
+ /**
127
+ * Remove from TypeAwareHNSW index with rollback support
128
+ *
129
+ * Rollback strategy:
130
+ * - Re-add item to type-specific index with original vector
131
+ */
132
+ export class RemoveFromTypeAwareHNSWOperation {
133
+ constructor(index, id, vector, // Required for rollback
134
+ nounType) {
135
+ this.index = index;
136
+ this.id = id;
137
+ this.vector = vector;
138
+ this.nounType = nounType;
139
+ this.name = 'RemoveFromTypeAwareHNSW';
140
+ }
141
+ async execute() {
142
+ // Remove from type-specific index
143
+ await this.index.removeItem(this.id, this.nounType);
144
+ // Return rollback action
145
+ return async () => {
146
+ // Re-add item with original vector to type-specific index
147
+ await this.index.addItem({ id: this.id, vector: this.vector }, this.nounType);
148
+ };
149
+ }
150
+ }
151
+ /**
152
+ * Add to metadata index with rollback support
153
+ *
154
+ * Rollback strategy:
155
+ * - Remove item from index
156
+ */
157
+ export class AddToMetadataIndexOperation {
158
+ constructor(index, id, entity // Entity or metadata structure
159
+ ) {
160
+ this.index = index;
161
+ this.id = id;
162
+ this.entity = entity;
163
+ this.name = 'AddToMetadataIndex';
164
+ }
165
+ async execute() {
166
+ // Add to metadata index (skipFlush=true for transaction atomicity)
167
+ await this.index.addToIndex(this.id, this.entity, true);
168
+ // Return rollback action
169
+ return async () => {
170
+ // Remove from metadata index
171
+ await this.index.removeFromIndex(this.id, this.entity);
172
+ };
173
+ }
174
+ }
175
+ /**
176
+ * Remove from metadata index with rollback support
177
+ *
178
+ * Rollback strategy:
179
+ * - Re-add item to index with original metadata
180
+ */
181
+ export class RemoveFromMetadataIndexOperation {
182
+ constructor(index, id, entity // Required for rollback
183
+ ) {
184
+ this.index = index;
185
+ this.id = id;
186
+ this.entity = entity;
187
+ this.name = 'RemoveFromMetadataIndex';
188
+ }
189
+ async execute() {
190
+ // Remove from metadata index
191
+ await this.index.removeFromIndex(this.id, this.entity);
192
+ // Return rollback action
193
+ return async () => {
194
+ // Re-add with original metadata (skipFlush=true)
195
+ await this.index.addToIndex(this.id, this.entity, true);
196
+ };
197
+ }
198
+ }
199
+ /**
200
+ * Add verb to graph index with rollback support
201
+ *
202
+ * Rollback strategy:
203
+ * - Remove verb from graph index
204
+ */
205
+ export class AddToGraphIndexOperation {
206
+ constructor(index, verb) {
207
+ this.index = index;
208
+ this.verb = verb;
209
+ this.name = 'AddToGraphIndex';
210
+ }
211
+ async execute() {
212
+ // Add verb to graph index
213
+ await this.index.addVerb(this.verb);
214
+ // Return rollback action
215
+ return async () => {
216
+ // Remove verb from graph index
217
+ await this.index.removeVerb(this.verb.id);
218
+ };
219
+ }
220
+ }
221
+ /**
222
+ * Remove verb from graph index with rollback support
223
+ *
224
+ * Rollback strategy:
225
+ * - Re-add verb to graph index
226
+ */
227
+ export class RemoveFromGraphIndexOperation {
228
+ constructor(index, verb // Required for rollback
229
+ ) {
230
+ this.index = index;
231
+ this.verb = verb;
232
+ this.name = 'RemoveFromGraphIndex';
233
+ }
234
+ async execute() {
235
+ // Remove verb from graph index
236
+ await this.index.removeVerb(this.verb.id);
237
+ // Return rollback action
238
+ return async () => {
239
+ // Re-add verb with original data
240
+ await this.index.addVerb(this.verb);
241
+ };
242
+ }
243
+ }
244
+ /**
245
+ * Batch operation: Add multiple items to HNSW index
246
+ *
247
+ * Useful for bulk imports with transaction support.
248
+ * Rolls back all items if any fail.
249
+ */
250
+ export class BatchAddToHNSWOperation {
251
+ constructor(index, items) {
252
+ this.name = 'BatchAddToHNSW';
253
+ this.operations = items.map(item => new AddToHNSWOperation(index, item.id, item.vector));
254
+ }
255
+ async execute() {
256
+ const rollbackActions = [];
257
+ // Execute all operations
258
+ for (const op of this.operations) {
259
+ const rollback = await op.execute();
260
+ if (rollback) {
261
+ rollbackActions.push(rollback);
262
+ }
263
+ }
264
+ // Return combined rollback action
265
+ return async () => {
266
+ // Execute all rollbacks in reverse order
267
+ for (let i = rollbackActions.length - 1; i >= 0; i--) {
268
+ await rollbackActions[i]();
269
+ }
270
+ };
271
+ }
272
+ }
273
+ /**
274
+ * Batch operation: Add multiple entities to metadata index
275
+ *
276
+ * Useful for bulk imports with transaction support.
277
+ */
278
+ export class BatchAddToMetadataIndexOperation {
279
+ constructor(index, items) {
280
+ this.name = 'BatchAddToMetadataIndex';
281
+ this.operations = items.map(item => new AddToMetadataIndexOperation(index, item.id, item.entity));
282
+ }
283
+ async execute() {
284
+ const rollbackActions = [];
285
+ // Execute all operations
286
+ for (const op of this.operations) {
287
+ const rollback = await op.execute();
288
+ if (rollback) {
289
+ rollbackActions.push(rollback);
290
+ }
291
+ }
292
+ // Return combined rollback action
293
+ return async () => {
294
+ // Execute all rollbacks in reverse order
295
+ for (let i = rollbackActions.length - 1; i >= 0; i--) {
296
+ await rollbackActions[i]();
297
+ }
298
+ };
299
+ }
300
+ }
301
+ //# sourceMappingURL=IndexOperations.js.map