@bpinternal/gravity

中文文档
0.2.0发布于 2026/11/05MITTypeScriptESM + CJS构建来源已验证近 30 天下载 1.1万

Generic density-based clustering (UMAP + HDBSCAN), compiled from Rust to WebAssembly.

原始使用文档
文档目录6 节
  1. Usage
  2. Options
  3. Result shape
  4. How it works
  5. Development
  6. Disclaimer ⚠️

@bpinternal/gravity

Generic density-based clustering for high-dimensional embeddings, with no native dependencies. The clustering engine is written in Rust, compiled to wasm32-unknown-unknown, and wrapped in a small TypeScript API — so it runs anywhere Node runs, no system libraries, and no build step at install time.

The pipeline reduces vectors to a low-dimensional space with UMAP, then groups them with HDBSCAN. Points that don't fall into any dense region are returned as noise.

Usage

import { cluster } from '@bpinternal/gravity'

const result = cluster({
  ids: ['a', 'b', 'c', 'd'],
  embeddings: [
    [0.1, 0.2 /* ... */],
    [0.1, 0.3 /* ... */],
    [0.9, 0.8 /* ... */],
    [0.7, 0.1 /* ... */]
  ],
  dim: 1536
})

result.clusters // [{ itemIds, centroid, assignRadius }, ...]
result.noise // ids that weren't assigned to any cluster
result.labels // raw HDBSCAN label per input point, in input order (-1 = noise)

embeddings[i] is the vector for ids[i], and every vector must have exactly dim components.

Options

cluster takes an optional second argument to override the UMAP/HDBSCAN parameters. Any field left unset falls back to its default.

cluster(dataset, {
  // UMAP (dimensionality reduction)
  initType: 'PCA', // 'PCA' | 'Random'           (default 'PCA')
  nNeighbors: 15, //   local neighborhood size    (default 15)
  nComponents: 5, //   reduced dimensions         (default 5)
  minDist: 0, //       packing tightness          (default 0)
  spread: 1, //        embedded scale             (default 1)
  nEpochs: 500, //     optimization epochs        (default 500)
  negativeSampleRate: 5, //                        (default 5)
  seed: 42, //         RNG seed (for determinism) (default 42)

  // HDBSCAN (density clustering)
  minClusterSize: 10, // min points per cluster   (default 10)
  minSamples: 1 //      core-distance neighbors   (default 1)
})

Result shape

FieldDescription
clustersThe discovered clusters.
clusters[].itemIdsMember ids, ordered by ascending cosine distance from the centroid.
clusters[].centroidMean of the members in the original vector space.
clusters[].assignRadiusCosine-distance radius (clamped to [0.25, 0.35]) for assigning new points.
noiseIds not assigned to any cluster.
labelsRaw HDBSCAN label per input point, input order. -1 is noise.
umap_config / hdbscan_configThe parameters the run used.

The run is deterministic: the same dataset, in the same order, always yields the same clusters (UMAP is seeded with a fixed seed).

How it works

  1. UMAP projects the raw vectors (e.g. 1536-D) into a low-dimensional space (5-D) using a cosine metric, preserving local structure.
  2. HDBSCAN finds density clusters in the reduced space (euclidean metric, EOM cluster selection), labelling sparse points as noise.
  3. Centroids and an assignment radius are computed in the original vector space so downstream code can assign new points by cosine similarity.

Development

The engine lives in rssrc/ and is exposed across the WASM boundary with wasm-bindgen. wasm-pack compiles it to node and web targets, the wasm binary is base64-inlined into the generated glue, and build.ts bundles the TypeScript wrapper in src/ around it with esbuild. This matches the shape of the other Rust packages in this repo (entities, verel).

Building requires the Rust toolchain (the wasm target is added automatically by wasm-pack):

pnpm build        # wasm-pack (node + web) + esbuild bundles + type declarations
pnpm build:node   # just the Node target
pnpm build:web    # just the browser target
pnpm build:worker # just the Cloudflare Workers target
pnpm test         # run the Rust tests, then build the node target and run the wrapper tests
pnpm test:rust    # just the Rust unit tests (cargo)
pnpm test:unit    # just the TS wrapper tests (vitest)
pnpm check        # format + type check

The build emits dist/node (CommonJS), dist/web (ESM), dist/worker (ESM), and dist/types. The node and web bundles inline the wasm as base64. Cloudflare Workers cannot compile WebAssembly from bytes, so dist/worker imports gravity_bg.wasm as a module, which wrangler and the Cloudflare Vite plugin compile at build time. The package's workerd export condition selects it, so import { cluster } from '@bpinternal/gravity' works in a Worker with no setup.

Disclaimer ⚠️

This package is published under the @bpinternal organization. All packages of this organization are meant to be used by the Botpress team internally and are not meant for our community. Since the packages are catered to our own use-cases, they might have less stable APIs, receive breaking changes without much warning, have minimal documentation and lack community-focused support. However, these packages were still left intentionally public for an important reason : We Love Open-Source. Therefore, if you wish to install or fork this package feel absolutely free to do it. We strongly recommend that you tag your versions properly.

The Botpress Engineering team.

版本与发布记录

@bpinternal/gravity 共发布 3 个版本,首次发布于 2026/07/01;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.0latest2026/11/053 天前
0.1.12026/07/014 个月前
0.1.02026/07/014 个月前

关于 @bpinternal/gravity 的常见问题

@bpinternal/gravity 是什么?

Generic density-based clustering (UMAP + HDBSCAN), compiled from Rust to WebAssembly.

如何安装 @bpinternal/gravity?

在项目目录运行 npm install @bpinternal/gravity,或使用 pnpm add / yarn add / bun add。

@bpinternal/gravity 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bpinternal/gravity 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bpinternal/gravity 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bpinternal/gravity 可以商用吗?

@bpinternal/gravity 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bpinternal/gravity 的下载量有多少?

根据 npm 官方下载统计,@bpinternal/gravity 在 2026/10/09 – 2026/11/07 这 30 天内被下载 10,910 次,日均约 364 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bpinternal/gravity 还在维护吗?

最新版本 0.2.0 发布于 2026/11/05(3 天前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/07/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。