"use strict";
/**
 * Base prompt templates for AI-generated audit narrative sections.
 *
 * Every prompt is deterministic: given the same event list and audience,
 * the same template text is produced. The template hash (SHA-256 of the
 * rendered prompt text) is stored in AiProvenance.promptTemplateHash and
 * sealed into the audit chain.
 *
 * Prompt structure enforces citation discipline: the model is explicitly
 * told which event IDs are in scope and is instructed to never reference
 * any event that does not appear in the provided list. Post-generation
 * validation catches violations and throws UncitedClaimError.
 *
 * Spec: P4-AIR-01 AC 3 (citation enforcement)
 *
 * @connexum/ai-governance
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.PROMPT_TEMPLATE_IDS = void 0;
exports.buildAgentToolUsePrompt = buildAgentToolUsePrompt;
exports.buildApprovalDecisionPrompt = buildApprovalDecisionPrompt;
exports.buildPolicyViolationPrompt = buildPolicyViolationPrompt;
exports.buildEnforcementEventPrompt = buildEnforcementEventPrompt;
exports.getTemplateStaticText = getTemplateStaticText;
// ---------------------------------------------------------------------------
// Template IDs -- stable identifiers referenced in NarrativeSection
// ---------------------------------------------------------------------------
exports.PROMPT_TEMPLATE_IDS = {
    AGENT_TOOL_USE: 'tpl-agent-tool-use-v1',
    APPROVAL_DECISION: 'tpl-approval-decision-v1',
    POLICY_VIOLATION: 'tpl-policy-violation-v1',
    ENFORCEMENT_EVENT: 'tpl-enforcement-event-v1',
};
// ---------------------------------------------------------------------------
// Audience register
// ---------------------------------------------------------------------------
const AUDIENCE_INSTRUCTIONS = {
    auditor: 'Write for an independent external auditor conducting a formal compliance assessment. ' +
        'Use precise regulatory language, cite specific control references, and include ' +
        'quantified evidence (counts, percentages, timeframes).',
    regulator: 'Write for a regulatory examiner. Use formal, declarative language. ' +
        'Focus on compliance obligations met and any exceptions. ' +
        'Avoid technical jargon; use plain regulatory vocabulary.',
    board: 'Write for a board of directors. Use concise executive language. ' +
        'Emphasise risk posture, material events, and governance health. ' +
        'Limit technical detail; focus on strategic implications.',
    operator: 'Write for an internal compliance operator or security engineer. ' +
        'Include operational detail, specific event counts, and remediation context. ' +
        'Technical language is appropriate.',
};
// ---------------------------------------------------------------------------
// Citation enforcement instruction (inserted into every prompt)
// ---------------------------------------------------------------------------
const CITATION_MANDATE = `
CITATION RULES (MANDATORY -- NEVER VIOLATE):
1. Every factual claim you make MUST be followed immediately by the event ID in square brackets, e.g. [EVT-abc123].
2. You MAY ONLY cite event IDs from the AVAILABLE EVENTS list provided below. Do NOT invent, guess, or extrapolate event IDs.
3. If the available events are insufficient to support a factual claim, write "Insufficient evidence in the audit window to support this claim." Do NOT fabricate evidence.
4. Claims without citations will be automatically rejected by the citation validator before output is released. Your output will not reach the recipient if you violate this rule.
`.trim();
// ---------------------------------------------------------------------------
// System prompt (common to all section types)
// ---------------------------------------------------------------------------
function buildSystemPrompt(framework, audience) {
    const audienceInstruction = AUDIENCE_INSTRUCTIONS[audience] ?? AUDIENCE_INSTRUCTIONS.auditor;
    return (`You are an AI compliance analyst generating a section of a formal audit report.\n` +
        `Framework in scope: ${framework}\n` +
        `Audience: ${audienceInstruction}\n\n` +
        CITATION_MANDATE);
}
// ---------------------------------------------------------------------------
// Event serialisation for prompt context
// ---------------------------------------------------------------------------
function serialiseEvents(events) {
    if (events.length === 0) {
        return 'No events in the audit window for this section.';
    }
    return events
        .map((e) => `ID: ${e.id} | Type: ${e.type} | Time: ${e.timestamp}` +
        (e.agentName ? ` | Agent: ${e.agentName}` : '') +
        (e.toolName ? ` | Tool: ${e.toolName}` : '') +
        (e.decision ? ` | Decision: ${e.decision}` : '') +
        (e.reason ? ` | Reason: ${e.reason}` : ''))
        .join('\n');
}
// ---------------------------------------------------------------------------
// Section-type prompt builders
// ---------------------------------------------------------------------------
/**
 * Prompt template: agent tool-use section.
 * Template ID: tpl-agent-tool-use-v1
 */
function buildAgentToolUsePrompt(params) {
    const system = buildSystemPrompt(params.framework, params.audience);
    const user = `Write the "Agent Tool Use" section of a ${params.framework} compliance report.\n` +
        `Audit window: ${params.windowStart} to ${params.windowEnd}\n\n` +
        `AVAILABLE EVENTS (you may ONLY cite IDs from this list):\n` +
        serialiseEvents(params.events) +
        `\n\n` +
        `Write 2-4 paragraphs summarising:\n` +
        `- Total volume of agent tool invocations and outcome distribution (allowed/denied)\n` +
        `- Most significant tool-use events and their compliance implications\n` +
        `- Whether the observed pattern is consistent with the ${params.framework} control requirements\n` +
        `Remember: cite every factual claim with [event-id].`;
    return { system, user, templateId: exports.PROMPT_TEMPLATE_IDS.AGENT_TOOL_USE };
}
/**
 * Prompt template: approval decision section.
 * Template ID: tpl-approval-decision-v1
 */
