Skip to content

Commit

Permalink
feat: use quoted message as context for chat handler
Browse files Browse the repository at this point in the history
  • Loading branch information
davidramiro committed Nov 26, 2024
1 parent 5108032 commit c04ad15
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
3 changes: 3 additions & 0 deletions internal/adapters/handler/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
})
Expand Down
20 changes: 8 additions & 12 deletions internal/core/domain/commands/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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})
Expand Down
1 change: 1 addition & 0 deletions internal/core/domain/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Message struct {
ID int
ChatID int64
ReplyToMessageID *int
QuotedText string
ImageURL string
AudioURL string
Text string
Expand Down

0 comments on commit c04ad15

Please sign in to comment.