2025-08-26 12:32:21 -07:00
|
|
|
/**
|
2025-09-17 11:54:20 -07:00
|
|
|
* BrainyInterface - Modern API Only
|
|
|
|
|
*
|
|
|
|
|
* This interface defines the MODERN methods from Brainy 3.0.
|
|
|
|
|
* Used to break circular dependencies while enforcing modern API usage.
|
|
|
|
|
*
|
|
|
|
|
* NO DEPRECATED METHODS - Only clean, modern API patterns.
|
2025-08-26 12:32:21 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { Vector } from '../coreTypes.js'
|
2025-09-17 11:54:20 -07:00
|
|
|
import { AddParams, RelateParams, Result, Entity, FindParams, SimilarParams } from './brainy.types.js'
|
2026-01-06 14:52:12 -08:00
|
|
|
import { NounType, VerbType } from './graphTypes.js'
|
feat: add migration system with error handling, validation, and enterprise hardening
- MigrationRunner: per-entity error tracking (non-fatal), maxErrors bail-out,
static validateMigrations() called in constructor, branch error propagation
- RefManager: updateRefMetadata() method for clean metadata updates
- brainy.ts: eliminate as-any casts in migration methods, use updateRefMetadata,
forward maxErrors through full chain including branch migrations
- Types: MigrationError interface, errors field on MigrationResult, maxErrors on MigrateOptions
- Package exports: MigrationError type, migrate() on BrainyInterface, autoMigrate config
- 31 integration tests covering error handling, validation, branch error propagation
- Documentation: docs/guides/schema-migrations.md
2026-02-09 16:13:14 -08:00
|
|
|
import type { MigrationPreview, MigrationResult, MigrateOptions } from '../migration/types.js'
|
2025-08-26 12:32:21 -07:00
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
export interface BrainyInterface<T = unknown> {
|
2025-08-26 12:32:21 -07:00
|
|
|
/**
|
|
|
|
|
* Initialize the database
|
|
|
|
|
*/
|
|
|
|
|
init(): Promise<void>
|
|
|
|
|
|
2026-01-07 12:51:05 -08:00
|
|
|
/**
|
2026-01-27 15:38:21 -08:00
|
|
|
* Promise that resolves when initialization is complete
|
2026-01-07 12:51:05 -08:00
|
|
|
* Can be awaited multiple times safely.
|
|
|
|
|
*/
|
|
|
|
|
readonly ready: Promise<void>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if basic initialization is complete
|
|
|
|
|
*/
|
|
|
|
|
readonly isInitialized: boolean
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-27 15:38:21 -08:00
|
|
|
* Check if all initialization including background tasks is complete
|
2026-01-07 12:51:05 -08:00
|
|
|
*/
|
|
|
|
|
isFullyInitialized(): boolean
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-27 15:38:21 -08:00
|
|
|
* Wait for all background initialization tasks to complete
|
2026-01-07 12:51:05 -08:00
|
|
|
* For cloud storage adapters, this waits for bucket validation and count sync.
|
|
|
|
|
*/
|
|
|
|
|
awaitBackgroundInit(): Promise<void>
|
|
|
|
|
|
2025-08-26 12:32:21 -07:00
|
|
|
/**
|
2025-09-17 11:54:20 -07:00
|
|
|
* Modern add method - unified entity creation
|
|
|
|
|
* @param params Parameters for adding entities
|
|
|
|
|
* @returns The ID of the created entity
|
2025-08-26 12:32:21 -07:00
|
|
|
*/
|
2025-09-17 11:54:20 -07:00
|
|
|
add(params: AddParams<T>): Promise<string>
|
2025-08-26 12:32:21 -07:00
|
|
|
|
|
|
|
|
/**
|
2025-09-17 11:54:20 -07:00
|
|
|
* Modern relate method - unified relationship creation
|
|
|
|
|
* @param params Parameters for creating relationships
|
|
|
|
|
* @returns The ID of the created relationship
|
2025-08-26 12:32:21 -07:00
|
|
|
*/
|
2025-09-17 11:54:20 -07:00
|
|
|
relate(params: RelateParams<T>): Promise<string>
|
2025-08-26 12:32:21 -07:00
|
|
|
|
|
|
|
|
/**
|
2025-09-17 11:54:20 -07:00
|
|
|
* Modern find method - unified search and discovery
|
|
|
|
|
* @param query Search query or parameters object
|
|
|
|
|
* @returns Array of search results
|
2025-08-26 12:32:21 -07:00
|
|
|
*/
|
2025-09-17 11:54:20 -07:00
|
|
|
find(query: string | FindParams<T>): Promise<Result<T>[]>
|
2025-08-26 12:32:21 -07:00
|
|
|
|
|
|
|
|
/**
|
2025-09-17 11:54:20 -07:00
|
|
|
* Modern get method - retrieve entities by ID
|
|
|
|
|
* @param id The entity ID to retrieve
|
|
|
|
|
* @returns Entity or null if not found
|
2025-08-26 12:32:21 -07:00
|
|
|
*/
|
2025-09-17 11:54:20 -07:00
|
|
|
get(id: string): Promise<Entity<T> | null>
|
2025-08-26 12:32:21 -07:00
|
|
|
|
|
|
|
|
/**
|
2025-09-17 11:54:20 -07:00
|
|
|
* Modern similar method - find similar entities
|
|
|
|
|
* @param params Parameters for similarity search
|
|
|
|
|
* @returns Array of similar entities with scores
|
2025-08-26 12:32:21 -07:00
|
|
|
*/
|
2025-09-17 11:54:20 -07:00
|
|
|
similar(params: SimilarParams<T>): Promise<Result<T>[]>
|
2025-08-26 12:32:21 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate embedding vector from text
|
2026-01-06 14:52:12 -08:00
|
|
|
* @param data The data to embed (text, array, or object)
|
|
|
|
|
* @returns Vector representation of the data
|
2025-08-26 12:32:21 -07:00
|
|
|
*/
|
2026-01-06 14:52:12 -08:00
|
|
|
embed(data: any): Promise<Vector>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Batch embed multiple texts at once
|
|
|
|
|
* @param texts Array of texts to embed
|
|
|
|
|
* @returns Array of embedding vectors (384 dimensions each)
|
|
|
|
|
*/
|
|
|
|
|
embedBatch(texts: string[]): Promise<number[][]>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate semantic similarity between two texts
|
|
|
|
|
* @param textA First text
|
|
|
|
|
* @param textB Second text
|
|
|
|
|
* @returns Similarity score between 0 and 1
|
|
|
|
|
*/
|
|
|
|
|
similarity(textA: string, textB: string): Promise<number>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get comprehensive index statistics
|
|
|
|
|
* @returns Index statistics object
|
|
|
|
|
*/
|
|
|
|
|
indexStats(): Promise<{
|
|
|
|
|
entities: number
|
|
|
|
|
vectors: number
|
|
|
|
|
relationships: number
|
|
|
|
|
metadataFields: string[]
|
|
|
|
|
memoryUsage: {
|
|
|
|
|
vectors: number
|
|
|
|
|
graph: number
|
|
|
|
|
metadata: number
|
|
|
|
|
total: number
|
|
|
|
|
}
|
|
|
|
|
}>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get graph neighbors of an entity
|
|
|
|
|
* @param entityId The entity to get neighbors for
|
|
|
|
|
* @param options Optional traversal options
|
|
|
|
|
* @returns Array of neighbor entity IDs
|
|
|
|
|
*/
|
|
|
|
|
neighbors(
|
|
|
|
|
entityId: string,
|
|
|
|
|
options?: {
|
|
|
|
|
direction?: 'outgoing' | 'incoming' | 'both'
|
|
|
|
|
depth?: number
|
|
|
|
|
verbType?: VerbType
|
|
|
|
|
limit?: number
|
|
|
|
|
}
|
|
|
|
|
): Promise<string[]>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find semantic duplicates in the database
|
|
|
|
|
* @param options Optional search options
|
|
|
|
|
* @returns Array of duplicate groups with similarity scores
|
|
|
|
|
*/
|
|
|
|
|
findDuplicates(options?: {
|
|
|
|
|
threshold?: number
|
|
|
|
|
type?: NounType
|
|
|
|
|
limit?: number
|
|
|
|
|
}): Promise<Array<{
|
|
|
|
|
entity: Entity<any>
|
|
|
|
|
duplicates: Array<{ entity: Entity<any>; similarity: number }>
|
|
|
|
|
}>>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cluster entities by semantic similarity
|
|
|
|
|
* @param options Optional clustering options
|
|
|
|
|
* @returns Array of clusters with entities and optional centroids
|
|
|
|
|
*/
|
|
|
|
|
cluster(options?: {
|
|
|
|
|
threshold?: number
|
|
|
|
|
type?: NounType
|
|
|
|
|
minClusterSize?: number
|
|
|
|
|
limit?: number
|
|
|
|
|
includeCentroid?: boolean
|
|
|
|
|
}): Promise<Array<{
|
|
|
|
|
clusterId: string
|
|
|
|
|
entities: Entity<any>[]
|
|
|
|
|
centroid?: number[]
|
|
|
|
|
}>>
|
feat: add migration system with error handling, validation, and enterprise hardening
- MigrationRunner: per-entity error tracking (non-fatal), maxErrors bail-out,
static validateMigrations() called in constructor, branch error propagation
- RefManager: updateRefMetadata() method for clean metadata updates
- brainy.ts: eliminate as-any casts in migration methods, use updateRefMetadata,
forward maxErrors through full chain including branch migrations
- Types: MigrationError interface, errors field on MigrationResult, maxErrors on MigrateOptions
- Package exports: MigrationError type, migrate() on BrainyInterface, autoMigrate config
- 31 integration tests covering error handling, validation, branch error propagation
- Documentation: docs/guides/schema-migrations.md
2026-02-09 16:13:14 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run pending data migrations, or preview what would change.
|
|
|
|
|
*
|
|
|
|
|
* @param options - Pass { dryRun: true } to preview without writing
|
|
|
|
|
* @returns Migration result or preview depending on options
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```typescript
|
|
|
|
|
* // Preview
|
|
|
|
|
* const preview = await brain.migrate({ dryRun: true })
|
|
|
|
|
* console.log(preview.affectedEntities)
|
|
|
|
|
*
|
|
|
|
|
* // Apply
|
|
|
|
|
* const result = await brain.migrate()
|
|
|
|
|
* console.log(result.backupBranch) // 'pre-migration-7.17.0'
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
migrate(options?: MigrateOptions): Promise<MigrationResult | MigrationPreview>
|
2025-09-17 11:54:20 -07:00
|
|
|
}
|