diff --git a/backend/open_webui/routers/calendar.py b/backend/open_webui/routers/calendar.py index 944a588daa..056e366aa2 100644 --- a/backend/open_webui/routers/calendar.py +++ b/backend/open_webui/routers/calendar.py @@ -210,7 +210,31 @@ async def get_events( # Future runs: expand RRULEs for active automations only active_automations = await Automations.get_active_by_user(user.id) + now_ns = int(time.time_ns()) for auto in active_automations: + next_run_at = auto.next_run_at + action = auto.data['action'] + next_run_event = { + 'id': f'auto_{auto.id}_next', + 'calendar_id': SCHEDULED_TASKS_CALENDAR_ID, + 'user_id': user.id, + 'title': auto.name, + 'description': action.get('prompt') or action.get('code') or '', + 'start_at': next_run_at, + 'end_at': None, + 'all_day': False, + 'rrule': None, + 'color': None, + 'location': None, + 'data': None, + 'meta': {'automation_id': auto.id}, + 'is_cancelled': False, + 'attendees': [], + 'created_at': auto.created_at, + 'updated_at': auto.updated_at, + 'user': None, + } + scheduled_starts = set() schedules = [t['rrule'] for t in auto.data['triggers'] if t['type'] == 'schedule'] for i, rrule_str in enumerate(schedules): start_at = await next_run_ns(rrule_str, tz=user.timezone) or 0 @@ -222,36 +246,25 @@ async def get_events( except ValueError: pass - action = auto.data['action'] - virtual = { - 'id': f'auto_{auto.id}_{i}', - 'calendar_id': SCHEDULED_TASKS_CALENDAR_ID, - 'user_id': user.id, - 'title': auto.name, - 'description': action.get('prompt') or action.get('code') or '', - 'start_at': start_at, - 'end_at': None, - 'all_day': False, - 'rrule': rrule_str, - 'color': None, - 'location': None, - 'data': None, - 'meta': {'automation_id': auto.id}, - 'is_cancelled': False, - 'attendees': [], - 'created_at': auto.created_at, - 'updated_at': auto.updated_at, - 'user': None, - } + virtual = {**next_run_event, 'id': f'auto_{auto.id}_{i}', 'start_at': start_at, 'rrule': rrule_str} # Only expand into the future — past runs are handled below - now_ns = int(time.time_ns()) rrule_start = max(start_ns, now_ns) instances = await evaluate_recurrence( expand_recurring_event, virtual, rrule_start, end_ns, user.timezone ) for inst in instances: - expanded.append(CalendarEventUserResponse(**inst)) + # A next run moved by code replaces the scheduled times before it + if inst['start_at'] >= (next_run_at or 0): + scheduled_starts.add(inst['start_at']) + expanded.append(CalendarEventUserResponse(**inst)) + + if ( + next_run_at + and max(start_ns, now_ns) <= next_run_at < end_ns + and next_run_at not in scheduled_starts + ): + expanded.append(CalendarEventUserResponse(**next_run_event)) # Past runs: single range query joined with automation runs_with_auto = await AutomationRuns.get_runs_by_user_range(user.id, start_ns, end_ns) diff --git a/src/lib/components/automations/AutomationEditor.svelte b/src/lib/components/automations/AutomationEditor.svelte index 88158198c4..1722c3d180 100644 --- a/src/lib/components/automations/AutomationEditor.svelte +++ b/src/lib/components/automations/AutomationEditor.svelte @@ -1040,7 +1040,7 @@ - {#if action_type === 'code' || automation.data.triggers.some((trigger) => trigger.type === 'schedule') || automation.last_run_at} + {#if action_type === 'code' || automation.next_run_at || automation.data.triggers.some((trigger) => trigger.type === 'schedule') || automation.last_run_at}