fix: extraction, multi-hop traversal, and aggregate result shape (BR-ADV-FEATURES-BUN)
Three advanced-API correctness fixes, all reproducible on Node (not Bun-specific):
- Entity/concept extraction returned []. SmartExtractor combined agreeing signals
with a weighted sum compared against an absolute 0.60 gate, so a confident
low-weight signal lost selection to a mediocre high-weight one that then failed
the gate, dropping the whole result. Select and gate on a normalized weighted
average instead. The public `confidence` option now controls the threshold (was
a dead hardcoded 0.60), and the embedding-signal timeout is raised 100ms -> 2000ms
so the neural signal is not silently dropped on slower runtimes.
- Multi-hop find({ connected }) returned only the 1-hop neighbour. executeGraphSearch
ignored depth/via; it now delegates to the depth-aware neighbors() BFS.
- find({ aggregate }) hid groupKey/metrics/count under .metadata, so callers
expecting AggregateResult saw empty rows. Expose those fields at the top level.
Adds real-embedding regression tests in tests/integration/advanced-apis-regression.test.ts.
This commit is contained in:
parent
07754d135a
commit
0a9d1d9a17
8 changed files with 297 additions and 74 deletions
|
|
@ -4559,7 +4559,7 @@ export class Brainy<T = any> implements BrainyInterface<T> {
|
|||
}
|
||||
): Promise<string[]> {
|
||||
const entities = await this.extract(text, {
|
||||
types: [NounType.Concept, NounType.Concept],
|
||||
types: [NounType.Concept],
|
||||
confidence: options?.confidence || 0.7,
|
||||
neuralMatching: true
|
||||
})
|
||||
|
|
@ -6787,36 +6787,58 @@ export class Brainy<T = any> implements BrainyInterface<T> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Execute graph search component with O(1) traversal
|
||||
* Execute graph search component.
|
||||
*
|
||||
* Honors the full `GraphConstraints` contract: multi-hop `depth` (breadth-first via
|
||||
* `neighbors()`), `via`/`type` verb-type filtering, and `direction`. Previously this read
|
||||
* only `from`/`to`/`direction` and did a single 1-hop `getNeighbors()`, so `depth` and `via`
|
||||
* were silently ignored — `find({ connected: { from, depth: 3 } })` returned only the
|
||||
* immediate neighbour at every depth.
|
||||
*/
|
||||
private async executeGraphSearch(params: FindParams<T>, existingResults: Result<T>[]): Promise<Result<T>[]> {
|
||||
if (!params.connected) return existingResults
|
||||
|
||||
const { from, to, direction = 'both' } = params.connected
|
||||
const connectedIds: string[] = []
|
||||
|
||||
|
||||
const { from, to, depth, direction = 'both' } = params.connected
|
||||
const via = params.connected.via ?? params.connected.type
|
||||
|
||||
// GraphConstraints speaks 'in' | 'out' | 'both'; neighbors() speaks 'incoming' | 'outgoing' | 'both'.
|
||||
const toNeighborDir = (d: 'in' | 'out' | 'both'): 'incoming' | 'outgoing' | 'both' =>
|
||||
d === 'in' ? 'incoming' : d === 'out' ? 'outgoing' : 'both'
|
||||
|
||||
// Collect reachable ids honoring depth + verb-type filter. `via` may be a single VerbType
|
||||
// or an array; reuse the depth-aware BFS in neighbors() (one pass per requested verb type).
|
||||
const collect = async (id: string, dir: 'incoming' | 'outgoing' | 'both'): Promise<string[]> => {
|
||||
const verbTypes: Array<VerbType | undefined> =
|
||||
via === undefined ? [undefined] : Array.isArray(via) ? via : [via]
|
||||
const ids = new Set<string>()
|
||||
for (const verbType of verbTypes) {
|
||||
const hops = await this.neighbors(id, { direction: dir, depth: depth ?? 1, verbType })
|
||||
for (const n of hops) ids.add(n)
|
||||
}
|
||||
return [...ids]
|
||||
}
|
||||
|
||||
const connectedIds = new Set<string>()
|
||||
|
||||
if (from) {
|
||||
const neighbors = await this.graphIndex.getNeighbors(from, direction)
|
||||
connectedIds.push(...neighbors)
|
||||
for (const id of await collect(from, toNeighborDir(direction))) connectedIds.add(id)
|
||||
}
|
||||
|
||||
|
||||
if (to) {
|
||||
const reverseDirection = direction === 'in' ? 'out' : direction === 'out' ? 'in' : 'both'
|
||||
const neighbors = await this.graphIndex.getNeighbors(to, reverseDirection)
|
||||
connectedIds.push(...neighbors)
|
||||
const reverse: 'in' | 'out' | 'both' = direction === 'in' ? 'out' : direction === 'out' ? 'in' : 'both'
|
||||
for (const id of await collect(to, toNeighborDir(reverse))) connectedIds.add(id)
|
||||
}
|
||||
|
||||
|
||||
// Filter existing results to only connected entities
|
||||
if (existingResults.length > 0) {
|
||||
const connectedIdSet = new Set(connectedIds)
|
||||
return existingResults.filter(r => connectedIdSet.has(r.id))
|
||||
return existingResults.filter(r => connectedIds.has(r.id))
|
||||
}
|
||||
|
||||
// Batch-load connected entities for 10x faster cloud storage performance
|
||||
// GCS: 20 entities = 1×50ms vs 20×50ms = 1000ms (20x faster)
|
||||
|
||||
// Batch-load connected entities for fast cloud-storage performance
|
||||
const results: Result<T>[] = []
|
||||
const entitiesMap = await this.batchGet(connectedIds)
|
||||
for (const id of connectedIds) {
|
||||
const ids = [...connectedIds]
|
||||
const entitiesMap = await this.batchGet(ids)
|
||||
for (const id of ids) {
|
||||
const entity = entitiesMap.get(id)
|
||||
if (entity) {
|
||||
results.push(this.createResult(id, 1.0, entity))
|
||||
|
|
@ -7912,7 +7934,14 @@ export class Brainy<T = any> implements BrainyInterface<T> {
|
|||
type: NounType.Measurement,
|
||||
metadata: entity.metadata,
|
||||
data: entity.data,
|
||||
entity
|
||||
entity,
|
||||
// Surface the documented AggregateResult fields at the top level so consumers can read
|
||||
// groupKey/metrics/count directly. Previously these were only reachable under .metadata,
|
||||
// so callers expecting an AggregateResult saw rows with no groupKey/metrics/count and
|
||||
// interpreted the output as degenerate/empty.
|
||||
groupKey: agg.groupKey,
|
||||
metrics: agg.metrics,
|
||||
count: agg.count
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue