Skip to content

Commit f3684f5

Browse files
committed
update
1 parent d548d64 commit f3684f5

File tree

13 files changed

+640
-15
lines changed

13 files changed

+640
-15
lines changed

backend/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/go-playground/validator/v10 v10.14.0 // indirect
2424
github.com/go-sql-driver/mysql v1.7.0 // indirect
2525
github.com/goccy/go-json v0.10.2 // indirect
26+
github.com/google/uuid v1.6.0 // indirect
2627
github.com/jinzhu/inflection v1.0.0 // indirect
2728
github.com/jinzhu/now v1.1.5 // indirect
2829
github.com/json-iterator/go v1.1.12 // indirect

backend/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
3737
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
3838
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3939
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
40+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
41+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4042
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
4143
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
4244
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=

backend/internal/adapters/anthropic_adapter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (a *AnthropicAdapter) ProcessRequest() (*services.TargetRequest, error) {
6262
}
6363

6464
// 3. Build the target request.
65-
headers := utils.FilterRequestHeaders(a.c.Request.Header, []string{"x-api-key", "Authorization", "x-anthropic-api-key"})
65+
headers := utils.FilterRequestHeaders(a.c.Request.Header, []string{"x-api-key", "Authorization", "x-anthropic-api-key", "accept-encoding"})
6666

6767
// 优先使用数据库中为该proxyConfig保存的APIKeyName, 否则回退到默认值
6868
keyName := "x-api-key"

backend/internal/adapters/gemini_adapter.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"io"
66
"strings"
77

8-
"api-key-rotator/backend/internal/infrastructure/cache"
98
"api-key-rotator/backend/internal/config"
9+
"api-key-rotator/backend/internal/infrastructure/cache"
1010
"api-key-rotator/backend/internal/logger"
1111
"api-key-rotator/backend/internal/models"
1212
"api-key-rotator/backend/internal/services"
@@ -33,7 +33,7 @@ func NewGeminiAdapter(cfg *config.Config, db *gorm.DB, cacheClient cache.CacheIn
3333
func (a *GeminiAdapter) ProcessRequest() (*services.TargetRequest, error) {
3434
// 1. 代理访问认证 (劫持 'x-goog-api-key' Header)
3535
proxyKey := a.c.GetHeader("x-goog-api-key")
36-
36+
3737
validKeys := a.cfg.GetGlobalProxyKeys()
3838
isValidKey := false
3939
for _, key := range validKeys {
@@ -45,15 +45,15 @@ func (a *GeminiAdapter) ProcessRequest() (*services.TargetRequest, error) {
4545
if !isValidKey {
4646
return nil, fmt.Errorf("invalid Proxy Key. Provide it via the 'key' URL query parameter")
4747
}
48-
48+
4949
// 2. 轮询上游密钥
5050
upstreamKey, err := a.RotateUpstreamKey()
5151
if err != nil {
5252
return nil, err
5353
}
5454

5555
// 3. 构建目标请求 (偷梁换柱)
56-
headers := utils.FilterRequestHeaders(a.c.Request.Header, []string{"x-goog-api-key"})
56+
headers := utils.FilterRequestHeaders(a.c.Request.Header, []string{"x-goog-api-key", "accept-encoding"})
5757

5858
// 优先使用数据库中为该proxyConfig保存的APIKeyName, 否则回退到默认值
5959
keyName := "x-goog-api-key"
@@ -110,4 +110,4 @@ func (a *GeminiAdapter) ProcessRequest() (*services.TargetRequest, error) {
110110
Params: params,
111111
Body: body,
112112
}, nil
113-
}
113+
}

backend/internal/adapters/openai_adapter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"io"
77
"strings"
88

9-
"api-key-rotator/backend/internal/infrastructure/cache"
109
"api-key-rotator/backend/internal/config"
10+
"api-key-rotator/backend/internal/infrastructure/cache"
1111
"api-key-rotator/backend/internal/logger"
1212
"api-key-rotator/backend/internal/models"
1313
"api-key-rotator/backend/internal/services"
@@ -87,7 +87,7 @@ func (a *OpenAIAdapter) ProcessRequest() (*services.TargetRequest, error) {
8787
}
8888

8989
// 3. 构建目标请求 (偷梁换柱)
90-
headers := utils.FilterRequestHeaders(a.c.Request.Header, []string{"authorization"})
90+
headers := utils.FilterRequestHeaders(a.c.Request.Header, []string{"authorization", "accept-encoding"})
9191

9292
// 优先使用数据库中为该proxyConfig保存的APIKeyName, 否则回退到默认值
9393
keyName := "Authorization"
@@ -147,4 +147,4 @@ func (a *OpenAIAdapter) ProcessRequest() (*services.TargetRequest, error) {
147147
Params: params,
148148
Body: bodyBytes,
149149
}, nil
150-
}
150+
}

backend/internal/converters/converter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
_ "api-key-rotator/backend/internal/converters/formats/anthropic"
99
_ "api-key-rotator/backend/internal/converters/formats/gemini"
1010
_ "api-key-rotator/backend/internal/converters/formats/openai"
11+
_ "api-key-rotator/backend/internal/converters/formats/openai_responses"
1112
)
1213

1314
// Converter handles format conversion between different LLM API formats

backend/internal/converters/formats/openai/handler.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,25 @@ func (h *Handler) BuildResponse(resp *formats.UniversalResponse) ([]byte, error)
198198
return json.Marshal(openaiResp)
199199
}
200200

201-
// GetAPIPath implements FormatHandler
201+
// GetAPIPath implements FormatHandler - converts client action to OpenAI API path
202202
func (h *Handler) GetAPIPath(action string) string {
203-
return action
203+
switch action {
204+
case "v1/messages", "messages":
205+
// Anthropic endpoint -> OpenAI endpoint
206+
return "chat/completions"
207+
case "v1/chat/completions":
208+
return "chat/completions"
209+
default:
210+
return action
211+
}
204212
}
205213

206-
// GetClientAction implements FormatHandler
214+
// GetClientAction implements FormatHandler - converts API path to client action
207215
func (h *Handler) GetClientAction(apiPath string) string {
208-
return apiPath
216+
switch apiPath {
217+
case "chat/completions", "v1/chat/completions":
218+
return "v1/chat/completions"
219+
default:
220+
return apiPath
221+
}
209222
}

0 commit comments

Comments
 (0)