some fixes and changes
This commit is contained in:
@@ -2,6 +2,8 @@ package red
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"kurumibot/database/psql"
|
||||
"kurumibot/utils"
|
||||
@@ -71,8 +73,13 @@ 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) {
|
||||
func (rep RPRepository) GetChat(userId int) (RPChat, error) {
|
||||
var chat RPChat
|
||||
waifuId := rep.GetSelectedWaifu(userId)
|
||||
if waifuId == 0 {
|
||||
return chat, errors.New("no WaifuID")
|
||||
}
|
||||
|
||||
chatId, err := rep.GetOrCreateChatId(userId, waifuId)
|
||||
if err != nil {
|
||||
return chat, err
|
||||
@@ -87,17 +94,28 @@ func (rep RPRepository) GetChat(userId, waifuId int) (RPChat, error) {
|
||||
|
||||
chat.SettingID = rep.GetChatSettingID(userId, waifuId)
|
||||
psqlRep := psql.NewRPRepository(rep.db)
|
||||
setting, err := psqlRep.GetSetting(chat.SettingID)
|
||||
if err != nil {
|
||||
return chat, err
|
||||
if chat.SettingID > 0 {
|
||||
setting, err := psqlRep.GetSetting(chat.SettingID)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return chat, fmt.Errorf("setting %d not found", chat.SettingID)
|
||||
}
|
||||
return chat, err
|
||||
}
|
||||
chat.Setting = &setting
|
||||
}
|
||||
chat.Setting = &setting
|
||||
|
||||
chat.ScenariosIDs = rep.GetChatScenariosIDs(userId, waifuId)
|
||||
chat.Scenarios = make([]psql.RPScenario, len(chat.ScenariosIDs))
|
||||
for i, id := range chat.ScenariosIDs {
|
||||
if id <= 0 {
|
||||
continue
|
||||
}
|
||||
chat.Scenarios[i], err = psqlRep.GetScenario(id)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return chat, fmt.Errorf("scenario %d not found", id)
|
||||
}
|
||||
return chat, err
|
||||
}
|
||||
}
|
||||
@@ -105,7 +123,6 @@ func (rep RPRepository) GetChat(userId, waifuId int) (RPChat, error) {
|
||||
}
|
||||
|
||||
func (rep RPRepository) SaveChat(chat RPChat) error {
|
||||
chatId := chat.ID.String()
|
||||
waifuId := chat.WaifuID
|
||||
userId := chat.UserID
|
||||
var err error
|
||||
@@ -119,6 +136,29 @@ func (rep RPRepository) SaveChat(chat RPChat) error {
|
||||
if err = rep.SetChatTokens(userId, waifuId, int(chat.ChatTokens)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var settingId int
|
||||
if chat.Setting != nil {
|
||||
settingId = chat.Setting.ID
|
||||
} else {
|
||||
settingId = chat.SettingID
|
||||
}
|
||||
if err = rep.SetChatSettingID(userId, waifuId, settingId); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var ids []int
|
||||
if len(chat.Scenarios) > 0 {
|
||||
ids = utils.Map(chat.Scenarios, func(s psql.RPScenario) int {
|
||||
return s.ID
|
||||
})
|
||||
} else {
|
||||
ids = chat.ScenariosIDs
|
||||
}
|
||||
if err = rep.SetChatScenariosIDs(userId, waifuId, ids); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rep RPRepository) SetChatPrompt(userId, waifuId int, prompt string) error {
|
||||
@@ -176,9 +216,10 @@ func (rep RPRepository) GetChatSettingID(userId, waifuId int) int {
|
||||
return i
|
||||
}
|
||||
|
||||
func (rep RPRepository) SetChatScenariosIDs(userId, waifuId int, scenarioIds string) error {
|
||||
func (rep RPRepository) SetChatScenariosIDs(userId, waifuId int, scenarioIds []int) error {
|
||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.scenario_id", userId, waifuId)
|
||||
return rep.client.Set(ctx, key, scenarioIds, 0).Err()
|
||||
ids := strings.Join(utils.Map(scenarioIds, utils.AnyToString), ",")
|
||||
return rep.client.Set(ctx, key, ids, 0).Err()
|
||||
}
|
||||
func (rep RPRepository) GetChatScenariosIDs(userId, waifuId int) []int {
|
||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.scenario_id", userId, waifuId)
|
||||
|
||||
Reference in New Issue
Block a user