API Reference

Complete API documentation for semantic-node-router

Router

Constructor

typescript
new Router(config: RouterConfig)

Creates a new Router instance with the specified configuration.

Parameters:

  • routes: Route[] - Array of Route instances (required)
  • encoder: BaseEncoder - Encoder instance (required)
  • aggregationMethod: 'max' | 'mean' | 'sum' - Similarity aggregation (optional, default: 'max')
  • topK: number - Default number of top matches (optional, default: 1)
  • enableCache: boolean - Enable LRU cache (optional, default: true)
  • cacheSize: number - Cache size (optional, default: 1000)

initialize()

typescript
async initialize(): Promise<void>

Initialize the router by encoding all route utterances. Must be called before routing.

route()

typescript
async route(query: string): Promise<RouteResult>

Route a query to the best matching route.

Parameters:

  • query: string - The query to route

Returns:

{ route: string | null, score: number }

Returns null route if no match exceeds the threshold.

routeTopK()

typescript
async routeTopK(query: string, k?: number): Promise<RouteResult[]>

Get the top K matching routes sorted by score.

addRoute()

typescript
async addRoute(route: Route): Promise<void>

Dynamically add a new route. The route will be immediately encoded and available for routing.

removeRoute()

typescript
removeRoute(routeName: string): boolean

Remove a route by name. Returns true if removed, false if not found.

getRoutes()

typescript
getRoutes(): Route[]

Get all routes (returns a copy). Use this to access route metadata or inspect current routes.

clearCache() / getCacheStats()

typescript
clearCache(): void
getCacheStats(): { size: number, hits: number, misses: number }

Manage the internal LRU cache for query embeddings.