mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
config: make zen/go preference configurable via prefer field
Add a top-level "prefer" config field ("zen" or "go", default "go") that
controls which upstream is used when a model is available in both pools,
including during the temporary model-discovery fallback. Previously Zen
was always preferred.
This commit is contained in:
1 parent
e220e7a040
commit
adb0edb64a
5 files changed
+24
-7
No files matched your search
@@ -9,7 +9,7 @@
|
||||
- 支持普通响应和 SSE 流式响应
|
||||
- 支持文本、图片、工具定义、工具调用和工具结果转换
|
||||
- 分离配置 Zen key 池与 Zen Go key 池
|
||||
- 模型同时存在于两个上游时优先使用 Zen
|
||||
- 模型同时存在于两个上游时按 `prefer` 配置优先使用 Go 或 Zen(默认 Go)
|
||||
- 支持直连、HTTP、HTTPS、SOCKS5 和 SOCKS5H 代理
|
||||
- 将 key 自动均衡绑定到代理,保持连接亲和性
|
||||
- 使用无锁轮询分配并发请求
|
||||
@@ -51,6 +51,7 @@ cp config.example.json config.json
|
||||
"server_keys": ["change-this-local-key"],
|
||||
"zen_keys": ["sk-your-zen-key"],
|
||||
"go_keys": [],
|
||||
"prefer": "go",
|
||||
"proxies": ["direct"],
|
||||
"upstream": {
|
||||
"zen": "https://opencode.ai/zen",
|
||||
@@ -86,6 +87,7 @@ cp config.example.json config.json
|
||||
| `server_keys` | 调用本代理时使用的本地 API key 列表。它们只用于本地鉴权,不会发送给 OpenCode。 |
|
||||
| `zen_keys` | OpenCode Zen API key 池。允许配置多个 key。 |
|
||||
| `go_keys` | OpenCode Zen Go API key 池。没有 Go key 时可以使用空数组。 |
|
||||
| `prefer` | 模型同时存在于 Zen 与 Go 时优先使用的上游,值为 `go` 或 `zen`,默认 `go`。仅存在于某一池时不受影响。 |
|
||||
| `proxies` | 上游代理列表。支持 `direct`、`http://`、`https://`、`socks5://` 和 `socks5h://`。URL 可以包含代理用户名和密码。 |
|
||||
|
||||
`server_keys` 至少需要一个值;`zen_keys` 和 `go_keys` 至少有一个池不能为空。
|
||||
@@ -144,7 +146,7 @@ SOCKS5 代理示例:
|
||||
}
|
||||
```
|
||||
|
||||
模型同时存在于 Zen 与 Go 时优先选择 Zen;仅存在于 Go 时才使用 Go key 池。
|
||||
模型同时存在于 Zen 与 Go 时按 `prefer` 配置选择:值为 `go` 时优先 Go,值为 `zen` 时优先 Zen(默认 `go`)。仅存在于某一池时才使用该池的 key。
|
||||
|
||||
### `performance`
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
"server_keys": ["change-this-local-key"],
|
||||
"zen_keys": ["sk-your-zen-key"],
|
||||
"go_keys": [],
|
||||
"prefer": "go",
|
||||
"proxies": ["direct"],
|
||||
"upstream": {
|
||||
"zen": "https://opencode.ai/zen",
|
||||
|
||||
@@ -20,6 +20,7 @@ type Config struct {
|
||||
Models ModelsConfig `json:"models"`
|
||||
Performance PerformanceConfig `json:"performance"`
|
||||
Logging LoggingConfig `json:"logging"`
|
||||
Prefer Tier `json:"prefer"`
|
||||
}
|
||||
|
||||
type UpstreamConfig struct {
|
||||
@@ -63,6 +64,7 @@ func LoadConfig(path string) (Config, error) {
|
||||
Models: ModelsConfig{RefreshSeconds: 300, Protocols: map[string]string{}},
|
||||
Performance: PerformanceConfig{MaxIdleConns: 2048, MaxIdleConnsPerHost: 256, MaxConnsPerHost: 0, IdleConnTimeoutSeconds: 120, ConnectTimeoutSeconds: 5, FailureCooldownSeconds: 15},
|
||||
Logging: LoggingConfig{Level: "info"},
|
||||
Prefer: TierGo,
|
||||
}
|
||||
dec := json.NewDecoder(strings.NewReader(string(data)))
|
||||
dec.DisallowUnknownFields()
|
||||
@@ -73,6 +75,9 @@ func LoadConfig(path string) (Config, error) {
|
||||
trimList(&cfg.ZenKeys)
|
||||
trimList(&cfg.GoKeys)
|
||||
trimList(&cfg.Proxies)
|
||||
if cfg.Prefer != TierZen && cfg.Prefer != TierGo {
|
||||
return Config{}, errors.New("prefer must be \"zen\" or \"go\"")
|
||||
}
|
||||
if cfg.Listen == "" {
|
||||
return Config{}, errors.New("listen must not be empty")
|
||||
}
|
||||
|
||||
+1
-1
@@ -52,7 +52,7 @@ func NewGateway(cfg Config, logger *slog.Logger) (*Gateway, error) {
|
||||
transports: transports,
|
||||
zenNodes: zenNodes,
|
||||
goNodes: goNodes,
|
||||
catalog: newModelCatalog(cfg.Models.Protocols),
|
||||
catalog: newModelCatalog(cfg.Prefer, cfg.Models.Protocols),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -44,14 +44,15 @@ type modelCatalog struct {
|
||||
goModels map[string]bool
|
||||
protocols map[string]Protocol
|
||||
updatedAt time.Time
|
||||
prefer Tier
|
||||
}
|
||||
|
||||
func newModelCatalog(overrides map[string]string) *modelCatalog {
|
||||
func newModelCatalog(prefer Tier, overrides map[string]string) *modelCatalog {
|
||||
protocols := make(map[string]Protocol, len(overrides))
|
||||
for model, protocol := range overrides {
|
||||
protocols[model] = Protocol(protocol)
|
||||
}
|
||||
return &modelCatalog{zen: map[string]bool{}, goModels: map[string]bool{}, protocols: protocols}
|
||||
return &modelCatalog{zen: map[string]bool{}, goModels: map[string]bool{}, protocols: protocols, prefer: prefer}
|
||||
}
|
||||
|
||||
func (c *modelCatalog) Replace(zen, goModels []string) {
|
||||
@@ -73,14 +74,22 @@ func (c *modelCatalog) Route(model string, hasZenKeys, hasGoKeys bool) (modelRou
|
||||
if protocol == "" {
|
||||
protocol = inferProtocol(model)
|
||||
}
|
||||
// When a model exists on both tiers, honor the configured priority.
|
||||
preferGo := c.prefer == TierGo
|
||||
if preferGo && c.goModels[model] && hasGoKeys {
|
||||
return modelRoute{ID: model, Tier: TierGo, Protocol: protocol}, nil
|
||||
}
|
||||
if c.zen[model] && hasZenKeys {
|
||||
return modelRoute{ID: model, Tier: TierZen, Protocol: protocol}, nil
|
||||
}
|
||||
if c.goModels[model] && hasGoKeys {
|
||||
if !preferGo && c.goModels[model] && hasGoKeys {
|
||||
return modelRoute{ID: model, Tier: TierGo, Protocol: protocol}, nil
|
||||
}
|
||||
// Model discovery can temporarily fail. Prefer Zen as requested, then Go.
|
||||
// Model discovery can temporarily fail. Honor the configured priority.
|
||||
if len(c.zen) == 0 && len(c.goModels) == 0 {
|
||||
if preferGo && hasGoKeys {
|
||||
return modelRoute{ID: model, Tier: TierGo, Protocol: protocol}, nil
|
||||
}
|
||||
if hasZenKeys {
|
||||
return modelRoute{ID: model, Tier: TierZen, Protocol: protocol}, nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user