Adds history command

This commit is contained in:
Samuele Lorefice
2024-12-27 18:00:58 +01:00
parent 773203127f
commit ac63019fe6

View File

@@ -92,17 +92,7 @@ async Task OnKroMessage(Message msg, UpdateType type) {
//TODO: currently we only take in account private messages and messages directed to the bot/mentioning them.
// We should also take in account the last x messages in groups to add more context
async Task OnMessage(Message msg, Agent agent) {
//Check if the message contains the bot's username or a reply to a message sent by the bot or a private chat
var chatid = msg.Chat.Id;
var tokenlenght = oaiAgent.GetTokenLenght(msg.Text);
Console.WriteLine(
$"""
{agent.Name} has received message from {chatid} TokenLenght: {tokenlenght}
Message: {msg.Text}
""");
//Add the message to the chat history
oaiAgent.ChatHistoryAppend(Actor.User, chatid, "User: " + msg.Text);
//Check if the message is a reset command
if (msg.Text == "/reset" || msg.Text == "/reset@" + agent.Username) {
@@ -111,6 +101,26 @@ async Task OnMessage(Message msg, Agent agent) {
return;
}
if (msg.Text == "/history @"+agent.Username) {
var history = oaiAgent.GetChatHistory(chatid, agent.Actor);
var historyText = string.Join("\n", history.Select(x => x.Item1.Content[0].Text));
await agent.Bot.SendMessage(chatid, historyText);
return;
}
var text = $"{msg.From?.FirstName} {msg.From?.LastName}: {msg.Text}";
var tokenlenght = oaiAgent.GetTokenLenght(msg.Text!);
Console.WriteLine(
$"""
{agent.Name} has received message from {chatid} TokenLenght: {tokenlenght}
Message: {msg.Text}
""");
//Add the message to the chat history
oaiAgent.ChatHistoryAppend(Actor.User, chatid, text);
//Check if the message contains the bot's username or a reply to a message sent by the bot or a private chat
// Otherwise process it normally
if (msg.Text!.Contains(agent.Name, StringComparison.OrdinalIgnoreCase) ||
msg.ReplyToMessage?.From?.Id == agent.TelegramId ||
@@ -125,7 +135,6 @@ async Task OnMessage(Message msg, Agent agent) {
}
}
async Task AnswerChat(long chatId, Agent agent) {
//Get the response from the OpenAI API
var result = oaiAgent.GetChatResponse(chatId, agent);