2026-09-06 05:16:13 +09:00
|
|
|
import { readAuthFile } from '../../opencode/auth.js';
|
|
|
|
|
import {
|
|
|
|
|
getAuthEntry,
|
|
|
|
|
normalizeAuthEntry,
|
|
|
|
|
buildResult,
|
|
|
|
|
toUsageWindow,
|
|
|
|
|
toNumber,
|
2026-09-05 23:26:35 +03:00
|
|
|
formatMoney,
|
|
|
|
|
asObject,
|
|
|
|
|
asNonEmptyString
|
2026-09-06 05:16:13 +09:00
|
|
|
} from '../utils/index.js';
|
|
|
|
|
|
|
|
|
|
export const providerId = 'hyper';
|
|
|
|
|
export const providerName = 'Charm Hyper';
|
2026-09-05 23:26:35 +03:00
|
|
|
export const aliases = ['hyper'];
|
2026-09-06 05:16:13 +09:00
|
|
|
const HYPER_QUOTA_URL = 'https://hyper.charm.land/v1/credits';
|
|
|
|
|
const CREDIT_TO_USD = 0.05;
|
|
|
|
|
|
2026-09-05 23:26:35 +03:00
|
|
|
const getApiKey = (auth) => {
|
2026-09-06 05:16:13 +09:00
|
|
|
const entry = normalizeAuthEntry(getAuthEntry(auth, aliases));
|
2026-09-05 23:26:35 +03:00
|
|
|
return asNonEmptyString(entry?.key) ?? asNonEmptyString(entry?.token);
|
2026-09-06 05:16:13 +09:00
|
|
|
};
|
|
|
|
|
|
2026-09-05 23:26:35 +03:00
|
|
|
export const isConfigured = (auth = readAuthFile()) => Boolean(getApiKey(auth));
|
|
|
|
|
|
|
|
|
|
export const fetchQuota = async ({ readAuth = readAuthFile, fetchImpl = fetch } = {}) => {
|
|
|
|
|
const apiKey = getApiKey(readAuth());
|
2026-09-06 05:16:13 +09:00
|
|
|
|
|
|
|
|
if (!apiKey) {
|
|
|
|
|
return buildResult({
|
|
|
|
|
providerId,
|
|
|
|
|
providerName,
|
|
|
|
|
ok: false,
|
|
|
|
|
configured: false,
|
|
|
|
|
error: 'Not configured'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const timeoutSignal = AbortSignal.timeout(15_000);
|
|
|
|
|
|
|
|
|
|
try {
|
2026-09-05 23:26:35 +03:00
|
|
|
const response = await fetchImpl(HYPER_QUOTA_URL, {
|
2026-09-06 05:16:13 +09:00
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${apiKey}`,
|
|
|
|
|
'Accept-Encoding': 'identity'
|
|
|
|
|
},
|
|
|
|
|
signal: timeoutSignal
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
return buildResult({
|
|
|
|
|
providerId,
|
|
|
|
|
providerName,
|
|
|
|
|
ok: false,
|
|
|
|
|
configured: true,
|
|
|
|
|
error: response.status === 401 || response.status === 403
|
|
|
|
|
? 'Session expired — please re-authenticate with Charm Hyper'
|
|
|
|
|
: `API error: ${response.status}`
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 23:26:35 +03:00
|
|
|
const payload = asObject(await response.json());
|
2026-09-06 05:16:13 +09:00
|
|
|
const rawBalance = payload?.balance;
|
2026-09-05 23:26:35 +03:00
|
|
|
const balance = toNumber(asNonEmptyString(rawBalance)
|
|
|
|
|
?? (Number.isFinite(rawBalance) ? rawBalance : null));
|
2026-09-06 05:16:13 +09:00
|
|
|
|
|
|
|
|
if (balance === null) {
|
|
|
|
|
return buildResult({
|
|
|
|
|
providerId,
|
|
|
|
|
providerName,
|
|
|
|
|
ok: false,
|
|
|
|
|
configured: true,
|
|
|
|
|
error: 'No quota data in response'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const creditsLabel = Number.isInteger(balance) ? String(balance) : formatMoney(balance);
|
|
|
|
|
const windows = {
|
|
|
|
|
credits_balance: toUsageWindow({
|
|
|
|
|
usedPercent: null,
|
|
|
|
|
windowSeconds: null,
|
|
|
|
|
resetAt: null,
|
|
|
|
|
valueLabel: `$${formatMoney(balance * CREDIT_TO_USD)}`
|
|
|
|
|
}),
|
|
|
|
|
credits: toUsageWindow({
|
|
|
|
|
usedPercent: null,
|
|
|
|
|
windowSeconds: null,
|
|
|
|
|
resetAt: null,
|
2026-09-05 23:26:35 +03:00
|
|
|
valueLabel: creditsLabel
|
2026-09-06 05:16:13 +09:00
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return buildResult({
|
|
|
|
|
providerId,
|
|
|
|
|
providerName,
|
|
|
|
|
ok: true,
|
|
|
|
|
configured: true,
|
|
|
|
|
usage: { windows }
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const isTimeout = error instanceof DOMException && (
|
|
|
|
|
error.name === 'TimeoutError' || (error.name === 'AbortError' && timeoutSignal.aborted)
|
|
|
|
|
);
|
|
|
|
|
const isParseError = error instanceof SyntaxError;
|
|
|
|
|
return buildResult({
|
|
|
|
|
providerId,
|
|
|
|
|
providerName,
|
|
|
|
|
ok: false,
|
|
|
|
|
configured: true,
|
|
|
|
|
error: isTimeout
|
|
|
|
|
? 'Request timed out'
|
|
|
|
|
: isParseError
|
|
|
|
|
? 'Invalid response from provider'
|
|
|
|
|
: (error instanceof Error ? error.message : 'Request failed')
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-09-05 23:26:35 +03:00
|
|
|
};
|