Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
# 账户状态文件、目录或逗号分隔的多个路径
# Account state file, directory, or comma-separated paths
AISTUDIO_AUTH_STATES=auth

# 服务监听地址
# Service listen address
LISTEN_ADDR=127.0.0.1:2048

# 本地 API 认证密钥
# Local API authentication key
PROXY_API_KEY=

# setup 与未指定账户共同使用的 HTTPHTTPS SOCKS5 代理
# HTTP, HTTPS, or SOCKS5 proxy shared by setup and accounts without a dedicated proxy
PROXY=

# 单账户初始化超时
# Custom Camoufox browser path (optional; automatically downloaded to runtime/camoufox/ or uses system cache when unset)
# CAMOUFOX_PATH=

# Whether Camoufox runs in headless mode (default true for silent background; set to false to display browser window for debugging)
HEADLESS=true

# Per-account initialization timeout
INIT_TIMEOUT=2m

# 普通请求最大执行时间
# Maximum execution time for standard requests
REQUEST_TIMEOUT=5m

# 常驻预热账户数
# Number of resident warmed accounts
WARM_WORKER_LIMIT=5

# 高峰期最多同时运行的 Worker 数
# Maximum concurrently running workers during peak load
MAX_ACTIVE_WORKERS=10

# 服务启动时同时预热的账户数
# Concurrently warmed accounts during service startup
WARM_STARTUP_CONCURRENCY=2

# 单账号同时处理的请求数
# Concurrently processed requests per account
PER_ACCOUNT_CONCURRENCY=2

# 账户选择策略:round-robin 或 fill-first
# Account routing strategy:
# - round-robin: Rotates requests across all ready accounts to balance load and spread rate limits. Recommended for multi-account pools.
# - fill-first: Sticky routing, continuously routes requests to the first healthy account until full or cooling down before falling back to the next account.
ROUTING_STRATEGY=round-robin

# WAA 预热是否使用临时对话
# Whether WAA warmup uses temporary chat
TEMPORARY_CHAT=false
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# 构建产物
# Build artifacts
*.exe
*.dll
*.so
*.dylib
/aistudio2api
/dist/

# 本地配置与运行状态
# Local configuration and runtime state
.env
/auth/
/runtime/
*.log

# 前端产物
# Frontend artifacts
/web/node_modules/
/internal/webui/dist/

# Go 工作区
# Go workspace
go.work

# 编辑器与系统文件
# Editor and system files
.vscode/
.idea/
.DS_Store
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,10 @@ cp .env.example .env
| `MAX_ACTIVE_WORKERS` | `10` | 高峰期最多同时运行的 Worker 数 |
| `WARM_STARTUP_CONCURRENCY` | `2` | 同时初始化的预热账户数 |
| `PER_ACCOUNT_CONCURRENCY` | `2` | 单账号同时执行的请求数 |
| `ROUTING_STRATEGY` | `round-robin` | `round-robin` 轮询;`fill-first` 账号粘性优先 |
| `ROUTING_STRATEGY` | `round-robin` | 账户路由策略:`round-robin`(轮询分发,均摊速率限制与负载);`fill-first`(粘性优先,满载或冷却时溢出) |
| `TEMPORARY_CHAT` | `false` | WAA 预热页是否使用临时对话 |

| `HEADLESS` | `true` | Camoufox 是否启用无头模式(`true` 后台静默;`false` 显示浏览器窗口) |
| `CAMOUFOX_PATH` | 空 | 自定义 Camoufox 浏览器可执行文件路径(可选) |
服务启动时会载入 `AISTUDIO_AUTH_STATES` 中的全部账户;`WARM_WORKER_LIMIT` 控制常驻预热规模,`MAX_ACTIVE_WORKERS` 控制峰值 Worker 上限,`WARM_STARTUP_CONCURRENCY` 控制启动预热并发,`PER_ACCOUNT_CONCURRENCY` 控制单账户请求槽位。

### 端口配置
Expand All @@ -391,7 +392,7 @@ cp .env.example .env

支持通过无认证信息的 HTTP、HTTPS 或 SOCKS5 代理访问 AI Studio:

1. 在“服务配置”中设置全局代理
1. 在 `.env` 中设置全局代理 `PROXY`
2. 在“账户”页面编辑单个账户时可以设置账户专用代理
3. 账户代理同时用于登录、WAA 与业务请求

