@soulcraft/brainy 5.7.13 → 5.9.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.
Files changed (31) hide show
  1. package/CHANGELOG.md +18 -7
  2. package/README.md +6 -2
  3. package/dist/augmentations/intelligentImport/handlers/imageHandler.d.ts +12 -16
  4. package/dist/augmentations/intelligentImport/handlers/imageHandler.js +40 -22
  5. package/dist/brainy.d.ts +1 -0
  6. package/dist/brainy.js +137 -126
  7. package/dist/graph/graphAdjacencyIndex.d.ts +76 -7
  8. package/dist/graph/graphAdjacencyIndex.js +94 -9
  9. package/dist/hnsw/typeAwareHNSWIndex.d.ts +3 -3
  10. package/dist/hnsw/typeAwareHNSWIndex.js +3 -3
  11. package/dist/query/typeAwareQueryPlanner.d.ts +4 -4
  12. package/dist/query/typeAwareQueryPlanner.js +4 -4
  13. package/dist/transaction/Transaction.d.ts +55 -0
  14. package/dist/transaction/Transaction.js +175 -0
  15. package/dist/transaction/TransactionManager.d.ts +67 -0
  16. package/dist/transaction/TransactionManager.js +145 -0
  17. package/dist/transaction/errors.d.ts +41 -0
  18. package/dist/transaction/errors.js +66 -0
  19. package/dist/transaction/index.d.ts +14 -0
  20. package/dist/transaction/index.js +14 -0
  21. package/dist/transaction/operations/IndexOperations.d.ts +172 -0
  22. package/dist/transaction/operations/IndexOperations.js +301 -0
  23. package/dist/transaction/operations/StorageOperations.d.ts +128 -0
  24. package/dist/transaction/operations/StorageOperations.js +253 -0
  25. package/dist/transaction/operations/index.d.ts +10 -0
  26. package/dist/transaction/operations/index.js +13 -0
  27. package/dist/transaction/types.d.ts +84 -0
  28. package/dist/transaction/types.js +8 -0
  29. package/dist/vfs/VirtualFileSystem.d.ts +16 -0
  30. package/dist/vfs/VirtualFileSystem.js +138 -48
  31. package/package.json +3 -3
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Transaction Manager
3
+ *
4
+ * Manages transaction execution across the system.
5
+ * Provides high-level API for executing atomic operations.
6
+ *
7
+ * Usage:
8
+ * ```typescript
9
+ * const txManager = new TransactionManager()
10
+ *
11
+ * const result = await txManager.executeTransaction(async (tx) => {
12
+ * tx.addOperation(operation1)
13
+ * tx.addOperation(operation2)
14
+ * return someValue
15
+ * })
16
+ * ```
17
+ */
18
+ import { Transaction } from './Transaction.js';
19
+ import { TransactionError } from './errors.js';
20
+ import { prodLog } from '../utils/logger.js';
21
+ /**
22
+ * Transaction Manager
23
+ */
24
+ export class TransactionManager {
25
+ constructor() {
26
+ this.stats = {
27
+ totalTransactions: 0,
28
+ successfulTransactions: 0,
29
+ failedTransactions: 0,
30
+ rolledBackTransactions: 0,
31
+ averageExecutionTimeMs: 0,
32
+ averageOperationsPerTransaction: 0
33
+ };
34
+ this.totalExecutionTime = 0;
35
+ this.totalOperations = 0;
36
+ }
37
+ /**
38
+ * Execute a function within a transaction
39
+ * All operations succeed atomically or all rollback
40
+ *
41
+ * @param fn Function that builds and executes transaction
42
+ * @param options Transaction execution options
43
+ * @returns Result from user function
44
+ * @throws TransactionError if transaction fails
45
+ */
46
+ async executeTransaction(fn, options) {
47
+ const transaction = new Transaction(options);
48
+ this.stats.totalTransactions++;
49
+ try {
50
+ // Execute user function (builds operations list)
51
+ const result = await fn(transaction);
52
+ // Execute all operations atomically
53
+ await transaction.execute();
54
+ // Update statistics
55
+ this.stats.successfulTransactions++;
56
+ this.updateStats(transaction);
57
+ return result;
58
+ }
59
+ catch (error) {
60
+ // Transaction failed and rolled back
61
+ this.stats.failedTransactions++;
62
+ if (transaction.getState() === 'rolled_back') {
63
+ this.stats.rolledBackTransactions++;
64
+ }
65
+ this.updateStats(transaction);
66
+ // Re-throw with context
67
+ if (error instanceof TransactionError) {
68
+ throw error;
69
+ }
70
+ else {
71
+ throw new TransactionError(`Transaction failed: ${error.message}`, { cause: error });
72
+ }
73
+ }
74
+ }
75
+ /**
76
+ * Execute a transaction and return detailed result
77
+ */
78
+ async executeTransactionWithResult(fn, options) {
79
+ const startTime = Date.now();
80
+ const transaction = new Transaction(options);
81
+ try {
82
+ const value = await fn(transaction);
83
+ await transaction.execute();
84
+ const executionTimeMs = Date.now() - startTime;
85
+ return {
86
+ value,
87
+ operationCount: transaction.getOperationCount(),
88
+ executionTimeMs
89
+ };
90
+ }
91
+ catch (error) {
92
+ // Transaction failed
93
+ throw error;
94
+ }
95
+ }
96
+ /**
97
+ * Get transaction statistics
98
+ */
99
+ getStats() {
100
+ return { ...this.stats };
101
+ }
102
+ /**
103
+ * Reset transaction statistics
104
+ */
105
+ resetStats() {
106
+ this.stats = {
107
+ totalTransactions: 0,
108
+ successfulTransactions: 0,
109
+ failedTransactions: 0,
110
+ rolledBackTransactions: 0,
111
+ averageExecutionTimeMs: 0,
112
+ averageOperationsPerTransaction: 0
113
+ };
114
+ this.totalExecutionTime = 0;
115
+ this.totalOperations = 0;
116
+ }
117
+ /**
118
+ * Update running statistics
119
+ */
120
+ updateStats(transaction) {
121
+ const executionTime = transaction.getExecutionTimeMs();
122
+ const operationCount = transaction.getOperationCount();
123
+ if (executionTime !== undefined) {
124
+ this.totalExecutionTime += executionTime;
125
+ this.stats.averageExecutionTimeMs =
126
+ this.totalExecutionTime / this.stats.totalTransactions;
127
+ }
128
+ this.totalOperations += operationCount;
129
+ this.stats.averageOperationsPerTransaction =
130
+ this.totalOperations / this.stats.totalTransactions;
131
+ }
132
+ /**
133
+ * Log current statistics
134
+ */
135
+ logStats() {
136
+ prodLog.info('[TransactionManager] Statistics:');
137
+ prodLog.info(` Total: ${this.stats.totalTransactions}`);
138
+ prodLog.info(` Successful: ${this.stats.successfulTransactions}`);
139
+ prodLog.info(` Failed: ${this.stats.failedTransactions}`);
140
+ prodLog.info(` Rolled back: ${this.stats.rolledBackTransactions}`);
141
+ prodLog.info(` Avg execution time: ${this.stats.averageExecutionTimeMs.toFixed(2)}ms`);
142
+ prodLog.info(` Avg operations/tx: ${this.stats.averageOperationsPerTransaction.toFixed(2)}`);
143
+ }
144
+ }
145
+ //# sourceMappingURL=TransactionManager.js.map
@@ -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
+ }