Skip to content

Commit

Permalink
fix history expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
mazzz1y committed Aug 9, 2023
1 parent fc00565 commit 4a5f001
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ docker run -d --name matrix-gpt \
-e SQLITE_PATH="persistent path for sqlite database"
-e USER_IDS="allowed user ids"
ghcr.io/mazzz1y/matrix-gpt:latest

```
## Configuration

Expand Down
3 changes: 0 additions & 3 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package bot

import (
"sync"

_ "github.com/mattn/go-sqlite3"
"github.com/mazzz1y/matrix-gpt/internal/gpt"
"github.com/rs/zerolog/log"
Expand All @@ -11,7 +9,6 @@ import (
)

type Bot struct {
sync.Mutex
client *mautrix.Client
gptClient *gpt.Gpt
selfProfile mautrix.RespUserProfile
Expand Down
17 changes: 12 additions & 5 deletions internal/bot/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ func (b *Bot) historyResetHandler(user *gpt.User, evt *event.Event) (ok bool) {
}

// historyExpireHandler checks if the history for a user has expired and resets if necessary.
func (b *Bot) historyExpireHandler(user *gpt.User) {
if user.LastMsg.Add(time.Duration(b.historyExpire) * time.Hour).Before(time.Now()) {
func (b *Bot) historyExpireHandler(user *gpt.User) (ok bool) {
if user.GetLastMsgTime().Add(time.Duration(b.historyExpire) * time.Hour).Before(time.Now()) {
user.History.ResetHistory()
return true
}
return false
}

// msgEvtDispatcher dispatches incoming messages to their appropriate handlers.
Expand All @@ -67,15 +69,20 @@ func (b *Bot) msgEvtDispatcher(source mautrix.EventSource, evt *event.Event) {
return
}

if b.historyExpireHandler(user) {
l.Info().Msg("history has expired, resetting")
}
if b.historyResetHandler(user, evt) {
l.Info().Msg("reset history")
l.Info().Msg("history reset by user command")
return
}

err := b.sendAnswer(user, evt)
if err != nil {
l.Err(err).Msg("failed to send message")
} else {
l.Info().Msg("sending answer")
return
}

l.Info().Msg("sending answer")
user.UpdateLastMsgTime()
}
21 changes: 20 additions & 1 deletion internal/gpt/users.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
package gpt

import (
"sync"
"time"
)

// User represents a GPT user with a chat history and last message timestamp.
type User struct {
sync.RWMutex
History *HistoryManager
LastMsg time.Time
lastMsg time.Time
}

// NewGptUser creates a new GPT user instance with a given history size.
func NewGptUser(historySize int) *User {
return &User{
History: NewHistoryManager(historySize),
lastMsg: time.Now(),
}
}

// GetLastMsgTime retrieves the timestamp of the user's last message.
func (u *User) GetLastMsgTime() time.Time {
u.RLock()
defer u.RUnlock()

return u.lastMsg
}

// UpdateLastMsgTime updates the timestamp of the user's last message to the current time.
func (u *User) UpdateLastMsgTime() {
u.Lock()
defer u.Unlock()

u.lastMsg = time.Now()
}

0 comments on commit 4a5f001

Please sign in to comment.