Expand Down
7 changes: 4 additions & 3 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,10 @@ cp .env.example .env
| `MAX_ACTIVE_WORKERS` | `10` | Maximum workers active during peak load |
| `WARM_STARTUP_CONCURRENCY` | `2` | Accounts initialized concurrently during prewarming |
| `PER_ACCOUNT_CONCURRENCY` | `2` | Concurrent requests allowed per account |
| `ROUTING_STRATEGY` | `round-robin` | `round-robin` rotates accounts; `fill-first` reuses the first available account |
| `ROUTING_STRATEGY` | `round-robin` | Routing strategy: `round-robin` (balances load & rate limits across accounts); `fill-first` (sticky routing, spills over when full/cooling down) |
| `TEMPORARY_CHAT` | `false` | Use Temporary Chat for the WAA prewarm page |

| `HEADLESS` | `true` | Run Camoufox in headless mode (`true` for silent background; `false` to display browser window) |
| `CAMOUFOX_PATH` | empty | Custom path to Camoufox executable (optional) |
The service loads every account from `AISTUDIO_AUTH_STATES`. `WARM_WORKER_LIMIT` sets the resident warm pool, `MAX_ACTIVE_WORKERS` caps peak worker count, `WARM_STARTUP_CONCURRENCY` controls concurrent prewarming, and `PER_ACCOUNT_CONCURRENCY` controls request slots per account.

### Port Configuration
Expand All @@ -391,7 +392,7 @@ The service loads every account from `AISTUDIO_AUTH_STATES`. `WARM_WORKER_LIMIT`

HTTP, HTTPS, and SOCKS5 proxies without embedded credentials are supported:

1. Set the global proxy under Service Configuration
1. Set the global proxy `PROXY` in `.env`
2. Edit an account to set an account-specific proxy
3. The account proxy is used for login, WAA, and business requests

Expand Down
1 change: 0 additions & 1 deletion cmd/aistudio2api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/Mag1cFall/AIStudio2API/internal/app"
)

// main 执行 aistudio2api 命令入口
func main() {
os.Exit(app.Run(os.Args[1:]))
}
26 changes: 9 additions & 17 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,32 +152,24 @@ Camoufox 由 Go 通过 WebDriver BiDi 直接管理。启动数据面时,服务
| `MAX_ACTIVE_WORKERS` | 活动 Worker 容量上限,必须不小于热池目标 | `10` |
| `WARM_STARTUP_CONCURRENCY` | 同时初始化的预热账户数 | `2` |
| `PER_ACCOUNT_CONCURRENCY` | 单账号同时执行的请求数 | `2` |
| `ROUTING_STRATEGY` | 账户轮询 `round-robin` 或粘性优先 `fill-first` | `round-robin` |
| `ROUTING_STRATEGY` | 账户选择策略:`round-robin`(轮询均衡,均摊限流与负载);`fill-first`(粘性优先,满载或冷却后溢出) | `round-robin` |
| `TEMPORARY_CHAT` | WAA 预热页是否使用临时对话 | `false` |
| `HEADLESS` | Camoufox 是否启用无头模式(`true` 后台静默运行;`false` 弹出浏览器窗口) | `true` |
| `CAMOUFOX_PATH` | 自定义 Camoufox 浏览器可执行文件路径(可选) | 空 |

`LISTEN_ADDR` 使用 `host:port`,端口范围为 `1..65535`。时长和容量字段必须为正值,`WARM_STARTUP_CONCURRENCY` 的有效范围为 `1..WARM_WORKER_LIMIT`。全局代理 URL 使用 `http`、`https` 或 `socks5` 纯 origin 形状。命令行 `--auth` 与 `--proxy` 会覆盖每次启动生成服务时读取的保存值。

`GET /api/config` 与 `PUT /api/config` 同时暴露保存值和当前生效值
`GET /api/config` 暴露当前运行配置(只读)。配置在服务启动时从环境变量或 `.env` 加载,不支持通过 API 运行时修改

| 字段 | 语义 |
| --- | --- |
| `auth_states`、`proxy`、`init_timeout`、`request_timeout` | 下一次启动生成服务时使用的保存值 |
| `warm_worker_limit`、`max_active_workers`、`warm_startup_concurrency`、`per_account_concurrency` | 下一次启动生成服务时使用的容量参数 |
| `temporary_chat` | 下一次启动生成服务时使用的 WAA 配置 |
| `listen_addr`、`proxy_api_key` | 保存的管理监听配置 |
| `active_listen_addr`、`active_proxy_api_key` | 当前管理进程固定使用的值 |
| `management_restart_required` | 保存的监听地址或 API key 与当前管理进程不同 |
| `service_restart_required` | 保存的生成服务配置与当前生成服务实例不同 |

配置保存使用临时文件、`Sync` 和原子替换。监听地址与本地 API key 由管理进程持有,进程重启后应用;其余配置在停止并再次启动生成服务后应用。

