import { OculusClient } from '@oculus/sdk';
import { OculusAuthError, OculusAPIError, TokenRevokedException } from '@oculus/sdk';
import axios from 'axios';
const client = new OculusClient({
clientId: process.env.AGENT_CLIENT_ID!,
clientSecret: process.env.AGENT_CLIENT_SECRET!,
baseUrl: process.env.ANTI_AI_URL ?? 'https://api.antiailabs.com',
onTokenRevoked: (jti) => {
console.error(`Token ${jti} was revoked — agent access terminated`);
},
});
process.on('SIGTERM', () => client.destroy());
process.on('SIGINT', () => client.destroy());
async function callApi(url: string, scope: string): Promise<unknown> {
let token: string;
try {
token = await client.getToken(scope);
} catch (err) {
if (err instanceof TokenRevokedException) {
throw new Error('Agent access has been revoked. Contact your admin.');
}
if (err instanceof OculusAuthError) {
throw new Error(`Authentication failed: ${err.message}`);
}
if (err instanceof OculusAPIError) {
throw new Error(`Anti AI error (${err.statusCode}): ${err.message}`);
}
throw err;
}
const response = await axios.get(url, { headers: { Authorization: `Bearer ${token}` } });
return response.data;
}