# Implementation Summary: Rules CLI Generation Feature ## Status: ✅ Complete ## Files Modified ### D:\Claude_dms3\ccw\src\core\routes\rules-routes.ts **Changes:** 1. Added import for `executeCliTool` from cli-executor 2. Implemented `generateRuleViaCLI()` function 3. Modified POST `/api/rules/create` endpoint to support `mode: 'cli-generate'` ## Implementation Details ### 1. New Function: `generateRuleViaCLI()` **Location:** lines 224-340 **Purpose:** Generate rule content using Gemini CLI based on different generation strategies **Parameters:** - `generationType`: 'description' | 'template' | 'extract' - `description`: Natural language description of the rule - `templateType`: Template category for structured generation - `extractScope`: File pattern for code analysis (e.g., 'src/**/*.ts') - `extractFocus`: Focus areas for extraction (e.g., 'error handling, naming') - `fileName`: Target filename (must end with .md) - `location`: 'project' or 'user' - `subdirectory`: Optional subdirectory path - `projectPath`: Project root directory **Process Flow:** 1. Parse parameters and determine generation type 2. Build appropriate CLI prompt template based on type 3. Execute Gemini CLI with: - Tool: 'gemini' - Mode: 'write' for description/template, 'analysis' for extract - Timeout: 10 minutes (600000ms) - Working directory: projectPath 4. Validate CLI execution result 5. Extract generated content from stdout 6. Call `createRule()` to save the file 7. Return result with execution ID ### 2. Prompt Templates #### Description Mode (write) ``` PURPOSE: Generate Claude Code memory rule from description to guide Claude's behavior TASK: • Analyze rule requirements • Generate markdown content with clear instructions MODE: write EXPECTED: Complete rule content in markdown format RULES: $(cat ~/.claude/workflows/cli-templates/prompts/universal/00-universal-rigorous-style.txt) ``` #### Template Mode (write) ``` PURPOSE: Generate Claude Code rule from template type TASK: • Create rule based on {templateType} • Generate structured markdown content MODE: write EXPECTED: Complete rule content in markdown format following template structure RULES: $(cat ~/.claude/workflows/cli-templates/prompts/universal/00-universal-rigorous-style.txt) ``` #### Extract Mode (analysis) ``` PURPOSE: Extract coding rules from existing codebase to document patterns and conventions TASK: • Analyze code patterns • Extract common conventions • Identify best practices MODE: analysis CONTEXT: @{extractScope || '**/*'} EXPECTED: Rule content based on codebase analysis with examples RULES: $(cat ~/.claude/workflows/cli-templates/prompts/analysis/02-analyze-code-patterns.txt) ``` ### 3. API Endpoint Modification **Endpoint:** POST `/api/rules/create` **Enhanced Request Body:** ```json { "mode": "cli-generate", // NEW: triggers CLI generation "generationType": "description", // NEW: 'description' | 'template' | 'extract' "description": "...", // NEW: for description mode "templateType": "...", // NEW: for template mode "extractScope": "src/**/*.ts", // NEW: for extract mode "extractFocus": "...", // NEW: for extract mode "fileName": "rule-name.md", // REQUIRED "location": "project", // REQUIRED: 'project' | 'user' "subdirectory": "", // OPTIONAL "projectPath": "..." // OPTIONAL: defaults to initialPath } ``` **Backward Compatibility:** Existing manual creation still works: ```json { "fileName": "rule-name.md", "content": "# Rule Content\n...", "location": "project", "paths": [], "subdirectory": "" } ``` **Response Format:** ```json { "success": true, "fileName": "rule-name.md", "location": "project", "path": "/absolute/path/to/rule-name.md", "subdirectory": null, "generatedContent": "# Generated Content\n...", "executionId": "1734168000000-gemini" } ``` ## Error Handling ### Validation Errors - Missing `fileName`: "File name is required" - Missing `location`: "Location is required (project or user)" - Missing `generationType` in CLI mode: "generationType is required for CLI generation mode" - Missing `description` for description mode: "description is required for description-based generation" - Missing `templateType` for template mode: "templateType is required for template-based generation" - Unknown `generationType`: "Unknown generation type: {type}" ### CLI Execution Errors - CLI tool failure: Returns `{ error: "CLI execution failed: ...", stderr: "..." }` - Empty content: Returns `{ error: "CLI execution returned empty content", stdout: "...", stderr: "..." }` - Timeout: CLI executor will timeout after 10 minutes - File exists: "Rule '{fileName}' already exists in {location} location" ## Testing ### Test Document Created: `D:\Claude_dms3\test-rules-cli-generation.md` Contains: - API usage examples for all 3 generation types - Request/response format examples - Error handling scenarios - Integration details ### Compilation Test ✅ TypeScript compilation successful (`npm run build`) ## Integration Points ### Dependencies - **cli-executor.ts**: Provides `executeCliTool()` for Gemini execution - **createRule()**: Existing function for file creation - **handlePostRequest()**: Existing request handler from RouteContext ### CLI Tool - **Tool**: Gemini (via `executeCliTool()`) - **Timeout**: 10 minutes (600000ms) - **Mode**: 'write' for generation, 'analysis' for extraction - **Working Directory**: Project path for context access ## Next Steps (Not Implemented) 1. **UI Integration**: Add frontend interface in Rules Manager dashboard 2. **Streaming Output**: Display CLI execution progress in real-time 3. **Preview**: Show generated content before saving 4. **Refinement**: Allow iterative refinement of generated rules 5. **Templates Library**: Add predefined template types 6. **History**: Track generation history and allow regeneration ## Verification Checklist - [x] Import cli-executor functions - [x] Implement `generateRuleViaCLI()` with 3 generation types - [x] Build appropriate prompts for each type - [x] Use correct MODE (analysis vs write) - [x] Set timeout to at least 10 minutes - [x] Integrate with `createRule()` for file creation - [x] Modify POST endpoint to support `mode: 'cli-generate'` - [x] Validate required parameters - [x] Return unified result format - [x] Handle errors appropriately - [x] Maintain backward compatibility - [x] Verify TypeScript compilation - [x] Create test documentation ## Files Created - `D:\Claude_dms3\test-rules-cli-generation.md`: Test documentation - `D:\Claude_dms3\IMPLEMENTATION_SUMMARY.md`: This file