Skip to content

StarryKira/copilot2api-go

Repository files navigation

Copilot API Go

English | 中文


English

A reverse-engineered proxy for the GitHub Copilot API, rewritten in Go. Exposes Copilot as OpenAI and Anthropic compatible API services with a multi-account web console for management and load balancing.

Warning: This is a reverse-engineered proxy. It is not supported by GitHub and may break unexpectedly. Use at your own risk.

GitHub Security Notice: Excessive automated or scripted use of Copilot may trigger GitHub's abuse-detection systems. Please review GitHub Acceptable Use Policies and GitHub Copilot Terms.

Features

  • Multi-Account Management: Web console to add, remove, start, and stop multiple GitHub Copilot accounts
  • Pool Mode Load Balancing: Distribute requests across accounts using Round-Robin or Priority strategies
  • OpenAI Compatible API: /v1/chat/completions, /v1/models, /v1/embeddings
  • Anthropic Compatible API: /v1/messages, /v1/messages/count_tokens — automatic protocol translation
  • Model ID Mapping: Bidirectional mapping between Copilot internal model IDs and standard display IDs (e.g. claude-sonnet-4-20250514)
  • Streaming SSE: Full support for streaming responses in both OpenAI and Anthropic formats
  • GitHub OAuth Device Flow: Authenticate accounts directly from the web console
  • Admin Authentication: Password-protected console with session management
  • Bilingual Web UI: English and Chinese interface with auto-detection
  • Docker Ready: Multi-stage Dockerfile for minimal production images

Quick Start

From Source

# Build
go build -o copilot-go .

# Run (from project root so web UI is served)
./copilot-go

Docker

# Build image
docker build -t copilot-go .

# Run with persistent data
docker run -d \
  -p 3000:3000 \
  -p 4141:4141 \
  -v copilot-data:/root/.local/share/copilot-api \
  copilot-go

Docker Compose

services:
  copilot-go:
    build: .
    ports:
      - "3000:3000"
      - "4141:4141"
    volumes:
      - copilot-data:/root/.local/share/copilot-api
    restart: unless-stopped

volumes:
  copilot-data:

Command Line Options

Option Default Description
--web-port 3000 Web console port
--proxy-port 4141 Proxy API port
--verbose false Enable verbose logging
--auto-start true Auto-start enabled accounts on launch

Usage

  1. Open http://localhost:3000 — create an admin account on first visit
  2. Add a GitHub Copilot account via OAuth device flow
  3. Start the account instance
  4. Use the account's API Key (or Pool Key) to call the proxy

API Endpoints

OpenAI Compatible

Endpoint Method Description
/v1/chat/completions POST Chat completions (streaming supported)
/v1/models GET List available models
/v1/embeddings POST Create embeddings
/chat/completions POST Alias without /v1 prefix
/models GET Alias without /v1 prefix
/embeddings POST Alias without /v1 prefix

Anthropic Compatible

Endpoint Method Description
/v1/messages POST Messages API (streaming supported)
/v1/messages/count_tokens POST Token counting (estimation)

Authentication

All proxy endpoints require a Bearer token:

# Using Authorization header (OpenAI style)
curl -H "Authorization: Bearer sk-your-api-key" ...

# Using x-api-key header (Anthropic style)
curl -H "x-api-key: sk-your-api-key" ...

Examples

OpenAI Chat Completions

curl http://localhost:4141/v1/chat/completions \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "Hello!"}],
    "stream": true
  }'

Anthropic Messages

curl http://localhost:4141/v1/messages \
  -H "x-api-key: sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

Claude Code Integration

ANTHROPIC_BASE_URL=http://localhost:4141 \
ANTHROPIC_API_KEY=sk-your-api-key \
claude

Or create .claude/settings.json in your project:

{
  "env": {
    "ANTHROPIC_BASE_URL": "http://localhost:4141",
    "ANTHROPIC_AUTH_TOKEN": "sk-your-api-key",
    "ANTHROPIC_MODEL": "claude-sonnet-4",
    "ANTHROPIC_SMALL_FAST_MODEL": "gpt-4.1-mini"
  }
}

