ai-agent-router
v0.1.12
Published
A unified API gateway for managing multiple AI model providers (Anthropic, OpenAI, Gemini, etc.)
Maintainers
Readme
AI Agent Router
一个统一的 API 网关,用于管理多个 AI 模型供应商(Anthropic、OpenAI、Gemini 等),为 Claude、Zcode、Alma 等客户端软件提供统一的接口。
功能特性
- ✅ 支持多种 AI 模型协议(OpenAI、Anthropic、Gemini)
- ✅ 独立 Gateway 服务架构,可与 Web UI 分离运行
- ✅ Web 管理界面,可视化配置和管理
- ✅ 模型供应商管理(添加、编辑、删除)
- ✅ 模型管理(手动添加、自动拉取模型列表)
- ✅ 请求日志记录和查看(支持流式响应的美化展示)
- ✅ 双端点支持(
/v1/*和/api/gateway/*) - ✅ API Key 认证保护 Gateway 服务
- ✅ CLI 工具,易于安装和使用
- ✅ SQLite 数据库,轻量级存储
安装
npm install -g ai-agent-router使用方法
启动完整服务(Web UI + Gateway)
aar start默认在 http://localhost:9527 启动 Web 管理界面,Gateway 服务运行在 http://localhost:1357。
仅启动 Gateway 服务
aar gateway仅启动独立的 API 网关服务,默认在 http://localhost:1357。
配置选项
# 指定 Web UI 端口
aar start --port 8080
# 指定主机名
aar start --hostname 0.0.0.0
# 指定 Gateway 端口
aar gateway --port 3000配置管理
# 获取配置
aar config --get port
# 设置配置
aar config --set port 8080
# 设置 Gateway API Key(用于外部客户端认证)
aar config --set api_key "your-secret-key"Web 管理界面
启动后访问 http://localhost:9527,你可以:
- 配置网关:设置端口、API Key 等
- 管理供应商:添加、编辑、删除模型供应商
- 管理模型:手动添加模型或一键拉取模型列表
- 查看日志:查看所有 API 请求日志,包括流式响应的美化显示
- 服务管理:独立启动/停止 Gateway 服务,不影响 Web UI
添加供应商
- 访问 Web 界面,进入"供应商"页面
- 点击"添加供应商"
- 填写信息:
- 名称:供应商名称(如 "OpenAI")
- 协议:选择协议类型(OpenAI、Anthropic、Gemini)
- Base URL:API 基础 URL(如
https://api.openai.com/v1) - API Key:供应商的 API Key
添加模型
手动添加
- 进入"模型"页面
- 点击"手动添加"
- 选择供应商,填写模型名称和模型 ID
自动拉取
- 进入"模型"页面
- 在"一键拉取模型列表"区域,点击对应供应商的"拉取模型"按钮
- 系统会自动从供应商 API 拉取可用模型列表
使用网关
配置好供应商和模型后,客户端可以通过以下方式使用:
Gateway 端点(推荐 - 独立服务)
Gateway 服务运行在 http://localhost:1357,支持两种兼容的端点:
OpenAI 兼容接口
POST http://localhost:1357/v1/chat/completions
Content-Type: application/json
x-api-key: your-gateway-api-key
{
"model": "gpt-4",
"messages": [...],
"stream": true // 支持流式响应
}Gateway 专用接口
POST http://localhost:1357/api/gateway/v1/chat/completions
Content-Type: application/json
x-api-key: your-gateway-api-key
{
"model": "gpt-4",
"messages": [...],
"stream": true // 支持流式响应
}模型列表
GET http://localhost:1357/v1/models
GET http://localhost:1357/api/gateway/modelsAnthropic 兼容接口
POST http://localhost:1357/api/gateway/v1/messages
Content-Type: application/json
x-api-key: your-gateway-api-key
{
"model": "claude-3-5-sonnet-20241022",
"messages": [...]
}认证说明
- 如果配置了 Gateway API Key,客户端需要在请求头中提供:
x-api-key: your-gateway-api-key - 支持 Bearer token 格式:
Authorization: Bearer your-gateway-api-key
开发
本地开发
# 安装依赖
npm install
# 开发模式
npm run dev
# 构建
npm run build
# 启动生产模式
npm start项目结构
aar/
├── src/
│ ├── server/ # 网关服务器
│ │ ├── gateway.ts # 核心网关逻辑
│ │ ├── gateway-server.ts # 独立 Gateway 服务器
│ │ ├── service-manager.ts # 服务管理器
│ │ ├── providers/ # 协议适配器
│ │ └── logger.ts # 请求日志记录
│ ├── db/ # 数据库层
│ │ ├── database.ts # 数据库连接
│ │ ├── schema.ts # SQLite 表结构
│ │ └── queries.ts # 数据库查询
│ ├── app/ # Next.js 应用
│ │ ├── api/ # API 路由
│ │ │ ├── gateway/ # Gateway 代理端点
│ │ │ └── service/ # 服务管理端点
│ │ └── (pages)/ # 前端页面
│ └── cli/ # CLI 入口
│ ├── index.ts # 主 CLI 程序
│ └── gateway-server.ts # Gateway CLI 入口
├── package.json
└── README.md技术栈
- Next.js 14 - 全栈框架
- TypeScript - 类型安全
- Tailwind CSS - 样式框架
- SQLite3 - 数据库
- Commander.js - CLI 框架
许可证
MIT
贡献
欢迎提交 Issue 和 Pull Request!
