-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c28a8b
commit 8521e4a
Showing
7 changed files
with
162 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/dev/storozhenko/familybot/feature/ai/MemoryAddExecutor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package dev.storozhenko.familybot.feature.ai | ||
|
||
import dev.storozhenko.familybot.BotConfig | ||
import dev.storozhenko.familybot.common.extensions.send | ||
import dev.storozhenko.familybot.core.executors.Executor | ||
import dev.storozhenko.familybot.core.keyvalue.EasyKeyValueService | ||
import dev.storozhenko.familybot.core.routers.models.ExecutorContext | ||
import dev.storozhenko.familybot.core.routers.models.Priority | ||
import dev.storozhenko.familybot.feature.settings.models.ChatGPTMemory | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class MemoryAddExecutor( | ||
private val easyKeyValueService: EasyKeyValueService, | ||
private val botConfig: BotConfig | ||
) : Executor { | ||
|
||
|
||
override suspend fun execute(context: ExecutorContext) { | ||
val text = context.message.text | ||
if (text.isNullOrBlank()) { | ||
context.client.send(context, "Пришли нормально") | ||
return | ||
} | ||
if (text.length > 300) { | ||
context.client.send(context, "Говорили же меньше 300 символов. Ты прислал ${text.length}") | ||
return | ||
} | ||
|
||
val currentMemory = easyKeyValueService.get(ChatGPTMemory, context.chatKey, "") | ||
easyKeyValueService.put(ChatGPTMemory, context.chatKey, "$currentMemory\n$text") | ||
context.client.send(context, "Готово") | ||
} | ||
|
||
override fun canExecute(context: ExecutorContext): Boolean { | ||
val replyToMessage = context.update.message?.replyToMessage | ||
return replyToMessage != null && replyToMessage.from.userName == botConfig.botName | ||
&& replyToMessage.text == "Напиши что добавить в ответ на это сообщение, 300 символов максимум" | ||
} | ||
|
||
override fun priority(context: ExecutorContext) = Priority.MEDIUM | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/dev/storozhenko/familybot/feature/ai/MemoryCommandExecutor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package dev.storozhenko.familybot.feature.ai | ||
|
||
import dev.storozhenko.familybot.common.extensions.send | ||
import dev.storozhenko.familybot.core.executors.CommandExecutor | ||
import dev.storozhenko.familybot.core.models.telegram.Command | ||
import dev.storozhenko.familybot.core.routers.models.ExecutorContext | ||
import org.springframework.stereotype.Component | ||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup | ||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton | ||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardRow | ||
|
||
@Component | ||
class MemoryCommandExecutor : CommandExecutor() { | ||
override fun command() = Command.MEMORY | ||
|
||
override suspend fun execute(context: ExecutorContext) { | ||
context.client.send(context, "Какое действие с ИИ памятью вы хотите выполнить?", customization = { | ||
replyMarkup = InlineKeyboardMarkup( | ||
listOf( | ||
InlineKeyboardRow( | ||
listOf( | ||
InlineKeyboardButton("Добавить") | ||
.apply { callbackData = "add" }, | ||
InlineKeyboardButton("Показать что есть") | ||
.apply { callbackData = "show" }, | ||
InlineKeyboardButton("Стереть все") | ||
.apply { callbackData = "clear" }, | ||
) | ||
) | ||
) | ||
) | ||
}) | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/kotlin/dev/storozhenko/familybot/feature/ai/MemoryContinuousExecutor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package dev.storozhenko.familybot.feature.ai | ||
|
||
import dev.storozhenko.familybot.BotConfig | ||
import dev.storozhenko.familybot.common.extensions.isFromAdmin | ||
import dev.storozhenko.familybot.common.extensions.send | ||
import dev.storozhenko.familybot.core.executors.ContinuousConversationExecutor | ||
import dev.storozhenko.familybot.core.keyvalue.EasyKeyValueService | ||
import dev.storozhenko.familybot.core.models.telegram.Command | ||
import dev.storozhenko.familybot.core.routers.models.ExecutorContext | ||
import dev.storozhenko.familybot.feature.settings.models.ChatGPTMemory | ||
import org.springframework.stereotype.Component | ||
import org.telegram.telegrambots.meta.api.methods.AnswerCallbackQuery | ||
import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage | ||
|
||
@Component | ||
class MemoryContinuousExecutor( | ||
private val easyKeyValueService: EasyKeyValueService, | ||
botConfig: BotConfig | ||
) : ContinuousConversationExecutor(botConfig) { | ||
override fun getDialogMessages(context: ExecutorContext): Set<String> { | ||
return setOf("Какое действие с ИИ памятью вы хотите выполнить?") | ||
} | ||
|
||
override fun command() = Command.MEMORY | ||
|
||
override suspend fun execute(context: ExecutorContext) { | ||
context.client.execute(AnswerCallbackQuery(context.update.callbackQuery.id)) | ||
val data = context.update.callbackQuery.data | ||
when (data) { | ||
"add" -> add(context) | ||
"show" -> show(context) | ||
"clear" -> clear(context) | ||
} | ||
context.client.execute(DeleteMessage(context.chat.idString, context.message.messageId)) | ||
} | ||
|
||
private suspend fun show(context: ExecutorContext) { | ||
val memory = easyKeyValueService.get(ChatGPTMemory, context.chatKey) | ||
if (memory != null) { | ||
context.client.send(context, "Текущая память: $memory") | ||
} else { | ||
context.client.send(context, "Память отсутствует.") | ||
} | ||
} | ||
|
||
private suspend fun add(context: ExecutorContext) { | ||
context.client.send(context, "Напиши что добавить в ответ на это сообщение, 300 символов максимум") | ||
} | ||
|
||
private suspend fun clear(context: ExecutorContext) { | ||
if (context.client.isFromAdmin(context)) { | ||
val memory = easyKeyValueService.getAndRemove(ChatGPTMemory, context.chatKey) | ||
if (memory != null) { | ||
context.client.send(context, "Память удалена. Вот на память бэкап: $memory") | ||
} else { | ||
context.client.send(context, "Нечего чистить, памяти нет.") | ||
} | ||
} else { | ||
context.client.send(context, "Чистить память можно только админам") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters