-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
fix: 修复 OpenRouter 令牌余额查询 403 错误 #4950
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 all commits
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 |
|---|---|---|
|
|
@@ -121,6 +121,13 @@ type OpenRouterCreditResponse struct { | |
| } `json:"data"` | ||
| } | ||
|
|
||
| type OpenRouterKeyResponse struct { | ||
| Data struct { | ||
| LimitRemaining float64 `json:"limit_remaining"` | ||
| TotalUsage float64 `json:"usage"` | ||
| } | ||
| } | ||
|
|
||
| // GetAuthHeader get auth header | ||
| func GetAuthHeader(token string) http.Header { | ||
| h := http.Header{} | ||
|
|
@@ -307,19 +314,34 @@ func updateChannelAIGC2DBalance(channel *model.Channel) (float64, error) { | |
| } | ||
|
|
||
| func updateChannelOpenRouterBalance(channel *model.Channel) (float64, error) { | ||
| authHeader := GetAuthHeader(channel.Key) | ||
|
|
||
| url := "https://openrouter.ai/api/v1/credits" | ||
| body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key)) | ||
| if err != nil { | ||
| return 0, err | ||
| body, err := GetResponseBody("GET", url, channel, authHeader) | ||
| if err == nil { | ||
| response := OpenRouterCreditResponse{} | ||
| err = json.Unmarshal(body, &response) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| balance := response.Data.TotalCredits - response.Data.TotalUsage | ||
| channel.UpdateBalance(balance) | ||
| return balance, nil | ||
| } | ||
| response := OpenRouterCreditResponse{} | ||
| err = json.Unmarshal(body, &response) | ||
| if err != nil { | ||
| return 0, err | ||
|
|
||
| url = "https://openrouter.ai/api/v1/key" | ||
| body, err = GetResponseBody("GET", url, channel, authHeader) | ||
| if err == nil { | ||
| response := OpenRouterKeyResponse{} | ||
| err = json.Unmarshal(body, &response) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| balance := response.Data.LimitRemaining | ||
| channel.UpdateBalance(balance) | ||
| return balance, nil | ||
| } | ||
| balance := response.Data.TotalCredits - response.Data.TotalUsage | ||
| channel.UpdateBalance(balance) | ||
| return balance, nil | ||
| return 0, err | ||
| } | ||
|
Comment on lines
+320
to
345
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. Restrict fallback to 403 instead of all Current logic falls back to 🤖 Prompt for AI Agents |
||
|
|
||
| func updateChannelMoonshotBalance(channel *model.Channel) (float64, error) { | ||
|
|
||
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.
🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win
Use
common.Unmarshalinstead ofjson.Unmarshalin the new OpenRouter paths.The new changed lines directly call
json.Unmarshal, which violates the repo JSON-wrapper rule for Go business code.Suggested patch
As per coding guidelines: "
**/*.go: All JSON marshal/unmarshal operations MUST use wrapper functions fromcommon/json.go... Do NOT directly import or callencoding/jsonin business code."🤖 Prompt for AI Agents