# d0n9x1n/copilot-relay architecture

> Architecture diagram of the GitHub repository d0n9x1n/copilot-relay, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/d0n9x1n/copilot-relay
- Repository: https://github.com/d0n9x1n/copilot-relay
- Video tour (about a minute): https://gitdiagram.com/d0n9x1n/copilot-relay/video

## Overview

copilot-relay is a local Claude Messages API-compatible proxy that lets Claude Code use models available through a GitHub Copilot subscription. The main workflow is Claude Code request → HTTP admission and routing → Claude-to-Copilot translation or relay-managed web search → Copilot upstream → Claude response/stream. The repository also includes authentication and service controls, model discovery and deep checks, prompt-cache reporting, and offline request replay. Edges reflect the excerpts, SOURCE INDEX references, or README; some feature internals are unsampled, so their connections are documented only where supported.

Main entry points: [`src/server.ts`](https://github.com/d0n9x1n/copilot-relay/blob/main/src/server.ts) (HTTP server), [`src/main.ts`](https://github.com/d0n9x1n/copilot-relay/blob/main/src/main.ts) (CLI dispatcher), [`src/routes/claude.ts`](https://github.com/d0n9x1n/copilot-relay/blob/main/src/routes/claude.ts) (Messages route), [`src/copilot/client.ts`](https://github.com/d0n9x1n/copilot-relay/blob/main/src/copilot/client.ts) (Copilot HTTP client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI and service"]
  node_cli["CLI dispatcher<br/>[main.ts]"]
  node_auth["Copilot auth<br/>[auth.ts]"]
  node_service["Service lifecycle<br/>[start.ts]"]
  node_health["Relay status<br/>[status.ts]"]
  node_models["Model commands<br/>[models.ts]"]
end

subgraph group_runtime["Relay runtime"]
  node_server["HTTP server<br/>[server.ts]"]
  node_config["Runtime config<br/>[config.ts]"]
  node_state["Request state<br/>[state.ts]"]
end

subgraph group_compat["Claude compatibility"]
  node_claude_route["Messages route<br/>[claude.ts]"]
  node_translate["Request translation<br/>[translate.ts]"]
  node_response_stream["Claude streaming<br/>[stream.ts]"]
  node_web_search["Web search bridge<br/>[web-search.ts]"]
end

subgraph group_upstream["Copilot integration"]
  node_copilot_chat["Chat completions<br/>[chat.ts]"]
  node_native["Native Claude transport<br/>[native.ts]"]
  node_client["Copilot HTTP client<br/>[client.ts]"]
  node_catalog["Model catalog<br/>[models.ts]"]
end

subgraph group_ops["Diagnostics"]
  node_cache_report["Cache report<br/>[cache.ts]"]
  node_replay["Request replay<br/>[replay.ts]"]
  node_trace["Request capture<br/>[request-trace.ts]"]
  node_logs["Logging<br/>[log.ts]"]
end

node_user(("Claude Code user"))
node_claude["Claude Code"]
node_github["GitHub Copilot"]

node_user -->|"runs"| node_claude
node_claude -->|"sends requests"| node_server
node_cli -->|"dispatches"| node_auth
node_cli -->|"dispatches"| node_service
node_cli -->|"dispatches"| node_health
node_cli -->|"dispatches"| node_models
node_cli -->|"dispatches"| node_cache_report
node_cli -->|"dispatches"| node_replay
node_auth -->|"authenticates with"| node_github
node_service -.->|"starts"| node_server
node_claude -->|"connects to"| node_server
node_server -->|"routes requests"| node_claude_route
node_server -->|"snapshots config"| node_config
node_server -->|"sets request state"| node_state
node_server -->|"wraps request"| node_trace
node_claude_route -->|"translates"| node_translate
node_claude_route -->|"calls"| node_copilot_chat
node_claude_route -->|"streams response"| node_response_stream
node_claude_route -->|"delegates search"| node_web_search
node_claude_route -.->|"selects transport"| node_native
node_copilot_chat -->|"sends requests"| node_client
node_web_search -->|"searches via"| node_client
node_catalog -->|"fetches models"| node_client
node_models -.->|"discovers models"| node_catalog
node_client -->|"records traffic"| node_trace
node_client -->|"logs requests"| node_logs
node_trace -->|"captures for replay"| node_replay
node_cache_report -->|"reads local logs"| node_logs
node_copilot_chat -.->|"requests completions"| node_github
node_native -.->|"uses native transport"| node_github

click node_cli "https://github.com/d0n9x1n/copilot-relay/blob/main/src/main.ts"
click node_auth "https://github.com/d0n9x1n/copilot-relay/blob/main/src/auth.ts"
click node_service "https://github.com/d0n9x1n/copilot-relay/blob/main/src/start.ts"
click node_health "https://github.com/d0n9x1n/copilot-relay/blob/main/src/status.ts"
click node_server "https://github.com/d0n9x1n/copilot-relay/blob/main/src/server.ts"
click node_config "https://github.com/d0n9x1n/copilot-relay/blob/main/src/lib/config.ts"
click node_state "https://github.com/d0n9x1n/copilot-relay/blob/main/src/lib/state.ts"
click node_claude_route "https://github.com/d0n9x1n/copilot-relay/blob/main/src/routes/claude.ts"
click node_translate "https://github.com/d0n9x1n/copilot-relay/blob/main/src/claude/translate.ts"
click node_response_stream "https://github.com/d0n9x1n/copilot-relay/blob/main/src/claude/stream.ts"
click node_web_search "https://github.com/d0n9x1n/copilot-relay/blob/main/src/claude/web-search.ts"
click node_copilot_chat "https://github.com/d0n9x1n/copilot-relay/blob/main/src/copilot/chat.ts"
click node_native "https://github.com/d0n9x1n/copilot-relay/blob/main/src/copilot/native.ts"
click node_client "https://github.com/d0n9x1n/copilot-relay/blob/main/src/copilot/client.ts"
click node_catalog "https://github.com/d0n9x1n/copilot-relay/blob/main/src/copilot/models.ts"
click node_models "https://github.com/d0n9x1n/copilot-relay/blob/main/src/models.ts"
click node_cache_report "https://github.com/d0n9x1n/copilot-relay/blob/main/src/cache.ts"
click node_replay "https://github.com/d0n9x1n/copilot-relay/blob/main/src/replay.ts"
click node_trace "https://github.com/d0n9x1n/copilot-relay/blob/main/src/lib/request-trace.ts"
click node_logs "https://github.com/d0n9x1n/copilot-relay/blob/main/src/lib/log.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_auth,node_service,node_health,node_models,node_user toneBlue
class node_server,node_config,node_state toneAmber
class node_claude_route,node_translate,node_response_stream,node_web_search toneMint
class node_copilot_chat,node_native,node_client,node_catalog toneRose
class node_cache_report,node_replay,node_trace,node_logs,node_claude,node_github toneIndigo
```

## Components

### CLI and service

- [CLI dispatcher](https://github.com/d0n9x1n/copilot-relay/blob/main/src/main.ts): `src/main.ts`
- [Copilot auth](https://github.com/d0n9x1n/copilot-relay/blob/main/src/auth.ts): `src/auth.ts`
- [Service lifecycle](https://github.com/d0n9x1n/copilot-relay/blob/main/src/start.ts): `src/start.ts`
- [Relay status](https://github.com/d0n9x1n/copilot-relay/blob/main/src/status.ts): `src/status.ts`
- [Model commands](https://github.com/d0n9x1n/copilot-relay/blob/main/src/models.ts): `src/models.ts`

### Relay runtime

- [HTTP server](https://github.com/d0n9x1n/copilot-relay/blob/main/src/server.ts): `src/server.ts`
- [Runtime config](https://github.com/d0n9x1n/copilot-relay/blob/main/src/lib/config.ts): `src/lib/config.ts`
- [Request state](https://github.com/d0n9x1n/copilot-relay/blob/main/src/lib/state.ts): `src/lib/state.ts`

### Claude compatibility

- [Messages route](https://github.com/d0n9x1n/copilot-relay/blob/main/src/routes/claude.ts): `src/routes/claude.ts`
- [Request translation](https://github.com/d0n9x1n/copilot-relay/blob/main/src/claude/translate.ts): `src/claude/translate.ts`
- [Claude streaming](https://github.com/d0n9x1n/copilot-relay/blob/main/src/claude/stream.ts): `src/claude/stream.ts`
- [Web search bridge](https://github.com/d0n9x1n/copilot-relay/blob/main/src/claude/web-search.ts): `src/claude/web-search.ts`

### Copilot integration

- [Chat completions](https://github.com/d0n9x1n/copilot-relay/blob/main/src/copilot/chat.ts): `src/copilot/chat.ts`
- [Native Claude transport](https://github.com/d0n9x1n/copilot-relay/blob/main/src/copilot/native.ts): `src/copilot/native.ts`
- [Copilot HTTP client](https://github.com/d0n9x1n/copilot-relay/blob/main/src/copilot/client.ts): `src/copilot/client.ts`
- [Model catalog](https://github.com/d0n9x1n/copilot-relay/blob/main/src/copilot/models.ts): `src/copilot/models.ts`

### Diagnostics

- [Cache report](https://github.com/d0n9x1n/copilot-relay/blob/main/src/cache.ts): `src/cache.ts`
- [Request replay](https://github.com/d0n9x1n/copilot-relay/blob/main/src/replay.ts): `src/replay.ts`
- [Request capture](https://github.com/d0n9x1n/copilot-relay/blob/main/src/lib/request-trace.ts): `src/lib/request-trace.ts`
- [Logging](https://github.com/d0n9x1n/copilot-relay/blob/main/src/lib/log.ts): `src/lib/log.ts`

### Other components

- **Claude Code user**
- **Claude Code**
- **GitHub Copilot**

## Connections

- Claude Code user → Claude Code: runs
- Claude Code → HTTP server: sends requests
- CLI dispatcher → Copilot auth: dispatches (evidence: `src/main.ts`)
- CLI dispatcher → Service lifecycle: dispatches (evidence: `src/main.ts`)
- CLI dispatcher → Relay status: dispatches (evidence: `src/main.ts`)
- CLI dispatcher → Model commands: dispatches (evidence: `src/main.ts`)
- CLI dispatcher → Cache report: dispatches (evidence: `src/main.ts`)
- CLI dispatcher → Request replay: dispatches (evidence: `src/main.ts`)
- Copilot auth → GitHub Copilot: authenticates with
- Service lifecycle → HTTP server: starts
- Claude Code → HTTP server: connects to
- HTTP server → Messages route: routes requests (evidence: `src/server.ts`)
- HTTP server → Runtime config: snapshots config (evidence: `src/server.ts`)
- HTTP server → Request state: sets request state (evidence: `src/server.ts`)
- HTTP server → Request capture: wraps request (evidence: `src/server.ts`)
- Messages route → Request translation: translates (evidence: `src/routes/claude.ts`)
- Messages route → Chat completions: calls (evidence: `src/routes/claude.ts`)
- Messages route → Claude streaming: streams response (evidence: `src/routes/claude.ts`)
- Messages route → Web search bridge: delegates search (evidence: `src/routes/claude.ts`)
- Messages route → Native Claude transport: selects transport (evidence: `src/routes/claude.ts`)
- Chat completions → Copilot HTTP client: sends requests (evidence: `src/copilot/chat.ts`)
- Web search bridge → Copilot HTTP client: searches via (evidence: `src/claude/web-search.ts`)
- Model catalog → Copilot HTTP client: fetches models (evidence: `src/copilot/models.ts`)
- Model commands → Model catalog: discovers models
- Copilot HTTP client → Request capture: records traffic (evidence: `src/copilot/client.ts`)
- Copilot HTTP client → Logging: logs requests (evidence: `src/copilot/client.ts`)
- Request capture → Request replay: captures for replay (evidence: `src/replay.ts`)
- Cache report → Logging: reads local logs
- Chat completions → GitHub Copilot: requests completions
- Native Claude transport → GitHub Copilot: uses native transport
