mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-17 01:06:37 +08:00
feat: add OpenRouter balance update functionality and improve code formatting
This commit is contained in:
parent
eb96aa635e
commit
4d011c5f98
@ -112,6 +112,13 @@ type DeepSeekUsageResponse struct {
|
|||||||
} `json:"balance_infos"`
|
} `json:"balance_infos"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OpenRouterResponse struct {
|
||||||
|
Data struct {
|
||||||
|
TotalCredits float64 `json:"total_credits"`
|
||||||
|
TotalUsage float64 `json:"total_usage"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
// GetAuthHeader get auth header
|
// GetAuthHeader get auth header
|
||||||
func GetAuthHeader(token string) http.Header {
|
func GetAuthHeader(token string) http.Header {
|
||||||
h := http.Header{}
|
h := http.Header{}
|
||||||
@ -285,6 +292,22 @@ func updateChannelDeepSeekBalance(channel *model.Channel) (float64, error) {
|
|||||||
return balance, nil
|
return balance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateChannelOpenRouterBalance(channel *model.Channel) (float64, error) {
|
||||||
|
url := "https://openrouter.ai/api/v1/credits"
|
||||||
|
body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
response := OpenRouterResponse{}
|
||||||
|
err = json.Unmarshal(body, &response)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
balance := response.Data.TotalCredits - response.Data.TotalUsage
|
||||||
|
channel.UpdateBalance(balance)
|
||||||
|
return balance, nil
|
||||||
|
}
|
||||||
|
|
||||||
func updateChannelBalance(channel *model.Channel) (float64, error) {
|
func updateChannelBalance(channel *model.Channel) (float64, error) {
|
||||||
baseURL := channeltype.ChannelBaseURLs[channel.Type]
|
baseURL := channeltype.ChannelBaseURLs[channel.Type]
|
||||||
if channel.GetBaseURL() == "" {
|
if channel.GetBaseURL() == "" {
|
||||||
@ -313,6 +336,8 @@ func updateChannelBalance(channel *model.Channel) (float64, error) {
|
|||||||
return updateChannelSiliconFlowBalance(channel)
|
return updateChannelSiliconFlowBalance(channel)
|
||||||
case channeltype.DeepSeek:
|
case channeltype.DeepSeek:
|
||||||
return updateChannelDeepSeekBalance(channel)
|
return updateChannelDeepSeekBalance(channel)
|
||||||
|
case channeltype.OpenRouter:
|
||||||
|
return updateChannelOpenRouterBalance(channel)
|
||||||
default:
|
default:
|
||||||
return 0, errors.New("尚未实现")
|
return 0, errors.New("尚未实现")
|
||||||
}
|
}
|
||||||
|
@ -153,6 +153,7 @@ func testChannel(ctx context.Context, channel *model.Channel, request *relaymode
|
|||||||
rawResponse := w.Body.String()
|
rawResponse := w.Body.String()
|
||||||
_, responseMessage, err = parseTestResponse(rawResponse)
|
_, responseMessage, err = parseTestResponse(rawResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.SysError(fmt.Sprintf("failed to parse error: %s, \nresponse: %s", err.Error(), rawResponse))
|
||||||
return "", err, nil
|
return "", err, nil
|
||||||
}
|
}
|
||||||
result := w.Result()
|
result := w.Result()
|
||||||
|
@ -57,6 +57,8 @@ function renderBalance(type, balance, t) {
|
|||||||
return <span>¥{balance.toFixed(2)}</span>;
|
return <span>¥{balance.toFixed(2)}</span>;
|
||||||
case 13: // AIGC2D
|
case 13: // AIGC2D
|
||||||
return <span>{renderNumber(balance)}</span>;
|
return <span>{renderNumber(balance)}</span>;
|
||||||
|
case 20: // OpenRouter
|
||||||
|
return <span>${balance.toFixed(2)}</span>;
|
||||||
case 36: // DeepSeek
|
case 36: // DeepSeek
|
||||||
return <span>¥{balance.toFixed(2)}</span>;
|
return <span>¥{balance.toFixed(2)}</span>;
|
||||||
case 44: // SiliconFlow
|
case 44: // SiliconFlow
|
||||||
|
Loading…
Reference in New Issue
Block a user