Web Console API

Public Endpoints

Endpoint Method Description
/api/config GET Server config (proxy port, setup status)
/api/auth/setup POST Initial admin setup
/api/auth/login POST Admin login

Protected Endpoints (require admin session token)

Endpoint Method Description
/api/auth/check GET Validate session
/api/accounts GET List all accounts with status
/api/accounts/usage GET Batch usage query
/api/accounts/:id GET Get single account
/api/accounts POST Add account
/api/accounts/:id PUT Update account
/api/accounts/:id DELETE Delete account
/api/accounts/:id/regenerate-key POST Regenerate API key
/api/accounts/:id/start POST Start instance
/api/accounts/:id/stop POST Stop instance
/api/accounts/:id/usage GET Get account usage
/api/auth/device-code POST Start GitHub OAuth flow
/api/auth/poll/:sessionId GET Poll OAuth status
/api/auth/complete POST Complete OAuth and create account
/api/pool GET Get pool config
/api/pool PUT Update pool config
/api/pool/regenerate-key POST Regenerate pool API key
/api/model-map GET Get model ID mappings
/api/model-map PUT Batch update mappings
/api/model-map POST Add single mapping
/api/model-map/:copilotId DELETE Delete mapping

Model ID Mapping

Copilot returns non-standard model IDs. The mapping feature lets you configure bidirectional translations:

  • /v1/models returns mapped display IDs
  • Incoming requests translate display IDs back to Copilot internal IDs
  • Mappings are persisted to ~/.local/share/copilot-api/model_map.json
  • Configurable via the Web Console "Model ID Mapping" panel

Project Structure

copilot-go/
├── main.go                      # Entry point, starts web console + proxy
├── config/config.go             # Constants, State, header builders
├── store/                       # JSON file persistence
│   ├── paths.go                 # Data directory management
│   ├── account.go               # Account CRUD
│   ├── admin.go                 # Admin auth + sessions
│   └── model_map.go             # Model ID mapping
├── auth/device_flow.go          # GitHub OAuth device flow
├── copilot/vscode_version.go    # VSCode version fetcher
├── anthropic/                   # Anthropic ↔ OpenAI protocol translation
│   ├── types.go                 # All type definitions
│   ├── translate_request.go     # Anthropic → OpenAI request
│   ├── translate_response.go    # OpenAI → Anthropic response
│   ├── stream_translation.go    # Streaming SSE event translation
│   └── utils.go                 # Stop reason mapping
├── instance/                    # Instance lifecycle
│   ├── manager.go               # Start/stop, token refresh
│   ├── handler.go               # Proxy request handlers
│   └── load_balancer.go         # Round-robin / priority selection
├── handler/                     # HTTP routing
│   ├── console_api.go           # Web Console API + static files
│   └── proxy.go                 # Proxy routes + auth middleware
└── web/                         # React frontend (Vite + TypeScript)

Data Storage

All data is stored in ~/.local/share/copilot-api/:

File Content
accounts.json Account list
pool-config.json Pool mode settings
admin.json Admin password hash
model_map.json Model ID mappings

Credits

Based on ericc-ch/copilot-api (TypeScript/Bun), rewritten in Go with multi-account console mode.

License

MIT


中文

GitHub Copilot API 反向代理服务(Go 重写版),支持多账号 Web 管理、负载均衡,将 Copilot 转为 OpenAI/Anthropic 兼容接口。

警告:这是一个反向工程代理,未获得 GitHub 官方支持,可能随时失效。使用风险自负。

GitHub 安全提示:过度的自动化或脚本化使用 Copilot 可能触发 GitHub 的滥用检测系统。请查阅 GitHub 可接受使用政策GitHub Copilot 条款

