mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
经双视角评审裁决的收窄版 Phase-3 接缝(零行为变更):
- internal/gatewayplatform:Provider{Platform,Forward} 两方法 v1 接口 +
ForwardRequest{Parsed,Body,IsStickySession,SessionGroupID,SessionKey} +
构造期注册 Registry(重复 panic,运行期并发只读)
- 3 个单语句直返 adapter(anthropic/antigravity/gemini,错误零包裹——
BetaBlockedError/PromptTooLong 的 errors.As 链由 T4 特征化守护)
- Messages 端点两处平台分发改 registry 查找;Type!=APIKey 条件保留调用点
- OpenAI(独立 handler)、v1beta 端点、内核 internal/plugin 零触碰
门禁:T1-T5 特征化全绿、安全网 45 包、bench allocs 全部持平(266/329/93/134)。
39 lines
1.2 KiB
Go
39 lines
1.2 KiB
Go
package gatewayplatform
|
|
|
|
import "fmt"
|
|
|
|
// Registry 是 platform → Provider 的只读查找表:构造期一次性注册,
|
|
// 构造完成后并发只读(无锁)。
|
|
type Registry struct {
|
|
providers map[string]Provider
|
|
}
|
|
|
|
// NewRegistry 构造注册表。providers 中出现 nil Provider、空 Platform() 或
|
|
// 重复平台时 panic——装配错误必须在启动期(Wire 装配)暴露,而非请求期。
|
|
func NewRegistry(providers ...Provider) *Registry {
|
|
m := make(map[string]Provider, len(providers))
|
|
for _, p := range providers {
|
|
if p == nil {
|
|
panic("gatewayplatform: nil provider")
|
|
}
|
|
platform := p.Platform()
|
|
if platform == "" {
|
|
panic("gatewayplatform: provider with empty platform")
|
|
}
|
|
if _, dup := m[platform]; dup {
|
|
panic(fmt.Sprintf("gatewayplatform: duplicate provider for platform %q", platform))
|
|
}
|
|
m[platform] = p
|
|
}
|
|
return &Registry{providers: m}
|
|
}
|
|
|
|
// Get 返回平台对应的 Provider;未注册(或 nil Registry)返回 nil。
|
|
// 调用点须保证所查平台已在装配期注册(Wire 注册全集,miss 属编程错误)。
|
|
func (r *Registry) Get(platform string) Provider {
|
|
if r == nil {
|
|
return nil
|
|
}
|
|
return r.providers[platform]
|
|
}
|