-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
63 lines (50 loc) · 1.43 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
package main
import (
"os"
"time"
"github.com/jjkirkpatrick/clara/assistant"
"github.com/jjkirkpatrick/clara/chatui"
"github.com/jjkirkpatrick/clara/config"
openai "github.com/sashabaranov/go-openai"
)
var cfg = config.New()
var openaiClient *openai.Client
func main() {
if cfg.OpenAiAPIKey() == "" {
key := os.Getenv("OPENAI_API_KEY")
if key != "" {
cfg = cfg.SetOpenAiAPIKey(key)
} else {
key := assistant.GetUserMessage("Please enter your OpenAI API key: ")
cfg = cfg.SetOpenAiAPIKey(key)
}
}
cfg.AppLogger.Info("Clara is starting up... Please wait a moment.")
openaiClient = openai.NewClient(cfg.OpenAiAPIKey())
chat, err := chatui.NewChatUI()
clara := assistant.Start(cfg, openaiClient, chat)
if err != nil {
cfg.AppLogger.Fatalf("Error initializing chat UI: %v", err)
}
go func() {
if err := chat.Run(); err != nil {
cfg.AppLogger.Fatalf("Error running chat UI: %v", err)
}
}()
userMessagesChan := chat.GetUserMessagesChannel()
for {
select {
case userMessage, ok := <-userMessagesChan: // userMessage is a string containing the user's message.
if !ok {
// If the channel is closed, exit the loop.
cfg.AppLogger.Info("User message channel closed. Exiting.")
return
}
clara.Message(userMessage)
case <-time.After(10 * time.Minute):
// Timeout: if there's no activity for 5 minutes, exit.
cfg.AppLogger.Info("No activity for 10 minutes. Exiting.")
return
}
}
}