UNPKG

@overseerai/sdk-typescript

Version:

Official TypeScript SDK for Overseer content safety API

116 lines (115 loc) 4.28 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 cross_fetch_1 = __importDefault(require("cross-fetch")); class Overseer { constructor(config) { this.apiKey = config.apiKey; this.organizationId = config.organizationId; this.baseUrl = config.baseUrl || 'https://api.overseerai.app'; } /** * Validate content against specified policies */ async validate(options) { var _a; try { const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/v1/validate`, { method: 'POST', headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json', ...(this.organizationId && { 'X-Organization-ID': this.organizationId }) }, body: JSON.stringify({ content: options.content, policies: options.policies || ['safety'], systemId: options.systemId, policyId: options.policyId }) }); if (!response.ok) { const errorText = await response.text(); throw new Error(`API request failed: ${errorText}`); } const data = await response.json(); return { valid: !data.is_flagged, content: options.content, issues: data.is_flagged ? [{ type: 'safety', code: data.safety_code, message: ((_a = data.reasons) === null || _a === void 0 ? void 0 : _a[0]) || 'Content policy violation', severity: 'high', category: data.safety_code ? `MLCommons ${data.safety_code}` : undefined }] : undefined, safetyCode: data.safety_code, metadata: data.metadata }; } catch (error) { // Re-throw fetch errors directly if (error instanceof Error) { throw error; } // Handle unknown error types throw new Error('An unknown error occurred'); } } /** * Get all available policies */ async getPolicies() { try { const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/v1/policies`, { headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json', ...(this.organizationId && { 'X-Organization-ID': this.organizationId }) } }); if (!response.ok) { const errorText = await response.text(); throw new Error(`API request failed: ${errorText}`); } return response.json(); } catch (error) { if (error instanceof Error) { throw error; } throw new Error('An unknown error occurred'); } } /** * Create a new policy */ async createPolicy(policy) { try { const response = await (0, cross_fetch_1.default)(`${this.baseUrl}/api/v1/policies`, { method: 'POST', headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json', ...(this.organizationId && { 'X-Organization-ID': this.organizationId }) }, body: JSON.stringify(policy) }); if (!response.ok) { const errorText = await response.text(); throw new Error(`API request failed: ${errorText}`); } return response.json(); } catch (error) { if (error instanceof Error) { throw error; } throw new Error('An unknown error occurred'); } } } exports.Overseer = Overseer;