Files
openchamber/packages/web/server/lib/quota/providers/claude.js
T
Nelson Pires 073f44de2a refactor(server): split opencode config/auth/ui-auth into domain modules (#454)
* docs(agents): map opencode module documentation

* docs(opencode): document split module responsibilities

* refactor(opencode): centralize shared config and file helpers

* refactor(opencode): move auth storage helpers into domain module

* refactor(opencode): move UI auth implementation into domain module

* refactor(opencode): isolate agent scope and CRUD logic

* refactor(opencode): isolate command scope and CRUD logic

* refactor(opencode): isolate provider config helpers

* refactor(opencode): isolate skill discovery and CRUD logic

* refactor(opencode): define single public module entrypoint

* refactor(opencode): remove legacy opencode-config module

* refactor(opencode): remove obsolete opencode-config typings

* refactor(opencode): remove legacy opencode-auth module

* refactor(opencode): remove legacy ui-auth shim

* refactor(server): import opencode APIs from new module paths

* refactor(tts): consume auth helpers from opencode domain module

* refactor(quota): point claude provider auth import to opencode domain

* refactor(quota): point codex provider auth import to opencode domain

* refactor(quota): point copilot provider auth import to opencode domain

* refactor(quota): point google auth import to opencode domain

* refactor(quota): point kimi provider auth import to opencode domain

* refactor(quota): point nanogpt provider auth import to opencode domain

* refactor(quota): point openai provider auth import to opencode domain

* refactor(quota): point openrouter provider auth import to opencode domain

* refactor(quota): point zai provider auth import to opencode domain

* fix(opencode): restore provider source and disconnect semantics
2026-02-20 15:57:44 +02:00

108 lines
2.7 KiB
JavaScript

import { readAuthFile } from '../../opencode/auth.js';
import {
getAuthEntry,
normalizeAuthEntry,
buildResult,
toUsageWindow,
toNumber,
toTimestamp
} from '../utils/index.js';
export const providerId = 'claude';
export const providerName = 'Claude';
export const aliases = ['anthropic', 'claude'];
export const isConfigured = () => {
const auth = readAuthFile();
const entry = normalizeAuthEntry(getAuthEntry(auth, aliases));
return Boolean(entry?.access || entry?.token);
};
export const fetchQuota = async () => {
const auth = readAuthFile();
const entry = normalizeAuthEntry(getAuthEntry(auth, aliases));
const accessToken = entry?.access ?? entry?.token;
if (!accessToken) {
return buildResult({
providerId,
providerName,
ok: false,
configured: false,
error: 'Not configured'
});
}
try {
const response = await fetch('https://api.anthropic.com/api/oauth/usage', {
method: 'GET',
headers: {
Authorization: `Bearer ${accessToken}`,
'anthropic-beta': 'oauth-2025-04-20'
}
});
if (!response.ok) {
return buildResult({
providerId,
providerName,
ok: false,
configured: true,
error: `API error: ${response.status}`
});
}
const payload = await response.json();
const windows = {};
const fiveHour = payload?.five_hour ?? null;
const sevenDay = payload?.seven_day ?? null;
const sevenDaySonnet = payload?.seven_day_sonnet ?? null;
const sevenDayOpus = payload?.seven_day_opus ?? null;
if (fiveHour) {
windows['5h'] = toUsageWindow({
usedPercent: toNumber(fiveHour.utilization),
windowSeconds: null,
resetAt: toTimestamp(fiveHour.resets_at)
});
}
if (sevenDay) {
windows['7d'] = toUsageWindow({
usedPercent: toNumber(sevenDay.utilization),
windowSeconds: null,
resetAt: toTimestamp(sevenDay.resets_at)
});
}
if (sevenDaySonnet) {
windows['7d-sonnet'] = toUsageWindow({
usedPercent: toNumber(sevenDaySonnet.utilization),
windowSeconds: null,
resetAt: toTimestamp(sevenDaySonnet.resets_at)
});
}
if (sevenDayOpus) {
windows['7d-opus'] = toUsageWindow({
usedPercent: toNumber(sevenDayOpus.utilization),
windowSeconds: null,
resetAt: toTimestamp(sevenDayOpus.resets_at)
});
}
return buildResult({
providerId,
providerName,
ok: true,
configured: true,
usage: { windows }
});
} catch (error) {
return buildResult({
providerId,
providerName,
ok: false,
configured: true,
error: error instanceof Error ? error.message : 'Request failed'
});
}
};