fix(adapters): ensure placeholder cleanup on message_complete

When all buffered text has already been flushed via periodic timer,
buf.complete() calls flush(true) which returns early on empty buffer,
skipping the onFlush callback that cleans up placeholder state.
This causes subsequent messages to keep editing the old completed
message instead of creating a new one.

Add explicit placeholder cleanup in message_complete handler as
a safety net, matching the same pattern used for tool_use finalization.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
程序员阿江(Relakkes) 2026-04-09 20:52:00 +08:00
parent 824b1967ff
commit b4ad6f15de
2 changed files with 26 additions and 0 deletions

View File

@ -333,6 +333,16 @@ async function handleServerMessage(chatId: string, msg: ServerMessage): Promise<
case 'message_complete':
await buf.complete()
// Ensure state is always cleaned up even if buffer was already empty
if (state.replyMessageId) {
const text = accumulatedText.get(chatId)
if (text?.trim()) {
await patchMessage(state.replyMessageId, text)
}
accumulatedText.delete(chatId)
chatStates.delete(chatId)
buffers.get(chatId)?.reset()
}
break
case 'error':

View File

@ -236,6 +236,22 @@ async function handleServerMessage(chatId: string, msg: ServerMessage): Promise<
case 'message_complete':
await buf.complete()
// Ensure placeholder is always cleaned up even if buffer was already empty
if (placeholders.has(chatId)) {
const text = accumulatedText.get(chatId)
if (text?.trim()) {
try {
const chunks = splitMessage(text, TELEGRAM_TEXT_LIMIT)
await bot.api.editMessageText(numericChatId, placeholders.get(chatId)!.messageId, chunks[0]!)
for (let i = 1; i < chunks.length; i++) {
await bot.api.sendMessage(numericChatId, chunks[i]!)
}
} catch { /* ignore */ }
}
placeholders.delete(chatId)
accumulatedText.delete(chatId)
buffers.get(chatId)?.reset()
}
break
case 'error':