diff --git a/.env.example b/.env.example index 72645404e..c7851385b 100644 --- a/.env.example +++ b/.env.example @@ -56,8 +56,6 @@ # SESSION_SECRET=random_string # 其他配置 -# 渠道测试频率(单位:秒) -# CHANNEL_TEST_FREQUENCY=10 # 生成默认token # GENERATE_DEFAULT_TOKEN=false # Cohere 安全设置 diff --git a/.github/workflows/pr-target-branch-check.yml b/.github/workflows/pr-target-branch-check.yml deleted file mode 100644 index e7bd4c817..000000000 --- a/.github/workflows/pr-target-branch-check.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Check PR Branching Strategy -on: - pull_request: - types: [opened, synchronize, reopened, edited] - -jobs: - check-branching-strategy: - runs-on: ubuntu-latest - steps: - - name: Enforce branching strategy - run: | - if [[ "${{ github.base_ref }}" == "main" ]]; then - if [[ "${{ github.head_ref }}" != "alpha" ]]; then - echo "Error: Pull requests to 'main' are only allowed from the 'alpha' branch." - exit 1 - fi - elif [[ "${{ github.base_ref }}" != "alpha" ]]; then - echo "Error: Pull requests must be targeted to the 'alpha' or 'main' branch." - exit 1 - fi - echo "Branching strategy check passed." \ No newline at end of file diff --git a/README.md b/README.md index 45b048340..d68b3e135 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,11 @@ New API提供了丰富的功能,详细特性请参考[特性说明](https://do - 添加后缀 `-thinking` 启用思考模式 (例如: `claude-3-7-sonnet-20250219-thinking`) 16. 🔄 思考转内容功能 17. 🔄 针对用户的模型限流功能 -18. 💰 缓存计费支持,开启后可以在缓存命中时按照设定的比例计费: +18. 🔄 请求格式转换功能,支持以下三种格式转换: + 1. OpenAI Chat Completions => Claude Messages + 2. Clade Messages => OpenAI Chat Completions (可用于Claude Code调用第三方模型) + 3. OpenAI Chat Completions => Gemini Chat +19. 💰 缓存计费支持,开启后可以在缓存命中时按照设定的比例计费: 1. 在 `系统设置-运营设置` 中设置 `提示缓存倍率` 选项 2. 在渠道中设置 `提示缓存倍率`,范围 0-1,例如设置为 0.5 表示缓存命中时按照 50% 计费 3. 支持的渠道: diff --git a/common/database.go b/common/database.go index 71dbd94d5..38a54d5e6 100644 --- a/common/database.go +++ b/common/database.go @@ -12,4 +12,4 @@ var LogSqlType = DatabaseTypeSQLite // Default to SQLite for logging SQL queries var UsingMySQL = false var UsingClickHouse = false -var SQLitePath = "one-api.db?_busy_timeout=30000" +var SQLitePath = "one-api.db?_busy_timeout=30000" \ No newline at end of file diff --git a/common/utils.go b/common/utils.go index 17aecd950..883abfd1a 100644 --- a/common/utils.go +++ b/common/utils.go @@ -123,8 +123,16 @@ func Interface2String(inter interface{}) string { return fmt.Sprintf("%d", inter.(int)) case float64: return fmt.Sprintf("%f", inter.(float64)) + case bool: + if inter.(bool) { + return "true" + } else { + return "false" + } + case nil: + return "" } - return "Not Implemented" + return fmt.Sprintf("%v", inter) } func UnescapeHTML(x string) interface{} { @@ -257,32 +265,32 @@ func GetAudioDuration(ctx context.Context, filename string, ext string) (float64 if err != nil { return 0, errors.Wrap(err, "failed to get audio duration") } - durationStr := string(bytes.TrimSpace(output)) - if durationStr == "N/A" { - // Create a temporary output file name - tmpFp, err := os.CreateTemp("", "audio-*"+ext) - if err != nil { - return 0, errors.Wrap(err, "failed to create temporary file") - } - tmpName := tmpFp.Name() - // Close immediately so ffmpeg can open the file on Windows. - _ = tmpFp.Close() - defer os.Remove(tmpName) + durationStr := string(bytes.TrimSpace(output)) + if durationStr == "N/A" { + // Create a temporary output file name + tmpFp, err := os.CreateTemp("", "audio-*"+ext) + if err != nil { + return 0, errors.Wrap(err, "failed to create temporary file") + } + tmpName := tmpFp.Name() + // Close immediately so ffmpeg can open the file on Windows. + _ = tmpFp.Close() + defer os.Remove(tmpName) - // ffmpeg -y -i filename -vcodec copy -acodec copy - ffmpegCmd := exec.CommandContext(ctx, "ffmpeg", "-y", "-i", filename, "-vcodec", "copy", "-acodec", "copy", tmpName) - if err := ffmpegCmd.Run(); err != nil { - return 0, errors.Wrap(err, "failed to run ffmpeg") - } + // ffmpeg -y -i filename -vcodec copy -acodec copy + ffmpegCmd := exec.CommandContext(ctx, "ffmpeg", "-y", "-i", filename, "-vcodec", "copy", "-acodec", "copy", tmpName) + if err := ffmpegCmd.Run(); err != nil { + return 0, errors.Wrap(err, "failed to run ffmpeg") + } - // Recalculate the duration of the new file - c = exec.CommandContext(ctx, "ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", tmpName) - output, err := c.Output() - if err != nil { - return 0, errors.Wrap(err, "failed to get audio duration after ffmpeg") - } - durationStr = string(bytes.TrimSpace(output)) - } + // Recalculate the duration of the new file + c = exec.CommandContext(ctx, "ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", tmpName) + output, err := c.Output() + if err != nil { + return 0, errors.Wrap(err, "failed to get audio duration after ffmpeg") + } + durationStr = string(bytes.TrimSpace(output)) + } return strconv.ParseFloat(durationStr, 64) } diff --git a/controller/channel-billing.go b/controller/channel-billing.go index 18acf2319..1082b9e73 100644 --- a/controller/channel-billing.go +++ b/controller/channel-billing.go @@ -10,7 +10,7 @@ import ( "one-api/constant" "one-api/model" "one-api/service" - "one-api/setting" + "one-api/setting/operation_setting" "one-api/types" "strconv" "time" @@ -342,7 +342,7 @@ func updateChannelMoonshotBalance(channel *model.Channel) (float64, error) { return 0, fmt.Errorf("failed to update moonshot balance, status: %v, code: %d, scode: %s", response.Status, response.Code, response.Scode) } availableBalanceCny := response.Data.AvailableBalance - availableBalanceUsd := decimal.NewFromFloat(availableBalanceCny).Div(decimal.NewFromFloat(setting.Price)).InexactFloat64() + availableBalanceUsd := decimal.NewFromFloat(availableBalanceCny).Div(decimal.NewFromFloat(operation_setting.Price)).InexactFloat64() channel.UpdateBalance(availableBalanceUsd) return availableBalanceUsd, nil } diff --git a/controller/channel-test.go b/controller/channel-test.go index 81f7e19ab..5a668c488 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -20,6 +20,7 @@ import ( relayconstant "one-api/relay/constant" "one-api/relay/helper" "one-api/service" + "one-api/setting/operation_setting" "one-api/types" "strconv" "strings" @@ -234,7 +235,7 @@ func testChannel(channel *model.Channel, testModel string) testResult { if resp != nil { httpResp = resp.(*http.Response) if httpResp.StatusCode != http.StatusOK { - err := service.RelayErrorHandler(httpResp, true) + err := service.RelayErrorHandler(c.Request.Context(), httpResp, true) return testResult{ context: c, localErr: err, @@ -477,15 +478,26 @@ func TestAllChannels(c *gin.Context) { return } -func AutomaticallyTestChannels(frequency int) { - if frequency <= 0 { - common.SysLog("CHANNEL_TEST_FREQUENCY is not set or invalid, skipping automatic channel test") - return - } - for { - time.Sleep(time.Duration(frequency) * time.Minute) - common.SysLog("testing all channels") - _ = testAllChannels(false) - common.SysLog("channel test finished") - } +var autoTestChannelsOnce sync.Once + +func AutomaticallyTestChannels() { + autoTestChannelsOnce.Do(func() { + for { + if !operation_setting.GetMonitorSetting().AutoTestChannelEnabled { + time.Sleep(10 * time.Minute) + continue + } + frequency := operation_setting.GetMonitorSetting().AutoTestChannelMinutes + common.SysLog(fmt.Sprintf("automatically test channels with interval %d minutes", frequency)) + for { + time.Sleep(time.Duration(frequency) * time.Minute) + common.SysLog("automatically testing all channels") + _ = testAllChannels(false) + common.SysLog("automatically channel test finished") + if !operation_setting.GetMonitorSetting().AutoTestChannelEnabled { + break + } + } + } + }) } diff --git a/controller/channel.go b/controller/channel.go index 70be91d42..403eb04cc 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -6,6 +6,7 @@ import ( "net/http" "one-api/common" "one-api/constant" + "one-api/dto" "one-api/model" "strconv" "strings" @@ -560,7 +561,7 @@ func AddChannel(c *gin.Context) { case "multi_to_single": addChannelRequest.Channel.ChannelInfo.IsMultiKey = true addChannelRequest.Channel.ChannelInfo.MultiKeyMode = addChannelRequest.MultiKeyMode - if addChannelRequest.Channel.Type == constant.ChannelTypeVertexAi { + if addChannelRequest.Channel.Type == constant.ChannelTypeVertexAi && addChannelRequest.Channel.GetOtherSettings().VertexKeyType != dto.VertexKeyTypeAPIKey { array, err := getVertexArrayKeys(addChannelRequest.Channel.Key) if err != nil { c.JSON(http.StatusOK, gin.H{ @@ -585,7 +586,7 @@ func AddChannel(c *gin.Context) { } keys = []string{addChannelRequest.Channel.Key} case "batch": - if addChannelRequest.Channel.Type == constant.ChannelTypeVertexAi { + if addChannelRequest.Channel.Type == constant.ChannelTypeVertexAi && addChannelRequest.Channel.GetOtherSettings().VertexKeyType != dto.VertexKeyTypeAPIKey { // multi json keys, err = getVertexArrayKeys(addChannelRequest.Channel.Key) if err != nil { @@ -840,7 +841,7 @@ func UpdateChannel(c *gin.Context) { } // 处理 Vertex AI 的特殊情况 - if channel.Type == constant.ChannelTypeVertexAi { + if channel.Type == constant.ChannelTypeVertexAi && channel.GetOtherSettings().VertexKeyType != dto.VertexKeyTypeAPIKey { // 尝试解析新密钥为JSON数组 if strings.HasPrefix(strings.TrimSpace(channel.Key), "[") { array, err := getVertexArrayKeys(channel.Key) diff --git a/controller/midjourney.go b/controller/midjourney.go index a67d39c23..3a7304419 100644 --- a/controller/midjourney.go +++ b/controller/midjourney.go @@ -13,6 +13,7 @@ import ( "one-api/model" "one-api/service" "one-api/setting" + "one-api/setting/system_setting" "time" "github.com/gin-gonic/gin" @@ -259,7 +260,7 @@ func GetAllMidjourney(c *gin.Context) { if setting.MjForwardUrlEnabled { for i, midjourney := range items { - midjourney.ImageUrl = setting.ServerAddress + "/mj/image/" + midjourney.MjId + midjourney.ImageUrl = system_setting.ServerAddress + "/mj/image/" + midjourney.MjId items[i] = midjourney } } @@ -284,7 +285,7 @@ func GetUserMidjourney(c *gin.Context) { if setting.MjForwardUrlEnabled { for i, midjourney := range items { - midjourney.ImageUrl = setting.ServerAddress + "/mj/image/" + midjourney.MjId + midjourney.ImageUrl = system_setting.ServerAddress + "/mj/image/" + midjourney.MjId items[i] = midjourney } } diff --git a/controller/misc.go b/controller/misc.go index f30ab8c79..875142ffb 100644 --- a/controller/misc.go +++ b/controller/misc.go @@ -39,6 +39,8 @@ func TestStatus(c *gin.Context) { func GetStatus(c *gin.Context) { cs := console_setting.GetConsoleSetting() + common.OptionMapRWMutex.RLock() + defer common.OptionMapRWMutex.RUnlock() data := gin.H{ "version": common.Version, @@ -56,11 +58,7 @@ func GetStatus(c *gin.Context) { "footer_html": common.Footer, "wechat_qrcode": common.WeChatAccountQRCodeImageURL, "wechat_login": common.WeChatAuthEnabled, - "server_address": setting.ServerAddress, - "price": setting.Price, - "stripe_unit_price": setting.StripeUnitPrice, - "min_topup": setting.MinTopUp, - "stripe_min_topup": setting.StripeMinTopUp, + "server_address": system_setting.ServerAddress, "turnstile_check": common.TurnstileCheckEnabled, "turnstile_site_key": common.TurnstileSiteKey, "top_up_link": common.TopUpLink, @@ -73,15 +71,15 @@ func GetStatus(c *gin.Context) { "enable_data_export": common.DataExportEnabled, "data_export_default_time": common.DataExportDefaultTime, "default_collapse_sidebar": common.DefaultCollapseSidebar, - "enable_online_topup": setting.PayAddress != "" && setting.EpayId != "" && setting.EpayKey != "", - "enable_stripe_topup": setting.StripeApiSecret != "" && setting.StripeWebhookSecret != "" && setting.StripePriceId != "", "mj_notify_enabled": setting.MjNotifyEnabled, "chats": setting.Chats, "demo_site_enabled": operation_setting.DemoSiteEnabled, "self_use_mode_enabled": operation_setting.SelfUseModeEnabled, "default_use_auto_group": setting.DefaultUseAutoGroup, - "pay_methods": setting.PayMethods, - "usd_exchange_rate": setting.USDExchangeRate, + + "usd_exchange_rate": operation_setting.USDExchangeRate, + "price": operation_setting.Price, + "stripe_unit_price": setting.StripeUnitPrice, // 面板启用开关 "api_info_enabled": cs.ApiInfoEnabled, @@ -89,6 +87,10 @@ func GetStatus(c *gin.Context) { "announcements_enabled": cs.AnnouncementsEnabled, "faq_enabled": cs.FAQEnabled, + // 模块管理配置 + "HeaderNavModules": common.OptionMap["HeaderNavModules"], + "SidebarModulesAdmin": common.OptionMap["SidebarModulesAdmin"], + "oidc_enabled": system_setting.GetOIDCSettings().Enabled, "oidc_client_id": system_setting.GetOIDCSettings().ClientId, "oidc_authorization_endpoint": system_setting.GetOIDCSettings().AuthorizationEndpoint, @@ -247,7 +249,7 @@ func SendPasswordResetEmail(c *gin.Context) { } code := common.GenerateVerificationCode(0) common.RegisterVerificationCodeWithKey(email, code, common.PasswordResetPurpose) - link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", setting.ServerAddress, email, code) + link := fmt.Sprintf("%s/user/reset?email=%s&token=%s", system_setting.ServerAddress, email, code) subject := fmt.Sprintf("%s密码重置", common.SystemName) content := fmt.Sprintf("

您好,你正在进行%s密码重置。

"+ "

点击 此处 进行密码重置。

