-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.go
142 lines (112 loc) · 2.81 KB
/
files.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
)
var (
settingsPath string
sessionPath string
)
func initializeConfig() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("Couldn't determine the user's home directory: %w", err)
}
configDir := filepath.Join(homeDir, ".config/frnly")
if err := os.MkdirAll(configDir, 0755); err != nil {
log.Fatal("Couldn't create the configuration directory: ", err)
}
settingsPath = filepath.Join(configDir, "settings.conf")
sessionPath = filepath.Join(configDir, "session.log")
defaultSettings := `# settings.conf
# OpenAI API Configuration
API_KEY=""
# GPT Model and Tuning
Temperature=0.3
Model="gpt-4-0314"
Context=8192
# Styling
UserColor="#55DD55"
BotColor="#A6E3A1"
BoldColor="#66FF66"
CodeBlock="#7700AA"
TextBlock="#94E2D5"
Comments="#BAC2DE"
References="#FFAA00"
# Interaction
Clear="!clear"
Submit="!fin"
Reset="!reset"
Permanent="!perm"
Exit="!exit"`
initialSession := Session{
Permanent: "",
Dynamic: []ChatMessage{},
}
initialSessionJSON, err := json.Marshal(initialSession)
if err != nil {
return fmt.Errorf("Couldn't marshal initial session to JSON: %w", err)
}
err = createFile(sessionPath, string(initialSessionJSON))
err = createFile(settingsPath, defaultSettings)
return err
}
func createFile(filePath string, defaultContent string) error {
_, err := os.Stat(filePath)
if err == nil {
return nil
}
if !os.IsNotExist(err) {
return fmt.Errorf("Failed to stat the file %s: %w", filePath, err)
}
if err := os.WriteFile(filePath, []byte(defaultContent), 0644); err != nil {
return fmt.Errorf("Failed to write to the file %s: %w", filePath, err)
}
return nil
}
func readSession() (Session, error) {
fileData, err := os.ReadFile(sessionPath)
if err != nil {
return Session{}, fmt.Errorf("Failed to open file: %w", err)
}
if err := json.Unmarshal(fileData, &session); err != nil {
return Session{}, fmt.Errorf("Failed to unmarshal session: %w", err)
}
return session, nil
}
func writeSession() error {
content, err := json.MarshalIndent(session, "", " ")
if err != nil {
return fmt.Errorf("Failed to marshal session: %w", err)
}
err = os.WriteFile(sessionPath, content, 0644)
if err != nil {
return fmt.Errorf("Failed to write session to file: %w", err)
}
return nil
}
func updateSession() {
var sessionSize, messagesToRemove int
for _, msg := range session.Dynamic {
sessionSize += len(msg.Content)
}
excess := sessionSize - config.Context
if excess > 0 {
for i, msg := range session.Dynamic {
excess -= len(msg.Content)
if excess <= 0 {
messagesToRemove = i + 1
break
}
}
if messagesToRemove < len(session.Dynamic) {
session.Dynamic = session.Dynamic[messagesToRemove:]
} else {
session.Dynamic = []ChatMessage{}
}
}
writeSession()
}