This commit is contained in:
HugeFrog24
2026-05-25 23:39:40 +02:00
parent 7c74d91bbb
commit 4fc9d8a5c5
6 changed files with 144 additions and 101 deletions
+15 -32
View File
@@ -449,41 +449,24 @@ 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
// sendOneSegment delivers a single Telegram message without touching storage
// or chat memory. Used by the streaming response path: each completed text
// block fires this helper as it arrives, and the full turn is recorded once
// at end-of-stream via screenOutgoingMessage. Keeps the 1-reply-per-prompt
// storage invariant while letting the user see segments with natural rhythm.
func (b *Bot) sendOneSegment(ctx context.Context, chatID int64, text, businessConnectionID string) error {
params := &bot.SendMessageParams{
ChatID: chatID,
Text: text,
}
fullText := strings.Join(segments, "\n\n")
if _, err := b.screenOutgoingMessage(chatID, fullText); err != nil {
ErrorLogger.Printf("Error storing assistant message: %v", err)
if businessConnectionID != "" {
params.BusinessConnectionID = businessConnectionID
}
if _, err := b.tgBot.SendMessage(ctx, params); err != nil {
ErrorLogger.Printf("[%s] Error sending segment to chat %d with BusinessConnectionID %s: %v",
b.config.ID, chatID, businessConnectionID, 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
}