"+ diff --git a/controller/model.go b/controller/model.go index 398503e8d..f0571b995 100644 --- a/controller/model.go +++ b/controller/model.go @@ -207,6 +207,7 @@ func ListModels(c *gin.Context, modelType int) { c.JSON(200, gin.H{ "success": true, "data": userOpenAiModels, + "object": "list", }) } } diff --git a/controller/model_sync.go b/controller/model_sync.go new file mode 100644 index 000000000..74034b51a --- /dev/null +++ b/controller/model_sync.go @@ -0,0 +1,604 @@ +package controller + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "math/rand" + "net" + "net/http" + "strings" + "sync" + "time" + + "one-api/common" + "one-api/model" + + "github.com/gin-gonic/gin" + "gorm.io/gorm" +) + +// 上游地址 +const ( + upstreamModelsURL = "https://basellm.github.io/llm-metadata/api/newapi/models.json" + upstreamVendorsURL = "https://basellm.github.io/llm-metadata/api/newapi/vendors.json" +) + +func normalizeLocale(locale string) (string, bool) { + l := strings.ToLower(strings.TrimSpace(locale)) + switch l { + case "en", "zh", "ja": + return l, true + default: + return "", false + } +} + +func getUpstreamBase() string { + return common.GetEnvOrDefaultString("SYNC_UPSTREAM_BASE", "https://basellm.github.io/llm-metadata") +} + +func getUpstreamURLs(locale string) (modelsURL, vendorsURL string) { + base := strings.TrimRight(getUpstreamBase(), "/") + if l, ok := normalizeLocale(locale); ok && l != "" { + return fmt.Sprintf("%s/api/i18n/%s/newapi/models.json", base, l), + fmt.Sprintf("%s/api/i18n/%s/newapi/vendors.json", base, l) + } + return fmt.Sprintf("%s/api/newapi/models.json", base), fmt.Sprintf("%s/api/newapi/vendors.json", base) +} + +type upstreamEnvelope[T any] struct { + Success bool `json:"success"` + Message string `json:"message"` + Data []T `json:"data"` +} + +type upstreamModel struct { + Description string `json:"description"` + Endpoints json.RawMessage `json:"endpoints"` + Icon string `json:"icon"` + ModelName string `json:"model_name"` + NameRule int `json:"name_rule"` + Status int `json:"status"` + Tags string `json:"tags"` + VendorName string `json:"vendor_name"` +} + +type upstreamVendor struct { + Description string `json:"description"` + Icon string `json:"icon"` + Name string `json:"name"` + Status int `json:"status"` +} + +var ( + etagCache = make(map[string]string) + bodyCache = make(map[string][]byte) + cacheMutex sync.RWMutex +) + +type overwriteField struct { + ModelName string `json:"model_name"` + Fields []string `json:"fields"` +} + +type syncRequest struct { + Overwrite []overwriteField `json:"overwrite"` + Locale string `json:"locale"` +} + +func newHTTPClient() *http.Client { + timeoutSec := common.GetEnvOrDefault("SYNC_HTTP_TIMEOUT_SECONDS", 10) + dialer := &net.Dialer{Timeout: time.Duration(timeoutSec) * time.Second} + transport := &http.Transport{ + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: time.Duration(timeoutSec) * time.Second, + ExpectContinueTimeout: 1 * time.Second, + ResponseHeaderTimeout: time.Duration(timeoutSec) * time.Second, + } + transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { + host, _, err := net.SplitHostPort(addr) + if err != nil { + host = addr + } + if strings.HasSuffix(host, "github.io") { + if conn, err := dialer.DialContext(ctx, "tcp4", addr); err == nil { + return conn, nil + } + return dialer.DialContext(ctx, "tcp6", addr) + } + return dialer.DialContext(ctx, network, addr) + } + return &http.Client{Transport: transport} +} + +var httpClient = newHTTPClient() + +func fetchJSON[T any](ctx context.Context, url string, out *upstreamEnvelope[T]) error { + var lastErr error + attempts := common.GetEnvOrDefault("SYNC_HTTP_RETRY", 3) + if attempts < 1 { + attempts = 1 + } + baseDelay := 200 * time.Millisecond + maxMB := common.GetEnvOrDefault("SYNC_HTTP_MAX_MB", 10) + maxBytes := int64(maxMB) << 20 + for attempt := 0; attempt < attempts; attempt++ { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return err + } + // ETag conditional request + cacheMutex.RLock() + if et := etagCache[url]; et != "" { + req.Header.Set("If-None-Match", et) + } + cacheMutex.RUnlock() + + resp, err := httpClient.Do(req) + if err != nil { + lastErr = err + // backoff with jitter + sleep := baseDelay * time.Duration(1< id + vendorIDCache := make(map[string]int) + + for _, name := range missing { + up, ok := modelByName[name] + if !ok { + skipped = append(skipped, name) + continue + } + + // 若本地已存在且设置为不同步,则跳过(极端情况:缺失列表与本地状态不同步时) + var existing model.Model + if err := model.DB.Where("model_name = ?", name).First(&existing).Error; err == nil { + if existing.SyncOfficial == 0 { + skipped = append(skipped, name) + continue + } + } + + // 确保 vendor 存在 + vendorID := ensureVendorID(up.VendorName, vendorByName, vendorIDCache, &createdVendors) + + // 创建模型 + mi := &model.Model{ + ModelName: name, + Description: up.Description, + Icon: up.Icon, + Tags: up.Tags, + VendorID: vendorID, + Status: chooseStatus(up.Status, 1), + NameRule: up.NameRule, + } + if err := mi.Insert(); err == nil { + createdModels++ + createdList = append(createdList, name) + } else { + skipped = append(skipped, name) + } + } + + // 4) 处理可选覆盖(更新本地已有模型的差异字段) + if len(req.Overwrite) > 0 { + // vendorIDCache 已用于创建阶段,可复用 + for _, ow := range req.Overwrite { + up, ok := modelByName[ow.ModelName] + if !ok { + continue + } + var local model.Model + if err := model.DB.Where("model_name = ?", ow.ModelName).First(&local).Error; err != nil { + continue + } + + // 跳过被禁用官方同步的模型 + if local.SyncOfficial == 0 { + continue + } + + // 映射 vendor + newVendorID := ensureVendorID(up.VendorName, vendorByName, vendorIDCache, &createdVendors) + + // 应用字段覆盖(事务) + _ = model.DB.Transaction(func(tx *gorm.DB) error { + needUpdate := false + if containsField(ow.Fields, "description") { + local.Description = up.Description + needUpdate = true + } + if containsField(ow.Fields, "icon") { + local.Icon = up.Icon + needUpdate = true + } + if containsField(ow.Fields, "tags") { + local.Tags = up.Tags + needUpdate = true + } + if containsField(ow.Fields, "vendor") { + local.VendorID = newVendorID + needUpdate = true + } + if containsField(ow.Fields, "name_rule") { + local.NameRule = up.NameRule + needUpdate = true + } + if containsField(ow.Fields, "status") { + local.Status = chooseStatus(up.Status, local.Status) + needUpdate = true + } + if !needUpdate { + return nil + } + if err := tx.Save(&local).Error; err != nil { + return err + } + updatedModels++ + updatedList = append(updatedList, ow.ModelName) + return nil + }) + } + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "data": gin.H{ + "created_models": createdModels, + "created_vendors": createdVendors, + "updated_models": updatedModels, + "skipped_models": skipped, + "created_list": createdList, + "updated_list": updatedList, + "source": gin.H{ + "locale": req.Locale, + "models_url": modelsURL, + "vendors_url": vendorsURL, + }, + }, + }) +} + +func containsField(fields []string, key string) bool { + key = strings.ToLower(strings.TrimSpace(key)) + for _, f := range fields { + if strings.ToLower(strings.TrimSpace(f)) == key { + return true + } + } + return false +} + +func coalesce(a, b string) string { + if strings.TrimSpace(a) != "" { + return a + } + return b +} + +func chooseStatus(primary, fallback int) int { + if primary == 0 && fallback != 0 { + return fallback + } + if primary != 0 { + return primary + } + return 1 +} + +// SyncUpstreamPreview 预览上游与本地的差异(仅用于弹窗选择) +func SyncUpstreamPreview(c *gin.Context) { + // 1) 拉取上游数据 + timeoutSec := common.GetEnvOrDefault("SYNC_HTTP_TIMEOUT_SECONDS", 15) + ctx, cancel := context.WithTimeout(c.Request.Context(), time.Duration(timeoutSec)*time.Second) + defer cancel() + + locale := c.Query("locale") + modelsURL, vendorsURL := getUpstreamURLs(locale) + + var vendorsEnv upstreamEnvelope[upstreamVendor] + var modelsEnv upstreamEnvelope[upstreamModel] + var fetchErr error + var wg sync.WaitGroup + wg.Add(2) + go func() { + defer wg.Done() + _ = fetchJSON(ctx, vendorsURL, &vendorsEnv) + }() + go func() { + defer wg.Done() + if err := fetchJSON(ctx, modelsURL, &modelsEnv); err != nil { + fetchErr = err + } + }() + wg.Wait() + if fetchErr != nil { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "获取上游模型失败: " + fetchErr.Error(), "locale": locale, "source_urls": gin.H{"models_url": modelsURL, "vendors_url": vendorsURL}}) + return + } + + vendorByName := make(map[string]upstreamVendor) + for _, v := range vendorsEnv.Data { + if v.Name != "" { + vendorByName[v.Name] = v + } + } + modelByName := make(map[string]upstreamModel) + upstreamNames := make([]string, 0, len(modelsEnv.Data)) + for _, m := range modelsEnv.Data { + if m.ModelName != "" { + modelByName[m.ModelName] = m + upstreamNames = append(upstreamNames, m.ModelName) + } + } + + // 2) 本地已有模型 + var locals []model.Model + if len(upstreamNames) > 0 { + _ = model.DB.Where("model_name IN ? AND sync_official <> 0", upstreamNames).Find(&locals).Error + } + + // 本地 vendor 名称映射 + vendorIdSet := make(map[int]struct{}) + for _, m := range locals { + if m.VendorID != 0 { + vendorIdSet[m.VendorID] = struct{}{} + } + } + vendorIDs := make([]int, 0, len(vendorIdSet)) + for id := range vendorIdSet { + vendorIDs = append(vendorIDs, id) + } + idToVendorName := make(map[int]string) + if len(vendorIDs) > 0 { + var dbVendors []model.Vendor + _ = model.DB.Where("id IN ?", vendorIDs).Find(&dbVendors).Error + for _, v := range dbVendors { + idToVendorName[v.Id] = v.Name + } + } + + // 3) 缺失且上游存在的模型 + missingList, _ := model.GetMissingModels() + var missing []string + for _, name := range missingList { + if _, ok := modelByName[name]; ok { + missing = append(missing, name) + } + } + + // 4) 计算冲突字段 + type conflictField struct { + Field string `json:"field"` + Local interface{} `json:"local"` + Upstream interface{} `json:"upstream"` + } + type conflictItem struct { + ModelName string `json:"model_name"` + Fields []conflictField `json:"fields"` + } + + var conflicts []conflictItem + for _, local := range locals { + up, ok := modelByName[local.ModelName] + if !ok { + continue + } + fields := make([]conflictField, 0, 6) + if strings.TrimSpace(local.Description) != strings.TrimSpace(up.Description) { + fields = append(fields, conflictField{Field: "description", Local: local.Description, Upstream: up.Description}) + } + if strings.TrimSpace(local.Icon) != strings.TrimSpace(up.Icon) { + fields = append(fields, conflictField{Field: "icon", Local: local.Icon, Upstream: up.Icon}) + } + if strings.TrimSpace(local.Tags) != strings.TrimSpace(up.Tags) { + fields = append(fields, conflictField{Field: "tags", Local: local.Tags, Upstream: up.Tags}) + } + // vendor 对比使用名称 + localVendor := idToVendorName[local.VendorID] + if strings.TrimSpace(localVendor) != strings.TrimSpace(up.VendorName) { + fields = append(fields, conflictField{Field: "vendor", Local: localVendor, Upstream: up.VendorName}) + } + if local.NameRule != up.NameRule { + fields = append(fields, conflictField{Field: "name_rule", Local: local.NameRule, Upstream: up.NameRule}) + } + if local.Status != chooseStatus(up.Status, local.Status) { + fields = append(fields, conflictField{Field: "status", Local: local.Status, Upstream: up.Status}) + } + if len(fields) > 0 { + conflicts = append(conflicts, conflictItem{ModelName: local.ModelName, Fields: fields}) + } + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "data": gin.H{ + "missing": missing, + "conflicts": conflicts, + "source": gin.H{ + "locale": locale, + "models_url": modelsURL, + "vendors_url": vendorsURL, + }, + }, + }) +} diff --git a/controller/oidc.go b/controller/oidc.go index f3def0e34..8e254d38f 100644 --- a/controller/oidc.go +++ b/controller/oidc.go @@ -8,7 +8,6 @@ import ( "net/url" "one-api/common" "one-api/model" - "one-api/setting" "one-api/setting/system_setting" "strconv" "strings" @@ -45,7 +44,7 @@ func getOidcUserInfoByCode(code string) (*OidcUser, error) { values.Set("client_secret", system_setting.GetOIDCSettings().ClientSecret) values.Set("code", code) values.Set("grant_type", "authorization_code") - values.Set("redirect_uri", fmt.Sprintf("%s/oauth/oidc", setting.ServerAddress)) + values.Set("redirect_uri", fmt.Sprintf("%s/oauth/oidc", system_setting.ServerAddress)) formData := values.Encode() req, err := http.NewRequest("POST", system_setting.GetOIDCSettings().TokenEndpoint, strings.NewReader(formData)) if err != nil { diff --git a/controller/option.go b/controller/option.go index fb54d20a0..3e59c68e0 100644 --- a/controller/option.go +++ b/controller/option.go @@ -2,6 +2,7 @@ package controller import ( "encoding/json" + "fmt" "net/http" "one-api/common" "one-api/model" @@ -35,8 +36,13 @@ func GetOptions(c *gin.Context) { return } +type OptionUpdateRequest struct { + Key string `json:"key"` + Value any `json:"value"` +} + func UpdateOption(c *gin.Context) { - var option model.Option + var option OptionUpdateRequest err := json.NewDecoder(c.Request.Body).Decode(&option) if err != nil { c.JSON(http.StatusBadRequest, gin.H{ @@ -45,6 +51,16 @@ func UpdateOption(c *gin.Context) { }) return } + switch option.Value.(type) { + case bool: + option.Value = common.Interface2String(option.Value.(bool)) + case float64: + option.Value = common.Interface2String(option.Value.(float64)) + case int: + option.Value = common.Interface2String(option.Value.(int)) + default: + option.Value = fmt.Sprintf("%v", option.Value) + } switch option.Key { case "GitHubOAuthEnabled": if option.Value == "true" && common.GitHubClientId == "" { @@ -104,7 +120,7 @@ func UpdateOption(c *gin.Context) { return } case "GroupRatio": - err = ratio_setting.CheckGroupRatio(option.Value) + err = ratio_setting.CheckGroupRatio(option.Value.(string)) if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, @@ -140,7 +156,7 @@ func UpdateOption(c *gin.Context) { return } case "ModelRequestRateLimitGroup": - err = setting.CheckModelRequestRateLimitGroup(option.Value) + err = setting.CheckModelRequestRateLimitGroup(option.Value.(string)) if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, @@ -149,7 +165,7 @@ func UpdateOption(c *gin.Context) { return } case "console_setting.api_info": - err = console_setting.ValidateConsoleSettings(option.Value, "ApiInfo") + err = console_setting.ValidateConsoleSettings(option.Value.(string), "ApiInfo") if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, @@ -158,7 +174,7 @@ func UpdateOption(c *gin.Context) { return } case "console_setting.announcements": - err = console_setting.ValidateConsoleSettings(option.Value, "Announcements") + err = console_setting.ValidateConsoleSettings(option.Value.(string), "Announcements") if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, @@ -167,7 +183,7 @@ func UpdateOption(c *gin.Context) { return } case "console_setting.faq": - err = console_setting.ValidateConsoleSettings(option.Value, "FAQ") + err = console_setting.ValidateConsoleSettings(option.Value.(string), "FAQ") if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, @@ -176,7 +192,7 @@ func UpdateOption(c *gin.Context) { return } case "console_setting.uptime_kuma_groups": - err = console_setting.ValidateConsoleSettings(option.Value, "UptimeKumaGroups") + err = console_setting.ValidateConsoleSettings(option.Value.(string), "UptimeKumaGroups") if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, @@ -185,7 +201,7 @@ func UpdateOption(c *gin.Context) { return } } - err = model.UpdateOption(option.Key, option.Value) + err = model.UpdateOption(option.Key, option.Value.(string)) if err != nil { common.ApiError(c, err) return diff --git a/controller/ratio_config.go b/controller/ratio_config.go index 6ddc3d9ef..0cb4aa73b 100644 --- a/controller/ratio_config.go +++ b/controller/ratio_config.go @@ -1,24 +1,24 @@ package controller import ( - "net/http" - "one-api/setting/ratio_setting" + "net/http" + "one-api/setting/ratio_setting" - "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin" ) func GetRatioConfig(c *gin.Context) { - if !ratio_setting.IsExposeRatioEnabled() { - c.JSON(http.StatusForbidden, gin.H{ - "success": false, - "message": "倍率配置接口未启用", - }) - return - } + if !ratio_setting.IsExposeRatioEnabled() { + c.JSON(http.StatusForbidden, gin.H{ + "success": false, + "message": "倍率配置接口未启用", + }) + return + } - c.JSON(http.StatusOK, gin.H{ - "success": true, - "message": "", - "data": ratio_setting.GetExposedData(), - }) -} \ No newline at end of file + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "", + "data": ratio_setting.GetExposedData(), + }) +} diff --git a/controller/ratio_sync.go b/controller/ratio_sync.go index 6fba0aac3..7a481c476 100644 --- a/controller/ratio_sync.go +++ b/controller/ratio_sync.go @@ -4,6 +4,8 @@ import ( "context" "encoding/json" "fmt" + "io" + "net" "net/http" "one-api/logger" "strings" @@ -21,8 +23,26 @@ const ( defaultTimeoutSeconds = 10 defaultEndpoint = "/api/ratio_config" maxConcurrentFetches = 8 + maxRatioConfigBytes = 10 << 20 // 10MB + floatEpsilon = 1e-9 ) +func nearlyEqual(a, b float64) bool { + if a > b { + return a-b < floatEpsilon + } + return b-a < floatEpsilon +} + +func valuesEqual(a, b interface{}) bool { + af, aok := a.(float64) + bf, bok := b.(float64) + if aok && bok { + return nearlyEqual(af, bf) + } + return a == b +} + var ratioTypes = []string{"model_ratio", "completion_ratio", "cache_ratio", "model_price"} type upstreamResult struct { @@ -87,7 +107,23 @@ func FetchUpstreamRatios(c *gin.Context) { sem := make(chan struct{}, maxConcurrentFetches) - client := &http.Client{Transport: &http.Transport{MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second}} + dialer := &net.Dialer{Timeout: 10 * time.Second} + transport := &http.Transport{MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, ResponseHeaderTimeout: 10 * time.Second} + transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { + host, _, err := net.SplitHostPort(addr) + if err != nil { + host = addr + } + // 对 github.io 优先尝试 IPv4,失败则回退 IPv6 + if strings.HasSuffix(host, "github.io") { + if conn, err := dialer.DialContext(ctx, "tcp4", addr); err == nil { + return conn, nil + } + return dialer.DialContext(ctx, "tcp6", addr) + } + return dialer.DialContext(ctx, network, addr) + } + client := &http.Client{Transport: transport} for _, chn := range upstreams { wg.Add(1) @@ -98,12 +134,17 @@ func FetchUpstreamRatios(c *gin.Context) { defer func() { <-sem }() endpoint := chItem.Endpoint - if endpoint == "" { - endpoint = defaultEndpoint - } else if !strings.HasPrefix(endpoint, "/") { - endpoint = "/" + endpoint + var fullURL string + if strings.HasPrefix(endpoint, "http://") || strings.HasPrefix(endpoint, "https://") { + fullURL = endpoint + } else { + if endpoint == "" { + endpoint = defaultEndpoint + } else if !strings.HasPrefix(endpoint, "/") { + endpoint = "/" + endpoint + } + fullURL = chItem.BaseURL + endpoint } - fullURL := chItem.BaseURL + endpoint uniqueName := chItem.Name if chItem.ID != 0 { @@ -120,10 +161,19 @@ func FetchUpstreamRatios(c *gin.Context) { return } - resp, err := client.Do(httpReq) - if err != nil { - logger.LogWarn(c.Request.Context(), "http error on "+chItem.Name+": "+err.Error()) - ch <- upstreamResult{Name: uniqueName, Err: err.Error()} + // 简单重试:最多 3 次,指数退避 + var resp *http.Response + var lastErr error + for attempt := 0; attempt < 3; attempt++ { + resp, lastErr = client.Do(httpReq) + if lastErr == nil { + break + } + time.Sleep(time.Duration(200*(1< data 为 map[string]any,包含 model_ratio/completion_ratio/cache_ratio/model_price // type2: /api/pricing -> data 为 []Pricing 列表,需要转换为与 type1 相同的 map 格式 @@ -141,7 +197,7 @@ func FetchUpstreamRatios(c *gin.Context) { Message string `json:"message"` } - if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { + if err := json.NewDecoder(limited).Decode(&body); err != nil { logger.LogWarn(c.Request.Context(), "json decode failed from "+chItem.Name+": "+err.Error()) ch <- upstreamResult{Name: uniqueName, Err: err.Error()} return @@ -152,6 +208,8 @@ func FetchUpstreamRatios(c *gin.Context) { return } + // 若 Data 为空,将继续按 type1 尝试解析(与多数静态 ratio_config 兼容) + // 尝试按 type1 解析 var type1Data map[string]any if err := json.Unmarshal(body.Data, &type1Data); err == nil { @@ -357,9 +415,9 @@ func buildDifferences(localData map[string]any, successfulChannels []struct { upstreamValue = val hasUpstreamValue = true - if localValue != nil && localValue != val { + if localValue != nil && !valuesEqual(localValue, val) { hasDifference = true - } else if localValue == val { + } else if valuesEqual(localValue, val) { upstreamValue = "same" } } @@ -466,6 +524,13 @@ func GetSyncableChannels(c *gin.Context) { } } + syncableChannels = append(syncableChannels, dto.SyncableChannel{ + ID: -100, + Name: "官方倍率预设", + BaseURL: "https://basellm.github.io", + Status: 1, + }) + c.JSON(http.StatusOK, gin.H{ "success": true, "message": "", diff --git a/controller/relay.go b/controller/relay.go index d3d93192e..23d725153 100644 --- a/controller/relay.go +++ b/controller/relay.go @@ -139,15 +139,15 @@ func Relay(c *gin.Context, relayFormat types.RelayFormat) { // common.SetContextKey(c, constant.ContextKeyTokenCountMeta, meta) - preConsumedQuota, newAPIError := service.PreConsumeQuota(c, priceData.ShouldPreConsumedQuota, relayInfo) + newAPIError = service.PreConsumeQuota(c, priceData.ShouldPreConsumedQuota, relayInfo) if newAPIError != nil { return } defer func() { // Only return quota if downstream failed and quota was actually pre-consumed - if newAPIError != nil && preConsumedQuota != 0 { - service.ReturnPreConsumedQuota(c, relayInfo, preConsumedQuota) + if newAPIError != nil && relayInfo.FinalPreConsumedQuota != 0 { + service.ReturnPreConsumedQuota(c, relayInfo) } }() @@ -277,14 +277,13 @@ func shouldRetry(c *gin.Context, openaiErr *types.NewAPIError, retryTimes int) b func processChannelError(c *gin.Context, channelError types.ChannelError, err *types.NewAPIError) { logger.LogError(c, fmt.Sprintf("relay error (channel #%d, status code: %d): %s", channelError.ChannelId, err.StatusCode, err.Error())) - - gopool.Go(func() { - // 不要使用context获取渠道信息,异步处理时可能会出现渠道信息不一致的情况 - // do not use context to get channel info, there may be inconsistent channel info when processing asynchronously - if service.ShouldDisableChannel(channelError.ChannelId, err) && channelError.AutoBan { + // 不要使用context获取渠道信息,异步处理时可能会出现渠道信息不一致的情况 + // do not use context to get channel info, there may be inconsistent channel info when processing asynchronously + if service.ShouldDisableChannel(channelError.ChannelId, err) && channelError.AutoBan { + gopool.Go(func() { service.DisableChannel(channelError, err.Error()) - } - }) + }) + } if constant.ErrorLogEnabled && types.IsRecordErrorLog(err) { // 保存错误日志到mysql中 diff --git a/controller/setup.go b/controller/setup.go index 8943a1a02..44a7b3a73 100644 --- a/controller/setup.go +++ b/controller/setup.go @@ -178,4 +178,4 @@ func boolToString(b bool) string { return "true" } return "false" -} +} \ No newline at end of file diff --git a/controller/task_video.go b/controller/task_video.go index ffb6728ba..73d5c39b1 100644 --- a/controller/task_video.go +++ b/controller/task_video.go @@ -94,7 +94,7 @@ func updateVideoSingleTask(ctx context.Context, adaptor channel.TaskAdaptor, cha } else if taskResult, err = adaptor.ParseTaskResult(responseBody); err != nil { return fmt.Errorf("parseTaskResult failed for task %s: %w", taskId, err) } else { - task.Data = responseBody + task.Data = redactVideoResponseBody(responseBody) } now := time.Now().Unix() @@ -113,11 +113,13 @@ func updateVideoSingleTask(ctx context.Context, adaptor channel.TaskAdaptor, cha task.StartTime = now } case model.TaskStatusSuccess: - task.Progress = "100%" + task.Progress = "100%" if task.FinishTime == 0 { task.FinishTime = now } - task.FailReason = taskResult.Url + if !(len(taskResult.Url) > 5 && taskResult.Url[:5] == "data:") { + task.FailReason = taskResult.Url + } case model.TaskStatusFailure: task.Status = model.TaskStatusFailure task.Progress = "100%" @@ -146,3 +148,37 @@ func updateVideoSingleTask(ctx context.Context, adaptor channel.TaskAdaptor, cha return nil } + +func redactVideoResponseBody(body []byte) []byte { + var m map[string]any + if err := json.Unmarshal(body, &m); err != nil { + return body + } + resp, _ := m["response"].(map[string]any) + if resp != nil { + delete(resp, "bytesBase64Encoded") + if v, ok := resp["video"].(string); ok { + resp["video"] = truncateBase64(v) + } + if vs, ok := resp["videos"].([]any); ok { + for i := range vs { + if vm, ok := vs[i].(map[string]any); ok { + delete(vm, "bytesBase64Encoded") + } + } + } + } + b, err := json.Marshal(m) + if err != nil { + return body + } + return b +} + +func truncateBase64(s string) string { + const maxKeep = 256 + if len(s) <= maxKeep { + return s + } + return s[:maxKeep] + "..." +} diff --git a/controller/topup.go b/controller/topup.go index 3f3c86231..243e67940 100644 --- a/controller/topup.go +++ b/controller/topup.go @@ -9,6 +9,8 @@ import ( "one-api/model" "one-api/service" "one-api/setting" + "one-api/setting/operation_setting" + "one-api/setting/system_setting" "strconv" "sync" "time" @@ -19,6 +21,44 @@ import ( "github.com/shopspring/decimal" ) +func GetTopUpInfo(c *gin.Context) { + // 获取支付方式 + payMethods := operation_setting.PayMethods + + // 如果启用了 Stripe 支付,添加到支付方法列表 + if setting.StripeApiSecret != "" && setting.StripeWebhookSecret != "" && setting.StripePriceId != "" { + // 检查是否已经包含 Stripe + hasStripe := false + for _, method := range payMethods { + if method["type"] == "stripe" { + hasStripe = true + break + } + } + + if !hasStripe { + stripeMethod := map[string]string{ + "name": "Stripe", + "type": "stripe", + "color": "rgba(var(--semi-purple-5), 1)", + "min_topup": strconv.Itoa(setting.StripeMinTopUp), + } + payMethods = append(payMethods, stripeMethod) + } + } + + data := gin.H{ + "enable_online_topup": operation_setting.PayAddress != "" && operation_setting.EpayId != "" && operation_setting.EpayKey != "", + "enable_stripe_topup": setting.StripeApiSecret != "" && setting.StripeWebhookSecret != "" && setting.StripePriceId != "", + "pay_methods": payMethods, + "min_topup": operation_setting.MinTopUp, + "stripe_min_topup": setting.StripeMinTopUp, + "amount_options": operation_setting.GetPaymentSetting().AmountOptions, + "discount": operation_setting.GetPaymentSetting().AmountDiscount, + } + common.ApiSuccess(c, data) +} + type EpayRequest struct { Amount int64 `json:"amount"` PaymentMethod string `json:"payment_method"` @@ -31,13 +71,13 @@ type AmountRequest struct { } func GetEpayClient() *epay.Client { - if setting.PayAddress == "" || setting.EpayId == "" || setting.EpayKey == "" { + if operation_setting.PayAddress == "" || operation_setting.EpayId == "" || operation_setting.EpayKey == "" { return nil } withUrl, err := epay.NewClient(&epay.Config{ - PartnerID: setting.EpayId, - Key: setting.EpayKey, - }, setting.PayAddress) + PartnerID: operation_setting.EpayId, + Key: operation_setting.EpayKey, + }, operation_setting.PayAddress) if err != nil { return nil } @@ -58,15 +98,23 @@ func getPayMoney(amount int64, group string) float64 { } dTopupGroupRatio := decimal.NewFromFloat(topupGroupRatio) - dPrice := decimal.NewFromFloat(setting.Price) + dPrice := decimal.NewFromFloat(operation_setting.Price) + // apply optional preset discount by the original request amount (if configured), default 1.0 + discount := 1.0 + if ds, ok := operation_setting.GetPaymentSetting().AmountDiscount[int(amount)]; ok { + if ds > 0 { + discount = ds + } + } + dDiscount := decimal.NewFromFloat(discount) - payMoney := dAmount.Mul(dPrice).Mul(dTopupGroupRatio) + payMoney := dAmount.Mul(dPrice).Mul(dTopupGroupRatio).Mul(dDiscount) return payMoney.InexactFloat64() } func getMinTopup() int64 { - minTopup := setting.MinTopUp + minTopup := operation_setting.MinTopUp if !common.DisplayInCurrencyEnabled { dMinTopup := decimal.NewFromInt(int64(minTopup)) dQuotaPerUnit := decimal.NewFromFloat(common.QuotaPerUnit) @@ -99,13 +147,13 @@ func RequestEpay(c *gin.Context) { return } - if !setting.ContainsPayMethod(req.PaymentMethod) { + if !operation_setting.ContainsPayMethod(req.PaymentMethod) { c.JSON(200, gin.H{"message": "error", "data": "支付方式不存在"}) return } callBackAddress := service.GetCallbackAddress() - returnUrl, _ := url.Parse(setting.ServerAddress + "/console/log") + returnUrl, _ := url.Parse(system_setting.ServerAddress + "/console/log") notifyUrl, _ := url.Parse(callBackAddress + "/api/user/epay/notify") tradeNo := fmt.Sprintf("%s%d", common.GetRandomString(6), time.Now().Unix()) tradeNo = fmt.Sprintf("USR%dNO%s", id, tradeNo) diff --git a/controller/topup_stripe.go b/controller/topup_stripe.go index eb3208092..d462acb4b 100644 --- a/controller/topup_stripe.go +++ b/controller/topup_stripe.go @@ -8,6 +8,8 @@ import ( "one-api/common" "one-api/model" "one-api/setting" + "one-api/setting/operation_setting" + "one-api/setting/system_setting" "strconv" "strings" "time" @@ -215,8 +217,8 @@ func genStripeLink(referenceId string, customerId string, email string, amount i params := &stripe.CheckoutSessionParams{ ClientReferenceID: stripe.String(referenceId), - SuccessURL: stripe.String(setting.ServerAddress + "/log"), - CancelURL: stripe.String(setting.ServerAddress + "/topup"), + SuccessURL: stripe.String(system_setting.ServerAddress + "/log"), + CancelURL: stripe.String(system_setting.ServerAddress + "/topup"), LineItems: []*stripe.CheckoutSessionLineItemParams{ { Price: stripe.String(setting.StripePriceId), @@ -254,6 +256,7 @@ func GetChargedAmount(count float64, user model.User) float64 { } func getStripePayMoney(amount float64, group string) float64 { + originalAmount := amount if !common.DisplayInCurrencyEnabled { amount = amount / common.QuotaPerUnit } @@ -262,7 +265,14 @@ func getStripePayMoney(amount float64, group string) float64 { if topupGroupRatio == 0 { topupGroupRatio = 1 } - payMoney := amount * setting.StripeUnitPrice * topupGroupRatio + // apply optional preset discount by the original request amount (if configured), default 1.0 + discount := 1.0 + if ds, ok := operation_setting.GetPaymentSetting().AmountDiscount[int(originalAmount)]; ok { + if ds > 0 { + discount = ds + } + } + payMoney := amount * setting.StripeUnitPrice * topupGroupRatio * discount return payMoney } diff --git a/controller/uptime_kuma.go b/controller/uptime_kuma.go index 05d6297eb..41b9695c3 100644 --- a/controller/uptime_kuma.go +++ b/controller/uptime_kuma.go @@ -31,7 +31,7 @@ type Monitor struct { type UptimeGroupResult struct { CategoryName string `json:"categoryName"` - Monitors []Monitor `json:"monitors"` + Monitors []Monitor `json:"monitors"` } func getAndDecode(ctx context.Context, client *http.Client, url string, dest interface{}) error { @@ -57,29 +57,29 @@ func fetchGroupData(ctx context.Context, client *http.Client, groupConfig map[st url, _ := groupConfig["url"].(string) slug, _ := groupConfig["slug"].(string) categoryName, _ := groupConfig["categoryName"].(string) - + result := UptimeGroupResult{ CategoryName: categoryName, - Monitors: []Monitor{}, + Monitors: []Monitor{}, } - + if url == "" || slug == "" { return result } baseURL := strings.TrimSuffix(url, "/") - + var statusData struct { PublicGroupList []struct { - ID int `json:"id"` - Name string `json:"name"` + ID int `json:"id"` + Name string `json:"name"` MonitorList []struct { ID int `json:"id"` Name string `json:"name"` } `json:"monitorList"` } `json:"publicGroupList"` } - + var heartbeatData struct { HeartbeatList map[string][]struct { Status int `json:"status"` @@ -88,11 +88,11 @@ func fetchGroupData(ctx context.Context, client *http.Client, groupConfig map[st } g, gCtx := errgroup.WithContext(ctx) - g.Go(func() error { - return getAndDecode(gCtx, client, baseURL+apiStatusPath+slug, &statusData) + g.Go(func() error { + return getAndDecode(gCtx, client, baseURL+apiStatusPath+slug, &statusData) }) - g.Go(func() error { - return getAndDecode(gCtx, client, baseURL+apiHeartbeatPath+slug, &heartbeatData) + g.Go(func() error { + return getAndDecode(gCtx, client, baseURL+apiHeartbeatPath+slug, &heartbeatData) }) if g.Wait() != nil { @@ -139,7 +139,7 @@ func GetUptimeKumaStatus(c *gin.Context) { client := &http.Client{Timeout: httpTimeout} results := make([]UptimeGroupResult, len(groups)) - + g, gCtx := errgroup.WithContext(ctx) for i, group := range groups { i, group := i, group @@ -148,7 +148,7 @@ func GetUptimeKumaStatus(c *gin.Context) { return nil }) } - + g.Wait() c.JSON(http.StatusOK, gin.H{"success": true, "message": "", "data": results}) -} \ No newline at end of file +} diff --git a/controller/user.go b/controller/user.go index c9795c0cb..982329cec 100644 --- a/controller/user.go +++ b/controller/user.go @@ -210,6 +210,7 @@ func Register(c *gin.Context) { Password: user.Password, DisplayName: user.Username, InviterId: inviterId, + Role: common.RoleCommonUser, // 明确设置角色为普通用户 } if common.EmailVerificationEnabled { cleanUser.Email = user.Email @@ -426,6 +427,7 @@ func GetAffCode(c *gin.Context) { func GetSelf(c *gin.Context) { id := c.GetInt("id") + userRole := c.GetInt("role") user, err := model.GetUserById(id, false) if err != nil { common.ApiError(c, err) @@ -434,14 +436,134 @@ func GetSelf(c *gin.Context) { // Hide admin remarks: set to empty to trigger omitempty tag, ensuring the remark field is not included in JSON returned to regular users user.Remark = "" + // 计算用户权限信息 + permissions := calculateUserPermissions(userRole) + + // 获取用户设置并提取sidebar_modules + userSetting := user.GetSetting() + + // 构建响应数据,包含用户信息和权限 + responseData := map[string]interface{}{ + "id": user.Id, + "username": user.Username, + "display_name": user.DisplayName, + "role": user.Role, + "status": user.Status, + "email": user.Email, + "group": user.Group, + "quota": user.Quota, + "used_quota": user.UsedQuota, + "request_count": user.RequestCount, + "aff_code": user.AffCode, + "aff_count": user.AffCount, + "aff_quota": user.AffQuota, + "aff_history_quota": user.AffHistoryQuota, + "inviter_id": user.InviterId, + "linux_do_id": user.LinuxDOId, + "setting": user.Setting, + "stripe_customer": user.StripeCustomer, + "sidebar_modules": userSetting.SidebarModules, // 正确提取sidebar_modules字段 + "permissions": permissions, // 新增权限字段 + } + c.JSON(http.StatusOK, gin.H{ "success": true, "message": "", - "data": user, + "data": responseData, }) return } +// 计算用户权限的辅助函数 +func calculateUserPermissions(userRole int) map[string]interface{} { + permissions := map[string]interface{}{} + + // 根据用户角色计算权限 + if userRole == common.RoleRootUser { + // 超级管理员不需要边栏设置功能 + permissions["sidebar_settings"] = false + permissions["sidebar_modules"] = map[string]interface{}{} + } else if userRole == common.RoleAdminUser { + // 管理员可以设置边栏,但不包含系统设置功能 + permissions["sidebar_settings"] = true + permissions["sidebar_modules"] = map[string]interface{}{ + "admin": map[string]interface{}{ + "setting": false, // 管理员不能访问系统设置 + }, + } + } else { + // 普通用户只能设置个人功能,不包含管理员区域 + permissions["sidebar_settings"] = true + permissions["sidebar_modules"] = map[string]interface{}{ + "admin": false, // 普通用户不能访问管理员区域 + } + } + + return permissions +} + +// 根据用户角色生成默认的边栏配置 +func generateDefaultSidebarConfig(userRole int) string { + defaultConfig := map[string]interface{}{} + + // 聊天区域 - 所有用户都可以访问 + defaultConfig["chat"] = map[string]interface{}{ + "enabled": true, + "playground": true, + "chat": true, + } + + // 控制台区域 - 所有用户都可以访问 + defaultConfig["console"] = map[string]interface{}{ + "enabled": true, + "detail": true, + "token": true, + "log": true, + "midjourney": true, + "task": true, + } + + // 个人中心区域 - 所有用户都可以访问 + defaultConfig["personal"] = map[string]interface{}{ + "enabled": true, + "topup": true, + "personal": true, + } + + // 管理员区域 - 根据角色决定 + if userRole == common.RoleAdminUser { + // 管理员可以访问管理员区域,但不能访问系统设置 + defaultConfig["admin"] = map[string]interface{}{ + "enabled": true, + "channel": true, + "models": true, + "redemption": true, + "user": true, + "setting": false, // 管理员不能访问系统设置 + } + } else if userRole == common.RoleRootUser { + // 超级管理员可以访问所有功能 + defaultConfig["admin"] = map[string]interface{}{ + "enabled": true, + "channel": true, + "models": true, + "redemption": true, + "user": true, + "setting": true, + } + } + // 普通用户不包含admin区域 + + // 转换为JSON字符串 + configBytes, err := json.Marshal(defaultConfig) + if err != nil { + common.SysLog("生成默认边栏配置失败: " + err.Error()) + return "" + } + + return string(configBytes) +} + func GetUserModels(c *gin.Context) { id, err := strconv.Atoi(c.Param("id")) if err != nil { @@ -528,8 +650,8 @@ func UpdateUser(c *gin.Context) { } func UpdateSelf(c *gin.Context) { - var user model.User - err := json.NewDecoder(c.Request.Body).Decode(&user) + var requestData map[string]interface{} + err := json.NewDecoder(c.Request.Body).Decode(&requestData) if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, @@ -537,6 +659,60 @@ func UpdateSelf(c *gin.Context) { }) return } + + // 检查是否是sidebar_modules更新请求 + if sidebarModules, exists := requestData["sidebar_modules"]; exists { + userId := c.GetInt("id") + user, err := model.GetUserById(userId, false) + if err != nil { + common.ApiError(c, err) + return + } + + // 获取当前用户设置 + currentSetting := user.GetSetting() + + // 更新sidebar_modules字段 + if sidebarModulesStr, ok := sidebarModules.(string); ok { + currentSetting.SidebarModules = sidebarModulesStr + } + + // 保存更新后的设置 + user.SetSetting(currentSetting) + if err := user.Update(false); err != nil { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": "更新设置失败: " + err.Error(), + }) + return + } + + c.JSON(http.StatusOK, gin.H{ + "success": true, + "message": "设置更新成功", + }) + return + } + + // 原有的用户信息更新逻辑 + var user model.User + requestDataBytes, err := json.Marshal(requestData) + if err != nil { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": "无效的参数", + }) + return + } + err = json.Unmarshal(requestDataBytes, &user) + if err != nil { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": "无效的参数", + }) + return + } + if user.Password == "" { user.Password = "$I_LOVE_U" // make Validator happy :) } @@ -679,6 +855,7 @@ func CreateUser(c *gin.Context) { Username: user.Username, Password: user.Password, DisplayName: user.DisplayName, + Role: user.Role, // 保持管理员设置的角色 } if err := cleanUser.Insert(0); err != nil { common.ApiError(c, err) @@ -920,6 +1097,7 @@ type UpdateUserSettingRequest struct { WebhookUrl string `json:"webhook_url,omitempty"` WebhookSecret string `json:"webhook_secret,omitempty"` NotificationEmail string `json:"notification_email,omitempty"` + BarkUrl string `json:"bark_url,omitempty"` AcceptUnsetModelRatioModel bool `json:"accept_unset_model_ratio_model"` RecordIpLog bool `json:"record_ip_log"` } @@ -935,7 +1113,7 @@ func UpdateUserSetting(c *gin.Context) { } // 验证预警类型 - if req.QuotaWarningType != dto.NotifyTypeEmail && req.QuotaWarningType != dto.NotifyTypeWebhook { + if req.QuotaWarningType != dto.NotifyTypeEmail && req.QuotaWarningType != dto.NotifyTypeWebhook && req.QuotaWarningType != dto.NotifyTypeBark { c.JSON(http.StatusOK, gin.H{ "success": false, "message": "无效的预警类型", @@ -983,6 +1161,33 @@ func UpdateUserSetting(c *gin.Context) { } } + // 如果是Bark类型,验证Bark URL + if req.QuotaWarningType == dto.NotifyTypeBark { + if req.BarkUrl == "" { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": "Bark推送URL不能为空", + }) + return + } + // 验证URL格式 + if _, err := url.ParseRequestURI(req.BarkUrl); err != nil { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": "无效的Bark推送URL", + }) + return + } + // 检查是否是HTTP或HTTPS + if !strings.HasPrefix(req.BarkUrl, "https://") && !strings.HasPrefix(req.BarkUrl, "http://") { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": "Bark推送URL必须以http://或https://开头", + }) + return + } + } + userId := c.GetInt("id") user, err := model.GetUserById(userId, true) if err != nil { @@ -1011,6 +1216,11 @@ func UpdateUserSetting(c *gin.Context) { settings.NotificationEmail = req.NotificationEmail } + // 如果是Bark类型,添加Bark URL到设置中 + if req.QuotaWarningType == dto.NotifyTypeBark { + settings.BarkUrl = req.BarkUrl + } + // 更新用户设置 user.SetSetting(settings) if err := user.Update(false); err != nil { diff --git a/dto/channel_settings.go b/dto/channel_settings.go index 2c58795cb..8791f516e 100644 --- a/dto/channel_settings.go +++ b/dto/channel_settings.go @@ -9,6 +9,14 @@ type ChannelSettings struct { SystemPromptOverride bool `json:"system_prompt_override,omitempty"` } +type VertexKeyType string + +const ( + VertexKeyTypeJSON VertexKeyType = "json" + VertexKeyTypeAPIKey VertexKeyType = "api_key" +) + type ChannelOtherSettings struct { - AzureResponsesVersion string `json:"azure_responses_version,omitempty"` + AzureResponsesVersion string `json:"azure_responses_version,omitempty"` + VertexKeyType VertexKeyType `json:"vertex_key_type,omitempty"` // "json" or "api_key" } diff --git a/dto/openai_image.go b/dto/openai_image.go index 9e838688e..5aece25f2 100644 --- a/dto/openai_image.go +++ b/dto/openai_image.go @@ -59,6 +59,31 @@ func (i *ImageRequest) UnmarshalJSON(data []byte) error { return nil } +// 序列化时需要重新把字段平铺 +func (r ImageRequest) MarshalJSON() ([]byte, error) { + // 将已定义字段转为 map + type Alias ImageRequest + alias := Alias(r) + base, err := common.Marshal(alias) + if err != nil { + return nil, err + } + + var baseMap map[string]json.RawMessage + if err := common.Unmarshal(base, &baseMap); err != nil { + return nil, err + } + + // 合并 ExtraFields + for k, v := range r.Extra { + if _, exists := baseMap[k]; !exists { + baseMap[k] = v + } + } + + return json.Marshal(baseMap) +} + func GetJSONFieldNames(t reflect.Type) map[string]struct{} { fields := make(map[string]struct{}) for i := 0; i < t.NumField(); i++ { diff --git a/dto/ratio_sync.go b/dto/ratio_sync.go index 6315f31ae..d6bbf68e1 100644 --- a/dto/ratio_sync.go +++ b/dto/ratio_sync.go @@ -1,23 +1,23 @@ package dto type UpstreamDTO struct { - ID int `json:"id,omitempty"` - Name string `json:"name" binding:"required"` - BaseURL string `json:"base_url" binding:"required"` - Endpoint string `json:"endpoint"` + ID int `json:"id,omitempty"` + Name string `json:"name" binding:"required"` + BaseURL string `json:"base_url" binding:"required"` + Endpoint string `json:"endpoint"` } type UpstreamRequest struct { - ChannelIDs []int64 `json:"channel_ids"` - Upstreams []UpstreamDTO `json:"upstreams"` - Timeout int `json:"timeout"` + ChannelIDs []int64 `json:"channel_ids"` + Upstreams []UpstreamDTO `json:"upstreams"` + Timeout int `json:"timeout"` } // TestResult 上游测试连通性结果 type TestResult struct { - Name string `json:"name"` - Status string `json:"status"` - Error string `json:"error,omitempty"` + Name string `json:"name"` + Status string `json:"status"` + Error string `json:"error,omitempty"` } // DifferenceItem 差异项 @@ -25,14 +25,14 @@ type TestResult struct { // Upstreams 为各渠道的上游值,具体数值 / "same" / nil type DifferenceItem struct { - Current interface{} `json:"current"` - Upstreams map[string]interface{} `json:"upstreams"` - Confidence map[string]bool `json:"confidence"` + Current interface{} `json:"current"` + Upstreams map[string]interface{} `json:"upstreams"` + Confidence map[string]bool `json:"confidence"` } type SyncableChannel struct { - ID int `json:"id"` - Name string `json:"name"` - BaseURL string `json:"base_url"` - Status int `json:"status"` -} \ No newline at end of file + ID int `json:"id"` + Name string `json:"name"` + BaseURL string `json:"base_url"` + Status int `json:"status"` +} diff --git a/dto/user_settings.go b/dto/user_settings.go index 2e1a15418..89dd926ef 100644 --- a/dto/user_settings.go +++ b/dto/user_settings.go @@ -6,11 +6,14 @@ type UserSetting struct { WebhookUrl string `json:"webhook_url,omitempty"` // WebhookUrl webhook地址 WebhookSecret string `json:"webhook_secret,omitempty"` // WebhookSecret webhook密钥 NotificationEmail string `json:"notification_email,omitempty"` // NotificationEmail 通知邮箱地址 + BarkUrl string `json:"bark_url,omitempty"` // BarkUrl Bark推送URL AcceptUnsetRatioModel bool `json:"accept_unset_model_ratio_model,omitempty"` // AcceptUnsetRatioModel 是否接受未设置价格的模型 RecordIpLog bool `json:"record_ip_log,omitempty"` // 是否记录请求和错误日志IP + SidebarModules string `json:"sidebar_modules,omitempty"` // SidebarModules 左侧边栏模块配置 } var ( NotifyTypeEmail = "email" // Email 邮件 NotifyTypeWebhook = "webhook" // Webhook + NotifyTypeBark = "bark" // Bark 推送 ) diff --git a/main.go b/main.go index 2dfddaccf..0caf53617 100644 --- a/main.go +++ b/main.go @@ -94,13 +94,9 @@ func main() { } go controller.AutomaticallyUpdateChannels(frequency) } - if os.Getenv("CHANNEL_TEST_FREQUENCY") != "" { - frequency, err := strconv.Atoi(os.Getenv("CHANNEL_TEST_FREQUENCY")) - if err != nil { - common.FatalLog("failed to parse CHANNEL_TEST_FREQUENCY: " + err.Error()) - } - go controller.AutomaticallyTestChannels(frequency) - } + + go controller.AutomaticallyTestChannels() + if common.IsMasterNode && constant.UpdateTask { gopool.Go(func() { controller.UpdateMidjourneyTaskBulk() @@ -208,4 +204,4 @@ func InitResources() error { return err } return nil -} +} \ No newline at end of file diff --git a/middleware/distributor.go b/middleware/distributor.go index 1e6df872d..7fefeda49 100644 --- a/middleware/distributor.go +++ b/middleware/distributor.go @@ -166,9 +166,9 @@ func getModelRequest(c *gin.Context) (*ModelRequest, bool, error) { c.Set("platform", string(constant.TaskPlatformSuno)) c.Set("relay_mode", relayMode) } else if strings.Contains(c.Request.URL.Path, "/v1/video/generations") { - err = common.UnmarshalBodyReusable(c, &modelRequest) relayMode := relayconstant.RelayModeUnknown if c.Request.Method == http.MethodPost { + err = common.UnmarshalBodyReusable(c, &modelRequest) relayMode = relayconstant.RelayModeVideoSubmit } else if c.Request.Method == http.MethodGet { relayMode = relayconstant.RelayModeVideoFetchByID diff --git a/middleware/stats.go b/middleware/stats.go index 1c97983f7..e49e56991 100644 --- a/middleware/stats.go +++ b/middleware/stats.go @@ -18,12 +18,12 @@ func StatsMiddleware() gin.HandlerFunc { return func(c *gin.Context) { // 增加活跃连接数 atomic.AddInt64(&globalStats.activeConnections, 1) - + // 确保在请求结束时减少连接数 defer func() { atomic.AddInt64(&globalStats.activeConnections, -1) }() - + c.Next() } } @@ -38,4 +38,4 @@ func GetStats() StatsInfo { return StatsInfo{ ActiveConnections: atomic.LoadInt64(&globalStats.activeConnections), } -} \ No newline at end of file +} diff --git a/model/channel.go b/model/channel.go index f8e1cccc4..534e2f3f2 100644 --- a/model/channel.go +++ b/model/channel.go @@ -42,14 +42,16 @@ type Channel struct { Priority *int64 `json:"priority" gorm:"bigint;default:0"` AutoBan *int `json:"auto_ban" gorm:"default:1"` OtherInfo string `json:"other_info"` - OtherSettings string `json:"settings" gorm:"column:settings"` // 其他设置 Tag *string `json:"tag" gorm:"index"` Setting *string `json:"setting" gorm:"type:text"` // 渠道额外设置 ParamOverride *string `json:"param_override" gorm:"type:text"` HeaderOverride *string `json:"header_override" gorm:"type:text"` + Remark string `json:"remark,omitempty" gorm:"type:varchar(255)" validate:"max=255"` // add after v0.8.5 ChannelInfo ChannelInfo `json:"channel_info" gorm:"type:json"` + OtherSettings string `json:"settings" gorm:"column:settings"` // 其他设置,存储azure版本等不需要检索的信息,详见dto.ChannelOtherSettings + // cache info Keys []string `json:"-" gorm:"-"` } @@ -606,8 +608,12 @@ func UpdateChannelStatus(channelId int, usingKey string, status int, reason stri return false } if channelCache.ChannelInfo.IsMultiKey { + // Use per-channel lock to prevent concurrent map read/write with GetNextEnabledKey + pollingLock := GetChannelPollingLock(channelId) + pollingLock.Lock() // 如果是多Key模式,更新缓存中的状态 handlerMultiKeyUpdate(channelCache, usingKey, status, reason) + pollingLock.Unlock() //CacheUpdateChannel(channelCache) //return true } else { @@ -638,7 +644,11 @@ func UpdateChannelStatus(channelId int, usingKey string, status int, reason stri if channel.ChannelInfo.IsMultiKey { beforeStatus := channel.Status + // Protect map writes with the same per-channel lock used by readers + pollingLock := GetChannelPollingLock(channelId) + pollingLock.Lock() handlerMultiKeyUpdate(channel, usingKey, status, reason) + pollingLock.Unlock() if beforeStatus != channel.Status { shouldUpdateAbilities = true } diff --git a/model/main.go b/model/main.go index dbf271521..1a38d371b 100644 --- a/model/main.go +++ b/model/main.go @@ -64,22 +64,6 @@ var DB *gorm.DB var LOG_DB *gorm.DB -// dropIndexIfExists drops a MySQL index only if it exists to avoid noisy 1091 errors -func dropIndexIfExists(tableName string, indexName string) { - if !common.UsingMySQL { - return - } - var count int64 - // Check index existence via information_schema - err := DB.Raw( - "SELECT COUNT(1) FROM information_schema.statistics WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ?", - tableName, indexName, - ).Scan(&count).Error - if err == nil && count > 0 { - _ = DB.Exec("ALTER TABLE " + tableName + " DROP INDEX " + indexName + ";").Error - } -} - func createRootAccountIfNeed() error { var user User //if user.Status != common.UserStatusEnabled { @@ -263,16 +247,6 @@ func InitLogDB() (err error) { } func migrateDB() error { - // 修复旧版本留下的唯一索引,允许软删除后重新插入同名记录 - // 删除单列唯一索引(列级 UNIQUE)及早期命名方式,防止与新复合唯一索引 (model_name, deleted_at) 冲突 - dropIndexIfExists("models", "uk_model_name") // 新版复合索引名称(若已存在) - dropIndexIfExists("models", "model_name") // 旧版列级唯一索引名称 - - dropIndexIfExists("vendors", "uk_vendor_name") // 新版复合索引名称(若已存在) - dropIndexIfExists("vendors", "name") // 旧版列级唯一索引名称 - //if !common.UsingPostgreSQL { - // return migrateDBFast() - //} err := DB.AutoMigrate( &Channel{}, &Token{}, @@ -299,13 +273,6 @@ func migrateDB() error { } func migrateDBFast() error { - // 修复旧版本留下的唯一索引,允许软删除后重新插入同名记录 - // 删除单列唯一索引(列级 UNIQUE)及早期命名方式,防止与新复合唯一索引冲突 - dropIndexIfExists("models", "uk_model_name") - dropIndexIfExists("models", "model_name") - - dropIndexIfExists("vendors", "uk_vendor_name") - dropIndexIfExists("vendors", "name") var wg sync.WaitGroup diff --git a/model/model_meta.go b/model/model_meta.go index b7602b0ec..e41cbd090 100644 --- a/model/model_meta.go +++ b/model/model_meta.go @@ -20,17 +20,18 @@ type BoundChannel struct { } type Model struct { - Id int `json:"id"` - ModelName string `json:"model_name" gorm:"size:128;not null;uniqueIndex:uk_model_name,priority:1"` - Description string `json:"description,omitempty" gorm:"type:text"` - Icon string `json:"icon,omitempty" gorm:"type:varchar(128)"` - Tags string `json:"tags,omitempty" gorm:"type:varchar(255)"` - VendorID int `json:"vendor_id,omitempty" gorm:"index"` - Endpoints string `json:"endpoints,omitempty" gorm:"type:text"` - Status int `json:"status" gorm:"default:1"` - CreatedTime int64 `json:"created_time" gorm:"bigint"` - UpdatedTime int64 `json:"updated_time" gorm:"bigint"` - DeletedAt gorm.DeletedAt `json:"-" gorm:"index;uniqueIndex:uk_model_name,priority:2"` + Id int `json:"id"` + ModelName string `json:"model_name" gorm:"size:128;not null;uniqueIndex:uk_model_name_delete_at,priority:1"` + Description string `json:"description,omitempty" gorm:"type:text"` + Icon string `json:"icon,omitempty" gorm:"type:varchar(128)"` + Tags string `json:"tags,omitempty" gorm:"type:varchar(255)"` + VendorID int `json:"vendor_id,omitempty" gorm:"index"` + Endpoints string `json:"endpoints,omitempty" gorm:"type:text"` + Status int `json:"status" gorm:"default:1"` + SyncOfficial int `json:"sync_official" gorm:"default:1"` + CreatedTime int64 `json:"created_time" gorm:"bigint"` + UpdatedTime int64 `json:"updated_time" gorm:"bigint"` + DeletedAt gorm.DeletedAt `json:"-" gorm:"index;uniqueIndex:uk_model_name_delete_at,priority:2"` BoundChannels []BoundChannel `json:"bound_channels,omitempty" gorm:"-"` EnableGroups []string `json:"enable_groups,omitempty" gorm:"-"` diff --git a/model/option.go b/model/option.go index e589b46ed..ceecff658 100644 --- a/model/option.go +++ b/model/option.go @@ -6,6 +6,7 @@ import ( "one-api/setting/config" "one-api/setting/operation_setting" "one-api/setting/ratio_setting" + "one-api/setting/system_setting" "strconv" "strings" "time" @@ -66,16 +67,16 @@ func InitOptionMap() { common.OptionMap["SystemName"] = common.SystemName common.OptionMap["Logo"] = common.Logo common.OptionMap["ServerAddress"] = "" - common.OptionMap["WorkerUrl"] = setting.WorkerUrl - common.OptionMap["WorkerValidKey"] = setting.WorkerValidKey - common.OptionMap["WorkerAllowHttpImageRequestEnabled"] = strconv.FormatBool(setting.WorkerAllowHttpImageRequestEnabled) + common.OptionMap["WorkerUrl"] = system_setting.WorkerUrl + common.OptionMap["WorkerValidKey"] = system_setting.WorkerValidKey + common.OptionMap["WorkerAllowHttpImageRequestEnabled"] = strconv.FormatBool(system_setting.WorkerAllowHttpImageRequestEnabled) common.OptionMap["PayAddress"] = "" common.OptionMap["CustomCallbackAddress"] = "" common.OptionMap["EpayId"] = "" common.OptionMap["EpayKey"] = "" - common.OptionMap["Price"] = strconv.FormatFloat(setting.Price, 'f', -1, 64) - common.OptionMap["USDExchangeRate"] = strconv.FormatFloat(setting.USDExchangeRate, 'f', -1, 64) - common.OptionMap["MinTopUp"] = strconv.Itoa(setting.MinTopUp) + common.OptionMap["Price"] = strconv.FormatFloat(operation_setting.Price, 'f', -1, 64) + common.OptionMap["USDExchangeRate"] = strconv.FormatFloat(operation_setting.USDExchangeRate, 'f', -1, 64) + common.OptionMap["MinTopUp"] = strconv.Itoa(operation_setting.MinTopUp) common.OptionMap["StripeMinTopUp"] = strconv.Itoa(setting.StripeMinTopUp) common.OptionMap["StripeApiSecret"] = setting.StripeApiSecret common.OptionMap["StripeWebhookSecret"] = setting.StripeWebhookSecret @@ -85,7 +86,7 @@ func InitOptionMap() { common.OptionMap["Chats"] = setting.Chats2JsonString() common.OptionMap["AutoGroups"] = setting.AutoGroups2JsonString() common.OptionMap["DefaultUseAutoGroup"] = strconv.FormatBool(setting.DefaultUseAutoGroup) - common.OptionMap["PayMethods"] = setting.PayMethods2JsonString() + common.OptionMap["PayMethods"] = operation_setting.PayMethods2JsonString() common.OptionMap["GitHubClientId"] = "" common.OptionMap["GitHubClientSecret"] = "" common.OptionMap["TelegramBotToken"] = "" @@ -274,7 +275,7 @@ func updateOptionMap(key string, value string) (err error) { case "SMTPSSLEnabled": common.SMTPSSLEnabled = boolValue case "WorkerAllowHttpImageRequestEnabled": - setting.WorkerAllowHttpImageRequestEnabled = boolValue + system_setting.WorkerAllowHttpImageRequestEnabled = boolValue case "DefaultUseAutoGroup": setting.DefaultUseAutoGroup = boolValue case "ExposeRatioEnabled": @@ -296,29 +297,29 @@ func updateOptionMap(key string, value string) (err error) { case "SMTPToken": common.SMTPToken = value case "ServerAddress": - setting.ServerAddress = value + system_setting.ServerAddress = value case "WorkerUrl": - setting.WorkerUrl = value + system_setting.WorkerUrl = value case "WorkerValidKey": - setting.WorkerValidKey = value + system_setting.WorkerValidKey = value case "PayAddress": - setting.PayAddress = value + operation_setting.PayAddress = value case "Chats": err = setting.UpdateChatsByJsonString(value) case "AutoGroups": err = setting.UpdateAutoGroupsByJsonString(value) case "CustomCallbackAddress": - setting.CustomCallbackAddress = value + operation_setting.CustomCallbackAddress = value case "EpayId": - setting.EpayId = value + operation_setting.EpayId = value case "EpayKey": - setting.EpayKey = value + operation_setting.EpayKey = value case "Price": - setting.Price, _ = strconv.ParseFloat(value, 64) + operation_setting.Price, _ = strconv.ParseFloat(value, 64) case "USDExchangeRate": - setting.USDExchangeRate, _ = strconv.ParseFloat(value, 64) + operation_setting.USDExchangeRate, _ = strconv.ParseFloat(value, 64) case "MinTopUp": - setting.MinTopUp, _ = strconv.Atoi(value) + operation_setting.MinTopUp, _ = strconv.Atoi(value) case "StripeApiSecret": setting.StripeApiSecret = value case "StripeWebhookSecret": @@ -422,7 +423,7 @@ func updateOptionMap(key string, value string) (err error) { case "StreamCacheQueueLength": setting.StreamCacheQueueLength, _ = strconv.Atoi(value) case "PayMethods": - err = setting.UpdatePayMethodsByJsonString(value) + err = operation_setting.UpdatePayMethodsByJsonString(value) } return err } diff --git a/model/twofa.go b/model/twofa.go index 8e97289f9..2a3d33530 100644 --- a/model/twofa.go +++ b/model/twofa.go @@ -16,7 +16,7 @@ type TwoFA struct { Id int `json:"id" gorm:"primaryKey"` UserId int `json:"user_id" gorm:"unique;not null;index"` Secret string `json:"-" gorm:"type:varchar(255);not null"` // TOTP密钥,不返回给前端 - IsEnabled bool `json:"is_enabled" gorm:"default:false"` + IsEnabled bool `json:"is_enabled"` FailedAttempts int `json:"failed_attempts" gorm:"default:0"` LockedUntil *time.Time `json:"locked_until,omitempty"` LastUsedAt *time.Time `json:"last_used_at,omitempty"` @@ -30,7 +30,7 @@ type TwoFABackupCode struct { Id int `json:"id" gorm:"primaryKey"` UserId int `json:"user_id" gorm:"not null;index"` CodeHash string `json:"-" gorm:"type:varchar(255);not null"` // 备用码哈希 - IsUsed bool `json:"is_used" gorm:"default:false"` + IsUsed bool `json:"is_used"` UsedAt *time.Time `json:"used_at,omitempty"` CreatedAt time.Time `json:"created_at"` DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` diff --git a/model/user.go b/model/user.go index 29d7a4462..ea0584c5a 100644 --- a/model/user.go +++ b/model/user.go @@ -91,6 +91,68 @@ func (user *User) SetSetting(setting dto.UserSetting) { user.Setting = string(settingBytes) } +// 根据用户角色生成默认的边栏配置 +func generateDefaultSidebarConfigForRole(userRole int) string { + defaultConfig := map[string]interface{}{} + + // 聊天区域 - 所有用户都可以访问 + defaultConfig["chat"] = map[string]interface{}{ + "enabled": true, + "playground": true, + "chat": true, + } + + // 控制台区域 - 所有用户都可以访问 + defaultConfig["console"] = map[string]interface{}{ + "enabled": true, + "detail": true, + "token": true, + "log": true, + "midjourney": true, + "task": true, + } + + // 个人中心区域 - 所有用户都可以访问 + defaultConfig["personal"] = map[string]interface{}{ + "enabled": true, + "topup": true, + "personal": true, + } + + // 管理员区域 - 根据角色决定 + if userRole == common.RoleAdminUser { + // 管理员可以访问管理员区域,但不能访问系统设置 + defaultConfig["admin"] = map[string]interface{}{ + "enabled": true, + "channel": true, + "models": true, + "redemption": true, + "user": true, + "setting": false, // 管理员不能访问系统设置 + } + } else if userRole == common.RoleRootUser { + // 超级管理员可以访问所有功能 + defaultConfig["admin"] = map[string]interface{}{ + "enabled": true, + "channel": true, + "models": true, + "redemption": true, + "user": true, + "setting": true, + } + } + // 普通用户不包含admin区域 + + // 转换为JSON字符串 + configBytes, err := json.Marshal(defaultConfig) + if err != nil { + common.SysLog("生成默认边栏配置失败: " + err.Error()) + return "" + } + + return string(configBytes) +} + // CheckUserExistOrDeleted check if user exist or deleted, if not exist, return false, nil, if deleted or exist, return true, nil func CheckUserExistOrDeleted(username string, email string) (bool, error) { var user User @@ -320,10 +382,34 @@ func (user *User) Insert(inviterId int) error { user.Quota = common.QuotaForNewUser //user.SetAccessToken(common.GetUUID()) user.AffCode = common.GetRandomString(4) + + // 初始化用户设置,包括默认的边栏配置 + if user.Setting == "" { + defaultSetting := dto.UserSetting{} + // 这里暂时不设置SidebarModules,因为需要在用户创建后根据角色设置 + user.SetSetting(defaultSetting) + } + result := DB.Create(user) if result.Error != nil { return result.Error } + + // 用户创建成功后,根据角色初始化边栏配置 + // 需要重新获取用户以确保有正确的ID和Role + var createdUser User + if err := DB.Where("username = ?", user.Username).First(&createdUser).Error; err == nil { + // 生成基于角色的默认边栏配置 + defaultSidebarConfig := generateDefaultSidebarConfigForRole(createdUser.Role) + if defaultSidebarConfig != "" { + currentSetting := createdUser.GetSetting() + currentSetting.SidebarModules = defaultSidebarConfig + createdUser.SetSetting(currentSetting) + createdUser.Update(false) + common.SysLog(fmt.Sprintf("为新用户 %s (角色: %d) 初始化边栏配置", createdUser.Username, createdUser.Role)) + } + } + if common.QuotaForNewUser > 0 { RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("新用户注册赠送 %s", logger.LogQuota(common.QuotaForNewUser))) } diff --git a/model/vendor_meta.go b/model/vendor_meta.go index 88439f249..20deaea9b 100644 --- a/model/vendor_meta.go +++ b/model/vendor_meta.go @@ -14,13 +14,13 @@ import ( type Vendor struct { Id int `json:"id"` - Name string `json:"name" gorm:"size:128;not null;uniqueIndex:uk_vendor_name,priority:1"` + Name string `json:"name" gorm:"size:128;not null;uniqueIndex:uk_vendor_name_delete_at,priority:1"` Description string `json:"description,omitempty" gorm:"type:text"` Icon string `json:"icon,omitempty" gorm:"type:varchar(128)"` Status int `json:"status" gorm:"default:1"` CreatedTime int64 `json:"created_time" gorm:"bigint"` UpdatedTime int64 `json:"updated_time" gorm:"bigint"` - DeletedAt gorm.DeletedAt `json:"-" gorm:"index;uniqueIndex:uk_vendor_name,priority:2"` + DeletedAt gorm.DeletedAt `json:"-" gorm:"index;uniqueIndex:uk_vendor_name_delete_at,priority:2"` } // Insert 创建新的供应商记录 diff --git a/relay/audio_handler.go b/relay/audio_handler.go index 711cc7a9b..1357e3816 100644 --- a/relay/audio_handler.go +++ b/relay/audio_handler.go @@ -53,7 +53,7 @@ func AudioHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *type if resp != nil { httpResp = resp.(*http.Response) if httpResp.StatusCode != http.StatusOK { - newAPIError = service.RelayErrorHandler(httpResp, false) + newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false) // reset status code 重置状态码 service.ResetStatusCode(newAPIError, statusCodeMappingStr) return newAPIError diff --git a/relay/channel/api_request.go b/relay/channel/api_request.go index a50d5bdb5..a065caff7 100644 --- a/relay/channel/api_request.go +++ b/relay/channel/api_request.go @@ -264,9 +264,8 @@ func doRequest(c *gin.Context, req *http.Request, info *common.RelayInfo) (*http } resp, err := client.Do(req) - if err != nil { - return nil, err + return nil, types.NewError(err, types.ErrorCodeDoRequestFailed, types.ErrOptionWithHideErrMsg("upstream error: do request failed")) } if resp == nil { return nil, errors.New("resp is nil") diff --git a/relay/channel/aws/adaptor.go b/relay/channel/aws/adaptor.go index 1526a7f75..9d5e5891e 100644 --- a/relay/channel/aws/adaptor.go +++ b/relay/channel/aws/adaptor.go @@ -60,7 +60,16 @@ func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayIn if request == nil { return nil, errors.New("request is nil") } + // 检查是否为Nova模型 + if isNovaModel(request.Model) { + novaReq := convertToNovaRequest(request) + c.Set("request_model", request.Model) + c.Set("converted_request", novaReq) + c.Set("is_nova_model", true) + return novaReq, nil + } + // 原有的Claude模型处理逻辑 var claudeReq *dto.ClaudeRequest var err error claudeReq, err = claude.RequestOpenAI2ClaudeMessage(c, *request) @@ -69,6 +78,7 @@ func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayIn } c.Set("request_model", claudeReq.Model) c.Set("converted_request", claudeReq) + c.Set("is_nova_model", false) return claudeReq, err } diff --git a/relay/channel/aws/constants.go b/relay/channel/aws/constants.go index 3f8800b1e..72d0f9890 100644 --- a/relay/channel/aws/constants.go +++ b/relay/channel/aws/constants.go @@ -1,5 +1,7 @@ package aws +import "strings" + var awsModelIDMap = map[string]string{ "claude-instant-1.2": "anthropic.claude-instant-v1", "claude-2.0": "anthropic.claude-v2", @@ -14,6 +16,11 @@ var awsModelIDMap = map[string]string{ "claude-sonnet-4-20250514": "anthropic.claude-sonnet-4-20250514-v1:0", "claude-opus-4-20250514": "anthropic.claude-opus-4-20250514-v1:0", "claude-opus-4-1-20250805": "anthropic.claude-opus-4-1-20250805-v1:0", + // Nova models + "nova-micro-v1:0": "amazon.nova-micro-v1:0", + "nova-lite-v1:0": "amazon.nova-lite-v1:0", + "nova-pro-v1:0": "amazon.nova-pro-v1:0", + "nova-premier-v1:0": "amazon.nova-premier-v1:0", } var awsModelCanCrossRegionMap = map[string]map[string]bool{ @@ -58,7 +65,27 @@ var awsModelCanCrossRegionMap = map[string]map[string]bool{ "anthropic.claude-opus-4-1-20250805-v1:0": { "us": true, }, -} + // Nova models - all support three major regions + "amazon.nova-micro-v1:0": { + "us": true, + "eu": true, + "apac": true, + }, + "amazon.nova-lite-v1:0": { + "us": true, + "eu": true, + "apac": true, + }, + "amazon.nova-pro-v1:0": { + "us": true, + "eu": true, + "apac": true, + }, + "amazon.nova-premier-v1:0": { + "us": true, + "eu": true, + "apac": true, + }} var awsRegionCrossModelPrefixMap = map[string]string{ "us": "us", @@ -67,3 +94,8 @@ var awsRegionCrossModelPrefixMap = map[string]string{ } var ChannelName = "aws" + +// 判断是否为Nova模型 +func isNovaModel(modelId string) bool { + return strings.HasPrefix(modelId, "nova-") +} diff --git a/relay/channel/aws/dto.go b/relay/channel/aws/dto.go index 0188c30a9..9c9fe946f 100644 --- a/relay/channel/aws/dto.go +++ b/relay/channel/aws/dto.go @@ -34,3 +34,92 @@ func copyRequest(req *dto.ClaudeRequest) *AwsClaudeRequest { Thinking: req.Thinking, } } + +// NovaMessage Nova模型使用messages-v1格式 +type NovaMessage struct { + Role string `json:"role"` + Content []NovaContent `json:"content"` +} + +type NovaContent struct { + Text string `json:"text"` +} + +type NovaRequest struct { + SchemaVersion string `json:"schemaVersion"` // 请求版本,例如 "1.0" + Messages []NovaMessage `json:"messages"` // 对话消息列表 + InferenceConfig *NovaInferenceConfig `json:"inferenceConfig,omitempty"` // 推理配置,可选 +} + +type NovaInferenceConfig struct { + MaxTokens int `json:"maxTokens,omitempty"` // 最大生成的 token 数 + Temperature float64 `json:"temperature,omitempty"` // 随机性 (默认 0.7, 范围 0-1) + TopP float64 `json:"topP,omitempty"` // nucleus sampling (默认 0.9, 范围 0-1) + TopK int `json:"topK,omitempty"` // 限制候选 token 数 (默认 50, 范围 0-128) + StopSequences []string `json:"stopSequences,omitempty"` // 停止生成的序列 +} + +// 转换OpenAI请求为Nova格式 +func convertToNovaRequest(req *dto.GeneralOpenAIRequest) *NovaRequest { + novaMessages := make([]NovaMessage, len(req.Messages)) + for i, msg := range req.Messages { + novaMessages[i] = NovaMessage{ + Role: msg.Role, + Content: []NovaContent{{Text: msg.StringContent()}}, + } + } + + novaReq := &NovaRequest{ + SchemaVersion: "messages-v1", + Messages: novaMessages, + } + + // 设置推理配置 + if req.MaxTokens != 0 || (req.Temperature != nil && *req.Temperature != 0) || req.TopP != 0 || req.TopK != 0 || req.Stop != nil { + novaReq.InferenceConfig = &NovaInferenceConfig{} + if req.MaxTokens != 0 { + novaReq.InferenceConfig.MaxTokens = int(req.MaxTokens) + } + if req.Temperature != nil && *req.Temperature != 0 { + novaReq.InferenceConfig.Temperature = *req.Temperature + } + if req.TopP != 0 { + novaReq.InferenceConfig.TopP = req.TopP + } + if req.TopK != 0 { + novaReq.InferenceConfig.TopK = req.TopK + } + if req.Stop != nil { + if stopSequences := parseStopSequences(req.Stop); len(stopSequences) > 0 { + novaReq.InferenceConfig.StopSequences = stopSequences + } + } + } + + return novaReq +} + +// parseStopSequences 解析停止序列,支持字符串或字符串数组 +func parseStopSequences(stop any) []string { + if stop == nil { + return nil + } + + switch v := stop.(type) { + case string: + if v != "" { + return []string{v} + } + case []string: + return v + case []interface{}: + var sequences []string + for _, item := range v { + if str, ok := item.(string); ok && str != "" { + sequences = append(sequences, str) + } + } + return sequences + } + return nil +} diff --git a/relay/channel/aws/relay-aws.go b/relay/channel/aws/relay-aws.go index 5822e363a..eef26855a 100644 --- a/relay/channel/aws/relay-aws.go +++ b/relay/channel/aws/relay-aws.go @@ -1,6 +1,7 @@ package aws import ( + "encoding/json" "fmt" "net/http" "one-api/common" @@ -93,7 +94,19 @@ func awsHandler(c *gin.Context, info *relaycommon.RelayInfo, requestMode int) (* } awsModelId := awsModelID(c.GetString("request_model")) + // 检查是否为Nova模型 + isNova, _ := c.Get("is_nova_model") + if isNova == true { + // Nova模型也支持跨区域 + awsRegionPrefix := awsRegionPrefix(awsCli.Options().Region) + canCrossRegion := awsModelCanCrossRegion(awsModelId, awsRegionPrefix) + if canCrossRegion { + awsModelId = awsModelCrossRegion(awsModelId, awsRegionPrefix) + } + return handleNovaRequest(c, awsCli, info, awsModelId) + } + // 原有的Claude处理逻辑 awsRegionPrefix := awsRegionPrefix(awsCli.Options().Region) canCrossRegion := awsModelCanCrossRegion(awsModelId, awsRegionPrefix) if canCrossRegion { @@ -130,7 +143,12 @@ func awsHandler(c *gin.Context, info *relaycommon.RelayInfo, requestMode int) (* Usage: &dto.Usage{}, } - handlerErr := claude.HandleClaudeResponseData(c, info, claudeInfo, awsResp.Body, RequestModeMessage) + // 复制上游 Content-Type 到客户端响应头 + if awsResp.ContentType != nil && *awsResp.ContentType != "" { + c.Writer.Header().Set("Content-Type", *awsResp.ContentType) + } + + handlerErr := claude.HandleClaudeResponseData(c, info, claudeInfo, nil, awsResp.Body, RequestModeMessage) if handlerErr != nil { return handlerErr, nil } @@ -204,3 +222,74 @@ func awsStreamHandler(c *gin.Context, resp *http.Response, info *relaycommon.Rel claude.HandleStreamFinalResponse(c, info, claudeInfo, RequestModeMessage) return nil, claudeInfo.Usage } + +// Nova模型处理函数 +func handleNovaRequest(c *gin.Context, awsCli *bedrockruntime.Client, info *relaycommon.RelayInfo, awsModelId string) (*types.NewAPIError, *dto.Usage) { + novaReq_, ok := c.Get("converted_request") + if !ok { + return types.NewError(errors.New("nova request not found"), types.ErrorCodeInvalidRequest), nil + } + novaReq := novaReq_.(*NovaRequest) + + // 使用InvokeModel API,但使用Nova格式的请求体 + awsReq := &bedrockruntime.InvokeModelInput{ + ModelId: aws.String(awsModelId), + Accept: aws.String("application/json"), + ContentType: aws.String("application/json"), + } + + reqBody, err := json.Marshal(novaReq) + if err != nil { + return types.NewError(errors.Wrap(err, "marshal nova request"), types.ErrorCodeBadResponseBody), nil + } + awsReq.Body = reqBody + + awsResp, err := awsCli.InvokeModel(c.Request.Context(), awsReq) + if err != nil { + return types.NewError(errors.Wrap(err, "InvokeModel"), types.ErrorCodeChannelAwsClientError), nil + } + + // 解析Nova响应 + var novaResp struct { + Output struct { + Message struct { + Content []struct { + Text string `json:"text"` + } `json:"content"` + } `json:"message"` + } `json:"output"` + Usage struct { + InputTokens int `json:"inputTokens"` + OutputTokens int `json:"outputTokens"` + TotalTokens int `json:"totalTokens"` + } `json:"usage"` + } + + if err := json.Unmarshal(awsResp.Body, &novaResp); err != nil { + return types.NewError(errors.Wrap(err, "unmarshal nova response"), types.ErrorCodeBadResponseBody), nil + } + + // 构造OpenAI格式响应 + response := dto.OpenAITextResponse{ + Id: helper.GetResponseID(c), + Object: "chat.completion", + Created: common.GetTimestamp(), + Model: info.UpstreamModelName, + Choices: []dto.OpenAITextResponseChoice{{ + Index: 0, + Message: dto.Message{ + Role: "assistant", + Content: novaResp.Output.Message.Content[0].Text, + }, + FinishReason: "stop", + }}, + Usage: dto.Usage{ + PromptTokens: novaResp.Usage.InputTokens, + CompletionTokens: novaResp.Usage.OutputTokens, + TotalTokens: novaResp.Usage.TotalTokens, + }, + } + + c.JSON(http.StatusOK, response) + return nil, &response.Usage +} diff --git a/relay/channel/claude/relay-claude.go b/relay/channel/claude/relay-claude.go index 0c445bb9a..682256416 100644 --- a/relay/channel/claude/relay-claude.go +++ b/relay/channel/claude/relay-claude.go @@ -32,7 +32,7 @@ func stopReasonClaude2OpenAI(reason string) string { case "end_turn": return "stop" case "max_tokens": - return "max_tokens" + return "length" case "tool_use": return "tool_calls" default: @@ -274,19 +274,28 @@ func RequestOpenAI2ClaudeMessage(c *gin.Context, textRequest dto.GeneralOpenAIRe claudeMessages := make([]dto.ClaudeMessage, 0) isFirstMessage := true + // 初始化system消息数组,用于累积多个system消息 + var systemMessages []dto.ClaudeMediaMessage + for _, message := range formatMessages { if message.Role == "system" { + // 根据Claude API规范,system字段使用数组格式更有通用性 if message.IsStringContent() { - claudeRequest.System = message.StringContent() + systemMessages = append(systemMessages, dto.ClaudeMediaMessage{ + Type: "text", + Text: common.GetPointer[string](message.StringContent()), + }) } else { - contents := message.ParseContent() - content := "" - for _, ctx := range contents { + // 支持复合内容的system消息(虽然不常见,但需要考虑完整性) + for _, ctx := range message.ParseContent() { if ctx.Type == "text" { - content += ctx.Text + systemMessages = append(systemMessages, dto.ClaudeMediaMessage{ + Type: "text", + Text: common.GetPointer[string](ctx.Text), + }) } + // 未来可以在这里扩展对图片等其他类型的支持 } - claudeRequest.System = content } } else { if isFirstMessage { @@ -392,6 +401,12 @@ func RequestOpenAI2ClaudeMessage(c *gin.Context, textRequest dto.GeneralOpenAIRe claudeMessages = append(claudeMessages, claudeMessage) } } + + // 设置累积的system消息 + if len(systemMessages) > 0 { + claudeRequest.System = systemMessages + } + claudeRequest.Prompt = "" claudeRequest.Messages = claudeMessages return &claudeRequest, nil @@ -426,7 +441,10 @@ func StreamResponseClaude2OpenAI(reqMode int, claudeResponse *dto.ClaudeResponse choice.Delta.Role = "assistant" } else if claudeResponse.Type == "content_block_start" { if claudeResponse.ContentBlock != nil { - //choice.Delta.SetContentString(claudeResponse.ContentBlock.Text) + // 如果是文本块,尽可能发送首段文本(若存在) + if claudeResponse.ContentBlock.Type == "text" && claudeResponse.ContentBlock.Text != nil { + choice.Delta.SetContentString(*claudeResponse.ContentBlock.Text) + } if claudeResponse.ContentBlock.Type == "tool_use" { tools = append(tools, dto.ToolCallResponse{ Index: common.GetPointer(fcIdx), @@ -698,7 +716,7 @@ func ClaudeStreamHandler(c *gin.Context, resp *http.Response, info *relaycommon. return claudeInfo.Usage, nil } -func HandleClaudeResponseData(c *gin.Context, info *relaycommon.RelayInfo, claudeInfo *ClaudeResponseInfo, data []byte, requestMode int) *types.NewAPIError { +func HandleClaudeResponseData(c *gin.Context, info *relaycommon.RelayInfo, claudeInfo *ClaudeResponseInfo, httpResp *http.Response, data []byte, requestMode int) *types.NewAPIError { var claudeResponse dto.ClaudeResponse err := common.Unmarshal(data, &claudeResponse) if err != nil { @@ -736,7 +754,7 @@ func HandleClaudeResponseData(c *gin.Context, info *relaycommon.RelayInfo, claud c.Set("claude_web_search_requests", claudeResponse.Usage.ServerToolUse.WebSearchRequests) } - service.IOCopyBytesGracefully(c, nil, responseData) + service.IOCopyBytesGracefully(c, httpResp, responseData) return nil } @@ -757,7 +775,7 @@ func ClaudeHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayI if common.DebugEnabled { println("responseBody: ", string(responseBody)) } - handleErr := HandleClaudeResponseData(c, info, claudeInfo, responseBody, requestMode) + handleErr := HandleClaudeResponseData(c, info, claudeInfo, resp, responseBody, requestMode) if handleErr != nil { return nil, handleErr } diff --git a/relay/channel/gemini/relay-gemini.go b/relay/channel/gemini/relay-gemini.go index eb4afbae1..199c84664 100644 --- a/relay/channel/gemini/relay-gemini.go +++ b/relay/channel/gemini/relay-gemini.go @@ -23,6 +23,7 @@ import ( "github.com/gin-gonic/gin" ) +// https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference?hl=zh-cn#blob var geminiSupportedMimeTypes = map[string]bool{ "application/pdf": true, "audio/mpeg": true, @@ -30,6 +31,7 @@ var geminiSupportedMimeTypes = map[string]bool{ "audio/wav": true, "image/png": true, "image/jpeg": true, + "image/webp": true, "text/plain": true, "video/mov": true, "video/mpeg": true, diff --git a/relay/channel/mokaai/constants.go b/relay/channel/mokaai/constants.go index 415d83b7f..385a0876b 100644 --- a/relay/channel/mokaai/constants.go +++ b/relay/channel/mokaai/constants.go @@ -6,4 +6,4 @@ var ModelList = []string{ "m3e-small", } -var ChannelName = "mokaai" \ No newline at end of file +var ChannelName = "mokaai" diff --git a/relay/channel/openai/relay-openai.go b/relay/channel/openai/relay-openai.go index cce9235b5..4b13a7df1 100644 --- a/relay/channel/openai/relay-openai.go +++ b/relay/channel/openai/relay-openai.go @@ -2,6 +2,7 @@ package openai import ( "bytes" + "encoding/json" "fmt" "io" "math" @@ -280,11 +281,6 @@ func OpenaiTTSHandler(c *gin.Context, resp *http.Response, info *relaycommon.Rel func OpenaiSTTHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo, responseFormat string) (*types.NewAPIError, *dto.Usage) { defer service.CloseResponseBodyGracefully(resp) - // count tokens by audio file duration - audioTokens, err := countAudioTokens(c) - if err != nil { - return types.NewError(err, types.ErrorCodeCountTokenFailed), nil - } responseBody, err := io.ReadAll(resp.Body) if err != nil { return types.NewOpenAIError(err, types.ErrorCodeReadResponseBodyFailed, http.StatusInternalServerError), nil @@ -292,6 +288,26 @@ func OpenaiSTTHandler(c *gin.Context, resp *http.Response, info *relaycommon.Rel // 写入新的 response body service.IOCopyBytesGracefully(c, resp, responseBody) + var responseData struct { + Usage *dto.Usage `json:"usage"` + } + if err := json.Unmarshal(responseBody, &responseData); err == nil && responseData.Usage != nil { + if responseData.Usage.TotalTokens > 0 { + usage := responseData.Usage + if usage.PromptTokens == 0 { + usage.PromptTokens = usage.InputTokens + } + if usage.CompletionTokens == 0 { + usage.CompletionTokens = usage.OutputTokens + } + return nil, usage + } + } + + audioTokens, err := countAudioTokens(c) + if err != nil { + return types.NewError(err, types.ErrorCodeCountTokenFailed), nil + } usage := &dto.Usage{} usage.PromptTokens = audioTokens usage.CompletionTokens = 0 diff --git a/relay/channel/openai/relay_responses.go b/relay/channel/openai/relay_responses.go index 88fb88083..e188889e4 100644 --- a/relay/channel/openai/relay_responses.go +++ b/relay/channel/openai/relay_responses.go @@ -46,9 +46,17 @@ func OaiResponsesHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http usage.PromptTokensDetails.CachedTokens = responsesResponse.Usage.InputTokensDetails.CachedTokens } } + if info == nil || info.ResponsesUsageInfo == nil || info.ResponsesUsageInfo.BuiltInTools == nil { + return &usage, nil + } // 解析 Tools 用量 for _, tool := range responsesResponse.Tools { - info.ResponsesUsageInfo.BuiltInTools[common.Interface2String(tool["type"])].CallCount++ + buildToolinfo, ok := info.ResponsesUsageInfo.BuiltInTools[common.Interface2String(tool["type"])] + if !ok || buildToolinfo == nil { + logger.LogError(c, fmt.Sprintf("BuiltInTools not found for tool type: %v", tool["type"])) + continue + } + buildToolinfo.CallCount++ } return &usage, nil } @@ -72,7 +80,7 @@ func OaiResponsesStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp sendResponsesStreamData(c, streamResponse, data) switch streamResponse.Type { case "response.completed": - if streamResponse.Response.Usage != nil { + if streamResponse.Response != nil && streamResponse.Response.Usage != nil { if streamResponse.Response.Usage.InputTokens != 0 { usage.PromptTokens = streamResponse.Response.Usage.InputTokens } diff --git a/relay/channel/task/jimeng/adaptor.go b/relay/channel/task/jimeng/adaptor.go index 955e592a2..2bc45c547 100644 --- a/relay/channel/task/jimeng/adaptor.go +++ b/relay/channel/task/jimeng/adaptor.go @@ -18,7 +18,6 @@ import ( "github.com/gin-gonic/gin" "github.com/pkg/errors" - "one-api/common" "one-api/constant" "one-api/dto" "one-api/relay/channel" @@ -89,22 +88,7 @@ func (a *TaskAdaptor) Init(info *relaycommon.RelayInfo) { // ValidateRequestAndSetAction parses body, validates fields and sets default action. func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.RelayInfo) (taskErr *dto.TaskError) { // Accept only POST /v1/video/generations as "generate" action. - action := constant.TaskActionGenerate - info.Action = action - - req := relaycommon.TaskSubmitReq{} - if err := common.UnmarshalBodyReusable(c, &req); err != nil { - taskErr = service.TaskErrorWrapperLocal(err, "invalid_request", http.StatusBadRequest) - return - } - if strings.TrimSpace(req.Prompt) == "" { - taskErr = service.TaskErrorWrapperLocal(fmt.Errorf("prompt is required"), "invalid_request", http.StatusBadRequest) - return - } - - // Store into context for later usage - c.Set("task_request", req) - return nil + return relaycommon.ValidateBasicTaskRequest(c, info, constant.TaskActionGenerate) } // BuildRequestURL constructs the upstream URL. @@ -334,11 +318,11 @@ func (a *TaskAdaptor) convertToRequestPayload(req *relaycommon.TaskSubmitReq) (* } // Handle one-of image_urls or binary_data_base64 - if req.Image != "" { - if strings.HasPrefix(req.Image, "http") { - r.ImageUrls = []string{req.Image} + if req.HasImage() { + if strings.HasPrefix(req.Images[0], "http") { + r.ImageUrls = req.Images } else { - r.BinaryDataBase64 = []string{req.Image} + r.BinaryDataBase64 = req.Images } } metadata := req.Metadata diff --git a/relay/channel/task/kling/adaptor.go b/relay/channel/task/kling/adaptor.go index 3d6da253b..13f2af972 100644 --- a/relay/channel/task/kling/adaptor.go +++ b/relay/channel/task/kling/adaptor.go @@ -16,7 +16,6 @@ import ( "github.com/golang-jwt/jwt" "github.com/pkg/errors" - "one-api/common" "one-api/constant" "one-api/dto" "one-api/relay/channel" @@ -28,16 +27,6 @@ import ( // Request / Response structures // ============================ -type SubmitReq struct { - Prompt string `json:"prompt"` - Model string `json:"model,omitempty"` - Mode string `json:"mode,omitempty"` - Image string `json:"image,omitempty"` - Size string `json:"size,omitempty"` - Duration int `json:"duration,omitempty"` - Metadata map[string]interface{} `json:"metadata,omitempty"` -} - type TrajectoryPoint struct { X int `json:"x"` Y int `json:"y"` @@ -121,23 +110,8 @@ func (a *TaskAdaptor) Init(info *relaycommon.RelayInfo) { // ValidateRequestAndSetAction parses body, validates fields and sets default action. func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.RelayInfo) (taskErr *dto.TaskError) { - // Accept only POST /v1/video/generations as "generate" action. - action := constant.TaskActionGenerate - info.Action = action - - var req SubmitReq - if err := common.UnmarshalBodyReusable(c, &req); err != nil { - taskErr = service.TaskErrorWrapperLocal(err, "invalid_request", http.StatusBadRequest) - return - } - if strings.TrimSpace(req.Prompt) == "" { - taskErr = service.TaskErrorWrapperLocal(fmt.Errorf("prompt is required"), "invalid_request", http.StatusBadRequest) - return - } - - // Store into context for later usage - c.Set("task_request", req) - return nil + // Use the standard validation method for TaskSubmitReq + return relaycommon.ValidateBasicTaskRequest(c, info, constant.TaskActionGenerate) } // BuildRequestURL constructs the upstream URL. @@ -166,7 +140,7 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.RelayIn if !exists { return nil, fmt.Errorf("request not found in context") } - req := v.(SubmitReq) + req := v.(relaycommon.TaskSubmitReq) body, err := a.convertToRequestPayload(&req) if err != nil { @@ -255,7 +229,7 @@ func (a *TaskAdaptor) GetChannelName() string { // helpers // ============================ -func (a *TaskAdaptor) convertToRequestPayload(req *SubmitReq) (*requestPayload, error) { +func (a *TaskAdaptor) convertToRequestPayload(req *relaycommon.TaskSubmitReq) (*requestPayload, error) { r := requestPayload{ Prompt: req.Prompt, Image: req.Image, diff --git a/relay/channel/task/vertex/adaptor.go b/relay/channel/task/vertex/adaptor.go new file mode 100644 index 000000000..4a236b2f0 --- /dev/null +++ b/relay/channel/task/vertex/adaptor.go @@ -0,0 +1,355 @@ +package vertex + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "fmt" + "io" + "net/http" + "one-api/model" + "regexp" + "strings" + + "github.com/gin-gonic/gin" + + "one-api/constant" + "one-api/dto" + "one-api/relay/channel" + vertexcore "one-api/relay/channel/vertex" + relaycommon "one-api/relay/common" + "one-api/service" +) + +// ============================ +// Request / Response structures +// ============================ + +type requestPayload struct { + Instances []map[string]any `json:"instances"` + Parameters map[string]any `json:"parameters,omitempty"` +} + +type submitResponse struct { + Name string `json:"name"` +} + +type operationVideo struct { + MimeType string `json:"mimeType"` + BytesBase64Encoded string `json:"bytesBase64Encoded"` + Encoding string `json:"encoding"` +} + +type operationResponse struct { + Name string `json:"name"` + Done bool `json:"done"` + Response struct { + Type string `json:"@type"` + RaiMediaFilteredCount int `json:"raiMediaFilteredCount"` + Videos []operationVideo `json:"videos"` + BytesBase64Encoded string `json:"bytesBase64Encoded"` + Encoding string `json:"encoding"` + Video string `json:"video"` + } `json:"response"` + Error struct { + Message string `json:"message"` + } `json:"error"` +} + +// ============================ +// Adaptor implementation +// ============================ + +type TaskAdaptor struct { + ChannelType int + apiKey string + baseURL string +} + +func (a *TaskAdaptor) Init(info *relaycommon.RelayInfo) { + a.ChannelType = info.ChannelType + a.baseURL = info.ChannelBaseUrl + a.apiKey = info.ApiKey +} + +// ValidateRequestAndSetAction parses body, validates fields and sets default action. +func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.RelayInfo) (taskErr *dto.TaskError) { + // Use the standard validation method for TaskSubmitReq + return relaycommon.ValidateBasicTaskRequest(c, info, constant.TaskActionTextGenerate) +} + +// BuildRequestURL constructs the upstream URL. +func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.RelayInfo) (string, error) { + adc := &vertexcore.Credentials{} + if err := json.Unmarshal([]byte(a.apiKey), adc); err != nil { + return "", fmt.Errorf("failed to decode credentials: %w", err) + } + modelName := info.OriginModelName + if modelName == "" { + modelName = "veo-3.0-generate-001" + } + + region := vertexcore.GetModelRegion(info.ApiVersion, modelName) + if strings.TrimSpace(region) == "" { + region = "global" + } + if region == "global" { + return fmt.Sprintf( + "https://aiplatform.googleapis.com/v1/projects/%s/locations/global/publishers/google/models/%s:predictLongRunning", + adc.ProjectID, + modelName, + ), nil + } + return fmt.Sprintf( + "https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:predictLongRunning", + region, + adc.ProjectID, + region, + modelName, + ), nil +} + +// BuildRequestHeader sets required headers. +func (a *TaskAdaptor) BuildRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error { + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/json") + + adc := &vertexcore.Credentials{} + if err := json.Unmarshal([]byte(a.apiKey), adc); err != nil { + return fmt.Errorf("failed to decode credentials: %w", err) + } + + token, err := vertexcore.AcquireAccessToken(*adc, "") + if err != nil { + return fmt.Errorf("failed to acquire access token: %w", err) + } + req.Header.Set("Authorization", "Bearer "+token) + req.Header.Set("x-goog-user-project", adc.ProjectID) + return nil +} + +// BuildRequestBody converts request into Vertex specific format. +func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.RelayInfo) (io.Reader, error) { + v, ok := c.Get("task_request") + if !ok { + return nil, fmt.Errorf("request not found in context") + } + req := v.(relaycommon.TaskSubmitReq) + + body := requestPayload{ + Instances: []map[string]any{{"prompt": req.Prompt}}, + Parameters: map[string]any{}, + } + if req.Metadata != nil { + if v, ok := req.Metadata["storageUri"]; ok { + body.Parameters["storageUri"] = v + } + if v, ok := req.Metadata["sampleCount"]; ok { + body.Parameters["sampleCount"] = v + } + } + if _, ok := body.Parameters["sampleCount"]; !ok { + body.Parameters["sampleCount"] = 1 + } + + data, err := json.Marshal(body) + if err != nil { + return nil, err + } + return bytes.NewReader(data), nil +} + +// DoRequest delegates to common helper. +func (a *TaskAdaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) { + return channel.DoTaskApiRequest(a, c, info, requestBody) +} + +// DoResponse handles upstream response, returns taskID etc. +func (a *TaskAdaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (taskID string, taskData []byte, taskErr *dto.TaskError) { + responseBody, err := io.ReadAll(resp.Body) + if err != nil { + return "", nil, service.TaskErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError) + } + _ = resp.Body.Close() + + var s submitResponse + if err := json.Unmarshal(responseBody, &s); err != nil { + return "", nil, service.TaskErrorWrapper(err, "unmarshal_response_failed", http.StatusInternalServerError) + } + if strings.TrimSpace(s.Name) == "" { + return "", nil, service.TaskErrorWrapper(fmt.Errorf("missing operation name"), "invalid_response", http.StatusInternalServerError) + } + localID := encodeLocalTaskID(s.Name) + c.JSON(http.StatusOK, gin.H{"task_id": localID}) + return localID, responseBody, nil +} + +func (a *TaskAdaptor) GetModelList() []string { return []string{"veo-3.0-generate-001"} } +func (a *TaskAdaptor) GetChannelName() string { return "vertex" } + +// FetchTask fetch task status +func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any) (*http.Response, error) { + taskID, ok := body["task_id"].(string) + if !ok { + return nil, fmt.Errorf("invalid task_id") + } + upstreamName, err := decodeLocalTaskID(taskID) + if err != nil { + return nil, fmt.Errorf("decode task_id failed: %w", err) + } + region := extractRegionFromOperationName(upstreamName) + if region == "" { + region = "us-central1" + } + project := extractProjectFromOperationName(upstreamName) + modelName := extractModelFromOperationName(upstreamName) + if project == "" || modelName == "" { + return nil, fmt.Errorf("cannot extract project/model from operation name") + } + var url string + if region == "global" { + url = fmt.Sprintf("https://aiplatform.googleapis.com/v1/projects/%s/locations/global/publishers/google/models/%s:fetchPredictOperation", project, modelName) + } else { + url = fmt.Sprintf("https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:fetchPredictOperation", region, project, region, modelName) + } + payload := map[string]string{"operationName": upstreamName} + data, err := json.Marshal(payload) + if err != nil { + return nil, err + } + adc := &vertexcore.Credentials{} + if err := json.Unmarshal([]byte(key), adc); err != nil { + return nil, fmt.Errorf("failed to decode credentials: %w", err) + } + token, err := vertexcore.AcquireAccessToken(*adc, "") + if err != nil { + return nil, fmt.Errorf("failed to acquire access token: %w", err) + } + req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data)) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Accept", "application/json") + req.Header.Set("Authorization", "Bearer "+token) + req.Header.Set("x-goog-user-project", adc.ProjectID) + return service.GetHttpClient().Do(req) +} + +func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, error) { + var op operationResponse + if err := json.Unmarshal(respBody, &op); err != nil { + return nil, fmt.Errorf("unmarshal operation response failed: %w", err) + } + ti := &relaycommon.TaskInfo{} + if op.Error.Message != "" { + ti.Status = model.TaskStatusFailure + ti.Reason = op.Error.Message + ti.Progress = "100%" + return ti, nil + } + if !op.Done { + ti.Status = model.TaskStatusInProgress + ti.Progress = "50%" + return ti, nil + } + ti.Status = model.TaskStatusSuccess + ti.Progress = "100%" + if len(op.Response.Videos) > 0 { + v0 := op.Response.Videos[0] + if v0.BytesBase64Encoded != "" { + mime := strings.TrimSpace(v0.MimeType) + if mime == "" { + enc := strings.TrimSpace(v0.Encoding) + if enc == "" { + enc = "mp4" + } + if strings.Contains(enc, "/") { + mime = enc + } else { + mime = "video/" + enc + } + } + ti.Url = "data:" + mime + ";base64," + v0.BytesBase64Encoded + return ti, nil + } + } + if op.Response.BytesBase64Encoded != "" { + enc := strings.TrimSpace(op.Response.Encoding) + if enc == "" { + enc = "mp4" + } + mime := enc + if !strings.Contains(enc, "/") { + mime = "video/" + enc + } + ti.Url = "data:" + mime + ";base64," + op.Response.BytesBase64Encoded + return ti, nil + } + if op.Response.Video != "" { // some variants use `video` as base64 + enc := strings.TrimSpace(op.Response.Encoding) + if enc == "" { + enc = "mp4" + } + mime := enc + if !strings.Contains(enc, "/") { + mime = "video/" + enc + } + ti.Url = "data:" + mime + ";base64," + op.Response.Video + return ti, nil + } + return ti, nil +} + +// ============================ +// helpers +// ============================ + +func encodeLocalTaskID(name string) string { + return base64.RawURLEncoding.EncodeToString([]byte(name)) +} + +func decodeLocalTaskID(local string) (string, error) { + b, err := base64.RawURLEncoding.DecodeString(local) + if err != nil { + return "", err + } + return string(b), nil +} + +var regionRe = regexp.MustCompile(`locations/([a-z0-9-]+)/`) + +func extractRegionFromOperationName(name string) string { + m := regionRe.FindStringSubmatch(name) + if len(m) == 2 { + return m[1] + } + return "" +} + +var modelRe = regexp.MustCompile(`models/([^/]+)/operations/`) + +func extractModelFromOperationName(name string) string { + m := modelRe.FindStringSubmatch(name) + if len(m) == 2 { + return m[1] + } + idx := strings.Index(name, "models/") + if idx >= 0 { + s := name[idx+len("models/"):] + if p := strings.Index(s, "/operations/"); p > 0 { + return s[:p] + } + } + return "" +} + +var projectRe = regexp.MustCompile(`projects/([^/]+)/locations/`) + +func extractProjectFromOperationName(name string) string { + m := projectRe.FindStringSubmatch(name) + if len(m) == 2 { + return m[1] + } + return "" +} diff --git a/relay/channel/task/vidu/adaptor.go b/relay/channel/task/vidu/adaptor.go index c82c1c0e8..a1140d1e7 100644 --- a/relay/channel/task/vidu/adaptor.go +++ b/relay/channel/task/vidu/adaptor.go @@ -23,16 +23,6 @@ import ( // Request / Response structures // ============================ -type SubmitReq struct { - Prompt string `json:"prompt"` - Model string `json:"model,omitempty"` - Mode string `json:"mode,omitempty"` - Image string `json:"image,omitempty"` - Size string `json:"size,omitempty"` - Duration int `json:"duration,omitempty"` - Metadata map[string]interface{} `json:"metadata,omitempty"` -} - type requestPayload struct { Model string `json:"model"` Images []string `json:"images"` @@ -90,23 +80,8 @@ func (a *TaskAdaptor) Init(info *relaycommon.RelayInfo) { } func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.RelayInfo) *dto.TaskError { - var req SubmitReq - if err := c.ShouldBindJSON(&req); err != nil { - return service.TaskErrorWrapper(err, "invalid_request_body", http.StatusBadRequest) - } - - if req.Prompt == "" { - return service.TaskErrorWrapperLocal(fmt.Errorf("prompt is required"), "missing_prompt", http.StatusBadRequest) - } - - if req.Image != "" { - info.Action = constant.TaskActionGenerate - } else { - info.Action = constant.TaskActionTextGenerate - } - - c.Set("task_request", req) - return nil + // Use the unified validation method for TaskSubmitReq with image-based action determination + return relaycommon.ValidateTaskRequestWithImageBinding(c, info) } func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, _ *relaycommon.RelayInfo) (io.Reader, error) { @@ -114,7 +89,7 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, _ *relaycommon.RelayInfo) if !exists { return nil, fmt.Errorf("request not found in context") } - req := v.(SubmitReq) + req := v.(relaycommon.TaskSubmitReq) body, err := a.convertToRequestPayload(&req) if err != nil { @@ -211,7 +186,7 @@ func (a *TaskAdaptor) GetChannelName() string { // helpers // ============================ -func (a *TaskAdaptor) convertToRequestPayload(req *SubmitReq) (*requestPayload, error) { +func (a *TaskAdaptor) convertToRequestPayload(req *relaycommon.TaskSubmitReq) (*requestPayload, error) { var images []string if req.Image != "" { images = []string{req.Image} diff --git a/relay/channel/vertex/adaptor.go b/relay/channel/vertex/adaptor.go index 0b6b26743..a424cb1a4 100644 --- a/relay/channel/vertex/adaptor.go +++ b/relay/channel/vertex/adaptor.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "one-api/common" "one-api/dto" "one-api/relay/channel" "one-api/relay/channel/claude" @@ -80,16 +81,64 @@ func (a *Adaptor) Init(info *relaycommon.RelayInfo) { } } -func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) { - adc := &Credentials{} - if err := json.Unmarshal([]byte(info.ApiKey), adc); err != nil { - return "", fmt.Errorf("failed to decode credentials file: %w", err) - } +func (a *Adaptor) getRequestUrl(info *relaycommon.RelayInfo, modelName, suffix string) (string, error) { region := GetModelRegion(info.ApiVersion, info.OriginModelName) - a.AccountCredentials = *adc + if info.ChannelOtherSettings.VertexKeyType != dto.VertexKeyTypeAPIKey { + adc := &Credentials{} + if err := common.Unmarshal([]byte(info.ApiKey), adc); err != nil { + return "", fmt.Errorf("failed to decode credentials file: %w", err) + } + a.AccountCredentials = *adc + + if a.RequestMode == RequestModeLlama { + return fmt.Sprintf( + "https://%s-aiplatform.googleapis.com/v1beta1/projects/%s/locations/%s/endpoints/openapi/chat/completions", + region, + adc.ProjectID, + region, + ), nil + } + + if region == "global" { + return fmt.Sprintf( + "https://aiplatform.googleapis.com/v1/projects/%s/locations/global/publishers/google/models/%s:%s", + adc.ProjectID, + modelName, + suffix, + ), nil + } else { + return fmt.Sprintf( + "https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:%s", + region, + adc.ProjectID, + region, + modelName, + suffix, + ), nil + } + } else { + if region == "global" { + return fmt.Sprintf( + "https://aiplatform.googleapis.com/v1/publishers/google/models/%s:%s?key=%s", + modelName, + suffix, + info.ApiKey, + ), nil + } else { + return fmt.Sprintf( + "https://%s-aiplatform.googleapis.com/v1/publishers/google/models/%s:%s?key=%s", + region, + modelName, + suffix, + info.ApiKey, + ), nil + } + } +} + +func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) { suffix := "" if a.RequestMode == RequestModeGemini { - if model_setting.GetGeminiSettings().ThinkingAdapterEnabled { // 新增逻辑:处理 -thinking- 格式 if strings.Contains(info.UpstreamModelName, "-thinking-") { @@ -111,24 +160,7 @@ func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) { if strings.HasPrefix(info.UpstreamModelName, "imagen") { suffix = "predict" } - - if region == "global" { - return fmt.Sprintf( - "https://aiplatform.googleapis.com/v1/projects/%s/locations/global/publishers/google/models/%s:%s", - adc.ProjectID, - info.UpstreamModelName, - suffix, - ), nil - } else { - return fmt.Sprintf( - "https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:%s", - region, - adc.ProjectID, - region, - info.UpstreamModelName, - suffix, - ), nil - } + return a.getRequestUrl(info, info.UpstreamModelName, suffix) } else if a.RequestMode == RequestModeClaude { if info.IsStream { suffix = "streamRawPredict?alt=sse" @@ -139,41 +171,25 @@ func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) { if v, ok := claudeModelMap[info.UpstreamModelName]; ok { model = v } - if region == "global" { - return fmt.Sprintf( - "https://aiplatform.googleapis.com/v1/projects/%s/locations/global/publishers/anthropic/models/%s:%s", - adc.ProjectID, - model, - suffix, - ), nil - } else { - return fmt.Sprintf( - "https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/anthropic/models/%s:%s", - region, - adc.ProjectID, - region, - model, - suffix, - ), nil - } + return a.getRequestUrl(info, model, suffix) } else if a.RequestMode == RequestModeLlama { - return fmt.Sprintf( - "https://%s-aiplatform.googleapis.com/v1beta1/projects/%s/locations/%s/endpoints/openapi/chat/completions", - region, - adc.ProjectID, - region, - ), nil + return a.getRequestUrl(info, "", "") } return "", errors.New("unsupported request mode") } func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error { channel.SetupApiRequestHeader(info, c, req) - accessToken, err := getAccessToken(a, info) - if err != nil { - return err + if info.ChannelOtherSettings.VertexKeyType != dto.VertexKeyTypeAPIKey { + accessToken, err := getAccessToken(a, info) + if err != nil { + return err + } + req.Set("Authorization", "Bearer "+accessToken) + } + if a.AccountCredentials.ProjectID != "" { + req.Set("x-goog-user-project", a.AccountCredentials.ProjectID) } - req.Set("Authorization", "Bearer "+accessToken) return nil } diff --git a/relay/channel/vertex/relay-vertex.go b/relay/channel/vertex/relay-vertex.go index 5ed876654..f0b84906a 100644 --- a/relay/channel/vertex/relay-vertex.go +++ b/relay/channel/vertex/relay-vertex.go @@ -12,7 +12,10 @@ func GetModelRegion(other string, localModelName string) string { if m[localModelName] != nil { return m[localModelName].(string) } else { - return m["default"].(string) + if v, ok := m["default"]; ok { + return v.(string) + } + return "global" } } return other diff --git a/relay/channel/vertex/service_account.go b/relay/channel/vertex/service_account.go index 9a4650d98..f90d5454d 100644 --- a/relay/channel/vertex/service_account.go +++ b/relay/channel/vertex/service_account.go @@ -6,14 +6,15 @@ import ( "encoding/json" "encoding/pem" "errors" - "github.com/bytedance/gopkg/cache/asynccache" - "github.com/golang-jwt/jwt" "net/http" "net/url" relaycommon "one-api/relay/common" "one-api/service" "strings" + "github.com/bytedance/gopkg/cache/asynccache" + "github.com/golang-jwt/jwt" + "fmt" "time" ) @@ -137,3 +138,45 @@ func exchangeJwtForAccessToken(signedJWT string, info *relaycommon.RelayInfo) (s return "", fmt.Errorf("failed to get access token: %v", result) } + +func AcquireAccessToken(creds Credentials, proxy string) (string, error) { + signedJWT, err := createSignedJWT(creds.ClientEmail, creds.PrivateKey) + if err != nil { + return "", fmt.Errorf("failed to create signed JWT: %w", err) + } + return exchangeJwtForAccessTokenWithProxy(signedJWT, proxy) +} + +func exchangeJwtForAccessTokenWithProxy(signedJWT string, proxy string) (string, error) { + authURL := "https://www.googleapis.com/oauth2/v4/token" + data := url.Values{} + data.Set("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer") + data.Set("assertion", signedJWT) + + var client *http.Client + var err error + if proxy != "" { + client, err = service.NewProxyHttpClient(proxy) + if err != nil { + return "", fmt.Errorf("new proxy http client failed: %w", err) + } + } else { + client = service.GetHttpClient() + } + + resp, err := client.PostForm(authURL, data) + if err != nil { + return "", err + } + defer resp.Body.Close() + + var result map[string]interface{} + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + return "", err + } + + if accessToken, ok := result["access_token"].(string); ok { + return accessToken, nil + } + return "", fmt.Errorf("failed to get access token: %v", result) +} diff --git a/relay/claude_handler.go b/relay/claude_handler.go index 59c052f62..dbdc6ee1c 100644 --- a/relay/claude_handler.go +++ b/relay/claude_handler.go @@ -111,7 +111,7 @@ func ClaudeHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *typ httpResp = resp.(*http.Response) info.IsStream = info.IsStream || strings.HasPrefix(httpResp.Header.Get("Content-Type"), "text/event-stream") if httpResp.StatusCode != http.StatusOK { - newAPIError = service.RelayErrorHandler(httpResp, false) + newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false) // reset status code 重置状态码 service.ResetStatusCode(newAPIError, statusCodeMappingStr) return newAPIError diff --git a/relay/common/override.go b/relay/common/override.go index c8f216ed5..212cf7b47 100644 --- a/relay/common/override.go +++ b/relay/common/override.go @@ -5,6 +5,8 @@ import ( "fmt" "github.com/tidwall/gjson" "github.com/tidwall/sjson" + "regexp" + "strconv" "strings" ) @@ -151,7 +153,9 @@ func checkConditions(jsonStr string, conditions []ConditionOperation, logic stri } func checkSingleCondition(jsonStr string, condition ConditionOperation) (bool, error) { - value := gjson.Get(jsonStr, condition.Path) + // 处理负数索引 + path := processNegativeIndex(jsonStr, condition.Path) + value := gjson.Get(jsonStr, path) if !value.Exists() { if condition.PassMissingKey { return true, nil @@ -177,6 +181,37 @@ func checkSingleCondition(jsonStr string, condition ConditionOperation) (bool, e return result, nil } +func processNegativeIndex(jsonStr string, path string) string { + re := regexp.MustCompile(`\.(-\d+)`) + matches := re.FindAllStringSubmatch(path, -1) + + if len(matches) == 0 { + return path + } + + result := path + for _, match := range matches { + negIndex := match[1] + index, _ := strconv.Atoi(negIndex) + + arrayPath := strings.Split(path, negIndex)[0] + if strings.HasSuffix(arrayPath, ".") { + arrayPath = arrayPath[:len(arrayPath)-1] + } + + array := gjson.Get(jsonStr, arrayPath) + if array.IsArray() { + length := len(array.Array()) + actualIndex := length + index + if actualIndex >= 0 && actualIndex < length { + result = strings.Replace(result, match[0], "."+strconv.Itoa(actualIndex), 1) + } + } + } + + return result +} + // compareGjsonValues 直接比较两个gjson.Result,支持所有比较模式 func compareGjsonValues(jsonValue, targetValue gjson.Result, mode string) (bool, error) { switch mode { @@ -274,21 +309,25 @@ func applyOperations(jsonStr string, operations []ParamOperation) (string, error if !ok { continue // 条件不满足,跳过当前操作 } + // 处理路径中的负数索引 + opPath := processNegativeIndex(result, op.Path) + opFrom := processNegativeIndex(result, op.From) + opTo := processNegativeIndex(result, op.To) switch op.Mode { case "delete": - result, err = sjson.Delete(result, op.Path) + result, err = sjson.Delete(result, opPath) case "set": - if op.KeepOrigin && gjson.Get(result, op.Path).Exists() { + if op.KeepOrigin && gjson.Get(result, opPath).Exists() { continue } - result, err = sjson.Set(result, op.Path, op.Value) + result, err = sjson.Set(result, opPath, op.Value) case "move": - result, err = moveValue(result, op.From, op.To) + result, err = moveValue(result, opFrom, opTo) case "prepend": - result, err = modifyValue(result, op.Path, op.Value, op.KeepOrigin, true) + result, err = modifyValue(result, opPath, op.Value, op.KeepOrigin, true) case "append": - result, err = modifyValue(result, op.Path, op.Value, op.KeepOrigin, false) + result, err = modifyValue(result, opPath, op.Value, op.KeepOrigin, false) default: return "", fmt.Errorf("unknown operation: %s", op.Mode) } diff --git a/relay/common/relay_info.go b/relay/common/relay_info.go index da572c070..99925dc5d 100644 --- a/relay/common/relay_info.go +++ b/relay/common/relay_info.go @@ -481,11 +481,20 @@ type TaskSubmitReq struct { Model string `json:"model,omitempty"` Mode string `json:"mode,omitempty"` Image string `json:"image,omitempty"` + Images []string `json:"images,omitempty"` Size string `json:"size,omitempty"` Duration int `json:"duration,omitempty"` Metadata map[string]interface{} `json:"metadata,omitempty"` } +func (t TaskSubmitReq) GetPrompt() string { + return t.Prompt +} + +func (t TaskSubmitReq) HasImage() bool { + return len(t.Images) > 0 +} + type TaskInfo struct { Code int `json:"code"` TaskID string `json:"task_id"` diff --git a/relay/common/relay_utils.go b/relay/common/relay_utils.go index 290865854..cf6d08dda 100644 --- a/relay/common/relay_utils.go +++ b/relay/common/relay_utils.go @@ -2,14 +2,23 @@ package common import ( "fmt" - "github.com/gin-gonic/gin" - _ "image/gif" - _ "image/jpeg" - _ "image/png" + "net/http" + "one-api/common" "one-api/constant" + "one-api/dto" "strings" + + "github.com/gin-gonic/gin" ) +type HasPrompt interface { + GetPrompt() string +} + +type HasImage interface { + HasImage() bool +} + func GetFullRequestURL(baseURL string, requestURL string, channelType int) string { fullRequestURL := fmt.Sprintf("%s%s", baseURL, requestURL) @@ -32,3 +41,72 @@ func GetAPIVersion(c *gin.Context) string { } return apiVersion } + +func createTaskError(err error, code string, statusCode int, localError bool) *dto.TaskError { + return &dto.TaskError{ + Code: code, + Message: err.Error(), + StatusCode: statusCode, + LocalError: localError, + Error: err, + } +} + +func storeTaskRequest(c *gin.Context, info *RelayInfo, action string, requestObj interface{}) { + info.Action = action + c.Set("task_request", requestObj) +} + +func validatePrompt(prompt string) *dto.TaskError { + if strings.TrimSpace(prompt) == "" { + return createTaskError(fmt.Errorf("prompt is required"), "invalid_request", http.StatusBadRequest, true) + } + return nil +} + +func ValidateBasicTaskRequest(c *gin.Context, info *RelayInfo, action string) *dto.TaskError { + var req TaskSubmitReq + if err := common.UnmarshalBodyReusable(c, &req); err != nil { + return createTaskError(err, "invalid_request", http.StatusBadRequest, true) + } + + if taskErr := validatePrompt(req.Prompt); taskErr != nil { + return taskErr + } + + if len(req.Images) == 0 && strings.TrimSpace(req.Image) != "" { + // 兼容单图上传 + req.Images = []string{req.Image} + } + + storeTaskRequest(c, info, action, req) + return nil +} + +func ValidateTaskRequestWithImage(c *gin.Context, info *RelayInfo, requestObj interface{}) *dto.TaskError { + hasPrompt, ok := requestObj.(HasPrompt) + if !ok { + return createTaskError(fmt.Errorf("request must have prompt"), "invalid_request", http.StatusBadRequest, true) + } + + if taskErr := validatePrompt(hasPrompt.GetPrompt()); taskErr != nil { + return taskErr + } + + action := constant.TaskActionTextGenerate + if hasImage, ok := requestObj.(HasImage); ok && hasImage.HasImage() { + action = constant.TaskActionGenerate + } + + storeTaskRequest(c, info, action, requestObj) + return nil +} + +func ValidateTaskRequestWithImageBinding(c *gin.Context, info *RelayInfo) *dto.TaskError { + var req TaskSubmitReq + if err := c.ShouldBindJSON(&req); err != nil { + return createTaskError(err, "invalid_request_body", http.StatusBadRequest, false) + } + + return ValidateTaskRequestWithImage(c, info, req) +} diff --git a/relay/compatible_handler.go b/relay/compatible_handler.go index 1f6c525b5..01ab1fff4 100644 --- a/relay/compatible_handler.go +++ b/relay/compatible_handler.go @@ -158,7 +158,7 @@ func TextHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *types httpResp = resp.(*http.Response) info.IsStream = info.IsStream || strings.HasPrefix(httpResp.Header.Get("Content-Type"), "text/event-stream") if httpResp.StatusCode != http.StatusOK { - newApiErr := service.RelayErrorHandler(httpResp, false) + newApiErr := service.RelayErrorHandler(c.Request.Context(), httpResp, false) // reset status code 重置状态码 service.ResetStatusCode(newApiErr, statusCodeMappingStr) return newApiErr @@ -195,6 +195,8 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage imageTokens := usage.PromptTokensDetails.ImageTokens audioTokens := usage.PromptTokensDetails.AudioTokens completionTokens := usage.CompletionTokens + cachedCreationTokens := usage.PromptTokensDetails.CachedCreationTokens + modelName := relayInfo.OriginModelName tokenName := ctx.GetString("token_name") @@ -204,6 +206,7 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage modelRatio := relayInfo.PriceData.ModelRatio groupRatio := relayInfo.PriceData.GroupRatioInfo.GroupRatio modelPrice := relayInfo.PriceData.ModelPrice + cachedCreationRatio := relayInfo.PriceData.CacheCreationRatio // Convert values to decimal for precise calculation dPromptTokens := decimal.NewFromInt(int64(promptTokens)) @@ -211,12 +214,14 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage dImageTokens := decimal.NewFromInt(int64(imageTokens)) dAudioTokens := decimal.NewFromInt(int64(audioTokens)) dCompletionTokens := decimal.NewFromInt(int64(completionTokens)) + dCachedCreationTokens := decimal.NewFromInt(int64(cachedCreationTokens)) dCompletionRatio := decimal.NewFromFloat(completionRatio) dCacheRatio := decimal.NewFromFloat(cacheRatio) dImageRatio := decimal.NewFromFloat(imageRatio) dModelRatio := decimal.NewFromFloat(modelRatio) dGroupRatio := decimal.NewFromFloat(groupRatio) dModelPrice := decimal.NewFromFloat(modelPrice) + dCachedCreationRatio := decimal.NewFromFloat(cachedCreationRatio) dQuotaPerUnit := decimal.NewFromFloat(common.QuotaPerUnit) ratio := dModelRatio.Mul(dGroupRatio) @@ -284,6 +289,11 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage baseTokens = baseTokens.Sub(dCacheTokens) cachedTokensWithRatio = dCacheTokens.Mul(dCacheRatio) } + var dCachedCreationTokensWithRatio decimal.Decimal + if !dCachedCreationTokens.IsZero() { + baseTokens = baseTokens.Sub(dCachedCreationTokens) + dCachedCreationTokensWithRatio = dCachedCreationTokens.Mul(dCachedCreationRatio) + } // 减去 image tokens var imageTokensWithRatio decimal.Decimal @@ -302,7 +312,9 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage extraContent += fmt.Sprintf("Audio Input 花费 %s", audioInputQuota.String()) } } - promptQuota := baseTokens.Add(cachedTokensWithRatio).Add(imageTokensWithRatio) + promptQuota := baseTokens.Add(cachedTokensWithRatio). + Add(imageTokensWithRatio). + Add(dCachedCreationTokensWithRatio) completionQuota := dCompletionTokens.Mul(dCompletionRatio) @@ -384,6 +396,10 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage other["image_ratio"] = imageRatio other["image_output"] = imageTokens } + if cachedCreationTokens != 0 { + other["cache_creation_tokens"] = cachedCreationTokens + other["cache_creation_ratio"] = cachedCreationRatio + } if !dWebSearchQuota.IsZero() { if relayInfo.ResponsesUsageInfo != nil { if webSearchTool, exists := relayInfo.ResponsesUsageInfo.BuiltInTools[dto.BuildInToolWebSearchPreview]; exists { diff --git a/relay/embedding_handler.go b/relay/embedding_handler.go index 26dcf9719..3d8962bb4 100644 --- a/relay/embedding_handler.go +++ b/relay/embedding_handler.go @@ -58,7 +58,7 @@ func EmbeddingHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError * if resp != nil { httpResp = resp.(*http.Response) if httpResp.StatusCode != http.StatusOK { - newAPIError = service.RelayErrorHandler(httpResp, false) + newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false) // reset status code 重置状态码 service.ResetStatusCode(newAPIError, statusCodeMappingStr) return newAPIError diff --git a/relay/gemini_handler.go b/relay/gemini_handler.go index 460fd2f58..0252d6578 100644 --- a/relay/gemini_handler.go +++ b/relay/gemini_handler.go @@ -152,7 +152,7 @@ func GeminiHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *typ httpResp = resp.(*http.Response) info.IsStream = info.IsStream || strings.HasPrefix(httpResp.Header.Get("Content-Type"), "text/event-stream") if httpResp.StatusCode != http.StatusOK { - newAPIError = service.RelayErrorHandler(httpResp, false) + newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false) // reset status code 重置状态码 service.ResetStatusCode(newAPIError, statusCodeMappingStr) return newAPIError @@ -249,7 +249,7 @@ func GeminiEmbeddingHandler(c *gin.Context, info *relaycommon.RelayInfo) (newAPI if resp != nil { httpResp = resp.(*http.Response) if httpResp.StatusCode != http.StatusOK { - newAPIError = service.RelayErrorHandler(httpResp, false) + newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false) service.ResetStatusCode(newAPIError, statusCodeMappingStr) return newAPIError } diff --git a/relay/image_handler.go b/relay/image_handler.go index 14a7103c3..9c873d47f 100644 --- a/relay/image_handler.go +++ b/relay/image_handler.go @@ -91,7 +91,7 @@ func ImageHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *type httpResp = resp.(*http.Response) info.IsStream = info.IsStream || strings.HasPrefix(httpResp.Header.Get("Content-Type"), "text/event-stream") if httpResp.StatusCode != http.StatusOK { - newAPIError = service.RelayErrorHandler(httpResp, false) + newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false) // reset status code 重置状态码 service.ResetStatusCode(newAPIError, statusCodeMappingStr) return newAPIError @@ -120,7 +120,7 @@ func ImageHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *type var logContent string if len(request.Size) > 0 { - logContent = fmt.Sprintf("大小 %s, 品质 %s", request.Size, quality) + logContent = fmt.Sprintf("大小 %s, 品质 %s, 张数 %d", request.Size, quality, request.N) } postConsumeQuota(c, info, usage.(*dto.Usage), logContent) diff --git a/relay/mjproxy_handler.go b/relay/mjproxy_handler.go index 7c52cb6be..ec8dfc6b2 100644 --- a/relay/mjproxy_handler.go +++ b/relay/mjproxy_handler.go @@ -16,6 +16,7 @@ import ( "one-api/relay/helper" "one-api/service" "one-api/setting" + "one-api/setting/system_setting" "strconv" "strings" "time" @@ -131,7 +132,7 @@ func coverMidjourneyTaskDto(c *gin.Context, originTask *model.Midjourney) (midjo midjourneyTask.FinishTime = originTask.FinishTime midjourneyTask.ImageUrl = "" if originTask.ImageUrl != "" && setting.MjForwardUrlEnabled { - midjourneyTask.ImageUrl = setting.ServerAddress + "/mj/image/" + originTask.MjId + midjourneyTask.ImageUrl = system_setting.ServerAddress + "/mj/image/" + originTask.MjId if originTask.Status != "SUCCESS" { midjourneyTask.ImageUrl += "?rand=" + strconv.FormatInt(time.Now().UnixNano(), 10) } diff --git a/relay/relay_adaptor.go b/relay/relay_adaptor.go index 1ee85986c..0c271210b 100644 --- a/relay/relay_adaptor.go +++ b/relay/relay_adaptor.go @@ -1,7 +1,6 @@ package relay import ( - "github.com/gin-gonic/gin" "one-api/constant" "one-api/relay/channel" "one-api/relay/channel/ali" @@ -28,6 +27,7 @@ import ( taskjimeng "one-api/relay/channel/task/jimeng" "one-api/relay/channel/task/kling" "one-api/relay/channel/task/suno" + taskvertex "one-api/relay/channel/task/vertex" taskVidu "one-api/relay/channel/task/vidu" "one-api/relay/channel/tencent" "one-api/relay/channel/vertex" @@ -37,6 +37,8 @@ import ( "one-api/relay/channel/zhipu" "one-api/relay/channel/zhipu_4v" "strconv" + + "github.com/gin-gonic/gin" ) func GetAdaptor(apiType int) channel.Adaptor { @@ -126,6 +128,8 @@ func GetTaskAdaptor(platform constant.TaskPlatform) channel.TaskAdaptor { return &kling.TaskAdaptor{} case constant.ChannelTypeJimeng: return &taskjimeng.TaskAdaptor{} + case constant.ChannelTypeVertexAi: + return &taskvertex.TaskAdaptor{} case constant.ChannelTypeVidu: return &taskVidu.TaskAdaptor{} } diff --git a/relay/relay_task.go b/relay/relay_task.go index 0754e0234..9cb8cd5c8 100644 --- a/relay/relay_task.go +++ b/relay/relay_task.go @@ -15,6 +15,8 @@ import ( relayconstant "one-api/relay/constant" "one-api/service" "one-api/setting/ratio_setting" + "strconv" + "strings" "github.com/gin-gonic/gin" ) @@ -33,6 +35,7 @@ func RelayTaskSubmit(c *gin.Context, info *relaycommon.RelayInfo) (taskErr *dto. platform = GetTaskPlatform(c) } + info.InitChannelMeta(c) adaptor := GetTaskAdaptor(platform) if adaptor == nil { return service.TaskErrorWrapperLocal(fmt.Errorf("invalid api platform: %s", platform), "invalid_api_platform", http.StatusBadRequest) @@ -197,6 +200,9 @@ func RelayTaskFetch(c *gin.Context, relayMode int) (taskResp *dto.TaskError) { if taskErr != nil { return taskErr } + if len(respBody) == 0 { + respBody = []byte("{\"code\":\"success\",\"data\":null}") + } c.Writer.Header().Set("Content-Type", "application/json") _, err := io.Copy(c.Writer, bytes.NewBuffer(respBody)) @@ -276,10 +282,92 @@ func videoFetchByIDRespBodyBuilder(c *gin.Context) (respBody []byte, taskResp *d return } - respBody, err = json.Marshal(dto.TaskResponse[any]{ - Code: "success", - Data: TaskModel2Dto(originTask), - }) + func() { + channelModel, err2 := model.GetChannelById(originTask.ChannelId, true) + if err2 != nil { + return + } + if channelModel.Type != constant.ChannelTypeVertexAi { + return + } + baseURL := constant.ChannelBaseURLs[channelModel.Type] + if channelModel.GetBaseURL() != "" { + baseURL = channelModel.GetBaseURL() + } + adaptor := GetTaskAdaptor(constant.TaskPlatform(strconv.Itoa(channelModel.Type))) + if adaptor == nil { + return + } + resp, err2 := adaptor.FetchTask(baseURL, channelModel.Key, map[string]any{ + "task_id": originTask.TaskID, + "action": originTask.Action, + }) + if err2 != nil || resp == nil { + return + } + defer resp.Body.Close() + body, err2 := io.ReadAll(resp.Body) + if err2 != nil { + return + } + ti, err2 := adaptor.ParseTaskResult(body) + if err2 == nil && ti != nil { + if ti.Status != "" { + originTask.Status = model.TaskStatus(ti.Status) + } + if ti.Progress != "" { + originTask.Progress = ti.Progress + } + if ti.Url != "" { + originTask.FailReason = ti.Url + } + _ = originTask.Update() + var raw map[string]any + _ = json.Unmarshal(body, &raw) + format := "mp4" + if respObj, ok := raw["response"].(map[string]any); ok { + if vids, ok := respObj["videos"].([]any); ok && len(vids) > 0 { + if v0, ok := vids[0].(map[string]any); ok { + if mt, ok := v0["mimeType"].(string); ok && mt != "" { + if strings.Contains(mt, "mp4") { + format = "mp4" + } else { + format = mt + } + } + } + } + } + status := "processing" + switch originTask.Status { + case model.TaskStatusSuccess: + status = "succeeded" + case model.TaskStatusFailure: + status = "failed" + case model.TaskStatusQueued, model.TaskStatusSubmitted: + status = "queued" + } + out := map[string]any{ + "error": nil, + "format": format, + "metadata": nil, + "status": status, + "task_id": originTask.TaskID, + "url": originTask.FailReason, + } + respBody, _ = json.Marshal(dto.TaskResponse[any]{ + Code: "success", + Data: out, + }) + } + }() + + if len(respBody) == 0 { + respBody, err = json.Marshal(dto.TaskResponse[any]{ + Code: "success", + Data: TaskModel2Dto(originTask), + }) + } return } diff --git a/relay/rerank_handler.go b/relay/rerank_handler.go index fa3c7bbb4..46d2e25f6 100644 --- a/relay/rerank_handler.go +++ b/relay/rerank_handler.go @@ -81,7 +81,7 @@ func RerankHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError *typ if resp != nil { httpResp = resp.(*http.Response) if httpResp.StatusCode != http.StatusOK { - newAPIError = service.RelayErrorHandler(httpResp, false) + newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false) // reset status code 重置状态码 service.ResetStatusCode(newAPIError, statusCodeMappingStr) return newAPIError diff --git a/relay/responses_handler.go b/relay/responses_handler.go index f5f624c92..0c57a303f 100644 --- a/relay/responses_handler.go +++ b/relay/responses_handler.go @@ -41,7 +41,7 @@ func ResponsesHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError * } adaptor.Init(info) var requestBody io.Reader - if model_setting.GetGlobalSettings().PassThroughRequestEnabled { + if model_setting.GetGlobalSettings().PassThroughRequestEnabled || info.ChannelSetting.PassThroughBodyEnabled { body, err := common.GetRequestBody(c) if err != nil { return types.NewError(err, types.ErrorCodeReadRequestBodyFailed, types.ErrOptionWithSkipRetry()) @@ -82,7 +82,7 @@ func ResponsesHelper(c *gin.Context, info *relaycommon.RelayInfo) (newAPIError * httpResp = resp.(*http.Response) if httpResp.StatusCode != http.StatusOK { - newAPIError = service.RelayErrorHandler(httpResp, false) + newAPIError = service.RelayErrorHandler(c.Request.Context(), httpResp, false) // reset status code 重置状态码 service.ResetStatusCode(newAPIError, statusCodeMappingStr) return newAPIError diff --git a/router/api-router.go b/router/api-router.go index 311bb0a4b..e16d06628 100644 --- a/router/api-router.go +++ b/router/api-router.go @@ -60,6 +60,7 @@ func SetApiRouter(router *gin.Engine) { selfRoute.DELETE("/self", controller.DeleteSelf) selfRoute.GET("/token", controller.GenerateAccessToken) selfRoute.GET("/aff", controller.GetAffCode) + selfRoute.GET("/topup/info", controller.GetTopUpInfo) selfRoute.POST("/topup", middleware.CriticalRateLimit(), controller.TopUp) selfRoute.POST("/pay", middleware.CriticalRateLimit(), controller.RequestEpay) selfRoute.POST("/amount", controller.RequestAmount) @@ -224,6 +225,8 @@ func SetApiRouter(router *gin.Engine) { modelsRoute := apiRouter.Group("/models") modelsRoute.Use(middleware.AdminAuth()) { + modelsRoute.GET("/sync_upstream/preview", controller.SyncUpstreamPreview) + modelsRoute.POST("/sync_upstream", controller.SyncUpstreamModels) modelsRoute.GET("/missing", controller.GetMissingModels) modelsRoute.GET("/", controller.GetAllModelsMeta) modelsRoute.GET("/search", controller.SearchModelsMeta) diff --git a/service/epay.go b/service/epay.go index a8259d21d..48b84dd58 100644 --- a/service/epay.go +++ b/service/epay.go @@ -1,12 +1,13 @@ package service import ( - "one-api/setting" + "one-api/setting/operation_setting" + "one-api/setting/system_setting" ) func GetCallbackAddress() string { - if setting.CustomCallbackAddress == "" { - return setting.ServerAddress + if operation_setting.CustomCallbackAddress == "" { + return system_setting.ServerAddress } - return setting.CustomCallbackAddress + return operation_setting.CustomCallbackAddress } diff --git a/service/error.go b/service/error.go index ef5cbbde6..5c3bddd6e 100644 --- a/service/error.go +++ b/service/error.go @@ -1,12 +1,14 @@ package service import ( + "context" "errors" "fmt" "io" "net/http" "one-api/common" "one-api/dto" + "one-api/logger" "one-api/types" "strconv" "strings" @@ -78,7 +80,7 @@ func ClaudeErrorWrapperLocal(err error, code string, statusCode int) *dto.Claude return claudeErr } -func RelayErrorHandler(resp *http.Response, showBodyWhenFail bool) (newApiErr *types.NewAPIError) { +func RelayErrorHandler(ctx context.Context, resp *http.Response, showBodyWhenFail bool) (newApiErr *types.NewAPIError) { newApiErr = types.InitOpenAIError(types.ErrorCodeBadResponseStatusCode, resp.StatusCode) responseBody, err := io.ReadAll(resp.Body) @@ -94,7 +96,7 @@ func RelayErrorHandler(resp *http.Response, showBodyWhenFail bool) (newApiErr *t newApiErr.Err = fmt.Errorf("bad response status code %d, body: %s", resp.StatusCode, string(responseBody)) } else { if common.DebugEnabled { - println(fmt.Sprintf("bad response status code %d, body: %s", resp.StatusCode, string(responseBody))) + logger.LogInfo(ctx, fmt.Sprintf("bad response status code %d, body: %s", resp.StatusCode, string(responseBody))) } newApiErr.Err = fmt.Errorf("bad response status code %d", resp.StatusCode) } diff --git a/service/file_decoder.go b/service/file_decoder.go index 94f3f0282..99fdc3f9a 100644 --- a/service/file_decoder.go +++ b/service/file_decoder.go @@ -5,6 +5,9 @@ import ( "encoding/base64" "fmt" "image" + _ "image/gif" + _ "image/jpeg" + _ "image/png" "io" "net/http" "one-api/common" diff --git a/service/pre_consume_quota.go b/service/pre_consume_quota.go index 86b04e526..0cf53513b 100644 --- a/service/pre_consume_quota.go +++ b/service/pre_consume_quota.go @@ -13,13 +13,13 @@ import ( "github.com/gin-gonic/gin" ) -func ReturnPreConsumedQuota(c *gin.Context, relayInfo *relaycommon.RelayInfo, preConsumedQuota int) { - if preConsumedQuota != 0 { - logger.LogInfo(c, fmt.Sprintf("用户 %d 请求失败, 返还预扣费额度 %s", relayInfo.UserId, logger.FormatQuota(preConsumedQuota))) +func ReturnPreConsumedQuota(c *gin.Context, relayInfo *relaycommon.RelayInfo) { + if relayInfo.FinalPreConsumedQuota != 0 { + logger.LogInfo(c, fmt.Sprintf("用户 %d 请求失败, 返还预扣费额度 %s", relayInfo.UserId, logger.FormatQuota(relayInfo.FinalPreConsumedQuota))) gopool.Go(func() { relayInfoCopy := *relayInfo - err := PostConsumeQuota(&relayInfoCopy, -preConsumedQuota, 0, false) + err := PostConsumeQuota(&relayInfoCopy, -relayInfoCopy.FinalPreConsumedQuota, 0, false) if err != nil { common.SysLog("error return pre-consumed quota: " + err.Error()) } @@ -29,16 +29,16 @@ func ReturnPreConsumedQuota(c *gin.Context, relayInfo *relaycommon.RelayInfo, pr // PreConsumeQuota checks if the user has enough quota to pre-consume. // It returns the pre-consumed quota if successful, or an error if not. -func PreConsumeQuota(c *gin.Context, preConsumedQuota int, relayInfo *relaycommon.RelayInfo) (int, *types.NewAPIError) { +func PreConsumeQuota(c *gin.Context, preConsumedQuota int, relayInfo *relaycommon.RelayInfo) *types.NewAPIError { userQuota, err := model.GetUserQuota(relayInfo.UserId, false) if err != nil { - return 0, types.NewError(err, types.ErrorCodeQueryDataError, types.ErrOptionWithSkipRetry()) + return types.NewError(err, types.ErrorCodeQueryDataError, types.ErrOptionWithSkipRetry()) } if userQuota <= 0 { - return 0, types.NewErrorWithStatusCode(fmt.Errorf("用户额度不足, 剩余额度: %s", logger.FormatQuota(userQuota)), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog()) + return types.NewErrorWithStatusCode(fmt.Errorf("用户额度不足, 剩余额度: %s", logger.FormatQuota(userQuota)), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog()) } if userQuota-preConsumedQuota < 0 { - return 0, types.NewErrorWithStatusCode(fmt.Errorf("预扣费额度失败, 用户剩余额度: %s, 需要预扣费额度: %s", logger.FormatQuota(userQuota), logger.FormatQuota(preConsumedQuota)), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog()) + return types.NewErrorWithStatusCode(fmt.Errorf("预扣费额度失败, 用户剩余额度: %s, 需要预扣费额度: %s", logger.FormatQuota(userQuota), logger.FormatQuota(preConsumedQuota)), types.ErrorCodeInsufficientUserQuota, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog()) } trustQuota := common.GetTrustQuota() @@ -65,14 +65,14 @@ func PreConsumeQuota(c *gin.Context, preConsumedQuota int, relayInfo *relaycommo if preConsumedQuota > 0 { err := PreConsumeTokenQuota(relayInfo, preConsumedQuota) if err != nil { - return 0, types.NewErrorWithStatusCode(err, types.ErrorCodePreConsumeTokenQuotaFailed, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog()) + return types.NewErrorWithStatusCode(err, types.ErrorCodePreConsumeTokenQuotaFailed, http.StatusForbidden, types.ErrOptionWithSkipRetry(), types.ErrOptionWithNoRecordErrorLog()) } err = model.DecreaseUserQuota(relayInfo.UserId, preConsumedQuota) if err != nil { - return 0, types.NewError(err, types.ErrorCodeUpdateDataError, types.ErrOptionWithSkipRetry()) + return types.NewError(err, types.ErrorCodeUpdateDataError, types.ErrOptionWithSkipRetry()) } logger.LogInfo(c, fmt.Sprintf("用户 %d 预扣费 %s, 预扣费后剩余额度: %s", relayInfo.UserId, logger.FormatQuota(preConsumedQuota), logger.FormatQuota(userQuota-preConsumedQuota))) } relayInfo.FinalPreConsumedQuota = preConsumedQuota - return preConsumedQuota, nil + return nil } diff --git a/service/quota.go b/service/quota.go index 8f65bd20e..12017e11e 100644 --- a/service/quota.go +++ b/service/quota.go @@ -11,8 +11,8 @@ import ( "one-api/logger" "one-api/model" relaycommon "one-api/relay/common" - "one-api/setting" "one-api/setting/ratio_setting" + "one-api/setting/system_setting" "one-api/types" "strings" "time" @@ -534,9 +534,28 @@ func checkAndSendQuotaNotify(relayInfo *relaycommon.RelayInfo, quota int, preCon } if quotaTooLow { prompt := "您的额度即将用尽" - topUpLink := fmt.Sprintf("%s/topup", setting.ServerAddress) - content := "{{value}},当前剩余额度为 {{value}},为了不影响您的使用,请及时充值。
充值链接:{{value}}" - err := NotifyUser(relayInfo.UserId, relayInfo.UserEmail, relayInfo.UserSetting, dto.NewNotify(dto.NotifyTypeQuotaExceed, prompt, content, []interface{}{prompt, logger.FormatQuota(relayInfo.UserQuota), topUpLink, topUpLink})) + topUpLink := fmt.Sprintf("%s/topup", system_setting.ServerAddress) + + // 根据通知方式生成不同的内容格式 + var content string + var values []interface{} + + notifyType := userSetting.NotifyType + if notifyType == "" { + notifyType = dto.NotifyTypeEmail + } + + if notifyType == dto.NotifyTypeBark { + // Bark推送使用简短文本,不支持HTML + content = "{{value}},剩余额度:{{value}},请及时充值" + values = []interface{}{prompt, logger.FormatQuota(relayInfo.UserQuota)} + } else { + // 默认内容格式,适用于Email和Webhook + content = "{{value}},当前剩余额度为 {{value}},为了不影响您的使用,请及时充值。
充值链接:{{value}}" + values = []interface{}{prompt, logger.FormatQuota(relayInfo.UserQuota), topUpLink, topUpLink} + } + + err := NotifyUser(relayInfo.UserId, relayInfo.UserEmail, relayInfo.UserSetting, dto.NewNotify(dto.NotifyTypeQuotaExceed, prompt, content, values)) if err != nil { common.SysError(fmt.Sprintf("failed to send quota notify to user %d: %s", relayInfo.UserId, err.Error())) } diff --git a/service/token_counter.go b/service/token_counter.go index b7dd81f52..be5c2e80c 100644 --- a/service/token_counter.go +++ b/service/token_counter.go @@ -5,6 +5,9 @@ import ( "errors" "fmt" "image" + _ "image/gif" + _ "image/jpeg" + _ "image/png" "log" "math" "one-api/common" @@ -357,33 +360,6 @@ func CountRequestToken(c *gin.Context, meta *types.TokenCountMeta, info *relayco return tkm, nil } -//func CountTokenChatRequest(info *relaycommon.RelayInfo, request dto.GeneralOpenAIRequest) (int, error) { -// tkm := 0 -// msgTokens, err := CountTokenMessages(info, request.Messages, request.Model, request.Stream) -// if err != nil { -// return 0, err -// } -// tkm += msgTokens -// if request.Tools != nil { -// openaiTools := request.Tools -// countStr := "" -// for _, tool := range openaiTools { -// countStr = tool.Function.Name -// if tool.Function.Description != "" { -// countStr += tool.Function.Description -// } -// if tool.Function.Parameters != nil { -// countStr += fmt.Sprintf("%v", tool.Function.Parameters) -// } -// } -// toolTokens := CountTokenInput(countStr, request.Model) -// tkm += 8 -// tkm += toolTokens -// } -// -// return tkm, nil -//} - func CountTokenClaudeRequest(request dto.ClaudeRequest, model string) (int, error) { tkm := 0 @@ -543,56 +519,6 @@ func CountTokenRealtime(info *relaycommon.RelayInfo, request dto.RealtimeEvent, return textToken, audioToken, nil } -//func CountTokenMessages(info *relaycommon.RelayInfo, messages []dto.Message, model string, stream bool) (int, error) { -// //recover when panic -// tokenEncoder := getTokenEncoder(model) -// // Reference: -// // https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb -// // https://github.com/pkoukk/tiktoken-go/issues/6 -// // -// // Every message follows <|start|>{role/name}\n{content}<|end|>\n -// var tokensPerMessage int -// var tokensPerName int -// -// tokensPerMessage = 3 -// tokensPerName = 1 -// -// tokenNum := 0 -// for _, message := range messages { -// tokenNum += tokensPerMessage -// tokenNum += getTokenNum(tokenEncoder, message.Role) -// if message.Content != nil { -// if message.Name != nil { -// tokenNum += tokensPerName -// tokenNum += getTokenNum(tokenEncoder, *message.Name) -// } -// arrayContent := message.ParseContent() -// for _, m := range arrayContent { -// if m.Type == dto.ContentTypeImageURL { -// imageUrl := m.GetImageMedia() -// imageTokenNum, err := getImageToken(info, imageUrl, model, stream) -// if err != nil { -// return 0, err -// } -// tokenNum += imageTokenNum -// log.Printf("image token num: %d", imageTokenNum) -// } else if m.Type == dto.ContentTypeInputAudio { -// // TODO: 音频token数量计算 -// tokenNum += 100 -// } else if m.Type == dto.ContentTypeFile { -// tokenNum += 5000 -// } else if m.Type == dto.ContentTypeVideoUrl { -// tokenNum += 5000 -// } else { -// tokenNum += getTokenNum(tokenEncoder, m.Text) -// } -// } -// } -// } -// tokenNum += 3 // Every reply is primed with <|start|>assistant<|message|> -// return tokenNum, nil -//} - func CountTokenInput(input any, model string) int { switch v := input.(type) { case string: diff --git a/service/user_notify.go b/service/user_notify.go index 7c864a1b1..c4a3ea91f 100644 --- a/service/user_notify.go +++ b/service/user_notify.go @@ -2,9 +2,12 @@ package service import ( "fmt" + "net/http" + "net/url" "one-api/common" "one-api/dto" "one-api/model" + "one-api/setting" "strings" ) @@ -51,6 +54,13 @@ func NotifyUser(userId int, userEmail string, userSetting dto.UserSetting, data // 获取 webhook secret webhookSecret := userSetting.WebhookSecret return SendWebhookNotify(webhookURLStr, webhookSecret, data) + case dto.NotifyTypeBark: + barkURL := userSetting.BarkUrl + if barkURL == "" { + common.SysLog(fmt.Sprintf("user %d has no bark url, skip sending bark", userId)) + return nil + } + return sendBarkNotify(barkURL, data) } return nil } @@ -64,3 +74,67 @@ func sendEmailNotify(userEmail string, data dto.Notify) error { } return common.SendEmail(data.Title, userEmail, content) } + +func sendBarkNotify(barkURL string, data dto.Notify) error { + // 处理占位符 + content := data.Content + for _, value := range data.Values { + content = strings.Replace(content, dto.ContentValueParam, fmt.Sprintf("%v", value), 1) + } + + // 替换模板变量 + finalURL := strings.ReplaceAll(barkURL, "{{title}}", url.QueryEscape(data.Title)) + finalURL = strings.ReplaceAll(finalURL, "{{content}}", url.QueryEscape(content)) + + // 发送GET请求到Bark + var req *http.Request + var resp *http.Response + var err error + + if setting.EnableWorker() { + // 使用worker发送请求 + workerReq := &WorkerRequest{ + URL: finalURL, + Key: setting.WorkerValidKey, + Method: http.MethodGet, + Headers: map[string]string{ + "User-Agent": "OneAPI-Bark-Notify/1.0", + }, + } + + resp, err = DoWorkerRequest(workerReq) + if err != nil { + return fmt.Errorf("failed to send bark request through worker: %v", err) + } + defer resp.Body.Close() + + // 检查响应状态 + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return fmt.Errorf("bark request failed with status code: %d", resp.StatusCode) + } + } else { + // 直接发送请求 + req, err = http.NewRequest(http.MethodGet, finalURL, nil) + if err != nil { + return fmt.Errorf("failed to create bark request: %v", err) + } + + // 设置User-Agent + req.Header.Set("User-Agent", "OneAPI-Bark-Notify/1.0") + + // 发送请求 + client := GetHttpClient() + resp, err = client.Do(req) + if err != nil { + return fmt.Errorf("failed to send bark request: %v", err) + } + defer resp.Body.Close() + + // 检查响应状态 + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return fmt.Errorf("bark request failed with status code: %d", resp.StatusCode) + } + } + + return nil +} diff --git a/setting/console_setting/config.go b/setting/console_setting/config.go index 6327e5584..8cfcd0ed6 100644 --- a/setting/console_setting/config.go +++ b/setting/console_setting/config.go @@ -3,37 +3,37 @@ package console_setting import "one-api/setting/config" type ConsoleSetting struct { - ApiInfo string `json:"api_info"` // 控制台 API 信息 (JSON 数组字符串) - UptimeKumaGroups string `json:"uptime_kuma_groups"` // Uptime Kuma 分组配置 (JSON 数组字符串) - Announcements string `json:"announcements"` // 系统公告 (JSON 数组字符串) - FAQ string `json:"faq"` // 常见问题 (JSON 数组字符串) - ApiInfoEnabled bool `json:"api_info_enabled"` // 是否启用 API 信息面板 - UptimeKumaEnabled bool `json:"uptime_kuma_enabled"` // 是否启用 Uptime Kuma 面板 - AnnouncementsEnabled bool `json:"announcements_enabled"` // 是否启用系统公告面板 - FAQEnabled bool `json:"faq_enabled"` // 是否启用常见问答面板 + ApiInfo string `json:"api_info"` // 控制台 API 信息 (JSON 数组字符串) + UptimeKumaGroups string `json:"uptime_kuma_groups"` // Uptime Kuma 分组配置 (JSON 数组字符串) + Announcements string `json:"announcements"` // 系统公告 (JSON 数组字符串) + FAQ string `json:"faq"` // 常见问题 (JSON 数组字符串) + ApiInfoEnabled bool `json:"api_info_enabled"` // 是否启用 API 信息面板 + UptimeKumaEnabled bool `json:"uptime_kuma_enabled"` // 是否启用 Uptime Kuma 面板 + AnnouncementsEnabled bool `json:"announcements_enabled"` // 是否启用系统公告面板 + FAQEnabled bool `json:"faq_enabled"` // 是否启用常见问答面板 } // 默认配置 var defaultConsoleSetting = ConsoleSetting{ - ApiInfo: "", - UptimeKumaGroups: "", - Announcements: "", - FAQ: "", - ApiInfoEnabled: true, - UptimeKumaEnabled: true, - AnnouncementsEnabled: true, - FAQEnabled: true, + ApiInfo: "", + UptimeKumaGroups: "", + Announcements: "", + FAQ: "", + ApiInfoEnabled: true, + UptimeKumaEnabled: true, + AnnouncementsEnabled: true, + FAQEnabled: true, } // 全局实例 var consoleSetting = defaultConsoleSetting func init() { - // 注册到全局配置管理器,键名为 console_setting - config.GlobalConfig.Register("console_setting", &consoleSetting) + // 注册到全局配置管理器,键名为 console_setting + config.GlobalConfig.Register("console_setting", &consoleSetting) } // GetConsoleSetting 获取 ConsoleSetting 配置实例 func GetConsoleSetting() *ConsoleSetting { - return &consoleSetting -} \ No newline at end of file + return &consoleSetting +} diff --git a/setting/console_setting/validation.go b/setting/console_setting/validation.go index fda6453df..529457761 100644 --- a/setting/console_setting/validation.go +++ b/setting/console_setting/validation.go @@ -1,304 +1,304 @@ package console_setting import ( - "encoding/json" - "fmt" - "net/url" - "regexp" - "strings" - "time" - "sort" + "encoding/json" + "fmt" + "net/url" + "regexp" + "sort" + "strings" + "time" ) var ( - urlRegex = regexp.MustCompile(`^https?://(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:\:[0-9]{1,5})?(?:/.*)?$`) - dangerousChars = []string{" 50 { - return fmt.Errorf("API信息数量不能超过50个") - } + if len(apiInfoList) > 50 { + return fmt.Errorf("API信息数量不能超过50个") + } - for i, apiInfo := range apiInfoList { - urlStr, ok := apiInfo["url"].(string) - if !ok || urlStr == "" { - return fmt.Errorf("第%d个API信息缺少URL字段", i+1) - } - route, ok := apiInfo["route"].(string) - if !ok || route == "" { - return fmt.Errorf("第%d个API信息缺少线路描述字段", i+1) - } - description, ok := apiInfo["description"].(string) - if !ok || description == "" { - return fmt.Errorf("第%d个API信息缺少说明字段", i+1) - } - color, ok := apiInfo["color"].(string) - if !ok || color == "" { - return fmt.Errorf("第%d个API信息缺少颜色字段", i+1) - } + for i, apiInfo := range apiInfoList { + urlStr, ok := apiInfo["url"].(string) + if !ok || urlStr == "" { + return fmt.Errorf("第%d个API信息缺少URL字段", i+1) + } + route, ok := apiInfo["route"].(string) + if !ok || route == "" { + return fmt.Errorf("第%d个API信息缺少线路描述字段", i+1) + } + description, ok := apiInfo["description"].(string) + if !ok || description == "" { + return fmt.Errorf("第%d个API信息缺少说明字段", i+1) + } + color, ok := apiInfo["color"].(string) + if !ok || color == "" { + return fmt.Errorf("第%d个API信息缺少颜色字段", i+1) + } - if err := validateURL(urlStr, i+1, "API信息"); err != nil { - return err - } + if err := validateURL(urlStr, i+1, "API信息"); err != nil { + return err + } - if len(urlStr) > 500 { - return fmt.Errorf("第%d个API信息的URL长度不能超过500字符", i+1) - } - if len(route) > 100 { - return fmt.Errorf("第%d个API信息的线路描述长度不能超过100字符", i+1) - } - if len(description) > 200 { - return fmt.Errorf("第%d个API信息的说明长度不能超过200字符", i+1) - } + if len(urlStr) > 500 { + return fmt.Errorf("第%d个API信息的URL长度不能超过500字符", i+1) + } + if len(route) > 100 { + return fmt.Errorf("第%d个API信息的线路描述长度不能超过100字符", i+1) + } + if len(description) > 200 { + return fmt.Errorf("第%d个API信息的说明长度不能超过200字符", i+1) + } - if !validColors[color] { - return fmt.Errorf("第%d个API信息的颜色值不合法", i+1) - } + if !validColors[color] { + return fmt.Errorf("第%d个API信息的颜色值不合法", i+1) + } - if err := checkDangerousContent(description, i+1, "API信息"); err != nil { - return err - } - if err := checkDangerousContent(route, i+1, "API信息"); err != nil { - return err - } - } - return nil + if err := checkDangerousContent(description, i+1, "API信息"); err != nil { + return err + } + if err := checkDangerousContent(route, i+1, "API信息"); err != nil { + return err + } + } + return nil } func GetApiInfo() []map[string]interface{} { - return getJSONList(GetConsoleSetting().ApiInfo) + return getJSONList(GetConsoleSetting().ApiInfo) } func validateAnnouncements(announcementsStr string) error { - list, err := parseJSONArray(announcementsStr, "系统公告") - if err != nil { - return err - } - if len(list) > 100 { - return fmt.Errorf("系统公告数量不能超过100个") - } - validTypes := map[string]bool{ - "default": true, "ongoing": true, "success": true, "warning": true, "error": true, - } - for i, ann := range list { - content, ok := ann["content"].(string) - if !ok || content == "" { - return fmt.Errorf("第%d个公告缺少内容字段", i+1) - } - publishDateAny, exists := ann["publishDate"] - if !exists { - return fmt.Errorf("第%d个公告缺少发布日期字段", i+1) - } - publishDateStr, ok := publishDateAny.(string) - if !ok || publishDateStr == "" { - return fmt.Errorf("第%d个公告的发布日期不能为空", i+1) - } - if _, err := time.Parse(time.RFC3339, publishDateStr); err != nil { - return fmt.Errorf("第%d个公告的发布日期格式错误", i+1) - } - if t, exists := ann["type"]; exists { - if typeStr, ok := t.(string); ok { - if !validTypes[typeStr] { - return fmt.Errorf("第%d个公告的类型值不合法", i+1) - } - } - } - if len(content) > 500 { - return fmt.Errorf("第%d个公告的内容长度不能超过500字符", i+1) - } - if extra, exists := ann["extra"]; exists { - if extraStr, ok := extra.(string); ok && len(extraStr) > 200 { - return fmt.Errorf("第%d个公告的说明长度不能超过200字符", i+1) - } - } - } - return nil + list, err := parseJSONArray(announcementsStr, "系统公告") + if err != nil { + return err + } + if len(list) > 100 { + return fmt.Errorf("系统公告数量不能超过100个") + } + validTypes := map[string]bool{ + "default": true, "ongoing": true, "success": true, "warning": true, "error": true, + } + for i, ann := range list { + content, ok := ann["content"].(string) + if !ok || content == "" { + return fmt.Errorf("第%d个公告缺少内容字段", i+1) + } + publishDateAny, exists := ann["publishDate"] + if !exists { + return fmt.Errorf("第%d个公告缺少发布日期字段", i+1) + } + publishDateStr, ok := publishDateAny.(string) + if !ok || publishDateStr == "" { + return fmt.Errorf("第%d个公告的发布日期不能为空", i+1) + } + if _, err := time.Parse(time.RFC3339, publishDateStr); err != nil { + return fmt.Errorf("第%d个公告的发布日期格式错误", i+1) + } + if t, exists := ann["type"]; exists { + if typeStr, ok := t.(string); ok { + if !validTypes[typeStr] { + return fmt.Errorf("第%d个公告的类型值不合法", i+1) + } + } + } + if len(content) > 500 { + return fmt.Errorf("第%d个公告的内容长度不能超过500字符", i+1) + } + if extra, exists := ann["extra"]; exists { + if extraStr, ok := extra.(string); ok && len(extraStr) > 200 { + return fmt.Errorf("第%d个公告的说明长度不能超过200字符", i+1) + } + } + } + return nil } func validateFAQ(faqStr string) error { - list, err := parseJSONArray(faqStr, "FAQ信息") - if err != nil { - return err - } - if len(list) > 100 { - return fmt.Errorf("FAQ数量不能超过100个") - } - for i, faq := range list { - question, ok := faq["question"].(string) - if !ok || question == "" { - return fmt.Errorf("第%d个FAQ缺少问题字段", i+1) - } - answer, ok := faq["answer"].(string) - if !ok || answer == "" { - return fmt.Errorf("第%d个FAQ缺少答案字段", i+1) - } - if len(question) > 200 { - return fmt.Errorf("第%d个FAQ的问题长度不能超过200字符", i+1) - } - if len(answer) > 1000 { - return fmt.Errorf("第%d个FAQ的答案长度不能超过1000字符", i+1) - } - } - return nil + list, err := parseJSONArray(faqStr, "FAQ信息") + if err != nil { + return err + } + if len(list) > 100 { + return fmt.Errorf("FAQ数量不能超过100个") + } + for i, faq := range list { + question, ok := faq["question"].(string) + if !ok || question == "" { + return fmt.Errorf("第%d个FAQ缺少问题字段", i+1) + } + answer, ok := faq["answer"].(string) + if !ok || answer == "" { + return fmt.Errorf("第%d个FAQ缺少答案字段", i+1) + } + if len(question) > 200 { + return fmt.Errorf("第%d个FAQ的问题长度不能超过200字符", i+1) + } + if len(answer) > 1000 { + return fmt.Errorf("第%d个FAQ的答案长度不能超过1000字符", i+1) + } + } + return nil } func getPublishTime(item map[string]interface{}) time.Time { - if v, ok := item["publishDate"]; ok { - if s, ok2 := v.(string); ok2 { - if t, err := time.Parse(time.RFC3339, s); err == nil { - return t - } - } - } - return time.Time{} + if v, ok := item["publishDate"]; ok { + if s, ok2 := v.(string); ok2 { + if t, err := time.Parse(time.RFC3339, s); err == nil { + return t + } + } + } + return time.Time{} } func GetAnnouncements() []map[string]interface{} { - list := getJSONList(GetConsoleSetting().Announcements) - sort.SliceStable(list, func(i, j int) bool { - return getPublishTime(list[i]).After(getPublishTime(list[j])) - }) - return list + list := getJSONList(GetConsoleSetting().Announcements) + sort.SliceStable(list, func(i, j int) bool { + return getPublishTime(list[i]).After(getPublishTime(list[j])) + }) + return list } func GetFAQ() []map[string]interface{} { - return getJSONList(GetConsoleSetting().FAQ) + return getJSONList(GetConsoleSetting().FAQ) } func validateUptimeKumaGroups(groupsStr string) error { - groups, err := parseJSONArray(groupsStr, "Uptime Kuma分组配置") - if err != nil { - return err - } + groups, err := parseJSONArray(groupsStr, "Uptime Kuma分组配置") + if err != nil { + return err + } - if len(groups) > 20 { - return fmt.Errorf("Uptime Kuma分组数量不能超过20个") - } + if len(groups) > 20 { + return fmt.Errorf("Uptime Kuma分组数量不能超过20个") + } - nameSet := make(map[string]bool) + nameSet := make(map[string]bool) - for i, group := range groups { - categoryName, ok := group["categoryName"].(string) - if !ok || categoryName == "" { - return fmt.Errorf("第%d个分组缺少分类名称字段", i+1) - } - if nameSet[categoryName] { - return fmt.Errorf("第%d个分组的分类名称与其他分组重复", i+1) - } - nameSet[categoryName] = true - urlStr, ok := group["url"].(string) - if !ok || urlStr == "" { - return fmt.Errorf("第%d个分组缺少URL字段", i+1) - } - slug, ok := group["slug"].(string) - if !ok || slug == "" { - return fmt.Errorf("第%d个分组缺少Slug字段", i+1) - } - description, ok := group["description"].(string) - if !ok { - description = "" - } + for i, group := range groups { + categoryName, ok := group["categoryName"].(string) + if !ok || categoryName == "" { + return fmt.Errorf("第%d个分组缺少分类名称字段", i+1) + } + if nameSet[categoryName] { + return fmt.Errorf("第%d个分组的分类名称与其他分组重复", i+1) + } + nameSet[categoryName] = true + urlStr, ok := group["url"].(string) + if !ok || urlStr == "" { + return fmt.Errorf("第%d个分组缺少URL字段", i+1) + } + slug, ok := group["slug"].(string) + if !ok || slug == "" { + return fmt.Errorf("第%d个分组缺少Slug字段", i+1) + } + description, ok := group["description"].(string) + if !ok { + description = "" + } - if err := validateURL(urlStr, i+1, "分组"); err != nil { - return err - } + if err := validateURL(urlStr, i+1, "分组"); err != nil { + return err + } - if len(categoryName) > 50 { - return fmt.Errorf("第%d个分组的分类名称长度不能超过50字符", i+1) - } - if len(urlStr) > 500 { - return fmt.Errorf("第%d个分组的URL长度不能超过500字符", i+1) - } - if len(slug) > 100 { - return fmt.Errorf("第%d个分组的Slug长度不能超过100字符", i+1) - } - if len(description) > 200 { - return fmt.Errorf("第%d个分组的描述长度不能超过200字符", i+1) - } + if len(categoryName) > 50 { + return fmt.Errorf("第%d个分组的分类名称长度不能超过50字符", i+1) + } + if len(urlStr) > 500 { + return fmt.Errorf("第%d个分组的URL长度不能超过500字符", i+1) + } + if len(slug) > 100 { + return fmt.Errorf("第%d个分组的Slug长度不能超过100字符", i+1) + } + if len(description) > 200 { + return fmt.Errorf("第%d个分组的描述长度不能超过200字符", i+1) + } - if !slugRegex.MatchString(slug) { - return fmt.Errorf("第%d个分组的Slug只能包含字母、数字、下划线和连字符", i+1) - } + if !slugRegex.MatchString(slug) { + return fmt.Errorf("第%d个分组的Slug只能包含字母、数字、下划线和连字符", i+1) + } - if err := checkDangerousContent(description, i+1, "分组"); err != nil { - return err - } - if err := checkDangerousContent(categoryName, i+1, "分组"); err != nil { - return err - } - } - return nil + if err := checkDangerousContent(description, i+1, "分组"); err != nil { + return err + } + if err := checkDangerousContent(categoryName, i+1, "分组"); err != nil { + return err + } + } + return nil } func GetUptimeKumaGroups() []map[string]interface{} { - return getJSONList(GetConsoleSetting().UptimeKumaGroups) -} \ No newline at end of file + return getJSONList(GetConsoleSetting().UptimeKumaGroups) +} diff --git a/setting/operation_setting/monitor_setting.go b/setting/operation_setting/monitor_setting.go new file mode 100644 index 000000000..1d0bbec40 --- /dev/null +++ b/setting/operation_setting/monitor_setting.go @@ -0,0 +1,34 @@ +package operation_setting + +import ( + "one-api/setting/config" + "os" + "strconv" +) + +type MonitorSetting struct { + AutoTestChannelEnabled bool `json:"auto_test_channel_enabled"` + AutoTestChannelMinutes int `json:"auto_test_channel_minutes"` +} + +// 默认配置 +var monitorSetting = MonitorSetting{ + AutoTestChannelEnabled: false, + AutoTestChannelMinutes: 10, +} + +func init() { + // 注册到全局配置管理器 + config.GlobalConfig.Register("monitor_setting", &monitorSetting) +} + +func GetMonitorSetting() *MonitorSetting { + if os.Getenv("CHANNEL_TEST_FREQUENCY") != "" { + frequency, err := strconv.Atoi(os.Getenv("CHANNEL_TEST_FREQUENCY")) + if err == nil && frequency > 0 { + monitorSetting.AutoTestChannelEnabled = true + monitorSetting.AutoTestChannelMinutes = frequency + } + } + return &monitorSetting +} diff --git a/setting/operation_setting/payment_setting.go b/setting/operation_setting/payment_setting.go new file mode 100644 index 000000000..c8df039cf --- /dev/null +++ b/setting/operation_setting/payment_setting.go @@ -0,0 +1,23 @@ +package operation_setting + +import "one-api/setting/config" + +type PaymentSetting struct { + AmountOptions []int `json:"amount_options"` + AmountDiscount map[int]float64 `json:"amount_discount"` // 充值金额对应的折扣,例如 100 元 0.9 表示 100 元充值享受 9 折优惠 +} + +// 默认配置 +var paymentSetting = PaymentSetting{ + AmountOptions: []int{10, 20, 50, 100, 200, 500}, + AmountDiscount: map[int]float64{}, +} + +func init() { + // 注册到全局配置管理器 + config.GlobalConfig.Register("payment_setting", &paymentSetting) +} + +func GetPaymentSetting() *PaymentSetting { + return &paymentSetting +} diff --git a/setting/payment.go b/setting/operation_setting/payment_setting_old.go similarity index 57% rename from setting/payment.go rename to setting/operation_setting/payment_setting_old.go index 7fc5ad3fd..a6313179e 100644 --- a/setting/payment.go +++ b/setting/operation_setting/payment_setting_old.go @@ -1,6 +1,13 @@ -package setting +/** +此文件为旧版支付设置文件,如需增加新的参数、变量等,请在 payment_setting.go 中添加 +This file is the old version of the payment settings file. If you need to add new parameters, variables, etc., please add them in payment_setting.go +*/ -import "encoding/json" +package operation_setting + +import ( + "one-api/common" +) var PayAddress = "" var CustomCallbackAddress = "" @@ -21,15 +28,21 @@ var PayMethods = []map[string]string{ "color": "rgba(var(--semi-green-5), 1)", "type": "wxpay", }, + { + "name": "自定义1", + "color": "black", + "type": "custom1", + "min_topup": "50", + }, } func UpdatePayMethodsByJsonString(jsonString string) error { PayMethods = make([]map[string]string, 0) - return json.Unmarshal([]byte(jsonString), &PayMethods) + return common.Unmarshal([]byte(jsonString), &PayMethods) } func PayMethods2JsonString() string { - jsonBytes, err := json.Marshal(PayMethods) + jsonBytes, err := common.Marshal(PayMethods) if err != nil { return "[]" } diff --git a/setting/ratio_setting/expose_ratio.go b/setting/ratio_setting/expose_ratio.go index 8fca0bcb0..783d9778e 100644 --- a/setting/ratio_setting/expose_ratio.go +++ b/setting/ratio_setting/expose_ratio.go @@ -5,13 +5,13 @@ import "sync/atomic" var exposeRatioEnabled atomic.Bool func init() { - exposeRatioEnabled.Store(false) + exposeRatioEnabled.Store(false) } func SetExposeRatioEnabled(enabled bool) { - exposeRatioEnabled.Store(enabled) + exposeRatioEnabled.Store(enabled) } func IsExposeRatioEnabled() bool { - return exposeRatioEnabled.Load() -} \ No newline at end of file + return exposeRatioEnabled.Load() +} diff --git a/setting/ratio_setting/exposed_cache.go b/setting/ratio_setting/exposed_cache.go index 9e5b6c300..2fe2cd09b 100644 --- a/setting/ratio_setting/exposed_cache.go +++ b/setting/ratio_setting/exposed_cache.go @@ -1,55 +1,55 @@ package ratio_setting import ( - "sync" - "sync/atomic" - "time" + "sync" + "sync/atomic" + "time" - "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin" ) const exposedDataTTL = 30 * time.Second type exposedCache struct { - data gin.H - expiresAt time.Time + data gin.H + expiresAt time.Time } var ( - exposedData atomic.Value - rebuildMu sync.Mutex + exposedData atomic.Value + rebuildMu sync.Mutex ) func InvalidateExposedDataCache() { - exposedData.Store((*exposedCache)(nil)) + exposedData.Store((*exposedCache)(nil)) } func cloneGinH(src gin.H) gin.H { - dst := make(gin.H, len(src)) - for k, v := range src { - dst[k] = v - } - return dst + dst := make(gin.H, len(src)) + for k, v := range src { + dst[k] = v + } + return dst } func GetExposedData() gin.H { - if c, ok := exposedData.Load().(*exposedCache); ok && c != nil && time.Now().Before(c.expiresAt) { - return cloneGinH(c.data) - } - rebuildMu.Lock() - defer rebuildMu.Unlock() - if c, ok := exposedData.Load().(*exposedCache); ok && c != nil && time.Now().Before(c.expiresAt) { - return cloneGinH(c.data) - } - newData := gin.H{ - "model_ratio": GetModelRatioCopy(), - "completion_ratio": GetCompletionRatioCopy(), - "cache_ratio": GetCacheRatioCopy(), - "model_price": GetModelPriceCopy(), - } - exposedData.Store(&exposedCache{ - data: newData, - expiresAt: time.Now().Add(exposedDataTTL), - }) - return cloneGinH(newData) -} \ No newline at end of file + if c, ok := exposedData.Load().(*exposedCache); ok && c != nil && time.Now().Before(c.expiresAt) { + return cloneGinH(c.data) + } + rebuildMu.Lock() + defer rebuildMu.Unlock() + if c, ok := exposedData.Load().(*exposedCache); ok && c != nil && time.Now().Before(c.expiresAt) { + return cloneGinH(c.data) + } + newData := gin.H{ + "model_ratio": GetModelRatioCopy(), + "completion_ratio": GetCompletionRatioCopy(), + "cache_ratio": GetCacheRatioCopy(), + "model_price": GetModelPriceCopy(), + } + exposedData.Store(&exposedCache{ + data: newData, + expiresAt: time.Now().Add(exposedDataTTL), + }) + return cloneGinH(newData) +} diff --git a/setting/system_setting.go b/setting/system_setting/system_setting_old.go similarity index 89% rename from setting/system_setting.go rename to setting/system_setting/system_setting_old.go index c37a61235..4e0f1a502 100644 --- a/setting/system_setting.go +++ b/setting/system_setting/system_setting_old.go @@ -1,4 +1,4 @@ -package setting +package system_setting var ServerAddress = "http://localhost:3000" var WorkerUrl = "" diff --git a/types/error.go b/types/error.go index f653e9a28..883ee0641 100644 --- a/types/error.go +++ b/types/error.go @@ -185,6 +185,14 @@ func (e *NewAPIError) ToClaudeError() ClaudeError { type NewAPIErrorOptions func(*NewAPIError) func NewError(err error, errorCode ErrorCode, ops ...NewAPIErrorOptions) *NewAPIError { + var newErr *NewAPIError + // 保留深层传递的 new err + if errors.As(err, &newErr) { + for _, op := range ops { + op(newErr) + } + return newErr + } e := &NewAPIError{ Err: err, RelayError: nil, @@ -199,8 +207,21 @@ func NewError(err error, errorCode ErrorCode, ops ...NewAPIErrorOptions) *NewAPI } func NewOpenAIError(err error, errorCode ErrorCode, statusCode int, ops ...NewAPIErrorOptions) *NewAPIError { - if errorCode == ErrorCodeDoRequestFailed { - err = errors.New("upstream error: do request failed") + var newErr *NewAPIError + // 保留深层传递的 new err + if errors.As(err, &newErr) { + if newErr.RelayError == nil { + openaiError := OpenAIError{ + Message: newErr.Error(), + Type: string(errorCode), + Code: errorCode, + } + newErr.RelayError = openaiError + } + for _, op := range ops { + op(newErr) + } + return newErr } openaiError := OpenAIError{ Message: err.Error(), @@ -305,6 +326,15 @@ func ErrOptionWithNoRecordErrorLog() NewAPIErrorOptions { } } +func ErrOptionWithHideErrMsg(replaceStr string) NewAPIErrorOptions { + return func(e *NewAPIError) { + if common.DebugEnabled { + fmt.Printf("ErrOptionWithHideErrMsg: %s, origin error: %s", replaceStr, e.Err) + } + e.Err = errors.New(replaceStr) + } +} + func IsRecordErrorLog(e *NewAPIError) bool { if e == nil { return false diff --git a/web/src/App.jsx b/web/src/App.jsx index e3bd7db85..635742f91 100644 --- a/web/src/App.jsx +++ b/web/src/App.jsx @@ -17,7 +17,7 @@ along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ -import React, { lazy, Suspense } from 'react'; +import React, { lazy, Suspense, useContext, useMemo } from 'react'; import { Route, Routes, useLocation } from 'react-router-dom'; import Loading from './components/common/ui/Loading'; import User from './pages/User'; @@ -27,6 +27,7 @@ import LoginForm from './components/auth/LoginForm'; import NotFound from './pages/NotFound'; import Forbidden from './pages/Forbidden'; import Setting from './pages/Setting'; +import { StatusContext } from './context/Status'; import PasswordResetForm from './components/auth/PasswordResetForm'; import PasswordResetConfirm from './components/auth/PasswordResetConfirm'; @@ -53,6 +54,29 @@ const About = lazy(() => import('./pages/About')); function App() { const location = useLocation(); + const [statusState] = useContext(StatusContext); + + // 获取模型广场权限配置 + const pricingRequireAuth = useMemo(() => { + const headerNavModulesConfig = statusState?.status?.HeaderNavModules; + if (headerNavModulesConfig) { + try { + const modules = JSON.parse(headerNavModulesConfig); + + // 处理向后兼容性:如果pricing是boolean,默认不需要登录 + if (typeof modules.pricing === 'boolean') { + return false; // 默认不需要登录鉴权 + } + + // 如果是对象格式,使用requireAuth配置 + return modules.pricing?.requireAuth === true; + } catch (error) { + console.error('解析顶栏模块配置失败:', error); + return false; // 默认不需要登录 + } + } + return false; // 默认不需要登录 + }, [statusState?.status?.HeaderNavModules]); return ( @@ -253,9 +277,20 @@ function App() { } key={location.pathname}> - - + pricingRequireAuth ? ( + + } + key={location.pathname} + > + + + + ) : ( + } key={location.pathname}> + + + ) } /> - {t('支持6位TOTP验证码或8位备用码')} + {t('支持6位TOTP验证码或8位备用码,可到`个人设置-安全设置-两步验证设置`配置或查看。')} diff --git a/web/src/components/common/ui/JSONEditor.jsx b/web/src/components/common/ui/JSONEditor.jsx index 7acdc2e37..d89753872 100644 --- a/web/src/components/common/ui/JSONEditor.jsx +++ b/web/src/components/common/ui/JSONEditor.jsx @@ -443,7 +443,7 @@ const JSONEditor = ({ return ( - +
- {renderValueInput(pair.id, pair.value)} + {renderValueInput(pair.id, pair.value)} + +
); diff --git a/web/src/components/layout/components/SkeletonWrapper.jsx b/web/src/components/layout/components/SkeletonWrapper.jsx new file mode 100644 index 000000000..7fbd588ca --- /dev/null +++ b/web/src/components/layout/components/SkeletonWrapper.jsx @@ -0,0 +1,394 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import React from 'react'; +import { Skeleton } from '@douyinfe/semi-ui'; + +const SkeletonWrapper = ({ + loading = false, + type = 'text', + count = 1, + width = 60, + height = 16, + isMobile = false, + className = '', + collapsed = false, + showAdmin = true, + children, + ...props +}) => { + if (!loading) { + return children; + } + + // 导航链接骨架屏 + const renderNavigationSkeleton = () => { + const skeletonLinkClasses = isMobile + ? 'flex items-center gap-1 p-1 w-full rounded-md' + : 'flex items-center gap-1 p-2 rounded-md'; + + return Array(count) + .fill(null) + .map((_, index) => ( +
+ + } + /> +
+ )); + }; + + // 用户区域骨架屏 (头像 + 文本) + const renderUserAreaSkeleton = () => { + return ( +
+ + } + /> +
+ + } + /> +
+
+ ); + }; + + // Logo图片骨架屏 + const renderImageSkeleton = () => { + return ( + + } + /> + ); + }; + + // 系统名称骨架屏 + const renderTitleSkeleton = () => { + return ( + } + /> + ); + }; + + // 通用文本骨架屏 + const renderTextSkeleton = () => { + return ( +
+ } + /> +
+ ); + }; + + // 按钮骨架屏(支持圆角) + const renderButtonSkeleton = () => { + return ( +
+ + } + /> +
+ ); + }; + + // 侧边栏导航项骨架屏 (图标 + 文本) + const renderSidebarNavItemSkeleton = () => { + return Array(count) + .fill(null) + .map((_, index) => ( +
+ {/* 图标骨架屏 */} +
+ + } + /> +
+ {/* 文本骨架屏 */} + + } + /> +
+ )); + }; + + // 侧边栏组标题骨架屏 + const renderSidebarGroupTitleSkeleton = () => { + return ( +
+ + } + /> +
+ ); + }; + + // 完整侧边栏骨架屏 - 1:1 还原,去重实现 + const renderSidebarSkeleton = () => { + const NAV_WIDTH = 164; + const NAV_HEIGHT = 30; + const COLLAPSED_WIDTH = 44; + const COLLAPSED_HEIGHT = 44; + const ICON_SIZE = 16; + const TITLE_HEIGHT = 12; + const TEXT_HEIGHT = 16; + + const renderIcon = () => ( + + } + /> + ); + + const renderLabel = (labelWidth) => ( + + } + /> + ); + + const NavRow = ({ labelWidth }) => ( +
+
+ {renderIcon()} +
+ {renderLabel(labelWidth)} +
+ ); + + const CollapsedRow = ({ keyPrefix, index }) => ( +
+ + } + /> +
+ ); + + if (collapsed) { + return ( +
+ {Array(2) + .fill(null) + .map((_, i) => ( + + ))} + {Array(5) + .fill(null) + .map((_, i) => ( + + ))} + {Array(2) + .fill(null) + .map((_, i) => ( + + ))} + {Array(5) + .fill(null) + .map((_, i) => ( + + ))} +
+ ); + } + + const sections = [ + { key: 'chat', titleWidth: 32, itemWidths: [54, 32], wrapper: 'section' }, + { key: 'console', titleWidth: 48, itemWidths: [64, 64, 64, 64, 64] }, + { key: 'personal', titleWidth: 64, itemWidths: [64, 64] }, + ...(showAdmin + ? [{ key: 'admin', titleWidth: 48, itemWidths: [64, 64, 80, 64, 64] }] + : []), + ]; + + return ( +
+ {sections.map((sec, idx) => ( + + {sec.wrapper === 'section' ? ( +
+
+ + } + /> +
+ {sec.itemWidths.map((w, i) => ( + + ))} +
+ ) : ( +
+
+ + } + /> +
+ {sec.itemWidths.map((w, i) => ( + + ))} +
+ )} +
+ ))} +
+ ); + }; + + // 根据类型渲染不同的骨架屏 + switch (type) { + case 'navigation': + return renderNavigationSkeleton(); + case 'userArea': + return renderUserAreaSkeleton(); + case 'image': + return renderImageSkeleton(); + case 'title': + return renderTitleSkeleton(); + case 'sidebarNavItem': + return renderSidebarNavItemSkeleton(); + case 'sidebarGroupTitle': + return renderSidebarGroupTitleSkeleton(); + case 'sidebar': + return renderSidebarSkeleton(); + case 'button': + return renderButtonSkeleton(); + case 'text': + default: + return renderTextSkeleton(); + } +}; + +export default SkeletonWrapper; diff --git a/web/src/components/layout/HeaderBar/ActionButtons.jsx b/web/src/components/layout/headerbar/ActionButtons.jsx similarity index 100% rename from web/src/components/layout/HeaderBar/ActionButtons.jsx rename to web/src/components/layout/headerbar/ActionButtons.jsx diff --git a/web/src/components/layout/HeaderBar/HeaderLogo.jsx b/web/src/components/layout/headerbar/HeaderLogo.jsx similarity index 97% rename from web/src/components/layout/HeaderBar/HeaderLogo.jsx rename to web/src/components/layout/headerbar/HeaderLogo.jsx index c81e75d2d..73be0516b 100644 --- a/web/src/components/layout/HeaderBar/HeaderLogo.jsx +++ b/web/src/components/layout/headerbar/HeaderLogo.jsx @@ -20,7 +20,7 @@ For commercial licensing, please contact support@quantumnous.com import React from 'react'; import { Link } from 'react-router-dom'; import { Typography, Tag } from '@douyinfe/semi-ui'; -import SkeletonWrapper from './SkeletonWrapper'; +import SkeletonWrapper from '../components/SkeletonWrapper'; const HeaderLogo = ({ isMobile, diff --git a/web/src/components/layout/HeaderBar/LanguageSelector.jsx b/web/src/components/layout/headerbar/LanguageSelector.jsx similarity index 100% rename from web/src/components/layout/HeaderBar/LanguageSelector.jsx rename to web/src/components/layout/headerbar/LanguageSelector.jsx diff --git a/web/src/components/layout/HeaderBar/MobileMenuButton.jsx b/web/src/components/layout/headerbar/MobileMenuButton.jsx similarity index 100% rename from web/src/components/layout/HeaderBar/MobileMenuButton.jsx rename to web/src/components/layout/headerbar/MobileMenuButton.jsx diff --git a/web/src/components/layout/HeaderBar/Navigation.jsx b/web/src/components/layout/headerbar/Navigation.jsx similarity index 88% rename from web/src/components/layout/HeaderBar/Navigation.jsx rename to web/src/components/layout/headerbar/Navigation.jsx index ef8d715fe..e2a4a696e 100644 --- a/web/src/components/layout/HeaderBar/Navigation.jsx +++ b/web/src/components/layout/headerbar/Navigation.jsx @@ -19,9 +19,15 @@ For commercial licensing, please contact support@quantumnous.com import React from 'react'; import { Link } from 'react-router-dom'; -import SkeletonWrapper from './SkeletonWrapper'; +import SkeletonWrapper from '../components/SkeletonWrapper'; -const Navigation = ({ mainNavLinks, isMobile, isLoading, userState }) => { +const Navigation = ({ + mainNavLinks, + isMobile, + isLoading, + userState, + pricingRequireAuth, +}) => { const renderNavLinks = () => { const baseClasses = 'flex-shrink-0 flex items-center gap-1 font-semibold rounded-md transition-all duration-200 ease-in-out'; @@ -51,6 +57,9 @@ const Navigation = ({ mainNavLinks, isMobile, isLoading, userState }) => { if (link.itemKey === 'console' && !userState.user) { targetPath = '/login'; } + if (link.itemKey === 'pricing' && pricingRequireAuth && !userState.user) { + targetPath = '/login'; + } return ( diff --git a/web/src/components/layout/HeaderBar/NewYearButton.jsx b/web/src/components/layout/headerbar/NewYearButton.jsx similarity index 100% rename from web/src/components/layout/HeaderBar/NewYearButton.jsx rename to web/src/components/layout/headerbar/NewYearButton.jsx diff --git a/web/src/components/layout/HeaderBar/NotificationButton.jsx b/web/src/components/layout/headerbar/NotificationButton.jsx similarity index 100% rename from web/src/components/layout/HeaderBar/NotificationButton.jsx rename to web/src/components/layout/headerbar/NotificationButton.jsx diff --git a/web/src/components/layout/HeaderBar/ThemeToggle.jsx b/web/src/components/layout/headerbar/ThemeToggle.jsx similarity index 100% rename from web/src/components/layout/HeaderBar/ThemeToggle.jsx rename to web/src/components/layout/headerbar/ThemeToggle.jsx diff --git a/web/src/components/layout/HeaderBar/UserArea.jsx b/web/src/components/layout/headerbar/UserArea.jsx similarity index 99% rename from web/src/components/layout/HeaderBar/UserArea.jsx rename to web/src/components/layout/headerbar/UserArea.jsx index 5d2c04483..8ea70f47f 100644 --- a/web/src/components/layout/HeaderBar/UserArea.jsx +++ b/web/src/components/layout/headerbar/UserArea.jsx @@ -28,7 +28,7 @@ import { IconKey, } from '@douyinfe/semi-icons'; import { stringToColor } from '../../../helpers'; -import SkeletonWrapper from './SkeletonWrapper'; +import SkeletonWrapper from '../components/SkeletonWrapper'; const UserArea = ({ userState, diff --git a/web/src/components/layout/HeaderBar/index.jsx b/web/src/components/layout/headerbar/index.jsx similarity index 95% rename from web/src/components/layout/HeaderBar/index.jsx rename to web/src/components/layout/headerbar/index.jsx index db104de43..81b51d7fe 100644 --- a/web/src/components/layout/HeaderBar/index.jsx +++ b/web/src/components/layout/headerbar/index.jsx @@ -44,6 +44,8 @@ const HeaderBar = ({ onMobileMenuToggle, drawerOpen }) => { isDemoSiteMode, isConsoleRoute, theme, + headerNavModules, + pricingRequireAuth, logout, handleLanguageChange, handleThemeToggle, @@ -60,7 +62,7 @@ const HeaderBar = ({ onMobileMenuToggle, drawerOpen }) => { getUnreadKeys, } = useNotifications(statusState); - const { mainNavLinks } = useNavigation(t, docsLink); + const { mainNavLinks } = useNavigation(t, docsLink, headerNavModules); return (
@@ -102,6 +104,7 @@ const HeaderBar = ({ onMobileMenuToggle, drawerOpen }) => { isMobile={isMobile} isLoading={isLoading} userState={userState} + pricingRequireAuth={pricingRequireAuth} /> { + const id = record?.key ?? record?.value ?? record?._originalData?.id; + const base = record?._originalData?.base_url || ''; + const name = record?.label || ''; + return ( + id === -100 || + base === 'https://basellm.github.io' || + name === '官方倍率预设' + ); + }; + useEffect(() => { if (!allChannels) return; @@ -77,7 +88,13 @@ const ChannelSelectorModal = forwardRef( }) : allChannels; - setFilteredData(matched); + const sorted = [...matched].sort((a, b) => { + const wa = isOfficialChannel(a) ? 0 : 1; + const wb = isOfficialChannel(b) ? 0 : 1; + return wa - wb; + }); + + setFilteredData(sorted); }, [allChannels, searchText]); const total = filteredData.length; @@ -143,45 +160,49 @@ const ChannelSelectorModal = forwardRef( ); }; - const renderStatusCell = (status) => { + const renderStatusCell = (record) => { + const status = record?._originalData?.status || 0; + const official = isOfficialChannel(record); + let statusTag = null; switch (status) { case 1: - return ( - } - > + statusTag = ( + {t('已启用')} ); + break; case 2: - return ( - }> + statusTag = ( + {t('已禁用')} ); + break; case 3: - return ( - } - > + statusTag = ( + {t('自动禁用')} ); + break; default: - return ( - } - > + statusTag = ( + {t('未知状态')} ); } + return ( +
+ {statusTag} + {official && ( + + {t('官方')} + + )} +
+ ); }; const renderNameCell = (text) => ( @@ -207,8 +228,7 @@ const ChannelSelectorModal = forwardRef( { title: t('状态'), dataIndex: '_originalData.status', - render: (_, record) => - renderStatusCell(record._originalData?.status || 0), + render: (_, record) => renderStatusCell(record), }, { title: t('同步接口'), diff --git a/web/src/components/settings/OperationSetting.jsx b/web/src/components/settings/OperationSetting.jsx index d2669c643..0d6e44107 100644 --- a/web/src/components/settings/OperationSetting.jsx +++ b/web/src/components/settings/OperationSetting.jsx @@ -20,6 +20,8 @@ For commercial licensing, please contact support@quantumnous.com import React, { useEffect, useState } from 'react'; import { Card, Spin } from '@douyinfe/semi-ui'; import SettingsGeneral from '../../pages/Setting/Operation/SettingsGeneral'; +import SettingsHeaderNavModules from '../../pages/Setting/Operation/SettingsHeaderNavModules'; +import SettingsSidebarModulesAdmin from '../../pages/Setting/Operation/SettingsSidebarModulesAdmin'; import SettingsSensitiveWords from '../../pages/Setting/Operation/SettingsSensitiveWords'; import SettingsLog from '../../pages/Setting/Operation/SettingsLog'; import SettingsMonitoring from '../../pages/Setting/Operation/SettingsMonitoring'; @@ -46,6 +48,12 @@ const OperationSetting = () => { DemoSiteEnabled: false, SelfUseModeEnabled: false, + /* 顶栏模块管理 */ + HeaderNavModules: '', + + /* 左侧边栏模块管理(管理员) */ + SidebarModulesAdmin: '', + /* 敏感词设置 */ CheckSensitiveEnabled: false, CheckSensitiveOnPromptEnabled: false, @@ -60,6 +68,8 @@ const OperationSetting = () => { AutomaticDisableChannelEnabled: false, AutomaticEnableChannelEnabled: false, AutomaticDisableKeywords: '', + 'monitor_setting.auto_test_channel_enabled': false, + 'monitor_setting.auto_test_channel_minutes': 10, }); let [loading, setLoading] = useState(false); @@ -70,10 +80,7 @@ const OperationSetting = () => { if (success) { let newInputs = {}; data.forEach((item) => { - if ( - item.key.endsWith('Enabled') || - ['DefaultCollapseSidebar'].includes(item.key) - ) { + if (typeof inputs[item.key] === 'boolean') { newInputs[item.key] = toBoolean(item.value); } else { newInputs[item.key] = item.value; @@ -108,6 +115,14 @@ const OperationSetting = () => { + {/* 顶栏模块管理 */} +
+ +
+ {/* 左侧边栏模块管理(管理员) */} +
+ +
{/* 屏蔽词过滤设置 */} diff --git a/web/src/components/settings/PaymentSetting.jsx b/web/src/components/settings/PaymentSetting.jsx index a632760aa..faaa9561b 100644 --- a/web/src/components/settings/PaymentSetting.jsx +++ b/web/src/components/settings/PaymentSetting.jsx @@ -37,6 +37,8 @@ const PaymentSetting = () => { TopupGroupRatio: '', CustomCallbackAddress: '', PayMethods: '', + AmountOptions: '', + AmountDiscount: '', StripeApiSecret: '', StripeWebhookSecret: '', @@ -66,6 +68,30 @@ const PaymentSetting = () => { newInputs[item.key] = item.value; } break; + case 'payment_setting.amount_options': + try { + newInputs['AmountOptions'] = JSON.stringify( + JSON.parse(item.value), + null, + 2, + ); + } catch (error) { + console.error('解析AmountOptions出错:', error); + newInputs['AmountOptions'] = item.value; + } + break; + case 'payment_setting.amount_discount': + try { + newInputs['AmountDiscount'] = JSON.stringify( + JSON.parse(item.value), + null, + 2, + ); + } catch (error) { + console.error('解析AmountDiscount出错:', error); + newInputs['AmountDiscount'] = item.value; + } + break; case 'Price': case 'MinTopUp': case 'StripeUnitPrice': diff --git a/web/src/components/settings/PersonalSetting.jsx b/web/src/components/settings/PersonalSetting.jsx index cdeb1f511..3ba8dcfd3 100644 --- a/web/src/components/settings/PersonalSetting.jsx +++ b/web/src/components/settings/PersonalSetting.jsx @@ -65,6 +65,7 @@ const PersonalSetting = () => { webhookUrl: '', webhookSecret: '', notificationEmail: '', + barkUrl: '', acceptUnsetModelRatioModel: false, recordIpLog: false, }); @@ -106,6 +107,7 @@ const PersonalSetting = () => { webhookUrl: settings.webhook_url || '', webhookSecret: settings.webhook_secret || '', notificationEmail: settings.notification_email || '', + barkUrl: settings.bark_url || '', acceptUnsetModelRatioModel: settings.accept_unset_model_ratio_model || false, recordIpLog: settings.record_ip_log || false, @@ -283,6 +285,7 @@ const PersonalSetting = () => { webhook_url: notificationSettings.webhookUrl, webhook_secret: notificationSettings.webhookSecret, notification_email: notificationSettings.notificationEmail, + bark_url: notificationSettings.barkUrl, accept_unset_model_ratio_model: notificationSettings.acceptUnsetModelRatioModel, record_ip_log: notificationSettings.recordIpLog, diff --git a/web/src/components/settings/personal/cards/NotificationSettings.jsx b/web/src/components/settings/personal/cards/NotificationSettings.jsx index 6caffde7b..0b097eaff 100644 --- a/web/src/components/settings/personal/cards/NotificationSettings.jsx +++ b/web/src/components/settings/personal/cards/NotificationSettings.jsx @@ -17,7 +17,7 @@ along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ -import React, { useRef, useEffect } from 'react'; +import React, { useRef, useEffect, useState, useContext } from 'react'; import { Button, Typography, @@ -28,11 +28,22 @@ import { Toast, Tabs, TabPane, + Switch, + Row, + Col, } from '@douyinfe/semi-ui'; import { IconMail, IconKey, IconBell, IconLink } from '@douyinfe/semi-icons'; -import { ShieldCheck, Bell, DollarSign } from 'lucide-react'; -import { renderQuotaWithPrompt } from '../../../../helpers'; +import { ShieldCheck, Bell, DollarSign, Settings } from 'lucide-react'; +import { + renderQuotaWithPrompt, + API, + showSuccess, + showError, +} from '../../../../helpers'; import CodeViewer from '../../../playground/CodeViewer'; +import { StatusContext } from '../../../../context/Status'; +import { UserContext } from '../../../../context/User'; +import { useUserPermissions } from '../../../../hooks/common/useUserPermissions'; const NotificationSettings = ({ t, @@ -41,6 +52,142 @@ const NotificationSettings = ({ saveNotificationSettings, }) => { const formApiRef = useRef(null); + const [statusState] = useContext(StatusContext); + const [userState] = useContext(UserContext); + + // 左侧边栏设置相关状态 + const [sidebarLoading, setSidebarLoading] = useState(false); + const [activeTabKey, setActiveTabKey] = useState('notification'); + const [sidebarModulesUser, setSidebarModulesUser] = useState({ + chat: { + enabled: true, + playground: true, + chat: true, + }, + console: { + enabled: true, + detail: true, + token: true, + log: true, + midjourney: true, + task: true, + }, + personal: { + enabled: true, + topup: true, + personal: true, + }, + admin: { + enabled: true, + channel: true, + models: true, + redemption: true, + user: true, + setting: true, + }, + }); + const [adminConfig, setAdminConfig] = useState(null); + + // 使用后端权限验证替代前端角色判断 + const { + permissions, + loading: permissionsLoading, + hasSidebarSettingsPermission, + isSidebarSectionAllowed, + isSidebarModuleAllowed, + } = useUserPermissions(); + + // 左侧边栏设置处理函数 + const handleSectionChange = (sectionKey) => { + return (checked) => { + const newModules = { + ...sidebarModulesUser, + [sectionKey]: { + ...sidebarModulesUser[sectionKey], + enabled: checked, + }, + }; + setSidebarModulesUser(newModules); + }; + }; + + const handleModuleChange = (sectionKey, moduleKey) => { + return (checked) => { + const newModules = { + ...sidebarModulesUser, + [sectionKey]: { + ...sidebarModulesUser[sectionKey], + [moduleKey]: checked, + }, + }; + setSidebarModulesUser(newModules); + }; + }; + + const saveSidebarSettings = async () => { + setSidebarLoading(true); + try { + const res = await API.put('/api/user/self', { + sidebar_modules: JSON.stringify(sidebarModulesUser), + }); + if (res.data.success) { + showSuccess(t('侧边栏设置保存成功')); + } else { + showError(res.data.message); + } + } catch (error) { + showError(t('保存失败')); + } + setSidebarLoading(false); + }; + + const resetSidebarModules = () => { + const defaultConfig = { + chat: { enabled: true, playground: true, chat: true }, + console: { + enabled: true, + detail: true, + token: true, + log: true, + midjourney: true, + task: true, + }, + personal: { enabled: true, topup: true, personal: true }, + admin: { + enabled: true, + channel: true, + models: true, + redemption: true, + user: true, + setting: true, + }, + }; + setSidebarModulesUser(defaultConfig); + }; + + // 加载左侧边栏配置 + useEffect(() => { + const loadSidebarConfigs = async () => { + try { + // 获取管理员全局配置 + if (statusState?.status?.SidebarModulesAdmin) { + const adminConf = JSON.parse(statusState.status.SidebarModulesAdmin); + setAdminConfig(adminConf); + } + + // 获取用户个人配置 + const userRes = await API.get('/api/user/self'); + if (userRes.data.success && userRes.data.data.sidebar_modules) { + const userConf = JSON.parse(userRes.data.data.sidebar_modules); + setSidebarModulesUser(userConf); + } + } catch (error) { + console.error('加载边栏配置失败:', error); + } + }; + + loadSidebarConfigs(); + }, [statusState]); // 初始化表单值 useEffect(() => { @@ -54,6 +201,101 @@ const NotificationSettings = ({ handleNotificationSettingChange(field, value); }; + // 检查功能是否被管理员允许 + const isAllowedByAdmin = (sectionKey, moduleKey = null) => { + if (!adminConfig) return true; + + if (moduleKey) { + return ( + adminConfig[sectionKey]?.enabled && adminConfig[sectionKey]?.[moduleKey] + ); + } else { + return adminConfig[sectionKey]?.enabled; + } + }; + + // 区域配置数据(根据权限过滤) + const sectionConfigs = [ + { + key: 'chat', + title: t('聊天区域'), + description: t('操练场和聊天功能'), + modules: [ + { + key: 'playground', + title: t('操练场'), + description: t('AI模型测试环境'), + }, + { key: 'chat', title: t('聊天'), description: t('聊天会话管理') }, + ], + }, + { + key: 'console', + title: t('控制台区域'), + description: t('数据管理和日志查看'), + modules: [ + { key: 'detail', title: t('数据看板'), description: t('系统数据统计') }, + { key: 'token', title: t('令牌管理'), description: t('API令牌管理') }, + { key: 'log', title: t('使用日志'), description: t('API使用记录') }, + { + key: 'midjourney', + title: t('绘图日志'), + description: t('绘图任务记录'), + }, + { key: 'task', title: t('任务日志'), description: t('系统任务记录') }, + ], + }, + { + key: 'personal', + title: t('个人中心区域'), + description: t('用户个人功能'), + modules: [ + { key: 'topup', title: t('钱包管理'), description: t('余额充值管理') }, + { + key: 'personal', + title: t('个人设置'), + description: t('个人信息设置'), + }, + ], + }, + // 管理员区域:根据后端权限控制显示 + { + key: 'admin', + title: t('管理员区域'), + description: t('系统管理功能'), + modules: [ + { key: 'channel', title: t('渠道管理'), description: t('API渠道配置') }, + { key: 'models', title: t('模型管理'), description: t('AI模型配置') }, + { + key: 'redemption', + title: t('兑换码管理'), + description: t('兑换码生成管理'), + }, + { key: 'user', title: t('用户管理'), description: t('用户账户管理') }, + { + key: 'setting', + title: t('系统设置'), + description: t('系统参数配置'), + }, + ], + }, + ] + .filter((section) => { + // 使用后端权限验证替代前端角色判断 + return isSidebarSectionAllowed(section.key); + }) + .map((section) => ({ + ...section, + modules: section.modules.filter((module) => + isSidebarModuleAllowed(section.key, module.key), + ), + })) + .filter( + (section) => + // 过滤掉没有可用模块的区域 + section.modules.length > 0 && isAllowedByAdmin(section.key), + ); + // 表单提交 const handleSubmit = () => { if (formApiRef.current) { @@ -75,10 +317,32 @@ const NotificationSettings = ({ - +
+ {activeTabKey === 'sidebar' ? ( + // 边栏设置标签页的按钮 + <> + + + + ) : ( + // 其他标签页的通用保存按钮 + + )}
} > @@ -103,7 +367,11 @@ const NotificationSettings = ({ onSubmit={handleSubmit} > {() => ( - + setActiveTabKey(key)} + > {/* 通知配置 Tab */} {t('邮件通知')} {t('Webhook通知')} + {t('Bark通知')} )} + + {/* Bark推送设置 */} + {notificationSettings.warningType === 'bark' && ( + <> + handleFormChange('barkUrl', val)} + prefix={} + extraText={t( + '支持HTTP和HTTPS,模板变量: {{title}} (通知标题), {{content}} (通知内容)', + )} + showClear + rules={[ + { + required: notificationSettings.warningType === 'bark', + message: t('请输入Bark推送URL'), + }, + { + pattern: /^https?:\/\/.+/, + message: t('Bark推送URL必须以http://或https://开头'), + }, + ]} + /> + +
+
+ {t('模板示例')} +
+
+ https://api.day.app/yourkey/{'{{title}}'}/ + {'{{content}}'}?sound=alarm&group=quota +
+
+
+ • {'title'}: {t('通知标题')} +
+
+ • {'content'}: {t('通知内容')} +
+
+ + {t('更多参数请参考')} + {' '} + + Bark 官方文档 + +
+
+
+ + )}
@@ -312,6 +641,147 @@ const NotificationSettings = ({ /> + + {/* 左侧边栏设置 Tab - 根据后端权限控制显示 */} + {hasSidebarSettingsPermission() && ( + + + {t('边栏设置')} + + } + itemKey='sidebar' + > +
+
+ + {t('您可以个性化设置侧边栏的要显示功能')} + +
+ {/* 边栏设置功能区域容器 */} +
+ {sectionConfigs.map((section) => ( +
+ {/* 区域标题和总开关 */} +
+
+
+ {section.title} +
+ + {section.description} + +
+ +
+ + {/* 功能模块网格 */} + + {section.modules + .filter((module) => + isAllowedByAdmin(section.key, module.key), + ) + .map((module) => ( + + +
+
+
+ {module.title} +
+ + {module.description} + +
+
+ +
+
+
+ + ))} +
+
+ ))} +
{' '} + {/* 关闭边栏设置功能区域容器 */} +
+
+ )}
)} diff --git a/web/src/components/table/channels/ChannelsColumnDefs.jsx b/web/src/components/table/channels/ChannelsColumnDefs.jsx index 2230877c5..5b505baed 100644 --- a/web/src/components/table/channels/ChannelsColumnDefs.jsx +++ b/web/src/components/table/channels/ChannelsColumnDefs.jsx @@ -35,6 +35,8 @@ import { renderQuota, getChannelIcon, renderQuotaWithAmount, + showSuccess, + showError, } from '../../../helpers'; import { CHANNEL_OPTIONS } from '../../../constants'; import { IconTreeTriangleDown, IconMore } from '@douyinfe/semi-icons'; @@ -216,6 +218,42 @@ export const getChannelsColumns = ({ key: COLUMN_KEYS.NAME, title: t('名称'), dataIndex: 'name', + render: (text, record, index) => { + if (record.remark && record.remark.trim() !== '') { + return ( + +
{record.remark}
+ + + } + trigger='hover' + position='topLeft' + > + {text} +
+ ); + } + return text; + }, }, { key: COLUMN_KEYS.GROUP, diff --git a/web/src/components/table/channels/modals/EditChannelModal.jsx b/web/src/components/table/channels/modals/EditChannelModal.jsx index de2c70213..c0a216246 100644 --- a/web/src/components/table/channels/modals/EditChannelModal.jsx +++ b/web/src/components/table/channels/modals/EditChannelModal.jsx @@ -142,6 +142,8 @@ const EditChannelModal = (props) => { system_prompt: '', system_prompt_override: false, settings: '', + // 仅 Vertex: 密钥格式(存入 settings.vertex_key_type) + vertex_key_type: 'json', }; const [batch, setBatch] = useState(false); const [multiToSingle, setMultiToSingle] = useState(false); @@ -409,11 +411,17 @@ const EditChannelModal = (props) => { const parsedSettings = JSON.parse(data.settings); data.azure_responses_version = parsedSettings.azure_responses_version || ''; + // 读取 Vertex 密钥格式 + data.vertex_key_type = parsedSettings.vertex_key_type || 'json'; } catch (error) { console.error('解析其他设置失败:', error); data.azure_responses_version = ''; data.region = ''; + data.vertex_key_type = 'json'; } + } else { + // 兼容历史数据:老渠道没有 settings 时,默认按 json 展示 + data.vertex_key_type = 'json'; } setInputs(data); @@ -745,59 +753,56 @@ const EditChannelModal = (props) => { let localInputs = { ...formValues }; if (localInputs.type === 41) { - if (useManualInput) { - // 手动输入模式 - if (localInputs.key && localInputs.key.trim() !== '') { - try { - // 验证 JSON 格式 - const parsedKey = JSON.parse(localInputs.key); - // 确保是有效的密钥格式 - localInputs.key = JSON.stringify(parsedKey); - } catch (err) { - showError(t('密钥格式无效,请输入有效的 JSON 格式密钥')); - return; - } - } else if (!isEdit) { + const keyType = localInputs.vertex_key_type || 'json'; + if (keyType === 'api_key') { + // 直接作为普通字符串密钥处理 + if (!isEdit && (!localInputs.key || localInputs.key.trim() === '')) { showInfo(t('请输入密钥!')); return; } } else { - // 文件上传模式 - let keys = vertexKeys; - - // 若当前未选择文件,尝试从已上传文件列表解析(异步读取) - if (keys.length === 0 && vertexFileList.length > 0) { - try { - const parsed = await Promise.all( - vertexFileList.map(async (item) => { - const fileObj = item.fileInstance; - if (!fileObj) return null; - const txt = await fileObj.text(); - return JSON.parse(txt); - }), - ); - keys = parsed.filter(Boolean); - } catch (err) { - showError(t('解析密钥文件失败: {{msg}}', { msg: err.message })); + // JSON 服务账号密钥 + if (useManualInput) { + if (localInputs.key && localInputs.key.trim() !== '') { + try { + const parsedKey = JSON.parse(localInputs.key); + localInputs.key = JSON.stringify(parsedKey); + } catch (err) { + showError(t('密钥格式无效,请输入有效的 JSON 格式密钥')); + return; + } + } else if (!isEdit) { + showInfo(t('请输入密钥!')); return; } - } - - // 创建模式必须上传密钥;编辑模式可选 - if (keys.length === 0) { - if (!isEdit) { - showInfo(t('请上传密钥文件!')); - return; - } else { - // 编辑模式且未上传新密钥,不修改 key - delete localInputs.key; - } } else { - // 有新密钥,则覆盖 - if (batch) { - localInputs.key = JSON.stringify(keys); + // 文件上传模式 + let keys = vertexKeys; + if (keys.length === 0 && vertexFileList.length > 0) { + try { + const parsed = await Promise.all( + vertexFileList.map(async (item) => { + const fileObj = item.fileInstance; + if (!fileObj) return null; + const txt = await fileObj.text(); + return JSON.parse(txt); + }), + ); + keys = parsed.filter(Boolean); + } catch (err) { + showError(t('解析密钥文件失败: {{msg}}', { msg: err.message })); + return; + } + } + if (keys.length === 0) { + if (!isEdit) { + showInfo(t('请上传密钥文件!')); + return; + } else { + delete localInputs.key; + } } else { - localInputs.key = JSON.stringify(keys[0]); + localInputs.key = batch ? JSON.stringify(keys) : JSON.stringify(keys[0]); } } } @@ -853,6 +858,8 @@ const EditChannelModal = (props) => { delete localInputs.pass_through_body_enabled; delete localInputs.system_prompt; delete localInputs.system_prompt_override; + // 顶层的 vertex_key_type 不应发送给后端 + delete localInputs.vertex_key_type; let res; localInputs.auto_ban = localInputs.auto_ban ? 1 : 0; @@ -1178,8 +1185,40 @@ const EditChannelModal = (props) => { autoComplete='new-password' /> + {inputs.type === 41 && ( + { + // 更新设置中的 vertex_key_type + handleChannelOtherSettingsChange('vertex_key_type', value); + // 切换为 api_key 时,关闭批量与手动/文件切换,并清理已选文件 + if (value === 'api_key') { + setBatch(false); + setUseManualInput(false); + setVertexKeys([]); + setVertexFileList([]); + if (formApiRef.current) { + formApiRef.current.setValue('vertex_files', []); + } + } + }} + extraText={ + inputs.vertex_key_type === 'api_key' + ? t('API Key 模式下不支持批量创建') + : t('JSON 模式支持手动输入或上传服务账号 JSON') + } + /> + )} {batch ? ( - inputs.type === 41 ? ( + inputs.type === 41 && (inputs.vertex_key_type || 'json') === 'json' ? ( { ) ) : ( <> - {inputs.type === 41 ? ( + {inputs.type === 41 && (inputs.vertex_key_type || 'json') === 'json' ? ( <> {!batch && (
@@ -1993,6 +2032,14 @@ const EditChannelModal = (props) => { showClear onChange={(value) => handleInputChange('tag', value)} /> + handleInputChange('remark', value)} + /> diff --git a/web/src/components/table/models/ModelsActions.jsx b/web/src/components/table/models/ModelsActions.jsx index cc6b8ef8e..7e8f163bf 100644 --- a/web/src/components/table/models/ModelsActions.jsx +++ b/web/src/components/table/models/ModelsActions.jsx @@ -21,10 +21,12 @@ import React, { useState } from 'react'; import MissingModelsModal from './modals/MissingModelsModal'; import PrefillGroupManagement from './modals/PrefillGroupManagement'; import EditPrefillGroupModal from './modals/EditPrefillGroupModal'; -import { Button, Modal } from '@douyinfe/semi-ui'; +import { Button, Modal, Popover, RadioGroup, Radio } from '@douyinfe/semi-ui'; import { showSuccess, showError, copy } from '../../../helpers'; import CompactModeToggle from '../../common/ui/CompactModeToggle'; import SelectionNotification from './components/SelectionNotification'; +import UpstreamConflictModal from './modals/UpstreamConflictModal'; +import SyncWizardModal from './modals/SyncWizardModal'; const ModelsActions = ({ selectedKeys, @@ -32,6 +34,11 @@ const ModelsActions = ({ setEditingModel, setShowEdit, batchDeleteModels, + syncing, + previewing, + syncUpstream, + previewUpstreamDiff, + applyUpstreamOverwrite, compactMode, setCompactMode, t, @@ -42,6 +49,23 @@ const ModelsActions = ({ const [showGroupManagement, setShowGroupManagement] = useState(false); const [showAddPrefill, setShowAddPrefill] = useState(false); const [prefillInit, setPrefillInit] = useState({ id: undefined }); + const [showConflict, setShowConflict] = useState(false); + const [conflicts, setConflicts] = useState([]); + const [showSyncModal, setShowSyncModal] = useState(false); + const [syncLocale, setSyncLocale] = useState('zh'); + + const handleSyncUpstream = async (locale) => { + // 先预览 + const data = await previewUpstreamDiff?.({ locale }); + const conflictItems = data?.conflicts || []; + if (conflictItems.length > 0) { + setConflicts(conflictItems); + setShowConflict(true); + return; + } + // 无冲突,直接同步缺失 + await syncUpstream?.({ locale }); + }; // Handle delete selected models with confirmation const handleDeleteSelectedModels = () => { @@ -104,6 +128,41 @@ const ModelsActions = ({ {t('未配置模型')} + +
+ {t( + '模型社区需要大家的共同维护,如发现数据有误或想贡献新的模型数据,请访问:', + )} +
+ + https://github.com/basellm/llm-metadata + +
+ } + > + + + + )} + + {step === 0 && ( + + )} + {step === 1 && ( + + )} + + } + width={isMobile ? '100%' : 'small'} + > +
+ + + + +
+ + {step === 0 && ( +
+ setOption(e?.target?.value ?? e)} + type='card' + direction='horizontal' + aria-label='同步方式选择' + name='sync-mode-selection' + > + + {t('官方模型同步')} + + + {t('配置文件同步')} + + +
+ )} + + {step === 1 && ( +
+
+ {t('请选择同步语言')} +
+
+ setLocale(e?.target?.value ?? e)} + type='card' + direction='horizontal' + aria-label='语言选择' + name='sync-locale-selection' + > + + EN + + + ZH + + + JA + + +
+
+ )} + + ); +}; + +export default SyncWizardModal; diff --git a/web/src/components/table/models/modals/UpstreamConflictModal.jsx b/web/src/components/table/models/modals/UpstreamConflictModal.jsx new file mode 100644 index 000000000..3993f6dc7 --- /dev/null +++ b/web/src/components/table/models/modals/UpstreamConflictModal.jsx @@ -0,0 +1,324 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import React, { useEffect, useMemo, useState, useCallback } from 'react'; +import { + Modal, + Table, + Checkbox, + Typography, + Empty, + Tag, + Popover, + Input, +} from '@douyinfe/semi-ui'; +import { MousePointerClick } from 'lucide-react'; +import { useIsMobile } from '../../../../hooks/common/useIsMobile'; +import { MODEL_TABLE_PAGE_SIZE } from '../../../../constants'; +import { IconSearch } from '@douyinfe/semi-icons'; + +const { Text } = Typography; + +const FIELD_LABELS = { + description: '描述', + icon: '图标', + tags: '标签', + vendor: '供应商', + name_rule: '命名规则', + status: '状态', +}; +const FIELD_KEYS = Object.keys(FIELD_LABELS); + +const UpstreamConflictModal = ({ + visible, + onClose, + conflicts = [], + onSubmit, + t, + loading = false, +}) => { + const [selections, setSelections] = useState({}); + const isMobile = useIsMobile(); + const [currentPage, setCurrentPage] = useState(1); + const [searchKeyword, setSearchKeyword] = useState(''); + + const formatValue = (v) => { + if (v === null || v === undefined) return '-'; + if (typeof v === 'string') return v || '-'; + try { + return JSON.stringify(v, null, 2); + } catch (_) { + return String(v); + } + }; + + useEffect(() => { + if (visible) { + const init = {}; + conflicts.forEach((item) => { + init[item.model_name] = new Set(); + }); + setSelections(init); + setCurrentPage(1); + setSearchKeyword(''); + } else { + setSelections({}); + } + }, [visible, conflicts]); + + const toggleField = useCallback((modelName, field, checked) => { + setSelections((prev) => { + const next = { ...prev }; + const set = new Set(next[modelName] || []); + if (checked) set.add(field); + else set.delete(field); + next[modelName] = set; + return next; + }); + }, []); + + // 构造数据源与过滤后的数据源 + const dataSource = useMemo( + () => + (conflicts || []).map((c) => ({ + key: c.model_name, + model_name: c.model_name, + fields: c.fields || [], + })), + [conflicts], + ); + + const filteredDataSource = useMemo(() => { + const kw = (searchKeyword || '').toLowerCase(); + if (!kw) return dataSource; + return dataSource.filter((item) => + (item.model_name || '').toLowerCase().includes(kw), + ); + }, [dataSource, searchKeyword]); + + // 列头工具:当前过滤范围内可操作的行集合/勾选状态/批量设置 + const getPresentRowsForField = useCallback( + (fieldKey) => + (filteredDataSource || []).filter((row) => + (row.fields || []).some((f) => f.field === fieldKey), + ), + [filteredDataSource], + ); + + const getHeaderState = useCallback( + (fieldKey) => { + const presentRows = getPresentRowsForField(fieldKey); + const selectedCount = presentRows.filter((row) => + selections[row.model_name]?.has(fieldKey), + ).length; + const allCount = presentRows.length; + return { + headerChecked: allCount > 0 && selectedCount === allCount, + headerIndeterminate: selectedCount > 0 && selectedCount < allCount, + hasAny: allCount > 0, + }; + }, + [getPresentRowsForField, selections], + ); + + const applyHeaderChange = useCallback( + (fieldKey, checked) => { + setSelections((prev) => { + const next = { ...prev }; + getPresentRowsForField(fieldKey).forEach((row) => { + const set = new Set(next[row.model_name] || []); + if (checked) set.add(fieldKey); + else set.delete(fieldKey); + next[row.model_name] = set; + }); + return next; + }); + }, + [getPresentRowsForField], + ); + + const columns = useMemo(() => { + const base = [ + { + title: t('模型'), + dataIndex: 'model_name', + fixed: 'left', + render: (text) => {text}, + }, + ]; + + const cols = FIELD_KEYS.map((fieldKey) => { + const rawLabel = FIELD_LABELS[fieldKey] || fieldKey; + const label = t(rawLabel); + + const { headerChecked, headerIndeterminate, hasAny } = + getHeaderState(fieldKey); + if (!hasAny) return null; + const onHeaderChange = (e) => + applyHeaderChange(fieldKey, e?.target?.checked); + + return { + title: ( +
+ + {label} +
+ ), + dataIndex: fieldKey, + render: (_, record) => { + const f = (record.fields || []).find((x) => x.field === fieldKey); + if (!f) return -; + const checked = selections[record.model_name]?.has(fieldKey) || false; + return ( + + toggleField(record.model_name, fieldKey, e?.target?.checked) + } + > + +
+ + {t('本地')} + +
+                        {formatValue(f.local)}
+                      
+
+
+ + {t('官方')} + +
+                        {formatValue(f.upstream)}
+                      
+
+ + } + > + } + > + {t('点击查看差异')} + +
+
+ ); + }, + }; + }); + + return [...base, ...cols.filter(Boolean)]; + }, [ + t, + selections, + filteredDataSource, + getHeaderState, + applyHeaderChange, + toggleField, + ]); + + const pagedDataSource = useMemo(() => { + const start = (currentPage - 1) * MODEL_TABLE_PAGE_SIZE; + const end = start + MODEL_TABLE_PAGE_SIZE; + return filteredDataSource.slice(start, end); + }, [filteredDataSource, currentPage]); + + const handleOk = async () => { + const payload = Object.entries(selections) + .map(([modelName, set]) => ({ + model_name: modelName, + fields: Array.from(set || []), + })) + .filter((x) => x.fields.length > 0); + + const ok = await onSubmit?.(payload); + if (ok) onClose?.(); + }; + + return ( + + {dataSource.length === 0 ? ( + + ) : ( + <> +
+ {t('仅会覆盖你勾选的字段,未勾选的字段保持本地不变。')} +
+ {/* 搜索框 */} +
+ { + setSearchKeyword(v); + setCurrentPage(1); + }} + className='!w-full' + prefix={} + showClear + /> +
+ {filteredDataSource.length > 0 ? ( + setCurrentPage(page), + }} + scroll={{ x: 'max-content' }} + /> + ) : ( + + )} + + )} + + ); +}; + +export default UpstreamConflictModal; diff --git a/web/src/components/topup/RechargeCard.jsx b/web/src/components/topup/RechargeCard.jsx index 7fb06b0ca..03ea2b31e 100644 --- a/web/src/components/topup/RechargeCard.jsx +++ b/web/src/components/topup/RechargeCard.jsx @@ -21,6 +21,7 @@ import React, { useRef } from 'react'; import { Avatar, Typography, + Tag, Card, Button, Banner, @@ -29,7 +30,7 @@ import { Space, Row, Col, - Spin, + Spin, Tooltip } from '@douyinfe/semi-ui'; import { SiAlipay, SiWechat, SiStripe } from 'react-icons/si'; import { CreditCard, Coins, Wallet, BarChart2, TrendingUp } from 'lucide-react'; @@ -68,6 +69,7 @@ const RechargeCard = ({ userState, renderQuota, statusLoading, + topupInfo, }) => { const onlineFormApiRef = useRef(null); const redeemFormApiRef = useRef(null); @@ -261,44 +263,58 @@ const RechargeCard = ({ - - {payMethods.map((payMethod) => ( - - ))} - + {payMethods && payMethods.length > 0 ? ( + + {payMethods.map((payMethod) => { + const minTopupVal = Number(payMethod.min_topup) || 0; + const isStripe = payMethod.type === 'stripe'; + const disabled = + (!enableOnlineTopUp && !isStripe) || + (!enableStripeTopUp && isStripe) || + minTopupVal > Number(topUpCount || 0); + + const buttonEl = ( + + ); + + return disabled && minTopupVal > Number(topUpCount || 0) ? ( + + {buttonEl} + + ) : ( + {buttonEl} + ); + })} + + ) : ( +
+ {t('暂无可用的支付方式,请联系管理员配置')} +
+ )}
@@ -306,41 +322,60 @@ const RechargeCard = ({ {(enableOnlineTopUp || enableStripeTopUp) && ( - - {presetAmounts.map((preset, index) => ( - - ))} - +
+ {presetAmounts.map((preset, index) => { + const discount = preset.discount || topupInfo?.discount?.[preset.value] || 1.0; + const originalPrice = preset.value * priceRatio; + const discountedPrice = originalPrice * discount; + const hasDiscount = discount < 1.0; + const actualPay = discountedPrice; + const save = originalPrice - discountedPrice; + + return ( + { + selectPresetAmount(preset); + onlineFormApiRef.current?.setValue( + 'topUpCount', + preset.value, + ); + }} + > +
+ + + {formatLargeNumber(preset.value)} + {hasDiscount && ( + + {t('折').includes('off') ? + ((1 - parseFloat(discount)) * 100).toFixed(1) : + (discount * 10).toFixed(1)}{t('折')} + + )} + +
+ {t('实付')} {actualPay.toFixed(2)}, + {hasDiscount ? `${t('节省')} ${save.toFixed(2)}` : `${t('节省')} 0.00`} +
+
+
+ ); + })} +
)} diff --git a/web/src/components/topup/index.jsx b/web/src/components/topup/index.jsx index a09244488..929a47e39 100644 --- a/web/src/components/topup/index.jsx +++ b/web/src/components/topup/index.jsx @@ -80,6 +80,12 @@ const TopUp = () => { // 预设充值额度选项 const [presetAmounts, setPresetAmounts] = useState([]); const [selectedPreset, setSelectedPreset] = useState(null); + + // 充值配置信息 + const [topupInfo, setTopupInfo] = useState({ + amount_options: [], + discount: {} + }); const topUp = async () => { if (redemptionCode === '') { @@ -248,6 +254,99 @@ const TopUp = () => { } }; + // 获取充值配置信息 + const getTopupInfo = async () => { + try { + const res = await API.get('/api/user/topup/info'); + const { message, data, success } = res.data; + if (success) { + setTopupInfo({ + amount_options: data.amount_options || [], + discount: data.discount || {} + }); + + // 处理支付方式 + let payMethods = data.pay_methods || []; + try { + if (typeof payMethods === 'string') { + payMethods = JSON.parse(payMethods); + } + if (payMethods && payMethods.length > 0) { + // 检查name和type是否为空 + payMethods = payMethods.filter((method) => { + return method.name && method.type; + }); + // 如果没有color,则设置默认颜色 + payMethods = payMethods.map((method) => { + // 规范化最小充值数 + const normalizedMinTopup = Number(method.min_topup); + method.min_topup = Number.isFinite(normalizedMinTopup) ? normalizedMinTopup : 0; + + // Stripe 的最小充值从后端字段回填 + if (method.type === 'stripe' && (!method.min_topup || method.min_topup <= 0)) { + const stripeMin = Number(data.stripe_min_topup); + if (Number.isFinite(stripeMin)) { + method.min_topup = stripeMin; + } + } + + if (!method.color) { + if (method.type === 'alipay') { + method.color = 'rgba(var(--semi-blue-5), 1)'; + } else if (method.type === 'wxpay') { + method.color = 'rgba(var(--semi-green-5), 1)'; + } else if (method.type === 'stripe') { + method.color = 'rgba(var(--semi-purple-5), 1)'; + } else { + method.color = 'rgba(var(--semi-primary-5), 1)'; + } + } + return method; + }); + } else { + payMethods = []; + } + + // 如果启用了 Stripe 支付,添加到支付方法列表 + // 这个逻辑现在由后端处理,如果 Stripe 启用,后端会在 pay_methods 中包含它 + + setPayMethods(payMethods); + const enableStripeTopUp = data.enable_stripe_topup || false; + const enableOnlineTopUp = data.enable_online_topup || false; + const minTopUpValue = enableOnlineTopUp? data.min_topup : enableStripeTopUp? data.stripe_min_topup : 1; + setEnableOnlineTopUp(enableOnlineTopUp); + setEnableStripeTopUp(enableStripeTopUp); + setMinTopUp(minTopUpValue); + setTopUpCount(minTopUpValue); + + // 如果没有自定义充值数量选项,根据最小充值金额生成预设充值额度选项 + if (topupInfo.amount_options.length === 0) { + setPresetAmounts(generatePresetAmounts(minTopUpValue)); + } + + // 初始化显示实付金额 + getAmount(minTopUpValue); + } catch (e) { + console.log('解析支付方式失败:', e); + setPayMethods([]); + } + + // 如果有自定义充值数量选项,使用它们替换默认的预设选项 + if (data.amount_options && data.amount_options.length > 0) { + const customPresets = data.amount_options.map(amount => ({ + value: amount, + discount: data.discount[amount] || 1.0 + })); + setPresetAmounts(customPresets); + } + } else { + console.error('获取充值配置失败:', data); + } + } catch (error) { + console.error('获取充值配置异常:', error); + } + }; + // 获取邀请链接 const getAffLink = async () => { const res = await API.get('/api/user/aff'); @@ -290,52 +389,7 @@ const TopUp = () => { getUserQuota().then(); } setTransferAmount(getQuotaPerUnit()); - - let payMethods = localStorage.getItem('pay_methods'); - try { - payMethods = JSON.parse(payMethods); - if (payMethods && payMethods.length > 0) { - // 检查name和type是否为空 - payMethods = payMethods.filter((method) => { - return method.name && method.type; - }); - // 如果没有color,则设置默认颜色 - payMethods = payMethods.map((method) => { - if (!method.color) { - if (method.type === 'alipay') { - method.color = 'rgba(var(--semi-blue-5), 1)'; - } else if (method.type === 'wxpay') { - method.color = 'rgba(var(--semi-green-5), 1)'; - } else if (method.type === 'stripe') { - method.color = 'rgba(var(--semi-purple-5), 1)'; - } else { - method.color = 'rgba(var(--semi-primary-5), 1)'; - } - } - return method; - }); - } else { - payMethods = []; - } - - // 如果启用了 Stripe 支付,添加到支付方法列表 - if (statusState?.status?.enable_stripe_topup) { - const hasStripe = payMethods.some((method) => method.type === 'stripe'); - if (!hasStripe) { - payMethods.push({ - name: 'Stripe', - type: 'stripe', - color: 'rgba(var(--semi-purple-5), 1)', - }); - } - } - - setPayMethods(payMethods); - } catch (e) { - console.log(e); - showError(t('支付方式配置错误, 请联系管理员')); - } - }, [statusState?.status?.enable_stripe_topup]); + }, []); useEffect(() => { if (affFetchedRef.current) return; @@ -343,20 +397,18 @@ const TopUp = () => { getAffLink().then(); }, []); + // 在 statusState 可用时获取充值信息 + useEffect(() => { + getTopupInfo().then(); + }, []); + useEffect(() => { if (statusState?.status) { - const minTopUpValue = statusState.status.min_topup || 1; - setMinTopUp(minTopUpValue); - setTopUpCount(minTopUpValue); + // const minTopUpValue = statusState.status.min_topup || 1; + // setMinTopUp(minTopUpValue); + // setTopUpCount(minTopUpValue); setTopUpLink(statusState.status.top_up_link || ''); - setEnableOnlineTopUp(statusState.status.enable_online_topup || false); setPriceRatio(statusState.status.price || 1); - setEnableStripeTopUp(statusState.status.enable_stripe_topup || false); - - // 根据最小充值金额生成预设充值额度选项 - setPresetAmounts(generatePresetAmounts(minTopUpValue)); - // 初始化显示实付金额 - getAmount(minTopUpValue); setStatusLoading(false); } @@ -431,7 +483,11 @@ const TopUp = () => { const selectPresetAmount = (preset) => { setTopUpCount(preset.value); setSelectedPreset(preset.value); - setAmount(preset.value * priceRatio); + + // 计算实际支付金额,考虑折扣 + const discount = preset.discount || topupInfo.discount[preset.value] || 1.0; + const discountedAmount = preset.value * priceRatio * discount; + setAmount(discountedAmount); }; // 格式化大数字显示 @@ -475,6 +531,8 @@ const TopUp = () => { renderAmount={renderAmount} payWay={payWay} payMethods={payMethods} + amountNumber={amount} + discountRate={topupInfo?.discount?.[topUpCount] || 1.0} /> {/* 用户信息头部 */} @@ -512,6 +570,7 @@ const TopUp = () => { userState={userState} renderQuota={renderQuota} statusLoading={statusLoading} + topupInfo={topupInfo} /> diff --git a/web/src/components/topup/modals/PaymentConfirmModal.jsx b/web/src/components/topup/modals/PaymentConfirmModal.jsx index 76ea5eb22..1bffbfed1 100644 --- a/web/src/components/topup/modals/PaymentConfirmModal.jsx +++ b/web/src/components/topup/modals/PaymentConfirmModal.jsx @@ -36,7 +36,13 @@ const PaymentConfirmModal = ({ renderAmount, payWay, payMethods, + // 新增:用于显示折扣明细 + amountNumber, + discountRate, }) => { + const hasDiscount = discountRate && discountRate > 0 && discountRate < 1 && amountNumber > 0; + const originalAmount = hasDiscount ? (amountNumber / discountRate) : 0; + const discountAmount = hasDiscount ? (originalAmount - amountNumber) : 0; return ( ) : ( - - {renderAmount()} - +
+ + {renderAmount()} + + {hasDiscount && ( + + {Math.round(discountRate * 100)}% + + )} +
)} + {hasDiscount && !amountLoading && ( + <> +
+ + {t('原价')}: + + + {`${originalAmount.toFixed(2)} ${t('元')}`} + +
+
+ + {t('优惠')}: + + + {`- ${discountAmount.toFixed(2)} ${t('元')}`} + +
+ + )}
{t('支付方式')}: diff --git a/web/src/helpers/data.js b/web/src/helpers/data.js index 62353327c..b894a953c 100644 --- a/web/src/helpers/data.js +++ b/web/src/helpers/data.js @@ -28,7 +28,6 @@ export function setStatusData(data) { localStorage.setItem('enable_task', data.enable_task); localStorage.setItem('enable_data_export', data.enable_data_export); localStorage.setItem('chats', JSON.stringify(data.chats)); - localStorage.setItem('pay_methods', JSON.stringify(data.pay_methods)); localStorage.setItem( 'data_export_default_time', data.data_export_default_time, diff --git a/web/src/hooks/common/useHeaderBar.js b/web/src/hooks/common/useHeaderBar.js index 1b91511ba..3458a1d16 100644 --- a/web/src/hooks/common/useHeaderBar.js +++ b/web/src/hooks/common/useHeaderBar.js @@ -17,7 +17,7 @@ along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ -import { useState, useEffect, useContext, useCallback } from 'react'; +import { useState, useEffect, useContext, useCallback, useMemo } from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { UserContext } from '../../context/User'; @@ -51,6 +51,42 @@ export const useHeaderBar = ({ onMobileMenuToggle, drawerOpen }) => { const docsLink = statusState?.status?.docs_link || ''; const isDemoSiteMode = statusState?.status?.demo_site_enabled || false; + // 获取顶栏模块配置 + const headerNavModulesConfig = statusState?.status?.HeaderNavModules; + + // 使用useMemo确保headerNavModules正确响应statusState变化 + const headerNavModules = useMemo(() => { + if (headerNavModulesConfig) { + try { + const modules = JSON.parse(headerNavModulesConfig); + + // 处理向后兼容性:如果pricing是boolean,转换为对象格式 + if (typeof modules.pricing === 'boolean') { + modules.pricing = { + enabled: modules.pricing, + requireAuth: false, // 默认不需要登录鉴权 + }; + } + + return modules; + } catch (error) { + console.error('解析顶栏模块配置失败:', error); + return null; + } + } + return null; + }, [headerNavModulesConfig]); + + // 获取模型广场权限配置 + const pricingRequireAuth = useMemo(() => { + if (headerNavModules?.pricing) { + return typeof headerNavModules.pricing === 'object' + ? headerNavModules.pricing.requireAuth + : false; // 默认不需要登录 + } + return false; // 默认不需要登录 + }, [headerNavModules]); + const isConsoleRoute = location.pathname.startsWith('/console'); const theme = useTheme(); @@ -156,6 +192,8 @@ export const useHeaderBar = ({ onMobileMenuToggle, drawerOpen }) => { isConsoleRoute, theme, drawerOpen, + headerNavModules, + pricingRequireAuth, // Actions logout, diff --git a/web/src/hooks/common/useNavigation.js b/web/src/hooks/common/useNavigation.js index e0bac5521..f7e61a203 100644 --- a/web/src/hooks/common/useNavigation.js +++ b/web/src/hooks/common/useNavigation.js @@ -19,9 +19,21 @@ For commercial licensing, please contact support@quantumnous.com import { useMemo } from 'react'; -export const useNavigation = (t, docsLink) => { - const mainNavLinks = useMemo( - () => [ +export const useNavigation = (t, docsLink, headerNavModules) => { + const mainNavLinks = useMemo(() => { + // 默认配置,如果没有传入配置则显示所有模块 + const defaultModules = { + home: true, + console: true, + pricing: true, + docs: true, + about: true, + }; + + // 使用传入的配置或默认配置 + const modules = headerNavModules || defaultModules; + + const allLinks = [ { text: t('首页'), itemKey: 'home', @@ -52,9 +64,22 @@ export const useNavigation = (t, docsLink) => { itemKey: 'about', to: '/about', }, - ], - [t, docsLink], - ); + ]; + + // 根据配置过滤导航链接 + return allLinks.filter((link) => { + if (link.itemKey === 'docs') { + return docsLink && modules.docs; + } + if (link.itemKey === 'pricing') { + // 支持新的pricing配置格式 + return typeof modules.pricing === 'object' + ? modules.pricing.enabled + : modules.pricing; + } + return modules[link.itemKey] === true; + }); + }, [t, docsLink, headerNavModules]); return { mainNavLinks, diff --git a/web/src/hooks/common/useSidebar.js b/web/src/hooks/common/useSidebar.js new file mode 100644 index 000000000..5dce44f9e --- /dev/null +++ b/web/src/hooks/common/useSidebar.js @@ -0,0 +1,227 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import { useState, useEffect, useMemo, useContext } from 'react'; +import { StatusContext } from '../../context/Status'; +import { API } from '../../helpers'; + +export const useSidebar = () => { + const [statusState] = useContext(StatusContext); + const [userConfig, setUserConfig] = useState(null); + const [loading, setLoading] = useState(true); + + // 默认配置 + const defaultAdminConfig = { + chat: { + enabled: true, + playground: true, + chat: true, + }, + console: { + enabled: true, + detail: true, + token: true, + log: true, + midjourney: true, + task: true, + }, + personal: { + enabled: true, + topup: true, + personal: true, + }, + admin: { + enabled: true, + channel: true, + models: true, + redemption: true, + user: true, + setting: true, + }, + }; + + // 获取管理员配置 + const adminConfig = useMemo(() => { + if (statusState?.status?.SidebarModulesAdmin) { + try { + const config = JSON.parse(statusState.status.SidebarModulesAdmin); + return config; + } catch (error) { + return defaultAdminConfig; + } + } + return defaultAdminConfig; + }, [statusState?.status?.SidebarModulesAdmin]); + + // 加载用户配置的通用方法 + const loadUserConfig = async () => { + try { + setLoading(true); + const res = await API.get('/api/user/self'); + if (res.data.success && res.data.data.sidebar_modules) { + let config; + // 检查sidebar_modules是字符串还是对象 + if (typeof res.data.data.sidebar_modules === 'string') { + config = JSON.parse(res.data.data.sidebar_modules); + } else { + config = res.data.data.sidebar_modules; + } + setUserConfig(config); + } else { + // 当用户没有配置时,生成一个基于管理员配置的默认用户配置 + // 这样可以确保权限控制正确生效 + const defaultUserConfig = {}; + Object.keys(adminConfig).forEach((sectionKey) => { + if (adminConfig[sectionKey]?.enabled) { + defaultUserConfig[sectionKey] = { enabled: true }; + // 为每个管理员允许的模块设置默认值为true + Object.keys(adminConfig[sectionKey]).forEach((moduleKey) => { + if ( + moduleKey !== 'enabled' && + adminConfig[sectionKey][moduleKey] + ) { + defaultUserConfig[sectionKey][moduleKey] = true; + } + }); + } + }); + setUserConfig(defaultUserConfig); + } + } catch (error) { + // 出错时也生成默认配置,而不是设置为空对象 + const defaultUserConfig = {}; + Object.keys(adminConfig).forEach((sectionKey) => { + if (adminConfig[sectionKey]?.enabled) { + defaultUserConfig[sectionKey] = { enabled: true }; + Object.keys(adminConfig[sectionKey]).forEach((moduleKey) => { + if (moduleKey !== 'enabled' && adminConfig[sectionKey][moduleKey]) { + defaultUserConfig[sectionKey][moduleKey] = true; + } + }); + } + }); + setUserConfig(defaultUserConfig); + } finally { + setLoading(false); + } + }; + + // 刷新用户配置的方法(供外部调用) + const refreshUserConfig = async () => { + if (Object.keys(adminConfig).length > 0) { + await loadUserConfig(); + } + }; + + // 加载用户配置 + useEffect(() => { + // 只有当管理员配置加载完成后才加载用户配置 + if (Object.keys(adminConfig).length > 0) { + loadUserConfig(); + } + }, [adminConfig]); + + // 计算最终的显示配置 + const finalConfig = useMemo(() => { + const result = {}; + + // 确保adminConfig已加载 + if (!adminConfig || Object.keys(adminConfig).length === 0) { + return result; + } + + // 如果userConfig未加载,等待加载完成 + if (!userConfig) { + return result; + } + + // 遍历所有区域 + Object.keys(adminConfig).forEach((sectionKey) => { + const adminSection = adminConfig[sectionKey]; + const userSection = userConfig[sectionKey]; + + // 如果管理员禁用了整个区域,则该区域不显示 + if (!adminSection?.enabled) { + result[sectionKey] = { enabled: false }; + return; + } + + // 区域级别:用户可以选择隐藏管理员允许的区域 + // 当userSection存在时检查enabled状态,否则默认为true + const sectionEnabled = userSection ? userSection.enabled !== false : true; + result[sectionKey] = { enabled: sectionEnabled }; + + // 功能级别:只有管理员和用户都允许的功能才显示 + Object.keys(adminSection).forEach((moduleKey) => { + if (moduleKey === 'enabled') return; + + const adminAllowed = adminSection[moduleKey]; + // 当userSection存在时检查模块状态,否则默认为true + const userAllowed = userSection + ? userSection[moduleKey] !== false + : true; + + result[sectionKey][moduleKey] = + adminAllowed && userAllowed && sectionEnabled; + }); + }); + + return result; + }, [adminConfig, userConfig]); + + // 检查特定功能是否应该显示 + const isModuleVisible = (sectionKey, moduleKey = null) => { + if (moduleKey) { + return finalConfig[sectionKey]?.[moduleKey] === true; + } else { + return finalConfig[sectionKey]?.enabled === true; + } + }; + + // 检查区域是否有任何可见的功能 + const hasSectionVisibleModules = (sectionKey) => { + const section = finalConfig[sectionKey]; + if (!section?.enabled) return false; + + return Object.keys(section).some( + (key) => key !== 'enabled' && section[key] === true, + ); + }; + + // 获取区域的可见功能列表 + const getVisibleModules = (sectionKey) => { + const section = finalConfig[sectionKey]; + if (!section?.enabled) return []; + + return Object.keys(section).filter( + (key) => key !== 'enabled' && section[key] === true, + ); + }; + + return { + loading, + adminConfig, + userConfig, + finalConfig, + isModuleVisible, + hasSectionVisibleModules, + getVisibleModules, + refreshUserConfig, + }; +}; diff --git a/web/src/hooks/common/useUserPermissions.js b/web/src/hooks/common/useUserPermissions.js new file mode 100644 index 000000000..8d57f972e --- /dev/null +++ b/web/src/hooks/common/useUserPermissions.js @@ -0,0 +1,119 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ +import { useState, useEffect } from 'react'; +import { API } from '../../helpers'; + +/** + * 用户权限钩子 - 从后端获取用户权限,替代前端角色判断 + * 确保权限控制的安全性,防止前端绕过 + */ +export const useUserPermissions = () => { + const [permissions, setPermissions] = useState(null); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + + // 加载用户权限(从用户信息接口获取) + const loadPermissions = async () => { + try { + setLoading(true); + setError(null); + const res = await API.get('/api/user/self'); + if (res.data.success) { + const userPermissions = res.data.data.permissions; + setPermissions(userPermissions); + console.log('用户权限加载成功:', userPermissions); + } else { + setError(res.data.message || '获取权限失败'); + console.error('获取权限失败:', res.data.message); + } + } catch (error) { + setError('网络错误,请重试'); + console.error('加载用户权限异常:', error); + } finally { + setLoading(false); + } + }; + + useEffect(() => { + loadPermissions(); + }, []); + + // 检查是否有边栏设置权限 + const hasSidebarSettingsPermission = () => { + return permissions?.sidebar_settings === true; + }; + + // 检查是否允许访问特定的边栏区域 + const isSidebarSectionAllowed = (sectionKey) => { + if (!permissions?.sidebar_modules) return true; + const sectionPerms = permissions.sidebar_modules[sectionKey]; + return sectionPerms !== false; + }; + + // 检查是否允许访问特定的边栏模块 + const isSidebarModuleAllowed = (sectionKey, moduleKey) => { + if (!permissions?.sidebar_modules) return true; + const sectionPerms = permissions.sidebar_modules[sectionKey]; + + // 如果整个区域被禁用 + if (sectionPerms === false) return false; + + // 如果区域存在但模块被禁用 + if (sectionPerms && sectionPerms[moduleKey] === false) return false; + + return true; + }; + + // 获取允许的边栏区域列表 + const getAllowedSidebarSections = () => { + if (!permissions?.sidebar_modules) return []; + + return Object.keys(permissions.sidebar_modules).filter((sectionKey) => + isSidebarSectionAllowed(sectionKey), + ); + }; + + // 获取特定区域允许的模块列表 + const getAllowedSidebarModules = (sectionKey) => { + if (!permissions?.sidebar_modules) return []; + const sectionPerms = permissions.sidebar_modules[sectionKey]; + + if (sectionPerms === false) return []; + if (!sectionPerms || typeof sectionPerms !== 'object') return []; + + return Object.keys(sectionPerms).filter( + (moduleKey) => + moduleKey !== 'enabled' && sectionPerms[moduleKey] === true, + ); + }; + + return { + permissions, + loading, + error, + loadPermissions, + hasSidebarSettingsPermission, + isSidebarSectionAllowed, + isSidebarModuleAllowed, + getAllowedSidebarSections, + getAllowedSidebarModules, + }; +}; + +export default useUserPermissions; diff --git a/web/src/hooks/models/useModelsData.jsx b/web/src/hooks/models/useModelsData.jsx index 8e024b122..b1bd8b65d 100644 --- a/web/src/hooks/models/useModelsData.jsx +++ b/web/src/hooks/models/useModelsData.jsx @@ -95,6 +95,8 @@ export const useModelsData = () => { const [showAddVendor, setShowAddVendor] = useState(false); const [showEditVendor, setShowEditVendor] = useState(false); const [editingVendor, setEditingVendor] = useState({ id: undefined }); + const [syncing, setSyncing] = useState(false); + const [previewing, setPreviewing] = useState(false); const vendorMap = useMemo(() => { const map = {}; @@ -163,6 +165,91 @@ export const useModelsData = () => { await loadModels(page, pageSize); }; + // Sync upstream models/vendors for missing models only + const syncUpstream = async (opts = {}) => { + const locale = opts?.locale; + setSyncing(true); + try { + const body = {}; + if (locale) body.locale = locale; + const res = await API.post('/api/models/sync_upstream', body); + const { success, message, data } = res.data || {}; + if (success) { + const createdModels = data?.created_models || 0; + const createdVendors = data?.created_vendors || 0; + const skipped = (data?.skipped_models || []).length || 0; + showSuccess( + t( + `已同步:新增 ${createdModels} 模型,新增 ${createdVendors} 供应商,跳过 ${skipped} 项`, + ), + ); + await loadVendors(); + await refresh(); + } else { + showError(message || t('同步失败')); + } + } catch (e) { + showError(t('同步失败')); + } + setSyncing(false); + }; + + // Preview upstream differences + const previewUpstreamDiff = async (opts = {}) => { + const locale = opts?.locale; + setPreviewing(true); + try { + const url = `/api/models/sync_upstream/preview${locale ? `?locale=${locale}` : ''}`; + const res = await API.get(url); + const { success, message, data } = res.data || {}; + if (success) { + return data || { missing: [], conflicts: [] }; + } + showError(message || t('预览失败')); + return { missing: [], conflicts: [] }; + } catch (e) { + showError(t('预览失败')); + return { missing: [], conflicts: [] }; + } finally { + setPreviewing(false); + } + }; + + // Apply selected overwrite + const applyUpstreamOverwrite = async (payloadOrArray = []) => { + const isArray = Array.isArray(payloadOrArray); + const overwrite = isArray ? payloadOrArray : payloadOrArray.overwrite || []; + const locale = isArray ? undefined : payloadOrArray.locale; + setSyncing(true); + try { + const body = { overwrite }; + if (locale) body.locale = locale; + const res = await API.post('/api/models/sync_upstream', body); + const { success, message, data } = res.data || {}; + if (success) { + const createdModels = data?.created_models || 0; + const updatedModels = data?.updated_models || 0; + const createdVendors = data?.created_vendors || 0; + const skipped = (data?.skipped_models || []).length || 0; + showSuccess( + t( + `完成:新增 ${createdModels} 模型,更新 ${updatedModels} 模型,新增 ${createdVendors} 供应商,跳过 ${skipped} 项`, + ), + ); + await loadVendors(); + await refresh(); + return true; + } + showError(message || t('同步失败')); + return false; + } catch (e) { + showError(t('同步失败')); + return false; + } finally { + setSyncing(false); + } + }; + // Search models with keyword and vendor const searchModels = async () => { const { searchKeyword = '', searchVendor = '' } = getFormValues(); @@ -375,6 +462,7 @@ export const useModelsData = () => { copyText, // Pagination + setActivePage, handlePageChange, handlePageSizeChange, @@ -398,5 +486,12 @@ export const useModelsData = () => { // Translation t, + + // Upstream sync + syncing, + previewing, + syncUpstream, + previewUpstreamDiff, + applyUpstreamOverwrite, }; }; diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index 62c8ebb79..c86fb0e7f 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -1,6 +1,5 @@ { "主页": "Home", - "文档": "Docs", "控制台": "Console", "$%.6f 额度": "$%.6f quota", "或": "or", @@ -165,7 +164,6 @@ "出现错误,第 ${count} 次重试中...": "Error occurred, retry attempt ${count}...", "首页": "Home", "渠道": "Channel", - "渠道管理": "Channels", "令牌": "Tokens", "兑换额度": "Redeem", "充值": "Recharge", @@ -174,7 +172,6 @@ "设置": "Settings", "关于": "About", "价格": "Pricing", - "聊天": "Chat", "注销成功!": "Logout successful!", "注销": "Logout", "登录": "Sign in", @@ -443,9 +440,7 @@ "兑换码": "Redeem Code", "管理用户": "Manage Users", "额度明细": "Quota Details", - "个人设置": "Personal Settings", "运营设置": "Operation Settings", - "系统设置": "System Settings", "其他设置": "Other Settings", "项目仓库地址": "Project Repository Address", "可在设置页面设置关于内容,支持 HTML & Markdown": "The About content can be set on the settings page, supporting HTML & Markdown", @@ -585,10 +580,7 @@ "确定是否要修复数据库一致性?": "Are you sure you want to repair database consistency?", "进行该操作时,可能导致渠道访问错误,请仅在数据库出现问题时使用": "When performing this operation, it may cause channel access errors. Please only use it when there is a problem with the database.", "当前没有可用的启用令牌,请确认是否有令牌处于启用状态!": "There are currently no enablement tokens available, please confirm if one is enabled!", - "令牌管理": "API Keys", - "使用日志": "Usage log", "Midjourney日志": "Midjourney", - "数据看板": "Dashboard", "模型列表": "Model list", "常见问题": "FAQ", "免费体验": "Free trial", @@ -628,7 +620,6 @@ "重置成功": "Reset successful", "加载数据出错:": "Error loading data:", "加载数据时发生错误: ": "An error occurred while loading data:", - "保存成功": "Saved successfully", "部分保存失败,请重试": "Partial saving failed, please try again", "请检查输入": "Please check your input", "如何区分不同分组不同模型的价格:供参考的配置方式": "How to distinguish the prices of different models in different groups: configuration method for reference", @@ -653,7 +644,6 @@ "窗口等待": "window wait", "失败": "Failed", "绘图": "Drawing", - "绘图日志": "Drawing log", "放大": "Upscalers", "微妙放大": "Upscale (Subtle)", "创造放大": "Upscale (Creative)", @@ -793,7 +783,6 @@ "邮箱": "Email", "已有账户?": "Already have an account?", "创意任务": "Tasks", - "用户管理": "User Management", "任务ID(点击查看详情)": "Task ID (click to view details)", "进度": "schedule", "花费时间": "spend time", @@ -943,7 +932,6 @@ "不是合法的 JSON 字符串": "Not a valid JSON string", "个人中心": "Personal center", "代理商": "Agent", - "钱包管理": "Wallet", "备注": "Remark", "工作台": "Workbench", "已复制:": "Copied:", @@ -957,7 +945,6 @@ "黑夜模式": "Dark mode", "管理员设置": "Admin", "待更新": "To be updated", - "模型广场": "Pricing", "支付中..": "Paying", "查看图片": "View pictures", "并发限制": "Concurrency limit", @@ -1043,7 +1030,6 @@ "在iframe中加载": "Load in iframe", "补全倍率": "Completion ratio", "保存分组数据失败": "Failed to save group data", - "保存失败,请重试": "Save failed, please try again", "没有可用的使用信息": "No usage information available", "使用详情": "Usage details", "收起": "Collapse", @@ -1187,7 +1173,6 @@ "知识库 ID": "Knowledge Base ID", "请输入知识库 ID,例如:123456": "Please enter knowledge base ID, e.g.: 123456", "可选值": "Optional value", - "任务日志": "Task log", "你好": "Hello", "你好,请问有什么可以帮助您的吗?": "Hello, how may I help you?", "用户分组": "Your default group", @@ -1329,7 +1314,6 @@ "当剩余额度低于此数值时,系统将通过选择的方式发送通知": "When the remaining quota is lower than this value, the system will send a notification through the selected method", "Webhook请求结构": "Webhook request structure", "只支持https,系统将以 POST 方式发送通知,请确保地址可以接收 POST 请求": "Only https is supported, the system will send a notification through POST, please ensure the address can receive POST requests", - "保存设置": "Save settings", "通知邮箱": "Notification email", "设置用于接收额度预警的邮箱地址,不填则使用账号绑定的邮箱": "Set the email address for receiving quota warning notifications, if not set, the email address bound to the account will be used", "留空则使用账号绑定的邮箱": "If left blank, the email address bound to the account will be used", @@ -1501,7 +1485,6 @@ "收益": "Earnings", "无邀请人": "No Inviter", "邀请人": "Inviter", - "兑换码管理": "Redemption Code", "设置兑换码的基本信息": "Set redemption code basic information", "设置兑换码的额度和数量": "Set redemption code quota and quantity", "编辑用户": "Edit User", @@ -1595,7 +1578,6 @@ "加载中...": "Loading...", "正在跳转...": "Redirecting...", "暂无公告": "No Notice", - "操练场": "Playground", "欢迎使用,请完成以下设置以开始使用系统": "Welcome to use, please complete the following settings to start using the system", "数据库检查": "Database Check", "验证数据库连接状态": "Verify database connection status", @@ -1811,7 +1793,6 @@ "系统提示覆盖": "System prompt override", "模型: {{ratio}}": "Model: {{ratio}}", "专属倍率": "Exclusive group ratio", - "模型管理": "Models", "匹配类型": "Matching type", "描述": "Description", "供应商": "Vendor", @@ -1820,7 +1801,7 @@ "已绑定渠道": "Bound channels", "更新时间": "Update time", "未配置模型": "No model configured", - "预填组管理": "Pre-filled group management", + "预填组管理": "Pre-filled group", "搜索供应商": "Search vendor", "新增供应商": "Add vendor", "创建新的模型": "Create new model", @@ -2012,7 +1993,7 @@ "安全验证": "Security verification", "验证": "Verify", "为了保护账户安全,请验证您的两步验证码。": "To protect account security, please verify your two-factor authentication code.", - "支持6位TOTP验证码或8位备用码": "Supports 6-digit TOTP verification code or 8-digit backup code", + "支持6位TOTP验证码或8位备用码,可到`个人设置-安全设置-两步验证设置`配置或查看。": "Supports 6-digit TOTP verification code or 8-digit backup code, can be configured or viewed in `Personal Settings - Security Settings - Two-Factor Authentication Settings`.", "获取密钥失败": "Failed to get key", "查看密钥": "View key", "查看渠道密钥": "View channel key", @@ -2027,5 +2008,91 @@ "音频输出补全相关的倍率设置,键为模型名称,值为倍率": "Audio output completion related ratio settings, key is model name, value is ratio", "为一个 JSON 文本,键为模型名称,值为倍率,例如:{\"gpt-image-1\": 2}": "A JSON text with model name as key and ratio as value, e.g.: {\"gpt-image-1\": 2}", "为一个 JSON 文本,键为模型名称,值为倍率,例如:{\"gpt-4o-audio-preview\": 16}": "A JSON text with model name as key and ratio as value, e.g.: {\"gpt-4o-audio-preview\": 16}", - "为一个 JSON 文本,键为模型名称,值为倍率,例如:{\"gpt-4o-realtime\": 2}": "A JSON text with model name as key and ratio as value, e.g.: {\"gpt-4o-realtime\": 2}" + "为一个 JSON 文本,键为模型名称,值为倍率,例如:{\"gpt-4o-realtime\": 2}": "A JSON text with model name as key and ratio as value, e.g.: {\"gpt-4o-realtime\": 2}", + "顶栏管理": "Header Management", + "控制顶栏模块显示状态,全局生效": "Control header module display status, global effect", + "用户主页,展示系统信息": "User homepage, displaying system information", + "用户控制面板,管理账户": "User control panel for account management", + "模型广场": "Model Marketplace", + "模型定价,需要登录访问": "Model pricing, requires login to access", + "文档": "Documentation", + "系统文档和帮助信息": "System documentation and help information", + "关于系统的详细信息": "Detailed information about the system", + "重置为默认": "Reset to Default", + "保存设置": "Save Settings", + "已重置为默认配置": "Reset to default configuration", + "保存成功": "Saved successfully", + "保存失败,请重试": "Save failed, please try again", + "侧边栏管理(全局控制)": "Sidebar Management (Global Control)", + "全局控制侧边栏区域和功能显示,管理员隐藏的功能用户无法启用": "Global control of sidebar areas and functions, users cannot enable functions hidden by administrators", + "聊天区域": "Chat Area", + "操练场和聊天功能": "Playground and chat functions", + "操练场": "Playground", + "AI模型测试环境": "AI model testing environment", + "聊天": "Chat", + "聊天会话管理": "Chat session management", + "控制台区域": "Console Area", + "数据管理和日志查看": "Data management and log viewing", + "数据看板": "Dashboard", + "系统数据统计": "System data statistics", + "令牌管理": "Token Management", + "API令牌管理": "API token management", + "使用日志": "Usage Logs", + "API使用记录": "API usage records", + "绘图日志": "Drawing Logs", + "绘图任务记录": "Drawing task records", + "任务日志": "Task Logs", + "系统任务记录": "System task records", + "个人中心区域": "Personal Center Area", + "用户个人功能": "User personal functions", + "钱包管理": "Wallet Management", + "余额充值管理": "Balance recharge management", + "个人设置": "Personal Settings", + "个人信息设置": "Personal information settings", + "管理员区域": "Administrator Area", + "系统管理功能": "System management functions", + "渠道管理": "Channel Management", + "API渠道配置": "API channel configuration", + "模型管理": "Model Management", + "AI模型配置": "AI model configuration", + "兑换码管理": "Redemption Code Management", + "兑换码生成管理": "Redemption code generation management", + "用户管理": "User Management", + "用户账户管理": "User account management", + "系统设置": "System Settings", + "系统参数配置": "System parameter configuration", + "边栏设置": "Sidebar Settings", + "您可以个性化设置侧边栏的要显示功能": "You can customize the sidebar functions to display", + "保存边栏设置": "Save Sidebar Settings", + "侧边栏设置保存成功": "Sidebar settings saved successfully", + "需要登录访问": "Require Login", + "开启后未登录用户无法访问模型广场": "When enabled, unauthenticated users cannot access the model marketplace", + "参与官方同步": "Participate in official sync", + "关闭后,此模型将不会被\"同步官方\"自动覆盖或创建": "When turned off, this model will be skipped by Sync official (no auto create/overwrite)", + "同步": "Sync", + "同步向导": "Sync Wizard", + "选择方式": "Select method", + "选择同步来源": "Select sync source", + "选择语言": "Select language", + "选择同步语言": "Select sync language", + "请选择同步语言": "Please select sync language", + "从官方模型库同步": "Sync from official model library", + "官方模型同步": "Official models sync", + "从配置文件同步": "Sync from config file", + "配置文件同步": "Config file sync", + "开始同步": "Start sync", + "选择要覆盖的冲突项": "Select conflict items to overwrite", + "点击查看差异": "Click to view differences", + "无冲突项": "No conflict items", + "应用覆盖": "Apply overwrite", + "仅会覆盖你勾选的字段,未勾选的字段保持本地不变。": "Only selected fields will be overwritten, unselected fields remain unchanged.", + "本地": "Local", + "官方": "Official", + "模型社区需要大家的共同维护,如发现数据有误或想贡献新的模型数据,请访问:": "The model community needs everyone's contribution. If you find incorrect data or want to contribute new models, please visit:", + "是": "Yes", + "否": "No", + "原价": "Original price", + "优惠": "Discount", + "折": "% off", + "节省": "Save" } diff --git a/web/src/index.css b/web/src/index.css index fbbd76827..251bf79c6 100644 --- a/web/src/index.css +++ b/web/src/index.css @@ -71,6 +71,11 @@ code { margin-right: 0; } +.sidebar-nav .semi-navigation-item-text { + flex: 1; + min-width: 0; +} + .semi-navigation-item, .semi-navigation-sub-title { height: 100% !important; @@ -184,6 +189,7 @@ code { justify-content: center; align-items: center; padding: 12px; + margin-top: auto; cursor: pointer; background-color: var(--semi-color-bg-0); position: sticky; diff --git a/web/src/pages/Setting/Operation/SettingsGeneral.jsx b/web/src/pages/Setting/Operation/SettingsGeneral.jsx index c94c0dd5a..5af750ec3 100644 --- a/web/src/pages/Setting/Operation/SettingsGeneral.jsx +++ b/web/src/pages/Setting/Operation/SettingsGeneral.jsx @@ -130,17 +130,19 @@ export default function GeneralSettings(props) { showClear /> -
- setShowQuotaWarning(true)} - /> - + {inputs.QuotaPerUnit !== '500000' && inputs.QuotaPerUnit !== 500000 && ( + + setShowQuotaWarning(true)} + /> + + )} - + . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import React, { useEffect, useState, useContext } from 'react'; +import { + Button, + Card, + Col, + Form, + Row, + Switch, + Typography, +} from '@douyinfe/semi-ui'; +import { API, showError, showSuccess } from '../../../helpers'; +import { useTranslation } from 'react-i18next'; +import { StatusContext } from '../../../context/Status'; + +const { Text } = Typography; + +export default function SettingsHeaderNavModules(props) { + const { t } = useTranslation(); + const [loading, setLoading] = useState(false); + const [statusState, statusDispatch] = useContext(StatusContext); + + // 顶栏模块管理状态 + const [headerNavModules, setHeaderNavModules] = useState({ + home: true, + console: true, + pricing: { + enabled: true, + requireAuth: false, // 默认不需要登录鉴权 + }, + docs: true, + about: true, + }); + + // 处理顶栏模块配置变更 + function handleHeaderNavModuleChange(moduleKey) { + return (checked) => { + const newModules = { ...headerNavModules }; + if (moduleKey === 'pricing') { + // 对于pricing模块,只更新enabled属性 + newModules[moduleKey] = { + ...newModules[moduleKey], + enabled: checked, + }; + } else { + newModules[moduleKey] = checked; + } + setHeaderNavModules(newModules); + }; + } + + // 处理模型广场权限控制变更 + function handlePricingAuthChange(checked) { + const newModules = { ...headerNavModules }; + newModules.pricing = { + ...newModules.pricing, + requireAuth: checked, + }; + setHeaderNavModules(newModules); + } + + // 重置顶栏模块为默认配置 + function resetHeaderNavModules() { + const defaultModules = { + home: true, + console: true, + pricing: { + enabled: true, + requireAuth: false, + }, + docs: true, + about: true, + }; + setHeaderNavModules(defaultModules); + showSuccess(t('已重置为默认配置')); + } + + // 保存配置 + async function onSubmit() { + setLoading(true); + try { + const res = await API.put('/api/option/', { + key: 'HeaderNavModules', + value: JSON.stringify(headerNavModules), + }); + const { success, message } = res.data; + if (success) { + showSuccess(t('保存成功')); + + // 立即更新StatusContext中的状态 + statusDispatch({ + type: 'set', + payload: { + ...statusState.status, + HeaderNavModules: JSON.stringify(headerNavModules), + }, + }); + + // 刷新父组件状态 + if (props.refresh) { + await props.refresh(); + } + } else { + showError(message); + } + } catch (error) { + showError(t('保存失败,请重试')); + } finally { + setLoading(false); + } + } + + useEffect(() => { + // 从 props.options 中获取配置 + if (props.options && props.options.HeaderNavModules) { + try { + const modules = JSON.parse(props.options.HeaderNavModules); + + // 处理向后兼容性:如果pricing是boolean,转换为对象格式 + if (typeof modules.pricing === 'boolean') { + modules.pricing = { + enabled: modules.pricing, + requireAuth: false, // 默认不需要登录鉴权 + }; + } + + setHeaderNavModules(modules); + } catch (error) { + // 使用默认配置 + const defaultModules = { + home: true, + console: true, + pricing: { + enabled: true, + requireAuth: false, + }, + docs: true, + about: true, + }; + setHeaderNavModules(defaultModules); + } + } + }, [props.options]); + + // 模块配置数据 + const moduleConfigs = [ + { + key: 'home', + title: t('首页'), + description: t('用户主页,展示系统信息'), + }, + { + key: 'console', + title: t('控制台'), + description: t('用户控制面板,管理账户'), + }, + { + key: 'pricing', + title: t('模型广场'), + description: t('模型定价,需要登录访问'), + hasSubConfig: true, // 标识该模块有子配置 + }, + { + key: 'docs', + title: t('文档'), + description: t('系统文档和帮助信息'), + }, + { + key: 'about', + title: t('关于'), + description: t('关于系统的详细信息'), + }, + ]; + + return ( + + + + {moduleConfigs.map((module) => ( + + +
+
+
+ {module.title} +
+ + {module.description} + +
+
+ +
+
+ + {/* 为模型广场添加权限控制子开关 */} + {module.key === 'pricing' && + (module.key === 'pricing' + ? headerNavModules[module.key]?.enabled + : headerNavModules[module.key]) && ( +
+
+
+
+ {t('需要登录访问')} +
+ + {t('开启后未登录用户无法访问模型广场')} + +
+
+ +
+
+
+ )} +
+ + ))} + + +
+ + +
+ + + ); +} diff --git a/web/src/pages/Setting/Operation/SettingsMonitoring.jsx b/web/src/pages/Setting/Operation/SettingsMonitoring.jsx index f4de4f6ea..d64f19b63 100644 --- a/web/src/pages/Setting/Operation/SettingsMonitoring.jsx +++ b/web/src/pages/Setting/Operation/SettingsMonitoring.jsx @@ -38,6 +38,8 @@ export default function SettingsMonitoring(props) { AutomaticDisableChannelEnabled: false, AutomaticEnableChannelEnabled: false, AutomaticDisableKeywords: '', + 'monitor_setting.auto_test_channel_enabled': false, + 'monitor_setting.auto_test_channel_minutes': 10, }); const refForm = useRef(); const [inputsRow, setInputsRow] = useState(inputs); @@ -98,6 +100,40 @@ export default function SettingsMonitoring(props) { style={{ marginBottom: 15 }} > + + + + setInputs({ + ...inputs, + 'monitor_setting.auto_test_channel_enabled': value, + }) + } + /> + + + + setInputs({ + ...inputs, + 'monitor_setting.auto_test_channel_minutes': parseInt(value), + }) + } + /> + + . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import React, { useState, useEffect, useContext } from 'react'; +import { useTranslation } from 'react-i18next'; +import { + Card, + Form, + Button, + Switch, + Row, + Col, + Typography, +} from '@douyinfe/semi-ui'; +import { API, showSuccess, showError } from '../../../helpers'; +import { StatusContext } from '../../../context/Status'; + +const { Text } = Typography; + +export default function SettingsSidebarModulesAdmin(props) { + const { t } = useTranslation(); + const [loading, setLoading] = useState(false); + const [statusState, statusDispatch] = useContext(StatusContext); + + // 左侧边栏模块管理状态(管理员全局控制) + const [sidebarModulesAdmin, setSidebarModulesAdmin] = useState({ + chat: { + enabled: true, + playground: true, + chat: true, + }, + console: { + enabled: true, + detail: true, + token: true, + log: true, + midjourney: true, + task: true, + }, + personal: { + enabled: true, + topup: true, + personal: true, + }, + admin: { + enabled: true, + channel: true, + models: true, + redemption: true, + user: true, + setting: true, + }, + }); + + // 处理区域级别开关变更 + function handleSectionChange(sectionKey) { + return (checked) => { + const newModules = { + ...sidebarModulesAdmin, + [sectionKey]: { + ...sidebarModulesAdmin[sectionKey], + enabled: checked, + }, + }; + setSidebarModulesAdmin(newModules); + }; + } + + // 处理功能级别开关变更 + function handleModuleChange(sectionKey, moduleKey) { + return (checked) => { + const newModules = { + ...sidebarModulesAdmin, + [sectionKey]: { + ...sidebarModulesAdmin[sectionKey], + [moduleKey]: checked, + }, + }; + setSidebarModulesAdmin(newModules); + }; + } + + // 重置为默认配置 + function resetSidebarModules() { + const defaultModules = { + chat: { + enabled: true, + playground: true, + chat: true, + }, + console: { + enabled: true, + detail: true, + token: true, + log: true, + midjourney: true, + task: true, + }, + personal: { + enabled: true, + topup: true, + personal: true, + }, + admin: { + enabled: true, + channel: true, + models: true, + redemption: true, + user: true, + setting: true, + }, + }; + setSidebarModulesAdmin(defaultModules); + showSuccess(t('已重置为默认配置')); + } + + // 保存配置 + async function onSubmit() { + setLoading(true); + try { + const res = await API.put('/api/option/', { + key: 'SidebarModulesAdmin', + value: JSON.stringify(sidebarModulesAdmin), + }); + const { success, message } = res.data; + if (success) { + showSuccess(t('保存成功')); + + // 立即更新StatusContext中的状态 + statusDispatch({ + type: 'set', + payload: { + ...statusState.status, + SidebarModulesAdmin: JSON.stringify(sidebarModulesAdmin), + }, + }); + + // 刷新父组件状态 + if (props.refresh) { + await props.refresh(); + } + } else { + showError(message); + } + } catch (error) { + showError(t('保存失败,请重试')); + } finally { + setLoading(false); + } + } + + useEffect(() => { + // 从 props.options 中获取配置 + if (props.options && props.options.SidebarModulesAdmin) { + try { + const modules = JSON.parse(props.options.SidebarModulesAdmin); + setSidebarModulesAdmin(modules); + } catch (error) { + // 使用默认配置 + const defaultModules = { + chat: { enabled: true, playground: true, chat: true }, + console: { + enabled: true, + detail: true, + token: true, + log: true, + midjourney: true, + task: true, + }, + personal: { enabled: true, topup: true, personal: true }, + admin: { + enabled: true, + channel: true, + models: true, + redemption: true, + user: true, + setting: true, + }, + }; + setSidebarModulesAdmin(defaultModules); + } + } + }, [props.options]); + + // 区域配置数据 + const sectionConfigs = [ + { + key: 'chat', + title: t('聊天区域'), + description: t('操练场和聊天功能'), + modules: [ + { + key: 'playground', + title: t('操练场'), + description: t('AI模型测试环境'), + }, + { key: 'chat', title: t('聊天'), description: t('聊天会话管理') }, + ], + }, + { + key: 'console', + title: t('控制台区域'), + description: t('数据管理和日志查看'), + modules: [ + { key: 'detail', title: t('数据看板'), description: t('系统数据统计') }, + { key: 'token', title: t('令牌管理'), description: t('API令牌管理') }, + { key: 'log', title: t('使用日志'), description: t('API使用记录') }, + { + key: 'midjourney', + title: t('绘图日志'), + description: t('绘图任务记录'), + }, + { key: 'task', title: t('任务日志'), description: t('系统任务记录') }, + ], + }, + { + key: 'personal', + title: t('个人中心区域'), + description: t('用户个人功能'), + modules: [ + { key: 'topup', title: t('钱包管理'), description: t('余额充值管理') }, + { + key: 'personal', + title: t('个人设置'), + description: t('个人信息设置'), + }, + ], + }, + { + key: 'admin', + title: t('管理员区域'), + description: t('系统管理功能'), + modules: [ + { key: 'channel', title: t('渠道管理'), description: t('API渠道配置') }, + { key: 'models', title: t('模型管理'), description: t('AI模型配置') }, + { + key: 'redemption', + title: t('兑换码管理'), + description: t('兑换码生成管理'), + }, + { key: 'user', title: t('用户管理'), description: t('用户账户管理') }, + { + key: 'setting', + title: t('系统设置'), + description: t('系统参数配置'), + }, + ], + }, + ]; + + return ( + + + {sectionConfigs.map((section) => ( +
+ {/* 区域标题和总开关 */} +
+
+
+ {section.title} +
+ + {section.description} + +
+ +
+ + {/* 功能模块网格 */} + + {section.modules.map((module) => ( +
+ +
+
+
+ {module.title} +
+ + {module.description} + +
+
+ +
+
+
+ + ))} + + + ))} + +
+ + +
+ + + ); +} diff --git a/web/src/pages/Setting/Payment/SettingsPaymentGateway.jsx b/web/src/pages/Setting/Payment/SettingsPaymentGateway.jsx index ce8958dca..d681b6a27 100644 --- a/web/src/pages/Setting/Payment/SettingsPaymentGateway.jsx +++ b/web/src/pages/Setting/Payment/SettingsPaymentGateway.jsx @@ -41,6 +41,8 @@ export default function SettingsPaymentGateway(props) { TopupGroupRatio: '', CustomCallbackAddress: '', PayMethods: '', + AmountOptions: '', + AmountDiscount: '', }); const [originInputs, setOriginInputs] = useState({}); const formApiRef = useRef(null); @@ -62,7 +64,30 @@ export default function SettingsPaymentGateway(props) { TopupGroupRatio: props.options.TopupGroupRatio || '', CustomCallbackAddress: props.options.CustomCallbackAddress || '', PayMethods: props.options.PayMethods || '', + AmountOptions: props.options.AmountOptions || '', + AmountDiscount: props.options.AmountDiscount || '', }; + + // 美化 JSON 展示 + try { + if (currentInputs.AmountOptions) { + currentInputs.AmountOptions = JSON.stringify( + JSON.parse(currentInputs.AmountOptions), + null, + 2, + ); + } + } catch {} + try { + if (currentInputs.AmountDiscount) { + currentInputs.AmountDiscount = JSON.stringify( + JSON.parse(currentInputs.AmountDiscount), + null, + 2, + ); + } + } catch {} + setInputs(currentInputs); setOriginInputs({ ...currentInputs }); formApiRef.current.setValues(currentInputs); @@ -93,6 +118,20 @@ export default function SettingsPaymentGateway(props) { } } + if (originInputs['AmountOptions'] !== inputs.AmountOptions && inputs.AmountOptions.trim() !== '') { + if (!verifyJSON(inputs.AmountOptions)) { + showError(t('自定义充值数量选项不是合法的 JSON 数组')); + return; + } + } + + if (originInputs['AmountDiscount'] !== inputs.AmountDiscount && inputs.AmountDiscount.trim() !== '') { + if (!verifyJSON(inputs.AmountDiscount)) { + showError(t('充值金额折扣配置不是合法的 JSON 对象')); + return; + } + } + setLoading(true); try { const options = [ @@ -123,6 +162,12 @@ export default function SettingsPaymentGateway(props) { if (originInputs['PayMethods'] !== inputs.PayMethods) { options.push({ key: 'PayMethods', value: inputs.PayMethods }); } + if (originInputs['AmountOptions'] !== inputs.AmountOptions) { + options.push({ key: 'payment_setting.amount_options', value: inputs.AmountOptions }); + } + if (originInputs['AmountDiscount'] !== inputs.AmountDiscount) { + options.push({ key: 'payment_setting.amount_discount', value: inputs.AmountDiscount }); + } // 发送请求 const requestQueue = options.map((opt) => @@ -228,6 +273,37 @@ export default function SettingsPaymentGateway(props) { placeholder={t('为一个 JSON 文本')} autosize /> + + + + + + + + + + + + + diff --git a/web/src/pages/Setting/Personal/SettingsSidebarModulesUser.jsx b/web/src/pages/Setting/Personal/SettingsSidebarModulesUser.jsx new file mode 100644 index 000000000..939e76cfc --- /dev/null +++ b/web/src/pages/Setting/Personal/SettingsSidebarModulesUser.jsx @@ -0,0 +1,453 @@ +/* +Copyright (C) 2025 QuantumNous + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +For commercial licensing, please contact support@quantumnous.com +*/ + +import { useState, useEffect, useContext } from 'react'; +import { useTranslation } from 'react-i18next'; +import { + Card, + Button, + Switch, + Typography, + Row, + Col, + Avatar, +} from '@douyinfe/semi-ui'; +import { API, showSuccess, showError } from '../../../helpers'; +import { StatusContext } from '../../../context/Status'; +import { UserContext } from '../../../context/User'; +import { useUserPermissions } from '../../../hooks/common/useUserPermissions'; +import { useSidebar } from '../../../hooks/common/useSidebar'; +import { Settings } from 'lucide-react'; + +const { Text } = Typography; + +export default function SettingsSidebarModulesUser() { + const { t } = useTranslation(); + const [loading, setLoading] = useState(false); + const [statusState] = useContext(StatusContext); + + // 使用后端权限验证替代前端角色判断 + const { + permissions, + loading: permissionsLoading, + hasSidebarSettingsPermission, + isSidebarSectionAllowed, + isSidebarModuleAllowed, + } = useUserPermissions(); + + // 使用useSidebar钩子获取刷新方法 + const { refreshUserConfig } = useSidebar(); + + // 如果没有边栏设置权限,不显示此组件 + if (!permissionsLoading && !hasSidebarSettingsPermission()) { + return null; + } + + // 权限加载中,显示加载状态 + if (permissionsLoading) { + return null; + } + + // 根据用户权限生成默认配置 + const generateDefaultConfig = () => { + const defaultConfig = {}; + + // 聊天区域 - 所有用户都可以访问 + if (isSidebarSectionAllowed('chat')) { + defaultConfig.chat = { + enabled: true, + playground: isSidebarModuleAllowed('chat', 'playground'), + chat: isSidebarModuleAllowed('chat', 'chat'), + }; + } + + // 控制台区域 - 所有用户都可以访问 + if (isSidebarSectionAllowed('console')) { + defaultConfig.console = { + enabled: true, + detail: isSidebarModuleAllowed('console', 'detail'), + token: isSidebarModuleAllowed('console', 'token'), + log: isSidebarModuleAllowed('console', 'log'), + midjourney: isSidebarModuleAllowed('console', 'midjourney'), + task: isSidebarModuleAllowed('console', 'task'), + }; + } + + // 个人中心区域 - 所有用户都可以访问 + if (isSidebarSectionAllowed('personal')) { + defaultConfig.personal = { + enabled: true, + topup: isSidebarModuleAllowed('personal', 'topup'), + personal: isSidebarModuleAllowed('personal', 'personal'), + }; + } + + // 管理员区域 - 只有管理员可以访问 + if (isSidebarSectionAllowed('admin')) { + defaultConfig.admin = { + enabled: true, + channel: isSidebarModuleAllowed('admin', 'channel'), + models: isSidebarModuleAllowed('admin', 'models'), + redemption: isSidebarModuleAllowed('admin', 'redemption'), + user: isSidebarModuleAllowed('admin', 'user'), + setting: isSidebarModuleAllowed('admin', 'setting'), + }; + } + + return defaultConfig; + }; + + // 用户个人左侧边栏模块设置 + const [sidebarModulesUser, setSidebarModulesUser] = useState({}); + + // 管理员全局配置 + const [adminConfig, setAdminConfig] = useState(null); + + // 处理区域级别开关变更 + function handleSectionChange(sectionKey) { + return (checked) => { + const newModules = { + ...sidebarModulesUser, + [sectionKey]: { + ...sidebarModulesUser[sectionKey], + enabled: checked, + }, + }; + setSidebarModulesUser(newModules); + console.log('用户边栏区域配置变更:', sectionKey, checked, newModules); + }; + } + + // 处理功能级别开关变更 + function handleModuleChange(sectionKey, moduleKey) { + return (checked) => { + const newModules = { + ...sidebarModulesUser, + [sectionKey]: { + ...sidebarModulesUser[sectionKey], + [moduleKey]: checked, + }, + }; + setSidebarModulesUser(newModules); + console.log( + '用户边栏功能配置变更:', + sectionKey, + moduleKey, + checked, + newModules, + ); + }; + } + + // 重置为默认配置(基于权限过滤) + function resetSidebarModules() { + const defaultConfig = generateDefaultConfig(); + setSidebarModulesUser(defaultConfig); + showSuccess(t('已重置为默认配置')); + console.log('用户边栏配置重置为默认:', defaultConfig); + } + + // 保存配置 + async function onSubmit() { + setLoading(true); + try { + console.log('保存用户边栏配置:', sidebarModulesUser); + const res = await API.put('/api/user/self', { + sidebar_modules: JSON.stringify(sidebarModulesUser), + }); + const { success, message } = res.data; + if (success) { + showSuccess(t('保存成功')); + console.log('用户边栏配置保存成功'); + + // 刷新useSidebar钩子中的用户配置,实现实时更新 + await refreshUserConfig(); + console.log('用户边栏配置已刷新,边栏将立即更新'); + } else { + showError(message); + console.error('用户边栏配置保存失败:', message); + } + } catch (error) { + showError(t('保存失败,请重试')); + console.error('用户边栏配置保存异常:', error); + } finally { + setLoading(false); + } + } + + // 统一的配置加载逻辑 + useEffect(() => { + const loadConfigs = async () => { + try { + // 获取管理员全局配置 + if (statusState?.status?.SidebarModulesAdmin) { + const adminConf = JSON.parse(statusState.status.SidebarModulesAdmin); + setAdminConfig(adminConf); + console.log('加载管理员边栏配置:', adminConf); + } + + // 获取用户个人配置 + const userRes = await API.get('/api/user/self'); + if (userRes.data.success && userRes.data.data.sidebar_modules) { + let userConf; + // 检查sidebar_modules是字符串还是对象 + if (typeof userRes.data.data.sidebar_modules === 'string') { + userConf = JSON.parse(userRes.data.data.sidebar_modules); + } else { + userConf = userRes.data.data.sidebar_modules; + } + console.log('从API加载的用户配置:', userConf); + + // 确保用户配置也经过权限过滤 + const filteredUserConf = {}; + Object.keys(userConf).forEach((sectionKey) => { + if (isSidebarSectionAllowed(sectionKey)) { + filteredUserConf[sectionKey] = { ...userConf[sectionKey] }; + // 过滤不允许的模块 + Object.keys(userConf[sectionKey]).forEach((moduleKey) => { + if ( + moduleKey !== 'enabled' && + !isSidebarModuleAllowed(sectionKey, moduleKey) + ) { + delete filteredUserConf[sectionKey][moduleKey]; + } + }); + } + }); + setSidebarModulesUser(filteredUserConf); + console.log('权限过滤后的用户配置:', filteredUserConf); + } else { + // 如果用户没有配置,使用权限过滤后的默认配置 + const defaultConfig = generateDefaultConfig(); + setSidebarModulesUser(defaultConfig); + console.log('用户无配置,使用默认配置:', defaultConfig); + } + } catch (error) { + console.error('加载边栏配置失败:', error); + // 出错时也使用默认配置 + const defaultConfig = generateDefaultConfig(); + setSidebarModulesUser(defaultConfig); + } + }; + + // 只有权限加载完成且有边栏设置权限时才加载配置 + if (!permissionsLoading && hasSidebarSettingsPermission()) { + loadConfigs(); + } + }, [ + statusState, + permissionsLoading, + hasSidebarSettingsPermission, + isSidebarSectionAllowed, + isSidebarModuleAllowed, + ]); + + // 检查功能是否被管理员允许 + const isAllowedByAdmin = (sectionKey, moduleKey = null) => { + if (!adminConfig) return true; + + if (moduleKey) { + return ( + adminConfig[sectionKey]?.enabled && adminConfig[sectionKey]?.[moduleKey] + ); + } else { + return adminConfig[sectionKey]?.enabled; + } + }; + + // 区域配置数据(根据后端权限过滤) + const sectionConfigs = [ + { + key: 'chat', + title: t('聊天区域'), + description: t('操练场和聊天功能'), + modules: [ + { + key: 'playground', + title: t('操练场'), + description: t('AI模型测试环境'), + }, + { key: 'chat', title: t('聊天'), description: t('聊天会话管理') }, + ], + }, + { + key: 'console', + title: t('控制台区域'), + description: t('数据管理和日志查看'), + modules: [ + { key: 'detail', title: t('数据看板'), description: t('系统数据统计') }, + { key: 'token', title: t('令牌管理'), description: t('API令牌管理') }, + { key: 'log', title: t('使用日志'), description: t('API使用记录') }, + { + key: 'midjourney', + title: t('绘图日志'), + description: t('绘图任务记录'), + }, + { key: 'task', title: t('任务日志'), description: t('系统任务记录') }, + ], + }, + { + key: 'personal', + title: t('个人中心区域'), + description: t('用户个人功能'), + modules: [ + { key: 'topup', title: t('钱包管理'), description: t('余额充值管理') }, + { + key: 'personal', + title: t('个人设置'), + description: t('个人信息设置'), + }, + ], + }, + { + key: 'admin', + title: t('管理员区域'), + description: t('系统管理功能'), + modules: [ + { key: 'channel', title: t('渠道管理'), description: t('API渠道配置') }, + { key: 'models', title: t('模型管理'), description: t('AI模型配置') }, + { + key: 'redemption', + title: t('兑换码管理'), + description: t('兑换码生成管理'), + }, + { key: 'user', title: t('用户管理'), description: t('用户账户管理') }, + { + key: 'setting', + title: t('系统设置'), + description: t('系统参数配置'), + }, + ], + }, + ] + .filter((section) => { + // 使用后端权限验证替代前端角色判断 + return isSidebarSectionAllowed(section.key); + }) + .map((section) => ({ + ...section, + modules: section.modules.filter((module) => + isSidebarModuleAllowed(section.key, module.key), + ), + })) + .filter( + (section) => + // 过滤掉没有可用模块的区域 + section.modules.length > 0 && isAllowedByAdmin(section.key), + ); + + return ( + + {/* 卡片头部 */} +
+ + + +
+ + {t('左侧边栏个人设置')} + +
+ {t('个性化设置左侧边栏的显示内容')} +
+
+
+ +
+ + {t('您可以个性化设置侧边栏的要显示功能')} + +
+ + {sectionConfigs.map((section) => ( +
+ {/* 区域标题和总开关 */} +
+
+
+ {section.title} +
+ + {section.description} + +
+ +
+ + {/* 功能模块网格 */} + + {section.modules.map((module) => ( +
+ +
+
+
+ {module.title} +
+ + {module.description} + +
+
+ +
+
+
+ + ))} + + + ))} + + {/* 底部按钮 */} +
+ + +
+ + ); +} diff --git a/web/src/pages/Setting/Ratio/ModelRationNotSetEditor.jsx b/web/src/pages/Setting/Ratio/ModelRationNotSetEditor.jsx index 5ca8686b8..5d6dd154f 100644 --- a/web/src/pages/Setting/Ratio/ModelRationNotSetEditor.jsx +++ b/web/src/pages/Setting/Ratio/ModelRationNotSetEditor.jsx @@ -130,9 +130,7 @@ export default function ModelRatioNotSetEditor(props) { // 在 return 语句之前,先处理过滤和分页逻辑 const filteredModels = models.filter((model) => - searchText - ? model.name.toLowerCase().includes(searchText.toLowerCase()) - : true, + searchText ? model.name.includes(searchText) : true, ); // 然后基于过滤后的数据计算分页数据 diff --git a/web/src/pages/Setting/Ratio/ModelSettingsVisualEditor.jsx b/web/src/pages/Setting/Ratio/ModelSettingsVisualEditor.jsx index 3a075251a..b5ad3e58d 100644 --- a/web/src/pages/Setting/Ratio/ModelSettingsVisualEditor.jsx +++ b/web/src/pages/Setting/Ratio/ModelSettingsVisualEditor.jsx @@ -99,9 +99,7 @@ export default function ModelSettingsVisualEditor(props) { // 在 return 语句之前,先处理过滤和分页逻辑 const filteredModels = models.filter((model) => { - const keywordMatch = searchText - ? model.name.toLowerCase().includes(searchText.toLowerCase()) - : true; + const keywordMatch = searchText ? model.name.includes(searchText) : true; const conflictMatch = conflictOnly ? model.hasConflict : true; return keywordMatch && conflictMatch; }); diff --git a/web/src/pages/Setting/Ratio/UpstreamRatioSync.jsx b/web/src/pages/Setting/Ratio/UpstreamRatioSync.jsx index 0f483717b..86efcce02 100644 --- a/web/src/pages/Setting/Ratio/UpstreamRatioSync.jsx +++ b/web/src/pages/Setting/Ratio/UpstreamRatioSync.jsx @@ -151,8 +151,17 @@ export default function UpstreamRatioSync(props) { setChannelEndpoints((prev) => { const merged = { ...prev }; transferData.forEach((channel) => { - if (!merged[channel.key]) { - merged[channel.key] = DEFAULT_ENDPOINT; + const id = channel.key; + const base = channel._originalData?.base_url || ''; + const name = channel.label || ''; + const isOfficial = + id === -100 || + base === 'https://basellm.github.io' || + name === '官方倍率预设'; + if (!merged[id]) { + merged[id] = isOfficial + ? '/llm-metadata/api/newapi/ratio_config-v1-base.json' + : DEFAULT_ENDPOINT; } }); return merged;