fix(grok): 兼容账单接口缺省 creditUsagePercent 时正常解析配额周期与重置时间 - #64
Open
Frost2026 wants to merge 1 commit into
Open
Conversation
Owner
|
感谢修复。百分比缺失时仍保留配额周期和重置时间,这个目标合理;不过当前实现会跳过原有的旧字段用量计算,需要调整后再合并。 我已复现以下情况: {
"config": {
"currentPeriod": {
"type": "USAGE_PERIOD_TYPE_WEEKLY",
"end": "2026-09-10T12:00:00Z"
},
"monthlyLimit": { "val": 100 },
"used": { "val": 100 }
}
}
建议将“用量”和“周期”分开解析:
周期信息优先使用 至少请补充以下回归测试:
最小修复可以先把“仅周期兜底”放到旧字段计算之后,消除已复现的回归,再确认缺省零值语义。现有测试通过,但没有覆盖上述新旧字段同时出现的情况。 |
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.
背景与问题
目前在
fetchGrokUsage(src/providers/grok.ts:355)中,解析周限额窗口强依赖于creditUsagePercent字段必须为有效的数字类型:但在真实的 xAI 线上账单接口(
https://cli-chat-proxy.grok.com/v1/billing?format=credits)中,对于未产生扣费消耗、当周刚重置或部分新订阅账号,xAI 返回的 payload 中:currentPeriod: { type: "USAGE_PERIOD_TYPE_WEEKLY", end: "..." };creditUsagePercent字段可能为null、undefined或被直接省略。由于上述严苛校验,代码会直接跳过提取,导致解析出的配额窗口
windows为空数组[]。在 DSH 前端界面上表现为完全看不到 Grok 的用量额度、无法显示当周用量进度条与重置倒计时。解决方案
config.currentPeriod(包含重置周期信息)或creditUsagePercent为有效数字,即判定为有效配额窗口;creditUsagePercent未提供时,usedPercent保底默认为0,并正常提取resetsAt(周期重置时间戳)。验证
test/usage.spec.ts中补充了针对性单元测试:fetchGrokUsage extracts reset window when creditUsagePercent is absent but currentPeriod is presentcurrentPeriod时,能够正确提取出配额窗口(kind: "weekly",usedPercent: 0,正确计算resetsAt);pnpm test(370/370 pass);pnpm build(tsc && tsdown零报错)。