diff --git a/src/bot/index.js b/src/bot/index.js index 5f08c29a..d96f8a7c 100644 --- a/src/bot/index.js +++ b/src/bot/index.js @@ -10,6 +10,7 @@ import { getRTK } from '../utils/rtk.js'; import { isDuplicate, markProcessed } from './deduplication.js'; import { queueRequest, clearQueue, isProcessing } from './request-queue.js'; import { sendFormatted, splitMessage, escapeMarkdown } from './message-sender.js'; +import { withSelfCorrection } from './self-correction.js'; function buildSessionKey(chatId, threadId) { return threadId ? `${chatId}:${threadId}` : String(chatId); @@ -314,7 +315,10 @@ export async function initBot(config, api, tools, skills, agents) { 'šŸ“š *Skills:* ' + svc.skills.length + ' loaded', 'šŸ¤– *Agents:* ' + svc.agents.length + ' available', '', - 'šŸ“‹ *Commands:* /tools /skills /agents /model /stats /voice /mcp /memory /cron /cancel', + 'šŸ”„ *Self-Correction*: 2 retries + auto-simplification', + '⚔ *Streaming*: Real-time text delivery', + '', + 'šŸ“‹ *Commands:* /tools /skills /agents /model /stats /voice /mcp /memory /cron /cancel /selfcorrection', '', 'Or just chat — I will use tools when needed.', `Model: \`${svc.config?.api?.models?.default || 'glm-5.1'}\``, @@ -349,6 +353,10 @@ export async function initBot(config, api, tools, skills, agents) { } await sendStreamingMessage(ctx, lines.join('\n')); }); + + bot.command('selfcorrection', async (ctx) => { + await sendStreamingMessage(ctx, `šŸ”„ *Self-Correction Loops* — FULLY ENABLED\n\nzCode CLI X now uses automatic self-correction:\n\n• **Max Retries**: 2 attempts\n• **Retry Delay**: 500ms → 1s → 1.5s (exponential backoff)\n• **Triggers**: \n - āŒ Error responses\n - Rate limits\n - Timeouts\n - 5xx server errors\n\n• **Auto-Simplification**: On retry, prompts are simplified to avoid recurring errors\n\n• **Logging**: All retries are logged with retry count and reason\n\nThis ensures robust responses even when the AI initially fails.`); + }); bot.command('model', async (ctx) => { const text = ctx.match?.trim(); @@ -427,7 +435,7 @@ export async function initBot(config, api, tools, skills, agents) { }); } - // ── Message text handler (with dedup + queue) ── + // ── Message text handler (with dedup + queue + self-correction) ── bot.on('message:text', async (ctx) => { if (isDuplicate(ctx.message.message_id)) return; markProcessed(ctx.message.message_id); @@ -439,7 +447,13 @@ export async function initBot(config, api, tools, skills, agents) { await queueRequest(key, text, async () => { await ctx.api.sendChatAction(ctx.chat.id, 'typing'); - const result = await chatWithAI([ + + // Wrap chatWithAI with self-correction + const chatWithCorrection = withSelfCorrection(async (msgs) => { + return await chatWithAI(msgs, {}); + }); + + const result = await chatWithCorrection([ { role: 'system', content: buildSystemPrompt(svc) }, { role: 'user', content: text }, ]);