| `auth_states`、`proxy`、`init_timeout`、`request_timeout` | 当前生成服务实例使用的基础配置 |
| `warm_worker_limit`、`max_active_workers`、`warm_startup_concurrency`、`per_account_concurrency` | 当前使用的容量与并发参数 |
| `temporary_chat`、`headless` | WAA 预热临时对话与无头运行模式 |
| `listen_addr`、`proxy_api_key` | 管理监听配置与 API 密钥 |
生成服务启动顺序如下。源码中的 `generation` 表示一次 Stop/Start 创建的生成服务实例:

```text
PUT /api/config
-> 校验并原子写入 .env
-> 返回 saved/active 差异

POST /api/control/stop
-> 取消 LAUNCHING 或活动请求
-> 等待模型目录刷新退出并关闭当前 Worker
Expand Down Expand Up @@ -270,7 +262,7 @@ Worker 容量由热池目标、活动上限和单账户并发共同约束。活
| 模型与账户 | `GET /api/models`、`GET/POST /api/accounts`、`GET/POST /api/accounts/import/chrome`、`PUT/DELETE /api/accounts/{id}` |
| 登录与验证 | `POST /api/accounts/{id}/login`、`POST /api/accounts/{id}/verify` |
| 生成服务 | `POST /api/control/start`、`POST /api/control/stop` |
| 配置 | `GET /api/config`、`PUT /api/config` |
| 配置 | `GET /api/config` |
| 冷却与请求 | `GET /api/cooldowns`、`GET /api/requests`、`POST /api/requests/{id}/cancel` |
| 日志与事件 | `DELETE /api/logs`、`GET /api/events` |

Expand Down
2 changes: 1 addition & 1 deletion docs/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ INFO service 生成服务已处于停止状态
ERROR service 生成服务停止失败 | 耗时=10.006s | 错误=<JOINED_ERROR>
```

`服务配置已保存` 表示 `.env` 已写入。管理进程配置通过进程重启应用;生成服务配置通过停止、启动生成服务应用。


### Worker 生命周期

Expand Down
6 changes: 3 additions & 3 deletions docs/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ server content 的 index `0/1/2/4/5/6` 分别为 model content、turn complete
| 生成服务 | `POST /api/control/start`、`POST /api/control/stop` |
| 账户 | `GET /api/accounts`、`POST /api/accounts`、`GET/POST /api/accounts/import/chrome`、`PUT /api/accounts/{id}`、`DELETE /api/accounts/{id}` |
| 账户认证 | `POST /api/accounts/{id}/login`、`POST /api/accounts/{id}/verify` |
| 配置 | `GET /api/config`、`PUT /api/config` |
| 配置 | `GET /api/config` |
| 冷却与请求 | `GET /api/cooldowns`、`GET /api/requests`、`POST /api/requests/{id}/cancel` |
| 日志与事件 | `DELETE /api/logs`、`GET /api/events` |

Expand All @@ -1137,7 +1137,7 @@ server content 的 index `0/1/2/4/5/6` 分别为 model content、turn complete
| `POST /api/accounts/{id}/login`、`verify` | 200 | `{"account":AdminAccount}` |
| `DELETE /api/accounts/{id}` | 204 | 空 body |
| `POST /api/control/start`、`stop` | 200 | `AdminStatus` |
| `GET /api/config`、`PUT /api/config` | 200 | `RuntimeConfig` |
| `GET /api/config` | 200 | `RuntimeConfig` |
| `GET /api/cooldowns` | 200 | `{"cooldowns":[AdminCooldown,...]}` |
| `GET /api/requests` | 200 | `{"requests":[AdminRequest,...]}` |
| `POST /api/requests/{id}/cancel` | 204 | 空 body |
Expand Down Expand Up @@ -1178,7 +1178,7 @@ server content 的 index `0/1/2/4/5/6` 分别为 model content、turn complete
| `management_restart_required` | response-only;保存的 listen/key 与当前管理进程不同 |
| `service_restart_required` | response-only;保存的生成服务配置与当前生成服务实例不同 |

`PUT /api/config` 原子保存配置。监听地址和 API key 在管理进程重启后生效;账户路径、代理、timeout、容量与临时对话在 Stop/Start 创建的新生成服务实例中生效。启动时读取最新配置;配置加载、校验、实例创建失败或启用前取消时保留原实例,切换到新实例后由它完成启动或进入 `STOPPED`
`GET /api/config` 返回当前运行配置(只读)。配置在启动时从环境变量或 `.env` 加载,不支持通过 API 运行时修改

`GET /api/events` 的初始顺序为 `status`、`models`、`accounts`、最近 200 条 `log`、`cooldowns`、按开始时间排序的活动 `request`。后续事件的 `data` 形状:

Expand Down
Loading