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
This commit is contained in:
@@ -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);
|
||||
const msg = choice.message;
|
||||
if (msg.tool_calls?.length) {
|
||||
const parts = [];
|
||||
for (const tc of msg.tool_calls) {
|
||||
const fn = tc.function;
|
||||
try {
|
||||
const parsed = JSON.parse(data);
|
||||
const content = parsed.choices?.[0]?.delta?.content;
|
||||
if (content) {
|
||||
fullText += content;
|
||||
}
|
||||
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) {
|
||||
// Skip parse errors for invalid JSON chunks
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user