|
1 | 1 | import * as fs from "fs/promises" |
2 | 2 | import * as path from "path" |
| 3 | +import { |
| 4 | + generateRulesInstructions, |
| 5 | + ruleTypeDefinitions, |
| 6 | + RulesGenerationOptions, |
| 7 | + RuleInstruction, |
| 8 | +} from "../../core/prompts/instructions/generate-rules" |
3 | 9 |
|
4 | 10 | /** |
5 | 11 | * Creates a comprehensive task message for rules generation that can be used with initClineWithTask |
@@ -32,132 +38,20 @@ export async function createRulesGenerationTaskMessage( |
32 | 38 | } |
33 | 39 |
|
34 | 40 | // Create rule-specific instructions based on selected types |
35 | | - interface RuleInstruction { |
36 | | - path: string |
37 | | - focus: string |
38 | | - analysisSteps: string[] |
39 | | - } |
40 | | - |
41 | 41 | const ruleInstructions: RuleInstruction[] = selectedRuleTypes |
42 | 42 | .map((type) => { |
43 | | - switch (type) { |
44 | | - case "general": |
45 | | - return { |
46 | | - path: ".roo/rules/coding-standards.md", |
47 | | - focus: "General coding standards that apply to all modes, including naming conventions, file organization, and general best practices", |
48 | | - analysisSteps: [ |
49 | | - "Examine the project structure and file organization patterns", |
50 | | - "Identify naming conventions for files, functions, variables, and classes", |
51 | | - "Look for general coding patterns and conventions used throughout the codebase", |
52 | | - "Check for any existing documentation or README files that describe project standards", |
53 | | - ], |
54 | | - } |
55 | | - case "code": |
56 | | - return { |
57 | | - path: ".roo/rules-code/implementation-rules.md", |
58 | | - focus: "Specific rules for code implementation, focusing on syntax patterns, code structure, error handling, testing approaches, and detailed implementation guidelines", |
59 | | - analysisSteps: [ |
60 | | - "Analyze package.json or equivalent files to identify dependencies and build tools", |
61 | | - "Check for linting and formatting tools (ESLint, Prettier, etc.) and their configurations", |
62 | | - "Examine test files to understand testing patterns and frameworks used", |
63 | | - "Look for error handling patterns and logging strategies", |
64 | | - "Identify code style preferences and import/export patterns", |
65 | | - "Check for TypeScript usage and type definition patterns if applicable", |
66 | | - ], |
67 | | - } |
68 | | - case "architect": |
69 | | - return { |
70 | | - path: ".roo/rules-architect/architecture-rules.md", |
71 | | - focus: "High-level system design rules, focusing on file layout, module organization, architectural patterns, and system-wide design principles", |
72 | | - analysisSteps: [ |
73 | | - "Analyze the overall directory structure and module organization", |
74 | | - "Identify architectural patterns (MVC, microservices, monorepo, etc.)", |
75 | | - "Look for separation of concerns and layering patterns", |
76 | | - "Check for API design patterns and service boundaries", |
77 | | - "Examine how different parts of the system communicate", |
78 | | - ], |
79 | | - } |
80 | | - case "debug": |
81 | | - return { |
82 | | - path: ".roo/rules-debug/debugging-rules.md", |
83 | | - focus: "Debugging workflow rules, including error investigation approaches, logging strategies, troubleshooting patterns, and debugging best practices", |
84 | | - analysisSteps: [ |
85 | | - "Identify logging frameworks and patterns used in the codebase", |
86 | | - "Look for error handling and exception patterns", |
87 | | - "Check for debugging tools or scripts in the project", |
88 | | - "Analyze test structure for debugging approaches", |
89 | | - "Look for monitoring or observability patterns", |
90 | | - ], |
91 | | - } |
92 | | - case "docs-extractor": |
93 | | - return { |
94 | | - path: ".roo/rules-docs-extractor/documentation-rules.md", |
95 | | - focus: "Documentation extraction and formatting rules, including documentation style guides, API documentation patterns, and content organization", |
96 | | - analysisSteps: [ |
97 | | - "Check for existing documentation files and their formats", |
98 | | - "Analyze code comments and documentation patterns", |
99 | | - "Look for API documentation tools or generators", |
100 | | - "Identify documentation structure and organization patterns", |
101 | | - "Check for examples or tutorials in the codebase", |
102 | | - ], |
103 | | - } |
104 | | - default: |
105 | | - return null |
106 | | - } |
| 43 | + const definition = ruleTypeDefinitions[type as keyof typeof ruleTypeDefinitions] |
| 44 | + return definition || null |
107 | 45 | }) |
108 | 46 | .filter((rule): rule is RuleInstruction => rule !== null) |
109 | 47 |
|
110 | | - // Create a comprehensive message for the rules generation task |
111 | | - const taskMessage = `Analyze this codebase and generate comprehensive rules for AI agents working in this repository. |
112 | | -
|
113 | | -Your task is to: |
114 | | -
|
115 | | -1. **Analyze the project structure** by: |
116 | | -${ruleInstructions.map((rule) => ` - For ${rule.path.split("/").pop()}: ${rule.analysisSteps.join("; ")}`).join("\n")} |
117 | | -
|
118 | | -2. **Look for existing rule files** that might provide guidance: |
119 | | - - Check for CLAUDE.md, .cursorrules, .cursor/rules, or .github/copilot-instructions.md |
120 | | - - If found, incorporate and improve upon their content |
121 | | -
|
122 | | -3. **Generate and save the following rule files**: |
123 | | -${ruleInstructions |
124 | | - .map( |
125 | | - (rule, index) => ` |
126 | | - ${index + 1}. **${rule.path}** |
127 | | - - Focus: ${rule.focus}${alwaysAllowWriteProtected ? "\n - The directory has already been created for you" : "\n - Create the necessary directories if they don't exist"} |
128 | | - - Always overwrite the existing file if it exists |
129 | | - - Use the \`write_to_file\` tool to save the content${alwaysAllowWriteProtected ? "\n - Note: Auto-approval for protected file writes is enabled, so you can write to .roo directories without manual approval" : "\n - Note: You will need to approve the creation of protected directories and files"}`, |
130 | | - ) |
131 | | - .join("\n")} |
132 | | -
|
133 | | -4. **Make the rules actionable and specific** by including: |
134 | | - - Build/lint/test commands (especially for running single tests) |
135 | | - - Code style guidelines including imports, formatting, types, naming conventions |
136 | | - - Error handling patterns specific to this project |
137 | | - - Project-specific conventions and best practices |
138 | | - - File organization patterns |
139 | | -
|
140 | | -5. **Keep rules concise** - aim for 20 lines per file, focusing on the most important guidelines |
141 | | -
|
142 | | -6. **Open the generated files** in the editor for review after creation |
143 | | -
|
144 | | -Use the \`safeWriteJson\` utility from \`src/utils/safeWriteJson.ts\` for any JSON file operations to ensure atomic writes. |
145 | | -
|
146 | | -${ |
147 | | - addToGitignore |
148 | | - ? `7. **Add the generated files to .gitignore**: |
149 | | - - After generating all rule files, add entries to .gitignore to prevent them from being committed |
150 | | - - Add each generated file path to .gitignore (e.g., .roo/rules/coding-standards.md) |
151 | | - - If .gitignore doesn't exist, create it |
152 | | - - If the entries already exist in .gitignore, don't duplicate them` |
153 | | - : "" |
154 | | -} |
155 | | -
|
156 | | -${ |
157 | | - includeCustomRules && customRulesText |
158 | | - ? `\n**Additional rules from User to add to the rules file:**\n${customRulesText}` |
159 | | - : "" |
160 | | -}` |
| 48 | + const options: RulesGenerationOptions = { |
| 49 | + selectedRuleTypes, |
| 50 | + addToGitignore, |
| 51 | + alwaysAllowWriteProtected, |
| 52 | + includeCustomRules, |
| 53 | + customRulesText, |
| 54 | + } |
161 | 55 |
|
162 | | - return taskMessage |
| 56 | + return generateRulesInstructions(ruleInstructions, options) |
163 | 57 | } |
0 commit comments