From c04ad154c505487523e57c337661004f9b099d54 Mon Sep 17 00:00:00 2001 From: davidramiro Date: Tue, 26 Nov 2024 13:10:54 +0100 Subject: [PATCH] feat: use quoted message as context for chat handler --- internal/adapters/handler/command.go | 3 +++ internal/core/domain/commands/chat.go | 20 ++++++++------------ internal/core/domain/model.go | 1 + 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/internal/adapters/handler/command.go b/internal/adapters/handler/command.go index e9a20df..dd251b3 100644 --- a/internal/adapters/handler/command.go +++ b/internal/adapters/handler/command.go @@ -34,8 +34,10 @@ func (h *CommandHandler) Handle(ctx context.Context, b *bot.Bot, update *models. } replyToMessageID := new(int) + var quotedText string if update.Message.ReplyToMessage != nil { *replyToMessageID = update.Message.ReplyToMessage.ID + quotedText = update.Message.ReplyToMessage.Text } imageURL := make(chan string) @@ -50,6 +52,7 @@ func (h *CommandHandler) Handle(ctx context.Context, b *bot.Bot, update *models. ChatID: update.Message.Chat.ID, Text: update.Message.Text, ReplyToMessageID: replyToMessageID, + QuotedText: quotedText, ImageURL: <-imageURL, AudioURL: <-audioURL, }) diff --git a/internal/core/domain/commands/chat.go b/internal/core/domain/commands/chat.go index 1dc4a24..20eaaab 100644 --- a/internal/core/domain/commands/chat.go +++ b/internal/core/domain/commands/chat.go @@ -30,6 +30,7 @@ func NewChatHandler(textGenerator port.TextGenerator, textSender port.TextSender textGenerator: textGenerator, textSender: textSender, command: command, + cache: make(map[int64]*Conversation), } go h.clearCache(cacheDuration, tickRate) @@ -69,19 +70,14 @@ func (h *ChatHandler) Respond(ctx context.Context, timeout time.Duration, messag conversation, ok := h.cache[message.ChatID] if !ok { l.Debug().Msg("new conversation") - h.cache = make(map[int64]*Conversation) - - h.cache[message.ChatID] = &Conversation{ - messages: []domain.Prompt{ - { - Author: domain.User, - Prompt: promptText, - ImageURL: message.ImageURL, - }, - }, - } + + h.cache[message.ChatID] = &Conversation{} conversation = h.cache[message.ChatID] - l.Debug().Int("message cache size", len(h.cache[message.ChatID].messages)).Send() + } + + if message.QuotedText != "" && message.ImageURL == "" { + conversation.messages = append(conversation.messages, domain.Prompt{Author: domain.User, + Prompt: fmt.Sprintf("%s:%s", promptText, message.QuotedText)}) } else { conversation.messages = append(conversation.messages, domain.Prompt{Author: domain.User, Prompt: promptText, ImageURL: message.ImageURL}) diff --git a/internal/core/domain/model.go b/internal/core/domain/model.go index 4f7b8b1..141e0d9 100644 --- a/internal/core/domain/model.go +++ b/internal/core/domain/model.go @@ -17,6 +17,7 @@ type Message struct { ID int ChatID int64 ReplyToMessageID *int + QuotedText string ImageURL string AudioURL string Text string