feat: export provider contracts for the plugin surface brainy consumes

Native accelerators (cortex) register providers for metadataIndex, graphIndex,
hnsw, entityIdMapper, cache, columnStore, and aggregation. Until now the exact
method/property surface brainy calls on each was implicit — a provider could
drop a member brainy depends on and only fail at runtime when that path ran.

This defines and exports the provider contracts from the stable
@soulcraft/brainy/plugin entrypoint:
- MetadataIndexProvider, GraphIndexProvider, HnswProvider,
  EntityIdMapperProvider, CacheProvider — each typed as exactly the surface
  brainy calls (optional/feature-detected members like HNSW setPersistMode and
  enableCOW are intentionally excluded).
- Re-exports ColumnStoreProvider and AggregationProvider (and the aggregate
  types) from the same entrypoint so a plugin author can import the whole
  provider surface from one place.

Brainy's own baseline classes now `implements` these contracts
(MetadataIndexManager, GraphAdjacencyIndex, HNSWIndex, EntityIdMapper,
UnifiedCache), so the interfaces can never silently diverge from what brainy
ships — and any provider that declares `implements` gets a compile error the
moment brainy starts requiring a new member.

The embeddings/embedBatch providers stay typed by the existing EmbeddingFunction
(no duplicate interface added). Type-only changes; no runtime behavior change.
This commit is contained in:
David Snelling 2026-05-27 14:45:40 -07:00
parent 46fc7f2c4a
commit 4b6f63ed67
6 changed files with 227 additions and 7 deletions

View file

@ -17,6 +17,7 @@ import { getGlobalCache, UnifiedCache } from '../utils/unifiedCache.js'
import { prodLog } from '../utils/logger.js'
import { quantizeSQ8, distanceSQ8 } from '../utils/vectorQuantization.js'
import type { SQ8QuantizedVector } from '../utils/vectorQuantization.js'
import type { HnswProvider } from '../plugin.js'
// Default HNSW parameters
const DEFAULT_CONFIG: HNSWConfig = {
@ -26,7 +27,12 @@ const DEFAULT_CONFIG: HNSWConfig = {
ml: 16 // Max level
}
export class HNSWIndex {
/**
* Implements {@link HnswProvider}: the vector-index surface Brainy calls on
* whatever the `'hnsw'` factory returns (its own `HNSWIndex`, or Cortex's
* native engine).
*/
export class HNSWIndex implements HnswProvider {
private nouns: Map<string, HNSWNoun> = new Map()
private entryPointId: string | null = null
private maxLevel = 0