From ca005a93707f7345d9d23455a9168f5bbf92babd Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Wed, 13 Mar 2024 00:32:52 +0300 Subject: [PATCH] Extracting request context creation to separate file. --- bot/bot.go | 33 ------------------------ bot/request_context.go | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 bot/request_context.go diff --git a/bot/bot.go b/bot/bot.go index f741a21..eaac998 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -257,39 +257,6 @@ func (b *Bot) statsHandler(bot *telego.Bot, update telego.Update) { } } -func (b *Bot) createLlmRequestContext(update telego.Update) llm.RequestContext { - message := update.Message - - rc := llm.RequestContext{} - - if message == nil { - slog.Debug("request context creation problem: no message provided. returning empty context.", "request-context", rc) - - return rc - } - - user := message.From - if user != nil { - rc.User = llm.UserContext{ - Username: user.Username, - FirstName: user.FirstName, - LastName: user.LastName, - IsPremium: user.IsPremium, - } - } - - chat := message.Chat - rc.Chat = llm.ChatContext{ - Title: chat.Title, - Description: chat.Description, - Type: chat.Type, - } - - slog.Debug("request context created", "request-context", rc) - - return rc -} - func (b *Bot) escapeMarkdownV1Symbols(input string) string { return b.markdownV1Replacer.Replace(input) } diff --git a/bot/request_context.go b/bot/request_context.go new file mode 100644 index 0000000..6c155ae --- /dev/null +++ b/bot/request_context.go @@ -0,0 +1,58 @@ +package bot + +import ( + "github.com/mymmrac/telego" + "log/slog" + "telegram-ollama-reply-bot/llm" +) + +func (b *Bot) createLlmRequestContext(update telego.Update) llm.RequestContext { + message := update.Message + iq := update.InlineQuery + + rc := llm.RequestContext{ + Empty: true, + Inline: false, + } + + switch { + case message == nil && iq == nil: + slog.Debug("request context creation problem: no message provided. returning empty context.", "request-context", rc) + + return rc + case iq != nil: + rc.Inline = true + } + + rc.Empty = false + + var user *telego.User + + if rc.Inline { + user = &iq.From + } else { + user = message.From + } + + if user != nil { + rc.User = llm.UserContext{ + Username: user.Username, + FirstName: user.FirstName, + LastName: user.LastName, + IsPremium: user.IsPremium, + } + } + + if !rc.Inline { + chat := message.Chat + rc.Chat = llm.ChatContext{ + Title: chat.Title, + Description: chat.Description, + Type: chat.Type, + } + } + + slog.Debug("request context created", "request-context", rc) + + return rc +}