feat(donehub): add user self endpoint handling and balance calculation logic

This commit is contained in:
cita
2026-03-05 12:23:45 +08:00
parent 24769706e1
commit 11fc61f4fa
4 changed files with 136 additions and 9 deletions
+18 -1
View File
@@ -1,5 +1,5 @@
import { OneHubAdapter } from './oneHub.js';
import type { CheckinResult } from './base.js';
import type { BalanceInfo, CheckinResult } from './base.js';
export class DoneHubAdapter extends OneHubAdapter {
readonly platformName: string = 'done-hub';
@@ -15,6 +15,23 @@ export class DoneHubAdapter extends OneHubAdapter {
return { success: false, message: 'checkin endpoint not found' };
}
// DoneHub reports `quota` as remaining balance and `used_quota` as spent amount.
// Sum them to get the total quota instead of subtracting used from quota.
override async getBalance(baseUrl: string, accessToken: string): Promise<BalanceInfo> {
const res = await this.fetchJson<any>(`${baseUrl}/api/user/self`, {
headers: { Authorization: `Bearer ${accessToken}` },
});
const data = res?.data ?? res;
const quotaRemaining = (data?.quota || 0) / 500000;
const used = (data?.used_quota || 0) / 500000;
const total = quotaRemaining + used;
const todayIncome = Number.isFinite(data?.today_income) ? (data.today_income / 500000) : undefined;
const todayQuotaConsumption = Number.isFinite(data?.today_quota_consumption)
? (data.today_quota_consumption / 500000)
: undefined;
return { balance: quotaRemaining, used, quota: total, todayIncome, todayQuotaConsumption };
}
// getModels is inherited from OneHubAdapter which already has /api/available_model fallback.
// No need to override here — OneHub's implementation handles this correctly.
}