feat: 接入 OpenCode Go 供应商(17 个 OpenAI 兼容文本模型)
- gateway.yaml 新增 providers.opencode(requires_session_header + 专属 UA), 收录 17 个 chat/completions 模型(glm/kimi/longcat/deepseek/mimo/hy), 全部并入 wjd(wjd 现 20 个文本模型) - config.py 加 ProviderConfig.requires_session_header 开关 - openai_compat 每次请求动态注入 x-opencode-session(缓存冻结坑),create_adapter 透传 - settings.py 面板视图/写回增补该字段(修 roundtrip 配置被改) - Settings.vue 默认模型下拉追加 17 个模型 - 测试:providers/wjd 断言更新;修 test_effective_rps_priority 共享 cfg 污染(finally 还原) - 单测 155 passed / 0 failed;typecheck 通过;真实 key 端到端非流式+流式皆通
This commit is contained in:
1 parent
c2f27e6e39
commit
8bab67d31d
9 files changed
+195
-23
No files matched your search
@@ -12,9 +12,10 @@ def create_adapter(
|
||||
) -> BaseAdapter:
|
||||
"""根据供应商配置创建对应适配器。
|
||||
|
||||
当前两个供应商(Agnes / 火山方舟)均为 OpenAI 兼容协议,
|
||||
当前三个供应商(Agnes / 火山方舟 / OpenCode Go)均为 OpenAI 兼容协议,
|
||||
统一使用 OpenAICompatAdapter,通过 base_url 和 extra_headers 区分。
|
||||
图像/视频端点通过 media 配置覆盖(Agnes 走 /videos + /agnesapi)。
|
||||
OpenCode Go 由 requires_session_header 触发每请求动态注入 x-opencode-session。
|
||||
"""
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token}",
|
||||
@@ -28,4 +29,5 @@ def create_adapter(
|
||||
headers=headers,
|
||||
timeout=timeout,
|
||||
media=pconfig.media,
|
||||
requires_session_header=pconfig.requires_session_header,
|
||||
)
|
||||
@@ -11,6 +11,7 @@ Agnes / 火山方舟 两家均使用 OpenAI 兼容 API
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import uuid
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
|
||||
@@ -28,9 +29,22 @@ class OpenAICompatAdapter(BaseAdapter):
|
||||
headers: dict[str, str] | None = None,
|
||||
timeout: int = 120,
|
||||
media=None,
|
||||
requires_session_header: bool = False,
|
||||
):
|
||||
super().__init__(name=name, base_url=base_url, timeout=timeout, media=media)
|
||||
self._headers: dict[str, str] = headers or {}
|
||||
# OpenCode Go 要求每个 /chat/completions 带稳定 x-opencode-session。
|
||||
# 不能放静态 headers(_get_adapter 会缓存适配器、冻结 header),
|
||||
# 所以在每次请求时动态生成一个 UUID 会话头。
|
||||
self._requires_session = requires_session_header
|
||||
|
||||
def _req_headers(self) -> dict[str, str]:
|
||||
"""每次请求的 headers。需要会话头的供应商动态注入 x-opencode-session。"""
|
||||
if not self._requires_session:
|
||||
return self._headers
|
||||
headers = dict(self._headers)
|
||||
headers["x-opencode-session"] = uuid.uuid4().hex
|
||||
return headers
|
||||
|
||||
def _build_payload(
|
||||
self,
|
||||
@@ -87,7 +101,7 @@ class OpenAICompatAdapter(BaseAdapter):
|
||||
)
|
||||
# Bug 4 修复: 用 lease 占用 client,热重载 close 会等本次请求结束才真正关连接
|
||||
async with self._lease() as client:
|
||||
resp = await client.post("/chat/completions", json=payload, headers=self._headers)
|
||||
resp = await client.post("/chat/completions", json=payload, headers=self._req_headers())
|
||||
resp.raise_for_status()
|
||||
data = json_or_raise(resp)
|
||||
|
||||
@@ -120,7 +134,7 @@ class OpenAICompatAdapter(BaseAdapter):
|
||||
# Bug 4 修复: lease 在整个流式期间持有占用,热重载/关闭不会掐断在途流
|
||||
async with self._lease() as client:
|
||||
async with client.stream(
|
||||
"POST", "/chat/completions", json=payload, headers=self._headers
|
||||
"POST", "/chat/completions", json=payload, headers=self._req_headers()
|
||||
) as resp:
|
||||
resp.raise_for_status()
|
||||
async for line in resp.aiter_lines():
|
||||
|
||||
Reference in new issue
Block a user