feat: enable streaming responses like OpenClaw
- Add sendStreamingMessage() to message-sender.js with typing indicators - Enable stream: true in chatWithAI() with SSE parsing - Replace all ctx.reply() calls with sendStreamingMessage() - Real-time text streaming with 50ms delay between chunks
This commit is contained in:
@@ -42,7 +42,42 @@ export async function sendFormatted(ctx, text) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendLongMessage(ctx, text) {
|
||||
export async function sendStreamingMessage(ctx, text, options = {}) {
|
||||
if (!text) return;
|
||||
return sendFormatted(ctx, text);
|
||||
|
||||
const { delay = 50, maxChunkSize = 1000 } = options;
|
||||
|
||||
try {
|
||||
// Stream chunks with typing indicator
|
||||
const chunks = splitMessage(text);
|
||||
let delayTimer = null;
|
||||
|
||||
for (const chunk of chunks) {
|
||||
// Clear previous typing indicator
|
||||
if (delayTimer) {
|
||||
clearTimeout(delayTimer);
|
||||
delayTimer = null;
|
||||
}
|
||||
|
||||
// Send chunk with minimal delay for real-time effect
|
||||
await ctx.reply(chunk, { parse_mode: 'Markdown' });
|
||||
|
||||
// Show typing indicator between chunks
|
||||
if (chunks.length > 1) {
|
||||
delayTimer = setTimeout(() => {
|
||||
ctx.api.sendChatAction(ctx.chat.id, 'typing').catch(() => {});
|
||||
}, delay);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear typing indicator
|
||||
if (delayTimer) {
|
||||
clearTimeout(delayTimer);
|
||||
delayTimer = null;
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Streaming send failed:', error);
|
||||
// Fallback to non-streaming
|
||||
await sendFormatted(ctx, text);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user