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:
admin
2026-05-05 13:18:32 +00:00
Unverified
parent 48011b2ca6
commit e658842458
2 changed files with 82 additions and 28 deletions

View File

@@ -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);
}
}