@@ -112,6 +112,13 @@ type DeepSeekUsageResponse struct {
112
112
} `json:"balance_infos"`
113
113
}
114
114
115
+ type OpenRouterResponse struct {
116
+ Data struct {
117
+ TotalCredits float64 `json:"total_credits"`
118
+ TotalUsage float64 `json:"total_usage"`
119
+ } `json:"data"`
120
+ }
121
+
115
122
// GetAuthHeader get auth header
116
123
func GetAuthHeader (token string ) http.Header {
117
124
h := http.Header {}
@@ -285,6 +292,22 @@ func updateChannelDeepSeekBalance(channel *model.Channel) (float64, error) {
285
292
return balance , nil
286
293
}
287
294
295
+ func updateChannelOpenRouterBalance (channel * model.Channel ) (float64 , error ) {
296
+ url := "https://openrouter.ai/api/v1/credits"
297
+ body , err := GetResponseBody ("GET" , url , channel , GetAuthHeader (channel .Key ))
298
+ if err != nil {
299
+ return 0 , err
300
+ }
301
+ response := OpenRouterResponse {}
302
+ err = json .Unmarshal (body , & response )
303
+ if err != nil {
304
+ return 0 , err
305
+ }
306
+ balance := response .Data .TotalCredits - response .Data .TotalUsage
307
+ channel .UpdateBalance (balance )
308
+ return balance , nil
309
+ }
310
+
288
311
func updateChannelBalance (channel * model.Channel ) (float64 , error ) {
289
312
baseURL := channeltype .ChannelBaseURLs [channel .Type ]
290
313
if channel .GetBaseURL () == "" {
@@ -313,6 +336,8 @@ func updateChannelBalance(channel *model.Channel) (float64, error) {
313
336
return updateChannelSiliconFlowBalance (channel )
314
337
case channeltype .DeepSeek :
315
338
return updateChannelDeepSeekBalance (channel )
339
+ case channeltype .OpenRouter :
340
+ return updateChannelOpenRouterBalance (channel )
316
341
default :
317
342
return 0 , errors .New ("尚未实现" )
318
343
}
0 commit comments