memory leak

This commit is contained in:
2026-02-05 15:14:47 +03:00
parent b894cbd9c3
commit 0fded455b8
22 changed files with 420 additions and 170 deletions

35
database/mdb/ai_chats.go Normal file
View File

@@ -0,0 +1,35 @@
package mdb
import (
"context"
"kurumibot/database"
"time"
"git.nix13.pw/scuroneko/laniakea"
"go.mongodb.org/mongo-driver/v2/bson"
)
func GetGptChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*AiChatMessage, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "gpt_chat_messages")
cursor, err := col.Find(ctx, bson.M{"chatId": chatId})
if err != nil {
return nil, err
}
result := make([]*AiChatMessage, 0)
err = cursor.All(ctx, &result)
return result, err
}
func UpdateGptChatHistory(db *laniakea.DatabaseContext, chatId, role, message string) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "gpt_chat_messages")
_, err := col.InsertOne(ctx, AiChatMessage{
bson.NewObjectID(),
chatId,
role,
message, 0,
})
return err
}

View File

@@ -5,16 +5,19 @@ import (
"kurumibot/database"
"time"
"git.nix13.pw/scuroneko/extypes"
"git.nix13.pw/scuroneko/laniakea"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
type ConsoleLogEntry struct {
Level string `bson:"level"`
Prefix string `bson:"prefix"`
Traceback string `bson:"traceback"`
Message string `bson:"message"`
Time time.Time `bson:"time"`
TimeStamp int64 `bson:"timeStamp"`
Level string `bson:"level" json:"level"`
Prefix string `bson:"prefix" json:"prefix"`
Traceback string `bson:"traceback" json:"traceback"`
Message string `bson:"message" json:"message"`
Time time.Time `bson:"time" json:"time"`
TimeStamp int64 `bson:"timeStamp" json:"time_stamp"`
}
func WriteConsoleLog(db *laniakea.DatabaseContext, e *ConsoleLogEntry) error {
@@ -24,13 +27,29 @@ func WriteConsoleLog(db *laniakea.DatabaseContext, e *ConsoleLogEntry) error {
_, err := col.InsertOne(ctx, e)
return err
}
func GetConsoleLogs(db *laniakea.DatabaseContext) (extypes.Slice[*ConsoleLogEntry], error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "logs")
opts := options.Find()
opts.SetLimit(5)
opts.SetSort(bson.D{{"_id", 1}})
cursor, err := col.Find(ctx, bson.D{}, opts)
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
result := make(extypes.Slice[*ConsoleLogEntry], 0)
err = cursor.All(ctx, &result)
return result, nil
}
type MessageLogEntry struct {
MessageID int `bson:"messageId"`
SenderID int `bson:"senderId"`
ChatID int `bson:"chatId"`
Text string `bson:"text"`
TimeStamp int64 `bson:"timestamp"`
MessageID int `bson:"messageId" json:"message_id"`
SenderID int `bson:"senderId" json:"sender_id"`
ChatID int `bson:"chatId" json:"chat_id"`
Text string `bson:"text" json:"text"`
TimeStamp int64 `bson:"timestamp" json:"timestamp"`
}
func WriteMessageLog(db *laniakea.DatabaseContext, e *MessageLogEntry) error {
@@ -40,3 +59,19 @@ func WriteMessageLog(db *laniakea.DatabaseContext, e *MessageLogEntry) error {
_, err := col.InsertOne(ctx, e)
return err
}
func GetMessageLogs(db *laniakea.DatabaseContext) (extypes.Slice[*MessageLogEntry], error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "msg_logs")
opts := options.Find()
opts.SetLimit(5)
opts.SetSort(bson.D{{"_id", 1}})
cursor, err := col.Find(ctx, bson.D{}, opts)
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
result := make(extypes.Slice[*MessageLogEntry], 0)
err = cursor.All(ctx, &result)
return result, nil
}

View File

@@ -9,7 +9,7 @@ import (
"go.mongodb.org/mongo-driver/v2/bson"
)
type RPChatMessage struct {
type AiChatMessage struct {
Id bson.ObjectID `bson:"_id"`
ChatID string `bson:"chatId"`
Role string `bson:"role"`
@@ -17,7 +17,7 @@ type RPChatMessage struct {
Index int `bson:"index"`
}
func GetChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*RPChatMessage, error) {
func GetRPChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*AiChatMessage, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "rp_chat_messages")
@@ -25,15 +25,15 @@ func GetChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*RPChatMessa
if err != nil {
return nil, err
}
result := make([]*RPChatMessage, 0)
result := make([]*AiChatMessage, 0)
err = cursor.All(ctx, &result)
return result, err
}
func UpdateChatHistory(db *laniakea.DatabaseContext, chatId, role, message string, index int) error {
func UpdateRPChatHistory(db *laniakea.DatabaseContext, chatId, role, message string, index int) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "rp_chat_messages")
_, err := col.InsertOne(ctx, RPChatMessage{
_, err := col.InsertOne(ctx, AiChatMessage{
bson.NewObjectID(),
chatId,
role,
@@ -41,13 +41,13 @@ func UpdateChatHistory(db *laniakea.DatabaseContext, chatId, role, message strin
})
return err
}
func GetChatHistorySize(db *laniakea.DatabaseContext, chatId string) (int64, error) {
func GetRPChatHistorySize(db *laniakea.DatabaseContext, chatId string) (int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "rp_chat_messages")
return col.CountDocuments(ctx, bson.M{"chatId": chatId})
}
func DeleteChatEntry(db *laniakea.DatabaseContext, entry *RPChatMessage) error {
func DeleteRPChatEntry(db *laniakea.DatabaseContext, entry *AiChatMessage) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "rp_chat_messages")