diff --git a/memento-note/app/(main)/agents/agents-page-client.tsx b/memento-note/app/(main)/agents/agents-page-client.tsx index 83a79c1..2d4873c 100644 --- a/memento-note/app/(main)/agents/agents-page-client.tsx +++ b/memento-note/app/(main)/agents/agents-page-client.tsx @@ -90,44 +90,59 @@ export function AgentsPageClient({ } }, []) - // Track latest action per agent to detect new executions - // null = agent tracked with no initial actions, undefined = not tracked yet + // Track latest action ID per agent to detect new executions const prevActionsRef = useRef>({}) - // Poll every 30s to detect agent executions and show toast notifications - useEffect(() => { - // Initialize tracking from initial data — use null for agents without actions - // so we can still detect their FIRST execution - for (const agent of initialAgents) { - prevActionsRef.current[agent.id] = agent.actions[0]?.id ?? null - } + // Check for new actions and show toast notifications + const checkForNewActions = useCallback((updated: AgentItem[]) => { + for (const agent of updated) { + const lastAction = agent.actions[0] + if (!lastAction) continue - const interval = setInterval(async () => { - const updated = await refreshAgents() - if (!updated) return + const prevId = prevActionsRef.current[agent.id] + if (prevId === undefined) continue // Not tracked - for (const agent of updated) { - const lastAction = agent.actions[0] - if (!lastAction) continue - - const prevId = prevActionsRef.current[agent.id] - // undefined = agent not in initial list (created by someone else, skip) - if (prevId === undefined) continue - - if (prevId !== lastAction.id) { - // New execution detected (first action ever, or new run) + if (prevId !== lastAction.id) { + // Only toast for recently created actions (within 5 min) + const age = Date.now() - new Date(lastAction.createdAt).getTime() + if (age < 5 * 60 * 1000) { if (lastAction.status === 'success') { toast.success(t('agents.toasts.autoRunSuccess', { name: agent.name })) } else if (lastAction.status === 'failure') { toast.error(t('agents.toasts.autoRunError', { name: agent.name })) } } - prevActionsRef.current[agent.id] = lastAction.id } + prevActionsRef.current[agent.id] = lastAction.id + } + }, [t]) + + useEffect(() => { + // Initialize tracking from initial data + for (const agent of initialAgents) { + prevActionsRef.current[agent.id] = agent.actions[0]?.id ?? null + } + + // Interval polling every 30s + const interval = setInterval(async () => { + const updated = await refreshAgents() + if (updated) checkForNewActions(updated) }, 30000) - return () => clearInterval(interval) - }, []) + // Refresh immediately when user comes back to the tab + const onVisible = async () => { + if (document.visibilityState === 'visible') { + const updated = await refreshAgents() + if (updated) checkForNewActions(updated) + } + } + document.addEventListener('visibilitychange', onVisible) + + return () => { + clearInterval(interval) + document.removeEventListener('visibilitychange', onVisible) + } + }, [refreshAgents, checkForNewActions]) const handleToggle = useCallback((id: string, isEnabled: boolean) => { setAgents(prev => prev.map(a => a.id === id ? { ...a, isEnabled } : a)) diff --git a/memento-note/app/api/cron/agents/route.ts b/memento-note/app/api/cron/agents/route.ts index 8190ee2..73bb401 100644 --- a/memento-note/app/api/cron/agents/route.ts +++ b/memento-note/app/api/cron/agents/route.ts @@ -46,13 +46,14 @@ export async function POST(request: NextRequest) { return NextResponse.json({ success: true, executed: 0 }) } - console.log(`[CronAgents] Found ${dueAgents.length} due agent(s)`) + console.log(`[CronAgents] Found ${dueAgents.length} due agent(s): ${dueAgents.map(a => a.id).join(', ')}`) const results: { id: string; success: boolean; error?: string }[] = [] - // Execute agents sequentially (max 3 per cycle) to avoid overwhelming the AI provider + // Execute agents sequentially (max 3 per cycle) for (const agent of dueAgents.slice(0, 3)) { try { + console.log(`[CronAgents] Executing agent ${agent.id} (${agent.frequency})`) const { executeAgent } = await import('@/lib/ai/services/agent-executor.service') const result = await executeAgent(agent.id, agent.userId) @@ -64,6 +65,8 @@ export async function POST(request: NextRequest) { timezone: agent.timezone, }) + console.log(`[CronAgents] Agent ${agent.id} done. success=${result.success}, nextRun=${nextRun?.toISOString() ?? 'null'}`) + await prisma.agent.update({ where: { id: agent.id }, data: { nextRun }, @@ -73,7 +76,6 @@ export async function POST(request: NextRequest) { } catch (error) { const msg = error instanceof Error ? error.message : 'Unknown error' console.error(`[CronAgents] Agent ${agent.id} failed:`, msg) - results.push({ id: agent.id, success: false, error: msg }) // Still schedule next run even on failure const nextRun = calculateNextRun({ @@ -86,6 +88,8 @@ export async function POST(request: NextRequest) { where: { id: agent.id }, data: { nextRun }, }) + + results.push({ id: agent.id, success: false, error: msg }) } } diff --git a/memento-note/components/agents/agent-card.tsx b/memento-note/components/agents/agent-card.tsx index 61961a5..d851325 100644 --- a/memento-note/components/agents/agent-card.tsx +++ b/memento-note/components/agents/agent-card.tsx @@ -197,14 +197,12 @@ export function AgentCard({ agent, onEdit, onRefresh, onToggle }: AgentCardProps {t('agents.metadata.executions', { count: agent._count.actions })} - {agent.frequency !== 'manual' && agent.nextRun && ( + {agent.frequency !== 'manual' && agent.nextRun && new Date(agent.nextRun) > new Date() && (
{t('agents.schedule.nextRun')}{' '} {mounted - ? new Date(agent.nextRun) > new Date() - ? formatDistanceToNow(new Date(agent.nextRun), { addSuffix: true, locale: dateLocale }) - : t('agents.schedule.pending') + ? formatDistanceToNow(new Date(agent.nextRun), { addSuffix: true, locale: dateLocale }) : new Date(agent.nextRun).toISOString().split('T')[0]}
)}