function buildApprovalDecisionPrompt(params) {
    const system = buildSystemPrompt(params.framework, params.audience);
    const user = `Write the "Human-in-the-Loop Approval Decisions" section of a ${params.framework} compliance report.\n` +
        `Audit window: ${params.windowStart} to ${params.windowEnd}\n\n` +
        `AVAILABLE EVENTS (you may ONLY cite IDs from this list):\n` +
        serialiseEvents(params.events) +
        `\n\n` +
        `Write 2-4 paragraphs summarising:\n` +
        `- Total approval requests and their outcomes (approved/denied/expired)\n` +
        `- Response time patterns and any SLA-relevant observations\n` +
        `- How the approval process demonstrates ${params.framework} human oversight requirements\n` +
        `Remember: cite every factual claim with [event-id].`;
    return { system, user, templateId: exports.PROMPT_TEMPLATE_IDS.APPROVAL_DECISION };
}
/**
 * Prompt template: policy violation section.
 * Template ID: tpl-policy-violation-v1
 */
function buildPolicyViolationPrompt(params) {
    const system = buildSystemPrompt(params.framework, params.audience);
    const user = `Write the "Policy Violations and Exceptions" section of a ${params.framework} compliance report.\n` +
        `Audit window: ${params.windowStart} to ${params.windowEnd}\n\n` +
        `AVAILABLE EVENTS (you may ONLY cite IDs from this list):\n` +
        serialiseEvents(params.events) +
        `\n\n` +
        `Write 2-4 paragraphs covering:\n` +
        `- Each policy violation observed, its severity, and the enforcement action taken\n` +
        `- Whether violations indicate systemic issues or isolated incidents\n` +
        `- Remediation steps taken or recommended under ${params.framework} requirements\n` +
        `If no violations occurred, state that explicitly with supporting event citations.\n` +
        `Remember: cite every factual claim with [event-id].`;
    return { system, user, templateId: exports.PROMPT_TEMPLATE_IDS.POLICY_VIOLATION };
}
/**
 * Prompt template: enforcement event section.
 * Template ID: tpl-enforcement-event-v1
 */
function buildEnforcementEventPrompt(params) {
    const system = buildSystemPrompt(params.framework, params.audience);
    const user = `Write the "Governance Enforcement Events" section of a ${params.framework} compliance report.\n` +
        `Audit window: ${params.windowStart} to ${params.windowEnd}\n\n` +
        `AVAILABLE EVENTS (you may ONLY cite IDs from this list):\n` +
        serialiseEvents(params.events) +
        `\n\n` +
        `Write 2-4 paragraphs covering:\n` +
        `- Automated enforcement actions (circuit-breaker trips, session suspensions, anomaly alerts)\n` +
        `- Evidence that governance controls were actively enforced during the window\n` +
        `- How the enforcement record supports ${params.framework} control assertions\n` +
        `Remember: cite every factual claim with [event-id].`;
    return { system, user, templateId: exports.PROMPT_TEMPLATE_IDS.ENFORCEMENT_EVENT };
}
// ---------------------------------------------------------------------------
// Prompt template text extraction (for SHA-256 hashing)
// ---------------------------------------------------------------------------
/**
 * Return the static template text for a given template ID.
 * Used to compute promptTemplateHash without event data.
 *
 * Only the structural, non-data parts of the prompt are hashed so the
 * hash is stable across different event sets for the same template.
 */
function getTemplateStaticText(templateId) {
    switch (templateId) {
        case exports.PROMPT_TEMPLATE_IDS.AGENT_TOOL_USE:
            return ('SYSTEM:' + buildSystemPrompt('__FRAMEWORK__', 'auditor') +
                '\nTEMPLATE:agent-tool-use-v1' +
                '\nWrite the "Agent Tool Use" section...' +
                CITATION_MANDATE);
        case exports.PROMPT_TEMPLATE_IDS.APPROVAL_DECISION:
            return ('SYSTEM:' + buildSystemPrompt('__FRAMEWORK__', 'auditor') +
                '\nTEMPLATE:approval-decision-v1' +
                '\nWrite the "Human-in-the-Loop Approval Decisions" section...' +
                CITATION_MANDATE);
        case exports.PROMPT_TEMPLATE_IDS.POLICY_VIOLATION:
            return ('SYSTEM:' + buildSystemPrompt('__FRAMEWORK__', 'auditor') +
                '\nTEMPLATE:policy-violation-v1' +
                '\nWrite the "Policy Violations and Exceptions" section...' +
                CITATION_MANDATE);
        case exports.PROMPT_TEMPLATE_IDS.ENFORCEMENT_EVENT:
            return ('SYSTEM:' + buildSystemPrompt('__FRAMEWORK__', 'auditor') +
                '\nTEMPLATE:enforcement-event-v1' +
                '\nWrite the "Governance Enforcement Events" section...' +
                CITATION_MANDATE);
    }
}
//# sourceMappingURL=base.js.map