Skip to content

Commit

Permalink
Merge pull request #7 from segersniels/fix/inconsistent-request-errors
Browse files Browse the repository at this point in the history
🐛 Fix inconsistent error reporting between commit and generate functions
  • Loading branch information
segersniels authored Nov 18, 2024
2 parents 2b7efa1 + fa237a7 commit 7a1e60f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 46 deletions.
16 changes: 10 additions & 6 deletions apps/cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ var _ MessageClient = (*OpenAI)(nil)

type OpenAI struct {
apiKey string
model string
model Model
}

func NewOpenAI(apiKey string, model string) *OpenAI {
func NewOpenAI(apiKey string, model Model) *OpenAI {
return &OpenAI{
apiKey,
model,
Expand All @@ -40,7 +40,7 @@ func (o *OpenAI) CreateMessage(diff string, gitmojis []byte) (string, error) {
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: o.model,
Model: string(o.model),
Messages: []openai.ChatCompletionMessage{
{
Role: MessageRoleSystem,
Expand Down Expand Up @@ -96,19 +96,23 @@ var _ MessageClient = (*Anthropic)(nil)

type Anthropic struct {
apiKey string
model string
model Model
}

func NewAnthropic(apiKey, model string) *Anthropic {
func NewAnthropic(apiKey string, model Model) *Anthropic {
return &Anthropic{
apiKey,
model,
}
}

func (a *Anthropic) CreateMessage(diff string, gitmojis []byte) (string, error) {
if a.model == _Claude3Dot5Sonnet {
a.model = Claude3Dot5Sonnet
}

body, err := json.Marshal(map[string]interface{}{
"model": a.model,
"model": string(a.model),
"max_tokens": 4096,
"system": SYSTEM_MESSAGE + string(gitmojis),
"messages": []ClaudeMessage{
Expand Down
35 changes: 9 additions & 26 deletions apps/cli/genmoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,46 +60,29 @@ func (g *Genmoji) Generate() (string, error) {
}

var response string
err = spinner.New().TitleStyle(lipgloss.NewStyle()).Title("Generating your commit message...").Action(func() {
if err := spinner.New().TitleStyle(lipgloss.NewStyle()).Title("Generating your commit message...").Action(func() {
response, err = g.client.CreateMessage(diff, list)
}).Run()

if err != nil {
if err != nil {
log.Fatal(err)
}
}).Run(); err != nil {
return "", err
}

return response, nil
}

func (g *Genmoji) Commit() error {
diff, err := getStagedChanges()
if err != nil {
return err
}

gitmojis, err := fetchGitmojis()
if err != nil {
return err
}

list, err := json.Marshal(gitmojis)
if err != nil {
return err
}

var response string
var err error
for {
if err := spinner.New().TitleStyle(lipgloss.NewStyle()).Title("Generating your commit message...").Action(func() {
response, err = g.client.CreateMessage(diff, list)
if err != nil {
log.Fatal(err)
}
}).Run(); err != nil {
response, err = g.Generate()
if err != nil {
return err
}

var confirmation bool
err := huh.NewConfirm().Title(response).Description("Do you want to commit this message?").Value(&confirmation).Run()
err = huh.NewConfirm().Title(response).Description("Do you want to commit this message?").Value(&confirmation).Run()
if err != nil {
return err
}
Expand Down
30 changes: 17 additions & 13 deletions apps/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,28 @@ import (
"github.com/urfave/cli/v2"
)

type Model string

const (
GPT4o = "gpt-4o"
GPT4oMini = "gpt-4o-mini"
GPT4Turbo = "gpt-4-turbo"
GPT3Dot5Turbo = "gpt-3.5-turbo"
Claude3Dot5Sonnet = "claude-3-5-sonnet-20240620"
GPT4o Model = "gpt-4o"
GPT4oMini Model = "gpt-4o-mini"
GPT4Turbo Model = "gpt-4-turbo"
GPT3Dot5Turbo Model = "gpt-3.5-turbo"
Claude3Dot5Sonnet Model = "claude-3-5-sonnet-latest"
_Claude3Dot5Sonnet Model = "claude-3-5-sonnet-20240620"
)

var AppVersion string
var AppName string

type ConfigData struct {
Model string `json:"model"`
Model Model `json:"model"`
}

var CONFIG = config.NewConfig("genmoji", ConfigData{
Model: GPT4oMini,
})
var (
AppVersion string
AppName string
CONFIG = config.NewConfig("genmoji", ConfigData{
Model: GPT4oMini,
})
)

func main() {
genmoji := NewGenmoji()
Expand Down Expand Up @@ -69,7 +73,7 @@ func main() {
models := huh.NewOptions(GPT4o, GPT4oMini, GPT4Turbo, GPT3Dot5Turbo, Claude3Dot5Sonnet)
form := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().Title("Model").Description("Configure the default model").Options(models...).Value(&CONFIG.Data.Model),
huh.NewSelect[Model]().Title("Model").Description("Configure the default model").Options(models...).Value(&CONFIG.Data.Model),
),
)

Expand Down
8 changes: 7 additions & 1 deletion apps/cli/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"fmt"
"os/exec"
"strings"
"sync"
Expand Down Expand Up @@ -121,5 +122,10 @@ func getStagedChanges() (string, error) {

func commit(message string) error {
cmd := exec.Command("git", "commit", "-m", message)
return cmd.Run()
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("error committing: %s", string(output))
}

return nil
}

0 comments on commit 7a1e60f

Please sign in to comment.