-
Notifications
You must be signed in to change notification settings - Fork 3
/
handlers.go
228 lines (184 loc) · 5.21 KB
/
handlers.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
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/google/uuid"
ai "github.com/sashabaranov/go-openai"
vip "github.com/spf13/viper"
)
func sendGreeting(ctx *ChatContext) {
log.Println("sending greeting...")
ctx.Session.Message(ctx, ai.ChatMessageRoleAssistant, ctx.Personality.Greeting)
rch := ChatCompletionTask(ctx)
_ = spoolFromChannel(ctx, rch)
ctx.Session.Reset()
}
func spoolFromChannel(ctx *ChatContext, msgch <-chan *string) *string {
all := strings.Builder{}
for reply := range msgch {
all.WriteString(*reply)
sendMessage(ctx, reply)
}
s := all.String()
return &s
}
func sendMessage(ctx *ChatContext, message *string) {
log.Println("<<", ctx.Personality.Nick, *message)
ctx.Reply(*message)
}
var configParams = map[string]string{"prompt": "", "model": "", "nick": "", "greeting": "", "goodbye": "", "directory": "", "session": "", "addressed": ""}
func handleSet(ctx *ChatContext) {
if !ctx.IsAdmin() {
ctx.Reply("You don't have permission to perform this action.")
return
}
if len(ctx.Args) < 3 {
ctx.Reply(fmt.Sprintf("Usage: /set %s <value>", keysAsString(configParams)))
return
}
param, v := ctx.Args[1], ctx.Args[2:]
value := strings.Join(v, " ")
if _, ok := configParams[param]; !ok {
ctx.Reply(fmt.Sprintf("Unknown parameter. Supported parameters: %v", keysAsString(configParams)))
return
}
// set on global config
vip.Set(param, value)
ctx.Reply(fmt.Sprintf("%s set to: %s", param, vip.GetString(param)))
if param == "nick" {
ctx.Client.Cmd.Nick(value)
}
ctx.Session.Reset()
}
func handleGet(ctx *ChatContext) {
tokens := ctx.Args
if len(tokens) < 2 {
ctx.Reply(fmt.Sprintf("Usage: /get %s", keysAsString(configParams)))
return
}
param := tokens[1]
if _, ok := configParams[param]; !ok {
ctx.Reply(fmt.Sprintf("Unknown parameter. Supported parameters: %v", keysAsString(configParams)))
return
}
value := vip.GetString(param)
ctx.Reply(fmt.Sprintf("%s: %s", param, value))
}
func handleSave(ctx *ChatContext) {
tokens := ctx.Args
if !ctx.IsAdmin() {
ctx.Reply("You don't have permission to perform this action.")
return
}
if len(tokens) < 2 {
ctx.Reply("Usage: /save <name>")
return
}
filename := tokens[1]
v := vip.New()
v.Set("nick", ctx.Personality.Nick)
v.Set("prompt", ctx.Personality.Prompt)
v.Set("model", ctx.Personality.Model)
v.Set("greeting", ctx.Personality.Greeting)
v.Set("goodbye", ctx.Personality.Goodbye)
if err := v.WriteConfigAs(vip.GetString("directory") + "/" + filename + ".yml"); err != nil {
ctx.Reply(fmt.Sprintf("Error saving configuration: %s", err.Error()))
return
}
ctx.Reply(fmt.Sprintf("Configuration saved to: %s", filename))
}
func handleBecome(ctx *ChatContext) {
if !ctx.IsAdmin() {
ctx.Reply("You don't have permission to perform this action.")
return
}
tokens := ctx.Args
if len(tokens) < 2 {
ctx.Reply("Usage: /become <personality>")
return
}
personality := tokens[1]
if cfg, err := loadPersonality(personality); err != nil {
ctx.Reply(fmt.Sprintf("Error loading personality: %s", err.Error()))
return
} else {
vip.MergeConfigMap(cfg.AllSettings())
ctx.SetConfig(cfg)
}
ctx.Session.Reset()
log.Printf("changing nick to %s", ctx.Personality.Nick)
ctx.Client.Cmd.Nick(ctx.Personality.Nick)
time.Sleep(2 * time.Second)
sendGreeting(ctx)
}
func handleList(ctx *ChatContext) {
personalities := listPersonalities()
ctx.Reply(fmt.Sprintf("Available personalities: %s", strings.Join(personalities, ", ")))
}
func handleLeave(ctx *ChatContext) {
if !ctx.IsAdmin() {
ctx.Reply("You don't have permission to perform this action.")
return
}
// sendMessage(c, &e, getChatCompletionString(
// []ai.ChatCompletionMessage{
// {
// Role: ai.ChatMessageRoleAssistant,
// Content: viper.GetString("prompt") + viper.GetString("goodbye"),
// },
// },
// ))
log.Println("exiting...")
go func() {
time.Sleep(1 * time.Second)
os.Exit(0)
}()
}
func handleDefault(ctx *ChatContext) {
args := ctx.Args
msg := strings.Join(args, " ")
ctx.Session.Message(ctx, ai.ChatMessageRoleUser, msg)
rch := ChatCompletionTask(ctx)
reply := spoolFromChannel(ctx, rch)
ctx.Session.Message(ctx, ai.ChatMessageRoleAssistant, *reply)
}
func handleSay(ctx *ChatContext) {
if !ctx.IsAdmin() {
ctx.Reply("You don't have permission to perform this action.")
return
}
if len(ctx.Args) < 2 {
ctx.Reply("Usage: /say [/as <personality>] <message>")
ctx.Reply("Example: /msg chatbot /say /as marvin talk about life")
return
}
// if second token is '/as' then third token is a personality
// and we should play as that personality
as := vip.GetString("become")
if len(ctx.Args) > 2 && ctx.Args[1] == "/as" {
as = ctx.Args[2]
ctx.Args = ctx.Args[2:]
}
if cfg, err := loadPersonality(as); err != nil {
ctx.Reply(fmt.Sprintf("Error loading personality: %s", err.Error()))
return
} else {
ctx.SetConfig(cfg)
}
ctx.Session = sessions.Get(uuid.New().String())
ctx.Session.Reset()
ctx.Event.Params[0] = ctx.Config.Channel
ctx.Event.Source.Name = ctx.Personality.Nick
ctx.Args = ctx.Args[1:]
handleDefault(ctx)
}
func keysAsString(m map[string]string) string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return strings.Join(keys, ", ")
}