mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-14 20:12:39 +08:00
Compare commits
261
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
325213ae66 | ||
|
|
0a215c9318 | ||
|
|
2061508ad8 | ||
|
|
fb90414c05 | ||
|
|
9fade3e8c2 | ||
|
|
1920a7d687 | ||
|
|
a444551a6d | ||
|
|
0bba06d64a | ||
|
|
150c52b8de | ||
|
|
3e4b6a09b2 | ||
|
|
dd2e6b3680 | ||
|
|
d4439129a7 | ||
|
|
1e1d15cca5 | ||
|
|
f7e959ae5f | ||
|
|
008391f699 | ||
|
|
a4c517917b | ||
|
|
e9169901d2 | ||
|
|
13c1d4cde2 | ||
|
|
4bd7132ba9 | ||
|
|
28a4ea914d | ||
|
|
5ecfaa4837 | ||
|
|
06af9f871c | ||
|
|
52558381c6 | ||
|
|
e3cce574d7 | ||
|
|
3d6c207723 | ||
|
|
a1d5968b24 | ||
|
|
e5272c130c | ||
|
|
196c15b5cd | ||
|
|
62635532a6 | ||
|
|
bc8da78150 | ||
|
|
d2c2b46c9e | ||
|
|
7e152e4279 | ||
|
|
bdb42e22f8 | ||
|
|
9383e0b154 | ||
|
|
c7ed614240 | ||
|
|
a59f0c7fac | ||
|
|
0a378f3432 | ||
|
|
f2b51e3b24 | ||
|
|
329641a86f | ||
|
|
f0dd497780 | ||
|
|
99b93b2985 | ||
|
|
67845665d6 | ||
|
|
3a070ec1bf | ||
|
|
749cd7c354 | ||
|
|
eaa4083e23 | ||
|
|
3873215908 | ||
|
|
2a4f3d1a71 | ||
|
|
4ff3e6dfb9 | ||
|
|
f78c4b241e | ||
|
|
e67ffda7ae | ||
|
|
d2067668d3 | ||
|
|
ba57ea914b | ||
|
|
5948988aae | ||
|
|
76b3f3c7cf | ||
|
|
db15ddf07f | ||
|
|
5d5b395d00 | ||
|
|
cf3aca6e3b | ||
|
|
956f4672e4 | ||
|
|
6206ce940e | ||
|
|
5968fd0ed9 | ||
|
|
c6727ed4c9 | ||
|
|
bb0b50d8bc | ||
|
|
429d6f0480 | ||
|
|
5bafffed51 | ||
|
|
781a02aea7 | ||
|
|
3645ce5f43 | ||
|
|
7cf90c79d6 | ||
|
|
195a331322 | ||
|
|
29371b0819 | ||
|
|
8e7954438d | ||
|
|
a65d476f25 | ||
|
|
961d9e94f3 | ||
|
|
51c5d03888 | ||
|
|
9b6a49b8e7 | ||
|
|
213de0797e | ||
|
|
d8326fccfc | ||
|
|
9eb120dd40 | ||
|
|
4726bdd08b | ||
|
|
8ea4dc56f0 | ||
|
|
23eef9ecc3 | ||
|
|
cdb5cfaf6c | ||
|
|
dfa83fbe93 | ||
|
|
2dff7af0f9 | ||
|
|
1067e89fab | ||
|
|
04c45e9dc2 | ||
|
|
5ee436d8a2 | ||
|
|
e5e04de137 | ||
|
|
fa565eafb1 | ||
|
|
eacff0a629 | ||
|
|
c0d5119375 | ||
|
|
22dffa1bb9 | ||
|
|
efcc2252e5 | ||
|
|
981279c996 | ||
|
|
c3b3072a6f | ||
|
|
489968fd7a | ||
|
|
7c008bd8c1 | ||
|
|
9c30951e4a | ||
|
|
40cf3f50a6 | ||
|
|
75b7dd1e0b | ||
|
|
642d20b8ac | ||
|
|
623c32e391 | ||
|
|
310f8b7fa2 | ||
|
|
6254aba5e9 | ||
|
|
7145484fde | ||
|
|
fd300ab6f3 | ||
|
|
88011a6a2e | ||
|
|
14029e50aa | ||
|
|
3fb03cdac1 | ||
|
|
8f9a9a255b | ||
|
|
bae37a00fb | ||
|
|
4543ddc5c1 | ||
|
|
5af7e7438a | ||
|
|
613722eee4 | ||
|
|
66a10d4939 | ||
|
|
d0ca057ca4 | ||
|
|
e4c369bd56 | ||
|
|
1035a4daf0 | ||
|
|
242907854d | ||
|
|
0116c5a1e1 | ||
|
|
67d3a896bd | ||
|
|
501cc19d0e | ||
|
|
8efe2fd8cf | ||
|
|
0aac71c6ee | ||
|
|
f4dd88b001 | ||
|
|
0be30886a5 | ||
|
|
324a47e2b7 | ||
|
|
2493b0dc31 | ||
|
|
264cbbec26 | ||
|
|
87e01596b7 | ||
|
|
43f9383d40 | ||
|
|
685e97a3a6 | ||
|
|
e71d291b39 | ||
|
|
4adcef4ff4 | ||
|
|
799e9938fb | ||
|
|
8f2cba0c20 | ||
|
|
6feefb7aec | ||
|
|
9d475f9edb | ||
|
|
ab9bd9e877 | ||
|
|
b112805ffa | ||
|
|
8c56eabcdc | ||
|
|
d3bd614ba7 | ||
|
|
e9f5add97c | ||
|
|
ab3b398b5f | ||
|
|
5032d06697 | ||
|
|
bed1e3c362 | ||
|
|
a5b07b2960 | ||
|
|
df8eceba0c | ||
|
|
95a04c8dff | ||
|
|
cd1ee1d1ad | ||
|
|
9e7c2d713c | ||
|
|
06b61ba96f | ||
|
|
4a4b50d807 | ||
|
|
1c0932e173 | ||
|
|
a4edda36d5 | ||
|
|
1ed36679bd | ||
|
|
4e5632c3e3 | ||
|
|
44bc47a3e3 | ||
|
|
21323c31f8 | ||
|
|
c76db386c0 | ||
|
|
58e6e6b325 | ||
|
|
567ea21dc8 | ||
|
|
b9d0728687 | ||
|
|
de76baaa0d | ||
|
|
26798773cb | ||
|
|
98d86915be | ||
|
|
5de5e2bed0 | ||
|
|
a0babc93dd | ||
|
|
7ccc8a6f50 | ||
|
|
29a36a10be | ||
|
|
f180fbb884 | ||
|
|
1d4e0436e4 | ||
|
|
270eac6973 | ||
|
|
266ac9c11b | ||
|
|
fd880839f6 | ||
|
|
570106ac17 | ||
|
|
775348b402 | ||
|
|
d9efbf8229 | ||
|
|
233f3dffe6 | ||
|
|
f9d7826711 | ||
|
|
c54897a59d | ||
|
|
29646ba6b4 | ||
|
|
6f0d0ababc | ||
|
|
52f7bcaedd | ||
|
|
95acbf1f03 | ||
|
|
43569bb44c | ||
|
|
15557e7796 | ||
|
|
e2fd418a96 | ||
|
|
4e9b01fd59 | ||
|
|
aa167a25a7 | ||
|
|
5cbb9191a7 | ||
|
|
ae4cc14b28 | ||
|
|
fa556700c2 | ||
|
|
68773aab98 | ||
|
|
1e6114c270 | ||
|
|
9e7039ebca | ||
|
|
1923d1c27d | ||
|
|
b638445234 | ||
|
|
9bcf8a5b80 | ||
|
|
979fe247f9 | ||
|
|
6378fb0cba | ||
|
|
3ee92b40c5 | ||
|
|
4892b8f17e | ||
|
|
28b807a735 | ||
|
|
4e5d67df35 | ||
|
|
dbe92a1c24 | ||
|
|
188e3a9f90 | ||
|
|
10940ecf39 | ||
|
|
3495635a52 | ||
|
|
c8deeb0b05 | ||
|
|
b6ee9f0a32 | ||
|
|
6c8ad0bd4a | ||
|
|
7137fae2a6 | ||
|
|
859de4c8fd | ||
|
|
e835f79819 | ||
|
|
99eba19ffc | ||
|
|
38cfd7e2d9 | ||
|
|
3fc08745a6 | ||
|
|
efc6e4a816 | ||
|
|
df64b5f368 | ||
|
|
772a0382f0 | ||
|
|
8fa67d477d | ||
|
|
ef891643e9 | ||
|
|
4a7739acdf | ||
|
|
14e0a49e17 | ||
|
|
b439dddfda | ||
|
|
7e62731863 | ||
|
|
b42157a725 | ||
|
|
335fcdc1d6 | ||
|
|
e094a3f40b | ||
|
|
57387445f3 | ||
|
|
2fc24d8879 | ||
|
|
cc91155fe4 | ||
|
|
b7dba62678 | ||
|
|
140e172951 | ||
|
|
941a0487ab | ||
|
|
1d4b9ead45 | ||
|
|
7f0f579bb0 | ||
|
|
dfb3978b9d | ||
|
|
6f2295bfc7 | ||
|
|
35e69af41b | ||
|
|
0d3dbae710 | ||
|
|
19382f275e | ||
|
|
be4ab92b2b | ||
|
|
76efc52fb6 | ||
|
|
6c2d2ed043 | ||
|
|
58f461b087 | ||
|
|
b97a798ebc | ||
|
|
017cc62e41 | ||
|
|
8ed57b000c | ||
|
|
27bcec7645 | ||
|
|
1859519573 | ||
|
|
8ed48e27a6 | ||
|
|
f5e4da5e72 | ||
|
|
9dc4c40bff | ||
|
|
81fd853005 | ||
|
|
01bd9b71a2 | ||
|
|
a10ff1255c | ||
|
|
163eb7ffae | ||
|
|
5fea83dcae | ||
|
|
6d339ec93b | ||
|
|
1ee929e4b9 |
@@ -4,6 +4,9 @@ FRONTEND_CRITICAL_VITEST := \
|
||||
src/i18n/__tests__/localeKeyCompleteness.spec.ts \
|
||||
src/api/__tests__/client.spec.ts \
|
||||
src/api/__tests__/tokenRefresh.spec.ts \
|
||||
src/api/__tests__/keys.bulkUpdate.spec.ts \
|
||||
src/components/keys/__tests__/BulkEditKeysModal.spec.ts \
|
||||
src/views/user/__tests__/KeysView.spec.ts \
|
||||
src/api/__tests__/channelMonitorV2.spec.ts \
|
||||
src/views/auth/__tests__/LinuxDoCallbackView.spec.ts \
|
||||
src/views/auth/__tests__/WechatCallbackView.spec.ts \
|
||||
|
||||
@@ -166,6 +166,16 @@ Please read the following carefully before using this project:
|
||||
<td>Thanks to AxisNow for sponsoring this project! <a href="https://www.axisnow.io/">AxisNow</a> protects and accelerates websites and APIs, delivering an optimal access experience across mainland China and globally, while extending acceleration and security capabilities to native/mobile apps through client SDKs — <strong>self-hosted private-deployment CDN</strong> | <strong>subscription-based DDoS-protected CDN</strong> | <strong>autonomous, flexibly composable CDN network</strong>.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="180"><a href="https://pp.dog/register?aff=SUB2API"><img src="assets/partners/logos/ppdog.png" alt="PP.dog" width="150"></a></td>
|
||||
<td><a href="https://pp.dog/register?aff=SUB2API">PP.dog</a> is a source-level API gateway with its own massive account pool, purpose-built as an upstream relay for downstream transit stations and high-frequency developers — saving you the hassle of building and maintaining your own account pool. ✅ Direct from source: self-operated account pool with no middleman markup; 🧧 Cost crusher: combined rate multiplier as low as 0.03x, just 0.35% of official pricing; 🚀 Blazing fast: first-token latency < 1s, on par with the official native API. <a href="https://www.pp.dog/register?aff=SUB2API">Get started with PP.dog now</a></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="180"><a href="https://colaproxy.com/?utm_source=sub2api&utm_medium=sub2api&ref=sub2api"><img src="assets/partners/logos/cola-proxy.jpg" alt="ColaProxy" width="150"></a></td>
|
||||
<td>ColaProxy provides high-quality residential proxies built for web scraping, automation, and multi-account management. Get a free trial with non-expiring traffic, pricing starting as low as $0.3/GB, unlimited concurrent connections, and intelligent IP rotation for a smoother, more stable proxy experience. Use discount code COLA10 to get 10% off and start scaling your projects with reliable residential proxies today. <a href="https://colaproxy.com/?utm_source=sub2api&utm_medium=sub2api&ref=sub2api">Get started with ColaProxy</a></td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
||||
## Overview
|
||||
|
||||
@@ -169,6 +169,16 @@
|
||||
<td>感谢 AxisNow 赞助了本项目!<a href="https://www.axisnow.io/">AxisNow</a> 保护并加速网站与 API,兼顾中国大陆及全球的访问体验,并通过客户端 SDK,将加速与安全能力延伸至原生/移动 App — <strong>自建私有部署 CDN</strong>|<strong>订阅式高防 CDN</strong>|<strong>自主可控、灵活组合的 CDN 网络</strong>。</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="180"><a href="https://pp.dog/register?aff=SUB2API"><img src="assets/partners/logos/ppdog.png" alt="PP.dog" width="150"></a></td>
|
||||
<td><a href="https://pp.dog/register?aff=SUB2API">PP.dog</a> 是自建账号池的源头 API 网关,专注为下游中转站与高频开发者提供 API 网关中继服务,帮您省去自建号池的一切麻烦——✅ 源头直供:自持海量账号池,无中间商赚差价;🧧 成本屠夫:综合倍率低至 0.03x,成本仅为官方的千分之3.5;🚀 极速体验:首 Token 延迟 < 1s,流畅媲美官方原生 API。<a href="https://www.pp.dog/register?aff=SUB2API">立即接入PP.dog</a></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="180"><a href="https://colaproxy.com/?utm_source=sub2api&utm_medium=sub2api&ref=sub2api"><img src="assets/partners/logos/cola-proxy.jpg" alt="ColaProxy" width="150"></a></td>
|
||||
<td>ColaProxy 提供专为网页抓取、自动化和多账号管理打造的高质量住宅代理。免费试用,流量永不过期,价格低至 $0.3/GB,支持无限并发连接和智能 IP 轮换,带来更流畅、更稳定的代理体验。使用优惠码 COLA10 立享 9 折优惠,立即开始使用可靠的住宅代理扩展您的项目。<a href="https://colaproxy.com/?utm_source=sub2api&utm_medium=sub2api&ref=sub2api">立即开始使用 ColaProxy</a></td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
||||
## 项目概述
|
||||
@@ -217,6 +227,30 @@ underscores_in_headers on;
|
||||
|
||||
Nginx 默认会丢弃名称中含下划线的请求头(如 `session_id`),这会导致多账号环境下的粘性会话功能失效。
|
||||
|
||||
## Codex Fast/Flex 策略说明
|
||||
|
||||
管理员后台的 `系统设置 -> 网关服务 -> OpenAI Fast/Flex 策略` 只负责处理请求体中的 `service_tier`,不会修改 Codex 客户端的模型目录,也不会让 Codex UI 自动出现 Speed 或 `/fast` 选项。
|
||||
|
||||
策略支持以下处理方式:
|
||||
|
||||
- `pass`:保留客户端传入的 `service_tier`;`fast` 会规范为上游使用的 `priority`。
|
||||
- `filter`:移除 `service_tier`,按普通优先级请求。
|
||||
- `block`:拒绝匹配的 Fast/Flex 请求。
|
||||
- `force_priority`:将匹配请求强制设置为 `priority`,会按 Priority/Fast 价格计费。为避免升级后改变既有规则语义,`all` 只匹配显式存在的 tier;如需让省略 `service_tier` 的 OpenAI 请求也强制升级,必须新增 `service_tier=missing + force_priority` 规则。非 OpenAI 平台不会执行缺省 tier 注入。这种方式可以让请求实际使用 Fast,但 Codex UI 仍可能不显示 Fast 状态。
|
||||
|
||||
Codex 的 Fast 入口由客户端模型目录驱动。只有当前模型目录声明了 `additional_speed_tiers: ["fast"]` 和对应的 `service_tiers`,Codex 才会显示 `/fast`。通过 API Key 或自定义模型提供商连接 Sub2API 时,如果模型目录没有这些字段,即使后台配置了 `force_priority`,重启 Codex 后也不会出现 Speed 选项。
|
||||
|
||||
客户端可在 `~/.codex/config.toml` 中直接指定默认请求级别:
|
||||
|
||||
```toml
|
||||
service_tier = "fast"
|
||||
|
||||
[features]
|
||||
fast_mode = true
|
||||
```
|
||||
|
||||
其中 `service_tier = "fast"` 会让请求携带 Fast 设置;`features.fast_mode` 只启用客户端 Fast 功能。模型目录没有声明 Fast 能力时,`/fast` 仍可能不显示。可通过 Sub2API 使用记录确认最终 `service_tier` 是否为 `priority`。
|
||||
|
||||
---
|
||||
|
||||
## 部署方式
|
||||
@@ -696,6 +730,14 @@ go generate ./cmd/server
|
||||
|
||||
---
|
||||
|
||||
## OpenAI 图片模型
|
||||
|
||||
支持 `gpt-image-2.5-flare`、`gpt-image-2.5-sunburst` 及其 `2026-09-08` 日期快照,可通过 `/v1/images/generations`、`/v1/images/edits` 调用。`quality` 支持 `xhigh`、`max`、`auto`,合法自定义尺寸和图片 usage 明细保持透传。
|
||||
|
||||
OAuth / Setup Token 图片请求使用 Responses 主控模型调用 `image_generation` 工具,默认主控为 `gpt-5.6-luna`。可设置 `SUB2API_IMAGES_MAIN_MODEL` 切换为账号支持的文本模型;Docker Compose 用户修改 `.env` 后执行 `docker compose up -d` 重建容器。该配置不会替换所选图片模型,也不会覆盖 `/v1/responses` 请求中已经提供的文本主控模型。
|
||||
|
||||
升级后,无模型限制的账号自动支持新模型。已有显式账号映射或分组白名单需要加入两个 2.5 模型(日期快照按需加入);升级不会自动扩大管理员设置的模型权限。新模型内置价格包含官方文本输入、图片输入和图片输出 token 费率,远端价格表尚未更新时使用内置 2.5 价格;实际按次或按 token 计费仍由既有分组/渠道配置决定。
|
||||
|
||||
## 简易模式
|
||||
|
||||
简易模式适合个人开发者或内部团队快速使用,不依赖完整 SaaS 功能。
|
||||
|
||||
@@ -168,6 +168,16 @@
|
||||
<td>AxisNow のご支援に感謝します!<a href="https://www.axisnow.io/">AxisNow</a> はウェブサイトと API を保護・高速化し、中国本土および世界中で快適なアクセス体験を実現します。クライアント SDK により、ネイティブ/モバイルアプリにも高速化・セキュリティ機能を拡張できます — <strong>自社構築・プライベートデプロイ CDN</strong>|<strong>サブスクリプション型高防御 CDN</strong>|<strong>自律制御で柔軟に組み合わせられる CDN ネットワーク</strong>。</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="180"><a href="https://pp.dog/register?aff=SUB2API"><img src="assets/partners/logos/ppdog.png" alt="PP.dog" width="150"></a></td>
|
||||
<td><a href="https://pp.dog/register?aff=SUB2API">PP.dog</a> は自社運用の大規模アカウントプールを持つソースレベルの API ゲートウェイで、下流の中継ステーションや高頻度開発者向けに API ゲートウェイリレーサービスを提供し、アカウントプールの自主構築にかかるすべての手間を省きます。✅ ソース直供:自社運用の大規模アカウントプール、中間マージンなし;🧧 コスト最強:総合レート倍率わずか 0.03x、コストは公式の 0.35% のみ;🚀 超高速体験:ファーストトークンレイテンシ 1 秒未満、公式ネイティブ API に匹敵するスムーズさ。<a href="https://www.pp.dog/register?aff=SUB2API">今すぐ PP.dog に接続</a></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="180"><a href="https://colaproxy.com/?utm_source=sub2api&utm_medium=sub2api&ref=sub2api"><img src="assets/partners/logos/cola-proxy.jpg" alt="ColaProxy" width="150"></a></td>
|
||||
<td>ColaProxy は、Web スクレイピング、自動化、マルチアカウント管理向けに構築された高品質な住宅プロキシを提供します。有効期限なしのトラフィックで無料トライアルが利用でき、料金は $0.3/GB から、無制限の同時接続とインテリジェントな IP ローテーションにより、よりスムーズで安定したプロキシ体験を実現します。割引コード COLA10 で 10% オフ、信頼性の高い住宅プロキシでプロジェクトを今すぐスケールしましょう。<a href="https://colaproxy.com/?utm_source=sub2api&utm_medium=sub2api&ref=sub2api">ColaProxy を始める</a></td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
||||
## 概要
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 6.8 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 75 KiB |
@@ -1 +1 @@
|
||||
0.2.1
|
||||
0.2.4
|
||||
|
||||
@@ -117,10 +117,12 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
||||
openAI403CounterCache := repository.NewOpenAI403CounterCache(redisClient)
|
||||
geminiTokenCache := repository.NewGeminiTokenCache(redisClient)
|
||||
compositeTokenCacheInvalidator := service.NewCompositeTokenCacheInvalidator(geminiTokenCache)
|
||||
rateLimitService := service.ProvideRateLimitService(accountRepository, usageLogRepository, configConfig, geminiQuotaService, tempUnschedCache, timeoutCounterCache, openAI403CounterCache, settingService, compositeTokenCacheInvalidator)
|
||||
httpUpstream := repository.NewHTTPUpstream(configConfig)
|
||||
leaderLockCache := repository.NewLeaderLockCache(redisClient)
|
||||
ollamaCloudUsageService := service.ProvideOllamaCloudUsageService(accountRepository, httpUpstream, settingService, secretEncryptor, configConfig, leaderLockCache, db)
|
||||
rateLimitService := service.ProvideRateLimitService(accountRepository, usageLogRepository, configConfig, geminiQuotaService, tempUnschedCache, timeoutCounterCache, openAI403CounterCache, settingService, compositeTokenCacheInvalidator, ollamaCloudUsageService)
|
||||
identityCache := repository.NewIdentityCache(redisClient)
|
||||
identityService := service.NewIdentityService(identityCache)
|
||||
httpUpstream := repository.NewHTTPUpstream(configConfig)
|
||||
timingWheelService, err := service.ProvideTimingWheelService()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -137,7 +139,8 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
||||
tlsFingerprintProfileCache := repository.NewTLSFingerprintProfileCache(redisClient)
|
||||
tlsFingerprintProfileService := service.NewTLSFingerprintProfileService(tlsFingerprintProfileRepository, tlsFingerprintProfileCache)
|
||||
channelRepository := repository.NewChannelRepository(db)
|
||||
channelService := service.NewChannelService(channelRepository, groupRepository, apiKeyAuthCacheInvalidator, pricingService)
|
||||
channelCachePubSub := repository.NewChannelCache(redisClient)
|
||||
channelService := service.NewChannelService(channelRepository, groupRepository, apiKeyAuthCacheInvalidator, pricingService, channelCachePubSub)
|
||||
modelPricingResolver := service.NewModelPricingResolver(channelService, billingService)
|
||||
compositeModelRouteRepository := repository.NewCompositeModelRouteRepository(client)
|
||||
compositeRouteResolver := service.NewCompositeRouteResolver(compositeModelRouteRepository)
|
||||
@@ -182,7 +185,6 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
||||
dashboardAggregationRepository := repository.NewDashboardAggregationRepository(db)
|
||||
dashboardStatsCache := repository.NewDashboardCache(redisClient, configConfig)
|
||||
dashboardService := service.NewDashboardService(usageLogRepository, dashboardAggregationRepository, dashboardStatsCache, configConfig)
|
||||
leaderLockCache := repository.NewLeaderLockCache(redisClient)
|
||||
dashboardAggregationService := service.ProvideDashboardAggregationService(dashboardAggregationRepository, timingWheelService, leaderLockCache, db, configConfig)
|
||||
dashboardHandler := admin.NewDashboardHandler(dashboardService, dashboardAggregationService)
|
||||
adminGroupRepository := repository.NewAdminGroupRepository(client, db)
|
||||
@@ -283,7 +285,6 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
||||
auditLogService := service.ProvideAuditLogService(auditLogRepository, settingService)
|
||||
auditLogHandler := admin.NewAuditLogHandler(auditLogService, totpService)
|
||||
upstreamBillingProbeService := service.ProvideUpstreamBillingProbeService(accountRepository, accountTestService, settingService, leaderLockCache, db)
|
||||
ollamaCloudUsageService := service.ProvideOllamaCloudUsageService(accountRepository, httpUpstream, settingService, secretEncryptor, configConfig, leaderLockCache, db)
|
||||
adminHandlers := handler.ProvideAdminHandlers(dashboardHandler, adminUserHandler, groupHandler, accountHandler, adminAnnouncementHandler, dataManagementHandler, backupHandler, oAuthHandler, openAIOAuthHandler, geminiOAuthHandler, antigravityOAuthHandler, grokOAuthHandler, cnProviderHandler, proxyHandler, adminRedeemHandler, promoHandler, settingHandler, opsHandler, systemHandler, adminSubscriptionHandler, adminUsageHandler, userAttributeHandler, errorPassthroughHandler, tlsFingerprintProfileHandler, pluginHandler, adminAPIKeyHandler, scheduledTestHandler, channelHandler, channelMonitorHandler, channelMonitorRequestTemplateHandler, contentModerationHandler, promptAdminHandler, paymentHandler, affiliateHandler, complianceHandler, auditLogHandler, upstreamBillingProbeService, ollamaCloudUsageService)
|
||||
usageRecordWorkerPool := service.NewUsageRecordWorkerPool(configConfig)
|
||||
userMsgQueueCache := repository.NewUserMsgQueueCache(redisClient)
|
||||
|
||||
@@ -182,6 +182,8 @@ const (
|
||||
ProviderKimi Provider = "kimi"
|
||||
ProviderZhipu Provider = "zhipu"
|
||||
ProviderDeepseek Provider = "deepseek"
|
||||
ProviderMinimax Provider = "minimax"
|
||||
ProviderOpencodeGo Provider = "opencode_go"
|
||||
)
|
||||
|
||||
func (pr Provider) String() string {
|
||||
@@ -191,7 +193,7 @@ func (pr Provider) String() string {
|
||||
// ProviderValidator is a validator for the "provider" field enum values. It is called by the builders before save.
|
||||
func ProviderValidator(pr Provider) error {
|
||||
switch pr {
|
||||
case ProviderOpenai, ProviderAnthropic, ProviderGemini, ProviderGrok, ProviderAntigravity, ProviderKimi, ProviderZhipu, ProviderDeepseek:
|
||||
case ProviderOpenai, ProviderAnthropic, ProviderGemini, ProviderGrok, ProviderAntigravity, ProviderKimi, ProviderZhipu, ProviderDeepseek, ProviderMinimax, ProviderOpencodeGo:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("channelmonitor: invalid enum value for provider field: %q", pr)
|
||||
|
||||
@@ -108,6 +108,8 @@ const (
|
||||
ProviderKimi Provider = "kimi"
|
||||
ProviderZhipu Provider = "zhipu"
|
||||
ProviderDeepseek Provider = "deepseek"
|
||||
ProviderMinimax Provider = "minimax"
|
||||
ProviderOpencodeGo Provider = "opencode_go"
|
||||
)
|
||||
|
||||
func (pr Provider) String() string {
|
||||
@@ -117,7 +119,7 @@ func (pr Provider) String() string {
|
||||
// ProviderValidator is a validator for the "provider" field enum values. It is called by the builders before save.
|
||||
func ProviderValidator(pr Provider) error {
|
||||
switch pr {
|
||||
case ProviderOpenai, ProviderAnthropic, ProviderGemini, ProviderGrok, ProviderAntigravity, ProviderKimi, ProviderZhipu, ProviderDeepseek:
|
||||
case ProviderOpenai, ProviderAnthropic, ProviderGemini, ProviderGrok, ProviderAntigravity, ProviderKimi, ProviderZhipu, ProviderDeepseek, ProviderMinimax, ProviderOpencodeGo:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("channelmonitorrequesttemplate: invalid enum value for provider field: %q", pr)
|
||||
|
||||
+17
-1
@@ -4595,6 +4595,22 @@ func (c *ProxyClient) QueryAccounts(_m *Proxy) *AccountQuery {
|
||||
return query
|
||||
}
|
||||
|
||||
// QueryPrimaryProxies queries the primary_proxies edge of a Proxy.
|
||||
func (c *ProxyClient) QueryPrimaryProxies(_m *Proxy) *ProxyQuery {
|
||||
query := (&ProxyClient{config: c.config}).Query()
|
||||
query.path = func(context.Context) (fromV *sql.Selector, _ error) {
|
||||
id := _m.ID
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(proxy.Table, proxy.FieldID, id),
|
||||
sqlgraph.To(proxy.Table, proxy.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, true, proxy.PrimaryProxiesTable, proxy.PrimaryProxiesColumn),
|
||||
)
|
||||
fromV = sqlgraph.Neighbors(_m.driver.Dialect(), step)
|
||||
return fromV, nil
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
// QueryBackupProxy queries the backup_proxy edge of a Proxy.
|
||||
func (c *ProxyClient) QueryBackupProxy(_m *Proxy) *ProxyQuery {
|
||||
query := (&ProxyClient{config: c.config}).Query()
|
||||
@@ -4603,7 +4619,7 @@ func (c *ProxyClient) QueryBackupProxy(_m *Proxy) *ProxyQuery {
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(proxy.Table, proxy.FieldID, id),
|
||||
sqlgraph.To(proxy.Table, proxy.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2O, false, proxy.BackupProxyTable, proxy.BackupProxyColumn),
|
||||
sqlgraph.Edge(sqlgraph.M2O, false, proxy.BackupProxyTable, proxy.BackupProxyColumn),
|
||||
)
|
||||
fromV = sqlgraph.Neighbors(_m.driver.Dialect(), step)
|
||||
return fromV, nil
|
||||
|
||||
@@ -623,7 +623,7 @@ var (
|
||||
{Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
|
||||
{Name: "updated_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
|
||||
{Name: "name", Type: field.TypeString, Size: 100},
|
||||
{Name: "provider", Type: field.TypeEnum, Enums: []string{"openai", "anthropic", "gemini", "grok", "antigravity", "kimi", "zhipu", "deepseek"}},
|
||||
{Name: "provider", Type: field.TypeEnum, Enums: []string{"openai", "anthropic", "gemini", "grok", "antigravity", "kimi", "zhipu", "deepseek", "minimax", "opencode_go"}},
|
||||
{Name: "check_mode", Type: field.TypeString, Size: 32, Default: "probe"},
|
||||
{Name: "account_id", Type: field.TypeInt64, Nullable: true},
|
||||
{Name: "api_mode", Type: field.TypeString, Size: 32, Default: "chat_completions"},
|
||||
@@ -776,7 +776,7 @@ var (
|
||||
{Name: "created_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
|
||||
{Name: "updated_at", Type: field.TypeTime, SchemaType: map[string]string{"postgres": "timestamptz"}},
|
||||
{Name: "name", Type: field.TypeString, Size: 100},
|
||||
{Name: "provider", Type: field.TypeEnum, Enums: []string{"openai", "anthropic", "gemini", "grok", "antigravity", "kimi", "zhipu", "deepseek"}},
|
||||
{Name: "provider", Type: field.TypeEnum, Enums: []string{"openai", "anthropic", "gemini", "grok", "antigravity", "kimi", "zhipu", "deepseek", "minimax", "opencode_go"}},
|
||||
{Name: "api_mode", Type: field.TypeString, Size: 32, Default: "chat_completions"},
|
||||
{Name: "description", Type: field.TypeString, Nullable: true, Size: 500, Default: ""},
|
||||
{Name: "extra_headers", Type: field.TypeJSON},
|
||||
@@ -1413,7 +1413,7 @@ var (
|
||||
{Name: "expires_at", Type: field.TypeTime, Nullable: true},
|
||||
{Name: "fallback_mode", Type: field.TypeString, Size: 20, Default: "none"},
|
||||
{Name: "expiry_warn_days", Type: field.TypeInt, Default: 7},
|
||||
{Name: "backup_proxy_id", Type: field.TypeInt64, Unique: true, Nullable: true},
|
||||
{Name: "backup_proxy_id", Type: field.TypeInt64, Nullable: true},
|
||||
}
|
||||
// ProxiesTable holds the schema information for the "proxies" table.
|
||||
ProxiesTable = &schema.Table{
|
||||
|
||||
+113
-30
@@ -37366,33 +37366,36 @@ func (m *PromoCodeUsageMutation) ResetEdge(name string) error {
|
||||
// ProxyMutation represents an operation that mutates the Proxy nodes in the graph.
|
||||
type ProxyMutation struct {
|
||||
config
|
||||
op Op
|
||||
typ string
|
||||
id *int64
|
||||
created_at *time.Time
|
||||
updated_at *time.Time
|
||||
deleted_at *time.Time
|
||||
name *string
|
||||
protocol *string
|
||||
host *string
|
||||
port *int
|
||||
addport *int
|
||||
username *string
|
||||
password *string
|
||||
status *string
|
||||
expires_at *time.Time
|
||||
fallback_mode *string
|
||||
expiry_warn_days *int
|
||||
addexpiry_warn_days *int
|
||||
clearedFields map[string]struct{}
|
||||
accounts map[int64]struct{}
|
||||
removedaccounts map[int64]struct{}
|
||||
clearedaccounts bool
|
||||
backup_proxy *int64
|
||||
clearedbackup_proxy bool
|
||||
done bool
|
||||
oldValue func(context.Context) (*Proxy, error)
|
||||
predicates []predicate.Proxy
|
||||
op Op
|
||||
typ string
|
||||
id *int64
|
||||
created_at *time.Time
|
||||
updated_at *time.Time
|
||||
deleted_at *time.Time
|
||||
name *string
|
||||
protocol *string
|
||||
host *string
|
||||
port *int
|
||||
addport *int
|
||||
username *string
|
||||
password *string
|
||||
status *string
|
||||
expires_at *time.Time
|
||||
fallback_mode *string
|
||||
expiry_warn_days *int
|
||||
addexpiry_warn_days *int
|
||||
clearedFields map[string]struct{}
|
||||
accounts map[int64]struct{}
|
||||
removedaccounts map[int64]struct{}
|
||||
clearedaccounts bool
|
||||
primary_proxies map[int64]struct{}
|
||||
removedprimary_proxies map[int64]struct{}
|
||||
clearedprimary_proxies bool
|
||||
backup_proxy *int64
|
||||
clearedbackup_proxy bool
|
||||
done bool
|
||||
oldValue func(context.Context) (*Proxy, error)
|
||||
predicates []predicate.Proxy
|
||||
}
|
||||
|
||||
var _ ent.Mutation = (*ProxyMutation)(nil)
|
||||
@@ -38156,6 +38159,60 @@ func (m *ProxyMutation) ResetAccounts() {
|
||||
m.removedaccounts = nil
|
||||
}
|
||||
|
||||
// AddPrimaryProxyIDs adds the "primary_proxies" edge to the Proxy entity by ids.
|
||||
func (m *ProxyMutation) AddPrimaryProxyIDs(ids ...int64) {
|
||||
if m.primary_proxies == nil {
|
||||
m.primary_proxies = make(map[int64]struct{})
|
||||
}
|
||||
for i := range ids {
|
||||
m.primary_proxies[ids[i]] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// ClearPrimaryProxies clears the "primary_proxies" edge to the Proxy entity.
|
||||
func (m *ProxyMutation) ClearPrimaryProxies() {
|
||||
m.clearedprimary_proxies = true
|
||||
}
|
||||
|
||||
// PrimaryProxiesCleared reports if the "primary_proxies" edge to the Proxy entity was cleared.
|
||||
func (m *ProxyMutation) PrimaryProxiesCleared() bool {
|
||||
return m.clearedprimary_proxies
|
||||
}
|
||||
|
||||
// RemovePrimaryProxyIDs removes the "primary_proxies" edge to the Proxy entity by IDs.
|
||||
func (m *ProxyMutation) RemovePrimaryProxyIDs(ids ...int64) {
|
||||
if m.removedprimary_proxies == nil {
|
||||
m.removedprimary_proxies = make(map[int64]struct{})
|
||||
}
|
||||
for i := range ids {
|
||||
delete(m.primary_proxies, ids[i])
|
||||
m.removedprimary_proxies[ids[i]] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// RemovedPrimaryProxies returns the removed IDs of the "primary_proxies" edge to the Proxy entity.
|
||||
func (m *ProxyMutation) RemovedPrimaryProxiesIDs() (ids []int64) {
|
||||
for id := range m.removedprimary_proxies {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// PrimaryProxiesIDs returns the "primary_proxies" edge IDs in the mutation.
|
||||
func (m *ProxyMutation) PrimaryProxiesIDs() (ids []int64) {
|
||||
for id := range m.primary_proxies {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ResetPrimaryProxies resets all changes to the "primary_proxies" edge.
|
||||
func (m *ProxyMutation) ResetPrimaryProxies() {
|
||||
m.primary_proxies = nil
|
||||
m.clearedprimary_proxies = false
|
||||
m.removedprimary_proxies = nil
|
||||
}
|
||||
|
||||
// ClearBackupProxy clears the "backup_proxy" edge to the Proxy entity.
|
||||
func (m *ProxyMutation) ClearBackupProxy() {
|
||||
m.clearedbackup_proxy = true
|
||||
@@ -38597,10 +38654,13 @@ func (m *ProxyMutation) ResetField(name string) error {
|
||||
|
||||
// AddedEdges returns all edge names that were set/added in this mutation.
|
||||
func (m *ProxyMutation) AddedEdges() []string {
|
||||
edges := make([]string, 0, 2)
|
||||
edges := make([]string, 0, 3)
|
||||
if m.accounts != nil {
|
||||
edges = append(edges, proxy.EdgeAccounts)
|
||||
}
|
||||
if m.primary_proxies != nil {
|
||||
edges = append(edges, proxy.EdgePrimaryProxies)
|
||||
}
|
||||
if m.backup_proxy != nil {
|
||||
edges = append(edges, proxy.EdgeBackupProxy)
|
||||
}
|
||||
@@ -38617,6 +38677,12 @@ func (m *ProxyMutation) AddedIDs(name string) []ent.Value {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return ids
|
||||
case proxy.EdgePrimaryProxies:
|
||||
ids := make([]ent.Value, 0, len(m.primary_proxies))
|
||||
for id := range m.primary_proxies {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return ids
|
||||
case proxy.EdgeBackupProxy:
|
||||
if id := m.backup_proxy; id != nil {
|
||||
return []ent.Value{*id}
|
||||
@@ -38627,10 +38693,13 @@ func (m *ProxyMutation) AddedIDs(name string) []ent.Value {
|
||||
|
||||
// RemovedEdges returns all edge names that were removed in this mutation.
|
||||
func (m *ProxyMutation) RemovedEdges() []string {
|
||||
edges := make([]string, 0, 2)
|
||||
edges := make([]string, 0, 3)
|
||||
if m.removedaccounts != nil {
|
||||
edges = append(edges, proxy.EdgeAccounts)
|
||||
}
|
||||
if m.removedprimary_proxies != nil {
|
||||
edges = append(edges, proxy.EdgePrimaryProxies)
|
||||
}
|
||||
return edges
|
||||
}
|
||||
|
||||
@@ -38644,16 +38713,25 @@ func (m *ProxyMutation) RemovedIDs(name string) []ent.Value {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return ids
|
||||
case proxy.EdgePrimaryProxies:
|
||||
ids := make([]ent.Value, 0, len(m.removedprimary_proxies))
|
||||
for id := range m.removedprimary_proxies {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ClearedEdges returns all edge names that were cleared in this mutation.
|
||||
func (m *ProxyMutation) ClearedEdges() []string {
|
||||
edges := make([]string, 0, 2)
|
||||
edges := make([]string, 0, 3)
|
||||
if m.clearedaccounts {
|
||||
edges = append(edges, proxy.EdgeAccounts)
|
||||
}
|
||||
if m.clearedprimary_proxies {
|
||||
edges = append(edges, proxy.EdgePrimaryProxies)
|
||||
}
|
||||
if m.clearedbackup_proxy {
|
||||
edges = append(edges, proxy.EdgeBackupProxy)
|
||||
}
|
||||
@@ -38666,6 +38744,8 @@ func (m *ProxyMutation) EdgeCleared(name string) bool {
|
||||
switch name {
|
||||
case proxy.EdgeAccounts:
|
||||
return m.clearedaccounts
|
||||
case proxy.EdgePrimaryProxies:
|
||||
return m.clearedprimary_proxies
|
||||
case proxy.EdgeBackupProxy:
|
||||
return m.clearedbackup_proxy
|
||||
}
|
||||
@@ -38690,6 +38770,9 @@ func (m *ProxyMutation) ResetEdge(name string) error {
|
||||
case proxy.EdgeAccounts:
|
||||
m.ResetAccounts()
|
||||
return nil
|
||||
case proxy.EdgePrimaryProxies:
|
||||
m.ResetPrimaryProxies()
|
||||
return nil
|
||||
case proxy.EdgeBackupProxy:
|
||||
m.ResetBackupProxy()
|
||||
return nil
|
||||
|
||||
+18
-2
@@ -55,11 +55,13 @@ type Proxy struct {
|
||||
type ProxyEdges struct {
|
||||
// Accounts holds the value of the accounts edge.
|
||||
Accounts []*Account `json:"accounts,omitempty"`
|
||||
// PrimaryProxies holds the value of the primary_proxies edge.
|
||||
PrimaryProxies []*Proxy `json:"primary_proxies,omitempty"`
|
||||
// BackupProxy holds the value of the backup_proxy edge.
|
||||
BackupProxy *Proxy `json:"backup_proxy,omitempty"`
|
||||
// loadedTypes holds the information for reporting if a
|
||||
// type was loaded (or requested) in eager-loading or not.
|
||||
loadedTypes [2]bool
|
||||
loadedTypes [3]bool
|
||||
}
|
||||
|
||||
// AccountsOrErr returns the Accounts value or an error if the edge
|
||||
@@ -71,12 +73,21 @@ func (e ProxyEdges) AccountsOrErr() ([]*Account, error) {
|
||||
return nil, &NotLoadedError{edge: "accounts"}
|
||||
}
|
||||
|
||||
// PrimaryProxiesOrErr returns the PrimaryProxies value or an error if the edge
|
||||
// was not loaded in eager-loading.
|
||||
func (e ProxyEdges) PrimaryProxiesOrErr() ([]*Proxy, error) {
|
||||
if e.loadedTypes[1] {
|
||||
return e.PrimaryProxies, nil
|
||||
}
|
||||
return nil, &NotLoadedError{edge: "primary_proxies"}
|
||||
}
|
||||
|
||||
// BackupProxyOrErr returns the BackupProxy value or an error if the edge
|
||||
// was not loaded in eager-loading, or loaded but was not found.
|
||||
func (e ProxyEdges) BackupProxyOrErr() (*Proxy, error) {
|
||||
if e.BackupProxy != nil {
|
||||
return e.BackupProxy, nil
|
||||
} else if e.loadedTypes[1] {
|
||||
} else if e.loadedTypes[2] {
|
||||
return nil, &NotFoundError{label: proxy.Label}
|
||||
}
|
||||
return nil, &NotLoadedError{edge: "backup_proxy"}
|
||||
@@ -221,6 +232,11 @@ func (_m *Proxy) QueryAccounts() *AccountQuery {
|
||||
return NewProxyClient(_m.config).QueryAccounts(_m)
|
||||
}
|
||||
|
||||
// QueryPrimaryProxies queries the "primary_proxies" edge of the Proxy entity.
|
||||
func (_m *Proxy) QueryPrimaryProxies() *ProxyQuery {
|
||||
return NewProxyClient(_m.config).QueryPrimaryProxies(_m)
|
||||
}
|
||||
|
||||
// QueryBackupProxy queries the "backup_proxy" edge of the Proxy entity.
|
||||
func (_m *Proxy) QueryBackupProxy() *ProxyQuery {
|
||||
return NewProxyClient(_m.config).QueryBackupProxy(_m)
|
||||
|
||||
@@ -45,6 +45,8 @@ const (
|
||||
FieldExpiryWarnDays = "expiry_warn_days"
|
||||
// EdgeAccounts holds the string denoting the accounts edge name in mutations.
|
||||
EdgeAccounts = "accounts"
|
||||
// EdgePrimaryProxies holds the string denoting the primary_proxies edge name in mutations.
|
||||
EdgePrimaryProxies = "primary_proxies"
|
||||
// EdgeBackupProxy holds the string denoting the backup_proxy edge name in mutations.
|
||||
EdgeBackupProxy = "backup_proxy"
|
||||
// Table holds the table name of the proxy in the database.
|
||||
@@ -56,6 +58,10 @@ const (
|
||||
AccountsInverseTable = "accounts"
|
||||
// AccountsColumn is the table column denoting the accounts relation/edge.
|
||||
AccountsColumn = "proxy_id"
|
||||
// PrimaryProxiesTable is the table that holds the primary_proxies relation/edge.
|
||||
PrimaryProxiesTable = "proxies"
|
||||
// PrimaryProxiesColumn is the table column denoting the primary_proxies relation/edge.
|
||||
PrimaryProxiesColumn = "backup_proxy_id"
|
||||
// BackupProxyTable is the table that holds the backup_proxy relation/edge.
|
||||
BackupProxyTable = "proxies"
|
||||
// BackupProxyColumn is the table column denoting the backup_proxy relation/edge.
|
||||
@@ -219,6 +225,20 @@ func ByAccounts(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption {
|
||||
}
|
||||
}
|
||||
|
||||
// ByPrimaryProxiesCount orders the results by primary_proxies count.
|
||||
func ByPrimaryProxiesCount(opts ...sql.OrderTermOption) OrderOption {
|
||||
return func(s *sql.Selector) {
|
||||
sqlgraph.OrderByNeighborsCount(s, newPrimaryProxiesStep(), opts...)
|
||||
}
|
||||
}
|
||||
|
||||
// ByPrimaryProxies orders the results by primary_proxies terms.
|
||||
func ByPrimaryProxies(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption {
|
||||
return func(s *sql.Selector) {
|
||||
sqlgraph.OrderByNeighborTerms(s, newPrimaryProxiesStep(), append([]sql.OrderTerm{term}, terms...)...)
|
||||
}
|
||||
}
|
||||
|
||||
// ByBackupProxyField orders the results by backup_proxy field.
|
||||
func ByBackupProxyField(field string, opts ...sql.OrderTermOption) OrderOption {
|
||||
return func(s *sql.Selector) {
|
||||
@@ -232,10 +252,17 @@ func newAccountsStep() *sqlgraph.Step {
|
||||
sqlgraph.Edge(sqlgraph.O2M, true, AccountsTable, AccountsColumn),
|
||||
)
|
||||
}
|
||||
func newPrimaryProxiesStep() *sqlgraph.Step {
|
||||
return sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.To(Table, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, true, PrimaryProxiesTable, PrimaryProxiesColumn),
|
||||
)
|
||||
}
|
||||
func newBackupProxyStep() *sqlgraph.Step {
|
||||
return sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.To(Table, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2O, false, BackupProxyTable, BackupProxyColumn),
|
||||
sqlgraph.Edge(sqlgraph.M2O, false, BackupProxyTable, BackupProxyColumn),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -913,12 +913,35 @@ func HasAccountsWith(preds ...predicate.Account) predicate.Proxy {
|
||||
})
|
||||
}
|
||||
|
||||
// HasPrimaryProxies applies the HasEdge predicate on the "primary_proxies" edge.
|
||||
func HasPrimaryProxies() predicate.Proxy {
|
||||
return predicate.Proxy(func(s *sql.Selector) {
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, true, PrimaryProxiesTable, PrimaryProxiesColumn),
|
||||
)
|
||||
sqlgraph.HasNeighbors(s, step)
|
||||
})
|
||||
}
|
||||
|
||||
// HasPrimaryProxiesWith applies the HasEdge predicate on the "primary_proxies" edge with a given conditions (other predicates).
|
||||
func HasPrimaryProxiesWith(preds ...predicate.Proxy) predicate.Proxy {
|
||||
return predicate.Proxy(func(s *sql.Selector) {
|
||||
step := newPrimaryProxiesStep()
|
||||
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
|
||||
for _, p := range preds {
|
||||
p(s)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// HasBackupProxy applies the HasEdge predicate on the "backup_proxy" edge.
|
||||
func HasBackupProxy() predicate.Proxy {
|
||||
return predicate.Proxy(func(s *sql.Selector) {
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(Table, FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2O, false, BackupProxyTable, BackupProxyColumn),
|
||||
sqlgraph.Edge(sqlgraph.M2O, false, BackupProxyTable, BackupProxyColumn),
|
||||
)
|
||||
sqlgraph.HasNeighbors(s, step)
|
||||
})
|
||||
|
||||
@@ -202,6 +202,21 @@ func (_c *ProxyCreate) AddAccounts(v ...*Account) *ProxyCreate {
|
||||
return _c.AddAccountIDs(ids...)
|
||||
}
|
||||
|
||||
// AddPrimaryProxyIDs adds the "primary_proxies" edge to the Proxy entity by IDs.
|
||||
func (_c *ProxyCreate) AddPrimaryProxyIDs(ids ...int64) *ProxyCreate {
|
||||
_c.mutation.AddPrimaryProxyIDs(ids...)
|
||||
return _c
|
||||
}
|
||||
|
||||
// AddPrimaryProxies adds the "primary_proxies" edges to the Proxy entity.
|
||||
func (_c *ProxyCreate) AddPrimaryProxies(v ...*Proxy) *ProxyCreate {
|
||||
ids := make([]int64, len(v))
|
||||
for i := range v {
|
||||
ids[i] = v[i].ID
|
||||
}
|
||||
return _c.AddPrimaryProxyIDs(ids...)
|
||||
}
|
||||
|
||||
// SetBackupProxy sets the "backup_proxy" edge to the Proxy entity.
|
||||
func (_c *ProxyCreate) SetBackupProxy(v *Proxy) *ProxyCreate {
|
||||
return _c.SetBackupProxyID(v.ID)
|
||||
@@ -432,13 +447,29 @@ func (_c *ProxyCreate) createSpec() (*Proxy, *sqlgraph.CreateSpec) {
|
||||
}
|
||||
_spec.Edges = append(_spec.Edges, edge)
|
||||
}
|
||||
if nodes := _c.mutation.PrimaryProxiesIDs(); len(nodes) > 0 {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.O2M,
|
||||
Inverse: true,
|
||||
Table: proxy.PrimaryProxiesTable,
|
||||
Columns: []string{proxy.PrimaryProxiesColumn},
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64),
|
||||
},
|
||||
}
|
||||
for _, k := range nodes {
|
||||
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
||||
}
|
||||
_spec.Edges = append(_spec.Edges, edge)
|
||||
}
|
||||
if nodes := _c.mutation.BackupProxyIDs(); len(nodes) > 0 {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.O2O,
|
||||
Rel: sqlgraph.M2O,
|
||||
Inverse: false,
|
||||
Table: proxy.BackupProxyTable,
|
||||
Columns: []string{proxy.BackupProxyColumn},
|
||||
Bidi: true,
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64),
|
||||
},
|
||||
|
||||
+92
-16
@@ -21,13 +21,14 @@ import (
|
||||
// ProxyQuery is the builder for querying Proxy entities.
|
||||
type ProxyQuery struct {
|
||||
config
|
||||
ctx *QueryContext
|
||||
order []proxy.OrderOption
|
||||
inters []Interceptor
|
||||
predicates []predicate.Proxy
|
||||
withAccounts *AccountQuery
|
||||
withBackupProxy *ProxyQuery
|
||||
modifiers []func(*sql.Selector)
|
||||
ctx *QueryContext
|
||||
order []proxy.OrderOption
|
||||
inters []Interceptor
|
||||
predicates []predicate.Proxy
|
||||
withAccounts *AccountQuery
|
||||
withPrimaryProxies *ProxyQuery
|
||||
withBackupProxy *ProxyQuery
|
||||
modifiers []func(*sql.Selector)
|
||||
// intermediate query (i.e. traversal path).
|
||||
sql *sql.Selector
|
||||
path func(context.Context) (*sql.Selector, error)
|
||||
@@ -86,6 +87,28 @@ func (_q *ProxyQuery) QueryAccounts() *AccountQuery {
|
||||
return query
|
||||
}
|
||||
|
||||
// QueryPrimaryProxies chains the current query on the "primary_proxies" edge.
|
||||
func (_q *ProxyQuery) QueryPrimaryProxies() *ProxyQuery {
|
||||
query := (&ProxyClient{config: _q.config}).Query()
|
||||
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
||||
if err := _q.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
selector := _q.sqlQuery(ctx)
|
||||
if err := selector.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(proxy.Table, proxy.FieldID, selector),
|
||||
sqlgraph.To(proxy.Table, proxy.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, true, proxy.PrimaryProxiesTable, proxy.PrimaryProxiesColumn),
|
||||
)
|
||||
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
|
||||
return fromU, nil
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
// QueryBackupProxy chains the current query on the "backup_proxy" edge.
|
||||
func (_q *ProxyQuery) QueryBackupProxy() *ProxyQuery {
|
||||
query := (&ProxyClient{config: _q.config}).Query()
|
||||
@@ -100,7 +123,7 @@ func (_q *ProxyQuery) QueryBackupProxy() *ProxyQuery {
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(proxy.Table, proxy.FieldID, selector),
|
||||
sqlgraph.To(proxy.Table, proxy.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2O, false, proxy.BackupProxyTable, proxy.BackupProxyColumn),
|
||||
sqlgraph.Edge(sqlgraph.M2O, false, proxy.BackupProxyTable, proxy.BackupProxyColumn),
|
||||
)
|
||||
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
|
||||
return fromU, nil
|
||||
@@ -295,13 +318,14 @@ func (_q *ProxyQuery) Clone() *ProxyQuery {
|
||||
return nil
|
||||
}
|
||||
return &ProxyQuery{
|
||||
config: _q.config,
|
||||
ctx: _q.ctx.Clone(),
|
||||
order: append([]proxy.OrderOption{}, _q.order...),
|
||||
inters: append([]Interceptor{}, _q.inters...),
|
||||
predicates: append([]predicate.Proxy{}, _q.predicates...),
|
||||
withAccounts: _q.withAccounts.Clone(),
|
||||
withBackupProxy: _q.withBackupProxy.Clone(),
|
||||
config: _q.config,
|
||||
ctx: _q.ctx.Clone(),
|
||||
order: append([]proxy.OrderOption{}, _q.order...),
|
||||
inters: append([]Interceptor{}, _q.inters...),
|
||||
predicates: append([]predicate.Proxy{}, _q.predicates...),
|
||||
withAccounts: _q.withAccounts.Clone(),
|
||||
withPrimaryProxies: _q.withPrimaryProxies.Clone(),
|
||||
withBackupProxy: _q.withBackupProxy.Clone(),
|
||||
// clone intermediate query.
|
||||
sql: _q.sql.Clone(),
|
||||
path: _q.path,
|
||||
@@ -319,6 +343,17 @@ func (_q *ProxyQuery) WithAccounts(opts ...func(*AccountQuery)) *ProxyQuery {
|
||||
return _q
|
||||
}
|
||||
|
||||
// WithPrimaryProxies tells the query-builder to eager-load the nodes that are connected to
|
||||
// the "primary_proxies" edge. The optional arguments are used to configure the query builder of the edge.
|
||||
func (_q *ProxyQuery) WithPrimaryProxies(opts ...func(*ProxyQuery)) *ProxyQuery {
|
||||
query := (&ProxyClient{config: _q.config}).Query()
|
||||
for _, opt := range opts {
|
||||
opt(query)
|
||||
}
|
||||
_q.withPrimaryProxies = query
|
||||
return _q
|
||||
}
|
||||
|
||||
// WithBackupProxy tells the query-builder to eager-load the nodes that are connected to
|
||||
// the "backup_proxy" edge. The optional arguments are used to configure the query builder of the edge.
|
||||
func (_q *ProxyQuery) WithBackupProxy(opts ...func(*ProxyQuery)) *ProxyQuery {
|
||||
@@ -408,8 +443,9 @@ func (_q *ProxyQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Proxy,
|
||||
var (
|
||||
nodes = []*Proxy{}
|
||||
_spec = _q.querySpec()
|
||||
loadedTypes = [2]bool{
|
||||
loadedTypes = [3]bool{
|
||||
_q.withAccounts != nil,
|
||||
_q.withPrimaryProxies != nil,
|
||||
_q.withBackupProxy != nil,
|
||||
}
|
||||
)
|
||||
@@ -441,6 +477,13 @@ func (_q *ProxyQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Proxy,
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if query := _q.withPrimaryProxies; query != nil {
|
||||
if err := _q.loadPrimaryProxies(ctx, query, nodes,
|
||||
func(n *Proxy) { n.Edges.PrimaryProxies = []*Proxy{} },
|
||||
func(n *Proxy, e *Proxy) { n.Edges.PrimaryProxies = append(n.Edges.PrimaryProxies, e) }); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if query := _q.withBackupProxy; query != nil {
|
||||
if err := _q.loadBackupProxy(ctx, query, nodes, nil,
|
||||
func(n *Proxy, e *Proxy) { n.Edges.BackupProxy = e }); err != nil {
|
||||
@@ -483,6 +526,39 @@ func (_q *ProxyQuery) loadAccounts(ctx context.Context, query *AccountQuery, nod
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (_q *ProxyQuery) loadPrimaryProxies(ctx context.Context, query *ProxyQuery, nodes []*Proxy, init func(*Proxy), assign func(*Proxy, *Proxy)) error {
|
||||
fks := make([]driver.Value, 0, len(nodes))
|
||||
nodeids := make(map[int64]*Proxy)
|
||||
for i := range nodes {
|
||||
fks = append(fks, nodes[i].ID)
|
||||
nodeids[nodes[i].ID] = nodes[i]
|
||||
if init != nil {
|
||||
init(nodes[i])
|
||||
}
|
||||
}
|
||||
if len(query.ctx.Fields) > 0 {
|
||||
query.ctx.AppendFieldOnce(proxy.FieldBackupProxyID)
|
||||
}
|
||||
query.Where(predicate.Proxy(func(s *sql.Selector) {
|
||||
s.Where(sql.InValues(s.C(proxy.PrimaryProxiesColumn), fks...))
|
||||
}))
|
||||
neighbors, err := query.All(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, n := range neighbors {
|
||||
fk := n.BackupProxyID
|
||||
if fk == nil {
|
||||
return fmt.Errorf(`foreign-key "backup_proxy_id" is nil for node %v`, n.ID)
|
||||
}
|
||||
node, ok := nodeids[*fk]
|
||||
if !ok {
|
||||
return fmt.Errorf(`unexpected referenced foreign-key "backup_proxy_id" returned %v for node %v`, *fk, n.ID)
|
||||
}
|
||||
assign(node, n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (_q *ProxyQuery) loadBackupProxy(ctx context.Context, query *ProxyQuery, nodes []*Proxy, init func(*Proxy), assign func(*Proxy, *Proxy)) error {
|
||||
ids := make([]int64, 0, len(nodes))
|
||||
nodeids := make(map[int64][]*Proxy)
|
||||
|
||||
+170
-8
@@ -262,6 +262,21 @@ func (_u *ProxyUpdate) AddAccounts(v ...*Account) *ProxyUpdate {
|
||||
return _u.AddAccountIDs(ids...)
|
||||
}
|
||||
|
||||
// AddPrimaryProxyIDs adds the "primary_proxies" edge to the Proxy entity by IDs.
|
||||
func (_u *ProxyUpdate) AddPrimaryProxyIDs(ids ...int64) *ProxyUpdate {
|
||||
_u.mutation.AddPrimaryProxyIDs(ids...)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AddPrimaryProxies adds the "primary_proxies" edges to the Proxy entity.
|
||||
func (_u *ProxyUpdate) AddPrimaryProxies(v ...*Proxy) *ProxyUpdate {
|
||||
ids := make([]int64, len(v))
|
||||
for i := range v {
|
||||
ids[i] = v[i].ID
|
||||
}
|
||||
return _u.AddPrimaryProxyIDs(ids...)
|
||||
}
|
||||
|
||||
// SetBackupProxy sets the "backup_proxy" edge to the Proxy entity.
|
||||
func (_u *ProxyUpdate) SetBackupProxy(v *Proxy) *ProxyUpdate {
|
||||
return _u.SetBackupProxyID(v.ID)
|
||||
@@ -293,6 +308,27 @@ func (_u *ProxyUpdate) RemoveAccounts(v ...*Account) *ProxyUpdate {
|
||||
return _u.RemoveAccountIDs(ids...)
|
||||
}
|
||||
|
||||
// ClearPrimaryProxies clears all "primary_proxies" edges to the Proxy entity.
|
||||
func (_u *ProxyUpdate) ClearPrimaryProxies() *ProxyUpdate {
|
||||
_u.mutation.ClearPrimaryProxies()
|
||||
return _u
|
||||
}
|
||||
|
||||
// RemovePrimaryProxyIDs removes the "primary_proxies" edge to Proxy entities by IDs.
|
||||
func (_u *ProxyUpdate) RemovePrimaryProxyIDs(ids ...int64) *ProxyUpdate {
|
||||
_u.mutation.RemovePrimaryProxyIDs(ids...)
|
||||
return _u
|
||||
}
|
||||
|
||||
// RemovePrimaryProxies removes "primary_proxies" edges to Proxy entities.
|
||||
func (_u *ProxyUpdate) RemovePrimaryProxies(v ...*Proxy) *ProxyUpdate {
|
||||
ids := make([]int64, len(v))
|
||||
for i := range v {
|
||||
ids[i] = v[i].ID
|
||||
}
|
||||
return _u.RemovePrimaryProxyIDs(ids...)
|
||||
}
|
||||
|
||||
// ClearBackupProxy clears the "backup_proxy" edge to the Proxy entity.
|
||||
func (_u *ProxyUpdate) ClearBackupProxy() *ProxyUpdate {
|
||||
_u.mutation.ClearBackupProxy()
|
||||
@@ -492,13 +528,58 @@ func (_u *ProxyUpdate) sqlSave(ctx context.Context) (_node int, err error) {
|
||||
}
|
||||
_spec.Edges.Add = append(_spec.Edges.Add, edge)
|
||||
}
|
||||
if _u.mutation.PrimaryProxiesCleared() {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.O2M,
|
||||
Inverse: true,
|
||||
Table: proxy.PrimaryProxiesTable,
|
||||
Columns: []string{proxy.PrimaryProxiesColumn},
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64),
|
||||
},
|
||||
}
|
||||
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
||||
}
|
||||
if nodes := _u.mutation.RemovedPrimaryProxiesIDs(); len(nodes) > 0 && !_u.mutation.PrimaryProxiesCleared() {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.O2M,
|
||||
Inverse: true,
|
||||
Table: proxy.PrimaryProxiesTable,
|
||||
Columns: []string{proxy.PrimaryProxiesColumn},
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64),
|
||||
},
|
||||
}
|
||||
for _, k := range nodes {
|
||||
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
||||
}
|
||||
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
||||
}
|
||||
if nodes := _u.mutation.PrimaryProxiesIDs(); len(nodes) > 0 {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.O2M,
|
||||
Inverse: true,
|
||||
Table: proxy.PrimaryProxiesTable,
|
||||
Columns: []string{proxy.PrimaryProxiesColumn},
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64),
|
||||
},
|
||||
}
|
||||
for _, k := range nodes {
|
||||
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
||||
}
|
||||
_spec.Edges.Add = append(_spec.Edges.Add, edge)
|
||||
}
|
||||
if _u.mutation.BackupProxyCleared() {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.O2O,
|
||||
Rel: sqlgraph.M2O,
|
||||
Inverse: false,
|
||||
Table: proxy.BackupProxyTable,
|
||||
Columns: []string{proxy.BackupProxyColumn},
|
||||
Bidi: true,
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64),
|
||||
},
|
||||
@@ -507,11 +588,11 @@ func (_u *ProxyUpdate) sqlSave(ctx context.Context) (_node int, err error) {
|
||||
}
|
||||
if nodes := _u.mutation.BackupProxyIDs(); len(nodes) > 0 {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.O2O,
|
||||
Rel: sqlgraph.M2O,
|
||||
Inverse: false,
|
||||
Table: proxy.BackupProxyTable,
|
||||
Columns: []string{proxy.BackupProxyColumn},
|
||||
Bidi: true,
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64),
|
||||
},
|
||||
@@ -774,6 +855,21 @@ func (_u *ProxyUpdateOne) AddAccounts(v ...*Account) *ProxyUpdateOne {
|
||||
return _u.AddAccountIDs(ids...)
|
||||
}
|
||||
|
||||
// AddPrimaryProxyIDs adds the "primary_proxies" edge to the Proxy entity by IDs.
|
||||
func (_u *ProxyUpdateOne) AddPrimaryProxyIDs(ids ...int64) *ProxyUpdateOne {
|
||||
_u.mutation.AddPrimaryProxyIDs(ids...)
|
||||
return _u
|
||||
}
|
||||
|
||||
// AddPrimaryProxies adds the "primary_proxies" edges to the Proxy entity.
|
||||
func (_u *ProxyUpdateOne) AddPrimaryProxies(v ...*Proxy) *ProxyUpdateOne {
|
||||
ids := make([]int64, len(v))
|
||||
for i := range v {
|
||||
ids[i] = v[i].ID
|
||||
}
|
||||
return _u.AddPrimaryProxyIDs(ids...)
|
||||
}
|
||||
|
||||
// SetBackupProxy sets the "backup_proxy" edge to the Proxy entity.
|
||||
func (_u *ProxyUpdateOne) SetBackupProxy(v *Proxy) *ProxyUpdateOne {
|
||||
return _u.SetBackupProxyID(v.ID)
|
||||
@@ -805,6 +901,27 @@ func (_u *ProxyUpdateOne) RemoveAccounts(v ...*Account) *ProxyUpdateOne {
|
||||
return _u.RemoveAccountIDs(ids...)
|
||||
}
|
||||
|
||||
// ClearPrimaryProxies clears all "primary_proxies" edges to the Proxy entity.
|
||||
func (_u *ProxyUpdateOne) ClearPrimaryProxies() *ProxyUpdateOne {
|
||||
_u.mutation.ClearPrimaryProxies()
|
||||
return _u
|
||||
}
|
||||
|
||||
// RemovePrimaryProxyIDs removes the "primary_proxies" edge to Proxy entities by IDs.
|
||||
func (_u *ProxyUpdateOne) RemovePrimaryProxyIDs(ids ...int64) *ProxyUpdateOne {
|
||||
_u.mutation.RemovePrimaryProxyIDs(ids...)
|
||||
return _u
|
||||
}
|
||||
|
||||
// RemovePrimaryProxies removes "primary_proxies" edges to Proxy entities.
|
||||
func (_u *ProxyUpdateOne) RemovePrimaryProxies(v ...*Proxy) *ProxyUpdateOne {
|
||||
ids := make([]int64, len(v))
|
||||
for i := range v {
|
||||
ids[i] = v[i].ID
|
||||
}
|
||||
return _u.RemovePrimaryProxyIDs(ids...)
|
||||
}
|
||||
|
||||
// ClearBackupProxy clears the "backup_proxy" edge to the Proxy entity.
|
||||
func (_u *ProxyUpdateOne) ClearBackupProxy() *ProxyUpdateOne {
|
||||
_u.mutation.ClearBackupProxy()
|
||||
@@ -1034,13 +1151,58 @@ func (_u *ProxyUpdateOne) sqlSave(ctx context.Context) (_node *Proxy, err error)
|
||||
}
|
||||
_spec.Edges.Add = append(_spec.Edges.Add, edge)
|
||||
}
|
||||
if _u.mutation.PrimaryProxiesCleared() {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.O2M,
|
||||
Inverse: true,
|
||||
Table: proxy.PrimaryProxiesTable,
|
||||
Columns: []string{proxy.PrimaryProxiesColumn},
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64),
|
||||
},
|
||||
}
|
||||
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
||||
}
|
||||
if nodes := _u.mutation.RemovedPrimaryProxiesIDs(); len(nodes) > 0 && !_u.mutation.PrimaryProxiesCleared() {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.O2M,
|
||||
Inverse: true,
|
||||
Table: proxy.PrimaryProxiesTable,
|
||||
Columns: []string{proxy.PrimaryProxiesColumn},
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64),
|
||||
},
|
||||
}
|
||||
for _, k := range nodes {
|
||||
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
||||
}
|
||||
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
||||
}
|
||||
if nodes := _u.mutation.PrimaryProxiesIDs(); len(nodes) > 0 {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.O2M,
|
||||
Inverse: true,
|
||||
Table: proxy.PrimaryProxiesTable,
|
||||
Columns: []string{proxy.PrimaryProxiesColumn},
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64),
|
||||
},
|
||||
}
|
||||
for _, k := range nodes {
|
||||
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
||||
}
|
||||
_spec.Edges.Add = append(_spec.Edges.Add, edge)
|
||||
}
|
||||
if _u.mutation.BackupProxyCleared() {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.O2O,
|
||||
Rel: sqlgraph.M2O,
|
||||
Inverse: false,
|
||||
Table: proxy.BackupProxyTable,
|
||||
Columns: []string{proxy.BackupProxyColumn},
|
||||
Bidi: true,
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64),
|
||||
},
|
||||
@@ -1049,11 +1211,11 @@ func (_u *ProxyUpdateOne) sqlSave(ctx context.Context) (_node *Proxy, err error)
|
||||
}
|
||||
if nodes := _u.mutation.BackupProxyIDs(); len(nodes) > 0 {
|
||||
edge := &sqlgraph.EdgeSpec{
|
||||
Rel: sqlgraph.O2O,
|
||||
Rel: sqlgraph.M2O,
|
||||
Inverse: false,
|
||||
Table: proxy.BackupProxyTable,
|
||||
Columns: []string{proxy.BackupProxyColumn},
|
||||
Bidi: true,
|
||||
Bidi: false,
|
||||
Target: &sqlgraph.EdgeTarget{
|
||||
IDSpec: sqlgraph.NewFieldSpec(proxy.FieldID, field.TypeInt64),
|
||||
},
|
||||
|
||||
@@ -36,7 +36,7 @@ func (ChannelMonitor) Fields() []ent.Field {
|
||||
MaxLen(100),
|
||||
field.Enum("provider").
|
||||
Values("openai", "anthropic", "gemini", "grok",
|
||||
"antigravity", "kimi", "zhipu", "deepseek"),
|
||||
"antigravity", "kimi", "zhipu", "deepseek", "minimax", "opencode_go"),
|
||||
// check_mode: 'probe' | 'quota' | 'quota_probe'
|
||||
// probe - LLM 探活(默认,原有行为)
|
||||
// quota - 仅查关联账号的用量/余额(零 LLM 成本;endpoint/api_key 可空)
|
||||
|
||||
@@ -40,7 +40,7 @@ func (ChannelMonitorRequestTemplate) Fields() []ent.Field {
|
||||
MaxLen(100),
|
||||
field.Enum("provider").
|
||||
Values("openai", "anthropic", "gemini", "grok",
|
||||
"antigravity", "kimi", "zhipu", "deepseek"),
|
||||
"antigravity", "kimi", "zhipu", "deepseek", "minimax", "opencode_go"),
|
||||
field.String("api_mode").
|
||||
Default("chat_completions").
|
||||
MaxLen(32).
|
||||
|
||||
@@ -73,6 +73,10 @@ func (Proxy) Edges() []ent.Edge {
|
||||
// accounts: 使用此代理的账户(反向边)
|
||||
edge.From("accounts", Account.Type).
|
||||
Ref("proxy"),
|
||||
// Directed many-to-one: a backup can serve multiple primary proxies.
|
||||
// The inverse edge prevents Ent from treating this self-reference as symmetric.
|
||||
edge.From("primary_proxies", Proxy.Type).
|
||||
Ref("backup_proxy"),
|
||||
edge.To("backup_proxy", Proxy.Type).
|
||||
Field("backup_proxy_id").
|
||||
Unique(),
|
||||
|
||||
@@ -42,7 +42,7 @@ func (UserPlatformQuota) Fields() []ent.Field {
|
||||
// 此处为 ent 构建期约束,需与 service.AllowedQuotaPlatforms 保持同步。
|
||||
switch s {
|
||||
case "anthropic", "openai", "gemini", "antigravity", "grok",
|
||||
"kimi", "zhipu", "deepseek":
|
||||
"kimi", "zhipu", "deepseek", "minimax", "opencode_go":
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("platform %q is not allowed", s)
|
||||
|
||||
+3
-4
@@ -31,7 +31,7 @@ require (
|
||||
github.com/lib/pq v1.10.9
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||
github.com/pquerna/otp v1.5.0
|
||||
github.com/redis/go-redis/v9 v9.17.2
|
||||
github.com/redis/go-redis/v9 v9.22.0
|
||||
github.com/refraction-networking/utls v1.8.2
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/shirou/gopsutil/v4 v4.25.6
|
||||
@@ -101,7 +101,6 @@ require (
|
||||
github.com/containerd/platforms v0.2.1 // indirect
|
||||
github.com/cpuguy83/dockercfg v0.3.2 // indirect
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/distribution/reference v0.6.0 // indirect
|
||||
github.com/dlclark/regexp2/v2 v2.1.0 // indirect
|
||||
github.com/docker/docker v28.5.1+incompatible // indirect
|
||||
@@ -135,7 +134,7 @@ require (
|
||||
github.com/hashicorp/yamux v0.1.2 // indirect
|
||||
github.com/icholy/digest v1.1.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
|
||||
github.com/leodido/go-urn v1.2.4 // indirect
|
||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
|
||||
github.com/magiconair/properties v1.8.10 // indirect
|
||||
@@ -197,7 +196,7 @@ require (
|
||||
go.opentelemetry.io/otel v1.43.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.43.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.43.0 // indirect
|
||||
go.uber.org/atomic v1.10.0 // indirect
|
||||
go.uber.org/atomic v1.11.0 // indirect
|
||||
go.uber.org/automaxprocs v1.6.0 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
golang.org/x/arch v0.3.0 // indirect
|
||||
|
||||
+16
-4
@@ -159,8 +159,6 @@ github.com/dgraph-io/ristretto v0.2.0 h1:XAfl+7cmoUDWW/2Lx8TGZQjjxIQ2Ley9DSf52dr
|
||||
github.com/dgraph-io/ristretto v0.2.0/go.mod h1:8uBHCU/PBV4Ag0CJrP47b9Ofby5dqWNh4FicAdoqFNU=
|
||||
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WAFKLNi6ZS0675eEUC9y3AlwSbQu1Y=
|
||||
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
|
||||
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
|
||||
github.com/dlclark/regexp2/v2 v2.1.0 h1:jHXRmHRZGbuQzDZjMlCAXOvQb75iv3HyLDzXGj5H1AY=
|
||||
@@ -255,6 +253,8 @@ github.com/google/go-tpm-tools v0.3.13-0.20230620182252-4639ecce2aba/go.mod h1:E
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
||||
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
|
||||
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/google/wire v0.7.0 h1:JxUKI6+CVBgCO2WToKy/nQk0sS+amI9z9EjVmdaocj4=
|
||||
@@ -302,6 +302,8 @@ github.com/klauspost/compress v1.18.2/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxh
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
|
||||
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
@@ -327,6 +329,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
|
||||
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI=
|
||||
@@ -365,6 +369,8 @@ github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJm
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
|
||||
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
|
||||
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
||||
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
|
||||
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
||||
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
||||
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
|
||||
@@ -393,12 +399,14 @@ github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
|
||||
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
|
||||
github.com/quic-go/quic-go v0.60.0 h1:xcQioE8OM66UQLeUMHltK1CCcOu3JbVB4JAQdDQSB+0=
|
||||
github.com/quic-go/quic-go v0.60.0/go.mod h1:wpKpjmPpftl30sL6pFh7REVpjbcCVy4zt2vDyK1TuJk=
|
||||
github.com/redis/go-redis/v9 v9.17.2 h1:P2EGsA4qVIM3Pp+aPocCJ7DguDHhqrXNhVcEp4ViluI=
|
||||
github.com/redis/go-redis/v9 v9.17.2/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
|
||||
github.com/redis/go-redis/v9 v9.22.0 h1:laDvpYXTJtZLloinw1fA5Kqd6HAEH2XKxOkG/PDq2F0=
|
||||
github.com/redis/go-redis/v9 v9.22.0/go.mod h1:y2g0Wj8rQvuK0ELM+oxSudcLtC09JScs98I/X9gRWY4=
|
||||
github.com/refraction-networking/utls v1.8.2 h1:j4Q1gJj0xngdeH+Ox/qND11aEfhpgoEvV+S9iJ2IdQo=
|
||||
github.com/refraction-networking/utls v1.8.2/go.mod h1:jkSOEkLqn+S/jtpEHPOsVv/4V4EVnelwbMQl4vCWXAM=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||
@@ -434,6 +442,8 @@ github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
|
||||
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
|
||||
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
|
||||
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
|
||||
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
|
||||
@@ -535,6 +545,8 @@ go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeX
|
||||
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
|
||||
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
|
||||
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
|
||||
go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
|
||||
@@ -1274,11 +1274,15 @@ type GatewayOpenAIWSConfig struct {
|
||||
MaxConnsPerAccount int `mapstructure:"max_conns_per_account"`
|
||||
MinIdlePerAccount int `mapstructure:"min_idle_per_account"`
|
||||
MaxIdlePerAccount int `mapstructure:"max_idle_per_account"`
|
||||
// DynamicMaxConnsByAccountConcurrencyEnabled: 是否按账号并发动态计算连接池上限
|
||||
// DynamicMaxConnsByAccountConcurrencyEnabled: 是否按账号并发动态计算连接池上限。
|
||||
// 旧版及 mode_router_v2 的 ctx_pool 共用此开关和类型系数;关闭后使用 max_conns_per_account。
|
||||
// mode_router_v2 下并发数 <= 0 的账号仍不可调度。
|
||||
DynamicMaxConnsByAccountConcurrencyEnabled bool `mapstructure:"dynamic_max_conns_by_account_concurrency_enabled"`
|
||||
// OAuthMaxConnsFactor: OAuth 账号连接池系数(effective=ceil(concurrency*factor))
|
||||
// OAuthMaxConnsFactor: OAuth 账号连接池系数(effective=ceil(concurrency*factor),再受 max_conns_per_account 封顶)。
|
||||
// ctx_pool 接入下每个客户端会话在整个生命周期(含轮次之间)持有一条上游连接,此上限限制的是同时持有连接的会话数,
|
||||
// 在飞请求数另由账号并发槽限制;系数 1.0 会让存活会话数一到并发数就返回 1013 busy,默认 5.0。
|
||||
OAuthMaxConnsFactor float64 `mapstructure:"oauth_max_conns_factor"`
|
||||
// APIKeyMaxConnsFactor: API Key 账号连接池系数(effective=ceil(concurrency*factor))
|
||||
// APIKeyMaxConnsFactor: API Key 账号连接池系数,含义与 OAuthMaxConnsFactor 相同,默认 5.0。
|
||||
APIKeyMaxConnsFactor float64 `mapstructure:"apikey_max_conns_factor"`
|
||||
DialTimeoutSeconds int `mapstructure:"dial_timeout_seconds"`
|
||||
ReadTimeoutSeconds int `mapstructure:"read_timeout_seconds"`
|
||||
@@ -1613,10 +1617,10 @@ type OpsCleanupConfig struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
Schedule string `mapstructure:"schedule"`
|
||||
|
||||
// Retention days (0 disables that cleanup target).
|
||||
//
|
||||
// vNext requirement: default 30 days across ops datasets.
|
||||
// Retention days. Error and metrics targets accept 0 as an explicit truncate;
|
||||
// system logs require a positive value because their runtime setting is bounded.
|
||||
ErrorLogRetentionDays int `mapstructure:"error_log_retention_days"`
|
||||
SystemLogRetentionDays int `mapstructure:"system_log_retention_days"`
|
||||
MinuteMetricsRetentionDays int `mapstructure:"minute_metrics_retention_days"`
|
||||
HourlyMetricsRetentionDays int `mapstructure:"hourly_metrics_retention_days"`
|
||||
}
|
||||
@@ -2046,7 +2050,9 @@ func setDefaults() {
|
||||
"api.moonshot.ai",
|
||||
"api.moonshot.cn",
|
||||
"open.bigmodel.cn",
|
||||
"api.minimaxi.com",
|
||||
"api.minimaxi.com", // MiniMax CN quota + inference
|
||||
"api.minimax.io", // MiniMax intl; frozen allowlists must add this host to use the intl site
|
||||
"opencode.ai",
|
||||
"generativelanguage.googleapis.com",
|
||||
"cloudcode-pa.googleapis.com",
|
||||
"*.openai.azure.com",
|
||||
@@ -2250,6 +2256,7 @@ func setDefaults() {
|
||||
viper.SetDefault("ops.cleanup.schedule", "0 2 * * *")
|
||||
// Retention days: vNext defaults to 30 days across ops datasets.
|
||||
viper.SetDefault("ops.cleanup.error_log_retention_days", 30)
|
||||
viper.SetDefault("ops.cleanup.system_log_retention_days", 30)
|
||||
viper.SetDefault("ops.cleanup.minute_metrics_retention_days", 30)
|
||||
viper.SetDefault("ops.cleanup.hourly_metrics_retention_days", 30)
|
||||
viper.SetDefault("ops.aggregation.enabled", true)
|
||||
@@ -2373,7 +2380,7 @@ func setDefaults() {
|
||||
viper.SetDefault("gateway.disable_codex_originator_normalization", false)
|
||||
viper.SetDefault("gateway.codex_image_generation_bridge_enabled", false)
|
||||
viper.SetDefault("gateway.openai_passthrough_allow_timeout_headers", false)
|
||||
viper.SetDefault("gateway.openai_compact_model", "gpt-5.4")
|
||||
viper.SetDefault("gateway.openai_compact_model", "gpt-5.5")
|
||||
viper.SetDefault("gateway.live.max_session_duration_seconds", 3600)
|
||||
// OpenAI Responses WebSocket(默认开启;可通过 force_http 紧急回滚)
|
||||
viper.SetDefault("gateway.openai_ws.enabled", true)
|
||||
@@ -2399,8 +2406,8 @@ func setDefaults() {
|
||||
viper.SetDefault("gateway.openai_ws.min_idle_per_account", 4)
|
||||
viper.SetDefault("gateway.openai_ws.max_idle_per_account", 12)
|
||||
viper.SetDefault("gateway.openai_ws.dynamic_max_conns_by_account_concurrency_enabled", true)
|
||||
viper.SetDefault("gateway.openai_ws.oauth_max_conns_factor", 1.0)
|
||||
viper.SetDefault("gateway.openai_ws.apikey_max_conns_factor", 1.0)
|
||||
viper.SetDefault("gateway.openai_ws.oauth_max_conns_factor", 5.0)
|
||||
viper.SetDefault("gateway.openai_ws.apikey_max_conns_factor", 5.0)
|
||||
viper.SetDefault("gateway.openai_ws.dial_timeout_seconds", 10)
|
||||
viper.SetDefault("gateway.openai_ws.read_timeout_seconds", 900)
|
||||
viper.SetDefault("gateway.openai_ws.write_timeout_seconds", 120)
|
||||
@@ -3673,6 +3680,9 @@ func (c *Config) Validate() error {
|
||||
if c.Ops.Cleanup.ErrorLogRetentionDays < 0 {
|
||||
return fmt.Errorf("ops.cleanup.error_log_retention_days must be non-negative")
|
||||
}
|
||||
if c.Ops.Cleanup.Enabled && c.Ops.Cleanup.SystemLogRetentionDays <= 0 {
|
||||
return fmt.Errorf("ops.cleanup.system_log_retention_days must be positive when ops cleanup is enabled")
|
||||
}
|
||||
if c.Ops.Cleanup.MinuteMetricsRetentionDays < 0 {
|
||||
return fmt.Errorf("ops.cleanup.minute_metrics_retention_days must be non-negative")
|
||||
}
|
||||
|
||||
@@ -452,11 +452,11 @@ func TestLoadDefaultOpenAIWSConfig(t *testing.T) {
|
||||
if !cfg.Gateway.OpenAIWS.DynamicMaxConnsByAccountConcurrencyEnabled {
|
||||
t.Fatalf("Gateway.OpenAIWS.DynamicMaxConnsByAccountConcurrencyEnabled = false, want true")
|
||||
}
|
||||
if cfg.Gateway.OpenAIWS.OAuthMaxConnsFactor != 1.0 {
|
||||
t.Fatalf("Gateway.OpenAIWS.OAuthMaxConnsFactor = %v, want 1.0", cfg.Gateway.OpenAIWS.OAuthMaxConnsFactor)
|
||||
if cfg.Gateway.OpenAIWS.OAuthMaxConnsFactor != 5.0 {
|
||||
t.Fatalf("Gateway.OpenAIWS.OAuthMaxConnsFactor = %v, want 5.0", cfg.Gateway.OpenAIWS.OAuthMaxConnsFactor)
|
||||
}
|
||||
if cfg.Gateway.OpenAIWS.APIKeyMaxConnsFactor != 1.0 {
|
||||
t.Fatalf("Gateway.OpenAIWS.APIKeyMaxConnsFactor = %v, want 1.0", cfg.Gateway.OpenAIWS.APIKeyMaxConnsFactor)
|
||||
if cfg.Gateway.OpenAIWS.APIKeyMaxConnsFactor != 5.0 {
|
||||
t.Fatalf("Gateway.OpenAIWS.APIKeyMaxConnsFactor = %v, want 5.0", cfg.Gateway.OpenAIWS.APIKeyMaxConnsFactor)
|
||||
}
|
||||
if cfg.Gateway.OpenAIWS.StickySessionTTLSeconds != 3600 {
|
||||
t.Fatalf("Gateway.OpenAIWS.StickySessionTTLSeconds = %d, want 3600", cfg.Gateway.OpenAIWS.StickySessionTTLSeconds)
|
||||
@@ -574,7 +574,7 @@ func TestLoadDefaultOpenAICompactModel(t *testing.T) {
|
||||
|
||||
cfg, err := Load()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "gpt-5.4", cfg.Gateway.OpenAICompactModel)
|
||||
require.Equal(t, "gpt-5.5", cfg.Gateway.OpenAICompactModel)
|
||||
}
|
||||
|
||||
func TestLoadOpenAICompactModelFromEnv(t *testing.T) {
|
||||
@@ -1191,6 +1191,21 @@ func TestLoadDefaultUsageCleanupConfig(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDefaultOpsCleanupConfig(t *testing.T) {
|
||||
resetViperWithJWTSecret(t)
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load() error: %v", err)
|
||||
}
|
||||
if !cfg.Ops.Cleanup.Enabled {
|
||||
t.Fatal("Ops.Cleanup.Enabled = false, want true")
|
||||
}
|
||||
if cfg.Ops.Cleanup.SystemLogRetentionDays != 30 {
|
||||
t.Fatalf("Ops.Cleanup.SystemLogRetentionDays = %d, want 30", cfg.Ops.Cleanup.SystemLogRetentionDays)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateUsageCleanupConfigEnabled(t *testing.T) {
|
||||
resetViperWithJWTSecret(t)
|
||||
|
||||
@@ -2137,6 +2152,11 @@ func TestValidateConfigErrors(t *testing.T) {
|
||||
mutate: func(c *Config) { c.Ops.Cleanup.ErrorLogRetentionDays = -1 },
|
||||
wantErr: "ops.cleanup.error_log_retention_days",
|
||||
},
|
||||
{
|
||||
name: "ops cleanup system log retention",
|
||||
mutate: func(c *Config) { c.Ops.Cleanup.SystemLogRetentionDays = 0 },
|
||||
wantErr: "ops.cleanup.system_log_retention_days",
|
||||
},
|
||||
{
|
||||
name: "ops cleanup minute retention",
|
||||
mutate: func(c *Config) { c.Ops.Cleanup.MinuteMetricsRetentionDays = -1 },
|
||||
|
||||
@@ -19,7 +19,8 @@ import "time"
|
||||
// - "7d" 7 天窗口(Claude/Codex)
|
||||
// - "7d-sonnet" Claude 7 天 Sonnet 独立额度
|
||||
// - "7d-fable" Claude 7 天 Fable 独立额度
|
||||
// - "weekly" 周窗口(Kimi/Zhipu coding plan)
|
||||
// - "weekly" 周窗口(Kimi/Zhipu coding plan / OpenCode Go)
|
||||
// - "monthly" 月窗口(OpenCode Go)
|
||||
// - "daily" 日窗口(Gemini 日配额 / Grok 日请求)
|
||||
// - "30d" 30 天窗口(Grok 月度)
|
||||
// - "total" 无窗口语义的总量额度(Antigravity per-model 等)
|
||||
|
||||
@@ -24,10 +24,14 @@ const (
|
||||
PlatformAntigravity = "antigravity"
|
||||
PlatformGrok = "grok"
|
||||
// 国产 OpenAI 兼容供应商(经 OpenAI 网关转发,按 Chat Completions 协议)。
|
||||
PlatformKimi = "kimi" // Kimi (月之暗面 / Moonshot)
|
||||
PlatformZhipu = "zhipu" // 智谱 GLM (bigmodel)
|
||||
PlatformDeepseek = "deepseek" // DeepSeek
|
||||
PlatformComposite = "composite"
|
||||
PlatformKimi = "kimi" // Kimi (月之暗面 / Moonshot)
|
||||
PlatformZhipu = "zhipu" // 智谱 GLM (bigmodel)
|
||||
PlatformDeepseek = "deepseek" // DeepSeek
|
||||
PlatformMiniMax = "minimax" // MiniMax (M 系列)
|
||||
// PlatformOpenCodeGo 是 OpenCode 平台(账号类型 Zen 按量 / Go 订阅)。
|
||||
// 值保持 opencode_go 以兼容已落库的分组、配额与 Composite 路由 CHECK。
|
||||
PlatformOpenCodeGo = "opencode_go"
|
||||
PlatformComposite = "composite"
|
||||
)
|
||||
|
||||
// Account mode constants 区分国产供应商的「按量付费(余额)」与「Coding Plan」两种接入方式。
|
||||
@@ -35,6 +39,8 @@ const (
|
||||
const (
|
||||
AccountModePayG = "payg" // 按量付费:消耗余额,做余额检测冷却
|
||||
AccountModeCoding = "coding" // Coding Plan:滚动用量窗口冷却(5h / weekly)
|
||||
AccountModeZen = "zen" // OpenCode Zen:按量付费,https://opencode.ai/zen/v1
|
||||
AccountModeGo = "go" // OpenCode Go:订阅额度窗口,https://opencode.ai/zen/go/v1
|
||||
)
|
||||
|
||||
// API protocol constants 国产供应商的上游 API 协议维度。存储于
|
||||
@@ -43,7 +49,7 @@ const (
|
||||
const (
|
||||
APIProtocolChatCompletions = "chat_completions" // OpenAI Chat Completions(默认)
|
||||
APIProtocolAnthropic = "anthropic" // 原生 Anthropic /v1/messages(适配 Claude Code)
|
||||
APIProtocolResponses = "responses" // OpenAI Responses(deepseek / kimi 原生端点,适配 Codex)
|
||||
APIProtocolResponses = "responses" // OpenAI Responses(deepseek / kimi / minimax 原生端点,适配 Codex)
|
||||
APIProtocolAdaptive = "adaptive" // 按入站协议优先选择供应商原生端点
|
||||
)
|
||||
|
||||
@@ -145,6 +151,18 @@ var DefaultAntigravityModelMapping = map[string]string{
|
||||
"gemini-3.6-flash-low": "gemini-3.6-flash-low",
|
||||
"gemini-3.6-flash-medium": "gemini-3.6-flash-medium",
|
||||
"gemini-3.6-flash-tiered": "gemini-3.6-flash-tiered",
|
||||
// Gemini 3.7 Flash tiered models
|
||||
"gemini-3.7-flash": "gemini-3.7-flash",
|
||||
"gemini-3.7-flash-high": "gemini-3.7-flash-high",
|
||||
"gemini-3.7-flash-low": "gemini-3.7-flash-low",
|
||||
"gemini-3.7-flash-medium": "gemini-3.7-flash-medium",
|
||||
"gemini-3.7-flash-tiered": "gemini-3.7-flash-tiered",
|
||||
// Gemini 3.8 Flash tiered models
|
||||
"gemini-3.8-flash": "gemini-3.8-flash",
|
||||
"gemini-3.8-flash-high": "gemini-3.8-flash-high",
|
||||
"gemini-3.8-flash-low": "gemini-3.8-flash-low",
|
||||
"gemini-3.8-flash-medium": "gemini-3.8-flash-medium",
|
||||
"gemini-3.8-flash-tiered": "gemini-3.8-flash-tiered",
|
||||
// Gemini 3 image 兼容映射(向 3.1 image 迁移)
|
||||
"gemini-3-pro-image": "gemini-3.1-flash-image",
|
||||
"gemini-3-pro-image-preview": "gemini-3.1-flash-image",
|
||||
|
||||
@@ -89,6 +89,22 @@ func TestDefaultAntigravityModelMapping_Gemini36FlashModels(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultAntigravityModelMapping_Gemini37FlashModels(t *testing.T) {
|
||||
for _, model := range []string{"gemini-3.7-flash", "gemini-3.7-flash-high", "gemini-3.7-flash-low", "gemini-3.7-flash-medium", "gemini-3.7-flash-tiered"} {
|
||||
if got := DefaultAntigravityModelMapping[model]; got != model {
|
||||
t.Fatalf("expected %s to map to itself, got %q", model, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultAntigravityModelMapping_Gemini38FlashModels(t *testing.T) {
|
||||
for _, model := range []string{"gemini-3.8-flash", "gemini-3.8-flash-high", "gemini-3.8-flash-low", "gemini-3.8-flash-medium", "gemini-3.8-flash-tiered"} {
|
||||
if got := DefaultAntigravityModelMapping[model]; got != model {
|
||||
t.Fatalf("expected %s to map to itself, got %q", model, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultBedrockModelMapping_ContainsNewClaudeModels(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
@@ -309,15 +309,17 @@ func (h *AccountHandler) importData(ctx context.Context, req DataImportRequest)
|
||||
_, _ = h.adminService.UpdateProxy(ctx, existingID, &service.UpdateProxyInput{
|
||||
Status: normalizedStatus,
|
||||
ExpiresAt: existingExpiresAt,
|
||||
ClearExpiresAt: existingExpiresAt == nil,
|
||||
FallbackMode: existingFallbackMode,
|
||||
BackupProxyID: existingBackupProxyID,
|
||||
ExpiryWarnDays: item.ExpiryWarnDays,
|
||||
ClearBackupID: existingBackupProxyID == nil,
|
||||
ExpiryWarnDays: &item.ExpiryWarnDays,
|
||||
Name: proxy.Name,
|
||||
Protocol: proxy.Protocol,
|
||||
Host: proxy.Host,
|
||||
Port: proxy.Port,
|
||||
Username: proxy.Username,
|
||||
Password: proxy.Password,
|
||||
Username: &proxy.Username,
|
||||
Password: &proxy.Password,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -383,15 +385,17 @@ func (h *AccountHandler) importData(ctx context.Context, req DataImportRequest)
|
||||
_, _ = h.adminService.UpdateProxy(ctx, created.ID, &service.UpdateProxyInput{
|
||||
Status: normalizedStatus,
|
||||
ExpiresAt: expiresAt,
|
||||
ClearExpiresAt: expiresAt == nil,
|
||||
FallbackMode: fallbackMode,
|
||||
BackupProxyID: backupProxyID,
|
||||
ExpiryWarnDays: item.ExpiryWarnDays,
|
||||
ClearBackupID: backupProxyID == nil,
|
||||
ExpiryWarnDays: &item.ExpiryWarnDays,
|
||||
Name: created.Name,
|
||||
Protocol: created.Protocol,
|
||||
Host: created.Host,
|
||||
Port: created.Port,
|
||||
Username: created.Username,
|
||||
Password: created.Password,
|
||||
Username: &created.Username,
|
||||
Password: &created.Password,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1545,6 +1545,7 @@ func (h *AccountHandler) Refresh(c *gin.Context) {
|
||||
|
||||
if warning == "missing_project_id_temporary" {
|
||||
response.Success(c, gin.H{
|
||||
"account": h.buildAccountResponseWithRuntime(c.Request.Context(), updatedAccount),
|
||||
"message": "Token refreshed successfully, but project_id could not be retrieved (will retry automatically)",
|
||||
"warning": "missing_project_id_temporary",
|
||||
})
|
||||
|
||||
@@ -643,6 +643,8 @@ var platformToLiteLLMProvider = map[string]string{
|
||||
service.PlatformKimi: "moonshot",
|
||||
service.PlatformZhipu: "zhipu",
|
||||
service.PlatformDeepseek: "deepseek",
|
||||
service.PlatformMiniMax: "minimax",
|
||||
service.PlatformOpenCodeGo: "opencode-go",
|
||||
}
|
||||
|
||||
// SyncPricingModels 返回 LiteLLM 定价目录中指定平台的最新模型列表
|
||||
|
||||
@@ -549,7 +549,7 @@ func TestSyncPricingModels_ValidPlatform_EmptyService(t *testing.T) {
|
||||
svc := service.NewPricingService(nil, nil)
|
||||
router := setupSyncPricingModelsRouter(svc)
|
||||
|
||||
for _, platform := range []string{"anthropic", "openai", "gemini", "antigravity", "grok", "kimi", "zhipu", "deepseek"} {
|
||||
for _, platform := range []string{"anthropic", "openai", "gemini", "antigravity", "grok", "kimi", "zhipu", "deepseek", "minimax"} {
|
||||
req := httptest.NewRequest(http.MethodGet, "/channels/pricing/sync-models?platform="+platform, nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
@@ -40,7 +40,7 @@ func NewChannelMonitorHandler(monitorService *service.ChannelMonitorService) *Ch
|
||||
|
||||
type channelMonitorCreateRequest struct {
|
||||
Name string `json:"name" binding:"required,max=100"`
|
||||
Provider string `json:"provider" binding:"required,oneof=openai anthropic gemini grok antigravity kimi zhipu deepseek"`
|
||||
Provider string `json:"provider" binding:"required,oneof=openai anthropic gemini grok antigravity kimi zhipu deepseek minimax opencode_go"`
|
||||
APIMode string `json:"api_mode" binding:"omitempty,oneof=chat_completions responses"`
|
||||
Endpoint string `json:"endpoint" binding:"omitempty,max=500"`
|
||||
APIKey string `json:"api_key" binding:"omitempty,max=2000"`
|
||||
@@ -64,7 +64,7 @@ type channelMonitorCreateRequest struct {
|
||||
|
||||
type channelMonitorUpdateRequest struct {
|
||||
Name *string `json:"name" binding:"omitempty,max=100"`
|
||||
Provider *string `json:"provider" binding:"omitempty,oneof=openai anthropic gemini grok antigravity kimi zhipu deepseek"`
|
||||
Provider *string `json:"provider" binding:"omitempty,oneof=openai anthropic gemini grok antigravity kimi zhipu deepseek minimax opencode_go"`
|
||||
APIMode *string `json:"api_mode" binding:"omitempty,oneof=chat_completions responses"`
|
||||
Endpoint *string `json:"endpoint" binding:"omitempty,max=500"`
|
||||
APIKey *string `json:"api_key" binding:"omitempty,max=2000"`
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors"
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
grokMediaEligibilityModeAuto = "auto"
|
||||
grokMediaEligibilityModeEnabled = "enabled"
|
||||
grokMediaEligibilityModeDisabled = "disabled"
|
||||
)
|
||||
|
||||
type grokMediaEligibilityResponse struct {
|
||||
AccountID int64 `json:"account_id"`
|
||||
Mode string `json:"mode"`
|
||||
Eligible bool `json:"eligible"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
type updateGrokMediaEligibilityRequest struct {
|
||||
Mode string `json:"mode" binding:"required"`
|
||||
}
|
||||
|
||||
// GetGrokMediaEligibility returns the current manual override and evaluated
|
||||
// media-routing decision for a Grok OAuth account.
|
||||
// GET /api/v1/admin/accounts/:id/grok-media-eligibility
|
||||
func (h *AccountHandler) GetGrokMediaEligibility(c *gin.Context) {
|
||||
account, err := h.getGrokOAuthAccount(c)
|
||||
if err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, buildGrokMediaEligibilityResponse(account))
|
||||
}
|
||||
|
||||
// UpdateGrokMediaEligibility updates only the manual media eligibility
|
||||
// override. It deliberately uses UpdateAccountExtra so unrelated account
|
||||
// extra fields are preserved.
|
||||
// PUT /api/v1/admin/accounts/:id/grok-media-eligibility
|
||||
func (h *AccountHandler) UpdateGrokMediaEligibility(c *gin.Context) {
|
||||
account, err := h.getGrokOAuthAccount(c)
|
||||
if err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
var req updateGrokMediaEligibilityRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "Invalid request: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
var value any
|
||||
switch req.Mode {
|
||||
case grokMediaEligibilityModeAuto:
|
||||
value = nil
|
||||
case grokMediaEligibilityModeEnabled:
|
||||
value = true
|
||||
case grokMediaEligibilityModeDisabled:
|
||||
value = false
|
||||
default:
|
||||
response.ErrorFrom(c, infraerrors.BadRequest(
|
||||
"GROK_MEDIA_ELIGIBILITY_INVALID_MODE",
|
||||
"mode must be one of auto, enabled, disabled",
|
||||
))
|
||||
return
|
||||
}
|
||||
|
||||
updates := map[string]any{service.GrokMediaEligibleExtraKey: value}
|
||||
if err := service.ValidateGrokMediaEligibilityExtra(account.Platform, updates); err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
if err := h.adminService.UpdateAccountExtra(c.Request.Context(), account.ID, updates); err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
updated, err := h.adminService.GetAccount(c.Request.Context(), account.ID)
|
||||
if err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
if err := ensureGrokOAuthAccount(updated); err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, buildGrokMediaEligibilityResponse(updated))
|
||||
}
|
||||
|
||||
func (h *AccountHandler) getGrokOAuthAccount(c *gin.Context) (*service.Account, error) {
|
||||
accountID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
return nil, infraerrors.BadRequest("INVALID_ACCOUNT_ID", "Invalid account ID")
|
||||
}
|
||||
account, err := h.adminService.GetAccount(c.Request.Context(), accountID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ensureGrokOAuthAccount(account); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return account, nil
|
||||
}
|
||||
|
||||
func ensureGrokOAuthAccount(account *service.Account) error {
|
||||
if account == nil {
|
||||
return infraerrors.NotFound("ACCOUNT_NOT_FOUND", "account not found")
|
||||
}
|
||||
if account.Platform != service.PlatformGrok || account.Type != service.AccountTypeOAuth {
|
||||
return infraerrors.BadRequest(
|
||||
"GROK_MEDIA_ELIGIBILITY_UNSUPPORTED_ACCOUNT",
|
||||
"media eligibility is only supported for Grok OAuth accounts",
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildGrokMediaEligibilityResponse(account *service.Account) grokMediaEligibilityResponse {
|
||||
eligible, reason := account.GrokMediaGenerationEligibility()
|
||||
return grokMediaEligibilityResponse{
|
||||
AccountID: account.ID,
|
||||
Mode: grokMediaEligibilityModeFromExtra(account.Extra),
|
||||
Eligible: eligible,
|
||||
Reason: reason,
|
||||
}
|
||||
}
|
||||
|
||||
func grokMediaEligibilityModeFromExtra(extra map[string]any) string {
|
||||
if extra != nil {
|
||||
if enabled, ok := extra[service.GrokMediaEligibleExtraKey].(bool); ok {
|
||||
if enabled {
|
||||
return grokMediaEligibilityModeEnabled
|
||||
}
|
||||
return grokMediaEligibilityModeDisabled
|
||||
}
|
||||
}
|
||||
return grokMediaEligibilityModeAuto
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type grokMediaEligibilityAdminServiceStub struct {
|
||||
*stubAdminService
|
||||
account *service.Account
|
||||
}
|
||||
|
||||
func (s *grokMediaEligibilityAdminServiceStub) GetAccount(_ context.Context, _ int64) (*service.Account, error) {
|
||||
return s.account, nil
|
||||
}
|
||||
|
||||
func (s *grokMediaEligibilityAdminServiceStub) UpdateAccountExtra(_ context.Context, _ int64, updates map[string]any) error {
|
||||
if s.account.Extra == nil {
|
||||
s.account.Extra = map[string]any{}
|
||||
}
|
||||
for key, value := range updates {
|
||||
if value == nil {
|
||||
delete(s.account.Extra, key)
|
||||
} else {
|
||||
s.account.Extra[key] = value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newGrokMediaEligibilityRouter(account *service.Account) *gin.Engine {
|
||||
stub := &grokMediaEligibilityAdminServiceStub{
|
||||
stubAdminService: newStubAdminService(),
|
||||
account: account,
|
||||
}
|
||||
h := NewAccountHandler(stub, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
|
||||
router := gin.New()
|
||||
router.GET("/accounts/:id/grok-media-eligibility", h.GetGrokMediaEligibility)
|
||||
router.PUT("/accounts/:id/grok-media-eligibility", h.UpdateGrokMediaEligibility)
|
||||
return router
|
||||
}
|
||||
|
||||
func TestGrokMediaEligibilityHandlerGetReturnsModeAndReason(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := newGrokMediaEligibilityRouter(&service.Account{
|
||||
ID: 12,
|
||||
Platform: service.PlatformGrok,
|
||||
Type: service.AccountTypeOAuth,
|
||||
Extra: map[string]any{
|
||||
"grok_billing_snapshot": map[string]any{"status_code": 200, "partial": true},
|
||||
"unrelated_setting": "preserve",
|
||||
},
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/accounts/12/grok-media-eligibility", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
router.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
var envelope struct {
|
||||
Data grokMediaEligibilityResponse `json:"data"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &envelope))
|
||||
require.Equal(t, int64(12), envelope.Data.AccountID)
|
||||
require.Equal(t, grokMediaEligibilityModeAuto, envelope.Data.Mode)
|
||||
require.Equal(t, "billing_inconclusive", envelope.Data.Reason)
|
||||
}
|
||||
|
||||
func TestGrokMediaEligibilityHandlerUpdateModesPreservesExtra(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
account := &service.Account{
|
||||
ID: 12,
|
||||
Platform: service.PlatformGrok,
|
||||
Type: service.AccountTypeOAuth,
|
||||
Extra: map[string]any{"unrelated_setting": "preserve"},
|
||||
}
|
||||
router := newGrokMediaEligibilityRouter(account)
|
||||
|
||||
tests := []struct {
|
||||
mode string
|
||||
wantMode string
|
||||
wantExtra any
|
||||
wantEligible bool
|
||||
wantReason string
|
||||
}{
|
||||
{mode: "enabled", wantMode: grokMediaEligibilityModeEnabled, wantExtra: true, wantEligible: true, wantReason: "override_enabled"},
|
||||
{mode: "disabled", wantMode: grokMediaEligibilityModeDisabled, wantExtra: false, wantEligible: false, wantReason: "override_disabled"},
|
||||
{mode: "auto", wantMode: grokMediaEligibilityModeAuto, wantExtra: nil, wantEligible: false, wantReason: "billing_unobserved"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.mode, func(t *testing.T) {
|
||||
body, err := json.Marshal(map[string]string{"mode": tt.mode})
|
||||
require.NoError(t, err)
|
||||
req := httptest.NewRequest(http.MethodPut, "/accounts/12/grok-media-eligibility", bytes.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
router.ServeHTTP(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
var envelope struct {
|
||||
Data grokMediaEligibilityResponse `json:"data"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &envelope))
|
||||
require.Equal(t, tt.wantMode, envelope.Data.Mode)
|
||||
require.Equal(t, tt.wantEligible, envelope.Data.Eligible)
|
||||
require.Equal(t, tt.wantReason, envelope.Data.Reason)
|
||||
require.Equal(t, "preserve", account.Extra["unrelated_setting"])
|
||||
if tt.wantExtra == nil {
|
||||
_, exists := account.Extra[service.GrokMediaEligibleExtraKey]
|
||||
require.False(t, exists)
|
||||
} else {
|
||||
require.Equal(t, tt.wantExtra, account.Extra[service.GrokMediaEligibleExtraKey])
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrokMediaEligibilityHandlerRejectsUnsupportedAndInvalidModes(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
account *service.Account
|
||||
body string
|
||||
}{
|
||||
{name: "unsupported account", account: &service.Account{ID: 1, Platform: service.PlatformOpenAI, Type: service.AccountTypeOAuth}, body: `{"mode":"enabled"}`},
|
||||
{name: "invalid mode", account: &service.Account{ID: 1, Platform: service.PlatformGrok, Type: service.AccountTypeOAuth}, body: `{"mode":"force"}`},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
router := newGrokMediaEligibilityRouter(tt.account)
|
||||
req := httptest.NewRequest(http.MethodPut, "/accounts/1/grok-media-eligibility", bytes.NewBufferString(tt.body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec := httptest.NewRecorder()
|
||||
router.ServeHTTP(rec, req)
|
||||
require.Equal(t, http.StatusBadRequest, rec.Code)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -184,7 +184,7 @@ func sanitizeUpdateGroupRequestForSimpleMode(req *UpdateGroupRequest) {
|
||||
type CreateGroupRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
Platform string `json:"platform" binding:"omitempty,oneof=anthropic openai gemini antigravity grok kimi zhipu deepseek composite"`
|
||||
Platform string `json:"platform" binding:"omitempty,oneof=anthropic openai gemini antigravity grok kimi zhipu deepseek minimax opencode_go composite"`
|
||||
RateMultiplier float64 `json:"rate_multiplier"`
|
||||
IsExclusive bool `json:"is_exclusive"`
|
||||
SubscriptionType string `json:"subscription_type" binding:"omitempty,oneof=standard subscription"`
|
||||
@@ -258,7 +258,7 @@ type CreateGroupRequest struct {
|
||||
type UpdateGroupRequest struct {
|
||||
Name string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
Platform string `json:"platform" binding:"omitempty,oneof=anthropic openai gemini antigravity grok kimi zhipu deepseek composite"`
|
||||
Platform string `json:"platform" binding:"omitempty,oneof=anthropic openai gemini antigravity grok kimi zhipu deepseek minimax opencode_go composite"`
|
||||
RateMultiplier *float64 `json:"rate_multiplier"`
|
||||
IsExclusive *bool `json:"is_exclusive"`
|
||||
Status string `json:"status" binding:"omitempty,oneof=active inactive"`
|
||||
@@ -332,7 +332,7 @@ type UpdateGroupRequest struct {
|
||||
type CompositeRouteRequest struct {
|
||||
PublicModel string `json:"public_model" binding:"required"`
|
||||
MatchType string `json:"match_type" binding:"omitempty,oneof=exact prefix"`
|
||||
TargetPlatform string `json:"target_platform" binding:"required,oneof=anthropic openai gemini antigravity grok kimi zhipu deepseek"`
|
||||
TargetPlatform string `json:"target_platform" binding:"required,oneof=anthropic openai gemini antigravity grok kimi zhipu deepseek minimax opencode_go"`
|
||||
UpstreamModel string `json:"upstream_model"`
|
||||
Endpoint string `json:"endpoint" binding:"omitempty,oneof=any messages count_tokens responses chat_completions embeddings images gemini"`
|
||||
Priority int `json:"priority"`
|
||||
|
||||
@@ -12,9 +12,9 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// 回归分组平台枚举:kimi/zhipu/deepseek 必须能通过 Create/Update 的 binding 校验
|
||||
// (历史 bug:调度/路由链路已支持 CN 平台分组,但 oneof 白名单漏加三平台,导致
|
||||
// 平台分组无法创建、CN 账号"无可用分组");非法值仍须被拒。
|
||||
// 回归分组平台枚举:kimi/zhipu/deepseek/opencode_go 必须能通过 Create/Update 的
|
||||
// binding 校验(历史 bug:调度/路由链路已支持这些平台,但 oneof 白名单漏加,
|
||||
// 导致平台分组无法创建、账号"无可用分组");非法值仍须被拒。
|
||||
func bindGroupPlatformJSON(t *testing.T, target any, body string) error {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
@@ -27,7 +27,7 @@ func bindGroupPlatformJSON(t *testing.T, target any, body string) error {
|
||||
func TestGroupPlatformBinding_AllowedPlatforms(t *testing.T) {
|
||||
allowed := []string{
|
||||
"anthropic", "openai", "gemini", "antigravity", "grok",
|
||||
"kimi", "zhipu", "deepseek", "composite",
|
||||
"kimi", "zhipu", "deepseek", "minimax", "opencode_go", "composite",
|
||||
}
|
||||
for _, platform := range allowed {
|
||||
t.Run("create_"+platform, func(t *testing.T) {
|
||||
@@ -72,10 +72,15 @@ func TestGroupPlatformBinding_RejectsInvalidPlatforms(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCompositeRouteTargetPlatform_AllowsCNProviders(t *testing.T) {
|
||||
for _, platform := range []string{"kimi", "zhipu", "deepseek"} {
|
||||
for _, platform := range []string{"kimi", "zhipu", "deepseek", "minimax", "opencode_go"} {
|
||||
var req CompositeRouteRequest
|
||||
body := fmt.Sprintf(`{"public_model":"m","target_platform":%q}`, platform)
|
||||
require.NoError(t, bindGroupPlatformJSON(t, &req, body))
|
||||
require.Equal(t, platform, req.TargetPlatform)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompositeRouteTargetPlatform_RejectsComposite(t *testing.T) {
|
||||
var req CompositeRouteRequest
|
||||
require.Error(t, bindGroupPlatformJSON(t, &req, `{"public_model":"m","target_platform":"composite"}`))
|
||||
}
|
||||
|
||||
@@ -27,10 +27,27 @@ func executeAdminIdempotent(
|
||||
payload any,
|
||||
ttl time.Duration,
|
||||
execute func(context.Context) (any, error),
|
||||
) (*service.IdempotencyExecuteResult, error) {
|
||||
return executeAdminIdempotentWithTimeout(c, scope, payload, ttl, 0, execute)
|
||||
}
|
||||
|
||||
func executeAdminIdempotentWithTimeout(
|
||||
c *gin.Context,
|
||||
scope string,
|
||||
payload any,
|
||||
ttl time.Duration,
|
||||
executionTimeout time.Duration,
|
||||
execute func(context.Context) (any, error),
|
||||
) (*service.IdempotencyExecuteResult, error) {
|
||||
coordinator := service.DefaultIdempotencyCoordinator()
|
||||
if coordinator == nil {
|
||||
data, err := execute(c.Request.Context())
|
||||
ctx := c.Request.Context()
|
||||
if executionTimeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(context.WithoutCancel(ctx), executionTimeout)
|
||||
defer cancel()
|
||||
}
|
||||
data, err := execute(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -38,14 +55,15 @@ func executeAdminIdempotent(
|
||||
}
|
||||
|
||||
return coordinator.Execute(c.Request.Context(), service.IdempotencyExecuteOptions{
|
||||
Scope: scope,
|
||||
ActorScope: adminActorScope(c),
|
||||
Method: c.Request.Method,
|
||||
Route: c.FullPath(),
|
||||
IdempotencyKey: c.GetHeader("Idempotency-Key"),
|
||||
Payload: payload,
|
||||
RequireKey: true,
|
||||
TTL: ttl,
|
||||
Scope: scope,
|
||||
ActorScope: adminActorScope(c),
|
||||
Method: c.Request.Method,
|
||||
Route: c.FullPath(),
|
||||
IdempotencyKey: c.GetHeader("Idempotency-Key"),
|
||||
Payload: payload,
|
||||
RequireKey: true,
|
||||
TTL: ttl,
|
||||
ExecutionTimeout: executionTimeout,
|
||||
}, execute)
|
||||
}
|
||||
|
||||
@@ -64,7 +82,18 @@ func executeAdminIdempotentJSON(
|
||||
ttl time.Duration,
|
||||
execute func(context.Context) (any, error),
|
||||
) {
|
||||
executeAdminIdempotentJSONWithMode(c, scope, payload, ttl, idempotencyStoreUnavailableFailClose, execute)
|
||||
executeAdminIdempotentJSONWithMode(c, scope, payload, ttl, 0, idempotencyStoreUnavailableFailClose, execute)
|
||||
}
|
||||
|
||||
func executeAdminIdempotentJSONWithTimeout(
|
||||
c *gin.Context,
|
||||
scope string,
|
||||
payload any,
|
||||
ttl time.Duration,
|
||||
executionTimeout time.Duration,
|
||||
execute func(context.Context) (any, error),
|
||||
) {
|
||||
executeAdminIdempotentJSONWithMode(c, scope, payload, ttl, executionTimeout, idempotencyStoreUnavailableFailClose, execute)
|
||||
}
|
||||
|
||||
func executeAdminIdempotentJSONFailOpenOnStoreUnavailable(
|
||||
@@ -74,7 +103,7 @@ func executeAdminIdempotentJSONFailOpenOnStoreUnavailable(
|
||||
ttl time.Duration,
|
||||
execute func(context.Context) (any, error),
|
||||
) {
|
||||
executeAdminIdempotentJSONWithMode(c, scope, payload, ttl, idempotencyStoreUnavailableFailOpen, execute)
|
||||
executeAdminIdempotentJSONWithMode(c, scope, payload, ttl, 0, idempotencyStoreUnavailableFailOpen, execute)
|
||||
}
|
||||
|
||||
func executeAdminIdempotentJSONWithMode(
|
||||
@@ -82,10 +111,11 @@ func executeAdminIdempotentJSONWithMode(
|
||||
scope string,
|
||||
payload any,
|
||||
ttl time.Duration,
|
||||
executionTimeout time.Duration,
|
||||
mode idempotencyStoreUnavailableMode,
|
||||
execute func(context.Context) (any, error),
|
||||
) {
|
||||
result, err := executeAdminIdempotent(c, scope, payload, ttl, execute)
|
||||
result, err := executeAdminIdempotentWithTimeout(c, scope, payload, ttl, executionTimeout, execute)
|
||||
if err != nil {
|
||||
if infraerrors.Code(err) == infraerrors.Code(service.ErrIdempotencyStoreUnavail) {
|
||||
strategy := "fail_close"
|
||||
|
||||
@@ -219,7 +219,8 @@ func (h *OpsHandler) GetDashboardErrorDistribution(c *gin.Context) {
|
||||
response.Success(c, data)
|
||||
}
|
||||
|
||||
// GetDashboardOpenAITokenStats returns OpenAI token efficiency stats grouped by model.
|
||||
// GetDashboardOpenAITokenStats returns token efficiency stats for all platforms, grouped by model.
|
||||
// The endpoint name is retained for compatibility; platform and group_id optionally narrow the scope.
|
||||
// GET /api/v1/admin/ops/dashboard/openai-token-stats
|
||||
func (h *OpsHandler) GetDashboardOpenAITokenStats(c *gin.Context) {
|
||||
if h.opsService == nil {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
//go:build unit
|
||||
|
||||
package admin
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestProxyHandlerUpdateCredentialPresence(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
check := func(name, body string, username, password *string) {
|
||||
t.Helper()
|
||||
t.Run(name, func(t *testing.T) {
|
||||
svc := &proxyPartialUpdateService{}
|
||||
router := gin.New()
|
||||
router.PUT("/proxies/:id", NewProxyHandler(svc).Update)
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPut, "/proxies/9", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
router.ServeHTTP(w, req)
|
||||
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
|
||||
require.NotNil(t, svc.input)
|
||||
require.Equal(t, username, svc.input.Username)
|
||||
require.Equal(t, password, svc.input.Password)
|
||||
})
|
||||
}
|
||||
empty, username, password := "", "user", "pass"
|
||||
check("omitted", `{"status":"inactive"}`, nil, nil)
|
||||
check("null preserves", `{"username":null,"password":null}`, nil, nil)
|
||||
check("clear both", `{"username":"","password":""}`, &empty, &empty)
|
||||
check("clear username only", `{"username":""}`, &empty, nil)
|
||||
check("clear password only", `{"password":""}`, nil, &empty)
|
||||
check("trim values", `{"username":" user ","password":" pass "}`, &username, &password)
|
||||
check("whitespace clears", `{"username":" ","password":" "}`, &empty, &empty)
|
||||
}
|
||||
@@ -172,16 +172,18 @@ func (h *ProxyHandler) ImportData(c *gin.Context) {
|
||||
updateInput := &service.UpdateProxyInput{
|
||||
Status: normalizedStatus,
|
||||
ExpiresAt: existingExpiresAt,
|
||||
ClearExpiresAt: existingExpiresAt == nil,
|
||||
FallbackMode: existingFallbackMode,
|
||||
BackupProxyID: existingBackupProxyID,
|
||||
ExpiryWarnDays: item.ExpiryWarnDays,
|
||||
ClearBackupID: existingBackupProxyID == nil,
|
||||
ExpiryWarnDays: &item.ExpiryWarnDays,
|
||||
// 保留已存在代理的网络配置字段
|
||||
Name: existing.Name,
|
||||
Protocol: existing.Protocol,
|
||||
Host: existing.Host,
|
||||
Port: existing.Port,
|
||||
Username: existing.Username,
|
||||
Password: existing.Password,
|
||||
Username: &existing.Username,
|
||||
Password: &existing.Password,
|
||||
}
|
||||
if _, err := h.adminService.UpdateProxy(ctx, existing.ID, updateInput); err != nil {
|
||||
result.Errors = append(result.Errors, DataImportError{
|
||||
@@ -255,15 +257,17 @@ func (h *ProxyHandler) ImportData(c *gin.Context) {
|
||||
if _, err := h.adminService.UpdateProxy(ctx, created.ID, &service.UpdateProxyInput{
|
||||
Status: normalizedStatus,
|
||||
ExpiresAt: expiresAt,
|
||||
ClearExpiresAt: expiresAt == nil,
|
||||
FallbackMode: fallbackMode,
|
||||
BackupProxyID: backupProxyID,
|
||||
ExpiryWarnDays: item.ExpiryWarnDays,
|
||||
ClearBackupID: backupProxyID == nil,
|
||||
ExpiryWarnDays: &item.ExpiryWarnDays,
|
||||
Name: created.Name,
|
||||
Protocol: created.Protocol,
|
||||
Host: created.Host,
|
||||
Port: created.Port,
|
||||
Username: created.Username,
|
||||
Password: created.Password,
|
||||
Username: &created.Username,
|
||||
Password: &created.Password,
|
||||
}); err != nil {
|
||||
result.Errors = append(result.Errors, DataImportError{
|
||||
Kind: "proxy",
|
||||
|
||||
@@ -41,17 +41,17 @@ type CreateProxyRequest struct {
|
||||
|
||||
// UpdateProxyRequest represents update proxy request
|
||||
type UpdateProxyRequest struct {
|
||||
Name string `json:"name"`
|
||||
Protocol string `json:"protocol" binding:"omitempty,oneof=http https socks5 socks5h"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port" binding:"omitempty,min=1,max=65535"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Status string `json:"status" binding:"omitempty,oneof=active inactive"`
|
||||
ExpiresAt *int64 `json:"expires_at"`
|
||||
FallbackMode string `json:"fallback_mode" binding:"omitempty,oneof=none proxy direct"`
|
||||
BackupProxyID *int64 `json:"backup_proxy_id"`
|
||||
ExpiryWarnDays int `json:"expiry_warn_days" binding:"omitempty,min=0"`
|
||||
Name string `json:"name"`
|
||||
Protocol string `json:"protocol" binding:"omitempty,oneof=http https socks5 socks5h"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port" binding:"omitempty,min=1,max=65535"`
|
||||
Username *string `json:"username"`
|
||||
Password *string `json:"password"`
|
||||
Status string `json:"status" binding:"omitempty,oneof=active inactive"`
|
||||
ExpiresAt dto.NullableInt64Field `json:"expires_at"`
|
||||
FallbackMode string `json:"fallback_mode" binding:"omitempty,oneof=none proxy direct"`
|
||||
BackupProxyID dto.NullableInt64Field `json:"backup_proxy_id"`
|
||||
ExpiryWarnDays *int `json:"expiry_warn_days" binding:"omitempty,min=0"`
|
||||
}
|
||||
|
||||
// List handles listing all proxies with pagination
|
||||
@@ -183,21 +183,29 @@ func (h *ProxyHandler) Update(c *gin.Context) {
|
||||
}
|
||||
|
||||
var expiresAt *time.Time
|
||||
if req.ExpiresAt != nil && *req.ExpiresAt > 0 {
|
||||
t := time.Unix(*req.ExpiresAt, 0).UTC()
|
||||
if req.ExpiresAt.Value != nil && *req.ExpiresAt.Value > 0 {
|
||||
t := time.Unix(*req.ExpiresAt.Value, 0).UTC()
|
||||
expiresAt = &t
|
||||
}
|
||||
if req.Username != nil {
|
||||
*req.Username = strings.TrimSpace(*req.Username)
|
||||
}
|
||||
if req.Password != nil {
|
||||
*req.Password = strings.TrimSpace(*req.Password)
|
||||
}
|
||||
proxy, err := h.adminService.UpdateProxy(c.Request.Context(), proxyID, &service.UpdateProxyInput{
|
||||
Name: strings.TrimSpace(req.Name),
|
||||
Protocol: strings.TrimSpace(req.Protocol),
|
||||
Host: strings.TrimSpace(req.Host),
|
||||
Port: req.Port,
|
||||
Username: strings.TrimSpace(req.Username),
|
||||
Password: strings.TrimSpace(req.Password),
|
||||
Username: req.Username,
|
||||
Password: req.Password,
|
||||
Status: strings.TrimSpace(req.Status),
|
||||
ExpiresAt: expiresAt,
|
||||
ClearExpiresAt: req.ExpiresAt.Set && expiresAt == nil,
|
||||
FallbackMode: strings.TrimSpace(req.FallbackMode),
|
||||
BackupProxyID: req.BackupProxyID,
|
||||
BackupProxyID: req.BackupProxyID.Value,
|
||||
ClearBackupID: req.BackupProxyID.Set && req.BackupProxyID.Value == nil,
|
||||
ExpiryWarnDays: req.ExpiryWarnDays,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
//go:build unit
|
||||
|
||||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type proxyPartialUpdateService struct {
|
||||
service.AdminService
|
||||
input *service.UpdateProxyInput
|
||||
}
|
||||
|
||||
func (s *proxyPartialUpdateService) UpdateProxy(_ context.Context, _ int64, input *service.UpdateProxyInput) (*service.Proxy, error) {
|
||||
s.input = input
|
||||
return &service.Proxy{ID: 9}, nil
|
||||
}
|
||||
|
||||
func TestProxyHandlerUpdatePreservesFieldPresence(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
for _, tc := range []struct {
|
||||
name, body string
|
||||
set, clear, warn bool
|
||||
}{
|
||||
{name: "omitted", body: `{"status":"inactive"}`},
|
||||
{name: "null clears", body: `{"expires_at":null,"backup_proxy_id":null,"fallback_mode":"none","expiry_warn_days":0}`, set: true, clear: true, warn: true},
|
||||
{name: "zero clears expiry", body: `{"expires_at":0,"backup_proxy_id":null}`, set: true, clear: true},
|
||||
{name: "values", body: `{"expires_at":1800000000,"backup_proxy_id":10,"fallback_mode":"proxy","expiry_warn_days":7}`, set: true, warn: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
svc := &proxyPartialUpdateService{}
|
||||
router := gin.New()
|
||||
router.PUT("/proxies/:id", NewProxyHandler(svc).Update)
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPut, "/proxies/9", strings.NewReader(tc.body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
router.ServeHTTP(w, req)
|
||||
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
|
||||
require.NotNil(t, svc.input)
|
||||
require.Equal(t, tc.set, svc.input.ExpiresAt != nil || svc.input.ClearExpiresAt)
|
||||
require.Equal(t, tc.set, svc.input.BackupProxyID != nil || svc.input.ClearBackupID)
|
||||
require.Equal(t, tc.warn, svc.input.ExpiryWarnDays != nil)
|
||||
if !tc.set || tc.clear {
|
||||
require.Nil(t, svc.input.ExpiresAt)
|
||||
require.Nil(t, svc.input.BackupProxyID)
|
||||
} else {
|
||||
require.Equal(t, int64(1800000000), svc.input.ExpiresAt.Unix())
|
||||
require.Equal(t, int64(10), *svc.input.BackupProxyID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -376,12 +376,14 @@ func (h *SettingHandler) GetSettings(c *gin.Context) {
|
||||
ChannelMonitorDefaultIntervalSeconds: settings.ChannelMonitorDefaultIntervalSeconds,
|
||||
ChannelMonitorHideThroughput: settings.ChannelMonitorHideThroughput,
|
||||
ChannelMonitorShowQuota: settings.ChannelMonitorShowQuota,
|
||||
ChannelMonitorHideUserRanking: settings.ChannelMonitorHideUserRanking,
|
||||
|
||||
GrokDefaultTextModel: settings.GrokDefaultTextModel,
|
||||
GrokCrossClientModelMapEnabled: settings.GrokCrossClientModelMapEnabled,
|
||||
GrokDefaultBaseURLMode: settings.GrokDefaultBaseURLMode,
|
||||
|
||||
AvailableChannelsEnabled: settings.AvailableChannelsEnabled,
|
||||
SubscriptionEnabled: settings.SubscriptionEnabled,
|
||||
|
||||
ModelPlazaEnabled: settings.ModelPlazaEnabled,
|
||||
ModelPlazaRequireAuth: settings.ModelPlazaRequireAuth,
|
||||
|
||||
@@ -579,6 +579,9 @@ func diffSettings(before *service.SystemSettings, after *service.SystemSettings,
|
||||
if before.AvailableChannelsEnabled != after.AvailableChannelsEnabled {
|
||||
changed = append(changed, "available_channels_enabled")
|
||||
}
|
||||
if before.SubscriptionEnabled != after.SubscriptionEnabled {
|
||||
changed = append(changed, "subscription_enabled")
|
||||
}
|
||||
if before.ModelPlazaEnabled != after.ModelPlazaEnabled {
|
||||
changed = append(changed, "model_plaza_enabled")
|
||||
}
|
||||
|
||||
@@ -186,3 +186,20 @@ func TestUpdateSettingsValidatesTencentCaptchaAppIDWhenEnabledFlagIsOmitted(t *t
|
||||
require.Equal(t, http.StatusBadRequest, rec.Code)
|
||||
require.Contains(t, rec.Body.String(), "positive integer")
|
||||
}
|
||||
|
||||
// subscription_enabled is an opt-out switch: an explicit false is written as-is,
|
||||
// and a later payload that omits the field keeps the stored value.
|
||||
func TestUpdateSettingsSubscriptionEnabledIsWritableAndKeptWhenOmitted(t *testing.T) {
|
||||
h, repo := newStepUpSwitchTestHandler(t, map[string]string{
|
||||
service.SettingKeySubscriptionEnabled: "true",
|
||||
})
|
||||
|
||||
rec := doUpdateSettings(t, h, map[string]any{"subscription_enabled": false}, nil)
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
require.Equal(t, "false", repo.values[service.SettingKeySubscriptionEnabled])
|
||||
|
||||
rec = doUpdateSettings(t, h, map[string]any{"site_name": "Example Gateway"}, nil)
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
require.Equal(t, "false", repo.values[service.SettingKeySubscriptionEnabled],
|
||||
"a payload without subscription_enabled must not flip the stored value back to true")
|
||||
}
|
||||
|
||||
@@ -334,6 +334,7 @@ type UpdateSettingsRequest struct {
|
||||
ChannelMonitorDefaultIntervalSeconds *int `json:"channel_monitor_default_interval_seconds"`
|
||||
ChannelMonitorHideThroughput *bool `json:"channel_monitor_hide_throughput"`
|
||||
ChannelMonitorShowQuota *bool `json:"channel_monitor_show_quota"`
|
||||
ChannelMonitorHideUserRanking *bool `json:"channel_monitor_hide_user_ranking"`
|
||||
|
||||
// Grok model mapping policy
|
||||
GrokDefaultTextModel *string `json:"grok_default_text_model"`
|
||||
@@ -343,6 +344,9 @@ type UpdateSettingsRequest struct {
|
||||
// Available Channels feature switch (user-facing)
|
||||
AvailableChannelsEnabled *bool `json:"available_channels_enabled"`
|
||||
|
||||
// Subscription feature switch (user-facing subscription surface; see SettingKeySubscriptionEnabled)
|
||||
SubscriptionEnabled *bool `json:"subscription_enabled"`
|
||||
|
||||
// Model Plaza feature switches + description
|
||||
ModelPlazaEnabled *bool `json:"model_plaza_enabled"`
|
||||
ModelPlazaRequireAuth *bool `json:"model_plaza_require_auth"`
|
||||
@@ -1906,6 +1910,12 @@ func (h *SettingHandler) UpdateSettings(c *gin.Context) {
|
||||
}
|
||||
return previousSettings.ChannelMonitorShowQuota
|
||||
}(),
|
||||
ChannelMonitorHideUserRanking: func() bool {
|
||||
if req.ChannelMonitorHideUserRanking != nil {
|
||||
return *req.ChannelMonitorHideUserRanking
|
||||
}
|
||||
return previousSettings.ChannelMonitorHideUserRanking
|
||||
}(),
|
||||
GrokDefaultTextModel: func() string {
|
||||
if req.GrokDefaultTextModel != nil {
|
||||
return *req.GrokDefaultTextModel
|
||||
@@ -1930,6 +1940,12 @@ func (h *SettingHandler) UpdateSettings(c *gin.Context) {
|
||||
}
|
||||
return previousSettings.AvailableChannelsEnabled
|
||||
}(),
|
||||
SubscriptionEnabled: func() bool {
|
||||
if req.SubscriptionEnabled != nil {
|
||||
return *req.SubscriptionEnabled
|
||||
}
|
||||
return previousSettings.SubscriptionEnabled
|
||||
}(),
|
||||
ModelPlazaEnabled: func() bool {
|
||||
if req.ModelPlazaEnabled != nil {
|
||||
return *req.ModelPlazaEnabled
|
||||
@@ -2366,12 +2382,14 @@ func (h *SettingHandler) UpdateSettings(c *gin.Context) {
|
||||
ChannelMonitorDefaultIntervalSeconds: updatedSettings.ChannelMonitorDefaultIntervalSeconds,
|
||||
ChannelMonitorHideThroughput: updatedSettings.ChannelMonitorHideThroughput,
|
||||
ChannelMonitorShowQuota: updatedSettings.ChannelMonitorShowQuota,
|
||||
ChannelMonitorHideUserRanking: updatedSettings.ChannelMonitorHideUserRanking,
|
||||
|
||||
GrokDefaultTextModel: updatedSettings.GrokDefaultTextModel,
|
||||
GrokCrossClientModelMapEnabled: updatedSettings.GrokCrossClientModelMapEnabled,
|
||||
GrokDefaultBaseURLMode: updatedSettings.GrokDefaultBaseURLMode,
|
||||
|
||||
AvailableChannelsEnabled: updatedSettings.AvailableChannelsEnabled,
|
||||
SubscriptionEnabled: updatedSettings.SubscriptionEnabled,
|
||||
|
||||
ModelPlazaEnabled: updatedSettings.ModelPlazaEnabled,
|
||||
ModelPlazaRequireAuth: updatedSettings.ModelPlazaRequireAuth,
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type bulkActionHandlerSubscriptionRepo struct {
|
||||
service.UserSubscriptionRepository
|
||||
sub *service.UserSubscription
|
||||
extendCalls int
|
||||
}
|
||||
|
||||
type cancellationAwareBulkActionRepo struct {
|
||||
*bulkActionHandlerSubscriptionRepo
|
||||
cancelRequest context.CancelFunc
|
||||
}
|
||||
|
||||
func (r *cancellationAwareBulkActionRepo) GetByID(ctx context.Context, id int64) (*service.UserSubscription, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.bulkActionHandlerSubscriptionRepo.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
func (r *cancellationAwareBulkActionRepo) ExtendExpiry(ctx context.Context, id int64, expiry time.Time) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
err := r.bulkActionHandlerSubscriptionRepo.ExtendExpiry(ctx, id, expiry)
|
||||
r.cancelRequest()
|
||||
return err
|
||||
}
|
||||
|
||||
type cancellationAwareAdminIdempotencyRepo struct {
|
||||
*memoryIdempotencyRepoStub
|
||||
saves int
|
||||
}
|
||||
|
||||
func (r *cancellationAwareAdminIdempotencyRepo) MarkSucceeded(ctx context.Context, id int64, status int, body string, expiresAt time.Time) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
r.saves++
|
||||
return r.memoryIdempotencyRepoStub.MarkSucceeded(ctx, id, status, body, expiresAt)
|
||||
}
|
||||
|
||||
func (r *bulkActionHandlerSubscriptionRepo) GetByID(_ context.Context, id int64) (*service.UserSubscription, error) {
|
||||
if r.sub == nil || r.sub.ID != id {
|
||||
return nil, service.ErrSubscriptionNotFound
|
||||
}
|
||||
copy := *r.sub
|
||||
return ©, nil
|
||||
}
|
||||
|
||||
func (r *bulkActionHandlerSubscriptionRepo) GetByIDForUpdate(ctx context.Context, id int64) (*service.UserSubscription, error) {
|
||||
return r.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
func (r *bulkActionHandlerSubscriptionRepo) ExtendExpiry(_ context.Context, _ int64, expiry time.Time) error {
|
||||
r.extendCalls++
|
||||
r.sub.ExpiresAt = expiry
|
||||
return nil
|
||||
}
|
||||
|
||||
func bulkActionHandlerRequest(router *gin.Engine, path, body, key string) *httptest.ResponseRecorder {
|
||||
req := httptest.NewRequest(http.MethodPost, path, bytes.NewBufferString(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if key != "" {
|
||||
req.Header.Set("Idempotency-Key", key)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
router.ServeHTTP(rec, req)
|
||||
return rec
|
||||
}
|
||||
|
||||
func TestSubscriptionBulkAction_ReplaysPartialResultWithoutRepeatingExtension(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
cfg := service.DefaultIdempotencyConfig()
|
||||
cfg.ObserveOnly = false
|
||||
service.SetDefaultIdempotencyCoordinator(service.NewIdempotencyCoordinator(newMemoryIdempotencyRepoStub(), cfg))
|
||||
t.Cleanup(func() { service.SetDefaultIdempotencyCoordinator(nil) })
|
||||
expiresAt := time.Now().AddDate(0, 0, 30)
|
||||
repo := &bulkActionHandlerSubscriptionRepo{sub: &service.UserSubscription{ID: 1, UserID: 1, GroupID: 10, ExpiresAt: expiresAt}}
|
||||
svc := service.NewSubscriptionService(nil, repo, nil, nil, nil)
|
||||
t.Cleanup(svc.Stop)
|
||||
h := NewSubscriptionHandler(svc)
|
||||
router := gin.New()
|
||||
path := "/api/v1/admin/subscriptions/bulk-action"
|
||||
router.POST(path, h.BulkAction)
|
||||
body := `{"subscription_ids":[1,404,1],"action":"extend","days":7}`
|
||||
|
||||
missingKey := bulkActionHandlerRequest(router, path, body, "")
|
||||
require.Equal(t, http.StatusBadRequest, missingKey.Code)
|
||||
require.Zero(t, repo.extendCalls)
|
||||
|
||||
first := bulkActionHandlerRequest(router, path, body, "bulk-extend-once")
|
||||
require.Equal(t, http.StatusOK, first.Code, first.Body.String())
|
||||
var result struct {
|
||||
Data service.BulkSubscriptionActionResult `json:"data"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(first.Body.Bytes(), &result))
|
||||
require.Equal(t, 1, result.Data.SuccessCount)
|
||||
require.Equal(t, 1, result.Data.FailedCount)
|
||||
require.Len(t, result.Data.Results, 2)
|
||||
|
||||
replayed := bulkActionHandlerRequest(router, path, body, "bulk-extend-once")
|
||||
require.Equal(t, http.StatusOK, replayed.Code, replayed.Body.String())
|
||||
require.Equal(t, "true", replayed.Header().Get("X-Idempotency-Replayed"))
|
||||
require.JSONEq(t, first.Body.String(), replayed.Body.String())
|
||||
require.Equal(t, 1, repo.extendCalls)
|
||||
require.Equal(t, expiresAt.AddDate(0, 0, 7), repo.sub.ExpiresAt)
|
||||
|
||||
conflict := bulkActionHandlerRequest(router, path, `{"subscription_ids":[1,404,1],"action":"extend","days":14}`, "bulk-extend-once")
|
||||
require.Equal(t, http.StatusConflict, conflict.Code)
|
||||
require.Equal(t, 1, repo.extendCalls)
|
||||
}
|
||||
|
||||
func TestSubscriptionBulkAction_ClientCancellationStillPersistsReplay(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
idempotencyRepo := &cancellationAwareAdminIdempotencyRepo{memoryIdempotencyRepoStub: newMemoryIdempotencyRepoStub()}
|
||||
service.SetDefaultIdempotencyCoordinator(service.NewIdempotencyCoordinator(idempotencyRepo, service.DefaultIdempotencyConfig()))
|
||||
t.Cleanup(func() { service.SetDefaultIdempotencyCoordinator(nil) })
|
||||
requestCtx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
expiresAt := time.Now().AddDate(0, 0, 30)
|
||||
repo := &cancellationAwareBulkActionRepo{
|
||||
bulkActionHandlerSubscriptionRepo: &bulkActionHandlerSubscriptionRepo{sub: &service.UserSubscription{ID: 1, UserID: 1, GroupID: 10, ExpiresAt: expiresAt}},
|
||||
cancelRequest: cancel,
|
||||
}
|
||||
svc := service.NewSubscriptionService(nil, repo, nil, nil, nil)
|
||||
t.Cleanup(svc.Stop)
|
||||
router := gin.New()
|
||||
path := "/api/v1/admin/subscriptions/bulk-action"
|
||||
router.POST(path, NewSubscriptionHandler(svc).BulkAction)
|
||||
body := `{"subscription_ids":[1],"action":"extend","days":7}`
|
||||
request := httptest.NewRequest(http.MethodPost, path, bytes.NewBufferString(body)).WithContext(requestCtx)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("Idempotency-Key", "canceled-request")
|
||||
first := httptest.NewRecorder()
|
||||
router.ServeHTTP(first, request)
|
||||
require.ErrorIs(t, requestCtx.Err(), context.Canceled)
|
||||
require.Equal(t, http.StatusOK, first.Code, first.Body.String())
|
||||
require.Equal(t, 1, idempotencyRepo.saves)
|
||||
|
||||
replayed := bulkActionHandlerRequest(router, path, body, "canceled-request")
|
||||
require.Equal(t, http.StatusOK, replayed.Code, replayed.Body.String())
|
||||
require.Equal(t, "true", replayed.Header().Get("X-Idempotency-Replayed"))
|
||||
require.JSONEq(t, first.Body.String(), replayed.Body.String())
|
||||
require.Equal(t, 1, repo.extendCalls)
|
||||
require.Equal(t, expiresAt.AddDate(0, 0, 7), repo.sub.ExpiresAt)
|
||||
}
|
||||
|
||||
func TestSubscriptionBulkAction_ValidatesBeforeIdempotencyAndExecution(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
service.SetDefaultIdempotencyCoordinator(service.NewIdempotencyCoordinator(storeUnavailableRepoStub{}, service.DefaultIdempotencyConfig()))
|
||||
t.Cleanup(func() { service.SetDefaultIdempotencyCoordinator(nil) })
|
||||
router := gin.New()
|
||||
path := "/api/v1/admin/subscriptions/bulk-action"
|
||||
router.POST(path, NewSubscriptionHandler(nil).BulkAction)
|
||||
tooManyIDs := make([]int64, 101)
|
||||
for i := range tooManyIDs {
|
||||
tooManyIDs[i] = 1
|
||||
}
|
||||
tooManyBody, err := json.Marshal(service.BulkSubscriptionActionInput{SubscriptionIDs: tooManyIDs, Action: "revoke"})
|
||||
require.NoError(t, err)
|
||||
for _, body := range []string{
|
||||
`{"subscription_ids":[1],"action":"delete"}`,
|
||||
`{"subscription_ids":[1,0],"action":"revoke"}`,
|
||||
`{"subscription_ids":[],"action":"restore"}`,
|
||||
`{"subscription_ids":[1],"action":"extend","days":0}`,
|
||||
`{"subscription_ids":[1],"action":"extend","days":36501}`,
|
||||
`{"subscription_ids":[1],"action":"reset_quota"}`,
|
||||
`{"subscription_ids":[1],"action":"reset_quota","daily":"yes"}`,
|
||||
`{"subscription_ids":[1],"action":"revoke"`,
|
||||
string(tooManyBody),
|
||||
} {
|
||||
t.Run(body, func(t *testing.T) {
|
||||
response := bulkActionHandlerRequest(router, path, body, "bulk-invalid")
|
||||
require.Equal(t, http.StatusBadRequest, response.Code, response.Body.String())
|
||||
})
|
||||
}
|
||||
|
||||
// A valid request must still fail closed when replay protection is unavailable.
|
||||
response := bulkActionHandlerRequest(router, path, `{"subscription_ids":[1],"action":"revoke"}`, "bulk-valid")
|
||||
require.Equal(t, http.StatusServiceUnavailable, response.Code)
|
||||
}
|
||||
|
||||
func TestSubscriptionBulkAssign_RejectsInvalidUserIDsBeforeExecution(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
path := "/api/v1/admin/subscriptions/bulk-assign"
|
||||
router.POST(path, NewSubscriptionHandler(nil).BulkAssign)
|
||||
for _, ids := range [][]int64{{}, {1, 0}, {1, -1}, make([]int64, 101)} {
|
||||
t.Run(fmt.Sprint(len(ids), ids), func(t *testing.T) {
|
||||
body, err := json.Marshal(BulkAssignSubscriptionRequest{UserIDs: ids, GroupID: 1, ValidityDays: 30})
|
||||
require.NoError(t, err)
|
||||
response := bulkActionHandlerRequest(router, path, string(body), "")
|
||||
require.Equal(t, http.StatusBadRequest, response.Code, response.Body.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package admin
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
|
||||
@@ -48,7 +49,7 @@ type AssignSubscriptionRequest struct {
|
||||
|
||||
// BulkAssignSubscriptionRequest represents bulk assign subscription request
|
||||
type BulkAssignSubscriptionRequest struct {
|
||||
UserIDs []int64 `json:"user_ids" binding:"required,min=1"`
|
||||
UserIDs []int64 `json:"user_ids" binding:"required,min=1,max=100,dive,gt=0"`
|
||||
GroupID int64 `json:"group_id" binding:"required"`
|
||||
ValidityDays int `json:"validity_days" binding:"omitempty,max=36500"` // max 100 years
|
||||
Notes string `json:"notes"`
|
||||
@@ -186,6 +187,23 @@ func (h *SubscriptionHandler) BulkAssign(c *gin.Context) {
|
||||
response.Success(c, dto.BulkAssignResultFromService(result))
|
||||
}
|
||||
|
||||
// BulkAction applies one operation to selected subscriptions, returning each outcome.
|
||||
// POST /api/v1/admin/subscriptions/bulk-action
|
||||
func (h *SubscriptionHandler) BulkAction(c *gin.Context) {
|
||||
var req service.BulkSubscriptionActionInput
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "Invalid request: "+err.Error())
|
||||
return
|
||||
}
|
||||
if err := req.Validate(); err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
executeAdminIdempotentJSONWithTimeout(c, "admin.subscriptions.bulk-action", req, service.DefaultWriteIdempotencyTTL(), 2*time.Minute, func(ctx context.Context) (any, error) {
|
||||
return h.subscriptionService.BulkSubscriptionAction(ctx, &req)
|
||||
})
|
||||
}
|
||||
|
||||
// Extend handles adjusting a subscription (extend or shorten)
|
||||
// POST /api/v1/admin/subscriptions/:id/extend
|
||||
func (h *SubscriptionHandler) Extend(c *gin.Context) {
|
||||
|
||||
@@ -790,15 +790,21 @@ func (h *UserHandler) UpdateUserPlatformQuotas(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// 三档全空的输入不落库:user_platform_quotas 只保存至少配置了一档限额的记录,
|
||||
// 不存在的行等价于不限额。未进入 records 的平台由 UpsertForUser 软删,审计里记为 removed。
|
||||
records := make([]service.UserPlatformQuotaRecord, 0, len(req.Quotas))
|
||||
for _, q := range req.Quotas {
|
||||
records = append(records, service.UserPlatformQuotaRecord{
|
||||
rec := service.UserPlatformQuotaRecord{
|
||||
UserID: userID,
|
||||
Platform: q.Platform,
|
||||
DailyLimitUSD: q.DailyLimitUSD,
|
||||
WeeklyLimitUSD: q.WeeklyLimitUSD,
|
||||
MonthlyLimitUSD: q.MonthlyLimitUSD,
|
||||
})
|
||||
}
|
||||
if !rec.HasAnyLimit() {
|
||||
continue
|
||||
}
|
||||
records = append(records, rec)
|
||||
}
|
||||
|
||||
ctx := c.Request.Context()
|
||||
@@ -860,6 +866,7 @@ func (h *UserHandler) UpdateUserPlatformQuotas(c *gin.Context) {
|
||||
slog.Info("admin.quota_updated",
|
||||
"actor_admin_id", getAdminIDFromContext(c),
|
||||
"target_user_id", userID,
|
||||
"submitted_count", len(req.Quotas),
|
||||
"platform_count", len(records),
|
||||
"before_snapshot_available", beforeErr == nil,
|
||||
"changes", changes)
|
||||
|
||||
@@ -112,16 +112,61 @@ func TestUpdateUserPlatformQuotas_Success(t *testing.T) {
|
||||
if len(repo.upsertCalls) != 1 {
|
||||
t.Fatalf("UpsertForUser should be called once, got %d", len(repo.upsertCalls))
|
||||
}
|
||||
// upsert 记录数 = 请求体中给出的平台数(未给出的平台不落库)。
|
||||
if repo.upsertCalls[0].userID != 42 || len(repo.upsertCalls[0].records) != 5 {
|
||||
// upsert 记录数 = 请求体中至少配置了一档限额的平台数;三档全空的平台不落库,
|
||||
// 由 UpsertForUser 的"不在列表里就软删"分支处理。
|
||||
if repo.upsertCalls[0].userID != 42 || len(repo.upsertCalls[0].records) != 2 {
|
||||
t.Errorf("unexpected upsert call: %+v", repo.upsertCalls[0])
|
||||
}
|
||||
for _, r := range repo.upsertCalls[0].records {
|
||||
if r.Platform != "anthropic" && r.Platform != "openai" {
|
||||
t.Errorf("platform %q has no configured limit and must not be upserted", r.Platform)
|
||||
}
|
||||
}
|
||||
// 缓存失效:按全部允许平台统一失效(含 kimi/zhipu/deepseek)。
|
||||
if len(cache.deleteCalls) != len(service.AllowedQuotaPlatforms) {
|
||||
t.Errorf("expected %d cache delete calls, got %d: %+v", len(service.AllowedQuotaPlatforms), len(cache.deleteCalls), cache.deleteCalls)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateUserPlatformQuotas_AllUnlimitedClearsRows 锁定:全部平台三档全空等价于清空,
|
||||
// UpsertForUser 收到空列表(软删该用户所有活跃行),且 0 = 显式禁用仍算已配置。
|
||||
func TestUpdateUserPlatformQuotas_AllUnlimitedClearsRows(t *testing.T) {
|
||||
repo := &upsertCapturingQuotaRepo{}
|
||||
cache := &billingCacheStub{}
|
||||
h := buildTestHandler(repo, cache)
|
||||
|
||||
body := `{"quotas":[
|
||||
{"platform":"anthropic","daily_limit_usd":null,"weekly_limit_usd":null,"monthly_limit_usd":null},
|
||||
{"platform":"openai"}
|
||||
]}`
|
||||
c, w := putReq(t, body)
|
||||
h.UpdateUserPlatformQuotas(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if len(repo.upsertCalls) != 1 {
|
||||
t.Fatalf("UpsertForUser should be called once, got %d", len(repo.upsertCalls))
|
||||
}
|
||||
if len(repo.upsertCalls[0].records) != 0 {
|
||||
t.Errorf("all-unlimited input must upsert zero records, got %+v", repo.upsertCalls[0].records)
|
||||
}
|
||||
|
||||
repo = &upsertCapturingQuotaRepo{}
|
||||
h = buildTestHandler(repo, &billingCacheStub{})
|
||||
c, w = putReq(t, `{"quotas":[{"platform":"gemini","daily_limit_usd":0}]}`)
|
||||
h.UpdateUserPlatformQuotas(c)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if len(repo.upsertCalls) != 1 || len(repo.upsertCalls[0].records) != 1 {
|
||||
t.Fatalf("zero limit is a configured limit and must be upserted: %+v", repo.upsertCalls)
|
||||
}
|
||||
if r := repo.upsertCalls[0].records[0]; r.Platform != "gemini" || r.DailyLimitUSD == nil || *r.DailyLimitUSD != 0 {
|
||||
t.Errorf("unexpected record: %+v", r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateUserPlatformQuotas_RejectsDuplicatePlatform(t *testing.T) {
|
||||
h := buildTestHandler(&upsertCapturingQuotaRepo{}, &billingCacheStub{})
|
||||
body := `{"quotas":[
|
||||
|
||||
@@ -35,6 +35,7 @@ func TestOpenAICompatibleTextTargetAllowsCompositeProviders(t *testing.T) {
|
||||
{model: "k3", platform: service.PlatformKimi},
|
||||
{model: "glm-5.2", platform: service.PlatformZhipu},
|
||||
{model: "deepseek-v3.2", platform: service.PlatformDeepseek},
|
||||
{model: "MiniMax-M3", platform: service.PlatformMiniMax},
|
||||
}
|
||||
for _, path := range []string{"/v1/messages", "/v1/chat/completions", "/v1/responses", "/v1/responses/input_tokens", "/v1/messages/count_tokens"} {
|
||||
for _, provider := range providers {
|
||||
@@ -56,7 +57,7 @@ func TestResponsesWebSocketCompositePlatformGuardKeepsOpenAIAndGrokOnly(t *testi
|
||||
require.True(t, isResponsesWebSocketCompositePlatform(service.PlatformOpenAI))
|
||||
require.True(t, isResponsesWebSocketCompositePlatform(service.PlatformGrok))
|
||||
for _, platform := range []string{
|
||||
service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek,
|
||||
service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek, service.PlatformMiniMax,
|
||||
service.PlatformAnthropic, service.PlatformGemini,
|
||||
} {
|
||||
require.False(t, isResponsesWebSocketCompositePlatform(platform), "platform=%s", platform)
|
||||
|
||||
@@ -9,13 +9,14 @@ import (
|
||||
|
||||
// CustomMenuItem represents a user-configured custom menu entry.
|
||||
type CustomMenuItem struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
IconSVG string `json:"icon_svg"`
|
||||
URL string `json:"url"`
|
||||
PageSlug string `json:"page_slug,omitempty"`
|
||||
Visibility string `json:"visibility"` // "user" or "admin"
|
||||
SortOrder int `json:"sort_order"`
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
IconSVG string `json:"icon_svg"`
|
||||
URL string `json:"url"`
|
||||
PageSlug string `json:"page_slug,omitempty"`
|
||||
Visibility string `json:"visibility"` // "user" or "admin"
|
||||
SortOrder int `json:"sort_order"`
|
||||
HideOpenButton bool `json:"hide_open_button,omitempty"`
|
||||
}
|
||||
|
||||
// CustomEndpoint represents an admin-configured API endpoint for quick copy.
|
||||
@@ -307,6 +308,7 @@ type SystemSettings struct {
|
||||
ChannelMonitorDefaultIntervalSeconds int `json:"channel_monitor_default_interval_seconds"`
|
||||
ChannelMonitorHideThroughput bool `json:"channel_monitor_hide_throughput"`
|
||||
ChannelMonitorShowQuota bool `json:"channel_monitor_show_quota"`
|
||||
ChannelMonitorHideUserRanking bool `json:"channel_monitor_hide_user_ranking"`
|
||||
|
||||
// Grok model mapping policy (admin settings; empty account mapping falls back to these).
|
||||
GrokDefaultTextModel string `json:"grok_default_text_model"`
|
||||
@@ -316,6 +318,10 @@ type SystemSettings struct {
|
||||
// Available Channels feature switch (user-facing aggregate view)
|
||||
AvailableChannelsEnabled bool `json:"available_channels_enabled"`
|
||||
|
||||
// Subscription feature switch: gates the whole user-facing subscription surface
|
||||
// (sidebar entries, purchase-page subscription tab, header badge, /subscriptions route).
|
||||
SubscriptionEnabled bool `json:"subscription_enabled"`
|
||||
|
||||
// Model Plaza feature (public group/model pricing showcase)
|
||||
ModelPlazaEnabled bool `json:"model_plaza_enabled"`
|
||||
ModelPlazaRequireAuth bool `json:"model_plaza_require_auth"`
|
||||
@@ -402,7 +408,11 @@ type PublicSettings struct {
|
||||
GoogleOAuthEnabled bool `json:"google_oauth_enabled"`
|
||||
BackendModeEnabled bool `json:"backend_mode_enabled"`
|
||||
PaymentEnabled bool `json:"payment_enabled"`
|
||||
Version string `json:"version"`
|
||||
// PaymentBalanceDisabled mirrors the payment-config BALANCE_PAYMENT_DISABLED switch so the
|
||||
// user shell can derive the site billing mode (recharge & subscription / recharge only /
|
||||
// subscription only) before any authenticated checkout call.
|
||||
PaymentBalanceDisabled bool `json:"payment_balance_disabled"`
|
||||
Version string `json:"version"`
|
||||
// 服务器全局时区(IANA 名称与当前 UTC 偏移,如 "Asia/Shanghai" / "+08:00")。
|
||||
// 高峰时段等按服务器本地时间判定的窗口,前端展示时据此标注,避免用户按浏览器本地时间误读。
|
||||
ServerTimezone string `json:"server_timezone"`
|
||||
@@ -417,9 +427,12 @@ type PublicSettings struct {
|
||||
ChannelMonitorDefaultIntervalSeconds int `json:"channel_monitor_default_interval_seconds"`
|
||||
ChannelMonitorHideThroughput bool `json:"channel_monitor_hide_throughput"`
|
||||
ChannelMonitorShowQuota bool `json:"channel_monitor_show_quota"`
|
||||
ChannelMonitorHideUserRanking bool `json:"channel_monitor_hide_user_ranking"`
|
||||
|
||||
AvailableChannelsEnabled bool `json:"available_channels_enabled"`
|
||||
|
||||
SubscriptionEnabled bool `json:"subscription_enabled"`
|
||||
|
||||
ModelPlazaEnabled bool `json:"model_plaza_enabled"`
|
||||
ModelPlazaRequireAuth bool `json:"model_plaza_require_auth"`
|
||||
PluginManagementEnabled bool `json:"plugin_management_enabled"`
|
||||
|
||||
@@ -315,7 +315,7 @@ func GetUpstreamEndpoint(c *gin.Context, platform string) string {
|
||||
// OpenAI 转发服务维护独立的运行时端点上下文,覆盖普通入站推导。
|
||||
// 这对 force_chat_completions 的错误路径尤为重要:此时可能没有
|
||||
// ForwardResult,不能把入站 /v1/responses 误报成上游端点。
|
||||
if platform == service.PlatformOpenAI || platform == service.PlatformGrok || service.IsCNProvider(platform) {
|
||||
if platform == service.PlatformOpenAI || platform == service.PlatformGrok || service.IsMultiProtocolAPIKeyProvider(platform) {
|
||||
if endpoint := service.GetActualOpenAIUpstreamEndpoint(c); endpoint != "" {
|
||||
return endpoint
|
||||
}
|
||||
|
||||
@@ -233,6 +233,18 @@ func TestResolveOpenAIUpstreamEndpointPrefersForwardResult(t *testing.T) {
|
||||
result: &service.OpenAIForwardResult{},
|
||||
want: EndpointResponses,
|
||||
},
|
||||
{
|
||||
name: "opencode go conversion result reports responses",
|
||||
account: &service.Account{Platform: service.PlatformOpenCodeGo, Type: service.AccountTypeAPIKey},
|
||||
result: &service.OpenAIForwardResult{UpstreamEndpoint: EndpointResponses},
|
||||
want: EndpointResponses,
|
||||
},
|
||||
{
|
||||
name: "opencode go empty result without runtime stays inbound",
|
||||
account: &service.Account{Platform: service.PlatformOpenCodeGo, Type: service.AccountTypeAPIKey},
|
||||
result: &service.OpenAIForwardResult{},
|
||||
want: EndpointChatCompletions,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
@@ -1116,8 +1116,8 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// Models handles listing available models
|
||||
// GET /v1/models
|
||||
// Models lists visible models, or retrieves the exact list entry for a model path parameter.
|
||||
// GET /v1/models and /v1/models/:model (also exposed through root aliases)
|
||||
// Returns models based on account configurations (model_mapping whitelist)
|
||||
// Falls back to default models if no whitelist is configured
|
||||
func (h *GatewayHandler) Models(c *gin.Context) {
|
||||
@@ -1173,18 +1173,12 @@ func (h *GatewayHandler) Models(c *gin.Context) {
|
||||
|
||||
// Fallback to default models
|
||||
if platform == service.PlatformOpenAI {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"object": "list",
|
||||
"data": openai.DefaultModels,
|
||||
})
|
||||
writeModelsListResponse(c, openai.DefaultModels)
|
||||
return
|
||||
}
|
||||
|
||||
if platform == service.PlatformGemini {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"object": "list",
|
||||
"data": geminicli.DefaultModels,
|
||||
})
|
||||
writeModelsListResponse(c, geminicli.DefaultModels)
|
||||
return
|
||||
}
|
||||
if platform == service.PlatformGrok {
|
||||
@@ -1192,10 +1186,7 @@ func (h *GatewayHandler) Models(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"object": "list",
|
||||
"data": claude.DefaultModels,
|
||||
})
|
||||
writeModelsListResponse(c, claude.DefaultModels)
|
||||
}
|
||||
|
||||
// CodexModels returns the effective group model list using the manifest shape
|
||||
@@ -1278,12 +1269,12 @@ func (h *GatewayHandler) compositeAvailableModels(ctx context.Context, groupID *
|
||||
seen := make(map[string]struct{})
|
||||
models := make([]string, 0)
|
||||
schedulablePlatforms := h.gatewayService.GetSchedulablePlatforms(ctx, groupID)
|
||||
for _, platform := range []string{service.PlatformAnthropic, service.PlatformGemini, service.PlatformOpenAI, service.PlatformAntigravity, service.PlatformGrok, service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek} {
|
||||
for _, platform := range []string{service.PlatformAnthropic, service.PlatformGemini, service.PlatformOpenAI, service.PlatformAntigravity, service.PlatformGrok, service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek, service.PlatformMiniMax, service.PlatformOpenCodeGo} {
|
||||
platformModels := h.gatewayService.GetAvailableModels(ctx, groupID, platform)
|
||||
if len(platformModels) == 0 {
|
||||
// CN 供应商没有静态默认模型列表(defaultModelIDsForPlatform 的
|
||||
// default 分支是 Claude 列表),composite 下只暴露账号映射键。
|
||||
if _, ok := schedulablePlatforms[platform]; ok && !service.IsCNProvider(platform) {
|
||||
if _, ok := schedulablePlatforms[platform]; ok && !service.IsMultiProtocolAPIKeyProvider(platform) {
|
||||
platformModels = defaultModelIDsForPlatform(platform)
|
||||
}
|
||||
}
|
||||
@@ -1320,10 +1311,7 @@ func writeModelsList(c *gin.Context, platform string, modelIDs []string) {
|
||||
CreatedAt: "2024-01-01T00:00:00Z",
|
||||
})
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"object": "list",
|
||||
"data": models,
|
||||
})
|
||||
writeModelsListResponse(c, models)
|
||||
}
|
||||
|
||||
func writeAllowlistedModelsList(c *gin.Context, platform string, modelIDs []string) {
|
||||
@@ -1382,10 +1370,7 @@ func writeGrokModelsList(c *gin.Context, modelIDs []string) {
|
||||
models = append(models, item)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"object": "list",
|
||||
"data": models,
|
||||
})
|
||||
writeModelsListResponse(c, models)
|
||||
}
|
||||
|
||||
func grokModelSupportsConfigurableReasoning(modelID string) bool {
|
||||
@@ -1418,10 +1403,7 @@ func writeOpenAIModelsList(c *gin.Context, modelIDs []string) {
|
||||
DisplayName: modelID,
|
||||
})
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"object": "list",
|
||||
"data": models,
|
||||
})
|
||||
writeModelsListResponse(c, models)
|
||||
}
|
||||
|
||||
// modelListingSource 汇总模型列表过滤的候选来源:账号映射键(availableModels)
|
||||
@@ -1440,7 +1422,9 @@ func modelListingSource(platform string, availableModels, fallbackModels []strin
|
||||
func defaultCodexModelIDsForPlatform(platform string) []string {
|
||||
switch platform {
|
||||
case service.PlatformDeepseek:
|
||||
return []string{"deepseek-v4-pro", "deepseek-v4-flash"}
|
||||
return []string{"deepseek-v4-pro", "deepseek-v4-flash", "deepseek-flash"}
|
||||
case service.PlatformMiniMax:
|
||||
return []string{"MiniMax-M3", "MiniMax-M2.7", "MiniMax-M2.5"}
|
||||
default:
|
||||
return defaultModelIDsForPlatform(platform)
|
||||
}
|
||||
@@ -1467,10 +1451,12 @@ func defaultModelIDsForPlatform(platform string) []string {
|
||||
return claude.DefaultModelIDs()
|
||||
case service.PlatformGrok:
|
||||
return xai.DefaultModelIDs()
|
||||
case service.PlatformOpenCodeGo:
|
||||
return service.DefaultOpenCodeGoModelIDs()
|
||||
case service.PlatformComposite:
|
||||
ids := make([]string, 0)
|
||||
seen := make(map[string]struct{})
|
||||
for _, concretePlatform := range []string{service.PlatformAnthropic, service.PlatformGemini, service.PlatformOpenAI, service.PlatformAntigravity, service.PlatformGrok, service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek} {
|
||||
for _, concretePlatform := range []string{service.PlatformAnthropic, service.PlatformGemini, service.PlatformOpenAI, service.PlatformAntigravity, service.PlatformGrok, service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek, service.PlatformMiniMax, service.PlatformOpenCodeGo} {
|
||||
for _, id := range defaultModelIDsForPlatform(concretePlatform) {
|
||||
if _, ok := seen[id]; ok {
|
||||
continue
|
||||
|
||||
@@ -89,6 +89,10 @@ func claudeCodeBodyMapFromParsedRequest(parsedReq *service.ParsedRequest) map[st
|
||||
bodyMap := map[string]any{
|
||||
"model": parsedReq.Model,
|
||||
}
|
||||
// 探测识别(max_tokens=1)需要看到该字段,复用已解析请求时一并带上。
|
||||
if parsedReq.MaxTokens > 0 {
|
||||
bodyMap["max_tokens"] = parsedReq.MaxTokens
|
||||
}
|
||||
if parsedReq.HasSystem {
|
||||
if system, ok := parsedReq.SystemValue(); ok {
|
||||
bodyMap["system"] = system
|
||||
|
||||
@@ -500,3 +500,19 @@ type helperConcurrencyCacheStubWithError struct {
|
||||
func (s *helperConcurrencyCacheStubWithError) AcquireAccountSlot(ctx context.Context, accountID int64, maxConcurrency int, requestID string) (bool, error) {
|
||||
return false, s.err
|
||||
}
|
||||
|
||||
func TestSetClaudeCodeClientContext_ParsedRequestProbeWithoutSystemPrompt(t *testing.T) {
|
||||
c, _ := newHelperTestContext(http.MethodPost, "/v1/messages")
|
||||
c.Request.Header.Set("User-Agent", "claude-cli/2.1.260 (external, cli)")
|
||||
|
||||
// The hot path reuses ParsedRequest instead of re-parsing the body; the probe
|
||||
// marker must survive that projection or the exemption only works cold.
|
||||
parsed := &service.ParsedRequest{Model: "claude-sonnet-4-5", MaxTokens: 1}
|
||||
SetClaudeCodeClientContext(c, nil, parsed)
|
||||
require.True(t, service.IsClaudeCodeClient(c.Request.Context()))
|
||||
|
||||
c2, _ := newHelperTestContext(http.MethodPost, "/v1/messages")
|
||||
c2.Request.Header.Set("User-Agent", "claude-cli/2.1.260 (external, cli)")
|
||||
SetClaudeCodeClientContext(c2, nil, &service.ParsedRequest{Model: "claude-sonnet-4-5", MaxTokens: 64})
|
||||
require.False(t, service.IsClaudeCodeClient(c2.Request.Context()))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func requestModelForTest(h *GatewayHandler, group *service.Group, modelID, etag string) *httptest.ResponseRecorder {
|
||||
rec := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/v1/models", nil)
|
||||
c.Request.Header.Set("If-None-Match", etag)
|
||||
if modelID != "" {
|
||||
c.Params = gin.Params{{Key: "model", Value: modelID}}
|
||||
}
|
||||
c.Set(string(middleware2.ContextKeyAPIKey), &service.APIKey{GroupID: &group.ID, Group: group})
|
||||
h.Models(c)
|
||||
return rec
|
||||
}
|
||||
|
||||
func TestRetrieveModelMatchesVisibleCatalogue(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
for _, platform := range []string{service.PlatformOpenAI, service.PlatformAnthropic, service.PlatformGemini, service.PlatformGrok, service.PlatformComposite} {
|
||||
for _, mapped := range []bool{false, true} {
|
||||
name := platform + "/fallback"
|
||||
if mapped {
|
||||
name = platform + "/mapped"
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
accountPlatform := platform
|
||||
if platform == service.PlatformComposite {
|
||||
accountPlatform = service.PlatformOpenAI
|
||||
}
|
||||
credentials := map[string]any{}
|
||||
if mapped {
|
||||
credentials["model_mapping"] = map[string]any{"custom-model": "upstream-only-model"}
|
||||
}
|
||||
group := &service.Group{ID: 71, Platform: platform}
|
||||
h := newGatewayModelsHandlerForTest(&gatewayModelsAccountRepoStub{byGroup: map[int64][]service.Account{
|
||||
group.ID: {{ID: 1, Platform: accountPlatform, Type: service.AccountTypeAPIKey, Status: service.StatusActive, Schedulable: true, Credentials: credentials}},
|
||||
}})
|
||||
list := requestModelForTest(h, group, "", "")
|
||||
require.Equal(t, http.StatusOK, list.Code, list.Body.String())
|
||||
var catalog struct {
|
||||
Data []json.RawMessage `json:"data"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(list.Body.Bytes(), &catalog))
|
||||
require.NotEmpty(t, catalog.Data)
|
||||
var model struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(catalog.Data[0], &model))
|
||||
retrieved := requestModelForTest(h, group, model.ID, "")
|
||||
require.Equal(t, http.StatusOK, retrieved.Code, retrieved.Body.String())
|
||||
require.JSONEq(t, string(catalog.Data[0]), retrieved.Body.String())
|
||||
require.Equal(t, http.StatusNotFound, requestModelForTest(h, group, "unknown-model", "").Code)
|
||||
group.ModelAllowlist = service.GroupModelAllowlist{Enabled: true, Models: []string{"absent-from-source"}}
|
||||
hidden := requestModelForTest(h, group, model.ID, "")
|
||||
require.Equal(t, http.StatusNotFound, hidden.Code, hidden.Body.String())
|
||||
require.Contains(t, hidden.Body.String(), `"code":"model_not_found"`)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetrievePinnedModelPreservesMetadataFilteringAndErrors(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
status int
|
||||
selected []string
|
||||
wantStatus int
|
||||
}{
|
||||
{name: "metadata", wantStatus: http.StatusOK},
|
||||
{name: "hidden", selected: []string{"other-model"}, wantStatus: http.StatusNotFound},
|
||||
{name: "upstream failure", status: http.StatusServiceUnavailable, wantStatus: http.StatusBadGateway},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
upstream := &codexModelsPinnedHTTPUpstream{bodies: map[int64]string{
|
||||
2: `{"data":[{"id":"special-model","ID":"not-a-model-id","owned_by":"source-owner","created":123,"extra":{"context":999}}]}`,
|
||||
}, statuses: map[int64]int{}}
|
||||
if tc.status != 0 {
|
||||
upstream.statuses[2] = tc.status
|
||||
}
|
||||
codex := newPinnedCodexTestHandler([]service.Account{newPinnedCodexAccount(2, service.StatusActive, true, false)}, upstream, 3)
|
||||
h := &GatewayHandler{openAIGatewayService: codex.gatewayService, maxAccountSwitches: 3}
|
||||
group := &service.Group{ID: 72, Platform: service.PlatformOpenAI,
|
||||
ModelAllowlist: service.GroupModelAllowlist{Enabled: len(tc.selected) > 0, Models: tc.selected},
|
||||
CodexModelsManifestConfig: service.GroupCodexModelsManifestConfig{Enabled: true, AccountIDs: []int64{2}}}
|
||||
got := requestModelForTest(h, group, "special-model", "collection-etag")
|
||||
require.Equal(t, tc.wantStatus, got.Code, got.Body.String())
|
||||
if tc.wantStatus == http.StatusOK {
|
||||
list := requestModelForTest(h, group, "", "")
|
||||
var catalog struct {
|
||||
Data []json.RawMessage `json:"data"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(list.Body.Bytes(), &catalog))
|
||||
require.JSONEq(t, string(catalog.Data[0]), got.Body.String())
|
||||
require.Contains(t, got.Body.String(), `"owned_by":"source-owner"`)
|
||||
require.Contains(t, got.Body.String(), `"created":123`)
|
||||
again := requestModelForTest(h, group, "special-model", list.Header().Get("ETag"))
|
||||
require.Equal(t, http.StatusOK, again.Code)
|
||||
require.Empty(t, again.Header().Get("ETag"))
|
||||
require.Equal(t, http.StatusNotFound, requestModelForTest(h, group, "not-a-model-id", "").Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -791,6 +791,13 @@ func TestGatewayModels_CompositeCustomModelsListFiltersAcrossConcretePlatforms(t
|
||||
"model_mapping": map[string]any{"deepseek-custom": "deepseek-upstream"},
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: 7,
|
||||
Platform: service.PlatformMiniMax,
|
||||
Credentials: map[string]any{
|
||||
"model_mapping": map[string]any{"minimax-custom": "MiniMax-M3"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -805,7 +812,7 @@ func TestGatewayModels_CompositeCustomModelsListFiltersAcrossConcretePlatforms(t
|
||||
Platform: service.PlatformComposite,
|
||||
ModelAllowlist: service.GroupModelAllowlist{
|
||||
Enabled: true,
|
||||
Models: []string{"gemini-2.5-flash", "missing-model", "ag-custom-model", "gpt-5.5", "kimi-custom", "glm-custom", "deepseek-custom"},
|
||||
Models: []string{"gemini-2.5-flash", "missing-model", "ag-custom-model", "gpt-5.5", "kimi-custom", "glm-custom", "deepseek-custom", "minimax-custom"},
|
||||
},
|
||||
},
|
||||
})
|
||||
@@ -816,7 +823,7 @@ func TestGatewayModels_CompositeCustomModelsListFiltersAcrossConcretePlatforms(t
|
||||
|
||||
var got gatewayModelsResponseForTest
|
||||
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got))
|
||||
require.Equal(t, []string{"gemini-2.5-flash", "ag-custom-model", "gpt-5.5", "kimi-custom", "glm-custom", "deepseek-custom"}, modelIDsForTest(got.Data))
|
||||
require.Equal(t, []string{"gemini-2.5-flash", "ag-custom-model", "gpt-5.5", "kimi-custom", "glm-custom", "deepseek-custom", "minimax-custom"}, modelIDsForTest(got.Data))
|
||||
}
|
||||
|
||||
func TestGatewayModels_CompositeUnmappedAccountsFallbackToLinkedPlatformsOnly(t *testing.T) {
|
||||
@@ -869,6 +876,7 @@ func TestGatewayModels_CompositeUnmappedCNAccountsContributeNoDefaults(t *testin
|
||||
{ID: 2, Platform: service.PlatformKimi},
|
||||
{ID: 3, Platform: service.PlatformZhipu},
|
||||
{ID: 4, Platform: service.PlatformDeepseek},
|
||||
{ID: 5, Platform: service.PlatformMiniMax},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -900,13 +908,14 @@ func TestDefaultModelIDsForPlatform_CNProvidersKeepClaudeDefaults(t *testing.T)
|
||||
for _, model := range claude.DefaultModels {
|
||||
want = append(want, model.ID)
|
||||
}
|
||||
for _, platform := range []string{service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek} {
|
||||
for _, platform := range []string{service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek, service.PlatformMiniMax} {
|
||||
require.Equal(t, want, defaultModelIDsForPlatform(platform), "platform=%s", platform)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultCodexModelIDsForPlatform_DeepSeekUsesDeepSeekModels(t *testing.T) {
|
||||
require.Equal(t, []string{"deepseek-v4-pro", "deepseek-v4-flash"}, defaultCodexModelIDsForPlatform(service.PlatformDeepseek))
|
||||
require.Equal(t, []string{"deepseek-v4-pro", "deepseek-v4-flash", "deepseek-flash"}, defaultCodexModelIDsForPlatform(service.PlatformDeepseek))
|
||||
require.Equal(t, []string{"MiniMax-M3", "MiniMax-M2.7", "MiniMax-M2.5"}, defaultCodexModelIDsForPlatform(service.PlatformMiniMax))
|
||||
require.Equal(t, defaultModelIDsForPlatform(service.PlatformAnthropic), defaultCodexModelIDsForPlatform(service.PlatformAnthropic))
|
||||
}
|
||||
|
||||
|
||||
@@ -198,30 +198,57 @@ func (h *OpenAIGatewayHandler) handleGrokMedia(c *gin.Context, endpoint service.
|
||||
}
|
||||
routingStart := time.Now()
|
||||
requiredCapability := grokMediaRequiredCapability(endpoint)
|
||||
var accountReleaseFunc func()
|
||||
releaseAccount := func() {
|
||||
if accountReleaseFunc != nil {
|
||||
accountReleaseFunc()
|
||||
accountReleaseFunc = nil
|
||||
}
|
||||
}
|
||||
defer releaseAccount()
|
||||
|
||||
for {
|
||||
releaseAccount()
|
||||
if failoverClientGone(c) {
|
||||
return
|
||||
}
|
||||
selection, scheduleDecision, err := h.gatewayService.SelectAccountWithSchedulerForCapability(
|
||||
requestCtx,
|
||||
apiKey.GroupID,
|
||||
"",
|
||||
sessionHash,
|
||||
routingModel,
|
||||
failedAccountIDs,
|
||||
service.OpenAIUpstreamTransportHTTPSSE,
|
||||
requiredCapability,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
service.PlatformGrok,
|
||||
)
|
||||
var selection *service.AccountSelectionResult
|
||||
var scheduleDecision service.OpenAIAccountScheduleDecision
|
||||
if boundLookupAccountID > 0 {
|
||||
selection, scheduleDecision, err = h.gatewayService.SelectGrokMediaVideoRequestAccount(
|
||||
requestCtx, apiKey.GroupID, sessionHash, boundLookupAccountID, routingModel,
|
||||
)
|
||||
} else {
|
||||
selection, scheduleDecision, err = h.gatewayService.SelectAccountWithSchedulerForCapability(
|
||||
requestCtx,
|
||||
apiKey.GroupID,
|
||||
"",
|
||||
sessionHash,
|
||||
routingModel,
|
||||
failedAccountIDs,
|
||||
service.OpenAIUpstreamTransportHTTPSSE,
|
||||
requiredCapability,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
service.PlatformGrok,
|
||||
)
|
||||
}
|
||||
// Own an eagerly acquired slot before any rejection or eligibility probe.
|
||||
// Forwarding takes over the same once-only release after admission.
|
||||
if selection != nil && selection.Acquired {
|
||||
selection.ReleaseFunc = wrapReleaseOnDone(requestCtx, selection.ReleaseFunc)
|
||||
accountReleaseFunc = selection.ReleaseFunc
|
||||
}
|
||||
if err != nil {
|
||||
if failoverClientGone(c) {
|
||||
reqLog.Info("grok_media.account_select_aborted_client_disconnected", zap.Error(err))
|
||||
return
|
||||
}
|
||||
if boundLookupAccountID > 0 && errors.Is(err, service.ErrNoAvailableAccounts) {
|
||||
h.errorResponse(c, http.StatusNotFound, "not_found_error", "Video request not found")
|
||||
return
|
||||
}
|
||||
reqLog.Warn("grok_media.account_select_failed",
|
||||
zap.Error(err),
|
||||
zap.Int("excluded_account_count", len(failedAccountIDs)),
|
||||
@@ -260,6 +287,9 @@ func (h *OpenAIGatewayHandler) handleGrokMedia(c *gin.Context, endpoint service.
|
||||
h.errorResponse(c, cls.Status, cls.ErrType, cls.Message)
|
||||
return
|
||||
}
|
||||
if failoverClientGone(c) {
|
||||
return
|
||||
}
|
||||
if boundLookupAccountID > 0 && selection.Account.ID != boundLookupAccountID {
|
||||
reqLog.Warn("grok_media.video_lookup_bound_account_unavailable",
|
||||
zap.Int64("bound_account_id", boundLookupAccountID),
|
||||
@@ -282,6 +312,7 @@ func (h *OpenAIGatewayHandler) handleGrokMedia(c *gin.Context, endpoint service.
|
||||
if endpoint.IsGenerationRequest() {
|
||||
eligible, eligibilityReason, eligibilityErr := h.ensureGrokMediaAccountEligibility(requestCtx, account)
|
||||
if !eligible {
|
||||
releaseAccount()
|
||||
mediaEligibilityRejected = true
|
||||
failedAccountIDs[account.ID] = struct{}{}
|
||||
reqLog.Warn("grok_media.account_eligibility_rejected",
|
||||
@@ -298,10 +329,19 @@ func (h *OpenAIGatewayHandler) handleGrokMedia(c *gin.Context, endpoint service.
|
||||
continue
|
||||
}
|
||||
}
|
||||
if failoverClientGone(c) {
|
||||
return
|
||||
}
|
||||
sessionHash = ensureOpenAIPoolModeSessionHash(sessionHash, account)
|
||||
setOpsSelectedAccount(c, account.ID, account.Platform)
|
||||
|
||||
accountReleaseFunc, slotResult := h.acquireResponsesAccountSlot(c, apiKey.GroupID, sessionHash, selection, false, &streamStarted, reqLog)
|
||||
admissionSessionHash := sessionHash
|
||||
if boundLookupAccountID > 0 {
|
||||
// Waiting must not replace the video ownership TTL with text sticky TTL.
|
||||
admissionSessionHash = ""
|
||||
}
|
||||
var slotResult openAISlotAcquireResult
|
||||
accountReleaseFunc, slotResult = h.acquireResponsesAccountSlot(c, apiKey.GroupID, admissionSessionHash, selection, false, &streamStarted, reqLog)
|
||||
if slotResult == openAISlotAcquireProfitVetoed {
|
||||
// 媒体路径已显式豁免利润门(suppress 标记),此分支仅防御性兜底,
|
||||
// 同样受否决上限约束。
|
||||
@@ -319,11 +359,7 @@ func (h *OpenAIGatewayHandler) handleGrokMedia(c *gin.Context, endpoint service.
|
||||
forwardStart := time.Now()
|
||||
writerSizeBeforeForward := c.Writer.Size()
|
||||
result, err := func() (*service.OpenAIForwardResult, error) {
|
||||
defer func() {
|
||||
if accountReleaseFunc != nil {
|
||||
accountReleaseFunc()
|
||||
}
|
||||
}()
|
||||
defer releaseAccount()
|
||||
return h.gatewayService.ForwardGrokMedia(requestCtx, c, account, endpoint, requestID, body, contentType)
|
||||
}()
|
||||
|
||||
|
||||
@@ -0,0 +1,405 @@
|
||||
//go:build unit
|
||||
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/config"
|
||||
middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
"github.com/Wei-Shaw/sub2api/internal/testutil"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type grokMediaSlotsCache struct {
|
||||
testutil.StubConcurrencyCache
|
||||
mu sync.Mutex
|
||||
accounts map[string]int64
|
||||
users map[string]int64
|
||||
acquired, released, userAcquired, userReleased int
|
||||
waiting, denied int
|
||||
full bool
|
||||
queueFull bool
|
||||
onWait func()
|
||||
duplicateRelease bool
|
||||
peak int
|
||||
}
|
||||
|
||||
func (s *grokMediaSlotsCache) AcquireAccountSlot(_ context.Context, id int64, _ int, request string) (bool, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.full || s.denied > 0 {
|
||||
if s.denied > 0 {
|
||||
s.denied--
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
s.accounts[request] = id
|
||||
s.acquired++
|
||||
if len(s.accounts) > s.peak {
|
||||
s.peak = len(s.accounts)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *grokMediaSlotsCache) ReleaseAccountSlot(_ context.Context, _ int64, request string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if _, ok := s.accounts[request]; !ok {
|
||||
s.duplicateRelease = true
|
||||
}
|
||||
delete(s.accounts, request)
|
||||
s.released++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *grokMediaSlotsCache) AcquireUserSlot(_ context.Context, id int64, _ int, request string) (bool, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.users[request] = id
|
||||
s.userAcquired++
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *grokMediaSlotsCache) ReleaseUserSlot(_ context.Context, _ int64, request string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if _, ok := s.users[request]; !ok {
|
||||
s.duplicateRelease = true
|
||||
}
|
||||
delete(s.users, request)
|
||||
s.userReleased++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *grokMediaSlotsCache) IncrementAccountWaitCount(context.Context, int64, int) (bool, error) {
|
||||
s.mu.Lock()
|
||||
if s.queueFull {
|
||||
s.mu.Unlock()
|
||||
return false, nil
|
||||
}
|
||||
s.waiting++
|
||||
onWait := s.onWait
|
||||
s.mu.Unlock()
|
||||
if onWait != nil {
|
||||
onWait()
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *grokMediaSlotsCache) DecrementAccountWaitCount(context.Context, int64) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.waiting--
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *grokMediaSlotsCache) assertReleased(t *testing.T) {
|
||||
t.Helper()
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
require.Empty(t, s.accounts)
|
||||
require.Empty(t, s.users)
|
||||
require.Equal(t, s.acquired, s.released)
|
||||
require.Equal(t, s.userAcquired, s.userReleased)
|
||||
require.False(t, s.duplicateRelease)
|
||||
require.Zero(t, s.waiting)
|
||||
}
|
||||
|
||||
type grokMediaSlotBindings struct {
|
||||
testutil.StubGatewayCache
|
||||
owner int64
|
||||
writes int
|
||||
key string
|
||||
billed map[string]bool
|
||||
}
|
||||
|
||||
func (s *grokMediaSlotBindings) GetSessionAccountID(_ context.Context, groupID int64, key string) (int64, error) {
|
||||
if groupID != 24 || key != s.key {
|
||||
return 0, service.ErrStickySessionNotFound
|
||||
}
|
||||
return s.owner, nil
|
||||
}
|
||||
func (s *grokMediaSlotBindings) SetSessionAccountID(_ context.Context, _ int64, key string, owner int64, _ time.Duration) error {
|
||||
s.key, s.owner = key, owner
|
||||
s.writes++
|
||||
return nil
|
||||
}
|
||||
func (s *grokMediaSlotBindings) RefreshSessionTTL(context.Context, int64, string, time.Duration) error {
|
||||
s.writes++
|
||||
return nil
|
||||
}
|
||||
func (s *grokMediaSlotBindings) DeleteSessionAccountID(context.Context, int64, string) error {
|
||||
s.writes++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *grokMediaSlotBindings) ClaimGrokVideoBilled(_ context.Context, key string, _ time.Duration) (bool, error) {
|
||||
if s.billed == nil {
|
||||
s.billed = make(map[string]bool)
|
||||
}
|
||||
if s.billed[key] {
|
||||
return false, nil
|
||||
}
|
||||
s.billed[key] = true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
type grokMediaSlotRepo struct {
|
||||
openAIImagesFailoverAccountRepo
|
||||
mismatch bool
|
||||
}
|
||||
|
||||
func (r grokMediaSlotRepo) GetByID(ctx context.Context, id int64) (*service.Account, error) {
|
||||
if r.mismatch {
|
||||
return r.openAIImagesFailoverAccountRepo.GetByID(ctx, 2)
|
||||
}
|
||||
return r.openAIImagesFailoverAccountRepo.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
type grokMediaSlotUpstream struct {
|
||||
service.HTTPUpstream
|
||||
call func(*http.Request, int64) (*http.Response, error)
|
||||
calls int
|
||||
}
|
||||
|
||||
func (u *grokMediaSlotUpstream) Do(req *http.Request, _ string, id int64, _ int) (*http.Response, error) {
|
||||
u.calls++
|
||||
return u.call(req, id)
|
||||
}
|
||||
|
||||
type grokMediaSlotProber func(context.Context, int64) (bool, string, error)
|
||||
|
||||
func (p grokMediaSlotProber) ProbeMediaEligibility(ctx context.Context, id int64) (bool, string, error) {
|
||||
return p(ctx, id)
|
||||
}
|
||||
|
||||
func newGrokMediaSlotHandler(t *testing.T, oauth, mismatch bool) (*OpenAIGatewayHandler, *grokMediaSlotsCache, *grokMediaSlotBindings, *grokMediaSlotUpstream) {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
accounts := make([]service.Account, 3)
|
||||
for i := range accounts {
|
||||
accounts[i] = service.Account{ID: int64(i + 1), Platform: service.PlatformGrok, Type: service.AccountTypeAPIKey,
|
||||
Status: service.StatusActive, Schedulable: true, Concurrency: 50, Priority: i,
|
||||
GroupIDs: []int64{24}, Credentials: map[string]any{"api_key": "test-key", "access_token": "test-token"}}
|
||||
if oauth {
|
||||
accounts[i].Type = service.AccountTypeOAuth
|
||||
accounts[i].Credentials["refresh_token"] = "test-refresh"
|
||||
accounts[i].Credentials["expires_at"] = time.Now().Add(7 * 24 * time.Hour).UTC().Format(time.RFC3339)
|
||||
}
|
||||
}
|
||||
slots := &grokMediaSlotsCache{accounts: map[string]int64{}, users: map[string]int64{}}
|
||||
concurrency := service.NewConcurrencyService(slots)
|
||||
bindings := &grokMediaSlotBindings{owner: 1}
|
||||
upstream := &grokMediaSlotUpstream{call: func(*http.Request, int64) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
Body: io.NopCloser(strings.NewReader(`{"request_id":"task","status":"pending"}`))}, nil
|
||||
}}
|
||||
cfg := &config.Config{RunMode: config.RunModeSimple}
|
||||
cfg.Gateway.Scheduling.StickySessionWaitTimeout = 20 * time.Millisecond
|
||||
cfg.Gateway.Scheduling.StickySessionMaxWaiting = 3
|
||||
cfg.Gateway.OpenAIScheduler.StickyEscapeEnabled = true
|
||||
repo := grokMediaSlotRepo{openAIImagesFailoverAccountRepo: openAIImagesFailoverAccountRepo{accounts: accounts}, mismatch: mismatch}
|
||||
provider := service.NewGrokTokenProvider(repo, nil)
|
||||
if oauth {
|
||||
_, err := provider.GetAccessToken(context.Background(), &accounts[1])
|
||||
require.NoError(t, err)
|
||||
}
|
||||
gateway := service.NewOpenAIGatewayService(repo, nil, nil, nil, nil, nil, bindings, cfg, nil, concurrency, nil, nil, nil, upstream, nil, nil, provider, nil, nil, nil, nil, nil)
|
||||
groupID := int64(24)
|
||||
require.NoError(t, gateway.BindGrokMediaVideoRequestAccount(context.Background(), &groupID, "task", 10, 20, 1))
|
||||
bindings.writes = 0
|
||||
billing := service.NewBillingCacheService(nil, nil, nil, nil, nil, nil, cfg, nil)
|
||||
t.Cleanup(billing.Stop)
|
||||
handler := NewOpenAIGatewayHandler(gateway, concurrency, billing, service.NewAPIKeyService(nil, nil, nil, nil, nil, nil, cfg), nil, nil, nil, nil, cfg)
|
||||
return handler, slots, bindings, upstream
|
||||
}
|
||||
|
||||
func grokMediaSlotContext(ctx context.Context, generation bool) (*gin.Context, *httptest.ResponseRecorder) {
|
||||
groupID := int64(24)
|
||||
method, path, body := http.MethodGet, "/v1/videos/task", ""
|
||||
if generation {
|
||||
method, path, body = http.MethodPost, "/v1/videos/generations", `{"model":"grok-imagine-video","prompt":"test","duration":6}`
|
||||
}
|
||||
req := httptest.NewRequest(method, path, strings.NewReader(body)).WithContext(ctx)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if !generation {
|
||||
req.Header.Set("session-id", "unrelated-client-session")
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = req
|
||||
c.Params = gin.Params{{Key: "request_id", Value: "task"}}
|
||||
c.Set(string(middleware2.ContextKeyAPIKey), &service.APIKey{ID: 20, UserID: 10, GroupID: &groupID,
|
||||
Group: &service.Group{ID: groupID, Platform: service.PlatformGrok, AllowImageGeneration: true}, User: &service.User{ID: 10}})
|
||||
c.Set(string(middleware2.ContextKeyUser), middleware2.AuthSubject{UserID: 10, Concurrency: 5})
|
||||
return c, w
|
||||
}
|
||||
|
||||
func TestGrokMediaLookupSlotLifecycle(t *testing.T) {
|
||||
for _, scenario := range []string{"normal", "mismatch acquired", "mismatch wait", "full", "queue full", "cancel while waiting", "wait then acquired", "upstream error", "cancel", "panic"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
h, slots, bindings, upstream := newGrokMediaSlotHandler(t, false, strings.HasPrefix(scenario, "mismatch"))
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
if scenario == "full" || scenario == "mismatch wait" || scenario == "queue full" || scenario == "cancel while waiting" {
|
||||
slots.full = true
|
||||
}
|
||||
if scenario == "queue full" {
|
||||
slots.queueFull = true
|
||||
}
|
||||
if scenario == "cancel while waiting" {
|
||||
slots.onWait = cancel
|
||||
}
|
||||
if scenario == "wait then acquired" {
|
||||
slots.denied = 1
|
||||
}
|
||||
original := upstream.call
|
||||
upstream.call = func(req *http.Request, id int64) (*http.Response, error) {
|
||||
require.Equal(t, int64(1), id)
|
||||
switch scenario {
|
||||
case "upstream error":
|
||||
return nil, errors.New("upstream unavailable")
|
||||
case "cancel":
|
||||
cancel()
|
||||
return nil, context.Canceled
|
||||
case "panic":
|
||||
panic("test upstream panic")
|
||||
}
|
||||
return original(req, id)
|
||||
}
|
||||
for range 20 {
|
||||
c, w := grokMediaSlotContext(ctx, false)
|
||||
h.GrokVideoStatus(c)
|
||||
slots.assertReleased(t)
|
||||
if strings.HasPrefix(scenario, "mismatch") {
|
||||
require.Equal(t, 404, w.Code)
|
||||
}
|
||||
if scenario == "normal" || scenario == "wait then acquired" {
|
||||
require.Equal(t, 200, w.Code)
|
||||
}
|
||||
if scenario == "full" || scenario == "queue full" {
|
||||
require.Equal(t, http.StatusTooManyRequests, w.Code)
|
||||
}
|
||||
}
|
||||
require.Zero(t, bindings.writes, "lookups must preserve owner and TTL")
|
||||
if scenario == "mismatch acquired" {
|
||||
require.Equal(t, 20, slots.released)
|
||||
}
|
||||
if slots.full {
|
||||
require.Zero(t, slots.released)
|
||||
require.Zero(t, upstream.calls)
|
||||
}
|
||||
if scenario == "full" || strings.HasPrefix(scenario, "mismatch") {
|
||||
require.Zero(t, upstream.calls)
|
||||
}
|
||||
switch scenario {
|
||||
case "normal", "wait then acquired", "upstream error", "panic":
|
||||
require.Equal(t, 20, upstream.calls)
|
||||
require.Equal(t, 20, slots.released)
|
||||
case "cancel":
|
||||
require.Equal(t, 1, upstream.calls)
|
||||
require.Equal(t, 1, slots.released)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrokMediaEligibilityReleasesBeforeSwitch(t *testing.T) {
|
||||
for _, scenario := range []string{"switch", "exhausted", "cancel during probe"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
h, slots, _, upstream := newGrokMediaSlotHandler(t, true, false)
|
||||
h.maxAccountSwitches = 1
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
probes := 0
|
||||
h.grokMediaEligibilityProber = grokMediaSlotProber(func(context.Context, int64) (bool, string, error) {
|
||||
probes++
|
||||
if scenario == "cancel during probe" {
|
||||
cancel()
|
||||
require.Eventually(t, func() bool { slots.mu.Lock(); defer slots.mu.Unlock(); return len(slots.accounts) == 0 }, time.Second, time.Millisecond)
|
||||
}
|
||||
if scenario == "switch" && probes == 2 {
|
||||
return true, "eligible", nil
|
||||
}
|
||||
return false, "billing_unobserved", errors.New("probe failed")
|
||||
})
|
||||
c, w := grokMediaSlotContext(ctx, true)
|
||||
h.GrokVideoGeneration(c)
|
||||
slots.assertReleased(t)
|
||||
require.Equal(t, 1, slots.peak, "rejected attempts must release before the next selection")
|
||||
if scenario == "switch" {
|
||||
events, _ := c.Get(service.OpsUpstreamErrorsKey)
|
||||
detail, err := json.Marshal(events)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 200, w.Code, "body=%s probes=%d upstream=%d events=%s", w.Body.String(), probes, upstream.calls, detail)
|
||||
require.Equal(t, 1, upstream.calls)
|
||||
}
|
||||
if scenario == "exhausted" {
|
||||
require.Equal(t, 503, w.Code)
|
||||
require.Equal(t, 2, slots.released)
|
||||
require.Zero(t, upstream.calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrokMediaVideoLookupOwnerIsolation(t *testing.T) {
|
||||
for _, other := range []string{"user", "api key", "group", "task"} {
|
||||
t.Run(other, func(t *testing.T) {
|
||||
h, slots, bindings, upstream := newGrokMediaSlotHandler(t, false, false)
|
||||
c, w := grokMediaSlotContext(context.Background(), false)
|
||||
key, ok := middleware2.GetAPIKeyFromContext(c)
|
||||
require.True(t, ok)
|
||||
switch other {
|
||||
case "user":
|
||||
c.Set(string(middleware2.ContextKeyUser), middleware2.AuthSubject{UserID: 11, Concurrency: 5})
|
||||
case "api key":
|
||||
key.ID = 21
|
||||
case "group":
|
||||
groupID := int64(25)
|
||||
key.GroupID = &groupID
|
||||
case "task":
|
||||
c.Params = gin.Params{{Key: "request_id", Value: "other-task"}}
|
||||
}
|
||||
h.GrokVideoStatus(c)
|
||||
require.Equal(t, http.StatusNotFound, w.Code)
|
||||
slots.assertReleased(t)
|
||||
require.Zero(t, slots.acquired)
|
||||
require.Zero(t, upstream.calls)
|
||||
require.Zero(t, bindings.writes)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrokMediaVideoCompletionStillClaimsBillingOnce(t *testing.T) {
|
||||
h, _, bindings, _ := newGrokMediaSlotHandler(t, false, false)
|
||||
c, _ := grokMediaSlotContext(context.Background(), false)
|
||||
key, ok := middleware2.GetAPIKeyFromContext(c)
|
||||
require.True(t, ok)
|
||||
subject := middleware2.AuthSubject{UserID: 10, Concurrency: 5}
|
||||
result := &service.OpenAIForwardResult{ResponseID: "task", Model: "grok-imagine-video",
|
||||
VideoCount: 1, VideoDurationSeconds: 6}
|
||||
for i := range 20 {
|
||||
bill := prepareGrokVideoCompletionBilling(c.Request.Context(), h, zap.NewNop(), key, subject, "task", result)
|
||||
if i == 0 {
|
||||
require.NotNil(t, bill)
|
||||
require.Equal(t, service.StableGrokVideoBillingRequestID("task"), bill.RequestID)
|
||||
} else {
|
||||
require.Nil(t, bill)
|
||||
}
|
||||
}
|
||||
require.Len(t, bindings.billed, 1)
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
// 广场路由挂 OptionalJWT 中间件:匿名可访问(除非 require_auth 开启),带 token 则
|
||||
// 识别用户。可见性规则(橱窗语义,与「可用渠道」的可绑定语义不同):
|
||||
// - 匿名:仅非专属分组(订阅型照常展示);
|
||||
// - 登录:非专属分组 + user_allowed_groups 授权的专属分组(不检查订阅有效性);
|
||||
// - 登录:非专属分组 + user_allowed_groups 授权或持有有效订阅的专属分组;
|
||||
// 若该用户开启了公开分组限制,则公开分组同样需要落在授权集合内。
|
||||
type ModelPlazaHandler struct {
|
||||
plazaService *service.ModelPlazaService
|
||||
@@ -160,7 +160,7 @@ func (h *ModelPlazaHandler) Get(c *gin.Context) {
|
||||
}
|
||||
|
||||
// filterPlazaVisibleGroups 按登录态裁剪分组可见性。
|
||||
// allowedGroups == nil 表示匿名(仅非专属);非 nil 表示登录(非专属 + 授权专属)。
|
||||
// allowedGroups == nil 表示匿名(仅非专属);非 nil 包含普通授权及有效订阅分组。
|
||||
// restrictPublicGroups 为 true 时,公开分组也必须落在 allowedGroups 内,否则用户会
|
||||
// 在广场看到自己实际绑定不了的分组。
|
||||
func filterPlazaVisibleGroups(
|
||||
|
||||
@@ -244,3 +244,17 @@ func TestToModelPlazaGroupDTO_TimePricing(t *testing.T) {
|
||||
weekdaysTP := weekdaysModel["time_pricing"].(map[string]any)
|
||||
require.Equal(t, true, weekdaysTP["weekdays_only"])
|
||||
}
|
||||
|
||||
func TestFilterPlazaVisibleGroups_SubscribedExclusiveGroup(t *testing.T) {
|
||||
groups := []service.PlazaGroup{
|
||||
{ID: 42, IsExclusive: true, SubscriptionType: "subscription"},
|
||||
{ID: 43, IsExclusive: true, SubscriptionType: "subscription"},
|
||||
{ID: 44, IsExclusive: true, SubscriptionType: "standard"},
|
||||
}
|
||||
require.Empty(t, filterPlazaVisibleGroups(groups, nil, false))
|
||||
for _, restricted := range []bool{false, true} {
|
||||
visible := filterPlazaVisibleGroups(groups, map[int64]struct{}{42: {}}, restricted)
|
||||
require.Len(t, visible, 1)
|
||||
require.Equal(t, int64(42), visible[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,3 +166,44 @@ func TestNormalizeCodexAutomationBootstrapPreservesOrderAndIsIdempotent(t *testi
|
||||
require.False(t, changedAgain)
|
||||
require.Equal(t, got, again)
|
||||
}
|
||||
|
||||
func TestNormalizeCodexAutomationBootstrapFullHeartbeat(t *testing.T) {
|
||||
const output = `<heartbeat>
|
||||
<automation_id>flutter</automation_id>
|
||||
<current_time_iso>2026-09-09T00:33:34.775Z</current_time_iso>
|
||||
<instructions>
|
||||
Read the reference index and report changes. Preserve A & B and <tags>.
|
||||
</instructions>
|
||||
</heartbeat>`
|
||||
body := codexAutomationBootstrapBody(t, output, "")
|
||||
got, changed := normalizeCodexAutomationBootstrap(body)
|
||||
require.True(t, changed)
|
||||
require.Equal(t, "user", gjson.GetBytes(got, "input.0.role").String())
|
||||
require.Equal(t, output, gjson.GetBytes(got, "input.0.content.0.text").String())
|
||||
again, changedAgain := normalizeCodexAutomationBootstrap(got)
|
||||
require.False(t, changedAgain)
|
||||
require.Equal(t, got, again)
|
||||
|
||||
for _, field := range []string{"automation_id", "current_time_iso", "instructions"} {
|
||||
duplicate := strings.Replace(output, "</heartbeat>", "<"+field+">duplicate</"+field+"></heartbeat>", 1)
|
||||
require.False(t, validCodexAutomationHeartbeat(duplicate), field)
|
||||
}
|
||||
for _, invalid := range []string{
|
||||
strings.Replace(output, "2026-09-09T00:33:34.775Z", "yesterday", 1),
|
||||
strings.Replace(output, "<current_time_iso>2026-09-09T00:33:34.775Z</current_time_iso>", "", 1),
|
||||
strings.Replace(output, "Read the reference index and report changes. Preserve A & B and <tags>.", " ", 1),
|
||||
strings.Replace(output, "<instructions>", `<instructions source="other">`, 1),
|
||||
strings.Replace(output, "<instructions>", "<instructions><nested/>", 1),
|
||||
} {
|
||||
require.False(t, validCodexAutomationHeartbeat(invalid))
|
||||
}
|
||||
for _, guarded := range [][]byte{
|
||||
codexAutomationBootstrapBody(t, output, `,"call_id":"call-1"`),
|
||||
[]byte(strings.Replace(string(body), `"model":"gpt-5"`, `"model":"gpt-5","previous_response_id":"resp-1"`, 1)),
|
||||
[]byte(strings.Replace(string(body), `"namespace":"codex_app"`, `"namespace":"other"`, 1)),
|
||||
} {
|
||||
unchanged, normalized := normalizeCodexAutomationBootstrap(guarded)
|
||||
require.False(t, normalized)
|
||||
require.Equal(t, guarded, unchanged)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestOpenAIEnsureForwardErrorResponse_SkipsCanceledClient(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
recorder := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(recorder)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
c.Request = httptest.NewRequest(http.MethodPost, EndpointResponses, nil).WithContext(ctx)
|
||||
cancel()
|
||||
|
||||
h := &OpenAIGatewayHandler{}
|
||||
require.False(t, h.ensureForwardErrorResponse(c, true))
|
||||
require.Equal(t, statusClientClosedRequest, c.Writer.Status())
|
||||
require.Empty(t, recorder.Body.String())
|
||||
}
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
func TestAllowOpenAICompatibleMessagesDispatch_CNProvidersExempt(t *testing.T) {
|
||||
require.True(t, allowOpenAICompatibleMessagesDispatch(nil, nil), "无 key 保持放行")
|
||||
|
||||
for _, platform := range []string{service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek, service.PlatformGrok} {
|
||||
for _, platform := range []string{service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek, service.PlatformMiniMax, service.PlatformOpenCodeGo, service.PlatformGrok} {
|
||||
apiKey := &service.APIKey{Group: &service.Group{Platform: platform, AllowMessagesDispatch: false}}
|
||||
require.True(t, allowOpenAICompatibleMessagesDispatch(nil, apiKey),
|
||||
"%s 分组必须豁免 allow_messages_dispatch 闸门", platform)
|
||||
@@ -74,4 +74,11 @@ func TestResolveOpenAIMessagesDispatchMappedModel_CompositeCNTargetsSkipGroupMap
|
||||
|
||||
require.Empty(t, resolveOpenAIMessagesDispatchMappedModel(c, apiKey, "claude-sonnet-4-5-20250929"), "model=%s", model)
|
||||
}
|
||||
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest("POST", "/v1/messages", nil)
|
||||
c.Request = c.Request.WithContext(service.WithResolvedTargetPlatform(c.Request.Context(), service.PlatformOpenCodeGo))
|
||||
apiKey := &service.APIKey{Group: &service.Group{Platform: service.PlatformComposite}}
|
||||
require.Empty(t, resolveOpenAIMessagesDispatchMappedModel(c, apiKey, "claude-sonnet-4-5-20250929"),
|
||||
"composite → opencode_go 不得注入 openai 默认 gpt-5.x 映射")
|
||||
}
|
||||
|
||||
@@ -198,11 +198,11 @@ func resolveOpenAIMessagesDispatchMappedModel(c *gin.Context, apiKey *service.AP
|
||||
if apiKey == nil || apiKey.Group == nil {
|
||||
return ""
|
||||
}
|
||||
// composite 解析到 grok/CN 目标时调度级映射不适用(Group 级映射的 gpt-5.x
|
||||
// 默认值是 openai 专属,发给这些上游必错),模型改写交给账号级 model_mapping。
|
||||
// composite 解析到 grok/CN/OpenCode 目标时调度级映射不适用(Group 级映射的
|
||||
// gpt-5.x 默认值是 openai 专属,发给这些上游必错),模型改写交给账号级 model_mapping。
|
||||
if apiKey.Group.Platform == service.PlatformComposite && c != nil && c.Request != nil {
|
||||
if platform, ok := service.ResolvedTargetPlatformFromContext(c.Request.Context()); ok &&
|
||||
(platform == service.PlatformGrok || service.IsCNProvider(platform)) {
|
||||
(platform == service.PlatformGrok || service.IsMultiProtocolAPIKeyProvider(platform)) {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
@@ -305,14 +305,14 @@ func allowOpenAICompatibleMessagesDispatch(c *gin.Context, apiKey *service.APIKe
|
||||
// 协议账号原生直通 Claude Code),无需 allow_messages_dispatch 开关授权——
|
||||
// 该开关对非 openai/composite 平台恒被 sanitizeGroupMessagesDispatchFields 置 false,
|
||||
// 若不豁免,CN 分组将永远 403。
|
||||
if service.IsCNProvider(apiKey.Group.Platform) {
|
||||
if service.IsMultiProtocolAPIKeyProvider(apiKey.Group.Platform) {
|
||||
return true
|
||||
}
|
||||
// composite 分组解析到 grok/CN 目标时与对应独立分组同语义豁免;
|
||||
// composite 分组解析到 grok/CN/OpenCode Go 目标时与对应独立分组同语义豁免;
|
||||
// 解析到 openai 目标则受 composite 分组自身的可配置开关控制。
|
||||
if apiKey.Group.Platform == service.PlatformComposite && c != nil && c.Request != nil {
|
||||
if platform, ok := service.ResolvedTargetPlatformFromContext(c.Request.Context()); ok &&
|
||||
(platform == service.PlatformGrok || service.IsCNProvider(platform)) {
|
||||
(platform == service.PlatformGrok || service.IsMultiProtocolAPIKeyProvider(platform)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -322,7 +322,8 @@ func allowOpenAICompatibleMessagesDispatch(c *gin.Context, apiKey *service.APIKe
|
||||
func openAICompatibleTextTargetAllowed(c *gin.Context, apiKey *service.APIKey, model string) bool {
|
||||
return compositeTargetPlatformAllowed(c, apiKey, model,
|
||||
service.PlatformOpenAI, service.PlatformGrok,
|
||||
service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek)
|
||||
service.PlatformKimi, service.PlatformZhipu, service.PlatformDeepseek,
|
||||
service.PlatformMiniMax, service.PlatformOpenCodeGo)
|
||||
}
|
||||
|
||||
// isResponsesWebSocketCompositePlatform 限定 composite 分组在 Responses WebSocket
|
||||
@@ -835,6 +836,22 @@ func (h *OpenAIGatewayHandler) Responses(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
if result != nil && result.ClientDisconnect {
|
||||
reqLog.Info("openai.client_disconnected",
|
||||
zap.Int64("account_id", account.ID),
|
||||
zap.Error(err),
|
||||
)
|
||||
submitResponsesUsage(result)
|
||||
return
|
||||
}
|
||||
if failoverClientGone(c) {
|
||||
reqLog.Info("openai.client_disconnected",
|
||||
zap.Int64("account_id", account.ID),
|
||||
zap.Error(err),
|
||||
)
|
||||
submitResponsesUsage(result)
|
||||
return
|
||||
}
|
||||
if result != nil && result.ImageCount > 0 {
|
||||
reqLog.Warn("openai.forward_partial_error_with_image_result",
|
||||
zap.Int64("account_id", account.ID),
|
||||
@@ -1873,12 +1890,24 @@ func validCodexAutomationLastRun(value string) bool {
|
||||
func validCodexAutomationHeartbeat(value string) bool {
|
||||
decoder := xml.NewDecoder(strings.NewReader(value))
|
||||
var rootSeen, automationIDSeen bool
|
||||
var automationID bytes.Buffer
|
||||
var childName string
|
||||
var childText bytes.Buffer
|
||||
fields := make(map[string]string)
|
||||
depth := 0
|
||||
for {
|
||||
token, err := decoder.Token()
|
||||
if err == io.EOF {
|
||||
id := automationID.String()
|
||||
id := fields["automation_id"]
|
||||
timestamp, hasTime := fields["current_time_iso"]
|
||||
instructions, hasInstructions := fields["instructions"]
|
||||
if hasTime != hasInstructions {
|
||||
return false
|
||||
}
|
||||
if hasTime {
|
||||
if _, err := time.Parse(time.RFC3339Nano, timestamp); err != nil || strings.TrimSpace(instructions) == "" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return rootSeen && automationIDSeen && depth == 0 &&
|
||||
strings.TrimSpace(id) == id && validCodexAutomationID(id)
|
||||
}
|
||||
@@ -1896,21 +1925,33 @@ func validCodexAutomationHeartbeat(value string) bool {
|
||||
return false
|
||||
}
|
||||
rootSeen = true
|
||||
} else if automationIDSeen || current.Name.Local != "automation_id" {
|
||||
return false
|
||||
} else {
|
||||
childName = current.Name.Local
|
||||
switch childName {
|
||||
case "automation_id", "current_time_iso", "instructions":
|
||||
default:
|
||||
return false
|
||||
}
|
||||
if _, duplicate := fields[childName]; duplicate {
|
||||
return false
|
||||
}
|
||||
childText.Reset()
|
||||
}
|
||||
automationIDSeen = depth == 2
|
||||
case xml.EndElement:
|
||||
if current.Name.Space != "" {
|
||||
return false
|
||||
}
|
||||
if depth == 2 {
|
||||
fields[childName] = childText.String()
|
||||
automationIDSeen = automationIDSeen || childName == "automation_id"
|
||||
}
|
||||
depth--
|
||||
if depth < 0 {
|
||||
return false
|
||||
}
|
||||
case xml.CharData:
|
||||
if depth == 2 {
|
||||
_, _ = automationID.Write(current)
|
||||
_, _ = childText.Write(current)
|
||||
} else if len(bytes.TrimSpace(current)) != 0 {
|
||||
return false
|
||||
}
|
||||
@@ -2976,7 +3017,7 @@ func (h *OpenAIGatewayHandler) ResponsesWebSocket(c *gin.Context) {
|
||||
|
||||
// WebSocket 首包可能很大,hash 必须在 hooks 外算成字符串,避免 AfterTurn 闭包保活请求体。
|
||||
requestPayloadHash = service.HashUsageRequestPayload(wsFirstMessage)
|
||||
if preemptCtx, cleanupPreempt, armed := h.gatewayService.BeginOpenAIWSIngressSessionPreemption(ctx, c, account, wsFirstMessage); armed {
|
||||
if preemptCtx, cleanupPreempt, armed := h.gatewayService.BeginOpenAIWSIngressSessionPreemptionWithClient(ctx, c, account, wsFirstMessage, wsConn); armed {
|
||||
ctx = preemptCtx
|
||||
defer cleanupPreempt()
|
||||
}
|
||||
@@ -2988,6 +3029,8 @@ func (h *OpenAIGatewayHandler) ResponsesWebSocket(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if service.IsOpenAIWSSessionPreemptedError(err) {
|
||||
// 关闭帧已由抢占登记在取消前发给本连接,这里只记录并释放。
|
||||
reqLog.Info("openai.websocket_ingress_preempted", zap.Int64("account_id", account.ID))
|
||||
return
|
||||
}
|
||||
var failoverErr *service.UpstreamFailoverError
|
||||
@@ -3528,6 +3571,10 @@ func (h *OpenAIGatewayHandler) ensureForwardErrorResponse(c *gin.Context, stream
|
||||
if c == nil || c.Writer == nil {
|
||||
return false
|
||||
}
|
||||
if c.Request != nil && errors.Is(c.Request.Context().Err(), context.Canceled) {
|
||||
failoverClientGone(c)
|
||||
return false
|
||||
}
|
||||
// 先停 compact 心跳再读 Writer 状态,避免与心跳 goroutine 竞争。
|
||||
compactKeepaliveCommitted := service.StopOpenAICompactSSEKeepaliveCommitted(c)
|
||||
if compactKeepaliveCommitted {
|
||||
|
||||
@@ -2960,7 +2960,7 @@ func runOpenAIResponsesWebSocketUsageLogCase(t *testing.T, tc openAIResponsesWSU
|
||||
BillingModelSource: tc.billingModelSource,
|
||||
}},
|
||||
groupPlatforms: map[int64]string{groupID: service.PlatformOpenAI},
|
||||
}, nil, nil, nil)
|
||||
}, nil, nil, nil, nil)
|
||||
}
|
||||
|
||||
billingCacheSvc := service.NewBillingCacheService(nil, nil, nil, nil, nil, nil, cfg, nil)
|
||||
|
||||
@@ -157,7 +157,7 @@ func TestOpenAIGatewayHandlerImages_ServerErrorFailsOverAndReturnsClearErrorWhen
|
||||
)
|
||||
handler.maxAccountSwitches = 10
|
||||
|
||||
body := []byte(`{"model":"gpt-image-2","prompt":"draw a cat","quality":"high","size":"1536x1024"}`)
|
||||
body := []byte(`{"model":"gpt-image-1","prompt":"draw a cat","quality":"high","size":"1536x1024"}`)
|
||||
core, observedLogs := observer.New(zap.DebugLevel)
|
||||
requestCtx := logger.IntoContext(context.Background(), zap.New(core))
|
||||
req := httptest.NewRequest(http.MethodPost, "/v1/images/generations", bytes.NewReader(body)).WithContext(requestCtx)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors"
|
||||
@@ -17,8 +19,12 @@ func (h *GatewayHandler) pinnedOpenAIModels(c *gin.Context, group *service.Group
|
||||
writeOpenAIModelsError(c, http.StatusInternalServerError, "api_error", "OpenAI model discovery is not configured")
|
||||
return
|
||||
}
|
||||
etag := c.GetHeader("If-None-Match")
|
||||
if c.Param("model") != "" {
|
||||
etag = "" // A collection ETag cannot validate a single-model representation.
|
||||
}
|
||||
response, account, err := h.openAIGatewayService.FetchPinnedOpenAIModelsList(
|
||||
c.Request.Context(), group, h.maxAccountSwitches, c.GetHeader("If-None-Match"),
|
||||
c.Request.Context(), group, h.maxAccountSwitches, etag,
|
||||
)
|
||||
if c.Request.Context().Err() != nil {
|
||||
return
|
||||
@@ -40,6 +46,10 @@ func writeOpenAIModelsError(c *gin.Context, status int, errorType, message strin
|
||||
}
|
||||
|
||||
func writeOpenAIModelsResponse(c *gin.Context, manifest *service.OpenAIModelsResponse) {
|
||||
if c.Param("model") != "" {
|
||||
writeRetrievedModel(c, manifest.Body)
|
||||
return
|
||||
}
|
||||
if manifest.ETag != "" {
|
||||
c.Header("ETag", manifest.ETag)
|
||||
}
|
||||
@@ -50,3 +60,50 @@ func writeOpenAIModelsResponse(c *gin.Context, manifest *service.OpenAIModelsRes
|
||||
}
|
||||
c.Data(http.StatusOK, "application/json", manifest.Body)
|
||||
}
|
||||
|
||||
// Both discovery endpoints consume the same final catalogue, after group/platform
|
||||
// selection and allowlist filtering. Preserve every field on the selected entry.
|
||||
func writeModelsListResponse(c *gin.Context, models any) {
|
||||
response := gin.H{"object": "list", "data": models}
|
||||
if c.Param("model") == "" {
|
||||
c.JSON(http.StatusOK, response)
|
||||
return
|
||||
}
|
||||
body, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
writeOpenAIModelsError(c, http.StatusInternalServerError, "api_error", "Failed to encode model catalogue")
|
||||
return
|
||||
}
|
||||
writeRetrievedModel(c, body)
|
||||
}
|
||||
|
||||
func writeRetrievedModel(c *gin.Context, body []byte) {
|
||||
var catalog struct {
|
||||
Data []json.RawMessage `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &catalog); err != nil {
|
||||
writeOpenAIModelsError(c, http.StatusBadGateway, "upstream_error", "Invalid model catalogue")
|
||||
return
|
||||
}
|
||||
modelID := c.Param("model")
|
||||
for _, raw := range catalog.Data {
|
||||
var model map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &model); err != nil {
|
||||
writeOpenAIModelsError(c, http.StatusBadGateway, "upstream_error", "Invalid model catalogue entry")
|
||||
return
|
||||
}
|
||||
var id string
|
||||
if err := json.Unmarshal(model["id"], &id); err != nil {
|
||||
writeOpenAIModelsError(c, http.StatusBadGateway, "upstream_error", "Invalid model catalogue ID")
|
||||
return
|
||||
}
|
||||
if id == modelID {
|
||||
c.Data(http.StatusOK, "application/json", raw)
|
||||
return
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": gin.H{
|
||||
"type": "invalid_request_error", "code": "model_not_found", "param": "model",
|
||||
"message": fmt.Sprintf("Model %q does not exist or is not available for this group", modelID),
|
||||
}})
|
||||
}
|
||||
|
||||
@@ -1124,8 +1124,12 @@ func OpsErrorLoggerMiddleware(ops *service.OpsService) gin.HandlerFunc {
|
||||
// A marked in-band error is a visible request failure even though its
|
||||
// wire status is already 200. Otherwise retain recovered attempts as a
|
||||
// provider-health row whose 2xx status keeps it outside request SLA.
|
||||
if len(service.GetOpsStreamErrors(c)) > 0 {
|
||||
if streamErrs := service.GetOpsStreamErrors(c); len(streamErrs) > 0 {
|
||||
logOpsStreamError(c, ops, status)
|
||||
// 请求级带内结果不承载上游归因,此前尝试的上游错误仍按恢复行记录。
|
||||
if opsStreamErrorsAllRequestScoped(streamErrs) {
|
||||
logOpsRecoveredUpstream(c, ops, status)
|
||||
}
|
||||
} else {
|
||||
logOpsRecoveredUpstream(c, ops, status)
|
||||
}
|
||||
@@ -1405,22 +1409,31 @@ func opsRequestTypeFromContext(c *gin.Context) *int16 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// logOpsStreamError 记录一次挂在已固化 HTTP 200 SSE 流上的就地错误。
|
||||
// 由于 wire 状态码停留在 200,常规的 status>=400 捕获路径永远不会触发;
|
||||
// handleStreamingAwareError 通过 service.MarkOpsStreamError 标记这类错误,
|
||||
// 此函数据此补记一条错误日志,让并发限流/流内失败在错误看板里可见。
|
||||
//
|
||||
// 仅在 status<400 且不存在上游错误上下文时调用:上游透传错误已由中间件的
|
||||
// upstream-context 分支落库,无需在此重复记录。
|
||||
// logOpsStreamError 记录挂在 2xx 响应上的带内错误(就地 SSE error 帧、非流式正文里的
|
||||
// 请求级结果等)。由于 wire 状态码停留在 2xx,常规的 status>=400 捕获路径不会触发;
|
||||
// 标记方通过 service.MarkOpsStreamError / MarkOpsStreamErrorValue 登记,此函数据此补记
|
||||
// 错误日志。上游错误上下文(若有)是否参与分类与归因由标记的 RequestScoped 决定。
|
||||
func logOpsStreamError(c *gin.Context, ops *service.OpsService, wireStatus int) {
|
||||
for _, streamErr := range service.GetOpsStreamErrors(c) {
|
||||
logOpsStreamErrorValue(c, ops, wireStatus, streamErr)
|
||||
}
|
||||
}
|
||||
|
||||
func opsStreamErrorsAllRequestScoped(streamErrs []service.OpsStreamError) bool {
|
||||
if len(streamErrs) == 0 {
|
||||
return false
|
||||
}
|
||||
for _, streamErr := range streamErrs {
|
||||
if !streamErr.RequestScoped {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func logOpsStreamErrorValue(c *gin.Context, ops *service.OpsService, wireStatus int, streamErr service.OpsStreamError) {
|
||||
// 命中 skip_monitoring=true 透传规则的请求跳过落库,与其它分支一致。
|
||||
if streamErr.SkipMonitoring || (streamErr.Turn == 0 && shouldSkipFinalOpsFailure(c)) {
|
||||
if streamErr.SkipMonitoring || (streamErr.Turn == 0 && !streamErr.RequestScoped && shouldSkipFinalOpsFailure(c)) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1435,9 +1448,19 @@ func logOpsStreamErrorValue(c *gin.Context, ops *service.OpsService, wireStatus
|
||||
classifyStatus = wireStatus
|
||||
}
|
||||
normalizedType := normalizeOpsErrorType(streamErr.ErrType, streamErr.Code)
|
||||
phase, isBusinessLimited, errorOwner, errorSource := classifyOpsErrorLog(c, normalizedType, streamErr.Message, streamErr.Code, classifyStatus)
|
||||
var phase, errorOwner, errorSource string
|
||||
var isBusinessLimited bool
|
||||
if streamErr.RequestScoped {
|
||||
// 请求级带内结果只按错误类型分类,此前尝试残留的上游错误上下文不参与判定。
|
||||
phase = classifyOpsPhase(normalizedType, streamErr.Message, streamErr.Code)
|
||||
isBusinessLimited = true
|
||||
errorOwner = classifyOpsErrorOwner(phase, streamErr.Message)
|
||||
errorSource = classifyOpsErrorSource(phase, streamErr.Message)
|
||||
} else {
|
||||
phase, isBusinessLimited, errorOwner, errorSource = classifyOpsErrorLog(c, normalizedType, streamErr.Message, streamErr.Code, classifyStatus)
|
||||
}
|
||||
recordedStatus := wireStatus
|
||||
if streamErr.CountTowardsSLA && streamErr.IntendedStatus >= 400 {
|
||||
if streamErr.IntendedStatus >= 400 && (streamErr.CountTowardsSLA || streamErr.RequestScoped) {
|
||||
recordedStatus = streamErr.IntendedStatus
|
||||
}
|
||||
errorBody := ""
|
||||
@@ -1488,8 +1511,8 @@ func logOpsStreamErrorValue(c *gin.Context, ops *service.OpsService, wireStatus
|
||||
}
|
||||
return ""
|
||||
}(),
|
||||
// 就地 SSE 错误只出现在流式请求上。
|
||||
Stream: true,
|
||||
// 带内错误默认挂在 SSE 流上;NonStream 标记的来自非流式 2xx 响应体。
|
||||
Stream: !streamErr.NonStream,
|
||||
InboundEndpoint: GetInboundEndpoint(c),
|
||||
UpstreamEndpoint: GetUpstreamEndpoint(c, platform),
|
||||
RequestedModel: modelName,
|
||||
@@ -1530,8 +1553,10 @@ func logOpsStreamErrorValue(c *gin.Context, ops *service.OpsService, wireStatus
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
applyOpsLatencyFieldsFromContext(c, entry)
|
||||
applyOpsUpstreamFieldsFromContext(c, entry)
|
||||
if streamErr.Turn > 0 {
|
||||
if !streamErr.RequestScoped {
|
||||
applyOpsUpstreamFieldsFromContext(c, entry)
|
||||
}
|
||||
if streamErr.Turn > 0 && !streamErr.RequestScoped {
|
||||
applyOpsStreamErrorSnapshot(entry, streamErr)
|
||||
}
|
||||
|
||||
|
||||
@@ -1996,3 +1996,175 @@ func TestClassifyOpsLocalModelConfigurationWithoutIngressMarkStaysRouting(t *tes
|
||||
require.True(t, isBusinessLimited)
|
||||
require.Equal(t, "platform", errorOwner)
|
||||
}
|
||||
|
||||
// 带内错误也可能出现在非流式 2xx 响应体里(如 Gemini generateContent 的 finishReason):
|
||||
// NonStream 标记落库 stream=false,未标记的沿用流式。
|
||||
func TestLogOpsStreamError_NonStreamInBandContentPolicy(t *testing.T) {
|
||||
setupOpsErrorLogTestQueue(t, 4)
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
rec := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
c.Request = httptest.NewRequest(http.MethodPost, "/v1beta/models/gemini-3.7-flash:generateContent", nil)
|
||||
setOpsRequestContext(c, "gemini-3.7-flash", false)
|
||||
|
||||
service.MarkOpsStreamErrorValue(c, service.OpsStreamError{
|
||||
ErrType: "invalid_request_error",
|
||||
Code: "PROHIBITED_CONTENT",
|
||||
Message: "Gemini content policy stop (finishReason=PROHIBITED_CONTENT): Response stopped due to prohibited content.",
|
||||
IntendedStatus: http.StatusBadRequest,
|
||||
RequestScoped: true,
|
||||
NonStream: true,
|
||||
})
|
||||
|
||||
ops := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
|
||||
logOpsStreamError(c, ops, http.StatusOK)
|
||||
|
||||
job := <-opsErrorLogQueue
|
||||
require.NotNil(t, job.entry)
|
||||
require.False(t, job.entry.Stream)
|
||||
require.Equal(t, "invalid_request_error", job.entry.ErrorType)
|
||||
require.Equal(t, "request", job.entry.ErrorPhase)
|
||||
require.Equal(t, "client", job.entry.ErrorOwner)
|
||||
require.True(t, job.entry.IsBusinessLimited)
|
||||
require.Equal(t, http.StatusBadRequest, job.entry.StatusCode, "请求级带内结果按 IntendedStatus 落库,才能进入错误列表")
|
||||
require.Equal(t, "P3", job.entry.Severity)
|
||||
require.Equal(t, "gemini-3.7-flash", job.entry.Model)
|
||||
require.Contains(t, job.entry.ErrorBody, "PROHIBITED_CONTENT")
|
||||
}
|
||||
|
||||
// 请求级带内结果(RequestScoped):此前尝试残留的上游错误上下文不参与分类、不做上游归因,
|
||||
// 按业务限制计;透传规则的 skip_monitoring 残留也不影响它落库。
|
||||
func TestLogOpsStreamError_RequestScopedIgnoresResidualUpstreamContext(t *testing.T) {
|
||||
setupOpsErrorLogTestQueue(t, 4)
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
rec := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
c.Request = httptest.NewRequest(http.MethodPost, "/v1beta/models/gemini-3.7-flash:streamGenerateContent", nil)
|
||||
setOpsRequestContext(c, "gemini-3.7-flash", true)
|
||||
c.Set(service.OpsUpstreamStatusCodeKey, http.StatusTooManyRequests)
|
||||
c.Set(service.OpsUpstreamErrorMessageKey, "earlier attempt was rate limited")
|
||||
c.Set(service.OpsUpstreamErrorsKey, []*service.OpsUpstreamErrorEvent{{
|
||||
UpstreamStatusCode: http.StatusTooManyRequests,
|
||||
Message: "earlier attempt was rate limited",
|
||||
SkipMonitoring: true,
|
||||
}})
|
||||
|
||||
service.MarkOpsStreamErrorValue(c, service.OpsStreamError{
|
||||
ErrType: "invalid_request_error",
|
||||
Code: "PROHIBITED_CONTENT",
|
||||
Message: "Gemini content policy stop (finishReason=PROHIBITED_CONTENT): Response stopped due to prohibited content.",
|
||||
IntendedStatus: http.StatusBadRequest,
|
||||
RequestScoped: true,
|
||||
})
|
||||
|
||||
ops := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
|
||||
logOpsStreamError(c, ops, http.StatusOK)
|
||||
|
||||
require.Equal(t, int64(1), OpsErrorLogQueueLength())
|
||||
job := <-opsErrorLogQueue
|
||||
require.NotNil(t, job.entry)
|
||||
require.Equal(t, "invalid_request_error", job.entry.ErrorType)
|
||||
require.Equal(t, "request", job.entry.ErrorPhase)
|
||||
require.Equal(t, "client", job.entry.ErrorOwner)
|
||||
require.Equal(t, "client_request", job.entry.ErrorSource)
|
||||
require.True(t, job.entry.IsBusinessLimited)
|
||||
require.True(t, job.entry.Stream)
|
||||
require.Equal(t, http.StatusBadRequest, job.entry.StatusCode)
|
||||
require.Nil(t, job.entry.UpstreamStatusCode, "残留的 429 不得贴到内容策略行上")
|
||||
require.Nil(t, job.entry.UpstreamErrorMessage)
|
||||
require.Nil(t, job.entry.UpstreamErrorsJSON)
|
||||
require.Contains(t, job.entry.ErrorBody, "PROHIBITED_CONTENT")
|
||||
}
|
||||
|
||||
// 请求级带内结果与此前尝试的上游错误并存时,中间件既落带内错误行,也保留恢复行的 provider 遥测。
|
||||
func TestOpsErrorLoggerMiddleware_RequestScopedInBandErrorKeepsRecoveredTelemetry(t *testing.T) {
|
||||
setupOpsErrorLogTestQueue(t, 4)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
ops := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
|
||||
router := gin.New()
|
||||
router.Use(OpsErrorLoggerMiddleware(ops))
|
||||
router.POST("/v1beta/models/gemini-3.7-flash:generateContent", func(c *gin.Context) {
|
||||
setOpsRequestContext(c, "gemini-3.7-flash", false)
|
||||
c.Set(service.OpsUpstreamErrorsKey, []*service.OpsUpstreamErrorEvent{{
|
||||
UpstreamStatusCode: http.StatusTooManyRequests,
|
||||
Message: "earlier attempt was rate limited",
|
||||
}})
|
||||
service.MarkOpsStreamErrorValue(c, service.OpsStreamError{
|
||||
ErrType: "invalid_request_error",
|
||||
Code: "SAFETY",
|
||||
Message: "Gemini content policy stop (finishReason=SAFETY): Response stopped due to safety reasons.",
|
||||
IntendedStatus: http.StatusBadRequest,
|
||||
RequestScoped: true,
|
||||
NonStream: true,
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"candidates": []any{}})
|
||||
})
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
router.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/v1beta/models/gemini-3.7-flash:generateContent", nil))
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
require.Equal(t, int64(2), OpsErrorLogQueueLength())
|
||||
inBand := <-opsErrorLogQueue
|
||||
require.Equal(t, "invalid_request_error", inBand.entry.ErrorType)
|
||||
require.Equal(t, "request", inBand.entry.ErrorPhase)
|
||||
require.Equal(t, "client", inBand.entry.ErrorOwner)
|
||||
require.True(t, inBand.entry.IsBusinessLimited)
|
||||
require.False(t, inBand.entry.Stream)
|
||||
require.Equal(t, http.StatusBadRequest, inBand.entry.StatusCode)
|
||||
require.Nil(t, inBand.entry.UpstreamStatusCode)
|
||||
require.Nil(t, inBand.entry.UpstreamErrorsJSON)
|
||||
|
||||
recovered := <-opsErrorLogQueue
|
||||
require.Equal(t, "upstream", recovered.entry.ErrorPhase)
|
||||
require.Equal(t, "provider", recovered.entry.ErrorOwner)
|
||||
require.Equal(t, http.StatusOK, recovered.entry.StatusCode)
|
||||
require.Equal(t, "Recovered upstream error 429: earlier attempt was rate limited", recovered.entry.ErrorMessage)
|
||||
}
|
||||
|
||||
// 非请求级带内失败(如上游错误信封)仍与既有语义一致:上游上下文参与分类并做归因,只落一行。
|
||||
func TestOpsErrorLoggerMiddleware_UpstreamInBandFailureStillSingleRow(t *testing.T) {
|
||||
setupOpsErrorLogTestQueue(t, 4)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
ops := service.NewOpsService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
|
||||
router := gin.New()
|
||||
router.Use(OpsErrorLoggerMiddleware(ops))
|
||||
router.POST("/v1beta/models/gemini-3.7-flash:streamGenerateContent", func(c *gin.Context) {
|
||||
setOpsRequestContext(c, "gemini-3.7-flash", true)
|
||||
service.SetOpsUpstreamError(c, http.StatusTooManyRequests, "quota", "")
|
||||
c.Set(service.OpsUpstreamErrorsKey, []*service.OpsUpstreamErrorEvent{{
|
||||
UpstreamStatusCode: http.StatusTooManyRequests,
|
||||
Message: "quota",
|
||||
Kind: "stream_failed",
|
||||
}})
|
||||
service.MarkOpsStreamFailure(c, "rate_limit_error", "RESOURCE_EXHAUSTED", "quota", http.StatusTooManyRequests)
|
||||
c.Data(http.StatusOK, "text/event-stream", []byte("data: {\"error\":{\"code\":429}}\n\n"))
|
||||
})
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
router.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/v1beta/models/gemini-3.7-flash:streamGenerateContent", nil))
|
||||
|
||||
require.Equal(t, int64(1), OpsErrorLogQueueLength())
|
||||
job := <-opsErrorLogQueue
|
||||
require.Equal(t, "rate_limit_error", job.entry.ErrorType)
|
||||
require.Equal(t, "upstream", job.entry.ErrorPhase)
|
||||
require.Equal(t, "provider", job.entry.ErrorOwner)
|
||||
require.Equal(t, http.StatusTooManyRequests, job.entry.StatusCode)
|
||||
require.True(t, job.entry.Stream)
|
||||
require.NotNil(t, job.entry.UpstreamStatusCode)
|
||||
require.Equal(t, http.StatusTooManyRequests, *job.entry.UpstreamStatusCode)
|
||||
}
|
||||
|
||||
// Gemini 带内信号登记使用的错误类型必须都在 ops 白名单里,否则会被归一化成 api_error。
|
||||
func TestNormalizeOpsErrorType_KeepsGeminiInBandSignalTypes(t *testing.T) {
|
||||
for _, errType := range []string{
|
||||
"invalid_request_error", "authentication_error", "permission_error",
|
||||
"not_found_error", "rate_limit_error", "upstream_error",
|
||||
} {
|
||||
require.Equal(t, errType, normalizeOpsErrorType(errType, "PROHIBITED_CONTENT"), errType)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,6 +95,7 @@ func (h *SettingHandler) GetPublicSettings(c *gin.Context) {
|
||||
GoogleOAuthEnabled: settings.GoogleOAuthEnabled,
|
||||
BackendModeEnabled: settings.BackendModeEnabled,
|
||||
PaymentEnabled: settings.PaymentEnabled,
|
||||
PaymentBalanceDisabled: settings.PaymentBalanceDisabled,
|
||||
Version: h.version,
|
||||
ServerTimezone: timezone.Name(),
|
||||
ServerUTCOffset: timezone.UTCOffset(),
|
||||
@@ -108,8 +109,10 @@ func (h *SettingHandler) GetPublicSettings(c *gin.Context) {
|
||||
ChannelMonitorDefaultIntervalSeconds: settings.ChannelMonitorDefaultIntervalSeconds,
|
||||
ChannelMonitorHideThroughput: settings.ChannelMonitorHideThroughput,
|
||||
ChannelMonitorShowQuota: settings.ChannelMonitorShowQuota,
|
||||
ChannelMonitorHideUserRanking: settings.ChannelMonitorHideUserRanking,
|
||||
|
||||
AvailableChannelsEnabled: settings.AvailableChannelsEnabled,
|
||||
SubscriptionEnabled: settings.SubscriptionEnabled,
|
||||
|
||||
ModelPlazaEnabled: settings.ModelPlazaEnabled,
|
||||
ModelPlazaRequireAuth: settings.ModelPlazaRequireAuth,
|
||||
|
||||
@@ -44,6 +44,8 @@ const (
|
||||
PlatformKimi = domain.PlatformKimi
|
||||
PlatformZhipu = domain.PlatformZhipu
|
||||
PlatformDeepseek = domain.PlatformDeepseek
|
||||
PlatformMiniMax = domain.PlatformMiniMax
|
||||
PlatformOpenCodeGo = domain.PlatformOpenCodeGo
|
||||
)
|
||||
|
||||
// AllPlatforms 返回所有支持的平台列表
|
||||
@@ -57,6 +59,8 @@ func AllPlatforms() []string {
|
||||
PlatformKimi,
|
||||
PlatformZhipu,
|
||||
PlatformDeepseek,
|
||||
PlatformMiniMax,
|
||||
PlatformOpenCodeGo,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,5 +16,7 @@ func TestAllPlatformsIncludesEveryConcretePlatform(t *testing.T) {
|
||||
"kimi",
|
||||
"zhipu",
|
||||
"deepseek",
|
||||
"minimax",
|
||||
"opencode_go",
|
||||
}, AllPlatforms())
|
||||
}
|
||||
|
||||
@@ -202,7 +202,7 @@ func TestEasyPayCustomMethodsUseConfiguredUpstreamType(t *testing.T) {
|
||||
"notifyUrl": "https://example.com/notify",
|
||||
"returnUrl": "https://example.com/return",
|
||||
"paymentMode": paymentModePopup,
|
||||
"customMethods": `[{"type":"ldc","upstreamType":"epay","displayName":"LDC"},{"type":"usdt_trc20","upstreamType":"usdt","displayName":"USDT-TRC20"}]`,
|
||||
"customMethods": `[{"type":"ldc","upstreamType":"epay","displayName":"LDC"},{"type":"usdt_trc20","upstreamType":"usdt.trc20","displayName":"USDT-TRC20"}]`,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewEasyPay: %v", err)
|
||||
@@ -221,8 +221,8 @@ func TestEasyPayCustomMethodsUseConfiguredUpstreamType(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("parse pay url: %v", err)
|
||||
}
|
||||
if got := payURL.Query().Get("type"); got != "usdt" {
|
||||
t.Fatalf("pay url type = %q, want usdt (%s)", got, resp.PayURL)
|
||||
if got := payURL.Query().Get("type"); got != "usdt.trc20" {
|
||||
t.Fatalf("pay url type = %q, want usdt.trc20 (%s)", got, resp.PayURL)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -184,6 +184,16 @@ var geminiModels = []modelDef{
|
||||
{ID: "gemini-3.6-flash-low", DisplayName: "Gemini 3.6 Flash Low", CreatedAt: "2026-07-21T00:00:00Z", IsReasoning: true},
|
||||
{ID: "gemini-3.6-flash-medium", DisplayName: "Gemini 3.6 Flash Medium", CreatedAt: "2026-07-21T00:00:00Z", IsReasoning: true},
|
||||
{ID: "gemini-3.6-flash-tiered", DisplayName: "Gemini 3.6 Flash", CreatedAt: "2026-07-21T00:00:00Z", IsReasoning: true},
|
||||
{ID: "gemini-3.7-flash", DisplayName: "Gemini 3.7 Flash", CreatedAt: "2026-08-13T00:00:00Z"},
|
||||
{ID: "gemini-3.7-flash-high", DisplayName: "Gemini 3.7 Flash High", CreatedAt: "2026-08-13T00:00:00Z", IsReasoning: true},
|
||||
{ID: "gemini-3.7-flash-low", DisplayName: "Gemini 3.7 Flash Low", CreatedAt: "2026-08-13T00:00:00Z", IsReasoning: true},
|
||||
{ID: "gemini-3.7-flash-medium", DisplayName: "Gemini 3.7 Flash Medium", CreatedAt: "2026-08-13T00:00:00Z", IsReasoning: true},
|
||||
{ID: "gemini-3.7-flash-tiered", DisplayName: "Gemini 3.7 Flash", CreatedAt: "2026-08-13T00:00:00Z", IsReasoning: true},
|
||||
{ID: "gemini-3.8-flash", DisplayName: "Gemini 3.8 Flash", CreatedAt: "2026-09-02T00:00:00Z"},
|
||||
{ID: "gemini-3.8-flash-high", DisplayName: "Gemini 3.8 Flash High", CreatedAt: "2026-09-02T00:00:00Z", IsReasoning: true},
|
||||
{ID: "gemini-3.8-flash-low", DisplayName: "Gemini 3.8 Flash Low", CreatedAt: "2026-09-02T00:00:00Z", IsReasoning: true},
|
||||
{ID: "gemini-3.8-flash-medium", DisplayName: "Gemini 3.8 Flash Medium", CreatedAt: "2026-09-02T00:00:00Z", IsReasoning: true},
|
||||
{ID: "gemini-3.8-flash-tiered", DisplayName: "Gemini 3.8 Flash", CreatedAt: "2026-09-02T00:00:00Z", IsReasoning: true},
|
||||
{ID: "gemini-3-pro-preview", DisplayName: "Gemini 3 Pro Preview", CreatedAt: "2025-06-01T00:00:00Z", IsReasoning: true},
|
||||
{ID: "gemini-3-pro-image", DisplayName: "Gemini 3 Pro Image", CreatedAt: "2025-06-01T00:00:00Z"},
|
||||
}
|
||||
|
||||
@@ -26,6 +26,16 @@ func TestDefaultModels_ContainsNewAndLegacyImageModels(t *testing.T) {
|
||||
"gemini-3.6-flash-low",
|
||||
"gemini-3.6-flash-medium",
|
||||
"gemini-3.6-flash-tiered",
|
||||
"gemini-3.7-flash",
|
||||
"gemini-3.7-flash-high",
|
||||
"gemini-3.7-flash-low",
|
||||
"gemini-3.7-flash-medium",
|
||||
"gemini-3.7-flash-tiered",
|
||||
"gemini-3.8-flash",
|
||||
"gemini-3.8-flash-high",
|
||||
"gemini-3.8-flash-low",
|
||||
"gemini-3.8-flash-medium",
|
||||
"gemini-3.8-flash-tiered",
|
||||
}
|
||||
|
||||
for _, id := range requiredIDs {
|
||||
|
||||
@@ -88,11 +88,14 @@ func TransformClaudeToGeminiWithOptions(claudeReq *ClaudeRequest, projectID, map
|
||||
// 用于存储 tool_use id -> name 映射
|
||||
toolIDToName := make(map[string]string)
|
||||
|
||||
// 检测是否有 web_search 工具
|
||||
hasWebSearchTool := hasWebSearchTool(claudeReq.Tools)
|
||||
// 仅在「只有内置 web_search、没有客户端 function tools」时走 web_search 降级模型。
|
||||
// Antigravity v1internal 不支持内置工具与 functionDeclarations 混用(即使设置
|
||||
// includeServerSideToolInvocations 仍会 400,见 issue #6464),混用时会丢弃内置搜索,
|
||||
// 因此不能再强制切到 gemini-2.5-flash,否则 Codex 等带 shell 工具的请求会整单失败。
|
||||
useWebSearchRequest := hasWebSearchTool(claudeReq.Tools) && !hasClientFunctionTools(claudeReq.Tools)
|
||||
requestType := "agent"
|
||||
targetModel := mappedModel
|
||||
if hasWebSearchTool {
|
||||
if useWebSearchRequest {
|
||||
requestType = "web_search"
|
||||
if targetModel != webSearchFallbackModel {
|
||||
targetModel = webSearchFallbackModel
|
||||
@@ -147,22 +150,16 @@ func TransformClaudeToGeminiWithOptions(claudeReq *ClaudeRequest, projectID, map
|
||||
SessionID: generateStableSessionID(contents),
|
||||
}
|
||||
|
||||
// 针对 Gemini Reasoning 模型(如 gemini-3.1-pro-high等)过滤强制空 ToolConfig
|
||||
// toolConfig must always be present: upstream rejects requests without it,
|
||||
// including reasoning models called without any tools.
|
||||
// 总是设置 toolConfig,与官方客户端一致
|
||||
// 总是设置 toolConfig,与官方客户端一致。
|
||||
// 注意:buildTools 会在客户端 function tools 存在时丢弃 googleSearch/codeExecution
|
||||
// (issue #6464),因此这里不再注入 includeServerSideToolInvocations。
|
||||
innerRequest.ToolConfig = &GeminiToolConfig{
|
||||
FunctionCallingConfig: &GeminiFunctionCallingConfig{
|
||||
Mode: "VALIDATED",
|
||||
},
|
||||
}
|
||||
// 内置工具(googleSearch)与函数调用混用时,上游要求显式开启
|
||||
// includeServerSideToolInvocations,否则返回 400(issue #5709)。
|
||||
// 与 raw 透传路的 enableMixedGeminiToolInvocations 注入保持同一语义。
|
||||
if hasMixedToolInvocations(tools) {
|
||||
enabled := true
|
||||
innerRequest.ToolConfig.IncludeServerSideToolInvocations = &enabled
|
||||
}
|
||||
|
||||
if systemInstruction != nil {
|
||||
innerRequest.SystemInstruction = systemInstruction
|
||||
@@ -696,6 +693,20 @@ func hasWebSearchTool(tools []ClaudeTool) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// hasClientFunctionTools 判断是否存在可转发的客户端 function/custom 工具。
|
||||
// 内置 web_search / code_execution 不算客户端工具。
|
||||
func hasClientFunctionTools(tools []ClaudeTool) bool {
|
||||
for _, tool := range tools {
|
||||
if isWebSearchTool(tool) || isCodeExecutionTool(tool) {
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(tool.Name) != "" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isWebSearchTool(tool ClaudeTool) bool {
|
||||
if strings.HasPrefix(tool.Type, "web_search") || tool.Type == "google_search" {
|
||||
return true
|
||||
@@ -714,21 +725,6 @@ func isCodeExecutionTool(tool ClaudeTool) bool {
|
||||
return strings.TrimSpace(tool.Type) == "code_execution"
|
||||
}
|
||||
|
||||
// hasMixedToolInvocations 判断构建后的工具声明是否同时包含函数声明与内置工具
|
||||
// (googleSearch)。仅在两者并存时需要开启 includeServerSideToolInvocations。
|
||||
func hasMixedToolInvocations(declarations []GeminiToolDeclaration) bool {
|
||||
hasFunc, hasBuiltin := false, false
|
||||
for _, d := range declarations {
|
||||
if len(d.FunctionDeclarations) > 0 {
|
||||
hasFunc = true
|
||||
}
|
||||
if d.GoogleSearch != nil || d.CodeExecution != nil {
|
||||
hasBuiltin = true
|
||||
}
|
||||
}
|
||||
return hasFunc && hasBuiltin
|
||||
}
|
||||
|
||||
// buildTools 构建 tools
|
||||
func buildTools(tools []ClaudeTool) []GeminiToolDeclaration {
|
||||
if len(tools) == 0 {
|
||||
@@ -794,6 +790,18 @@ func buildTools(tools []ClaudeTool) []GeminiToolDeclaration {
|
||||
})
|
||||
}
|
||||
|
||||
// Antigravity v1internal 协议不支持内置工具与 functionDeclarations 混用:
|
||||
// 即便带上 includeServerSideToolInvocations 仍返回 400(issue #6464)。
|
||||
// Codex 默认同时带 web_search 与 shell 等客户端工具,优先保留客户端工具,
|
||||
// 使代理会话可继续,而不是整单 upstream_error。
|
||||
if len(funcDecls) > 0 {
|
||||
if hasWebSearch || hasCodeExecution {
|
||||
log.Printf("[antigravity] dropping built-in tools (web_search/code_execution) because client function tools are present; Antigravity v1internal rejects the mix")
|
||||
}
|
||||
hasWebSearch = false
|
||||
hasCodeExecution = false
|
||||
}
|
||||
|
||||
var declarations []GeminiToolDeclaration
|
||||
if len(funcDecls) > 0 {
|
||||
declarations = append(declarations, GeminiToolDeclaration{
|
||||
|
||||
@@ -263,7 +263,7 @@ func TestBuildTools_CustomTypeTools(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildTools_PreservesWebSearchAlongsideFunctions(t *testing.T) {
|
||||
func TestBuildTools_DropsWebSearchWhenClientFunctionsPresent(t *testing.T) {
|
||||
tools := []ClaudeTool{
|
||||
{
|
||||
Name: "get_weather",
|
||||
@@ -277,13 +277,27 @@ func TestBuildTools_PreservesWebSearchAlongsideFunctions(t *testing.T) {
|
||||
}
|
||||
|
||||
result := buildTools(tools)
|
||||
require.Len(t, result, 2)
|
||||
// Antigravity v1internal rejects mixed built-in + functionDeclarations (#6464).
|
||||
require.Len(t, result, 1)
|
||||
require.Len(t, result[0].FunctionDeclarations, 1)
|
||||
require.Equal(t, "get_weather", result[0].FunctionDeclarations[0].Name)
|
||||
require.NotNil(t, result[1].GoogleSearch)
|
||||
require.NotNil(t, result[1].GoogleSearch.EnhancedContent)
|
||||
require.NotNil(t, result[1].GoogleSearch.EnhancedContent.ImageSearch)
|
||||
require.Equal(t, 5, result[1].GoogleSearch.EnhancedContent.ImageSearch.MaxResultCount)
|
||||
require.Nil(t, result[0].GoogleSearch)
|
||||
}
|
||||
|
||||
func TestBuildTools_KeepsWebSearchWhenNoClientFunctions(t *testing.T) {
|
||||
tools := []ClaudeTool{
|
||||
{
|
||||
Type: "web_search_20250305",
|
||||
Name: "web_search",
|
||||
},
|
||||
}
|
||||
|
||||
result := buildTools(tools)
|
||||
require.Len(t, result, 1)
|
||||
require.NotNil(t, result[0].GoogleSearch)
|
||||
require.NotNil(t, result[0].GoogleSearch.EnhancedContent)
|
||||
require.NotNil(t, result[0].GoogleSearch.EnhancedContent.ImageSearch)
|
||||
require.Equal(t, 5, result[0].GoogleSearch.EnhancedContent.ImageSearch.MaxResultCount)
|
||||
}
|
||||
|
||||
func TestBuildGenerationConfig_ThinkingDynamicBudget(t *testing.T) {
|
||||
@@ -555,18 +569,22 @@ func TestTransformClaudeToGeminiWithOptions_PreservesWebSearchAlongsideFunctions
|
||||
},
|
||||
}
|
||||
|
||||
body, err := TransformClaudeToGeminiWithOptions(claudeReq, "project-1", "gemini-2.5-flash", DefaultTransformOptions())
|
||||
body, err := TransformClaudeToGeminiWithOptions(claudeReq, "project-1", "claude-sonnet-4-6", DefaultTransformOptions())
|
||||
require.NoError(t, err)
|
||||
|
||||
var req V1InternalRequest
|
||||
require.NoError(t, json.Unmarshal(body, &req))
|
||||
require.Len(t, req.Request.Tools, 2)
|
||||
// Antigravity v1internal cannot mix googleSearch with functionDeclarations
|
||||
// (#6464). Prefer client tools so Codex-style sessions keep working.
|
||||
require.Equal(t, "agent", req.RequestType)
|
||||
require.Equal(t, "claude-sonnet-4-6", req.Model)
|
||||
require.Len(t, req.Request.Tools, 1)
|
||||
require.Len(t, req.Request.Tools[0].FunctionDeclarations, 1)
|
||||
require.Equal(t, "get_weather", req.Request.Tools[0].FunctionDeclarations[0].Name)
|
||||
require.NotNil(t, req.Request.Tools[1].GoogleSearch)
|
||||
require.Nil(t, req.Request.Tools[0].GoogleSearch)
|
||||
}
|
||||
|
||||
func TestGeminiToolConfig_IncludeServerSideToolInvocations(t *testing.T) {
|
||||
func TestGeminiToolConfig_DropsBuiltinsWhenClientFunctionsPresent(t *testing.T) {
|
||||
functionTool := ClaudeTool{
|
||||
Name: "get_weather",
|
||||
Description: "Get weather information",
|
||||
@@ -577,7 +595,7 @@ func TestGeminiToolConfig_IncludeServerSideToolInvocations(t *testing.T) {
|
||||
Name: "web_search",
|
||||
}
|
||||
|
||||
transform := func(t *testing.T, tools []ClaudeTool) (V1InternalRequest, string) {
|
||||
transform := func(t *testing.T, tools []ClaudeTool, mappedModel string) (V1InternalRequest, string) {
|
||||
t.Helper()
|
||||
body, err := TransformClaudeToGeminiWithOptions(&ClaudeRequest{
|
||||
Model: "claude-3-5-sonnet-latest",
|
||||
@@ -588,7 +606,7 @@ func TestGeminiToolConfig_IncludeServerSideToolInvocations(t *testing.T) {
|
||||
},
|
||||
},
|
||||
Tools: tools,
|
||||
}, "project-1", "gemini-2.5-flash", DefaultTransformOptions())
|
||||
}, "project-1", mappedModel, DefaultTransformOptions())
|
||||
require.NoError(t, err)
|
||||
|
||||
var req V1InternalRequest
|
||||
@@ -596,27 +614,33 @@ func TestGeminiToolConfig_IncludeServerSideToolInvocations(t *testing.T) {
|
||||
return req, string(body)
|
||||
}
|
||||
|
||||
t.Run("mixed builtin and function tools enable server-side tool invocations", func(t *testing.T) {
|
||||
req, raw := transform(t, []ClaudeTool{functionTool, webSearchTool})
|
||||
t.Run("mixed builtin and function tools keep only client functions", func(t *testing.T) {
|
||||
req, raw := transform(t, []ClaudeTool{functionTool, webSearchTool}, "claude-sonnet-4-6")
|
||||
|
||||
require.NotNil(t, req.Request.ToolConfig)
|
||||
require.NotNil(t, req.Request.ToolConfig.IncludeServerSideToolInvocations)
|
||||
require.True(t, *req.Request.ToolConfig.IncludeServerSideToolInvocations)
|
||||
require.Contains(t, raw, `"includeServerSideToolInvocations":true`)
|
||||
require.Equal(t, "agent", req.RequestType)
|
||||
require.Equal(t, "claude-sonnet-4-6", req.Model)
|
||||
require.Len(t, req.Request.Tools, 1)
|
||||
require.Len(t, req.Request.Tools[0].FunctionDeclarations, 1)
|
||||
require.Nil(t, req.Request.ToolConfig.IncludeServerSideToolInvocations)
|
||||
require.NotContains(t, raw, "googleSearch")
|
||||
require.NotContains(t, raw, "includeServerSideToolInvocations")
|
||||
})
|
||||
|
||||
t.Run("function tools only leave the flag unset", func(t *testing.T) {
|
||||
req, raw := transform(t, []ClaudeTool{functionTool})
|
||||
req, raw := transform(t, []ClaudeTool{functionTool}, "gemini-2.5-flash")
|
||||
|
||||
require.NotNil(t, req.Request.ToolConfig)
|
||||
require.Nil(t, req.Request.ToolConfig.IncludeServerSideToolInvocations)
|
||||
require.NotContains(t, raw, "includeServerSideToolInvocations")
|
||||
})
|
||||
|
||||
t.Run("web search only leaves the flag unset", func(t *testing.T) {
|
||||
req, raw := transform(t, []ClaudeTool{webSearchTool})
|
||||
t.Run("web search only keeps googleSearch and fallback model", func(t *testing.T) {
|
||||
req, raw := transform(t, []ClaudeTool{webSearchTool}, "claude-sonnet-4-6")
|
||||
|
||||
require.NotNil(t, req.Request.ToolConfig)
|
||||
require.Equal(t, "web_search", req.RequestType)
|
||||
require.Equal(t, "gemini-2.5-flash", req.Model)
|
||||
require.Len(t, req.Request.Tools, 1)
|
||||
require.NotNil(t, req.Request.Tools[0].GoogleSearch)
|
||||
require.Nil(t, req.Request.ToolConfig.IncludeServerSideToolInvocations)
|
||||
require.NotContains(t, raw, "includeServerSideToolInvocations")
|
||||
})
|
||||
|
||||
@@ -339,7 +339,63 @@ func responsesInputToChatMessagesWithOptions(instructions string, inputRaw json.
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return normalizeChatMessagesWithToolOutputMedia(built, mediaByCallID), nil
|
||||
normalized := normalizeChatMessagesWithToolOutputMedia(built, mediaByCallID)
|
||||
return normalizeResponsesDerivedChatMessageRoles(normalized), nil
|
||||
}
|
||||
|
||||
// normalizeResponsesDerivedChatMessageRoles rewrites the Chat Completions
|
||||
// message list produced by the Responses bridge so that strict upstreams which
|
||||
// only accept system content at the very start of the conversation accept it.
|
||||
//
|
||||
// The bridge turns the Responses `instructions` field and every role:"developer"
|
||||
// item into a system message. Codex always sends both instructions and a leading
|
||||
// developer item, and it also injects developer notices into the middle of the
|
||||
// message history (for example when the user switches models). Forwarded as-is
|
||||
// that produces two leading system messages and mid-conversation system
|
||||
// messages, which Qwen-family upstreams reject with
|
||||
// "System message must be at the beginning." (HTTP 400).
|
||||
//
|
||||
// Leading system/developer messages are therefore merged into a single leading
|
||||
// system message, while later ones keep their position but are downgraded to
|
||||
// user messages so the same text still reaches the model.
|
||||
func normalizeResponsesDerivedChatMessageRoles(messages []ChatMessage) []ChatMessage {
|
||||
isInstructionRole := func(role string) bool {
|
||||
return role == "system" || role == "developer"
|
||||
}
|
||||
|
||||
leading := 0
|
||||
for leading < len(messages) && isInstructionRole(messages[leading].Role) {
|
||||
leading++
|
||||
}
|
||||
|
||||
out := make([]ChatMessage, 0, len(messages))
|
||||
switch leading {
|
||||
case 0:
|
||||
// No leading instructions, nothing to merge.
|
||||
case 1:
|
||||
// A single leading prompt is already valid; keep its content byte for
|
||||
// byte instead of round-tripping it through the text merge below.
|
||||
out = append(out, messages[0])
|
||||
default:
|
||||
merged := make([]string, 0, leading)
|
||||
for _, m := range messages[:leading] {
|
||||
if text := strings.TrimSpace(chatMessageContentText(m.Content)); text != "" {
|
||||
merged = append(merged, text)
|
||||
}
|
||||
}
|
||||
if len(merged) > 0 {
|
||||
content, _ := json.Marshal(strings.Join(merged, "\n\n"))
|
||||
out = append(out, ChatMessage{Role: "system", Content: content})
|
||||
}
|
||||
}
|
||||
|
||||
for _, m := range messages[leading:] {
|
||||
if isInstructionRole(m.Role) {
|
||||
m.Role = "user"
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// buildChatMessagesFromItems walks the Responses input items and appends the
|
||||
@@ -523,6 +579,21 @@ func buildChatMessagesFromItems(messages []ChatMessage, rawItems []json.RawMessa
|
||||
})
|
||||
pendingReasoning = ""
|
||||
continue
|
||||
case "agent_message":
|
||||
// Codex multi_agent_v2 用 agent_message 在父线程与子智能体之间传递任务和回复:
|
||||
// input_text 是信封(消息类型、任务名、发送者),正文放在 encrypted_content 片段里
|
||||
// (自定义 provider 下为明文)。chat 上游没有对应条目,按原顺序拼成一条 user 消息,
|
||||
// 否则子智能体收不到任务却仍返回 200。
|
||||
text := agentMessageText(item["content"])
|
||||
if text == "" {
|
||||
pendingReasoning = ""
|
||||
continue
|
||||
}
|
||||
content, _ := json.Marshal(text)
|
||||
messages = append(messages, ChatMessage{Role: "user", Content: content})
|
||||
pendingReasoning = ""
|
||||
lastTurnReasoning = ""
|
||||
continue
|
||||
case "input_text", "text":
|
||||
content, _ := json.Marshal(rawString(item["text"]))
|
||||
messages = append(messages, ChatMessage{Role: "user", Content: content})
|
||||
@@ -582,6 +653,32 @@ func buildChatMessagesFromItems(messages []ChatMessage, rawItems []json.RawMessa
|
||||
return messages, mediaByCallID, nil
|
||||
}
|
||||
|
||||
// agentMessageText 按原顺序拼接 agent_message 里 input_text 与 encrypted_content 片段的文本。
|
||||
func agentMessageText(raw json.RawMessage) string {
|
||||
raw = bytesTrimSpace(raw)
|
||||
if len(raw) == 0 || string(raw) == "null" {
|
||||
return ""
|
||||
}
|
||||
var text string
|
||||
if err := json.Unmarshal(raw, &text); err == nil {
|
||||
return text
|
||||
}
|
||||
var parts []map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &parts); err != nil {
|
||||
return ""
|
||||
}
|
||||
var b strings.Builder
|
||||
for _, part := range parts {
|
||||
switch rawString(part["type"]) {
|
||||
case "input_text", "text":
|
||||
_, _ = b.WriteString(rawString(part["text"]))
|
||||
case "encrypted_content":
|
||||
_, _ = b.WriteString(rawString(part["encrypted_content"]))
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// extractToolOutputMedia rewrites only recognized image nodes. Media-free
|
||||
// outputs return rewritten=false so the caller can preserve their original
|
||||
// bytes and prompt-cache prefix.
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package apicompat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// #6653:Codex 把运行时工具放在 input[].additional_tools 里,force_chat_completions
|
||||
// 下必须仍降级成标准 chat 工具随 /v1/chat/completions 发出,而不是因无法转换退回 Responses。
|
||||
func TestResponsesToChatCompletionsRequest_LowersAdditionalToolsForChatOnlyUpstream(t *testing.T) {
|
||||
body := `{
|
||||
"model":"gpt-5.6-luna","stream":true,"store":false,
|
||||
"reasoning":{"effort":"medium","context":"all_turns"},
|
||||
"tool_choice":"auto","parallel_tool_calls":false,
|
||||
"include":["reasoning.encrypted_content"],
|
||||
"input":[
|
||||
{"type":"additional_tools","role":"developer","tools":[
|
||||
{"type":"custom","name":"exec","description":"run a shell command","format":{"type":"grammar","syntax":"lark","definition":"start: /.*/"}},
|
||||
{"type":"function","name":"wait","strict":false,"parameters":{"type":"object"}},
|
||||
{"type":"namespace","name":"collaboration","tools":[{"type":"function","name":"spawn_agent","parameters":{"type":"object"}}]}
|
||||
]},
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"reply ok"}]}
|
||||
]
|
||||
}`
|
||||
var req ResponsesRequest
|
||||
require.NoError(t, json.Unmarshal([]byte(body), &req))
|
||||
|
||||
tools, err := EffectiveResponsesTools(&req)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, tools, 3, "additional_tools 里的 custom / function / namespace 都要被提升")
|
||||
require.True(t, CustomToolNames(tools)["exec"])
|
||||
require.Equal(t, "collaboration", NamespaceToolNames(tools)["collaboration__spawn_agent"].Namespace)
|
||||
|
||||
chat, err := ResponsesToChatCompletionsRequest(&req)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, chat.Messages, 1, "additional_tools 不能变成 chat 消息")
|
||||
require.Equal(t, "user", chat.Messages[0].Role)
|
||||
|
||||
names := make([]string, 0, len(chat.Tools))
|
||||
for _, tool := range chat.Tools {
|
||||
require.Equal(t, "function", tool.Type, "chat 上游只认 function 工具")
|
||||
require.NotNil(t, tool.Function)
|
||||
names = append(names, tool.Function.Name)
|
||||
}
|
||||
require.Equal(t, []string{"exec", "wait", "collaboration__spawn_agent"}, names)
|
||||
require.JSONEq(t, customToolInputSchema, string(chat.Tools[0].Function.Parameters), "custom 工具降级为单一 input 字符串参数")
|
||||
require.NotNil(t, chat.ParallelToolCalls)
|
||||
require.False(t, *chat.ParallelToolCalls)
|
||||
require.Equal(t, "medium", chat.ReasoningEffort)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package apicompat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// #6887:Codex multi_agent_v2 用 agent_message 条目在父线程与子智能体之间传递任务和回复,
|
||||
// 任务正文放在 encrypted_content 片段里(自定义 provider 下为明文)。chat 上游没有对应条目,
|
||||
// 桥必须把它按原顺序拼成一条 user 消息,否则子智能体收不到任务。
|
||||
func TestResponsesToChatCompletionsRequest_AgentMessageBecomesUserMessage(t *testing.T) {
|
||||
req := &ResponsesRequest{
|
||||
Model: "glm-5.3",
|
||||
Input: json.RawMessage(`[
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"<environment_context>cwd</environment_context>"}]},
|
||||
{"type":"agent_message","id":"amsg_1","author":"/root","recipient":"/root/alpha_task","content":[
|
||||
{"type":"input_text","text":"Message Type: NEW_TASK\nTask name: /root/alpha_task\nSender: /root\nPayload:\n"},
|
||||
{"type":"encrypted_content","encrypted_content":"Reply with the single word: ALPHA"}
|
||||
]},
|
||||
{"type":"reasoning","summary":[{"type":"summary_text","text":"just answer"}]},
|
||||
{"type":"message","role":"assistant","content":[{"type":"output_text","text":"ALPHA"}]},
|
||||
{"type":"agent_message","id":"amsg_2","author":"/root/alpha_task","recipient":"/root","content":[
|
||||
{"type":"input_text","text":"Message Type: FINAL_ANSWER\nTask name: /root\nSender: /root/alpha_task\nPayload:\nALPHA"}
|
||||
]}
|
||||
]`),
|
||||
}
|
||||
|
||||
out, err := ResponsesToChatCompletionsRequest(req)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, out.Messages, 4)
|
||||
|
||||
require.Equal(t, "user", out.Messages[0].Role)
|
||||
require.Equal(t, "user", out.Messages[1].Role, "父线程发来的任务要成为 user 消息")
|
||||
require.JSONEq(t, `"Message Type: NEW_TASK\nTask name: /root/alpha_task\nSender: /root\nPayload:\nReply with the single word: ALPHA"`, string(out.Messages[1].Content), "信封与 encrypted_content 里的正文要按原顺序拼接")
|
||||
require.Equal(t, "assistant", out.Messages[2].Role)
|
||||
require.JSONEq(t, `"ALPHA"`, string(out.Messages[2].Content))
|
||||
require.Equal(t, "user", out.Messages[3].Role, "子智能体回给父线程的消息同样是 user 消息")
|
||||
require.JSONEq(t, `"Message Type: FINAL_ANSWER\nTask name: /root\nSender: /root/alpha_task\nPayload:\nALPHA"`, string(out.Messages[3].Content))
|
||||
}
|
||||
|
||||
func TestResponsesToChatCompletionsRequest_AgentMessageWithoutTextIsSkipped(t *testing.T) {
|
||||
req := &ResponsesRequest{
|
||||
Model: "glm-5.3",
|
||||
Input: json.RawMessage(`[
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]},
|
||||
{"type":"agent_message","author":"/root","recipient":"/root/a","content":[]},
|
||||
{"type":"agent_message","author":"/root","recipient":"/root/a"},
|
||||
{"type":"agent_message","author":"/root","recipient":"/root/a","content":[{"type":"input_image","image_url":"data:image/png;base64,AA=="}]}
|
||||
]`),
|
||||
}
|
||||
|
||||
out, err := ResponsesToChatCompletionsRequest(req)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, out.Messages, 1, "没有文本正文的 agent_message 不产生消息")
|
||||
require.Equal(t, "user", out.Messages[0].Role)
|
||||
}
|
||||
@@ -96,7 +96,7 @@ func TestResponsesInputToChatMessages_EmptyRoleFallsBackToUser(t *testing.T) {
|
||||
assert.Equal(t, "user", messages[0].Role)
|
||||
}
|
||||
|
||||
func TestResponsesInputToChatMessages_DeveloperRoleTrimAndCaseInsensitive(t *testing.T) {
|
||||
func TestResponsesInputToChatMessages_LeadingDeveloperRolesMergeIntoOneSystem(t *testing.T) {
|
||||
input := json.RawMessage(`[
|
||||
{"role":" Developer ","content":"one"},
|
||||
{"role":"\tDEVELOPER\n","content":"two"}
|
||||
@@ -104,9 +104,10 @@ func TestResponsesInputToChatMessages_DeveloperRoleTrimAndCaseInsensitive(t *tes
|
||||
|
||||
messages, err := responsesInputToChatMessages("", input)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, messages, 2)
|
||||
require.Len(t, messages, 1)
|
||||
|
||||
assert.Equal(t, []string{"system", "system"}, chatMessageRoles(messages))
|
||||
assert.Equal(t, []string{"system"}, chatMessageRoles(messages))
|
||||
assert.JSONEq(t, `"one\n\ntwo"`, string(messages[0].Content))
|
||||
}
|
||||
|
||||
func TestResponsesToChatCompletionsRequest_InstructionsAndInputDeveloperRole(t *testing.T) {
|
||||
@@ -121,12 +122,29 @@ func TestResponsesToChatCompletionsRequest_InstructionsAndInputDeveloperRole(t *
|
||||
|
||||
out, err := ResponsesToChatCompletionsRequest(req)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, out.Messages, 3)
|
||||
require.Len(t, out.Messages, 2)
|
||||
|
||||
assert.Equal(t, []string{"system", "system", "user"}, chatMessageRoles(out.Messages))
|
||||
assert.JSONEq(t, `"Use concise answers."`, string(out.Messages[0].Content))
|
||||
assert.JSONEq(t, `"Prefer JSON."`, string(out.Messages[1].Content))
|
||||
assert.JSONEq(t, `"Hello"`, string(out.Messages[2].Content))
|
||||
assert.Equal(t, []string{"system", "user"}, chatMessageRoles(out.Messages))
|
||||
assert.JSONEq(t, `"Use concise answers.\n\nPrefer JSON."`, string(out.Messages[0].Content))
|
||||
assert.JSONEq(t, `"Hello"`, string(out.Messages[1].Content))
|
||||
}
|
||||
|
||||
func TestResponsesInputToChatMessages_MidConversationDeveloperBecomesUser(t *testing.T) {
|
||||
input := json.RawMessage(`[
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"hello"}]},
|
||||
{"type":"message","role":"assistant","content":[{"type":"output_text","text":"hi"}]},
|
||||
{"type":"message","role":"developer","content":[{"type":"input_text","text":"<model_switch> switched model"}]},
|
||||
{"type":"message","role":"system","content":[{"type":"input_text","text":"be terse"}]},
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"continue"}]}
|
||||
]`)
|
||||
|
||||
messages, err := responsesInputToChatMessages("", input)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, messages, 5)
|
||||
|
||||
assert.Equal(t, []string{"user", "assistant", "user", "user", "user"}, chatMessageRoles(messages))
|
||||
assert.JSONEq(t, `"<model_switch> switched model"`, string(messages[2].Content))
|
||||
assert.JSONEq(t, `"be terse"`, string(messages[3].Content))
|
||||
}
|
||||
|
||||
func TestResponsesToChatCompletionsRequest_TextFormatJsonObject(t *testing.T) {
|
||||
|
||||
@@ -154,7 +154,7 @@ func TestResponsesToolOutputMedia_InterleavedMessagesFollowMediaBatch(t *testing
|
||||
{"type":"function_call_output","call_id":"call_A","output":[{"type":"input_image","image_url":"data:image/png;base64,AQID"}]}
|
||||
]`)
|
||||
|
||||
require.Equal(t, []string{"assistant", "tool", "user", "system", "user"}, chatMessageRoles(messages))
|
||||
require.Equal(t, []string{"assistant", "tool", "user", "user", "user"}, chatMessageRoles(messages))
|
||||
require.Equal(t, "[Tool output media for call call_A]", chatContentParts(t, messages[2])[0].Text)
|
||||
require.JSONEq(t, `"approval saved"`, string(messages[3].Content))
|
||||
require.JSONEq(t, `"continue"`, string(messages[4].Content))
|
||||
|
||||
@@ -170,6 +170,12 @@ func sanitizeAnthropicToolUseInput(name string, raw string) json.RawMessage {
|
||||
// Streaming: ResponsesStreamEvent → []AnthropicStreamEvent (stateful converter)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// responsesTextPart identifies one output_text part of a streamed response.
|
||||
type responsesTextPart struct {
|
||||
OutputIndex int
|
||||
ContentIndex int
|
||||
}
|
||||
|
||||
// ResponsesEventToAnthropicState tracks state for converting a sequence of
|
||||
// Responses SSE events directly into Anthropic SSE events.
|
||||
type ResponsesEventToAnthropicState struct {
|
||||
@@ -190,6 +196,13 @@ type ResponsesEventToAnthropicState struct {
|
||||
// OutputIndexToBlockIdx maps Responses output_index → Anthropic content block index.
|
||||
OutputIndexToBlockIdx map[int]int
|
||||
|
||||
// textByPart records the text already delivered for each output_text part
|
||||
// so that a done payload can be reconciled against it. It outlives the
|
||||
// content block; closeCurrentBlock must not reset it.
|
||||
textByPart map[responsesTextPart]*strings.Builder
|
||||
// textDelivered records whether any assistant text reached the client.
|
||||
textDelivered bool
|
||||
|
||||
InputTokens int
|
||||
OutputTokens int
|
||||
CacheReadInputTokens int
|
||||
@@ -204,6 +217,7 @@ type ResponsesEventToAnthropicState struct {
|
||||
func NewResponsesEventToAnthropicState() *ResponsesEventToAnthropicState {
|
||||
return &ResponsesEventToAnthropicState{
|
||||
OutputIndexToBlockIdx: make(map[int]int),
|
||||
textByPart: make(map[responsesTextPart]*strings.Builder),
|
||||
Created: time.Now().Unix(),
|
||||
}
|
||||
}
|
||||
@@ -222,7 +236,7 @@ func ResponsesEventToAnthropicEvents(
|
||||
case "response.output_text.delta":
|
||||
return resToAnthHandleTextDelta(evt, state)
|
||||
case "response.output_text.done":
|
||||
return resToAnthHandleBlockDone(state)
|
||||
return resToAnthHandleTextDone(evt, state)
|
||||
case "response.function_call_arguments.delta",
|
||||
// custom/freeform 工具的输入增量与 function_call 参数增量同形。
|
||||
"response.custom_tool_call_input.delta":
|
||||
@@ -390,7 +404,18 @@ func resToAnthHandleOutputItemAdded(evt *ResponsesStreamEvent, state *ResponsesE
|
||||
}
|
||||
|
||||
func resToAnthHandleTextDelta(evt *ResponsesStreamEvent, state *ResponsesEventToAnthropicState) []AnthropicStreamEvent {
|
||||
if evt.Delta == "" {
|
||||
return resToAnthEmitText(evt.Delta, resToAnthTextPartOf(evt), state)
|
||||
}
|
||||
|
||||
func resToAnthTextPartOf(evt *ResponsesStreamEvent) responsesTextPart {
|
||||
return responsesTextPart{OutputIndex: evt.OutputIndex, ContentIndex: evt.ContentIndex}
|
||||
}
|
||||
|
||||
// resToAnthEmitText opens a text block when needed, emits text, and records it
|
||||
// against its part so that a later payload for the same part is reconciled
|
||||
// against what the client already received.
|
||||
func resToAnthEmitText(text string, part responsesTextPart, state *ResponsesEventToAnthropicState) []AnthropicStreamEvent {
|
||||
if text == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -413,18 +438,63 @@ func resToAnthHandleTextDelta(evt *ResponsesStreamEvent, state *ResponsesEventTo
|
||||
})
|
||||
}
|
||||
|
||||
delivered, ok := state.textByPart[part]
|
||||
if !ok {
|
||||
delivered = &strings.Builder{}
|
||||
state.textByPart[part] = delivered
|
||||
}
|
||||
_, _ = delivered.WriteString(text)
|
||||
state.textDelivered = true
|
||||
|
||||
idx := state.ContentBlockIndex
|
||||
events = append(events, AnthropicStreamEvent{
|
||||
Type: "content_block_delta",
|
||||
Index: &idx,
|
||||
Delta: &AnthropicDelta{
|
||||
Type: "text_delta",
|
||||
Text: evt.Delta,
|
||||
Text: text,
|
||||
},
|
||||
})
|
||||
return events
|
||||
}
|
||||
|
||||
// resToAnthRecoverText emits the tail of a finished text payload that never
|
||||
// reached the client. Streamed text cannot be recalled, so a payload that does
|
||||
// not extend what was already delivered is left alone.
|
||||
func resToAnthRecoverText(text string, part responsesTextPart, state *ResponsesEventToAnthropicState) []AnthropicStreamEvent {
|
||||
builder, known := state.textByPart[part]
|
||||
if !known && state.textDelivered {
|
||||
// The payload is indexed differently from every delta seen so far, so
|
||||
// which part it finishes cannot be established. Recovering it could
|
||||
// repeat an answer the client already has, which is worse than leaving
|
||||
// a partially delivered one alone.
|
||||
return nil
|
||||
}
|
||||
|
||||
var delivered string
|
||||
if known {
|
||||
delivered = builder.String()
|
||||
}
|
||||
if text == delivered || !strings.HasPrefix(text, delivered) {
|
||||
return nil
|
||||
}
|
||||
return resToAnthEmitText(text[len(delivered):], part, state)
|
||||
}
|
||||
|
||||
// resToAnthHandleTextDone recovers text that upstream carried only on the done
|
||||
// event before closing the block, which some streams use instead of sending
|
||||
// output_text.delta at all.
|
||||
func resToAnthHandleTextDone(evt *ResponsesStreamEvent, state *ResponsesEventToAnthropicState) []AnthropicStreamEvent {
|
||||
if state.MessageStopSent {
|
||||
// The message is already terminated; a late payload cannot be delivered
|
||||
// without emitting a content block after message_stop.
|
||||
return resToAnthHandleBlockDone(state)
|
||||
}
|
||||
|
||||
events := resToAnthRecoverText(evt.Text, resToAnthTextPartOf(evt), state)
|
||||
return append(events, resToAnthHandleBlockDone(state)...)
|
||||
}
|
||||
|
||||
func resToAnthHandleFuncArgsDelta(evt *ResponsesStreamEvent, state *ResponsesEventToAnthropicState) []AnthropicStreamEvent {
|
||||
if evt.Delta == "" {
|
||||
return nil
|
||||
@@ -622,6 +692,38 @@ func resToAnthHandleWebSearchDone(evt *ResponsesStreamEvent, state *ResponsesEve
|
||||
return events
|
||||
}
|
||||
|
||||
// resToAnthRecoverTerminalText emits assistant text that only ever appeared in
|
||||
// the terminal response payload, which some streams populate without sending
|
||||
// any output_text event.
|
||||
//
|
||||
// It only runs when no text at all reached the client. Streamed events and the
|
||||
// terminal output array carry no guaranteed common identity, so reconciling
|
||||
// them part by part risks repeating an answer the client already has, which is
|
||||
// worse than leaving a partially streamed response as it is.
|
||||
func resToAnthRecoverTerminalText(evt *ResponsesStreamEvent, state *ResponsesEventToAnthropicState) []AnthropicStreamEvent {
|
||||
if state.textDelivered || evt.Response == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var events []AnthropicStreamEvent
|
||||
for outputIndex, item := range evt.Response.Output {
|
||||
if item.Type != "message" {
|
||||
continue
|
||||
}
|
||||
for contentIndex, content := range item.Content {
|
||||
if content.Type != "output_text" {
|
||||
continue
|
||||
}
|
||||
part := responsesTextPart{OutputIndex: outputIndex, ContentIndex: contentIndex}
|
||||
events = append(events, resToAnthEmitText(content.Text, part, state)...)
|
||||
}
|
||||
}
|
||||
if len(events) > 0 {
|
||||
events = append(events, closeCurrentBlock(state)...)
|
||||
}
|
||||
return events
|
||||
}
|
||||
|
||||
func resToAnthHandleCompleted(evt *ResponsesStreamEvent, state *ResponsesEventToAnthropicState) []AnthropicStreamEvent {
|
||||
if state.MessageStopSent {
|
||||
return nil
|
||||
@@ -629,6 +731,7 @@ func resToAnthHandleCompleted(evt *ResponsesStreamEvent, state *ResponsesEventTo
|
||||
|
||||
var events []AnthropicStreamEvent
|
||||
events = append(events, closeCurrentBlock(state)...)
|
||||
events = append(events, resToAnthRecoverTerminalText(evt, state)...)
|
||||
|
||||
stopReason := "end_turn"
|
||||
if evt.Usage != nil {
|
||||
|
||||
@@ -0,0 +1,532 @@
|
||||
package apicompat
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// feedResponsesEvents converts a whole event sequence with a single state.
|
||||
func feedResponsesEvents(events ...*ResponsesStreamEvent) []AnthropicStreamEvent {
|
||||
state := NewResponsesEventToAnthropicState()
|
||||
var out []AnthropicStreamEvent
|
||||
for _, evt := range events {
|
||||
out = append(out, ResponsesEventToAnthropicEvents(evt, state)...)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// collectAnthropicText accumulates the visible assistant text a Claude client
|
||||
// reconstructs from a converted event stream.
|
||||
func collectAnthropicText(events []AnthropicStreamEvent) string {
|
||||
text := ""
|
||||
for _, event := range events {
|
||||
if event.Type == "content_block_delta" && event.Delta != nil && event.Delta.Type == "text_delta" {
|
||||
text += event.Delta.Text
|
||||
}
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
// requireAnthropicBlockLifecycle checks the block framing a strict client
|
||||
// relies on: indices start at zero and increase by one, every block is closed,
|
||||
// and no delta arrives outside an open block.
|
||||
func requireAnthropicBlockLifecycle(t *testing.T, events []AnthropicStreamEvent) {
|
||||
t.Helper()
|
||||
|
||||
open := false
|
||||
next := 0
|
||||
for _, event := range events {
|
||||
switch event.Type {
|
||||
case "content_block_start":
|
||||
require.False(t, open, "content_block_start while a block is open")
|
||||
require.NotNil(t, event.Index)
|
||||
require.Equal(t, next, *event.Index, "content block indices must increase by one")
|
||||
open = true
|
||||
case "content_block_delta":
|
||||
require.True(t, open, "content_block_delta outside an open block")
|
||||
require.NotNil(t, event.Index)
|
||||
require.Equal(t, next, *event.Index)
|
||||
case "content_block_stop":
|
||||
require.True(t, open, "content_block_stop without an open block")
|
||||
require.NotNil(t, event.Index)
|
||||
require.Equal(t, next, *event.Index)
|
||||
open = false
|
||||
next++
|
||||
}
|
||||
}
|
||||
require.False(t, open, "stream ended with an unclosed content block")
|
||||
}
|
||||
|
||||
func responsesCreated() *ResponsesStreamEvent {
|
||||
return &ResponsesStreamEvent{
|
||||
Type: "response.created",
|
||||
Response: &ResponsesResponse{ID: "resp_text_recovery", Model: "gpt-5.2"},
|
||||
}
|
||||
}
|
||||
|
||||
func responsesMessageOutput(texts ...string) []ResponsesOutput {
|
||||
content := make([]ResponsesContentPart, 0, len(texts))
|
||||
for _, text := range texts {
|
||||
content = append(content, ResponsesContentPart{Type: "output_text", Text: text})
|
||||
}
|
||||
return []ResponsesOutput{{Type: "message", Role: "assistant", Content: content}}
|
||||
}
|
||||
|
||||
// Some streams carry the finished text only on response.output_text.done and
|
||||
// send no output_text.delta at all. Dropping it leaves a protocol-valid message
|
||||
// with no text, which clients report as an empty response.
|
||||
func TestResponsesEventToAnthropicEvents_RecoversTextFromDoneEvent(t *testing.T) {
|
||||
const answer = "recovered from done"
|
||||
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.output_item.added", Item: &ResponsesOutput{Type: "message"}},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.done", Text: answer},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{Status: "completed"}},
|
||||
)
|
||||
|
||||
assert.Equal(t, answer, collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
require.NotEmpty(t, events)
|
||||
assert.Equal(t, "message_stop", events[len(events)-1].Type)
|
||||
}
|
||||
|
||||
// When nothing streamed at all, the terminal event still carries the finished
|
||||
// output. Dropping it produces the same empty turn.
|
||||
func TestResponsesEventToAnthropicEvents_RecoversTextFromTerminalOutput(t *testing.T) {
|
||||
const answer = "recovered from terminal"
|
||||
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{
|
||||
Status: "completed",
|
||||
Output: responsesMessageOutput(answer),
|
||||
}},
|
||||
)
|
||||
|
||||
assert.Equal(t, answer, collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
require.NotEmpty(t, events)
|
||||
assert.Equal(t, "message_stop", events[len(events)-1].Type)
|
||||
}
|
||||
|
||||
// The normal path delivers text through deltas. Neither the done payload nor
|
||||
// the terminal payload may repeat it.
|
||||
func TestResponsesEventToAnthropicEvents_DoesNotDuplicateTextDeliveredByDeltas(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.output_text.delta", Delta: "Hel"},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.delta", Delta: "lo"},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.done", Text: "Hello"},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{
|
||||
Status: "completed",
|
||||
Output: responsesMessageOutput("Hello"),
|
||||
}},
|
||||
)
|
||||
|
||||
assert.Equal(t, "Hello", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
}
|
||||
|
||||
// Text recovered from a done event must not be repeated by the terminal
|
||||
// payload that also carries it.
|
||||
func TestResponsesEventToAnthropicEvents_DoesNotDuplicateTextRecoveredFromDoneEvent(t *testing.T) {
|
||||
const answer = "recovered once"
|
||||
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.output_text.done", Text: answer},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{
|
||||
Status: "completed",
|
||||
Output: responsesMessageOutput(answer),
|
||||
}},
|
||||
)
|
||||
|
||||
assert.Equal(t, answer, collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
}
|
||||
|
||||
// Deduplication is per output_text part, not per content block: an event that
|
||||
// closes the block between the deltas and the matching done must not make the
|
||||
// done payload look undelivered.
|
||||
func TestResponsesEventToAnthropicEvents_DoesNotDuplicateTextWhenBlockClosedBeforeDone(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.output_text.delta", Delta: "Hello"},
|
||||
&ResponsesStreamEvent{
|
||||
Type: "response.output_item.added",
|
||||
OutputIndex: 1,
|
||||
Item: &ResponsesOutput{Type: "function_call", CallID: "call_1", Name: "lookup"},
|
||||
},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.done", Text: "Hello"},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{Status: "completed"}},
|
||||
)
|
||||
|
||||
assert.Equal(t, "Hello", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
}
|
||||
|
||||
// When only part of the text streamed, the done payload is authoritative and
|
||||
// the missing suffix must still reach the client.
|
||||
func TestResponsesEventToAnthropicEvents_AppendsOnlySuffixMissingFromDeltas(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.output_text.delta", Delta: "Hel"},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.done", Text: "Hello world"},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{
|
||||
Status: "completed",
|
||||
Output: responsesMessageOutput("Hello world"),
|
||||
}},
|
||||
)
|
||||
|
||||
assert.Equal(t, "Hello world", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
}
|
||||
|
||||
// Streamed text cannot be recalled, so a payload that contradicts what was
|
||||
// already delivered is left alone rather than appended.
|
||||
func TestResponsesEventToAnthropicEvents_IgnoresDonePayloadDivergingFromDeltas(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.output_text.delta", Delta: "Hello"},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.done", Text: "Goodbye"},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{Status: "completed"}},
|
||||
)
|
||||
|
||||
assert.Equal(t, "Hello", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
}
|
||||
|
||||
// Streamed events and the terminal output array share no guaranteed identity,
|
||||
// so once any text has been delivered the terminal payload is left alone. An
|
||||
// upstream that numbers its streamed items differently from its terminal array
|
||||
// must not make a delivered answer look undelivered.
|
||||
func TestResponsesEventToAnthropicEvents_DoesNotDuplicateWhenTerminalOutputIsIndexedDifferently(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{
|
||||
Type: "response.output_item.added",
|
||||
OutputIndex: 0,
|
||||
Item: &ResponsesOutput{Type: "reasoning", ID: "rs_1"},
|
||||
},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.delta", OutputIndex: 1, Delta: "Hello"},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{
|
||||
Status: "completed",
|
||||
// The reasoning item is absent here, so the message sits at array
|
||||
// position 0 while it streamed at output_index 1.
|
||||
Output: responsesMessageOutput("Hello"),
|
||||
}},
|
||||
)
|
||||
|
||||
assert.Equal(t, "Hello", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
}
|
||||
|
||||
// Partially streamed responses keep exactly what streamed. Recovering the rest
|
||||
// would mean matching two index spaces that upstreams do not keep aligned.
|
||||
func TestResponsesEventToAnthropicEvents_LeavesTerminalOutputAloneOnceTextStreamed(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.output_text.delta", ContentIndex: 0, Delta: "first"},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.done", ContentIndex: 0, Text: "first"},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{
|
||||
Status: "completed",
|
||||
Output: responsesMessageOutput("first", "second"),
|
||||
}},
|
||||
)
|
||||
|
||||
assert.Equal(t, "first", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
}
|
||||
|
||||
// When nothing streamed, every output_text part of the terminal payload is
|
||||
// recovered, merged into one block the way contiguous streamed text is.
|
||||
func TestResponsesEventToAnthropicEvents_RecoversEveryTerminalPartWhenNothingStreamed(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{
|
||||
Status: "completed",
|
||||
Output: responsesMessageOutput("first", "second"),
|
||||
}},
|
||||
)
|
||||
|
||||
assert.Equal(t, "firstsecond", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
|
||||
starts := 0
|
||||
for _, event := range events {
|
||||
if event.Type == "content_block_start" {
|
||||
starts++
|
||||
}
|
||||
}
|
||||
assert.Equal(t, 1, starts, "contiguous recovered text belongs in one block")
|
||||
}
|
||||
|
||||
// A turn that produced only a tool call must stay text-free: the terminal
|
||||
// payload carries no message item and nothing may be synthesised.
|
||||
func TestResponsesEventToAnthropicEvents_LeavesToolOnlyTurnTextFree(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{
|
||||
Type: "response.output_item.added",
|
||||
OutputIndex: 0,
|
||||
Item: &ResponsesOutput{Type: "function_call", CallID: "call_1", Name: "lookup"},
|
||||
},
|
||||
&ResponsesStreamEvent{
|
||||
Type: "response.function_call_arguments.done",
|
||||
OutputIndex: 0,
|
||||
Arguments: `{"id":1}`,
|
||||
},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{
|
||||
Status: "completed",
|
||||
Output: []ResponsesOutput{{Type: "function_call", CallID: "call_1", Name: "lookup", Arguments: `{"id":1}`}},
|
||||
}},
|
||||
)
|
||||
|
||||
assert.Empty(t, collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
}
|
||||
|
||||
// Recovery must behave the same on every terminal alias the converter accepts,
|
||||
// not only on response.completed.
|
||||
func TestResponsesEventToAnthropicEvents_RecoversTextOnEveryTerminalAlias(t *testing.T) {
|
||||
for _, terminal := range []string{
|
||||
"response.completed",
|
||||
"response.done",
|
||||
"response.incomplete",
|
||||
"response.failed",
|
||||
} {
|
||||
t.Run(terminal, func(t *testing.T) {
|
||||
const answer = "recovered on a terminal alias"
|
||||
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: terminal, Response: &ResponsesResponse{
|
||||
Status: "completed",
|
||||
Output: responsesMessageOutput(answer),
|
||||
}},
|
||||
)
|
||||
|
||||
assert.Equal(t, answer, collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
require.NotEmpty(t, events)
|
||||
assert.Equal(t, "message_stop", events[len(events)-1].Type)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A done payload indexed differently from every delta seen so far cannot be
|
||||
// matched to a part. Emitting it anyway would repeat an answer the client
|
||||
// already has, which is the failure the terminal guard also exists to prevent.
|
||||
func TestResponsesEventToAnthropicEvents_DoesNotDuplicateWhenDoneIsIndexedDifferently(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
done *ResponsesStreamEvent
|
||||
}{
|
||||
{"content index diverges", &ResponsesStreamEvent{Type: "response.output_text.done", ContentIndex: 1, Text: "Hello world"}},
|
||||
{"output index diverges", &ResponsesStreamEvent{Type: "response.output_text.done", OutputIndex: 1, Text: "Hello world"}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.output_text.delta", Delta: "Hello world"},
|
||||
tc.done,
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{Status: "completed"}},
|
||||
)
|
||||
|
||||
assert.Equal(t, "Hello world", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// The same rule holds when only part of the text streamed: an unmatchable
|
||||
// payload is left alone rather than appended on top of what was delivered.
|
||||
func TestResponsesEventToAnthropicEvents_LeavesUnmatchableDoneAloneAfterPartialDeltas(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.output_text.delta", ContentIndex: 0, Delta: "Hello"},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.done", ContentIndex: 1, Text: "Hello world"},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{Status: "completed"}},
|
||||
)
|
||||
|
||||
assert.Equal(t, "Hello", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
}
|
||||
|
||||
// Delivered text is keyed by output index and content index together. Keying on
|
||||
// either alone would make one of these parts look already delivered.
|
||||
func TestResponsesEventToAnthropicEvents_KeysDeliveredTextByBothIndices(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.output_text.delta", OutputIndex: 0, ContentIndex: 0, Delta: "first"},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.done", OutputIndex: 0, ContentIndex: 0, Text: "first"},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.delta", OutputIndex: 0, ContentIndex: 1, Delta: "second"},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.done", OutputIndex: 0, ContentIndex: 1, Text: "second-tail"},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{Status: "completed"}},
|
||||
)
|
||||
|
||||
assert.Equal(t, "firstsecond-tail", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
}
|
||||
|
||||
// A stray done event after the terminal one must not resurrect a stopped
|
||||
// message. The baseline dropped it outright; recovery has to keep that, or a
|
||||
// client sees a content block after message_stop.
|
||||
func TestResponsesEventToAnthropicEvents_IgnoresDoneEventAfterMessageStop(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{
|
||||
Status: "completed",
|
||||
Output: responsesMessageOutput("the answer"),
|
||||
}},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.done", OutputIndex: 7, Text: "late text"},
|
||||
)
|
||||
|
||||
assert.Equal(t, "the answer", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
require.NotEmpty(t, events)
|
||||
assert.Equal(t, "message_stop", events[len(events)-1].Type, "no content may follow message_stop")
|
||||
}
|
||||
|
||||
// The tool path reads arguments, never text. A tool done event carrying a
|
||||
// stray text field must not produce a text block.
|
||||
func TestResponsesEventToAnthropicEvents_LeavesToolArgumentsDoneTextFree(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{
|
||||
Type: "response.output_item.added",
|
||||
OutputIndex: 0,
|
||||
Item: &ResponsesOutput{Type: "function_call", CallID: "call_1", Name: "lookup"},
|
||||
},
|
||||
&ResponsesStreamEvent{
|
||||
Type: "response.function_call_arguments.done",
|
||||
OutputIndex: 0,
|
||||
Arguments: `{"id":1}`,
|
||||
Text: "must not be emitted",
|
||||
},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{Status: "completed"}},
|
||||
)
|
||||
|
||||
assert.Empty(t, collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
}
|
||||
|
||||
// Recovery closes whatever block is open through the shared helper, which owns
|
||||
// signature emission. A thinking block must still get its signature_delta
|
||||
// before its stop.
|
||||
func TestResponsesEventToAnthropicEvents_PreservesThinkingSignatureWhenRecovering(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{
|
||||
Type: "response.output_item.added",
|
||||
OutputIndex: 0,
|
||||
Item: &ResponsesOutput{Type: "reasoning", EncryptedContent: "sig-abc"},
|
||||
},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.done", OutputIndex: 1, Text: "recovered"},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{Status: "completed"}},
|
||||
)
|
||||
|
||||
assert.Equal(t, "recovered", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
|
||||
var signatures, stopsBefore int
|
||||
for _, event := range events {
|
||||
if event.Type == "content_block_delta" && event.Delta != nil && event.Delta.Type == "signature_delta" {
|
||||
assert.Equal(t, "sig-abc", event.Delta.Signature)
|
||||
signatures++
|
||||
assert.Equal(t, 0, stopsBefore, "signature_delta must precede the thinking block stop")
|
||||
}
|
||||
if event.Type == "content_block_stop" {
|
||||
stopsBefore++
|
||||
}
|
||||
}
|
||||
assert.Equal(t, 1, signatures, "the thinking signature must survive recovery")
|
||||
}
|
||||
|
||||
// A stream that ends without any terminal event exits through the synthetic
|
||||
// finalizer, which must still close a block recovery opened.
|
||||
func TestResponsesEventToAnthropicEvents_RecoveredTextSurvivesTheSyntheticFinalizer(t *testing.T) {
|
||||
state := NewResponsesEventToAnthropicState()
|
||||
var events []AnthropicStreamEvent
|
||||
for _, evt := range []*ResponsesStreamEvent{
|
||||
responsesCreated(),
|
||||
{Type: "response.output_text.done", Text: "recovered before the stream died"},
|
||||
} {
|
||||
events = append(events, ResponsesEventToAnthropicEvents(evt, state)...)
|
||||
}
|
||||
events = append(events, FinalizeResponsesAnthropicStream(state)...)
|
||||
|
||||
assert.Equal(t, "recovered before the stream died", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
require.NotEmpty(t, events)
|
||||
assert.Equal(t, "message_stop", events[len(events)-1].Type)
|
||||
}
|
||||
|
||||
// The terminal walk only takes output_text parts of message items.
|
||||
func TestResponsesEventToAnthropicEvents_DeclinesTerminalRecoveryWithoutMessageText(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
response *ResponsesResponse
|
||||
}{
|
||||
{"no response payload", nil},
|
||||
{"no message item", &ResponsesResponse{Status: "completed", Output: []ResponsesOutput{{Type: "reasoning"}}}},
|
||||
{"message without output_text", &ResponsesResponse{Status: "completed", Output: []ResponsesOutput{{
|
||||
Type: "message",
|
||||
Role: "assistant",
|
||||
Content: []ResponsesContentPart{{Type: "refusal", Text: "refused"}},
|
||||
}}}},
|
||||
{"empty output_text", &ResponsesResponse{Status: "completed", Output: responsesMessageOutput("")}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: tc.response},
|
||||
)
|
||||
|
||||
assert.Empty(t, collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Every message item of the terminal payload is recovered, and non-message
|
||||
// items between them are skipped without breaking the merged block.
|
||||
func TestResponsesEventToAnthropicEvents_RecoversEveryTerminalMessageItem(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{
|
||||
Status: "completed",
|
||||
Output: []ResponsesOutput{
|
||||
{Type: "reasoning"},
|
||||
{Type: "message", Role: "assistant", Content: []ResponsesContentPart{{Type: "output_text", Text: "first"}}},
|
||||
{Type: "function_call", CallID: "call_1", Name: "lookup"},
|
||||
{Type: "message", Role: "assistant", Content: []ResponsesContentPart{{Type: "output_text", Text: "second"}}},
|
||||
},
|
||||
}},
|
||||
)
|
||||
|
||||
assert.Equal(t, "firstsecond", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
}
|
||||
|
||||
// Text that upstream delivers only on a done event still reaches the client
|
||||
// when a tool block is open, which the baseline dropped. The tool block is
|
||||
// closed first, so later block indices shift by one.
|
||||
func TestResponsesEventToAnthropicEvents_EmitsDoneTextWhileAToolBlockIsOpen(t *testing.T) {
|
||||
events := feedResponsesEvents(
|
||||
responsesCreated(),
|
||||
&ResponsesStreamEvent{
|
||||
Type: "response.output_item.added",
|
||||
OutputIndex: 0,
|
||||
Item: &ResponsesOutput{Type: "function_call", CallID: "call_1", Name: "lookup"},
|
||||
},
|
||||
&ResponsesStreamEvent{Type: "response.output_text.done", OutputIndex: 1, Text: "spoken after the call"},
|
||||
&ResponsesStreamEvent{Type: "response.completed", Response: &ResponsesResponse{Status: "completed"}},
|
||||
)
|
||||
|
||||
assert.Equal(t, "spoken after the call", collectAnthropicText(events))
|
||||
requireAnthropicBlockLifecycle(t, events)
|
||||
}
|
||||
@@ -20,12 +20,13 @@ const (
|
||||
BetaFastMode = "fast-mode-2026-02-01"
|
||||
|
||||
// 新增(对齐官方 CLI 2.1.9x 以来的流量)
|
||||
BetaPromptCachingScope = "prompt-caching-scope-2026-01-05"
|
||||
BetaEffort = "effort-2025-11-24"
|
||||
BetaRedactThinking = "redact-thinking-2026-02-12"
|
||||
BetaContextManagement = "context-management-2025-06-27"
|
||||
BetaThinkingBindingControls = "thinking-binding-controls-2026-08-01"
|
||||
BetaExtendedCacheTTL = "extended-cache-ttl-2025-04-11"
|
||||
BetaPromptCachingScope = "prompt-caching-scope-2026-01-05"
|
||||
BetaEffort = "effort-2025-11-24"
|
||||
BetaRedactThinking = "redact-thinking-2026-02-12"
|
||||
BetaContextManagement = "context-management-2025-06-27"
|
||||
BetaThinkingBindingControls = "thinking-binding-controls-2026-08-01"
|
||||
BetaMidConversationOutputConfig = "mid-conversation-output-config-2026-07-01"
|
||||
BetaExtendedCacheTTL = "extended-cache-ttl-2025-04-11"
|
||||
|
||||
// server-side refusal fallback beta 字段族(beta Messages API 专有)。
|
||||
// 客户端(Claude Code / SDK / OpenCode 等)会默认透传 body.fallbacks /
|
||||
@@ -101,6 +102,7 @@ func FullClaudeCodeMimicryBetas() []string {
|
||||
BetaEffort,
|
||||
BetaContextManagement,
|
||||
BetaThinkingBindingControls,
|
||||
BetaMidConversationOutputConfig,
|
||||
BetaExtendedCacheTTL,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,11 +83,10 @@ func ReadRequestBodyWithPrealloc(req *http.Request) ([]byte, error) {
|
||||
}
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(make([]byte, 0, capHint))
|
||||
if _, err := io.Copy(buf, req.Body); err != nil {
|
||||
raw, err := readRequestBodyChunks(req.Body, capHint, req.ContentLength)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
raw := buf.Bytes()
|
||||
|
||||
enc := strings.ToLower(strings.TrimSpace(req.Header.Get("Content-Encoding")))
|
||||
if enc == "" || enc == "identity" {
|
||||
@@ -106,6 +105,56 @@ func ReadRequestBodyWithPrealloc(req *http.Request) ([]byte, error) {
|
||||
return decoded, nil
|
||||
}
|
||||
|
||||
// Read bounded chunks as bytes arrive, then assemble the exact-size result.
|
||||
// This avoids doubling large buffers or eagerly allocating an untrusted
|
||||
// Content-Length before the corresponding bytes have arrived.
|
||||
func readRequestBodyChunks(reader io.Reader, initialCapacity int, contentLength int64) ([]byte, error) {
|
||||
capacity := initialCapacity
|
||||
var chunks [][]byte
|
||||
total := 0
|
||||
for {
|
||||
chunkCapacity := capacity
|
||||
if remaining := contentLength - int64(total); remaining >= 0 && remaining < int64(chunkCapacity) {
|
||||
chunkCapacity = int(remaining) + 1
|
||||
}
|
||||
chunk := make([]byte, chunkCapacity)
|
||||
n := 0
|
||||
var err error
|
||||
for n < len(chunk) && err == nil {
|
||||
var read int
|
||||
read, err = reader.Read(chunk[n:])
|
||||
n += read
|
||||
}
|
||||
if err != nil && err != io.EOF {
|
||||
return nil, err
|
||||
}
|
||||
if n > 0 {
|
||||
chunks = append(chunks, chunk[:n])
|
||||
total += n
|
||||
}
|
||||
if err != nil {
|
||||
if len(chunks) == 0 {
|
||||
return chunk[:0], nil
|
||||
}
|
||||
if len(chunks) == 1 {
|
||||
return chunks[0], nil
|
||||
}
|
||||
body := make([]byte, total)
|
||||
offset := 0
|
||||
for _, part := range chunks {
|
||||
offset += copy(body[offset:], part)
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
if capacity < requestBodyReadMaxInitCap {
|
||||
capacity *= 2
|
||||
if capacity > requestBodyReadMaxInitCap {
|
||||
capacity = requestBodyReadMaxInitCap
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReadLenientJSONRequestBodyWithPrealloc reads a request body and normalizes
|
||||
// JSON string control bytes before strict validation.
|
||||
func ReadLenientJSONRequestBodyWithPrealloc(req *http.Request, maxNormalizedBytes int64) ([]byte, error) {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package httputil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"testing"
|
||||
"testing/iotest"
|
||||
)
|
||||
|
||||
type bodyReaderOnly struct{ io.Reader }
|
||||
|
||||
func TestReadRequestBodyChunksPreservesContent(t *testing.T) {
|
||||
for _, size := range []int{0, 1, 512, 513, 1 << 20, (2 << 20) + 17} {
|
||||
body := bytes.Repeat([]byte{'x'}, size)
|
||||
for _, declared := range []int64{-1, 0, 1, int64(size), 1 << 40} {
|
||||
t.Run(fmt.Sprintf("size_%d_length_%d", size, declared), func(t *testing.T) {
|
||||
req := &http.Request{Body: io.NopCloser(bodyReaderOnly{bytes.NewReader(body)}), ContentLength: declared, Header: make(http.Header)}
|
||||
got, err := ReadRequestBodyWithPrealloc(req)
|
||||
if err != nil || !bytes.Equal(got, body) {
|
||||
t.Fatalf("body was lost or changed: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadRequestBodyChunksPreservesReadErrors(t *testing.T) {
|
||||
for _, readErr := range []error{io.ErrUnexpectedEOF, errors.New("connection reset")} {
|
||||
reader := io.MultiReader(bytes.NewReader([]byte("partial")), iotest.ErrReader(readErr))
|
||||
req := &http.Request{Body: io.NopCloser(reader), ContentLength: 100, Header: make(http.Header)}
|
||||
got, err := ReadRequestBodyWithPrealloc(req)
|
||||
if !errors.Is(err, readErr) || got != nil {
|
||||
t.Fatalf("truncated body accepted: body=%q err=%v", got, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadRequestBodyChunksPreservesMaxBytesReader(t *testing.T) {
|
||||
body := bytes.Repeat([]byte{'x'}, (2<<20)+1)
|
||||
req := &http.Request{Body: http.MaxBytesReader(nil, io.NopCloser(bytes.NewReader(body)), 2<<20), ContentLength: int64(len(body)), Header: make(http.Header)}
|
||||
got, err := ReadRequestBodyWithPrealloc(req)
|
||||
var limitErr *http.MaxBytesError
|
||||
if !errors.As(err, &limitErr) || limitErr.Limit != 2<<20 || got != nil {
|
||||
t.Fatalf("body limit changed: len=%d err=%v", len(got), err)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkReadLargeBody(b *testing.B) {
|
||||
for _, size := range []int{512 << 10, 69 << 20} {
|
||||
b.Run(fmt.Sprintf("bytes_%d", size), func(b *testing.B) {
|
||||
body := bytes.Repeat([]byte{'x'}, size)
|
||||
b.ReportAllocs()
|
||||
b.SetBytes(int64(size))
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
req := &http.Request{Body: io.NopCloser(bodyReaderOnly{bytes.NewReader(body)}), ContentLength: int64(size), Header: make(http.Header)}
|
||||
got, err := ReadRequestBodyWithPrealloc(req)
|
||||
if err != nil || !bytes.Equal(got, body) {
|
||||
b.Fatalf("body changed: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,8 @@ var DefaultModels = []Model{
|
||||
{ID: "gpt-image-1", Object: "model", Created: 1733875200, OwnedBy: "openai", Type: "model", DisplayName: "GPT Image 1"},
|
||||
{ID: "gpt-image-1.5", Object: "model", Created: 1735689600, OwnedBy: "openai", Type: "model", DisplayName: "GPT Image 1.5"},
|
||||
{ID: "gpt-image-2", Object: "model", Created: 1738368000, OwnedBy: "openai", Type: "model", DisplayName: "GPT Image 2"},
|
||||
{ID: "gpt-image-2.5-flare", Object: "model", Created: 1788825600, OwnedBy: "openai", Type: "model", DisplayName: "GPT Image 2.5 Flare"},
|
||||
{ID: "gpt-image-2.5-sunburst", Object: "model", Created: 1788825600, OwnedBy: "openai", Type: "model", DisplayName: "GPT Image 2.5 Sunburst"},
|
||||
}
|
||||
|
||||
// DefaultModelIDs returns the default model ID list
|
||||
|
||||
@@ -27,3 +27,8 @@ func TestDefaultModelsPreferConcreteGPT56SolForAccountTests(t *testing.T) {
|
||||
require.NotEmpty(t, DefaultModels)
|
||||
require.Equal(t, "gpt-5.6-sol", DefaultModels[0].ID)
|
||||
}
|
||||
|
||||
func TestDefaultModelsIncludeGPTImage25(t *testing.T) {
|
||||
require.Contains(t, DefaultModelIDs(), "gpt-image-2.5-flare")
|
||||
require.Contains(t, DefaultModelIDs(), "gpt-image-2.5-sunburst")
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package openai
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/http/httpguts"
|
||||
)
|
||||
|
||||
// CodexCLIUserAgentPrefixes matches Codex CLI User-Agent patterns
|
||||
@@ -198,6 +200,10 @@ func matchCodexClientHeaderStrictPrefixes(value string, prefixes []string) bool
|
||||
// UA 首段后配对,保留真实版本/OS/终端指纹;
|
||||
// 3. 均不命中 → ok=false,调用方应整体回退为默认官方身份。
|
||||
func PairCodexClientIdentity(userAgent string) (originator string, pairedUA string, ok bool) {
|
||||
// Validate before trimming so control bytes cannot become a valid identity.
|
||||
if !validCodexUserAgentValue(userAgent) {
|
||||
return "", "", false
|
||||
}
|
||||
ua := strings.TrimSpace(userAgent)
|
||||
slash := strings.IndexByte(ua, '/')
|
||||
if slash <= 0 {
|
||||
@@ -217,6 +223,15 @@ func PairCodexClientIdentity(userAgent string) (originator string, pairedUA stri
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
func validCodexUserAgentValue(value string) bool {
|
||||
if !httpguts.ValidHeaderFieldValue(value) {
|
||||
return false
|
||||
}
|
||||
// httpguts follows the legacy field-value grammar and permits obs-fold.
|
||||
// User-Agent is not an obs-folded header; reject CR/LF before forwarding.
|
||||
return !strings.ContainsAny(value, "\r\n")
|
||||
}
|
||||
|
||||
// codexOriginatorMaxLen 官方 clientInfo.name 均为短 ASCII 标识,远低于此上限。
|
||||
const codexOriginatorMaxLen = 64
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPairCodexClientIdentityRejectsInvalidHeaderBytes(t *testing.T) {
|
||||
for name, ua := range map[string]string{
|
||||
"crlf_suffix": "codex-tui/0.146.0 (Linux)\r\nX-Injected: value",
|
||||
"nul_suffix": "codex-tui/0.146.0 (Linux\x00)",
|
||||
"del_suffix": "codex-tui/0.146.0 terminal\x7f",
|
||||
"newline_prefix": "\ncodex-tui/0.146.0",
|
||||
"newline_suffix": "codex-tui/0.146.0\n",
|
||||
"control_in_trailer": "custom/0.146.0 bad\x01 (codex-tui; 0.146.0)",
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
originator, paired, ok := PairCodexClientIdentity(ua)
|
||||
require.False(t, ok)
|
||||
require.Empty(t, originator)
|
||||
require.Empty(t, paired)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPairCodexClientIdentityPreservesValidSuffix(t *testing.T) {
|
||||
for _, ua := range []string{
|
||||
"codex-tui/0.146.0 (Mac OS X 14.0; arm64) iTerm",
|
||||
"codex-tui/0.146.0 (Windows NT 10.0; x86_64) terminal",
|
||||
"codex-tui/0.146.0 (Linux)\tterminal",
|
||||
} {
|
||||
_, paired, ok := PairCodexClientIdentity(ua)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, ua, paired)
|
||||
}
|
||||
}
|
||||
@@ -2257,6 +2257,61 @@ func (r *accountRepository) ClearRateLimitIfObserved(ctx context.Context, id int
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// SetRateLimitedIfUnchanged atomically applies a rate-limit reset only while the
|
||||
// account still carries exactly the generation the caller observed: its
|
||||
// UpdatedAt row version, its RateLimitedAt and its RateLimitResetAt (nil means
|
||||
// that field is currently unset). It is the write-back CAS counterpart to
|
||||
// ClearRateLimitIfObserved: an async rate-limit reset (e.g. an Ollama Cloud
|
||||
// usage probe) must not overwrite a newer 429, an admin clear, a re-armed
|
||||
// generation, or a key/state change observed by another writer between the
|
||||
// caller's read and this write. The whole update is a single statement, so the
|
||||
// write itself is race-free. updated reports whether the write happened, and the
|
||||
// caller must ONLY send its scheduling notification when updated == true (this
|
||||
// method already performed the DB update; no further SetRateLimited call is
|
||||
// allowed, as a second unconditional write would reintroduce the race). No new
|
||||
// migration is required.
|
||||
func (r *accountRepository) SetRateLimitedIfUnchanged(
|
||||
ctx context.Context,
|
||||
id int64,
|
||||
expectedUpdatedAt time.Time,
|
||||
expectedLimitedAt, expectedResetAt *time.Time,
|
||||
newResetAt time.Time,
|
||||
) (bool, error) {
|
||||
preds := []dbpredicate.Account{dbaccount.IDEQ(id), dbaccount.UpdatedAtEQ(expectedUpdatedAt)}
|
||||
if expectedLimitedAt == nil {
|
||||
preds = append(preds, dbaccount.RateLimitedAtIsNil())
|
||||
} else {
|
||||
preds = append(preds, dbaccount.RateLimitedAtEQ(*expectedLimitedAt))
|
||||
}
|
||||
if expectedResetAt == nil {
|
||||
preds = append(preds, dbaccount.RateLimitResetAtIsNil())
|
||||
} else {
|
||||
preds = append(preds, dbaccount.RateLimitResetAtEQ(*expectedResetAt))
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
updated, err := r.client.Account.Update().
|
||||
Where(preds...).
|
||||
SetRateLimitedAt(now).
|
||||
SetRateLimitResetAt(newResetAt).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if updated == 0 {
|
||||
// The generation changed concurrently (cleared, re-armed, or the account
|
||||
// was otherwise updated elsewhere): do not announce anything, just
|
||||
// refresh the local scheduler snapshot.
|
||||
r.syncSchedulerAccountSnapshot(ctx, id)
|
||||
return false, nil
|
||||
}
|
||||
if err := enqueueSchedulerOutbox(ctx, r.sql, service.SchedulerOutboxEventAccountChanged, &id, nil, nil); err != nil {
|
||||
logger.LegacyPrintf("repository.account", "[SchedulerOutbox] enqueue rate limit failed: account=%d err=%v", id, err)
|
||||
}
|
||||
r.syncSchedulerAccountSnapshot(ctx, id)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (r *accountRepository) SetModelRateLimit(ctx context.Context, id int64, scope string, resetAt time.Time, reason ...string) error {
|
||||
if scope == "" {
|
||||
return nil
|
||||
|
||||
@@ -1540,6 +1540,41 @@ func (s *AccountRepoSuite) TestUpdateExtra_SchedulerNeutralSkipsOutboxAndSyncsFr
|
||||
s.Require().Equal("2026-03-11T10:00:00Z", cacheRecorder.accounts[account.ID].Extra["codex_usage_updated_at"])
|
||||
}
|
||||
|
||||
// Exercise the complete UpdateExtra -> PostgreSQL -> Redis metadata -> admission
|
||||
// path. A recorder-only cache would miss fields discarded by the slim projection.
|
||||
func (s *AccountRepoSuite) TestUpdateExtra_AnthropicThresholdRefreshesCandidateSnapshot() {
|
||||
now := time.Now().UTC().Truncate(time.Second)
|
||||
end := now.Add(time.Hour)
|
||||
account := mustCreateAccount(s.T(), s.client, &service.Account{
|
||||
Name: "threshold-refresh", Platform: service.PlatformAnthropic, Type: service.AccountTypeOAuth,
|
||||
Credentials: map[string]any{"account_scheduling_threshold": 60},
|
||||
Extra: map[string]any{"passive_usage_7d_utilization": .59, "passive_usage_7d_reset": end.Unix()},
|
||||
})
|
||||
cache := NewSchedulerCache(testRedis(s.T()))
|
||||
s.repo.schedulerCache = cache
|
||||
bucket := service.SchedulerBucket{GroupID: account.ID, Platform: service.PlatformAnthropic, Mode: service.SchedulerModeSingle}
|
||||
token, err := cache.CaptureBucketWriteToken(s.ctx, bucket)
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(cache.SetSnapshot(s.ctx, bucket, token, []service.Account{*account}))
|
||||
for _, step := range []struct {
|
||||
used float64
|
||||
reset time.Time
|
||||
paused bool
|
||||
}{
|
||||
{.59, end, false}, {.66, end, true}, {.66, now.Add(-time.Hour), false}, {.10, end, false},
|
||||
} {
|
||||
s.Require().NoError(s.repo.UpdateExtra(s.ctx, account.ID, map[string]any{
|
||||
"passive_usage_7d_utilization": step.used, "passive_usage_7d_reset": step.reset.Unix(),
|
||||
}))
|
||||
candidates, hit, err := cache.GetSnapshot(s.ctx, bucket)
|
||||
s.Require().NoError(err)
|
||||
s.Require().True(hit)
|
||||
s.Require().Len(candidates, 1)
|
||||
decision := service.EvaluateAccountSchedulingThreshold(candidates[0], map[string]int{service.PlatformAnthropic: 100}, now)
|
||||
s.Require().Equal(step.paused, decision.ShouldPause)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AccountRepoSuite) TestUpdateExtra_ExhaustedCodexSnapshotSyncsSchedulerCache() {
|
||||
account := mustCreateAccount(s.T(), s.client, &service.Account{
|
||||
Name: "acc-extra-codex-exhausted",
|
||||
|
||||
@@ -18,7 +18,7 @@ const (
|
||||
// ollamaCloudUsagePlatformsSQL 是 service.isOllamaCloudUsagePlatform 的 SQL
|
||||
// 镜像:Ollama Cloud key 允许挂在 openai/anthropic 与国产 OpenAI 兼容平台
|
||||
// 下复用。所有平台白名单 SQL 只允许引用本常量,不得各处重写字面量,防止漂移。
|
||||
ollamaCloudUsagePlatformsSQL = "'openai', 'anthropic', 'kimi', 'zhipu', 'deepseek'"
|
||||
ollamaCloudUsagePlatformsSQL = "'openai', 'anthropic', 'kimi', 'zhipu', 'deepseek', 'minimax'"
|
||||
ollamaCloudUsageEligibleSQL = `
|
||||
platform IN (` + ollamaCloudUsagePlatformsSQL + `)
|
||||
AND type = 'apikey'
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user