/
message_handler.go
243 lines (208 loc) · 8.36 KB
/
message_handler.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 gpt
import (
"log"
"time"
discord "github.com/bwmarrin/discordgo"
"github.com/raikerian/go-remai-bot-discord/pkg/bot"
"github.com/raikerian/go-remai-bot-discord/pkg/utils"
"github.com/sashabaranov/go-openai"
)
const (
gptDiscordChannelMessagesRequestMaxRetries = 4
gptDiscordTypingIndicatorCooldownSeconds = 10
gptEmojiAck = "⌛"
gptEmojiErr = "❌"
)
func chatGPTMessageHandler(ctx *bot.MessageContext, client *openai.Client, messagesCache *MessagesCache, ignoredChannelsCache *IgnoredChannelsCache) {
if !shouldHandleMessageType(ctx.Message.Type) {
// ignore message types that should not be handled by this command
return
}
if ctx.Session.State.User.ID == ctx.Message.Author.ID {
// ignore self messages
return
}
if _, exists := (*ignoredChannelsCache)[ctx.Message.ChannelID]; exists {
// skip over ignored channels list
return
}
if ctx.Message.Content == "" {
// ignore messages with empty content
return
}
ch, err := ctx.Session.State.Channel(ctx.Message.ChannelID)
if err != nil {
log.Printf("[GID: %s, CHID: %s, MID: %s] Failed to get channel info with the error: %v\n", ctx.Message.GuildID, ctx.Message.ChannelID, ctx.Message.ID, err)
return
}
if !ch.IsThread() {
// ignore non threads
(*ignoredChannelsCache)[ctx.Message.ChannelID] = struct{}{}
return
}
if ch.ThreadMetadata != nil && (ch.ThreadMetadata.Locked || ch.ThreadMetadata.Archived) {
// We don't want to handle messages in locked or archived threads
log.Printf("[GID: %s, CHID: %s, MID: %s] Ignoring new message in a potential thread as it is locked or/and archived\n", ctx.Message.GuildID, ctx.Message.ChannelID, ctx.Message.ID)
return
}
log.Printf("[GID: %s, CHID: %s, MID: %s] Handling new message in a potential GPT thread\n", ctx.Message.GuildID, ctx.Message.ChannelID, ctx.Message.ID)
cacheItem, ok := messagesCache.Get(ctx.Message.ChannelID)
if !ok {
isGPTThread := true
cacheItem = &MessagesCacheData{}
var lastID string
retries := 0
for {
if retries >= gptDiscordChannelMessagesRequestMaxRetries {
// max retries reached
break
}
// Get messages in batches of 100 (maximum allowed by Discord API)
batch, err := ctx.Session.ChannelMessages(ch.ID, 100, lastID, "", "")
if err != nil {
// Since we cannot fetch messages, that means we cannot determine whether this a GPT thread,
// and if it was, we cannot get the full context to provide a better user experience. Do retries
// and print the error in the log
log.Printf("[GID: %s, CHID: %s, MID: %s] Failed to get channel messages with the error: %v. Retries left: %d\n", ctx.Message.GuildID, ctx.Message.ChannelID, ctx.Message.ID, err, (gptDiscordChannelMessagesRequestMaxRetries - retries))
retries++
continue
}
transformed := make([]openai.ChatCompletionMessage, 0, len(batch))
for _, value := range batch {
role := openai.ChatMessageRoleUser
if value.Author.ID == ctx.Session.State.User.ID {
role = openai.ChatMessageRoleAssistant
}
content := value.Content
// First message is always a referenced message
// Check if it is, and then modify to get the original prompt
if value.Type == discord.MessageTypeThreadStarterMessage {
if value.Author.ID != ctx.Session.State.User.ID || value.ReferencedMessage == nil {
// this is not gpt thread, ignore
isGPTThread = false
break
}
role = openai.ChatMessageRoleUser
prompt, context, model, temperature := parseInteractionReply(value.ReferencedMessage)
if prompt == "" {
isGPTThread = false
break
}
content = prompt
var systemMessage *openai.ChatCompletionMessage
if context != "" {
context, _ = getContentOrURLData(ctx.Client, context)
systemMessage = &openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleSystem,
Content: context,
}
}
if model == "" {
model = gptDefaultModel
}
if temperature != nil {
cacheItem.Temperature = temperature
}
cacheItem.SystemMessage = systemMessage
cacheItem.Model = model
} else if !shouldHandleMessageType(value.Type) {
// ignore message types that are
// not related to conversation
continue
}
transformed = append(transformed, openai.ChatCompletionMessage{
Role: role,
Content: content,
})
}
reverseMessages(&transformed)
// Add the messages to the beginning of the main list
cacheItem.Messages = append(transformed, cacheItem.Messages...)
// If there are no more messages in the thread, break the loop
if len(batch) == 0 {
break
}
// Set the lastID to the last message's ID to get the next batch of messages
lastID = batch[len(batch)-1].ID
}
if retries >= gptDiscordChannelMessagesRequestMaxRetries {
// max retries reached on fetching messages
log.Printf("[GID: %s, CHID: %s, MID: %s] Failed to get channel messages. Reached max retries\n", ctx.Message.GuildID, ctx.Message.ChannelID, ctx.Message.ID)
return
}
if !isGPTThread {
// this was not a GPT thread
log.Printf("[GID: %s, CHID: %s, MID: %s] Not a GPT thread, saving to ignored cache to skip over it later\n", ctx.Message.GuildID, ctx.Message.ChannelID, ctx.Message.ID)
// save threadID to ignored cache, so we can always ignore it later
(*ignoredChannelsCache)[ctx.Message.ChannelID] = struct{}{}
return
}
messagesCache.Add(ctx.Message.ChannelID, cacheItem)
} else {
cacheItem.Messages = append(cacheItem.Messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: ctx.Message.Content,
})
}
// check if current message cache is within allowed token limit
if ok, count := isCacheItemWithinTruncateLimit(cacheItem); !ok {
log.Printf("[GID: %s, CHID: %s, MID: %s] Current thread cache token count of %d exceeds truncate limit. Performing adjustments.\n", ctx.Message.GuildID, ctx.Message.ChannelID, ctx.Message.ID, count)
adjustMessageTokens(cacheItem)
log.Printf("[GID: %s, CHID: %s, MID: %s] Tokens adjustments finished. Current cache tokens: %d\n", ctx.Message.GuildID, ctx.Message.ChannelID, ctx.Message.ID, cacheItem.TokenCount)
}
// Lock the thread while we are generating ChatGPT answser
utils.ToggleDiscordThreadLock(ctx.Session, ctx.Message.ChannelID, true)
// Unlock the thread at the end
defer utils.ToggleDiscordThreadLock(ctx.Session, ctx.Message.ChannelID, false)
ctx.AddReaction(gptEmojiAck)
defer ctx.RemoveReaction(gptEmojiAck)
// Create a ticker and a channel for signaling request completion
// Discord stops showing typing indicator after 10 seconds, so we
// need to send it again
ctx.ChannelTyping()
typingTicker := time.NewTicker(gptDiscordTypingIndicatorCooldownSeconds * time.Second)
done := make(chan bool)
go func() {
for {
select {
case <-typingTicker.C:
ctx.ChannelTyping()
case <-done:
typingTicker.Stop()
return
}
}
}()
log.Printf("[GID: %s, CHID: %s] ChatGPT Request invoked with [Model: %s]. Current cache size: %v\n", ctx.Message.GuildID, ctx.Message.ChannelID, cacheItem.Model, len(cacheItem.Messages))
resp, err := sendChatGPTRequest(client, cacheItem)
// Signal the typing ticker to stop
done <- true
if err != nil {
// ChatGPT failed for whatever reason, tell users about it
log.Printf("[GID: %s, CHID: %s] ChatGPT request ChatCompletion failed with the error: %v\n", ctx.Message.GuildID, ctx.Message.ChannelID, err)
ctx.AddReaction(gptEmojiErr)
ctx.EmbedReply(&discord.MessageEmbed{
Title: "❌ OpenAI API failed",
Description: err.Error(),
Color: 0xff0000,
})
return
}
log.Printf("[GID: %s, CHID: %s] ChatGPT Request [Model: %s] responded with a usage: [PromptTokens: %d, CompletionTokens: %d, TotalTokens: %d]\n", ctx.Message.GuildID, ctx.Message.ChannelID, cacheItem.Model, resp.usage.PromptTokens, resp.usage.CompletionTokens, resp.usage.TotalTokens)
messages := splitMessage(resp.content)
var replyMessage *discord.Message
for _, message := range messages {
replyMessage, err = ctx.Reply(message)
if err != nil {
log.Printf("[GID: %s, CHID: %s, MID: %s] Failed to reply in the thread with the error: %v\n", ctx.Message.GuildID, ctx.Message.ChannelID, ctx.Message.ID, err)
ctx.AddReaction(gptEmojiErr)
ctx.EmbedReply(&discord.MessageEmbed{
Title: "❌ Discord API Error",
Description: err.Error(),
Color: 0xff0000,
})
return
}
}
attachUsageInfo(ctx.Session, replyMessage, resp.usage, cacheItem.Model)
}