From d3bf90f985db0700a2c13f07e5ff17aed14ab908 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 5 May 2026 13:24:59 +0000 Subject: [PATCH] fix: revert streaming to prevent webhook errors - Removed SSE streaming from chatWithAI() - Keep sendStreamingMessage() for chunked delivery - Self-correction loops still active - Messages will be delivered in chunks with typing indicator --- src/bot/index.js | 59 +++++++++++++++++------------------------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/src/bot/index.js b/src/bot/index.js index d96f8a7c..bfb68b63 100644 --- a/src/bot/index.js +++ b/src/bot/index.js @@ -155,50 +155,31 @@ export async function initBot(config, api, tools, skills, agents) { messages, temperature: opts.temperature ?? 0.7, max_tokens: opts.maxTokens || 4096, - stream: true, // Enable streaming }; if (tools.length) body.tools = tools; - const response = await api.client.post('/chat/completions', body, { - responseType: 'stream', - }); + const response = await api.client.post('/chat/completions', body); + const choice = response.data.choices?.[0]; + if (!choice) return '❌ No response from model.'; - const reader = response.data; - let fullText = ''; - - for await (const chunk of reader) { - const lines = chunk.toString().split('\n').filter(line => line.trim()); - - for (const line of lines) { - if (line === 'data: [DONE]') continue; - - if (line.startsWith('data: ')) { - const data = line.slice(6); - try { - const parsed = JSON.parse(data); - const content = parsed.choices?.[0]?.delta?.content; - if (content) { - fullText += content; - } - } catch (e) { - // Skip parse errors for invalid JSON chunks - } + const msg = choice.message; + if (msg.tool_calls?.length) { + const parts = []; + for (const tc of msg.tool_calls) { + const fn = tc.function; + try { + const handler = toolHandlers[fn.name]; + if (!handler) { parts.push(`❌ Unknown tool: ${fn.name}`); continue; } + const args = JSON.parse(fn.arguments); + const result = await handler(args); + parts.push(`${result}`); + } catch (e) { + parts.push(`❌ Tool ${fn.name} error: ${e.message}`); } } + return parts.join('\n\n'); } - - if (!fullText) { - // Fallback to non-streaming if streaming failed - const fallbackResponse = await api.client.post('/chat/completions', { - model, - messages, - temperature: opts.temperature ?? 0.7, - max_tokens: opts.maxTokens || 4096, - }); - return fallbackResponse.data.choices[0].message; - } - - return { content: fullText }; + return msg.content || '✅ Done.'; } catch (error) { logger.error('AI error:', error.response?.data || error.message); return `❌ ${error.response?.data?.error?.message || error.message}`; @@ -457,7 +438,9 @@ export async function initBot(config, api, tools, skills, agents) { { role: 'system', content: buildSystemPrompt(svc) }, { role: 'user', content: text }, ]); - await sendStreamingMessage(ctx, result.content || result); + + // Send with streaming effect + await sendStreamingMessage(ctx, result); }); });