group

← All adapters

import { group } from '@routecraft/routecraft'

Transformer that groups an array into clusters using a comparator. Use with .transform(group(options)). By default it reads the body as the array and replaces the body with the array of clusters; use from / to to read and write sub-fields, and map to shape each cluster.

.transform(group({
  comparator: cosine({ field: 'embedding', threshold: 0.82 }),
  from: (body) => body.items,
  map: (cluster) => ({ size: cluster.length, first: cluster[0] }),
}))

Options (GroupOptions):

OptionTypeRequiredDescription
comparatorComparator<T>YesDecides whether two items belong in the same cluster (e.g. from cosine())
from(body) => T[]NoRead the array to cluster (default: the body itself)
map(cluster: T[]) => RNoShape each resulting cluster (default: the raw cluster)
to(body, result: R[]) => unknownNoWrite the clusters back (default: replace the body)