diff --git a/README.md b/README.md index 0d36c13c9..d87f9e878 100644 --- a/README.md +++ b/README.md @@ -1,351 +1,89 @@ # Copilot API Proxy > [!WARNING] -> This is a reverse-engineered proxy of GitHub Copilot API. It is not supported by GitHub, and may break unexpectedly. Use at your own risk. +> This project proxies GitHub Copilot into OpenAI-compatible and Anthropic-compatible endpoints. It is unofficial and may break if upstream behavior changes. -> [!WARNING] -> **GitHub Security Notice:** -> Excessive automated or scripted use of Copilot (including rapid or bulk requests, such as via automated tools) may trigger GitHub's abuse-detection systems. -> You may receive a warning from GitHub Security, and further anomalous activity could result in temporary suspension of your Copilot access. -> -> GitHub prohibits use of their servers for excessive automated bulk activity or any activity that places undue burden on their infrastructure. -> -> Please review: -> -> - [GitHub Acceptable Use Policies](https://docs.github.com/site-policy/acceptable-use-policies/github-acceptable-use-policies#4-spam-and-inauthentic-activity-on-github) -> - [GitHub Copilot Terms](https://docs.github.com/site-policy/github-terms/github-terms-for-additional-products-and-features#github-copilot) -> -> Use this proxy responsibly to avoid account restrictions. - -[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/E1E519XS7W) - ---- - -**Note:** If you are using [opencode](https://github.com/sst/opencode), you do not need this project. Opencode supports GitHub Copilot provider out of the box. +## Overview ---- +This service exposes GitHub Copilot through a small compatibility layer so it can be used by tools expecting OpenAI or Anthropic style APIs. -## Project Overview - -A reverse-engineered proxy for the GitHub Copilot API that exposes it as an OpenAI and Anthropic compatible service. This allows you to use GitHub Copilot with any tool that supports the OpenAI Chat Completions API or the Anthropic Messages API, including to power [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview). +Compared with the original upstream project, this fork keeps the README intentionally simpler and includes support for the `responses` passthrough endpoint. ## Features -- **OpenAI & Anthropic Compatibility**: Exposes GitHub Copilot as an OpenAI-compatible (`/v1/chat/completions`, `/v1/models`, `/v1/embeddings`) and Anthropic-compatible (`/v1/messages`) API. -- **Claude Code Integration**: Easily configure and launch [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview) to use Copilot as its backend with a simple command-line flag (`--claude-code`). -- **Usage Dashboard**: A web-based dashboard to monitor your Copilot API usage, view quotas, and see detailed statistics. -- **Rate Limit Control**: Manage API usage with rate-limiting options (`--rate-limit`) and a waiting mechanism (`--wait`) to prevent errors from rapid requests. -- **Manual Request Approval**: Manually approve or deny each API request for fine-grained control over usage (`--manual`). -- **Token Visibility**: Option to display GitHub and Copilot tokens during authentication and refresh for debugging (`--show-token`). -- **Flexible Authentication**: Authenticate interactively or provide a GitHub token directly, suitable for CI/CD environments. -- **Support for Different Account Types**: Works with individual, business, and enterprise GitHub Copilot plans. - -## Demo - -https://github.com/user-attachments/assets/7654b383-669d-4eb9-b23c-06d7aefee8c5 - -## Prerequisites - -- Bun (>= 1.2.x) -- GitHub account with Copilot subscription (individual, business, or enterprise) +- OpenAI-compatible endpoints for chat, models, embeddings, and responses +- Anthropic-compatible messages endpoint +- Usage and token inspection endpoints +- Optional rate limit control and manual approval flow +- Support for individual, business, and enterprise Copilot accounts ## Installation -To install dependencies, run: - ```sh bun install ``` -## Using with Docker - -Build image - -```sh -docker build -t copilot-api . -``` - -Run the container - -```sh -# Create a directory on your host to persist the GitHub token and related data -mkdir -p ./copilot-data - -# Run the container with a bind mount to persist the token -# This ensures your authentication survives container restarts - -docker run -p 4141:4141 -v $(pwd)/copilot-data:/root/.local/share/copilot-api copilot-api -``` +## Run -> **Note:** -> The GitHub token and related data will be stored in `copilot-data` on your host. This is mapped to `/root/.local/share/copilot-api` inside the container, ensuring persistence across restarts. - -### Docker with Environment Variables - -You can pass the GitHub token directly to the container using environment variables: +Development: ```sh -# Build with GitHub token -docker build --build-arg GH_TOKEN=your_github_token_here -t copilot-api . - -# Run with GitHub token -docker run -p 4141:4141 -e GH_TOKEN=your_github_token_here copilot-api - -# Run with additional options -docker run -p 4141:4141 -e GH_TOKEN=your_token copilot-api start --verbose --port 4141 -``` - -### Docker Compose Example - -```yaml -version: "3.8" -services: - copilot-api: - build: . - ports: - - "4141:4141" - environment: - - GH_TOKEN=your_github_token_here - restart: unless-stopped -``` - -The Docker image includes: - -- Multi-stage build for optimized image size -- Non-root user for enhanced security -- Health check for container monitoring -- Pinned base image version for reproducible builds - -## Using with npx - -You can run the project directly using npx: - -```sh -npx copilot-api@latest start -``` - -With options: - -```sh -npx copilot-api@latest start --port 8080 +bun run dev ``` -For authentication only: +Production: ```sh -npx copilot-api@latest auth +bun run start ``` -## Command Structure - -Copilot API now uses a subcommand structure with these main commands: - -- `start`: Start the Copilot API server. This command will also handle authentication if needed. -- `auth`: Run GitHub authentication flow without starting the server. This is typically used if you need to generate a token for use with the `--github-token` option, especially in non-interactive environments. -- `check-usage`: Show your current GitHub Copilot usage and quota information directly in the terminal (no server required). -- `debug`: Display diagnostic information including version, runtime details, file paths, and authentication status. Useful for troubleshooting and support. - -## Command Line Options - -### Start Command Options - -The following command line options are available for the `start` command: +## Common Commands -| Option | Description | Default | Alias | -| -------------- | ----------------------------------------------------------------------------- | ---------- | ----- | -| --port | Port to listen on | 4141 | -p | -| --verbose | Enable verbose logging | false | -v | -| --account-type | Account type to use (individual, business, enterprise) | individual | -a | -| --manual | Enable manual request approval | false | none | -| --rate-limit | Rate limit in seconds between requests | none | -r | -| --wait | Wait instead of error when rate limit is hit | false | -w | -| --github-token | Provide GitHub token directly (must be generated using the `auth` subcommand) | none | -g | -| --claude-code | Generate a command to launch Claude Code with Copilot API config | false | -c | -| --show-token | Show GitHub and Copilot tokens on fetch and refresh | false | none | -| --proxy-env | Initialize proxy from environment variables | false | none | - -### Auth Command Options - -| Option | Description | Default | Alias | -| ------------ | ------------------------- | ------- | ----- | -| --verbose | Enable verbose logging | false | -v | -| --show-token | Show GitHub token on auth | false | none | - -### Debug Command Options - -| Option | Description | Default | Alias | -| ------ | ------------------------- | ------- | ----- | -| --json | Output debug info as JSON | false | none | +- Build: `bun run build` +- Lint: `bun run lint` +- Test: `bun test` +- Start: `bun run start` ## API Endpoints -The server exposes several endpoints to interact with the Copilot API. It provides OpenAI-compatible endpoints and now also includes support for Anthropic-compatible endpoints, allowing for greater flexibility with different tools and services. - -### OpenAI Compatible Endpoints - -These endpoints mimic the OpenAI API structure. +### OpenAI-compatible -| Endpoint | Method | Description | -| --------------------------- | ------ | --------------------------------------------------------- | -| `POST /v1/chat/completions` | `POST` | Creates a model response for the given chat conversation. | -| `GET /v1/models` | `GET` | Lists the currently available models. | -| `POST /v1/embeddings` | `POST` | Creates an embedding vector representing the input text. | +| Endpoint | Method | Notes | +| --- | --- | --- | +| `/chat/completions` | `POST` | Chat completions passthrough | +| `/v1/chat/completions` | `POST` | Chat completions passthrough | +| `/embeddings` | `POST` | Embeddings passthrough | +| `/v1/embeddings` | `POST` | Embeddings passthrough | +| `/models` | `GET` | Model list | +| `/v1/models` | `GET` | Model list | +| `/responses` | `POST` | Responses passthrough | +| `/v1/responses` | `POST` | Responses passthrough | -### Anthropic Compatible Endpoints +### Anthropic-compatible -These endpoints are designed to be compatible with the Anthropic Messages API. +| Endpoint | Method | Notes | +| --- | --- | --- | +| `/v1/messages` | `POST` | Anthropic messages compatibility | +| `/v1/messages/count_tokens` | `POST` | Token counting | -| Endpoint | Method | Description | -| -------------------------------- | ------ | ------------------------------------------------------------ | -| `POST /v1/messages` | `POST` | Creates a model response for a given conversation. | -| `POST /v1/messages/count_tokens` | `POST` | Calculates the number of tokens for a given set of messages. | +### Utility -### Usage Monitoring Endpoints +| Endpoint | Method | Notes | +| --- | --- | --- | +| `/usage` | `GET` | Usage information | +| `/token` | `GET` | Current Copilot token | -New endpoints for monitoring your Copilot usage and quotas. +## `responses` Support -| Endpoint | Method | Description | -| ------------ | ------ | ------------------------------------------------------------ | -| `GET /usage` | `GET` | Get detailed Copilot usage statistics and quota information. | -| `GET /token` | `GET` | Get the current Copilot token being used by the API. | +This fork adds direct passthrough for the OpenAI-style `responses` API: -## Example Usage +- `POST /responses` +- `POST /v1/responses` -Using with npx: - -```sh -# Basic usage with start command -npx copilot-api@latest start - -# Run on custom port with verbose logging -npx copilot-api@latest start --port 8080 --verbose - -# Use with a business plan GitHub account -npx copilot-api@latest start --account-type business - -# Use with an enterprise plan GitHub account -npx copilot-api@latest start --account-type enterprise - -# Enable manual approval for each request -npx copilot-api@latest start --manual - -# Set rate limit to 30 seconds between requests -npx copilot-api@latest start --rate-limit 30 - -# Wait instead of error when rate limit is hit -npx copilot-api@latest start --rate-limit 30 --wait - -# Provide GitHub token directly -npx copilot-api@latest start --github-token ghp_YOUR_TOKEN_HERE - -# Run only the auth flow -npx copilot-api@latest auth - -# Run auth flow with verbose logging -npx copilot-api@latest auth --verbose - -# Show your Copilot usage/quota in the terminal (no server needed) -npx copilot-api@latest check-usage - -# Display debug information for troubleshooting -npx copilot-api@latest debug - -# Display debug information in JSON format -npx copilot-api@latest debug --json - -# Initialize proxy from environment variables (HTTP_PROXY, HTTPS_PROXY, etc.) -npx copilot-api@latest start --proxy-env -``` - -## Using the Usage Viewer - -After starting the server, a URL to the Copilot Usage Dashboard will be displayed in your console. This dashboard is a web interface for monitoring your API usage. - -1. Start the server. For example, using npx: - ```sh - npx copilot-api@latest start - ``` -2. The server will output a URL to the usage viewer. Copy and paste this URL into your browser. It will look something like this: - `https://ericc-ch.github.io/copilot-api?endpoint=http://localhost:4141/usage` - - If you use the `start.bat` script on Windows, this page will open automatically. - -The dashboard provides a user-friendly interface to view your Copilot usage data: - -- **API Endpoint URL**: The dashboard is pre-configured to fetch data from your local server endpoint via the URL query parameter. You can change this URL to point to any other compatible API endpoint. -- **Fetch Data**: Click the "Fetch" button to load or refresh the usage data. The dashboard will automatically fetch data on load. -- **Usage Quotas**: View a summary of your usage quotas for different services like Chat and Completions, displayed with progress bars for a quick overview. -- **Detailed Information**: See the full JSON response from the API for a detailed breakdown of all available usage statistics. -- **URL-based Configuration**: You can also specify the API endpoint directly in the URL using a query parameter. This is useful for bookmarks or sharing links. For example: - `https://ericc-ch.github.io/copilot-api?endpoint=http://your-api-server/usage` - -## Using with Claude Code - -This proxy can be used to power [Claude Code](https://docs.anthropic.com/en/claude-code), an experimental conversational AI assistant for developers from Anthropic. - -There are two ways to configure Claude Code to use this proxy: - -### Interactive Setup with `--claude-code` flag - -To get started, run the `start` command with the `--claude-code` flag: - -```sh -npx copilot-api@latest start --claude-code -``` - -You will be prompted to select a primary model and a "small, fast" model for background tasks. After selecting the models, a command will be copied to your clipboard. This command sets the necessary environment variables for Claude Code to use the proxy. - -Paste and run this command in a new terminal to launch Claude Code. - -### Manual Configuration with `settings.json` - -Alternatively, you can configure Claude Code by creating a `.claude/settings.json` file in your project's root directory. This file should contain the environment variables needed by Claude Code. This way you don't need to run the interactive setup every time. - -Here is an example `.claude/settings.json` file: - -```json -{ - "env": { - "ANTHROPIC_BASE_URL": "http://localhost:4141", - "ANTHROPIC_AUTH_TOKEN": "dummy", - "ANTHROPIC_MODEL": "gpt-4.1", - "ANTHROPIC_DEFAULT_SONNET_MODEL": "gpt-4.1", - "ANTHROPIC_SMALL_FAST_MODEL": "gpt-4.1", - "ANTHROPIC_DEFAULT_HAIKU_MODEL": "gpt-4.1", - "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1", - "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1" - }, - "permissions": { - "deny": [ - "WebSearch" - ] - } -} -``` - -You can find more options here: [Claude Code settings](https://docs.anthropic.com/en/docs/claude-code/settings#environment-variables) - -You can also read more about IDE integration here: [Add Claude Code to your IDE](https://docs.anthropic.com/en/docs/claude-code/ide-integrations) - -## Running from Source - -The project can be run from source in several ways: - -### Development Mode - -```sh -bun run dev -``` - -### Production Mode - -```sh -bun run start -``` +The server forwards the incoming request body to Copilot's `responses` endpoint and returns the upstream response body, headers, and status code directly. -## Usage Tips +## Notes -- To avoid hitting GitHub Copilot's rate limits, you can use the following flags: - - `--manual`: Enables manual approval for each request, giving you full control over when requests are sent. - - `--rate-limit `: Enforces a minimum time interval between requests. For example, `copilot-api start --rate-limit 30` will ensure there's at least a 30-second gap between requests. - - `--wait`: Use this with `--rate-limit`. It makes the server wait for the cooldown period to end instead of rejecting the request with an error. This is useful for clients that don't automatically retry on rate limit errors. -- If you have a GitHub business or enterprise plan account with Copilot, use the `--account-type` flag (e.g., `--account-type business`). See the [official documentation](https://docs.github.com/en/enterprise-cloud@latest/copilot/managing-copilot/managing-github-copilot-in-your-organization/managing-access-to-github-copilot-in-your-organization/managing-github-copilot-access-to-your-organizations-network#configuring-copilot-subscription-based-network-routing-for-your-enterprise-or-organization) for more details. +- Requires Bun and a valid GitHub Copilot subscription +- Authentication and runtime behavior still follow the existing project implementation +- Use responsibly and avoid abusive automated traffic patterns diff --git a/bun.lock b/bun.lock index 20e895e7f..9ece87578 100644 --- a/bun.lock +++ b/bun.lock @@ -1,5 +1,6 @@ { "lockfileVersion": 1, + "configVersion": 0, "workspaces": { "": { "name": "copilot-api", diff --git a/src/routes/responses/route.ts b/src/routes/responses/route.ts new file mode 100644 index 000000000..05481026c --- /dev/null +++ b/src/routes/responses/route.ts @@ -0,0 +1,23 @@ +import { Hono } from "hono" + +import { forwardError } from "~/lib/error" +import { + createResponses, + type ResponsesPayload, +} from "~/services/copilot/create-responses" + +export const responsesRoutes = new Hono() + +responsesRoutes.post("/", async (c) => { + try { + const payload = await c.req.json() + const response = await createResponses(payload) + + return new Response(response.body, { + status: response.status, + headers: response.headers, + }) + } catch (error) { + return await forwardError(c, error) + } +}) diff --git a/src/server.ts b/src/server.ts index 462a278f3..4c968195e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -6,6 +6,7 @@ import { completionRoutes } from "./routes/chat-completions/route" import { embeddingRoutes } from "./routes/embeddings/route" import { messageRoutes } from "./routes/messages/route" import { modelRoutes } from "./routes/models/route" +import { responsesRoutes } from "./routes/responses/route" import { tokenRoute } from "./routes/token/route" import { usageRoute } from "./routes/usage/route" @@ -19,6 +20,7 @@ server.get("/", (c) => c.text("Server running")) server.route("/chat/completions", completionRoutes) server.route("/models", modelRoutes) server.route("/embeddings", embeddingRoutes) +server.route("/responses", responsesRoutes) server.route("/usage", usageRoute) server.route("/token", tokenRoute) @@ -26,6 +28,7 @@ server.route("/token", tokenRoute) server.route("/v1/chat/completions", completionRoutes) server.route("/v1/models", modelRoutes) server.route("/v1/embeddings", embeddingRoutes) +server.route("/v1/responses", responsesRoutes) // Anthropic compatible endpoints server.route("/v1/messages", messageRoutes) diff --git a/src/services/copilot/create-responses.ts b/src/services/copilot/create-responses.ts new file mode 100644 index 000000000..b94459bfc --- /dev/null +++ b/src/services/copilot/create-responses.ts @@ -0,0 +1,25 @@ +/// + +import { copilotHeaders, copilotBaseUrl } from "~/lib/api-config" +import { HTTPError } from "~/lib/error" +import { state } from "~/lib/state" + +export interface ResponsesPayload { + stream?: boolean | null + input?: unknown + [key: string]: unknown +} + +export const createResponses = async (payload: ResponsesPayload) => { + if (!state.copilotToken) throw new Error("Copilot token not found") + + const response = await fetch(`${copilotBaseUrl(state)}/responses`, { + method: "POST", + headers: copilotHeaders(state), + body: JSON.stringify(payload), + }) + + if (!response.ok) throw new HTTPError("Failed to create response", response) + + return response +} diff --git a/tests/create-responses.test.ts b/tests/create-responses.test.ts new file mode 100644 index 000000000..4b717e348 --- /dev/null +++ b/tests/create-responses.test.ts @@ -0,0 +1,69 @@ +import { afterEach, beforeEach, expect, mock, test } from "bun:test" + +import { state } from "../src/lib/state" +import { + createResponses, + type ResponsesPayload, +} from "../src/services/copilot/create-responses" + +const originalFetch = globalThis.fetch + +beforeEach(() => { + state.copilotToken = "test-token" + state.vsCodeVersion = "1.0.0" + state.accountType = "individual" +}) + +afterEach(() => { + globalThis.fetch = originalFetch +}) + +test("posts payload to copilot responses endpoint", async () => { + const responseBody = JSON.stringify({ id: "resp_123", object: "response" }) + const fetchMock = mock((_url: string, _opts?: RequestInit) => + Promise.resolve( + new Response(responseBody, { + status: 200, + headers: { "content-type": "application/json" }, + }), + ), + ) + globalThis.fetch = fetchMock as unknown as typeof fetch + + const payload: ResponsesPayload = { + model: "gpt-4.1", + input: "hello", + } + + const response = await createResponses(payload) + + expect(fetchMock).toHaveBeenCalledTimes(1) + expect(fetchMock.mock.calls[0]?.[0]).toBe( + "https://api.githubcopilot.com/responses", + ) + expect(fetchMock.mock.calls[0]?.[1]).toMatchObject({ + method: "POST", + }) + expect(await response.json()).toEqual({ id: "resp_123", object: "response" }) +}) + +test("preserves streaming response metadata", async () => { + const fetchMock = mock((_url: string, _opts?: RequestInit) => + Promise.resolve( + new Response("data: hello\n\n", { + status: 200, + headers: { "content-type": "text/event-stream" }, + }), + ), + ) + globalThis.fetch = fetchMock as unknown as typeof fetch + + const response = await createResponses({ + model: "gpt-4.1", + input: "hello", + stream: true, + }) + + expect(response.headers.get("content-type")).toBe("text/event-stream") + expect(await response.text()).toBe("data: hello\n\n") +})