UNPKG

@overseerai/sdk

Version:

Node.js SDK for Overseer AI content safety

60 lines (59 loc) 2.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Overseer = void 0; const axios_1 = __importDefault(require("axios")); class Overseer { constructor(config) { this.apiKey = config.apiKey; this.baseUrl = config.baseUrl || 'https://api.overseerai.app'; } /** * Validate AI-generated text * @param text The text to validate * @returns ValidationResult with either the original text or a rejection message */ async validate(text) { var _a, _b, _c, _d; try { const response = await axios_1.default.post(`${this.baseUrl}/api/v1/responses/check`, { text, rules: null }, { headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json' } }); const result = response.data; if (!result.is_allowed) { return { isAllowed: false, text: "Sorry, I can't help with that!", details: { reason: (_a = result.details) === null || _a === void 0 ? void 0 : _a.reason, safetyCode: (_b = result.details) === null || _b === void 0 ? void 0 : _b.safety_code } }; } return { isAllowed: true, text: text }; } catch (error) { // Handle API errors gracefully if (axios_1.default.isAxiosError(error)) { if (((_c = error.response) === null || _c === void 0 ? void 0 : _c.status) === 401) { throw new Error('Invalid API key'); } if (((_d = error.response) === null || _d === void 0 ? void 0 : _d.status) === 429) { throw new Error('Rate limit exceeded'); } } throw error; } } } exports.Overseer = Overseer; // Default export exports.default = Overseer;