|
| 1 | +import * as path from 'path'; |
| 2 | +import { BaseAdapter } from './base-adapter.js'; |
| 3 | +import { CommandTemplate } from '../../types/agent.js'; |
| 4 | +import { FileSystem } from '../../utils/file-system.js'; |
| 5 | +import { IntegrationError } from '../../types/errors.js'; |
| 6 | +import { ClavixConfig } from '../../types/config.js'; |
| 7 | + |
| 8 | +/** |
| 9 | + * GitHub Copilot Prompts Adapter |
| 10 | + * |
| 11 | + * Generates custom slash commands as .prompt.md files in .github/prompts/ |
| 12 | + * These appear as /clavix-improve, /clavix-prd, etc. in VS Code with GitHub Copilot |
| 13 | + * |
| 14 | + * Format reference: https://code.visualstudio.com/docs/copilot/copilot-customization |
| 15 | + * |
| 16 | + * @since v6.1.0 |
| 17 | + */ |
| 18 | +export class CopilotPromptsAdapter extends BaseAdapter { |
| 19 | + readonly name = 'copilot'; |
| 20 | + readonly displayName = 'GitHub Copilot'; |
| 21 | + readonly directory = '.github/prompts'; |
| 22 | + readonly fileExtension = '.prompt.md'; |
| 23 | + readonly features = { |
| 24 | + supportsSubdirectories: false, |
| 25 | + commandFormat: { separator: '-' as const }, |
| 26 | + }; |
| 27 | + protected readonly userConfig?: ClavixConfig; |
| 28 | + |
| 29 | + constructor(userConfig?: ClavixConfig) { |
| 30 | + super(); |
| 31 | + this.userConfig = userConfig; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Detect if GitHub Copilot is likely in use |
| 36 | + * Check for .github directory (common for GitHub projects) |
| 37 | + */ |
| 38 | + async detectProject(): Promise<boolean> { |
| 39 | + return await FileSystem.exists('.github'); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Get command path for Copilot prompts |
| 44 | + */ |
| 45 | + getCommandPath(): string { |
| 46 | + return this.directory; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get target filename for a command |
| 51 | + * Format: clavix-{name}.prompt.md |
| 52 | + */ |
| 53 | + getTargetFilename(name: string): string { |
| 54 | + return `clavix-${name}${this.fileExtension}`; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Determine if a file is a Clavix-generated prompt |
| 59 | + */ |
| 60 | + protected isClavixGeneratedCommand(filename: string): boolean { |
| 61 | + return filename.startsWith('clavix-') && filename.endsWith('.prompt.md'); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Format command content with Copilot prompt file frontmatter |
| 66 | + * |
| 67 | + * Copilot prompt files support YAML frontmatter with: |
| 68 | + * - name: The slash command name (shown after /) |
| 69 | + * - description: Short description shown in command picker |
| 70 | + * - agent: Which agent to use (ask, edit, agent) |
| 71 | + * - tools: List of available tools |
| 72 | + */ |
| 73 | + protected formatCommand(template: CommandTemplate): string { |
| 74 | + const frontmatter = this.generateFrontmatter(template); |
| 75 | + return `${frontmatter}\n${template.content}`; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Generate YAML frontmatter for Copilot prompt file |
| 80 | + */ |
| 81 | + private generateFrontmatter(template: CommandTemplate): string { |
| 82 | + // Extract description from template frontmatter if present |
| 83 | + const descMatch = template.content.match(/^---\s*\n[\s\S]*?description:\s*(.+)\n[\s\S]*?---/); |
| 84 | + const description = descMatch ? descMatch[1].trim() : template.description; |
| 85 | + |
| 86 | + // Map Clavix commands to appropriate Copilot agents |
| 87 | + const agentMapping: Record<string, string> = { |
| 88 | + improve: 'ask', |
| 89 | + prd: 'ask', |
| 90 | + start: 'ask', |
| 91 | + summarize: 'ask', |
| 92 | + refine: 'ask', |
| 93 | + plan: 'ask', |
| 94 | + review: 'ask', |
| 95 | + verify: 'ask', |
| 96 | + implement: 'agent', |
| 97 | + archive: 'agent', |
| 98 | + }; |
| 99 | + |
| 100 | + const agent = agentMapping[template.name] || 'ask'; |
| 101 | + |
| 102 | + // For implementation commands, include tools |
| 103 | + const toolsLine = |
| 104 | + agent === 'agent' ? '\ntools:\n - editFiles\n - runCommands\n - codebase' : ''; |
| 105 | + |
| 106 | + return `--- |
| 107 | +name: clavix-${template.name} |
| 108 | +description: "${description.replace(/"/g, '\\"')}" |
| 109 | +agent: ${agent}${toolsLine} |
| 110 | +---`; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Generate commands with Copilot-specific formatting |
| 115 | + */ |
| 116 | + async generateCommands(templates: CommandTemplate[]): Promise<void> { |
| 117 | + const commandPath = this.getCommandPath(); |
| 118 | + |
| 119 | + try { |
| 120 | + // Ensure directory exists |
| 121 | + await FileSystem.ensureDir(commandPath); |
| 122 | + |
| 123 | + // Generate each command file |
| 124 | + for (const template of templates) { |
| 125 | + const content = this.formatCommand(template); |
| 126 | + const filename = this.getTargetFilename(template.name); |
| 127 | + const filePath = path.join(commandPath, filename); |
| 128 | + await FileSystem.writeFileAtomic(filePath, content); |
| 129 | + } |
| 130 | + } catch (error) { |
| 131 | + throw new IntegrationError( |
| 132 | + `Failed to generate ${this.displayName} commands: ${error}`, |
| 133 | + `Ensure ${commandPath} is writable` |
| 134 | + ); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments