scai
Version:
> **AI-powered CLI for local code analysis, commit message suggestions, and natural-language queries.** 100% local, private, GDPR-friendly, made in Denmark/EU with ❤️.
73 lines (68 loc) • 2.4 kB
JavaScript
import { Config } from '../../config.js';
import { generate } from '../../lib/generate.js';
import { detectFileType } from '../../fileRules/detectFileType.js';
export const addCommentsModule = {
name: 'comments',
description: 'Adds meaningful comments to any file type (code, config, or data)',
groups: ["transform"],
run: async (input) => {
const model = Config.getModel();
const filepath = typeof input.query === 'string' ? input.query : '';
const content = typeof input.content === 'string' ? input.content : '';
const fileType = detectFileType(filepath);
const commentMap = {
javascript: '//',
typescript: '//',
java: '//',
rust: '//',
c: '//',
cpp: '//',
csharp: '//',
go: '//',
swift: '//',
kotlin: '//',
scala: '//',
python: '#',
ruby: '#',
php: '//',
shell: '#',
bash: '#',
config: '#',
yaml: '#',
markdown: '<!-- -->',
html: '<!-- -->',
xml: '<!-- -->',
json: '/* */',
sql: '--',
csv: '#',
tsv: '#',
text: '#',
};
const commentSyntax = commentMap[fileType] || '//';
const prompt = `
You are a senior engineer reviewing a ${fileType} file.
Please:
- Add summary comments (2–3 lines) at relevant points for greater class insights.
- Add clear, helpful inline-comments to explain non-obvious logic inside functions.
- Use "${commentSyntax}" as the comment syntax appropriate for ${fileType}.
Rules:
- Return the full original chunk of code with added comments.
- Inline comments should clarify complex or tricky parts.
${content}
`.trim();
// ✅ Generate now uses ModuleIO (query + content)
const response = await generate({
query: filepath,
content: prompt,
});
// ✅ The result is now in `data`
const commentedContent = typeof response.data === 'string' && response.data !== 'NO UPDATE'
? response.data
: content;
// ✅ Return proper ModuleIO shape (output → data)
return {
query: filepath,
data: commentedContent,
};
},
};