|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Action for 'praxis spec validate <spec-id>' |
| 6 | + */ |
| 7 | +export async function specValidateAction(specId: string) { |
| 8 | + const rootDir = process.cwd(); |
| 9 | + const specsDir = path.join(rootDir, '.praxis', 'specs', 'wip'); |
| 10 | + const intentsDir = path.join(rootDir, '.praxis', 'intents', 'wip'); |
| 11 | + const specTemplatePath = path.join(rootDir, '.praxis', 'templates', 'spec-template.md'); |
| 12 | + |
| 13 | + if (!fs.existsSync(specsDir)) { |
| 14 | + console.log('No specs found (directory .praxis/specs does not exist).'); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + // 1. Find spec directory |
| 19 | + const specDirectory = findSpecDirectory(specsDir, specId); |
| 20 | + if (!specDirectory) { |
| 21 | + console.error(`Error: Spec with ID "${specId}" not found.`); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + // 2. Read spec template |
| 26 | + let templateContent = 'No spec template found.'; |
| 27 | + if (fs.existsSync(specTemplatePath)) { |
| 28 | + templateContent = fs.readFileSync(specTemplatePath, 'utf8'); |
| 29 | + } |
| 30 | + |
| 31 | + // 3. Read spec content and artifacts |
| 32 | + const specFile = path.join(specDirectory, 'spec.md'); |
| 33 | + let specContent = ''; |
| 34 | + if (fs.existsSync(specFile)) { |
| 35 | + specContent = fs.readFileSync(specFile, 'utf8'); |
| 36 | + } |
| 37 | + |
| 38 | + const otherArtifacts: { name: string, content: string }[] = []; |
| 39 | + const files = fs.readdirSync(specDirectory); |
| 40 | + for (const file of files) { |
| 41 | + if (file === 'spec.md' || file === '.gitkeep') continue; |
| 42 | + const filePath = path.join(specDirectory, file); |
| 43 | + if (fs.statSync(filePath).isFile()) { |
| 44 | + otherArtifacts.push({ |
| 45 | + name: file, |
| 46 | + content: fs.readFileSync(filePath, 'utf8') |
| 47 | + }); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // 4. Extract intent-id and find intent content |
| 52 | + const intentId = extractIntentId(specContent); |
| 53 | + let intentContent = 'No associated intent found.'; |
| 54 | + if (intentId) { |
| 55 | + const intentDir = findIntentDirectory(intentsDir, intentId); |
| 56 | + if (intentDir) { |
| 57 | + const intentFile = path.join(intentDir, 'intent.md'); |
| 58 | + if (fs.existsSync(intentFile)) { |
| 59 | + intentContent = fs.readFileSync(intentFile, 'utf8'); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // 5. Generate Prompt |
| 65 | + const prompt = generateSpecValidatePrompt(specId, specContent, templateContent, intentContent, otherArtifacts); |
| 66 | + |
| 67 | + // 6. Output |
| 68 | + console.log('\n--- IDE AI CHAT PROMPT ---'); |
| 69 | + console.log('Copy and paste the following into your IDE chat to validate the specification:'); |
| 70 | + console.log('--------------------------'); |
| 71 | + console.log(prompt); |
| 72 | + console.log('--------------------------\n'); |
| 73 | +} |
| 74 | + |
| 75 | +export function findSpecDirectory(dir: string, targetId: string): string | null { |
| 76 | + try { |
| 77 | + const list = fs.readdirSync(dir); |
| 78 | + |
| 79 | + // Check if direct match exists |
| 80 | + const directPath = path.join(dir, targetId); |
| 81 | + if (fs.existsSync(directPath) && fs.statSync(directPath).isDirectory()) { |
| 82 | + if (fs.existsSync(path.join(directPath, 'spec.md'))) { |
| 83 | + return directPath; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Recursive search |
| 88 | + for (const file of list) { |
| 89 | + const filePath = path.join(dir, file); |
| 90 | + const stat = fs.statSync(filePath); |
| 91 | + |
| 92 | + if (stat.isDirectory()) { |
| 93 | + if (file === targetId) { |
| 94 | + if (fs.existsSync(path.join(filePath, 'spec.md'))) { |
| 95 | + return filePath; |
| 96 | + } |
| 97 | + } |
| 98 | + const found = findSpecDirectory(filePath, targetId); |
| 99 | + if (found) return found; |
| 100 | + } |
| 101 | + } |
| 102 | + } catch (e) { |
| 103 | + // Ignore errors |
| 104 | + } |
| 105 | + return null; |
| 106 | +} |
| 107 | + |
| 108 | +export function findIntentDirectory(dir: string, targetId: string): string | null { |
| 109 | + try { |
| 110 | + if (!fs.existsSync(dir)) return null; |
| 111 | + const list = fs.readdirSync(dir); |
| 112 | + |
| 113 | + const directPath = path.join(dir, targetId); |
| 114 | + if (fs.existsSync(directPath) && fs.statSync(directPath).isDirectory()) { |
| 115 | + if (fs.existsSync(path.join(directPath, 'intent.md'))) { |
| 116 | + return directPath; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + for (const file of list) { |
| 121 | + const filePath = path.join(dir, file); |
| 122 | + const stat = fs.statSync(filePath); |
| 123 | + |
| 124 | + if (stat.isDirectory()) { |
| 125 | + if (file === targetId) { |
| 126 | + if (fs.existsSync(path.join(filePath, 'intent.md'))) { |
| 127 | + return filePath; |
| 128 | + } |
| 129 | + } |
| 130 | + const found = findIntentDirectory(filePath, targetId); |
| 131 | + if (found) return found; |
| 132 | + } |
| 133 | + } |
| 134 | + } catch (e) { |
| 135 | + // Ignore errors |
| 136 | + } |
| 137 | + return null; |
| 138 | +} |
| 139 | + |
| 140 | +export function extractIntentId(specContent: string): string | null { |
| 141 | + const match = specContent.match(/\*\*Intent\*\*:\s*([^\s\r\n]+)/); |
| 142 | + return match ? match[1] : null; |
| 143 | +} |
| 144 | + |
| 145 | +export function generateSpecValidatePrompt( |
| 146 | + specId: string, |
| 147 | + specContent: string, |
| 148 | + templateContent: string, |
| 149 | + intentContent: string, |
| 150 | + artifacts: { name: string, content: string }[] |
| 151 | +): string { |
| 152 | + let artifactsSection = ''; |
| 153 | + if (artifacts.length > 0) { |
| 154 | + artifactsSection = '\n**Additional Artifacts found in spec directory**:\n'; |
| 155 | + artifacts.forEach(art => { |
| 156 | + artifactsSection += `\n- **${art.name}**:\n\`\`\`\n${art.content}\n\`\`\`\n`; |
| 157 | + }); |
| 158 | + } |
| 159 | + |
| 160 | + return `You are an expert software architect and quality assurance engineer. |
| 161 | +I need to validate the Specification with ID: **${specId}**. |
| 162 | +
|
| 163 | +The validation MUST take into account: |
| 164 | +1. The **Specification Content** against the **Specification Template**. |
| 165 | +2. The **Specification Content** against its parent **Intent Content** (to ensure traceability and alignment). |
| 166 | +3. Any **Additional Artifacts** related to this specification. |
| 167 | +4. The overall internal consistency and completeness of the specification. |
| 168 | +
|
| 169 | +**SPECIFICATION TEMPLATE**: |
| 170 | +\`\`\`markdown |
| 171 | +${templateContent} |
| 172 | +\`\`\` |
| 173 | +
|
| 174 | +**PARENT INTENT CONTENT**: |
| 175 | +\`\`\`markdown |
| 176 | +${intentContent} |
| 177 | +\`\`\` |
| 178 | +
|
| 179 | +**SPECIFICATION CONTENT**: |
| 180 | +\`\`\`markdown |
| 181 | +${specContent || 'No spec.md content found.'} |
| 182 | +\`\`\` |
| 183 | +${artifactsSection} |
| 184 | +
|
| 185 | +Please perform a thorough validation and suggest any necessary modifications to the artifacts to improve clarity, completeness, or alignment with the template and the parent intent. |
| 186 | +Your output must result in a modification of the artifacts if any issues are found.`; |
| 187 | +} |
0 commit comments