brainy/src/utils/operationUtils.ts
David Snelling 9c87982a7d 🧠 Brainy 2.0.0 - Zero-Configuration AI Database with Triple Intelligence™
MAJOR RELEASE: Complete evolution of Brainy with groundbreaking features and performance.

🎯 KEY FEATURES:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 Triple Intelligence™ Engine
  - Unified Vector + Metadata + Graph search
  - O(log n) performance on all operations
  - 3ms average search latency at any scale

 API Consolidation
  - 15+ search methods → 2 clean APIs
  - search() for vector similarity
  - find() for natural language queries

 Natural Language Processing
  - 220+ pre-computed NLP patterns
  - Instant context understanding
  - "Show me recent React components with tests"

 Zero Configuration
  - Works instantly, no setup required
  - Built-in embedding models (no API keys)
  - Smart defaults for everything
  - Automatic optimization

 Enterprise Features (Free for Everyone)
  - Scales to 10M+ items
  - Write-Ahead Logging (WAL) for durability
  - Distributed architecture with sharding
  - Read/write separation
  - Connection pooling & request deduplication
  - Built-in monitoring & health checks

 Universal Compatibility
  - Node.js, Browser, Edge Workers
  - 4 Storage Adapters (Memory, FileSystem, OPFS, S3)
  - TypeScript with full type safety
  - Worker-based embeddings

📦 WHAT'S INCLUDED:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• Core AI Database with HNSW indexing
• 19 Production-ready augmentations
• Universal Memory Manager
• Complete CLI with all commands
• Brain Cloud integration (soulcraft.com)
• Comprehensive documentation
• 52 test files with 400+ tests
• Migration guide from 1.x

📊 PERFORMANCE:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• Initialize: 450ms (24MB memory)
• Search: 3ms average (up to 10M items)
• Metadata Filter: 0.8ms (O(log n))
• Bulk Import: 2.3s per 1000 items
• Production Scale: 5.8ms at 10M items

🔧 TECHNICAL IMPROVEMENTS:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• TypeScript compilation: 153 errors → 0
• Memory usage: 200MB → 24MB baseline
• Circular dependencies resolved
• Worker thread communication fixed
• Storage adapter consistency
• Request coalescing for 3x performance

🛠️ CLI FEATURES:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• brainy add - Smart data ingestion
• brainy find - Natural language search
• brainy search - Vector similarity
• brainy chat - AI conversation mode
• brainy cloud - Brain Cloud integration
• brainy augment - Manage extensions
• 100% API compatibility

📚 DOCUMENTATION:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• Professional README with examples
• Quick Start guide (5 minutes)
• Enterprise Features guide
• Migration guide from 1.x
• API reference
• Architecture documentation

🌟 USE CASES:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• AI memory layer for chatbots
• Semantic document search
• Code intelligence platforms
• Knowledge management systems
• Real-time recommendation engines
• Customer support automation

MIT License - Enterprise features included free for everyone.
No premium tiers, no paywalls, no limits.

Built with ❤️ by the Brainy community.
Visit https://soulcraft.com for Brain Cloud integration.
2025-08-26 12:32:21 -07:00

204 lines
5.7 KiB
TypeScript

