UNPKG

@bestdefense/bd-agent

Version:

An AI-powered coding assistant CLI that connects to AWS Bedrock

222 lines (212 loc) 7.97 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClaudeMdManager = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const chalk_1 = __importDefault(require("chalk")); class ClaudeMdManager { config = {}; filePath; constructor(projectRoot) { this.filePath = path.join(projectRoot || process.cwd(), 'CLAUDE.md'); this.load(); } load() { try { if (fs.existsSync(this.filePath)) { const content = fs.readFileSync(this.filePath, 'utf-8'); this.parseContent(content); console.log(chalk_1.default.green('✓ Loaded project instructions from CLAUDE.md')); } } catch (error) { console.error(chalk_1.default.yellow('⚠️ Failed to load CLAUDE.md:'), error); } } parseContent(content) { // Parse different sections of CLAUDE.md const sections = this.extractSections(content); // Extract main instructions if (sections.main) { this.config.instructions = sections.main.trim(); } // Extract commands section if (sections.commands) { this.config.commands = this.parseCommands(sections.commands); } // Extract context section if (sections.context) { this.config.context = sections.context.trim(); } // Extract rules section if (sections.rules) { this.config.rules = this.parseRules(sections.rules); } } extractSections(content) { const sections = {}; // Split by headers const headerRegex = /^#{1,3}\s+(.+)$/gm; const parts = content.split(headerRegex); // First part is main content before any headers if (parts[0]) { sections.main = parts[0]; } // Process header sections for (let i = 1; i < parts.length; i += 2) { const header = parts[i].toLowerCase().trim(); const content = parts[i + 1] || ''; if (header.includes('command')) { sections.commands = content; } else if (header.includes('context') || header.includes('architecture')) { sections.context = content; } else if (header.includes('rule') || header.includes('guideline')) { sections.rules = content; } else { // Add to context if not recognized sections.context = (sections.context || '') + '\n\n## ' + parts[i] + '\n' + content; } } return sections; } parseCommands(commandsSection) { const commands = {}; // Parse command blocks (e.g., "- `npm run dev` - Start development server") const commandRegex = /[-*]\s*`([^`]+)`\s*[-:]?\s*(.+)/g; let match; while ((match = commandRegex.exec(commandsSection)) !== null) { commands[match[1]] = match[2].trim(); } // Also parse code blocks with commands const codeBlockRegex = /```(?:bash|shell|sh)?\n([\s\S]*?)```/g; while ((match = codeBlockRegex.exec(commandsSection)) !== null) { const lines = match[1].trim().split('\n'); lines.forEach(line => { const cmdMatch = line.match(/^(\S+(?:\s+\S+)*?)(?:\s*#\s*(.+))?$/); if (cmdMatch && cmdMatch[1]) { commands[cmdMatch[1]] = cmdMatch[2] || 'Run command'; } }); } return commands; } parseRules(rulesSection) { const rules = []; // Parse bullet points const bulletRegex = /[-*]\s+(.+)/g; let match; while ((match = bulletRegex.exec(rulesSection)) !== null) { rules.push(match[1].trim()); } // Parse numbered lists const numberedRegex = /\d+\.\s+(.+)/g; while ((match = numberedRegex.exec(rulesSection)) !== null) { rules.push(match[1].trim()); } return rules; } getInstructions() { return this.config.instructions || ''; } getContext() { return this.config.context || ''; } getRules() { return this.config.rules || []; } getCommands() { return this.config.commands || {}; } getSystemPromptAdditions() { let additions = ''; if (this.config.instructions) { additions += `\n\n## Project-Specific Instructions\n${this.config.instructions}`; } if (this.config.context) { additions += `\n\n## Project Context\n${this.config.context}`; } if (this.config.rules && this.config.rules.length > 0) { additions += `\n\n## Project Rules\n${this.config.rules.map(r => `- ${r}`).join('\n')}`; } if (this.config.commands && Object.keys(this.config.commands).length > 0) { additions += `\n\n## Available Commands\n`; for (const [cmd, desc] of Object.entries(this.config.commands)) { additions += `- \`${cmd}\` - ${desc}\n`; } } return additions; } // Check if a file exists for the project exists() { return fs.existsSync(this.filePath); } // Create a template CLAUDE.md file static createTemplate(projectPath) { const templatePath = path.join(projectPath, 'CLAUDE.md'); if (fs.existsSync(templatePath)) { console.log(chalk_1.default.yellow('CLAUDE.md already exists')); return; } const template = `# CLAUDE.md This file provides project-specific instructions for BD Agent. ## Commands ### Development - \`npm run dev\` - Start the development server - \`npm run build\` - Build the project - \`npm test\` - Run tests ### Deployment - \`npm run deploy\` - Deploy to production ## Architecture Describe your project architecture here... ## Rules and Guidelines - Always follow the project's coding standards - Run tests before committing - Update documentation when adding new features ## Context Any additional context about the project... `; fs.writeFileSync(templatePath, template); console.log(chalk_1.default.green(`✓ Created CLAUDE.md template at ${templatePath}`)); } } exports.ClaudeMdManager = ClaudeMdManager; //# sourceMappingURL=claude-md.js.map