Files
archie/internal/channels/telegram/draft.go
T
sam 3e3911a423 feat(telegram): native rich messages + streamed replies (16.32)
Upgrades go-telegram/bot v1.18.0 -> v1.22.0 and uses the Bot API's rich
message support instead of hand-rolled formatting.

The previous commit converted LLM Markdown into Telegram's HTML subset,
on the premise that the API could not take Markdown directly and had no
table element. Both are true of v1.18.0 but not of v1.22.0, which adds
sendRichMessage/sendRichMessageDraft taking InputRichMessage{Markdown},
plus RichBlockTableCell for native tables. markdownToHTML and its tests
are deleted: LLM Markdown, tables included, now goes to Telegram as-is.

Replies also stream. runtime.ChatStream already returned a TextStream
and sendRichMessageDraft renders partial output, so the only missing
piece was a gateway seam: gateway.LLMStreamResponder plus Router.
RouteStream, which degrades to the blocking Route when unset so any
adapter can call it unconditionally. Local commands answer from local
state and are not streamed; an unrecognised /command is, since Route
hands it to the LLM. main.go derives both responders from one shared
turn implementation.

Draft updates are throttled to one per 700ms to respect per-chat rate
limits, and every streaming/rich path falls back rather than dropping a
reply: a rejected rich send retries unformatted, and a failed draft
update is skipped since the final message is authoritative.
2026-07-27 04:57:36 +10:00

83 lines
2.6 KiB
Go

package telegram
import (
"context"
"strings"
"sync"
"time"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
// draftInterval is the minimum gap between draft updates. Telegram rate
// limits per-chat writes, and an LLM emits tokens far faster than any
// human reads, so deltas are coalesced into one update per tick rather
// than sent individually.
const draftInterval = 700 * time.Millisecond
// draft streams a reply into a Telegram message draft (sendMessageDraft)
// so the user watches the answer being written instead of waiting on a
// silent "typing…" indicator.
//
// A draft is deliberately best-effort and self-contained: it never blocks
// the generating goroutine and never surfaces an error. The authoritative
// reply is always the final SendMessage the caller makes once the turn
// completes -- if every draft update failed, the user simply sees the
// finished message appear the way it did before streaming existed.
type draft struct {
g *Gateway
b *bot.Bot
chatID int64
messageThreadID int
draftID int
mu sync.Mutex
text strings.Builder
last time.Time
}
// newDraft creates a draft bound to one chat/thread. The draft ID only has
// to be unique per streaming session.
func (g *Gateway) newDraft(b *bot.Bot, chatID int64, messageThreadID int) *draft {
return &draft{
g: g,
b: b,
chatID: chatID,
messageThreadID: messageThreadID,
draftID: int(time.Now().UnixNano() & 0x7fffffff),
}
}
// onDelta accumulates a fragment and pushes a throttled draft update.
// It satisfies the onDelta callback of gateway.Router.RouteStream.
func (d *draft) onDelta(delta string) {
if delta == "" {
return
}
d.mu.Lock()
d.text.WriteString(delta)
if time.Since(d.last) < draftInterval {
d.mu.Unlock()
return
}
d.last = time.Now()
// A block cursor marks the reply as still being written.
body := d.text.String() + " ▌"
d.mu.Unlock()
// Stream as a rich message so partial Markdown renders the same way
// the final reply will. Mid-stream text is frequently unbalanced (an
// unclosed ** or code fence); a failed draft update is logged and
// skipped, and the next tick -- or the final message -- corrects it.
if _, err := d.b.SendRichMessageDraft(context.Background(), &bot.SendRichMessageDraftParams{
ChatID: d.chatID,
MessageThreadID: d.messageThreadID,
DraftID: d.draftID,
RichMessage: models.InputRichMessage{Markdown: body},
}); err != nil {
d.g.log.Debug("send rich message draft failed", "error", err)
}
}