Files
ai-agent-admin/backend-fastapi/ai_platform/models/workflow.py
T
2026-06-08 18:14:59 +08:00

84 lines
4.6 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.
"""
AI 工作流模型
"""
from sqlalchemy import Column, String, Text, Integer, Boolean, DateTime, JSON, Index
from app.base_model import BaseModel
class AIWorkflow(BaseModel):
"""
AI 工作流定义
独立于 AIApp 的工作流定义,可以被多个应用引用
"""
__tablename__ = "ai_workflow"
application_id = Column(String(21), nullable=True, index=True, comment="所属应用ID(逻辑外键关联core_application")
is_global = Column(Boolean, default=False, comment="是否在子应用中可见")
name = Column(String(100), nullable=False, comment="工作流名称")
code = Column(String(100), unique=True, nullable=False, comment="工作流编码")
workflow_type = Column(String(30), default="general", comment="工作流类型: general/application/form/report/data_process/automation")
description = Column(Text, default="", comment="描述")
status = Column(String(20), default="draft", comment="状态: draft/published/disabled")
version = Column(Integer, default=1, comment="当前草稿版本号")
published_version = Column(Integer, nullable=True, comment="已发布的版本号")
published_at = Column(DateTime, nullable=True, comment="最后发布时间")
published_definition = Column(JSON, default=dict, comment="已发布版本的工作流定义")
definition = Column(JSON, default=dict, comment="工作流定义(草稿)")
input_variables = Column(JSON, default=list, comment="输入变量定义")
output_variables = Column(JSON, default=list, comment="输出变量定义")
run_count = Column(Integer, default=0, comment="运行次数")
success_count = Column(Integer, default=0, comment="成功次数")
class AIWorkflowVersion(BaseModel):
"""
AI 工作流版本历史
每次发布时创建一条版本记录
"""
__tablename__ = "ai_workflow_version"
workflow_id = Column(String(21), nullable=False, index=True, comment="工作流ID(逻辑外键关联ai_workflow")
version = Column(Integer, nullable=False, comment="版本号")
definition = Column(JSON, default=dict, comment="该版本的工作流定义")
description = Column(Text, default="", comment="版本说明")
published_by_id = Column(String(21), nullable=True, comment="发布人ID(逻辑外键关联core_user")
published_at = Column(DateTime, nullable=True, comment="发布时间")
run_count = Column(Integer, default=0, comment="运行次数")
success_count = Column(Integer, default=0, comment="成功次数")
class AIWorkflowRun(BaseModel):
"""
AI 工作流运行记录
"""
__tablename__ = "ai_workflow_run"
workflow_id = Column(String(21), nullable=False, index=True, comment="工作流ID(逻辑外键关联ai_workflow")
app_id = Column(String(21), nullable=True, index=True, comment="关联应用ID(逻辑外键关联ai_app)")
conversation_id = Column(String(21), nullable=True, comment="关联对话ID(逻辑外键关联ai_conversation")
user_id = Column(String(21), nullable=True, index=True, comment="执行用户ID(逻辑外键关联core_user")
status = Column(String(20), default="pending", comment="状态: pending/running/waiting/completed/failed/stopped")
trigger_type = Column(String(30), default="api", comment="触发来源: editor_draft/editor_published/agent/api/form_button")
use_draft = Column(Boolean, default=False, comment="是否使用草稿定义执行")
workflow_version = Column(Integer, nullable=True, comment="执行时发布版本号,草稿运行为空")
definition_snapshot = Column(JSON, default=dict, comment="运行开始时的工作流定义快照")
inputs = Column(JSON, default=dict, comment="输入数据")
outputs = Column(JSON, default=dict, comment="输出数据")
execution_log = Column(JSON, default=list, comment="执行日志")
current_node_id = Column(String(100), default="", comment="当前节点 ID")
waiting_config = Column(JSON, default=dict, comment="等待用户输入的配置")
error_message = Column(Text, default="", comment="错误信息")
total_tokens = Column(Integer, default=0, comment="总 Token 数")
total_steps = Column(Integer, default=0, comment="总步骤数")
elapsed_time = Column(Integer, default=0, comment="总耗时(毫秒)")
started_at = Column(DateTime, nullable=True, comment="开始时间")
completed_at = Column(DateTime, nullable=True, comment="完成时间")
__table_args__ = (
Index("ix_ai_workflow_run_workflow_status", "workflow_id", "status"),
Index("ix_ai_workflow_run_user_status", "user_id", "status"),
)