← Back to PRs

#18764: fix(telegram): preserve streamed draft when tool error occurs

by stakeswky open 2026-02-17 01:47 View on GitHub →
channel: telegram stale size: S
## 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