|
| 1 | +import Anthropic from "@anthropic-ai/sdk"; |
| 2 | +import { readFileSync, writeFileSync, existsSync } from "fs"; |
| 3 | +import { resolve, dirname } from "path"; |
| 4 | +import { fileURLToPath } from "url"; |
| 5 | + |
| 6 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 7 | + |
| 8 | +/** |
| 9 | + * Generate Implementation - Use Claude to analyze codebase and generate implementation |
| 10 | + * |
| 11 | + * This script uses Claude to: |
| 12 | + * 1. Analyze the existing codebase |
| 13 | + * 2. Generate an implementation plan |
| 14 | + * 3. Create code changes for the feature request |
| 15 | + */ |
| 16 | + |
| 17 | +const enhancedTask = process.env.ENHANCED_TASK || process.env.TASK || ""; |
| 18 | +const issueNumber = process.env.ISSUE_NUMBER || "unknown"; |
| 19 | +const anthropicApiKey = process.env.ANTHROPIC_API_KEY; |
| 20 | +const model = process.env.CLAUDE_MODEL || "claude-sonnet-4-5-20250929"; |
| 21 | + |
| 22 | +if (!enhancedTask) { |
| 23 | + console.error("[IMPLEMENTATION] ENHANCED_TASK or TASK environment variable not set"); |
| 24 | + process.exit(1); |
| 25 | +} |
| 26 | + |
| 27 | +if (!anthropicApiKey) { |
| 28 | + console.error("[IMPLEMENTATION] ANTHROPIC_API_KEY not set"); |
| 29 | + process.exit(1); |
| 30 | +} |
| 31 | + |
| 32 | +const anthropic = new Anthropic({ apiKey: anthropicApiKey }); |
| 33 | + |
| 34 | +/** |
| 35 | + * Read relevant project files to give Claude context |
| 36 | + */ |
| 37 | +function getProjectContext() { |
| 38 | + const repoRoot = resolve(__dirname, "../.."); |
| 39 | + const files = []; |
| 40 | + |
| 41 | + // Read package.json for dependencies |
| 42 | + try { |
| 43 | + const packageJson = readFileSync(resolve(repoRoot, "package.json"), "utf8"); |
| 44 | + files.push({ |
| 45 | + path: "package.json", |
| 46 | + content: packageJson |
| 47 | + }); |
| 48 | + } catch (e) { |
| 49 | + console.warn("[IMPLEMENTATION] Could not read package.json"); |
| 50 | + } |
| 51 | + |
| 52 | + // Read main index file |
| 53 | + try { |
| 54 | + const indexJs = readFileSync(resolve(repoRoot, "index.js"), "utf8"); |
| 55 | + files.push({ |
| 56 | + path: "index.js", |
| 57 | + content: indexJs.substring(0, 5000) // First 5000 chars to avoid token limits |
| 58 | + }); |
| 59 | + } catch (e) { |
| 60 | + console.warn("[IMPLEMENTATION] Could not read index.js"); |
| 61 | + } |
| 62 | + |
| 63 | + // Read lib directory structure |
| 64 | + try { |
| 65 | + const libFiles = [ |
| 66 | + "lib/slack.js", |
| 67 | + "lib/discord.js", |
| 68 | + "lib/voting.js", |
| 69 | + "lib/command-handlers.js", |
| 70 | + "lib/ai-handler.js", |
| 71 | + "lib/spotify.js" |
| 72 | + ]; |
| 73 | + |
| 74 | + for (const libFile of libFiles) { |
| 75 | + const fullPath = resolve(repoRoot, libFile); |
| 76 | + if (existsSync(fullPath)) { |
| 77 | + const content = readFileSync(fullPath, "utf8"); |
| 78 | + files.push({ |
| 79 | + path: libFile, |
| 80 | + content: content.substring(0, 3000) // First 3000 chars per file |
| 81 | + }); |
| 82 | + } |
| 83 | + } |
| 84 | + } catch (e) { |
| 85 | + console.warn("[IMPLEMENTATION] Could not read lib files"); |
| 86 | + } |
| 87 | + |
| 88 | + return files; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Generate implementation using Claude |
| 93 | + */ |
| 94 | +async function generateImplementation() { |
| 95 | + try { |
| 96 | + console.log(`[IMPLEMENTATION] Generating implementation with ${model}...`); |
| 97 | + console.log(`[IMPLEMENTATION] Feature request: ${enhancedTask}`); |
| 98 | + |
| 99 | + const projectFiles = getProjectContext(); |
| 100 | + |
| 101 | + // Build context from project files |
| 102 | + let contextText = "Here are relevant files from the project:\n\n"; |
| 103 | + for (const file of projectFiles) { |
| 104 | + contextText += `--- ${file.path} ---\n${file.content}\n\n`; |
| 105 | + } |
| 106 | + |
| 107 | + const systemPrompt = `You are an expert software developer working on a Slack/Discord bot for controlling Sonos speakers. |
| 108 | +
|
| 109 | +The project is called SlackONOS and is a democratic bot where users can vote on songs to play. |
| 110 | +
|
| 111 | +Your task is to analyze the feature request and generate a concrete implementation plan with code suggestions. |
| 112 | +
|
| 113 | +Project context: |
| 114 | +- Node.js application |
| 115 | +- Uses Slack Socket Mode / Events API (via @slack/socket-mode) and Discord.js |
| 116 | +- Controls Sonos speakers |
| 117 | +- Has voting system for democratic music control |
| 118 | +- Uses AI for natural language commands |
| 119 | +- Supports Spotify integration |
| 120 | +
|
| 121 | +Output format: |
| 122 | +1. **Implementation Plan** - Brief overview of what needs to be changed |
| 123 | +2. **Files to Modify/Create** - List specific files |
| 124 | +3. **Code Changes** - Provide actual code snippets or full file contents |
| 125 | +
|
| 126 | +Be specific and actionable. Focus on the actual code changes needed.`; |
| 127 | + |
| 128 | + const userPrompt = `Feature Request to Implement: |
| 129 | +
|
| 130 | +${enhancedTask} |
| 131 | +
|
| 132 | +${contextText} |
| 133 | +
|
| 134 | +Please provide: |
| 135 | +1. A brief implementation plan |
| 136 | +2. List of files to modify or create |
| 137 | +3. Actual code changes with clear instructions |
| 138 | +
|
| 139 | +Make it actionable and ready to commit.`; |
| 140 | + |
| 141 | + const response = await anthropic.messages.create({ |
| 142 | + model: model, |
| 143 | + max_tokens: 4096, |
| 144 | + messages: [ |
| 145 | + { |
| 146 | + role: "user", |
| 147 | + content: [ |
| 148 | + { |
| 149 | + type: "text", |
| 150 | + text: `${systemPrompt}\n\n${userPrompt}` |
| 151 | + } |
| 152 | + ] |
| 153 | + } |
| 154 | + ] |
| 155 | + }); |
| 156 | + |
| 157 | + const implementation = response.content[0].text; |
| 158 | + |
| 159 | + console.log(`[IMPLEMENTATION] Generated implementation plan:`); |
| 160 | + console.log(implementation); |
| 161 | + |
| 162 | + // Save implementation to file for the workflow to use |
| 163 | + const outputPath = resolve(__dirname, `../../implementation-${issueNumber}.md`); |
| 164 | + writeFileSync(outputPath, implementation, "utf8"); |
| 165 | + console.log(`[IMPLEMENTATION] Saved to ${outputPath}`); |
| 166 | + |
| 167 | + // Output markers for workflow parsing |
| 168 | + console.log(`\nIMPLEMENTATION_FILE:${outputPath}`); |
| 169 | + console.log(`IMPLEMENTATION_START`); |
| 170 | + console.log(implementation); |
| 171 | + console.log(`IMPLEMENTATION_END`); |
| 172 | + |
| 173 | + return implementation; |
| 174 | + |
| 175 | + } catch (error) { |
| 176 | + console.error(`[IMPLEMENTATION] Error generating implementation: ${error.message}`); |
| 177 | + if (error.response) { |
| 178 | + console.error(`[IMPLEMENTATION] API Error: ${JSON.stringify(error.response.data)}`); |
| 179 | + } |
| 180 | + process.exit(1); |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +generateImplementation(); |
0 commit comments