fix: Telegram messages truncated when MarkdownV2 edit fails (#236)
editMessage() had no fallback for MarkdownV2 failures (unlike sendMessage which already falls back to plain text). When the agent generates markdown tables or other complex formatting, the MarkdownV2 conversion can fail mid-stream, silently leaving the user with whatever the last successful streaming edit was -- a truncated message. Three fixes: - editMessage() now mirrors sendMessage's try/catch with plain-text fallback - Final send retry no longer guarded by !messageId, so failed edits fall back to sending a new complete message - Streaming edit errors are logged instead of silently swallowed Written by Cameron ◯ Letta Code "If you want to go fast, go alone. If you want to go far, go together." - African Proverb
This commit is contained in:
@@ -501,8 +501,14 @@ export class TelegramAdapter implements ChannelAdapter {
|
||||
|
||||
async editMessage(chatId: string, messageId: string, text: string): Promise<void> {
|
||||
const { markdownToTelegramV2 } = await import('./telegram-format.js');
|
||||
try {
|
||||
const formatted = await markdownToTelegramV2(text);
|
||||
await this.bot.api.editMessageText(chatId, Number(messageId), formatted, { parse_mode: 'MarkdownV2' });
|
||||
} catch (e) {
|
||||
// If MarkdownV2 fails, fall back to plain text (mirrors sendMessage fallback)
|
||||
console.warn('[Telegram] MarkdownV2 edit failed, falling back to raw text:', e);
|
||||
await this.bot.api.editMessageText(chatId, Number(messageId), text);
|
||||
}
|
||||
}
|
||||
|
||||
async addReaction(chatId: string, messageId: string, emoji: string): Promise<void> {
|
||||
|
||||
@@ -633,8 +633,10 @@ export class LettaBot implements AgentSession {
|
||||
messageId = result.messageId;
|
||||
sentAnyMessage = true;
|
||||
}
|
||||
} catch {
|
||||
// Ignore edit errors (e.g. rate limits)
|
||||
} catch (editErr) {
|
||||
// Log but don't fail - streaming edits are best-effort
|
||||
// (e.g. rate limits, MarkdownV2 formatting issues mid-stream)
|
||||
console.warn('[Bot] Streaming edit failed:', editErr instanceof Error ? editErr.message : editErr);
|
||||
}
|
||||
lastUpdate = Date.now();
|
||||
}
|
||||
@@ -729,7 +731,8 @@ export class LettaBot implements AgentSession {
|
||||
console.log(`[Bot] Sent: "${preview}"`);
|
||||
} catch (sendError) {
|
||||
console.error('[Bot] Error sending response:', sendError);
|
||||
if (!messageId) {
|
||||
// If edit failed (messageId exists), send the complete response as a new message
|
||||
// so the user isn't left with a truncated streaming edit
|
||||
try {
|
||||
await adapter.sendMessage({ chatId: msg.chatId, text: response, threadId: msg.threadId });
|
||||
sentAnyMessage = true;
|
||||
@@ -740,7 +743,6 @@ export class LettaBot implements AgentSession {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Only show "no response" if we never sent anything
|
||||
if (!sentAnyMessage) {
|
||||
|
||||
Reference in New Issue
Block a user