mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-07 00:40:22 +08:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa4b102108 | ||
|
|
56da498b7e | ||
|
|
1bba1a62b1 | ||
|
|
4a84ca9a02 | ||
|
|
a70d37a676 | ||
|
|
6892e84ad2 | ||
|
|
73f455745c | ||
|
|
7d66f7ff0d | ||
|
|
470b37be7e | ||
|
|
51572b5da0 | ||
|
|
91ca28b7e3 | ||
|
|
04cedce9a1 | ||
|
|
5e0d789440 |
@@ -1 +1 @@
|
|||||||
0.1.74.7
|
0.1.76
|
||||||
@@ -424,10 +424,17 @@ type TestAccountRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SyncFromCRSRequest struct {
|
type SyncFromCRSRequest struct {
|
||||||
BaseURL string `json:"base_url" binding:"required"`
|
BaseURL string `json:"base_url" binding:"required"`
|
||||||
Username string `json:"username" binding:"required"`
|
Username string `json:"username" binding:"required"`
|
||||||
Password string `json:"password" binding:"required"`
|
Password string `json:"password" binding:"required"`
|
||||||
SyncProxies *bool `json:"sync_proxies"`
|
SyncProxies *bool `json:"sync_proxies"`
|
||||||
|
SelectedAccountIDs []string `json:"selected_account_ids"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PreviewFromCRSRequest struct {
|
||||||
|
BaseURL string `json:"base_url" binding:"required"`
|
||||||
|
Username string `json:"username" binding:"required"`
|
||||||
|
Password string `json:"password" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test handles testing account connectivity with SSE streaming
|
// Test handles testing account connectivity with SSE streaming
|
||||||
@@ -466,10 +473,11 @@ func (h *AccountHandler) SyncFromCRS(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result, err := h.crsSyncService.SyncFromCRS(c.Request.Context(), service.SyncFromCRSInput{
|
result, err := h.crsSyncService.SyncFromCRS(c.Request.Context(), service.SyncFromCRSInput{
|
||||||
BaseURL: req.BaseURL,
|
BaseURL: req.BaseURL,
|
||||||
Username: req.Username,
|
Username: req.Username,
|
||||||
Password: req.Password,
|
Password: req.Password,
|
||||||
SyncProxies: syncProxies,
|
SyncProxies: syncProxies,
|
||||||
|
SelectedAccountIDs: req.SelectedAccountIDs,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Provide detailed error message for CRS sync failures
|
// Provide detailed error message for CRS sync failures
|
||||||
@@ -480,6 +488,28 @@ func (h *AccountHandler) SyncFromCRS(c *gin.Context) {
|
|||||||
response.Success(c, result)
|
response.Success(c, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PreviewFromCRS handles previewing accounts from CRS before sync
|
||||||
|
// POST /api/v1/admin/accounts/sync/crs/preview
|
||||||
|
func (h *AccountHandler) PreviewFromCRS(c *gin.Context) {
|
||||||
|
var req PreviewFromCRSRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
response.BadRequest(c, "Invalid request: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := h.crsSyncService.PreviewFromCRS(c.Request.Context(), service.SyncFromCRSInput{
|
||||||
|
BaseURL: req.BaseURL,
|
||||||
|
Username: req.Username,
|
||||||
|
Password: req.Password,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
response.InternalError(c, "CRS preview failed: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response.Success(c, result)
|
||||||
|
}
|
||||||
|
|
||||||
// Refresh handles refreshing account credentials
|
// Refresh handles refreshing account credentials
|
||||||
// POST /api/v1/admin/accounts/:id/refresh
|
// POST /api/v1/admin/accounts/:id/refresh
|
||||||
func (h *AccountHandler) Refresh(c *gin.Context) {
|
func (h *AccountHandler) Refresh(c *gin.Context) {
|
||||||
|
|||||||
@@ -282,6 +282,34 @@ func (r *accountRepository) GetByCRSAccountID(ctx context.Context, crsAccountID
|
|||||||
return &accounts[0], nil
|
return &accounts[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *accountRepository) ListCRSAccountIDs(ctx context.Context) (map[string]int64, error) {
|
||||||
|
rows, err := r.sql.QueryContext(ctx, `
|
||||||
|
SELECT id, extra->>'crs_account_id'
|
||||||
|
FROM accounts
|
||||||
|
WHERE deleted_at IS NULL
|
||||||
|
AND extra->>'crs_account_id' IS NOT NULL
|
||||||
|
AND extra->>'crs_account_id' != ''
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer func() { _ = rows.Close() }()
|
||||||
|
|
||||||
|
result := make(map[string]int64)
|
||||||
|
for rows.Next() {
|
||||||
|
var id int64
|
||||||
|
var crsID string
|
||||||
|
if err := rows.Scan(&id, &crsID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result[crsID] = id
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *accountRepository) Update(ctx context.Context, account *service.Account) error {
|
func (r *accountRepository) Update(ctx context.Context, account *service.Account) error {
|
||||||
if account == nil {
|
if account == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -1049,6 +1049,10 @@ func (s *stubAccountRepo) BulkUpdate(ctx context.Context, ids []int64, updates s
|
|||||||
return int64(len(ids)), nil
|
return int64(len(ids)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *stubAccountRepo) ListCRSAccountIDs(ctx context.Context) (map[string]int64, error) {
|
||||||
|
return nil, errors.New("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
type stubProxyRepo struct{}
|
type stubProxyRepo struct{}
|
||||||
|
|
||||||
func (stubProxyRepo) Create(ctx context.Context, proxy *service.Proxy) error {
|
func (stubProxyRepo) Create(ctx context.Context, proxy *service.Proxy) error {
|
||||||
|
|||||||
@@ -209,6 +209,7 @@ func registerAccountRoutes(admin *gin.RouterGroup, h *handler.Handlers) {
|
|||||||
accounts.GET("/:id", h.Admin.Account.GetByID)
|
accounts.GET("/:id", h.Admin.Account.GetByID)
|
||||||
accounts.POST("", h.Admin.Account.Create)
|
accounts.POST("", h.Admin.Account.Create)
|
||||||
accounts.POST("/sync/crs", h.Admin.Account.SyncFromCRS)
|
accounts.POST("/sync/crs", h.Admin.Account.SyncFromCRS)
|
||||||
|
accounts.POST("/sync/crs/preview", h.Admin.Account.PreviewFromCRS)
|
||||||
accounts.PUT("/:id", h.Admin.Account.Update)
|
accounts.PUT("/:id", h.Admin.Account.Update)
|
||||||
accounts.DELETE("/:id", h.Admin.Account.Delete)
|
accounts.DELETE("/:id", h.Admin.Account.Delete)
|
||||||
accounts.POST("/:id/test", h.Admin.Account.Test)
|
accounts.POST("/:id/test", h.Admin.Account.Test)
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ type AccountRepository interface {
|
|||||||
// GetByCRSAccountID finds an account previously synced from CRS.
|
// GetByCRSAccountID finds an account previously synced from CRS.
|
||||||
// Returns (nil, nil) if not found.
|
// Returns (nil, nil) if not found.
|
||||||
GetByCRSAccountID(ctx context.Context, crsAccountID string) (*Account, error)
|
GetByCRSAccountID(ctx context.Context, crsAccountID string) (*Account, error)
|
||||||
|
// ListCRSAccountIDs returns a map of crs_account_id -> local account ID
|
||||||
|
// for all accounts that have been synced from CRS.
|
||||||
|
ListCRSAccountIDs(ctx context.Context) (map[string]int64, error)
|
||||||
Update(ctx context.Context, account *Account) error
|
Update(ctx context.Context, account *Account) error
|
||||||
Delete(ctx context.Context, id int64) error
|
Delete(ctx context.Context, id int64) error
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,10 @@ func (s *accountRepoStub) GetByCRSAccountID(ctx context.Context, crsAccountID st
|
|||||||
panic("unexpected GetByCRSAccountID call")
|
panic("unexpected GetByCRSAccountID call")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *accountRepoStub) ListCRSAccountIDs(ctx context.Context) (map[string]int64, error) {
|
||||||
|
panic("unexpected ListCRSAccountIDs call")
|
||||||
|
}
|
||||||
|
|
||||||
func (s *accountRepoStub) Update(ctx context.Context, account *Account) error {
|
func (s *accountRepoStub) Update(ctx context.Context, account *Account) error {
|
||||||
panic("unexpected Update call")
|
panic("unexpected Update call")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -371,12 +371,12 @@ urlFallbackLoop:
|
|||||||
_ = resp.Body.Close()
|
_ = resp.Body.Close()
|
||||||
|
|
||||||
// ★ 统一入口:自定义错误码 + 临时不可调度
|
// ★ 统一入口:自定义错误码 + 临时不可调度
|
||||||
if handled, policyErr := s.applyErrorPolicy(p, resp.StatusCode, resp.Header, respBody); handled {
|
if handled, outStatus, policyErr := s.applyErrorPolicy(p, resp.StatusCode, resp.Header, respBody); handled {
|
||||||
if policyErr != nil {
|
if policyErr != nil {
|
||||||
return nil, policyErr
|
return nil, policyErr
|
||||||
}
|
}
|
||||||
resp = &http.Response{
|
resp = &http.Response{
|
||||||
StatusCode: resp.StatusCode,
|
StatusCode: outStatus,
|
||||||
Header: resp.Header.Clone(),
|
Header: resp.Header.Clone(),
|
||||||
Body: io.NopCloser(bytes.NewReader(respBody)),
|
Body: io.NopCloser(bytes.NewReader(respBody)),
|
||||||
}
|
}
|
||||||
@@ -610,21 +610,22 @@ func (s *AntigravityGatewayService) checkErrorPolicy(ctx context.Context, accoun
|
|||||||
return s.rateLimitService.CheckErrorPolicy(ctx, account, statusCode, body)
|
return s.rateLimitService.CheckErrorPolicy(ctx, account, statusCode, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// applyErrorPolicy 应用错误策略结果,返回是否应终止当前循环
|
// applyErrorPolicy 应用错误策略结果,返回是否应终止当前循环及应返回的状态码。
|
||||||
func (s *AntigravityGatewayService) applyErrorPolicy(p antigravityRetryLoopParams, statusCode int, headers http.Header, respBody []byte) (handled bool, retErr error) {
|
// ErrorPolicySkipped 时 outStatus 为 500(前端约定:未命中的错误返回 500)。
|
||||||
|
func (s *AntigravityGatewayService) applyErrorPolicy(p antigravityRetryLoopParams, statusCode int, headers http.Header, respBody []byte) (handled bool, outStatus int, retErr error) {
|
||||||
switch s.checkErrorPolicy(p.ctx, p.account, statusCode, respBody) {
|
switch s.checkErrorPolicy(p.ctx, p.account, statusCode, respBody) {
|
||||||
case ErrorPolicySkipped:
|
case ErrorPolicySkipped:
|
||||||
return true, nil
|
return true, http.StatusInternalServerError, nil
|
||||||
case ErrorPolicyMatched:
|
case ErrorPolicyMatched:
|
||||||
_ = p.handleError(p.ctx, p.prefix, p.account, statusCode, headers, respBody,
|
_ = p.handleError(p.ctx, p.prefix, p.account, statusCode, headers, respBody,
|
||||||
p.requestedModel, p.groupID, p.sessionHash, p.isStickySession)
|
p.requestedModel, p.groupID, p.sessionHash, p.isStickySession)
|
||||||
return true, nil
|
return true, statusCode, nil
|
||||||
case ErrorPolicyTempUnscheduled:
|
case ErrorPolicyTempUnscheduled:
|
||||||
slog.Info("temp_unschedulable_matched",
|
slog.Info("temp_unschedulable_matched",
|
||||||
"prefix", p.prefix, "status_code", statusCode, "account_id", p.account.ID)
|
"prefix", p.prefix, "status_code", statusCode, "account_id", p.account.ID)
|
||||||
return true, &AntigravityAccountSwitchError{OriginalAccountID: p.account.ID, IsStickySession: p.isStickySession}
|
return true, statusCode, &AntigravityAccountSwitchError{OriginalAccountID: p.account.ID, IsStickySession: p.isStickySession}
|
||||||
}
|
}
|
||||||
return false, nil
|
return false, statusCode, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// mapAntigravityModel 获取映射后的模型名
|
// mapAntigravityModel 获取映射后的模型名
|
||||||
@@ -2242,6 +2243,10 @@ func (s *AntigravityGatewayService) handleUpstreamError(
|
|||||||
requestedModel string,
|
requestedModel string,
|
||||||
groupID int64, sessionHash string, isStickySession bool,
|
groupID int64, sessionHash string, isStickySession bool,
|
||||||
) *handleModelRateLimitResult {
|
) *handleModelRateLimitResult {
|
||||||
|
// 遵守自定义错误码策略:未命中则跳过所有限流处理
|
||||||
|
if !account.ShouldHandleErrorCode(statusCode) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
// 模型级限流处理(优先)
|
// 模型级限流处理(优先)
|
||||||
result := s.handleModelRateLimit(&handleModelRateLimitParams{
|
result := s.handleModelRateLimit(&handleModelRateLimitParams{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
|
|||||||
112
backend/internal/service/crs_sync_helpers_test.go
Normal file
112
backend/internal/service/crs_sync_helpers_test.go
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBuildSelectedSet(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
ids []string
|
||||||
|
wantNil bool
|
||||||
|
wantSize int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "nil input returns nil (backward compatible: create all)",
|
||||||
|
ids: nil,
|
||||||
|
wantNil: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty slice returns empty map (create none)",
|
||||||
|
ids: []string{},
|
||||||
|
wantNil: false,
|
||||||
|
wantSize: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "single ID",
|
||||||
|
ids: []string{"abc-123"},
|
||||||
|
wantNil: false,
|
||||||
|
wantSize: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiple IDs",
|
||||||
|
ids: []string{"a", "b", "c"},
|
||||||
|
wantNil: false,
|
||||||
|
wantSize: 3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "duplicate IDs are deduplicated",
|
||||||
|
ids: []string{"a", "a", "b"},
|
||||||
|
wantNil: false,
|
||||||
|
wantSize: 2,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := buildSelectedSet(tt.ids)
|
||||||
|
if tt.wantNil {
|
||||||
|
if got != nil {
|
||||||
|
t.Errorf("buildSelectedSet(%v) = %v, want nil", tt.ids, got)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if got == nil {
|
||||||
|
t.Fatalf("buildSelectedSet(%v) = nil, want non-nil map", tt.ids)
|
||||||
|
}
|
||||||
|
if len(got) != tt.wantSize {
|
||||||
|
t.Errorf("buildSelectedSet(%v) has %d entries, want %d", tt.ids, len(got), tt.wantSize)
|
||||||
|
}
|
||||||
|
// Verify all unique IDs are present
|
||||||
|
for _, id := range tt.ids {
|
||||||
|
if _, ok := got[id]; !ok {
|
||||||
|
t.Errorf("buildSelectedSet(%v) missing key %q", tt.ids, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShouldCreateAccount(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
crsID string
|
||||||
|
selectedSet map[string]struct{}
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "nil set allows all (backward compatible)",
|
||||||
|
crsID: "any-id",
|
||||||
|
selectedSet: nil,
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty set blocks all",
|
||||||
|
crsID: "any-id",
|
||||||
|
selectedSet: map[string]struct{}{},
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ID in set is allowed",
|
||||||
|
crsID: "abc-123",
|
||||||
|
selectedSet: map[string]struct{}{"abc-123": {}, "def-456": {}},
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ID not in set is blocked",
|
||||||
|
crsID: "xyz-789",
|
||||||
|
selectedSet: map[string]struct{}{"abc-123": {}, "def-456": {}},
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := shouldCreateAccount(tt.crsID, tt.selectedSet)
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("shouldCreateAccount(%q, %v) = %v, want %v",
|
||||||
|
tt.crsID, tt.selectedSet, got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,10 +45,11 @@ func NewCRSSyncService(
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SyncFromCRSInput struct {
|
type SyncFromCRSInput struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
SyncProxies bool
|
SyncProxies bool
|
||||||
|
SelectedAccountIDs []string // if non-empty, only create new accounts with these CRS IDs
|
||||||
}
|
}
|
||||||
|
|
||||||
type SyncFromCRSItemResult struct {
|
type SyncFromCRSItemResult struct {
|
||||||
@@ -190,25 +191,27 @@ type crsGeminiAPIKeyAccount struct {
|
|||||||
Extra map[string]any `json:"extra"`
|
Extra map[string]any `json:"extra"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CRSSyncService) SyncFromCRS(ctx context.Context, input SyncFromCRSInput) (*SyncFromCRSResult, error) {
|
// fetchCRSExport validates the connection parameters, authenticates with CRS,
|
||||||
|
// and returns the exported accounts. Shared by SyncFromCRS and PreviewFromCRS.
|
||||||
|
func (s *CRSSyncService) fetchCRSExport(ctx context.Context, baseURL, username, password string) (*crsExportResponse, error) {
|
||||||
if s.cfg == nil {
|
if s.cfg == nil {
|
||||||
return nil, errors.New("config is not available")
|
return nil, errors.New("config is not available")
|
||||||
}
|
}
|
||||||
baseURL := strings.TrimSpace(input.BaseURL)
|
normalizedURL := strings.TrimSpace(baseURL)
|
||||||
if s.cfg.Security.URLAllowlist.Enabled {
|
if s.cfg.Security.URLAllowlist.Enabled {
|
||||||
normalized, err := normalizeBaseURL(baseURL, s.cfg.Security.URLAllowlist.CRSHosts, s.cfg.Security.URLAllowlist.AllowPrivateHosts)
|
normalized, err := normalizeBaseURL(normalizedURL, s.cfg.Security.URLAllowlist.CRSHosts, s.cfg.Security.URLAllowlist.AllowPrivateHosts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
baseURL = normalized
|
normalizedURL = normalized
|
||||||
} else {
|
} else {
|
||||||
normalized, err := urlvalidator.ValidateURLFormat(baseURL, s.cfg.Security.URLAllowlist.AllowInsecureHTTP)
|
normalized, err := urlvalidator.ValidateURLFormat(normalizedURL, s.cfg.Security.URLAllowlist.AllowInsecureHTTP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("invalid base_url: %w", err)
|
return nil, fmt.Errorf("invalid base_url: %w", err)
|
||||||
}
|
}
|
||||||
baseURL = normalized
|
normalizedURL = normalized
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(input.Username) == "" || strings.TrimSpace(input.Password) == "" {
|
if strings.TrimSpace(username) == "" || strings.TrimSpace(password) == "" {
|
||||||
return nil, errors.New("username and password are required")
|
return nil, errors.New("username and password are required")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,12 +224,16 @@ func (s *CRSSyncService) SyncFromCRS(ctx context.Context, input SyncFromCRSInput
|
|||||||
client = &http.Client{Timeout: 20 * time.Second}
|
client = &http.Client{Timeout: 20 * time.Second}
|
||||||
}
|
}
|
||||||
|
|
||||||
adminToken, err := crsLogin(ctx, client, baseURL, input.Username, input.Password)
|
adminToken, err := crsLogin(ctx, client, normalizedURL, username, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
exported, err := crsExportAccounts(ctx, client, baseURL, adminToken)
|
return crsExportAccounts(ctx, client, normalizedURL, adminToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *CRSSyncService) SyncFromCRS(ctx context.Context, input SyncFromCRSInput) (*SyncFromCRSResult, error) {
|
||||||
|
exported, err := s.fetchCRSExport(ctx, input.BaseURL, input.Username, input.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -241,6 +248,8 @@ func (s *CRSSyncService) SyncFromCRS(ctx context.Context, input SyncFromCRSInput
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
selectedSet := buildSelectedSet(input.SelectedAccountIDs)
|
||||||
|
|
||||||
var proxies []Proxy
|
var proxies []Proxy
|
||||||
if input.SyncProxies {
|
if input.SyncProxies {
|
||||||
proxies, _ = s.proxyRepo.ListActive(ctx)
|
proxies, _ = s.proxyRepo.ListActive(ctx)
|
||||||
@@ -329,6 +338,13 @@ func (s *CRSSyncService) SyncFromCRS(ctx context.Context, input SyncFromCRSInput
|
|||||||
}
|
}
|
||||||
|
|
||||||
if existing == nil {
|
if existing == nil {
|
||||||
|
if !shouldCreateAccount(src.ID, selectedSet) {
|
||||||
|
item.Action = "skipped"
|
||||||
|
item.Error = "not selected"
|
||||||
|
result.Skipped++
|
||||||
|
result.Items = append(result.Items, item)
|
||||||
|
continue
|
||||||
|
}
|
||||||
account := &Account{
|
account := &Account{
|
||||||
Name: defaultName(src.Name, src.ID),
|
Name: defaultName(src.Name, src.ID),
|
||||||
Platform: PlatformAnthropic,
|
Platform: PlatformAnthropic,
|
||||||
@@ -446,6 +462,13 @@ func (s *CRSSyncService) SyncFromCRS(ctx context.Context, input SyncFromCRSInput
|
|||||||
}
|
}
|
||||||
|
|
||||||
if existing == nil {
|
if existing == nil {
|
||||||
|
if !shouldCreateAccount(src.ID, selectedSet) {
|
||||||
|
item.Action = "skipped"
|
||||||
|
item.Error = "not selected"
|
||||||
|
result.Skipped++
|
||||||
|
result.Items = append(result.Items, item)
|
||||||
|
continue
|
||||||
|
}
|
||||||
account := &Account{
|
account := &Account{
|
||||||
Name: defaultName(src.Name, src.ID),
|
Name: defaultName(src.Name, src.ID),
|
||||||
Platform: PlatformAnthropic,
|
Platform: PlatformAnthropic,
|
||||||
@@ -569,6 +592,13 @@ func (s *CRSSyncService) SyncFromCRS(ctx context.Context, input SyncFromCRSInput
|
|||||||
}
|
}
|
||||||
|
|
||||||
if existing == nil {
|
if existing == nil {
|
||||||
|
if !shouldCreateAccount(src.ID, selectedSet) {
|
||||||
|
item.Action = "skipped"
|
||||||
|
item.Error = "not selected"
|
||||||
|
result.Skipped++
|
||||||
|
result.Items = append(result.Items, item)
|
||||||
|
continue
|
||||||
|
}
|
||||||
account := &Account{
|
account := &Account{
|
||||||
Name: defaultName(src.Name, src.ID),
|
Name: defaultName(src.Name, src.ID),
|
||||||
Platform: PlatformOpenAI,
|
Platform: PlatformOpenAI,
|
||||||
@@ -690,6 +720,13 @@ func (s *CRSSyncService) SyncFromCRS(ctx context.Context, input SyncFromCRSInput
|
|||||||
}
|
}
|
||||||
|
|
||||||
if existing == nil {
|
if existing == nil {
|
||||||
|
if !shouldCreateAccount(src.ID, selectedSet) {
|
||||||
|
item.Action = "skipped"
|
||||||
|
item.Error = "not selected"
|
||||||
|
result.Skipped++
|
||||||
|
result.Items = append(result.Items, item)
|
||||||
|
continue
|
||||||
|
}
|
||||||
account := &Account{
|
account := &Account{
|
||||||
Name: defaultName(src.Name, src.ID),
|
Name: defaultName(src.Name, src.ID),
|
||||||
Platform: PlatformOpenAI,
|
Platform: PlatformOpenAI,
|
||||||
@@ -798,6 +835,13 @@ func (s *CRSSyncService) SyncFromCRS(ctx context.Context, input SyncFromCRSInput
|
|||||||
}
|
}
|
||||||
|
|
||||||
if existing == nil {
|
if existing == nil {
|
||||||
|
if !shouldCreateAccount(src.ID, selectedSet) {
|
||||||
|
item.Action = "skipped"
|
||||||
|
item.Error = "not selected"
|
||||||
|
result.Skipped++
|
||||||
|
result.Items = append(result.Items, item)
|
||||||
|
continue
|
||||||
|
}
|
||||||
account := &Account{
|
account := &Account{
|
||||||
Name: defaultName(src.Name, src.ID),
|
Name: defaultName(src.Name, src.ID),
|
||||||
Platform: PlatformGemini,
|
Platform: PlatformGemini,
|
||||||
@@ -909,6 +953,13 @@ func (s *CRSSyncService) SyncFromCRS(ctx context.Context, input SyncFromCRSInput
|
|||||||
}
|
}
|
||||||
|
|
||||||
if existing == nil {
|
if existing == nil {
|
||||||
|
if !shouldCreateAccount(src.ID, selectedSet) {
|
||||||
|
item.Action = "skipped"
|
||||||
|
item.Error = "not selected"
|
||||||
|
result.Skipped++
|
||||||
|
result.Items = append(result.Items, item)
|
||||||
|
continue
|
||||||
|
}
|
||||||
account := &Account{
|
account := &Account{
|
||||||
Name: defaultName(src.Name, src.ID),
|
Name: defaultName(src.Name, src.ID),
|
||||||
Platform: PlatformGemini,
|
Platform: PlatformGemini,
|
||||||
@@ -1253,3 +1304,102 @@ func (s *CRSSyncService) refreshOAuthToken(ctx context.Context, account *Account
|
|||||||
|
|
||||||
return newCredentials
|
return newCredentials
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// buildSelectedSet converts a slice of selected CRS account IDs to a set for O(1) lookup.
|
||||||
|
// Returns nil if ids is nil (field not sent → backward compatible: create all).
|
||||||
|
// Returns an empty map if ids is non-nil but empty (user selected none → create none).
|
||||||
|
func buildSelectedSet(ids []string) map[string]struct{} {
|
||||||
|
if ids == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
set := make(map[string]struct{}, len(ids))
|
||||||
|
for _, id := range ids {
|
||||||
|
set[id] = struct{}{}
|
||||||
|
}
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
|
// shouldCreateAccount checks if a new CRS account should be created based on user selection.
|
||||||
|
// Returns true if selectedSet is nil (backward compatible: create all) or if crsID is in the set.
|
||||||
|
func shouldCreateAccount(crsID string, selectedSet map[string]struct{}) bool {
|
||||||
|
if selectedSet == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
_, ok := selectedSet[crsID]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// PreviewFromCRSResult contains the preview of accounts from CRS before sync.
|
||||||
|
type PreviewFromCRSResult struct {
|
||||||
|
NewAccounts []CRSPreviewAccount `json:"new_accounts"`
|
||||||
|
ExistingAccounts []CRSPreviewAccount `json:"existing_accounts"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CRSPreviewAccount represents a single account in the preview result.
|
||||||
|
type CRSPreviewAccount struct {
|
||||||
|
CRSAccountID string `json:"crs_account_id"`
|
||||||
|
Kind string `json:"kind"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Platform string `json:"platform"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PreviewFromCRS connects to CRS, fetches all accounts, and classifies them
|
||||||
|
// as new or existing by batch-querying local crs_account_id mappings.
|
||||||
|
func (s *CRSSyncService) PreviewFromCRS(ctx context.Context, input SyncFromCRSInput) (*PreviewFromCRSResult, error) {
|
||||||
|
exported, err := s.fetchCRSExport(ctx, input.BaseURL, input.Username, input.Password)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Batch query all existing CRS account IDs
|
||||||
|
existingCRSIDs, err := s.accountRepo.ListCRSAccountIDs(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to list existing CRS accounts: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result := &PreviewFromCRSResult{
|
||||||
|
NewAccounts: make([]CRSPreviewAccount, 0),
|
||||||
|
ExistingAccounts: make([]CRSPreviewAccount, 0),
|
||||||
|
}
|
||||||
|
|
||||||
|
classify := func(crsID, kind, name, platform, accountType string) {
|
||||||
|
preview := CRSPreviewAccount{
|
||||||
|
CRSAccountID: crsID,
|
||||||
|
Kind: kind,
|
||||||
|
Name: defaultName(name, crsID),
|
||||||
|
Platform: platform,
|
||||||
|
Type: accountType,
|
||||||
|
}
|
||||||
|
if _, exists := existingCRSIDs[crsID]; exists {
|
||||||
|
result.ExistingAccounts = append(result.ExistingAccounts, preview)
|
||||||
|
} else {
|
||||||
|
result.NewAccounts = append(result.NewAccounts, preview)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, src := range exported.Data.ClaudeAccounts {
|
||||||
|
authType := strings.TrimSpace(src.AuthType)
|
||||||
|
if authType == "" {
|
||||||
|
authType = AccountTypeOAuth
|
||||||
|
}
|
||||||
|
classify(src.ID, src.Kind, src.Name, PlatformAnthropic, authType)
|
||||||
|
}
|
||||||
|
for _, src := range exported.Data.ClaudeConsoleAccounts {
|
||||||
|
classify(src.ID, src.Kind, src.Name, PlatformAnthropic, AccountTypeAPIKey)
|
||||||
|
}
|
||||||
|
for _, src := range exported.Data.OpenAIOAuthAccounts {
|
||||||
|
classify(src.ID, src.Kind, src.Name, PlatformOpenAI, AccountTypeOAuth)
|
||||||
|
}
|
||||||
|
for _, src := range exported.Data.OpenAIResponsesAccounts {
|
||||||
|
classify(src.ID, src.Kind, src.Name, PlatformOpenAI, AccountTypeAPIKey)
|
||||||
|
}
|
||||||
|
for _, src := range exported.Data.GeminiOAuthAccounts {
|
||||||
|
classify(src.ID, src.Kind, src.Name, PlatformGemini, AccountTypeOAuth)
|
||||||
|
}
|
||||||
|
for _, src := range exported.Data.GeminiAPIKeyAccounts {
|
||||||
|
classify(src.ID, src.Kind, src.Name, PlatformGemini, AccountTypeAPIKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ func TestRetryLoop_ErrorPolicy_CustomErrorCodes(t *testing.T) {
|
|||||||
customCodes: []any{float64(500)},
|
customCodes: []any{float64(500)},
|
||||||
expectHandleError: 0,
|
expectHandleError: 0,
|
||||||
expectUpstream: 1,
|
expectUpstream: 1,
|
||||||
expectStatusCode: 429,
|
expectStatusCode: 500,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "500_in_custom_codes_matched",
|
name: "500_in_custom_codes_matched",
|
||||||
@@ -364,3 +364,109 @@ func TestRetryLoop_ErrorPolicy_NoPolicy_OriginalBehavior(t *testing.T) {
|
|||||||
require.Equal(t, antigravityMaxRetries, upstream.calls, "should exhaust all retries")
|
require.Equal(t, antigravityMaxRetries, upstream.calls, "should exhaust all retries")
|
||||||
require.Equal(t, 1, handleErrorCount, "handleError should be called once after retries exhausted")
|
require.Equal(t, 1, handleErrorCount, "handleError should be called once after retries exhausted")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// epTrackingRepo — records SetRateLimited / SetError calls for verification.
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
type epTrackingRepo struct {
|
||||||
|
mockAccountRepoForGemini
|
||||||
|
rateLimitedCalls int
|
||||||
|
rateLimitedID int64
|
||||||
|
setErrCalls int
|
||||||
|
setErrID int64
|
||||||
|
tempCalls int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *epTrackingRepo) SetRateLimited(_ context.Context, id int64, _ time.Time) error {
|
||||||
|
r.rateLimitedCalls++
|
||||||
|
r.rateLimitedID = id
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *epTrackingRepo) SetError(_ context.Context, id int64, _ string) error {
|
||||||
|
r.setErrCalls++
|
||||||
|
r.setErrID = id
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *epTrackingRepo) SetTempUnschedulable(_ context.Context, _ int64, _ time.Time, _ string) error {
|
||||||
|
r.tempCalls++
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// TestCustomErrorCode599_SkippedErrors_Return500_NoRateLimit
|
||||||
|
//
|
||||||
|
// 核心场景:自定义错误码设为 [599](一个不会真正出现的错误码),
|
||||||
|
// 当上游返回 429/500/503/401 时:
|
||||||
|
// - 返回给客户端的状态码必须是 500(而不是透传原始状态码)
|
||||||
|
// - 不调用 SetRateLimited(不进入限流状态)
|
||||||
|
// - 不调用 SetError(不停止调度)
|
||||||
|
// - 不调用 handleError
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
func TestCustomErrorCode599_SkippedErrors_Return500_NoRateLimit(t *testing.T) {
|
||||||
|
errorCodes := []int{429, 500, 503, 401, 403}
|
||||||
|
|
||||||
|
for _, upstreamStatus := range errorCodes {
|
||||||
|
t.Run(http.StatusText(upstreamStatus), func(t *testing.T) {
|
||||||
|
saveAndSetBaseURLs(t)
|
||||||
|
|
||||||
|
upstream := &epFixedUpstream{
|
||||||
|
statusCode: upstreamStatus,
|
||||||
|
body: `{"error":"some upstream error"}`,
|
||||||
|
}
|
||||||
|
repo := &epTrackingRepo{}
|
||||||
|
rlSvc := NewRateLimitService(repo, nil, &config.Config{}, nil, nil)
|
||||||
|
svc := &AntigravityGatewayService{rateLimitService: rlSvc}
|
||||||
|
|
||||||
|
account := &Account{
|
||||||
|
ID: 500,
|
||||||
|
Type: AccountTypeAPIKey,
|
||||||
|
Platform: PlatformAntigravity,
|
||||||
|
Schedulable: true,
|
||||||
|
Status: StatusActive,
|
||||||
|
Concurrency: 1,
|
||||||
|
Credentials: map[string]any{
|
||||||
|
"custom_error_codes_enabled": true,
|
||||||
|
"custom_error_codes": []any{float64(599)},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var handleErrorCount int
|
||||||
|
p := newRetryParams(account, upstream, func(_ context.Context, _ string, _ *Account, _ int, _ http.Header, _ []byte, _ string, _ int64, _ string, _ bool) *handleModelRateLimitResult {
|
||||||
|
handleErrorCount++
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
result, err := svc.antigravityRetryLoop(p)
|
||||||
|
|
||||||
|
// 不应返回 error(Skipped 不触发账号切换)
|
||||||
|
require.NoError(t, err, "should not return error")
|
||||||
|
require.NotNil(t, result, "result should not be nil")
|
||||||
|
require.NotNil(t, result.resp, "response should not be nil")
|
||||||
|
defer func() { _ = result.resp.Body.Close() }()
|
||||||
|
|
||||||
|
// 状态码必须是 500(不透传原始状态码)
|
||||||
|
require.Equal(t, http.StatusInternalServerError, result.resp.StatusCode,
|
||||||
|
"skipped error should return 500, not %d", upstreamStatus)
|
||||||
|
|
||||||
|
// 不调用 handleError
|
||||||
|
require.Equal(t, 0, handleErrorCount,
|
||||||
|
"handleError should NOT be called for skipped errors")
|
||||||
|
|
||||||
|
// 不标记限流
|
||||||
|
require.Equal(t, 0, repo.rateLimitedCalls,
|
||||||
|
"SetRateLimited should NOT be called for skipped errors")
|
||||||
|
|
||||||
|
// 不停止调度
|
||||||
|
require.Equal(t, 0, repo.setErrCalls,
|
||||||
|
"SetError should NOT be called for skipped errors")
|
||||||
|
|
||||||
|
// 只调用一次上游(不重试)
|
||||||
|
require.Equal(t, 1, upstream.calls,
|
||||||
|
"should call upstream exactly once (no retry)")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -158,6 +158,7 @@ func TestApplyErrorPolicy(t *testing.T) {
|
|||||||
statusCode int
|
statusCode int
|
||||||
body []byte
|
body []byte
|
||||||
expectedHandled bool
|
expectedHandled bool
|
||||||
|
expectedStatus int // expected outStatus
|
||||||
expectedSwitchErr bool // expect *AntigravityAccountSwitchError
|
expectedSwitchErr bool // expect *AntigravityAccountSwitchError
|
||||||
handleErrorCalls int
|
handleErrorCalls int
|
||||||
}{
|
}{
|
||||||
@@ -171,6 +172,7 @@ func TestApplyErrorPolicy(t *testing.T) {
|
|||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
body: []byte(`"error"`),
|
body: []byte(`"error"`),
|
||||||
expectedHandled: false,
|
expectedHandled: false,
|
||||||
|
expectedStatus: 500, // passthrough
|
||||||
handleErrorCalls: 0,
|
handleErrorCalls: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -187,6 +189,7 @@ func TestApplyErrorPolicy(t *testing.T) {
|
|||||||
statusCode: 500, // not in custom codes
|
statusCode: 500, // not in custom codes
|
||||||
body: []byte(`"error"`),
|
body: []byte(`"error"`),
|
||||||
expectedHandled: true,
|
expectedHandled: true,
|
||||||
|
expectedStatus: http.StatusInternalServerError, // skipped → 500
|
||||||
handleErrorCalls: 0,
|
handleErrorCalls: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -203,6 +206,7 @@ func TestApplyErrorPolicy(t *testing.T) {
|
|||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
body: []byte(`"error"`),
|
body: []byte(`"error"`),
|
||||||
expectedHandled: true,
|
expectedHandled: true,
|
||||||
|
expectedStatus: 500, // matched → original status
|
||||||
handleErrorCalls: 1,
|
handleErrorCalls: 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -225,6 +229,7 @@ func TestApplyErrorPolicy(t *testing.T) {
|
|||||||
statusCode: 503,
|
statusCode: 503,
|
||||||
body: []byte(`overloaded`),
|
body: []byte(`overloaded`),
|
||||||
expectedHandled: true,
|
expectedHandled: true,
|
||||||
|
expectedStatus: 503, // temp_unscheduled → original status
|
||||||
expectedSwitchErr: true,
|
expectedSwitchErr: true,
|
||||||
handleErrorCalls: 0,
|
handleErrorCalls: 0,
|
||||||
},
|
},
|
||||||
@@ -250,9 +255,10 @@ func TestApplyErrorPolicy(t *testing.T) {
|
|||||||
isStickySession: true,
|
isStickySession: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
handled, retErr := svc.applyErrorPolicy(p, tt.statusCode, http.Header{}, tt.body)
|
handled, outStatus, retErr := svc.applyErrorPolicy(p, tt.statusCode, http.Header{}, tt.body)
|
||||||
|
|
||||||
require.Equal(t, tt.expectedHandled, handled, "handled mismatch")
|
require.Equal(t, tt.expectedHandled, handled, "handled mismatch")
|
||||||
|
require.Equal(t, tt.expectedStatus, outStatus, "outStatus mismatch")
|
||||||
require.Equal(t, tt.handleErrorCalls, handleErrorCount, "handleError call count mismatch")
|
require.Equal(t, tt.handleErrorCalls, handleErrorCount, "handleError call count mismatch")
|
||||||
|
|
||||||
if tt.expectedSwitchErr {
|
if tt.expectedSwitchErr {
|
||||||
|
|||||||
@@ -77,6 +77,9 @@ func (m *mockAccountRepoForPlatform) Create(ctx context.Context, account *Accoun
|
|||||||
func (m *mockAccountRepoForPlatform) GetByCRSAccountID(ctx context.Context, crsAccountID string) (*Account, error) {
|
func (m *mockAccountRepoForPlatform) GetByCRSAccountID(ctx context.Context, crsAccountID string) (*Account, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
func (m *mockAccountRepoForPlatform) ListCRSAccountIDs(ctx context.Context) (map[string]int64, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
func (m *mockAccountRepoForPlatform) Update(ctx context.Context, account *Account) error {
|
func (m *mockAccountRepoForPlatform) Update(ctx context.Context, account *Account) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -770,6 +770,14 @@ func (s *GeminiMessagesCompatService) Forward(ctx context.Context, c *gin.Contex
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 错误策略优先:匹配则跳过重试直接处理。
|
||||||
|
if matched, rebuilt := s.checkErrorPolicyInLoop(ctx, account, resp); matched {
|
||||||
|
resp = rebuilt
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
resp = rebuilt
|
||||||
|
}
|
||||||
|
|
||||||
if resp.StatusCode >= 400 && s.shouldRetryGeminiUpstreamError(account, resp.StatusCode) {
|
if resp.StatusCode >= 400 && s.shouldRetryGeminiUpstreamError(account, resp.StatusCode) {
|
||||||
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 2<<20))
|
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 2<<20))
|
||||||
_ = resp.Body.Close()
|
_ = resp.Body.Close()
|
||||||
@@ -839,7 +847,7 @@ func (s *GeminiMessagesCompatService) Forward(ctx context.Context, c *gin.Contex
|
|||||||
if upstreamReqID == "" {
|
if upstreamReqID == "" {
|
||||||
upstreamReqID = resp.Header.Get("x-goog-request-id")
|
upstreamReqID = resp.Header.Get("x-goog-request-id")
|
||||||
}
|
}
|
||||||
return nil, s.writeGeminiMappedError(c, account, resp.StatusCode, upstreamReqID, respBody)
|
return nil, s.writeGeminiMappedError(c, account, http.StatusInternalServerError, upstreamReqID, respBody)
|
||||||
case ErrorPolicyMatched, ErrorPolicyTempUnscheduled:
|
case ErrorPolicyMatched, ErrorPolicyTempUnscheduled:
|
||||||
s.handleGeminiUpstreamError(ctx, account, resp.StatusCode, resp.Header, respBody)
|
s.handleGeminiUpstreamError(ctx, account, resp.StatusCode, resp.Header, respBody)
|
||||||
upstreamReqID := resp.Header.Get(requestIDHeader)
|
upstreamReqID := resp.Header.Get(requestIDHeader)
|
||||||
@@ -1176,6 +1184,14 @@ func (s *GeminiMessagesCompatService) ForwardNative(ctx context.Context, c *gin.
|
|||||||
return nil, s.writeGoogleError(c, http.StatusBadGateway, "Upstream request failed after retries: "+safeErr)
|
return nil, s.writeGoogleError(c, http.StatusBadGateway, "Upstream request failed after retries: "+safeErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 错误策略优先:匹配则跳过重试直接处理。
|
||||||
|
if matched, rebuilt := s.checkErrorPolicyInLoop(ctx, account, resp); matched {
|
||||||
|
resp = rebuilt
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
resp = rebuilt
|
||||||
|
}
|
||||||
|
|
||||||
if resp.StatusCode >= 400 && s.shouldRetryGeminiUpstreamError(account, resp.StatusCode) {
|
if resp.StatusCode >= 400 && s.shouldRetryGeminiUpstreamError(account, resp.StatusCode) {
|
||||||
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 2<<20))
|
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 2<<20))
|
||||||
_ = resp.Body.Close()
|
_ = resp.Body.Close()
|
||||||
@@ -1283,7 +1299,7 @@ func (s *GeminiMessagesCompatService) ForwardNative(ctx context.Context, c *gin.
|
|||||||
if contentType == "" {
|
if contentType == "" {
|
||||||
contentType = "application/json"
|
contentType = "application/json"
|
||||||
}
|
}
|
||||||
c.Data(resp.StatusCode, contentType, respBody)
|
c.Data(http.StatusInternalServerError, contentType, respBody)
|
||||||
return nil, fmt.Errorf("gemini upstream error: %d (skipped by error policy)", resp.StatusCode)
|
return nil, fmt.Errorf("gemini upstream error: %d (skipped by error policy)", resp.StatusCode)
|
||||||
case ErrorPolicyMatched, ErrorPolicyTempUnscheduled:
|
case ErrorPolicyMatched, ErrorPolicyTempUnscheduled:
|
||||||
s.handleGeminiUpstreamError(ctx, account, resp.StatusCode, resp.Header, respBody)
|
s.handleGeminiUpstreamError(ctx, account, resp.StatusCode, resp.Header, respBody)
|
||||||
@@ -1425,6 +1441,26 @@ func (s *GeminiMessagesCompatService) ForwardNative(ctx context.Context, c *gin.
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkErrorPolicyInLoop 在重试循环内预检查错误策略。
|
||||||
|
// 返回 true 表示策略已匹配(调用者应 break),resp 已重建可直接使用。
|
||||||
|
// 返回 false 表示 ErrorPolicyNone,resp 已重建,调用者继续走重试逻辑。
|
||||||
|
func (s *GeminiMessagesCompatService) checkErrorPolicyInLoop(
|
||||||
|
ctx context.Context, account *Account, resp *http.Response,
|
||||||
|
) (matched bool, rebuilt *http.Response) {
|
||||||
|
if resp.StatusCode < 400 || s.rateLimitService == nil {
|
||||||
|
return false, resp
|
||||||
|
}
|
||||||
|
body, _ := io.ReadAll(io.LimitReader(resp.Body, 2<<20))
|
||||||
|
_ = resp.Body.Close()
|
||||||
|
rebuilt = &http.Response{
|
||||||
|
StatusCode: resp.StatusCode,
|
||||||
|
Header: resp.Header.Clone(),
|
||||||
|
Body: io.NopCloser(bytes.NewReader(body)),
|
||||||
|
}
|
||||||
|
policy := s.rateLimitService.CheckErrorPolicy(ctx, account, resp.StatusCode, body)
|
||||||
|
return policy != ErrorPolicyNone, rebuilt
|
||||||
|
}
|
||||||
|
|
||||||
func (s *GeminiMessagesCompatService) shouldRetryGeminiUpstreamError(account *Account, statusCode int) bool {
|
func (s *GeminiMessagesCompatService) shouldRetryGeminiUpstreamError(account *Account, statusCode int) bool {
|
||||||
switch statusCode {
|
switch statusCode {
|
||||||
case 429, 500, 502, 503, 504, 529:
|
case 429, 500, 502, 503, 504, 529:
|
||||||
@@ -2597,6 +2633,10 @@ func asInt(v any) (int, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *GeminiMessagesCompatService) handleGeminiUpstreamError(ctx context.Context, account *Account, statusCode int, headers http.Header, body []byte) {
|
func (s *GeminiMessagesCompatService) handleGeminiUpstreamError(ctx context.Context, account *Account, statusCode int, headers http.Header, body []byte) {
|
||||||
|
// 遵守自定义错误码策略:未命中则跳过所有限流处理
|
||||||
|
if !account.ShouldHandleErrorCode(statusCode) {
|
||||||
|
return
|
||||||
|
}
|
||||||
if s.rateLimitService != nil && (statusCode == 401 || statusCode == 403 || statusCode == 529) {
|
if s.rateLimitService != nil && (statusCode == 401 || statusCode == 403 || statusCode == 529) {
|
||||||
s.rateLimitService.HandleUpstreamError(ctx, account, statusCode, headers, body)
|
s.rateLimitService.HandleUpstreamError(ctx, account, statusCode, headers, body)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -66,6 +66,9 @@ func (m *mockAccountRepoForGemini) Create(ctx context.Context, account *Account)
|
|||||||
func (m *mockAccountRepoForGemini) GetByCRSAccountID(ctx context.Context, crsAccountID string) (*Account, error) {
|
func (m *mockAccountRepoForGemini) GetByCRSAccountID(ctx context.Context, crsAccountID string) (*Account, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
func (m *mockAccountRepoForGemini) ListCRSAccountIDs(ctx context.Context) (map[string]int64, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
func (m *mockAccountRepoForGemini) Update(ctx context.Context, account *Account) error { return nil }
|
func (m *mockAccountRepoForGemini) Update(ctx context.Context, account *Account) error { return nil }
|
||||||
func (m *mockAccountRepoForGemini) Delete(ctx context.Context, id int64) error { return nil }
|
func (m *mockAccountRepoForGemini) Delete(ctx context.Context, id int64) error { return nil }
|
||||||
func (m *mockAccountRepoForGemini) List(ctx context.Context, params pagination.PaginationParams) ([]Account, *pagination.PaginationResult, error) {
|
func (m *mockAccountRepoForGemini) List(ctx context.Context, params pagination.PaginationParams) ([]Account, *pagination.PaginationResult, error) {
|
||||||
|
|||||||
@@ -623,6 +623,10 @@ func (s *RateLimitService) ClearTempUnschedulable(ctx context.Context, accountID
|
|||||||
slog.Warn("temp_unsched_cache_delete_failed", "account_id", accountID, "error", err)
|
slog.Warn("temp_unsched_cache_delete_failed", "account_id", accountID, "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 同时清除模型级别限流
|
||||||
|
if err := s.accountRepo.ClearModelRateLimits(ctx, accountID); err != nil {
|
||||||
|
slog.Warn("clear_model_rate_limits_on_temp_unsched_reset_failed", "account_id", accountID, "error", err)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -327,11 +327,34 @@ export async function getAvailableModels(id: number): Promise<ClaudeModel[]> {
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CRSPreviewAccount {
|
||||||
|
crs_account_id: string
|
||||||
|
kind: string
|
||||||
|
name: string
|
||||||
|
platform: string
|
||||||
|
type: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PreviewFromCRSResult {
|
||||||
|
new_accounts: CRSPreviewAccount[]
|
||||||
|
existing_accounts: CRSPreviewAccount[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function previewFromCrs(params: {
|
||||||
|
base_url: string
|
||||||
|
username: string
|
||||||
|
password: string
|
||||||
|
}): Promise<PreviewFromCRSResult> {
|
||||||
|
const { data } = await apiClient.post<PreviewFromCRSResult>('/admin/accounts/sync/crs/preview', params)
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
export async function syncFromCrs(params: {
|
export async function syncFromCrs(params: {
|
||||||
base_url: string
|
base_url: string
|
||||||
username: string
|
username: string
|
||||||
password: string
|
password: string
|
||||||
sync_proxies?: boolean
|
sync_proxies?: boolean
|
||||||
|
selected_account_ids?: string[]
|
||||||
}): Promise<{
|
}): Promise<{
|
||||||
created: number
|
created: number
|
||||||
updated: number
|
updated: number
|
||||||
@@ -345,7 +368,19 @@ export async function syncFromCrs(params: {
|
|||||||
error?: string
|
error?: string
|
||||||
}>
|
}>
|
||||||
}> {
|
}> {
|
||||||
const { data } = await apiClient.post('/admin/accounts/sync/crs', params)
|
const { data } = await apiClient.post<{
|
||||||
|
created: number
|
||||||
|
updated: number
|
||||||
|
skipped: number
|
||||||
|
failed: number
|
||||||
|
items: Array<{
|
||||||
|
crs_account_id: string
|
||||||
|
kind: string
|
||||||
|
name: string
|
||||||
|
action: string
|
||||||
|
error?: string
|
||||||
|
}>
|
||||||
|
}>('/admin/accounts/sync/crs', params)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -442,6 +477,7 @@ export const accountsAPI = {
|
|||||||
batchCreate,
|
batchCreate,
|
||||||
batchUpdateCredentials,
|
batchUpdateCredentials,
|
||||||
bulkUpdate,
|
bulkUpdate,
|
||||||
|
previewFromCrs,
|
||||||
syncFromCrs,
|
syncFromCrs,
|
||||||
exportData,
|
exportData,
|
||||||
importData,
|
importData,
|
||||||
|
|||||||
@@ -665,8 +665,8 @@
|
|||||||
<Icon name="cloud" size="sm" />
|
<Icon name="cloud" size="sm" />
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<span class="block text-sm font-medium text-gray-900 dark:text-white">{{ t('admin.accounts.types.upstream') }}</span>
|
<span class="block text-sm font-medium text-gray-900 dark:text-white">API Key</span>
|
||||||
<span class="text-xs text-gray-500 dark:text-gray-400">{{ t('admin.accounts.types.upstreamDesc') }}</span>
|
<span class="text-xs text-gray-500 dark:text-gray-400">{{ t('admin.accounts.types.antigravityApikey') }}</span>
|
||||||
</div>
|
</div>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -681,7 +681,7 @@
|
|||||||
type="text"
|
type="text"
|
||||||
required
|
required
|
||||||
class="input"
|
class="input"
|
||||||
placeholder="https://s.konstants.xyz"
|
placeholder="https://cloudcode-pa.googleapis.com"
|
||||||
/>
|
/>
|
||||||
<p class="input-hint">{{ t('admin.accounts.upstream.baseUrlHint') }}</p>
|
<p class="input-hint">{{ t('admin.accounts.upstream.baseUrlHint') }}</p>
|
||||||
</div>
|
</div>
|
||||||
@@ -816,8 +816,8 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- API Key input (only for apikey type) -->
|
<!-- API Key input (only for apikey type, excluding Antigravity which has its own fields) -->
|
||||||
<div v-if="form.type === 'apikey'" class="space-y-4">
|
<div v-if="form.type === 'apikey' && form.platform !== 'antigravity'" class="space-y-4">
|
||||||
<div>
|
<div>
|
||||||
<label class="input-label">{{ t('admin.accounts.baseUrl') }}</label>
|
<label class="input-label">{{ t('admin.accounts.baseUrl') }}</label>
|
||||||
<input
|
<input
|
||||||
@@ -862,7 +862,7 @@
|
|||||||
<p class="input-hint">{{ t('admin.accounts.gemini.tier.aiStudioHint') }}</p>
|
<p class="input-hint">{{ t('admin.accounts.gemini.tier.aiStudioHint') }}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Model Restriction Section (不适用于 Gemini) -->
|
<!-- Model Restriction Section (不适用于 Gemini,Antigravity 已在上层条件排除) -->
|
||||||
<div v-if="form.platform !== 'gemini'" class="border-t border-gray-200 pt-4 dark:border-dark-600">
|
<div v-if="form.platform !== 'gemini'" class="border-t border-gray-200 pt-4 dark:border-dark-600">
|
||||||
<label class="input-label">{{ t('admin.accounts.modelRestriction') }}</label>
|
<label class="input-label">{{ t('admin.accounts.modelRestriction') }}</label>
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,9 @@
|
|||||||
? 'https://api.openai.com'
|
? 'https://api.openai.com'
|
||||||
: account.platform === 'gemini'
|
: account.platform === 'gemini'
|
||||||
? 'https://generativelanguage.googleapis.com'
|
? 'https://generativelanguage.googleapis.com'
|
||||||
: 'https://api.anthropic.com'
|
: account.platform === 'antigravity'
|
||||||
|
? 'https://cloudcode-pa.googleapis.com'
|
||||||
|
: 'https://api.anthropic.com'
|
||||||
"
|
"
|
||||||
/>
|
/>
|
||||||
<p class="input-hint">{{ baseUrlHint }}</p>
|
<p class="input-hint">{{ baseUrlHint }}</p>
|
||||||
@@ -55,14 +57,16 @@
|
|||||||
? 'sk-proj-...'
|
? 'sk-proj-...'
|
||||||
: account.platform === 'gemini'
|
: account.platform === 'gemini'
|
||||||
? 'AIza...'
|
? 'AIza...'
|
||||||
: 'sk-ant-...'
|
: account.platform === 'antigravity'
|
||||||
|
? 'sk-...'
|
||||||
|
: 'sk-ant-...'
|
||||||
"
|
"
|
||||||
/>
|
/>
|
||||||
<p class="input-hint">{{ t('admin.accounts.leaveEmptyToKeep') }}</p>
|
<p class="input-hint">{{ t('admin.accounts.leaveEmptyToKeep') }}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Model Restriction Section (不适用于 Gemini) -->
|
<!-- Model Restriction Section (不适用于 Gemini 和 Antigravity) -->
|
||||||
<div v-if="account.platform !== 'gemini'" class="border-t border-gray-200 pt-4 dark:border-dark-600">
|
<div v-if="account.platform !== 'gemini' && account.platform !== 'antigravity'" class="border-t border-gray-200 pt-4 dark:border-dark-600">
|
||||||
<label class="input-label">{{ t('admin.accounts.modelRestriction') }}</label>
|
<label class="input-label">{{ t('admin.accounts.modelRestriction') }}</label>
|
||||||
|
|
||||||
<!-- Mode Toggle -->
|
<!-- Mode Toggle -->
|
||||||
@@ -372,7 +376,7 @@
|
|||||||
v-model="editBaseUrl"
|
v-model="editBaseUrl"
|
||||||
type="text"
|
type="text"
|
||||||
class="input"
|
class="input"
|
||||||
placeholder="https://s.konstants.xyz"
|
placeholder="https://cloudcode-pa.googleapis.com"
|
||||||
/>
|
/>
|
||||||
<p class="input-hint">{{ t('admin.accounts.upstream.baseUrlHint') }}</p>
|
<p class="input-hint">{{ t('admin.accounts.upstream.baseUrlHint') }}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -6,15 +6,20 @@
|
|||||||
close-on-click-outside
|
close-on-click-outside
|
||||||
@close="handleClose"
|
@close="handleClose"
|
||||||
>
|
>
|
||||||
<form id="sync-from-crs-form" class="space-y-4" @submit.prevent="handleSync">
|
<!-- Step 1: Input credentials -->
|
||||||
|
<form
|
||||||
|
v-if="currentStep === 'input'"
|
||||||
|
id="sync-from-crs-form"
|
||||||
|
class="space-y-4"
|
||||||
|
@submit.prevent="handlePreview"
|
||||||
|
>
|
||||||
<div class="text-sm text-gray-600 dark:text-dark-300">
|
<div class="text-sm text-gray-600 dark:text-dark-300">
|
||||||
{{ t('admin.accounts.syncFromCrsDesc') }}
|
{{ t('admin.accounts.syncFromCrsDesc') }}
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
class="rounded-lg bg-gray-50 p-3 text-xs text-gray-500 dark:bg-dark-700/60 dark:text-dark-400"
|
class="rounded-lg bg-gray-50 p-3 text-xs text-gray-500 dark:bg-dark-700/60 dark:text-dark-400"
|
||||||
>
|
>
|
||||||
已有账号仅同步 CRS
|
{{ t('admin.accounts.crsUpdateBehaviorNote') }}
|
||||||
返回的字段,缺失字段保持原值;凭据按键合并,不会清空未下发的键;未勾选"同步代理"时保留原有代理。
|
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
class="rounded-lg border border-amber-200 bg-amber-50 p-3 text-xs text-amber-600 dark:border-amber-800 dark:bg-amber-900/20 dark:text-amber-400"
|
class="rounded-lg border border-amber-200 bg-amber-50 p-3 text-xs text-amber-600 dark:border-amber-800 dark:bg-amber-900/20 dark:text-amber-400"
|
||||||
@@ -24,26 +29,30 @@
|
|||||||
|
|
||||||
<div class="grid grid-cols-1 gap-4">
|
<div class="grid grid-cols-1 gap-4">
|
||||||
<div>
|
<div>
|
||||||
<label class="input-label">{{ t('admin.accounts.crsBaseUrl') }}</label>
|
<label for="crs-base-url" class="input-label">{{ t('admin.accounts.crsBaseUrl') }}</label>
|
||||||
<input
|
<input
|
||||||
|
id="crs-base-url"
|
||||||
v-model="form.base_url"
|
v-model="form.base_url"
|
||||||
type="text"
|
type="text"
|
||||||
class="input"
|
class="input"
|
||||||
|
required
|
||||||
:placeholder="t('admin.accounts.crsBaseUrlPlaceholder')"
|
:placeholder="t('admin.accounts.crsBaseUrlPlaceholder')"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||||
<div>
|
<div>
|
||||||
<label class="input-label">{{ t('admin.accounts.crsUsername') }}</label>
|
<label for="crs-username" class="input-label">{{ t('admin.accounts.crsUsername') }}</label>
|
||||||
<input v-model="form.username" type="text" class="input" autocomplete="username" />
|
<input id="crs-username" v-model="form.username" type="text" class="input" required autocomplete="username" />
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label class="input-label">{{ t('admin.accounts.crsPassword') }}</label>
|
<label for="crs-password" class="input-label">{{ t('admin.accounts.crsPassword') }}</label>
|
||||||
<input
|
<input
|
||||||
|
id="crs-password"
|
||||||
v-model="form.password"
|
v-model="form.password"
|
||||||
type="password"
|
type="password"
|
||||||
class="input"
|
class="input"
|
||||||
|
required
|
||||||
autocomplete="current-password"
|
autocomplete="current-password"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@@ -58,9 +67,101 @@
|
|||||||
{{ t('admin.accounts.syncProxies') }}
|
{{ t('admin.accounts.syncProxies') }}
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<!-- Step 2: Preview & select -->
|
||||||
|
<div v-else-if="currentStep === 'preview' && previewResult" class="space-y-4">
|
||||||
|
<!-- Existing accounts (read-only info) -->
|
||||||
|
<div
|
||||||
|
v-if="previewResult.existing_accounts.length"
|
||||||
|
class="rounded-lg bg-gray-50 p-3 dark:bg-dark-700/60"
|
||||||
|
>
|
||||||
|
<div class="mb-2 text-sm font-medium text-gray-700 dark:text-dark-300">
|
||||||
|
{{ t('admin.accounts.crsExistingAccounts') }}
|
||||||
|
<span class="ml-1 text-xs text-gray-400">({{ previewResult.existing_accounts.length }})</span>
|
||||||
|
</div>
|
||||||
|
<div class="max-h-32 overflow-auto text-xs text-gray-500 dark:text-dark-400">
|
||||||
|
<div
|
||||||
|
v-for="acc in previewResult.existing_accounts"
|
||||||
|
:key="acc.crs_account_id"
|
||||||
|
class="flex items-center gap-2 py-0.5"
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
class="inline-block rounded bg-blue-100 px-1.5 py-0.5 text-[10px] font-medium text-blue-700 dark:bg-blue-900/30 dark:text-blue-400"
|
||||||
|
>{{ acc.platform }} / {{ acc.type }}</span>
|
||||||
|
<span class="truncate">{{ acc.name }}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- New accounts (selectable) -->
|
||||||
|
<div v-if="previewResult.new_accounts.length">
|
||||||
|
<div class="mb-2 flex items-center justify-between">
|
||||||
|
<div class="text-sm font-medium text-gray-900 dark:text-white">
|
||||||
|
{{ t('admin.accounts.crsNewAccounts') }}
|
||||||
|
<span class="ml-1 text-xs text-gray-400">({{ previewResult.new_accounts.length }})</span>
|
||||||
|
</div>
|
||||||
|
<div class="flex gap-2">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="text-xs text-blue-600 hover:text-blue-700 dark:text-blue-400"
|
||||||
|
@click="selectAll"
|
||||||
|
>{{ t('admin.accounts.crsSelectAll') }}</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="text-xs text-gray-500 hover:text-gray-600 dark:text-gray-400"
|
||||||
|
@click="selectNone"
|
||||||
|
>{{ t('admin.accounts.crsSelectNone') }}</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
class="max-h-48 overflow-auto rounded-lg border border-gray-200 p-2 dark:border-dark-600"
|
||||||
|
>
|
||||||
|
<label
|
||||||
|
v-for="acc in previewResult.new_accounts"
|
||||||
|
:key="acc.crs_account_id"
|
||||||
|
class="flex cursor-pointer items-center gap-2 rounded px-2 py-1.5 hover:bg-gray-50 dark:hover:bg-dark-700/40"
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
:checked="selectedIds.has(acc.crs_account_id)"
|
||||||
|
class="rounded border-gray-300 dark:border-dark-600"
|
||||||
|
@change="toggleSelect(acc.crs_account_id)"
|
||||||
|
/>
|
||||||
|
<span
|
||||||
|
class="inline-block rounded bg-green-100 px-1.5 py-0.5 text-[10px] font-medium text-green-700 dark:bg-green-900/30 dark:text-green-400"
|
||||||
|
>{{ acc.platform }} / {{ acc.type }}</span>
|
||||||
|
<span class="truncate text-sm text-gray-700 dark:text-dark-300">{{ acc.name }}</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<div class="mt-1 text-xs text-gray-400">
|
||||||
|
{{ t('admin.accounts.crsSelectedCount', { count: selectedIds.size }) }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Sync options summary -->
|
||||||
|
<div class="flex items-center gap-2 text-xs text-gray-500 dark:text-dark-400">
|
||||||
|
<span>{{ t('admin.accounts.syncProxies') }}:</span>
|
||||||
|
<span :class="form.sync_proxies ? 'text-green-600 dark:text-green-400' : 'text-gray-400 dark:text-dark-500'">
|
||||||
|
{{ form.sync_proxies ? t('common.yes') : t('common.no') }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- No new accounts -->
|
||||||
|
<div
|
||||||
|
v-if="!previewResult.new_accounts.length"
|
||||||
|
class="rounded-lg bg-gray-50 p-4 text-center text-sm text-gray-500 dark:bg-dark-700/60 dark:text-dark-400"
|
||||||
|
>
|
||||||
|
{{ t('admin.accounts.crsNoNewAccounts') }}
|
||||||
|
<span v-if="previewResult.existing_accounts.length">
|
||||||
|
{{ t('admin.accounts.crsWillUpdate', { count: previewResult.existing_accounts.length }) }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Step 3: Result -->
|
||||||
|
<div v-else-if="currentStep === 'result' && result" class="space-y-4">
|
||||||
<div
|
<div
|
||||||
v-if="result"
|
|
||||||
class="space-y-2 rounded-xl border border-gray-200 p-4 dark:border-dark-700"
|
class="space-y-2 rounded-xl border border-gray-200 p-4 dark:border-dark-700"
|
||||||
>
|
>
|
||||||
<div class="text-sm font-medium text-gray-900 dark:text-white">
|
<div class="text-sm font-medium text-gray-900 dark:text-white">
|
||||||
@@ -84,21 +185,56 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</div>
|
||||||
|
|
||||||
<template #footer>
|
<template #footer>
|
||||||
<div class="flex justify-end gap-3">
|
<div class="flex justify-end gap-3">
|
||||||
<button class="btn btn-secondary" type="button" :disabled="syncing" @click="handleClose">
|
<!-- Step 1: Input -->
|
||||||
{{ t('common.cancel') }}
|
<template v-if="currentStep === 'input'">
|
||||||
</button>
|
<button
|
||||||
<button
|
class="btn btn-secondary"
|
||||||
class="btn btn-primary"
|
type="button"
|
||||||
type="submit"
|
:disabled="previewing"
|
||||||
form="sync-from-crs-form"
|
@click="handleClose"
|
||||||
:disabled="syncing"
|
>
|
||||||
>
|
{{ t('common.cancel') }}
|
||||||
{{ syncing ? t('admin.accounts.syncing') : t('admin.accounts.syncNow') }}
|
</button>
|
||||||
</button>
|
<button
|
||||||
|
class="btn btn-primary"
|
||||||
|
type="submit"
|
||||||
|
form="sync-from-crs-form"
|
||||||
|
:disabled="previewing"
|
||||||
|
>
|
||||||
|
{{ previewing ? t('admin.accounts.crsPreviewing') : t('admin.accounts.crsPreview') }}
|
||||||
|
</button>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Step 2: Preview -->
|
||||||
|
<template v-else-if="currentStep === 'preview'">
|
||||||
|
<button
|
||||||
|
class="btn btn-secondary"
|
||||||
|
type="button"
|
||||||
|
:disabled="syncing"
|
||||||
|
@click="handleBack"
|
||||||
|
>
|
||||||
|
{{ t('admin.accounts.crsBack') }}
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="btn btn-primary"
|
||||||
|
type="button"
|
||||||
|
:disabled="syncing || hasNewButNoneSelected"
|
||||||
|
@click="handleSync"
|
||||||
|
>
|
||||||
|
{{ syncing ? t('admin.accounts.syncing') : t('admin.accounts.syncNow') }}
|
||||||
|
</button>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<!-- Step 3: Result -->
|
||||||
|
<template v-else-if="currentStep === 'result'">
|
||||||
|
<button class="btn btn-secondary" type="button" @click="handleClose">
|
||||||
|
{{ t('common.close') }}
|
||||||
|
</button>
|
||||||
|
</template>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</BaseDialog>
|
</BaseDialog>
|
||||||
@@ -110,6 +246,7 @@ import { useI18n } from 'vue-i18n'
|
|||||||
import BaseDialog from '@/components/common/BaseDialog.vue'
|
import BaseDialog from '@/components/common/BaseDialog.vue'
|
||||||
import { useAppStore } from '@/stores/app'
|
import { useAppStore } from '@/stores/app'
|
||||||
import { adminAPI } from '@/api/admin'
|
import { adminAPI } from '@/api/admin'
|
||||||
|
import type { PreviewFromCRSResult } from '@/api/admin/accounts'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
show: boolean
|
show: boolean
|
||||||
@@ -126,7 +263,12 @@ const emit = defineEmits<Emits>()
|
|||||||
const { t } = useI18n()
|
const { t } = useI18n()
|
||||||
const appStore = useAppStore()
|
const appStore = useAppStore()
|
||||||
|
|
||||||
|
type Step = 'input' | 'preview' | 'result'
|
||||||
|
const currentStep = ref<Step>('input')
|
||||||
|
const previewing = ref(false)
|
||||||
const syncing = ref(false)
|
const syncing = ref(false)
|
||||||
|
const previewResult = ref<PreviewFromCRSResult | null>(null)
|
||||||
|
const selectedIds = ref(new Set<string>())
|
||||||
const result = ref<Awaited<ReturnType<typeof adminAPI.accounts.syncFromCrs>> | null>(null)
|
const result = ref<Awaited<ReturnType<typeof adminAPI.accounts.syncFromCrs>> | null>(null)
|
||||||
|
|
||||||
const form = reactive({
|
const form = reactive({
|
||||||
@@ -136,28 +278,90 @@ const form = reactive({
|
|||||||
sync_proxies: true
|
sync_proxies: true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const hasNewButNoneSelected = computed(() => {
|
||||||
|
if (!previewResult.value) return false
|
||||||
|
return previewResult.value.new_accounts.length > 0 && selectedIds.value.size === 0
|
||||||
|
})
|
||||||
|
|
||||||
const errorItems = computed(() => {
|
const errorItems = computed(() => {
|
||||||
if (!result.value?.items) return []
|
if (!result.value?.items) return []
|
||||||
return result.value.items.filter((i) => i.action === 'failed' || i.action === 'skipped')
|
return result.value.items.filter(
|
||||||
|
(i) => i.action === 'failed' || (i.action === 'skipped' && i.error !== 'not selected')
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => props.show,
|
() => props.show,
|
||||||
(open) => {
|
(open) => {
|
||||||
if (open) {
|
if (open) {
|
||||||
|
currentStep.value = 'input'
|
||||||
|
previewResult.value = null
|
||||||
|
selectedIds.value = new Set()
|
||||||
result.value = null
|
result.value = null
|
||||||
|
form.base_url = ''
|
||||||
|
form.username = ''
|
||||||
|
form.password = ''
|
||||||
|
form.sync_proxies = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
const handleClose = () => {
|
const handleClose = () => {
|
||||||
// 防止在同步进行中关闭对话框
|
if (syncing.value || previewing.value) {
|
||||||
if (syncing.value) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
emit('close')
|
emit('close')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleBack = () => {
|
||||||
|
currentStep.value = 'input'
|
||||||
|
previewResult.value = null
|
||||||
|
selectedIds.value = new Set()
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectAll = () => {
|
||||||
|
if (!previewResult.value) return
|
||||||
|
selectedIds.value = new Set(previewResult.value.new_accounts.map((a) => a.crs_account_id))
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectNone = () => {
|
||||||
|
selectedIds.value = new Set()
|
||||||
|
}
|
||||||
|
|
||||||
|
const toggleSelect = (id: string) => {
|
||||||
|
const s = new Set(selectedIds.value)
|
||||||
|
if (s.has(id)) {
|
||||||
|
s.delete(id)
|
||||||
|
} else {
|
||||||
|
s.add(id)
|
||||||
|
}
|
||||||
|
selectedIds.value = s
|
||||||
|
}
|
||||||
|
|
||||||
|
const handlePreview = async () => {
|
||||||
|
if (!form.base_url.trim() || !form.username.trim() || !form.password.trim()) {
|
||||||
|
appStore.showError(t('admin.accounts.syncMissingFields'))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
previewing.value = true
|
||||||
|
try {
|
||||||
|
const res = await adminAPI.accounts.previewFromCrs({
|
||||||
|
base_url: form.base_url.trim(),
|
||||||
|
username: form.username.trim(),
|
||||||
|
password: form.password
|
||||||
|
})
|
||||||
|
previewResult.value = res
|
||||||
|
// Auto-select all new accounts
|
||||||
|
selectedIds.value = new Set(res.new_accounts.map((a) => a.crs_account_id))
|
||||||
|
currentStep.value = 'preview'
|
||||||
|
} catch (error: any) {
|
||||||
|
appStore.showError(error?.message || t('admin.accounts.crsPreviewFailed'))
|
||||||
|
} finally {
|
||||||
|
previewing.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const handleSync = async () => {
|
const handleSync = async () => {
|
||||||
if (!form.base_url.trim() || !form.username.trim() || !form.password.trim()) {
|
if (!form.base_url.trim() || !form.username.trim() || !form.password.trim()) {
|
||||||
appStore.showError(t('admin.accounts.syncMissingFields'))
|
appStore.showError(t('admin.accounts.syncMissingFields'))
|
||||||
@@ -170,16 +374,18 @@ const handleSync = async () => {
|
|||||||
base_url: form.base_url.trim(),
|
base_url: form.base_url.trim(),
|
||||||
username: form.username.trim(),
|
username: form.username.trim(),
|
||||||
password: form.password,
|
password: form.password,
|
||||||
sync_proxies: form.sync_proxies
|
sync_proxies: form.sync_proxies,
|
||||||
|
selected_account_ids: [...selectedIds.value]
|
||||||
})
|
})
|
||||||
result.value = res
|
result.value = res
|
||||||
|
currentStep.value = 'result'
|
||||||
|
|
||||||
if (res.failed > 0) {
|
if (res.failed > 0) {
|
||||||
appStore.showError(t('admin.accounts.syncCompletedWithErrors', res))
|
appStore.showError(t('admin.accounts.syncCompletedWithErrors', res))
|
||||||
} else {
|
} else {
|
||||||
appStore.showSuccess(t('admin.accounts.syncCompleted', res))
|
appStore.showSuccess(t('admin.accounts.syncCompleted', res))
|
||||||
emit('synced')
|
|
||||||
}
|
}
|
||||||
|
emit('synced')
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
appStore.showError(error?.message || t('admin.accounts.syncFailed'))
|
appStore.showError(error?.message || t('admin.accounts.syncFailed'))
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -53,7 +53,19 @@ import type { Account } from '@/types'
|
|||||||
const props = defineProps<{ show: boolean; account: Account | null; position: { top: number; left: number } | null }>()
|
const props = defineProps<{ show: boolean; account: Account | null; position: { top: number; left: number } | null }>()
|
||||||
const emit = defineEmits(['close', 'test', 'stats', 'reauth', 'refresh-token', 'reset-status', 'clear-rate-limit'])
|
const emit = defineEmits(['close', 'test', 'stats', 'reauth', 'refresh-token', 'reset-status', 'clear-rate-limit'])
|
||||||
const { t } = useI18n()
|
const { t } = useI18n()
|
||||||
const isRateLimited = computed(() => props.account?.rate_limit_reset_at && new Date(props.account.rate_limit_reset_at) > new Date())
|
const isRateLimited = computed(() => {
|
||||||
|
if (props.account?.rate_limit_reset_at && new Date(props.account.rate_limit_reset_at) > new Date()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
const modelLimits = (props.account?.extra as Record<string, unknown> | undefined)?.model_rate_limits as
|
||||||
|
| Record<string, { rate_limit_reset_at: string }>
|
||||||
|
| undefined
|
||||||
|
if (modelLimits) {
|
||||||
|
const now = new Date()
|
||||||
|
return Object.values(modelLimits).some(info => new Date(info.rate_limit_reset_at) > now)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
const isOverloaded = computed(() => props.account?.overload_until && new Date(props.account.overload_until) > new Date())
|
const isOverloaded = computed(() => props.account?.overload_until && new Date(props.account.overload_until) > new Date())
|
||||||
|
|
||||||
const handleKeydown = (event: KeyboardEvent) => {
|
const handleKeydown = (event: KeyboardEvent) => {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
/>
|
/>
|
||||||
<GroupBadge
|
<GroupBadge
|
||||||
:name="group.name"
|
:name="group.name"
|
||||||
|
:platform="group.platform"
|
||||||
:subscription-type="group.subscription_type"
|
:subscription-type="group.subscription_type"
|
||||||
:rate-multiplier="group.rate_multiplier"
|
:rate-multiplier="group.rate_multiplier"
|
||||||
class="min-w-0 flex-1"
|
class="min-w-0 flex-1"
|
||||||
|
|||||||
@@ -1309,10 +1309,23 @@ export default {
|
|||||||
syncResult: 'Sync Result',
|
syncResult: 'Sync Result',
|
||||||
syncResultSummary: 'Created {created}, updated {updated}, skipped {skipped}, failed {failed}',
|
syncResultSummary: 'Created {created}, updated {updated}, skipped {skipped}, failed {failed}',
|
||||||
syncErrors: 'Errors / Skipped Details',
|
syncErrors: 'Errors / Skipped Details',
|
||||||
syncCompleted: 'Sync completed: created {created}, updated {updated}',
|
syncCompleted: 'Sync completed: created {created}, updated {updated}, skipped {skipped}',
|
||||||
syncCompletedWithErrors:
|
syncCompletedWithErrors:
|
||||||
'Sync completed with errors: failed {failed} (created {created}, updated {updated})',
|
'Sync completed with errors: failed {failed} (created {created}, updated {updated}, skipped {skipped})',
|
||||||
syncFailed: 'Sync failed',
|
syncFailed: 'Sync failed',
|
||||||
|
crsPreview: 'Preview',
|
||||||
|
crsPreviewing: 'Previewing...',
|
||||||
|
crsPreviewFailed: 'Preview failed',
|
||||||
|
crsExistingAccounts: 'Existing accounts (will be updated)',
|
||||||
|
crsNewAccounts: 'New accounts (select to sync)',
|
||||||
|
crsSelectAll: 'Select all',
|
||||||
|
crsSelectNone: 'Select none',
|
||||||
|
crsNoNewAccounts: 'All CRS accounts are already synced.',
|
||||||
|
crsWillUpdate: 'Will update {count} existing accounts.',
|
||||||
|
crsSelectedCount: '{count} new accounts selected',
|
||||||
|
crsUpdateBehaviorNote:
|
||||||
|
'Existing accounts only sync fields returned by CRS; missing fields keep their current values. Credentials are merged by key — keys not returned by CRS are preserved. Proxies are kept when "Sync proxies" is unchecked.',
|
||||||
|
crsBack: 'Back',
|
||||||
editAccount: 'Edit Account',
|
editAccount: 'Edit Account',
|
||||||
deleteAccount: 'Delete Account',
|
deleteAccount: 'Delete Account',
|
||||||
searchAccounts: 'Search accounts...',
|
searchAccounts: 'Search accounts...',
|
||||||
@@ -1346,6 +1359,7 @@ export default {
|
|||||||
googleOauth: 'Google OAuth',
|
googleOauth: 'Google OAuth',
|
||||||
codeAssist: 'Code Assist',
|
codeAssist: 'Code Assist',
|
||||||
antigravityOauth: 'Antigravity OAuth',
|
antigravityOauth: 'Antigravity OAuth',
|
||||||
|
antigravityApikey: 'Connect via Base URL + API Key',
|
||||||
upstream: 'Upstream',
|
upstream: 'Upstream',
|
||||||
upstreamDesc: 'Connect via Base URL + API Key'
|
upstreamDesc: 'Connect via Base URL + API Key'
|
||||||
},
|
},
|
||||||
@@ -1612,7 +1626,7 @@ export default {
|
|||||||
// Upstream type
|
// Upstream type
|
||||||
upstream: {
|
upstream: {
|
||||||
baseUrl: 'Upstream Base URL',
|
baseUrl: 'Upstream Base URL',
|
||||||
baseUrlHint: 'The address of the upstream Antigravity service, e.g., https://s.konstants.xyz',
|
baseUrlHint: 'The address of the upstream Antigravity service, e.g., https://cloudcode-pa.googleapis.com',
|
||||||
apiKey: 'Upstream API Key',
|
apiKey: 'Upstream API Key',
|
||||||
apiKeyHint: 'API Key for the upstream service',
|
apiKeyHint: 'API Key for the upstream service',
|
||||||
pleaseEnterBaseUrl: 'Please enter upstream Base URL',
|
pleaseEnterBaseUrl: 'Please enter upstream Base URL',
|
||||||
|
|||||||
@@ -1397,9 +1397,22 @@ export default {
|
|||||||
syncResult: '同步结果',
|
syncResult: '同步结果',
|
||||||
syncResultSummary: '创建 {created},更新 {updated},跳过 {skipped},失败 {failed}',
|
syncResultSummary: '创建 {created},更新 {updated},跳过 {skipped},失败 {failed}',
|
||||||
syncErrors: '错误/跳过详情',
|
syncErrors: '错误/跳过详情',
|
||||||
syncCompleted: '同步完成:创建 {created},更新 {updated}',
|
syncCompleted: '同步完成:创建 {created},更新 {updated},跳过 {skipped}',
|
||||||
syncCompletedWithErrors: '同步完成但有错误:失败 {failed}(创建 {created},更新 {updated})',
|
syncCompletedWithErrors: '同步完成但有错误:失败 {failed}(创建 {created},更新 {updated},跳过 {skipped})',
|
||||||
syncFailed: '同步失败',
|
syncFailed: '同步失败',
|
||||||
|
crsPreview: '预览',
|
||||||
|
crsPreviewing: '预览中...',
|
||||||
|
crsPreviewFailed: '预览失败',
|
||||||
|
crsExistingAccounts: '将自动更新的已有账号',
|
||||||
|
crsNewAccounts: '新账号(可选择)',
|
||||||
|
crsSelectAll: '全选',
|
||||||
|
crsSelectNone: '全不选',
|
||||||
|
crsNoNewAccounts: '所有 CRS 账号均已同步。',
|
||||||
|
crsWillUpdate: '将更新 {count} 个已有账号。',
|
||||||
|
crsSelectedCount: '已选择 {count} 个新账号',
|
||||||
|
crsUpdateBehaviorNote:
|
||||||
|
'已有账号仅同步 CRS 返回的字段,缺失字段保持原值;凭据按键合并,不会清空未下发的键;未勾选"同步代理"时保留原有代理。',
|
||||||
|
crsBack: '返回',
|
||||||
editAccount: '编辑账号',
|
editAccount: '编辑账号',
|
||||||
deleteAccount: '删除账号',
|
deleteAccount: '删除账号',
|
||||||
deleteConfirmMessage: "确定要删除账号 '{name}' 吗?",
|
deleteConfirmMessage: "确定要删除账号 '{name}' 吗?",
|
||||||
@@ -1480,6 +1493,7 @@ export default {
|
|||||||
googleOauth: 'Google OAuth',
|
googleOauth: 'Google OAuth',
|
||||||
codeAssist: 'Code Assist',
|
codeAssist: 'Code Assist',
|
||||||
antigravityOauth: 'Antigravity OAuth',
|
antigravityOauth: 'Antigravity OAuth',
|
||||||
|
antigravityApikey: '通过 Base URL + API Key 连接',
|
||||||
upstream: '对接上游',
|
upstream: '对接上游',
|
||||||
upstreamDesc: '通过 Base URL + API Key 连接上游',
|
upstreamDesc: '通过 Base URL + API Key 连接上游',
|
||||||
api_key: 'API Key',
|
api_key: 'API Key',
|
||||||
@@ -1758,7 +1772,7 @@ export default {
|
|||||||
// Upstream type
|
// Upstream type
|
||||||
upstream: {
|
upstream: {
|
||||||
baseUrl: '上游 Base URL',
|
baseUrl: '上游 Base URL',
|
||||||
baseUrlHint: '上游 Antigravity 服务的地址,例如:https://s.konstants.xyz',
|
baseUrlHint: '上游 Antigravity 服务的地址,例如:https://cloudcode-pa.googleapis.com',
|
||||||
apiKey: '上游 API Key',
|
apiKey: '上游 API Key',
|
||||||
apiKeyHint: '上游服务的 API Key',
|
apiKeyHint: '上游服务的 API Key',
|
||||||
pleaseEnterBaseUrl: '请输入上游 Base URL',
|
pleaseEnterBaseUrl: '请输入上游 Base URL',
|
||||||
|
|||||||
Reference in New Issue
Block a user