Calendar 工作区(中文译文)
原始 DeepWiki 页面:https://deepwiki.com/open-webui/open-webui/20.2-calendar-workspace
翻译时间:2026-06-09T16:12:47.348Z
翻译模型:deepseek-chat
原文字符数:9363
项目:Open WebUI (open-webui)
---
日历工作区
相关源文件
以下文件被用作生成此 wiki 页面的上下文:
backend/open_webui/migrations/versions/56359461a091_add_calendar_tables.pybackend/open_webui/models/calendar.pybackend/open_webui/routers/calendar.pybackend/open_webui/utils/calendar.pysrc/lib/apis/calendar/index.tssrc/lib/components/calendar/CalendarEventChip.sveltesrc/lib/components/calendar/CalendarEventModal.sveltesrc/lib/components/calendar/CalendarSidebar.sveltesrc/lib/components/calendar/CalendarView.sveltesrc/routes/(app)/calendar/+page.svelte/calendar/+page.svelte)
日历工作区是 v0.9.0 中引入的一项功能,提供了一个集中式界面来管理基于时间的事件、重复日程和 AI 驱动的自动化。它直接与 Open WebUI 自动化引擎集成,允许用户将计划中的 AI 任务与手动日历条目一起可视化。
系统架构与数据流
日历系统遵循标准的三层架构:基于 SvelteKit 的前端用于可视化,FastAPI 后端用于业务逻辑和权限执行,以及 SQLAlchemy 管理的关系数据库用于持久化。
数据流:事件检索
下图说明了获取事件的过程,突出了物理数据库记录与虚拟自动化实例的合并。
事件解析管道
sequenceDiagram
participant UI as "CalendarView.svelte"
participant API as "calendar.py (Router)"
participant DB as "CalendarEvent (SQLAlchemy)"
participant AUTO as "Automations (Model)"
participant RR as "expand_recurring_event (Utils)"
UI->>API: GET /calendars/events?start={ISO}&end={ISO}
API->>DB: get_events_by_range(user_id, start_ns, end_ns)
DB-->>API: List[CalendarEvent]
API->>RR: expand_recurring_event(event_dict, start_ns, end_ns)
RR-->>API: List[CalendarEventUserResponse]
alt 自动化已启用
API->>AUTO: get_active_by_user(user_id)
AUTO-->>API: List[Automation]
API->>RR: expand_recurring_event(auto_dict, ...)
RR-->>API: List[VirtualAutomationEvents]
end
API-->>UI: 合并后的 JSON (items, total)
来源:backend/open_webui/routers/calendar.py:123-210,src/routes/(app)/calendar/+page.svelte:92-99,backend/open_webui/utils/calendar.py:17-74
数据模型与模式
日历系统使用三个主要表来管理日历、事件和参与情况。
数据库实体
| 表 | 描述 | 关键字段 |
|---|---|---|
calendar | 事件的容器;支持通过访问授权进行共享。 | id,user_id,name,color,is_default |
calendar_event | 单个或重复的时间段。 | id,calendar_id,start_at,end_at,rrule,meta |
calendar_event_attendee | 跟踪用户对特定事件的 RSVP 状态。 | event_id,user_id,status (pending/accepted/etc) |
来源:backend/open_webui/models/calendar.py:37-97,backend/open_webui/migrations/versions/56359461a091_add_calendar_tables.py:23-73
代码实体映射
此图将逻辑概念映射到 Python 和 TypeScript 代码库中的具体实现。
后端模型架构
classDiagram
class "Calendar (SQLAlchemy)" {
+String id
+String user_id
+JSON data
+JSON meta
}
class "CalendarEvent (SQLAlchemy)" {
+String title
+BigInteger start_at
+String rrule
+Boolean all_day
}
class "CalendarEventAttendee (SQLAlchemy)" {
+String status
}
class "CalendarTable (Internal)" {
+insert_new_calendar()
+get_calendars_by_user()
}
class "CalendarEventsTable (Internal)" {
+get_events_by_range()
}
"Calendar (SQLAlchemy)" "1" --* "many" "CalendarEvent (SQLAlchemy)" : 包含
"CalendarEvent (SQLAlchemy)" "1" --* "many" "CalendarEventAttendee (SQLAlchemy)" : 拥有
"CalendarTable (Internal)" ..> "Calendar (SQLAlchemy)" : 管理
"CalendarEventsTable (Internal)" ..> "CalendarEvent (SQLAlchemy)" : 管理
来源:backend/open_webui/models/calendar.py:37-97,backend/open_webui/models/calendar.py:235-255
核心功能
1. 事件管理
用户可以通过 CalendarEventModal.svelte 组件创建、更新和删除事件。事件支持:
- 时间选择:以纳秒精度存储为
BigInteger(ns),使用转换因子NS = 1_000_000将毫秒转换为纳秒src/lib/components/calendar/CalendarEventModal.svelte:39-51。 - 重复日程:使用 iCalendar
RRULE字符串定义模式,由后端的dateutil.rrule展开backend/open_webui/utils/calendar.py:29-41。 - 全天事件:布尔标志,调整 UI 渲染并将时间默认为
00:00或23:59src/lib/components/calendar/CalendarEventModal.svelte:99-100。
2. 自动化集成
日历包含一个虚拟系统日历,标识为 SCHEDULED_TASKS_CALENDAR_ID(__scheduled_tasks__)backend/open_webui/routers/calendar.py:33。
- 虚拟事件:此日历中的事件不存储在
calendar_event表中。相反,它们通过expand_recurring_event工具从活动的Automations动态生成backend/open_webui/routers/calendar.py:182-200。 - 导航:在 UI 中点击自动化事件会通过
goto将用户重定向到关联的聊天或自动化配置页面src/routes/(app)/calendar/+page.svelte:138-144。
3. 提醒与通知
事件元数据包含一个 alert_minutes 字段 src/lib/components/calendar/CalendarEventModal.svelte:35。系统支持:
- 应用内 Toast:由前端在事件临近或成功执行 CRUD 操作时触发
src/routes/(app)/calendar/+page.svelte:119。 - RSVP 跟踪:用户可以使用
RSVPForm中定义的状态响应邀请:accepted、declined、tentative或pendingbackend/open_webui/models/calendar.py:213。
权限与访问控制
访问权限由全局配置和每个用户的权限共同控制:
- 功能开关:由应用程序状态中的
ENABLE_CALENDAR控制backend/open_webui/routers/calendar.py:38。 - 用户权限:非管理员用户必须通过
has_permission检查features.calendar权限backend/open_webui/routers/calendar.py:43-45。 - 资源共享:
_check_calendar_access函数使用AccessGrants.has_access验证共享日历的所有权或是否存在AccessGrantbackend/open_webui/routers/calendar.py:61-78。
前端组件
UI 使用模块化组件结构构建:
| 组件 | 文件路径 | 职责 |
|---|---|---|
CalendarView | CalendarView.svelte | 月、周和日视图的主网格渲染 src/lib/components/calendar/CalendarView.svelte:144-204。 |
CalendarSidebar | CalendarSidebar.svelte | 迷你日历导航和日历可见性切换 src/lib/components/calendar/CalendarSidebar.svelte:105-233。 |
CalendarEventModal | CalendarEventModal.svelte | 用于创建/编辑事件详情和提醒的表单 src/lib/components/calendar/CalendarEventModal.svelte:159-228。 |
CalendarEventChip | CalendarEventChip.svelte | 在日历网格中渲染的紧凑事件摘要 src/lib/components/calendar/CalendarEventChip.svelte:12-32。 |
前端组件层次结构
graph TD
Page["+page.svelte (路由)"] --> Sidebar["CalendarSidebar.svelte"]
Page --> View["CalendarView.svelte"]
Page --> Modal["CalendarEventModal.svelte"]
View --> Chip["CalendarEventChip.svelte"]
Sidebar --> MiniCal["迷你日历网格"]
Sidebar --> List["日历列表"]
来源:src/routes/(app)/calendar/+page.svelte:13-15,src/lib/components/calendar/CalendarView.svelte:4,src/lib/components/calendar/CalendarSidebar.svelte:4
API 端点
日历功能通过 /calendars 路由前缀暴露:
GET /:列出所有可用的日历backend/open_webui/routers/calendar.py:86。POST /create:创建新日历backend/open_webui/routers/calendar.py:111。GET /events:获取特定日期范围的事件backend/open_webui/routers/calendar.py:123。POST /events/create:向日历添加新事件backend/open_webui/routers/calendar.py:246。POST /events/{event_id}/rsvp:更新用户参与状态backend/open_webui/routers/calendar.py:348。
来源:backend/open_webui/routers/calendar.py:31-360,src/lib/apis/calendar/index.ts:74-263