import { handleOpenAIError } from "./Errors.js";
import { DEFAULT_MODELS } from "../../constants.js";
import { buildUrl } from "./utils.js";
import { logger } from "../../utils/logger.js";
import { fetchWithTimeout } from "../../utils/fetch.js";
export class OpenAIModeration {
    baseUrl;
    apiKey;
    constructor(baseUrl, apiKey) {
        this.baseUrl = baseUrl;
        this.apiKey = apiKey;
    }
    async execute(request) {
        const body = {
            input: request.input,
            model: request.model || DEFAULT_MODELS.MODERATION
        };
        const url = buildUrl(this.baseUrl, "/moderations");
        logger.logRequest("OpenAI", "POST", url, body);
        const response = await fetchWithTimeout(url, {
            method: "POST",
            headers: {
                Authorization: `Bearer ${this.apiKey}`,
                "Content-Type": "application/json"
            },
            body: JSON.stringify(body)
        }, request.requestTimeout);
        if (!response.ok) {
            await handleOpenAIError(response, request.model || DEFAULT_MODELS.MODERATION);
        }
        const json = (await response.json());
        logger.logResponse("OpenAI", response.status, response.statusText, json);
        return json;
    }
}
