// engine/token-budget/index.ts — Token Budget Injection (SPEC-620)
// Implements the TALE framework (ACL 2025): injecting <token_budget>N</token_budget>
// into LLM-facing prompts reduces output verbosity ~68% without quality degradation.
// Budget constants (tokens)
const BUDGET_READONLY = 1200;
const BUDGET_ARCHITECTURE = 4000;
const BUDGET_CONCISE = 800;
const BUDGET_STANDARD = 2000;
const BUDGET_COMPLEX = 4000;
// devHours tier thresholds
const HOURS_CONCISE_MAX = 4;
const HOURS_STANDARD_MAX = 12;
/**
 * Resolves the TokenBudgetTier label for a given budget value.
 */
function resolveTier(budget) {
    if (budget === BUDGET_READONLY) {
        return 'readonly';
    }
    if (budget === BUDGET_CONCISE) {
        return 'concise';
    }
    if (budget === BUDGET_STANDARD) {
        return 'standard';
    }
    return 'complex';
}
/**
 * Calculates the appropriate token budget for an LLM operation.
 *
 * Tier rules:
 * - operationType 'read'         → always 1200 (readonly cap)
 * - operationType 'architecture' → always 4000 (complex)
 * - devHours ≤ 4                 → 800 (concise)
 * - devHours ≤ 12                → 2000 (standard)
 * - devHours > 12                → 4000 (complex)
 */
export function calculateTokenBudget(devHours, operationType) {
    if (operationType === 'read') {
        return BUDGET_READONLY;
    }
    if (operationType === 'architecture') {
        return BUDGET_ARCHITECTURE;
    }
    if (devHours <= HOURS_CONCISE_MAX) {
        return BUDGET_CONCISE;
    }
    if (devHours <= HOURS_STANDARD_MAX) {
        return BUDGET_STANDARD;
    }
    return BUDGET_COMPLEX;
}
/**
 * Formats a numeric budget into the TALE-compliant XML tag.
 *
 * @example formatBudgetTag(800) → "<token_budget>800</token_budget>"
 */
export function formatBudgetTag(budget) {
    return `<token_budget>${String(budget)}</token_budget>`;
}
/**
 * Appends the token budget tag at the end of a prompt string.
 * The tag is placed on its own line after a blank separator.
 *
 * @param prompt  The LLM-facing prompt to augment.
 * @param budget  Token budget value produced by calculateTokenBudget.
 */
export function injectBudgetIntoPrompt(prompt, budget) {
    const tag = formatBudgetTag(budget);
    return `${prompt}\n\n${tag}`;
}
/**
 * Full token budget computation: calculates budget, derives tier and tag.
 *
 * @param devHours      Estimated development hours of the spec (0 = unknown → treat as concise boundary).
 * @param operationType The kind of LLM operation being performed.
 * @param config        Optional overrides (disable or custom budget).
 */
export function computeTokenBudget(devHours, operationType, config) {
    if (config?.disabled === true) {
        // Return a neutral high-cap result when injection is disabled
        const budget = BUDGET_COMPLEX;
        return { budget, tier: resolveTier(budget), tag: formatBudgetTag(budget) };
    }
    const effectiveHours = devHours;
    const budget = config?.customBudget ?? calculateTokenBudget(effectiveHours, operationType);
    const tier = resolveTier(budget);
    const tag = formatBudgetTag(budget);
    return { budget, tier, tag };
}
//# sourceMappingURL=index.js.map