Repository navigation
fix: chat user quota - #7800
Merged
Merged
fix: chat user quota#7800
Conversation
shaohuzhang1
commented
Nov 10, 2026
| tokenLimit: quota.token_limit == null ? 10_000_000 : Math.round(quota.token_limit / 1000), | ||
| }) | ||
| } catch { | ||
| // 请求层已提示错误;保留默认表单,用户可自行设置后再保存 |
Contributor
Author
There was a problem hiding this comment.
Main issue: the default value was not converted to the new frontend unit.
Issues
-
Critical: default
10_000_000is now interpreted as KloadQuota()setstokenLimit: 10_000_000whenquota.token_limitis null.- Since
buildPayload()multiplies by1000, saving that default sends10_000_000_000tokens, not10_000_000. - If the intended default is
10_000_000raw tokens, the form default should be10_000.
-
Build-side conversion is not validated or rounded
quotaSettingsForm.tokenLimit * 1000can produce:- fractional values if the form allows decimals,
NaNif the input is invalid,Infinityfor very large values.
- Backend likely expects an integer token count. Use
Math.round(...)and validate.
-
Precision loss on load/save round trip
Math.round(quota.token_limit / 1000)loses precision.- Example: backend
10_000_500becomes10_001K, then saves as10_001_000. - If exact values matter, either keep fractional K in the form or preserve the original raw value.
-
Unit conversion is duplicated and uses magic numbers
- The
1000conversion appears in two places. - This makes it easy to miss one conversion, as happened with the default.
- Prefer a shared constant/helper and clearer naming such as
tokenLimitK.
- The
-
Possible string-to-number coercion
- If the form field is a string,
tokenLimit * 1000silently coerces it. - Empty string becomes
0; invalid input becomesNaN. - Explicitly parse and validate.
- If the form field is a string,
Suggested changes
const TOKENS_PER_K = 1000;
const DEFAULT_TOKEN_LIMIT_TOKENS = 10_000_000;
const DEFAULT_TOKEN_LIMIT_K = DEFAULT_TOKEN_LIMIT_TOKENS / TOKENS_PER_K; // 10_000
function kToTokens(k: number): number {
return Math.round(k * TOKENS_PER_K);
}
function tokensToK(tokens: number): number {
return Math.round(tokens / TOKENS_PER_K);
}function buildPayload(): ChatUserQuotaPayload {
const tokenLimitK = Number(quotaSettingsForm.tokenLimit);
if (!Number.isFinite(tokenLimitK) || tokenLimitK < 0) {
throw new Error("Invalid token limit");
}
const tokenLimit = kToTokens(tokenLimitK);
if (!Number.isSafeInteger(tokenLimit)) {
throw new Error("Token limit is too large or not an integer");
}
return {
quota_type: QUOTA_TYPE.PERIODIC,
period_type: quotaSettingsForm.periodType,
period_value: quotaSettingsForm.periodValue,
token_limit: tokenLimit,
};
}tokenLimit:
quota.token_limit == null
? DEFAULT_TOKEN_LIMIT_K
: tokensToK(quota.token_limit),If exact backend values must be preserved, avoid Math.round on load and allow decimal K, e.g. quota.token_limit / TOKENS_PER_K, or store the original raw value separately.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: chat user quota