-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
243 lines (214 loc) · 5.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/log"
"github.com/sashabaranov/go-openai"
"github.com/segersniels/config"
updater "github.com/segersniels/updater"
"github.com/urfave/cli/v2"
)
var (
AppVersion string
AppName string
)
const (
GPT4o = "gpt-4o"
GPT4oMini = "gpt-4o-mini"
GPT4Turbo = "gpt-4-turbo"
GPT3Dot5Turbo = "gpt-3.5-turbo"
Claude3Dot5Sonnet = "claude-3-5-sonnet-latest"
)
type MessageRole string
const (
MessageRoleSystem MessageRole = "system"
MessageRoleUser MessageRole = "user"
MessageRoleAssistant MessageRole = "assistant"
)
type Message struct {
Role MessageRole `json:"role"`
Content string `json:"content"`
}
type MessageClient interface {
CreateMessage(ctx context.Context, system string, messages []Message) (string, error)
}
type Config struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
Template string `json:"template"`
PrettyPrint bool `json:"pretty_print"`
}
var CONFIG = config.NewConfig("propr", Config{
Model: openai.GPT4o,
Prompt: SYSTEM_MESSAGE,
Template: "# Description",
PrettyPrint: true,
})
func printMarkdown(content string, pretty bool) error {
// Remove leading and trailing backticks
if strings.HasPrefix(content, "```") {
content = strings.Trim(content, "`")
}
if !pretty {
println(content)
return nil
}
r, _ := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
)
out, err := r.Render(content)
if err != nil {
return err
}
println(out)
return nil
}
func main() {
upd := updater.NewUpdater(AppName, AppVersion, "segersniels")
err := upd.CheckIfNewVersionIsAvailable()
if err != nil {
log.Debug("Failed to check for latest release", "error", err)
}
debug := os.Getenv("DEBUG")
if debug != "" {
log.SetLevel(log.DebugLevel)
}
propr := NewPropr()
app := &cli.App{
Name: AppName,
Usage: "Generate your PRs from the command line with AI",
Version: AppVersion,
Commands: []*cli.Command{
{
Name: "update",
Usage: "Update to the latest version",
Action: func(ctx *cli.Context) error {
return upd.Update()
},
},
{
Name: "create",
Usage: "Creates a PR with a generated description",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "branch",
Usage: "The branch to compare your changes against",
DefaultText: "HEAD",
},
&cli.BoolFlag{
Name: "empty",
Usage: "Create an empty PR",
},
&cli.BoolFlag{
Name: "draft",
Usage: "Create a draft PR",
},
},
Action: func(ctx *cli.Context) error {
draft := ctx.Bool("draft")
branch := ctx.String("branch")
if ctx.Bool("empty") {
return propr.Create(branch, "", draft)
}
var description string
for {
response, err := propr.Generate(branch)
if err != nil {
log.Fatal(err)
}
err = printMarkdown(response, CONFIG.Data.PrettyPrint)
if err != nil {
log.Fatal(err)
}
var confirmation bool
err = huh.NewConfirm().Title("Do you want to create this pull request?").Value(&confirmation).Run()
if err != nil {
return nil
}
if confirmation {
description = response
break
}
}
return propr.Create(branch, description, draft)
},
},
{
Name: "generate",
Usage: "Generates a PR description and outputs it",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "plain",
Usage: "Output the generated description without any formatting",
},
&cli.StringFlag{
Name: "branch",
Usage: "The branch to compare your changes against",
DefaultText: "HEAD",
},
},
Action: func(ctx *cli.Context) error {
description, err := propr.Generate(ctx.String("branch"))
if err != nil {
log.Fatal(err)
}
pretty := CONFIG.Data.PrettyPrint
if ctx.Bool("plain") {
pretty = false
}
return printMarkdown(description, pretty)
},
},
{
Name: "config",
Usage: "Configure propr to your liking",
Subcommands: []*cli.Command{
{
Name: "ls",
Usage: "List the current configuration",
Action: func(ctx *cli.Context) error {
data, err := json.MarshalIndent(CONFIG.Data, "", " ")
if err != nil {
return err
}
fmt.Println(string(data))
return nil
},
},
{
Name: "init",
Usage: "Initializes propr with a base configuration",
Action: func(ctx *cli.Context) error {
models := huh.NewOptions(GPT4oMini, GPT4o, 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.NewText().Title("Prompt").Description("Configure the default prompt").CharLimit(99999).Value(&CONFIG.Data.Prompt),
),
huh.NewGroup(
huh.NewText().Title("Template").Description("Configure the default template").Value(&CONFIG.Data.Template),
),
huh.NewGroup(
huh.NewConfirm().Title("Pretty Print").Description("Do you want to pretty print the generated output?").Value(&CONFIG.Data.PrettyPrint),
),
)
err := form.Run()
if err != nil {
return err
}
return CONFIG.Save()
},
},
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}