Files
aiagent/backend/app/models/task.py
renjianbo 02d7cf8f62 feat: add Goal/Task data models, service layer, and API routes (Phase 1)
Main Agent 数字员工工厂基础设施:
- 新增 Goal 和 Task SQLAlchemy 数据模型
- Agent 模型新增 agent_type / input_schema / output_schema
- Execution 模型新增 goal_id 关联
- 新增 Goal/Task CRUD 服务层(含依赖检查、任务树、进度计算)
- 新增 /api/v1/goals (9端点) + /api/v1/tasks (8端点)
- 数据库迁移 013_add_goals_tasks

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-08 19:50:16 +08:00

60 lines
2.7 KiB
Python

"""
任务模型 — Goal 拆解的子任务
"""
from sqlalchemy import Column, String, Text, Integer, DateTime, JSON, Boolean, ForeignKey, func
from sqlalchemy.dialects.mysql import CHAR
from sqlalchemy.orm import relationship
from app.core.database import Base
import uuid
class Task(Base):
"""任务表 — Main Agent 将目标分解为可执行的子任务"""
__tablename__ = "tasks"
id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="任务ID")
goal_id = Column(CHAR(36), ForeignKey("goals.id"), nullable=False, comment="所属目标ID")
title = Column(String(500), nullable=False, comment="任务标题")
description = Column(Text, comment="任务描述")
status = Column(
String(20), default="pending",
comment="状态: pending/in_progress/awaiting_approval/completed/failed/cancelled"
)
priority = Column(Integer, default=5, comment="优先级 1-10")
# 任务编排配置
task_config = Column(JSON, comment="编排配置: {orchestration_mode, agents:[], workflow_id, input_data}")
# 依赖关系
parent_task_id = Column(CHAR(36), ForeignKey("tasks.id"), nullable=True, comment="父任务ID")
depends_on = Column(JSON, default=list, comment="前置依赖任务ID列表")
# 执行结果
result = Column(JSON, comment="执行输出结果")
error_message = Column(Text, comment="错误信息")
execution_id = Column(CHAR(36), ForeignKey("executions.id"), nullable=True, comment="关联的执行记录ID")
# 分配
assigned_agent_id = Column(CHAR(36), ForeignKey("agents.id"), nullable=True, comment="分配的 Agent ID")
assigned_agent_name = Column(String(200), comment="分配的 Agent 名称(冗余便于展示)")
# 审批
requires_approval = Column(Boolean, default=False, comment="是否需要人工审批")
approver_id = Column(CHAR(36), ForeignKey("users.id"), nullable=True, comment="审批人ID")
approval_status = Column(String(20), comment="审批状态: pending/approved/rejected")
# 时间
started_at = Column(DateTime, comment="开始时间")
completed_at = Column(DateTime, comment="完成时间")
deadline = Column(DateTime, comment="截止时间")
created_at = Column(DateTime, default=func.now(), comment="创建时间")
updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间")
# 关系
goal = relationship("Goal", backref="tasks")
assigned_agent = relationship("Agent", backref="assigned_tasks")
execution = relationship("Execution", backref="task")
def __repr__(self):
return f"<Task(id={self.id}, title={self.title}, status={self.status})>"