Private/Build-ReviewPrompt.ps1

function Build-ReviewPrompt {
    <#
    .SYNOPSIS
    Build system and user prompts for AI code review
     
    .DESCRIPTION
    Constructs structured prompts for AI providers with review rules and code context.
     
    .PARAMETER Rules
    Markdown content containing review rules
     
    .PARAMETER ReviewContext
    Array of file diffs with context
     
    .EXAMPLE
    $prompts = Build-ReviewPrompt -Rules $rules -ReviewContext $context
     
    .OUTPUTS
    System.Object - Hashtable with SystemPrompt and UserPrompt
     
    .NOTES
    Author: waldo
    Version: 1.0.0
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [string]$Rules,
        
        [Parameter(Mandatory = $true)]
        [array]$ReviewContext,
        
        [Parameter(Mandatory = $false)]
        [string]$SystemPromptText
    )
    
    begin {
        Write-Verbose "Starting $($MyInvocation.MyCommand.Name)"
    }
    
    process {
        try {
            # Use custom system prompt if provided, otherwise use default
            if ($SystemPromptText) {
                Write-Verbose "Using custom system prompt"
                $systemPrompt = $SystemPromptText
            }
            else {
                Write-Verbose "Using default system prompt"
                $systemPrompt = @"
You are an expert AL (Application Language) code reviewer for Microsoft Dynamics 365 Business Central at iFacto.
 
Your task is to review changed code in a Pull Request and identify violations EXCLUSIVELY against the company-specific coding standards provided in the user prompt.
 
**CRITICAL REQUIREMENT:**
- Apply ONLY the rules and standards explicitly defined in the "Review Rules" section of the user prompt
- Do NOT apply generic AL best practices, public Microsoft guidelines, or other external standards
- Do NOT use your general knowledge of AL development
- If the provided rules don't cover a specific pattern, do NOT flag it as an issue
- Every violation MUST directly reference a specific rule ID or standard from the documentation in the user prompt
 
## Output Requirements
 
Before providing violations, first list the guidelines you are applying from the Review Rules. Format as:
 
**Guidelines Applied:**
- Guideline 1 name
- Guideline 2 name
- etc.
 
Then provide your review as a JSON array with the following schema:
 
[
  {
    "file": "path/to/file.al",
    "line": 45,
    "severity": "error|warning|info",
    "message": "Clear description of the issue",
    "suggestion": "Optional: Suggested code fix"
  }
]
 
**Severity Guidelines:**
- error: Bugs, security issues, breaking changes, violations of critical standards
- warning: Code smells, performance concerns, maintainability issues
- info: Suggestions for improvement, style preferences
 
**Important:**
1. Start with the "Guidelines Applied" list
2. Only review the CHANGED lines (marked with + in diffs)
3. Do not report on pre-existing code unless directly related to changes
4. Be specific: reference exact line numbers and code snippets
5. Provide actionable feedback with clear explanations
 
Return the guidelines list followed by the JSON array, no additional text or markdown formatting.
"@

            }
            
            # Build user prompt with rules and code diffs
            $userPrompt = "## Review Rules (iFacto Company Standards - EXCLUSIVE)`n`n"
            $userPrompt += $Rules
            $userPrompt += "`n`n---`n`n"
            $userPrompt += "## Code Changes to Review`n`n"
            $userPrompt += "Review the following code changes against the rules above:`n`n"
            
            foreach ($context in $ReviewContext) {
                $userPrompt += "### File: $($context.FilePath)`n`n"
                $userPrompt += "``````diff`n"
                $userPrompt += $context.FullDiff
                $userPrompt += "`n``````"
                $userPrompt += "`n`n"
            }
            
            $userPrompt += "`n---`n`n"
            $userPrompt += "Provide your response in the format specified in the system prompt:`n"
            $userPrompt += "1. List the guidelines you applied`n"
            $userPrompt += "2. Provide violations as JSON array (or empty array if none)`n"
            
            return @{
                SystemPrompt = $systemPrompt
                UserPrompt = $userPrompt
            }
        }
        catch {
            Write-Error "Error in $($MyInvocation.MyCommand.Name): $_"
            throw
        }
    }
    
    end {
        Write-Verbose "Completed $($MyInvocation.MyCommand.Name)"
    }
}