This commit is contained in:
HugeFrog24
2026-05-25 22:56:12 +02:00
parent 8c699ab70a
commit b22b8b98fe
4 changed files with 95 additions and 19 deletions
+38
View File
@@ -449,6 +449,44 @@ func (b *Bot) sendResponse(ctx context.Context, chatID int64, text string, busin
return nil
}
// sendMultiResponse delivers a multi-block LLM response as separate Telegram
// messages while keeping a single logical assistant turn in storage. The DB
// row and chat memory hold the joined text (segments separated by blank lines),
// so the model's next-turn context sees one assistant turn — matching today's
// 1-reply-per-prompt invariant — even though the user saw N bubbles.
//
// Partial send failures (a later segment fails after earlier ones succeeded)
// are logged but do not abort the remaining sends. The DB record is canonical:
// the model's next turn will reference what it intended to say.
func (b *Bot) sendMultiResponse(ctx context.Context, chatID int64, segments []string, businessConnectionID string) error {
if len(segments) == 0 {
return nil
}
fullText := strings.Join(segments, "\n\n")
if _, err := b.screenOutgoingMessage(chatID, fullText); err != nil {
ErrorLogger.Printf("Error storing assistant message: %v", err)
return err
}
for i, seg := range segments {
params := &bot.SendMessageParams{
ChatID: chatID,
Text: seg,
}
if businessConnectionID != "" {
params.BusinessConnectionID = businessConnectionID
}
if _, err := b.tgBot.SendMessage(ctx, params); err != nil {
ErrorLogger.Printf("[%s] Error sending segment %d/%d to chat %d with BusinessConnectionID %s: %v",
b.config.ID, i+1, len(segments), chatID, businessConnectionID, err)
// Keep going: earlier segments are already in the user's chat,
// and the DB has the full turn recorded.
}
}
return nil
}
// sendStats sends the bot statistics to the specified chat.
func (b *Bot) sendStats(ctx context.Context, chatID int64, userID int64, targetUserID int64, businessConnectionID string) {
// If targetUserID is 0, show global stats