"use strict";
// =============================================================================
// Pure Agent - Prompt Builder (Helpers)
// Ported from Hermes agent/prompt_builder.py
// =============================================================================
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadSoulMd = loadSoulMd;
exports.getAgentIdentity = getAgentIdentity;
exports.buildEnvironmentHints = buildEnvironmentHints;
exports.buildContextFilesPrompt = buildContextFilesPrompt;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const constants_1 = require("./constants");
// ---------------------------------------------------------------------------
// Load SOUL.md (agent persona)
// ---------------------------------------------------------------------------
function loadSoulMd() {
    const soulPath = (0, constants_1.getSOULPath)();
    try {
        if (fs.existsSync(soulPath)) {
            const content = fs.readFileSync(soulPath, 'utf-8').trim();
            if (content.length > 0) {
                return content.length > 10000 ? content.slice(0, 10000) : content;
            }
        }
    }
    catch { /* ignore */ }
    return null;
}
// ---------------------------------------------------------------------------
// Get agent identity (SOUL.md or default)
// ---------------------------------------------------------------------------
function getAgentIdentity() {
    const soul = loadSoulMd();
    if (soul)
        return soul;
    return constants_1.DEFAULT_AGENT_IDENTITY;
}
// ---------------------------------------------------------------------------
// Build environment hints
// ---------------------------------------------------------------------------
function buildEnvironmentHints(workingDir) {
    const hints = [];
    hints.push(`Platform: ${os.platform()} (${os.arch()})`);
    hints.push(`Shell: ${(0, constants_1.getDefaultShell)()}`);
    hints.push(`Node.js: ${process.version}`);
    hints.push(`Working directory: ${workingDir}`);
    hints.push(`Home: ${os.homedir()}`);
    if ((0, constants_1.isWSL)()) {
        hints.push('Environment: WSL (Windows Subsystem for Linux). The Windows host filesystem is mounted under /mnt/ — /mnt/c/ is the C: drive.');
    }
    if ((0, constants_1.isWindows)()) {
        hints.push('Environment: Windows — use Windows-compatible commands. Path separator: backslash (\\) or forward slash (/) both work in Node.js.');
    }
    return `## Environment\n${hints.join('\n')}`;
}
// ---------------------------------------------------------------------------
// Build context files prompt
// ---------------------------------------------------------------------------
function buildContextFilesPrompt(workingDir) {
    const contextFiles = [];
    const candidates = [
        { name: '.pure-agent.md', maxChars: 20000 },
        { name: 'AGENTS.md', maxChars: 20000 },
        { name: 'CLAUDE.md', maxChars: 20000 },
        { name: '.cursorrules', maxChars: 20000 },
    ];
    for (const candidate of candidates) {
        const filePath = path.join(workingDir, candidate.name);
        try {
            if (fs.existsSync(filePath)) {
                let content = fs.readFileSync(filePath, 'utf-8').trim();
                if (content.length > candidate.maxChars) {
                    const headLen = Math.floor(candidate.maxChars * 0.7);
                    const tailLen = Math.floor(candidate.maxChars * 0.2);
                    content = content.slice(0, headLen) + `\n\n... [truncated] ...\n\n` + content.slice(-tailLen);
                }
                contextFiles.push(`### ${candidate.name}\n${content}`);
            }
        }
        catch { /* skip */ }
    }
    // .cursor/rules/*.mdc
    const cursorRulesDir = path.join(workingDir, '.cursor', 'rules');
    try {
        if (fs.existsSync(cursorRulesDir)) {
            for (const entry of fs.readdirSync(cursorRulesDir)) {
                if (entry.endsWith('.mdc') || entry.endsWith('.md')) {
                    try {
                        let content = fs.readFileSync(path.join(cursorRulesDir, entry), 'utf-8').trim();
                        if (content.length > 10000)
                            content = content.slice(0, 7000) + `\n\n... [truncated] ...\n\n` + content.slice(-2000);
                        contextFiles.push(`### .cursor/rules/${entry}\n${content}`);
                    }
                    catch { /* skip */ }
                }
            }
        }
    }
    catch { /* skip */ }
    if (contextFiles.length === 0)
        return '';
    return `## Project Context Files\n${contextFiles.join('\n\n')}`;
}
//# sourceMappingURL=prompt-builder.js.map