Files
aiagent/backend/app/models/goal.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

46 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
目标模型 — Main Agent 管理的顶层目标
"""
from sqlalchemy import Column, String, Text, Integer, Float, DateTime, JSON, ForeignKey, func
from sqlalchemy.dialects.mysql import CHAR
from sqlalchemy.orm import relationship
from app.core.database import Base
import uuid
class Goal(Base):
"""目标表 — Main Agent 数字员工的顶层工作目标"""
__tablename__ = "goals"
id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="目标ID")
title = Column(String(500), nullable=False, comment="目标标题")
description = Column(Text, comment="目标描述(自然语言)")
status = Column(String(20), default="active", comment="状态: active/paused/completed/failed/cancelled")
priority = Column(Integer, default=5, comment="优先级 1-10")
progress = Column(Float, default=0.0, comment="完成进度 0.0 - 1.0")
# Main Agent 分解后的结构化计划
plan = Column(JSON, comment="执行计划: [{phase, tasks:[], assigned_agent_id, deadline}]")
# 自主循环配置
autonomy_config = Column(JSON, comment="自主循环配置: {check_interval_minutes, max_idle_hours, auto_replan, notify_on_progress}")
# 关联
creator_id = Column(CHAR(36), ForeignKey("users.id"), nullable=False, comment="创建者ID")
main_agent_id = Column(CHAR(36), ForeignKey("agents.id"), nullable=True, comment="管理此目标的 Main Agent ID")
parent_goal_id = Column(CHAR(36), ForeignKey("goals.id"), nullable=True, comment="父目标ID支持目标嵌套")
# 时间
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="更新时间")
# 关系
creator = relationship("User", backref="goals")
main_agent = relationship("Agent", backref="managed_goals")
def __repr__(self):
return f"<Goal(id={self.id}, title={self.title}, status={self.status})>"