|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +This is a reverse-engineered proxy that transforms the GitHub Copilot API into OpenAI and Anthropic-compatible endpoints. It enables using GitHub Copilot with any tool that supports OpenAI Chat Completions or Anthropic Messages APIs. |
| 8 | + |
| 9 | +**Runtime:** Bun (>= 1.2.x) |
| 10 | +**Primary framework:** Hono (web server) |
| 11 | +**CLI framework:** citty (command definitions) |
| 12 | + |
| 13 | +## Development Commands |
| 14 | + |
| 15 | +```bash |
| 16 | +# Development (watch mode) |
| 17 | +bun run dev |
| 18 | + |
| 19 | +# Production build |
| 20 | +bun run build |
| 21 | + |
| 22 | +# Production start |
| 23 | +bun run start |
| 24 | + |
| 25 | +# Linting |
| 26 | +bun run lint # Lint staged files |
| 27 | +bun run lint:all # Lint entire codebase |
| 28 | + |
| 29 | +# Type checking |
| 30 | +bun run typecheck |
| 31 | + |
| 32 | +# Testing |
| 33 | +bun test # Run all tests |
| 34 | +bun test tests/example.test.ts # Run single test |
| 35 | + |
| 36 | +# Release |
| 37 | +bun run release # Bumps version and publishes |
| 38 | +``` |
| 39 | + |
| 40 | +## Architecture |
| 41 | + |
| 42 | +### Command Structure |
| 43 | + |
| 44 | +The CLI uses a subcommand architecture defined in `src/main.ts`: |
| 45 | + |
| 46 | +- **`start`** (`src/start.ts`): Main server command. Handles GitHub auth, Copilot token management, and starts the Hono server |
| 47 | +- **`auth`** (`src/auth.ts`): Standalone GitHub authentication flow |
| 48 | +- **`check-usage`** (`src/check-usage.ts`): Display Copilot usage/quota in terminal |
| 49 | +- **`debug`** (`src/debug.ts`): Display diagnostic information |
| 50 | + |
| 51 | +### Server Layer (`src/server.ts`) |
| 52 | + |
| 53 | +Hono app with the following routes: |
| 54 | + |
| 55 | +``` |
| 56 | +/v1/chat/completions → OpenAI-compatible chat completions |
| 57 | +/v1/models → List available Copilot models |
| 58 | +/v1/embeddings → OpenAI-compatible embeddings |
| 59 | +/v1/messages → Anthropic-compatible messages endpoint |
| 60 | +/v1/messages/count_tokens → Token counting |
| 61 | +/usage → Usage/quota monitoring |
| 62 | +/token → Current Copilot token |
| 63 | +``` |
| 64 | + |
| 65 | +Routes without `/v1` prefix are also exposed for compatibility. |
| 66 | + |
| 67 | +### Core Flow |
| 68 | + |
| 69 | +1. **Token Management** (`src/lib/token.ts`): |
| 70 | + - GitHub OAuth device flow → GitHub token → stored in `~/.local/share/copilot-api/github-token` |
| 71 | + - GitHub token → Copilot token (via `services/github/get-copilot-token.ts`) |
| 72 | + - Copilot token auto-refreshes on interval |
| 73 | + |
| 74 | +2. **Request Translation** (`src/routes/`): |
| 75 | + - **Anthropic → OpenAI**: `routes/messages/handler.ts` translates Anthropic Messages API to OpenAI format before calling Copilot |
| 76 | + - **Direct OpenAI**: `routes/chat-completions/handler.ts` passes through with minimal modification |
| 77 | + |
| 78 | +3. **Copilot API Calls** (`src/services/copilot/`): |
| 79 | + - All Copilot API calls go through `create-chat-completions.ts` |
| 80 | + - Headers include auth token, VSCode version, account type, and X-Initiator (user vs agent) |
| 81 | + |
| 82 | +### State Management (`src/lib/state.ts`) |
| 83 | + |
| 84 | +Global singleton state object holds: |
| 85 | +- `githubToken`, `copilotToken`: Authentication tokens |
| 86 | +- `accountType`: "individual" | "business" | "enterprise" |
| 87 | +- `models`, `vsCodeVersion`: Cached from Copilot API |
| 88 | +- `manualApprove`, `rateLimitSeconds`, `rateLimitWait`: User-configured behavior |
| 89 | + |
| 90 | +### Key Modules |
| 91 | + |
| 92 | +- **`lib/approval.ts`**: Manual request approval flow (when `--manual` flag used) |
| 93 | +- **`lib/rate-limit.ts`**: Rate limiting enforcement |
| 94 | +- **`lib/tokenizer.ts`**: Token counting using `gpt-tokenizer` |
| 95 | +- **`lib/api-config.ts`**: Constructs headers and base URLs for Copilot API (varies by account type) |
| 96 | +- **`lib/proxy.ts`**: Proxy configuration from environment variables |
| 97 | + |
| 98 | +### Translation Layers |
| 99 | + |
| 100 | +**Anthropic → OpenAI** (`routes/messages/`): |
| 101 | +- `non-stream-translation.ts`: Bidirectional translation for non-streaming requests/responses |
| 102 | +- `stream-translation.ts`: Translates OpenAI SSE chunks to Anthropic event stream format |
| 103 | +- `anthropic-types.ts`: Type definitions for Anthropic Messages API |
| 104 | + |
| 105 | +### GitHub Services (`services/github/`) |
| 106 | + |
| 107 | +- `get-device-code.ts`: Initiates OAuth device flow |
| 108 | +- `poll-access-token.ts`: Polls for user authorization |
| 109 | +- `get-copilot-token.ts`: Exchanges GitHub token for Copilot token |
| 110 | +- `get-copilot-usage.ts`: Fetches usage/quota data |
| 111 | + |
| 112 | +## TypeScript Configuration |
| 113 | + |
| 114 | +- **Module system**: ESNext with `"type": "module"` in package.json |
| 115 | +- **Path aliases**: `~/*` maps to `src/*` |
| 116 | +- **Strict mode**: Enabled with `noUnusedLocals`, `noUnusedParameters`, `noFallthroughCasesInSwitch` |
| 117 | +- **No emit**: Type checking only; build handled by tsdown |
| 118 | + |
| 119 | +## Testing |
| 120 | + |
| 121 | +Uses Bun's built-in test runner. Place tests in `tests/` directory with `*.test.ts` naming. |
| 122 | + |
| 123 | +## Account Type Behavior |
| 124 | + |
| 125 | +The `--account-type` flag changes the Copilot API endpoint: |
| 126 | +- **individual**: `api.githubcopilot.com` |
| 127 | +- **business/enterprise**: `api.individual.githubcopilot.com` |
| 128 | + |
| 129 | +See `lib/api-config.ts` for implementation details. |
| 130 | + |
| 131 | +## Important Context |
| 132 | + |
| 133 | +- **Security warning**: This is a reverse-engineered proxy. Excessive automated use may trigger GitHub abuse detection |
| 134 | +- **Token persistence**: GitHub token stored in `~/.local/share/copilot-api/` (use `lib/paths.ts` for path logic) |
| 135 | +- **VSCode version**: The proxy mimics VSCode to authenticate with Copilot (`services/get-vscode-version.ts`) |
| 136 | +- **Streaming**: Both streaming and non-streaming are supported. Response type determined by `stream` parameter in request |
0 commit comments