41 lines
1015 B
Go
41 lines
1015 B
Go
package mdb
|
|
|
|
import (
|
|
"context"
|
|
"kurumibot/database"
|
|
"kurumibot/laniakea"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
type RPChatMessage struct {
|
|
ChatID string `bson:"chat_id"`
|
|
Role string `bson:"role"`
|
|
Message string `bson:"message"`
|
|
}
|
|
|
|
func GetChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*RPChatMessage, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
col := database.GetMongoCollection(db, "rp_chat_history")
|
|
cursor, err := col.Find(ctx, bson.M{"chat_id": chatId})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]*RPChatMessage, 0)
|
|
err = cursor.All(ctx, &result)
|
|
return result, err
|
|
}
|
|
func UpdateChatHistory(db *laniakea.DatabaseContext, chatId, role, message string) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
col := database.GetMongoCollection(db, "rp_chat_history")
|
|
_, err := col.InsertOne(ctx, RPChatMessage{
|
|
chatId,
|
|
role,
|
|
message,
|
|
})
|
|
return err
|
|
}
|