Skip to content

Commit 605ba22

Browse files
Jimmy KerkettaJimmy Kerketta
authored andcommitted
feat: add Claude AI provider
1 parent 0327b7d commit 605ba22

4 files changed

Lines changed: 192 additions & 93 deletions

File tree

CONTRIBUTING.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This project is participating in [Hacktoberfest](https://hacktoberfest.com)! We
1919
## Code of Conduct
2020

2121
This project adheres to the Hacktoberfest values:
22+
2223
- **Everyone is welcome** - We value diversity and inclusivity
2324
- **Quantity is fun, quality is key** - We prioritize meaningful contributions
2425
- **Short-term action, long-term impact** - Your contributions help build the future
@@ -30,6 +31,7 @@ Please be respectful and constructive in all interactions.
3031
### 🐛 Reporting Bugs
3132

3233
Before creating a bug report:
34+
3335
- Check the [existing issues](https://github.com/dfanso/commit-msg/issues) to avoid duplicates
3436
- Collect information about the bug:
3537
- OS and version
@@ -40,6 +42,7 @@ Before creating a bug report:
4042
### 💡 Suggesting Enhancements
4143

4244
Enhancement suggestions are welcome! Please:
45+
4346
- Use a clear and descriptive title
4447
- Provide a detailed description of the proposed feature
4548
- Explain why this enhancement would be useful
@@ -51,6 +54,7 @@ Look for issues labeled `good first issue` or `help wanted` - these are great fo
5154
### 📝 Documentation
5255

5356
Improving documentation is always appreciated:
57+
5458
- Fix typos or unclear instructions
5559
- Add examples
5660
- Improve README clarity
@@ -78,10 +82,12 @@ Improving documentation is always appreciated:
7882
- API key for either:
7983
- Google Gemini (`GEMINI_API_KEY`)
8084
- Grok (`GROK_API_KEY`)
85+
- Claude (`CLAUDE_API_KEY`)
8186

8287
### Environment Setup
8388

8489
1. Set up your environment variables:
90+
8591
```bash
8692
export COMMIT_LLM=gemini # or "grok"
8793
export GEMINI_API_KEY=your-api-key-here
@@ -90,11 +96,13 @@ Improving documentation is always appreciated:
9096
```
9197

9298
2. Install dependencies:
99+
93100
```bash
94101
go mod download
95102
```
96103

97104
3. Run the application:
105+
98106
```bash
99107
go run src/main.go .
100108
```
@@ -107,6 +115,7 @@ Improving documentation is always appreciated:
107115
### Testing Your Changes
108116

109117
Before submitting a PR:
118+
110119
1. Test the application in a Git repository
111120
2. Verify both LLM providers work (if applicable)
112121
3. Check for any errors or warnings
@@ -124,6 +133,7 @@ Before submitting a PR:
124133
### PR Title Format
125134

126135
Use conventional commit format:
136+
127137
- `feat:` - New feature
128138
- `fix:` - Bug fix
129139
- `docs:` - Documentation changes
@@ -151,7 +161,7 @@ func GenerateMessage(changes string, apiKey string) (string, error) {
151161
if changes == "" {
152162
return "", fmt.Errorf("no changes provided")
153163
}
154-
164+
155165
// Implementation here
156166
return message, nil
157167
}
@@ -170,6 +180,7 @@ func GenerateMessage(changes string, apiKey string) (string, error) {
170180
## Questions?
171181

172182
Feel free to:
183+
173184
- Open an issue with the `question` label
174185
- Reach out to the maintainers
175186
- Check existing issues and PRs for similar questions
@@ -181,4 +192,3 @@ By contributing, you agree that your contributions will be licensed under the sa
181192
---
182193

183194
Thank you for contributing to commit-msg! 🚀
184-

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Before running the application, ensure you have set the system environment varia
2020
This project is participating in **Hacktoberfest 2025**! We welcome contributions from developers of all skill levels.
2121

2222
Looking to contribute? Check out:
23+
2324
- [Good First Issues](https://github.com/dfanso/commit-msg/labels/good%20first%20issue)
2425
- [Help Wanted Issues](https://github.com/dfanso/commit-msg/labels/help%20wanted)
2526
- [Contributing Guidelines](CONTRIBUTING.md)
@@ -32,20 +33,20 @@ Looking to contribute? Check out:
3233
📋 **Auto-Copy to Clipboard** - Generated messages are automatically copied for instant use
3334
📊 **File Statistics Display** - Visual preview of changed files and line counts
3435
🚀 **Easy to Use** - Simple CLI interface with beautiful terminal UI
35-
**Fast** - Quick generation of commit messages
36+
**Fast** - Quick generation of commit messages
3637

3738
## Supported LLM Providers
3839

3940
You can use either **Google Gemini** or **Grok** as the LLM to generate commit messages:
4041

4142
### Environment Variables
4243

43-
| Variable | Values | Description |
44-
|----------|--------|-------------|
45-
| `COMMIT_LLM` | `gemini`, `grok`, or `chatgpt` | Choose your LLM provider |
46-
| `GEMINI_API_KEY` | Your API key | Required if using Gemini |
47-
| `GROK_API_KEY` | Your API key | Required if using Grok |
48-
| `OPENAI_API_KEY` | Your API key | Required if using ChatGPT |
44+
| Variable | Values | Description |
45+
| ---------------- | ---------------------------------------- | ------------------------- |
46+
| `COMMIT_LLM` | `gemini`, `grok`, `claude`, or `chatgpt` | Choose your LLM provider |
47+
| `GEMINI_API_KEY` | Your API key | Required if using Gemini |
48+
| `GROK_API_KEY` | Your API key | Required if using Grok |
49+
| `OPENAI_API_KEY` | Your API key | Required if using ChatGPT |
4950

5051
---
5152

@@ -58,11 +59,13 @@ You can use either **Google Gemini** or **Grok** as the LLM to generate commit m
5859
3. Add the directory to your system PATH:
5960

6061
**Windows:**
62+
6163
```cmd
6264
setx PATH "%PATH%;C:\path\to\commit-msg"
6365
```
6466

6567
**Linux/macOS:**
68+
6669
```bash
6770
export PATH=$PATH:/path/to/commit-msg
6871
echo 'export PATH=$PATH:/path/to/commit-msg' >> ~/.bashrc # or ~/.zshrc
@@ -71,12 +74,14 @@ You can use either **Google Gemini** or **Grok** as the LLM to generate commit m
7174
4. Set up environment variables:
7275

7376
**Windows:**
77+
7478
```cmd
7579
setx COMMIT_LLM "gemini"
7680
setx GEMINI_API_KEY "your-api-key-here"
7781
```
7882

7983
**Linux/macOS:**
84+
8085
```bash
8186
export COMMIT_LLM=gemini
8287
export GEMINI_API_KEY=your-api-key-here
@@ -156,16 +161,19 @@ commit .
156161
### Getting API Keys
157162

158163
**Google Gemini:**
164+
159165
1. Visit [Google AI Studio](https://makersuite.google.com/app/apikey)
160166
2. Create a new API key
161167
3. Set the `GEMINI_API_KEY` environment variable
162168

163169
**Grok (X.AI):**
170+
164171
1. Visit [X.AI Console](https://console.x.ai/)
165172
2. Generate an API key
166173
3. Set the `GROK_API_KEY` environment variable
167174

168175
**OpenAI (ChatGPT):**
176+
169177
1. Visit [OpenAI Platform](https://platform.openai.com/api-keys)
170178
2. Create a new API key
171179
3. Set the `OPENAI_API_KEY` environment variable
@@ -219,7 +227,3 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
219227
---
220228

221229
Made with ❤️ for Hacktoberfest 2025
222-
223-
224-
225-

src/claude/claude.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package claude
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"net/http"
9+
10+
"github.com/dfanso/commit-msg/src/types"
11+
)
12+
13+
type ClaudeRequest struct {
14+
Model string `json:"model"`
15+
Messages []Message `json:"messages"`
16+
MaxTokens int `json:"max_tokens"`
17+
}
18+
19+
type Message struct {
20+
Role string `json:"role"`
21+
Content string `json:"content"`
22+
}
23+
24+
type ClaudeResponse struct {
25+
ID string `json:"id"`
26+
Type string `json:"type"`
27+
Content []struct {
28+
Type string `json:"type"`
29+
Text string `json:"text"`
30+
} `json:"content"`
31+
}
32+
33+
func GenerateCommitMessage(config *types.Config, changes string, apiKey string) (string, error) {
34+
35+
prompt := fmt.Sprintf("%s\n\n%s", types.CommitPrompt, changes)
36+
37+
reqBody := ClaudeRequest{
38+
Model: "claude-3-5-sonnet-20241022",
39+
MaxTokens: 200,
40+
Messages: []Message{
41+
{
42+
Role: "user",
43+
Content: prompt,
44+
},
45+
},
46+
}
47+
48+
jsonData, err := json.Marshal(reqBody)
49+
if err != nil {
50+
return "", err
51+
}
52+
53+
ctx := context.Background()
54+
req, err := http.NewRequestWithContext(ctx, "POST", "https://api.anthropic.com/v1/messages", bytes.NewBuffer(jsonData))
55+
if err != nil {
56+
return "", err
57+
}
58+
59+
req.Header.Set("Content-Type", "application/json")
60+
req.Header.Set("x-api-key", apiKey)
61+
req.Header.Set("anthropic-version", "2023-06-01")
62+
63+
client := &http.Client{}
64+
resp, err := client.Do(req)
65+
if err != nil {
66+
return "", err
67+
}
68+
69+
var claudeResponse ClaudeResponse
70+
if err := json.NewDecoder(resp.Body).Decode(&claudeResponse); err != nil {
71+
return "", err
72+
}
73+
74+
if len(claudeResponse.Content) == 0 {
75+
return "", fmt.Errorf("no response generated")
76+
}
77+
78+
commitMsg := claudeResponse.Content[0].Text
79+
return commitMsg, nil
80+
}

0 commit comments

Comments
 (0)