feat: implement real-time message editing streaming (like OpenClaw)
This commit is contained in:
@@ -46,44 +46,52 @@ export async function sendStreamingMessage(ctx, text, options = {}) {
|
||||
if (!text) return;
|
||||
|
||||
const {
|
||||
delay = 100,
|
||||
minDelay = 50,
|
||||
maxDelay = 250,
|
||||
charMode = false // Set to true for character-by-character, false for word-by-word
|
||||
delay = 80,
|
||||
minDelay = 40,
|
||||
maxDelay = 150,
|
||||
charMode = false // Set to true for character-by-character
|
||||
} = options;
|
||||
|
||||
try {
|
||||
// Send initial "typing..." indicator
|
||||
const sendTyping = () => ctx.api.sendChatAction(ctx.chat.id, 'typing').catch(() => {});
|
||||
// Send initial placeholder message
|
||||
const sentMsg = await ctx.reply('⌨️ Typing...', { parse_mode: 'Markdown' });
|
||||
|
||||
let sentText = '';
|
||||
|
||||
if (charMode) {
|
||||
// Character-by-character streaming
|
||||
let sentText = '';
|
||||
const chars = text.split('');
|
||||
|
||||
for (const char of chars) {
|
||||
sentText += char;
|
||||
|
||||
// Send message with the accumulated text
|
||||
await ctx.reply(sentText, { parse_mode: 'Markdown' });
|
||||
// Edit the message in place
|
||||
await ctx.api.editMessageText(sentText, {
|
||||
message_id: sentMsg.message_id,
|
||||
chat_id: sentMsg.chat.id,
|
||||
parse_mode: 'Markdown'
|
||||
});
|
||||
|
||||
// Random delay between min and max
|
||||
const delayMs = minDelay + Math.random() * (maxDelay - minDelay);
|
||||
await new Promise(resolve => setTimeout(resolve, delayMs));
|
||||
}
|
||||
} else {
|
||||
// Word-by-word streaming (default)
|
||||
// Word-by-word streaming
|
||||
const words = text.split(' ');
|
||||
let sentText = '';
|
||||
|
||||
for (let i = 0; i < words.length; i++) {
|
||||
sentText += (i > 0 ? ' ' : '') + words[i];
|
||||
|
||||
// Send accumulated text
|
||||
await ctx.reply(sentText, { parse_mode: 'Markdown' });
|
||||
// Edit the message in place
|
||||
await ctx.api.editMessageText(sentText, {
|
||||
message_id: sentMsg.message_id,
|
||||
chat_id: sentMsg.chat.id,
|
||||
parse_mode: 'Markdown'
|
||||
});
|
||||
|
||||
// Variable delay based on word length
|
||||
const wordDelay = Math.max(minDelay, Math.min(maxDelay, delay + words[i].length * 10));
|
||||
const wordDelay = Math.max(minDelay, Math.min(maxDelay, delay + words[i].length * 8));
|
||||
await new Promise(resolve => setTimeout(resolve, wordDelay));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user