"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatePRDescription = exports.generatePRTitle = void 0;
const logger_1 = require("../utils/logger");
const GatewayClient_1 = require("./GatewayClient");
const gitPrompts_1 = require("../prompts/gitPrompts");
const log = (0, logger_1.createLogger)('PRTitleGenerator');
const HAIKU_MODEL = 'claude-haiku-4-5-20251001';
const TITLE_MAX_TOKENS = 100;
const DESCRIPTION_MAX_TOKENS = 1024;
const MAX_COMMIT_LOG_LENGTH = 4000;
const MAX_DIFF_LENGTH = 12000;
/** Join commit messages and truncate to stay within prompt size limits. */
const truncateCommitLog = (commitMessages) => {
    const commitLog = commitMessages.join('\n');
    return commitLog.length > MAX_COMMIT_LOG_LENGTH
        ? commitLog.slice(0, MAX_COMMIT_LOG_LENGTH) + '\n... (truncated)'
        : commitLog;
};
/** Generate an LLM-powered pull request title from commit history. */
const generatePRTitle = async (context) => {
    const { commitMessages, filesChanged, branchName } = context;
    try {
        const gateway = new GatewayClient_1.GatewayClient();
        const truncatedLog = truncateCommitLog(commitMessages);
        const prompt = (0, gitPrompts_1.buildPRTitlePrompt)(truncatedLog, filesChanged);
        const rawText = await gateway.sendMessage({
            model: HAIKU_MODEL,
            maxTokens: TITLE_MAX_TOKENS,
            temperature: 0.3,
            prompt,
        });
        const title = rawText.replace(/^["']|["']$/g, '').trim();
        log.info(`Generated PR title: ${title}`);
        return title;
    }
    catch (error) {
        log.warn('LLM PR title generation failed, using fallback:', error);
        return `Rivet: ${branchName}`;
    }
};
exports.generatePRTitle = generatePRTitle;
/** Generate an LLM-powered pull request description from commit history and diff. */
const generatePRDescription = async (context) => {
    const { commitMessages, filesChanged, diff, branchName } = context;
    try {
        const gateway = new GatewayClient_1.GatewayClient();
        const truncatedLog = truncateCommitLog(commitMessages);
        const truncatedDiff = diff.length > MAX_DIFF_LENGTH
            ? diff.slice(0, MAX_DIFF_LENGTH) + '\n... (diff truncated)'
            : diff;
        const prompt = (0, gitPrompts_1.buildPRDescriptionPrompt)(truncatedLog, filesChanged, truncatedDiff);
        const rawText = await gateway.sendMessage({
            model: HAIKU_MODEL,
            maxTokens: DESCRIPTION_MAX_TOKENS,
            temperature: 0.3,
            prompt,
        });
        const description = rawText.trim();
        log.info('Generated PR description');
        return description;
    }
    catch (error) {
        log.warn('LLM PR description generation failed, using fallback:', error);
        return buildFallbackDescription(commitMessages, filesChanged, branchName);
    }
};
exports.generatePRDescription = generatePRDescription;
const buildFallbackDescription = (commitMessages, filesChanged, branchName) => {
    const lines = [`Changes on \`${branchName}\`:`];
    if (commitMessages.length > 0) {
        lines.push('');
        lines.push('## Changes');
        for (const msg of commitMessages.slice(0, 10)) {
            lines.push(`- ${msg}`);
        }
        if (commitMessages.length > 10) {
            lines.push(`- ... and ${commitMessages.length - 10} more commits`);
        }
    }
    if (filesChanged.length > 0) {
        lines.push('');
        lines.push('## Files');
        for (const file of filesChanged.slice(0, 15)) {
            lines.push(`- \`${file}\``);
        }
        if (filesChanged.length > 15) {
            lines.push(`- ... and ${filesChanged.length - 15} more files`);
        }
    }
    lines.push('');
    lines.push('Made with [Rivet](https://rivet.design)');
    return lines.join('\n');
};
//# sourceMappingURL=PRTitleGenerator.js.map