Files
openchamber/packages/web/server/lib/quota/providers/openai.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

92 lines
2.3 KiB
JavaScript

import { readAuthFile } from '../../opencode/auth.js';
import {
getAuthEntry,
normalizeAuthEntry,
buildResult,
toUsageWindow,
toNumber,
toTimestamp
} from '../utils/index.js';
export const providerId = 'openai';
export const providerName = 'OpenAI';
export const aliases = ['openai', 'codex', 'chatgpt'];
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://chatgpt.com/backend-api/wham/usage', {
method: 'GET',
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json'
}
});
if (!response.ok) {
return buildResult({
providerId,
providerName,
ok: false,
configured: true,
error: `API error: ${response.status}`
});
}
const payload = await response.json();
const primary = payload?.rate_limit?.primary_window ?? null;
const secondary = payload?.rate_limit?.secondary_window ?? null;
const windows = {};
if (primary) {
windows['5h'] = toUsageWindow({
usedPercent: primary.used_percent ?? null,
windowSeconds: primary.limit_window_seconds ?? null,
resetAt: primary.reset_at ? primary.reset_at * 1000 : null
});
}
if (secondary) {
windows['weekly'] = toUsageWindow({
usedPercent: secondary.used_percent ?? null,
windowSeconds: secondary.limit_window_seconds ?? null,
resetAt: secondary.reset_at ? secondary.reset_at * 1000 : null
});
}
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'
});
}
};