"""Agent 定时任务表:按 cron 表达式周期执行 Agent""" import uuid from datetime import datetime from sqlalchemy import Column, String, Text, Integer, DateTime, ForeignKey, Boolean, JSON, Index from sqlalchemy.dialects.mysql import CHAR from app.core.database import Base class AgentSchedule(Base): """Agent 定时任务 — 按 cron 表达式周期执行指定 Agent""" __tablename__ = "agent_schedules" id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4())) agent_id = Column(CHAR(36), ForeignKey("agents.id"), nullable=True, index=True, comment="关联 Agent ID(schedule_type=agent 时必填)") schedule_type = Column(String(20), default="agent", comment="调度类型: agent / goal") goal_id = Column(CHAR(36), ForeignKey("goals.id"), nullable=True, index=True, comment="关联 Goal ID(schedule_type=goal 时必填)") goal_config = Column(JSON, nullable=True, comment="Goal 调度配置: {title, description, priority}") name = Column(String(100), nullable=False, comment="任务名称") cron_expression = Column(String(100), nullable=False, comment="cron 表达式,如 0 9 * * *") input_message = Column(Text, nullable=False, comment="定时执行时发送的消息内容") timezone = Column(String(64), default="Asia/Shanghai", comment="时区") webhook_url = Column(String(512), nullable=True, comment="飞书机器人 Webhook URL(可选),执行完成后推送通知") enabled = Column(Boolean, default=True, comment="是否启用") last_run_at = Column(DateTime, nullable=True, comment="上次执行时间") last_run_status = Column(String(32), nullable=True, comment="上次执行状态: success/failed") next_run_at = Column(DateTime, nullable=False, comment="下次执行时间") user_id = Column(CHAR(36), ForeignKey("users.id"), nullable=False, index=True, comment="创建者 ID") workspace_id = Column(CHAR(36), ForeignKey("workspaces.id"), nullable=True, comment="所属工作区ID") created_at = Column(DateTime, default=datetime.utcnow, comment="创建时间") updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间") def __repr__(self): return f""