@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
52 lines (51 loc) • 2.42 kB
TypeScript
/**
* Tool-signature deduplication — pure module, no I/O, no side effects.
*
* ## Algorithm
*
* `computeToolSignature` builds a canonical, order-insensitive string from:
* 1. The tool's name (exact, case-preserved).
* 2. A normalised description: lowercased, all whitespace runs collapsed to a
* single space, leading/trailing whitespace stripped.
* 3. A sorted, deduplicated list of JSON-schema property names (top-level
* `properties` keys from `inputSchema`). If the schema exposes a `type`
* field on each property, that type string is appended as `name:type`.
*
* `dedupeTools` clusters tools by pairwise token-set Jaccard similarity:
*
* Jaccard(A, B) = |A ∩ B| / |A ∪ B|
*
* where A and B are the _sets_ of whitespace-split tokens from each tool's
* canonical signature. This is deterministic, symmetric, and independent of
* token ordering — making it robust to minor rewording while being fast (O(n²)
* in the number of tools, which is bounded in practice by provider limits).
*
* The first tool encountered in stable input-iteration order becomes the
* cluster representative; subsequent tools in the same cluster are dropped.
*/
import type { Tool } from "../types/index.js";
import type { ToolDedupConfig, ToolDedupResult } from "../types/index.js";
/**
* Build a canonical, order-insensitive signature string for a named tool.
*
* The signature is composed of:
* - the tool's name
* - a normalised description (lowercased, whitespace-collapsed)
* - sorted parameter property names (with types when available)
*
* Stable regardless of property declaration order in the schema.
*/
export declare function computeToolSignature(name: string, tool: Tool): string;
/**
* Collapse near-duplicate tools in a name→Tool record.
*
* When `options.enabled` is falsy (the default), returns the original record
* and an empty `removed` array — byte-for-byte unchanged behaviour.
*
* When enabled, tools whose token-set Jaccard similarity (over their canonical
* signatures) meets or exceeds `options.threshold` (default 0.9) are collapsed
* to a single representative (the first in stable iteration order).
*
* Any exception thrown internally returns the ORIGINAL tool set (fail-open).
*/
export declare function dedupeTools<T extends Tool>(tools: Record<string, T>, options: ToolDedupConfig): ToolDedupResult<Record<string, T>>;