fix: restore SSE streaming via StreamConsumer for text messages
This commit is contained in:
@@ -675,24 +675,31 @@ export async function initBot(config, api, tools, skills, agents) {
|
|||||||
|
|
||||||
await queueRequest(key, text, async () => {
|
await queueRequest(key, text, async () => {
|
||||||
await ctx.api.sendChatAction(ctx.chat.id, 'typing');
|
await ctx.api.sendChatAction(ctx.chat.id, 'typing');
|
||||||
|
|
||||||
// Use self-correction + direct send (streaming disabled for reliability)
|
// Create stream consumer for real-time edit-in-place
|
||||||
|
const consumer = new StreamConsumer(ctx, { editInterval: 1000 });
|
||||||
|
const runPromise = consumer.run();
|
||||||
|
|
||||||
|
// Wrap chatWithAI with self-correction + streaming
|
||||||
const chatWithCorrection = withSelfCorrection(async (msgs) => {
|
const chatWithCorrection = withSelfCorrection(async (msgs) => {
|
||||||
return await chatWithAI(msgs);
|
return await chatWithAI(msgs, { onDelta: (token) => consumer.onDelta(token) });
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await chatWithCorrection([
|
const result = await chatWithCorrection([
|
||||||
{ role: 'system', content: buildSystemPrompt(svc) },
|
{ role: 'system', content: buildSystemPrompt(svc) },
|
||||||
{ role: 'user', content: text },
|
{ role: 'user', content: text },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Send formatted response
|
// Signal completion and wait for final edit
|
||||||
if (result) {
|
consumer.finish();
|
||||||
logger.info(`📤 Sending response (${result.length} chars)`);
|
await runPromise;
|
||||||
|
|
||||||
|
// If streaming failed to deliver (no message sent), fallback to plain send
|
||||||
|
if (!consumer.alreadySent && result) {
|
||||||
|
logger.info(`📤 Streaming failed, fallback send (${result.length} chars)`);
|
||||||
await sendFormatted(ctx, result);
|
await sendFormatted(ctx, result);
|
||||||
logger.info('✅ Response delivered');
|
|
||||||
} else {
|
} else {
|
||||||
logger.warn('Empty response from AI — nothing to send');
|
logger.info('✅ Stream delivered');
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Self-learning: extract patterns from this interaction ──
|
// ── Self-learning: extract patterns from this interaction ──
|
||||||
|
|||||||
Reference in New Issue
Block a user