ExAgent.Patterns.Router (ExAgent v0.2.0)

Copy Markdown View Source

Parallel dispatch and synthesis pattern.

A stateless module that classifies input, dispatches to multiple specialized agents in parallel, and synthesizes results into a single response.

Summary

Functions

Evaluates each route's match_fn against the input.

Dispatches input to all given routes' agents in parallel.

Routes input through matching agents and synthesizes results.

Combines results with route name headers.

Types

route()

@type route() :: %{
  name: String.t(),
  agent: pid() | atom(),
  match_fn: (String.t() -> boolean())
}

router_opts()

@type router_opts() :: [
  routes: [route()],
  synthesizer: (String.t(), [{String.t(), String.t()}] -> String.t()),
  timeout: pos_integer()
]

Functions

classify(input, routes)

@spec classify(String.t(), [route()]) :: [route()]

Evaluates each route's match_fn against the input.

Returns all routes whose match_fn returns true.

dispatch(input, routes, timeout \\ 30000)

@spec dispatch(String.t(), [route()], pos_integer()) :: [{String.t(), String.t()}]

Dispatches input to all given routes' agents in parallel.

Returns a list of {route_name, response_content} tuples. Failed agents are included with an error message.

route(input, opts)

@spec route(String.t(), router_opts()) :: {:ok, String.t()} | {:error, term()}

Routes input through matching agents and synthesizes results.

  1. Classifies the input by evaluating each route's match_fn
  2. Dispatches to all matching agents in parallel
  3. Synthesizes the results using the provided synthesizer function

Options

  • :routes (required) - list of route maps with :name, :agent, :match_fn
  • :synthesizer - function (input, results) -> combined_string (default: joins with headers)
  • :timeout - milliseconds to wait for each agent (default: 30_000)

synthesize(input, results, synthesizer \\ &default_synthesizer/2)

@spec synthesize(String.t(), [{String.t(), String.t()}], fun()) :: String.t()

Combines results with route name headers.