mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-09 09:34:46 +08:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
269a659200 | ||
|
|
2c31bf46b5 | ||
|
|
8f6639f825 | ||
|
|
fc17d9d7df | ||
|
|
ab092e88a8 | ||
|
|
56a1e29cdd | ||
|
|
0059a232a6 | ||
|
|
45676fdc8d | ||
|
|
e32c5f534f | ||
|
|
a55cfebd09 |
3
.github/workflows/release.yml
vendored
3
.github/workflows/release.yml
vendored
@@ -222,8 +222,9 @@ jobs:
|
|||||||
REPO="${{ github.repository }}"
|
REPO="${{ github.repository }}"
|
||||||
GHCR_IMAGE="ghcr.io/${REPO,,}" # ${,,} converts to lowercase
|
GHCR_IMAGE="ghcr.io/${REPO,,}" # ${,,} converts to lowercase
|
||||||
|
|
||||||
# 获取 tag message 内容
|
# 获取 tag message 内容并转义 Markdown 特殊字符
|
||||||
TAG_MESSAGE='${{ steps.tag_message.outputs.message }}'
|
TAG_MESSAGE='${{ steps.tag_message.outputs.message }}'
|
||||||
|
TAG_MESSAGE=$(echo "$TAG_MESSAGE" | sed 's/\([_*`\[]\)/\\\1/g')
|
||||||
|
|
||||||
# 限制消息长度(Telegram 消息限制 4096 字符,预留空间给头尾固定内容)
|
# 限制消息长度(Telegram 消息限制 4096 字符,预留空间给头尾固定内容)
|
||||||
if [ ${#TAG_MESSAGE} -gt 3500 ]; then
|
if [ ${#TAG_MESSAGE} -gt 3500 ]; then
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
0.1.46
|
0.1.61
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
package response
|
package response
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -74,6 +75,12 @@ func ErrorFrom(c *gin.Context, err error) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
statusCode, status := infraerrors.ToHTTP(err)
|
statusCode, status := infraerrors.ToHTTP(err)
|
||||||
|
|
||||||
|
// Log internal errors with full details for debugging
|
||||||
|
if statusCode >= 500 && c.Request != nil {
|
||||||
|
log.Printf("[ERROR] %s %s\n Error: %s", c.Request.Method, c.Request.URL.Path, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
ErrorWithDetails(c, statusCode, status.Message, status.Reason, status.Metadata)
|
ErrorWithDetails(c, statusCode, status.Message, status.Reason, status.Metadata)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ package repository
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors"
|
||||||
"github.com/Wei-Shaw/sub2api/internal/pkg/openai"
|
"github.com/Wei-Shaw/sub2api/internal/pkg/openai"
|
||||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||||
"github.com/imroc/req/v3"
|
"github.com/imroc/req/v3"
|
||||||
@@ -22,7 +22,7 @@ type openaiOAuthService struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *openaiOAuthService) ExchangeCode(ctx context.Context, code, codeVerifier, redirectURI, proxyURL string) (*openai.TokenResponse, error) {
|
func (s *openaiOAuthService) ExchangeCode(ctx context.Context, code, codeVerifier, redirectURI, proxyURL string) (*openai.TokenResponse, error) {
|
||||||
client := createOpenAIReqClient(s.tokenURL, proxyURL)
|
client := createOpenAIReqClient(proxyURL)
|
||||||
|
|
||||||
if redirectURI == "" {
|
if redirectURI == "" {
|
||||||
redirectURI = openai.DefaultRedirectURI
|
redirectURI = openai.DefaultRedirectURI
|
||||||
@@ -39,23 +39,24 @@ func (s *openaiOAuthService) ExchangeCode(ctx context.Context, code, codeVerifie
|
|||||||
|
|
||||||
resp, err := client.R().
|
resp, err := client.R().
|
||||||
SetContext(ctx).
|
SetContext(ctx).
|
||||||
|
SetHeader("User-Agent", "codex-cli/0.91.0").
|
||||||
SetFormDataFromValues(formData).
|
SetFormDataFromValues(formData).
|
||||||
SetSuccessResult(&tokenResp).
|
SetSuccessResult(&tokenResp).
|
||||||
Post(s.tokenURL)
|
Post(s.tokenURL)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("request failed: %w", err)
|
return nil, infraerrors.Newf(http.StatusBadGateway, "OPENAI_OAUTH_REQUEST_FAILED", "request failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !resp.IsSuccessState() {
|
if !resp.IsSuccessState() {
|
||||||
return nil, fmt.Errorf("token exchange failed: status %d, body: %s", resp.StatusCode, resp.String())
|
return nil, infraerrors.Newf(http.StatusBadGateway, "OPENAI_OAUTH_TOKEN_EXCHANGE_FAILED", "token exchange failed: status %d, body: %s", resp.StatusCode, resp.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
return &tokenResp, nil
|
return &tokenResp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *openaiOAuthService) RefreshToken(ctx context.Context, refreshToken, proxyURL string) (*openai.TokenResponse, error) {
|
func (s *openaiOAuthService) RefreshToken(ctx context.Context, refreshToken, proxyURL string) (*openai.TokenResponse, error) {
|
||||||
client := createOpenAIReqClient(s.tokenURL, proxyURL)
|
client := createOpenAIReqClient(proxyURL)
|
||||||
|
|
||||||
formData := url.Values{}
|
formData := url.Values{}
|
||||||
formData.Set("grant_type", "refresh_token")
|
formData.Set("grant_type", "refresh_token")
|
||||||
@@ -67,29 +68,25 @@ func (s *openaiOAuthService) RefreshToken(ctx context.Context, refreshToken, pro
|
|||||||
|
|
||||||
resp, err := client.R().
|
resp, err := client.R().
|
||||||
SetContext(ctx).
|
SetContext(ctx).
|
||||||
|
SetHeader("User-Agent", "codex-cli/0.91.0").
|
||||||
SetFormDataFromValues(formData).
|
SetFormDataFromValues(formData).
|
||||||
SetSuccessResult(&tokenResp).
|
SetSuccessResult(&tokenResp).
|
||||||
Post(s.tokenURL)
|
Post(s.tokenURL)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("request failed: %w", err)
|
return nil, infraerrors.Newf(http.StatusBadGateway, "OPENAI_OAUTH_REQUEST_FAILED", "request failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !resp.IsSuccessState() {
|
if !resp.IsSuccessState() {
|
||||||
return nil, fmt.Errorf("token refresh failed: status %d, body: %s", resp.StatusCode, resp.String())
|
return nil, infraerrors.Newf(http.StatusBadGateway, "OPENAI_OAUTH_TOKEN_REFRESH_FAILED", "token refresh failed: status %d, body: %s", resp.StatusCode, resp.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
return &tokenResp, nil
|
return &tokenResp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createOpenAIReqClient(tokenURL, proxyURL string) *req.Client {
|
func createOpenAIReqClient(proxyURL string) *req.Client {
|
||||||
forceHTTP2 := false
|
|
||||||
if parsedURL, err := url.Parse(tokenURL); err == nil {
|
|
||||||
forceHTTP2 = strings.EqualFold(parsedURL.Scheme, "https")
|
|
||||||
}
|
|
||||||
return getSharedReqClient(reqClientOptions{
|
return getSharedReqClient(reqClientOptions{
|
||||||
ProxyURL: proxyURL,
|
ProxyURL: proxyURL,
|
||||||
Timeout: 120 * time.Second,
|
Timeout: 120 * time.Second,
|
||||||
ForceHTTP2: forceHTTP2,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,21 +77,9 @@ func TestGetSharedReqClient_ImpersonateAndProxy(t *testing.T) {
|
|||||||
require.Equal(t, "http://proxy.local:8080|4s|true|false", buildReqClientKey(opts))
|
require.Equal(t, "http://proxy.local:8080|4s|true|false", buildReqClientKey(opts))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateOpenAIReqClient_ForceHTTP2Enabled(t *testing.T) {
|
|
||||||
sharedReqClients = sync.Map{}
|
|
||||||
client := createOpenAIReqClient("https://auth.openai.com/oauth/token", "http://proxy.local:8080")
|
|
||||||
require.Equal(t, "2", forceHTTPVersion(t, client))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreateOpenAIReqClient_ForceHTTP2DisabledForHTTP(t *testing.T) {
|
|
||||||
sharedReqClients = sync.Map{}
|
|
||||||
client := createOpenAIReqClient("http://localhost/oauth/token", "http://proxy.local:8080")
|
|
||||||
require.Equal(t, "", forceHTTPVersion(t, client))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreateOpenAIReqClient_Timeout120Seconds(t *testing.T) {
|
func TestCreateOpenAIReqClient_Timeout120Seconds(t *testing.T) {
|
||||||
sharedReqClients = sync.Map{}
|
sharedReqClients = sync.Map{}
|
||||||
client := createOpenAIReqClient("https://auth.openai.com/oauth/token", "http://proxy.local:8080")
|
client := createOpenAIReqClient("http://proxy.local:8080")
|
||||||
require.Equal(t, 120*time.Second, client.GetClient().Timeout)
|
require.Equal(t, 120*time.Second, client.GetClient().Timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3372,19 +3372,12 @@ func (s *GatewayService) parseSSEUsage(data string, usage *ClaudeUsage) {
|
|||||||
} `json:"usage"`
|
} `json:"usage"`
|
||||||
}
|
}
|
||||||
if json.Unmarshal([]byte(data), &msgDelta) == nil && msgDelta.Type == "message_delta" {
|
if json.Unmarshal([]byte(data), &msgDelta) == nil && msgDelta.Type == "message_delta" {
|
||||||
// output_tokens 总是从 message_delta 获取
|
// message_delta 是推理结束后的最终统计,应完全覆盖 message_start 的数据
|
||||||
|
// 这对于 Claude API 和 GLM 等兼容 API 都是正确的行为
|
||||||
|
usage.InputTokens = msgDelta.Usage.InputTokens
|
||||||
usage.OutputTokens = msgDelta.Usage.OutputTokens
|
usage.OutputTokens = msgDelta.Usage.OutputTokens
|
||||||
|
usage.CacheCreationInputTokens = msgDelta.Usage.CacheCreationInputTokens
|
||||||
// 如果 message_start 中没有值,则从 message_delta 获取(兼容GLM等API)
|
usage.CacheReadInputTokens = msgDelta.Usage.CacheReadInputTokens
|
||||||
if usage.InputTokens == 0 {
|
|
||||||
usage.InputTokens = msgDelta.Usage.InputTokens
|
|
||||||
}
|
|
||||||
if usage.CacheCreationInputTokens == 0 {
|
|
||||||
usage.CacheCreationInputTokens = msgDelta.Usage.CacheCreationInputTokens
|
|
||||||
}
|
|
||||||
if usage.CacheReadInputTokens == 0 {
|
|
||||||
usage.CacheReadInputTokens = msgDelta.Usage.CacheReadInputTokens
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -931,6 +931,13 @@ func (s *GeminiMessagesCompatService) Forward(ctx context.Context, c *gin.Contex
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 图片生成计费
|
||||||
|
imageCount := 0
|
||||||
|
imageSize := s.extractImageSize(body)
|
||||||
|
if isImageGenerationModel(originalModel) {
|
||||||
|
imageCount = 1
|
||||||
|
}
|
||||||
|
|
||||||
return &ForwardResult{
|
return &ForwardResult{
|
||||||
RequestID: requestID,
|
RequestID: requestID,
|
||||||
Usage: *usage,
|
Usage: *usage,
|
||||||
@@ -938,6 +945,8 @@ func (s *GeminiMessagesCompatService) Forward(ctx context.Context, c *gin.Contex
|
|||||||
Stream: req.Stream,
|
Stream: req.Stream,
|
||||||
Duration: time.Since(startTime),
|
Duration: time.Since(startTime),
|
||||||
FirstTokenMs: firstTokenMs,
|
FirstTokenMs: firstTokenMs,
|
||||||
|
ImageCount: imageCount,
|
||||||
|
ImageSize: imageSize,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1371,6 +1380,13 @@ func (s *GeminiMessagesCompatService) ForwardNative(ctx context.Context, c *gin.
|
|||||||
usage = &ClaudeUsage{}
|
usage = &ClaudeUsage{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 图片生成计费
|
||||||
|
imageCount := 0
|
||||||
|
imageSize := s.extractImageSize(body)
|
||||||
|
if isImageGenerationModel(originalModel) {
|
||||||
|
imageCount = 1
|
||||||
|
}
|
||||||
|
|
||||||
return &ForwardResult{
|
return &ForwardResult{
|
||||||
RequestID: requestID,
|
RequestID: requestID,
|
||||||
Usage: *usage,
|
Usage: *usage,
|
||||||
@@ -1378,6 +1394,8 @@ func (s *GeminiMessagesCompatService) ForwardNative(ctx context.Context, c *gin.
|
|||||||
Stream: stream,
|
Stream: stream,
|
||||||
Duration: time.Since(startTime),
|
Duration: time.Since(startTime),
|
||||||
FirstTokenMs: firstTokenMs,
|
FirstTokenMs: firstTokenMs,
|
||||||
|
ImageCount: imageCount,
|
||||||
|
ImageSize: imageSize,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3031,3 +3049,26 @@ func convertClaudeGenerationConfig(req map[string]any) map[string]any {
|
|||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// extractImageSize 从 Gemini 请求中提取 image_size 参数
|
||||||
|
func (s *GeminiMessagesCompatService) extractImageSize(body []byte) string {
|
||||||
|
var req struct {
|
||||||
|
GenerationConfig *struct {
|
||||||
|
ImageConfig *struct {
|
||||||
|
ImageSize string `json:"imageSize"`
|
||||||
|
} `json:"imageConfig"`
|
||||||
|
} `json:"generationConfig"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(body, &req); err != nil {
|
||||||
|
return "2K"
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.GenerationConfig != nil && req.GenerationConfig.ImageConfig != nil {
|
||||||
|
size := strings.ToUpper(strings.TrimSpace(req.GenerationConfig.ImageConfig.ImageSize))
|
||||||
|
if size == "1K" || size == "2K" || size == "4K" {
|
||||||
|
return size
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "2K"
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,9 +2,10 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors"
|
||||||
"github.com/Wei-Shaw/sub2api/internal/pkg/openai"
|
"github.com/Wei-Shaw/sub2api/internal/pkg/openai"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,12 +36,12 @@ func (s *OpenAIOAuthService) GenerateAuthURL(ctx context.Context, proxyID *int64
|
|||||||
// Generate PKCE values
|
// Generate PKCE values
|
||||||
state, err := openai.GenerateState()
|
state, err := openai.GenerateState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to generate state: %w", err)
|
return nil, infraerrors.Newf(http.StatusInternalServerError, "OPENAI_OAUTH_STATE_FAILED", "failed to generate state: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
codeVerifier, err := openai.GenerateCodeVerifier()
|
codeVerifier, err := openai.GenerateCodeVerifier()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to generate code verifier: %w", err)
|
return nil, infraerrors.Newf(http.StatusInternalServerError, "OPENAI_OAUTH_VERIFIER_FAILED", "failed to generate code verifier: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
codeChallenge := openai.GenerateCodeChallenge(codeVerifier)
|
codeChallenge := openai.GenerateCodeChallenge(codeVerifier)
|
||||||
@@ -48,14 +49,17 @@ func (s *OpenAIOAuthService) GenerateAuthURL(ctx context.Context, proxyID *int64
|
|||||||
// Generate session ID
|
// Generate session ID
|
||||||
sessionID, err := openai.GenerateSessionID()
|
sessionID, err := openai.GenerateSessionID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to generate session ID: %w", err)
|
return nil, infraerrors.Newf(http.StatusInternalServerError, "OPENAI_OAUTH_SESSION_FAILED", "failed to generate session ID: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get proxy URL if specified
|
// Get proxy URL if specified
|
||||||
var proxyURL string
|
var proxyURL string
|
||||||
if proxyID != nil {
|
if proxyID != nil {
|
||||||
proxy, err := s.proxyRepo.GetByID(ctx, *proxyID)
|
proxy, err := s.proxyRepo.GetByID(ctx, *proxyID)
|
||||||
if err == nil && proxy != nil {
|
if err != nil {
|
||||||
|
return nil, infraerrors.Newf(http.StatusBadRequest, "OPENAI_OAUTH_PROXY_NOT_FOUND", "proxy not found: %v", err)
|
||||||
|
}
|
||||||
|
if proxy != nil {
|
||||||
proxyURL = proxy.URL()
|
proxyURL = proxy.URL()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,14 +114,17 @@ func (s *OpenAIOAuthService) ExchangeCode(ctx context.Context, input *OpenAIExch
|
|||||||
// Get session
|
// Get session
|
||||||
session, ok := s.sessionStore.Get(input.SessionID)
|
session, ok := s.sessionStore.Get(input.SessionID)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("session not found or expired")
|
return nil, infraerrors.New(http.StatusBadRequest, "OPENAI_OAUTH_SESSION_NOT_FOUND", "session not found or expired")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get proxy URL
|
// Get proxy URL: prefer input.ProxyID, fallback to session.ProxyURL
|
||||||
proxyURL := session.ProxyURL
|
proxyURL := session.ProxyURL
|
||||||
if input.ProxyID != nil {
|
if input.ProxyID != nil {
|
||||||
proxy, err := s.proxyRepo.GetByID(ctx, *input.ProxyID)
|
proxy, err := s.proxyRepo.GetByID(ctx, *input.ProxyID)
|
||||||
if err == nil && proxy != nil {
|
if err != nil {
|
||||||
|
return nil, infraerrors.Newf(http.StatusBadRequest, "OPENAI_OAUTH_PROXY_NOT_FOUND", "proxy not found: %v", err)
|
||||||
|
}
|
||||||
|
if proxy != nil {
|
||||||
proxyURL = proxy.URL()
|
proxyURL = proxy.URL()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -131,7 +138,7 @@ func (s *OpenAIOAuthService) ExchangeCode(ctx context.Context, input *OpenAIExch
|
|||||||
// Exchange code for token
|
// Exchange code for token
|
||||||
tokenResp, err := s.oauthClient.ExchangeCode(ctx, input.Code, session.CodeVerifier, redirectURI, proxyURL)
|
tokenResp, err := s.oauthClient.ExchangeCode(ctx, input.Code, session.CodeVerifier, redirectURI, proxyURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to exchange code: %w", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse ID token to get user info
|
// Parse ID token to get user info
|
||||||
@@ -201,12 +208,12 @@ func (s *OpenAIOAuthService) RefreshToken(ctx context.Context, refreshToken stri
|
|||||||
// RefreshAccountToken refreshes token for an OpenAI account
|
// RefreshAccountToken refreshes token for an OpenAI account
|
||||||
func (s *OpenAIOAuthService) RefreshAccountToken(ctx context.Context, account *Account) (*OpenAITokenInfo, error) {
|
func (s *OpenAIOAuthService) RefreshAccountToken(ctx context.Context, account *Account) (*OpenAITokenInfo, error) {
|
||||||
if !account.IsOpenAI() {
|
if !account.IsOpenAI() {
|
||||||
return nil, fmt.Errorf("account is not an OpenAI account")
|
return nil, infraerrors.New(http.StatusBadRequest, "OPENAI_OAUTH_INVALID_ACCOUNT", "account is not an OpenAI account")
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshToken := account.GetOpenAIRefreshToken()
|
refreshToken := account.GetOpenAIRefreshToken()
|
||||||
if refreshToken == "" {
|
if refreshToken == "" {
|
||||||
return nil, fmt.Errorf("no refresh token available")
|
return nil, infraerrors.New(http.StatusBadRequest, "OPENAI_OAUTH_NO_REFRESH_TOKEN", "no refresh token available")
|
||||||
}
|
}
|
||||||
|
|
||||||
var proxyURL string
|
var proxyURL string
|
||||||
|
|||||||
@@ -343,7 +343,7 @@ func (s *RateLimitService) handleCustomErrorCode(ctx context.Context, account *A
|
|||||||
// handle429 处理429限流错误
|
// handle429 处理429限流错误
|
||||||
// 解析响应头获取重置时间,标记账号为限流状态
|
// 解析响应头获取重置时间,标记账号为限流状态
|
||||||
func (s *RateLimitService) handle429(ctx context.Context, account *Account, headers http.Header, responseBody []byte) {
|
func (s *RateLimitService) handle429(ctx context.Context, account *Account, headers http.Header, responseBody []byte) {
|
||||||
// OpenAI 平台:解析 x-codex-* 响应头
|
// 1. OpenAI 平台:优先尝试解析 x-codex-* 响应头(用于 rate_limit_exceeded)
|
||||||
if account.Platform == PlatformOpenAI {
|
if account.Platform == PlatformOpenAI {
|
||||||
if resetAt := s.calculateOpenAI429ResetTime(headers); resetAt != nil {
|
if resetAt := s.calculateOpenAI429ResetTime(headers); resetAt != nil {
|
||||||
if err := s.accountRepo.SetRateLimited(ctx, account.ID, *resetAt); err != nil {
|
if err := s.accountRepo.SetRateLimited(ctx, account.ID, *resetAt); err != nil {
|
||||||
@@ -353,12 +353,38 @@ func (s *RateLimitService) handle429(ctx context.Context, account *Account, head
|
|||||||
slog.Info("openai_account_rate_limited", "account_id", account.ID, "reset_at", *resetAt)
|
slog.Info("openai_account_rate_limited", "account_id", account.ID, "reset_at", *resetAt)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 如果解析失败,继续使用默认逻辑
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析重置时间戳
|
// 2. 尝试从响应头解析重置时间(Anthropic)
|
||||||
resetTimestamp := headers.Get("anthropic-ratelimit-unified-reset")
|
resetTimestamp := headers.Get("anthropic-ratelimit-unified-reset")
|
||||||
|
|
||||||
|
// 3. 如果响应头没有,尝试从响应体解析(OpenAI usage_limit_reached, Gemini)
|
||||||
if resetTimestamp == "" {
|
if resetTimestamp == "" {
|
||||||
|
switch account.Platform {
|
||||||
|
case PlatformOpenAI:
|
||||||
|
// 尝试解析 OpenAI 的 usage_limit_reached 错误
|
||||||
|
if resetAt := parseOpenAIRateLimitResetTime(responseBody); resetAt != nil {
|
||||||
|
resetTime := time.Unix(*resetAt, 0)
|
||||||
|
if err := s.accountRepo.SetRateLimited(ctx, account.ID, resetTime); err != nil {
|
||||||
|
slog.Warn("rate_limit_set_failed", "account_id", account.ID, "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
slog.Info("account_rate_limited", "account_id", account.ID, "platform", account.Platform, "reset_at", resetTime, "reset_in", time.Until(resetTime).Truncate(time.Second))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case PlatformGemini, PlatformAntigravity:
|
||||||
|
// 尝试解析 Gemini 格式(用于其他平台)
|
||||||
|
if resetAt := ParseGeminiRateLimitResetTime(responseBody); resetAt != nil {
|
||||||
|
resetTime := time.Unix(*resetAt, 0)
|
||||||
|
if err := s.accountRepo.SetRateLimited(ctx, account.ID, resetTime); err != nil {
|
||||||
|
slog.Warn("rate_limit_set_failed", "account_id", account.ID, "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
slog.Info("account_rate_limited", "account_id", account.ID, "platform", account.Platform, "reset_at", resetTime, "reset_in", time.Until(resetTime).Truncate(time.Second))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 没有重置时间,使用默认5分钟
|
// 没有重置时间,使用默认5分钟
|
||||||
resetAt := time.Now().Add(5 * time.Minute)
|
resetAt := time.Now().Add(5 * time.Minute)
|
||||||
if s.shouldScopeClaudeSonnetRateLimit(account, responseBody) {
|
if s.shouldScopeClaudeSonnetRateLimit(account, responseBody) {
|
||||||
@@ -369,6 +395,7 @@ func (s *RateLimitService) handle429(ctx context.Context, account *Account, head
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
slog.Warn("rate_limit_no_reset_time", "account_id", account.ID, "platform", account.Platform, "using_default", "5m")
|
||||||
if err := s.accountRepo.SetRateLimited(ctx, account.ID, resetAt); err != nil {
|
if err := s.accountRepo.SetRateLimited(ctx, account.ID, resetAt); err != nil {
|
||||||
slog.Warn("rate_limit_set_failed", "account_id", account.ID, "error", err)
|
slog.Warn("rate_limit_set_failed", "account_id", account.ID, "error", err)
|
||||||
}
|
}
|
||||||
@@ -480,6 +507,60 @@ func (s *RateLimitService) calculateOpenAI429ResetTime(headers http.Header) *tim
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseOpenAIRateLimitResetTime 解析 OpenAI 格式的 429 响应,返回重置时间的 Unix 时间戳
|
||||||
|
// OpenAI 的 usage_limit_reached 错误格式:
|
||||||
|
//
|
||||||
|
// {
|
||||||
|
// "error": {
|
||||||
|
// "message": "The usage limit has been reached",
|
||||||
|
// "type": "usage_limit_reached",
|
||||||
|
// "resets_at": 1769404154,
|
||||||
|
// "resets_in_seconds": 133107
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
func parseOpenAIRateLimitResetTime(body []byte) *int64 {
|
||||||
|
var parsed map[string]any
|
||||||
|
if err := json.Unmarshal(body, &parsed); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
errObj, ok := parsed["error"].(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否为 usage_limit_reached 或 rate_limit_exceeded 类型
|
||||||
|
errType, _ := errObj["type"].(string)
|
||||||
|
if errType != "usage_limit_reached" && errType != "rate_limit_exceeded" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 优先使用 resets_at(Unix 时间戳)
|
||||||
|
if resetsAt, ok := errObj["resets_at"].(float64); ok {
|
||||||
|
ts := int64(resetsAt)
|
||||||
|
return &ts
|
||||||
|
}
|
||||||
|
if resetsAt, ok := errObj["resets_at"].(string); ok {
|
||||||
|
if ts, err := strconv.ParseInt(resetsAt, 10, 64); err == nil {
|
||||||
|
return &ts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有 resets_at,尝试使用 resets_in_seconds
|
||||||
|
if resetsInSeconds, ok := errObj["resets_in_seconds"].(float64); ok {
|
||||||
|
ts := time.Now().Unix() + int64(resetsInSeconds)
|
||||||
|
return &ts
|
||||||
|
}
|
||||||
|
if resetsInSeconds, ok := errObj["resets_in_seconds"].(string); ok {
|
||||||
|
if sec, err := strconv.ParseInt(resetsInSeconds, 10, 64); err == nil {
|
||||||
|
ts := time.Now().Unix() + sec
|
||||||
|
return &ts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// handle529 处理529过载错误
|
// handle529 处理529过载错误
|
||||||
// 根据配置设置过载冷却时间
|
// 根据配置设置过载冷却时间
|
||||||
func (s *RateLimitService) handle529(ctx context.Context, account *Account) {
|
func (s *RateLimitService) handle529(ctx context.Context, account *Account) {
|
||||||
|
|||||||
Reference in New Issue
Block a user