"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.personaGenerator = exports.PersonaGenerator = void 0;
const logger_js_1 = require("../../utils/logger.js");
const client_js_1 = require("../openrouter/client.js");
const index_js_1 = require("../data-store/index.js");
const prompt_builder_js_1 = require("./prompt-builder.js");
const log = (0, logger_js_1.createModuleLogger)('persona-generator');
const MAX_ATTEMPTS = 3;
const REQUIRED_SECTIONS = [
    'character_profile',
    'language_rules',
    'conversation_scenarios',
    'contextual_expressions',
    'message_length_patterns',
    'do_not_use',
    'training_notes',
];
class PersonaGenerator {
    /**
     * CharacterDNA ve opsiyonel ConversationProfile'dan persona üretir.
     * Retry logic dahil. Başarısız olursa null döner.
     */
    async generate(dna, profile) {
        const messages = (0, prompt_builder_js_1.buildPersonaGenerationPrompt)(dna, profile);
        const client = (0, client_js_1.createOpenRouterClient)();
        for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
            try {
                log.info({ characterId: dna.id, attempt }, 'Generating persona');
                const response = await client.chatCompletion(messages, {
                    max_tokens: 4096,
                    temperature: 0.8,
                });
                const content = response.choices?.[0]?.message?.content ?? '';
                const jsonStr = extractJSON(content);
                const parsed = JSON.parse(jsonStr);
                const persona = this.validatePersonaJSON(parsed);
                if (persona) {
                    log.info({ characterId: dna.id }, 'Persona generated successfully');
                    return persona;
                }
                log.warn({ characterId: dna.id, attempt }, 'Persona validation failed, retrying');
            }
            catch (err) {
                const message = err instanceof Error ? err.message : String(err);
                log.warn({ characterId: dna.id, attempt, error: message }, 'Persona generation attempt failed');
            }
        }
        log.error({ characterId: dna.id }, 'All persona generation attempts failed');
        return null;
    }
    /**
     * Mevcut karakter için personayı yeniden üretir.
     * Karakter bulunamazsa hata fırlatır.
     */
    async regenerate(characterId) {
        const dna = await index_js_1.dataStore.getCharacter(characterId);
        if (!dna) {
            throw new Error(`Character not found: ${characterId}`);
        }
        const profile = await index_js_1.dataStore.getConversationProfile(characterId);
        const persona = await this.generate(dna, profile ?? undefined);
        if (persona) {
            await index_js_1.dataStore.saveConversationPersona(characterId, persona);
        }
        return persona;
    }
    /**
     * AI yanıtını parse eder ve yapısal doğrulama yapar.
     * Geçersizse null döner.
     */
    validatePersonaJSON(raw) {
        if (!raw || typeof raw !== 'object')
            return null;
        const obj = raw;
        // Check all required top-level sections exist
        for (const section of REQUIRED_SECTIONS) {
            if (!(section in obj) || obj[section] == null) {
                log.warn({ missingSection: section }, 'Persona validation: missing section');
                return null;
            }
        }
        // Validate language_rules has required entries
        const langRules = obj.language_rules;
        if (!langRules || typeof langRules !== 'object')
            return null;
        const generalRules = langRules.general_rules;
        if (!Array.isArray(generalRules) || generalRules.length < 1) {
            log.warn('Persona validation: general_rules empty');
            return null;
        }
        const abbreviations = langRules.abbreviations;
        if (!abbreviations || typeof abbreviations !== 'object' || Object.keys(abbreviations).length < 1) {
            log.warn('Persona validation: abbreviations empty');
            return null;
        }
        return raw;
    }
}
exports.PersonaGenerator = PersonaGenerator;
/**
 * JSON string'i response'dan çıkarır.
 * Markdown code block varsa temizler.
 */
function extractJSON(content) {
    const trimmed = content.trim();
    const match = trimmed.match(/```(?:json)?\s*([\s\S]*?)```/);
    if (match)
        return match[1].trim();
    return trimmed;
}
exports.personaGenerator = new PersonaGenerator();
//# sourceMappingURL=index.js.map