Repository navigation
feat: CNY localization, channel UX fixes, and JSON editor improvements #4832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 11 commits
71ea68f
9435a3d
e749047
bae904b
b9e177f
4c69588
3c35ddb
06fed0e
54fa429
fd90731
bfeaa8d
9e1db04
6357a5b
1ff60b5
45396fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,15 +118,14 @@ func logHelper(ctx context.Context, level string, msg string) { | |
| } | ||
|
|
||
| func LogQuota(quota int) string { | ||
| // 新逻辑:根据额度展示类型输出 | ||
| // 计价基准为人民币,直接除以 QuotaPerUnit 即为 CNY | ||
| q := float64(quota) | ||
| switch operation_setting.GetQuotaDisplayType() { | ||
| case operation_setting.QuotaDisplayTypeCNY: | ||
| usd := q / common.QuotaPerUnit | ||
| cny := usd * operation_setting.USDExchangeRate | ||
| cny := q / common.QuotaPerUnit | ||
| return fmt.Sprintf("¥%.6f 额度", cny) | ||
| case operation_setting.QuotaDisplayTypeCustom: | ||
| usd := q / common.QuotaPerUnit | ||
| base := q / common.QuotaPerUnit | ||
| rate := operation_setting.GetGeneralSetting().CustomCurrencyExchangeRate | ||
| symbol := operation_setting.GetGeneralSetting().CustomCurrencySymbol | ||
| if symbol == "" { | ||
|
|
@@ -135,24 +134,24 @@ func LogQuota(quota int) string { | |
| if rate <= 0 { | ||
| rate = 1 | ||
| } | ||
| v := usd * rate | ||
| v := base * rate | ||
| return fmt.Sprintf("%s%.6f 额度", symbol, v) | ||
| case operation_setting.QuotaDisplayTypeTokens: | ||
| return fmt.Sprintf("%d 点额度", quota) | ||
| default: // USD | ||
| return fmt.Sprintf("$%.6f 额度", q/common.QuotaPerUnit) | ||
| usd := q / common.QuotaPerUnit / operation_setting.USDExchangeRate | ||
| return fmt.Sprintf("$%.6f 额度", usd) | ||
| } | ||
|
Comment on lines
141
to
144
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate USDExchangeRate before division. Line 142 (and line 168 in 🛡️ Proposed fix to add validation default: // USD
+ exchangeRate := operation_setting.USDExchangeRate
+ if exchangeRate <= 0 {
+ exchangeRate = 1.0
+ }
- usd := q / common.QuotaPerUnit / operation_setting.USDExchangeRate
+ usd := q / common.QuotaPerUnit / exchangeRate
return fmt.Sprintf("$%.6f 额度", usd)
}Apply a similar fix to 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| func FormatQuota(quota int) string { | ||
| q := float64(quota) | ||
| switch operation_setting.GetQuotaDisplayType() { | ||
| case operation_setting.QuotaDisplayTypeCNY: | ||
| usd := q / common.QuotaPerUnit | ||
| cny := usd * operation_setting.USDExchangeRate | ||
| cny := q / common.QuotaPerUnit | ||
| return fmt.Sprintf("¥%.6f", cny) | ||
| case operation_setting.QuotaDisplayTypeCustom: | ||
| usd := q / common.QuotaPerUnit | ||
| base := q / common.QuotaPerUnit | ||
| rate := operation_setting.GetGeneralSetting().CustomCurrencyExchangeRate | ||
| symbol := operation_setting.GetGeneralSetting().CustomCurrencySymbol | ||
| if symbol == "" { | ||
|
|
@@ -161,12 +160,13 @@ func FormatQuota(quota int) string { | |
| if rate <= 0 { | ||
| rate = 1 | ||
| } | ||
| v := usd * rate | ||
| v := base * rate | ||
| return fmt.Sprintf("%s%.6f", symbol, v) | ||
| case operation_setting.QuotaDisplayTypeTokens: | ||
| return fmt.Sprintf("%d", quota) | ||
| default: | ||
| return fmt.Sprintf("$%.6f", q/common.QuotaPerUnit) | ||
| default: // USD | ||
| usd := q / common.QuotaPerUnit / operation_setting.USDExchangeRate | ||
| return fmt.Sprintf("$%.6f", usd) | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 860
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 112
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 6307
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 3586
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 1081
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 45
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 1474
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 469
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 305
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 4805
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 45
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 1027
Validate
USDExchangeRateto prevent division by zero.The default case divides by
operation_setting.USDExchangeRateat lines 53 and 99 without validation. If the exchange rate is zero or becomes zero (e.g., from a failedstrconv.ParseFloatin model/option.go:369), this will cause a runtime panic. Additional unguarded divisions exist in logger/logger.go at lines 142 and 168.🛡️ Proposed fix to add validation
switch operation_setting.GetQuotaDisplayType() { case operation_setting.QuotaDisplayTypeCNY: amount = amount / common.QuotaPerUnit case operation_setting.QuotaDisplayTypeTokens: // amount 保持 tokens 数值 default: + if operation_setting.USDExchangeRate <= 0 { + operation_setting.USDExchangeRate = 1.0 // fallback to 1:1 if invalid + } amount = amount / common.QuotaPerUnit / operation_setting.USDExchangeRate }📝 Committable suggestion
🤖 Prompt for AI Agents