**refactor(core): remove redundant whitespace and format code**
- **Code Cleanup**: Removed unnecessary trailing whitespaces across `src/brainyData.ts`. Adjusted formatting for inline object spreads to maintain a consistent coding style. - **Purpose**: Enhance code readability and ensure adherence to formatting standards without altering functionality.
This commit is contained in:
parent
186b4b4555
commit
98d9cbdcee
1 changed files with 54 additions and 54 deletions
|
|
@ -263,7 +263,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current augmentation name if available
|
* Get the current augmentation name if available
|
||||||
* This is used to auto-detect the service performing data operations
|
* This is used to auto-detect the service performing data operations
|
||||||
|
|
@ -273,11 +273,11 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
try {
|
try {
|
||||||
// Get all registered augmentations
|
// Get all registered augmentations
|
||||||
const augmentationTypes = augmentationPipeline.getAvailableAugmentationTypes()
|
const augmentationTypes = augmentationPipeline.getAvailableAugmentationTypes()
|
||||||
|
|
||||||
// Check each type of augmentation
|
// Check each type of augmentation
|
||||||
for (const type of augmentationTypes) {
|
for (const type of augmentationTypes) {
|
||||||
const augmentations = augmentationPipeline.getAugmentationsByType(type)
|
const augmentations = augmentationPipeline.getAugmentationsByType(type)
|
||||||
|
|
||||||
// Find the first enabled augmentation
|
// Find the first enabled augmentation
|
||||||
for (const augmentation of augmentations) {
|
for (const augmentation of augmentations) {
|
||||||
if (augmentation.enabled) {
|
if (augmentation.enabled) {
|
||||||
|
|
@ -285,7 +285,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'default'
|
return 'default'
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// If there's any error in detection, return default
|
// If there's any error in detection, return default
|
||||||
|
|
@ -561,11 +561,11 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
// Set a default noun type
|
// Set a default noun type
|
||||||
;(metadata as unknown as GraphNoun).noun = NounType.Concept
|
;(metadata as unknown as GraphNoun).noun = NounType.Concept
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure createdBy field is populated for GraphNoun
|
// Ensure createdBy field is populated for GraphNoun
|
||||||
const service = options.service || this.getCurrentAugmentation()
|
const service = options.service || this.getCurrentAugmentation()
|
||||||
const graphNoun = metadata as unknown as GraphNoun
|
const graphNoun = metadata as unknown as GraphNoun
|
||||||
|
|
||||||
// Only set createdBy if it doesn't exist or is being explicitly updated
|
// Only set createdBy if it doesn't exist or is being explicitly updated
|
||||||
if (!graphNoun.createdBy || options.service) {
|
if (!graphNoun.createdBy || options.service) {
|
||||||
graphNoun.createdBy = {
|
graphNoun.createdBy = {
|
||||||
|
|
@ -573,19 +573,19 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
version: '1.0' // TODO: Get actual version from augmentation
|
version: '1.0' // TODO: Get actual version from augmentation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update timestamps
|
// Update timestamps
|
||||||
const now = new Date()
|
const now = new Date()
|
||||||
const timestamp = {
|
const timestamp = {
|
||||||
seconds: Math.floor(now.getTime() / 1000),
|
seconds: Math.floor(now.getTime() / 1000),
|
||||||
nanoseconds: (now.getTime() % 1000) * 1000000
|
nanoseconds: (now.getTime() % 1000) * 1000000
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set createdAt if it doesn't exist
|
// Set createdAt if it doesn't exist
|
||||||
if (!graphNoun.createdAt) {
|
if (!graphNoun.createdAt) {
|
||||||
graphNoun.createdAt = timestamp
|
graphNoun.createdAt = timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always update updatedAt
|
// Always update updatedAt
|
||||||
graphNoun.updatedAt = timestamp
|
graphNoun.updatedAt = timestamp
|
||||||
}
|
}
|
||||||
|
|
@ -597,7 +597,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.storage!.saveMetadata(id, metadataToSave)
|
await this.storage!.saveMetadata(id, metadataToSave)
|
||||||
|
|
||||||
// Track metadata statistics
|
// Track metadata statistics
|
||||||
const metadataService = options.service || this.getCurrentAugmentation()
|
const metadataService = options.service || this.getCurrentAugmentation()
|
||||||
await this.storage!.incrementStatistic('metadata', metadataService)
|
await this.storage!.incrementStatistic('metadata', metadataService)
|
||||||
|
|
@ -873,18 +873,18 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
service?: string
|
service?: string
|
||||||
): R[] {
|
): R[] {
|
||||||
if (!service) return results
|
if (!service) return results
|
||||||
|
|
||||||
return results.filter(result => {
|
return results.filter(result => {
|
||||||
if (!result.metadata || typeof result.metadata !== 'object') return false
|
if (!result.metadata || typeof result.metadata !== 'object') return false
|
||||||
if (!('createdBy' in result.metadata)) return false
|
if (!('createdBy' in result.metadata)) return false
|
||||||
|
|
||||||
const createdBy = result.metadata.createdBy as any
|
const createdBy = result.metadata.createdBy as any
|
||||||
if (!createdBy) return false
|
if (!createdBy) return false
|
||||||
|
|
||||||
return createdBy.augmentation === service
|
return createdBy.augmentation === service
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search for similar vectors within specific noun types
|
* Search for similar vectors within specific noun types
|
||||||
* @param queryVectorOrData Query vector or data to search for
|
* @param queryVectorOrData Query vector or data to search for
|
||||||
|
|
@ -905,14 +905,14 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
// Helper function to filter results by service
|
// Helper function to filter results by service
|
||||||
const filterByService = (metadata: any): boolean => {
|
const filterByService = (metadata: any): boolean => {
|
||||||
if (!options.service) return true // No filter, include all
|
if (!options.service) return true // No filter, include all
|
||||||
|
|
||||||
// Check if metadata has createdBy field with matching service
|
// Check if metadata has createdBy field with matching service
|
||||||
if (!metadata || typeof metadata !== 'object') return false
|
if (!metadata || typeof metadata !== 'object') return false
|
||||||
if (!('createdBy' in metadata)) return false
|
if (!('createdBy' in metadata)) return false
|
||||||
|
|
||||||
const createdBy = metadata.createdBy as any
|
const createdBy = metadata.createdBy as any
|
||||||
if (!createdBy) return false
|
if (!createdBy) return false
|
||||||
|
|
||||||
return createdBy.augmentation === options.service
|
return createdBy.augmentation === options.service
|
||||||
}
|
}
|
||||||
if (!this.isInitialized) {
|
if (!this.isInitialized) {
|
||||||
|
|
@ -967,7 +967,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
|
|
||||||
// Ensure metadata has the id field
|
// Ensure metadata has the id field
|
||||||
if (metadata && typeof metadata === 'object') {
|
if (metadata && typeof metadata === 'object') {
|
||||||
metadata = { ...metadata, id } as T
|
metadata = {...metadata, id} as T
|
||||||
}
|
}
|
||||||
|
|
||||||
searchResults.push({
|
searchResults.push({
|
||||||
|
|
@ -1027,7 +1027,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
|
|
||||||
// Ensure metadata has the id field
|
// Ensure metadata has the id field
|
||||||
if (metadata && typeof metadata === 'object') {
|
if (metadata && typeof metadata === 'object') {
|
||||||
metadata = { ...metadata, id } as T
|
metadata = {...metadata, id} as T
|
||||||
}
|
}
|
||||||
|
|
||||||
searchResults.push({
|
searchResults.push({
|
||||||
|
|
@ -1337,7 +1337,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
* @returns Promise that resolves to true if the metadata was updated, false otherwise
|
* @returns Promise that resolves to true if the metadata was updated, false otherwise
|
||||||
*/
|
*/
|
||||||
public async updateMetadata(
|
public async updateMetadata(
|
||||||
id: string,
|
id: string,
|
||||||
metadata: T,
|
metadata: T,
|
||||||
options: {
|
options: {
|
||||||
service?: string // The service that is updating the data
|
service?: string // The service that is updating the data
|
||||||
|
|
@ -1369,20 +1369,20 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
// Set a default noun type
|
// Set a default noun type
|
||||||
;(metadata as unknown as GraphNoun).noun = NounType.Concept
|
;(metadata as unknown as GraphNoun).noun = NounType.Concept
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the service that's updating the metadata
|
// Get the service that's updating the metadata
|
||||||
const service = options.service || this.getCurrentAugmentation()
|
const service = options.service || this.getCurrentAugmentation()
|
||||||
const graphNoun = metadata as unknown as GraphNoun
|
const graphNoun = metadata as unknown as GraphNoun
|
||||||
|
|
||||||
// Preserve existing createdBy and createdAt if they exist
|
// Preserve existing createdBy and createdAt if they exist
|
||||||
const existingMetadata = await this.storage!.getMetadata(id) as any
|
const existingMetadata = await this.storage!.getMetadata(id) as any
|
||||||
|
|
||||||
if (existingMetadata &&
|
if (existingMetadata &&
|
||||||
typeof existingMetadata === 'object' &&
|
typeof existingMetadata === 'object' &&
|
||||||
'createdBy' in existingMetadata) {
|
'createdBy' in existingMetadata) {
|
||||||
// Preserve the original creator information
|
// Preserve the original creator information
|
||||||
graphNoun.createdBy = existingMetadata.createdBy
|
graphNoun.createdBy = existingMetadata.createdBy
|
||||||
|
|
||||||
// Also preserve creation timestamp if it exists
|
// Also preserve creation timestamp if it exists
|
||||||
if ('createdAt' in existingMetadata) {
|
if ('createdAt' in existingMetadata) {
|
||||||
graphNoun.createdAt = existingMetadata.createdAt
|
graphNoun.createdAt = existingMetadata.createdAt
|
||||||
|
|
@ -1393,7 +1393,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
augmentation: service,
|
augmentation: service,
|
||||||
version: '1.0' // TODO: Get actual version from augmentation
|
version: '1.0' // TODO: Get actual version from augmentation
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set createdAt if it doesn't exist
|
// Set createdAt if it doesn't exist
|
||||||
if (!graphNoun.createdAt) {
|
if (!graphNoun.createdAt) {
|
||||||
const now = new Date()
|
const now = new Date()
|
||||||
|
|
@ -1403,7 +1403,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always update the updatedAt timestamp
|
// Always update the updatedAt timestamp
|
||||||
const now = new Date()
|
const now = new Date()
|
||||||
graphNoun.updatedAt = {
|
graphNoun.updatedAt = {
|
||||||
|
|
@ -1414,7 +1414,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
|
|
||||||
// Update metadata
|
// Update metadata
|
||||||
await this.storage!.saveMetadata(id, metadata)
|
await this.storage!.saveMetadata(id, metadata)
|
||||||
|
|
||||||
// Track metadata statistics
|
// Track metadata statistics
|
||||||
const service = options.service || this.getCurrentAugmentation()
|
const service = options.service || this.getCurrentAugmentation()
|
||||||
await this.storage!.incrementStatistic('metadata', service)
|
await this.storage!.incrementStatistic('metadata', service)
|
||||||
|
|
@ -1513,7 +1513,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
seconds: Math.floor(now.getTime() / 1000),
|
seconds: Math.floor(now.getTime() / 1000),
|
||||||
nanoseconds: (now.getTime() % 1000) * 1000000
|
nanoseconds: (now.getTime() % 1000) * 1000000
|
||||||
}
|
}
|
||||||
|
|
||||||
const metadata = options.missingNounMetadata || {
|
const metadata = options.missingNounMetadata || {
|
||||||
autoCreated: true,
|
autoCreated: true,
|
||||||
createdAt: timestamp,
|
createdAt: timestamp,
|
||||||
|
|
@ -1550,7 +1550,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
seconds: Math.floor(now.getTime() / 1000),
|
seconds: Math.floor(now.getTime() / 1000),
|
||||||
nanoseconds: (now.getTime() % 1000) * 1000000
|
nanoseconds: (now.getTime() % 1000) * 1000000
|
||||||
}
|
}
|
||||||
|
|
||||||
const metadata = options.missingNounMetadata || {
|
const metadata = options.missingNounMetadata || {
|
||||||
autoCreated: true,
|
autoCreated: true,
|
||||||
createdAt: timestamp,
|
createdAt: timestamp,
|
||||||
|
|
@ -1835,7 +1835,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
public size(): number {
|
public size(): number {
|
||||||
return this.index.size()
|
return this.index.size()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the number of nouns in the database (excluding verbs)
|
* Get the number of nouns in the database (excluding verbs)
|
||||||
* This is used for statistics reporting to match the expected behavior in tests
|
* This is used for statistics reporting to match the expected behavior in tests
|
||||||
|
|
@ -1844,13 +1844,13 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
private async getNounCount(): Promise<number> {
|
private async getNounCount(): Promise<number> {
|
||||||
// Get all verbs from storage
|
// Get all verbs from storage
|
||||||
const allVerbs = await this.storage!.getAllVerbs()
|
const allVerbs = await this.storage!.getAllVerbs()
|
||||||
|
|
||||||
// Create a set of verb IDs for faster lookup
|
// Create a set of verb IDs for faster lookup
|
||||||
const verbIds = new Set(allVerbs.map(verb => verb.id))
|
const verbIds = new Set(allVerbs.map(verb => verb.id))
|
||||||
|
|
||||||
// Get all nouns from the index
|
// Get all nouns from the index
|
||||||
const nouns = this.index.getNouns()
|
const nouns = this.index.getNouns()
|
||||||
|
|
||||||
// Count nouns that are not verbs
|
// Count nouns that are not verbs
|
||||||
let nounCount = 0
|
let nounCount = 0
|
||||||
for (const [id] of nouns.entries()) {
|
for (const [id] of nouns.entries()) {
|
||||||
|
|
@ -1858,7 +1858,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
nounCount++
|
nounCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nounCount
|
return nounCount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1906,7 +1906,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter by service if specified
|
// Filter by service if specified
|
||||||
const services = options.service
|
const services = options.service
|
||||||
? (Array.isArray(options.service) ? options.service : [options.service])
|
? (Array.isArray(options.service) ? options.service : [options.service])
|
||||||
: Object.keys({...stats.nounCount, ...stats.verbCount, ...stats.metadataCount})
|
: Object.keys({...stats.nounCount, ...stats.verbCount, ...stats.metadataCount})
|
||||||
|
|
||||||
|
|
@ -1934,11 +1934,11 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
|
|
||||||
// If statistics are not available, fall back to calculating them on-demand
|
// If statistics are not available, fall back to calculating them on-demand
|
||||||
console.warn('Persistent statistics not available, calculating on-demand')
|
console.warn('Persistent statistics not available, calculating on-demand')
|
||||||
|
|
||||||
// Get all verbs from storage
|
// Get all verbs from storage
|
||||||
const allVerbs = await this.storage!.getAllVerbs()
|
const allVerbs = await this.storage!.getAllVerbs()
|
||||||
const verbCount = allVerbs.length
|
const verbCount = allVerbs.length
|
||||||
|
|
||||||
// Get the noun count using the helper method
|
// Get the noun count using the helper method
|
||||||
const nounCount = await this.getNounCount()
|
const nounCount = await this.getNounCount()
|
||||||
|
|
||||||
|
|
@ -1973,9 +1973,9 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
// Initialize persistent statistics
|
// Initialize persistent statistics
|
||||||
const service = 'default'
|
const service = 'default'
|
||||||
await this.storage!.saveStatistics({
|
await this.storage!.saveStatistics({
|
||||||
nounCount: { [service]: nounCount },
|
nounCount: {[service]: nounCount},
|
||||||
verbCount: { [service]: verbCount },
|
verbCount: {[service]: verbCount},
|
||||||
metadataCount: { [service]: metadataCount },
|
metadataCount: {[service]: metadataCount},
|
||||||
hnswIndexSize,
|
hnswIndexSize,
|
||||||
lastUpdated: new Date().toISOString()
|
lastUpdated: new Date().toISOString()
|
||||||
})
|
})
|
||||||
|
|
@ -2061,19 +2061,19 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
|
|
||||||
// First use the HNSW index to find similar vectors efficiently
|
// First use the HNSW index to find similar vectors efficiently
|
||||||
const searchResults = await this.index.search(queryVector, k * 2)
|
const searchResults = await this.index.search(queryVector, k * 2)
|
||||||
|
|
||||||
// Get all verbs for filtering
|
// Get all verbs for filtering
|
||||||
const allVerbs = await this.storage!.getAllVerbs()
|
const allVerbs = await this.storage!.getAllVerbs()
|
||||||
|
|
||||||
// Create a map of verb IDs for faster lookup
|
// Create a map of verb IDs for faster lookup
|
||||||
const verbMap = new Map<string, GraphVerb>()
|
const verbMap = new Map<string, GraphVerb>()
|
||||||
for (const verb of allVerbs) {
|
for (const verb of allVerbs) {
|
||||||
verbMap.set(verb.id, verb)
|
verbMap.set(verb.id, verb)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter search results to only include verbs
|
// Filter search results to only include verbs
|
||||||
const verbResults: Array<GraphVerb & { similarity: number }> = []
|
const verbResults: Array<GraphVerb & { similarity: number }> = []
|
||||||
|
|
||||||
for (const result of searchResults) {
|
for (const result of searchResults) {
|
||||||
// Search results are [id, distance] tuples
|
// Search results are [id, distance] tuples
|
||||||
const [id, distance] = result
|
const [id, distance] = result
|
||||||
|
|
@ -2085,21 +2085,21 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
verbResults.push({
|
verbResults.push({
|
||||||
...verb,
|
...verb,
|
||||||
similarity: distance
|
similarity: distance
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we didn't get enough results from the index, fall back to the old method
|
// If we didn't get enough results from the index, fall back to the old method
|
||||||
if (verbResults.length < k) {
|
if (verbResults.length < k) {
|
||||||
console.warn('Not enough verb results from HNSW index, falling back to manual search')
|
console.warn('Not enough verb results from HNSW index, falling back to manual search')
|
||||||
|
|
||||||
// Get verbs to search through
|
// Get verbs to search through
|
||||||
let verbs: GraphVerb[] = []
|
let verbs: GraphVerb[] = []
|
||||||
|
|
||||||
// If verb types are specified, get verbs of those types
|
// If verb types are specified, get verbs of those types
|
||||||
if (options.verbTypes && options.verbTypes.length > 0) {
|
if (options.verbTypes && options.verbTypes.length > 0) {
|
||||||
// Get verbs for each verb type in parallel
|
// Get verbs for each verb type in parallel
|
||||||
|
|
@ -2107,7 +2107,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
this.getVerbsByType(verbType)
|
this.getVerbsByType(verbType)
|
||||||
)
|
)
|
||||||
const verbArrays = await Promise.all(verbPromises)
|
const verbArrays = await Promise.all(verbPromises)
|
||||||
|
|
||||||
// Combine all verbs
|
// Combine all verbs
|
||||||
for (const verbArray of verbArrays) {
|
for (const verbArray of verbArrays) {
|
||||||
verbs.push(...verbArray)
|
verbs.push(...verbArray)
|
||||||
|
|
@ -2116,7 +2116,7 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
// Use all verbs
|
// Use all verbs
|
||||||
verbs = allVerbs
|
verbs = allVerbs
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate similarity for each verb not already in results
|
// Calculate similarity for each verb not already in results
|
||||||
const existingIds = new Set(verbResults.map(v => v.id))
|
const existingIds = new Set(verbResults.map(v => v.id))
|
||||||
for (const verb of verbs) {
|
for (const verb of verbs) {
|
||||||
|
|
@ -2132,10 +2132,10 @@ export class BrainyData<T = any> implements BrainyDataInterface<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort by similarity (ascending distance)
|
// Sort by similarity (ascending distance)
|
||||||
verbResults.sort((a, b) => a.similarity - b.similarity)
|
verbResults.sort((a, b) => a.similarity - b.similarity)
|
||||||
|
|
||||||
// Take top k results
|
// Take top k results
|
||||||
return verbResults.slice(0, k)
|
return verbResults.slice(0, k)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue