[ADD] Merge code

This commit is contained in:
Mark
2025-12-15 19:50:21 +08:00
parent ea0a445d5b
commit 7bbef35b7d
54 changed files with 6956 additions and 652 deletions

View File

@@ -15,9 +15,11 @@ from .end_user_model import EndUser
from .appshare_model import AppShare
from .release_share_model import ReleaseShare
from .conversation_model import Conversation, Message
from .api_key_model import ApiKey, ApiKeyLog, ApiKeyType, ResourceType
from .api_key_model import ApiKey, ApiKeyLog, ApiKeyType
from .data_config_model import DataConfig
from .multi_agent_model import MultiAgentConfig, AgentInvocation
from .workflow_model import WorkflowConfig, WorkflowExecution, WorkflowNodeExecution
from .retrieval_info import RetrievalInfo
__all__ = [
"Tenants",
@@ -46,8 +48,11 @@ __all__ = [
"ApiKey",
"ApiKeyLog",
"ApiKeyType",
"ResourceType",
"DataConfig",
"MultiAgentConfig",
"AgentInvocation"
"AgentInvocation",
"WorkflowConfig",
"WorkflowExecution",
"WorkflowNodeExecution",
"RetrievalInfo"
]

View File

@@ -2,7 +2,7 @@
import datetime
import uuid
from sqlalchemy import Column, String, Boolean, DateTime, Integer, ForeignKey, Text, Enum
from sqlalchemy import Column, String, Boolean, DateTime, Integer, ForeignKey, Text
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import relationship
from enum import StrEnum
@@ -12,18 +12,10 @@ from app.db import Base
class ApiKeyType(StrEnum):
"""API Key 类型"""
APP = "app" # 应用 API Key
RAG = "rag" # RAG API Key
MEMORY = "memory" # Memory API Key
class ResourceType(StrEnum):
"""资源类型枚举"""
AGENT = "Agent" # 智能体
CLUSTER = "Cluster" # 集群
WORKFLOW = "Workflow" # 工作流
KNOWLEDGE = "Knowledge" # 知识库
MEMORY_ENGINE = "Memory_Engine" # 记忆引擎
AGENT = "agent" # 智能体
CLUSTER = "cluster" # 集群
WORKFLOW = "workflow" # 工作流
SERVICE = "service" # 服务
class ApiKey(Base):
@@ -35,18 +27,16 @@ class ApiKey(Base):
# 基本信息
name = Column(String(255), nullable=False, comment="API Key 名称")
description = Column(Text, comment="描述")
key_prefix = Column(String(20), nullable=False, comment="Key 前缀")
key_hash = Column(String(255), nullable=False, unique=True, index=True, comment="Key 哈希值")
api_key = Column(String(255), nullable=False, unique=True, index=True, comment="API Key 明文")
# 类型和权限
type = Column(String(50), nullable=False, index=True, comment="API Key 类型")
scopes = Column(JSONB, nullable=False, default=list, comment="权限范围列表")
scopes = Column(JSONB, default=list, comment="权限范围列表")
# 关联资源
workspace_id = Column(UUID(as_uuid=True), ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False,
index=True, comment="所属工作空间")
resource_id = Column(UUID(as_uuid=True), index=True, comment="关联资源ID")
resource_type = Column(String(50), comment="资源类型")
# 限制和配额
rate_limit = Column(Integer, default=10, comment="QPS限制请求/秒)")

View File

@@ -86,6 +86,14 @@ class App(Base):
uselist=False,
cascade="all, delete-orphan",
)
# 一对一:工作流配置(仅当 type=workflow 时有效)
workflow_config = relationship(
"WorkflowConfig",
back_populates="app",
uselist=False,
cascade="all, delete-orphan",
)
# 发布版本关联
current_release = relationship("AppRelease", foreign_keys=[current_release_id])

View File

@@ -0,0 +1,196 @@
"""
工作流相关数据模型
"""
import datetime
import uuid
from sqlalchemy import Column, String, Boolean, DateTime, Integer, Float, ForeignKey, Text
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import relationship
from app.db import Base
class WorkflowConfig(Base):
"""工作流配置表"""
__tablename__ = "workflow_configs"
# 主键
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
# 关联应用(一对一)
app_id = Column(
UUID(as_uuid=True),
ForeignKey("apps.id", ondelete="CASCADE"),
nullable=False,
unique=True,
index=True
)
# 节点和边的定义JSON 格式)
nodes = Column(JSONB, nullable=False, default=list)
edges = Column(JSONB, nullable=False, default=list)
# 全局变量定义
variables = Column(JSONB, default=list)
# 执行配置
execution_config = Column(JSONB, nullable=False, default=dict)
# 触发器配置(可选)
triggers = Column(JSONB, default=list)
# 状态
is_active = Column(Boolean, nullable=False, default=True)
created_at = Column(DateTime, nullable=False, default=datetime.datetime.now)
updated_at = Column(
DateTime,
nullable=False,
default=datetime.datetime.now,
onupdate=datetime.datetime.now
)
# 关系
app = relationship("App", back_populates="workflow_config")
executions = relationship(
"WorkflowExecution",
back_populates="workflow_config",
cascade="all, delete-orphan"
)
def __repr__(self):
return f"<WorkflowConfig(id={self.id}, app_id={self.app_id})>"
class WorkflowExecution(Base):
"""工作流执行记录表"""
__tablename__ = "workflow_executions"
# 主键
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
# 关联信息
workflow_config_id = Column(
UUID(as_uuid=True),
ForeignKey("workflow_configs.id", ondelete="CASCADE"),
nullable=False,
index=True
)
app_id = Column(
UUID(as_uuid=True),
ForeignKey("apps.id", ondelete="CASCADE"),
nullable=False,
index=True
)
conversation_id = Column(
UUID(as_uuid=True),
ForeignKey("conversations.id", ondelete="SET NULL"),
nullable=True,
index=True
)
# 执行信息
execution_id = Column(String(100), nullable=False, unique=True, index=True)
trigger_type = Column(String(20), nullable=False) # manual, schedule, webhook, event
triggered_by = Column(
UUID(as_uuid=True),
ForeignKey("users.id"),
nullable=True
)
# 输入输出
input_data = Column(JSONB)
output_data = Column(JSONB)
context = Column(JSONB, default=dict)
# 状态
status = Column(String(20), nullable=False, default="pending", index=True)
# 可选值pending, running, completed, failed, cancelled, timeout
error_message = Column(Text)
error_node_id = Column(String(100))
# 性能指标
started_at = Column(DateTime, nullable=False, default=datetime.datetime.now, index=True)
completed_at = Column(DateTime)
elapsed_time = Column(Float) # 耗时(秒)
# 资源使用
token_usage = Column(JSONB)
# 元数据(使用 meta_data 避免与 SQLAlchemy 保留字 metadata 冲突)
meta_data = Column(JSONB, default=dict)
created_at = Column(DateTime, nullable=False, default=datetime.datetime.now)
# 关系
workflow_config = relationship("WorkflowConfig", back_populates="executions")
app = relationship("App")
conversation = relationship("Conversation")
triggered_by_user = relationship("User", foreign_keys=[triggered_by])
node_executions = relationship(
"WorkflowNodeExecution",
back_populates="execution",
cascade="all, delete-orphan",
order_by="WorkflowNodeExecution.execution_order"
)
def __repr__(self):
return f"<WorkflowExecution(id={self.id}, execution_id={self.execution_id}, status={self.status})>"
class WorkflowNodeExecution(Base):
"""工作流节点执行记录表"""
__tablename__ = "workflow_node_executions"
# 主键
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
# 关联执行
execution_id = Column(
UUID(as_uuid=True),
ForeignKey("workflow_executions.id", ondelete="CASCADE"),
nullable=False,
index=True
)
# 节点信息
node_id = Column(String(100), nullable=False, index=True)
node_type = Column(String(20), nullable=False)
node_name = Column(String(100))
# 执行顺序
execution_order = Column(Integer, nullable=False)
retry_count = Column(Integer, nullable=False, default=0)
# 输入输出
input_data = Column(JSONB)
output_data = Column(JSONB)
# 状态
status = Column(String(20), nullable=False, default="pending", index=True)
# 可选值pending, running, completed, failed, skipped, cached
error_message = Column(Text)
# 性能指标
started_at = Column(DateTime, nullable=False, default=datetime.datetime.now)
completed_at = Column(DateTime)
elapsed_time = Column(Float) # 耗时(秒)
# 资源使用(针对 LLM 节点)
token_usage = Column(JSONB)
# 缓存信息
cache_hit = Column(Boolean, default=False)
cache_key = Column(String(255))
# 元数据(使用 meta_data 避免与 SQLAlchemy 保留字 metadata 冲突)
meta_data = Column(JSONB, default=dict)
created_at = Column(DateTime, nullable=False, default=datetime.datetime.now)
# 关系
execution = relationship("WorkflowExecution", back_populates="node_executions")
def __repr__(self):
return f"<WorkflowNodeExecution(id={self.id}, node_id={self.node_id}, status={self.status})>"