Skip to main content

GitHub Copilot API Proxy - A Flask application serving as a proxy server for GitHub Copilot API

Project description

GitHub Copilot API Proxy (ghc-api)

A Python Flask application that serves as a proxy server for GitHub Copilot API, providing OpenAI and Anthropic API compatibility with caching and monitoring capabilities.

Features

  • OpenAI API Compatibility: /v1/chat/completions endpoint
  • Anthropic API Compatibility: /v1/messages endpoint with automatic request/response translation
  • Model Listing: /v1/models endpoint listing available models
  • Model Name Mapping: Translate model names with exact and prefix-based matching
  • Token Management: Automatic GitHub Copilot token refresh
  • Vision Support: Handle image inputs and enable vision capabilities
  • Memory Caching: Cache all requests and responses (up to 1000 entries)
  • Web Dashboard: Real-time statistics and request browser
  • Request Details: View full request/response bodies with JSON formatting
  • Export/Import: Export and import request history as JSON Lines files
  • Optional Request File Logging: Save completed requests to daily JSON Lines files
  • Content Filtering: Remove or add content from system prompts and tool results
  • Code Agent Manager UI: Install Codex/Claude/Copilot CLI and manage config sync from dashboard
  • Config Sync: Sync Claude Code, Codex, and ghc-api config files with OneDrive
  • Safe Backups: Auto backup overwritten config files as *.YYYYMMDD_HHMMSS.bak
  • Machine Token Usage Logs: Periodic token usage JSONL per machine with cross-machine overview in dashboard

Installation

Install the package using pip:

pip install ghc-api

Or install from source:

pip install .

Usage

Start the server with the ghc-api command:

ghc-api

By default, the server will start on http://localhost:8313.

Command Line Options

  • -p PORT or --port PORT: Specify the port to listen on (default: 8313)
  • -a ADDRESS or --address ADDRESS: Specify the address to listen on (default: localhost)
  • -c or --config: Generate a YAML config file in ~/.ghc-api/config.yaml
  • -v or --version: Show version (for example ghc-api 1.0.8)
  • --help: Show help message

Configuration

The application looks for a configuration file at ~/.ghc-api/config.yaml. You can generate this file using:

ghc-api --config

The config file contains:

# Server Settings
address: localhost
port: 8313
debug: false

# GitHub Copilot Account Type
# Options: "individual", "business", "enterprise"
account_type: individual

# Version settings (used to build request headers)
vscode_version: "1.93.0"
api_version: "2025-04-01"
copilot_version: "0.26.7"

# Model Name Mappings
model_mappings:
  # Exact match mappings
  exact:
    opus: claude-opus-4.5
    sonnet: claude-sonnet-4.5
    haiku: claude-haiku-4.5
  # Prefix match mappings
  prefix:
    claude-sonnet-4-: claude-sonnet-4
    claude-opus-4.5-: claude-opus-4.5

# Content Filtering
system_prompt_remove: []    # Strings to remove from system prompts
system_prompt_add: []       # Strings to append to system prompts
tool_result_suffix_remove: [] # Strings to remove from end of tool results

# Optional request persistence
save_request_to_file: false # If true, save completed requests to requests/YYYY-MM-DD.jl

Token Management

The application follows this priority for getting the GitHub token:

  1. GITHUB_TOKEN environment variable
  2. Token file at ~/.ghc-api/github_token.txt
  3. Interactive GitHub Device Flow authentication

Config Sync and OneDrive

ghc-api can manage and sync these files:

  • Claude Code: ~/.claude/settings.json
  • Codex: ~/.codex/config.toml
  • ghc-api: ~/.ghc-api/config.yaml (or %APPDATA%/ghc-api/config.yaml on Windows)

OneDrive detection priority:

  1. ~/OneDrive - *
  2. ~/OneDrive
  3. In WSL: /mnt/c/Users/<username>/OneDrive - * then /mnt/c/Users/<username>/OneDrive

Sync target folder:

  • .ghc-api/configSync under detected OneDrive root

Machine folder:

  • .ghc-api/agents/{hostname}_{os} where os is Win, Linux, or WSL

Hash files:

  • .ghc-api/configSync/config.sha1
  • .ghc-api/agents/{hostname}_{os}/ghc-api/config.sha1

Hashes are recalculated when local config file timestamp is newer than the hash file. On startup, ghc-api checks synced files and prints config differences to stdout (and UI indicator if different).

Token Usage Logging

Every 5 minutes, ghc-api writes token usage delta (if non-zero) to:

  • OneDrive mode: .ghc-api/agents/{hostname}_{os}/token_usage.jl
  • Fallback when OneDrive is unavailable: ~/.ghc-api/token_usage.jl

Also flushes pending usage on shutdown (Ctrl+C/termination/normal exit).

