LlmCore.Router (llm_core v0.3.0)

Copy Markdown View Source

GenServer that resolves task types to full LLM agent configurations.

The Router maintains a RoutingTable loaded from TOML configuration and resolves task strings (like "coding", "reasoning") to ResolvedRoute structs containing the provider alias, execution mode, and agent metadata.

Configuration

Routing rules are defined in TOML under [routing]:

[routing]
default = "claude"

[routing.tasks.coding]
alias = "openai"
mode = "passthrough"
capabilities = { structured_output = true, tool_use = true }

Usage

# Resolve a task type
{:ok, route} = LlmCore.Router.resolve(:coding)
route.alias #=> "openai"
route.mode #=> :passthrough

# Send a prompt through routing
{:ok, response} = LlmCore.Router.send("Write a function", :coding)

# Stream
{:ok, stream} = LlmCore.Router.stream("Explain this", :reasoning)

Hot Reload

The Router listens for {:config_reloaded, :routing} messages and refreshes its routing table from Config.Store. It also syncs every 60 seconds as a safety net.

Summary

Functions

Returns a specification to start this module under a supervisor.

Returns the current routing table (debug/introspection).

Resolves a task type (e.g., "coding", "planning") to a full agent config.

Resolves a task type using a provided routing table (used for execution snapshots).

Sends a prompt through the router, automatically selecting the provider.

Sends a CommBus protocol packet through the router. Pass :task_type via opts to override metadata-derived routing.

Starts the Router GenServer linked to the calling process.

Initiates a streaming prompt through the routed provider.

Streams a CommBus protocol packet through the routed provider.

Forces an immediate sync from the config store.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_routing_table()

@spec get_routing_table() :: LlmCore.Router.RoutingTable.t() | nil

Returns the current routing table (debug/introspection).

resolve(task_type)

@spec resolve(String.t() | atom()) ::
  {:ok, LlmCore.Router.ResolvedRoute.t()} | {:error, term()}

Resolves a task type (e.g., "coding", "planning") to a full agent config.

resolve_from_table(task_type, table)

@spec resolve_from_table(String.t() | atom(), LlmCore.Router.RoutingTable.t()) ::
  {:ok, LlmCore.Router.ResolvedRoute.t()} | {:error, term()}

Resolves a task type using a provided routing table (used for execution snapshots).

send(prompt, task_type, opts \\ [])

@spec send(String.t(), String.t() | atom(), keyword()) ::
  {:ok, LlmCore.LLM.Response.t()} | {:error, term()}

Sends a prompt through the router, automatically selecting the provider.

send_packet(packet, opts \\ [])

@spec send_packet(
  map(),
  keyword()
) :: {:ok, LlmCore.LLM.Response.t()} | {:error, term()}

Sends a CommBus protocol packet through the router. Pass :task_type via opts to override metadata-derived routing.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts the Router GenServer linked to the calling process.

stream(prompt, task_type, opts \\ [])

@spec stream(String.t(), String.t() | atom(), keyword()) ::
  {:ok, Enumerable.t()} | {:error, term()}

Initiates a streaming prompt through the routed provider.

stream_packet(packet, opts \\ [])

@spec stream_packet(
  map(),
  keyword()
) :: {:ok, Enumerable.t()} | {:error, term()}

Streams a CommBus protocol packet through the routed provider.

sync()

@spec sync() :: :ok

Forces an immediate sync from the config store.