""" 虚拟团队模型 — Team + TeamMember 用于将多个 Agent 组织为角色化的虚拟软件团队 """ from sqlalchemy import Column, String, Text, Integer, DateTime, JSON, ForeignKey, Boolean, Index, func from sqlalchemy.dialects.mysql import CHAR from sqlalchemy.orm import relationship from app.core.database import Base import uuid class Team(Base): """虚拟团队表""" __tablename__ = "teams" id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="团队ID") name = Column(String(100), nullable=False, comment="团队名称") description = Column(Text, comment="团队描述") workspace_id = Column(CHAR(36), ForeignKey("workspaces.id"), nullable=True, comment="所属工作区ID") user_id = Column(CHAR(36), ForeignKey("users.id"), nullable=False, comment="创建者ID") is_template = Column(Boolean, default=False, comment="是否为预置模板") config = Column(JSON, nullable=True, comment="团队配置(协作模式等)") status = Column(String(20), default="active", comment="状态: active/inactive") created_at = Column(DateTime, default=func.now(), comment="创建时间") updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间") __table_args__ = ( Index("idx_team_workspace", "workspace_id"), Index("idx_team_user_id", "user_id"), ) members = relationship("TeamMember", back_populates="team", cascade="all, delete-orphan") user = relationship("User", backref="teams") def __repr__(self): return f"" def to_dict(self, include_members=False): result = { "id": self.id, "name": self.name, "description": self.description, "workspace_id": self.workspace_id, "user_id": self.user_id, "is_template": self.is_template, "config": self.config, "status": self.status, "created_at": self.created_at.isoformat() if self.created_at else None, "updated_at": self.updated_at.isoformat() if self.updated_at else None, } if include_members: result["members"] = [m.to_dict() for m in self.members] if self.members else [] return result class TeamMember(Base): """团队成员表 — Agent 在团队中的角色映射""" __tablename__ = "team_members" id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="成员ID") team_id = Column(CHAR(36), ForeignKey("teams.id"), nullable=False, comment="团队ID") agent_id = Column(CHAR(36), ForeignKey("agents.id"), nullable=False, comment="Agent ID") role = Column(String(30), nullable=False, comment="角色: pm/developer/qa/designer/devops/custom") position = Column(Integer, default=0, comment="排序位置") is_lead = Column(Boolean, default=False, comment="是否为团队Leader") created_at = Column(DateTime, default=func.now(), comment="创建时间") __table_args__ = ( Index("idx_tm_team_id", "team_id"), Index("idx_tm_agent_id", "agent_id"), ) team = relationship("Team", back_populates="members") agent = relationship("Agent", backref="team_memberships") def __repr__(self): return f"" def to_dict(self): return { "id": self.id, "team_id": self.team_id, "agent_id": self.agent_id, "role": self.role, "position": self.position, "is_lead": self.is_lead, "created_at": self.created_at.isoformat() if self.created_at else None, }