opencode-agentrouter-auth 0.1.10 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -1
- package/README.md +34 -18
- package/docs/OPTIMIZATION.md +14 -3
- package/index.mjs +182 -27
- package/package.json +4 -3
- package/response.mjs +12 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,7 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
[使用说明](<README.md>) · [优化设计与兼容边界](<docs/OPTIMIZATION.md>)
|
|
4
4
|
|
|
5
|
-
本页记录源码行为,当前版本为 **0.1.
|
|
5
|
+
本页记录源码行为,当前版本为 **0.1.12**。npm 可用版本以官方 registry 查询为准;文档和源码更新不代表远程网关升级或模型验证成功。
|
|
6
|
+
|
|
7
|
+
## 0.1.12 — 2026-12-03
|
|
8
|
+
|
|
9
|
+
- 修复 [Issue #21](https://github.com/ZhaoJun233/agentrouter-magpie-plugin/issues/21):`signIn()` 将 `POST /api/user/login` 返回的占位零值(`quota === 0 && used_quota === 0`)识别为无有效余额(同时保留真实耗尽账号 `quota === 0 && used_quota > 0` 与正余额零消耗账号 `quota > 0 && used_quota === 0` 的正常展示),防止将缓存余额覆写成 `$0.00 / $0.00`。
|
|
10
|
+
- `balance()` 与 `usageOf()` 在登录未提供有效余额时保留前置 `blocked` / `expired` 错误原因:已确认被阿里云 WAF 拦截时不重复撞 WAF,直接回退 `spendOf()` 并明确提示“未覆写为 $0.00”(由 magpie 保留上次有效读数);仅在非 WAF 失败且登录返回 `Set-Cookie: session=...` 时,使用新会话 Cookie(不带过期 `accessToken`)补查一次 `/api/user/self`(`userHeaders` 同步优化为无令牌时省略空的 `Authorization` 头)。
|
|
11
|
+
- 在 [usage.test.mjs](<usage.test.mjs>) 新增 5 组回归用例,并同步更新 [README.md](<README.md>) 余额说明。
|
|
12
|
+
- npm 安装示例、当前登录流程与升级指南固定到 0.1.12;包含提交 `bec3cfd` 的登录占位零值与 WAF 错误透传修复,GitHub Release 附带完整 tarball 与 `SHA256SUMS`。
|
|
13
|
+
|
|
14
|
+
## 0.1.11 — 2026-12-03
|
|
15
|
+
|
|
16
|
+
- 新增上游 Gemini Native(`@ai-sdk/google` + `/v1beta`)自动适配:`modelsOf` 根据 `/v1/models` 的 `supported_endpoint_types`(缺失时按 `gemini-*` 兜底)识别 Gemini 模型并绑定对应国际站/国内站/自定义端点的 `/v1beta` 基地址,不再发送 `/v1/responses`,同时补全 1M 上下文、视觉/多模态输入与思考强度 `variants`(并修复非 Gemini 模型及降级默认列表的 `capabilities.reasoning`/`toolcall` 声明)。使用 Gemini 系列模型时请先接入 Agent(比如 Pi)再进行使用。
|
|
17
|
+
- `loader.fetch` 在保持原有缺失 key 拦截、单次请求与零凭据泄露边界的同时,将 `/v1(?:/v1beta)?/models/{id}:(generateContent|streamGenerateContent)` 路径自动归一化为 `/v1beta/models/...`(保留 `?alt=sse`、自定义子路径、含 `/` 模型 ID 与 `Request.signal`),同时注入 `x-goog-api-key` 与受 `send-codex-headers` 控制的客户端标识。
|
|
18
|
+
- Gemini `generateContent` 正文不受 `/responses` 专属开关(`codex-session-repair`、`portable-responses`、`text-filter`)改写;`response.mjs` 对 HTTP `200`/`400`/`429` JSON 及 HTTP `200` SSE 中的单元素数组错误封套 `[{"error": ...}]`(含假 200 中的 5xx 错误体)保守兼容识别,保留正常 Gemini JSON/SSE 与原生 5xx 响应逐字节透传及业务错误否决。新增回归测试 [gemini-compat.test.mjs](<gemini-compat.test.mjs>)。
|
|
19
|
+
- npm 安装示例、当前登录流程与升级指南固定到 0.1.11;包含提交 `1aa6df7` 与 `30ed417` 的上游 Gemini Native 适配与边界加固,GitHub Release 附带完整 tarball 与 `SHA256SUMS`。
|
|
6
20
|
|
|
7
21
|
## 0.1.10 — 2026-11-17
|
|
8
22
|
|
package/README.md
CHANGED
|
@@ -8,11 +8,25 @@
|
|
|
8
8
|
- [安全政策](<SECURITY.md>):敏感信息处理与私密漏洞报告。
|
|
9
9
|
|
|
10
10
|
- [优化设计与升级指南](<docs/OPTIMIZATION.md>):问题背景、与参考项目的取舍、兼容边界、升级/回滚与本地验证。
|
|
11
|
-
- [更新日志](<CHANGELOG.md>):0.1.
|
|
11
|
+
- [更新日志](<CHANGELOG.md>):0.1.12 登录占位 0/0 余额防覆写修复、0.1.11 上游 Gemini Native 自动适配、会话资源清理与历史版本的变化。
|
|
12
12
|
- [本地签到说明](<LOCAL-CHECKIN.md>):独立签到工具的使用与安全边界。
|
|
13
13
|
|
|
14
14
|
> 仓库源码更新不等于 npm 发布或你的网关已升级。请按升级指南自行验证;不要把真实 Cookie、访问令牌、私有词表或抓包提交到仓库。
|
|
15
15
|
|
|
16
|
+
### 0.1.12:修复登录占位 0/0 余额误覆写(Issue #21)
|
|
17
|
+
|
|
18
|
+
- **识别登录占位零值**:当 `/api/user/self` 失败并回退到 `POST /api/user/login` 时,`signIn()` 不再把登录响应中的占位 `quota === 0 && used_quota === 0` 当作真实 `$0.00` 余额覆写缓存;真实耗尽账号(`quota === 0 && used_quota > 0`)与正余额零消耗账号(`quota > 0 && used_quota === 0`)保持兼容。
|
|
19
|
+
- **保留 WAF/过期原因与非 WAF 会话补查**:若前序 `/api/user/self` 已确认被阿里云 WAF 拦截,则不再重复撞 WAF,直接保留 `blocked` 原因回退 `spendOf()` 并提示未覆写为 `$0.00`(由宿主保留上次有效余额);仅在前序未触发 WAF 且登录下发了 `session` Cookie 时,用新会话 Cookie(剥离过期 `accessToken`)补查一次 `/api/user/self`。
|
|
20
|
+
|
|
21
|
+
### 0.1.11:上游 Gemini Native 自动适配
|
|
22
|
+
|
|
23
|
+
> **使用提示**:使用 Gemini 系列模型时,请先接入 Agent(比如 Pi)再进行使用。
|
|
24
|
+
|
|
25
|
+
- **自动协议分流**:`modelsOf` 根据上游 `/v1/models` 返回的 `supported_endpoint_types`(含 `"gemini"` 时启用;缺省时按 `gemini-*` 模型 ID 兜底)将 Gemini 模型声明为 `@ai-sdk/google` 并指向对应的 `/v1beta` 基地址,不再对 Gemini 发送 `/v1/responses`;非 Gemini 模型继续使用 `@ai-sdk/openai` 与 Responses API,原有 GPT 路由不受影响。
|
|
26
|
+
- **能力与思考强度声明**:为 Gemini 模型声明 1M 上下文(`1048576`,上游明确返回正数 `context_length` / `max_output_tokens` 时优先采用)、多模态输入(文本/图像/音频/视频/PDF)与思考强度 `variants`(如 `gemini-4-neon` 含 `low`/`medium`/`high`/`xhigh`,`gemini-4-carbon` 含 `low`/`medium`/`high`),同时为非 Gemini 模型(含降级默认列表)补齐 `capabilities.reasoning` 与 `capabilities.toolcall`。
|
|
27
|
+
- **路径归一化与认证请求头**:`loader.fetch` 自动将宿主可能发往 `/v1/models/{id}:generateContent` 或 `:streamGenerateContent`(含 `?alt=sse`、自定义子路径与含 `/` 的模型 ID)的请求归一化为 `/v1beta/models/...`,按 `send-codex-headers` 开关保留受认可的客户端 `User-Agent`,并同时附带 `x-goog-api-key` 与 `Authorization: Bearer`。
|
|
28
|
+
- **边界与兼容性**:三个 `/responses` 正文开关(`codex-session-repair`、`portable-responses`、`text-filter`)只作用于 `POST .../responses`,不会改动 Gemini `generateContent` 请求体;`send-codex-headers` 与可配置 `retryAfterSeconds` 同样生效,`response.mjs` 对 HTTP `200`/`400`/`429` JSON 及 HTTP `200` SSE 中的单元素数组错误封套 `[{"error": ...}]`(含假 200 内的 5xx 错误体)保守识别并保留业务错误否决,原生 HTTP 5xx JSON 数组直接透传。专项验证运行 `npm run test:gemini`。
|
|
29
|
+
|
|
16
30
|
### 0.1.10:会话资源清理与中文功能选项
|
|
17
31
|
|
|
18
32
|
在 **AgentRouter 提供商编辑页** 的功能开关中配置以下选项,保存后下一次请求读取新值,无需重新登录。它们属于提供商的 `auth.options`,不是「插件 → 选项」里的 `retryAfterSeconds` JSON 配置。
|
|
@@ -51,7 +65,7 @@
|
|
|
51
65
|
- **移除 LinuxDo OAuth 登录入口。** 真实验证结论:`connect.linux.do` 授权后回落到站点注册路由 `https://agentrouter.org/oauth/linuxdo`(并非请求里的 `/oauth/linuxdo/magpie` 子路径),站点前端在该路由挂载时立即请求 `/api/oauth/linuxdo`(真实验证记录到 1 次、HTTP 403)并跳转控制台,地址栏不再保留 `code`/`state`,插件侧手动回填在任何兑换请求之前即被拒绝。公开资料没有关于 LinuxDo redirect_uri 匹配语义的说明,站点前端确认该路由为精确匹配且没有任何可抑制自动兑换的标志位。
|
|
52
66
|
- 移除范围包含 LinuxDo 方法、其常量、`/api/status` 的 `linuxdo_client_id` 读取与死代码 `codeOf`;GitHub 登录链路、提问形状与密钥筛选规则不变。
|
|
53
67
|
|
|
54
|
-
> **本页登录流程适用于 0.1.
|
|
68
|
+
> **本页登录流程适用于 0.1.12。** 下方命令固定该版本,安装前先查询 npm 官方 registry;源码版本号不等于已发布或你的网关已升级。0.1.7 曾发布 LinuxDo 登录入口,0.1.8 已将其移除,这不改变 0.1.7 已安装账号保存的数据。
|
|
55
69
|
|
|
56
70
|
### 0.1.7:站点选择与 LinuxDo 登录
|
|
57
71
|
|
|
@@ -75,7 +89,7 @@
|
|
|
75
89
|
|
|
76
90
|
### 0.1.4 变更
|
|
77
91
|
|
|
78
|
-
- 历史版本改进了系统令牌遮蔽输入和可选签到配置;当前操作请以下文 0.1.
|
|
92
|
+
- 历史版本改进了系统令牌遮蔽输入和可选签到配置;当前操作请以下文 0.1.12 登录流程为准。
|
|
79
93
|
- 修复完整历史消息含空 `logprobs: []` 时漏掉内联重放的情况,保留工具关联、非空概率数据、引用注释和不透明状态。
|
|
80
94
|
- 无法恢复仅存在于其他上游资源的历史或加密状态;这是范围受限的兼容补充,并非所有跨资源错误的通用解法。
|
|
81
95
|
|
|
@@ -91,19 +105,19 @@
|
|
|
91
105
|
|
|
92
106
|
```bash
|
|
93
107
|
# 在有 Node.js / npm 的终端查询目标版本;确认可用后再安装
|
|
94
|
-
npm view opencode-agentrouter-auth@0.1.
|
|
108
|
+
npm view opencode-agentrouter-auth@0.1.12 version --registry=https://registry.npmjs.org
|
|
95
109
|
|
|
96
110
|
# Docker:查询成功后安装,容器名按你的实际部署修改
|
|
97
|
-
docker exec magpie magpie plugin add npm:opencode-agentrouter-auth@0.1.
|
|
111
|
+
docker exec magpie magpie plugin add npm:opencode-agentrouter-auth@0.1.12
|
|
98
112
|
|
|
99
113
|
# 或者,本地 / 原生进程部署
|
|
100
|
-
magpie plugin add npm:opencode-agentrouter-auth@0.1.
|
|
114
|
+
magpie plugin add npm:opencode-agentrouter-auth@0.1.12
|
|
101
115
|
```
|
|
102
116
|
|
|
103
117
|
- Docker 与原生进程命令二选一。安装后用 `magpie plugin list --json`(Docker 中在命令前加 `docker exec magpie`)核对插件,再在面板检查 AgentRouter 的插件选项和登录状态。
|
|
104
118
|
- 如果版本查询返回 `E404`,该版本在所查 registry 尚不可用;不要把源码版本号当成 npm 已发布证明,可改用下方源码安装。若仅宿主安装失败,可运行 `magpie plugin registry` 检查它实际使用的 registry;查询命令不会替你修改宿主设置。
|
|
105
119
|
- 已用本地目录安装的用户,继续更新原目录即可;不要直接再添加一份 npm 插件。先备份并确认宿主如何关联插件路径、认证及选项,再决定是否迁移,避免重复注册或丢失原配置。
|
|
106
|
-
- 固定 `@0.1.
|
|
120
|
+
- 固定 `@0.1.12` 便于复现;省略版本会让宿主选择版本,但并不等同于更新所有插件。`magpie plugin update` 则可能更新其他插件,不作为本插件的定点升级步骤。
|
|
107
121
|
- 已通过 npm 安装的用户,升级或降级前须确认当前宿主的**单包替换方式以及认证/选项保留行为**;未确认前暂停操作,不直接重复执行 `plugin add`。旧版本也须经 npm 查询确认存在,不能假设仓库旧版本都已发布。下方[升级/回滚指南](<docs/OPTIMIZATION.md#11-升级与回退>)针对本地包/目录备份,不是已验证的 npm 定点降级流程。
|
|
108
122
|
|
|
109
123
|
#### Docker 源码安装
|
|
@@ -131,9 +145,9 @@ magpie plugin add /path/to/agentrouter-magpie-plugin
|
|
|
131
145
|
|
|
132
146
|
---
|
|
133
147
|
|
|
134
|
-
### 2. 登录并绑定账号(0.1.
|
|
148
|
+
### 2. 登录并绑定账号(0.1.12)
|
|
135
149
|
|
|
136
|
-
以下流程适用于已加载 0.1.
|
|
150
|
+
以下流程适用于已加载 0.1.12 的宿主,沿用 0.1.8 的登录方式;0.1.7 及更早版本的菜单不同(0.1.7 的 LinuxDo 入口已在 0.1.8 移除)。新账号运行交互式登录命令(升级既有账号不要求重新登录):
|
|
137
151
|
```bash
|
|
138
152
|
docker exec -it magpie magpie plugin login agentrouter
|
|
139
153
|
# 非 Docker 环境直接运行:magpie plugin login agentrouter
|
|
@@ -283,11 +297,11 @@ AgentRouter 的每日额度由**重新登录**触发,余额查询成功不等
|
|
|
283
297
|
读取顺序:
|
|
284
298
|
|
|
285
299
|
1. 有 Cookie 或访问令牌时,带用户 ID 请求 `GET /api/user/self`,读取 `quota`(剩余)和 `used_quota`(累计消耗)。
|
|
286
|
-
2. 失败后,有站点用户名密码则 `POST /api/user/login
|
|
300
|
+
2. 失败后,有站点用户名密码则 `POST /api/user/login`,尝试从登录响应读取同样的字段;若登录响应返回占位零值(`quota = 0` 且 `used_quota = 0`),插件不会将其写成 `$0.00`。当 `/api/user/self` 此前未被 WAF 拦截且登录下发了会话 `session` Cookie 时,会再用该会话补查一次 `/api/user/self`。
|
|
287
301
|
3. 仍无余额时,尝试 `GET /api/log/self/stat`,读取账号累计消耗。
|
|
288
302
|
4. 再失败则用 API 密钥请求 `/v1/dashboard/billing/usage`,将 `total_usage` 的美分换成美元。
|
|
289
303
|
|
|
290
|
-
|
|
304
|
+
只有前两条返回有效额度字段(或非占位零值)时才显示余额和使用比例。缺字段或登录占位 `0 / 0` 不会当成零覆盖缓存。只读到消耗时,插件返回“累计消耗 $…;剩余额度未知”,不会把消耗计入余额,也不会生成虚假的 0% 使用条。
|
|
291
305
|
|
|
292
306
|
magpie 可能在查询失败时保留上次成功的额度,并标记旧读取时间(`asOf`)。这种余额是历史数据,不能当成刚查到的实时余额;没有历史读数的账号会直接显示上述状态提示。
|
|
293
307
|
|
|
@@ -306,7 +320,7 @@ AgentRouter 的网关只回应它认识的客户端 —— 白名单里是它**
|
|
|
306
320
|
| `curl/8.0` | `401 unauthorized client detected` |
|
|
307
321
|
| 空(删掉) | `401` |
|
|
308
322
|
|
|
309
|
-
**这条只管模型网关**(`/v1/models`、`/v1/responses
|
|
323
|
+
**这条只管模型网关**(`/v1/models`、`/v1/responses` 以及 Gemini `/v1beta/models/*:generateContent` 等)。站点自己的路由(`/api/...`)不看这个头,浏览器 UA 也一样能用;反过来,`codex_cli_rs` 也不会帮忙过滑动验证码。
|
|
310
324
|
|
|
311
325
|
所以请求头在这里不是装饰,是**决定成败**的东西。插件默认发 Codex CLI 的头,可通过开关和以下配置层控制:
|
|
312
326
|
|
|
@@ -330,15 +344,15 @@ AgentRouter 的网关只回应它认识的客户端 —— 白名单里是它**
|
|
|
330
344
|
|
|
331
345
|
`sk-` key 不能代替账号访问令牌。`/api/user/token` 会重新生成访问令牌,使旧令牌失效;插件不调用此接口,GitHub 流程用你填写的系统访问令牌查找已有模型 key 并随成功认证保存,不会把它当作模型 key。
|
|
332
346
|
|
|
333
|
-
##
|
|
347
|
+
## 协议:Responses API 与 Gemini Native
|
|
334
348
|
|
|
335
|
-
AgentRouter
|
|
349
|
+
AgentRouter 的非 Gemini 模型**不支持** `/v1/chat/completions`:
|
|
336
350
|
|
|
337
351
|
```
|
|
338
352
|
400 模型 gpt-5.6-sol 不支持 /v1/chat/completions 接口,请改用 /v1/responses
|
|
339
353
|
```
|
|
340
354
|
|
|
341
|
-
|
|
355
|
+
因此 provider 默认与非 Gemini 模型声明为 `npm: "@ai-sdk/openai"`(Responses API);对于上游返回 `"gemini"` 端点的模型(缺省时按 `gemini-*` 兜底),插件自动声明为 `@ai-sdk/google` 并路由到对应站点的 `/v1beta` 原生接口,由 magpie 网关负责协议翻译与分发。使用 Gemini 系列模型时,请先接入 Agent(比如 Pi)再进行使用。
|
|
342
356
|
|
|
343
357
|
### 跨 OpenAI 资源的历史条目错误(0.1.2)
|
|
344
358
|
|
|
@@ -643,10 +657,11 @@ docker exec magpie magpie plugin options word-guard '{
|
|
|
643
657
|
在仓库根目录运行:
|
|
644
658
|
|
|
645
659
|
```bash
|
|
646
|
-
npm test
|
|
660
|
+
npm test # Node.js 插件与本地签到回归
|
|
661
|
+
npm run test:gemini # Gemini Native 适配、路径归一化与错误封套专项回归
|
|
647
662
|
npm run test:session-repair # 会话资源清理、开关与中文说明专项回归
|
|
648
|
-
npm run test:policy
|
|
649
|
-
npm run test:headers
|
|
663
|
+
npm run test:policy # PR 元数据规则回归
|
|
664
|
+
npm run test:headers # 需要 Python 3;仅执行维护脚本的 Python 部分,不调用 Docker
|
|
650
665
|
```
|
|
651
666
|
|
|
652
667
|
## 文件
|
|
@@ -657,6 +672,7 @@ npm run test:headers # 需要 Python 3;仅执行维护脚本的 Python 部
|
|
|
657
672
|
- `package.json` — 包描述
|
|
658
673
|
- `set-ar-headers.sh` — 改请求头的小工具
|
|
659
674
|
- `usage.test.mjs` — 额度读取、登录与 HTTP 错误回归测试
|
|
675
|
+
- `gemini-compat.test.mjs` — Gemini Native 适配、路径归一化与错误封套回归测试
|
|
660
676
|
- `loader-stream.test.mjs` — 流内错误、空完成、成功字节、工具调用等回归测试
|
|
661
677
|
- `response-bounds.test.mjs` — 有界缓存、检查超时与取消测试(统一运行 `npm test`)
|
|
662
678
|
|
package/docs/OPTIMIZATION.md
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
# 0.1.
|
|
1
|
+
# 0.1.12 优化与兼容边界
|
|
2
2
|
|
|
3
3
|
[使用说明](<../README.md>) · [版本记录](<../CHANGELOG.md>) · [MIT 许可](<../LICENSE>)
|
|
4
4
|
|
|
5
|
-
本文说明 **0.1.
|
|
5
|
+
本文说明 **0.1.12** 的登录占位 `0 / 0` 余额防覆写与 WAF 错误透传修复,以及延续的 0.1.11 上游 Gemini Native(`@ai-sdk/google` + `/v1beta`)自动适配、可选会话资源清理、中文功能选项、可配置限流等待、站点选择、系统访问令牌接入与兼容能力。安装命令固定 0.1.12,先查询 npm 官方 registry 确认可用;源码、npm 包和宿主实际加载版本应分别核对。下文保留既有优化与安全边界,示例不含真实请求、账号或凭据。
|
|
6
|
+
|
|
7
|
+
0.1.12 在 `signIn()`、`balance()` 与 `usageOf()` 中排除 `/api/user/login` 占位 `quota === 0 && used_quota === 0` 导致的 `$0.00 / $0.00` 误覆写,在已确认 WAF 拦截时保留 `blocked` 原因并直接回退 `spendOf()`,并在非 WAF 场景且登录返回 `Set-Cookie: session=...` 时补查一次 `/api/user/self`(不发送空/过期 `Authorization` 头)。
|
|
8
|
+
|
|
9
|
+
0.1.11 针对上游 Gemini 模型自动路由至 `@ai-sdk/google` `/v1beta`,补充 1M 上下文、多模态输入、思考强度 `variants`、路径归一化、`x-goog-api-key` 注入及单元素数组错误封套识别;使用 Gemini 系列模型时请先接入 Agent(比如 Pi)再进行使用,详见 [README](<../README.md#0111上游-gemini-native-自动适配>)。
|
|
6
10
|
|
|
7
11
|
0.1.10 增加 `codex-session-repair` 有损清理开关和中文功能说明。开关默认关闭;其发送前删减不等同于恢复本地会话,具体边界见 [README](<../README.md#0110会话资源清理与中文功能选项>)。
|
|
8
12
|
|
|
@@ -249,11 +253,13 @@ Markdown 采取保守策略,而不是试图实现完整语法解析器:
|
|
|
249
253
|
|
|
250
254
|
## 11. 升级与回退
|
|
251
255
|
|
|
252
|
-
1. 阅读 [使用说明](<../README.md>) 和 [版本记录](<../CHANGELOG.md>),核对来源:npm 安装固定 0.1.
|
|
256
|
+
1. 阅读 [使用说明](<../README.md>) 和 [版本记录](<../CHANGELOG.md>),核对来源:npm 安装固定 0.1.12 并先查询官方 registry;本地包/目录安装应核对提交、包版本和宿主实际运行版本。不能把源码版本号当成 npm 发布或远程网关升级证明。
|
|
253
257
|
2. 在受保护的位置备份当前插件版本、私有词表及宿主管理的配置;不要将凭据备份加入公开仓库。
|
|
254
258
|
3. 使用已核验的完整本地包或完整插件目录升级,不能只覆盖入口文件。[body-inspection.mjs](<../body-inspection.mjs>) 与 0.1.10 新增的 [session-repair.mjs](<../session-repair.mjs>) 是运行时必需模块,漏带会导致模块加载失败。
|
|
255
259
|
4. 无需迁移认证字段或环境变量;升级前后核对旧 key、site 和其他设置不变,不必重新登录。新增有损开关默认关闭,旧开关值不改写。重新加载插件,让运行中的 loader 使用新源码与规则快照。先确认独立开关组合符合自己的使用意图。
|
|
256
260
|
5. 如只需停用正文变换,可关闭三个正文选项;这不会停用响应诊断。需要完整回退时,恢复此前验证过的完整版本并重新加载,不混用新旧模块。
|
|
261
|
+
回退到 0.1.11 会失去 [Issue #21](https://github.com/ZhaoJun233/agentrouter-magpie-plugin/issues/21) 的登录占位 `0 / 0` 余额拦截与 WAF 错误透传修复。
|
|
262
|
+
回退到 0.1.10 会失去上游 Gemini Native(`@ai-sdk/google` + `/v1beta`)自动分流、路径归一化、`x-goog-api-key` 注入以及单元素数组错误封套识别。
|
|
257
263
|
回退到 0.1.9 会失去会话资源清理开关和本次中文功能说明,恢复旧开关行为;不会恢复已经在先前发送副本中丢弃的上下文,也不改写客户端文件。
|
|
258
264
|
6. 回退至 npm 0.1.7 会得到 0.1.7 的 LinuxDo 登录入口(该入口已通过真实验证判定不可用),并失去系统访问令牌方式;回退至 0.1.6 会再失去站点选择、OAuth session 透传及本次 assistant 历史与 Windows 权限修复。已存的显式 apiBase 仍是旧版本识别的字段,回退不会自动撤销新账号的站点选择;应保留并核对原有配置备份。更早版本还会失去各自后续引入的密钥保护、诊断、SSE 或过滤修复;回退不能恢复已缺失的上游历史。
|
|
259
265
|
|
|
@@ -272,6 +278,10 @@ npm pack --dry-run
|
|
|
272
278
|
|
|
273
279
|
Python 命令仅执行维护脚本的测试部分,不调用 Docker。这些回归不需要真实账号或凭据;不要用真实登录或生产请求替代合成回归。
|
|
274
280
|
|
|
281
|
+
0.1.12 发布准备在 Windows、Node.js v24.22.1、Python 3.13.12 上完成 **Node.js 369 项、policy 13 项、Python 2 项**本地回归;打包预检为 16 个文件,不包含测试文件、私有词表、凭据或会话记录,包含提交 `bec3cfd` 的登录占位 `0 / 0` 余额拦截、WAF 错误透传与非 WAF 会话补查修复。合成检查不代表 npm 已完成发布、浏览器开关操作或宿主升级已验证。
|
|
282
|
+
|
|
283
|
+
0.1.11 发布准备在 Windows、Node.js v24.22.1、Python 3.13.12 上完成 **Node.js 364 项、policy 13 项、Python 2 项**本地回归;打包预检为 16 个文件,不包含测试文件、私有词表、凭据或会话记录。提交 `1aa6df7` 与 `30ed417` 的 GitHub Actions CI(Linux/Windows × Node 20/22/24)与 CodeQL 均通过。合成检查不代表 npm 已完成发布、浏览器开关操作或宿主升级已验证。
|
|
284
|
+
|
|
275
285
|
0.1.10 发布准备在 Windows、Node.js 24.22.1、Python 3.13.12 上重新完成 **Node.js 358 项、policy 13 项、Python 2 项**本地回归;打包预检为 16 个文件,包含 `session-repair.mjs`,不包含私有词表、凭据或会话记录。功能 [PR #16](https://github.com/ZhaoJun233/agentrouter-magpie-plugin/pull/16) 的 Linux/Windows × Node 20/22/24、PR policy 和 CodeQL 均通过。合成检查不代表真实模型对话、浏览器开关操作或 npm 升级/回退已验证。
|
|
276
286
|
|
|
277
287
|
0.1.7 所用运行时代码在合并前完成 **Node.js 297 项、policy 13 项、Python 2 项**本地回归,另有 38 项独立合成检查,结果见 [PR #9 复审记录](https://github.com/ZhaoJun233/agentrouter-magpie-plugin/pull/9#issuecomment-6761271376)。独立检查不计入仓库 `npm test` 数量,且不证明真实 OAuth 或模型调用成功。0.1.6 历史原生记录为 Node.js 278 项、Python 2 项;后续运行时代码改变须重新验证,不能沿用旧计数。
|
|
@@ -289,6 +299,7 @@ PR 作者报告的真实账号复测为 2 次 `/api/token` 请求、0 次 `/api/
|
|
|
289
299
|
- [流式回归](<../loader-stream.test.mjs>) 与 [响应边界回归](<../response-bounds.test.mjs>):成功原始字节、早期/后期失败、超限、超时、在途读取、取消与背压。
|
|
290
300
|
- [文本过滤回归](<../content-filter.test.mjs>) 与 [请求内联回归](<../request-replay.test.mjs>):四种开关组合、空/非空概率数据、无变化字节、幂等性、工具及不透明状态保留。
|
|
291
301
|
- [会话清理回归](<../session-repair.test.mjs>):默认关闭、仅布尔 `true` 开启、请求间即时切换、客户端输入保留、工具关联、空输入/不透明工具零请求、独立开关组合及既有读取边界。
|
|
302
|
+
- [Gemini 兼容回归](<../gemini-compat.test.mjs>):按 `supported_endpoint_types`(及 `gemini-*` 兜底)分流至 `@ai-sdk/google` `/v1beta`、1M 上下文与 `variants` 声明、`/v1/models/...` → `/v1beta/models/...` 路径归一化、`x-goog-api-key` 与 Codex UA 协同、Gemini 正文免于 `/responses` 改写及单元素数组错误封套处理。
|
|
292
303
|
|
|
293
304
|
## 13. 已知非保证
|
|
294
305
|
|
package/index.mjs
CHANGED
|
@@ -71,6 +71,9 @@ const MODEL_KEY_PAGE_SIZE = 50;
|
|
|
71
71
|
|
|
72
72
|
// Models are listed by the vendor, so this is only what to show before a
|
|
73
73
|
// sign-in and the shape (limits, tool calls) each listed model takes.
|
|
74
|
+
const OPENAI_NPM = "@ai-sdk/openai";
|
|
75
|
+
const GEMINI_NPM = "@ai-sdk/google";
|
|
76
|
+
|
|
74
77
|
const MODEL_SHAPE = {
|
|
75
78
|
name: undefined,
|
|
76
79
|
limit: { context: 300000, output: 128000 },
|
|
@@ -78,6 +81,16 @@ const MODEL_SHAPE = {
|
|
|
78
81
|
reasoning: true,
|
|
79
82
|
};
|
|
80
83
|
|
|
84
|
+
const GEMINI_INPUT_MODALITIES = { text: true, image: true, audio: true, video: true, pdf: true };
|
|
85
|
+
const TEXT_OUTPUT_MODALITIES = { text: true, image: false, audio: false, video: false, pdf: false };
|
|
86
|
+
|
|
87
|
+
const GEMINI_MODEL_DEFAULTS = {
|
|
88
|
+
"gemini-4-neon": { name: "Gemini 4 Neon", context: 1048576, output: 65536, variants: ["low", "medium", "high", "xhigh"] },
|
|
89
|
+
"gemini-4-carbon": { name: "Gemini 4 Carbon", context: 1048576, output: 32768, variants: ["low", "medium", "high"] },
|
|
90
|
+
};
|
|
91
|
+
const GEMINI_FALLBACK_DEFAULTS = { context: 1048576, output: 65536, variants: ["low", "medium", "high"] };
|
|
92
|
+
const GEMINI_MODEL_ACTION = /^\/models\/[^/:]+(?:\/[^/:]+)*:(?:generateContent|streamGenerateContent)$/;
|
|
93
|
+
|
|
81
94
|
// The models this plugin itself ships, by the name to show them under. They
|
|
82
95
|
// are what the row lists before the vendor's own list arrives — and what it
|
|
83
96
|
// falls back to when that list cannot be read.
|
|
@@ -215,6 +228,70 @@ function apiBase(provider, auth) {
|
|
|
215
228
|
return `${ENDPOINT}/v1`;
|
|
216
229
|
}
|
|
217
230
|
|
|
231
|
+
function positiveNumber(value) {
|
|
232
|
+
if (typeof value === "number") return Number.isFinite(value) && value > 0 ? value : undefined;
|
|
233
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
234
|
+
const n = Number(value);
|
|
235
|
+
return Number.isFinite(n) && n > 0 ? n : undefined;
|
|
236
|
+
}
|
|
237
|
+
return undefined;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function isGeminiModel(model) {
|
|
241
|
+
const types = model?.supported_endpoint_types;
|
|
242
|
+
if (Array.isArray(types) && types.length > 0) {
|
|
243
|
+
return types.some(type => String(type ?? "").trim().toLowerCase() === "gemini");
|
|
244
|
+
}
|
|
245
|
+
return /^gemini(?:[-._/]|$)/i.test(String(model?.id ?? "").trim());
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function toGeminiBase(v1Base) {
|
|
249
|
+
const clean = String(v1Base ?? "").trim().replace(/\/+$/, "");
|
|
250
|
+
if (!clean) return `${ENDPOINT}/v1beta`;
|
|
251
|
+
const stripped = clean.endsWith("/v1") ? clean.slice(0, -3) : clean;
|
|
252
|
+
if (stripped.endsWith("/v1beta")) return stripped;
|
|
253
|
+
return `${stripped}/v1beta`;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function inspectGeminiUrl(rawUrl) {
|
|
257
|
+
const original = String(rawUrl ?? "");
|
|
258
|
+
let u;
|
|
259
|
+
try { u = new URL(original); } catch { return { url: original, isGemini: false }; }
|
|
260
|
+
const idx = u.pathname.lastIndexOf("/models/");
|
|
261
|
+
if (idx < 0) return { url: original, isGemini: false };
|
|
262
|
+
const prefix = u.pathname.slice(0, idx);
|
|
263
|
+
const suffix = u.pathname.slice(idx);
|
|
264
|
+
if (!GEMINI_MODEL_ACTION.test(suffix)) return { url: original, isGemini: false };
|
|
265
|
+
if (prefix.endsWith("/v1/v1beta")) {
|
|
266
|
+
u.pathname = `${prefix.slice(0, -10)}/v1beta${suffix}`;
|
|
267
|
+
return { url: u.href, isGemini: true };
|
|
268
|
+
}
|
|
269
|
+
if (prefix.endsWith("/v1")) {
|
|
270
|
+
u.pathname = `${prefix.slice(0, -3)}/v1beta${suffix}`;
|
|
271
|
+
return { url: u.href, isGemini: true };
|
|
272
|
+
}
|
|
273
|
+
return { url: original, isGemini: true };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function resolveGeminiTarget(input, init) {
|
|
277
|
+
const rawUrl = input instanceof Request ? input.url : input instanceof URL ? input.href : String(input ?? "");
|
|
278
|
+
const { url: rewritten, isGemini } = inspectGeminiUrl(rawUrl);
|
|
279
|
+
if (rewritten === rawUrl) return { target: input, isGemini };
|
|
280
|
+
if (!(input instanceof Request)) return { target: rewritten, isGemini };
|
|
281
|
+
const hasInitBody = init != null && init.body !== undefined;
|
|
282
|
+
const method = (init?.method ?? input.method ?? "GET").toUpperCase();
|
|
283
|
+
const useInputBody = !hasInitBody && input.body != null && method !== "GET" && method !== "HEAD";
|
|
284
|
+
return {
|
|
285
|
+
target: new Request(rewritten, {
|
|
286
|
+
method: input.method,
|
|
287
|
+
headers: input.headers,
|
|
288
|
+
...(useInputBody ? { body: input.body, duplex: "half" } : {}),
|
|
289
|
+
signal: input.signal,
|
|
290
|
+
}),
|
|
291
|
+
isGemini,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
218
295
|
// siteBase is where the site's own web routes are: the balance and the
|
|
219
296
|
// check-in. It follows the site the account signed in to, not the request
|
|
220
297
|
// endpoint, since a front door's web routes are its own.
|
|
@@ -310,7 +387,9 @@ function siteName(auth) {
|
|
|
310
387
|
// WAF when the account has one.
|
|
311
388
|
function userHeaders(provider, auth) {
|
|
312
389
|
const h = headersOf(provider, auth);
|
|
313
|
-
|
|
390
|
+
const token = userToken(auth);
|
|
391
|
+
if (token) h.Authorization = token;
|
|
392
|
+
else delete h.Authorization;
|
|
314
393
|
delete h["Content-Type"];
|
|
315
394
|
if (userId(auth)) h["New-Api-User"] = userId(auth);
|
|
316
395
|
const cookie = parseCookies(cookieText(auth));
|
|
@@ -586,20 +665,21 @@ async function storedKeyOf(base, token, id, keyId) {
|
|
|
586
665
|
}
|
|
587
666
|
}
|
|
588
667
|
|
|
589
|
-
// signIn is one POST to the site's login route, and it
|
|
590
|
-
//
|
|
668
|
+
// signIn is one POST to the site's login route, and it handles both the day's
|
|
669
|
+
// check-in and (when populated) the balance fallback.
|
|
591
670
|
//
|
|
592
671
|
// The vendor grants its daily credit at sign-in rather than through a route
|
|
593
672
|
// of its own — its front end says "签到成功,新增额度已到账" when the login
|
|
594
|
-
// response has checked_in
|
|
595
|
-
// used_quota,
|
|
596
|
-
//
|
|
597
|
-
//
|
|
598
|
-
//
|
|
673
|
+
// response has checked_in. The login response may also carry quota and
|
|
674
|
+
// used_quota, though on deployments that omit quota fields in the login struct
|
|
675
|
+
// both return placeholder zero values (`quota: 0, used_quota: 0`), which must
|
|
676
|
+
// not be trusted as a `$0.00` balance. When populated, signIn also surfaces the
|
|
677
|
+
// response's `session` cookie and `userId` in memory for an optional non-WAF
|
|
678
|
+
// `/api/user/self` lookup.
|
|
599
679
|
//
|
|
600
|
-
// It answers {ok, checkedIn?, balance?, why?}. The
|
|
601
|
-
// false `ok` is the account's to fix (wrong password)
|
|
602
|
-
// is down), so the reasons stay distinguishable here.
|
|
680
|
+
// It answers {ok, checkedIn?, balance?, name?, session?, userId?, why?}. The
|
|
681
|
+
// caller decides whether a false `ok` is the account's to fix (wrong password)
|
|
682
|
+
// or nobody's (the site is down), so the reasons stay distinguishable here.
|
|
603
683
|
async function signIn(provider, auth) {
|
|
604
684
|
const user = String(meta(auth, "username") ?? "").trim();
|
|
605
685
|
const pass = String(meta(auth, "password") ?? "");
|
|
@@ -634,14 +714,25 @@ async function signIn(provider, auth) {
|
|
|
634
714
|
const d = json.data ?? {};
|
|
635
715
|
const quota = quotaNumber(d.quota);
|
|
636
716
|
const used = quotaNumber(d.used_quota);
|
|
717
|
+
const hasUsableQuota =
|
|
718
|
+
Number.isFinite(quota) && Number.isFinite(used) && !(quota === 0 && used === 0);
|
|
637
719
|
const balance =
|
|
638
|
-
|
|
720
|
+
hasUsableQuota
|
|
639
721
|
? { quota, used, name: d.username ?? d.display_name ?? "" }
|
|
640
722
|
: undefined;
|
|
641
723
|
if (balance) balance.total = balance.quota + balance.used;
|
|
724
|
+
const session = sessionOf(res);
|
|
725
|
+
const loginUserId = d.id != null ? String(d.id).trim() : "";
|
|
642
726
|
// `checked_in: true` means *this* sign-in granted the day's credit. A false
|
|
643
727
|
// means no new grant; a missing/non-boolean marker proves nothing.
|
|
644
|
-
return {
|
|
728
|
+
return {
|
|
729
|
+
ok: true,
|
|
730
|
+
checkedIn: d.checked_in,
|
|
731
|
+
balance,
|
|
732
|
+
name: d.username ?? d.display_name ?? "",
|
|
733
|
+
...(session ? { session } : {}),
|
|
734
|
+
...(loginUserId ? { userId: loginUserId } : {}),
|
|
735
|
+
};
|
|
645
736
|
}
|
|
646
737
|
|
|
647
738
|
// Try the read-only user endpoint first, then password sign-in. Preserve
|
|
@@ -651,7 +742,25 @@ async function balance(provider, auth) {
|
|
|
651
742
|
if (viaToken.ok) return viaToken;
|
|
652
743
|
const viaLogin = await signIn(provider, auth);
|
|
653
744
|
if (viaLogin.ok && viaLogin.balance) return { ok: true, balance: viaLogin.balance };
|
|
654
|
-
if (viaLogin.ok)
|
|
745
|
+
if (viaLogin.ok) {
|
|
746
|
+
if (viaToken.blocked) return { ...viaToken, loginNoQuota: true };
|
|
747
|
+
if (viaLogin.session) {
|
|
748
|
+
const sessionAuth = {
|
|
749
|
+
metadata: {
|
|
750
|
+
site: meta(auth, "site"),
|
|
751
|
+
siteURL: meta(auth, "siteURL"),
|
|
752
|
+
headers: meta(auth, "headers"),
|
|
753
|
+
cookie: `session=${viaLogin.session}`,
|
|
754
|
+
userId: viaLogin.userId || userId(auth),
|
|
755
|
+
},
|
|
756
|
+
};
|
|
757
|
+
const viaSession = await balanceByToken(provider, sessionAuth);
|
|
758
|
+
if (viaSession.ok) return viaSession;
|
|
759
|
+
if (viaSession.blocked) return { ...viaSession, loginNoQuota: true };
|
|
760
|
+
}
|
|
761
|
+
if (viaToken.expired) return { ...viaToken, loginNoQuota: true };
|
|
762
|
+
return { ok: false, why: "no-quota" };
|
|
763
|
+
}
|
|
655
764
|
// Report the reason the user can act on: a wrong password outranks a
|
|
656
765
|
// captcha, and a missing password means the token route's failure is the
|
|
657
766
|
// real one.
|
|
@@ -753,7 +862,7 @@ async function usageOf(getAuth, provider) {
|
|
|
753
862
|
const spend = await spendOf(provider, auth);
|
|
754
863
|
if (spend.ok) {
|
|
755
864
|
const reason = b.blocked
|
|
756
|
-
? "余额接口被滑动验证码拦截"
|
|
865
|
+
? (b.loginNoQuota ? "余额接口被滑动验证码拦截,且登录响应未返回有效额度" : "余额接口被滑动验证码拦截")
|
|
757
866
|
: b.expired ? "站点拒绝了余额查询凭据,请核对用户名密码或访问令牌" : "余额接口未返回可用余额";
|
|
758
867
|
return {
|
|
759
868
|
error: `AgentRouter:累计消耗 $${spend.spent.toFixed(2)};剩余额度未知(${reason})。读取余额需有效的 Cookie/访问令牌和用户 ID,或站点用户名密码;Cookie 不能保证通过验证码`,
|
|
@@ -764,7 +873,9 @@ async function usageOf(getAuth, provider) {
|
|
|
764
873
|
b.expired
|
|
765
874
|
? "AgentRouter:站点拒绝了凭据,请核对用户名密码、访问令牌或 Cookie 及用户 ID"
|
|
766
875
|
: b.blocked
|
|
767
|
-
?
|
|
876
|
+
? (b.loginNoQuota
|
|
877
|
+
? "AgentRouter:余额接口被滑动验证码拦截,且登录响应未返回有效额度,剩余额度未知(未覆写为 $0.00)"
|
|
878
|
+
: "AgentRouter:余额接口被滑动验证码拦截,剩余额度未知。Cookie 不能保证通过验证码;可尝试有效的站点用户名密码读取余额")
|
|
768
879
|
: b.why === "no-token"
|
|
769
880
|
? "AgentRouter:账号里没有访问令牌,也没有浏览器 Cookie,无法读取余额(个人设置 → 访问令牌)"
|
|
770
881
|
: b.why === "no-quota"
|
|
@@ -949,7 +1060,13 @@ async function checkinOf(getAuth, provider) {
|
|
|
949
1060
|
// wants: a fresh object each time, because magpie edits what it is given.
|
|
950
1061
|
function declaredModels() {
|
|
951
1062
|
const out = {};
|
|
952
|
-
for (const [id, name] of Object.entries(DECLARED_MODELS))
|
|
1063
|
+
for (const [id, name] of Object.entries(DECLARED_MODELS)) {
|
|
1064
|
+
out[id] = {
|
|
1065
|
+
...MODEL_SHAPE,
|
|
1066
|
+
name,
|
|
1067
|
+
capabilities: { reasoning: true, toolcall: true },
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
953
1070
|
return out;
|
|
954
1071
|
}
|
|
955
1072
|
|
|
@@ -964,7 +1081,9 @@ async function modelsOf(provider, { auth } = {}) {
|
|
|
964
1081
|
const key = apiKey(auth);
|
|
965
1082
|
if (!key) return fallback;
|
|
966
1083
|
try {
|
|
967
|
-
const
|
|
1084
|
+
const v1Base = apiBase(provider, auth);
|
|
1085
|
+
const v1betaBase = toGeminiBase(v1Base);
|
|
1086
|
+
const res = await fetch(`${v1Base}/models`, {
|
|
968
1087
|
headers: applyHeaders(new Headers(), { ...headersOf(provider, auth), Authorization: `Bearer ${key}` }),
|
|
969
1088
|
signal: withTimeout(REQUEST_TIMEOUT),
|
|
970
1089
|
});
|
|
@@ -973,14 +1092,43 @@ async function modelsOf(provider, { auth } = {}) {
|
|
|
973
1092
|
const out = {};
|
|
974
1093
|
for (const m of json.data) {
|
|
975
1094
|
if (!m?.id) continue;
|
|
1095
|
+
const name = m.name ?? m.display_name;
|
|
1096
|
+
const endpoints = m.supported_endpoint_types?.length ? { endpoints: m.supported_endpoint_types } : {};
|
|
1097
|
+
const limitOf = (defaults) => ({
|
|
1098
|
+
context: positiveNumber(m.context_length) ?? positiveNumber(m.context_window) ?? defaults.context,
|
|
1099
|
+
output: positiveNumber(m.max_output_tokens) ?? positiveNumber(m.max_tokens) ?? defaults.output,
|
|
1100
|
+
});
|
|
1101
|
+
if (isGeminiModel(m)) {
|
|
1102
|
+
const spec = Object.hasOwn(GEMINI_MODEL_DEFAULTS, m.id) ? GEMINI_MODEL_DEFAULTS[m.id] : GEMINI_FALLBACK_DEFAULTS;
|
|
1103
|
+
out[m.id] = {
|
|
1104
|
+
id: m.id,
|
|
1105
|
+
name: name ?? spec.name ?? m.id,
|
|
1106
|
+
api: { id: m.id, url: v1betaBase, npm: GEMINI_NPM },
|
|
1107
|
+
limit: limitOf(spec),
|
|
1108
|
+
capabilities: {
|
|
1109
|
+
temperature: true,
|
|
1110
|
+
reasoning: true,
|
|
1111
|
+
attachment: true,
|
|
1112
|
+
toolcall: true,
|
|
1113
|
+
input: { ...GEMINI_INPUT_MODALITIES },
|
|
1114
|
+
output: { ...TEXT_OUTPUT_MODALITIES },
|
|
1115
|
+
},
|
|
1116
|
+
variants: Object.fromEntries(spec.variants.map(v => [v, {}])),
|
|
1117
|
+
tool_call: true,
|
|
1118
|
+
reasoning: true,
|
|
1119
|
+
...endpoints,
|
|
1120
|
+
};
|
|
1121
|
+
continue;
|
|
1122
|
+
}
|
|
976
1123
|
out[m.id] = {
|
|
977
1124
|
...MODEL_SHAPE,
|
|
978
|
-
name:
|
|
979
|
-
limit:
|
|
980
|
-
|
|
981
|
-
|
|
1125
|
+
name: name ?? m.id,
|
|
1126
|
+
limit: limitOf(MODEL_SHAPE.limit),
|
|
1127
|
+
capabilities: {
|
|
1128
|
+
reasoning: true,
|
|
1129
|
+
toolcall: true,
|
|
982
1130
|
},
|
|
983
|
-
...
|
|
1131
|
+
...endpoints,
|
|
984
1132
|
// The vendor says "openai" for what is its Responses API; the gateway
|
|
985
1133
|
// reads that as the protocol the model speaks.
|
|
986
1134
|
responses: true,
|
|
@@ -1013,7 +1161,7 @@ export const AgentRouterAuthPlugin = async (input, options = {}) => {
|
|
|
1013
1161
|
cfg.provider ??= {};
|
|
1014
1162
|
cfg.provider[PROVIDER] ??= {
|
|
1015
1163
|
name: "AgentRouter",
|
|
1016
|
-
npm:
|
|
1164
|
+
npm: OPENAI_NPM,
|
|
1017
1165
|
api: `${ENDPOINT}/v1`,
|
|
1018
1166
|
models: declaredModels(),
|
|
1019
1167
|
};
|
|
@@ -1169,14 +1317,18 @@ export const AgentRouterAuthPlugin = async (input, options = {}) => {
|
|
|
1169
1317
|
message: "尚未配置模型 API Key,请使用 API Key 入口补充。系统访问令牌和 GitHub Cookie 不能用于模型调用。",
|
|
1170
1318
|
} }, { status: 400 });
|
|
1171
1319
|
}
|
|
1320
|
+
const { target, isGemini } = resolveGeminiTarget(input, init);
|
|
1172
1321
|
const h = new Headers(input instanceof Request ? input.headers : undefined);
|
|
1173
1322
|
new Headers(init.headers ?? {}).forEach((v, k) => h.set(k, v));
|
|
1174
1323
|
applyHeaders(h, headersOf(provider, auth));
|
|
1175
|
-
if (key)
|
|
1324
|
+
if (key) {
|
|
1325
|
+
h.set("Authorization", `Bearer ${key}`);
|
|
1326
|
+
if (isGemini) h.set("x-goog-api-key", key);
|
|
1327
|
+
}
|
|
1176
1328
|
const requestInit = { ...init, headers: h };
|
|
1177
1329
|
let prepared;
|
|
1178
1330
|
try {
|
|
1179
|
-
prepared = await prepareRequest(
|
|
1331
|
+
prepared = await prepareRequest(target, requestInit, {
|
|
1180
1332
|
portableResponses: portableResponses(),
|
|
1181
1333
|
textFilter: textFilterEnabled() ? textFilter : null,
|
|
1182
1334
|
sessionRepair: sessionRepairEnabled(),
|
|
@@ -1187,7 +1339,7 @@ export const AgentRouterAuthPlugin = async (input, options = {}) => {
|
|
|
1187
1339
|
type: "invalid_request_error", code: error.code, message: error.message,
|
|
1188
1340
|
} }, { status: 400 });
|
|
1189
1341
|
}
|
|
1190
|
-
return explainResponse(await fetch(
|
|
1342
|
+
return explainResponse(await fetch(target, prepared), {
|
|
1191
1343
|
retryAfterSeconds: meta(auth, "retryAfterSeconds") ?? options?.retryAfterSeconds,
|
|
1192
1344
|
});
|
|
1193
1345
|
},
|
|
@@ -1253,6 +1405,9 @@ export const _internal = {
|
|
|
1253
1405
|
headersOf,
|
|
1254
1406
|
applyHeaders,
|
|
1255
1407
|
apiBase,
|
|
1408
|
+
toGeminiBase,
|
|
1409
|
+
isGeminiModel,
|
|
1410
|
+
inspectGeminiUrl,
|
|
1256
1411
|
siteBase,
|
|
1257
1412
|
userHeaders,
|
|
1258
1413
|
masked,
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-agentrouter-auth",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "适用于 magpie 和 OpenCode 的 AgentRouter 插件:国际/国内站选择、API Key/GitHub
|
|
3
|
+
"version": "0.1.12",
|
|
4
|
+
"description": "适用于 magpie 和 OpenCode 的 AgentRouter 插件:国际/国内站选择、API Key/GitHub/系统访问令牌登录、自定义请求头、余额与签到、上游 Gemini Native 自动适配、文字替换、历史内联及可选的 Codex 会话资源清理",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.mjs",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test:policy": "node --test .github/scripts/pr-policy.test.cjs",
|
|
9
|
-
"test": "node --test body-inspection.test.mjs usage.test.mjs loader-stream.test.mjs response-bounds.test.mjs checkin.test.mjs local-checkin.test.mjs local-checkin.integration.test.mjs windows-acl.test.mjs request-replay.test.mjs content-filter.test.mjs auth-guidance.test.mjs auth-flow.test.mjs resource-compat.test.mjs auth-request-boundary.test.mjs response-preservation.test.mjs session-repair.test.mjs",
|
|
9
|
+
"test": "node --test body-inspection.test.mjs usage.test.mjs loader-stream.test.mjs response-bounds.test.mjs checkin.test.mjs local-checkin.test.mjs local-checkin.integration.test.mjs windows-acl.test.mjs request-replay.test.mjs content-filter.test.mjs auth-guidance.test.mjs auth-flow.test.mjs resource-compat.test.mjs auth-request-boundary.test.mjs response-preservation.test.mjs session-repair.test.mjs gemini-compat.test.mjs",
|
|
10
|
+
"test:gemini": "node --test gemini-compat.test.mjs",
|
|
10
11
|
"test:session-repair": "node --test session-repair.test.mjs",
|
|
11
12
|
"test:headers": "python3 set-ar-headers.test.py"
|
|
12
13
|
},
|
package/response.mjs
CHANGED
|
@@ -194,6 +194,14 @@ function hasChatOutput(choice) {
|
|
|
194
194
|
(choice.logprobs !== undefined && choice.logprobs !== null);
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
+
function unwrapErrorEnvelope(value) {
|
|
198
|
+
if (Array.isArray(value) && value.length === 1 && value[0] != null &&
|
|
199
|
+
typeof value[0] === "object" && !Array.isArray(value[0]) && "error" in value[0]) {
|
|
200
|
+
return value[0];
|
|
201
|
+
}
|
|
202
|
+
return value;
|
|
203
|
+
}
|
|
204
|
+
|
|
197
205
|
// null means harmless prelude; true means release unchanged; Response is an
|
|
198
206
|
// explicit early failure. Looking only in error envelopes avoids matching text
|
|
199
207
|
// in a perfectly good answer that quotes an error message.
|
|
@@ -213,7 +221,8 @@ function inspectEvent(frame, headers, retryAfterSeconds) {
|
|
|
213
221
|
if (data === "[DONE]") return true;
|
|
214
222
|
let j;
|
|
215
223
|
try { j = JSON.parse(data); } catch { return true; }
|
|
216
|
-
|
|
224
|
+
j = unwrapErrorEnvelope(j);
|
|
225
|
+
if (!j || typeof j !== "object" || Array.isArray(j)) return true;
|
|
217
226
|
const type = j.type ?? name;
|
|
218
227
|
const knownTypes = ["message", "error", "response.failed", "response.completed", "response.created", "response.in_progress", "response.output_item.added", "response.output_item.done", "response.content_part.added"];
|
|
219
228
|
const namedFailure = name === "error" || name === "response.failed";
|
|
@@ -319,6 +328,8 @@ export async function explainResponse(res, { retryAfterSeconds } = {}) {
|
|
|
319
328
|
if (text === null) return res;
|
|
320
329
|
let j;
|
|
321
330
|
try { j = JSON.parse(text); } catch { return res; }
|
|
331
|
+
j = unwrapErrorEnvelope(j);
|
|
332
|
+
if (!j || typeof j !== "object" || Array.isArray(j)) return res;
|
|
322
333
|
const error = j?.error ?? j?.response?.error ??
|
|
323
334
|
((res.status >= 400 || j?.type === "error" || j?.type === "response.failed") ? j : null);
|
|
324
335
|
return error ? failureResponse(j, res.headers, waitSeconds) ?? res : res;
|