UNPKG

trace.ai-cli

Version:

A powerful AI-powered CLI tool

60 lines (49 loc) 1.54 kB
const path = require('path'); const { getFileType, readFileContent } = require('../utils/fileUtils'); const { processWithAI } = require('./aiService'); const { extractTextFromImage } = require('./imageService'); async function analyzeFile(filePath, query = '') { try { const fileType = getFileType(filePath); if (fileType === 'image') { return await extractTextFromImage(filePath); } const content = await readFileContent(filePath); const fileName = path.basename(filePath); let prompt; if (query) { prompt = `File: ${fileName} (${fileType}) Content: \`\`\` ${content} \`\`\` User Question: ${query}`; } else { if (fileType !== 'Unknown' && fileType !== 'Text' && fileType !== 'Markdown') { prompt = `Analyze this ${fileType} code file (${fileName}): \`\`\` ${content} \`\`\` Please provide: 1. Code overview and purpose 2. Key functions/components 3. Potential issues or improvements 4. Code quality assessment 5. Suggestions for optimization`; } else { prompt = `Analyze this file content (${fileName}): \`\`\` ${content} \`\`\` Please provide a summary and analysis of the content.`; } } const result = await processWithAI(prompt); return { text: result || 'No response generated' }; // Ensure consistent return format } catch (error) { throw error; } } module.exports = { analyzeFile };