|
| 1 | +"""AI client and GitHub API utilities.""" |
| 2 | +import os |
| 3 | +import requests |
| 4 | +from anthropic import Anthropic |
| 5 | + |
| 6 | +# Hardcoded agent context for portability (template-friendly) |
| 7 | +AGENT_CONTEXT = """ |
| 8 | +**Your Role**: |
| 9 | +You are the Codebase Agent for this repository. You assist with code reviews, |
| 10 | +technical guidance, and maintaining code quality standards. |
| 11 | +
|
| 12 | +**Operating Principles**: |
| 13 | +
|
| 14 | +1. **Safety First** |
| 15 | + - Show plan before major changes |
| 16 | + - Explain reasoning and alternatives |
| 17 | + - Ask for clarification when requirements are ambiguous |
| 18 | +
|
| 19 | +2. **High Signal, Low Noise** |
| 20 | + - Only comment when adding unique value |
| 21 | + - Be concise and get to the point |
| 22 | + - Focus on critical issues, not minor style differences |
| 23 | +
|
| 24 | +**Code Review Focus**: |
| 25 | +When reviewing code, prioritize: |
| 26 | +- **Bugs**: Logic errors, edge cases, error handling |
| 27 | +- **Security**: Input validation, OWASP Top 10 vulnerabilities |
| 28 | +- **Performance**: Inefficient algorithms, unnecessary operations |
| 29 | +- **Style**: Code quality and maintainability |
| 30 | +- **Testing**: Coverage, missing test cases |
| 31 | +
|
| 32 | +**Feedback Guidelines**: |
| 33 | +- Be specific and actionable |
| 34 | +- Provide code examples for fixes |
| 35 | +- Explain "why" not just "what" |
| 36 | +- Prioritize critical issues |
| 37 | +- Acknowledge good practices |
| 38 | +
|
| 39 | +**Communication Style**: |
| 40 | +- Direct and technical (assume user has context) |
| 41 | +- Code-focused (show examples, not just descriptions) |
| 42 | +- Actionable (always provide next steps) |
| 43 | +- Honest (admit uncertainty, ask for clarification) |
| 44 | +
|
| 45 | +**What NOT to Do**: |
| 46 | +- No generic AI responses or "AI slop" |
| 47 | +- Don't state the obvious or add filler content |
| 48 | +- Don't make assumptions about ambiguous requirements |
| 49 | +- Don't include unnecessary praise or validation |
| 50 | +""" |
| 51 | + |
| 52 | + |
| 53 | +def call_claude(repo_name: str, command: str, url: str) -> str: |
| 54 | + """Call Claude API with context. |
| 55 | +
|
| 56 | + Args: |
| 57 | + repo_name: Repository name (owner/repo) |
| 58 | + command: User command to execute |
| 59 | + url: GitHub issue/PR URL |
| 60 | +
|
| 61 | + Returns: |
| 62 | + AI response text |
| 63 | +
|
| 64 | + Raises: |
| 65 | + RuntimeError: If AI API call fails |
| 66 | + """ |
| 67 | + api_key = os.environ.get("ANTHROPIC_API_KEY") |
| 68 | + if not api_key: |
| 69 | + raise RuntimeError("ANTHROPIC_API_KEY environment variable not set") |
| 70 | + |
| 71 | + client = Anthropic(api_key=api_key) |
| 72 | + |
| 73 | + prompt = f"""You are the Codebase Agent for {repo_name}. |
| 74 | +
|
| 75 | +{AGENT_CONTEXT} |
| 76 | +
|
| 77 | +--- |
| 78 | +
|
| 79 | +**Current Task**: |
| 80 | +Command: {command} |
| 81 | +Context: {url} |
| 82 | +
|
| 83 | +Provide a helpful, concise response following the operating principles above.""" |
| 84 | + |
| 85 | + try: |
| 86 | + message = client.messages.create( |
| 87 | + model="claude-sonnet-4-5-20250929", |
| 88 | + max_tokens=2000, |
| 89 | + messages=[{"role": "user", "content": prompt}], |
| 90 | + ) |
| 91 | + return message.content[0].text |
| 92 | + except Exception as e: |
| 93 | + raise RuntimeError(f"AI API error: {e}") |
| 94 | + |
| 95 | + |
| 96 | +def post_github_comment(repo: str, issue_number: int, body: str): |
| 97 | + """Post comment to GitHub issue/PR. |
| 98 | +
|
| 99 | + Args: |
| 100 | + repo: Repository name (owner/repo) |
| 101 | + issue_number: Issue or PR number |
| 102 | + body: Comment body text |
| 103 | +
|
| 104 | + Raises: |
| 105 | + requests.HTTPError: If GitHub API call fails |
| 106 | + """ |
| 107 | + token = os.environ.get("GITHUB_TOKEN") |
| 108 | + if not token: |
| 109 | + raise RuntimeError("GITHUB_TOKEN environment variable not set") |
| 110 | + |
| 111 | + url = f"https://api.github.com/repos/{repo}/issues/{issue_number}/comments" |
| 112 | + |
| 113 | + try: |
| 114 | + response = requests.post( |
| 115 | + url, |
| 116 | + headers={ |
| 117 | + "Authorization": f"token {token}", |
| 118 | + "Accept": "application/vnd.github.v3+json", |
| 119 | + }, |
| 120 | + json={"body": body}, |
| 121 | + timeout=30, |
| 122 | + ) |
| 123 | + response.raise_for_status() |
| 124 | + except requests.exceptions.RequestException as e: |
| 125 | + raise RuntimeError(f"GitHub API error: {e}") |
0 commit comments