Skip to content

Commit 4600a53

Browse files
committed
feat(gateway): 模型 id 前缀 cn: → workbuddy:(用户可读)
- /v1/models 账号池目录统一 workbuddy:<model> 前缀,客户端里一眼 可辨;cn/global 同名模型自然合并(路由本就不分区域),region 字段 仍保留区域信息 - 路由兼容:workbuddy:/cn:/global: 三前缀与纯模型名都按账号池调度, 旧客户端配置的 cn:* 不断 - embeddings 同步剥 workbuddy:/cn: 前缀;global: 保持拒绝 - 新增 TestGatewayModels::test_legacy_cn_prefix_still_routes 四前缀 路由断言,51/51
1 parent f21417f commit 4600a53

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

‎tests/test_wbspy.py‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,15 +441,33 @@ def test_pool_models_carry_name(self):
441441
from wbspy.gateway import create_app
442442
from wbspy.pool import AccountPool
443443
app = create_app(AccountPool([]), api_key="")
444-
fake_pool = [{"id": "cn:fast-model", "name": "快速", "region": "cn"}]
444+
fake_pool = [{"id": "workbuddy:fast-model", "name": "快速", "region": "cn"}]
445445
pool = AccountPool([])
446446
app = create_app(pool, api_key="")
447447
with patch("wbspy.gateway.aggregate.models", return_value=[]), \
448448
patch.object(pool, "models", return_value=fake_pool):
449449
row = TestClient(app).get("/v1/models").json()["data"][0]
450450
self.assertEqual(row["name"], "快速")
451451
self.assertEqual(row["region"], "cn")
452-
self.assertEqual(row["id"], "cn:fast-model")
452+
self.assertEqual(row["id"], "workbuddy:fast-model")
453+
454+
def test_legacy_cn_prefix_still_routes(self):
455+
"""旧客户端配置的 cn:/global: 前缀继续按账号池路由(不断)。"""
456+
from fastapi.testclient import TestClient
457+
from wbspy.gateway import create_app
458+
from wbspy.pool import AccountPool
459+
460+
seen: list[str] = []
461+
462+
class _P(AccountPool):
463+
def chat_failover(self, raw, *_a, **_k):
464+
seen.append(raw["model"])
465+
raise RuntimeError("stop-here") # 到池即停,只验证前缀剥除
466+
467+
c = TestClient(create_app(_P([]), api_key=""), raise_server_exceptions=False)
468+
for mid in ("cn:auto", "global:auto", "workbuddy:auto", "auto"):
469+
c.post("/v1/chat/completions", json={"model": mid, "messages": []})
470+
self.assertEqual(seen[-1], "auto", mid)
453471

454472

455473
class TestGatewayKey(unittest.TestCase):

‎wbspy/gateway.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
33
统一端点,三类模型同池路由:
44
5-
* ``cn:*`` / ``global:*`` — WorkBuddy 账号池(多账号轮换容错)
5+
* ``workbuddy:*`` — WorkBuddy 账号池(多账号轮换容错)
66
* ``<provider>/<model>`` — 76 家聚合渠道(按 key 路由,OpenAI 兼容透传)
7-
* 纯模型名(无前缀) — 兼容写法,等价 ``cn:<model>``
7+
* 纯模型名(无前缀) — 兼容写法,等价 ``workbuddy:<model>``
8+
* 兼容旧 id:``cn:*`` / ``global:*`` 仍按账号池路由(旧客户端配置不断)
89
910
端点:
1011
@@ -237,12 +238,12 @@ async def chat_completions(request: Request, authorization: str = Header("")) ->
237238
model = raw["model"]
238239

239240
# ── 路由一:聚合渠道 <provider>/<model> ─────────────────────────────
240-
if "/" in model and not model.startswith(("cn:", "global:")):
241+
if "/" in model and not model.startswith(("workbuddy:", "cn:", "global:")):
241242
return _aggregate_chat(raw)
242243

243244
# ── 路由二:WorkBuddy 账号池 ────────────────────────────────────────
244-
# 纯模型名等价 cn:<model>;区域前缀剥掉后由池按各账号可用性调度
245-
if model.split(":", 1)[0] in ("cn", "global") and len(model) > 2:
245+
# 纯模型名等价 workbuddy:<model>;区域/命名前缀剥掉后由池按各账号可用性调度
246+
if model.split(":", 1)[0] in ("workbuddy", "cn", "global") and ":" in model:
246247
raw = {**raw, "model": model.split(":", 1)[1]}
247248
raw, _trimmed = trim_context(raw)
248249

@@ -280,9 +281,12 @@ async def embeddings(request: Request, authorization: str = Header("")) -> Any:
280281
except ValueError:
281282
raise HTTPException(400, "请求须为 JSON 对象。")
282283
model = raw.get("model", "")
283-
if isinstance(model, str) and model.startswith("global:"):
284+
if isinstance(model, str) and model.split(":", 1)[0] == "global" and ":" in model:
284285
raise HTTPException(400, "unsupported_region",
285286
"向量模型目前仅国内版可用,不接受 global: 前缀。")
287+
if isinstance(model, str) and model.split(":", 1)[0] in ("workbuddy", "cn") \
288+
and ":" in model:
289+
raw = {**raw, "model": model.split(":", 1)[1]} # 剥命名前缀再路由
286290
inputs = raw.get("input", [])
287291
inputs = [inputs] if isinstance(inputs, str) else list(inputs)
288292
doc = pool.pick(model)

‎wbspy/pool.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,14 @@ def chat_failover(self, payload: dict):
148148
raise # 渠道拒绝重试过 / 上游 5xx 不换号
149149

150150
def models(self) -> list[dict]:
151-
"""聚合全部账号的模型目录(去重)。"""
151+
"""聚合全部账号的模型目录(去重)。统一 ``workbuddy:`` 前缀——
152+
客户端里一眼可辨;区域信息在 region 字段(路由时本就不分区域,
153+
cn/global 同名模型自然合并)。"""
152154
seen: dict[str, dict] = {}
153155
for doc in self.healthy():
154156
try:
155157
for row in self.clients[_key(doc)].models():
156-
key = f"{'global' if doc.region == 'global' else 'cn'}:{row['id']}"
158+
key = f"workbuddy:{row['id']}"
157159
seen.setdefault(key, {**row, "id": key})
158160
except UpstreamError:
159161
continue

0 commit comments

Comments
 (0)