setting and scenario in chat stat
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"kurumibot/laniakea"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
@@ -44,6 +45,14 @@ func (rep *RPRepository) GetChatId(userId, waifuId int) string {
|
||||
}
|
||||
return res.Val()
|
||||
}
|
||||
func (rep *RPRepository) GetOrCreateChatId(userId, waifuId int) (string, error) {
|
||||
chatId := rep.GetChatId(userId, waifuId)
|
||||
if chatId == "" {
|
||||
chatId = uuid.New().String()
|
||||
}
|
||||
err := rep.SetChatId(userId, waifuId, chatId)
|
||||
return chatId, err
|
||||
}
|
||||
|
||||
func (rep *RPRepository) SetChatPrompt(userId, waifuId int, prompt string) error {
|
||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.prompt", userId, waifuId)
|
||||
@@ -85,3 +94,31 @@ func (rep *RPRepository) GetChatTokens(userId, waifuId int) int {
|
||||
i, _ := res.Int()
|
||||
return i
|
||||
}
|
||||
|
||||
func (rep *RPRepository) SetChatSettingID(userId, waifuId, settingId int) error {
|
||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.setting_id", userId, waifuId)
|
||||
return rep.client.Set(ctx, key, settingId, 0).Err()
|
||||
}
|
||||
func (rep *RPRepository) GetChatSettingID(userId, waifuId int) int {
|
||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.setting_id", userId, waifuId)
|
||||
res := rep.client.Get(ctx, key)
|
||||
if res.Err() != nil {
|
||||
return 0
|
||||
}
|
||||
i, _ := res.Int()
|
||||
return i
|
||||
}
|
||||
|
||||
func (rep *RPRepository) SetChatScenarioID(userId, waifuId, scenarioId int) error {
|
||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.scenario_id", userId, waifuId)
|
||||
return rep.client.Set(ctx, key, scenarioId, 0).Err()
|
||||
}
|
||||
func (rep *RPRepository) GetChatScenarioID(userId, waifuId int) int {
|
||||
key := fmt.Sprintf("ai.chats.rp.%d.%d.scenario_id", userId, waifuId)
|
||||
res := rep.client.Get(ctx, key)
|
||||
if res.Err() != nil {
|
||||
return 0
|
||||
}
|
||||
i, _ := res.Int()
|
||||
return i
|
||||
}
|
||||
|
||||
2
laniakea
2
laniakea
Submodule laniakea updated: 37397ba90f...c503b68814
@@ -29,16 +29,12 @@ func RegisterRP(bot *laniakea.Bot) {
|
||||
rp.Payload(rpScenarioList, "rp.scenario_list")
|
||||
rp.Payload(rpSettingList, "rp.setting_list")
|
||||
rp.Payload(chatStat, "rp.chat_stat")
|
||||
|
||||
rp.Payload(newChatStage1, "rp.new_chat_s1")
|
||||
rp.Payload(newChatStage2, "rp.new_chat_s2")
|
||||
rp.Payload(newChat, "rp.new_chat")
|
||||
|
||||
rp.Command(rpUserPromptGet, "rpuserpget")
|
||||
rp.Command(rpUserPromptSet, "rpuserpset")
|
||||
rp.Command(generate, "g", "gen", "г")
|
||||
rp.Payload(compress, "rp.compress_chat")
|
||||
|
||||
rp.Payload(generalClose, "general.close")
|
||||
|
||||
bot.AddPlugins(rp.Build())
|
||||
@@ -95,7 +91,7 @@ func rpInfo(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
fmt.Sprintf("*Выбранная вайфу*: %s", waifu.Name),
|
||||
fmt.Sprintf("*Выбранный пресет*: %s", laniakea.EscapeMarkdown(rpUser.Preset.Name)),
|
||||
fmt.Sprintf("*Выбранная модель*: %s", rpUser.Model.Name),
|
||||
fmt.Sprintf("*Твое описание персонажа*: %s", rpUser.UserPrompt),
|
||||
fmt.Sprintf("*Твоё описание персонажа*: %s", rpUser.UserPrompt),
|
||||
}
|
||||
|
||||
kb := laniakea.NewInlineKeyboard(2)
|
||||
@@ -283,13 +279,9 @@ func chatStat(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
}
|
||||
chatId := redisRpRep.GetChatId(ctx.FromID, waifuId)
|
||||
if chatId == "" {
|
||||
chatId = uuid.New().String()
|
||||
err := redisRpRep.SetChatId(ctx.FromID, waifuId, chatId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
ctx.Answer("Нет активного чата")
|
||||
return
|
||||
}
|
||||
}
|
||||
messageCount, err := mdb.GetChatHistorySize(db, chatId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
@@ -306,6 +298,24 @@ func chatStat(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
fmt.Sprintf("*Кол-во сообщений*: %d", messageCount),
|
||||
fmt.Sprintf("*Кол-во токенов*: %d", tokens),
|
||||
}
|
||||
psqlRpRep := psql.NewRPRepository(db)
|
||||
scenarioId := redisRpRep.GetChatScenarioID(ctx.FromID, waifuId)
|
||||
if scenarioId > 0 {
|
||||
scenario, err := psqlRpRep.GetScenario(scenarioId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
out = append(out, fmt.Sprintf("*Выбранный сценарий*: %s (ID: %d)", scenario.Name, scenario.ID))
|
||||
}
|
||||
|
||||
settingId := redisRpRep.GetChatSettingID(ctx.FromID, waifuId)
|
||||
if settingId > 0 {
|
||||
setting, err := psqlRpRep.GetSetting(settingId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
out = append(out, fmt.Sprintf("*Выбранный сеттинг*: %s (ID: %d)", setting.Name, setting.ID))
|
||||
}
|
||||
ctx.EditCallback(strings.Join(out, "\n"), kb)
|
||||
}
|
||||
|
||||
@@ -423,34 +433,28 @@ func newChat(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
}
|
||||
}
|
||||
|
||||
err = redisRpRep.SetChatSettingID(ctx.FromID, waifuId, settingId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
err = redisRpRep.SetChatScenarioID(ctx.FromID, waifuId, scenarioId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
|
||||
err = redisRpRep.SetCounter(ctx.FromID, waifuId, 0)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
err = redisRpRep.SetChatTokens(ctx.FromID, waifuId, 0)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
kb := laniakea.NewInlineKeyboard(2)
|
||||
kb.AddCallbackButton("На главную", "rp.info").AddCallbackButton("Закрыть", "general.close")
|
||||
ctx.EditCallback(`Был создан новый чат.\nДля общения используй "/г промпт"`, kb)
|
||||
ctx.EditCallback("Был создан новый чат. Для общения используй `/г промпт`.", kb)
|
||||
}
|
||||
|
||||
func rpUserPromptGet(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
rep := psql.NewRPRepository(db)
|
||||
user, err := rep.GetOrCreateUser(int64(ctx.FromID))
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
if user.UserPrompt == "" {
|
||||
ctx.Answer("У тебя нет описания")
|
||||
return
|
||||
}
|
||||
ctx.Answerf("Вот твое РП описание пользователя\n%s", user.UserPrompt)
|
||||
}
|
||||
func rpUserPromptSet(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
if len(ctx.Args) == 0 || ctx.Args[0] == "" {
|
||||
return
|
||||
@@ -468,33 +472,25 @@ func rpUserPromptSet(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
ctx.Answer("Описание пользователя было обновлено.")
|
||||
ctx.Answer("Описание пользователя было обновлено")
|
||||
}
|
||||
|
||||
func generate(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
redisRpRep := red.NewRPRepository(db)
|
||||
rpRep := psql.NewRPRepository(db)
|
||||
rpUser, err := rpRep.GetOrCreateUser(int64(ctx.FromID))
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
waifuId := redisRpRep.GetSelectedWaifu(ctx.FromID)
|
||||
if waifuId == 0 {
|
||||
ctx.Answer("Не выбрана вайфу")
|
||||
return
|
||||
}
|
||||
chatId := redisRpRep.GetChatId(ctx.FromID, waifuId)
|
||||
if chatId == "" {
|
||||
chatId = uuid.New().String()
|
||||
err := redisRpRep.SetChatId(ctx.FromID, waifuId, chatId)
|
||||
waifuRep := psql.NewWaifuRepository(db)
|
||||
waifu, err := waifuRep.GetById(waifuId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
waifuRep := psql.NewWaifuRepository(db)
|
||||
waifu, err := waifuRep.GetById(waifuId)
|
||||
|
||||
rpUser, err := rpRep.GetOrCreateUser(int64(ctx.FromID))
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
@@ -524,6 +520,11 @@ func generate(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
Content: ai.FormatPrompt(preset.PostHistory, waifu.Name, ctx.From.FirstName),
|
||||
}
|
||||
|
||||
chatId, err := redisRpRep.GetOrCreateChatId(ctx.FromID, waifuId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
history, err := mdb.GetChatHistory(db, chatId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
@@ -589,15 +590,11 @@ func compress(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
ctx.Answer("Не выбрана вайфу")
|
||||
return
|
||||
}
|
||||
chatId := redisRpRep.GetChatId(ctx.FromID, waifuId)
|
||||
if chatId == "" {
|
||||
chatId = uuid.New().String()
|
||||
err := redisRpRep.SetChatId(ctx.FromID, waifuId, chatId)
|
||||
chatId, err := redisRpRep.GetOrCreateChatId(ctx.FromID, waifuId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
history, err := mdb.GetChatHistory(db, chatId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
|
||||
Reference in New Issue
Block a user