diff --git a/src/index.ts b/src/index.ts index 59e6ce8e..97381769 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,226 +13,226 @@ import './setup.js' // Export main BrainyData class and related types -import { BrainyData, BrainyDataConfig } from './brainyData.js' +import {BrainyData, BrainyDataConfig} from './brainyData.js' -export { BrainyData } -export type { BrainyDataConfig } +export {BrainyData} +export type {BrainyDataConfig} // Export distance functions for convenience import { - euclideanDistance, - cosineDistance, - manhattanDistance, - dotProductDistance + euclideanDistance, + cosineDistance, + manhattanDistance, + dotProductDistance } from './utils/index.js' export { - euclideanDistance, - cosineDistance, - manhattanDistance, - dotProductDistance + euclideanDistance, + cosineDistance, + manhattanDistance, + dotProductDistance } // Export embedding functionality import { - UniversalSentenceEncoder, - createEmbeddingFunction, - createTensorFlowEmbeddingFunction, - createThreadedEmbeddingFunction, - defaultEmbeddingFunction + UniversalSentenceEncoder, + createEmbeddingFunction, + createTensorFlowEmbeddingFunction, + createThreadedEmbeddingFunction, + defaultEmbeddingFunction } from './utils/embedding.js' // Export worker utilities -import { executeInThread, cleanupWorkerPools } from './utils/workerUtils.js' +import {executeInThread, cleanupWorkerPools} from './utils/workerUtils.js' // Export environment utilities import { - isBrowser, - isNode, - isWebWorker, - areWebWorkersAvailable, - areWorkerThreadsAvailable, - areWorkerThreadsAvailableSync, - isThreadingAvailable, - isThreadingAvailableAsync + isBrowser, + isNode, + isWebWorker, + areWebWorkersAvailable, + areWorkerThreadsAvailable, + areWorkerThreadsAvailableSync, + isThreadingAvailable, + isThreadingAvailableAsync } from './utils/environment.js' export { - UniversalSentenceEncoder, - createEmbeddingFunction, - createTensorFlowEmbeddingFunction, - createThreadedEmbeddingFunction, - defaultEmbeddingFunction, + UniversalSentenceEncoder, + createEmbeddingFunction, + createTensorFlowEmbeddingFunction, + createThreadedEmbeddingFunction, + defaultEmbeddingFunction, - // Worker utilities - executeInThread, - cleanupWorkerPools, + // Worker utilities + executeInThread, + cleanupWorkerPools, - // Environment utilities - isBrowser, - isNode, - isWebWorker, - areWebWorkersAvailable, - areWorkerThreadsAvailable, - areWorkerThreadsAvailableSync, - isThreadingAvailable, - isThreadingAvailableAsync + // Environment utilities + isBrowser, + isNode, + isWebWorker, + areWebWorkersAvailable, + areWorkerThreadsAvailable, + areWorkerThreadsAvailableSync, + isThreadingAvailable, + isThreadingAvailableAsync } // Export storage adapters import { - OPFSStorage, - MemoryStorage, - FileSystemStorage, - R2Storage, - S3CompatibleStorage, - createStorage + OPFSStorage, + MemoryStorage, + FileSystemStorage, + R2Storage, + S3CompatibleStorage, + createStorage } from './storage/storageFactory.js' export { - OPFSStorage, - MemoryStorage, - FileSystemStorage, - R2Storage, - S3CompatibleStorage, - createStorage + OPFSStorage, + MemoryStorage, + FileSystemStorage, + R2Storage, + S3CompatibleStorage, + createStorage } // Export unified pipeline import { - Pipeline, - pipeline, - augmentationPipeline, - ExecutionMode, - PipelineOptions, - PipelineResult, - executeStreamlined, - executeByType, - executeSingle, - processStaticData, - processStreamingData, - createPipeline, - createStreamingPipeline, - StreamlinedExecutionMode, - StreamlinedPipelineOptions, - StreamlinedPipelineResult + Pipeline, + pipeline, + augmentationPipeline, + ExecutionMode, + PipelineOptions, + PipelineResult, + executeStreamlined, + executeByType, + executeSingle, + processStaticData, + processStreamingData, + createPipeline, + createStreamingPipeline, + StreamlinedExecutionMode, + StreamlinedPipelineOptions, + StreamlinedPipelineResult } from './pipeline.js' // Export sequential pipeline (for backward compatibility) import { - SequentialPipeline, - sequentialPipeline, - SequentialPipelineOptions + SequentialPipeline, + sequentialPipeline, + SequentialPipelineOptions } from './sequentialPipeline.js' // Export augmentation factory import { - createSenseAugmentation, - addWebSocketSupport, - executeAugmentation, - loadAugmentationModule, - AugmentationOptions + createSenseAugmentation, + addWebSocketSupport, + executeAugmentation, + loadAugmentationModule, + AugmentationOptions } from './augmentationFactory.js' export { - // Unified pipeline exports - Pipeline, - pipeline, - augmentationPipeline, - ExecutionMode, - SequentialPipeline, - sequentialPipeline, + // Unified pipeline exports + Pipeline, + pipeline, + augmentationPipeline, + ExecutionMode, + SequentialPipeline, + sequentialPipeline, - // Streamlined pipeline exports (now part of unified pipeline) - executeStreamlined, - executeByType, - executeSingle, - processStaticData, - processStreamingData, - createPipeline, - createStreamingPipeline, - StreamlinedExecutionMode, + // Streamlined pipeline exports (now part of unified pipeline) + executeStreamlined, + executeByType, + executeSingle, + processStaticData, + processStreamingData, + createPipeline, + createStreamingPipeline, + StreamlinedExecutionMode, - // Augmentation factory exports - createSenseAugmentation, - addWebSocketSupport, - executeAugmentation, - loadAugmentationModule + // Augmentation factory exports + createSenseAugmentation, + addWebSocketSupport, + executeAugmentation, + loadAugmentationModule } export type { - PipelineOptions, - PipelineResult, - SequentialPipelineOptions, - StreamlinedPipelineOptions, - StreamlinedPipelineResult, - AugmentationOptions + PipelineOptions, + PipelineResult, + SequentialPipelineOptions, + StreamlinedPipelineOptions, + StreamlinedPipelineResult, + AugmentationOptions } // Export augmentation registry for build-time loading import { - availableAugmentations, - registerAugmentation, - initializeAugmentationPipeline, - setAugmentationEnabled, - getAugmentationsByType + availableAugmentations, + registerAugmentation, + initializeAugmentationPipeline, + setAugmentationEnabled, + getAugmentationsByType } from './augmentationRegistry.js' export { - availableAugmentations, - registerAugmentation, - initializeAugmentationPipeline, - setAugmentationEnabled, - getAugmentationsByType + availableAugmentations, + registerAugmentation, + initializeAugmentationPipeline, + setAugmentationEnabled, + getAugmentationsByType } // Export augmentation registry loader for build tools import { - loadAugmentationsFromModules, - createAugmentationRegistryPlugin, - createAugmentationRegistryRollupPlugin + loadAugmentationsFromModules, + createAugmentationRegistryPlugin, + createAugmentationRegistryRollupPlugin } from './augmentationRegistryLoader.js' import type { - AugmentationRegistryLoaderOptions, - AugmentationLoadResult + AugmentationRegistryLoaderOptions, + AugmentationLoadResult } from './augmentationRegistryLoader.js' export { - loadAugmentationsFromModules, - createAugmentationRegistryPlugin, - createAugmentationRegistryRollupPlugin + loadAugmentationsFromModules, + createAugmentationRegistryPlugin, + createAugmentationRegistryRollupPlugin } -export type { AugmentationRegistryLoaderOptions, AugmentationLoadResult } +export type {AugmentationRegistryLoaderOptions, AugmentationLoadResult} // Export augmentation implementations import { - MemoryStorageAugmentation, - FileSystemStorageAugmentation, - OPFSStorageAugmentation, - createMemoryAugmentation + MemoryStorageAugmentation, + FileSystemStorageAugmentation, + OPFSStorageAugmentation, + createMemoryAugmentation } from './augmentations/memoryAugmentations.js' import { - WebSocketConduitAugmentation, - WebRTCConduitAugmentation, - createConduitAugmentation + WebSocketConduitAugmentation, + WebRTCConduitAugmentation, + createConduitAugmentation } from './augmentations/conduitAugmentations.js' import { - ServerSearchConduitAugmentation, - ServerSearchActivationAugmentation, - createServerSearchAugmentations + ServerSearchConduitAugmentation, + ServerSearchActivationAugmentation, + createServerSearchAugmentations } from './augmentations/serverSearchAugmentations.js' // Non-LLM exports export { - MemoryStorageAugmentation, - FileSystemStorageAugmentation, - OPFSStorageAugmentation, - createMemoryAugmentation, - WebSocketConduitAugmentation, - WebRTCConduitAugmentation, - createConduitAugmentation, - ServerSearchConduitAugmentation, - ServerSearchActivationAugmentation, - createServerSearchAugmentations + MemoryStorageAugmentation, + FileSystemStorageAugmentation, + OPFSStorageAugmentation, + createMemoryAugmentation, + WebSocketConduitAugmentation, + WebRTCConduitAugmentation, + createConduitAugmentation, + ServerSearchConduitAugmentation, + ServerSearchActivationAugmentation, + createServerSearchAugmentations } // LLM augmentations are optional and not imported by default @@ -241,142 +241,142 @@ export { // Export types import type { - Vector, - VectorDocument, - SearchResult, - DistanceFunction, - EmbeddingFunction, - EmbeddingModel, - HNSWNoun, - GraphVerb, - HNSWConfig, - StorageAdapter + Vector, + VectorDocument, + SearchResult, + DistanceFunction, + EmbeddingFunction, + EmbeddingModel, + HNSWNoun, + GraphVerb, + HNSWConfig, + StorageAdapter } from './coreTypes.js' // Export HNSW index and optimized version -import { HNSWIndex } from './hnsw/hnswIndex.js' +import {HNSWIndex} from './hnsw/hnswIndex.js' import { - HNSWIndexOptimized, - HNSWOptimizedConfig + HNSWIndexOptimized, + HNSWOptimizedConfig } from './hnsw/hnswIndexOptimized.js' -export { HNSWIndex, HNSWIndexOptimized } +export {HNSWIndex, HNSWIndexOptimized} export type { - Vector, - VectorDocument, - SearchResult, - DistanceFunction, - EmbeddingFunction, - EmbeddingModel, - HNSWNoun, - GraphVerb, - HNSWConfig, - HNSWOptimizedConfig, - StorageAdapter + Vector, + VectorDocument, + SearchResult, + DistanceFunction, + EmbeddingFunction, + EmbeddingModel, + HNSWNoun, + GraphVerb, + HNSWConfig, + HNSWOptimizedConfig, + StorageAdapter } // Export augmentation types import type { - IAugmentation, - AugmentationResponse, - IWebSocketSupport, - ISenseAugmentation, - IConduitAugmentation, - ICognitionAugmentation, - IMemoryAugmentation, - IPerceptionAugmentation, - IDialogAugmentation, - IActivationAugmentation + IAugmentation, + AugmentationResponse, + IWebSocketSupport, + ISenseAugmentation, + IConduitAugmentation, + ICognitionAugmentation, + IMemoryAugmentation, + IPerceptionAugmentation, + IDialogAugmentation, + IActivationAugmentation } from './types/augmentations.js' -import { AugmentationType, BrainyAugmentations } from './types/augmentations.js' +import {AugmentationType, BrainyAugmentations} from './types/augmentations.js' -export type { IAugmentation, AugmentationResponse, IWebSocketSupport } +export type {IAugmentation, AugmentationResponse, IWebSocketSupport} export { - AugmentationType, - BrainyAugmentations, - ISenseAugmentation, - IConduitAugmentation, - ICognitionAugmentation, - IMemoryAugmentation, - IPerceptionAugmentation, - IDialogAugmentation, - IActivationAugmentation + AugmentationType, + BrainyAugmentations, + ISenseAugmentation, + IConduitAugmentation, + ICognitionAugmentation, + IMemoryAugmentation, + IPerceptionAugmentation, + IDialogAugmentation, + IActivationAugmentation } // Export combined WebSocket augmentation interfaces export type { - IWebSocketCognitionAugmentation, - IWebSocketSenseAugmentation, - IWebSocketPerceptionAugmentation, - IWebSocketActivationAugmentation, - IWebSocketDialogAugmentation, - IWebSocketConduitAugmentation, - IWebSocketMemoryAugmentation + IWebSocketCognitionAugmentation, + IWebSocketSenseAugmentation, + IWebSocketPerceptionAugmentation, + IWebSocketActivationAugmentation, + IWebSocketDialogAugmentation, + IWebSocketConduitAugmentation, + IWebSocketMemoryAugmentation } from './types/augmentations.js' // Export graph types import type { - GraphNoun, - EmbeddedGraphVerb, - Person, - Place, - Thing, - Event, - Concept, - Content + GraphNoun, + EmbeddedGraphVerb, + Person, + Place, + Thing, + Event, + Concept, + Content } from './types/graphTypes.js' -import { NounType, VerbType } from './types/graphTypes.js' +import {NounType, VerbType} from './types/graphTypes.js' export type { - GraphNoun, - EmbeddedGraphVerb, - Person, - Place, - Thing, - Event, - Concept, - Content + GraphNoun, + EmbeddedGraphVerb, + Person, + Place, + Thing, + Event, + Concept, + Content } -export { NounType, VerbType } +export {NounType, VerbType} // Export MCP (Model Control Protocol) components import { - BrainyMCPAdapter, - MCPAugmentationToolset, - BrainyMCPService + BrainyMCPAdapter, + MCPAugmentationToolset, + BrainyMCPService } from './mcp/index.js' // Import from mcp/index.js import { - MCPRequest, - MCPResponse, - MCPDataAccessRequest, - MCPToolExecutionRequest, - MCPSystemInfoRequest, - MCPAuthenticationRequest, - MCPRequestType, - MCPServiceOptions, - MCPTool, - MCP_VERSION + MCPRequest, + MCPResponse, + MCPDataAccessRequest, + MCPToolExecutionRequest, + MCPSystemInfoRequest, + MCPAuthenticationRequest, + MCPRequestType, + MCPServiceOptions, + MCPTool, + MCP_VERSION } from './types/mcpTypes.js' export { - // MCP classes - BrainyMCPAdapter, - MCPAugmentationToolset, - BrainyMCPService, + // MCP classes + BrainyMCPAdapter, + MCPAugmentationToolset, + BrainyMCPService, - // MCP types - MCPRequestType, - MCP_VERSION + // MCP types + MCPRequestType, + MCP_VERSION } export type { - MCPRequest, - MCPResponse, - MCPDataAccessRequest, - MCPToolExecutionRequest, - MCPSystemInfoRequest, - MCPAuthenticationRequest, - MCPServiceOptions, - MCPTool + MCPRequest, + MCPResponse, + MCPDataAccessRequest, + MCPToolExecutionRequest, + MCPSystemInfoRequest, + MCPAuthenticationRequest, + MCPServiceOptions, + MCPTool } diff --git a/src/storage/storageFactory.ts b/src/storage/storageFactory.ts index 339d31ad..be64030e 100644 --- a/src/storage/storageFactory.ts +++ b/src/storage/storageFactory.ts @@ -3,167 +3,167 @@ * Creates the appropriate storage adapter based on the environment and configuration */ -import { StorageAdapter } from '../coreTypes.js' -import { MemoryStorage } from './adapters/memoryStorage.js' -import { OPFSStorage } from './adapters/opfsStorage.js' -import { FileSystemStorage } from './adapters/fileSystemStorage.js' -import { S3CompatibleStorage, R2Storage } from './adapters/s3CompatibleStorage.js' +import {StorageAdapter} from '../coreTypes.js' +import {MemoryStorage} from './adapters/memoryStorage.js' +import {OPFSStorage} from './adapters/opfsStorage.js' +import {FileSystemStorage} from './adapters/fileSystemStorage.js' +import {S3CompatibleStorage, R2Storage} from './adapters/s3CompatibleStorage.js' /** * Options for creating a storage adapter */ export interface StorageOptions { - /** - * The type of storage to use - * - 'auto': Automatically select the best storage adapter based on the environment - * - 'memory': Use in-memory storage - * - 'opfs': Use Origin Private File System storage (browser only) - * - 'filesystem': Use file system storage (Node.js only) - * - 's3': Use Amazon S3 storage - * - 'r2': Use Cloudflare R2 storage - * - 'gcs': Use Google Cloud Storage - */ - type?: 'auto' | 'memory' | 'opfs' | 'filesystem' | 's3' | 'r2' | 'gcs' - - /** - * Force the use of memory storage even if other storage types are available - */ - forceMemoryStorage?: boolean - - /** - * Force the use of file system storage even if other storage types are available - */ - forceFileSystemStorage?: boolean - - /** - * Request persistent storage permission from the user (browser only) - */ - requestPersistentStorage?: boolean - - /** - * Root directory for file system storage (Node.js only) - */ - rootDirectory?: string - - /** - * Configuration for Amazon S3 storage - */ - s3Storage?: { /** - * S3 bucket name + * The type of storage to use + * - 'auto': Automatically select the best storage adapter based on the environment + * - 'memory': Use in-memory storage + * - 'opfs': Use Origin Private File System storage (browser only) + * - 'filesystem': Use file system storage (Node.js only) + * - 's3': Use Amazon S3 storage + * - 'r2': Use Cloudflare R2 storage + * - 'gcs': Use Google Cloud Storage */ - bucketName: string + type?: 'auto' | 'memory' | 'opfs' | 'filesystem' | 's3' | 'r2' | 'gcs' /** - * AWS region (e.g., 'us-east-1') + * Force the use of memory storage even if other storage types are available */ - region?: string + forceMemoryStorage?: boolean /** - * AWS access key ID + * Force the use of file system storage even if other storage types are available */ - accessKeyId: string + forceFileSystemStorage?: boolean /** - * AWS secret access key + * Request persistent storage permission from the user (browser only) */ - secretAccessKey: string + requestPersistentStorage?: boolean /** - * AWS session token (optional) + * Root directory for file system storage (Node.js only) */ - sessionToken?: string - } - - /** - * Configuration for Cloudflare R2 storage - */ - r2Storage?: { - /** - * R2 bucket name - */ - bucketName: string + rootDirectory?: string /** - * Cloudflare account ID + * Configuration for Amazon S3 storage */ - accountId: string + s3Storage?: { + /** + * S3 bucket name + */ + bucketName: string + + /** + * AWS region (e.g., 'us-east-1') + */ + region?: string + + /** + * AWS access key ID + */ + accessKeyId: string + + /** + * AWS secret access key + */ + secretAccessKey: string + + /** + * AWS session token (optional) + */ + sessionToken?: string + } /** - * R2 access key ID + * Configuration for Cloudflare R2 storage */ - accessKeyId: string + r2Storage?: { + /** + * R2 bucket name + */ + bucketName: string + + /** + * Cloudflare account ID + */ + accountId: string + + /** + * R2 access key ID + */ + accessKeyId: string + + /** + * R2 secret access key + */ + secretAccessKey: string + } /** - * R2 secret access key + * Configuration for Google Cloud Storage */ - secretAccessKey: string - } + gcsStorage?: { + /** + * GCS bucket name + */ + bucketName: string - /** - * Configuration for Google Cloud Storage - */ - gcsStorage?: { - /** - * GCS bucket name - */ - bucketName: string + /** + * GCS region (e.g., 'us-central1') + */ + region?: string + + /** + * GCS access key ID + */ + accessKeyId: string + + /** + * GCS secret access key + */ + secretAccessKey: string + + /** + * GCS endpoint (e.g., 'https://storage.googleapis.com') + */ + endpoint?: string + } /** - * GCS region (e.g., 'us-central1') + * Configuration for custom S3-compatible storage */ - region?: string + customS3Storage?: { + /** + * S3-compatible bucket name + */ + bucketName: string - /** - * GCS access key ID - */ - accessKeyId: string + /** + * S3-compatible region + */ + region?: string - /** - * GCS secret access key - */ - secretAccessKey: string + /** + * S3-compatible endpoint URL + */ + endpoint: string - /** - * GCS endpoint (e.g., 'https://storage.googleapis.com') - */ - endpoint?: string - } + /** + * S3-compatible access key ID + */ + accessKeyId: string - /** - * Configuration for custom S3-compatible storage - */ - customS3Storage?: { - /** - * S3-compatible bucket name - */ - bucketName: string + /** + * S3-compatible secret access key + */ + secretAccessKey: string - /** - * S3-compatible region - */ - region?: string - - /** - * S3-compatible endpoint URL - */ - endpoint: string - - /** - * S3-compatible access key ID - */ - accessKeyId: string - - /** - * S3-compatible secret access key - */ - secretAccessKey: string - - /** - * S3-compatible service type (for logging and error messages) - */ - serviceType?: string - } + /** + * S3-compatible service type (for logging and error messages) + */ + serviceType?: string + } } /** @@ -172,194 +172,194 @@ export interface StorageOptions { * @returns Promise that resolves to a storage adapter */ export async function createStorage( - options: StorageOptions = {} + options: StorageOptions = {} ): Promise { - // If memory storage is forced, use it regardless of other options - if (options.forceMemoryStorage) { - console.log('Using memory storage (forced)') - return new MemoryStorage() - } - - // If file system storage is forced, use it regardless of other options - if (options.forceFileSystemStorage) { - console.log('Using file system storage (forced)') - return new FileSystemStorage(options.rootDirectory || './brainy-data') - } - - // If a specific storage type is specified, use it - if (options.type && options.type !== 'auto') { - switch (options.type) { - case 'memory': - console.log('Using memory storage') + // If memory storage is forced, use it regardless of other options + if (options.forceMemoryStorage) { + console.log('Using memory storage (forced)') return new MemoryStorage() + } - case 'opfs': { - // Check if OPFS is available - const opfsStorage = new OPFSStorage() - if (opfsStorage.isOPFSAvailable()) { - console.log('Using OPFS storage') - await opfsStorage.init() - - // Request persistent storage if specified - if (options.requestPersistentStorage) { - const isPersistent = await opfsStorage.requestPersistentStorage() - console.log(`Persistent storage ${isPersistent ? 'granted' : 'denied'}`) - } - - return opfsStorage - } else { - console.warn('OPFS storage is not available, falling back to memory storage') - return new MemoryStorage() - } - } - - case 'filesystem': - console.log('Using file system storage') + // If file system storage is forced, use it regardless of other options + if (options.forceFileSystemStorage) { + console.log('Using file system storage (forced)') return new FileSystemStorage(options.rootDirectory || './brainy-data') + } - case 's3': - if (options.s3Storage) { - console.log('Using Amazon S3 storage') - return new S3CompatibleStorage({ + // If a specific storage type is specified, use it + if (options.type && options.type !== 'auto') { + switch (options.type) { + case 'memory': + console.log('Using memory storage') + return new MemoryStorage() + + case 'opfs': { + // Check if OPFS is available + const opfsStorage = new OPFSStorage() + if (opfsStorage.isOPFSAvailable()) { + console.log('Using OPFS storage') + await opfsStorage.init() + + // Request persistent storage if specified + if (options.requestPersistentStorage) { + const isPersistent = await opfsStorage.requestPersistentStorage() + console.log(`Persistent storage ${isPersistent ? 'granted' : 'denied'}`) + } + + return opfsStorage + } else { + console.warn('OPFS storage is not available, falling back to memory storage') + return new MemoryStorage() + } + } + + case 'filesystem': + console.log('Using file system storage') + return new FileSystemStorage(options.rootDirectory || './brainy-data') + + case 's3': + if (options.s3Storage) { + console.log('Using Amazon S3 storage') + return new S3CompatibleStorage({ + bucketName: options.s3Storage.bucketName, + region: options.s3Storage.region, + accessKeyId: options.s3Storage.accessKeyId, + secretAccessKey: options.s3Storage.secretAccessKey, + sessionToken: options.s3Storage.sessionToken, + serviceType: 's3' + }) + } else { + console.warn('S3 storage configuration is missing, falling back to memory storage') + return new MemoryStorage() + } + + case 'r2': + if (options.r2Storage) { + console.log('Using Cloudflare R2 storage') + return new R2Storage({ + bucketName: options.r2Storage.bucketName, + accountId: options.r2Storage.accountId, + accessKeyId: options.r2Storage.accessKeyId, + secretAccessKey: options.r2Storage.secretAccessKey, + serviceType: 'r2' + }) + } else { + console.warn('R2 storage configuration is missing, falling back to memory storage') + return new MemoryStorage() + } + + case 'gcs': + if (options.gcsStorage) { + console.log('Using Google Cloud Storage') + return new S3CompatibleStorage({ + bucketName: options.gcsStorage.bucketName, + region: options.gcsStorage.region, + endpoint: options.gcsStorage.endpoint || 'https://storage.googleapis.com', + accessKeyId: options.gcsStorage.accessKeyId, + secretAccessKey: options.gcsStorage.secretAccessKey, + serviceType: 'gcs' + }) + } else { + console.warn('GCS storage configuration is missing, falling back to memory storage') + return new MemoryStorage() + } + + default: + console.warn(`Unknown storage type: ${options.type}, falling back to memory storage`) + return new MemoryStorage() + } + } + + // If custom S3-compatible storage is specified, use it + if (options.customS3Storage) { + console.log(`Using custom S3-compatible storage: ${options.customS3Storage.serviceType || 'custom'}`) + return new S3CompatibleStorage({ + bucketName: options.customS3Storage.bucketName, + region: options.customS3Storage.region, + endpoint: options.customS3Storage.endpoint, + accessKeyId: options.customS3Storage.accessKeyId, + secretAccessKey: options.customS3Storage.secretAccessKey, + serviceType: options.customS3Storage.serviceType || 'custom' + }) + } + + // If R2 storage is specified, use it + if (options.r2Storage) { + console.log('Using Cloudflare R2 storage') + return new R2Storage({ + bucketName: options.r2Storage.bucketName, + accountId: options.r2Storage.accountId, + accessKeyId: options.r2Storage.accessKeyId, + secretAccessKey: options.r2Storage.secretAccessKey, + serviceType: 'r2' + }) + } + + // If S3 storage is specified, use it + if (options.s3Storage) { + console.log('Using Amazon S3 storage') + return new S3CompatibleStorage({ bucketName: options.s3Storage.bucketName, region: options.s3Storage.region, accessKeyId: options.s3Storage.accessKeyId, secretAccessKey: options.s3Storage.secretAccessKey, sessionToken: options.s3Storage.sessionToken, serviceType: 's3' - }) - } else { - console.warn('S3 storage configuration is missing, falling back to memory storage') - return new MemoryStorage() - } + }) + } - case 'r2': - if (options.r2Storage) { - console.log('Using Cloudflare R2 storage') - return new R2Storage({ - bucketName: options.r2Storage.bucketName, - accountId: options.r2Storage.accountId, - accessKeyId: options.r2Storage.accessKeyId, - secretAccessKey: options.r2Storage.secretAccessKey, - serviceType: 'r2' - }) - } else { - console.warn('R2 storage configuration is missing, falling back to memory storage') - return new MemoryStorage() - } - - case 'gcs': - if (options.gcsStorage) { - console.log('Using Google Cloud Storage') - return new S3CompatibleStorage({ + // If GCS storage is specified, use it + if (options.gcsStorage) { + console.log('Using Google Cloud Storage') + return new S3CompatibleStorage({ bucketName: options.gcsStorage.bucketName, region: options.gcsStorage.region, endpoint: options.gcsStorage.endpoint || 'https://storage.googleapis.com', accessKeyId: options.gcsStorage.accessKeyId, secretAccessKey: options.gcsStorage.secretAccessKey, serviceType: 'gcs' - }) - } else { - console.warn('GCS storage configuration is missing, falling back to memory storage') - return new MemoryStorage() + }) + } + + // Auto-detect the best storage adapter based on the environment + // First, try OPFS (browser only) + const opfsStorage = new OPFSStorage() + if (opfsStorage.isOPFSAvailable()) { + console.log('Using OPFS storage (auto-detected)') + await opfsStorage.init() + + // Request persistent storage if specified + if (options.requestPersistentStorage) { + const isPersistent = await opfsStorage.requestPersistentStorage() + console.log(`Persistent storage ${isPersistent ? 'granted' : 'denied'}`) } - default: - console.warn(`Unknown storage type: ${options.type}, falling back to memory storage`) - return new MemoryStorage() + return opfsStorage } - } - // If custom S3-compatible storage is specified, use it - if (options.customS3Storage) { - console.log(`Using custom S3-compatible storage: ${options.customS3Storage.serviceType || 'custom'}`) - return new S3CompatibleStorage({ - bucketName: options.customS3Storage.bucketName, - region: options.customS3Storage.region, - endpoint: options.customS3Storage.endpoint, - accessKeyId: options.customS3Storage.accessKeyId, - secretAccessKey: options.customS3Storage.secretAccessKey, - serviceType: options.customS3Storage.serviceType || 'custom' - }) - } - - // If R2 storage is specified, use it - if (options.r2Storage) { - console.log('Using Cloudflare R2 storage') - return new R2Storage({ - bucketName: options.r2Storage.bucketName, - accountId: options.r2Storage.accountId, - accessKeyId: options.r2Storage.accessKeyId, - secretAccessKey: options.r2Storage.secretAccessKey, - serviceType: 'r2' - }) - } - - // If S3 storage is specified, use it - if (options.s3Storage) { - console.log('Using Amazon S3 storage') - return new S3CompatibleStorage({ - bucketName: options.s3Storage.bucketName, - region: options.s3Storage.region, - accessKeyId: options.s3Storage.accessKeyId, - secretAccessKey: options.s3Storage.secretAccessKey, - sessionToken: options.s3Storage.sessionToken, - serviceType: 's3' - }) - } - - // If GCS storage is specified, use it - if (options.gcsStorage) { - console.log('Using Google Cloud Storage') - return new S3CompatibleStorage({ - bucketName: options.gcsStorage.bucketName, - region: options.gcsStorage.region, - endpoint: options.gcsStorage.endpoint || 'https://storage.googleapis.com', - accessKeyId: options.gcsStorage.accessKeyId, - secretAccessKey: options.gcsStorage.secretAccessKey, - serviceType: 'gcs' - }) - } - - // Auto-detect the best storage adapter based on the environment - // First, try OPFS (browser only) - const opfsStorage = new OPFSStorage() - if (opfsStorage.isOPFSAvailable()) { - console.log('Using OPFS storage (auto-detected)') - await opfsStorage.init() - - // Request persistent storage if specified - if (options.requestPersistentStorage) { - const isPersistent = await opfsStorage.requestPersistentStorage() - console.log(`Persistent storage ${isPersistent ? 'granted' : 'denied'}`) + // Next, try file system storage (Node.js only) + try { + // Check if we're in a Node.js environment + if (typeof process !== 'undefined' && process.versions && process.versions.node) { + console.log('Using file system storage (auto-detected)') + return new FileSystemStorage(options.rootDirectory || './brainy-data') + } + } catch (error) { + // Not in a Node.js environment or file system is not available } - - return opfsStorage - } - // Next, try file system storage (Node.js only) - try { - // Check if we're in a Node.js environment - if (typeof process !== 'undefined' && process.versions && process.versions.node) { - console.log('Using file system storage (auto-detected)') - return new FileSystemStorage(options.rootDirectory || './brainy-data') - } - } catch (error) { - // Not in a Node.js environment or file system is not available - } - - // Finally, fall back to memory storage - console.log('Using memory storage (auto-detected)') - return new MemoryStorage() + // Finally, fall back to memory storage + console.log('Using memory storage (auto-detected)') + return new MemoryStorage() } /** * Export all storage adapters */ export { - MemoryStorage, - OPFSStorage, - FileSystemStorage, - S3CompatibleStorage, - R2Storage + MemoryStorage, + OPFSStorage, + FileSystemStorage, + S3CompatibleStorage, + R2Storage } \ No newline at end of file