功能特性

  • 多账号管理:Web 控制台添加、删除、启停多个 GitHub Copilot 账号
  • Pool 模式负载均衡:轮询(Round-Robin)或优先级(Priority)策略分发请求
  • OpenAI 兼容接口/v1/chat/completions/v1/models/v1/embeddings
  • Anthropic 兼容接口/v1/messages/v1/messages/count_tokens — 自动协议转换
  • 模型 ID 映射:Copilot 内部 ID 与标准 ID 双向映射(如 claude-sonnet-4-20250514
  • 流式 SSE:完整支持 OpenAI 和 Anthropic 格式的流式响应
  • GitHub OAuth 设备流:在 Web 控制台直接完成账号认证
  • 管理员认证:密码保护的控制台,支持会话管理
  • 中英文界面:自动检测浏览器语言,支持手动切换
  • Docker 支持:多阶段构建,生产镜像体积小

快速开始

源码编译

# 编译
go build -o copilot-go .

# 运行(在项目根目录,以便加载 Web UI)
./copilot-go

Docker

# 构建镜像
docker build -t copilot-go .

# 运行(持久化数据)
docker run -d \
  -p 3000:3000 \
  -p 4141:4141 \
  -v copilot-data:/root/.local/share/copilot-api \
  copilot-go

Docker Compose

services:
  copilot-go:
    build: .
    ports:
      - "3000:3000"
      - "4141:4141"
    volumes:
      - copilot-data:/root/.local/share/copilot-api
    restart: unless-stopped

volumes:
  copilot-data:

命令行参数

参数 默认值 说明
--web-port 3000 Web 控制台端口
--proxy-port 4141 代理 API 端口
--verbose false 详细日志
--auto-start true 启动时自动启动已启用的账号

使用方法

  1. 访问 http://localhost:3000,首次使用创建管理员账号
  2. 通过 GitHub OAuth 设备流添加 Copilot 账号
  3. 启动账号实例
  4. 使用账号 API Key 或 Pool Key 调用代理接口

API 端点

OpenAI 兼容

端点 方法 说明
/v1/chat/completions POST 对话补全(支持流式)
/v1/models GET 模型列表
/v1/embeddings POST 文本嵌入

Anthropic 兼容

端点 方法 说明
/v1/messages POST 消息 API(支持流式)
/v1/messages/count_tokens POST Token 计数(估算)

认证方式

所有代理端点需要 Bearer token:

# OpenAI 风格
curl -H "Authorization: Bearer sk-your-api-key" ...

# Anthropic 风格
curl -H "x-api-key: sk-your-api-key" ...

使用示例

OpenAI 对话补全

curl http://localhost:4141/v1/chat/completions \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "你好!"}],
    "stream": true
  }'

Anthropic 消息

curl http://localhost:4141/v1/messages \
  -H "x-api-key: sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "你好!"}]
  }'

Claude Code 集成

ANTHROPIC_BASE_URL=http://localhost:4141 \
ANTHROPIC_API_KEY=sk-your-api-key \
claude

或在项目中创建 .claude/settings.json

{
  "env": {
    "ANTHROPIC_BASE_URL": "http://localhost:4141",
    "ANTHROPIC_AUTH_TOKEN": "sk-your-api-key",
    "ANTHROPIC_MODEL": "claude-sonnet-4",
    "ANTHROPIC_SMALL_FAST_MODEL": "gpt-4.1-mini"
  }
}

模型 ID 映射

Copilot 返回的模型 ID 不规范,映射功能支持双向转换:

  • /v1/models 返回映射后的标准 ID
  • 请求时自动将标准 ID 转回 Copilot 内部 ID
  • 映射持久化到 ~/.local/share/copilot-api/model_map.json
  • 通过 Web 控制台「模型 ID 映射」面板配置

数据存储

所有数据存储在 ~/.local/share/copilot-api/

文件 内容
accounts.json 账号列表
pool-config.json Pool 模式配置
admin.json 管理员密码哈希
model_map.json 模型 ID 映射表

致谢

基于 ericc-ch/copilot-api(TypeScript/Bun)重写为 Go,新增多账号控制台模式。

许可证

MIT

About

Turn GitHub Copilot into OpenAI/Anthropic API compatible server. Usable with Claude Code Or Codex Or Opencode!

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages