2023-04-22 20:39:27 +08:00
|
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-09-03 14:30:25 +08:00
|
|
|
|
"fmt"
|
2023-04-22 20:39:27 +08:00
|
|
|
|
"net/http"
|
|
|
|
|
|
"strings"
|
2023-07-30 12:44:41 +08:00
|
|
|
|
|
2025-10-11 15:30:09 +08:00
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
|
|
|
|
"github.com/QuantumNous/new-api/model"
|
|
|
|
|
|
"github.com/QuantumNous/new-api/setting"
|
|
|
|
|
|
"github.com/QuantumNous/new-api/setting/console_setting"
|
2026-01-12 18:47:45 +08:00
|
|
|
|
"github.com/QuantumNous/new-api/setting/operation_setting"
|
2025-10-11 15:30:09 +08:00
|
|
|
|
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
|
|
|
|
|
"github.com/QuantumNous/new-api/setting/system_setting"
|
|
|
|
|
|
|
2023-07-30 12:44:41 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-04-22 20:39:27 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-03-06 21:36:51 +08:00
|
|
|
|
var completionRatioMetaOptionKeys = []string{
|
|
|
|
|
|
"ModelPrice",
|
|
|
|
|
|
"ModelRatio",
|
|
|
|
|
|
"CompletionRatio",
|
|
|
|
|
|
"CacheRatio",
|
|
|
|
|
|
"CreateCacheRatio",
|
|
|
|
|
|
"ImageRatio",
|
|
|
|
|
|
"AudioRatio",
|
|
|
|
|
|
"AudioCompletionRatio",
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-18 14:22:54 +08:00
|
|
|
|
func isVisiblePublicKeyOption(key string) bool {
|
|
|
|
|
|
switch key {
|
|
|
|
|
|
case "WaffoPancakeWebhookPublicKey", "WaffoPancakeWebhookTestKey":
|
|
|
|
|
|
return true
|
|
|
|
|
|
default:
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-06 21:36:51 +08:00
|
|
|
|
func collectModelNamesFromOptionValue(raw string, modelNames map[string]struct{}) {
|
|
|
|
|
|
if strings.TrimSpace(raw) == "" {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var parsed map[string]any
|
|
|
|
|
|
if err := common.UnmarshalJsonStr(raw, &parsed); err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for modelName := range parsed {
|
|
|
|
|
|
modelNames[modelName] = struct{}{}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func buildCompletionRatioMetaValue(optionValues map[string]string) string {
|
|
|
|
|
|
modelNames := make(map[string]struct{})
|
|
|
|
|
|
for _, key := range completionRatioMetaOptionKeys {
|
|
|
|
|
|
collectModelNamesFromOptionValue(optionValues[key], modelNames)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
meta := make(map[string]ratio_setting.CompletionRatioInfo, len(modelNames))
|
|
|
|
|
|
for modelName := range modelNames {
|
|
|
|
|
|
meta[modelName] = ratio_setting.GetCompletionRatioInfo(modelName)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
jsonBytes, err := common.Marshal(meta)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "{}"
|
|
|
|
|
|
}
|
|
|
|
|
|
return string(jsonBytes)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-22 20:39:27 +08:00
|
|
|
|
func GetOptions(c *gin.Context) {
|
|
|
|
|
|
var options []*model.Option
|
2026-03-06 21:36:51 +08:00
|
|
|
|
optionValues := make(map[string]string)
|
2023-04-22 20:39:27 +08:00
|
|
|
|
common.OptionMapRWMutex.Lock()
|
|
|
|
|
|
for k, v := range common.OptionMap {
|
2026-03-06 21:36:51 +08:00
|
|
|
|
value := common.Interface2String(v)
|
2026-04-18 14:22:54 +08:00
|
|
|
|
isSensitiveKey := strings.HasSuffix(k, "Token") ||
|
2026-01-03 12:37:50 +08:00
|
|
|
|
strings.HasSuffix(k, "Secret") ||
|
|
|
|
|
|
strings.HasSuffix(k, "Key") ||
|
|
|
|
|
|
strings.HasSuffix(k, "secret") ||
|
2026-04-18 14:22:54 +08:00
|
|
|
|
strings.HasSuffix(k, "api_key")
|
|
|
|
|
|
if isSensitiveKey && !isVisiblePublicKeyOption(k) {
|
2023-04-22 20:39:27 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
options = append(options, &model.Option{
|
|
|
|
|
|
Key: k,
|
2026-03-06 21:36:51 +08:00
|
|
|
|
Value: value,
|
2023-04-22 20:39:27 +08:00
|
|
|
|
})
|
2026-03-06 21:36:51 +08:00
|
|
|
|
for _, optionKey := range completionRatioMetaOptionKeys {
|
|
|
|
|
|
if optionKey == k {
|
|
|
|
|
|
optionValues[k] = value
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-22 20:39:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
common.OptionMapRWMutex.Unlock()
|
2026-03-06 21:36:51 +08:00
|
|
|
|
options = append(options, &model.Option{
|
|
|
|
|
|
Key: "CompletionRatioMeta",
|
|
|
|
|
|
Value: buildCompletionRatioMetaValue(optionValues),
|
|
|
|
|
|
})
|
2023-04-22 20:39:27 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
"message": "",
|
|
|
|
|
|
"data": options,
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-03 14:30:25 +08:00
|
|
|
|
type OptionUpdateRequest struct {
|
|
|
|
|
|
Key string `json:"key"`
|
|
|
|
|
|
Value any `json:"value"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-22 20:39:27 +08:00
|
|
|
|
func UpdateOption(c *gin.Context) {
|
2025-09-03 14:30:25 +08:00
|
|
|
|
var option OptionUpdateRequest
|
2026-03-06 21:36:51 +08:00
|
|
|
|
err := common.DecodeJson(c.Request.Body, &option)
|
2023-04-22 20:39:27 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": "无效的参数",
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-09-03 14:30:25 +08:00
|
|
|
|
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)
|
|
|
|
|
|
}
|
2023-04-22 20:39:27 +08:00
|
|
|
|
switch option.Key {
|
|
|
|
|
|
case "GitHubOAuthEnabled":
|
|
|
|
|
|
if option.Value == "true" && common.GitHubClientId == "" {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
2023-10-03 14:19:03 +08:00
|
|
|
|
"message": "无法启用 GitHub OAuth,请先填入 GitHub Client Id 以及 GitHub Client Secret!",
|
2023-04-22 20:39:27 +08:00
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-22 19:06:53 +08:00
|
|
|
|
case "discord.enabled":
|
|
|
|
|
|
if option.Value == "true" && system_setting.GetDiscordSettings().ClientId == "" {
|
2025-11-22 18:38:24 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": "无法启用 Discord OAuth,请先填入 Discord Client Id 以及 Discord Client Secret!",
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-03-11 22:00:31 +08:00
|
|
|
|
case "oidc.enabled":
|
2025-03-31 00:46:13 +08:00
|
|
|
|
if option.Value == "true" && system_setting.GetOIDCSettings().ClientId == "" {
|
2025-02-28 15:18:03 +08:00
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": "无法启用 OIDC 登录,请先填入 OIDC Client Id 以及 OIDC Client Secret!",
|
|
|
|
|
|
})
|
2025-03-31 00:46:13 +08:00
|
|
|
|
return
|
2025-02-28 15:18:03 +08:00
|
|
|
|
}
|
2024-11-10 23:56:22 +08:00
|
|
|
|
case "LinuxDOOAuthEnabled":
|
|
|
|
|
|
if option.Value == "true" && common.LinuxDOClientId == "" {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": "无法启用 LinuxDO OAuth,请先填入 LinuxDO Client Id 以及 LinuxDO Client Secret!",
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-07-30 12:44:41 +08:00
|
|
|
|
case "EmailDomainRestrictionEnabled":
|
|
|
|
|
|
if option.Value == "true" && len(common.EmailDomainWhitelist) == 0 {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": "无法启用邮箱域名限制,请先填入限制的邮箱域名!",
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-04-22 20:39:27 +08:00
|
|
|
|
case "WeChatAuthEnabled":
|
|
|
|
|
|
if option.Value == "true" && common.WeChatServerAddress == "" {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": "无法启用微信登录,请先填入微信登录相关配置信息!",
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
case "TurnstileCheckEnabled":
|
|
|
|
|
|
if option.Value == "true" && common.TurnstileSiteKey == "" {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!",
|
|
|
|
|
|
})
|
2025-03-31 00:46:13 +08:00
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
case "TelegramOAuthEnabled":
|
|
|
|
|
|
if option.Value == "true" && common.TelegramBotToken == "" {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": "无法启用 Telegram OAuth,请先填入 Telegram Bot Token!",
|
|
|
|
|
|
})
|
2023-04-22 20:39:27 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-09-20 18:09:40 +08:00
|
|
|
|
case "GroupRatio":
|
2025-09-03 14:30:25 +08:00
|
|
|
|
err = ratio_setting.CheckGroupRatio(option.Value.(string))
|
2024-09-20 18:09:40 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-08-30 23:28:09 +08:00
|
|
|
|
case "ImageRatio":
|
2025-09-19 14:21:32 +08:00
|
|
|
|
err = ratio_setting.UpdateImageRatioByJSONString(option.Value.(string))
|
2025-08-30 23:28:09 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": "图片倍率设置失败: " + err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
case "AudioRatio":
|
2025-09-19 14:21:32 +08:00
|
|
|
|
err = ratio_setting.UpdateAudioRatioByJSONString(option.Value.(string))
|
2025-08-30 23:28:09 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": "音频倍率设置失败: " + err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
case "AudioCompletionRatio":
|
2025-09-19 14:21:32 +08:00
|
|
|
|
err = ratio_setting.UpdateAudioCompletionRatioByJSONString(option.Value.(string))
|
2025-08-30 23:28:09 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": "音频补全倍率设置失败: " + err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-02-06 19:46:59 +08:00
|
|
|
|
case "CreateCacheRatio":
|
|
|
|
|
|
err = ratio_setting.UpdateCreateCacheRatioByJSONString(option.Value.(string))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": "缓存创建倍率设置失败: " + err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-05-05 20:00:06 +08:00
|
|
|
|
case "ModelRequestRateLimitGroup":
|
2025-09-03 14:30:25 +08:00
|
|
|
|
err = setting.CheckModelRequestRateLimitGroup(option.Value.(string))
|
2025-05-05 20:00:06 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-01-12 18:47:45 +08:00
|
|
|
|
case "AutomaticDisableStatusCodes":
|
|
|
|
|
|
_, err = operation_setting.ParseHTTPStatusCodeRanges(option.Value.(string))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-01-14 14:34:12 +08:00
|
|
|
|
case "AutomaticRetryStatusCodes":
|
|
|
|
|
|
_, err = operation_setting.ParseHTTPStatusCodeRanges(option.Value.(string))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-06-14 00:40:29 +08:00
|
|
|
|
case "console_setting.api_info":
|
2025-09-03 14:30:25 +08:00
|
|
|
|
err = console_setting.ValidateConsoleSettings(option.Value.(string), "ApiInfo")
|
✨ feat: major refactor and enhancement of Detail dashboard component & add api url display
- **Code Organization & Architecture:**
- Restructured component with clear sections (Hooks, Constants, Helper Functions, etc.)
- Added comprehensive code organization comments for better maintainability
- Extracted reusable helper functions and constants for better separation of concerns
- **Performance Optimizations:**
- Implemented extensive use of useCallback and useMemo hooks for expensive operations
- Optimized data processing pipeline with dedicated processing functions
- Memoized chart configurations, performance metrics, and grouped stats data
- Cached helper functions like getTrendSpec, handleCopyUrl, and modal handlers
- **UI/UX Enhancements:**
- Added Empty state component with construction illustrations for better UX
- Implemented responsive grid layout with conditional API info section visibility
- Enhanced button styling with consistent rounded design and hover effects
- Added mini trend charts to statistics cards for visual data representation
- Improved form field consistency with reusable createFormField helper
- **Feature Improvements:**
- Added self-use mode detection to conditionally hide/show API information section
- Enhanced chart configurations with centralized CHART_CONFIG constant
- Improved time handling with dedicated helper functions (getTimeInterval, getInitialTimestamp)
- Added comprehensive performance metrics calculation (RPM/TPM trends)
- Implemented advanced data aggregation and processing workflows
- **Code Quality & Maintainability:**
- Extracted complex data processing logic into dedicated functions
- Added proper prop destructuring and state organization
- Implemented consistent naming conventions and helper utilities
- Enhanced error handling and loading states management
- Added comprehensive JSDoc-style comments for better code documentation
- **Technical Debt Reduction:**
- Replaced repetitive form field definitions with reusable components
- Consolidated chart update logic into centralized updateChartSpec function
- Improved data flow with better state management patterns
- Reduced code duplication through strategic use of helper functions
This refactor significantly improves component performance, maintainability, and user experience while maintaining backward compatibility and existing functionality.
2025-06-09 17:44:23 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-06-14 00:40:29 +08:00
|
|
|
|
case "console_setting.announcements":
|
2025-09-03 14:30:25 +08:00
|
|
|
|
err = console_setting.ValidateConsoleSettings(option.Value.(string), "Announcements")
|
2025-06-10 20:10:07 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-06-14 00:40:29 +08:00
|
|
|
|
case "console_setting.faq":
|
2025-09-03 14:30:25 +08:00
|
|
|
|
err = console_setting.ValidateConsoleSettings(option.Value.(string), "FAQ")
|
2025-06-10 20:10:07 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-06-15 02:54:54 +08:00
|
|
|
|
case "console_setting.uptime_kuma_groups":
|
2025-09-03 14:30:25 +08:00
|
|
|
|
err = console_setting.ValidateConsoleSettings(option.Value.(string), "UptimeKumaGroups")
|
2025-06-15 02:54:54 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": false,
|
|
|
|
|
|
"message": err.Error(),
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-04-22 20:39:27 +08:00
|
|
|
|
}
|
2025-09-03 14:30:25 +08:00
|
|
|
|
err = model.UpdateOption(option.Key, option.Value.(string))
|
2023-04-22 20:39:27 +08:00
|
|
|
|
if err != nil {
|
2025-07-14 21:59:42 +08:00
|
|
|
|
common.ApiError(c, err)
|
2023-04-22 20:39:27 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
|
"success": true,
|
|
|
|
|
|
"message": "",
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|