|
| 1 | +# Gemini Agent - Code Analysis Tool |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The Gemini Agent is a Node.js-based code analysis tool that uses Google's Gemini AI to analyze code for vulnerabilities, deviations from best practices, and mathematical instability. It's particularly designed for protocol engineering, ZK-Circuits, and DAO stability analysis. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Protocol Stability Score**: Assigns a 1-10 score for code stability |
| 10 | +- **Vulnerability Detection**: Identifies potential security flaws |
| 11 | +- **Code Repair Suggestions**: Provides repaired code blocks when issues are found |
| 12 | +- **Best Practices Analysis**: Checks for deviations from industry standards |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +1. Node.js installed (version 18 or higher for native fetch support) |
| 17 | +2. Google Gemini API Key (obtain from [Google AI Studio](https://aistudio.google.com/)) |
| 18 | +3. Dependencies installed (run `yarn install` or `npm install`) |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +```bash |
| 23 | +# Install dependencies |
| 24 | +yarn install |
| 25 | + |
| 26 | +# Or using npm |
| 27 | +npm install |
| 28 | +``` |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +### Command Line |
| 33 | + |
| 34 | +```bash |
| 35 | +node scripts/analysis/gemini-agent.js <filepath> --api-key=<your_api_key> |
| 36 | +``` |
| 37 | + |
| 38 | +### Using npm/yarn script |
| 39 | + |
| 40 | +```bash |
| 41 | +yarn analyze:gemini <filepath> --api-key=<your_api_key> |
| 42 | + |
| 43 | +# Or |
| 44 | +npm run analyze:gemini <filepath> --api-key=<your_api_key> |
| 45 | +``` |
| 46 | + |
| 47 | +### Example |
| 48 | + |
| 49 | +```bash |
| 50 | +# Analyze a JavaScript file |
| 51 | +node scripts/analysis/gemini-agent.js ./src/example.js --api-key=YOUR_GEMINI_API_KEY |
| 52 | + |
| 53 | +# Analyze a Go file |
| 54 | +node scripts/analysis/gemini-agent.js ./pkg/example.go --api-key=YOUR_GEMINI_API_KEY |
| 55 | +``` |
| 56 | + |
| 57 | +## Environment Variables |
| 58 | + |
| 59 | +For security, it's recommended to use environment variables instead of passing the API key directly: |
| 60 | + |
| 61 | +```bash |
| 62 | +export GEMINI_API_KEY=your_api_key_here |
| 63 | +node scripts/analysis/gemini-agent.js <filepath> --api-key=$GEMINI_API_KEY |
| 64 | +``` |
| 65 | + |
| 66 | +## GitHub Actions Integration |
| 67 | + |
| 68 | +To integrate with GitHub Actions, add the following to your workflow: |
| 69 | + |
| 70 | +```yaml |
| 71 | +- name: Analyze Code with Gemini Agent |
| 72 | + env: |
| 73 | + GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }} |
| 74 | + run: | |
| 75 | + node scripts/analysis/gemini-agent.js ${{ matrix.file }} --api-key=$GEMINI_API_KEY |
| 76 | +``` |
| 77 | +
|
| 78 | +Make sure to add `GEMINI_API_KEY` to your GitHub repository secrets. |
| 79 | + |
| 80 | +## Output |
| 81 | + |
| 82 | +The tool provides: |
| 83 | + |
| 84 | +1. **Protocol Stability Score**: A numerical rating (1-10) |
| 85 | +2. **Potential Flaws Summary**: Brief description of issues found |
| 86 | +3. **Repair Status**: Either: |
| 87 | + - `REPAIRED CODE BLOCK`: Contains the fixed code |
| 88 | + - `NO REPAIR NEEDED`: Code is in good condition |
| 89 | + |
| 90 | +## Error Handling |
| 91 | + |
| 92 | +The script handles common errors: |
| 93 | +- Missing API key |
| 94 | +- Invalid file paths |
| 95 | +- API connection issues |
| 96 | +- Malformed API responses |
| 97 | + |
| 98 | +## Security Notes |
| 99 | + |
| 100 | +⚠️ **Important Security Considerations:** |
| 101 | + |
| 102 | +1. Never commit API keys to version control |
| 103 | +2. Use environment variables or GitHub Secrets for API keys |
| 104 | +3. Add API keys to `.gitignore` if storing them in config files |
| 105 | +4. Rotate API keys regularly |
| 106 | +5. Use least-privilege API key permissions |
| 107 | + |
| 108 | +## Limitations |
| 109 | + |
| 110 | +- Requires active internet connection |
| 111 | +- Depends on Gemini API availability |
| 112 | +- API rate limits may apply (check Google's documentation) |
| 113 | +- Analysis quality depends on code context and complexity |
| 114 | + |
| 115 | +## Troubleshooting |
| 116 | + |
| 117 | +### "GEMINI_API_KEY is missing" |
| 118 | +- Ensure you're passing the `--api-key` parameter |
| 119 | +- Check that the API key is correctly formatted |
| 120 | + |
| 121 | +### "HTTP error! status: 403" |
| 122 | +- Verify your API key is valid |
| 123 | +- Check API key permissions |
| 124 | +- Ensure billing is enabled in Google Cloud Console |
| 125 | + |
| 126 | +### "Error reading file" |
| 127 | +- Verify the file path is correct |
| 128 | +- Ensure the file exists and is readable |
| 129 | +- Use absolute paths if relative paths fail |
| 130 | + |
| 131 | +## Contributing |
| 132 | + |
| 133 | +To improve the Gemini Agent: |
| 134 | + |
| 135 | +1. Follow the existing code style |
| 136 | +2. Test changes with multiple file types |
| 137 | +3. Update documentation for new features |
| 138 | +4. Consider adding unit tests |
| 139 | + |
| 140 | +## License |
| 141 | + |
| 142 | +This tool is part of the Grafana Pyroscope project and follows the same AGPL-3.0-only license. |
| 143 | + |
| 144 | +## Support |
| 145 | + |
| 146 | +For issues or questions: |
| 147 | +- Open an issue in the repository |
| 148 | +- Check existing documentation |
| 149 | +- Review Google Gemini API documentation |
0 commit comments