-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
380 lines (319 loc) · 9.03 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/charmbracelet/bubbles/filepicker"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
cl "github.com/guitarkeegan/cover-letter/assistant"
"github.com/joho/godotenv"
"github.com/sashabaranov/go-openai"
)
const (
Setup StageMsg = "setup"
Chat StageMsg = "chat"
End StageMsg = "end"
)
var dbg = func() func(format string, as ...any) {
if os.Getenv("DEBUG") == "" {
return func(string, ...any) {}
}
file, err := os.Create("log")
if err != nil {
log.Fatal("nooooo!!!")
}
// truncate = delete the rest
return func(format string, as ...any) {
fmt.Fprintf(file, format+"\n", as...)
}
}()
type StageMsg string
// no deeper cause here
var ErrMissingValue = errors.New("missing value")
var ErrBadUnicode = errors.New("bad unicode")
// classification of error
type UserInputError struct {
// error that has caused it
err error
// name of the input/value
name string
}
func NewUserInputError(e error, name string) *UserInputError {
return &UserInputError{
err: e,
name: name,
}
}
func (e *UserInputError) Error() string {
return fmt.Sprintf("input %q: %v", e.name, e.err)
}
// cause of the error
func (e *UserInputError) Unwrap() error {
return e.err
}
// action to take if error encountered
func (e *UserInputError) UserError() string {
return fmt.Sprintf("I am struggling to handle information that you have provided. The %q input has encountered the error: %v", e.name, e.err)
}
type ToAI struct {
description string
livingDoc string
}
type WithAIMsg struct {
aiConversation []openai.ChatCompletionMessage
coverLetter string
}
type AIChatMsg openai.ChatCompletionMessage
type FileMsg string
type model struct {
aiClient *openai.Client
stage StageMsg
textarea textarea.Model
textInput textinput.Model
viewport viewport.Model
filepicker filepicker.Model
selectedFile string
toAI ToAI
withAIMsg WithAIMsg
userApproved bool
err error
}
func (m model) renderViewport() []string {
var currMessages []string
for _, cm := range m.withAIMsg.aiConversation {
switch cm.Role {
case openai.ChatMessageRoleUser:
// TODO: remove after fix
you := lipgloss.NewStyle().Width(m.viewport.Width).Render(cm.Content)
currMessages = append(currMessages, "You: "+you)
case openai.ChatMessageRoleAssistant:
assistant := lipgloss.NewStyle().Width(m.viewport.Width).Render(cm.Content)
currMessages = append(currMessages, "Assistant: "+assistant)
}
}
return currMessages
}
func sendContextToAI(c *openai.Client, userInfo ToAI, msgHistory []openai.ChatCompletionMessage) tea.Cmd {
// performs io and returns a msg
compMsg := openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleSystem,
Content: fmt.Sprintf("Here is the user's information. Based on the job that they are applying for, and the user's experience, generate the first draft of a cover letter. Make the cover letter concise, and only write about the parts of the user's experience that could be relavent to the job description. No more than 2 paragraphs. Then, ask the user if they would like to make any modifications. Job Description: %s User Experience: %s", userInfo.description, userInfo.livingDoc),
}
resp := cl.ConverseWithAI(c, compMsg, msgHistory)
file, err := os.Create("firstCover")
if err != nil {
log.Fatal("Error writing firstCover file!", err)
}
file.WriteString(resp.Content)
err = file.Close()
if err != nil {
log.Fatal("Error closing file", err)
}
return func() tea.Msg {
return AIChatMsg(resp)
}
}
func sendMessageToAI(c *openai.Client, message openai.ChatCompletionMessage, msgHistory []openai.ChatCompletionMessage) tea.Cmd {
resp := cl.ConverseWithAI(c, message, msgHistory)
return func() tea.Msg {
return AIChatMsg(resp)
}
}
func getLivingDoc(path string) tea.Cmd {
return func() tea.Msg {
file, err := os.Open(path)
if err != nil {
// panic for now
log.Fatal("unable to find the file!")
}
defer file.Close()
content, err := io.ReadAll(file)
if err != nil {
log.Fatal("unable to ReadAll")
}
text := string(content)
return FileMsg(text)
}
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
dbg("msg: %[1]T, %[1]v", msg)
var (
cmd tea.Cmd
ti tea.Cmd
vp tea.Cmd
)
m.viewport, vp = m.viewport.Update(msg)
m.textInput, ti = m.textInput.Update(msg)
switch msg := msg.(type) {
// start with default exit keys
// send a setup msg 'setup' after event loop
// in event loop, case to handle msg listening for setup method
case tea.KeyMsg:
dbg(" Handling KeyMsg")
switch msg.Type {
case tea.KeyCtrlC:
dbg(" Handling KeyCtrlC")
return m, tea.Quit
case tea.KeyEsc:
dbg(" Handling KeyEsc")
if m.textarea.Focused() && m.toAI.description == "" {
m.textarea.Blur()
m.toAI.description = strings.Trim(m.textarea.Value(), " ")
return m, nil
}
return m, tea.Quit
case tea.KeyEnter:
if m.textInput.Focused() {
dbg(" Handling KeyEnter")
chatMessage := openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: m.textInput.Value(),
}
m.withAIMsg.aiConversation = append(m.withAIMsg.aiConversation, chatMessage)
currMessages := m.renderViewport()
m.viewport.SetContent(strings.Join(currMessages, "\n\n"))
m.textInput.Reset()
m.viewport.GotoBottom()
return m, tea.Batch(vp, ti, sendMessageToAI(m.aiClient, chatMessage, m.withAIMsg.aiConversation))
}
case tea.KeyUp:
dbg(" Handling KeyUp")
m.viewport.LineUp(1)
case tea.KeyDown:
dbg(" Handling KeyDown")
m.viewport.LineDown(1)
}
case FileMsg:
dbg(" Handling FileMsg")
m.toAI.livingDoc = string(msg)
m.stage = Chat
m.textInput.Focus()
return m, tea.Batch(sendContextToAI(m.aiClient, m.toAI, m.withAIMsg.aiConversation), ti, textinput.Blink, vp)
case AIChatMsg:
dbg(" Handling AIMessage")
m.withAIMsg.aiConversation = append(m.withAIMsg.aiConversation, openai.ChatCompletionMessage(msg))
currMessages := m.renderViewport()
m.viewport.SetContent(strings.Join(currMessages, "\n\n"))
m.viewport.GotoBottom()
return m, vp
}
if m.stage == Setup {
dbg("textarea updating")
m.textarea, cmd = m.textarea.Update(msg)
if cmd != nil {
dbg(" returning non nil cmd")
return m, cmd
}
}
if m.toAI.livingDoc == "" {
dbg("Update filepicker")
m.filepicker, cmd = m.filepicker.Update(msg)
if cmd != nil {
dbg(" cmd: %v", cmd)
return m, cmd
}
if didSelect, path := m.filepicker.DidSelectFile(msg); didSelect {
dbg(" didSelect!")
m.selectedFile = path
m.stage = Chat
// TODO: read in file
return m, getLivingDoc(m.selectedFile)
}
}
dbg("End of Update")
return m, tea.Batch(ti, vp)
}
func (m model) View() string {
switch m.stage {
case Setup:
if m.toAI.description == "" {
return fmt.Sprintf(
`Let's make your cover letter!
Paste in the job description below 👇`+"\n\n%s\n\npress 'Escape' when finished", m.textarea.View())
}
if m.selectedFile == "" {
return fmt.Sprintf("\n\nSelect a file where you give your work experience.\n\n%s\n\njob description: %q\n", m.filepicker.View(), m.toAI.description)
}
case Chat:
if !m.userApproved {
return fmt.Sprintf("%s\n%v\n", m.viewport.View(), m.textInput.View())
}
case End:
return "Good luck on the application!"
}
return "something else happended..?"
}
func (m model) Init() tea.Cmd {
initStage := func() tea.Msg {
dbg("Calling Init")
return Setup
}
fpInit := m.filepicker.Init()
return tea.Batch(initStage, fpInit)
}
func initialModel() model {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
apiKey := os.Getenv("OPENAI_API")
client := openai.NewClient(apiKey)
aiSetup := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "Your job is to help the user create a taylored cover letter based on the job description, and the user's experience.",
},
}
ta := textarea.New()
ta.Placeholder = "Paste the job description here..."
ta.ShowLineNumbers = false
ta.CharLimit = 3200
ta.Focus()
ti := textinput.New()
ti.Placeholder = "message to assistant..."
ti.CharLimit = 0
ti.Width = 80
fp := filepicker.New()
vp := viewport.New(80, 10)
vp.SetContent(`Press 'Enter' to send a message to the assistant`)
return model{
aiClient: client,
stage: Setup,
textarea: ta,
textInput: ti,
viewport: vp,
filepicker: fp,
selectedFile: "",
toAI: ToAI{},
withAIMsg: WithAIMsg{
aiConversation: aiSetup,
coverLetter: "",
},
}
}
type UserError interface {
error
UserError() string
}
func main() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
// err := NewUserInputError(ErrBadUnicode, "username")
// var uiErr *UserInputError
//var uErr UserError
// inspect error
//if errors.As(err, &uErr) {
// fmt.Println("hi there", uiErr.UserError())
//}
// dev
//fmt.Println(err)
}