#18764: fix(telegram): preserve streamed draft when tool error occurs
channel: telegram
stale
size: S
Cluster:
Telegram Streaming Enhancements
## Summary
Fixes #18012.
When a tool execution fails during Telegram streaming (streamMode `partial` or `block`), the draft preview message containing partial content the user already saw gets deleted and replaced by the error message. Users lose valuable context.
## Root Cause
1. Error payloads correctly skip the preview-edit path (`!payload.isError` guard already existed)
2. Because the preview isn't finalized, `finalizedViaPreviewMessage` stays `false`
3. The `finally` block unconditionally calls `draftStream.clear()`, which deletes the draft message
## Fix
- Added a `preserveDraft` flag
- When the final payload is an error AND the draft already has streamed content (`hasStreamedMessage` + valid `messageId`), set `preserveDraft = true` and call `forceNewMessage()` so the error is delivered as a separate message
- The `finally` block now skips `clear()` when `preserveDraft` is set
## Result
- Users keep the useful partial content they already saw
- The error is delivered as a new message below the draft
- When no content was streamed before the error, the draft is still cleared normally (no empty ghost messages)
## Tests
- `preserves draft message when error occurs after streaming content (#18012)` — verifies draft is NOT cleared and error is sent separately
- `still clears draft on error when no content was streamed` — verifies normal cleanup when there's nothing to preserve
- All 13 existing tests pass
Most Similar PRs
#18678: fix(telegram): preserve draft message when all final payloads are e...
by julianubico · 2026-02-16
80.4%
#20842: fix(telegram): preserve preview when only error payloads are delivered
by marcodelpin · 2026-02-19
78.3%
#18460: fix(telegram): send fallback when streamMode partial drops all mess...
by BinHPdev · 2026-02-16
74.8%
#19479: fix(telegram): skip redundant final edit in partial streaming mode
by v8hid · 2026-02-17
74.3%
#19673: fix(telegram): avoid starting streaming replies with only 1-2 words
by emanuelst · 2026-02-18
71.4%
#19665: feat(telegram): native sendMessageDraft streaming (Bot API 9.3)
by edonadei · 2026-02-18
71.2%
#5764: fix(telegram): enable streaming in private chats without topics
by garnetlyx · 2026-01-31
69.4%
#17953: fix(telegram): prevent silent message loss and duplicate messages i...
by zuyan9 · 2026-02-16
68.7%
#19235: fix(telegram): tool error warnings no longer overwrite streamed rep...
by gatewaybuddy · 2026-02-17
68.2%
#23118: fix(slack): await draft stream flush before messageId check
by dashed · 2026-02-22
67.7%