|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { fileURLToPath } from 'url' |
| 4 | +import { Command } from 'commander' |
| 5 | +import fs from 'fs' |
| 6 | +import yaml from 'js-yaml' |
| 7 | +import path from 'path' |
| 8 | +import ora from 'ora' |
| 9 | +import github from '#src/workflows/github.ts' |
| 10 | +import { callModelsApi } from '#src/ai-editors/lib/call-models-api.js' |
| 11 | + |
| 12 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 13 | +const promptDir = path.join(__dirname, '../prompts') |
| 14 | + |
| 15 | +if (!process.env.GITHUB_TOKEN) { |
| 16 | + throw new Error('Error! You must have a GITHUB_TOKEN set in an .env file to run this script.') |
| 17 | +} |
| 18 | + |
| 19 | +const responseTypes = { |
| 20 | + rewrite: 'Edit the versioning only. Return the edited content.', |
| 21 | + list: `Do NOT rewrite the content. Report your edits in numbered list format.`, |
| 22 | + json: `Do NOT rewrite the content. Report your edits as a JSON list, with the format { lineNumber, currentText, suggestion }.`, |
| 23 | +} |
| 24 | + |
| 25 | +const validResponseTypes = Object.keys(responseTypes) |
| 26 | + |
| 27 | +const editorTypes = { |
| 28 | + versioning: { |
| 29 | + promptFile: 'versioning-editor.prompt.yml', |
| 30 | + description: 'Review against simplifying versioning guidelines.', |
| 31 | + }, |
| 32 | + // TODO |
| 33 | + // scannability: { |
| 34 | + // promptFile: 'scannability-editor.prompt.yml', |
| 35 | + // description: 'Review against scannability guidelines.', |
| 36 | + // }, |
| 37 | + // readability: { |
| 38 | + // promptFile: 'readability-editor.prompt.yml', |
| 39 | + // description: |
| 40 | + // 'Review against readability criteria like Gunning Fog index, Hemingway, word count, sentence length, etc.', |
| 41 | + // }, |
| 42 | + // technical: { |
| 43 | + // promptFile: 'technical-editor.prompt.yml', |
| 44 | + // description: 'Review against provided product information for technical accuracy.', |
| 45 | + // }, |
| 46 | + // styleguide: { |
| 47 | + // promptFile: 'styleguide-editor.prompt.yml', |
| 48 | + // description: 'Review against the GitHub Docs style guide.', |
| 49 | + // }, |
| 50 | + // contentModels: { |
| 51 | + // promptFile: 'content-models-editor.prompt.yml', |
| 52 | + // description: 'Review against the GitHub Docs content models.', |
| 53 | + // }, |
| 54 | + // Add more here... |
| 55 | +} |
| 56 | + |
| 57 | +const editorDescriptions = () => { |
| 58 | + let str = '\n\n' |
| 59 | + Object.entries(editorTypes).forEach(([ed, edObj]) => { |
| 60 | + str += `\t${ed}\n\t\t\t${edObj.description}\n\n` |
| 61 | + }) |
| 62 | + return str |
| 63 | +} |
| 64 | + |
| 65 | +const program = new Command() |
| 66 | + |
| 67 | +program |
| 68 | + .name('ai-edit') |
| 69 | + .description('Edit content files using AI') |
| 70 | + .option('-v, --verbose', 'Enable verbose output') |
| 71 | + .option( |
| 72 | + '-e, --editor <type...>', |
| 73 | + `Specify one or more editor type: ${editorDescriptions().trimEnd()}\n`, |
| 74 | + ) |
| 75 | + .option( |
| 76 | + '-r, --response <type>', |
| 77 | + `Specify response type: ${validResponseTypes.join(', ')} (default: rewrite)`, |
| 78 | + ) |
| 79 | + .requiredOption( |
| 80 | + '-f, --files <files...>', |
| 81 | + 'One or more content file paths in the content directory', |
| 82 | + ) |
| 83 | + .action((options) => { |
| 84 | + ;(async () => { |
| 85 | + const spinner = ora('Starting AI review...').start() |
| 86 | + |
| 87 | + const files = options.files |
| 88 | + const editors = options.editor || ['versioning'] |
| 89 | + const response = options.response || 'rewrite' |
| 90 | + |
| 91 | + let responseTypeInstruction |
| 92 | + if (validResponseTypes.includes(response)) { |
| 93 | + responseTypeInstruction = responseTypes[response] |
| 94 | + } else { |
| 95 | + console.error( |
| 96 | + `Invalid response type: ${response}. Must be one of: ${validResponseTypes.join(', ')}`, |
| 97 | + ) |
| 98 | + process.exit(1) |
| 99 | + } |
| 100 | + |
| 101 | + for (const file of files) { |
| 102 | + const filePath = path.resolve(process.cwd(), file) |
| 103 | + spinner.text = `Checking file: ${file}` |
| 104 | + |
| 105 | + if (!fs.existsSync(filePath)) { |
| 106 | + spinner.fail(`File not found: ${filePath}`) |
| 107 | + process.exitCode = 1 |
| 108 | + continue |
| 109 | + } |
| 110 | + |
| 111 | + try { |
| 112 | + spinner.text = `Reading file: ${file}` |
| 113 | + const content = fs.readFileSync(filePath, 'utf8') |
| 114 | + |
| 115 | + for (const editorType of editors) { |
| 116 | + spinner.text = `Running the AI-powered ${editorType} editor...` |
| 117 | + const answer = await callEditor(editorType, responseTypeInstruction, content) |
| 118 | + |
| 119 | + if (response === 'rewrite') { |
| 120 | + fs.writeFileSync(file, answer, 'utf-8') |
| 121 | + spinner.succeed(`Processed file: ${file}`) |
| 122 | + console.log(`To see changes, run "git diff" on the file.`) |
| 123 | + } else { |
| 124 | + spinner.succeed(`Processed file: ${file}`) |
| 125 | + console.log(answer) |
| 126 | + } |
| 127 | + } |
| 128 | + } catch (err) { |
| 129 | + spinner.fail(`Error processing file ${file}: ${err.message}`) |
| 130 | + process.exitCode = 1 |
| 131 | + } |
| 132 | + } |
| 133 | + })() |
| 134 | + }) |
| 135 | + |
| 136 | +program.parse(process.argv) |
| 137 | + |
| 138 | +async function callEditor(editorType, responseTypeInstruction, content) { |
| 139 | + const promptName = editorTypes[editorType].promptFile |
| 140 | + const promptPath = path.join(promptDir, promptName) |
| 141 | + const prompt = yaml.load(fs.readFileSync(promptPath, 'utf8')) |
| 142 | + prompt.messages.forEach((msg) => { |
| 143 | + msg.content = msg.content.replace('{{responseTypeInstruction}}', responseTypeInstruction) |
| 144 | + msg.content = msg.content.replace('{{input}}', content) |
| 145 | + }) |
| 146 | + return callModelsApi(prompt) |
| 147 | +} |
0 commit comments