dev
This commit is contained in:
@@ -3,6 +3,7 @@ package red
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"kurumibot/database/psql"
|
||||
"kurumibot/utils"
|
||||
"strings"
|
||||
|
||||
@@ -15,10 +16,25 @@ var ctx = context.Background()
|
||||
|
||||
type RPRepository struct {
|
||||
client *redis.Client
|
||||
db *laniakea.DatabaseContext
|
||||
}
|
||||
|
||||
type RPChat struct {
|
||||
ID uuid.UUID
|
||||
UserID int
|
||||
WaifuID int
|
||||
Prompt string
|
||||
Counter int
|
||||
ChatTokens int64
|
||||
|
||||
SettingID int
|
||||
Setting *psql.RPSetting
|
||||
ScenariosIDs []int
|
||||
Scenarios []psql.RPScenario
|
||||
}
|
||||
|
||||
func NewRPRepository(db *laniakea.DatabaseContext) RPRepository {
|
||||
return RPRepository{client: db.Redis}
|
||||
return RPRepository{db.Redis, db}
|
||||
}
|
||||
|
||||
func (rep RPRepository) SetSelectedWaifu(userId, waifuId int) error {
|
||||
@@ -55,6 +71,55 @@ func (rep RPRepository) GetOrCreateChatId(userId, waifuId int) (string, error) {
|
||||
err := rep.SetChatId(userId, waifuId, chatId)
|
||||
return chatId, err
|
||||
}
|
||||
func (rep RPRepository) GetChat(userId, waifuId int) (RPChat, error) {
|
||||
var chat RPChat
|
||||
chatId, err := rep.GetOrCreateChatId(userId, waifuId)
|
||||
if err != nil {
|
||||
return chat, err
|
||||
}
|
||||
|
||||
chat.ID = uuid.MustParse(chatId)
|
||||
chat.UserID = userId
|
||||
chat.WaifuID = waifuId
|
||||
chat.Prompt = rep.GetChatPrompt(userId, waifuId)
|
||||
chat.Counter = rep.GetCounter(userId, waifuId)
|
||||
chat.ChatTokens = int64(rep.GetChatTokens(userId, waifuId))
|
||||
|
||||
chat.SettingID = rep.GetChatSettingID(userId, waifuId)
|
||||
psqlRep := psql.NewRPRepository(rep.db)
|
||||
setting, err := psqlRep.GetSetting(chat.SettingID)
|
||||
if err != nil {
|
||||
return chat, err
|
||||
}
|
||||
chat.Setting = &setting
|
||||
|
||||
chat.ScenariosIDs = rep.GetChatScenariosIDs(userId, waifuId)
|
||||
chat.Scenarios = make([]psql.RPScenario, len(chat.ScenariosIDs))
|
||||
for i, id := range chat.ScenariosIDs {
|
||||
chat.Scenarios[i], err = psqlRep.GetScenario(id)
|
||||
if err != nil {
|
||||
return chat, err
|
||||
}
|
||||
}
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (rep RPRepository) SaveChat(chat RPChat) error {
|
||||
chatId := chat.ID.String()
|
||||
waifuId := chat.WaifuID
|
||||
userId := chat.UserID
|
||||
var err error
|
||||
|
||||
if err = rep.SetChatPrompt(userId, waifuId, chat.Prompt); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = rep.SetCounter(userId, waifuId, chat.Counter); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = rep.SetChatTokens(userId, waifuId, int(chat.ChatTokens)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (rep RPRepository) SetChatPrompt(userId, waifuId int, prompt string) error {
|
||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.prompt", userId, waifuId)
|
||||
|
||||
Reference in New Issue
Block a user