feat(types): extend FileSystemHandle and optimize imports for consistency
- Added `getFile` method to `FileSystemHandle` interface for enhanced TypeScript compatibility with File System Access API. - Improved maintainability by reformatting and aligning imports in `brainyData.ts`. - Standardized spacing and indentation across structured comments and interface fields. Purpose: Improve TypeScript support for file system operations and enhance code readability with consistent formatting and import management.
This commit is contained in:
parent
87957d8fe7
commit
d95e86ace4
4 changed files with 2745 additions and 2723 deletions
|
|
@ -3,11 +3,9 @@ import {
|
|||
IMemoryAugmentation,
|
||||
AugmentationResponse
|
||||
} from '../types/augmentations.js'
|
||||
import { StorageAdapter, Vector } from '../coreTypes.js'
|
||||
import { MemoryStorage } from '../storage/opfsStorage.js'
|
||||
import { FileSystemStorage } from '../storage/fileSystemStorage.js'
|
||||
import { OPFSStorage } from '../storage/opfsStorage.js'
|
||||
import { cosineDistance } from '../utils/distance.js'
|
||||
import {StorageAdapter, Vector} from '../coreTypes.js'
|
||||
import {MemoryStorage, FileSystemStorage, OPFSStorage} from '../storage/storageFactory.js'
|
||||
import {cosineDistance} from '../utils/distance.js'
|
||||
|
||||
/**
|
||||
* Base class for memory augmentations that wrap a StorageAdapter
|
||||
|
|
@ -55,7 +53,7 @@ abstract class BaseMemoryAugmentation implements IMemoryAugmentation {
|
|||
|
||||
try {
|
||||
await this.storage.saveMetadata(key, data)
|
||||
return { success: true, data: true }
|
||||
return {success: true, data: true}
|
||||
} catch (error) {
|
||||
console.error(`Failed to store data for key ${key}:`, error)
|
||||
return {
|
||||
|
|
@ -97,7 +95,7 @@ abstract class BaseMemoryAugmentation implements IMemoryAugmentation {
|
|||
|
||||
try {
|
||||
await this.storage.saveMetadata(key, data)
|
||||
return { success: true, data: true }
|
||||
return {success: true, data: true}
|
||||
} catch (error) {
|
||||
console.error(`Failed to update data for key ${key}:`, error)
|
||||
return {
|
||||
|
|
@ -117,7 +115,7 @@ abstract class BaseMemoryAugmentation implements IMemoryAugmentation {
|
|||
try {
|
||||
// There's no direct deleteMetadata method, so we save null
|
||||
await this.storage.saveMetadata(key, null)
|
||||
return { success: true, data: true }
|
||||
return {success: true, data: true}
|
||||
} catch (error) {
|
||||
console.error(`Failed to delete data for key ${key}:`, error)
|
||||
return {
|
||||
|
|
@ -257,7 +255,7 @@ export class FileSystemStorageAugmentation extends BaseMemoryAugmentation {
|
|||
enabled = true
|
||||
|
||||
constructor(name: string, rootDirectory?: string) {
|
||||
super(name, new FileSystemStorage(rootDirectory))
|
||||
super(name, new FileSystemStorage(rootDirectory || '.'))
|
||||
}
|
||||
|
||||
getType(): AugmentationType {
|
||||
|
|
|
|||
|
|
@ -3,13 +3,13 @@
|
|||
* Main class that provides the vector database functionality
|
||||
*/
|
||||
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { HNSWIndex } from './hnsw/hnswIndex.js'
|
||||
import {v4 as uuidv4} from 'uuid'
|
||||
import {HNSWIndex} from './hnsw/hnswIndex.js'
|
||||
import {
|
||||
HNSWIndexOptimized,
|
||||
HNSWOptimizedConfig
|
||||
} from './hnsw/hnswIndexOptimized.js'
|
||||
import { createStorage } from './storage/opfsStorage.js'
|
||||
import {createStorage} from './storage/storageFactory.js'
|
||||
import {
|
||||
DistanceFunction,
|
||||
GraphVerb,
|
||||
|
|
@ -30,13 +30,13 @@ import {
|
|||
euclideanDistance,
|
||||
cleanupWorkerPools
|
||||
} from './utils/index.js'
|
||||
import { NounType, VerbType, GraphNoun } from './types/graphTypes.js'
|
||||
import {NounType, VerbType, GraphNoun} from './types/graphTypes.js'
|
||||
import {
|
||||
ServerSearchConduitAugmentation,
|
||||
createServerSearchAugmentations
|
||||
} from './augmentations/serverSearchAugmentations.js'
|
||||
import { WebSocketConnection } from './types/augmentations.js'
|
||||
import { BrainyDataInterface } from './types/brainyDataInterface.js'
|
||||
import {WebSocketConnection} from './types/augmentations.js'
|
||||
import {BrainyDataInterface} from './types/brainyDataInterface.js'
|
||||
|
||||
export interface BrainyDataConfig {
|
||||
/**
|
||||
|
|
@ -156,7 +156,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
private storageConfig: BrainyDataConfig['storage'] = {}
|
||||
private useOptimizedIndex: boolean = false
|
||||
private _dimensions: number
|
||||
private loggingConfig: BrainyDataConfig['logging'] = { verbose: true }
|
||||
private loggingConfig: BrainyDataConfig['logging'] = {verbose: true}
|
||||
|
||||
// Remote server properties
|
||||
private remoteServerConfig: BrainyDataConfig['remoteServer'] | null = null
|
||||
|
|
@ -301,7 +301,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
|
||||
// Try again with a different approach - use the non-threaded version
|
||||
// This is a fallback in case the threaded version fails
|
||||
const { createTensorFlowEmbeddingFunction } = await import(
|
||||
const {createTensorFlowEmbeddingFunction} = await import(
|
||||
'./utils/embedding.js'
|
||||
)
|
||||
const fallbackEmbeddingFunction = createTensorFlowEmbeddingFunction()
|
||||
|
|
@ -327,12 +327,28 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
// Initialize storage if not provided in constructor
|
||||
if (!this.storage) {
|
||||
// Combine storage config with requestPersistentStorage for backward compatibility
|
||||
const storageOptions = {
|
||||
let storageOptions = {
|
||||
...this.storageConfig,
|
||||
requestPersistentStorage: this.requestPersistentStorage
|
||||
}
|
||||
|
||||
this.storage = await createStorage(storageOptions)
|
||||
// Ensure s3Storage has all required fields if it's provided
|
||||
if (storageOptions.s3Storage) {
|
||||
// Only include s3Storage if all required fields are present
|
||||
if (storageOptions.s3Storage.bucketName &&
|
||||
storageOptions.s3Storage.accessKeyId &&
|
||||
storageOptions.s3Storage.secretAccessKey) {
|
||||
// All required fields are present, keep s3Storage as is
|
||||
} else {
|
||||
// Missing required fields, remove s3Storage to avoid type errors
|
||||
const { s3Storage, ...rest } = storageOptions
|
||||
storageOptions = rest
|
||||
console.warn('Ignoring s3Storage configuration due to missing required fields')
|
||||
}
|
||||
}
|
||||
|
||||
// Use type assertion to tell TypeScript that storageOptions conforms to StorageOptions
|
||||
this.storage = await createStorage(storageOptions as any)
|
||||
}
|
||||
|
||||
// Initialize storage
|
||||
|
|
@ -402,7 +418,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
|
||||
try {
|
||||
// Create server search augmentations
|
||||
const { conduit, connection } = await createServerSearchAugmentations(
|
||||
const {conduit, connection} = await createServerSearchAugmentations(
|
||||
serverUrl,
|
||||
{
|
||||
protocols,
|
||||
|
|
@ -481,7 +497,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
: uuidv4())
|
||||
|
||||
// Add to index
|
||||
await this.index.addItem({ id, vector })
|
||||
await this.index.addItem({id, vector})
|
||||
|
||||
// Get the noun from the index
|
||||
const noun = this.index.getNouns().get(id)
|
||||
|
|
@ -514,7 +530,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
// Ensure metadata has the correct id field
|
||||
let metadataToSave = metadata
|
||||
if (metadata && typeof metadata === 'object') {
|
||||
metadataToSave = { ...metadata, id }
|
||||
metadataToSave = {...metadata, id}
|
||||
}
|
||||
|
||||
await this.storage!.saveMetadata(id, metadataToSave)
|
||||
|
|
@ -555,7 +571,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
} = {}
|
||||
): Promise<string> {
|
||||
// Use the existing add method with forceEmbed to ensure text is embedded
|
||||
return this.add(text, metadata, { ...options, forceEmbed: true })
|
||||
return this.add(text, metadata, {...options, forceEmbed: true})
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -580,7 +596,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
}
|
||||
|
||||
// Add to local with addToRemote option
|
||||
return this.add(vectorOrData, metadata, { ...options, addToRemote: true })
|
||||
return this.add(vectorOrData, metadata, {...options, addToRemote: true})
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -772,7 +788,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
}
|
||||
|
||||
// Add to local with addToRemote option
|
||||
return this.addBatch(items, { ...options, addToRemote: true })
|
||||
return this.addBatch(items, {...options, addToRemote: true})
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1354,7 +1370,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
}
|
||||
|
||||
// Add to index
|
||||
await this.index.addItem({ id, vector: verbVector })
|
||||
await this.index.addItem({id, vector: verbVector})
|
||||
|
||||
// Get the noun from the index
|
||||
const indexNoun = this.index.getNouns().get(id)
|
||||
|
|
@ -1648,7 +1664,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
queryVectorOrData,
|
||||
k * 2, // Get more results initially to account for filtering
|
||||
null,
|
||||
{ forceEmbed: options.forceEmbed }
|
||||
{forceEmbed: options.forceEmbed}
|
||||
)
|
||||
|
||||
// If no verb types specified, return the noun results directly
|
||||
|
|
@ -1885,7 +1901,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
const remoteResults = await this.searchRemote(
|
||||
queryVectorOrData,
|
||||
k - localResults.length,
|
||||
{ ...options, storeResults: true }
|
||||
{...options, storeResults: true}
|
||||
)
|
||||
|
||||
// Combine results, removing duplicates
|
||||
|
|
@ -2027,7 +2043,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
type: 'any',
|
||||
used: 0,
|
||||
quota: null,
|
||||
details: { error: 'Storage not initialized' }
|
||||
details: {error: 'Storage not initialized'}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2311,7 +2327,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
}
|
||||
|
||||
// Add the noun with its vector and metadata
|
||||
await this.add(noun.vector, noun.metadata, { id: noun.id })
|
||||
await this.add(noun.vector, noun.metadata, {id: noun.id})
|
||||
nounsRestored++
|
||||
} catch (error) {
|
||||
console.error(`Failed to restore noun ${noun.id}:`, error)
|
||||
|
|
@ -2366,7 +2382,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
|||
// Re-add all nouns to the index
|
||||
for (const noun of data.nouns) {
|
||||
if (noun.vector && noun.vector.length > 0) {
|
||||
await this.index.addItem({ id: noun.id, vector: noun.vector })
|
||||
await this.index.addItem({id: noun.id, vector: noun.vector})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
10
src/index.ts
10
src/index.ts
|
|
@ -83,13 +83,11 @@ export {
|
|||
import {
|
||||
OPFSStorage,
|
||||
MemoryStorage,
|
||||
createStorage
|
||||
} from './storage/opfsStorage.js'
|
||||
import { FileSystemStorage } from './storage/fileSystemStorage.js'
|
||||
import {
|
||||
FileSystemStorage,
|
||||
R2Storage,
|
||||
S3CompatibleStorage
|
||||
} from './storage/s3CompatibleStorage.js'
|
||||
S3CompatibleStorage,
|
||||
createStorage
|
||||
} from './storage/storageFactory.js'
|
||||
|
||||
export {
|
||||
OPFSStorage,
|
||||
|
|
|
|||
|
|
@ -1,14 +1,24 @@
|
|||
/**
|
||||
* Type declarations for the File System Access API
|
||||
* Extends the FileSystemDirectoryHandle interface to include the [Symbol.asyncIterator] method
|
||||
* and FileSystemHandle to include getFile() method for TypeScript compatibility
|
||||
*/
|
||||
|
||||
// Extend the FileSystemDirectoryHandle interface
|
||||
interface FileSystemDirectoryHandle {
|
||||
[Symbol.asyncIterator](): AsyncIterableIterator<[string, FileSystemHandle]>;
|
||||
|
||||
keys(): AsyncIterableIterator<string>;
|
||||
|
||||
entries(): AsyncIterableIterator<[string, FileSystemHandle]>;
|
||||
}
|
||||
|
||||
// Extend the FileSystemHandle interface to include getFile method
|
||||
// This is needed because TypeScript doesn't recognize that a FileSystemHandle
|
||||
// can be a FileSystemFileHandle which has the getFile method
|
||||
interface FileSystemHandle {
|
||||
getFile?(): Promise<File>;
|
||||
}
|
||||
|
||||
// Export something to make this a module
|
||||
export const fileSystemTypesLoaded = true;
|
||||
export const fileSystemTypesLoaded = true
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue