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>
163 lines
8.4 KiB
Python
163 lines
8.4 KiB
Python
"""
|
||
智能体模型
|
||
"""
|
||
from sqlalchemy import Column, String, Text, Integer, DateTime, JSON, ForeignKey, func
|
||
from sqlalchemy.dialects.mysql import CHAR
|
||
from sqlalchemy.orm import relationship
|
||
from app.core.database import Base
|
||
import uuid
|
||
|
||
|
||
class Agent(Base):
|
||
"""智能体表"""
|
||
__tablename__ = "agents"
|
||
|
||
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="描述")
|
||
workflow_config = Column(JSON, nullable=False, comment="工作流配置")
|
||
agent_type = Column(String(20), default="specialist", comment="Agent类型: main/specialist")
|
||
budget_config = Column(
|
||
JSON,
|
||
nullable=True,
|
||
comment="执行预算:max_steps/max_llm_invocations/max_tool_calls(可选,覆盖全局默认)",
|
||
)
|
||
input_schema = Column(JSON, nullable=True, comment="输入数据类型约束(JSON Schema)")
|
||
output_schema = Column(JSON, nullable=True, comment="输出数据类型约束(JSON Schema)")
|
||
version = Column(Integer, default=1, comment="版本号")
|
||
status = Column(String(20), default="draft", comment="状态: draft/published/running/stopped")
|
||
user_id = Column(CHAR(36), ForeignKey("users.id"), comment="创建者ID")
|
||
|
||
# 技能市场字段
|
||
category = Column(String(50), nullable=True, comment="分类: llm/data_processing/automation/integration/other")
|
||
tags = Column(JSON, nullable=True, comment="标签列表")
|
||
thumbnail = Column(Text, nullable=True, comment="缩略图URL")
|
||
is_public = Column(Integer, default=0, comment="是否公开到市场: 0=私有 1=公开")
|
||
is_featured = Column(Integer, default=0, comment="是否精选: 0=否 1=是")
|
||
rating_avg = Column(String(10), default="0.0", comment="平均评分")
|
||
rating_count = Column(Integer, default=0, comment="评分人数")
|
||
use_count = Column(Integer, default=0, comment="被安装次数")
|
||
view_count = Column(Integer, default=0, comment="查看次数")
|
||
forked_from_id = Column(CHAR(36), nullable=True, comment="从哪个Agent Fork而来(市场安装)")
|
||
|
||
created_at = Column(DateTime, default=func.now(), comment="创建时间")
|
||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间")
|
||
|
||
# 关系
|
||
user = relationship("User", backref="agents")
|
||
|
||
def __repr__(self):
|
||
return f"<Agent(id={self.id}, name={self.name})>"
|
||
|
||
|
||
class AgentExtension(Base):
|
||
"""Agent 自主扩展记录表 — 用于 extension_log 工具"""
|
||
__tablename__ = "agent_extensions"
|
||
|
||
id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="扩展记录ID")
|
||
extension_type = Column(String(50), nullable=False, comment="扩展类型: agent_created/tool_registered/code_tool_created")
|
||
name = Column(String(100), nullable=False, comment="创建的Agent或工具名称")
|
||
reason = Column(Text, comment="创建原因(为什么需要扩展)")
|
||
detail = Column(JSON, comment="扩展详情(参数、结果等)")
|
||
success_rating = Column(String(20), comment="效果评价: success/partial/failed")
|
||
note = Column(Text, comment="反馈备注")
|
||
user_id = Column(CHAR(36), ForeignKey("users.id"), nullable=True, comment="创建者ID")
|
||
created_at = Column(DateTime, default=func.now(), comment="创建时间")
|
||
|
||
def __repr__(self):
|
||
return f"<AgentExtension(id={self.id}, type={self.extension_type}, name={self.name})>"
|
||
|
||
|
||
class GlobalKnowledge(Base):
|
||
"""Agent 间知识共享表 — 跨 Agent 的全局知识池"""
|
||
__tablename__ = "global_knowledge"
|
||
|
||
id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="知识ID")
|
||
content = Column(Text, nullable=False, comment="知识内容摘要")
|
||
embedding = Column(Text, nullable=True, comment="内容 embedding(JSON 序列化)")
|
||
source_agent_id = Column(CHAR(36), nullable=True, comment="来源 Agent ID")
|
||
source_user_id = Column(CHAR(36), nullable=True, comment="来源用户 ID")
|
||
tags = Column(JSON, nullable=True, comment="分类标签")
|
||
confidence = Column(String(20), default="medium", comment="置信度: low/medium/high")
|
||
expires_at = Column(DateTime, nullable=True, comment="过期时间,NULL 表示永不过期")
|
||
scope_kind = Column(String(50), default="agent", comment="作用域类型")
|
||
scope_id = Column(String(100), default="", comment="作用域 ID")
|
||
created_at = Column(DateTime, default=func.now(), comment="创建时间")
|
||
|
||
def __repr__(self):
|
||
return f"<GlobalKnowledge(id={self.id}, source_agent={self.source_agent_id})>"
|
||
|
||
|
||
class KnowledgeEntity(Base):
|
||
"""知识图谱实体表 — 学习知识点、概念、术语"""
|
||
__tablename__ = "knowledge_entities"
|
||
|
||
id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="实体ID")
|
||
name = Column(String(200), nullable=False, comment="实体名称")
|
||
entity_type = Column(String(50), nullable=False, default="concept", comment="实体类型: concept/formula/fact/term/task/skill")
|
||
description = Column(Text, comment="实体描述")
|
||
embedding = Column(Text, nullable=True, comment="实体名称+描述的 embedding(JSON 序列化)")
|
||
metadata_ = Column("metadata", JSON, nullable=True, comment="扩展元数据")
|
||
source = Column(String(50), default="extracted", comment="来源: extracted/manual/imported")
|
||
confidence = Column(String(20), default="medium", comment="置信度: low/medium/high")
|
||
scope_kind = Column(String(50), default="agent", comment="作用域类型")
|
||
scope_id = Column(String(100), default="", comment="作用域 ID")
|
||
user_id = Column(CHAR(36), ForeignKey("users.id"), nullable=True, comment="创建者ID")
|
||
created_at = Column(DateTime, default=func.now(), comment="创建时间")
|
||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间")
|
||
|
||
def __repr__(self):
|
||
return f"<KnowledgeEntity(id={self.id}, name={self.name}, type={self.entity_type})>"
|
||
|
||
|
||
class KnowledgeRelation(Base):
|
||
"""知识图谱关系表 — 实体之间的语义关系"""
|
||
__tablename__ = "knowledge_relations"
|
||
|
||
id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="关系ID")
|
||
source_entity_id = Column(CHAR(36), nullable=False, index=True, comment="源实体ID")
|
||
target_entity_id = Column(CHAR(36), nullable=False, index=True, comment="目标实体ID")
|
||
relation_type = Column(String(50), nullable=False, comment="关系类型: prerequisite/extends/contains/related_to/example_of/applies_to")
|
||
description = Column(Text, comment="关系描述")
|
||
weight = Column(String(20), default="1.0", comment="关系权重")
|
||
scope_kind = Column(String(50), default="agent", comment="作用域类型")
|
||
scope_id = Column(String(100), default="", comment="作用域 ID")
|
||
created_at = Column(DateTime, default=func.now(), comment="创建时间")
|
||
|
||
def __repr__(self):
|
||
return f"<KnowledgeRelation({self.source_entity_id}) -[{self.relation_type}]-> ({self.target_entity_id})>"
|
||
|
||
|
||
class AgentRating(Base):
|
||
"""Agent 技能市场评分/评论表"""
|
||
__tablename__ = "agent_ratings"
|
||
|
||
id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="评分ID")
|
||
agent_id = Column(CHAR(36), nullable=False, index=True, comment="Agent ID")
|
||
user_id = Column(CHAR(36), ForeignKey("users.id"), nullable=False, comment="评分用户ID")
|
||
rating = Column(Integer, nullable=False, comment="评分 1-5")
|
||
comment = Column(Text, nullable=True, comment="评论内容")
|
||
created_at = Column(DateTime, default=func.now(), comment="评分时间")
|
||
|
||
# 关系
|
||
user = relationship("User", backref="agent_ratings")
|
||
|
||
def __repr__(self):
|
||
return f"<AgentRating(agent={self.agent_id}, user={self.user_id}, rating={self.rating})>"
|
||
|
||
|
||
class AgentFavorite(Base):
|
||
"""Agent 技能市场收藏表"""
|
||
__tablename__ = "agent_favorites"
|
||
|
||
id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="收藏ID")
|
||
agent_id = Column(CHAR(36), nullable=False, index=True, comment="Agent ID")
|
||
user_id = Column(CHAR(36), ForeignKey("users.id"), nullable=False, comment="收藏用户ID")
|
||
created_at = Column(DateTime, default=func.now(), comment="收藏时间")
|
||
|
||
# 关系
|
||
user = relationship("User", backref="agent_favorites")
|
||
|
||
def __repr__(self):
|
||
return f"<AgentFavorite(agent={self.agent_id}, user={self.user_id})>"
|