aiclient-api
English | 中文
A unified AI gateway daemon that authenticates against GitHub Copilot and Kiro (AWS CodeWhisperer), exposing standard OpenAI-compatible and Anthropic-compatible API endpoints locally.
Use your existing Copilot or Kiro subscription with any tool that speaks the OpenAI or Anthropic API — Claude Code, Cursor, Continue, or your own scripts.
Features
- Dual provider support — GitHub Copilot (Individual / Business / Enterprise) and Kiro (Builder ID / Google / GitHub / IAM Identity Center)
- OpenAI-compatible endpoint —
POST /v1/chat/completions,GET /v1/models - Anthropic-compatible endpoint —
POST /v1/messages - Usage tracking —
GET /v1/usageto monitor token consumption by provider and model - Automatic format conversion — OpenAI <-> Anthropic, transparently
- Passthrough mode — Skip conversion when the provider natively supports the target format
- SSE streaming — Full streaming support with format-aware chunk conversion
- Smart model routing — By model prefix (
copilot/gpt-4),X-Providerheader, or config default - Bearer token auth — Optional API key protection on all
/v1/*routes - Per-IP rate limiting — Configurable request interval
- Daemon mode — Runs in background with PID management, or
--foregroundfor debugging - Unix socket control — CLI commands talk to the running daemon over a Unix socket
- Hot config reload —
SIGHUPorconfig reloadto apply changes without restart - XDG-compliant paths — Config, tokens, logs, PID, socket all follow XDG conventions
Quick Start
1. Build
2. Authenticate
# GitHub Copilot (device flow — opens browser)
# Kiro / AWS Builder ID (interactive menu)
# Kiro / IAM Identity Center (organization identity)
3. Start the daemon
# Background mode (default)
# Foreground mode (for debugging)
# Custom port and API key
4. Use it
# OpenAI format
# Anthropic format
# Kiro provider
# List available models
# Check usage statistics
Usage Tracking
Monitor your token consumption in real-time:
# Get usage statistics
|
# Example response
{
}
# Reset statistics
Features:
- ✅ Real-time tracking per provider and model
- ✅ Tracks input/output tokens separately
- ✅ Aggregated total statistics
- ⚠️ In-memory storage (resets on restart)
See USAGE_TRACKING.md for full documentation.
Configuration
Config file: ~/.config/aiclient-api/config.toml
= "openai" # "openai" or "anthropic"
= "copilot" # default provider when no prefix
= "" # empty = no auth required
[]
= "127.0.0.1"
= 9090
= 0 # 0 = disabled
[]
= "copilot"
= true
= "individual" # "individual", "business", "enterprise"
[]
= "kiro"
= true
= "us-east-1"
[]
= "info"
CLI Commands
| Command | Description |
|---|---|
aiclient-api start |
Start the daemon |
aiclient-api stop |
Stop the daemon |
aiclient-api restart |
Restart the daemon |
aiclient-api status |
Show daemon status and provider health |
aiclient-api auth copilot |
Authenticate with GitHub Copilot |
aiclient-api auth kiro |
Authenticate with Kiro (interactive) |
aiclient-api auth kiro --start-url <url> --region <region> |
Kiro IAM Identity Center auth |
aiclient-api auth list |
List authenticated providers |
aiclient-api auth revoke <provider> |
Revoke a provider's tokens |
aiclient-api models |
List available models from all providers |
aiclient-api config init |
Interactive configuration wizard |
aiclient-api config show |
Show current config |
aiclient-api config reload |
Reload config from disk |
aiclient-api provider enable <name> |
Enable a provider |
aiclient-api provider disable <name> |
Disable a provider |
aiclient-api logs |
Tail daemon logs |
aiclient-api logs --level error |
Filter by log level |
Model Routing
Models are routed by three mechanisms, in priority order:
- Model prefix —
copilot/gpt-4oroutes to thecopilotprovider with modelgpt-4o - X-Provider header —
X-Provider: kiroroutes to thekiroprovider - Config default — Falls back to
default_providerin config
API Endpoints
| Endpoint | Method | Format | Description |
|---|---|---|---|
/healthz |
GET | — | Health check (no auth) |
/v1/chat/completions |
POST | OpenAI | Chat completions |
/v1/models |
GET | OpenAI | List models |
/v1/messages |
POST | Anthropic | Messages API |
/v1/usage |
GET | JSON | Get usage statistics |
/v1/usage |
DELETE | JSON | Reset usage statistics |
Architecture
src/
auth/ # Token management
copilot.rs # GitHub device flow OAuth
kiro.rs # Builder ID + social auth (PKCE)
token_store.rs
cli/ # CLI commands
config/ # TOML config loading
convert/ # OpenAI <-> Anthropic format conversion
stream.rs # SSE chunk conversion
daemon/ # Process management + Unix socket control
providers/ # Provider implementations
copilot/ # GitHub Copilot (VSCode header spoofing)
kiro/ # Kiro / CodeWhisperer API
router.rs # Model-based provider routing
routes/ # HTTP route handlers
usage.rs # Usage tracking endpoints
server/ # Axum server, middleware (auth, rate-limit, CORS)
usage/ # Token usage tracking
tracker.rs # UsageTracker implementation
util/ # XDG paths, error types, streaming helpers
File Locations
| File | Path |
|---|---|
| Config | ~/.config/aiclient-api/config.toml |
| Copilot token | ~/.config/aiclient-api/copilot/token.json |
| Kiro token | ~/.config/aiclient-api/kiro/token.json |
| PID file | ~/.local/state/aiclient-api/daemon.pid |
| Unix socket | ~/.local/state/aiclient-api/daemon.sock |
| Log file | ~/.local/state/aiclient-api/daemon.log |
License
MIT
aiclient-api
English | 中文
统一 AI 网关守护进程,通过认证接入 GitHub Copilot 和 Kiro(AWS CodeWhisperer),在本地暴露标准的 OpenAI 兼容和 Anthropic 兼容 API 端点。
用你已有的 Copilot 或 Kiro 订阅,配合任何支持 OpenAI 或 Anthropic API 的工具使用 — Claude Code、Cursor、Continue,或你自己的脚本。
功能特性
- 双 Provider 支持 — GitHub Copilot(Individual / Business / Enterprise)和 Kiro(Builder ID / Google / GitHub / IAM Identity Center)
- OpenAI 兼容端点 —
POST /v1/chat/completions、GET /v1/models - Anthropic 兼容端点 —
POST /v1/messages - 使用量跟踪 —
GET /v1/usage实时监控按 Provider 和模型的 Token 消耗 - 自动格式转换 — OpenAI <-> Anthropic 双向透明转换
- 直通模式 — Provider 原生支持目标格式时跳过转换,直接透传
- SSE 流式 — 完整的流式支持,带格式感知的 chunk 转换
- 智能模型路由 — 通过模型前缀(
copilot/gpt-4)、X-Provider请求头或配置默认值 - Bearer Token 认证 — 可选的 API Key 保护,作用于所有
/v1/*路由 - IP 限流 — 可配置的请求频率限制
- 守护进程模式 — 后台运行 + PID 管理,或
--foreground前台调试 - Unix Socket 控制 — CLI 命令通过 Unix Socket 与运行中的守护进程通信
- 热重载配置 —
SIGHUP信号或config reload命令即时生效 - XDG 路径规范 — 配置、Token、日志、PID、Socket 均遵循 XDG 目录规范
快速开始
1. 构建
2. 认证
# GitHub Copilot(设备码流程 — 自动打开浏览器)
# Kiro / AWS Builder ID(交互式菜单)
# Kiro / IAM Identity Center(组织身份认证)
3. 启动守护进程
# 后台模式(默认)
# 前台模式(调试用)
# 自定义端口和 API Key
4. 使用
# OpenAI 格式
# Anthropic 格式
# 使用 Kiro Provider
# 列出可用模型
# 查询使用统计
使用量跟踪
实时监控你的 Token 消耗:
# 获取使用统计
|
# 响应示例
{
}
# 重置统计数据
功能特点:
- ✅ 按 Provider 和模型实时跟踪
- ✅ 分别统计输入/输出 tokens
- ✅ 聚合总计统计
- ⚠️ 内存存储(重启后清零)
详细文档参见 USAGE_TRACKING.md。
配置
配置文件路径:~/.config/aiclient-api/config.toml
= "openai" # "openai" 或 "anthropic"
= "copilot" # 无前缀时的默认 Provider
= "" # 留空 = 不需要认证
[]
= "127.0.0.1"
= 9090
= 0 # 0 = 不限流
[]
= "copilot"
= true
= "individual" # "individual"、"business"、"enterprise"
[]
= "kiro"
= true
= "us-east-1"
[]
= "info"
CLI 命令
| 命令 | 说明 |
|---|---|
aiclient-api start |
启动守护进程 |
aiclient-api stop |
停止守护进程 |
aiclient-api restart |
重启守护进程 |
aiclient-api status |
查看守护进程状态和 Provider 健康度 |
aiclient-api auth copilot |
GitHub Copilot 认证 |
aiclient-api auth kiro |
Kiro 认证(交互式) |
aiclient-api auth kiro --start-url <url> --region <region> |
Kiro IAM Identity Center 认证 |
aiclient-api auth list |
列出已认证的 Provider |
aiclient-api auth revoke <provider> |
撤销指定 Provider 的 Token |
aiclient-api models |
列出所有 Provider 的可用模型 |
aiclient-api config init |
交互式配置向导 |
aiclient-api config show |
显示当前配置 |
aiclient-api config reload |
重新加载配置文件 |
aiclient-api provider enable <name> |
启用 Provider |
aiclient-api provider disable <name> |
禁用 Provider |
aiclient-api logs |
查看守护进程日志 |
aiclient-api logs --level error |
按日志级别过滤 |
模型路由
模型按以下优先级路由:
- 模型前缀 —
copilot/gpt-4o路由到copilotProvider,使用模型gpt-4o - X-Provider 请求头 —
X-Provider: kiro路由到kiroProvider - 配置默认值 — 回退到配置文件中的
default_provider
API 端点
| 端点 | 方法 | 格式 | 说明 |
|---|---|---|---|
/healthz |
GET | — | 健康检查(无需认证) |
/v1/chat/completions |
POST | OpenAI | Chat 补全 |
/v1/models |
GET | OpenAI | 模型列表 |
/v1/messages |
POST | Anthropic | Messages API |
/v1/usage |
GET | JSON | 获取使用统计 |
/v1/usage |
DELETE | JSON | 重置使用统计 |
项目结构
src/
auth/ # Token 管理
copilot.rs # GitHub 设备码 OAuth 流程
kiro.rs # Builder ID + 社交认证 (PKCE)
token_store.rs
cli/ # CLI 命令
config/ # TOML 配置加载
convert/ # OpenAI <-> Anthropic 格式转换
stream.rs # SSE chunk 转换
daemon/ # 进程管理 + Unix Socket 控制服务
providers/ # Provider 实现
copilot/ # GitHub Copilot(VSCode 请求头伪装)
kiro/ # Kiro / CodeWhisperer API
router.rs # 基于模型名的 Provider 路由
routes/ # HTTP 路由处理器
usage.rs # 使用量跟踪端点
server/ # Axum 服务器、中间件(认证、限流、CORS)
usage/ # Token 使用量跟踪
tracker.rs # UsageTracker 实现
util/ # XDG 路径、错误类型、流式辅助工具
文件位置
| 文件 | 路径 |
|---|---|
| 配置文件 | ~/.config/aiclient-api/config.toml |
| Copilot Token | ~/.config/aiclient-api/copilot/token.json |
| Kiro Token | ~/.config/aiclient-api/kiro/token.json |
| PID 文件 | ~/.local/state/aiclient-api/daemon.pid |
| Unix Socket | ~/.local/state/aiclient-api/daemon.sock |
| 日志文件 | ~/.local/state/aiclient-api/daemon.log |
License
MIT