{"id":50750734,"url":"https://github.com/dcai/github-copilot-proxy","last_synced_at":"2026-06-11T01:02:40.825Z","repository":{"id":289627509,"uuid":"971849888","full_name":"dcai/github-copilot-proxy","owner":"dcai","description":null,"archived":false,"fork":false,"pushed_at":"2026-06-02T03:06:38.000Z","size":288,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-06-02T05:06:53.289Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://deepwiki.com/dcai/github-copilot-proxy","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dcai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":"AGENTS.md","dco":null,"cla":null}},"created_at":"2025-04-24T06:40:42.000Z","updated_at":"2026-06-02T03:06:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"f0587a7d-f834-46c2-99ef-34d909e642e9","html_url":"https://github.com/dcai/github-copilot-proxy","commit_stats":null,"previous_names":["dcai/github-copilot-proxy"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dcai/github-copilot-proxy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcai%2Fgithub-copilot-proxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcai%2Fgithub-copilot-proxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcai%2Fgithub-copilot-proxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcai%2Fgithub-copilot-proxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dcai","download_url":"https://codeload.github.com/dcai/github-copilot-proxy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcai%2Fgithub-copilot-proxy/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34177444,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-10T02:00:07.152Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-06-11T01:02:40.098Z","updated_at":"2026-06-11T01:02:40.806Z","avatar_url":"https://github.com/dcai.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Project Context: GitHub Copilot Proxy\n\n[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/dcai/github-copilot-proxy)\n\nThis project is a proxy server for GitHub Copilot, designed to handle requests and responses between a client and the GitHub Copilot API. It is built using Bun and Hono, a fast JavaScript runtime and web framework, and utilizes the `pino` logging library for logging purposes.\n\nThis is not intended to be hosted on public internet, it's strictly being used locally to simulate an openai like api interface as a local proxy.\n\n## Key Components\n\n### 1. `main.ts`\n\n- **Server Setup**: Uses Bun's `serve` method to create a server that listens on a specified port and host.\n- **Request Handling**:\n  - Handles POST requests to `/v1/chat/completions`: Proxies chat completion requests to the GitHub Copilot API, handling both standard and streaming responses. It also detects vision requests (requests with images).\n  - Handles GET requests to `/v1/models`: Fetches and returns a list of available models from the GitHub Copilot API.\n\n### 2. `helper.ts`\n\n- **Logger**: Utilizes `pino` for logging with a default log level of \"debug\". Includes utility functions for adding breadcrumbs to Sentry.\n- **TokenManager Class**: Manages the retrieval, caching, and refreshing of tokens from the GitHub Copilot API. It ensures that a valid token is available for authenticating requests.\n- **getHeaders Function**: Constructs headers for API requests, including authorization using a token managed by `TokenManager`. It supports different headers based on whether the request is a vision request.\n- **Utility Functions**: Includes helper functions such as `msToTime` for converting milliseconds to a human-readable time format, `shortenText` for shortening long text, `findUserMessageContent` for extracting user message content from a payload, and `hasImageInRequestBody` for detecting image requests.\n\n### 3. `setup.js`\n\n- **Device Code Flow**: Implements the device code flow for obtaining an OAuth token. It includes functions for getting the device code, polling for the access token, and saving the token to a `.env` file.\n\n## Environment Variables\n\n- `GHC_PORT`: Port on which the server listens (default: 7890).\n- `GHC_HOST`: Hostname for the server (default: \"0.0.0.0\").\n- `COPILOT_OAUTH_TOKEN`: OAuth token for authenticating with the GitHub Copilot API.\n- `SENTRY_DSN`: Sentry Data Source Name for error tracking.\n\n## Usage\n\n- Ensure all environment variables are set.\n- Start the server using Bun to handle requests to the GitHub Copilot API.\n- Use `setup.js` to obtain and configure the `COPILOT_OAUTH_TOKEN` if needed.\n\n## Project Summary and Features\n\nThis project is a local proxy server for GitHub Copilot. Its main purpose is to intercept and manage requests between a client application (e.g., an IDE or a custom tool) and the GitHub Copilot API. This allows for functionalities like custom logging, request/response modification, and local simulation of an OpenAI-like API interface for GitHub Copilot. It is designed for local use and not for public internet deployment.\n\nKey Features:\n\n- **API Proxying**: Forwards requests to the GitHub Copilot API.\n- **Chat Completions**: Handles POST requests to `/v1/chat/completions`, supporting both standard and streaming responses.\n- **Vision Request Detection**: Identifies requests that include images.\n- **Model Listing**: Handles GET requests to `/v1/models` to fetch and return available Copilot models.\n- **Token Management**: Manages GitHub Copilot authentication tokens, including retrieval, caching, and refreshing, via the `TokenManager` class.\n- **Custom Headers**: Constructs appropriate headers for API requests, including authorization.\n- **Logging**: Utilizes `pino` for structured logging.\n- **OAuth Setup**: Includes a script (`setup.js`) for obtaining OAuth tokens using the device code flow.\n- **Environment Configuration**: Loads settings from environment variables (e.g., port, host, OAuth token).\n\n## Project Key Directories and Files\n\nThe project primarily resides in a flat directory structure within the root. Key files include:\n\n- **`main.ts`**: The main entry point for the proxy server. It sets up the Hono server, defines API routes (`/v1/chat/completions`, `/v1/models`), and handles incoming requests by forwarding them to the GitHub Copilot API.\n- **`helper.ts`**: Contains utility functions and classes used across the project. This includes:\n  - `TokenManager`: Manages authentication tokens for the GitHub Copilot API.\n  - `getHeaders`: Constructs HTTP headers for API requests.\n  - Logging setup (`pino`).\n  - Helper functions for text manipulation, time conversion, and request/response parsing (e.g., `findUserMessageContent`, `hasImageInRequestBody`).\n  - Sentry breadcrumb integration.\n- **`setup.js`**: A script to facilitate the OAuth device flow to obtain a `COPILOT_OAUTH_TOKEN` from GitHub, which is then typically stored in a `.env` file.\n- **`package.json`**: Defines project metadata, dependencies (like `hono`, `pino`, `@sentry/bun`), and scripts (e.g., `start` script to run the server with `bun`).\n- **`.env` (not present in chat, but implied)**: Used to store environment variables such as `GHC_PORT`, `GHC_HOST`, `COPILOT_OAUTH_TOKEN`, and `SENTRY_DSN`.\n- **`README.md`**: This file, providing documentation for the project.\n- **`.gitignore`**: Specifies intentionally untracked files that Git should ignore.\n- **`bun.lockb`**: The lockfile for Bun, ensuring reproducible dependencies.\n- **`tsconfig.json` / `jsconfig.json`**: Configuration files for TypeScript and JavaScript development.\n- **`mise.toml`**: Configuration file for `mise` (a dev environment manager).\n\n## Guide to Navigate the Project\n\n- **API Endpoint Definitions and Core Logic**: Look into `main.ts`. This file contains the Hono app setup and the handlers for `/v1/chat/completions` and `/v1/models`.\n- **Helper Functions, Token Management, and Utilities**: Explore `helper.ts`. This is where you'll find the `TokenManager` class, header generation logic, logging configuration, and various utility functions.\n- **Authentication Setup**: If you need to understand how the OAuth token is obtained, refer to `setup.js`.\n- **Dependencies and Scripts**: Check `package.json` for a list of project dependencies and available run scripts.\n- **Environment Configuration**: Environment variables are typically managed in a `.env` file (not version controlled) and are documented in the README files.\n\n## Key Technologies and Components\n\n- **Runtime**: Bun (a fast JavaScript runtime)\n- **Programming Language**: TypeScript\n- **Web Framework**: Hono (a small, simple, and ultrafast web framework for the Edge)\n- **Logging**: Pino (a very low overhead Node.js logger) with `pino-pretty` for development.\n- **Authentication**: OAuth 2.0 Device Flow (for obtaining GitHub Copilot tokens).\n- **Error Tracking**: Sentry (via `@sentry/bun`), if `SENTRY_DSN` is configured.\n- **Package Management**: Bun\n- **Development Environment Management**: `mise` (optional, based on `mise.toml`)\n\n## Core Architecture\n\nThe project implements a proxy server pattern. It acts as an intermediary between a client application (e.g., an IDE plugin or a script using an OpenAI-compatible API) and the GitHub Copilot API.\n\n1.  **Client**: Sends requests (e.g., for chat completions or model listings) to the proxy server, typically formatted like OpenAI API requests.\n2.  **Proxy Server (`main.ts` with Hono)**:\n    - Listens for incoming HTTP requests on a configured port and host.\n    - Parses the incoming request.\n    - Uses `TokenManager` (from `helper.ts`) to obtain a valid GitHub Copilot authentication token. This token is cached and refreshed as needed.\n    - Constructs the appropriate headers for the GitHub Copilot API using `getHeaders` (from `helper.ts`), including the authentication token.\n    - Forwards the request to the actual GitHub Copilot API endpoint (e.g., `https://api.githubcopilot.com`).\n    - Handles the response from the GitHub Copilot API.\n    - If the request is for a streaming response (e.g., chat completions with `stream: true`), it streams the data back to the client chunk by chunk.\n    - If it's a regular response, it sends the complete response back to the client.\n    - Logs request and response information using `pino`.\n    - Optionally sends error reports and breadcrumbs to Sentry.\n3.  **GitHub Copilot API**: The official backend service provided by GitHub that processes the requests and provides AI-powered code suggestions and chat responses.\n\nThe `setup.js` script is a utility to perform the initial OAuth device flow to get the `COPILOT_OAUTH_TOKEN`, which the proxy then uses for authenticating its requests to the GitHub Copilot API.\n\n```mermaid\ngraph TD\n    Client[\"Client Application (e.g., IDE, Script)\"] --\u003e|\"HTTP Request (OpenAI format)\"| ProxyServer{\"GitHub Copilot Proxy Server (Bun + Hono)\"};\n    ProxyServer --\u003e|\"Uses\"| HelperUtils[\"helper.ts: TokenManager, getHeaders, Logging\"];\n    HelperUtils --\u003e|\"Fetches/Refreshes Token\"| GitHubAuthAPI[\"GitHub OAuth API for Tokens\"];\n    ProxyServer --\u003e|\"Proxied Request with Auth\"| CopilotAPI[\"GitHub Copilot API\"];\n    CopilotAPI --\u003e|\"API Response/Stream\"| ProxyServer;\n    ProxyServer --\u003e|\"HTTP Response/Stream\"| Client;\n    SentryDSN{{\"SENTRY_DSN?\"}} -- Optional --\u003e SentryService[\"Sentry.io\"];\n    ProxyServer -.-\u003e|\"Sends Errors/Breadcrumbs if SENTRY_DSN set\"| SentryService;\n    SetupScript[\"setup.js\"] --\u003e|\"Device Flow\"| GitHubAuthAPI;\n    SetupScript --\u003e|\"Saves Token to .env\"| DotEnvFile[\".env File\"];\n    DotEnvFile --\u003e|\"Loads Token\"| HelperUtils;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcai%2Fgithub-copilot-proxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdcai%2Fgithub-copilot-proxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcai%2Fgithub-copilot-proxy/lists"}