Files
ai-gateway-go/internal/provider/openai/adapter.go
T
superidou 5759c1862e AI Gateway Go 0.10.0 源码快照 + 旗舰版需求规划报告
M0-M7 已完成:核心网关(身份/RBAC/TOTP/OIDC/SAML/Provider/配额/路由/内容策略/审计/定价)+ 资源市场(MCP/Skills/数字员工)。
含 22 个 PostgreSQL 迁移、管理端/门户端前端源码、OpenAPI 契约、部署 compose。

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-12 11:45:54 +08:00

56 lines
1.3 KiB
Go

package openai
import (
"net/http"
"net/url"
"strings"
"aigateway.local/core/internal/provider"
)
type Adapter struct {
target *url.URL
apiKey string
}
func New(baseURL, apiKey string) (*Adapter, error) {
target, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
return &Adapter{target: target, apiKey: apiKey}, nil
}
func (a *Adapter) Name() string { return "openai-compatible" }
func (a *Adapter) Target() *url.URL {
copy := *a.target
return &copy
}
func (a *Adapter) Capabilities() []provider.Capability {
return []provider.Capability{
provider.CapabilityChat,
provider.CapabilityResponses,
provider.CapabilityEmbeddings,
provider.CapabilityMessages,
provider.CapabilityModels,
}
}
func (a *Adapter) Prepare(request *http.Request) {
request.Header.Del("X-Gateway-API-Key")
request.Header.Del("X-Gateway-Provider")
basePath := strings.TrimRight(a.target.Path, "/")
duplicatedVersionPrefix := basePath + "/v1/"
if strings.HasSuffix(basePath, "/v1") && strings.HasPrefix(request.URL.Path, duplicatedVersionPrefix) {
request.URL.Path = basePath + "/" + strings.TrimPrefix(request.URL.Path, duplicatedVersionPrefix)
request.URL.RawPath = ""
}
if a.apiKey != "" {
request.Header.Set("Authorization", "Bearer "+a.apiKey)
}
}
var _ provider.Adapter = (*Adapter)(nil)