From 5e75482ec11899229e428ae0853c87e012fd2fc4 Mon Sep 17 00:00:00 2001 From: AskIt Date: Mon, 26 Jan 2026 05:23:43 +0100 Subject: [PATCH] feat(bots): Add initial post trigger and improve scheduler logging - Add initial post trigger on bot creation to establish schedule reference point - Implement background post creation in scheduled bot processor using triggerPost - Add fresh content fetching before checking availability in scheduler - Enhance scheduler logging with detailed skip reasons and error reporting - Include autonomous bot skip reasons and scheduled bot status details in logs - Add error tracking and reporting for failed bot post attempts - Improve debugging visibility for bot task execution and failures --- src/app/settings/bots/new/page.tsx | 10 ++++++++++ src/lib/background/scheduler.ts | 27 +++++++++++++++++++++++++ src/lib/bots/scheduler.ts | 32 ++++++++++++++++++++++-------- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/app/settings/bots/new/page.tsx b/src/app/settings/bots/new/page.tsx index 9a0524c..7110b57 100644 --- a/src/app/settings/bots/new/page.tsx +++ b/src/app/settings/bots/new/page.tsx @@ -232,6 +232,16 @@ export default function NewBotPage() { body: JSON.stringify(sourcePayload), }); } + + // Trigger the first post to establish a reference point for the schedule + // This runs in the background - we don't wait for it + fetch(`/api/bots/${data.bot.id}/post`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({}), + }).catch(() => { + // Silently fail - the bot will post on the next scheduled run + }); } router.push(`/settings/bots/${data.bot.id}`); diff --git a/src/lib/background/scheduler.ts b/src/lib/background/scheduler.ts index 2d26afe..4397138 100644 --- a/src/lib/background/scheduler.ts +++ b/src/lib/background/scheduler.ts @@ -34,8 +34,35 @@ async function runBotTasks() { const autonomousResult = await processAllAutonomousBots(); const posted = autonomousResult.filter(r => r.result.posted).length; + const skipped = scheduledResult.skipped; + const errors = scheduledResult.errors.length + autonomousResult.filter(r => r.error).length; + + // Always log bot task results for debugging if (scheduledResult.processed > 0 || posted > 0) { log('BOTS', `Processed ${scheduledResult.processed} scheduled, ${posted} autonomous posts`); + } else if (scheduledResult.details.length > 0 || autonomousResult.length > 0) { + // Log why bots didn't post + const reasons = scheduledResult.details + .filter(d => d.status !== 'posted') + .map(d => `${d.botId.slice(0, 8)}: ${d.status}${d.message ? ` (${d.message})` : ''}`) + .slice(0, 3); + + const autoReasons = autonomousResult + .filter(r => !r.result.posted) + .map(r => `${r.botHandle}: ${r.result.reason || r.error || 'unknown'}`) + .slice(0, 3); + + if (reasons.length > 0 || autoReasons.length > 0) { + log('BOTS', `No posts created. Scheduled: ${scheduledResult.details.length} checked, ${skipped} skipped. Autonomous: ${autonomousResult.length} checked.`); + if (reasons.length > 0) log('BOTS', `Scheduled skip reasons: ${reasons.join('; ')}`); + if (autoReasons.length > 0) log('BOTS', `Autonomous skip reasons: ${autoReasons.join('; ')}`); + } + } else { + log('BOTS', 'No active bots found'); + } + + if (errors > 0) { + log('BOTS', `Errors: ${scheduledResult.errors.join('; ')}`); } } catch (error) { log('BOTS', `Error: ${error}`); diff --git a/src/lib/bots/scheduler.ts b/src/lib/bots/scheduler.ts index aadfaf7..8d176d4 100644 --- a/src/lib/bots/scheduler.ts +++ b/src/lib/bots/scheduler.ts @@ -10,6 +10,8 @@ import { db, bots, botContentSources, botContentItems } from '@/db'; import { eq, and } from 'drizzle-orm'; import { canPost } from './rateLimiter'; +import { triggerPost } from './posting'; +import { fetchAllSourcesForBot } from './contentFetcher'; // ============================================ // TYPES @@ -833,6 +835,9 @@ export async function processScheduledPosts(): Promise