# `Spectre.Router`
[🔗](https://github.com/elchemista/spectre/blob/0.3.0/lib/spectre/router/router.ex#L1)

Routes inbound input to a compiled Spectre rule.

Routing is evidence-first. Each configured strategy can add candidates to a
`%Spectre.Router.Context{}`; arbitration later decides which candidate becomes
the route. This keeps deterministic matches, trained classifiers, semantic
cache hits, and LLM fallback from overwriting each other prematurely.

# `candidate_rules`

```elixir
@spec candidate_rules(module(), Spectre.State.t()) :: [Spectre.Rule.t()]
```

Returns interrupt, current-flow, and fallback rules in evaluation order.

Interrupts are first so global commands like cancel/help remain responsive.
Current-flow rules come next to make multi-turn flows stable. Remaining rules
are kept as fallback candidates so users can still change topic.

Flows nest, so `current_flow` matches any rule whose `flow_path` contains the
flow name: setting `current_flow: :checkout` prioritizes rules declared in
`:checkout` and in every flow nested below it.

# `evaluate`

```elixir
@spec evaluate(module(), Spectre.Input.t() | String.t() | map(), keyword()) ::
  {:ok, Spectre.Router.Receipt.t()}
```

Evaluates routing without executing a handler, action, or state adapter.

Evaluation runs the real input and router pipelines but disables journal
delivery and online semantic learning. The returned receipt contains no raw
input, prompt, model output, matches, or handlers.

    {:ok, receipt} = Spectre.Router.evaluate(MyAgent, "help")

# `route`

```elixir
@spec route(Spectre.Input.t(), Spectre.Context.t()) ::
  {:ok, Spectre.Route.t()} | {:error, term()}
```

Routes input to the best matching compiled agent rule.

    {:ok, route} = Spectre.Router.route(input, ctx)

# `route_context`

```elixir
@spec route_context(Spectre.Input.t(), Spectre.Context.t()) ::
  {:ok, Spectre.Router.Context.t()} | {:error, term()}
```

Runs the router pipeline and returns the full routing context.

This is useful for runtimes that need enriched input fields produced by
router plugs.

    {:ok, router_context} = Spectre.Router.route_context(input, ctx)

# `route_from_context`

```elixir
@spec route_from_context(Spectre.Router.Context.t()) ::
  {:ok, Spectre.Route.t()} | {:error, term()}
```

Converts a completed router context into the selected route.

    {:ok, route} = Spectre.Router.route_from_context(router_context)

---

*Consult [api-reference.md](api-reference.md) for complete listing*
