Skip to main content
PraisonAI TypeScript provides a Router for directing requests to specialized agents based on keywords or patterns.

Installation

npm install praisonai
The simplified Router class uses keyword-based routing:
import { Agent, Router } from 'praisonai';

// Create specialized agents
const mathAgent = new Agent({ instructions: 'You are a math expert.' });
const codeAgent = new Agent({ instructions: 'You are a coding expert.' });

// Create router with keyword-based routing
const router = new Router({
  math: { agent: mathAgent, keywords: ['math', 'calculate', '+', '-', '*', '/'] },
  code: { agent: codeAgent, keywords: ['code', 'program', 'function', 'javascript'] }
});

// Route requests automatically
await router.chat('Calculate 2+2');  // Routes to math agent
await router.chat('Write a function');  // Routes to code agent

Router with Patterns

Use regex patterns for more complex routing:
import { Agent, Router } from 'praisonai';

const router = new Router({
  math: { 
    agent: new Agent({ instructions: 'Math expert' }), 
    pattern: /\d+\s*[\+\-\*\/]\s*\d+/  // Matches "2+2", "10 * 5", etc.
  },
  greeting: { 
    agent: new Agent({ instructions: 'Friendly greeter' }), 
    keywords: ['hello', 'hi', 'hey', 'greetings'] 
  }
});

await router.chat('What is 10 * 5?');  // Routes to math
await router.chat('Hello there!');     // Routes to greeting

Advanced Router (Legacy)

For more control, use the RouterAgent class with custom conditions:
import { RouterAgent, createRouter, routeConditions, Agent } from 'praisonai';

const mathAgent = new Agent({ instructions: 'You are a math expert.' });
const codeAgent = new Agent({ instructions: 'You are a coding expert.' });

const router = createRouter({
  routes: [
    {
      agent: mathAgent,
      condition: routeConditions.keywords(['math', 'calculate', 'equation'])
    },
    {
      agent: codeAgent,
      condition: routeConditions.keywords(['code', 'program', 'function'])
    }
  ],
  defaultAgent: mathAgent
});

const result = await router.route('Calculate 2+2');
console.log('Routed to:', result?.agent.name);
console.log('Response:', result?.response);

Route Conditions

import { routeConditions } from 'praisonai';

// Match by keywords
routeConditions.keywords(['math', 'calculate'])

// Match by regex
routeConditions.pattern(/\d+\s*[\+\-\*\/]\s*\d+/)

// Match by metadata
routeConditions.metadata('category', 'technical')

// Always match (for default)
routeConditions.always()

// Combine with AND
routeConditions.and(
  routeConditions.keywords(['code']),
  routeConditions.metadata('language', 'typescript')
)

// Combine with OR
routeConditions.or(
  routeConditions.keywords(['python']),
  routeConditions.keywords(['javascript'])
)

Priority Routing

const router = createRouter({
  routes: [
    {
      agent: urgentAgent,
      condition: routeConditions.keywords(['urgent', 'emergency']),
      priority: 100  // Higher priority
    },
    {
      agent: generalAgent,
      condition: routeConditions.always(),
      priority: 0
    }
  ]
});

Adding Routes Dynamically

const router = createRouter({ routes: [] });

router.addRoute({
  agent: newAgent,
  condition: routeConditions.keywords(['new-topic']),
  priority: 50
});

Context-Aware Routing

const result = await router.route('Help me', {
  history: ['previous message'],
  metadata: { userId: '123', premium: true }
});

Verbose Mode

const router = createRouter({
  routes: [...],
  verbose: true  // Logs routing decisions
});