"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateCommitMessage = 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)('CommitMessageGenerator');
const HAIKU_MODEL = 'claude-haiku-4-5-20251001';
const MAX_TOKENS = 60;
const MAX_DIFF_LENGTH = 8000;
const CO_AUTHORED_BY = 'Co-authored-by: rivet-bot <246447668+rivet-bot@users.noreply.github.com>';
/** Generate an LLM-powered commit message from a diff and user instruction. */
const generateCommitMessage = async (context) => {
    const { diff, instruction, filesChanged } = context;
    let message;
    try {
        const gateway = new GatewayClient_1.GatewayClient();
        const truncatedDiff = diff.length > MAX_DIFF_LENGTH
            ? diff.slice(0, MAX_DIFF_LENGTH) + '\n... (diff truncated)'
            : diff;
        const prompt = (0, gitPrompts_1.buildCommitMessagePrompt)(instruction, filesChanged, truncatedDiff);
        const rawText = await gateway.sendMessage({
            model: HAIKU_MODEL,
            maxTokens: MAX_TOKENS,
            temperature: 0.3,
            prompt,
        });
        // Clean up: strip quotes, trim
        message = rawText.replace(/^["']|["']$/g, '').trim();
        log.info(`Generated commit message: ${message.split('\n')[0]}`);
    }
    catch (error) {
        log.warn('LLM commit message generation failed, using fallback:', error);
        message = buildFallbackMessage(instruction, filesChanged);
    }
    return `${message}\n\n${CO_AUTHORED_BY}`;
};
exports.generateCommitMessage = generateCommitMessage;
/** Static fallback when the LLM is unavailable. */
const buildFallbackMessage = (instruction, filesChanged) => {
    const header = instruction.length > 72 ? instruction.slice(0, 69) + '...' : instruction;
    const fileList = filesChanged.map((f) => `- ${f}`).join('\n');
    return `${header}\n\n${fileList}`;
};
//# sourceMappingURL=CommitMessageGenerator.js.map