bot name in log

This commit is contained in:
Erik Winter 2023-06-14 15:45:44 +02:00
parent ee638a6fc8
commit 988ca34644
1 changed files with 11 additions and 23 deletions

View File

@ -25,18 +25,6 @@ func BotNameAppend(name string) {
botNames = append(botNames, name) botNames = append(botNames, name)
} }
func BotNameRegistered(want string) bool {
mu.Lock()
defer mu.Unlock()
for _, got := range botNames {
if strings.ToLower(want) == strings.ToLower(got) {
return true
}
}
return false
}
type ConfigOpenAI struct { type ConfigOpenAI struct {
APIKey string APIKey string
} }
@ -140,11 +128,11 @@ func (m *Bot) InviteHandler() (event.Type, mautrix.EventHandler) {
if evt.GetStateKey() == m.client.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite { if evt.GetStateKey() == m.client.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
_, err := m.client.JoinRoomByID(evt.RoomID) _, err := m.client.JoinRoomByID(evt.RoomID)
if err != nil { if err != nil {
m.logger.Error("failed to join room after invite", slog.String("err", err.Error()), slog.String("room_id", evt.RoomID.String()), slog.String("inviter", evt.Sender.String())) m.logger.Error("failed to join room after invite", slog.String("err", err.Error()), slog.String("room_id", evt.RoomID.String()), slog.String("inviter", evt.Sender.String()), slog.String("bot", m.config.UserDisplayName))
return return
} }
m.logger.Info("joined room after invite", slog.String("room_id", evt.RoomID.String()), slog.String("inviter", evt.Sender.String())) m.logger.Info("joined room after invite", slog.String("room_id", evt.RoomID.String()), slog.String("inviter", evt.Sender.String()), slog.String("bot", m.config.UserDisplayName))
} }
} }
} }
@ -157,13 +145,13 @@ func (m *Bot) ResponseHandler() (event.Type, mautrix.EventHandler) {
// ignore if the message is already recorded // ignore if the message is already recorded
if conv := m.conversations.FindByEventID(eventID); conv != nil { if conv := m.conversations.FindByEventID(eventID); conv != nil {
m.logger.Info("known message, ignoring", slog.String("event_id", eventID.String())) m.logger.Info("known message, ignoring", slog.String("event_id", eventID.String()), slog.String("bot", m.config.UserDisplayName))
return return
} }
// ignore if the message is sent by the bot itself // ignore if the message is sent by the bot itself
if evt.Sender == id.UserID(m.config.UserID) { if evt.Sender == id.UserID(m.config.UserID) {
m.logger.Info("message sent by bot itself, ignoring", slog.String("event_id", eventID.String())) m.logger.Info("message sent by bot itself, ignoring", slog.String("event_id", eventID.String()), slog.String("bot", m.config.UserDisplayName))
return return
} }
@ -176,7 +164,7 @@ func (m *Bot) ResponseHandler() (event.Type, mautrix.EventHandler) {
hasParent = true hasParent = true
m.logger.Info("message is a reply", slog.String("parent_id", parentID.String())) m.logger.Info("message is a reply", slog.String("parent_id", parentID.String()))
if c := m.conversations.FindByEventID(parentID); c != nil { if c := m.conversations.FindByEventID(parentID); c != nil {
m.logger.Info("found parent, appending message to conversation", slog.String("event_id", eventID.String())) m.logger.Info("found parent, appending message to conversation", slog.String("event_id", eventID.String()), slog.String("bot", m.config.UserDisplayName))
c.Add(Message{ c.Add(Message{
EventID: eventID, EventID: eventID,
ParentID: parentID, ParentID: parentID,
@ -197,26 +185,26 @@ func (m *Bot) ResponseHandler() (event.Type, mautrix.EventHandler) {
// find out if message is a new question addressed to the bot // find out if message is a new question addressed to the bot
if conv == nil && isAddressed && addressedTo == m.config.UserDisplayName { if conv == nil && isAddressed && addressedTo == m.config.UserDisplayName {
m.logger.Info("message is addressed to bot", slog.String("event_id", eventID.String())) m.logger.Info("message is addressed to bot", slog.String("event_id", eventID.String()), slog.String("bot", m.config.UserDisplayName))
conv = NewConversation(eventID, m.config.SystemPrompt, content.Body) conv = NewConversation(eventID, m.config.SystemPrompt, content.Body)
m.conversations = append(m.conversations, conv) m.conversations = append(m.conversations, conv)
} }
// find out if the message is addressed to no-one and this bot answers those // find out if the message is addressed to no-one and this bot answers those
if conv == nil && !isAddressed && !hasParent && m.config.AnswerUnaddressed { if conv == nil && !isAddressed && !hasParent && m.config.AnswerUnaddressed {
m.logger.Info("message is addressed to no-one", slog.String("event_id", eventID.String())) m.logger.Info("message is addressed to no-one", slog.String("event_id", eventID.String()), slog.String("bot", m.config.UserDisplayName))
conv = NewConversation(eventID, m.config.SystemPrompt, content.Body) conv = NewConversation(eventID, m.config.SystemPrompt, content.Body)
m.conversations = append(m.conversations, conv) m.conversations = append(m.conversations, conv)
} }
if conv == nil { if conv == nil {
m.logger.Info("apparently not for us, ignoring", slog.String("event_id", eventID.String())) m.logger.Info("apparently not for us, ignoring", slog.String("event_id", eventID.String()), slog.String("bot", m.config.UserDisplayName))
return return
} }
// get reply from GPT // get reply from GPT
reply, err := m.gptClient.Complete(conv) reply, err := m.gptClient.Complete(conv)
if err != nil { if err != nil {
m.logger.Error("failed to get reply from openai", slog.String("err", err.Error())) m.logger.Error("failed to get reply from openai", slog.String("err", err.Error()), slog.String("bot", m.config.UserDisplayName))
return return
} }
@ -228,7 +216,7 @@ func (m *Bot) ResponseHandler() (event.Type, mautrix.EventHandler) {
} }
res, err := m.client.SendMessageEvent(evt.RoomID, event.EventMessage, &formattedReply) res, err := m.client.SendMessageEvent(evt.RoomID, event.EventMessage, &formattedReply)
if err != nil { if err != nil {
m.logger.Error("failed to send message", slog.String("err", err.Error())) m.logger.Error("failed to send message", slog.String("err", err.Error()), slog.String("bot", m.config.UserDisplayName))
return return
} }
conv.Add(Message{ conv.Add(Message{
@ -241,6 +229,6 @@ func (m *Bot) ResponseHandler() (event.Type, mautrix.EventHandler) {
if len(reply) > 30 { if len(reply) > 30 {
reply = reply[:30] + "..." reply = reply[:30] + "..."
} }
m.logger.Info("sent reply", slog.String("parent_id", eventID.String()), slog.String("content", reply)) m.logger.Info("sent reply", slog.String("parent_id", eventID.String()), slog.String("content", reply), slog.String("bot", m.config.UserDisplayName))
} }
} }