scai
Version:
> AI-powered CLI tool for commit messages **and** pull request reviews — using local models.
75 lines (74 loc) • 3.39 kB
JavaScript
// File: src/utils/buildContextualPrompt.ts
import chalk from 'chalk';
import path from 'path';
function estimateTokenCount(text) {
return Math.round(text.length / 4); // simple heuristic approximation
}
export function buildContextualPrompt({ baseInstruction, code, summary, functions, relatedFiles, projectFileTree, }) {
const parts = [baseInstruction];
if (summary) {
parts.push(`📄 File Summary:\n${summary}`);
}
if (functions?.length) {
const formattedFunctions = functions
.map(fn => `• ${fn.name}:\n${fn.content}`)
.join('\n\n');
parts.push(`🔧 Functions:\n${formattedFunctions}`);
}
else {
console.log(chalk.yellow(` ⚠️ No functions found in top rated file.`));
}
if (relatedFiles?.length) {
const formattedRelatedFiles = relatedFiles
.map(f => {
const relatedFunctions = f.functions
.map(fn => ` • ${fn.name}:\n ${fn.content}`)
.join('\n\n');
return `• ${f.path}: ${f.summary}\n${relatedFunctions}`;
})
.join('\n\n');
parts.push(`📚 Related Files:\n${formattedRelatedFiles}`);
}
if (projectFileTree) {
parts.push(`📁 Project File Structure:\n\`\`\`\n${projectFileTree.trim()}\n\`\`\``);
}
parts.push(`\n--- CODE START ---\n${code}\n--- CODE END ---`);
const prompt = parts.join('\n\n');
const tokenEstimate = estimateTokenCount(prompt);
// 🔵 Colorized diagnostic output
// 🔵 Colorized diagnostic output
const header = chalk.bgBlue.white.bold(' [SCAI] Prompt Overview ');
const labelColor = chalk.cyan;
const contentColor = chalk.gray;
console.log('\n' + header);
console.log(labelColor('🔢 Token Estimate:'), contentColor(`${tokenEstimate.toLocaleString()} tokens`));
// 📄 Summary
if (summary) {
console.log(labelColor('📄 Summary:'), contentColor(`${estimateTokenCount(summary).toLocaleString()} tokens`));
}
// 🔧 Functions
if (functions?.length) {
const fnCount = functions.length;
const fnTokens = functions.reduce((sum, f) => sum + estimateTokenCount(f.content), 0);
console.log(labelColor(`🔧 Functions (${fnCount}):`), contentColor(`${fnTokens.toLocaleString()} tokens`));
}
// 📚 Related Files
if (relatedFiles?.length) {
const relCount = relatedFiles.length;
const relTokens = relatedFiles.reduce((sum, f) => sum + estimateTokenCount(f.summary), 0);
console.log(labelColor(`📚 Related Files (${relCount}):`), contentColor(`${relTokens.toLocaleString()} tokens`));
// Optional: Show top 3 file names
const fileList = relatedFiles.slice(0, 3).map(f => `- ${path.basename(f.path)}`).join('\n');
if (fileList)
console.log(contentColor(fileList + (relCount > 3 ? `\n ...+${relCount - 3} more` : '')));
}
// 📁 File Tree
if (projectFileTree) {
console.log(labelColor('📁 File Tree:'), contentColor(`${estimateTokenCount(projectFileTree).toLocaleString()} tokens`));
}
// 📦 Code Section
console.log(labelColor('📦 Code:'), contentColor(`${estimateTokenCount(prompt).toLocaleString()} tokens`));
// 📌 Key
console.log(labelColor('🔍 Key:'), contentColor('[buildContextualPrompt]\n'));
return prompt;
}