/**
* Utility functions for timeout and retry logic
* Used by storage adapters to handle network operations reliably
*/
import { BrainyError } from '../errors/brainyError.js'
export interface TimeoutConfig {
get?: number
add?: number
delete?: number
}
export interface RetryConfig {
maxRetries?: number
initialDelay?: number
maxDelay?: number
backoffMultiplier?: number
}
export interface OperationConfig {
timeouts?: TimeoutConfig
retryPolicy?: RetryConfig
}
// Default configuration values
export const DEFAULT_TIMEOUTS: Required<TimeoutConfig> = {
get: 30000, // 30 seconds
add: 60000, // 1 minute
delete: 30000 // 30 seconds
}
export const DEFAULT_RETRY_POLICY: Required<RetryConfig> = {
maxRetries: 3,
initialDelay: 1000,
maxDelay: 10000,
backoffMultiplier: 2
}
/**
* Wraps a promise with a timeout
*/
export function withTimeout<T>(
promise: Promise<T>,
timeoutMs: number,
operation: string
): Promise<T> {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(BrainyError.timeout(operation, timeoutMs))
}, timeoutMs)
promise
.then((result) => {
clearTimeout(timeoutId)
resolve(result)
})
.catch((error) => {
clearTimeout(timeoutId)
reject(error)
})
})
}
/**
* Calculates the delay for exponential backoff
*/
function calculateBackoffDelay(
attemptNumber: number,
initialDelay: number,
maxDelay: number,
backoffMultiplier: number
): number {
const delay = initialDelay * Math.pow(backoffMultiplier, attemptNumber - 1)
return Math.min(delay, maxDelay)
}
/**
* Sleeps for the specified number of milliseconds
*/
function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
/**
* Executes an operation with retry logic and exponential backoff
*/
export async function withRetry<T>(
operation: () => Promise<T>,
operationName: string,
config: RetryConfig = {}
): Promise<T> {
const {
maxRetries = DEFAULT_RETRY_POLICY.maxRetries,
initialDelay = DEFAULT_RETRY_POLICY.initialDelay,
maxDelay = DEFAULT_RETRY_POLICY.maxDelay,
backoffMultiplier = DEFAULT_RETRY_POLICY.backoffMultiplier
} = config
let lastError: Error | undefined
for (let attempt = 1; attempt <= maxRetries + 1; attempt++) {
try {
return await operation()
} catch (error) {
lastError = error instanceof Error ? error : new Error(String(error))
// If this is the last attempt, don't retry
if (attempt > maxRetries) {
break
}
// Check if the error is retryable
if (!BrainyError.isRetryable(lastError)) {
throw BrainyError.fromError(lastError, operationName)
}
// Calculate delay for exponential backoff
const delay = calculateBackoffDelay(attempt, initialDelay, maxDelay, backoffMultiplier)
console.warn(
`Operation '${operationName}' failed on attempt ${attempt}/${maxRetries + 1}. ` +
`Retrying in ${delay}ms. Error: ${lastError.message}`
)
// Wait before retrying
await sleep(delay)
}
}
// All retries exhausted
throw BrainyError.retryExhausted(operationName, maxRetries, lastError)
}
/**
* Executes an operation with both timeout and retry logic
*/
export async function withTimeoutAndRetry<T>(
operation: () => Promise<T>,
operationName: string,
timeoutMs: number,
retryConfig: RetryConfig = {}
): Promise<T> {
return withRetry(
() => withTimeout(operation(), timeoutMs, operationName),
operationName,
retryConfig
)
}
/**
* Creates a configured operation executor for a specific operation type
*/
export function createOperationExecutor(
operationType: keyof TimeoutConfig,
config: OperationConfig = {}
) {
const timeouts = { ...DEFAULT_TIMEOUTS, ...config.timeouts }
const retryPolicy = { ...DEFAULT_RETRY_POLICY, ...config.retryPolicy }
const timeoutMs = timeouts[operationType]
return async function executeOperation<T>(
operation: () => Promise<T>,
operationName: string
): Promise<T> {
return withTimeoutAndRetry(operation, operationName, timeoutMs, retryPolicy)
}
}
/**
* Storage operation executors for different operation types
*/
export class StorageOperationExecutors {
private getExecutor: ReturnType<typeof createOperationExecutor>
private addExecutor: ReturnType<typeof createOperationExecutor>
private deleteExecutor: ReturnType<typeof createOperationExecutor>
constructor(config: OperationConfig = {}) {
this.getExecutor = createOperationExecutor('get', config)
this.addExecutor = createOperationExecutor('add', config)
this.deleteExecutor = createOperationExecutor('delete', config)
}
/**
* Execute a get operation with timeout and retry
*/
async executeGet<T>(operation: () => Promise<T>, operationName: string): Promise<T> {
return this.getExecutor(operation, operationName)
}
/**
* Execute an add operation with timeout and retry
*/
async executeAdd<T>(operation: () => Promise<T>, operationName: string): Promise<T> {
return this.addExecutor(operation, operationName)
}
/**
* Execute a delete operation with timeout and retry
*/
async executeDelete<T>(operation: () => Promise<T>, operationName: string): Promise<T> {
return this.deleteExecutor(operation, operationName)
}
}