Each JSONL line includes:

  • timestamp (unix seconds)
  • models list with:
    • model
    • request_count
    • input_tokens
    • output_tokens
    • total_tokens

Request File Logging

When save_request_to_file: true, ghc-api appends each completed request to:

  • <ghc-api config dir>/requests/YYYY-MM-DD.jl

The saved .jl line format is the same as dashboard export (/api/requests/export) and can be imported by dashboard import (/api/requests/import).

API Endpoints

OpenAI Compatible

  • POST /v1/chat/completions - Chat completions
  • POST /chat/completions - Chat completions (without v1 prefix)
  • GET /v1/models - List available models
  • GET /models - List available models (without v1 prefix)

Anthropic Compatible

  • POST /v1/messages - Messages API (Anthropic format)

Dashboard & Monitoring

  • GET / - Web dashboard with statistics
  • GET /requests - Request browser page
  • GET /api/runtime-config - Read in-memory runtime config
  • POST /api/runtime-config - Update in-memory runtime config (no file write)
  • GET /api/stats - JSON statistics endpoint
  • GET /api/requests - Paginated list of requests
  • GET /api/requests/search - Full-text search in request/response bodies
  • GET /api/requests/export - Export all requests as JSON Lines file
  • POST /api/requests/import - Import requests from JSON Lines file
  • GET /api/request/<id> - Individual request details
  • GET /api/request/<id>/request-body - Request body only
  • GET /api/request/<id>/response-body - Response body only
  • GET /api/config-manager/status - Config manager status and diff info
  • POST /api/config-manager/install-tools - Install Codex/Claude/Copilot CLI
  • POST /api/config-manager/sync-to-onedrive - Sync local config to OneDrive
  • POST /api/config-manager/sync-from-onedrive - Copy OneDrive config to local machine with backups
  • GET /api/config-manager/token-usage?range=all|day|week|month - Cross-machine token usage overview
  • GET /api/config-manager/config-hashes - Config hash overview for shared OneDrive and each machine (with create time)

Example Usage

With OpenAI Python SDK

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8313/v1",
    api_key="not-needed"  # Token is managed by the proxy
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)

With Anthropic Python SDK

import anthropic

client = anthropic.Anthropic(
    base_url="http://localhost:8313",
    api_key="not-needed"  # Token is managed by the proxy
)

message = client.messages.create(
    model="claude-sonnet-4",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)
print(message.content[0].text)

With cURL

# Chat completions
curl http://localhost:8313/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

# List models
curl http://localhost:8313/v1/models

Dashboard

Access the web dashboard at http://localhost:8313/ to:

  • View overall statistics (total requests, data transfer)
  • See per-model usage statistics
  • See per-endpoint analytics
  • Browse recent requests
  • View detailed request/response bodies
  • Use Code Agent Manager to:
    • Install code-agent CLIs
    • Sync config files to/from OneDrive
    • See config mismatch alerts
    • View token usage overview by machine/model with time-range and machine filters
    • View config hash overview by machine and shared OneDrive hash with create times

Architecture

  • Modular Design: Organized into separate modules for maintainability
    • main.py - Entry point and configuration loading
    • app.py - Flask application factory
    • config.py - Configuration constants and model mappings
    • cache.py - Request caching and statistics
    • translator.py - OpenAI/Anthropic format translation
    • streaming.py - Streaming response handling
    • token_manager.py - GitHub token management
    • routes/ - API endpoint handlers (openai, anthropic, dashboard)
  • Thread-Safe Caching: Uses threading locks for concurrent access
  • Memory-Based Storage: No external database dependencies
  • RESTful API Design: Follows REST conventions

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ghc_api-1.0.9.tar.gz (64.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ghc_api-1.0.9-py3-none-any.whl (67.6 kB view details)

Uploaded Python 3

File details

Details for the file ghc_api-1.0.9.tar.gz.

File metadata

  • Download URL: ghc_api-1.0.9.tar.gz
  • Upload date:
  • Size: 64.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for ghc_api-1.0.9.tar.gz
Algorithm Hash digest
SHA256 d96a28a307433edf3d569c5fa51190ac5ae0687e75ea81aa700751fb7999e74d
MD5 2c90fe85027c65bac09806699e8d28c9
BLAKE2b-256 d0b54c428b38419abb275c965fd6053dfefdfe9c6761d2fea8c99583857f96d6

See more details on using hashes here.

File details

Details for the file ghc_api-1.0.9-py3-none-any.whl.

File metadata

  • Download URL: ghc_api-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 67.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for ghc_api-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 0bd1fb2796a4f3c1255bc5fc1c0d1872c1d83039424da4a4d61d83703ba3d500
MD5 32fa1e231fc48f95265f50d20965d033
BLAKE2b-256 41730156d72f890bbca75b34a0ec00177b583b8b1a829f3a8822b9900ee0b7d4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page