many changes; using dev laniakea
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
@@ -11,7 +12,6 @@ import (
|
||||
|
||||
"git.nix13.pw/scuroneko/laniakea"
|
||||
"git.nix13.pw/scuroneko/laniakea/tgapi"
|
||||
"git.nix13.pw/scuroneko/laniakea/utils"
|
||||
"github.com/vinovest/sqlx"
|
||||
)
|
||||
|
||||
@@ -58,7 +58,9 @@ func execSql(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
defer r.Close()
|
||||
defer func(r *sql.Rows) {
|
||||
_ = r.Close()
|
||||
}(r)
|
||||
for r.Next() {
|
||||
a := make(map[string]any)
|
||||
if err = sqlx.MapScan(r, a); err != nil {
|
||||
@@ -96,7 +98,9 @@ func uploadPhoto(ctx *laniakea.MsgContext, _ *database.Context) {
|
||||
return
|
||||
}
|
||||
u := tgapi.NewUploader(ctx.Api)
|
||||
defer u.Close()
|
||||
defer func(u *tgapi.Uploader) {
|
||||
_ = u.Close()
|
||||
}(u)
|
||||
content, err := ctx.Api.GetFileByLink(f.FilePath)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
@@ -111,5 +115,5 @@ func uploadPhoto(ctx *laniakea.MsgContext, _ *database.Context) {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
ctx.Answer(utils.EscapeMarkdown(msg.Photo.Last().FileID))
|
||||
ctx.Answer(msg.Photo.Last().FileID)
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ func rpInfo(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
out := []string{
|
||||
fmt.Sprintf("Привет, _%s_!", ctx.From.FirstName),
|
||||
fmt.Sprintf("*Выбранная вайфу*: %s", waifu.Name),
|
||||
fmt.Sprintf("*Выбранный пресет*: %s", laniakea.EscapeMarkdown(rpUser.Preset.Name)),
|
||||
fmt.Sprintf("*Выбранный пресет*: %s", laniakea.EscapeMarkdownV2(rpUser.Preset.Name)),
|
||||
fmt.Sprintf("*Выбранная модель*: %s", rpUser.Model.Name),
|
||||
fmt.Sprintf("*Использовано токенов*: %d", rpUser.UsedTokens),
|
||||
fmt.Sprintf("*Настройки сжатия*: %d %s", rpUser.CompressLimit, compressText),
|
||||
@@ -133,10 +133,10 @@ func rpInfo(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
kb.AddCallbackButtonStyle("Закрыть", laniakea.ButtonStyleDanger, "general.close")
|
||||
|
||||
if ctx.CallbackMsgId > 0 {
|
||||
ctx.EditCallback(strings.Join(out, "\n"), kb)
|
||||
ctx.EditCallbackMarkdown(laniakea.EscapePunctuation(strings.Join(out, "\n")), kb)
|
||||
ctx.AnswerCbQuery()
|
||||
} else {
|
||||
ctx.Keyboard(strings.Join(out, "\n"), kb)
|
||||
ctx.KeyboardMarkdown(laniakea.EscapePunctuation(strings.Join(out, "\n")), kb)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,13 +218,13 @@ func rpPresetsList(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
for i, preset := range presets {
|
||||
out[i] = fmt.Sprintf(
|
||||
"*%s* - %s",
|
||||
laniakea.EscapeMarkdown(preset.Name), preset.Description,
|
||||
laniakea.EscapeMarkdownV2(preset.Name), preset.Description,
|
||||
)
|
||||
kb.AddCallbackButton(preset.Name, "rp.preset_set", preset.ID)
|
||||
}
|
||||
kb.AddLine()
|
||||
kb.AddCallbackButtonStyle("Назад", laniakea.ButtonStyleDanger, "rp.info")
|
||||
ctx.EditCallback(strings.Join(out, "\n"), kb)
|
||||
ctx.EditCallbackMarkdown(strings.Join(out, "\n"), kb)
|
||||
ctx.AnswerCbQuery()
|
||||
}
|
||||
func rpPresetSet(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
@@ -270,7 +270,7 @@ func rpModelList(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
}
|
||||
kb.AddLine()
|
||||
kb.AddCallbackButtonStyle("Назад", laniakea.ButtonStyleDanger, "rp.info")
|
||||
ctx.EditCallback(strings.Join(out, "\n"), kb)
|
||||
ctx.EditCallbackMarkdown(laniakea.EscapePunctuation(strings.Join(out, "\n")), kb)
|
||||
ctx.AnswerCbQuery()
|
||||
}
|
||||
func rpModelSet(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
@@ -424,7 +424,6 @@ func newChatStage2(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
"Выбери сценарий:",
|
||||
}
|
||||
kb := laniakea.NewInlineKeyboard(2)
|
||||
scenariosIds := make(extypes.Slice[int], 0)
|
||||
for _, scenario := range scenarios {
|
||||
isSelected := selectedScenariosIds.Index(scenario.ID) >= 0
|
||||
prefix := ""
|
||||
@@ -435,18 +434,18 @@ func newChatStage2(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
}
|
||||
out = append(out, fmt.Sprintf("%s*%s* - %s", prefix, scenario.Name, scenario.Description))
|
||||
if isSelected {
|
||||
scenariosIds = selectedScenariosIds.Remove(scenario.ID)
|
||||
selectedScenariosIds = selectedScenariosIds.Remove(scenario.ID)
|
||||
} else {
|
||||
scenariosIds = selectedScenariosIds.Push(scenario.ID)
|
||||
selectedScenariosIds = selectedScenariosIds.Push(scenario.ID)
|
||||
}
|
||||
|
||||
kb.AddCallbackButtonStyle(
|
||||
fmt.Sprintf("%s%s", prefix, scenario.Name), style,
|
||||
"rp.new_chat_s2", utils.AppendToInt(settingId, scenariosIds)...,
|
||||
"rp.new_chat_s2", utils.AppendToInt(settingId, selectedScenariosIds)...,
|
||||
)
|
||||
}
|
||||
if len(scenariosIds) == 0 {
|
||||
scenariosIds = append(scenariosIds, 0)
|
||||
if len(selectedScenariosIds) == 0 {
|
||||
selectedScenariosIds = selectedScenariosIds.Push(0)
|
||||
}
|
||||
kb.AddCallbackButtonStyle("Создать", laniakea.ButtonStyleSuccess, "rp.new_chat", utils.AppendToInt(settingId, selectedScenariosIds)...)
|
||||
kb.AddLine()
|
||||
@@ -657,18 +656,14 @@ func generate(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
m := ctx.Keyboard("Генерация запущена...", kb)
|
||||
ctx.SendAction(tgapi.ChatActionTyping)
|
||||
api := openai.NewOpenAIAPI(ai.GPTBaseUrl, "", rpUser.Model.Key)
|
||||
defer api.Close()
|
||||
res, err := api.CreateCompletionStream(messages, userMessage, 0.5)
|
||||
if err != nil {
|
||||
_ = api.Close()
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
answerContent := ""
|
||||
type DraftQueue struct {
|
||||
message string
|
||||
mu sync.Mutex
|
||||
}
|
||||
draft := ctx.NewDraft()
|
||||
var mu sync.Mutex
|
||||
buffer := strings.Builder{} // накапливаем фрагменты
|
||||
@@ -729,13 +724,11 @@ func generate(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
mu.Lock()
|
||||
buffer.WriteString(content)
|
||||
mu.Unlock()
|
||||
|
||||
// Если нужна искусственная задержка, оставьте:
|
||||
// time.Sleep(time.Millisecond * 50)
|
||||
}
|
||||
|
||||
// Завершаем горутину и отправляем последнюю порцию
|
||||
close(done)
|
||||
_ = api.Close()
|
||||
if answerContent == "" {
|
||||
if m != nil {
|
||||
m.Delete()
|
||||
@@ -775,18 +768,18 @@ func generate(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
|
||||
kb = laniakea.NewInlineKeyboard(1)
|
||||
kb.AddCallbackButtonStyle("🔄 Перегенерировать 🔄", laniakea.ButtonStyleSuccess, "rp.regenerate", counter+2)
|
||||
//kb.AddButton(laniakea.NewInlineKbButton("Тест").SetStyle(laniakea.ButtonStyleSuccess).SetIconCustomEmojiId("5375155835846534814").SetCallbackData("rp.test"))
|
||||
ctx.Keyboard(laniakea.EscapeMarkdown(answerContent), kb)
|
||||
ctx.Keyboard(answerContent, kb)
|
||||
|
||||
// Auto compress
|
||||
compressMethod := rpUser.CompressMethod
|
||||
if compressMethod == "messages" {
|
||||
switch compressMethod {
|
||||
case "messages":
|
||||
if counter+2 >= rpUser.CompressLimit {
|
||||
m = ctx.Answer("Запущено сжатие чата…")
|
||||
_compress(ctx, db)
|
||||
m.Delete()
|
||||
}
|
||||
} else if compressMethod == "tokens" {
|
||||
case "tokens":
|
||||
if tokens >= rpUser.CompressLimit*1000 {
|
||||
m = ctx.Answer("Запущено сжатие чата…")
|
||||
_compress(ctx, db)
|
||||
@@ -844,7 +837,9 @@ func regenerateResponse(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
}
|
||||
|
||||
api := openai.NewOpenAIAPI(ai.GPTBaseUrl, "", rpUser.Model.Key)
|
||||
defer api.Close()
|
||||
defer func(api *openai.API) {
|
||||
_ = api.Close()
|
||||
}(api)
|
||||
|
||||
messages = messages.Pop(count - 2)
|
||||
messages = messages.Pop(count - 2)
|
||||
@@ -864,7 +859,7 @@ func regenerateResponse(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
|
||||
kb := laniakea.NewInlineKeyboard(1)
|
||||
kb.AddCallbackButtonStyle("🔄 Перегенерировать 🔄", laniakea.ButtonStyleSuccess, "rp.regenerate", count)
|
||||
ctx.EditCallback(laniakea.EscapeMarkdown(res.Choices[0].Message.Content), kb)
|
||||
ctx.EditCallback(res.Choices[0].Message.Content, kb)
|
||||
}
|
||||
|
||||
func compress(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
@@ -911,7 +906,9 @@ func _compress(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
}
|
||||
|
||||
api := openai.NewOpenAIAPI(ai.GPTBaseUrl, "", user.Model.Key)
|
||||
defer api.Close()
|
||||
defer func(api *openai.API) {
|
||||
_ = api.Close()
|
||||
}(api)
|
||||
res, err := ai.CompressChat(api, messages)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
@@ -949,7 +946,7 @@ func _compress(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
for _, m := range history {
|
||||
if m.Role == "assistant" {
|
||||
tokens += len(m.Message)
|
||||
err = mdb.UpdateRPChatHistory(db, chatId, m.Role, m.Message, index)
|
||||
_ = mdb.UpdateRPChatHistory(db, chatId, m.Role, m.Message, index)
|
||||
index++
|
||||
} else {
|
||||
break
|
||||
@@ -1007,12 +1004,13 @@ func compressSettingStage2(ctx *laniakea.MsgContext, _ *database.Context) {
|
||||
"При выборе слишком больших значений, некоторые детали могут теряться.",
|
||||
}
|
||||
kb := laniakea.NewInlineKeyboard(3)
|
||||
if method == "messages" {
|
||||
switch method {
|
||||
case "messages":
|
||||
out = append(out, "Выбери количество сообщений, после которых будет происходить сжатие чата")
|
||||
for _, i := range messagesMethodCount {
|
||||
kb.AddCallbackButton(strconv.Itoa(i), "rp.compress_setting", method, i)
|
||||
}
|
||||
} else if method == "tokens" {
|
||||
case "tokens":
|
||||
out = append(out, "Выбери количество токенов, после которых будет происходить сжатие чата")
|
||||
for _, i := range tokenMethodCount {
|
||||
kb.AddCallbackButton(strconv.Itoa(i), "rp.compress_setting", method, i)
|
||||
|
||||
@@ -20,9 +20,18 @@ func RegisterService() *laniakea.Plugin[database.Context] {
|
||||
}
|
||||
|
||||
func about(ctx *laniakea.MsgContext, _ *database.Context) {
|
||||
formatVersion := func() string {
|
||||
parts := strings.Split(laniakea.VersionString, "-")
|
||||
main := parts[0]
|
||||
if len(parts) > 1 {
|
||||
beta := strings.Join(strings.Split(parts[1], "."), " ")
|
||||
return main + " " + beta
|
||||
}
|
||||
return main
|
||||
}
|
||||
out := []string{
|
||||
fmt.Sprintf("Версия Go: %s", runtime.Version()[2:]),
|
||||
fmt.Sprintf("Версия Laniakea: %s", laniakea.VersionString),
|
||||
fmt.Sprintf("Версия Laniakea: %s", formatVersion()),
|
||||
fmt.Sprintf("Время сборки: %s", utils.BuildTime),
|
||||
fmt.Sprintf("Git хеш: %s", utils.GitCommit),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user