# `MingaClaudeBridge.Route`
[🔗](https://github.com/jsmestad/minga-claude-bridge/blob/main/lib/minga_claude_bridge/route.ex#L1)

The `MingaAgent.InferenceRoute` that serves Claude models through the user's Claude Code login.

Minga runs the agent: tools, approvals, retries, and history. This route only turns each Minga request into input for a long-lived Claude Code process (`MingaClaudeBridge.Session`) and turns the session's events back into Minga route events.

## Conversations

Each Minga conversation (`ConversationRef.session_id`) has at most one live session, started under `MingaClaudeBridge.SessionSupervisor` and found through `MingaClaudeBridge.Registry`. Its working directory key is derived from the conversation id, so it is stable across restarts. The session stops when the Minga agent session that owns the conversation (`ConversationRef.owner`) exits.

For a `:turn` request the route compares the request with what the session has seen (`MingaClaudeBridge.History`, built by `MingaClaudeBridge.Route.Mapping`):

  * The request adds only tool results, and the session waits for them: the results answer the held tool calls.
  * The request adds only user messages, and the session is idle: they are sent as the next prompt.
  * Anything else (a new conversation, an edit, branch, reset, compaction, a changed system prompt, tool set, model, or effort, a dead session, or a mix of tool results and user input): the old session stops and a fresh one starts. Earlier history is imported as one transcript text block in front of the newest input. This import is lossy; see `MingaClaudeBridge.Route.Mapping`. Importing a Claude Code session file for `--resume` is a follow-up.

After a turn finishes without an error, the session records the request's history plus the reply in the same normalized form Minga sends next time. A turn that ends with Claude Code's `result` also refreshes plan usage with `get_usage` on the now idle session.

`:summary` and `:title` requests, and turns without a conversation id, run in an isolated session with no tools and no session persistence. It stops when the stream ends, is cancelled, or its consumer exits.

## Streams

`stream/1` sends the request to the session with the calling process as the subscriber and returns a lazy stream that drains the session's `{ref, event}` messages in that process. The session monitors the subscriber, so a consumer that exits aborts the turn. `cancel`, which Minga calls when the user stops a turn or the consumer exits early, stops the conversation's Claude Code process; the next request starts a fresh one.

# `availability`

```elixir
@spec availability(
  String.t(),
  MingaAgent.InferenceRoute.Credential.t() | nil,
  MingaClaudeBridge.Route.Config.t()
) :: MingaAgent.InferenceRoute.availability()
```

`availability/2` with an explicit config.

# `stream`

```elixir
@spec stream(
  MingaAgent.InferenceRoute.Request.t(),
  MingaClaudeBridge.Route.Config.t()
) ::
  {:ok, MingaAgent.InferenceRoute.Response.t()}
```

`stream/1` with an explicit config.

---

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