All files / utils prompt.ts

100% Statements 46/46
96.87% Branches 31/32
100% Functions 2/2
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                                                            10x     10x         10x             10x 10x 10x 10x 10x   10x 1x         1x     10x   10x   2x 2x 8x   2x 2x 2x 1x 1x   2x     6x   6x 2x 2x 2x 4x 2x 2x 2x 1x 1x   2x 2x   2x 2x       10x       4x 1x 1x       1x      
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as tl from 'azure-pipelines-task-lib/task';
 
export interface PromptConfig {
    promptInput: string | undefined;
    promptFileInput: string | undefined;
    promptRawInput: string | undefined;
    promptFileRawInput: string | undefined;
    promptTemplatePath: string;
    workingDir: string;
}
 
/**
 * Resolves and validates the prompt inputs, returning the path to the final
 * prompt file to use for the CLI agent.
 *
 * All paths use the provided prompt template file path.
 * - Default: the template with %CUSTOMPROMPT% stripped
 * - Custom (inline or file): user text replaces %CUSTOMPROMPT%
 * - Raw (inline or file): used verbatim, bypassing the template entirely
 */
export function resolvePrompt(config: PromptConfig): string {
    const {
        promptInput,
        promptFileInput,
        promptRawInput,
        promptFileRawInput,
        promptTemplatePath,
        workingDir,
    } = config;
 
    // filePath inputs return the working directory when empty — treat as unset
    const isPromptFileSet = !!(
        promptFileInput &&
        fs.existsSync(promptFileInput) &&
        fs.statSync(promptFileInput).isFile()
    );
    const isPromptFileRawSet = !!(
        promptFileRawInput &&
        fs.existsSync(promptFileRawInput) &&
        fs.statSync(promptFileRawInput).isFile()
    );
 
    // Validate mutual exclusivity
    const active: string[] = [];
    if (promptInput) active.push('prompt');
    if (isPromptFileSet) active.push('promptFile');
    if (promptRawInput) active.push('promptRaw');
    if (isPromptFileRawSet) active.push('promptFileRaw');
 
    if (active.length > 1) {
        tl.setResult(
            tl.TaskResult.Failed,
            `Multiple prompt inputs are set (${active.join(', ')}). Only one prompt input should be provided. ` +
            'Please use only one of: prompt, promptFile, promptRaw, or promptFileRaw.'
        );
        process.exit(1);
    }
 
    const outPath = path.join(workingDir, '_copilot_prompt.txt');
 
    if (promptRawInput) {
        // Raw prompt: pass directly, bypass template
        console.log('  Prompt: raw (inline)');
        fs.writeFileSync(outPath, promptRawInput, 'utf8');
    } else if (isPromptFileRawSet) {
        // Raw prompt file: use contents as-is
        console.log('  Prompt: raw (file)');
        const fileContent = fs.readFileSync(promptFileRawInput!, 'utf8');
        if (!fileContent.trim()) {
            tl.setResult(tl.TaskResult.Failed, `Raw prompt file is empty: ${promptFileRawInput}`);
            process.exit(1);
        }
        fs.writeFileSync(outPath, fileContent, 'utf8');
    } else {
        // Template-based: read template and inject custom text or strip placeholder
        const template = fs.readFileSync(promptTemplatePath, 'utf8');
 
        if (promptInput) {
            console.log('  Prompt: custom (inline)');
            validateNoDoubleQuotes(promptInput);
            fs.writeFileSync(outPath, template.replace('%CUSTOMPROMPT%', promptInput), 'utf8');
        } else if (isPromptFileSet) {
            console.log('  Prompt: custom (file)');
            const fileContent = fs.readFileSync(promptFileInput!, 'utf8').trim();
            if (!fileContent) {
                tl.setResult(tl.TaskResult.Failed, `Prompt file is empty: ${promptFileInput}`);
                process.exit(1);
            }
            validateNoDoubleQuotes(fileContent, promptFileInput);
            fs.writeFileSync(outPath, template.replace('%CUSTOMPROMPT%', fileContent), 'utf8');
        } else {
            console.log('  Prompt: default');
            fs.writeFileSync(outPath, template.replace('%CUSTOMPROMPT%', ''), 'utf8');
        }
    }
 
    return outPath;
}
 
function validateNoDoubleQuotes(text: string, source?: string): void {
    if (text.includes('"')) {
        const location = source ? `the prompt file: ${source}` : 'your prompt input';
        tl.setResult(
            tl.TaskResult.Failed,
            `Custom prompts cannot include double quotes ("). Please remove any double quotes from ${location}.`
        );
        process.exit(1);
    }
}