diff --git a/.env.example b/.env.example index b6a7fe9b..6ebd0dd4 100644 --- a/.env.example +++ b/.env.example @@ -39,7 +39,7 @@ PROXY= # 单账户初始化超时 INIT_TIMEOUT=2m -# 普通请求最大执行时间 +# 上游连续没有数据的最长时间,从请求开始到首个数据同样计入 REQUEST_TIMEOUT=30m # 常驻预热账户数,未设置时 SpiderMonkey 后端为 10、Camoufox 后端为 5 diff --git a/README.md b/README.md index ee5b7a8e..54d9e1ff 100644 --- a/README.md +++ b/README.md @@ -357,12 +357,13 @@ curl http://127.0.0.1:2048/v1/chat/completions \ 4. API 密钥填写 `.env` 中的 `PROXY_API_KEY` 5. 从 `/v1/models` 获取模型,或手动添加 `gemini-3.8-flash`、`gemini-3.5-flash-lite` -[Claude Code](https://github.com/anthropics/claude-code) 使用 Anthropic 接口,子 Agent 按 opus、sonnet、haiku 档位选择模型,以下变量把它们映射到 AI Studio 模型;WebSearch 由 Google Search 执行: +[Claude Code](https://github.com/anthropics/claude-code) 使用 Anthropic 接口,子 Agent 按 fable、opus、sonnet、haiku 档位选择模型,以下变量把它们映射到 AI Studio 模型;WebSearch 由 Google Search 执行: ```powershell $env:ANTHROPIC_BASE_URL = "http://127.0.0.1:2048" $env:ANTHROPIC_API_KEY = "" $env:ANTHROPIC_MODEL = "gemini-3.8-flash" +$env:ANTHROPIC_DEFAULT_FABLE_MODEL = "gemini-4-argon" $env:ANTHROPIC_DEFAULT_OPUS_MODEL = "gemini-3.1-pro-preview" $env:ANTHROPIC_DEFAULT_SONNET_MODEL = "gemini-3.8-flash" $env:ANTHROPIC_DEFAULT_HAIKU_MODEL = "gemini-3.5-flash-lite" @@ -672,7 +673,7 @@ cp .env.example .env | `ADMIN_PASSWORD` | 空 | 管理员密码,开启登录时必填 | | `PROXY` | 空 | Chrome 导入、登录和账户默认使用的 HTTP、HTTPS 或 SOCKS5 代理,多个代理用逗号分隔组成代理池;留空时使用系统代理 | | `INIT_TIMEOUT` | `2m` | 单账户 WAA 初始化超时 | -| `REQUEST_TIMEOUT` | `30m` | 单次请求最大执行时间,自动续写的每一轮开始时重新计时 | +| `REQUEST_TIMEOUT` | `30m` | 上游连续没有数据的最长时间,从请求开始到首个数据同样计入;上游仍在输出的请求不受总时长限制 | | `WARM_WORKER_LIMIT` | `10`,Camoufox 后端 `5` | 常驻预热账户数 | | `MAX_ACTIVE_WORKERS` | `20`,Camoufox 后端 `10` | 高峰期最多同时运行的 Worker 数 | | `WARM_STARTUP_CONCURRENCY` | `5` | 按需扩容时同时冷启动的 Camoufox Worker 数,预热逐个启动;`WAA_BACKEND=spidermonkey` 时不限制 | @@ -806,7 +807,7 @@ API 试用页的 HTML 预览运行在 `PREVIEW_LISTEN_ADDR` 提供的独立来 权益与模型目录的读取: -- “刷新全部”立即读取全部启用账户的权益与模型目录,后台每 24 小时也会读取一次;请求的模型不在目录中且距上次读取超过 10 分钟时,服务先读取一次再处理该请求 +- “刷新全部”立即读取全部启用账户的权益与模型目录,在服务端运行,关闭或刷新管理页后继续,重新打开时显示进度与排队的账户;后台每 24 小时也会读取一次;请求的模型不在目录中且距上次读取超过 10 分钟时,服务先读取一次再处理该请求 - 从未读取成功的账户权益显示为未知,并显示最近一次失败原因;已有目录的账户读取失败时照常使用,账户行显示“目录同步失败”与原因 - 读取报告 AI Studio 不支持当前出口地区时,已有目录的账户同样显示该原因 diff --git a/README_en.md b/README_en.md index 2756eec8..9172d4bf 100644 --- a/README_en.md +++ b/README_en.md @@ -357,12 +357,13 @@ For Cherry Studio: 4. Set the API key to `PROXY_API_KEY` from `.env` 5. Load models from `/v1/models`, or add `gemini-3.8-flash` and `gemini-3.5-flash-lite` manually -[Claude Code](https://github.com/anthropics/claude-code) uses the Anthropic endpoint. Subagents pick models by the opus, sonnet, and haiku tiers; the variables below map them to AI Studio models. WebSearch runs on Google Search: +[Claude Code](https://github.com/anthropics/claude-code) uses the Anthropic endpoint. Subagents pick models by the fable, opus, sonnet, and haiku tiers; the variables below map them to AI Studio models. WebSearch runs on Google Search: ```powershell $env:ANTHROPIC_BASE_URL = "http://127.0.0.1:2048" $env:ANTHROPIC_API_KEY = "" $env:ANTHROPIC_MODEL = "gemini-3.8-flash" +$env:ANTHROPIC_DEFAULT_FABLE_MODEL = "gemini-4-argon" $env:ANTHROPIC_DEFAULT_OPUS_MODEL = "gemini-3.1-pro-preview" $env:ANTHROPIC_DEFAULT_SONNET_MODEL = "gemini-3.8-flash" $env:ANTHROPIC_DEFAULT_HAIKU_MODEL = "gemini-3.5-flash-lite" @@ -672,7 +673,7 @@ Process environment variables override the same keys in `.env`, and those settin | `ADMIN_PASSWORD` | empty | Administrator password, required when login is enabled | | `PROXY` | empty | HTTP, HTTPS, or SOCKS5 proxy used by Chrome import, login, and accounts without an override; separate several proxies with commas to form a proxy pool; the system proxy is used when empty | | `INIT_TIMEOUT` | `2m` | Per-account WAA initialization timeout | -| `REQUEST_TIMEOUT` | `30m` | Maximum request execution time, restarted at the start of each auto-continuation round | +| `REQUEST_TIMEOUT` | `30m` | Longest time upstream may send no data, counted from the start of the request until the first data too; requests that upstream is still streaming have no total time limit | | `WARM_WORKER_LIMIT` | `10`, `5` with Camoufox | Number of resident prewarmed accounts | | `MAX_ACTIVE_WORKERS` | `20`, `10` with Camoufox | Maximum workers active during peak load | | `WARM_STARTUP_CONCURRENCY` | `5` | Camoufox workers cold-started concurrently during on-demand scaling; prewarming starts workers one at a time; unlimited with `WAA_BACKEND=spidermonkey` | @@ -805,7 +806,7 @@ Account actions depend on state. Click the state in an account row to set the ac Reading the benefit tier and model catalog: -- Refresh all reads the benefit tier and model catalog of every enabled account immediately; the service also reads them every 24 hours, and when a request names a model missing from the catalog and the last read was more than 10 minutes ago, the service reads them first and then handles the request +- Refresh all reads the benefit tier and model catalog of every enabled account immediately; it runs on the server and keeps going after the console is closed or reloaded, and reopening the console shows its progress and the queued accounts; the service also reads them every 24 hours, and when a request names a model missing from the catalog and the last read was more than 10 minutes ago, the service reads them first and then handles the request - An account whose catalog has never been read shows an unknown tier and the reason for the last failed read; an account with an existing catalog keeps working when a read fails, and its row shows "Model sync failed" with the reason - When a read reports that AI Studio does not support the current egress region, accounts with an existing catalog show that reason as well diff --git a/docs/development.md b/docs/development.md index e2d2966b..e8d3f564 100644 --- a/docs/development.md +++ b/docs/development.md @@ -332,7 +332,7 @@ Camoufox 运行中与服务的 WebDriver BiDi 连接断开时,服务最多等 | `ADMIN_PASSWORD` | 管理员密码 | 空 | | `PROXY` | 全局出口代理或代理池 | 空 | | `INIT_TIMEOUT` | 单账户初始化超时 | `2m` | -| `REQUEST_TIMEOUT` | 单次请求最大执行时间 | `30m` | +| `REQUEST_TIMEOUT` | 上游连续没有数据的最长时间 | `30m` | | `WARM_WORKER_LIMIT` | 常驻预热账户数 | `10`,Camoufox 后端 `5` | | `MAX_ACTIVE_WORKERS` | 活动 Worker 容量上限 | `20`,Camoufox 后端 `10` | | `WARM_STARTUP_CONCURRENCY` | 同时冷启动的 Camoufox Worker 数 | `5` | @@ -373,7 +373,7 @@ Camoufox 运行中与服务的 WebDriver BiDi 连接断开时,服务最多等 - `LOG_MAX_TOTAL_MB`:超出时删除最旧的文件,不小于 20;保存后立即生效 - `ADMIN_PASSWORD`:开启登录时必填 - `PROXY`:setup 与未设置账户代理时使用的出口,多个代理用逗号分隔组成代理池;留空时生成服务使用系统代理 -- `REQUEST_TIMEOUT`:自动续写的每一轮开始时重新计时 +- `REQUEST_TIMEOUT`:从请求开始计时,每收到一次上游数据重新计时,上游仍在输出的请求不受总时长限制 - `MAX_ACTIVE_WORKERS`:必须不小于热池目标 - `WARM_STARTUP_CONCURRENCY`:限制按需扩容时的冷启动数,预热逐个启动;`WAA_BACKEND=spidermonkey` 时不限制 - `ROUTING_STRATEGY`:账户轮询 `round-robin` 或粘性优先 `fill-first` @@ -730,7 +730,7 @@ Worker 容量由热池目标、活动上限和单账户并发共同约束。 | 健康与状态 | `GET /health`、`GET /api/status` | | 模型与账户 | `GET /api/models`、`GET/POST /api/accounts`、`GET /api/accounts/exits`、`GET/POST /api/accounts/import/chrome`、`GET /api/accounts/import/browsers`、`POST /api/accounts/import/cookies/discover`、`POST /api/accounts/import/cookies`、`PUT/DELETE /api/accounts/{id}`、`PUT /api/accounts/{id}/state` | | 账户任务 | `GET /api/accounts/tasks`、`DELETE /api/accounts/tasks/{id}` | -| 登录与刷新 | `POST /api/accounts/{id}/login`、`POST /api/accounts/{id}/refresh`、`GET /api/login-view/{view}`(WebSocket) | +| 登录与刷新 | `POST /api/accounts/{id}/login`、`POST /api/accounts/{id}/refresh`、`POST /api/accounts/refresh`、`GET /api/login-view/{view}`(WebSocket) | | 远程配对 | `GET /api/pairing`、`POST /api/pairing`、`POST /api/pairing/accounts`、`PUT /api/pairing/accounts/{account}/cookies` | | 生成服务 | `POST /api/control/start`、`POST /api/control/stop` | | 配置 | `GET /api/config`、`PUT /api/config`、`GET /api/paths?path=`(列出服务所在机器的目录与账户状态文件 `storage-state.json`,`path=roots` 列出文件系统根)、`POST /api/paths/pick?path=`(桌面版以系统对话框选择账户目录) | diff --git a/docs/logging.md b/docs/logging.md index db15919b..ffb88164 100644 --- a/docs/logging.md +++ b/docs/logging.md @@ -32,7 +32,7 @@ `INFO` 记录状态推进和完成结果,`WARN` 记录等待、切换与客户端取消,`ERROR` 记录失败结果。管理页面支持按级别、来源、消息、阶段名、字段名与字段取值筛选,日志正文可以选择和横向滚动。 -页面每行的消息列依次为展开箭头、状态位、标题与字段、行尾耗时,各行标题左缘对齐:请求的状态位为 HTTP 状态,阶段为进度或结束状态图标,单条运行事件为中性圆点;字段 `duration` 显示在行尾。 +页面每行的消息列依次为展开箭头、状态标签、标题与字段、行尾耗时,各行标题左缘对齐:请求的状态标签为 HTTP 状态或“处理中”,阶段为进度或结束状态名,单条运行事件没有状态标签;字段 `duration` 显示在行尾。展开的详情从行首开始。 带 `stage` 的运行事件在页面上按来源与阶段名合并为一张卡片,显示阶段名、当前进度 `step/steps` 与最新一步的消息和字段。带 `state` 的事件结束同一来源进行中的卡片并显示结束状态,没有进行中的卡片时自成一张已结束的卡片;步骤号不大于当前步骤时另起一张卡片。卡片位于首条消息的时间位置,展开详情可查看每一步的时间、消息与字段;级别、来源和文本筛选在任一步命中时显示该卡片。 @@ -214,6 +214,7 @@ ERROR account@example.com [WAA Worker 停止 failed] WAA Worker 停止失败 p - 展开详情可查看接口、输入用量、原始终止原因、事件时间线与 JSON - 时间线中每次换号为一行,显示账户、上游状态、按状态归类的原因与上游原文,点击展开该账户的全部事件;请求的来源按换号顺序写成 `A → B`,上游要求验证账户的 403 归为“需要验证账户”,跳转到拒绝页的归为“服务受限” - 连续的同类进度事件合为一行并标出次数,悬停次数查看每一次的时刻与字段 +- 时间线每步一行,依次为时刻、消息与耗时(`duration` 或 `waited`),其余字段在下方按名称与取值对齐;`等待模型继续输出` 与随后的 `模型继续输出` 合为一行“停顿 N 后继续”;`rpc`、`mode`、`network_bytes`、`network_idle`、`reasoning_events`、`text_events`、`last_event`、`current_event` 以及与卡片相同的模型和通道只在 JSON 中 ```text 22:11:47 200 gemini-3.8-flash 换号 5 403 ×3 429 ×2 4.76 s 工具调用 1 @@ -253,11 +254,11 @@ ERROR account@example.com [WAA Worker 停止 failed] WAA Worker 停止失败 p 客户端取消记录为 `499`;认证、额度和上游失败分别使用对应 HTTP 状态与 `request.error`。 -管理端取消活动请求或停止生成服务时,仍连接的客户端按公开协议收到 `503 request_canceled` 或对应的流式 error event。 +管理端取消活动请求时,仍连接的客户端按公开协议收到 `503 request_canceled` 或对应的流式 error event;停止生成服务时收到 `503 service_stopped`,错误消息写明生成服务已停止。 ## 流式等待 -连续 15 秒没有阶段进展时会写入诊断日志。请求在收到上游终态、客户端取消或达到 `REQUEST_TIMEOUT` 时结束。 +连续 15 秒没有阶段进展时会写入诊断日志。请求在收到上游终态、客户端取消或上游连续 `REQUEST_TIMEOUT` 没有数据时结束。流在完成前中断或上游异常结束而重新生成时,记一条“重新生成”,`attempt` 为第几次,`reason` 为中断错误或结束原因。 | 事件 | 当前停留位置 | 重点字段 | | --- | --- | --- | diff --git a/docs/protocol.md b/docs/protocol.md index 07b60628..346c0a19 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -706,7 +706,7 @@ generation config 字段: - 模型只有 thinking level 能力时,显式 budget 按 0、1024、8192 以内与更大预算分别退化为 minimal、low、medium、high - 没有 thinking level 能力的模型忽略其余 effort;两类能力都缺少时同时忽略 budget -生成与计数请求的模型名可以带思考后缀 `<模型 ID>(<值>)`,例如 `gemini-4-argon(low)`、`gemini-2.5-pro(16384)`。值不区分大小写:`none`、`minimal`、`low`、`medium`、`high`、`xhigh`、`extra_high`、`max` 作为思考强度,非负整数作为思考预算,`auto` 与 `-1` 使用模型默认思考设置;后缀覆盖请求体中的思考强度与预算,去掉后缀的模型 ID 用于调度、请求日志与用量统计。其他值返回 400。 +生成与计数请求的模型名可以带思考后缀 `<模型 ID>(<值>)`,例如 `gemini-4-argon(low)`、`gemini-2.5-pro(16384)`。值不区分大小写:`none`、`minimal`、`low`、`medium`、`high`、`xhigh`、`extra_high`、`max` 作为思考强度,非负整数作为思考预算,`auto` 与 `-1` 使用模型默认思考设置;后缀覆盖请求体中的思考强度与预算,去掉后缀的模型 ID 用于调度、请求日志与用量统计。其他值返回 400。Claude Code 1M 上下文模式加在模型名末尾的 `[1m]`(不区分大小写)在完整模型名不是 `MODEL_MAPPING` 别名时去掉,例如 `gemini-4-argon(low)[1m]` 按 `gemini-4-argon(low)` 处理。 能力码 48 的模型在目录中另有 `<模型 ID>-build` 条目,经 Build 写代码助手生成,思考后缀写在 `-build` 之后,例如 `gemini-3.8-flash-build(low)`;条目定义、请求与流格式见 [Build 通道](build.md)。 @@ -955,7 +955,7 @@ ListModels 能力码 91(目录能力 `output_continuation`)的模型由官 - usage 以最后一轮为基础,visible output、thought 与 tool tokens 逐轮累加,total 为最后一轮 total 加之前各轮的这三项 - 未指定输出上限时,输出总量为模型的 `inputTokenLimit` - 输出总量超过 32768 的自动续写请求只经 Playground 调度;未指定输出上限且含 File Search、`serviceTier`、`store:false` 等经 Build 通道发送的设置时仍可经 Build,按 Build 的单轮上限发送 -- 请求期限为 `REQUEST_TIMEOUT`,每个续写轮开始时重新计时 +- 上游连续 `REQUEST_TIMEOUT` 没有数据时请求以超时结束,续写轮之间等待响应的时间同样计入 - 每个续写轮在日志中记一条“上游调用”,模式后附 `续写=<轮次>` 续写 token 可由其他账户接续:续写轮以 401、403、429 或 5xx 失败时,未指定账户、不含文件与内联媒体的请求换到本请求续写轮尚未失败的其他账户,以该账户的新 proof 重发同一续写轮,之后各轮沿用这个 proof。额度失败的账户按额度冷却规则冷却,用量明细记为最后生成的账户。 @@ -989,6 +989,7 @@ Build 通道的 Gemini API 终止原因去掉 `FINISH_REASON_` 前缀后转为 - 非流式请求丢弃被取代那一次的思考;流式请求已输出的思考保留,之后接着输出重新生成那一次的思考 - 每次重新生成从头匹配停止序列,上一次暂存的停止序列前缀丢弃 - 重新生成的请求发送失败时,本次生成以该错误结束 +- 每次重新生成在请求日志中记一条“重新生成”,写明第几次与原因(中断错误或结束原因) 两个通道第一次带终止原因的帧中的内容事件标记为结束帧,自动续写中转入下一轮的帧不标记;Gemini 流式输出把结束帧的内容放进最后一块。 @@ -1671,6 +1672,7 @@ GET/DELETE /v1beta/files/{file} - `/v1*`、`/upload/` 与 `/health` 响应允许任意 origin,并以 `Access-Control-Expose-Headers: *` 暴露响应头 - OPTIONS 预检跳过 API key 校验,允许 `GET/POST/PUT/DELETE/OPTIONS`,`Access-Control-Allow-Headers` 回显请求的 `Access-Control-Request-Headers` - `/v1*` 请求体上限约为 684 MiB,可容纳 Base64 编码的 512 MiB 文件 +- 请求头 `Content-Encoding` 为 `gzip`、`x-gzip`、`zstd` 或它们以逗号相连(多个同名请求头按出现顺序相连)时按顺序解压请求体,解压后的正文同样受上限约束;无法解压返回 400,其他编码返回 415 - `/upload/v1beta/files` 使用相同的认证与请求体上限;可续传上传的分块请求发往 start 响应 `X-Goog-Upload-URL` 给出的地址,凭其中的 `upload_id` 鉴权,无需 API key 公开 `/v1`、`/v1beta` 与 `/upload/` 的请求带 `X-AIStudio-Account: <账户 ID>` 时,账户池只在该账户上选号,视频后台生成与可续传上传同样只用该账户;上游出错时返回该错误,不换到其他账户,账户冷却中的处理与只有这一个可用账户时相同;该账户仍受所用 key 的 `API_KEY_ACCOUNTS` 绑定限制,不受 `MODEL_ACCOUNTS` 限制。 @@ -1696,7 +1698,7 @@ GET/DELETE /v1beta/files/{file} | 生成服务 | `POST /api/control/start`、`POST /api/control/stop` | | 账户 | `GET /api/accounts`、`GET /api/accounts/exits`、`POST /api/accounts`、`GET/POST /api/accounts/import/chrome`、`GET /api/accounts/import/browsers`、`POST /api/accounts/import/cookies/discover`、`POST /api/accounts/import/cookies`、`PUT /api/accounts/{id}`、`PUT /api/accounts/{id}/state`、`DELETE /api/accounts/{id}` | | 账户任务 | `GET /api/accounts/tasks`、`DELETE /api/accounts/tasks/{id}` | -| 账户认证与刷新 | `POST /api/accounts/{id}/login`、`POST /api/accounts/{id}/refresh`、`GET /api/login-view/{view}`(WebSocket) | +| 账户认证与刷新 | `POST /api/accounts/{id}/login`、`POST /api/accounts/{id}/refresh`、`POST /api/accounts/refresh`、`GET /api/login-view/{view}`(WebSocket) | | 远程配对 | `GET /api/pairing`、`POST /api/pairing`、`POST /api/pairing/accounts`、`PUT /api/pairing/accounts/{account}/cookies` | | 配置 | `GET /api/config`、`PUT /api/config`、`GET /api/first-event-latency` | | 冷却与请求 | `GET /api/cooldowns`、`DELETE /api/accounts/{id}/cooldowns/{scope}`、`GET /api/requests`、`POST /api/requests/{id}/cancel` | @@ -1730,6 +1732,7 @@ GET/DELETE /v1beta/files/{file} | `PUT /api/accounts/{id}/state` | 200 | `{"account":AdminAccount}` | | `POST /api/accounts/{id}/login` | 202 | `{"task":AccountTask}` | | `POST /api/accounts/{id}/refresh` | 200 | `AccountRefresh` | +| `POST /api/accounts/refresh` | 202 | `{"task":AccountTask}` | | `DELETE /api/accounts/{id}` | 204 | 空 body | | `POST /api/control/start`、`stop` | 200 | `AdminStatus` | | `GET /api/config`、`PUT /api/config` | 200 | `RuntimeConfig` | @@ -1766,9 +1769,10 @@ GET/DELETE /v1beta/files/{file} - `GET /api/accounts/import/browsers` 为每个浏览器的每个会话各列一项,查询参数 `proxy` 为识别账户的请求出口,省略时用默认出口 - `POST /api/accounts/import/cookies` 中每项的 `auth_user` 选择导入的会话,省略时为 0;`email` 为识别会话时列出的邮箱,作为导入账户的邮箱,为空时按该会话的对话页识别 - 账户的 `auth_user` 写入 `account.json`,运行时以 `/u/N/` 页面与 `X-Goog-AuthUser: N` 访问官网;浏览器重新登录后为 0 -- 添加、导入与重新登录账户在账户任务中运行: +- 添加、导入、重新登录与刷新账户在账户任务中运行: - 请求解析 body 后返回 202 与任务;任务在服务端运行到结束,请求结束或管理页面关闭时继续 - 任务中的账户同时处理;单个账户失败时在该项记下错误码与原因,其他账户照常处理 + - `POST /api/accounts/refresh` 以 `{"ids":[账户 ID,...]}` 刷新所列账户,任务的 `kind` 为 `refresh`,各项的 `label` 为账户 ID;同时刷新 4 个账户,其余以 `pending` 排队 - 错误码:Cookie 无法解析或缺少登录 Cookie 为 `invalid_cookies`,调试地址连接或读取失败为 `cdp_unavailable`,账户目录已存在而账户不在列表中时为 `account_exists` - 导入或浏览器登录得到账户列表中已有的账户时,就地替换它的认证状态,保留文件资源绑定 - 有账户失败时任务以 `failed` 结束,否则为 `done` @@ -2511,9 +2515,11 @@ Responses 的图片、视频与文件 Part 复用上述 data URL、file ID 与 Y | `local_shell_call` | `local_shell` 函数调用,参数为去掉 `type` 的 `action` | | `shell_call` | `shell` 函数调用,参数为 `action` | | `apply_patch_call` | `apply_patch` 函数调用,参数为 `operation` | -| `custom_tool_call_output`、`local_shell_call_output`、`shell_call_output`、`apply_patch_call_output` | 按 `call_id` 关联的函数结果 | +| `custom_tool_call_output`、`local_shell_call_output`、`shell_call_output`、`apply_patch_call_output` | 按 `call_id` 关联的函数结果;`function_call_output` 等的 `status` 为 `incomplete` 时结果为 `{"status":"incomplete","output":...}`,`output` 为带媒体内容字段(如 `image_url`、`file_data`、`source`、`data`)的单个媒体块对象时按只含它的块数组处理 | +| `reasoning` | `encrypted_content` 作为随后函数调用的签名,没有 `encrypted_content` 的 reasoning 项不改变待用的签名 | | `computer_call` | `computer` 函数调用,参数为 `action`,或 `{"actions":[...]}` | -| `web_search_call`、`code_interpreter_call`、`file_search_call`、`mcp_call`、`mcp_list_tools`、`mcp_approval_request`、`tool_search_call`、`tool_search_output`、`program`、`program_output` | assistant 文本摘要 ` `;输入含 `mcp_approval_response` 时 `mcp_approval_request` 为 `mcp____` 函数调用,带 `approval_request_id` 的 `mcp_call` 并入对应函数结果 | +| `tool_search_call`、`tool_search_output`(`execution` 为 `client` 或省略) | `tool_search` 函数的调用与按 `call_id` 关联的结果,结果为 `{"status","tools":[<工具名>]}`;`tool_search_output.tools` 中的工具并入请求的工具列表,与已有工具同名时只声明一次,同名命名空间合并成员,结果中的命名空间列出 `<命名空间>.<成员>` | +| `web_search_call`、`code_interpreter_call`、`file_search_call`、`mcp_call`、`mcp_list_tools`、`mcp_approval_request`、服务端执行的 `tool_search_call` 与 `tool_search_output`、`program`、`program_output` | assistant 文本摘要 ` `;输入含 `mcp_approval_response` 时 `mcp_approval_request` 为 `mcp____` 函数调用,带 `approval_request_id` 的 `mcp_call` 并入对应函数结果 | | `additional_tools` | `tools` 并入请求的工具列表 | | `configuration_update` | 最后一个带 `reasoning.effort` 的项覆盖请求的推理强度 | | `image_generation_call` | assistant 文本摘要与 `result` 图片 | @@ -2540,7 +2546,8 @@ Responses tool 字段: | `image_generation` | 图片模型按与 `POST /v1/images/generations` 相同的规则处理 `size`、`quality`、`output_format` 与 `output_compression`;`input_image_mask` 见下文 | | `computer_use_preview`、`computer` | 名为 `computer` 的函数;`computer_use_preview` 的 `display_width`、`display_height` 与 `environment` 写入描述,参数为单个 `action`,`computer` 的参数为 `actions` 列表 | | `mcp` | 上游 MCP 服务器:`server_label`、`server_url`、`authorization`、`headers`、`allowed_tools`(数组或 `tool_names`),`server_description` 写入系统指令 | -| `file_search`、`tool_search`、`programmatic_tool_calling` | 接受但不声明 | +| `tool_search`(`execution:"client"`) | 名为 `tool_search` 的函数,说明与参数取声明中的 `description` 与 `parameters`,没有时参数为 `query` 与 `limit` | +| `file_search`、服务端执行的 `tool_search`、`programmatic_tool_calling` | 接受但不声明 | - web search 工具的 `search_context_size`、`user_location` 与 `filters.allowed_domains` 写入搜索指令,域名限制使用 `site:` 查询;上游 grounding query 与 sources 原样返回 - `search_content_types` 只含 `image` 时只开启图片搜索,同时含 `text` 时同时开启网页搜索;图片搜索查询同样输出为 `web_search_call` @@ -2611,9 +2618,10 @@ output item 联合类型: | type | 字段 | | --- | --- | | `reasoning` | `id`、`status`、`summary:[{type:"summary_text",text}]`、可选 `encrypted_content`;摘要文本只在请求 `reasoning.summary`(或 `generate_summary`)为 `auto`、`concise`、`detailed` 时返回 | -| `message` | `id`、`status`、`role:"assistant"`、`content:[{type:"output_text",text,annotations}]` | -| `function_call` | `id`、`status`、`call_id`、`name`、`arguments` | -| `custom_tool_call` | `id`、`status`、`call_id`、`name`、`input`,可选 `namespace` | +| `message` | `id`、`status`、`role:"assistant"`、`content:[{type:"output_text",text,annotations,logprobs:[]}]` | +| `function_call` | `id`(`fc_` 开头)、`status`、`call_id`、`name`、`arguments`;`collaboration` 命名空间的 `spawn_agent`、`send_message`、`followup_task` 另带 `encrypted_function_args:[]` | +| `custom_tool_call` | `id`(`ctc_` 开头)、`status`、`call_id`、`name`、`input`,可选 `namespace` | +| `tool_search_call` | 客户端执行的 `tool_search` 被调用时输出:`id`(`tsc_` 开头)、`status`、`call_id`、`execution:"client"`、`arguments` 对象 | | `local_shell_call` | `id`、`status`、`call_id`、`action:{type:"exec",command,env,...}` | | `shell_call` | `id`、`status`、`call_id`、`action:{commands,timeout_ms,max_output_length}`、`environment:null` | | `apply_patch_call` | `id`、`status`、`call_id`、`operation:{type,path,diff}` | @@ -2718,8 +2726,10 @@ Responses usage: | `context_management` | `{edits:[...]}`,见下文上下文管理 | - `system` 消息以 `` 包裹并入相邻的用户内容:前一条为 user 时追加在其末尾,否则并入下一条 user 消息、排在其 `tool_result` 之后,使函数调用之后紧跟函数结果 -- `system` 消息中的 `tool_addition` 与 `tool_removal` 块按出现顺序增删声明的工具:`tool_reference` 按 `name` 引用 `tools` 中的工具(含 `defer_loading` 工具,`tools` 中的工具都声明给上游),`tool_definition` 的 `definition` 加入工具列表,`tool_removal` 移除同名工具 +- `system`、`user` 与 `assistant` 消息中的 `tool_addition` 与 `tool_removal` 块按出现顺序增删声明的工具,块以 `tool` 给出一个工具或以 `tools` 给出多个,这两类块不进入对话内容:`tool_reference` 按 `name` 引用 `tools` 中的工具(含 `defer_loading` 工具,`tools` 中的工具都声明给上游),`tool_definition` 的 `definition` 加入工具列表,`tool_removal` 移除同名工具 - `output_config.format` 的 `json_schema` 设置结构化输出,其他 type 返回 `400 invalid_request_error` +- `thinking.type:"disabled"` 时思考关闭,`output_config.effort` 不改变它;对话中途 `system` 消息的 `output_config.effort` 覆盖顶层的 `output_config.effort`,最后一个生效 +- 已开始流式响应后,5xx 原因的流内 `error` 事件 `error.type` 为 `overloaded_error`,消息为真实原因 - `thinking.display:"omitted"` 时 thinking block 的正文为空,签名照常返回 message content 可以是 string 或 block 数组: @@ -3200,7 +3210,7 @@ tool group 字段: `:countTokens` 也接受把生成请求放在 `generateContentRequest`(或 `generate_content_request`)中的包装形式,按其中的 `contents`、`systemInstruction` 与 `tools` 计数。 -`systemInstruction` 的各文本 Part 作为系统指令的独立分段发送,图片与文件 Part 移到首个 user 轮开头,函数调用与函数结果各以 Gemini API JSON 文本(如 `{"functionCall":{"args":{"city":"Paris"},"name":"get_weather"}}`)作为一个分段。 +`systemInstruction` 的各文本 Part 作为系统指令的独立分段发送,图片与文件 Part 移到首个 user 轮开头,函数调用与函数结果各以 Gemini API JSON 文本(如 `{"functionCall":{"args":{"city":"Paris"},"name":"get_weather"}}`)作为一个分段。`contents` 中 `role` 为 `system` 或 `developer` 的轮按 `systemInstruction` 的方式把函数调用与函数结果转为 JSON 文本,在首个对话轮之前时文本并入系统指令,在对话中途时以 `` 包裹并入相邻的 user 轮,图片与文件移到首个 user 轮开头。 `serviceTier` 取 `standard`、`flex`、`priority`,`store:false` 与 `labels` 经 Build 通道发送;`safetySettings` 中的 `HARM_CATEGORY_JAILBREAK` 同样经 Build 通道发送。Build 通道响应的 `usageMetadata.serviceTier` 为上游实际使用的等级。 @@ -3930,9 +3940,9 @@ Gemini 列中注明状态码的行以该状态码返回;Interactions 路由与 | 候选账户均在冷却且在本请求累计排队 1 分钟内不恢复,或已试账户都因额度失败而其余账户在冷却(套餐限额与每日限额冷却中的账户逐个发送一次,其中 5 到 60 分钟内拒绝过同样大或更小请求的账户、收到其他额度失败且该失败的冷却没有结束的账户跳过,全部拒绝时返回最后一次拒绝,全部跳过时立即返回;带 `Retry-After`,Gemini 错误体带 `RetryInfo`) | 429 | `rate_limit_exceeded` | `rate_limit_error` | `RESOURCE_EXHAUSTED` | | 上游过载 | 529 | `upstream_error` | `overloaded_error` | `INTERNAL` | | 当前客户端请求被管理端取消 | 503 | `request_canceled` | `api_error` | `UNAVAILABLE` | -| 生成服务已停止 | 503 | `service_stopped` | `api_error` | `UNAVAILABLE` | +| 生成服务已停止,包括停止时进行中的请求 | 503 | `service_stopped` | `api_error` | `UNAVAILABLE` | | 生成服务正在启动且模型目录尚未就绪 | 503 | `service_starting` | `api_error` | `UNAVAILABLE` | -| 请求期限到期 | 504 | `upstream_error` | `timeout_error` | `DEADLINE_EXCEEDED` | +| 上游连续 `REQUEST_TIMEOUT` 没有数据 | 504 | `upstream_error` | `timeout_error` | `DEADLINE_EXCEEDED` | | 传输、Content-Type、解码或缺失终态 | 502 | `upstream_error` | `api_error` | `INTERNAL` | | 上游以内容策略类原因结束生成或以 `other`、`image_safety` 拦截请求 | 502 | `upstream_error` | `api_error` | `INTERNAL` | | 上游异常结束、重新生成后仍如此且没有输出 | 502 | `upstream_error` | `api_error` | 200 与对应 `finishReason` | @@ -3943,7 +3953,7 @@ Gemini 列中注明状态码的行以该状态码返回;Interactions 路由与 - “支持请求的账户均不可调度”指这些账户都处于需要重新登录、服务受限、不可用或已停用状态,错误消息按状态给出账户数,例如 `No account can serve this request (accounts sign-in required: 2, service restricted: 1)` - 管理端启动生成服务期间到达的公开 API 请求等启动结束再处理;生成服务处于 `STOPPED` 时,生成与计数端点返回 `service_stopped`,错误消息说明在管理页面点击“启动服务”或开启“自动启动生成服务” - OpenAI 错误对象的 `type` 在参数、认证、权限与资源不存在时为 `invalid_request_error`,429 为 `requests`,其余为 `server_error`;`param` 为 `null` -- Responses 流中的 `response.failed` 的 `error.code` 取官方枚举:429 为 `rate_limit_exceeded`,400 为 `invalid_prompt`,其余为 `server_error` +- Responses 流中的 `response.failed` 的 `error.code` 取官方枚举:429 为 `rate_limit_exceeded`,400 为 `invalid_prompt`,其余为 `server_error`;输入 token 超过模型上限时为 `context_length_exceeded` - 错误消息为英文:上游错误为上游原文,例如 `You've reached your usage limit…`;服务自身的错误为英文说明,5xx 只给出最内层原因;消息中的邮箱地址写作 `[account]`;管理端请求记录保留完整原因,含上游方法、HTTP 状态、协议错误码与账户 - 可重试的 OpenAI 与 Anthropic 错误消息末尾另起一句 `Please try again in <秒数>s.`,秒数与 Retry-After 相同;已开始流式响应后的错误事件同样带这句,Gemini 流内错误对象带 RetryInfo - 候选账户均在冷却时,消息在最早恢复时间后附上该冷却的类型与触发它的上游原文,例如 `Daily limit: … limit: 100, model: gemini-3.8-flash …` diff --git a/go.mod b/go.mod index 02519c79..80e38cb7 100644 --- a/go.mod +++ b/go.mod @@ -7,8 +7,6 @@ require ( github.com/Mag1cFall/fhttp v0.6.8-firefox.2 github.com/Mag1cFall/go-mp3 v0.1.1 github.com/Mag1cFall/go-sentencepiece v0.7.1 - github.com/Mag1cFall/uquic v0.0.6-firefox.6 - github.com/Mag1cFall/utls v1.8.2-firefox.3 github.com/bogdanfinn/fhttp v0.6.8 github.com/bogdanfinn/tls-client v1.15.1 github.com/bogdanfinn/utls v1.7.7-barnius @@ -60,7 +58,6 @@ require ( github.com/mattn/go-isatty v0.0.24 // indirect github.com/mewkiz/pkg v0.0.0-20250417130911-3f050ff8c56d // indirect github.com/mewpkg/term v0.0.0-20241026122259-37a80af23985 // indirect - github.com/molecule-man/go-brrr v1.2.0 // indirect github.com/power-devops/perfstat v0.0.0-20260805114148-88456608a4f6 // indirect github.com/quic-go/qpack v0.6.0 // indirect github.com/segmentio/asm v1.1.3 // indirect diff --git a/go.sum b/go.sum index 75fcd075..1d7c850d 100644 --- a/go.sum +++ b/go.sum @@ -8,10 +8,6 @@ github.com/Mag1cFall/go-mp3 v0.1.1 h1:r/wBscYGXWpGdEhCQCcrHI36EDbNx6rtio2yv/Y547 github.com/Mag1cFall/go-mp3 v0.1.1/go.mod h1:/vhuC+guK5mKVhCBE8ZQSocH0FmHXM244FT7239cSlY= github.com/Mag1cFall/go-sentencepiece v0.7.1 h1:Evzu4bQYge6F2tv40wMqr9MazbtbvoPbSD/eLl2gJEA= github.com/Mag1cFall/go-sentencepiece v0.7.1/go.mod h1:5xb9a9oAB7EQ/J/47DTrj+BSrGoJIcXZ9CB5y27Nehw= -github.com/Mag1cFall/uquic v0.0.6-firefox.6 h1:AvAWHvAMST2ffRJb+LKTWv8+Gz0u6JfzGxcXuXxOPFI= -github.com/Mag1cFall/uquic v0.0.6-firefox.6/go.mod h1:JHH4tzlX5KM0a4TJSglkbL2kW1hdVBenlA0YFKD8o0E= -github.com/Mag1cFall/utls v1.8.2-firefox.3 h1:5DGK0Ps9Kew+pd1E0d96YeirOdk4AOHJmx+NeNJwRxM= -github.com/Mag1cFall/utls v1.8.2-firefox.3/go.mod h1:HFKOiYks7qa1VvvhZBWu7Ic9tTYm1xNXHKiRcpw3Mtc= github.com/adrg/xdg v0.5.3 h1:xRnxJXne7+oWDatRhR1JLnvuccuIeCoBu2rtuLqQB78= github.com/adrg/xdg v0.5.3/go.mod h1:nlTsY+NNiCBGCK2tpm09vRqfVzrc2fLmXGpBLF0zlTQ= github.com/andybalholm/brotli v1.2.0 h1:ukwgCxwYrmACq68yiUqwIWnGY0cTPox/M94sVwToPjQ= @@ -91,8 +87,6 @@ github.com/minio/selfupdate v0.6.0 h1:i76PgT0K5xO9+hjzKcacQtO7+MjJ4JKA8Ak8XQ9DDw github.com/minio/selfupdate v0.6.0/go.mod h1:bO02GTIPCMQFTEvE5h4DjYB58bCoZ35XLeBf0buTDdM= github.com/modelcontextprotocol/go-sdk v1.8.0 h1:KIvahhYqwtbeniWVPs3TcXEA7b8jEtwfBpOTAI+Urx4= github.com/modelcontextprotocol/go-sdk v1.8.0/go.mod h1:dL7u98E/zjJTGzEq+j30jQ8K2k1mb6LeAH4inEcSGts= -github.com/molecule-man/go-brrr v1.2.0 h1:dOJU45BC3Gc2WXoxAW0rgCAgVuu2gruAOGncfieA0Jw= -github.com/molecule-man/go-brrr v1.2.0/go.mod h1:7ybW6/7gA3oKY45jOfVNjSJDtrr6ea4tzbsTkjmQDC4= github.com/nlnwa/whatwg-url v0.6.2 h1:jU61lU2ig4LANydbEJmA2nPrtCGiKdtgT0rmMd2VZ/Q= github.com/nlnwa/whatwg-url v0.6.2/go.mod h1:x0FPXJzzOEieQtsBT/AKvbiBbQ46YlL6Xa7m02M1ECk= github.com/power-devops/perfstat v0.0.0-20260805114148-88456608a4f6 h1:jL3a8soXdzuTCcRnKhOmtcsVOObdDTFf4O2B403HPRU= @@ -157,8 +151,6 @@ golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c= -golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o= golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg= golang.org/x/mod v0.42.0/go.mod h1:u5fzIrRInkL8MaO1cbRkUHT64v303YIT5KEqpNr7jRs= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -184,8 +176,6 @@ golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk= -golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -239,7 +229,6 @@ golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58 golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= golang.org/x/tools v0.51.0 h1:k4Xc/1Om9jwkBJBo4NVLMSARBoWtK10mx+W5BnXCeAI= golang.org/x/tools v0.51.0/go.mod h1:9eEncMayCV6zRMGhR5eZEC2iBx98qWcF1HZ9Z7wJOoA= -golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= diff --git a/internal/aistudio/cookie_import.go b/internal/aistudio/cookie_import.go index 95ae1187..e3b59157 100644 --- a/internal/aistudio/cookie_import.go +++ b/internal/aistudio/cookie_import.go @@ -173,6 +173,9 @@ func parseCookieHeader(header string) ([]StateCookie, error) { } cookies := make([]StateCookie, 0) for _, item := range strings.Split(header, ";") { + if strings.TrimSpace(item) == "" { + continue + } name, value, found := strings.Cut(strings.TrimSpace(item), "=") name = strings.TrimSpace(name) if !found || name == "" { diff --git a/internal/aistudio/generate.go b/internal/aistudio/generate.go index 37172248..45dbe862 100644 --- a/internal/aistudio/generate.go +++ b/internal/aistudio/generate.go @@ -972,6 +972,11 @@ func (c *Client) Generate(ctx context.Context, request GenerateRequest) (<-chan err := decodeStream(observeStreamActivity(ctx, response.Body), roundEmit()) // 写代码助手的调用块错误已在 trajectory 内重答,这里只重新生成发送与读取失败 for regenerated := 0; regenerated < regenerations && regenerable(ctx, err, finish, committed) && (err != nil || !assistant); regenerated++ { + if err != nil { + reportRegeneration(ctx, regenerated+1, err.Error()) + } else { + reportRegeneration(ctx, regenerated+1, fmt.Sprintf("结束原因 %s", finish.FinishReason)) + } stopClose() _ = response.Body.Close() usage, finish, held, turn, roundUsage, output = nil, nil, nil, modelTurn{}, nil, Output{} @@ -1326,7 +1331,6 @@ func (c *Client) decodePlaygroundRounds(ctx context.Context, request GenerateReq } request.continuationRound++ request.continuationToken = decoder.continuation - reportContinuationRound(ctx) reportUpstreamMode(ctx, "GenerateContent", "stream", request.continuationRound, "") body, proofAt, err := EncodeGenerateContentRequest(request, defaults, runtime) if err != nil { diff --git a/internal/aistudio/navigation_timing.go b/internal/aistudio/navigation_timing.go index 991dcae0..268836a6 100644 --- a/internal/aistudio/navigation_timing.go +++ b/internal/aistudio/navigation_timing.go @@ -78,8 +78,6 @@ func documentTiming(entry map[string]any, resources time.Duration) { // nextHopProtocol 返回响应协议对应的 ALPN 名称 func nextHopProtocol(proto string) string { switch proto { - case "HTTP/3.0": - return "h3" case "HTTP/2.0": return "h2" default: diff --git a/internal/aistudio/request_phase.go b/internal/aistudio/request_phase.go index de50f940..fee02097 100644 --- a/internal/aistudio/request_phase.go +++ b/internal/aistudio/request_phase.go @@ -18,10 +18,10 @@ type requestPhaseContextKey struct{} type upstreamModeContextKey struct{} -type continuationContextKey struct{} - type serviceRunContextKey struct{} +type regenerationContextKey struct{} + // ContextWithServiceRun 记下生成服务本次运行的上下文,服务停止时它被取消 func ContextWithServiceRun(ctx context.Context, run context.Context) context.Context { return context.WithValue(ctx, serviceRunContextKey{}, run) @@ -39,14 +39,14 @@ func afterServiceStop(ctx context.Context, stop func()) func() bool { return context.AfterFunc(run, stop) } -// ContextWithContinuationObserver 在自动续写开始新一轮时调用 observer -func ContextWithContinuationObserver(ctx context.Context, observer func()) context.Context { - return context.WithValue(ctx, continuationContextKey{}, observer) +// ContextWithRegenerationObserver 在流在完成前中断、流尾为 5xx 或上游异常结束而重新生成之前调用 observer,attempt 从 1 起,reason 为中断错误或结束原因 +func ContextWithRegenerationObserver(ctx context.Context, observer func(attempt int, reason string)) context.Context { + return context.WithValue(ctx, regenerationContextKey{}, observer) } -func reportContinuationRound(ctx context.Context) { - if observer, ok := ctx.Value(continuationContextKey{}).(func()); ok { - observer() +func reportRegeneration(ctx context.Context, attempt int, reason string) { + if observer, ok := ctx.Value(regenerationContextKey{}).(func(int, string)); ok { + observer(attempt, reason) } } diff --git a/internal/aistudio/stream_activity.go b/internal/aistudio/stream_activity.go index 0322da53..1c68388b 100644 --- a/internal/aistudio/stream_activity.go +++ b/internal/aistudio/stream_activity.go @@ -12,8 +12,15 @@ type streamActivityReader struct { notify func(int) } -// ContextWithStreamActivityObserver 记录上游响应体实际到达的字节 +// ContextWithStreamActivityObserver 记录上游响应体实际到达的字节,ctx 已有的观察函数照常收到同样的字节 func ContextWithStreamActivityObserver(ctx context.Context, observer func(int)) context.Context { + if parent, _ := ctx.Value(streamActivityContextKey{}).(func(int)); parent != nil { + own := observer + observer = func(count int) { + parent(count) + own(count) + } + } return context.WithValue(ctx, streamActivityContextKey{}, observer) } diff --git a/internal/aistudio/transport_browser.go b/internal/aistudio/transport_browser.go index f45d2b3f..5ba28d1e 100644 --- a/internal/aistudio/transport_browser.go +++ b/internal/aistudio/transport_browser.go @@ -2,6 +2,7 @@ package aistudio import ( "context" + "encoding/binary" "fmt" "io" "net" @@ -29,6 +30,8 @@ const ( firefoxPingThreshold = 58 * time.Second // firefoxPingTimeout 是 Firefox network.http.http2.ping-timeout,PING 超过该时长未应答时关闭连接 firefoxPingTimeout = 8 * time.Second + // nssTLSGREASEECHInner 是 NSS 为 TCP ClientHello 的 GREASE ECH 估算的内层 ClientHello 基础长度 + nssTLSGREASEECHInner = 217 // firefoxTLSHandshakeTimeout 是 Firefox network.http.tls-handshake-timeout firefoxTLSHandshakeTimeout = 30 * time.Second // browserIdleConnTimeout 是 Firefox network.http.http2.timeout,空闲 HTTP/2 连接保留该时长后关闭 @@ -216,9 +219,7 @@ var browserManagedHeaders = map[string]struct{}{ type browserRoundTripper struct { transport *http2.Transport pool *firefoxConnPool - // http3 在直连时按 Alt-Svc 把请求切换到 HTTP/3,经代理时为 nil - http3 *firefoxHTTP3 - mu sync.Mutex + mu sync.Mutex // preflights 按 preflightKey 记录 CORS 预检缓存 preflights map[string]preflightGrant } @@ -248,7 +249,7 @@ type browserResponseBody struct { trailer stdhttp.Header } -// newBrowserRoundTripper 创建与当前 Camoufox 网络形状一致的传输:经代理时只用 HTTP/2,直连时按 Alt-Svc 改用 HTTP/3 +// newBrowserRoundTripper 创建与当前 Camoufox 网络形状一致的 HTTP/2 传输 func newBrowserRoundTripper(proxyURL string) (stdhttp.RoundTripper, error) { proxyURL = strings.TrimSpace(proxyURL) var proxyDialer proxy.ContextDialer = &net.Dialer{} @@ -295,11 +296,7 @@ func newBrowserRoundTripper(proxyURL string) (stdhttp.RoundTripper, error) { StreamWindowUpdate: firefoxStreamWindowUpdate, WindowUpdateThreshold: firefoxWindowUpdateThreshold, } - roundTripper := &browserRoundTripper{preflights: map[string]preflightGrant{}, transport: pool.transport, pool: pool} - if proxyURL == "" { - roundTripper.http3 = newFirefoxHTTP3(pool) - } - return roundTripper, nil + return &browserRoundTripper{preflights: map[string]preflightGrant{}, transport: pool.transport, pool: pool}, nil } // connectionTimingKey 是请求 context 中记录该请求自己建立连接各阶段时刻的 *connectionTiming @@ -354,7 +351,7 @@ func (dialer tracedDialer) DialContext(ctx context.Context, network, address str return conn, err } -// firefoxConnPool 按 Firefox 选择 HTTP/2 连接,匿名请求、携带凭据的请求与 HTTP/3 备用连接分开建连 +// firefoxConnPool 按 Firefox 选择 HTTP/2 连接,匿名请求与携带凭据的请求分开建连 type firefoxConnPool struct { transport *http2.Transport dial func(address string, anonymous bool, timing *connectionTiming) (net.Conn, error) @@ -368,14 +365,27 @@ type firefoxConnPool struct { added chan struct{} } +// requestClass 标记请求在连接池中的类别:匿名请求(CORS 预检)与携带凭据的请求使用不同连接 +type requestClass struct { + anonymous bool +} + +type requestClassKey struct{} + +func withRequestClass(ctx context.Context, class requestClass) context.Context { + return context.WithValue(ctx, requestClassKey{}, class) +} + +func requestClassOf(ctx context.Context) requestClass { + class, _ := ctx.Value(requestClassKey{}).(requestClass) + return class +} + // poolKey 是地址与请求类别组成的连接池键 func poolKey(address string, class requestClass) string { if class.anonymous { address += "|anonymous" } - if class.fallback { - address += "|fallback" - } return address } @@ -387,7 +397,7 @@ func (pool *firefoxConnPool) GetClientConn(request *fhttp.Request, address strin func (pool *firefoxConnPool) get(ctx context.Context, address string, dispatch bool) (*http2.ClientConn, error) { class := requestClassOf(ctx) key := poolKey(address, class) - inverted := poolKey(address, requestClass{anonymous: !class.anonymous, fallback: class.fallback}) + inverted := poolKey(address, requestClass{anonymous: !class.anonymous}) pool.mu.Lock() conn := pool.availableLocked(key, inverted, dispatch) for conn == nil && len(pool.conns[key])+pool.dialing[key] >= firefoxMaxPersistentConns { @@ -551,6 +561,26 @@ func (cache *firefoxSessionCache) take(peer firefoxSessionPeer) (*tls.ClientSess return nil, 0 } +// resumptionPSKLength 返回以编码为 raw(utls SessionState.Bytes)的 TLS 1.3 客户端会话恢复时 pre_shared_key 扩展的长度,会话不是 TLS 1.3 或票据已过期时为 0 +func resumptionPSKLength(raw []byte, ticket int) int { + if binary.BigEndian.Uint16(raw) != tls.VersionTLS13 || time.Now().After(time.Unix(int64(binary.BigEndian.Uint64(raw[len(raw)-12:])), 0)) { + return 0 + } + binder := 32 + if binary.BigEndian.Uint16(raw[3:]) == tls.TLS_AES_256_GCM_SHA384 { + binder = 48 + } + return 2 + 2 + ticket + 4 + 2 + 1 + binder +} + +// nssGREASEECHPayload 是 NSS 按内层基础长度 inner 与 pre_shared_key 扩展长度 psk 算出的 GREASE ECH 密文长度(不含 AEAD 标签),按 RFC 9849 补齐到 32 字节的整数倍 +func nssGREASEECHPayload(inner int, psk int) uint16 { + if psk > 0 { + inner += 4 + psk + } + return uint16(inner + 31 - (inner-1)%32) +} + // put 把服务器下发的票据存到 peer 名下,已满时丢弃最早收到的一张 func (cache *firefoxSessionCache) put(peer firefoxSessionPeer, session *tls.ClientSessionState) { cache.mu.Lock() @@ -655,21 +685,9 @@ func (transport *browserRoundTripper) RoundTrip(request *stdhttp.Request) (*stdh }, nil } -// roundTrip 执行上游请求,anonymous 表示不携带凭据的请求(CORS 预检与 CSP 报告):直连时先按 Alt-Svc 选择 HTTP/3,其余经 HTTP/2 发送并记录响应的 Alt-Svc +// roundTrip 经 HTTP/2 执行上游请求,anonymous 表示不携带凭据的请求(CORS 预检与 CSP 报告) func (transport *browserRoundTripper) roundTrip(request *fhttp.Request, anonymous bool) (*fhttp.Response, error) { - request = request.WithContext(withRequestClass(request.Context(), requestClass{anonymous: anonymous})) - if transport.http3 == nil { - return transport.roundTripHTTP2(request) - } - response, http2Request, err := transport.http3.send(request, anonymous) - if http2Request == nil { - return response, err - } - response, err = transport.roundTripHTTP2(http2Request) - if err == nil { - transport.http3.observe(request.URL.Hostname(), response.Header) - } - return response, err + return transport.roundTripHTTP2(request.WithContext(withRequestClass(request.Context(), requestClass{anonymous: anonymous}))) } // roundTripHTTP2 经 HTTP/2 以 Firefox 的 HEADERS 帧优先级执行上游请求,请求 context 结束时立即返回,未完成的拨号在后台结束 @@ -720,9 +738,6 @@ func (body *browserResponseBody) syncTrailer() { func (transport *browserRoundTripper) CloseIdleConnections() { transport.pool.shutdown() - if transport.http3 != nil { - transport.http3.shutdown() - } } func standardHeader(source fhttp.Header) stdhttp.Header { @@ -741,7 +756,7 @@ func preflightKey(request *stdhttp.Request) string { return request.URL.String() } -// ensurePreflight 为带 Origin 的跨域 cors 请求按 Firefox 的流程先发 CORS 预检:经 HTTP/2 时先在后台为实际请求的类别预先拨出连接,预检走匿名连接,预检响应允许的方法与请求头按 Access-Control-Max-Age(上限 24 小时)逐个缓存;no-cors 请求与只带安全头的 GET、HEAD、POST 不预检 +// ensurePreflight 为带 Origin 的跨域 cors 请求按 Firefox 的流程先发 CORS 预检:先在后台为实际请求的类别预先拨出连接,预检走匿名连接,预检响应允许的方法与请求头按 Access-Control-Max-Age(上限 24 小时)逐个缓存;no-cors 请求与只带安全头的 GET、HEAD、POST 不预检 func (transport *browserRoundTripper) ensurePreflight(request *stdhttp.Request) error { origin, err := url.Parse(request.Header.Get("Origin")) if request.Method == stdhttp.MethodOptions || request.Header.Get("Sec-Fetch-Mode") == "no-cors" || err != nil || origin.Host == "" || origin.Host == request.URL.Host { @@ -759,13 +774,11 @@ func (transport *browserRoundTripper) ensurePreflight(request *stdhttp.Request) if cached { return nil } - if transport.http3 == nil { - port := request.URL.Port() - if port == "" { - port = "443" - } - transport.pool.speculate(net.JoinHostPort(request.URL.Hostname(), port), requestClass{anonymous: requestClassOf(request.Context()).anonymous}) + port := request.URL.Port() + if port == "" { + port = "443" } + transport.pool.speculate(net.JoinHostPort(request.URL.Hostname(), port), requestClass{anonymous: requestClassOf(request.Context()).anonymous}) preflight, err := fhttp.NewRequestWithContext(request.Context(), stdhttp.MethodOptions, target, nil) if err != nil { return err @@ -914,7 +927,7 @@ func firefox158ClientHello(psk int) tls.ClientHelloID { typed.Curves = []tls.CurveID{tls.X25519MLKEM768, tls.X25519, tls.CurveP256, tls.CurveP384, tls.CurveP521} case *tls.GREASEEncryptedClientHelloExtension: typed.CandidateCipherSuites = []tls.HPKESymmetricCipherSuite{{KdfId: dicttls.HKDF_SHA256, AeadId: dicttls.AEAD_AES_128_GCM}, {KdfId: dicttls.HKDF_SHA256, AeadId: dicttls.AEAD_CHACHA20_POLY1305}} - typed.CandidatePayloadLens = []uint16{nssGREASEECHPayload(nssTLSGREASEECHInner, psk, false)} + typed.CandidatePayloadLens = []uint16{nssGREASEECHPayload(nssTLSGREASEECHInner, psk)} } } if psk > 0 { diff --git a/internal/aistudio/transport_http3.go b/internal/aistudio/transport_http3.go deleted file mode 100644 index efaab393..00000000 --- a/internal/aistudio/transport_http3.go +++ /dev/null @@ -1,458 +0,0 @@ -package aistudio - -import ( - "context" - "errors" - "net" - "strconv" - "strings" - "sync" - "time" - - fhttp "github.com/Mag1cFall/fhttp" - quic "github.com/Mag1cFall/uquic" -) - -const ( - h3FrameData = 0x00 - h3FrameHeaders = 0x01 - h3FrameSettings = 0x04 - h3FrameGoaway = 0x07 - // h3ControlStreamType 是 HTTP/3 控制流的单向流类型 - h3ControlStreamType = 0x00 - // h3StreamCreationError 是 H3_STREAM_CREATION_ERROR,neqo 以它拒绝推送流与未知类型的单向流 - h3StreamCreationError = 0x103 - // h3RequestCancelled 是 H3_REQUEST_CANCELLED,Firefox 取消请求时以它重置请求流 - h3RequestCancelled = 0x10c - // h3SettingQPACKCapacity 是 SETTINGS_QPACK_MAX_TABLE_CAPACITY - h3SettingQPACKCapacity = 0x01 - // h3SettingQPACKBlocked 是 SETTINGS_QPACK_BLOCKED_STREAMS - h3SettingQPACKBlocked = 0x07 - // firefoxHTTP3BackupDelay 是 Firefox network.http.http3.backup_timer_delay:HTTP/3 连接未就绪时开始备用 HTTP/2 连接的延迟 - firefoxHTTP3BackupDelay = 100 * time.Millisecond - // altSvcDefaultMaxAge 是 Alt-Svc 未带 ma 参数时的映射有效期 - altSvcDefaultMaxAge = 24 * time.Hour - // h3RequestBodyChunk 是请求体写成 DATA 帧时每帧的最大长度 - h3RequestBodyChunk = 32 << 10 -) - -// firefoxHTTP3Settings 是 Firefox 158 控制流 SETTINGS 的参数与顺序:QPACK 容量、阻塞流、WebTransport、两个 H3 DATAGRAM 与扩展 CONNECT -var firefoxHTTP3Settings = [][2]uint64{ - {h3SettingQPACKCapacity, firefoxQPACKTableSize}, - {h3SettingQPACKBlocked, firefoxQPACKBlockedStreams}, - {0x2b603742, 0}, - {0xffd277, 1}, - {0x33, 1}, - {0x08, 1}, -} - -// firefoxSafeMethods 是 Firefox nsHttpRequestHead::IsSafeMethod 认定的方法,0-RTT 连接只在握手完成前发送这些方法的请求 -var firefoxSafeMethods = map[string]bool{"GET": true, "HEAD": true, "OPTIONS": true, "TRACE": true, "PROPFIND": true, "REPORT": true, "SEARCH": true} - -// h3ExcludedRequestHeaders 是 Firefox 为 HTTP/3 请求丢弃的逐跳与连接头 -var h3ExcludedRequestHeaders = map[string]struct{}{ - "connection": {}, "host": {}, "keep-alive": {}, "proxy-connection": {}, "te": {}, "transfer-encoding": {}, "upgrade": {}, -} - -// requestClass 标记请求在连接池中的类别:匿名请求(CORS 预检)与携带凭据的请求使用不同连接,备用连接只供 HTTP/3 未就绪时回退 -type requestClass struct { - anonymous bool - fallback bool -} - -type requestClassKey struct{} - -func withRequestClass(ctx context.Context, class requestClass) context.Context { - return context.WithValue(ctx, requestClassKey{}, class) -} - -func requestClassOf(ctx context.Context) requestClass { - class, _ := ctx.Value(requestClassKey{}).(requestClass) - return class -} - -// firefoxHTTP3 按 Firefox 158 的 Alt-Svc 流程在直连时把请求切换到 HTTP/3:响应通告 h3 后以携带凭据的连接验证,验证成功后该主机的请求改走 HTTP/3 -type firefoxHTTP3 struct { - pool *firefoxConnPool - mu sync.Mutex - origins map[string]*altSvcOrigin - // resumption 按是否匿名分别保存会话票据、令牌与服务端设置 - resumption [2]*quicResumption -} - -// altSvcOrigin 是一个主机的 Alt-Svc h3 映射,classes 按是否匿名分别保存 HTTP/3 连接 -type altSvcOrigin struct { - validated bool - validation *http3Dial - expires time.Time - classes [2]http3Class -} - -// http3Class 是主机上一类请求的 HTTP/3 状态,excluded 表示备用 HTTP/2 连接抢先就绪后不再尝试 HTTP/3 -type http3Class struct { - conn *http3Conn - dial *http3Dial - excluded bool -} - -// http3Dial 是进行中的 QUIC 拨号 -type http3Dial struct { - done chan struct{} - conn *http3Conn - err error - cancel context.CancelFunc -} - -func newFirefoxHTTP3(pool *firefoxConnPool) *firefoxHTTP3 { - return &firefoxHTTP3{pool: pool, origins: map[string]*altSvcOrigin{}, resumption: [2]*quicResumption{newQUICResumption(), newQUICResumption()}} -} - -func classIndex(anonymous bool) int { - if anonymous { - return 1 - } - return 0 -} - -// observe 处理响应的 Alt-Svc:主机首次通告 h3 时开始验证连接,已验证的映射按 ma 续期,clear 删除映射 -func (h3 *firefoxHTTP3) observe(host string, header fhttp.Header) { - maxAge, clear, ok := parseAltSvcH3(header.Values("Alt-Svc"), host) - if !ok && !clear { - return - } - h3.mu.Lock() - defer h3.mu.Unlock() - origin := h3.origins[host] - if clear { - delete(h3.origins, host) - return - } - if origin != nil && origin.validated && time.Now().Before(origin.expires) { - origin.expires = time.Now().Add(maxAge) - return - } - if origin != nil && origin.validation != nil { - return - } - if origin == nil { - origin = &altSvcOrigin{} - h3.origins[host] = origin - } - origin.validated = false - validation := h3.startDial(host, false) - origin.validation = validation - go func() { - <-validation.done - h3.mu.Lock() - defer h3.mu.Unlock() - if h3.origins[host] != origin { - if validation.conn != nil { - validation.conn.close() - } - return - } - origin.validation = nil - if validation.err != nil { - delete(h3.origins, host) - return - } - origin.validated = true - origin.expires = time.Now().Add(maxAge) - h3.adopt(host, origin, false, validation.conn) - h3.pool.retire(net.JoinHostPort(host, "443"), requestClass{}) - }() -} - -// parseAltSvcH3 返回 Alt-Svc 中第一个指向同主机 443 端口的 h3 条目的有效期,clear 表示清除映射 -func parseAltSvcH3(values []string, host string) (time.Duration, bool, bool) { - for _, value := range values { - if strings.TrimSpace(value) == "clear" { - return 0, true, false - } - for _, entry := range strings.Split(value, ",") { - parameters := strings.Split(entry, ";") - protocol, authority, found := strings.Cut(strings.TrimSpace(parameters[0]), "=") - if !found || protocol != "h3" { - continue - } - alternate, port, err := net.SplitHostPort(strings.Trim(authority, `"`)) - if err != nil || port != "443" || (alternate != "" && alternate != host) { - continue - } - maxAge := altSvcDefaultMaxAge - for _, parameter := range parameters[1:] { - name, raw, _ := strings.Cut(strings.TrimSpace(parameter), "=") - if seconds, err := strconv.ParseUint(strings.Trim(raw, `"`), 10, 32); err == nil && strings.EqualFold(name, "ma") { - maxAge = time.Duration(seconds) * time.Second - } - } - return maxAge, false, true - } - } - return 0, false, false -} - -// startDial 在后台以 Firefox 的 QUIC 首航拨号并建立 HTTP/3 会话,该类别有会话票据时恢复会话 -func (h3 *firefoxHTTP3) startDial(host string, anonymous bool) *http3Dial { - ctx, cancel := context.WithCancel(context.Background()) - dial := &http3Dial{done: make(chan struct{}), cancel: cancel} - resumption := h3.resumption[classIndex(anonymous)] - go func() { - defer close(dial.done) - connection, settings, err := dialFirefoxQUIC(ctx, host, resumption) - if err != nil { - dial.err = err - return - } - dial.conn, dial.err = newHTTP3Conn(connection, settings, func(capacity uint64, blocked uint64) { - resumption.rememberSettings(host, capacity, blocked) - }) - }() - return dial -} - -// adopt 把建立好的连接放进主机对应类别,连接关闭或收到 GOAWAY 后自动移出 -func (h3 *firefoxHTTP3) adopt(host string, origin *altSvcOrigin, anonymous bool, conn *http3Conn) { - origin.classes[classIndex(anonymous)].conn = conn - go func() { - select { - case <-conn.quic.Context().Done(): - case <-conn.goaway: - } - h3.mu.Lock() - defer h3.mu.Unlock() - if class := &origin.classes[classIndex(anonymous)]; class.conn == conn { - class.conn = nil - } - }() -} - -// send 按 Firefox 的连接选择发送请求:返回 HTTP/3 响应,或返回改走 HTTP/2 的请求(备用连接已在请求 context 中标记) -func (h3 *firefoxHTTP3) send(request *fhttp.Request, anonymous bool) (*fhttp.Response, *fhttp.Request, error) { - if request.URL.Scheme != "https" || (request.URL.Port() != "" && request.URL.Port() != "443") { - return nil, request, nil - } - host := request.URL.Hostname() - for attempt := 0; ; attempt++ { - conn, mapped, fallback, err := h3.route(request, host, anonymous) - if err != nil { - return nil, nil, err - } - if conn == nil { - if fallback { - request = request.WithContext(withRequestClass(request.Context(), requestClass{anonymous: anonymous, fallback: true})) - } - return nil, request, nil - } - reused := conn.streams.Load() > 0 - response, opened, err := conn.roundTrip(request, h3RequestFields(request, mapped && !anonymous)) - if err != nil && attempt == 0 && request.Context().Err() == nil && (!opened || quicConnectionClosed(err) && (reused || firefoxSafeMethods[request.Method])) { - if restarted, ok := restartRequest(request, opened); ok { - conn.close() - h3.drop(host, anonymous, conn) - request = restarted - continue - } - } - if err == nil { - h3.observe(host, response.Header) - } - return response, nil, err - } -} - -// quicConnectionClosed 判断请求失败是否因为 QUIC 连接关闭,请求流被单独重置时为 false -func quicConnectionClosed(err error) bool { - var application *quic.ApplicationError - var transport *quic.TransportError - var idle *quic.IdleTimeoutError - var reset *quic.StatelessResetError - return errors.As(err, &application) || errors.As(err, &transport) || errors.As(err, &idle) || errors.As(err, &reset) -} - -// restartRequest 返回在新连接上重发的请求,请求流已打开时以 GetBody 重新取得请求体,取不到时 ok 为 false -func restartRequest(request *fhttp.Request, opened bool) (*fhttp.Request, bool) { - if !opened || request.Body == nil || request.Body == fhttp.NoBody { - return request, true - } - if request.GetBody == nil { - return nil, false - } - body, err := request.GetBody() - if err != nil { - return nil, false - } - restarted := request.Clone(request.Context()) - restarted.Body = body - return restarted, true -} - -// drop 把失败的连接从主机对应类别移出,使重发的请求改用新连接 -func (h3 *firefoxHTTP3) drop(host string, anonymous bool, conn *http3Conn) { - h3.mu.Lock() - defer h3.mu.Unlock() - if origin := h3.origins[host]; origin != nil && origin.classes[classIndex(anonymous)].conn == conn { - origin.classes[classIndex(anonymous)].conn = nil - } -} - -// route 选择请求的承载:返回可用的 HTTP/3 连接与是否经 Alt-Svc 映射发送,conn 为 nil 时经 HTTP/2,fallback 表示使用备用连接 -func (h3 *firefoxHTTP3) route(request *fhttp.Request, host string, anonymous bool) (conn *http3Conn, mapped bool, fallback bool, err error) { - ctx := request.Context() - address := net.JoinHostPort(host, "443") - h3.mu.Lock() - origin := h3.origins[host] - if origin == nil { - h3.mu.Unlock() - return nil, false, false, nil - } - if !origin.validated { - validation := origin.validation - h3.mu.Unlock() - if anonymous || validation == nil { - return nil, false, false, nil - } - ready := h3.http2Ready(ctx, address, requestClass{}) - for { - select { - case <-validation.done: - if validation.err == nil { - return validation.conn, false, false, nil - } - return nil, false, false, nil - case err := <-ready: - if err == nil { - return nil, false, false, nil - } - ready = nil - case <-ctx.Done(): - return nil, false, false, ctx.Err() - } - } - } - if time.Now().After(origin.expires) { - delete(h3.origins, host) - h3.mu.Unlock() - return nil, false, false, nil - } - class := &origin.classes[classIndex(anonymous)] - if class.excluded { - h3.mu.Unlock() - return nil, false, false, nil - } - if class.conn != nil { - conn = class.conn - h3.mu.Unlock() - return conn, true, false, nil - } - dial := class.dial - if dial == nil { - dial = h3.startDial(host, anonymous) - class.dial = dial - go h3.finishDial(host, origin, anonymous, dial) - } - idle := h3.pool.idle(address, requestClass{anonymous: anonymous}) - h3.mu.Unlock() - if idle { - return nil, false, false, nil - } - timer := time.NewTimer(firefoxHTTP3BackupDelay) - defer timer.Stop() - var backup <-chan error - var backupErr error - for { - select { - case <-dial.done: - if dial.err == nil { - return dial.conn, true, false, nil - } - if backupErr != nil { - return nil, false, false, errors.Join(dial.err, backupErr) - } - if backup == nil { - return nil, false, false, nil - } - select { - case err := <-backup: - if err != nil { - return nil, false, false, errors.Join(dial.err, err) - } - case <-ctx.Done(): - return nil, false, false, ctx.Err() - } - return nil, false, true, nil - case <-timer.C: - backup = h3.http2Ready(ctx, address, requestClass{anonymous: anonymous, fallback: true}) - case backupErr = <-backup: - if backupErr != nil { - backup = nil - continue - } - select { - case <-dial.done: - if dial.err == nil { - return dial.conn, true, false, nil - } - default: - h3.mu.Lock() - class.excluded = true - h3.mu.Unlock() - dial.cancel() - } - return nil, false, true, nil - case <-ctx.Done(): - return nil, false, false, ctx.Err() - } - } -} - -// finishDial 在拨号结束后把连接交给主机类别,拨号失败时该类别不再尝试 HTTP/3,类别已改走 HTTP/2 时以 H3_NO_ERROR 关闭连接 -func (h3 *firefoxHTTP3) finishDial(host string, origin *altSvcOrigin, anonymous bool, dial *http3Dial) { - <-dial.done - h3.mu.Lock() - defer h3.mu.Unlock() - class := &origin.classes[classIndex(anonymous)] - if class.dial == dial { - class.dial = nil - } - if dial.conn == nil { - class.excluded = true - return - } - if class.excluded || h3.origins[host] != origin { - dial.conn.close() - return - } - h3.adopt(host, origin, anonymous, dial.conn) -} - -// http2Ready 在后台取得一条该类别的 HTTP/2 连接,返回的 channel 交付取得结果 -func (h3 *firefoxHTTP3) http2Ready(ctx context.Context, address string, class requestClass) <-chan error { - ready := make(chan error, 1) - go func() { - _, err := h3.pool.get(withRequestClass(context.WithoutCancel(ctx), class), address, false) - ready <- err - }() - return ready -} - -// shutdown 关闭全部 HTTP/3 连接与进行中的拨号,Alt-Svc 映射保留 -func (h3 *firefoxHTTP3) shutdown() { - h3.mu.Lock() - defer h3.mu.Unlock() - for _, origin := range h3.origins { - if origin.validation != nil { - origin.validation.cancel() - } - for i := range origin.classes { - class := &origin.classes[i] - if class.dial != nil { - class.dial.cancel() - } - if class.conn != nil { - class.conn.close() - class.conn = nil - } - } - } -} diff --git a/internal/aistudio/transport_http3_conn.go b/internal/aistudio/transport_http3_conn.go deleted file mode 100644 index 6094c8b9..00000000 --- a/internal/aistudio/transport_http3_conn.go +++ /dev/null @@ -1,259 +0,0 @@ -package aistudio - -import ( - "bufio" - "context" - "crypto/rand" - "encoding/binary" - "fmt" - "io" - "sync" - "sync/atomic" - "time" - - quic "github.com/Mag1cFall/uquic" - "github.com/Mag1cFall/uquic/quicvarint" -) - -// http3Conn 是一条按 neqo 0.31 行为收发的 HTTP/3 连接 -type http3Conn struct { - quic *quic.Conn - // mu 保护 QPACK 编码器,并保证编码器流指令先于引用它的头块写出 - mu sync.Mutex - encoder *qpackEncoder - encoderStream *quic.SendStream - decoder *qpackDecoder - decoderMu sync.Mutex - decoderStream *quic.SendStream - ackPending atomic.Bool - goaway chan struct{} - goawayOnce sync.Once - // ready 在握手完成后关闭,0-RTT 被拒绝时在重新打开控制流与 QPACK 流之后关闭 - ready chan struct{} - // remember 记录服务端 SETTINGS 中的 QPACK 设置,供下次 0-RTT 沿用 - remember func(capacity uint64, blocked uint64) - // streams 是本连接已打开的请求流数 - streams atomic.Int64 -} - -// newHTTP3Conn 在 QUIC 连接上按 neqo 的顺序打开控制流、QPACK 编码器流与解码器流;settings 非 nil 表示握手尚未完成的 0-RTT 连接 -func newHTTP3Conn(connection *quic.Conn, settings *[2]uint64, remember func(capacity uint64, blocked uint64)) (*http3Conn, error) { - conn := &http3Conn{quic: connection, goaway: make(chan struct{}), ready: make(chan struct{}), remember: remember} - if err := conn.open(settings); err != nil { - conn.close() - return nil, err - } - if settings == nil { - close(conn.ready) - } else { - go conn.awaitHandshake() - } - return conn, nil -} - -// open 打开控制流、QPACK 编码器流与解码器流并写出各流开头,编码器与解码器从初始状态开始 -func (conn *http3Conn) open(settings *[2]uint64) error { - encoder, decoder := newQPACKEncoder(), newQPACKDecoder() - if settings != nil { - encoder.setPeerSettings(settings[0], settings[1]) - } - var encoderStream, decoderStream *quic.SendStream - control, err := conn.quic.OpenUniStream() - if err == nil { - _, err = control.Write(firefoxControlStream()) - } - if err == nil { - encoderStream, err = conn.quic.OpenUniStream() - } - if err == nil { - decoderStream, err = conn.quic.OpenUniStream() - } - if err != nil { - return err - } - conn.mu.Lock() - conn.encoder, conn.encoderStream = encoder, encoderStream - err = conn.flushEncoderLocked() - conn.mu.Unlock() - conn.decoderMu.Lock() - conn.decoder, conn.decoderStream = decoder, decoderStream - conn.decoderMu.Unlock() - if err == nil { - err = conn.flushDecoder() - } - if err == nil { - go conn.acceptUniStreams(encoder, decoder) - } - return err -} - -// awaitHandshake 等 0-RTT 连接握手完成;服务端拒绝 0-RTT 时按 neqo 重新打开控制流与 QPACK 流,编码器等待新的服务端 SETTINGS -func (conn *http3Conn) awaitHandshake() { - defer close(conn.ready) - select { - case <-conn.quic.HandshakeComplete(): - case <-conn.quic.Context().Done(): - return - } - if conn.quic.ConnectionState().Used0RTT { - return - } - if _, err := conn.quic.NextConnection(conn.quic.Context()); err != nil || conn.open(nil) != nil { - conn.close() - } -} - -// awaitReady 等待连接握手完成 -func (conn *http3Conn) awaitReady(ctx context.Context) error { - select { - case <-conn.ready: - return nil - case <-ctx.Done(): - return ctx.Err() - } -} - -// firefoxControlStream 返回控制流开头的流类型、SETTINGS 与一个随机 GREASE 帧 -func firefoxControlStream() []byte { - var settings []byte - for _, setting := range firefoxHTTP3Settings { - settings = quicvarint.Append(settings, setting[0]) - settings = quicvarint.Append(settings, setting[1]) - } - stream := quicvarint.Append(nil, h3ControlStreamType) - stream = appendH3Frame(stream, h3FrameSettings, settings) - var random [8]byte - _, _ = rand.Read(random[:]) - greaseType := (binary.NativeEndian.Uint64(random[:])>>7)*0x1f + 0x21 - return appendH3Frame(stream, greaseType, random[1:1+random[0]&0x7]) -} - -func appendH3Frame(buffer []byte, kind uint64, payload []byte) []byte { - buffer = quicvarint.Append(buffer, kind) - buffer = quicvarint.Append(buffer, uint64(len(payload))) - return append(buffer, payload...) -} - -func (conn *http3Conn) flushEncoderLocked() error { - if len(conn.encoder.pending) == 0 { - return nil - } - _, err := conn.encoderStream.Write(conn.encoder.pending) - conn.encoder.pending = nil - return err -} - -func (conn *http3Conn) flushDecoder() error { - conn.decoderMu.Lock() - defer conn.decoderMu.Unlock() - if pending := conn.decoder.takePending(); len(pending) > 0 { - _, err := conn.decoderStream.Write(pending) - return err - } - return nil -} - -// scheduleDecoderFlush 在本批编码器指令处理完的 1 毫秒后写出 Insert Count Increment,期间解码的头块先写出 Section Acknowledgment -func (conn *http3Conn) scheduleDecoderFlush() { - if conn.ackPending.CompareAndSwap(false, true) { - time.AfterFunc(time.Millisecond, func() { - conn.ackPending.Store(false) - _ = conn.flushDecoder() - }) - } -} - -// acceptUniStreams 处理服务端打开的单向流:控制流、QPACK 编码器流与解码器流,其余类型以 H3_STREAM_CREATION_ERROR 停止接收 -func (conn *http3Conn) acceptUniStreams(encoder *qpackEncoder, decoder *qpackDecoder) { - for { - stream, err := conn.quic.AcceptUniStream(conn.quic.Context()) - if err != nil { - return - } - go func() { - reader := bufio.NewReader(stream) - kind, err := quicvarint.Read(reader) - if err != nil { - return - } - switch kind { - case h3ControlStreamType: - err = conn.readControlStream(reader, encoder) - case qpackEncoderStreamType: - err = decoder.readEncoderStream(reader, conn.scheduleDecoderFlush) - case qpackDecoderStreamType: - err = encoder.readDecoderStream(reader, &conn.mu) - default: - stream.CancelRead(h3StreamCreationError) - return - } - if err != nil && conn.quic.Context().Err() == nil { - _ = conn.quic.CloseWithError(0x0104, "") - } - }() - } -} - -// readControlStream 读取服务端控制流:SETTINGS 设置并记录 QPACK 编码器参数,GOAWAY 后不再在本连接发起请求 -func (conn *http3Conn) readControlStream(reader *bufio.Reader, encoder *qpackEncoder) error { - for first := true; ; first = false { - kind, payload, err := readH3Frame(reader) - if err != nil { - return err - } - switch { - case kind == h3FrameSettings && first: - capacity, blocked := uint64(0), uint64(0) - for len(payload) > 0 { - id, n, err := quicvarint.Parse(payload) - if err != nil { - return err - } - value, m, err := quicvarint.Parse(payload[n:]) - if err != nil { - return err - } - payload = payload[n+m:] - switch id { - case h3SettingQPACKCapacity: - capacity = value - case h3SettingQPACKBlocked: - blocked = value - } - } - conn.remember(capacity, blocked) - conn.mu.Lock() - encoder.setPeerSettings(capacity, blocked) - err = conn.flushEncoderLocked() - conn.mu.Unlock() - if err != nil { - return err - } - case first: - return fmt.Errorf("HTTP/3 控制流首帧不是 SETTINGS") - case kind == h3FrameGoaway: - conn.goawayOnce.Do(func() { close(conn.goaway) }) - } - } -} - -func readH3Frame(reader *bufio.Reader) (uint64, []byte, error) { - kind, err := quicvarint.Read(reader) - if err != nil { - return 0, nil, err - } - length, err := quicvarint.Read(reader) - if err != nil { - return 0, nil, err - } - if length > 16<<20 { - return 0, nil, fmt.Errorf("HTTP/3 帧长度 %d 超出上限", length) - } - payload := make([]byte, length) - _, err = io.ReadFull(reader, payload) - return kind, payload, err -} - -func (conn *http3Conn) close() { - _ = conn.quic.CloseWithError(h3NoError, "") -} diff --git a/internal/aistudio/transport_http3_stream.go b/internal/aistudio/transport_http3_stream.go deleted file mode 100644 index e9681982..00000000 --- a/internal/aistudio/transport_http3_stream.go +++ /dev/null @@ -1,296 +0,0 @@ -package aistudio - -import ( - "bufio" - "context" - "errors" - "fmt" - "io" - "strconv" - "strings" - "sync/atomic" - - fhttp "github.com/Mag1cFall/fhttp" - quic "github.com/Mag1cFall/uquic" - "github.com/Mag1cFall/uquic/quicvarint" -) - -// h3RequestFields 按 Firefox 的 HTTP/3 请求头生成 QPACK 字段,altUsed 时写入 Alt-Used -func h3RequestFields(request *fhttp.Request, altUsed bool) []qpackField { - authority := request.Host - if authority == "" { - authority = request.URL.Host - } - fields := []qpackField{ - {":method", request.Method}, - {":scheme", request.URL.Scheme}, - {":authority", authority}, - {":path", request.URL.RequestURI()}, - } - header := request.Header.Clone() - delete(header, fhttp.HeaderOrderKey) - if altUsed && header.Get("Sec-Fetch-Dest") != "image" { - header.Set("Alt-Used", request.URL.Hostname()) - } - if length := request.ContentLength; length > 0 || (length == 0 && (request.Method == "POST" || request.Method == "PUT" || request.Method == "PATCH")) { - header.Set("Content-Length", strconv.FormatInt(length, 10)) - } - for _, name := range orderedHeaderNames(header, request.URL.Host) { - if _, excluded := h3ExcludedRequestHeaders[name]; excluded { - continue - } - values := header.Values(name) - if len(values) == 0 { - continue - } - separator := ", " - if name == "cookie" { - separator = "; " - } - fields = append(fields, qpackField{name, strings.Join(values, separator)}) - } - return fields -} - -// roundTrip 在本连接上发送请求并返回响应头已到达的响应,opened 表示请求流已经打开;0-RTT 被拒绝的请求在握手完成后重发一次 -func (conn *http3Conn) roundTrip(request *fhttp.Request, fields []qpackField) (*fhttp.Response, bool, error) { - if !firefoxSafeMethods[request.Method] { - if err := conn.awaitReady(request.Context()); err != nil { - return nil, false, err - } - } - response, opened, err := conn.exchange(request, fields) - if errors.Is(err, quic.Err0RTTRejected) { - if err := conn.awaitReady(request.Context()); err != nil { - return nil, false, err - } - return conn.exchange(request, fields) - } - return response, opened, err -} - -// exchange 打开请求流写出请求并读取响应头,0-RTT 被拒绝的请求流随 0-RTT 数据一起作废,不再重置 -func (conn *http3Conn) exchange(request *fhttp.Request, fields []qpackField) (*fhttp.Response, bool, error) { - ctx := request.Context() - stream, err := conn.quic.OpenStreamSync(ctx) - if err != nil { - return nil, false, err - } - conn.streams.Add(1) - id := int64(stream.StreamID()) - conn.mu.Lock() - block := conn.encoder.encode(fields, id) - err = conn.flushEncoderLocked() - conn.mu.Unlock() - if err == nil { - _, err = stream.Write(appendH3Frame(nil, h3FrameHeaders, block)) - } - if err != nil { - if !errors.Is(err, quic.Err0RTTRejected) { - stream.CancelWrite(h3RequestCancelled) - stream.CancelRead(h3RequestCancelled) - } - return nil, true, err - } - conn.decoderMu.Lock() - body := &http3Body{conn: conn, decoder: conn.decoder, stream: stream, reader: bufio.NewReader(stream)} - conn.decoderMu.Unlock() - body.stopWatch = context.AfterFunc(ctx, body.abort) - if request.Body != nil && request.Body != fhttp.NoBody { - go body.writeRequest(request.Body, request.ContentLength) - } else { - _ = stream.Close() - } - response, err := body.readResponse(ctx, request) - if err != nil { - body.stopWatch() - if !errors.Is(err, quic.Err0RTTRejected) { - body.abort() - } - if ctx.Err() != nil { - err = ctx.Err() - } - return nil, true, err - } - return response, true, nil -} - -// http3Body 是请求流上的响应体,按帧读取 DATA,结尾的 HEADERS 写入 trailer -type http3Body struct { - conn *http3Conn - decoder *qpackDecoder - stream *quic.Stream - reader *bufio.Reader - response *fhttp.Response - remaining uint64 - done bool - aborted atomic.Bool - stopWatch func() bool -} - -// h3DataHeaderMax 是 DATA 帧头的最大长度:1 字节帧类型与 4 字节帧长 -const h3DataHeaderMax = 5 - -// writeRequest 把 source 每次读到的正文写成一个 DATA 帧:读取缓冲前预留帧头的位置,帧头紧贴正文写入后以一次 Write 发出整帧;length 为已知的正文长度,小于每帧上限时缓冲只取这么长 -func (body *http3Body) writeRequest(source io.ReadCloser, length int64) { - defer source.Close() - size := h3RequestBodyChunk - if length > 0 && length < int64(size) { - size = int(length) - } - buffer := make([]byte, h3DataHeaderMax+size) - for { - read, err := source.Read(buffer[h3DataHeaderMax:]) - if read > 0 { - start := h3DataHeaderMax - quicvarint.Len(h3FrameData) - quicvarint.Len(uint64(read)) - quicvarint.Append(quicvarint.Append(buffer[start:start], h3FrameData), uint64(read)) - if _, werr := body.stream.Write(buffer[start : h3DataHeaderMax+read]); werr != nil { - return - } - } - if err == io.EOF { - _ = body.stream.Close() - return - } - if err != nil { - body.stream.CancelWrite(h3RequestCancelled) - return - } - } -} - -func (body *http3Body) readResponse(ctx context.Context, request *fhttp.Request) (*fhttp.Response, error) { - for { - kind, payload, err := readH3Frame(body.reader) - if err != nil { - return nil, err - } - if kind != h3FrameHeaders { - if kind == h3FrameData { - return nil, fmt.Errorf("HTTP/3 响应在 HEADERS 之前收到 DATA") - } - continue - } - fields, err := body.decoder.decode(ctx, payload, int64(body.stream.StreamID())) - if err != nil { - return nil, err - } - _ = body.conn.flushDecoder() - header := make(fhttp.Header, len(fields)) - status := 0 - for _, field := range fields { - if field.name == ":status" { - status, _ = strconv.Atoi(field.value) - continue - } - header.Add(field.name, field.value) - } - if status < 100 || status > 599 { - return nil, fmt.Errorf("HTTP/3 响应状态 %d 无效", status) - } - if status < 200 { - continue - } - response := &fhttp.Response{ - Status: strconv.Itoa(status) + " " + fhttp.StatusText(status), - StatusCode: status, - Proto: "HTTP/3.0", - ProtoMajor: 3, - Header: header, - ContentLength: -1, - Trailer: fhttp.Header{}, - Request: request, - Body: body, - } - if length, err := strconv.ParseInt(header.Get("Content-Length"), 10, 64); err == nil { - response.ContentLength = length - } - body.response = response - if request.Method != "HEAD" { - response.Body = fhttp.DecompressBody(response) - } - return response, nil - } -} - -func (body *http3Body) Read(buffer []byte) (int, error) { - for { - if body.done { - return 0, io.EOF - } - if body.remaining > 0 { - read, err := body.reader.Read(buffer[:min(uint64(len(buffer)), body.remaining)]) - body.remaining -= uint64(read) - if err == io.EOF { - err = nil - if body.remaining > 0 { - err = io.ErrUnexpectedEOF - } - } - return read, body.readError(err) - } - kind, err := quicvarint.Read(body.reader) - if err == io.EOF { - body.done = true - body.stopWatch() - return 0, io.EOF - } - if err != nil { - return 0, body.readError(err) - } - length, err := quicvarint.Read(body.reader) - if err != nil { - return 0, body.readError(err) - } - switch kind { - case h3FrameData: - body.remaining = length - case h3FrameHeaders: - payload := make([]byte, length) - if _, err := io.ReadFull(body.reader, payload); err != nil { - return 0, body.readError(err) - } - fields, err := body.decoder.decode(context.Background(), payload, int64(body.stream.StreamID())) - if err != nil { - return 0, err - } - _ = body.conn.flushDecoder() - for _, field := range fields { - body.response.Trailer.Add(field.name, field.value) - } - default: - if _, err := body.reader.Discard(int(length)); err != nil { - return 0, body.readError(err) - } - } - } -} - -func (body *http3Body) readError(err error) error { - if err != nil && body.aborted.Load() { - if ctxErr := body.response.Request.Context().Err(); ctxErr != nil { - return ctxErr - } - return errors.New("HTTP/3 响应体已关闭") - } - return err -} - -func (body *http3Body) Close() error { - body.stopWatch() - if !body.done { - body.abort() - } - return nil -} - -// abort 按 Firefox 取消请求:以 H3_REQUEST_CANCELLED 重置请求流并在解码器流写出 Stream Cancellation -func (body *http3Body) abort() { - if !body.aborted.CompareAndSwap(false, true) { - return - } - body.stream.CancelWrite(h3RequestCancelled) - body.stream.CancelRead(h3RequestCancelled) - body.decoder.cancel(int64(body.stream.StreamID())) - _ = body.conn.flushDecoder() -} diff --git a/internal/aistudio/transport_qpack.go b/internal/aistudio/transport_qpack.go deleted file mode 100644 index a1767d78..00000000 --- a/internal/aistudio/transport_qpack.go +++ /dev/null @@ -1,813 +0,0 @@ -package aistudio - -import ( - "bufio" - "context" - "errors" - "fmt" - "io" - "math" - "sync" - - "golang.org/x/net/http2/hpack" -) - -const ( - // qpackEncoderStreamType 是 QPACK 编码器单向流的类型 - qpackEncoderStreamType = 0x02 - // qpackDecoderStreamType 是 QPACK 解码器单向流的类型 - qpackDecoderStreamType = 0x03 - // firefoxQPACKTableSize 是 Firefox network.http.http3.default_qpack_table_size,编码与解码两侧共用 - firefoxQPACKTableSize = 65536 - // firefoxQPACKBlockedStreams 是 Firefox network.http.http3.default_max_stream_blocked - firefoxQPACKBlockedStreams = 20 - // qpackEntryOverhead 是 QPACK 动态表条目在名与值之外计入的字节数 - qpackEntryOverhead = 32 - // qpackDecoderMaxEntries 是 RFC 9204 3.2.2 按本端声明的动态表容量算出的最大条目数 - qpackDecoderMaxEntries uint64 = firefoxQPACKTableSize / qpackEntryOverhead -) - -// qpackField 是一条 QPACK 头字段 -type qpackField struct { - name string - value string -} - -// qpackStaticTable 是 RFC 9204 附录 A 的 QPACK 静态表 -var qpackStaticTable = [...]qpackField{ - {":authority", ""}, {":path", "/"}, {"age", "0"}, {"content-disposition", ""}, {"content-length", "0"}, {"cookie", ""}, {"date", ""}, {"etag", ""}, - {"if-modified-since", ""}, {"if-none-match", ""}, {"last-modified", ""}, {"link", ""}, {"location", ""}, {"referer", ""}, {"set-cookie", ""}, - {":method", "CONNECT"}, {":method", "DELETE"}, {":method", "GET"}, {":method", "HEAD"}, {":method", "OPTIONS"}, {":method", "POST"}, {":method", "PUT"}, - {":scheme", "http"}, {":scheme", "https"}, {":status", "103"}, {":status", "200"}, {":status", "304"}, {":status", "404"}, {":status", "503"}, - {"accept", "*/*"}, {"accept", "application/dns-message"}, {"accept-encoding", "gzip, deflate, br"}, {"accept-ranges", "bytes"}, - {"access-control-allow-headers", "cache-control"}, {"access-control-allow-headers", "content-type"}, {"access-control-allow-origin", "*"}, - {"cache-control", "max-age=0"}, {"cache-control", "max-age=2592000"}, {"cache-control", "max-age=604800"}, {"cache-control", "no-cache"}, - {"cache-control", "no-store"}, {"cache-control", "public, max-age=31536000"}, {"content-encoding", "br"}, {"content-encoding", "gzip"}, - {"content-type", "application/dns-message"}, {"content-type", "application/javascript"}, {"content-type", "application/json"}, - {"content-type", "application/x-www-form-urlencoded"}, {"content-type", "image/gif"}, {"content-type", "image/jpeg"}, {"content-type", "image/png"}, - {"content-type", "text/css"}, {"content-type", "text/html; charset=utf-8"}, {"content-type", "text/plain"}, {"content-type", "text/plain;charset=utf-8"}, - {"range", "bytes=0-"}, {"strict-transport-security", "max-age=31536000"}, {"strict-transport-security", "max-age=31536000; includesubdomains"}, - {"strict-transport-security", "max-age=31536000; includesubdomains; preload"}, {"vary", "accept-encoding"}, {"vary", "origin"}, - {"x-content-type-options", "nosniff"}, {"x-xss-protection", "1; mode=block"}, {":status", "100"}, {":status", "204"}, {":status", "206"}, - {":status", "302"}, {":status", "400"}, {":status", "403"}, {":status", "421"}, {":status", "425"}, {":status", "500"}, {"accept-language", ""}, - {"access-control-allow-credentials", "FALSE"}, {"access-control-allow-credentials", "TRUE"}, {"access-control-allow-headers", "*"}, - {"access-control-allow-methods", "get"}, {"access-control-allow-methods", "get, post, options"}, {"access-control-allow-methods", "options"}, - {"access-control-expose-headers", "content-length"}, {"access-control-request-headers", "content-type"}, {"access-control-request-method", "get"}, - {"access-control-request-method", "post"}, {"alt-svc", "clear"}, {"authorization", ""}, - {"content-security-policy", "script-src 'none'; object-src 'none'; base-uri 'none'"}, {"early-data", "1"}, {"expect-ct", ""}, {"forwarded", ""}, - {"if-range", ""}, {"origin", ""}, {"purpose", "prefetch"}, {"server", ""}, {"timing-allow-origin", "*"}, {"upgrade-insecure-requests", "1"}, - {"user-agent", ""}, {"x-forwarded-for", ""}, {"x-frame-options", "deny"}, {"x-frame-options", "sameorigin"}, -} - -// errQPACK 是 QPACK 数据无法按 RFC 9204 解释时的错误 -var errQPACK = errors.New("QPACK 数据无效") - -// qpackEntry 是动态表中的一条记录,index 为绝对索引 -type qpackEntry struct { - qpackField - index uint64 - refs int -} - -func (entry *qpackEntry) size() uint64 { - return uint64(len(entry.name)+len(entry.value)) + qpackEntryOverhead -} - -// qpackTable 是 neqo 语义的 QPACK 动态表,entries 按插入顺序从新到旧排列 -type qpackTable struct { - entries []*qpackEntry - capacity uint64 - used uint64 - base uint64 - acked uint64 -} - -func (table *qpackTable) canReduce(entry *qpackEntry) bool { - return entry.refs == 0 && entry.index < table.acked -} - -func (table *qpackTable) evictTo(size uint64) bool { - for len(table.entries) > 0 && table.used > size { - oldest := table.entries[len(table.entries)-1] - if !table.canReduce(oldest) { - return false - } - table.used -= oldest.size() - table.entries = table.entries[:len(table.entries)-1] - } - return true -} - -func (table *qpackTable) canEvictTo(size uint64) bool { - evictable := uint64(0) - for i := len(table.entries) - 1; i >= 0 && table.canReduce(table.entries[i]); i-- { - evictable += table.entries[i].size() - } - return table.used-evictable <= size -} - -func (table *qpackTable) setCapacity(capacity uint64) error { - if !table.evictTo(capacity) { - return errQPACK - } - table.capacity = capacity - return nil -} - -func (table *qpackTable) insert(field qpackField) (uint64, error) { - entry := &qpackEntry{qpackField: field, index: table.base} - if entry.size() > table.capacity || !table.evictTo(table.capacity-entry.size()) { - return 0, errQPACK - } - table.base++ - table.used += entry.size() - table.entries = append([]*qpackEntry{entry}, table.entries...) - return entry.index, nil -} - -func (table *qpackTable) absolute(index uint64) *qpackEntry { - if index >= table.base || table.base-index-1 >= uint64(len(table.entries)) { - return nil - } - return table.entries[table.base-index-1] -} - -// relative 按头块的 base 与相对索引取条目,post 表示 post-base 索引 -func (table *qpackTable) relative(index uint64, base uint64, post bool) (*qpackEntry, error) { - var absolute uint64 - if post { - absolute = base + index - } else { - if index >= base { - return nil, errQPACK - } - absolute = base - index - 1 - } - if entry := table.absolute(absolute); entry != nil { - return entry, nil - } - return nil, errQPACK -} - -// lookup 按 neqo 的顺序先查静态表再查动态表,返回完整匹配或首个同名条目 -func (table *qpackTable) lookup(field qpackField, canBlock bool) (index uint64, static bool, exact bool, found bool) { - for i, entry := range qpackStaticTable { - if entry.name != field.name { - continue - } - if entry.value == field.value { - return uint64(i), true, true, true - } - if !found { - index, static, found = uint64(i), true, true - } - } - for _, entry := range table.entries { - if !canBlock && entry.index >= table.acked { - continue - } - if entry.name != field.name { - continue - } - if entry.value == field.value { - return entry.index, false, true, true - } - if !found { - index, static, found = entry.index, false, true - } - } - return index, static, false, found -} - -// appendQPACKInt 按 RFC 9204 4.1.1 写入带前缀的整数,prefix 为前缀位,bits 为前缀位数 -func appendQPACKInt(buffer []byte, prefix byte, bits uint8, value uint64) []byte { - limit := uint64(1)<<(8-bits) - 1 - if value < limit { - return append(buffer, prefix|byte(value)) - } - buffer = append(buffer, prefix|byte(limit)) - value -= limit - for value >= 0x80 { - buffer = append(buffer, byte(value)|0x80) - value >>= 7 - } - return append(buffer, byte(value)) -} - -// appendQPACKString 以 Huffman 编码写入字符串字面量,H 位紧随 bits 个前缀位 -func appendQPACKString(buffer []byte, prefix byte, bits uint8, value string) []byte { - buffer = appendQPACKInt(buffer, prefix|0x80>>bits, bits+1, hpack.HuffmanEncodeLength(value)) - return hpack.AppendHuffmanString(buffer, value) -} - -// qpackEncoder 按 neqo 0.31 的策略编码请求头:静态表没有的头名连同取值写入动态表并按索引引用,字面量一律 Huffman 编码 -type qpackEncoder struct { - table qpackTable - maxEntries uint64 - maxBlocked uint64 - blocked uint64 - nextCapacity uint64 - hasNext bool - // unacked 记录每个请求流尚未确认的头块引用的动态表条目,按发送顺序排列 - unacked map[int64][][]uint64 - // pending 是待写入编码器流的字节 - pending []byte -} - -func newQPACKEncoder() *qpackEncoder { - return &qpackEncoder{unacked: map[int64][][]uint64{}, pending: []byte{qpackEncoderStreamType}} -} - -// setPeerSettings 按服务端 SETTINGS 的动态表容量与阻塞流上限设置编码器 -func (encoder *qpackEncoder) setPeerSettings(capacity uint64, blocked uint64) { - encoder.maxBlocked = blocked - if capacity = min(capacity, firefoxQPACKTableSize); capacity != encoder.table.capacity { - encoder.nextCapacity, encoder.hasNext = capacity, true - } - _ = encoder.sendCapacity() -} - -func (encoder *qpackEncoder) sendCapacity() error { - if !encoder.hasNext { - return nil - } - if encoder.nextCapacity < encoder.table.capacity && !encoder.table.canEvictTo(encoder.nextCapacity) { - return errQPACK - } - encoder.pending = appendQPACKInt(encoder.pending, 0x20, 3, encoder.nextCapacity) - _ = encoder.table.setCapacity(encoder.nextCapacity) - encoder.maxEntries = encoder.nextCapacity / 32 - encoder.hasNext = false - return nil -} - -func (encoder *qpackEncoder) isBlocker(stream int64) bool { - for _, refs := range encoder.unacked[stream] { - for _, index := range refs { - if index >= encoder.table.acked { - return true - } - } - } - return false -} - -// encode 编码一个头块,需要写入编码器流的指令追加到 pending -func (encoder *qpackEncoder) encode(fields []qpackField, stream int64) []byte { - encoderBlocked := encoder.sendCapacity() != nil - base := encoder.table.base - streamBlocker := encoder.isBlocker(stream) - canBlock := encoder.blocked < encoder.maxBlocked || streamBlocker - refs := make([]uint64, 0) - referenced := map[uint64]bool{} - maxRef, hasRef := uint64(0), false - reference := func(index uint64) { - if !hasRef || index > maxRef { - maxRef, hasRef = index, true - } - } - addRef := func(index uint64) { - if !referenced[index] { - referenced[index] = true - refs = append(refs, index) - encoder.table.absolute(index).refs++ - } - } - var body []byte - for _, field := range fields { - index, static, exact, found := encoder.table.lookup(field, canBlock) - switch { - case found && exact && static: - body = appendQPACKInt(body, 0xc0, 2, index) - case found && exact: - body = encoder.appendDynamicIndex(body, index, base) - reference(index) - case found && static: - body = appendQPACKInt(body, 0x50, 4, index) - body = appendQPACKString(body, 0, 0, field.value) - case found: - if index < base { - body = appendQPACKInt(body, 0x40, 4, base-index-1) - } else { - body = appendQPACKInt(body, 0x00, 5, index-base) - } - body = appendQPACKString(body, 0, 0, field.value) - reference(index) - case canBlock && !encoderBlocked: - inserted, ok := encoder.insert(field) - if !ok { - encoderBlocked = true - body = appendQPACKString(body, 0x20, 4, field.name) - body = appendQPACKString(body, 0, 0, field.value) - continue - } - body = encoder.appendDynamicIndex(body, inserted, base) - reference(inserted) - addRef(inserted) - continue - default: - body = appendQPACKString(body, 0x20, 4, field.name) - body = appendQPACKString(body, 0, 0, field.value) - continue - } - if found && !static { - addRef(index) - } - } - block := encoder.appendPrefix(nil, base, maxRef, hasRef) - block = append(block, body...) - if !streamBlocker && hasRef && maxRef >= encoder.table.acked { - encoder.blocked++ - } - if len(refs) > 0 { - encoder.unacked[stream] = append(encoder.unacked[stream], refs) - } - return block -} - -func (encoder *qpackEncoder) insert(field qpackField) (uint64, bool) { - size := uint64(len(field.name)+len(field.value)) + qpackEntryOverhead - if size > encoder.table.capacity || !encoder.table.canEvictTo(encoder.table.capacity-size) { - return 0, false - } - encoder.pending = appendQPACKString(encoder.pending, 0x40, 2, field.name) - encoder.pending = appendQPACKString(encoder.pending, 0, 0, field.value) - index, err := encoder.table.insert(field) - return index, err == nil -} - -func (encoder *qpackEncoder) appendDynamicIndex(buffer []byte, index uint64, base uint64) []byte { - if index < base { - return appendQPACKInt(buffer, 0x80, 2, base-index-1) - } - return appendQPACKInt(buffer, 0x10, 4, index-base) -} - -func (encoder *qpackEncoder) appendPrefix(buffer []byte, base uint64, maxRef uint64, hasRef bool) []byte { - if !hasRef { - buffer = appendQPACKInt(buffer, 0, 0, 0) - return appendQPACKInt(buffer, 0x00, 1, base) - } - required := maxRef + 1 - buffer = appendQPACKInt(buffer, 0, 0, required%(2*encoder.maxEntries)+1) - if required <= base { - return appendQPACKInt(buffer, 0x00, 1, base-required) - } - return appendQPACKInt(buffer, 0x80, 1, required-base-1) -} - -func (encoder *qpackEncoder) recalculateBlocked() { - encoder.blocked = 0 - for stream := range encoder.unacked { - if encoder.isBlocker(stream) { - encoder.blocked++ - } - } -} - -func (encoder *qpackEncoder) incrementAcked(increment uint64) error { - encoder.table.acked += increment - if encoder.table.base < encoder.table.acked { - return errQPACK - } - encoder.recalculateBlocked() - return nil -} - -func (encoder *qpackEncoder) sectionAck(stream int64) error { - blocks := encoder.unacked[stream] - if len(blocks) == 0 { - return errQPACK - } - acked := encoder.table.acked - for _, index := range blocks[0] { - encoder.table.absolute(index).refs-- - if index >= acked { - acked = index + 1 - } - } - if blocks = blocks[1:]; len(blocks) == 0 { - delete(encoder.unacked, stream) - } else { - encoder.unacked[stream] = blocks - } - if acked > encoder.table.acked { - return encoder.incrementAcked(acked - encoder.table.acked) - } - return nil -} - -func (encoder *qpackEncoder) streamCancel(stream int64) { - wasBlocker := false - for _, refs := range encoder.unacked[stream] { - for _, index := range refs { - encoder.table.absolute(index).refs-- - wasBlocker = wasBlocker || index >= encoder.table.acked - } - } - delete(encoder.unacked, stream) - if wasBlocker && encoder.blocked > 0 { - encoder.blocked-- - } -} - -// readQPACKInt 读取带前缀的整数,first 为已读出的首字节,bits 为前缀位数 -func readQPACKInt(reader io.ByteReader, first byte, bits uint8) (uint64, error) { - limit := uint64(1)<<(8-bits) - 1 - value := uint64(first) & limit - if value < limit { - return value, nil - } - for shift := uint(0); shift < 63; shift += 7 { - next, err := reader.ReadByte() - if err != nil { - return 0, err - } - value += uint64(next&0x7f) << shift - if next&0x80 == 0 { - return value, nil - } - } - return 0, errQPACK -} - -// qpackByteReader 是读取 QPACK 字面量所需的字节读取接口 -type qpackByteReader interface { - io.ByteReader - io.Reader -} - -// readQPACKValue 读取首字节没有其他字段的字符串字面量 -func readQPACKValue(reader qpackByteReader) (string, error) { - first, err := reader.ReadByte() - if err != nil { - return "", err - } - return readQPACKString(reader, first, 0) -} - -// readQPACKString 读取字符串字面量,H 位紧随 bits 个前缀位 -func readQPACKString(reader qpackByteReader, first byte, bits uint8) (string, error) { - length, err := readQPACKInt(reader, first, bits+1) - if err != nil { - return "", err - } - if length > 1<<20 { - return "", errQPACK - } - raw := make([]byte, length) - if _, err := io.ReadFull(reader, raw); err != nil { - return "", err - } - if first&(0x80>>bits) == 0 { - return string(raw), nil - } - return hpack.HuffmanDecodeToString(raw) -} - -// readDecoderStream 读取服务端解码器流上的指令并更新编码器状态 -func (encoder *qpackEncoder) readDecoderStream(stream io.Reader, mu *sync.Mutex) error { - reader := bufio.NewReader(stream) - for { - first, err := reader.ReadByte() - if err != nil { - return err - } - var apply func() error - switch { - case first&0x80 != 0: - id, err := readQPACKInt(reader, first, 1) - if err != nil { - return err - } - apply = func() error { return encoder.sectionAck(int64(id)) } - case first&0x40 != 0: - id, err := readQPACKInt(reader, first, 2) - if err != nil { - return err - } - apply = func() error { encoder.streamCancel(int64(id)); return nil } - default: - increment, err := readQPACKInt(reader, first, 2) - if err != nil { - return err - } - apply = func() error { return encoder.incrementAcked(increment) } - } - mu.Lock() - err = apply() - mu.Unlock() - if err != nil { - return err - } - } -} - -// qpackDecoder 维护服务端编码器写入的动态表并解码响应头块,回执时机与 neqo 0.31 一致 -type qpackDecoder struct { - mu sync.Mutex - changed chan struct{} - table qpackTable - acked uint64 - blocked int - // pending 是待写入解码器流的字节 - pending []byte -} - -func newQPACKDecoder() *qpackDecoder { - return &qpackDecoder{changed: make(chan struct{}), table: qpackTable{acked: math.MaxUint64}, pending: []byte{qpackDecoderStreamType}} -} - -// readEncoderStream 读取服务端编码器流的指令写入动态表,每读完一批到达的数据调用一次 flush -func (decoder *qpackDecoder) readEncoderStream(stream io.Reader, flush func()) error { - reader := bufio.NewReader(stream) - for { - first, err := reader.ReadByte() - if err != nil { - return err - } - if err := decoder.readInstruction(reader, first); err != nil { - return err - } - if reader.Buffered() == 0 { - flush() - } - } -} - -func (decoder *qpackDecoder) readInstruction(reader *bufio.Reader, first byte) error { - var field qpackField - switch { - case first&0x80 != 0: - index, err := readQPACKInt(reader, first, 2) - if err != nil { - return err - } - value, err := readQPACKValue(reader) - if err != nil { - return err - } - decoder.mu.Lock() - defer decoder.mu.Unlock() - if first&0x40 != 0 { - if index >= uint64(len(qpackStaticTable)) { - return errQPACK - } - field = qpackField{qpackStaticTable[index].name, value} - } else { - entry, err := decoder.table.relative(index, decoder.table.base, false) - if err != nil { - return err - } - field = qpackField{entry.name, value} - } - return decoder.insertLocked(field) - case first&0x40 != 0: - name, err := readQPACKString(reader, first, 2) - if err != nil { - return err - } - value, err := readQPACKValue(reader) - if err != nil { - return err - } - decoder.mu.Lock() - defer decoder.mu.Unlock() - return decoder.insertLocked(qpackField{name, value}) - case first&0x20 != 0: - capacity, err := readQPACKInt(reader, first, 3) - if err != nil { - return err - } - if capacity > firefoxQPACKTableSize { - return errQPACK - } - decoder.mu.Lock() - defer decoder.mu.Unlock() - return decoder.table.setCapacity(capacity) - default: - index, err := readQPACKInt(reader, first, 3) - if err != nil { - return err - } - decoder.mu.Lock() - defer decoder.mu.Unlock() - entry, err := decoder.table.relative(index, decoder.table.base, false) - if err != nil { - return err - } - return decoder.insertLocked(entry.qpackField) - } -} - -func (decoder *qpackDecoder) insertLocked(field qpackField) error { - if _, err := decoder.table.insert(field); err != nil { - return err - } - close(decoder.changed) - decoder.changed = make(chan struct{}) - return nil -} - -// decode 解码一个头块,引用尚未到达的动态表条目时等待编码器流,解码后追加 Section Acknowledgment -func (decoder *qpackDecoder) decode(ctx context.Context, block []byte, stream int64) ([]qpackField, error) { - reader := &qpackBlockReader{data: block} - first, err := reader.ReadByte() - if err != nil { - return nil, err - } - encoded, err := readQPACKInt(reader, first, 0) - if err != nil { - return nil, err - } - signByte, err := reader.ReadByte() - if err != nil { - return nil, err - } - delta, err := readQPACKInt(reader, signByte, 1) - if err != nil { - return nil, err - } - decoder.mu.Lock() - defer decoder.mu.Unlock() - required, err := decoder.requiredInsertCount(encoded) - if err != nil { - return nil, err - } - var base uint64 - if signByte&0x80 != 0 { - if required <= delta { - return nil, errQPACK - } - base = required - delta - 1 - } else { - base = required + delta - } - if decoder.table.base < required { - if decoder.blocked > firefoxQPACKBlockedStreams { - return nil, fmt.Errorf("QPACK 阻塞流超过 %d", firefoxQPACKBlockedStreams) - } - decoder.blocked++ - for decoder.table.base < required { - changed := decoder.changed - decoder.mu.Unlock() - select { - case <-changed: - decoder.mu.Lock() - case <-ctx.Done(): - decoder.mu.Lock() - decoder.blocked-- - return nil, ctx.Err() - } - } - decoder.blocked-- - } - fields, err := decoder.decodeFields(reader, base) - if err != nil { - return nil, err - } - if required != 0 { - decoder.pending = appendQPACKInt(decoder.pending, 0x80, 1, uint64(stream)) - decoder.acked = max(decoder.acked, required) - } - return fields, nil -} - -func (decoder *qpackDecoder) requiredInsertCount(encoded uint64) (uint64, error) { - if encoded == 0 { - return 0, nil - } - full := 2 * qpackDecoderMaxEntries - if encoded > full { - return 0, errQPACK - } - maxValue := decoder.table.base + qpackDecoderMaxEntries - required := maxValue/full*full + encoded - 1 - if required > maxValue { - if required < full { - return 0, errQPACK - } - required -= full - } - return required, nil -} - -func (decoder *qpackDecoder) decodeFields(reader *qpackBlockReader, base uint64) ([]qpackField, error) { - fields := make([]qpackField, 0, 16) - for reader.offset < len(reader.data) { - first, _ := reader.ReadByte() - var field qpackField - switch { - case first&0x80 != 0: - index, err := readQPACKInt(reader, first, 2) - if err != nil { - return nil, err - } - if field, err = decoder.lookup(index, base, first&0x40 != 0, false); err != nil { - return nil, err - } - case first&0x40 != 0: - index, err := readQPACKInt(reader, first, 4) - if err != nil { - return nil, err - } - if field, err = decoder.lookup(index, base, first&0x10 != 0, false); err != nil { - return nil, err - } - if field.value, err = readQPACKValue(reader); err != nil { - return nil, err - } - case first&0x20 != 0: - name, err := readQPACKString(reader, first, 4) - if err != nil { - return nil, err - } - value, err := readQPACKValue(reader) - if err != nil { - return nil, err - } - field = qpackField{name, value} - case first&0x10 != 0: - index, err := readQPACKInt(reader, first, 4) - if err != nil { - return nil, err - } - if field, err = decoder.lookup(index, base, false, true); err != nil { - return nil, err - } - default: - index, err := readQPACKInt(reader, first, 5) - if err != nil { - return nil, err - } - if field, err = decoder.lookup(index, base, false, true); err != nil { - return nil, err - } - if field.value, err = readQPACKValue(reader); err != nil { - return nil, err - } - } - fields = append(fields, field) - } - return fields, nil -} - -func (decoder *qpackDecoder) lookup(index uint64, base uint64, static bool, post bool) (qpackField, error) { - if static { - if index >= uint64(len(qpackStaticTable)) { - return qpackField{}, errQPACK - } - return qpackStaticTable[index], nil - } - entry, err := decoder.table.relative(index, base, post) - if err != nil { - return qpackField{}, err - } - return entry.qpackField, nil -} - -// cancel 在放弃读取请求流时追加 Stream Cancellation -func (decoder *qpackDecoder) cancel(stream int64) { - decoder.mu.Lock() - defer decoder.mu.Unlock() - if decoder.table.capacity > 0 { - decoder.pending = appendQPACKInt(decoder.pending, 0x40, 2, uint64(stream)) - } -} - -// takePending 追加尚未确认插入数的 Insert Count Increment 并取出待写入解码器流的字节 -func (decoder *qpackDecoder) takePending() []byte { - decoder.mu.Lock() - defer decoder.mu.Unlock() - if increment := decoder.table.base - decoder.acked; increment > 0 { - decoder.pending = appendQPACKInt(decoder.pending, 0x00, 2, increment) - decoder.acked = decoder.table.base - } - pending := decoder.pending - decoder.pending = nil - return pending -} - -// qpackBlockReader 顺序读取一个完整头块 -type qpackBlockReader struct { - data []byte - offset int -} - -func (reader *qpackBlockReader) ReadByte() (byte, error) { - if reader.offset >= len(reader.data) { - return 0, errQPACK - } - value := reader.data[reader.offset] - reader.offset++ - return value, nil -} - -func (reader *qpackBlockReader) Read(buffer []byte) (int, error) { - if reader.offset >= len(reader.data) { - return 0, io.EOF - } - read := copy(buffer, reader.data[reader.offset:]) - reader.offset += read - return read, nil -} diff --git a/internal/aistudio/transport_quic.go b/internal/aistudio/transport_quic.go deleted file mode 100644 index e3e73c75..00000000 --- a/internal/aistudio/transport_quic.go +++ /dev/null @@ -1,454 +0,0 @@ -package aistudio - -import ( - "context" - "crypto/rand" - "encoding/binary" - "fmt" - mrand "math/rand/v2" - "net" - "net/netip" - "sync" - "time" - - quic "github.com/Mag1cFall/uquic" - "github.com/Mag1cFall/uquic/quicvarint" - utls "github.com/Mag1cFall/utls" - "github.com/Mag1cFall/utls/dicttls" -) - -const ( - // firefoxQUICIdleTimeout 是 Firefox network.http.http3.idle_timeout - firefoxQUICIdleTimeout = 30 * time.Second - // firefoxQUICMaxData 是 Firefox network.http.http3.max_data - firefoxQUICMaxData = 25165824 - // firefoxQUICMaxStreamData 是 Firefox network.http.http3.max_stream_data - firefoxQUICMaxStreamData = 12582912 - // neqoStreamWindow 是 neqo 0.31 对端发起流与单向流的初始接收窗口 - neqoStreamWindow = 1048576 - // neqoMaxStreams 是 neqo 0.31 允许对端打开的双向流与单向流数 - neqoMaxStreams = 100 - // neqoResetStreamAtParameter 是 neqo 0.31 以空值声明支持 RESET_STREAM_AT 的传输参数 ID - neqoResetStreamAtParameter = 0x1d - // neqoMinAckDelayParameter 是 neqo 0.31 发送的 min_ack_delay 草案传输参数 ID - neqoMinAckDelayParameter = 0xff02de1a - // neqoMinAckDelay 是 neqo 0.31 声明的 min_ack_delay,单位微秒 - neqoMinAckDelay = 1000 - // neqoSourceConnectionIDLength 是 Firefox 为 neqo 配置的本端连接 ID 长度 - neqoSourceConnectionIDLength = 3 - // neqoIPv4Datagram 是 neqo 0.31 关闭 PMTUD 时 IPv4 的 UDP 载荷上限,Initial 数据报补零到该长度 - neqoIPv4Datagram = 1252 - // neqoIPv6Datagram 是 neqo 0.31 关闭 PMTUD 时 IPv6 的 UDP 载荷上限 - neqoIPv6Datagram = 1232 - // quicAEADOverhead 是 QUIC 包保护的 AEAD 标签长度 - quicAEADOverhead = 16 - // h3NoError 是 HTTP/3 H3_NO_ERROR - h3NoError = 0x100 - // nssQUICGREASEECHInner 是 NSS 为 QUIC ClientHello 的 GREASE ECH 估算的内层 ClientHello 基础长度 - nssQUICGREASEECHInner = 193 - // nssTLSGREASEECHInner 是 NSS 为 TCP ClientHello 的 GREASE ECH 估算的内层 ClientHello 基础长度,比 QUIC 多 12 个 TLS 1.2 密码套件 - nssTLSGREASEECHInner = 217 -) - -// quicResumption 是一类请求的 QUIC 连接共用的会话票据、NEW_TOKEN 令牌与各主机上次收到的服务端 QPACK 设置 -type quicResumption struct { - sessions utls.ClientSessionCache - tokens quic.TokenStore - mu sync.Mutex - settings map[string][2]uint64 -} - -func newQUICResumption() *quicResumption { - return &quicResumption{sessions: utls.NewLRUClientSessionCache(0), tokens: quic.NewLRUTokenStore(100, 1), settings: map[string][2]uint64{}} -} - -// lookup 按 utls loadSession 的判定返回 host 会话恢复时 pre_shared_key 扩展的长度(0 表示不恢复),票据允许 0-RTT 且记得服务端设置时 settings 非 nil -func (resumption *quicResumption) lookup(host string) (int, *[2]uint64) { - session, _ := resumption.sessions.Get(host) - ticket, state, err := session.ResumptionState() - if err != nil || state == nil { - return 0, nil - } - raw, err := state.Bytes() - if err != nil { - return 0, nil - } - psk := resumptionPSKLength(raw, len(ticket)) - if psk == 0 { - return 0, nil - } - settings, ok := resumption.rememberedSettings(host) - if !state.EarlyData || !ok { - return psk, nil - } - return psk, &settings -} - -// rememberSettings 记录 host 服务端 SETTINGS 中的 QPACK 动态表容量与阻塞流上限 -func (resumption *quicResumption) rememberSettings(host string, capacity uint64, blocked uint64) { - resumption.mu.Lock() - defer resumption.mu.Unlock() - resumption.settings[host] = [2]uint64{capacity, blocked} -} - -// rememberedSettings 返回 host 上次连接记录的服务端 QPACK 设置 -func (resumption *quicResumption) rememberedSettings(host string) ([2]uint64, bool) { - resumption.mu.Lock() - defer resumption.mu.Unlock() - settings, ok := resumption.settings[host] - return settings, ok -} - -// dialFirefoxQUIC 以 Firefox 158 的 QUIC 首航直连 host:443,有会话票据时恢复会话,票据允许 0-RTT 时不等握手完成即返回并返回沿用的服务端设置 -func dialFirefoxQUIC(ctx context.Context, host string, resumption *quicResumption) (*quic.Conn, *[2]uint64, error) { - addresses, err := net.DefaultResolver.LookupNetIP(ctx, "ip", host) - if err != nil { - return nil, nil, err - } - if len(addresses) == 0 { - return nil, nil, fmt.Errorf("%s 没有可用地址", host) - } - address := addresses[0].Unmap() - network, datagram := "udp4", neqoIPv4Datagram - if address.Is6() { - network, datagram = "udp6", neqoIPv6Datagram - } - socket, err := net.ListenUDP(network, nil) - if err != nil { - return nil, nil, err - } - psk, settings := resumption.lookup(host) - transport := &quic.UTransport{Transport: &quic.Transport{Conn: socket}, QUICSpec: firefoxQUICSpec(datagram, psk, settings != nil)} - dial := transport.Dial - if settings != nil { - dial = transport.DialEarly - } - connection, err := dial(ctx, net.UDPAddrFromAddrPort(netip.AddrPortFrom(address, 443)), &utls.Config{ - ServerName: host, - NextProtos: []string{"h3"}, - MinVersion: utls.VersionTLS13, - ClientSessionCache: resumption.sessions, - OmitEmptyPsk: true, - }, firefoxQUICConfig(datagram, resumption.tokens)) - if err != nil { - _ = transport.Close() - _ = socket.Close() - return nil, nil, err - } - go func() { - <-connection.Context().Done() - _ = transport.Close() - _ = socket.Close() - }() - return connection, settings, nil -} - -// firefoxQUICConfig 返回 Firefox 158 的 QUIC 连接配置:空闲超时 30 秒,与 neqo 一样只在请求流尚未完成时每半个空闲超时发送保活 PING -func firefoxQUICConfig(datagram int, tokens quic.TokenStore) *quic.Config { - return &quic.Config{ - Versions: []quic.Version{quic.Version1}, - HandshakeIdleTimeout: firefoxQUICIdleTimeout, - MaxIdleTimeout: firefoxQUICIdleTimeout, - KeepAlivePeriod: firefoxQUICIdleTimeout / 2, - KeepAliveStreamsOnly: true, - InitialStreamReceiveWindow: firefoxQUICMaxStreamData, - MaxStreamReceiveWindow: firefoxQUICMaxStreamData, - InitialConnectionReceiveWindow: firefoxQUICMaxData, - MaxConnectionReceiveWindow: firefoxQUICMaxData, - MaxIncomingStreams: neqoMaxStreams, - MaxIncomingUniStreams: neqoMaxStreams, - InitialPacketSize: uint16(datagram), - DisablePathMTUDiscovery: true, - EnableStreamResetPartialDelivery: true, - TokenStore: tokens, - } -} - -// firefoxQUICSpec 返回 Firefox 158(neqo 0.31)一次直连的 QUIC 首航,psk 与 early 决定 ClientHello 是否带 pre_shared_key 与 early_data -func firefoxQUICSpec(datagram int, psk int, early bool) *quic.QUICSpec { - var random [3]byte - _, _ = rand.Read(random[:]) - destination := max(8, 5+int(random[0]&(random[0]>>4))) - firstPN := uint64(random[1]&0x1f) + uint64(max(int(random[2])-224, 0))<<5 + 1 - lengths := []quic.PacketNumberLen{neqoPacketNumberLength(firstPN + 1), neqoPacketNumberLength(firstPN + 2), quic.PacketNumberLen1} - return &quic.QUICSpec{ - InitialPacketSpec: quic.InitialPacketSpec{ - SrcConnIDLength: neqoSourceConnectionIDLength, - DestConnIDLength: destination, - InitPacketNumber: firstPN, - InitPacketNumberLengths: lengths, - FrameBuilder: &neqoInitialFlight{limit: datagram - quicAEADOverhead}, - }, - ClientHelloSpec: firefoxQUICClientHello(psk, early), - UDPDatagramMinSize: datagram, - } -} - -// neqoPacketNumberLength 是 neqo 在尚无确认时为包号 pn 选择的编码长度,即 pn 的非零字节数 -func neqoPacketNumberLength(unacked uint64) quic.PacketNumberLen { - switch { - case unacked < 1<<8: - return quic.PacketNumberLen1 - case unacked < 1<<16: - return quic.PacketNumberLen2 - case unacked < 1<<24: - return quic.PacketNumberLen3 - default: - return quic.PacketNumberLen4 - } -} - -// firefoxQUICClientHello 返回 Firefox 158 经 NSS 发出的 QUIC ClientHello,psk 为 pre_shared_key 扩展长度,非 0 时该扩展在最后 -func firefoxQUICClientHello(psk int, early bool) *utls.ClientHelloSpec { - extensions := []utls.TLSExtension{ - &utls.UtlsCompressCertExtension{Algorithms: []utls.CertCompressionAlgo{utls.CertCompressionZlib, utls.CertCompressionZstd, utls.CertCompressionBrotli}}, - &utls.ExtendedMasterSecretExtension{}, - &utls.KeyShareExtension{KeyShares: append(utls.ReuseHybridAndClassicalKeyShares(utls.KeyShare{Group: utls.X25519MLKEM768}, utls.KeyShare{Group: utls.X25519}), utls.KeyShare{Group: utls.CurveP256})}, - &utls.StatusRequestExtension{}, - &utls.ALPNExtension{AlpnProtocols: []string{"h3"}}, - &utls.PSKKeyExchangeModesExtension{Modes: []uint8{utls.PskModeDHE}}, - &utls.SupportedCurvesExtension{Curves: []utls.CurveID{utls.X25519MLKEM768, utls.X25519, utls.CurveP256, utls.CurveP384, utls.CurveP521}}, - &utls.RenegotiationInfoExtension{Renegotiation: utls.RenegotiateOnceAsClient}, - &utls.SupportedVersionsExtension{Versions: []uint16{utls.VersionTLS13}}, - &utls.FakeRecordSizeLimitExtension{Limit: 0x4001}, - &utls.FakeDelegatedCredentialsExtension{SupportedSignatureAlgorithms: []utls.SignatureScheme{ - utls.ECDSAWithP256AndSHA256, utls.ECDSAWithP384AndSHA384, utls.ECDSAWithP521AndSHA512, utls.ECDSAWithSHA1, - }}, - &utls.SNIExtension{}, - &utls.SignatureAlgorithmsExtension{SupportedSignatureAlgorithms: []utls.SignatureScheme{ - utls.ECDSAWithP256AndSHA256, utls.ECDSAWithP384AndSHA384, utls.ECDSAWithP521AndSHA512, utls.ECDSAWithSHA1, - utls.PSSWithSHA256, utls.PSSWithSHA384, utls.PSSWithSHA512, - utls.PKCS1WithSHA256, utls.PKCS1WithSHA384, utls.PKCS1WithSHA512, utls.PKCS1WithSHA1, - }}, - } - if early { - extensions = append(extensions, &utls.GenericExtension{Id: 42}) - } - mrand.Shuffle(len(extensions), func(i, j int) { extensions[i], extensions[j] = extensions[j], extensions[i] }) - extensions = append(extensions, - &utls.QUICTransportParametersExtension{TransportParameters: utls.TransportParameters{ - utls.MaxIdleTimeout(firefoxQUICIdleTimeout.Milliseconds()), - utls.InitialMaxData(firefoxQUICMaxData), - utls.InitialMaxStreamDataBidiLocal(firefoxQUICMaxStreamData), - utls.InitialMaxStreamDataBidiRemote(neqoStreamWindow), - utls.InitialMaxStreamDataUni(neqoStreamWindow), - utls.InitialMaxStreamsBidi(neqoMaxStreams), - utls.InitialMaxStreamsUni(neqoMaxStreams), - utls.MaxAckDelay(20), - utls.ActiveConnectionIDLimit(8), - utls.InitialSourceConnectionID(nil), - &utls.VersionInformation{ChoosenVersion: utls.VERSION_1, AvailableVersions: []uint32{utls.VERSION_GREASE, utls.VERSION_2, utls.VERSION_1}}, - &utls.FakeQUICTransportParameter{Id: neqoResetStreamAtParameter}, - &utls.FakeQUICTransportParameter{Id: neqoMinAckDelayParameter, Val: quicvarint.Append(nil, neqoMinAckDelay)}, - utls.MaxDatagramFrameSize(65535), - }}, - &utls.GREASEEncryptedClientHelloExtension{ - CandidateCipherSuites: []utls.HPKESymmetricCipherSuite{{KdfId: dicttls.HKDF_SHA256, AeadId: dicttls.AEAD_AES_128_GCM}, {KdfId: dicttls.HKDF_SHA256, AeadId: dicttls.AEAD_CHACHA20_POLY1305}}, - CandidatePayloadLens: []uint16{nssGREASEECHPayload(nssQUICGREASEECHInner, psk, early)}, - }, - ) - if psk > 0 { - extensions = append(extensions, &utls.UtlsPreSharedKeyExtension{}) - } - return &utls.ClientHelloSpec{ - TLSVersMin: utls.VersionTLS13, - TLSVersMax: utls.VersionTLS13, - CipherSuites: []uint16{utls.TLS_AES_128_GCM_SHA256, utls.TLS_CHACHA20_POLY1305_SHA256, utls.TLS_AES_256_GCM_SHA384}, - CompressionMethods: []uint8{0}, - Extensions: extensions, - } -} - -// resumptionPSKLength 返回以编码为 raw(utls SessionState.Bytes)的 TLS 1.3 客户端会话恢复时 pre_shared_key 扩展的长度,会话不是 TLS 1.3 或票据已过期时为 0 -func resumptionPSKLength(raw []byte, ticket int) int { - if binary.BigEndian.Uint16(raw) != utls.VersionTLS13 || time.Now().After(time.Unix(int64(binary.BigEndian.Uint64(raw[len(raw)-12:])), 0)) { - return 0 - } - binder := 32 - if binary.BigEndian.Uint16(raw[3:]) == utls.TLS_AES_256_GCM_SHA384 { - binder = 48 - } - return 2 + 2 + ticket + 4 + 2 + 1 + binder -} - -// nssGREASEECHPayload 是 NSS 按内层基础长度 inner 算出的 GREASE ECH 密文长度(不含 AEAD 标签),按 RFC 9849 补齐到 32 字节的整数倍 -func nssGREASEECHPayload(inner int, psk int, early bool) uint16 { - if psk > 0 { - inner += 4 + psk - } - if early { - inner += 2 - } - return uint16(inner + 31 - (inner-1)%32) -} - -// neqoInitialFlight 按 neqo 0.31 的 SNI 切分把 ClientHello 排进 Initial 包 -type neqoInitialFlight struct { - // limit 是一个 Initial 包去掉 AEAD 标签后的长度上限 - limit int -} - -// cryptoRange 是 CRYPTO 流中的一段 -type cryptoRange struct { - offset int - data []byte -} - -func (flight *neqoInitialFlight) Build(crypto []byte) ([]byte, error) { - payloads, err := flight.BuildFlight(crypto, []quic.InitialDatagramBudget{{MaxFrameBytes: flight.limit}}) - if err != nil { - return nil, err - } - return payloads[0], nil -} - -// BuildFlight 按 budgets 给出的各包帧空间排布 CRYPTO 帧,包数超出 budgets 时沿用最后一项 -func (flight *neqoInitialFlight) BuildFlight(crypto []byte, budgets []quic.InitialDatagramBudget) ([][]byte, error) { - room := func(index int) int { return budgets[min(index, len(budgets)-1)].MaxFrameBytes } - var payloads [][]byte - pending := []cryptoRange{{0, crypto}} - if start, end, ok := findClientHelloSNI(crypto); ok { - middle := start + (end-start)/2 - packets := (len(crypto) + flight.limit - 1) / flight.limit - left, right := neqoLimitChunks(cryptoRange{0, crypto[:middle]}, cryptoRange{middle, crypto[middle:]}, len(crypto)/packets) - remaining := room(0) - var payload []byte - sent := make([]cryptoRange, 0, 2) - for _, chunk := range []cryptoRange{right, left} { - var written int - var ok bool - if payload, written, ok = appendCryptoChunk(payload, chunk, remaining-len(payload)); ok { - sent = append(sent, cryptoRange{chunk.offset, chunk.data[:written]}) - } - } - if len(sent) == 0 { - return nil, fmt.Errorf("Initial 包放不下 CRYPTO 帧") - } - payloads = append(payloads, payload) - pending = subtractCryptoRanges(crypto, sent) - } - for len(pending) > 0 { - remaining := room(len(payloads)) - var payload []byte - for len(pending) > 0 { - chunk := pending[0] - var written int - var ok bool - if payload, written, ok = appendCryptoChunk(payload, chunk, remaining-len(payload)); !ok { - break - } - if written < len(chunk.data) { - pending[0] = cryptoRange{chunk.offset + written, chunk.data[written:]} - break - } - pending = pending[1:] - } - if payload == nil { - return nil, fmt.Errorf("Initial 包放不下 CRYPTO 帧") - } - payloads = append(payloads, payload) - } - return payloads, nil -} - -// neqoLimitChunks 是 neqo limit_chunks:把 SNI 两侧的片段截到 limit 以内,使 SNI 分落在不同包 -func neqoLimitChunks(left cryptoRange, right cryptoRange, limit int) (cryptoRange, cryptoRange) { - switch { - case len(left.data)+len(right.data) <= limit: - case len(left.data) <= limit: - skip := len(right.data) + len(left.data) - limit - right = cryptoRange{right.offset + skip, right.data[skip:]} - case len(right.data) <= limit: - left.data = left.data[:limit-len(right.data)] - default: - left.data = left.data[:limit/2] - right.data = right.data[:limit/2] - } - return left, right -} - -// appendCryptoChunk 是 neqo write_chunk:在 room 字节内写入尽量多的 chunk 并返回写入的数据长度,放不下帧头与 1 字节数据时返回 false -func appendCryptoChunk(payload []byte, chunk cryptoRange, room int) ([]byte, int, bool) { - header := 1 + quicvarint.Len(uint64(chunk.offset)) + 1 - if room < header+1 { - return payload, 0, false - } - length := min(len(chunk.data), room-header) - header += quicvarint.Len(uint64(length)) - 1 - length = min(len(chunk.data), room-header) - payload = append(payload, 0x06) - payload = quicvarint.Append(payload, uint64(chunk.offset)) - payload = quicvarint.Append(payload, uint64(length)) - return append(payload, chunk.data[:length]...), length, true -} - -// subtractCryptoRanges 返回 crypto 中未被 sent 覆盖的各段,按偏移排列 -func subtractCryptoRanges(crypto []byte, sent []cryptoRange) []cryptoRange { - covered := make([]bool, len(crypto)) - for _, chunk := range sent { - for i := range chunk.data { - covered[chunk.offset+i] = true - } - } - var pending []cryptoRange - for start := 0; start < len(crypto); { - if covered[start] { - start++ - continue - } - end := start - for end < len(crypto) && !covered[end] { - end++ - } - pending = append(pending, cryptoRange{start, crypto[start:end]}) - start = end - } - return pending -} - -// findClientHelloSNI 是 neqo find_sni:返回 ClientHello 中 SNI 主机名的字节区间 -func findClientHelloSNI(hello []byte) (int, int, bool) { - if len(hello) < 1+3+2+32+1 || hello[0] != 1 { - return 0, 0, false - } - offset := 1 + 3 + 2 + 32 - skip := func(width int) bool { - if offset+width > len(hello) { - return false - } - length := 0 - for _, b := range hello[offset : offset+width] { - length = length<<8 | int(b) - } - offset += width + length - return offset <= len(hello) - } - if !skip(1) || !skip(2) || !skip(1) { - return 0, 0, false - } - offset += 2 - for offset+4 <= len(hello) { - kind := binary.BigEndian.Uint16(hello[offset:]) - length := int(binary.BigEndian.Uint16(hello[offset+2:])) - offset += 4 - if kind == 0 { - if offset+2 > len(hello) { - return 0, 0, false - } - list := int(binary.BigEndian.Uint16(hello[offset:])) - if list < 3 { - return 0, 0, false - } - start := offset + 2 + 3 - end := start + list - 3 - if end > len(hello) { - return 0, 0, false - } - return start, end, true - } - offset += length - } - return 0, 0, false -} - -var _ quic.QUICFlightFrameBuilder = (*neqoInitialFlight)(nil) diff --git a/internal/aistudio/transport_quic_test.go b/internal/aistudio/transport_quic_test.go deleted file mode 100644 index fe22cd0c..00000000 --- a/internal/aistudio/transport_quic_test.go +++ /dev/null @@ -1,126 +0,0 @@ -package aistudio - -import ( - "bufio" - "context" - "io" - "net/http/httptest" - "strings" - "sync" - "sync/atomic" - "testing" - "time" - - fhttp "github.com/Mag1cFall/fhttp" - quic "github.com/Mag1cFall/uquic" - utls "github.com/Mag1cFall/utls" -) - -// TestHTTP3SlowResponse 验证上游超过空闲超时才返回响应头或响应体时,请求流未完成期间的保活使连接不断开,POST 只发一次 -func TestHTTP3SlowResponse(t *testing.T) { - for _, flushHeaders := range []bool{false, true} { - name := "headers" - if flushHeaders { - name = "body" - } - t.Run(name, func(t *testing.T) { - var requests atomic.Int32 - conn := newTestHTTP3Conn(t, func(stream *quic.Stream) { - requests.Add(1) - block := newQPACKEncoder().encode([]qpackField{{":status", "200"}}, int64(stream.StreamID())) - headers := appendH3Frame(nil, h3FrameHeaders, block) - if flushHeaders { - _, _ = stream.Write(headers) - } - time.Sleep(1500 * time.Millisecond) - if !flushHeaders { - _, _ = stream.Write(headers) - } - _, _ = stream.Write(appendH3Frame(nil, h3FrameData, []byte("ok"))) - _ = stream.Close() - }) - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - request, err := fhttp.NewRequestWithContext(ctx, "POST", "https://localhost/", strings.NewReader("test")) - if err != nil { - t.Fatal(err) - } - response, opened, err := conn.roundTrip(request, h3RequestFields(request, false)) - if err != nil || !opened { - t.Fatalf("等待慢响应失败: opened=%v err=%v", opened, err) - } - defer response.Body.Close() - body, err := io.ReadAll(response.Body) - if err != nil || string(body) != "ok" || response.StatusCode != 200 { - t.Fatalf("响应=%q 状态=%d err=%v", body, response.StatusCode, err) - } - if requests.Load() != 1 { - t.Fatalf("POST 执行了 %d 次,应为 1 次", requests.Load()) - } - }) - } -} - -// newTestHTTP3Conn 返回连到本地 QUIC 服务的 HTTP/3 连接,连接使用生产 QUIC 配置并把空闲超时改为 500 ms,handler 处理正文为 test 的请求流 -func newTestHTTP3Conn(t *testing.T, handler func(*quic.Stream)) *http3Conn { - t.Helper() - certServer := httptest.NewTLSServer(nil) - t.Cleanup(certServer.Close) - cert := certServer.TLS.Certificates[0] - tlsConfig := &utls.Config{ - Certificates: []utls.Certificate{{Certificate: cert.Certificate, PrivateKey: cert.PrivateKey}}, - NextProtos: []string{"h3"}, - } - listener, err := quic.ListenAddr("127.0.0.1:0", tlsConfig, &quic.Config{MaxIdleTimeout: 500 * time.Millisecond}) - if err != nil { - t.Fatal(err) - } - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - t.Cleanup(cancel) - done := make(chan struct{}) - go func() { - defer close(done) - connection, err := listener.Accept(ctx) - if err != nil { - return - } - defer connection.CloseWithError(0, "") - var handlers sync.WaitGroup - defer handlers.Wait() - for { - stream, err := connection.AcceptStream(ctx) - if err != nil { - return - } - handlers.Add(1) - go func() { - defer handlers.Done() - reader := bufio.NewReader(stream) - if kind, _, err := readH3Frame(reader); err != nil || kind != h3FrameHeaders { - return - } - if kind, body, err := readH3Frame(reader); err == nil && kind == h3FrameData && string(body) == "test" { - handler(stream) - } - }() - } - }() - t.Cleanup(func() { - cancel() - _ = listener.Close() - <-done - }) - config := firefoxQUICConfig(neqoIPv4Datagram, nil) - config.MaxIdleTimeout = 500 * time.Millisecond - connection, err := quic.DialAddr(ctx, listener.Addr().String(), &utls.Config{InsecureSkipVerify: true, NextProtos: []string{"h3"}}, config) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { _ = connection.CloseWithError(0, "") }) - conn, err := newHTTP3Conn(connection, nil, func(uint64, uint64) {}) - if err != nil { - t.Fatal(err) - } - t.Cleanup(conn.close) - return conn -} diff --git a/internal/api/account_tasks.go b/internal/api/account_tasks.go index 7a843b2e..feea8f14 100644 --- a/internal/api/account_tasks.go +++ b/internal/api/account_tasks.go @@ -13,7 +13,7 @@ import ( "github.com/Mag1cFall/AIStudio2API/internal/chromeauth" ) -// AccountTask 是在服务端运行的添加或重新登录账户任务,管理页关闭或刷新后继续运行,结束后保留到用户关闭 +// AccountTask 是在服务端运行的添加、重新登录或刷新账户任务,管理页关闭或刷新后继续运行,结束后保留到用户关闭 type AccountTask struct { ID string `json:"id"` Kind string `json:"kind"` @@ -299,6 +299,50 @@ func (s *server) handleLoginAccount(w http.ResponseWriter, r *http.Request) { }) } +// refreshTaskConcurrency 是刷新任务同时刷新的账户数 +const refreshTaskConcurrency = 4 + +// handleRefreshAccounts 在账户任务中按 refreshTaskConcurrency 的并发依次刷新所列账户,账户标签为账户 ID,任务被关闭后不再开始刷新 +func (s *server) handleRefreshAccounts(w http.ResponseWriter, r *http.Request) { + var input struct { + IDs []string `json:"ids"` + } + if err := decodeJSON(r, &input); err != nil { + writeAdminError(w, http.StatusBadRequest, "invalid_request", err.Error()) + return + } + task := AccountTask{Kind: "refresh", Items: make([]AccountTaskItem, len(input.IDs))} + next := make(chan int, len(input.IDs)) + for index, id := range input.IDs { + task.Items[index] = AccountTaskItem{Label: id, State: "pending"} + next <- index + } + close(next) + s.startAccountTask(w, r, task, func(ctx context.Context, update accountTaskUpdate) { + var group sync.WaitGroup + for range min(refreshTaskConcurrency, len(input.IDs)) { + group.Go(func() { + for index := range next { + if ctx.Err() != nil { + return + } + update(func(task *AccountTask) { task.Items[index].State = "running" }) + _, err := s.config.Admin.RefreshAccount(ctx, input.IDs[index]) + update(func(task *AccountTask) { + item := &task.Items[index] + if err != nil { + item.State, item.Code, item.Error = "failed", codeFromError(err, ""), err.Error() + return + } + item.State = "done" + }) + } + }) + } + group.Wait() + }) +} + // handleImportChromeAccounts 在账户任务中同时导入选中的本机 Chrome 账号,各账号独立保存 func (s *server) handleImportChromeAccounts(w http.ResponseWriter, r *http.Request) { var input chromeImportRequest diff --git a/internal/api/admin.go b/internal/api/admin.go index 2a9b674b..cee6c50f 100644 --- a/internal/api/admin.go +++ b/internal/api/admin.go @@ -415,6 +415,7 @@ func (s *server) registerAdmin(mux *http.ServeMux) { mux.HandleFunc("POST /api/accounts/{id}/login", s.handleLoginAccount) mux.HandleFunc("GET /api/login-view/{view}", s.handleLoginView) mux.HandleFunc("POST /api/accounts/{id}/refresh", s.handleRefreshAccount) + mux.HandleFunc("POST /api/accounts/refresh", s.handleRefreshAccounts) mux.HandleFunc("POST /api/control/start", s.handleStartService) mux.HandleFunc("POST /api/control/stop", s.handleStopService) mux.HandleFunc("POST /api/camoufox/download", s.handleDownloadCamoufox) diff --git a/internal/api/anthropic.go b/internal/api/anthropic.go index 1bbcc618..37258965 100644 --- a/internal/api/anthropic.go +++ b/internal/api/anthropic.go @@ -63,6 +63,10 @@ func (request anthropicRequest) thinkingOmitted() bool { type anthropicMessage struct { Role string `json:"role"` Content json.RawMessage `json:"content"` + // OutputConfig 是对话中途 system 消息改变的输出设置,effort 作用于之后的轮次 + OutputConfig *struct { + Effort string `json:"effort"` + } `json:"output_config"` } type anthropicTool struct { @@ -350,10 +354,18 @@ func (request anthropicRequest) toGenerateRequest(id string) (aistudio.GenerateR for _, tool := range request.Tools { known[tool.Name] = tool } + messageEffort := "" for _, message := range request.Messages { + message.Content, declared, err = anthropicToolChanges(message.Content, declared, known) + if err != nil { + return aistudio.GenerateRequest{}, fmt.Errorf("%s message: %w", message.Role, err) + } if message.Role == "system" { + if message.OutputConfig != nil && message.OutputConfig.Effort != "" { + messageEffort = message.OutputConfig.Effort + } var text string - text, declared, err = anthropicSystemMessage(message.Content, declared, known) + text, err = anthropicSystemText(message.Content) if err != nil { return aistudio.GenerateRequest{}, fmt.Errorf("system message: %w", err) } @@ -404,11 +416,16 @@ func (request anthropicRequest) toGenerateRequest(id string) (aistudio.GenerateR } format := request.OutputFormat if request.OutputConfig != nil { - config.ReasoningEffort = request.OutputConfig.Effort + if request.OutputConfig.Effort != "" && config.ReasoningEffort != "none" { + config.ReasoningEffort = request.OutputConfig.Effort + } if request.OutputConfig.Format != nil { format = request.OutputConfig.Format } } + if messageEffort != "" && config.ReasoningEffort != "none" { + config.ReasoningEffort = messageEffort + } if format != nil { if format.Type != "json_schema" { return aistudio.GenerateRequest{}, fmt.Errorf("unsupported output format type %q", format.Type) @@ -447,48 +464,58 @@ func anthropicSystemText(raw json.RawMessage) (string, error) { return strings.Join(texts, "\n"), nil } -// anthropicSystemMessage 读取对话中途 system 消息的文本,并按其中的 tool_addition 与 tool_removal 更新声明的工具:按定义加入的工具记入 known,按名称引用的工具从 known 恢复,已声明的工具不重复声明 -func anthropicSystemMessage(raw json.RawMessage, tools []anthropicTool, known map[string]anthropicTool) (string, []anthropicTool, error) { - var text string - if err := json.Unmarshal(raw, &text); err == nil { - return text, tools, nil - } - var blocks []struct { - Type string `json:"type"` - Text string `json:"text"` - Tool struct { - Type string `json:"type"` - Name string `json:"name"` - Definition anthropicTool `json:"definition"` - } `json:"tool"` - } +// anthropicToolChange 是 tool_addition 与 tool_removal 块中的一个工具:tool_definition 带定义,tool_reference 与移除只带名称 +type anthropicToolChange struct { + Type string `json:"type"` + Name string `json:"name"` + Definition anthropicTool `json:"definition"` +} + +// anthropicToolChanges 按消息内容中的 tool_addition 与 tool_removal 块更新声明的工具并返回其余内容块,块以 tool 给出一个工具或以 tools 给出多个;按定义加入的工具记入 known,按名称引用的工具从 known 恢复,已声明的工具不重复声明 +func anthropicToolChanges(raw json.RawMessage, tools []anthropicTool, known map[string]anthropicTool) (json.RawMessage, []anthropicTool, error) { + var blocks []json.RawMessage if err := json.Unmarshal(raw, &blocks); err != nil { - return "", nil, fmt.Errorf("content must be a string or block array") + return raw, tools, nil } - texts := make([]string, 0, len(blocks)) - for _, block := range blocks { - switch block.Type { - case "text": - if block.Text != "" { - texts = append(texts, block.Text) + kept := make([]json.RawMessage, 0, len(blocks)) + for _, rawBlock := range blocks { + var block struct { + Type string `json:"type"` + Tool *anthropicToolChange `json:"tool"` + Tools []anthropicToolChange `json:"tools"` + } + if err := json.Unmarshal(rawBlock, &block); err != nil { + return nil, nil, err + } + if block.Type != "tool_addition" && block.Type != "tool_removal" { + kept = append(kept, rawBlock) + continue + } + changes := block.Tools + if block.Tool != nil { + changes = append(changes, *block.Tool) + } + for _, change := range changes { + if block.Type == "tool_removal" { + tools = slices.DeleteFunc(slices.Clone(tools), func(tool anthropicTool) bool { return tool.Name == change.Name }) + continue } - case "tool_addition": - tool, ok := block.Tool.Definition, block.Tool.Type == "tool_definition" + tool, ok := change.Definition, change.Type == "tool_definition" if ok { known[tool.Name] = tool - } else if block.Tool.Type == "tool_reference" { - tool, ok = known[block.Tool.Name] + } else if change.Type == "tool_reference" { + tool, ok = known[change.Name] } if ok && !slices.ContainsFunc(tools, func(declared anthropicTool) bool { return declared.Name == tool.Name }) { tools = append(slices.Clip(tools), tool) } - case "tool_removal": - tools = slices.DeleteFunc(slices.Clone(tools), func(tool anthropicTool) bool { return tool.Name == block.Tool.Name }) - default: - return "", nil, fmt.Errorf("unsupported system block type %q", block.Type) } } - return strings.Join(texts, "\n"), tools, nil + if len(kept) == len(blocks) { + return raw, tools, nil + } + content, err := json.Marshal(kept) + return content, tools, err } func anthropicRole(role string) (aistudio.Role, error) { @@ -1680,9 +1707,14 @@ func (writer *anthropicStreamWriter) finish(result generationResult) error { return writer.send("message_stop", map[string]string{"type": "message_stop"}) } +// error 写出流内错误事件,5xx 原因的错误类型为 Claude Code 流内会重试的 overloaded_error func (writer *anthropicStreamWriter) error(err error) error { + kind, status := anthropicErrorType(err), statusFromError(err) + if status >= http.StatusInternalServerError { + kind = "overloaded_error" + } return writer.send("error", map[string]any{ "type": "error", - "error": map[string]string{"type": anthropicErrorType(err), "message": retryMessage(publicErrorMessage(statusFromError(err), anthropicErrorMessage(err)), retryAfterSeconds(err))}, + "error": map[string]string{"type": kind, "message": retryMessage(publicErrorMessage(status, anthropicErrorMessage(err)), retryAfterSeconds(err))}, }) } diff --git a/internal/api/client_tools.go b/internal/api/client_tools.go index 664d11ca..63e13953 100644 --- a/internal/api/client_tools.go +++ b/internal/api/client_tools.go @@ -133,6 +133,10 @@ func responsesToolKind(tools []responsesTool, name string) string { if name == "computer" { return tool.Type } + case "tool_search": + if tool.Execution == "client" && name == clientToolSearch { + return "tool_search" + } case "namespace": for _, inner := range tool.Tools { if inner.Type == "custom" && namespacedName(tool.Name, inner.Name) == name { diff --git a/internal/api/errors.go b/internal/api/errors.go index 0bab5b80..2d20ea3c 100644 --- a/internal/api/errors.go +++ b/internal/api/errors.go @@ -218,6 +218,18 @@ func writeRouteNotFound(w http.ResponseWriter, r *http.Request) { } } +// writeRequestBodyError 以请求所属协议的错误体返回请求体无法读取的错误 +func writeRequestBodyError(w http.ResponseWriter, r *http.Request, status int, message string) { + switch requestProtocol(r) { + case "gemini": + writeGeminiError(w, status, "INVALID_ARGUMENT", message) + case "anthropic": + writeAnthropicError(w, status, "invalid_request_error", message) + default: + writeOpenAIError(w, status, "invalid_request", message) + } +} + // geminiInvalidKeyMessage 是 Gemini API 对无效 API key 的错误信息 const geminiInvalidKeyMessage = "API key not valid. Please pass a valid API key." @@ -394,7 +406,7 @@ type openAIErrorBody struct { } func openAIErrorType(status int, code string) string { - if status == http.StatusBadRequest || status == http.StatusUnauthorized || status == http.StatusForbidden || status == http.StatusNotFound { + if status == http.StatusBadRequest || status == http.StatusUnauthorized || status == http.StatusForbidden || status == http.StatusNotFound || status == http.StatusUnsupportedMediaType { return "invalid_request_error" } if code == "rate_limit_exceeded" { diff --git a/internal/api/gemini.go b/internal/api/gemini.go index 1f30a0e5..1081c0e7 100644 --- a/internal/api/gemini.go +++ b/internal/api/gemini.go @@ -701,6 +701,27 @@ func geminiJSONName(name string) string { return builder.String() } +// geminiSystemParts 把系统指令中的 part 分为指令文本与移到首个 user 轮开头的媒体,函数调用与结果转录为文本 +func geminiSystemParts(parts []aistudio.Part) ([]string, []aistudio.Part, error) { + var texts []string + var media []aistudio.Part + for _, part := range parts { + switch { + case part.FunctionCall != nil || part.FunctionResult != nil: + transcript, err := aistudio.ToolPartTranscript(part) + if err != nil { + return nil, nil, err + } + texts = append(texts, transcript) + case part.Text != "": + texts = append(texts, part.Text) + case part.InlineData != nil || part.File != nil || part.ExternalMedia != nil: + media = append(media, part) + } + } + return texts, media, nil +} + func (request geminiRequest) toGenerateRequest(id string, model string) (aistudio.GenerateRequest, error) { if err := request.GenerationConfig.validate(); err != nil { return aistudio.GenerateRequest{}, err @@ -712,24 +733,14 @@ func (request geminiRequest) toGenerateRequest(id string, model string) (aistudi if err != nil { return aistudio.GenerateRequest{}, fmt.Errorf("systemInstruction: %w", err) } - for _, part := range parts { - if part.FunctionCall != nil || part.FunctionResult != nil { - transcript, err := aistudio.ToolPartTranscript(part) - if err != nil { - return aistudio.GenerateRequest{}, fmt.Errorf("systemInstruction: %w", err) - } - system = append(system, transcript) - continue - } - if part.Text == "" && (part.InlineData != nil || part.File != nil || part.ExternalMedia != nil) { - systemMedia = append(systemMedia, part) - } - if part.Text != "" { - system = append(system, part.Text) - } + texts, media, err := geminiSystemParts(parts) + if err != nil { + return aistudio.GenerateRequest{}, fmt.Errorf("systemInstruction: %w", err) } + system, systemMedia = texts, media } contents := make([]aistudio.Content, 0, len(request.Contents)) + var reminders []aistudio.Part for _, content := range request.Contents { parts, hasResult, err := mapGeminiParts(content.Parts) if err != nil { @@ -738,6 +749,19 @@ func (request geminiRequest) toGenerateRequest(id string, model string) (aistudi if len(parts) == 0 { continue } + if content.Role == "system" || content.Role == "developer" { + texts, media, err := geminiSystemParts(parts) + if err != nil { + return aistudio.GenerateRequest{}, fmt.Errorf("%s content: %w", content.Role, err) + } + systemMedia = append(systemMedia, media...) + if len(contents) == 0 { + system = append(system, texts...) + } else if len(texts) > 0 { + reminders = addSystemReminder(contents, reminders, strings.Join(texts, "\n")) + } + continue + } role, err := geminiRole(content.Role) if err != nil { return aistudio.GenerateRequest{}, err @@ -745,7 +769,10 @@ func (request geminiRequest) toGenerateRequest(id string, model string) (aistudi if hasResult { role = aistudio.RoleTool } - contents = append(contents, aistudio.Content{Role: role, Parts: parts}) + contents, reminders = appendWithReminders(contents, aistudio.Content{Role: role, Parts: parts}, reminders) + } + if len(reminders) > 0 { + contents = append(contents, aistudio.Content{Role: aistudio.RoleUser, Parts: reminders}) } contents = prependSystemMedia(contents, systemMedia) tools, err := mapGeminiTools(request.Tools, request.ToolConfig) diff --git a/internal/api/generate.go b/internal/api/generate.go index d6a1d1fe..4908e839 100644 --- a/internal/api/generate.go +++ b/internal/api/generate.go @@ -275,7 +275,10 @@ var functionResultMediaTypes = map[string]bool{ "image_url": true, "input_image": true, "input_file": true, "input_audio": true, "video_url": true, "input_video": true, } -// functionResultParts 把工具结果转成函数结果内容与紧随其后的媒体 part,文本块以换行相连作为结果文本,isError 时结果写在 error 键下 +// functionResultMediaPayloads 是媒体块中承载内容的字段,单个对象带其中之一时才按媒体块处理 +var functionResultMediaPayloads = []string{"image_url", "file_id", "file_data", "file_url", "input_audio", "video_url", "source", "data", "uri"} + +// functionResultParts 把工具结果转成函数结果内容与紧随其后的媒体 part,文本块以换行相连作为结果文本,类型与内容字段都是媒体块的单个对象按只含它的块数组处理,isError 时结果写在 error 键下 func functionResultParts(raw json.RawMessage, isError bool, parse func(json.RawMessage) ([]aistudio.Part, error)) (json.RawMessage, []aistudio.Part, error) { key := "result" if isError { @@ -283,7 +286,15 @@ func functionResultParts(raw json.RawMessage, isError bool, parse func(json.RawM } trimmed := bytes.TrimSpace(raw) var blocks []json.RawMessage - if bytes.HasPrefix(trimmed, []byte("[")) && json.Unmarshal(raw, &blocks) == nil && len(blocks) > 0 { + var single struct { + Type string `json:"type"` + } + var fields map[string]json.RawMessage + if bytes.HasPrefix(trimmed, []byte("{")) && json.Unmarshal(raw, &single) == nil && functionResultMediaTypes[single.Type] && json.Unmarshal(raw, &fields) == nil && + slices.ContainsFunc(functionResultMediaPayloads, func(name string) bool { return fields[name] != nil }) { + blocks = []json.RawMessage{raw} + } + if len(blocks) > 0 || bytes.HasPrefix(trimmed, []byte("[")) && json.Unmarshal(raw, &blocks) == nil && len(blocks) > 0 { var texts []string var media []aistudio.Part contentBlocks := true diff --git a/internal/api/middleware.go b/internal/api/middleware.go index 212a74b8..4acd6aa2 100644 --- a/internal/api/middleware.go +++ b/internal/api/middleware.go @@ -4,6 +4,8 @@ import ( "context" "crypto/subtle" "errors" + "fmt" + "io" "net" "net/http" "net/url" @@ -15,6 +17,8 @@ import ( "unicode/utf8" "github.com/Mag1cFall/AIStudio2API/internal/aistudio" + "github.com/klauspost/compress/gzip" + "github.com/klauspost/compress/zstd" ) type accessLogContextKey struct{} @@ -500,6 +504,47 @@ func bodyLimitMiddleware(next http.Handler) http.Handler { }) } +// requestEncodingMiddleware 按 Content-Encoding 依次解压 gzip 与 zstd 请求体,解压后的正文同样受公开接口请求体上限约束,不支持的编码返回 415 +func requestEncodingMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + header := strings.Join(r.Header.Values("Content-Encoding"), ",") + if header == "" { + next.ServeHTTP(w, r) + return + } + encodings := strings.Split(header, ",") + var body io.Reader = r.Body + for index := len(encodings) - 1; index >= 0; index-- { + switch encoding := strings.ToLower(strings.TrimSpace(encodings[index])); encoding { + case "", "identity": + case "gzip", "x-gzip": + reader, err := gzip.NewReader(body) + if err != nil { + writeRequestBodyError(w, r, http.StatusBadRequest, fmt.Sprintf("invalid gzip request body: %v", err)) + return + } + defer reader.Close() + body = reader + case "zstd": + decoder, err := zstd.NewReader(body, zstd.WithDecoderConcurrency(1), zstd.WithDecoderMaxMemory(uint64(maxPublicBodyBytes))) + if err != nil { + writeRequestBodyError(w, r, http.StatusBadRequest, fmt.Sprintf("invalid zstd request body: %v", err)) + return + } + defer decoder.Close() + body = decoder + default: + writeRequestBodyError(w, r, http.StatusUnsupportedMediaType, fmt.Sprintf("unsupported Content-Encoding %q", encoding)) + return + } + } + r.Body = http.MaxBytesReader(w, io.NopCloser(body), maxPublicBodyBytes) + r.Header.Del("Content-Encoding") + r.ContentLength = -1 + next.ServeHTTP(w, r) + }) +} + func sameOriginMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Sec-Fetch-Site") == "cross-site" { diff --git a/internal/api/responses.go b/internal/api/responses.go index 758e65df..50a28b7a 100644 --- a/internal/api/responses.go +++ b/internal/api/responses.go @@ -102,8 +102,17 @@ type responsesTool struct { AllowedTools json.RawMessage `json:"allowed_tools,omitempty"` RequireApproval json.RawMessage `json:"require_approval,omitempty"` ServerDescription string `json:"server_description,omitempty"` + // Execution 是 tool_search 工具的执行方,client 时由客户端检索并在 tool_search_output 中给出找到的工具 + Execution string `json:"execution,omitempty"` } +// clientToolSearch 是客户端执行的 tool_search 在上游声明的函数名、默认说明与默认参数 +const ( + clientToolSearch = "tool_search" + clientToolSearchDescription = "Search for deferred tools by query and load the matching tools." + clientToolSearchParameters = `{"type":"object","properties":{"query":{"type":"string","description":"Search query for deferred tools."},"limit":{"type":"number","description":"Maximum number of tools to return."}},"required":["query"],"additionalProperties":false}` +) + // imageGenerationTool 返回请求声明的 image_generation 工具 func imageGenerationTool(tools []responsesTool) *responsesTool { index := slices.IndexFunc(tools, func(tool responsesTool) bool { return tool.Type == "image_generation" }) @@ -714,8 +723,7 @@ func (s *server) prepareResponses(ctx context.Context, request *responsesRequest request.Tools[index].Strict = &strict } } - additionalTools, _ := responsesInputConfig(input) - tools := slices.Concat(request.Tools, additionalTools) + tools, _ := responsesInputConfig(request.Tools, input) rewritten, approved, err := s.applyMCPApprovals(ctx, request.PreviousResponseID, tools, input.raw, execute) if err != nil { return responsesPreparation{}, err @@ -848,7 +856,7 @@ func responsesInputItems(input splitInput, responseID string) []storedItem { if json.Unmarshal(head.Content, &content) == nil { part := map[string]any{"type": "input_text", "text": content} if head.Role == "assistant" { - part = map[string]any{"type": "output_text", "text": content, "annotations": []any{}} + part = map[string]any{"type": "output_text", "text": content, "annotations": []any{}, "logprobs": []any{}} } item["content"], _ = json.Marshal([]any{part}) } @@ -890,8 +898,8 @@ func (request responsesRequest) toGenerateRequest(id string) (aistudio.GenerateR return aistudio.GenerateRequest{}, nil, err } instructions := request.instructions(inlineInstructions) - additionalTools, configuredEffort := responsesInputConfig(input) - tools, err := mapResponsesTools(slices.Concat(request.Tools, additionalTools), request.ToolChoice) + declared, configuredEffort := responsesInputConfig(request.Tools, input) + tools, err := mapResponsesTools(declared, request.ToolChoice) if err != nil { return aistudio.GenerateRequest{}, nil, err } @@ -1017,8 +1025,55 @@ func responsesContents(input splitInput) ([]aistudio.Content, []string, error) { continue case "additional_tools", "configuration_update": continue + case "tool_search_call", "tool_search_output": + var search struct { + CallID string `json:"call_id"` + Execution string `json:"execution"` + Status string `json:"status"` + Arguments json.RawMessage `json:"arguments"` + Tools []responsesTool `json:"tools"` + } + if err := json.Unmarshal(rawItem, &search); err != nil { + return nil, nil, fmt.Errorf("%s: %w", kind, err) + } + if search.Execution != "" && search.Execution != "client" { + pendingSignature = "" + content, err := responsesHostedItem(kind, rawItem) + if err != nil { + return nil, nil, fmt.Errorf("%s: %w", kind, err) + } + contents = append(contents, content) + continue + } + if kind == "tool_search_call" { + arguments := search.Arguments + var text string + if json.Unmarshal(arguments, &text) == nil { + arguments = json.RawMessage(text) + } + contents = append(contents, aistudio.Content{Role: aistudio.RoleAssistant, Parts: []aistudio.Part{{FunctionCall: &aistudio.FunctionCall{ + ID: search.CallID, Name: clientToolSearch, Arguments: functionCallArguments(string(arguments)), ThoughtSignature: pendingSignature, + }}}}) + pendingSignature = "" + continue + } + pendingSignature = "" + names := make([]string, 0, len(search.Tools)) + for _, tool := range search.Tools { + if tool.Type != "namespace" { + names = append(names, tool.Name) + } + for _, member := range tool.Tools { + names = append(names, namespacedName(tool.Name, member.Name)) + } + } + output, _ := json.Marshal(map[string]any{"status": cmp.Or(search.Status, "completed"), "tools": names}) + contents = append(contents, aistudio.Content{Role: aistudio.RoleTool, Parts: []aistudio.Part{{FunctionResult: &aistudio.FunctionResult{ + ID: search.CallID, Name: clientToolSearch, Content: output, + }}}}) + continue case "web_search_call", "code_interpreter_call", "image_generation_call", "file_search_call", - "mcp_call", "mcp_list_tools", "mcp_approval_request", "mcp_approval_response", "tool_search_call", "tool_search_output", "program", "program_output": + "mcp_call", "mcp_list_tools", "mcp_approval_request", "mcp_approval_response", "program", "program_output": pendingSignature = "" content, err := responsesHostedItem(kind, rawItem) if err != nil { @@ -1108,12 +1163,17 @@ func responsesContents(input splitInput) ([]aistudio.Content, []string, error) { if err != nil { return nil, nil, fmt.Errorf("%s: %w", item.Type, err) } + if item.Status == "incomplete" && item.Type != "apply_patch_call_output" { + output, _ = json.Marshal(map[string]any{"status": item.Status, "output": rawJSONValue(output, nil)}) + } contents = append(contents, aistudio.Content{Role: aistudio.RoleTool, Parts: append([]aistudio.Part{{FunctionResult: &aistudio.FunctionResult{ ID: item.CallID, Content: output, }}}, media...)}) continue case "reasoning": - pendingSignature = item.EncryptedContent + if item.EncryptedContent != "" { + pendingSignature = item.EncryptedContent + } continue default: return nil, nil, fmt.Errorf("unsupported input item type %q", item.Type) @@ -1148,27 +1208,35 @@ func agentMessageParts(raw json.RawMessage) ([]aistudio.Part, error) { return parts, nil } -// responsesInputConfig 返回输入项中 additional_tools 提供的工具与最后一个 configuration_update 的推理强度 -func responsesInputConfig(input splitInput) ([]responsesTool, string) { - if !input.hasType("additional_tools", "configuration_update") { - return nil, "" +// responsesInputConfig 返回 declared 加上输入项中 additional_tools 提供的工具与客户端 tool_search_output 找到的工具,以及最后一个 configuration_update 的推理强度 +func responsesInputConfig(declared []responsesTool, input splitInput) ([]responsesTool, string) { + tools := slices.Clone(declared) + if !input.hasType("additional_tools", "configuration_update", "tool_search_output") { + return tools, "" } var items []struct { Type string `json:"type"` + Execution string `json:"execution"` Tools []responsesTool `json:"tools"` Reasoning *struct { Effort string `json:"effort"` } `json:"reasoning"` } if json.Unmarshal(input.raw, &items) != nil { - return nil, "" + return tools, "" } - var tools []responsesTool effort := "" for _, item := range items { switch item.Type { case "additional_tools": tools = append(tools, item.Tools...) + case "tool_search_output": + if item.Execution != "" && item.Execution != "client" { + continue + } + for _, tool := range item.Tools { + tools = addFoundTool(tools, tool) + } case "configuration_update": if item.Reasoning != nil && item.Reasoning.Effort != "" { effort = item.Reasoning.Effort @@ -1178,6 +1246,26 @@ func responsesInputConfig(input splitInput) ([]responsesTool, string) { return tools, effort } +// addFoundTool 把 tool_search 找到的工具加入 tools:已有同类同名的工具时跳过,已有同名命名空间时加入其中没有的成员 +func addFoundTool(tools []responsesTool, tool responsesTool) []responsesTool { + index := slices.IndexFunc(tools, func(found responsesTool) bool { return found.Type == tool.Type && found.Name == tool.Name }) + if index < 0 { + return append(tools, tool) + } + if tool.Type != "namespace" { + return tools + } + namespace := tools[index] + namespace.Tools = slices.Clone(namespace.Tools) + for _, member := range tool.Tools { + if !slices.ContainsFunc(namespace.Tools, func(found responsesTool) bool { return found.Name == member.Name }) { + namespace.Tools = append(namespace.Tools, member) + } + } + tools[index] = namespace + return tools +} + // responsesItemType 读取输入项的 type 字段 func responsesItemType(raw json.RawMessage) string { var head struct { @@ -1402,7 +1490,18 @@ func mapResponsesTools(tools []responsesTool, choice json.RawMessage) (aistudio. if approval.never { mapped.MCPServers = append(mapped.MCPServers, server) } - case "image_generation", "file_search", "tool_search", "programmatic_tool_calling": + case "tool_search": + if tool.Execution != "client" { + continue + } + declaration := aistudio.FunctionDeclaration{Name: clientToolSearch, Description: cmp.Or(tool.Description, clientToolSearchDescription), Parameters: tool.Parameters} + if len(declaration.Parameters) == 0 { + declaration.Parameters = json.RawMessage(clientToolSearchParameters) + } + if err := addDeclaration(declaration); err != nil { + return aistudio.Tools{}, err + } + case "image_generation", "file_search", "programmatic_tool_calling": default: return aistudio.Tools{}, fmt.Errorf("unsupported tool type %q", tool.Type) } @@ -1541,6 +1640,9 @@ func responseFunctionCall(call aistudio.FunctionCall, tools []responsesTool) map } item := map[string]any{"id": "fc_" + call.ID, "status": "completed", "call_id": call.ID} switch responsesToolKind(tools, call.Name) { + case "tool_search": + item["id"], item["type"], item["execution"], item["arguments"] = "tsc_"+call.ID, "tool_search_call", "client", rawJSONValue(call.Arguments, map[string]any{}) + return item case "local_shell": item["type"], item["action"] = "local_shell_call", localShellAction(call.Arguments) return item @@ -1561,17 +1663,23 @@ func responseFunctionCall(call aistudio.FunctionCall, tools []responsesTool) map item["type"], item["actions"], item["pending_safety_checks"] = "computer_call", arguments.Actions, []any{} return item case "custom": - item["type"], item["name"], item["input"] = "custom_tool_call", call.Name, customToolInput(call.Arguments) + item["id"], item["type"], item["name"], item["input"] = "ctc_"+call.ID, "custom_tool_call", call.Name, customToolInput(call.Arguments) default: item["type"], item["name"], item["arguments"] = "function_call", call.Name, string(call.Arguments) } if namespace := responsesNamespace(tools, call.Name); namespace != "" { item["namespace"] = namespace item["name"] = strings.TrimPrefix(call.Name, namespace+".") + if namespace == "collaboration" && item["type"] == "function_call" && slices.Contains(collaborationPlaintextCalls, item["name"].(string)) { + item["encrypted_function_args"] = []string{} + } } return item } +// collaborationPlaintextCalls 是 Codex multi_agent_v2 collaboration 命名空间中以空 encrypted_function_args 标明参数为明文的函数 +var collaborationPlaintextCalls = []string{"spawn_agent", "send_message", "followup_task"} + // namespacedName 返回命名空间中函数的声明名:默认命名空间 functions 中的函数与顶层函数相同,用原名,其余为 <命名空间>.<名> func namespacedName(namespace, name string) string { if namespace == "" || namespace == "functions" { @@ -1682,7 +1790,7 @@ func (o *responsesOutput) add(event aistudio.Event) ([]map[string]any, error) { write(o.reasoning, o.reasoning["summary"].([]any)[0].(map[string]any), event.Text) case aistudio.EventText: if !isLast(o.message) { - o.message = map[string]any{"id": o.itemID("msg_"), "type": "message", "status": "completed", "role": "assistant", "content": []any{map[string]any{"type": "output_text", "text": "", "annotations": []any{}}}} + o.message = map[string]any{"id": o.itemID("msg_"), "type": "message", "status": "completed", "role": "assistant", "content": []any{map[string]any{"type": "output_text", "text": "", "annotations": []any{}, "logprobs": []any{}}}} o.texts[o.message["id"].(string)] = &strings.Builder{} o.starts[o.message["id"].(string)] = o.text push(o.message) @@ -1879,11 +1987,11 @@ func outputPrefix(compaction map[string]any, prepared responsesPreparation) []ma // newResponsesWriter 返回 Responses 输出写出器 func newResponsesWriter(w http.ResponseWriter, id string, created int64, request responsesRequest) *responsesStreamWriter { - additional, _ := responsesInputConfig(request.inputItems()) + tools, _ := responsesInputConfig(request.Tools, request.inputItems()) return &responsesStreamWriter{ w: w, id: id, created: created, request: request, output: &responsesOutput{ - id: id, request: request, tools: slices.Concat(request.Tools, additional), texts: make(map[string]*strings.Builder), + id: id, request: request, tools: tools, texts: make(map[string]*strings.Builder), starts: make(map[string]int), queries: make(map[string]struct{}), pages: make(map[string]struct{}), mcpCalls: make(map[string]aistudio.ServerToolStep), }, } @@ -2062,7 +2170,7 @@ func (writer *responsesStreamWriter) added(item map[string]any) error { writer.open = append(writer.open, item) return writer.emit("response.content_part.added", map[string]any{ "item_id": item["id"], "output_index": index, "content_index": 0, - "part": map[string]any{"type": "output_text", "text": "", "annotations": []any{}}, + "part": map[string]any{"type": "output_text", "text": "", "annotations": []any{}, "logprobs": []any{}}, }) case item["type"] == "reasoning": pending := maps.Clone(item) @@ -2326,8 +2434,11 @@ func responsesError(err error) map[string]any { return map[string]any{"code": responsesErrorCode(err), "message": retryMessage(publicErrorMessage(statusFromError(err), publicReason(err)), retryAfterSeconds(err))} } -// responsesErrorCode 返回 Responses 错误对象的官方枚举 code +// responsesErrorCode 返回 Responses 错误对象的官方枚举 code,输入 token 超限时为 Codex 识别的 context_length_exceeded func responsesErrorCode(err error) string { + if contextOverflow(err) { + return "context_length_exceeded" + } switch statusFromError(err) { case http.StatusTooManyRequests: return "rate_limit_exceeded" diff --git a/internal/api/router.go b/internal/api/router.go index cb9a38ee..c6afba75 100644 --- a/internal/api/router.go +++ b/internal/api/router.go @@ -157,7 +157,7 @@ func NewHandler(service aistudio.Service, config Config) http.Handler { root := http.NewServeMux() root.Handle("GET /health", corsMiddleware(http.HandlerFunc(s.handleHealth))) - publicHandler := bodyLimitMiddleware(browserOriginMiddleware(config.APIKey, authMiddleware(config.APIKey, apiKeyContextMiddleware(config.APIKey, accountContextMiddleware(requestBodyMiddleware(config.Ledger, waitLaunchMiddleware(config.WaitLaunch, s.catalogRead, public))))))) + publicHandler := bodyLimitMiddleware(browserOriginMiddleware(config.APIKey, authMiddleware(config.APIKey, apiKeyContextMiddleware(config.APIKey, accountContextMiddleware(requestEncodingMiddleware(requestBodyMiddleware(config.Ledger, waitLaunchMiddleware(config.WaitLaunch, s.catalogRead, public)))))))) loggedHandler := requestLoggingMiddleware(config.Admin, corsMiddleware(publicHandler)) root.Handle("/v1/", loggedHandler) root.Handle("/v1beta/", loggedHandler) diff --git a/internal/app/admin.go b/internal/app/admin.go index 0f93ad1e..ba4e2d9f 100644 --- a/internal/app/admin.go +++ b/internal/app/admin.go @@ -365,7 +365,7 @@ func (admin *runtimeAdmin) DiscoverCookieAccounts(ctx context.Context, input api admin.requests.log("auth", "ERROR", "识别 Cookie 中的账户失败", "error", err) return nil, invalidAccount(err) } - return cookieAccountEntries(api.CookieBrowser{DeviceBound: deviceBoundSession(state)}, accounts, admin.accountIDs()), nil + return cookieAccountEntries(api.CookieBrowser{DeviceBound: deviceBoundSession(state)}, accounts, importedAccountIDs(admin.pool.Status(), true)), nil } // deviceBoundSession 表示 Cookie 带 __Secure-1PSIDRTS,会话绑定签发它的设备 @@ -395,9 +395,14 @@ func cookieAccountEntries(template api.CookieBrowser, accounts []chromeauth.Cook // accountIDs 返回已导入且不需要重新登录的账户小写 ID 与其服务受限的原因,没有受限时原因为空 func (admin *runtimeAdmin) accountIDs() map[string]string { + return importedAccountIDs(admin.pool.Status(), false) +} + +// importedAccountIDs 返回 statuses 中已导入账户的小写 ID 与其服务受限的原因,signInRequired 为假时不含需要重新登录的账户 +func importedAccountIDs(statuses []aistudio.AccountStatus, signInRequired bool) map[string]string { existing := make(map[string]string) - for _, status := range admin.pool.Status() { - if status.State == aistudio.AccountAuthRequired { + for _, status := range statuses { + if status.State == aistudio.AccountAuthRequired && !signInRequired { continue } restricted := "" diff --git a/internal/app/embeddings.go b/internal/app/embeddings.go index 0fa97799..ef31ae92 100644 --- a/internal/app/embeddings.go +++ b/internal/app/embeddings.go @@ -59,6 +59,7 @@ func (service *trackedService) Embed(ctx context.Context, request aistudio.Embed service.requests.markChannel(request.ID, string(aistudio.ChannelBuild)) }) result, err := service.service.(aistudio.EmbeddingService).Embed(observed, request) + err = endedRequestError(requestCtx, err) if err == nil { api.SetAccessLogGenerationResult(requestCtx, &aistudio.Usage{InputTokens: result.TokenCount, TotalTokens: result.TokenCount}, 0) } diff --git a/internal/app/files.go b/internal/app/files.go index b964f76a..647253b4 100644 --- a/internal/app/files.go +++ b/internal/app/files.go @@ -15,6 +15,7 @@ func (service *trackedService) UploadFile(ctx context.Context, request aistudio. } defer cancel() file, requestErr := service.service.(aistudio.FileService).UploadFile(requestCtx, request) + requestErr = endedRequestError(requestCtx, requestErr) api.SetAccessLogError(requestCtx, requestErr) return file, requestErr } @@ -27,6 +28,7 @@ func (service *trackedService) FileMetadata(ctx context.Context, fileID string) } defer cancel() metadata, requestErr := service.service.(aistudio.FileService).FileMetadata(requestCtx, fileID) + requestErr = endedRequestError(requestCtx, requestErr) api.SetAccessLogError(requestCtx, requestErr) return metadata, requestErr } @@ -39,6 +41,7 @@ func (service *trackedService) ListFiles(ctx context.Context) ([]aistudio.FileMe } defer cancel() list, requestErr := service.service.(aistudio.FileService).ListFiles(requestCtx) + requestErr = endedRequestError(requestCtx, requestErr) api.SetAccessLogError(requestCtx, requestErr) return list, requestErr } @@ -51,6 +54,7 @@ func (service *trackedService) DeleteFile(ctx context.Context, fileID string) er } defer cancel() requestErr := service.service.(aistudio.FileService).DeleteFile(requestCtx, fileID) + requestErr = endedRequestError(requestCtx, requestErr) api.SetAccessLogError(requestCtx, requestErr) return requestErr } @@ -63,6 +67,7 @@ func (service *trackedService) VideoFrameTokens(ctx context.Context, request ais } defer cancel() tokens, requestErr := service.service.(aistudio.VideoFrameService).VideoFrameTokens(requestCtx, request) + requestErr = endedRequestError(requestCtx, requestErr) api.SetAccessLogError(requestCtx, requestErr) return tokens, requestErr } diff --git a/internal/app/runtime.go b/internal/app/runtime.go index 83962601..b96b8351 100644 --- a/internal/app/runtime.go +++ b/internal/app/runtime.go @@ -778,14 +778,21 @@ func (service *trackedService) withAliases(models []aistudio.Model) []aistudio.M return models } -// mappedModel 返回 MODEL_MAPPING 中别名映射到的模型名,别名带思考后缀时该后缀替换目标自带的后缀,不是别名时原样返回 +// oneMillionContextSuffix 是 Claude Code 1M 上下文模式加在模型名末尾的标记,按去掉它的模型处理 +const oneMillionContextSuffix = "[1m]" + +// mappedModel 返回 MODEL_MAPPING 中别名映射到的模型名,别名带思考后缀时该后缀替换目标自带的后缀,不是别名时原样返回;完整名称不是别名时去掉末尾的 [1m] 再映射 func (service *trackedService) mappedModel(name string) string { - trimmed := strings.TrimPrefix(strings.TrimSpace(name), "models/") + name = strings.TrimSpace(name) + trimmed := strings.TrimPrefix(name, "models/") for _, alias := range service.aliases() { if alias.Alias == trimmed { return alias.Model } } + if strings.HasSuffix(strings.ToLower(trimmed), oneMillionContextSuffix) { + return service.mappedModel(name[:len(name)-len(oneMillionContextSuffix)]) + } open := strings.LastIndex(trimmed, "(") if open <= 0 || !strings.HasSuffix(trimmed, ")") { return name @@ -1430,6 +1437,7 @@ func (service *trackedService) CountTokens(ctx context.Context, request aistudio } defer cancel() count, requestErr := service.service.CountTokens(requestCtx, request) + requestErr = endedRequestError(requestCtx, requestErr) api.SetAccessLogError(requestCtx, requestErr) return count, requestErr } @@ -1442,7 +1450,8 @@ func (service *trackedService) GenerateTitle(ctx context.Context, request aistud return "", err } defer cancel() - return service.service.(aistudio.TitleService).GenerateTitle(requestCtx, request) + title, err := service.service.(aistudio.TitleService).GenerateTitle(requestCtx, request) + return title, endedRequestError(requestCtx, err) } // GenerateImage 生成 Imagen 图片并登记请求目标与账户 @@ -1454,6 +1463,7 @@ func (service *trackedService) GenerateImage(ctx context.Context, request aistud } defer cancel() images, requestErr := service.service.(aistudio.ImageService).GenerateImage(requestCtx, request) + requestErr = endedRequestError(requestCtx, requestErr) api.SetAccessLogError(requestCtx, requestErr) return images, requestErr } @@ -1467,6 +1477,7 @@ func (service *trackedService) CreateDecision(ctx context.Context, request aistu } defer cancel() result, requestErr := service.service.(aistudio.DecisionService).CreateDecision(requestCtx, request) + requestErr = endedRequestError(requestCtx, requestErr) api.SetAccessLogError(requestCtx, requestErr) service.requests.log("service", "WARN", "Decision 请求使用 AI Studio 官网中隐藏的模型", "model", request.Model) return result, requestErr @@ -1495,6 +1506,7 @@ func (service *trackedService) GenerateVideo(ctx context.Context, request aistud return service.dataRequestContext(context.Background()) } operation, requestErr := service.service.(aistudio.VideoService).GenerateVideo(requestCtx, request) + requestErr = endedRequestError(requestCtx, requestErr) api.SetAccessLogError(requestCtx, requestErr) return operation, requestErr } @@ -1556,6 +1568,7 @@ func (service *trackedService) GetGenerateVideoOperation(ctx context.Context, op } defer cancel() operation, requestErr := service.service.(aistudio.VideoService).GetGenerateVideoOperation(requestCtx, operationID) + requestErr = endedRequestError(requestCtx, requestErr) api.SetAccessLogError(requestCtx, requestErr) return operation, requestErr } @@ -1568,6 +1581,7 @@ func (service *trackedService) ListVideos(ctx context.Context) ([]aistudio.Video } defer cancel() videos, requestErr := service.service.(aistudio.VideoService).ListVideos(requestCtx) + requestErr = endedRequestError(requestCtx, requestErr) api.SetAccessLogError(requestCtx, requestErr) return videos, requestErr } @@ -1580,6 +1594,7 @@ func (service *trackedService) DeleteVideo(ctx context.Context, operationID stri } defer cancel() requestErr := service.service.(aistudio.VideoService).DeleteVideo(requestCtx, operationID) + requestErr = endedRequestError(requestCtx, requestErr) api.SetAccessLogError(requestCtx, requestErr) return requestErr } @@ -1591,6 +1606,7 @@ func (service *trackedService) DownloadFile(ctx context.Context, fileID string) return aistudio.MediaStream{}, err } media, requestErr := service.service.(aistudio.VideoService).DownloadFile(requestCtx, fileID) + requestErr = endedRequestError(requestCtx, requestErr) api.SetAccessLogError(requestCtx, requestErr) if requestErr != nil { cancel() @@ -2053,6 +2069,7 @@ func (service *trackedService) Generate(ctx context.Context, request aistudio.Ge service.requests.start(request, cancel) resourceID, err := service.pool.ResourceIDForContents(requestCtx, request.Contents) if err != nil { + err = endedRequestError(requestCtx, err) api.SetAccessLogError(requestCtx, err) cancel() service.requests.finish(request.ID, finalRequestState(err)) @@ -2386,6 +2403,9 @@ func (service *trackedService) generateWithRetry( if activity != nil { api.SetAccessLogUpstreamBytes(requestCtx, activity.bytes.Load()) } + if cause := requestEndCause(requestCtx); cause != nil { + err = cause + } err = accountsUnavailable(err, retry.rejectionUntil) api.SetAccessLogError(requestCtx, err) service.requests.finish(request.ID, finalRequestState(err)) @@ -2509,6 +2529,9 @@ func (service *trackedService) runAttempt( service.requests.logRequestProgress(request.ID, accountLabel, "WARN", "请求准备等待", "waited", waited, "current_step", current, "model", modelID) }) ctx = aistudio.ContextWithStreamActivityObserver(ctx, attempt.activity.observe) + ctx = aistudio.ContextWithRegenerationObserver(ctx, func(regenerated int, reason string) { + service.requests.logRequestProgress(request.ID, accountLabel, "WARN", "重新生成", "attempt", regenerated, "model", modelID, "reason", reason) + }) ctx = aistudio.ContextWithRequestPhaseObserver(ctx, prepareTiming.observe) if err == nil { attempt.source, err = service.service.Generate(ctx, request) @@ -2998,84 +3021,141 @@ func (service *trackedService) lifecycleRequestContext(ctx context.Context) (con } } -// dataRequestContext 返回绑定数据面生命周期、期限为 REQUEST_TIMEOUT 的请求上下文,自动续写每开始一轮期限重新计时 +// dataRequestContext 返回绑定数据面生命周期的请求上下文,上游连续 REQUEST_TIMEOUT 没有数据时到期 func (service *trackedService) dataRequestContext(ctx context.Context) (context.Context, context.CancelFunc, error) { service.lifecycleMu.Lock() if service.state.Load() != serviceRunning || service.dataContext == nil { service.lifecycleMu.Unlock() return nil, nil, service.stoppedError() } - deadline, cancel := newRoundDeadlineContext(ctx, service.timeout) run := service.dataContext - stopData := context.AfterFunc(run, cancel) + deadline, cancel := newIdleDeadlineContext(ctx, run, service.timeout) + stopData := context.AfterFunc(run, func() { deadline.finish(context.Canceled, &serviceStoppedError{}) }) service.lifecycleMu.Unlock() - return aistudio.ContextWithServiceRun(aistudio.ContextWithContinuationObserver(deadline, deadline.extend), run), func() { + return aistudio.ContextWithServiceRun(aistudio.ContextWithStreamActivityObserver(deadline, deadline.observe), run), func() { stopData() cancel() }, nil } -// roundDeadlineContext 是期限可在自动续写每轮开始时重新计时的请求上下文,到期时 Err 为 context.DeadlineExceeded -type roundDeadlineContext struct { - parent context.Context - timeout time.Duration - done chan struct{} - mu sync.Mutex - timer *time.Timer - stop func() bool - deadline time.Time - err error +// idleDeadlineContext 是上游连续 timeout 没有数据时到期的请求上下文,请求开始到首个字节同样计入,到期时 Err 为 context.DeadlineExceeded +// 约束:active 为最近一次收到上游数据距 started 的纳秒数,由读取上游响应的 goroutine 并发写入 +type idleDeadlineContext struct { + parent context.Context + timeout time.Duration + started time.Time + active atomic.Int64 + done chan struct{} + mu sync.Mutex + timer *time.Timer + stop func() bool + err error + // cause 是到期或生成服务停止时说明原因的错误,其余结束方式为 nil + cause error + // run 是请求所属的生成服务数据面上下文 + run context.Context +} + +type idleDeadlineKey struct{} + +// upstreamIdleError 是上游连续 REQUEST_TIMEOUT 没有数据时请求的结束原因 +type upstreamIdleError struct { + timeout time.Duration +} + +func (e *upstreamIdleError) Error() string { + return fmt.Sprintf("上游连续 %s 没有数据,超过 REQUEST_TIMEOUT", e.timeout) +} + +func (e *upstreamIdleError) Is(target error) bool { + return target == context.DeadlineExceeded +} + +// requestEndCause 返回请求上下文结束的原因:上游连续没有数据超过 REQUEST_TIMEOUT 或生成服务停止时为说明原因的错误,其余为 ctx.Err() +func requestEndCause(ctx context.Context) error { + if deadline, ok := ctx.Value(idleDeadlineKey{}).(*idleDeadlineContext); ok { + deadline.mu.Lock() + cause := deadline.cause + deadline.mu.Unlock() + if cause != nil { + return cause + } + if deadline.run != nil && deadline.run.Err() != nil { + return &serviceStoppedError{} + } + } + return ctx.Err() +} + +// endedRequestError 在 err 为上下文取消或到期时返回请求上下文结束的原因,其余原样返回 err +func endedRequestError(ctx context.Context, err error) error { + if cause := requestEndCause(ctx); cause != nil && (errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)) { + return cause + } + return err } -// newRoundDeadlineContext 返回期限为 timeout、随 parent 结束的请求上下文 -func newRoundDeadlineContext(parent context.Context, timeout time.Duration) (*roundDeadlineContext, context.CancelFunc) { - ctx := &roundDeadlineContext{parent: parent, timeout: timeout, done: make(chan struct{}), deadline: time.Now().Add(timeout)} +// newIdleDeadlineContext 返回上游连续 timeout 没有数据时到期、随 parent 结束的请求上下文,run 为请求所属的生成服务数据面上下文 +func newIdleDeadlineContext(parent context.Context, run context.Context, timeout time.Duration) (*idleDeadlineContext, context.CancelFunc) { + ctx := &idleDeadlineContext{parent: parent, run: run, timeout: timeout, started: time.Now(), done: make(chan struct{})} ctx.mu.Lock() - ctx.timer = time.AfterFunc(timeout, func() { ctx.finish(context.DeadlineExceeded) }) - ctx.stop = context.AfterFunc(parent, func() { ctx.finish(parent.Err()) }) + ctx.timer = time.AfterFunc(timeout, ctx.expire) + ctx.stop = context.AfterFunc(parent, func() { ctx.finish(parent.Err(), nil) }) ctx.mu.Unlock() - return ctx, func() { ctx.finish(context.Canceled) } + return ctx, func() { ctx.finish(context.Canceled, nil) } } -func (ctx *roundDeadlineContext) finish(err error) { - ctx.mu.Lock() - defer ctx.mu.Unlock() - if ctx.err != nil { - return - } - ctx.err = err - ctx.timer.Stop() - ctx.stop() - close(ctx.done) +// observe 记录收到上游数据的时刻 +func (ctx *idleDeadlineContext) observe(int) { + ctx.active.Store(int64(time.Since(ctx.started))) } -// extend 把期限重新计为 timeout -func (ctx *roundDeadlineContext) extend() { +// expire 在计时到达时结束请求,计时期间收到过上游数据时改为从最近一次数据起再计 timeout +func (ctx *idleDeadlineContext) expire() { + remaining := time.Duration(ctx.active.Load()) + ctx.timeout - time.Since(ctx.started) + if remaining <= 0 { + ctx.finish(context.DeadlineExceeded, &upstreamIdleError{timeout: ctx.timeout}) + return + } ctx.mu.Lock() defer ctx.mu.Unlock() if ctx.err == nil { - ctx.deadline = time.Now().Add(ctx.timeout) - ctx.timer.Reset(ctx.timeout) + ctx.timer.Reset(remaining) } } -func (ctx *roundDeadlineContext) Deadline() (time.Time, bool) { +// finish 以 err 结束请求上下文,cause 为说明原因的错误 +func (ctx *idleDeadlineContext) finish(err error, cause error) { ctx.mu.Lock() defer ctx.mu.Unlock() - return ctx.deadline, true + if ctx.err != nil { + return + } + ctx.err, ctx.cause = err, cause + ctx.timer.Stop() + ctx.stop() + close(ctx.done) } -func (ctx *roundDeadlineContext) Done() <-chan struct{} { +// Deadline 返回最近一次收到上游数据之后 timeout 的时刻 +func (ctx *idleDeadlineContext) Deadline() (time.Time, bool) { + return ctx.started.Add(time.Duration(ctx.active.Load()) + ctx.timeout), true +} + +func (ctx *idleDeadlineContext) Done() <-chan struct{} { return ctx.done } -func (ctx *roundDeadlineContext) Err() error { +func (ctx *idleDeadlineContext) Err() error { ctx.mu.Lock() defer ctx.mu.Unlock() return ctx.err } -func (ctx *roundDeadlineContext) Value(key any) any { +func (ctx *idleDeadlineContext) Value(key any) any { + if key == (idleDeadlineKey{}) { + return ctx + } return ctx.parent.Value(key) } @@ -3109,7 +3189,7 @@ func (service *trackedService) forwardEvents( defer stallTimer.Stop() var stall <-chan time.Time finishFromContext := func() { - requestErr = requestCtx.Err() + requestErr = requestEndCause(requestCtx) state = finalRequestState(requestErr) if requestErr == nil || terminal || clientCtx.Err() != nil { return @@ -3224,6 +3304,7 @@ func (service *trackedService) forwardEvents( continue } if event.Kind == aistudio.EventError { + event.Err = endedRequestError(requestCtx, event.Err) event.Err = service.settleStreamError(requestCtx, requestID, requestedModelID, serving.Load(), event.Err) requestErr = event.Err state = finalRequestState(event.Err) diff --git a/internal/app/transcriptions.go b/internal/app/transcriptions.go index b5865b72..dcd417ff 100644 --- a/internal/app/transcriptions.go +++ b/internal/app/transcriptions.go @@ -81,6 +81,7 @@ func (service *trackedService) Transcribe( service.requests.markRunning(request.ID, account.ID, account.Config.Label) }) result, err := service.service.(aistudio.TranscriptionService).Transcribe(observed, request) + err = endedRequestError(requestCtx, err) api.SetAccessLogFirstEvent(requestCtx, result.FirstEvent, 0) api.SetAccessLogGenerationResult( requestCtx, &result.Usage, 0, diff --git a/internal/i18n/messages/de.json b/internal/i18n/messages/de.json index 84ef3dbe..984fc361 100644 --- a/internal/i18n/messages/de.json +++ b/internal/i18n/messages/de.json @@ -7,7 +7,6 @@ "%s 必须是正数时长,例如 30s 或 5m": "{0} muss eine positive Zeitspanne sein, z. B. 30s oder 5m", "%s 必须是正整数": "{0} muss eine positive Ganzzahl sein", "%s 未协商 HTTP/2,实际协议为 %q": "{0} hat HTTP/2 nicht ausgehandelt, tatsächliches Protokoll ist {1}", - "%s 没有可用地址": "{0} hat keine verfügbaren Adressen", "%s 状态为 %s": "Status von {0} ist {1}", "%s 的 token_service 没有 %s 的记录": "token_service von {0} hat keinen Eintrag für {1}", "%s 的 token_service 缺少认证材料": "token_service von {0} fehlen Authentifizierungsdaten", @@ -247,14 +246,8 @@ "Google AI Pro 或 Ultra 订阅": "Google AI Pro- oder Ultra-Abo", "Google AI Ultra 订阅": "Google AI Ultra-Abo", "Google 多账号会话序号 (0..9,对应 /u/0、/u/1...)": "Google-Mehrkonten-Sitzungsindex (0..9, entspricht /u/0, /u/1...)", - "HTTP/3 响应体已关闭": "HTTP/3-Antworttext ist bereits geschlossen", - "HTTP/3 响应在 HEADERS 之前收到 DATA": "HTTP/3-Antwort empfing DATA vor HEADERS", - "HTTP/3 响应状态 %d 无效": "HTTP/3-Antwortstatus {0} ist ungültig", - "HTTP/3 帧长度 %d 超出上限": "HTTP/3-Framelänge {0} überschreitet das Limit", - "HTTP/3 控制流首帧不是 SETTINGS": "Erster Frame im HTTP/3-Steuerstream ist nicht SETTINGS", "INIT_TIMEOUT 必须是正数时长": "INIT_TIMEOUT muss eine positive Zeitdauer sein", "INIT_TIMEOUT 无效: %w": "INIT_TIMEOUT ist ungültig: {0}", - "Initial 包放不下 CRYPTO 帧": "Initial-Paket kann den CRYPTO-Frame nicht aufnehmen", "Interaction 模型不接受 %s 消息": "Interaction-Modell akzeptiert keine {0}-Nachrichten", "Interaction 模型的 %w": "{0} des Interaction-Modells", "Interaction 模型的 contents[%d] 不接受视频截取、采样、处理方式与处理步骤": "contents[{0}] des Interaction-Modells akzeptiert kein Video-Clipping, Sampling, Verarbeitungsverfahren oder Verarbeitungsschritte", @@ -296,8 +289,6 @@ "PROXY 认证信息缺少用户名": "PROXY-Authentifizierung enthält keinen Benutzernamen", "Playground GenerateContent 使用服务端流,收集完成后返回": "Playground GenerateContent verwendet serverseitiges Streaming und antwortet nach Abschluss der Erfassung", "Playwright storage state 文件": "Playwright-storage-state-Datei", - "QPACK 数据无效": "Ungültige QPACK-Daten", - "QPACK 阻塞流超过 %d": "Blockierte QPACK-Streams überschreiten {0}", "REQUEST_TIMEOUT 必须是正数时长": "REQUEST_TIMEOUT muss eine positive Dauer sein", "REQUEST_TIMEOUT 无效: %w": "REQUEST_TIMEOUT ist ungültig: {0}", "ROUTING_STRATEGY 必须是 round-robin 或 fill-first": "ROUTING_STRATEGY muss round-robin oder fill-first sein", @@ -1504,5 +1495,8 @@ "页面缺少 gapi client 模块地址": "Der Seite fehlt die Adresse des gapi-client-Moduls", "关闭重新载入前的页面失败": "Seite von vor dem Neuladen konnte nicht geschlossen werden", "设置收缩模式: %w": "Komprimierungsmodus festlegen: {0}", - "预先打开页面失败": "Seite konnte nicht vorab geöffnet werden" + "预先打开页面失败": "Seite konnte nicht vorab geöffnet werden", + "重新生成": "Neu generieren", + "结束原因 %s": "Abschlussgrund {0}", + "上游连续 %s 没有数据,超过 REQUEST_TIMEOUT": "Seit {0} keine Daten vom Upstream, REQUEST_TIMEOUT überschritten" } diff --git a/internal/i18n/messages/en.json b/internal/i18n/messages/en.json index 79289080..702a1eb6 100644 --- a/internal/i18n/messages/en.json +++ b/internal/i18n/messages/en.json @@ -7,7 +7,6 @@ "%s 必须是正数时长,例如 30s 或 5m": "{0} must be a positive duration, such as 30s or 5m", "%s 必须是正整数": "{0} must be a positive integer", "%s 未协商 HTTP/2,实际协议为 %q": "{0} did not negotiate HTTP/2, actual protocol was {1}", - "%s 没有可用地址": "{0} has no available addresses", "%s 状态为 %s": "{0} status is {1}", "%s 的 token_service 没有 %s 的记录": "{0} token_service has no record for {1}", "%s 的 token_service 缺少认证材料": "{0} token_service is missing credentials", @@ -247,14 +246,8 @@ "Google AI Pro 或 Ultra 订阅": "Google AI Pro or Ultra subscription", "Google AI Ultra 订阅": "Google AI Ultra subscription", "Google 多账号会话序号 (0..9,对应 /u/0、/u/1...)": "Google multi-account session index (0..9, for /u/0, /u/1...)", - "HTTP/3 响应体已关闭": "HTTP/3 response body already closed", - "HTTP/3 响应在 HEADERS 之前收到 DATA": "HTTP/3 response received DATA before HEADERS", - "HTTP/3 响应状态 %d 无效": "Invalid HTTP/3 response status {0}", - "HTTP/3 帧长度 %d 超出上限": "HTTP/3 frame length {0} exceeds limit", - "HTTP/3 控制流首帧不是 SETTINGS": "First frame of HTTP/3 control stream is not SETTINGS", "INIT_TIMEOUT 必须是正数时长": "INIT_TIMEOUT must be a positive duration", "INIT_TIMEOUT 无效: %w": "Invalid INIT_TIMEOUT: {0}", - "Initial 包放不下 CRYPTO 帧": "Initial packet cannot fit CRYPTO frame", "Interaction 模型不接受 %s 消息": "Interaction models do not accept {0} messages", "Interaction 模型的 %w": "{0} of Interaction model", "Interaction 模型的 contents[%d] 不接受视频截取、采样、处理方式与处理步骤": "Interaction model contents[{0}] does not accept video clipping, sampling, processing mode, or processing steps", @@ -296,8 +289,6 @@ "PROXY 认证信息缺少用户名": "PROXY credentials missing username", "Playground GenerateContent 使用服务端流,收集完成后返回": "Playground GenerateContent uses server-side streaming, returning after collection is complete", "Playwright storage state 文件": "Playwright storage state file", - "QPACK 数据无效": "Invalid QPACK data", - "QPACK 阻塞流超过 %d": "QPACK blocked streams exceeded {0}", "REQUEST_TIMEOUT 必须是正数时长": "REQUEST_TIMEOUT must be a positive duration", "REQUEST_TIMEOUT 无效: %w": "Invalid REQUEST_TIMEOUT: {0}", "ROUTING_STRATEGY 必须是 round-robin 或 fill-first": "ROUTING_STRATEGY must be round-robin or fill-first", @@ -1504,5 +1495,8 @@ "页面缺少 gapi client 模块地址": "The page is missing the gapi client module URL", "关闭重新载入前的页面失败": "Failed to close the page from before the reload", "设置收缩模式: %w": "Set vacuum mode: {0}", - "预先打开页面失败": "Failed to open a spare page in advance" + "预先打开页面失败": "Failed to open a spare page in advance", + "重新生成": "Regenerating", + "结束原因 %s": "Finish reason {0}", + "上游连续 %s 没有数据,超过 REQUEST_TIMEOUT": "No data from upstream for {0}, exceeding REQUEST_TIMEOUT" } diff --git a/internal/i18n/messages/fr.json b/internal/i18n/messages/fr.json index 7f732e8f..3bfcac4d 100644 --- a/internal/i18n/messages/fr.json +++ b/internal/i18n/messages/fr.json @@ -7,7 +7,6 @@ "%s 必须是正数时长,例如 30s 或 5m": "{0} doit être une durée positive, par exemple 30s ou 5m", "%s 必须是正整数": "{0} doit être un entier positif", "%s 未协商 HTTP/2,实际协议为 %q": "{0} n’a pas négocié HTTP/2, protocole effectif : {1}", - "%s 没有可用地址": "{0} n’a aucune adresse disponible", "%s 状态为 %s": "État de {0} : {1}", "%s 的 token_service 没有 %s 的记录": "Le token_service de {0} n’a aucun enregistrement pour {1}", "%s 的 token_service 缺少认证材料": "Le token_service de {0} manque d’éléments d’authentification", @@ -247,14 +246,8 @@ "Google AI Pro 或 Ultra 订阅": "Abonnement Google AI Pro ou Ultra", "Google AI Ultra 订阅": "Abonnement Google AI Ultra", "Google 多账号会话序号 (0..9,对应 /u/0、/u/1...)": "Index de session Google multi-comptes (0..9, correspond à /u/0, /u/1...)", - "HTTP/3 响应体已关闭": "Le corps de réponse HTTP/3 est fermé", - "HTTP/3 响应在 HEADERS 之前收到 DATA": "Réponse HTTP/3 : DATA reçu avant HEADERS", - "HTTP/3 响应状态 %d 无效": "Code d’état de réponse HTTP/3 {0} non valide", - "HTTP/3 帧长度 %d 超出上限": "Longueur de trame HTTP/3 {0} supérieure à la limite", - "HTTP/3 控制流首帧不是 SETTINGS": "La première trame du flux de contrôle HTTP/3 n’est pas SETTINGS", "INIT_TIMEOUT 必须是正数时长": "INIT_TIMEOUT doit être une durée positive", "INIT_TIMEOUT 无效: %w": "INIT_TIMEOUT non valide : {0}", - "Initial 包放不下 CRYPTO 帧": "Le paquet Initial ne peut pas contenir la trame CRYPTO", "Interaction 模型不接受 %s 消息": "Les modèles Interaction n’acceptent pas les messages {0}", "Interaction 模型的 %w": "{0} du modèle Interaction", "Interaction 模型的 contents[%d] 不接受视频截取、采样、处理方式与处理步骤": "contents[{0}] du modèle Interaction n’accepte pas le découpage vidéo, l’échantillonnage, le mode de traitement ni les étapes de traitement", @@ -296,8 +289,6 @@ "PROXY 认证信息缺少用户名": "Nom d'utilisateur manquant dans les identifiants PROXY", "Playground GenerateContent 使用服务端流,收集完成后返回": "Playground GenerateContent utilise un flux côté serveur et retourne après collecte", "Playwright storage state 文件": "Fichier storage state de Playwright", - "QPACK 数据无效": "Données QPACK non valides", - "QPACK 阻塞流超过 %d": "Flux bloqués QPACK dépassant {0}", "REQUEST_TIMEOUT 必须是正数时长": "REQUEST_TIMEOUT doit être une durée positive", "REQUEST_TIMEOUT 无效: %w": "REQUEST_TIMEOUT non valide : {0}", "ROUTING_STRATEGY 必须是 round-robin 或 fill-first": "ROUTING_STRATEGY doit être round-robin ou fill-first", @@ -1504,5 +1495,8 @@ "页面缺少 gapi client 模块地址": "Adresse du module gapi client manquante dans la page", "关闭重新载入前的页面失败": "Échec de la fermeture de la page d’avant le rechargement", "设置收缩模式: %w": "Définir le mode de compactage : {0}", - "预先打开页面失败": "Échec de l’ouverture anticipée d’une page" + "预先打开页面失败": "Échec de l’ouverture anticipée d’une page", + "重新生成": "Nouvelle génération", + "结束原因 %s": "Motif de fin {0}", + "上游连续 %s 没有数据,超过 REQUEST_TIMEOUT": "Aucune donnée de l’amont pendant {0}, au-delà de REQUEST_TIMEOUT" } diff --git a/internal/i18n/messages/ja.json b/internal/i18n/messages/ja.json index 53aeed18..2e0f7828 100644 --- a/internal/i18n/messages/ja.json +++ b/internal/i18n/messages/ja.json @@ -7,7 +7,6 @@ "%s 必须是正数时长,例如 30s 或 5m": "{0} は 30s や 5m などの正の期間である必要があります", "%s 必须是正整数": "{0} は正の整数である必要があります", "%s 未协商 HTTP/2,实际协议为 %q": "{0} で HTTP/2 がネゴシエートされませんでした。実際のプロトコル: {1}", - "%s 没有可用地址": "{0} に利用可能なアドレスがありません", "%s 状态为 %s": "{0} の状態は {1}", "%s 的 token_service 没有 %s 的记录": "{0} の token_service に {1} のレコードがありません", "%s 的 token_service 缺少认证材料": "{0} の token_service に認証情報がありません", @@ -247,14 +246,8 @@ "Google AI Pro 或 Ultra 订阅": "Google AI Pro または Ultra サブスクリプション", "Google AI Ultra 订阅": "Google AI Ultra サブスクリプション", "Google 多账号会话序号 (0..9,对应 /u/0、/u/1...)": "Google マルチアカウントのセッション番号 (0..9、/u/0、/u/1... に対応)", - "HTTP/3 响应体已关闭": "HTTP/3 レスポンス本文はすでに閉じられています", - "HTTP/3 响应在 HEADERS 之前收到 DATA": "HTTP/3 レスポンスで HEADERS の前に DATA を受信しました", - "HTTP/3 响应状态 %d 无效": "HTTP/3 レスポンスステータス {0} は無効です", - "HTTP/3 帧长度 %d 超出上限": "HTTP/3 フレーム長 {0} が上限を超えています", - "HTTP/3 控制流首帧不是 SETTINGS": "HTTP/3 コントロールストリームの先頭フレームが SETTINGS ではありません", "INIT_TIMEOUT 必须是正数时长": "INIT_TIMEOUT は正の期間である必要があります", "INIT_TIMEOUT 无效: %w": "INIT_TIMEOUT が無効です: {0}", - "Initial 包放不下 CRYPTO 帧": "Initial パケットに CRYPTO フレームが収まりません", "Interaction 模型不接受 %s 消息": "Interaction モデルは {0} メッセージを受け付けません", "Interaction 模型的 %w": "Interaction モデルの {0}", "Interaction 模型的 contents[%d] 不接受视频截取、采样、处理方式与处理步骤": "Interaction モデルの contents[{0}] では、動画の切り出し、サンプリング、処理方法、処理手順を受け付けません", @@ -296,8 +289,6 @@ "PROXY 认证信息缺少用户名": "PROXY 認証情報にユーザー名がありません", "Playground GenerateContent 使用服务端流,收集完成后返回": "Playground GenerateContent はサーバー ストリームを使用し、収集完了後に返します", "Playwright storage state 文件": "Playwright storage state ファイル", - "QPACK 数据无效": "QPACK データが無効です", - "QPACK 阻塞流超过 %d": "QPACK ブロックされたストリームが {0} を超えました", "REQUEST_TIMEOUT 必须是正数时长": "REQUEST_TIMEOUT は正の時間である必要があります", "REQUEST_TIMEOUT 无效: %w": "REQUEST_TIMEOUT が無効です: {0}", "ROUTING_STRATEGY 必须是 round-robin 或 fill-first": "ROUTING_STRATEGY は round-robin または fill-first である必要があります", @@ -1504,5 +1495,8 @@ "页面缺少 gapi client 模块地址": "ページに gapi client モジュールのアドレスがありません", "关闭重新载入前的页面失败": "再読み込み前のページを閉じられませんでした", "设置收缩模式: %w": "縮小モードの設定: {0}", - "预先打开页面失败": "ページの事前オープンに失敗しました" + "预先打开页面失败": "ページの事前オープンに失敗しました", + "重新生成": "再生成", + "结束原因 %s": "終了理由 {0}", + "上游连续 %s 没有数据,超过 REQUEST_TIMEOUT": "上流から {0} データが届かず、REQUEST_TIMEOUT を超えました" } diff --git a/internal/i18n/messages/ko.json b/internal/i18n/messages/ko.json index 89968889..02470c17 100644 --- a/internal/i18n/messages/ko.json +++ b/internal/i18n/messages/ko.json @@ -7,7 +7,6 @@ "%s 必须是正数时长,例如 30s 或 5m": "{0}은(는) 30s 또는 5m 같은 양수 기간이어야 합니다", "%s 必须是正整数": "{0}은(는) 양의 정수여야 합니다", "%s 未协商 HTTP/2,实际协议为 %q": "{0}에서 HTTP/2가 협상되지 않음, 실제 프로토콜: {1}", - "%s 没有可用地址": "{0}에 사용 가능한 주소가 없습니다", "%s 状态为 %s": "{0} 상태: {1}", "%s 的 token_service 没有 %s 的记录": "{0}의 token_service에 {1} 레코드가 없습니다", "%s 的 token_service 缺少认证材料": "{0}의 token_service에 인증 정보가 없습니다", @@ -247,14 +246,8 @@ "Google AI Pro 或 Ultra 订阅": "Google AI Pro 또는 Ultra 구독", "Google AI Ultra 订阅": "Google AI Ultra 구독", "Google 多账号会话序号 (0..9,对应 /u/0、/u/1...)": "Google 다중 계정 세션 번호 (0..9, /u/0, /u/1... 에 해당)", - "HTTP/3 响应体已关闭": "HTTP/3 응답 본문이 닫혔습니다", - "HTTP/3 响应在 HEADERS 之前收到 DATA": "HTTP/3 응답에서 HEADERS 전에 DATA를 수신했습니다", - "HTTP/3 响应状态 %d 无效": "HTTP/3 응답 상태 {0}이(가) 유효하지 않습니다", - "HTTP/3 帧长度 %d 超出上限": "HTTP/3 프레임 길이 {0}이(가) 한도를 초과했습니다", - "HTTP/3 控制流首帧不是 SETTINGS": "HTTP/3 제어 스트림의 첫 번째 프레임이 SETTINGS가 아닙니다", "INIT_TIMEOUT 必须是正数时长": "INIT_TIMEOUT은(는) 양수 시간이어야 합니다", "INIT_TIMEOUT 无效: %w": "INIT_TIMEOUT이 유효하지 않습니다: {0}", - "Initial 包放不下 CRYPTO 帧": "Initial 패킷에 CRYPTO 프레임을 담을 수 없습니다", "Interaction 模型不接受 %s 消息": "Interaction 모델은 {0} 메시지를 허용하지 않습니다", "Interaction 模型的 %w": "Interaction 모델의 {0}", "Interaction 模型的 contents[%d] 不接受视频截取、采样、处理方式与处理步骤": "Interaction 모델의 contents[{0}]에는 동영상 클립, 샘플링, 처리 방법 및 처리 단계를 지정할 수 없습니다", @@ -296,8 +289,6 @@ "PROXY 认证信息缺少用户名": "PROXY 인증 정보에 사용자 이름이 없습니다", "Playground GenerateContent 使用服务端流,收集完成后返回": "Playground GenerateContent가 서버 스트림을 사용하며, 수집 완료 후 반환합니다", "Playwright storage state 文件": "Playwright storage state 파일", - "QPACK 数据无效": "QPACK 데이터가 잘못되었습니다", - "QPACK 阻塞流超过 %d": "QPACK 차단 스트림이 {0}을(를) 초과했습니다", "REQUEST_TIMEOUT 必须是正数时长": "REQUEST_TIMEOUT은 양수 시간이어야 합니다", "REQUEST_TIMEOUT 无效: %w": "REQUEST_TIMEOUT이 잘못되었습니다: {0}", "ROUTING_STRATEGY 必须是 round-robin 或 fill-first": "ROUTING_STRATEGY는 round-robin 또는 fill-first여야 합니다", @@ -1504,5 +1495,8 @@ "页面缺少 gapi client 模块地址": "페이지에 gapi client 모듈 주소가 없습니다", "关闭重新载入前的页面失败": "다시 불러오기 전 페이지를 닫지 못했습니다", "设置收缩模式: %w": "축소 모드 설정: {0}", - "预先打开页面失败": "페이지를 미리 열지 못했습니다" + "预先打开页面失败": "페이지를 미리 열지 못했습니다", + "重新生成": "다시 생성", + "结束原因 %s": "종료 사유 {0}", + "上游连续 %s 没有数据,超过 REQUEST_TIMEOUT": "업스트림에서 {0} 동안 데이터가 없어 REQUEST_TIMEOUT을 초과했습니다" } diff --git a/internal/i18n/messages/zh-TW.json b/internal/i18n/messages/zh-TW.json index bd7d4da6..8ad1a929 100644 --- a/internal/i18n/messages/zh-TW.json +++ b/internal/i18n/messages/zh-TW.json @@ -7,7 +7,6 @@ "%s 必须是正数时长,例如 30s 或 5m": "{0} 必須是正數時長,例如 30s 或 5m", "%s 必须是正整数": "{0} 必須是正整數", "%s 未协商 HTTP/2,实际协议为 %q": "{0} 未協商 HTTP/2,實際協定為 {1}", - "%s 没有可用地址": "{0} 沒有可用位址", "%s 状态为 %s": "{0} 狀態為 {1}", "%s 的 token_service 没有 %s 的记录": "{0} 的 token_service 沒有 {1} 的記錄", "%s 的 token_service 缺少认证材料": "{0} 的 token_service 缺少認證資料", @@ -247,14 +246,8 @@ "Google AI Pro 或 Ultra 订阅": "Google AI Pro 或 Ultra 訂閱", "Google AI Ultra 订阅": "Google AI Ultra 訂閱", "Google 多账号会话序号 (0..9,对应 /u/0、/u/1...)": "Google 多帳號工作階段序號 (0..9,對應 /u/0、/u/1...)", - "HTTP/3 响应体已关闭": "HTTP/3 回應主體已關閉", - "HTTP/3 响应在 HEADERS 之前收到 DATA": "HTTP/3 回應在 HEADERS 之前收到 DATA", - "HTTP/3 响应状态 %d 无效": "HTTP/3 回應狀態 {0} 無效", - "HTTP/3 帧长度 %d 超出上限": "HTTP/3 訊框長度 {0} 超出上限", - "HTTP/3 控制流首帧不是 SETTINGS": "HTTP/3 控制串流首個訊框不是 SETTINGS", "INIT_TIMEOUT 必须是正数时长": "INIT_TIMEOUT 必須是正數時間長度", "INIT_TIMEOUT 无效: %w": "INIT_TIMEOUT 無效: {0}", - "Initial 包放不下 CRYPTO 帧": "Initial 封包放不下 CRYPTO 訊框", "Interaction 模型不接受 %s 消息": "Interaction 模型不接受 {0} 訊息", "Interaction 模型的 %w": "Interaction 模型的 {0}", "Interaction 模型的 contents[%d] 不接受视频截取、采样、处理方式与处理步骤": "Interaction 模型的 contents[{0}] 不接受影片擷取、取樣、處理方式與處理步驟", @@ -296,8 +289,6 @@ "PROXY 认证信息缺少用户名": "PROXY 驗證資訊缺少使用者名稱", "Playground GenerateContent 使用服务端流,收集完成后返回": "Playground GenerateContent 使用伺服端串流,收集完成後傳回", "Playwright storage state 文件": "Playwright storage state 檔案", - "QPACK 数据无效": "QPACK 資料無效", - "QPACK 阻塞流超过 %d": "QPACK 阻塞串流超過 {0}", "REQUEST_TIMEOUT 必须是正数时长": "REQUEST_TIMEOUT 必須是正數時長", "REQUEST_TIMEOUT 无效: %w": "REQUEST_TIMEOUT 無效: {0}", "ROUTING_STRATEGY 必须是 round-robin 或 fill-first": "ROUTING_STRATEGY 必須是 round-robin 或 fill-first", @@ -1504,5 +1495,8 @@ "页面缺少 gapi client 模块地址": "頁面缺少 gapi client 模組位址", "关闭重新载入前的页面失败": "關閉重新載入前的頁面失敗", "设置收缩模式: %w": "設定收縮模式: {0}", - "预先打开页面失败": "預先開啟頁面失敗" + "预先打开页面失败": "預先開啟頁面失敗", + "重新生成": "重新生成", + "结束原因 %s": "結束原因 {0}", + "上游连续 %s 没有数据,超过 REQUEST_TIMEOUT": "上游連續 {0} 沒有資料,超過 REQUEST_TIMEOUT" } diff --git a/web/src/App.vue b/web/src/App.vue index 004cce4c..30c36d7d 100644 --- a/web/src/App.vue +++ b/web/src/App.vue @@ -507,6 +507,8 @@ onMounted(revealCurrentTab) :error="errors.accounts" :chrome-import="status?.chrome_import ?? false" :camoufox="status?.camoufox" + :models="models" + :cooldowns="cooldowns" @refresh="loadAccountData" @updated="updateAccount" @notice="showNotice" diff --git a/web/src/access/clients.ts b/web/src/access/clients.ts index b9209331..074d2f23 100644 --- a/web/src/access/clients.ts +++ b/web/src/access/clients.ts @@ -29,13 +29,13 @@ export const protocolNames: Record = { gemini: 'Gemini API', } -// ClientInput 是生成客户端配置所需的服务地址、API key、所选模型、Claude Code 三档模型、全部对话模型、实时目录、终端、接入协议与界面文案 +// ClientInput 是生成客户端配置所需的服务地址、API key、所选模型、Claude Code 四档模型、全部对话模型、实时目录、终端、接入协议与界面文案 export interface ClientInput { origin: string key: string model: Model | undefined modelID: string - tiers: { opus: string; sonnet: string; haiku: string } + tiers: { fable: string; opus: string; sonnet: string; haiku: string } models: Model[] catalog: Model[] shell: Shell @@ -242,12 +242,13 @@ function cherryHost(input: ClientInput): string { : input.origin } -// claudeVariables 是 Claude Code 使用的环境变量:Anthropic 地址、key、主模型、opus、sonnet、haiku 三档模型与主模型的上下文窗口 +// claudeVariables 是 Claude Code 使用的环境变量:Anthropic 地址、key、主模型、fable、opus、sonnet、haiku 四档模型与主模型的上下文窗口 function claudeVariables(input: ClientInput): [string, string][] { return [ ['ANTHROPIC_BASE_URL', input.origin], ['ANTHROPIC_AUTH_TOKEN', input.key], ['ANTHROPIC_MODEL', input.modelID], + ['ANTHROPIC_DEFAULT_FABLE_MODEL', input.tiers.fable], ['ANTHROPIC_DEFAULT_OPUS_MODEL', input.tiers.opus], ['ANTHROPIC_DEFAULT_SONNET_MODEL', input.tiers.sonnet], ['ANTHROPIC_DEFAULT_HAIKU_MODEL', input.tiers.haiku], diff --git a/web/src/api.ts b/web/src/api.ts index d22dc56f..b939918a 100644 --- a/web/src/api.ts +++ b/web/src/api.ts @@ -221,6 +221,13 @@ export const api = { requestJSON(`/api/accounts/${encodeURIComponent(id)}/refresh`, { method: 'POST', }), + refreshAccounts: async (ids: string[]) => + ( + await requestJSON('/api/accounts/refresh', { + method: 'POST', + body: jsonBody({ ids }), + }) + ).task, startService: () => requestJSON('/api/control/start', { method: 'POST' }), stopService: () => requestJSON('/api/control/stop', { method: 'POST' }), downloadCamoufox: () => requestJSON('/api/camoufox/download', { method: 'POST' }), diff --git a/web/src/components/AccessPanel.vue b/web/src/components/AccessPanel.vue index 8a6ad0ff..916187ec 100644 --- a/web/src/components/AccessPanel.vue +++ b/web/src/components/AccessPanel.vue @@ -237,12 +237,12 @@ const clientGroups = computed(() => })), ) const clientModel = ref('') -const tiers = reactive({ opus: '', sonnet: '', haiku: '' }) +const tiers = reactive({ fable: '', opus: '', sonnet: '', haiku: '' }) watch( chatModels, (list) => { clientModel.value = keepModel(clientModel.value, list) - for (const tier of ['opus', 'sonnet', 'haiku'] as const) + for (const tier of ['fable', 'opus', 'sonnet', 'haiku'] as const) tiers[tier] = list.some((model) => model.id === tiers[tier]) ? tiers[tier] : clientModel.value }, { immediate: true }, @@ -256,8 +256,9 @@ const clientProtocol = computed({ selectedProtocols[client.value.id] = value }, }) -// tierLabels 是 Claude Code 三档模型的选择框名称 +// tierLabels 是 Claude Code 四档模型的选择框名称 const tierLabels: { tier: keyof typeof tiers; label: TranslationKey }[] = [ + { tier: 'fable', label: 'access.tierFable' }, { tier: 'opus', label: 'access.tierOpus' }, { tier: 'sonnet', label: 'access.tierSonnet' }, { tier: 'haiku', label: 'access.tierHaiku' }, @@ -269,6 +270,7 @@ const clientInput = computed(() => ({ model: chatModels.value.find((model) => model.id === clientModel.value), modelID: clientModel.value || modelPlaceholder, tiers: { + fable: tiers.fable || modelPlaceholder, opus: tiers.opus || modelPlaceholder, sonnet: tiers.sonnet || modelPlaceholder, haiku: tiers.haiku || modelPlaceholder, diff --git a/web/src/components/AccountModelsDialog.vue b/web/src/components/AccountModelsDialog.vue index 77a80e31..2554a7f3 100644 --- a/web/src/components/AccountModelsDialog.vue +++ b/web/src/components/AccountModelsDialog.vue @@ -9,34 +9,217 @@ import { DialogRoot, DialogTitle, } from 'reka-ui' -import { useI18n } from '@/i18n' +import { splitReason } from '@/cooldowns' +import { useI18n, type TranslationKey } from '@/i18n' +import { + capabilityNames, + categoryLabels, + categoryOrder, + modelBadges, + modelFamily, + outputLimit, + tokenLimit, +} from '@/playground/catalog' +import type { Account, Cooldown, Model } from '@/types' +import CopyButton from './CopyButton.vue' import UiIcon from './UiIcon.vue' const props = defineProps<{ - // account 是要查看模型的账户邮箱,为空时对话框关闭 - account: string - models: string[] + // account 是要查看模型的账户,为空时对话框关闭 + account: Account | undefined + // catalog 是服务的实时模型目录,提供账户各模型的名称、上限与能力 + catalog: Model[] + cooldowns: Cooldown[] // trigger 是打开对话框的按钮,关闭后焦点回到这里 trigger: HTMLElement | null }>() const emit = defineEmits<{ close: [] }>() -const { t } = useI18n() +const { t, locale, logText } = useI18n() const query = ref('') const search = useTemplateRef('search') +// selectedID 是右侧显示详情的模型 +const selectedID = ref('') +// detailOpen 是窄屏下是否显示详情,宽屏下列表与详情并排 +const detailOpen = ref(false) + +// AccountModel 是账户的一个模型:目录中的模型信息、所属产品分类与该账户在这个模型上最晚结束的冷却 +interface AccountModel { + id: string + model: Model | undefined + family: number | undefined + cooldown: Cooldown | undefined +} + +// badgeLabels 是官网模型标记的文案 +const badgeLabels: Record<'Confidential' | 'New' | 'Paid', TranslationKey> = { + Confidential: 'pg.badgeConfidential', + New: 'pg.badgeNew', + Paid: 'pg.badgePaid', +} + +// entries 是账户的全部模型,顺序与账户目录相同 +const entries = computed(() => { + const account = props.account + if (account === undefined) return [] + const models = new Map(props.catalog.map((model) => [model.id, model])) + const latest = new Map() + for (const cooldown of props.cooldowns) { + if (cooldown.account_id !== account.id) continue + const current = latest.get(cooldown.model_id) + if (current === undefined || Date.parse(cooldown.until) > Date.parse(current.until)) + latest.set(cooldown.model_id, cooldown) + } + return account.models.map((id) => { + const model = models.get(id) + return { + id, + model, + family: model && modelFamily(model), + cooldown: latest.get(id), + } + }) +}) + +// familyName 返回产品分类的名称,不属于任何产品分类的模型归入其他 +function familyName(family: number | undefined): string { + return family === undefined ? t('accounts.modelsOther') : t(categoryLabels[family]!) +} + +// groups 是按产品分类分组的搜索结果,模型 ID、名称或分类名包含搜索词即命中,不区分大小写 +const groups = computed(() => { + const needle = query.value.trim().toLowerCase() + const families = new Map() + for (const entry of entries.value) { + const text = [entry.id, entry.model?.name ?? '', familyName(entry.family)].join('\n') + if (!text.toLowerCase().includes(needle)) continue + const items = families.get(entry.family) + if (items) items.push(entry) + else families.set(entry.family, [entry]) + } + // rank 是分类在模型选择面板中的位置,其他排在最后 + const rank = (family: number | undefined) => + family === undefined ? Infinity : categoryOrder.indexOf(family) + return [...families.entries()] + .sort(([left], [right]) => rank(left) - rank(right)) + .map(([family, items]) => ({ name: familyName(family), items })) +}) + +// cooling 是账户上正在冷却的模型数 +const cooling = computed(() => entries.value.filter((entry) => entry.cooldown).length) + +// selected 是显示详情的模型,所选模型被搜索滤掉时为结果中的第一个 +const selected = computed(() => { + const visible = groups.value.flatMap((group) => group.items) + return visible.find((entry) => entry.id === selectedID.value) ?? visible[0] +}) watch( - () => props.account, - () => (query.value = ''), + () => props.account?.id, + () => { + query.value = '' + selectedID.value = '' + detailOpen.value = false + }, ) -// matches 是模型 ID 包含搜索词的模型,不区分大小写 -const matches = computed(() => { - const needle = query.value.trim().toLowerCase() - return props.models.filter((model) => model.toLowerCase().includes(needle)) +// choose 显示一个模型的详情 +function choose(entry: AccountModel): void { + selectedID.value = entry.id + detailOpen.value = true +} + +// summary 是对话框标题下的一行:模型数、账户权益与冷却中的模型数 +const summary = computed(() => { + const account = props.account + if (account === undefined) return '' + const count = account.models.length + return [ + count === 1 ? t('models.countOne') : t('models.count').replace('{count}', String(count)), + account.benefit_tier === 'Unknown' ? t('accounts.tierUnknown') : account.benefit_tier, + ...(cooling.value + ? [t('accounts.modelsCooling').replace('{count}', String(cooling.value))] + : []), + ].join(' · ') +}) + +// facts 是详情中的上限与日期 +const facts = computed(() => { + const model = selected.value?.model + const details = model?.details + return [ + { label: t('models.context'), value: tokenLimit(model?.input_token_limit, locale.value) }, + { label: t('models.output'), value: tokenLimit(outputLimit(model), locale.value) }, + ...(details?.release_date + ? [ + { + label: t('models.releaseDate'), + value: new Intl.DateTimeFormat(locale.value, { dateStyle: 'medium' }).format( + details.release_date * 1000, + ), + }, + ] + : []), + ...(details?.knowledge_cutoff + ? [ + { + label: t('models.knowledgeCutoff'), + value: new Intl.DateTimeFormat(locale.value, { + year: 'numeric', + month: 'short', + }).format(details.knowledge_cutoff * 1000), + }, + ] + : []), + ] +}) + +// description 是官网模型卡片的简介,没有简介时为与名称不同的目录描述 +const description = computed(() => { + const model = selected.value?.model + if (model === undefined) return '' + if (model.details?.summary) return model.details.summary + return model.description !== model.name ? (model.description ?? '') : '' }) -// listHeight 是模型列表的固定高度,按全部模型的行数计算并以 24rem 与 55vh 为上限,搜索时对话框大小不变 -const listHeight = computed(() => `min(${props.models.length * 1.75}rem, 24rem, 55vh)`) +// badges 与 capabilities 是所选模型的官网标记与能力名称 +const badges = computed(() => + selected.value?.model ? modelBadges(selected.value.model, 'All') : [], +) +const capabilities = computed(() => + selected.value?.model ? capabilityNames(selected.value.model, t) : [], +) + +// reason 是所选模型当前冷却的上游错误原文 +const reason = computed(() => splitReason(selected.value?.cooldown?.reason).text) + +// cooldownCategory 返回冷却类型的界面文字 +function cooldownCategory(cooldown: Cooldown): string { + const category = splitReason(cooldown.reason).category + return category ? logText(category) : t('cooldowns.other') +} + +// untilText 返回冷却结束的本地时刻与距今的相对时间 +function untilText(cooldown: Cooldown): string { + const until = Date.parse(cooldown.until) + const seconds = Math.max(1, Math.round((until - Date.now()) / 1000)) + const [value, unit] = + seconds < 3600 + ? [Math.ceil(seconds / 60), 'minute' as const] + : seconds < 86_400 + ? [Math.round(seconds / 3600), 'hour' as const] + : [Math.round(seconds / 86_400), 'day' as const] + const relative = new Intl.RelativeTimeFormat(locale.value, { numeric: 'auto' }).format( + value, + unit, + ) + const time = new Intl.DateTimeFormat(locale.value, { + month: 'short', + day: 'numeric', + hour: '2-digit', + minute: '2-digit', + }).format(until) + return `${time} · ${relative}` +} // restoreFocus 在对话框关闭后把焦点放回打开它的按钮 function restoreFocus(event: Event): void { @@ -46,25 +229,24 @@ function restoreFocus(event: Event): void {