1. 管理后台支持为不同 Agent 应用上传和管理视频、PDF 等新手指南资料。 2. 前端聊天界面根据后台配置,动态展示“新手指南”入口。 3. 用户点击后可直接在系统内预览视频或 PDF 文档。
705 lines
33 KiB
Python
705 lines
33 KiB
Python
from datetime import datetime
|
|
from typing import Optional, List
|
|
from sqlalchemy import String, Text, Integer, ForeignKey, JSON, DateTime, Index, Boolean, UniqueConstraint, LargeBinary, text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from app import db
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
|
|
|
class Base(db.Model):
|
|
"""
|
|
基础模型类,设置为抽象基类
|
|
"""
|
|
__abstract__ = True
|
|
|
|
# RBAC 关联表:用户与 Agent 的多对多关系
|
|
# 用于存储用户拥有哪些 Agent 的访问权限
|
|
user_agent_permissions = db.Table(
|
|
'user_agent_permissions',
|
|
db.Column('user_id', ForeignKey('users.id'), primary_key=True),
|
|
db.Column('agent_id', ForeignKey('agents.id'), primary_key=True)
|
|
)
|
|
|
|
class ZlLanguage(Base):
|
|
"""
|
|
多语言配置表
|
|
用途:存储系统支持的语言列表
|
|
"""
|
|
__tablename__ = 'zl_languages'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
code: Mapped[str] = mapped_column(String(10), unique=True) # en, zh-CN, zh-TW, th
|
|
name: Mapped[str] = mapped_column(String(50)) # English, 简体中文, ...
|
|
is_active: Mapped[bool] = mapped_column(default=True)
|
|
|
|
class Department(Base):
|
|
"""
|
|
部门表
|
|
用途:存储公司部门列表
|
|
"""
|
|
__tablename__ = 'departments'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
name: Mapped[str] = mapped_column(String(100), unique=True)
|
|
code: Mapped[Optional[str]] = mapped_column(String(50), unique=True) # 部门编号
|
|
company: Mapped[Optional[str]] = mapped_column(String(100)) # 所属公司
|
|
description: Mapped[Optional[str]] = mapped_column(Text)
|
|
|
|
class UserHomepageConfig(Base):
|
|
"""
|
|
用户首页配置表
|
|
用途:存储用户自定义的首页布局与配置
|
|
"""
|
|
__tablename__ = 'user_homepage_configs'
|
|
|
|
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'), primary_key=True)
|
|
layout: Mapped[dict] = mapped_column(JSON, default={}) # 存储模块布局配置
|
|
# 示例: { "modules": [{"id": "favorites", "x": 0, "y": 0}, {"id": "agents", "x": 0, "y": 1}] }
|
|
|
|
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
class UserFavoriteAgent(Base):
|
|
"""
|
|
用户收藏 Agent 表
|
|
用途:存储用户收藏的 Agent
|
|
"""
|
|
__tablename__ = 'user_favorite_agents'
|
|
|
|
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'), primary_key=True)
|
|
agent_id: Mapped[int] = mapped_column(ForeignKey('agents.id'), primary_key=True)
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
|
|
class UserDefaultAgent(Base):
|
|
"""
|
|
用户默认 Agent 表 (嵌入首页)
|
|
用途:存储用户设置的默认嵌入 Agent (最多2个)
|
|
"""
|
|
__tablename__ = 'user_default_agents'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
|
|
agent_id: Mapped[int] = mapped_column(ForeignKey('agents.id'))
|
|
sort_order: Mapped[int] = mapped_column(default=0) # 排序,0为第一个,1为第二个
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
|
|
class User(Base):
|
|
"""
|
|
用户表
|
|
用途:存储系统用户信息
|
|
核心逻辑:包含部门、角色等字段,关联 Agent 权限
|
|
"""
|
|
__tablename__ = 'users'
|
|
|
|
# 主键 ID
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
# 用户名,唯一且带索引
|
|
username: Mapped[str] = mapped_column(String(50), unique=True, index=True)
|
|
# 邮箱,唯一
|
|
email: Mapped[str] = mapped_column(String(100), unique=True)
|
|
# 部门信息
|
|
department: Mapped[Optional[str]] = mapped_column(String(100))
|
|
# 角色:admin 或 user
|
|
role: Mapped[str] = mapped_column(String(20), default='user')
|
|
# 账号状态:active (正常), pending (待审批), disabled (禁用)
|
|
status: Mapped[str] = mapped_column(String(20), default='active')
|
|
# 密码哈希
|
|
password_hash: Mapped[Optional[str]] = mapped_column(String(255))
|
|
# 首选语言
|
|
language: Mapped[str] = mapped_column(String(10), default='zh-CN')
|
|
# 主题偏好: light / dark / system
|
|
theme_preference: Mapped[str] = mapped_column(String(10), default='system')
|
|
# 企业微信关联字段
|
|
wecom_userid: Mapped[Optional[str]] = mapped_column(String(128), unique=True, nullable=True)
|
|
wecom_unionid: Mapped[Optional[str]] = mapped_column(String(128), unique=True, nullable=True)
|
|
|
|
# 真实姓名
|
|
real_name: Mapped[Optional[str]] = mapped_column(String(50))
|
|
# 公司工号,唯一
|
|
employee_id: Mapped[Optional[str]] = mapped_column(String(50), unique=True)
|
|
# 手机号,唯一
|
|
phone_number: Mapped[Optional[str]] = mapped_column(String(20), unique=True)
|
|
|
|
# 默认嵌入的 Agent ID (忽略权限检查)
|
|
default_agent_id: Mapped[Optional[int]] = mapped_column(ForeignKey('agents.id'))
|
|
|
|
# 关系:用户拥有的 Agent 权限 (多对多)
|
|
agents: Mapped[List["Agent"]] = relationship(
|
|
secondary=user_agent_permissions,
|
|
back_populates="users"
|
|
)
|
|
|
|
# 密码重置 Token
|
|
reset_token: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
|
reset_token_expiry: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
|
|
def set_password(self, password):
|
|
self.password_hash = generate_password_hash(password)
|
|
|
|
def check_password(self, password):
|
|
if not self.password_hash:
|
|
return False
|
|
return check_password_hash(self.password_hash, password)
|
|
|
|
class Agent(Base):
|
|
"""
|
|
AI 应用配置表
|
|
用途:存储接入的 Dify Agent 配置信息
|
|
核心逻辑:存储 Dify App ID 和 API Key,控制应用开关
|
|
"""
|
|
__tablename__ = 'agents'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
# 应用名称
|
|
name: Mapped[str] = mapped_column(String(100))
|
|
# 应用描述
|
|
description: Mapped[Optional[str]] = mapped_column(Text)
|
|
# 图标 URL
|
|
icon_url: Mapped[Optional[str]] = mapped_column(String(255))
|
|
|
|
# Dify 配置:App ID (唯一,对于 iframe 模式可选)
|
|
dify_app_id: Mapped[Optional[str]] = mapped_column(String(100), unique=True, nullable=True)
|
|
# Dify 配置:API Key (实际生产应加密存储,对于 iframe 模式可选)
|
|
dify_api_key: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
|
|
|
# 类型:api (默认) 或 iframe
|
|
type: Mapped[str] = mapped_column(String(20), default='api')
|
|
# 自定义 API Base URL (覆盖全局配置)
|
|
api_base_url: Mapped[Optional[str]] = mapped_column(String(255))
|
|
# 额外配置 (JSON),用于存储 iframe_url 等
|
|
extra_config: Mapped[Optional[dict]] = mapped_column(JSON)
|
|
|
|
# 是否激活
|
|
is_active: Mapped[bool] = mapped_column(default=True)
|
|
# 创建时间
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
|
|
# 关系:拥有该应用权限的用户 (多对多)
|
|
users: Mapped[List["User"]] = relationship(
|
|
secondary=user_agent_permissions,
|
|
back_populates="agents"
|
|
)
|
|
|
|
class ChatSession(Base):
|
|
"""
|
|
聊天会话表
|
|
用途:独立持久化会话元数据,使生成中会话也能立即出现在历史记录中
|
|
"""
|
|
__tablename__ = 'chat_sessions'
|
|
__table_args__ = (
|
|
Index('ix_chat_sessions_user_agent_last_active', 'user_id', 'agent_id', 'last_active_at'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
session_id: Mapped[str] = mapped_column(String(100), unique=True, index=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'), index=True)
|
|
agent_id: Mapped[int] = mapped_column(ForeignKey('agents.id'), index=True)
|
|
title: Mapped[str] = mapped_column(String(255), default='')
|
|
status: Mapped[str] = mapped_column(String(20), default='running')
|
|
external_conversation_id: Mapped[Optional[str]] = mapped_column(String(100), index=True, nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
|
|
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
last_active_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
|
|
|
|
class ChatLog(Base):
|
|
"""
|
|
对话日志表
|
|
用途:存储用户与 Agent 的对话记录
|
|
"""
|
|
__tablename__ = 'chat_logs'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
|
|
agent_id: Mapped[int] = mapped_column(ForeignKey('agents.id'))
|
|
query: Mapped[str] = mapped_column(Text)
|
|
answer: Mapped[str] = mapped_column(Text)
|
|
latency: Mapped[int] = mapped_column(default=0)
|
|
conversation_id: Mapped[Optional[str]] = mapped_column(String(100))
|
|
session_id: Mapped[Optional[str]] = mapped_column(String(100), index=True, nullable=True)
|
|
message_type: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)
|
|
payload: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
|
|
tokens_used: Mapped[int] = mapped_column(default=0)
|
|
# Persist citation/source metadata so history messages can still show sources.
|
|
retriever_resources: Mapped[Optional[list]] = mapped_column(JSON)
|
|
attribution_parts: Mapped[Optional[list]] = mapped_column(JSON)
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
|
|
# 关联 Feedback
|
|
feedback: Mapped[Optional["Feedback"]] = relationship(back_populates="chat_log", uselist=False)
|
|
|
|
class Feedback(Base):
|
|
"""
|
|
反馈表
|
|
用途:存储用户对回答的评分与评价
|
|
"""
|
|
__tablename__ = 'feedbacks'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
chat_log_id: Mapped[int] = mapped_column(ForeignKey('chat_logs.id'), unique=True)
|
|
score: Mapped[int] = mapped_column(Integer) # 1-5
|
|
comment: Mapped[Optional[str]] = mapped_column(Text)
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
|
|
chat_log: Mapped["ChatLog"] = relationship(back_populates="feedback")
|
|
|
|
class GeneralFeedback(Base):
|
|
"""
|
|
通用意见反馈表
|
|
用于存储 Workspace/Chat 页面提交的通用意见反馈
|
|
"""
|
|
__tablename__ = 'general_feedbacks'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'), index=True)
|
|
chat_log_id: Mapped[Optional[int]] = mapped_column(ForeignKey('chat_logs.id'), nullable=True, index=True)
|
|
username: Mapped[str] = mapped_column(String(50), nullable=False)
|
|
feedback_type: Mapped[str] = mapped_column(String(32), nullable=False)
|
|
feedback_module: Mapped[str] = mapped_column(String(32), nullable=False)
|
|
content: Mapped[str] = mapped_column(Text, nullable=False)
|
|
source_page: Mapped[Optional[str]] = mapped_column(String(32))
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
|
|
|
|
class PermissionRequest(Base):
|
|
"""
|
|
权限申请表
|
|
用途:存储用户的权限申请记录
|
|
"""
|
|
__tablename__ = 'permission_requests'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
|
|
agent_id: Mapped[int] = mapped_column(ForeignKey('agents.id'))
|
|
reason: Mapped[str] = mapped_column(Text)
|
|
status: Mapped[str] = mapped_column(String(20), default='pending') # pending, approved, rejected
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
reviewed_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
|
|
|
|
class QuickQuestion(Base):
|
|
"""
|
|
常用问题表
|
|
用途:存储用户自定义的常用问题
|
|
"""
|
|
__tablename__ = 'quick_questions'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
|
agent_id: Mapped[int] = mapped_column(ForeignKey('agents.id'))
|
|
title: Mapped[str] = mapped_column(String(100))
|
|
question: Mapped[str] = mapped_column(Text)
|
|
sort_order: Mapped[int] = mapped_column(default=0)
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
|
|
class Announcement(Base):
|
|
"""
|
|
公告表
|
|
用途:存储系统公告
|
|
"""
|
|
__tablename__ = 'announcements'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
title: Mapped[str] = mapped_column(String(200))
|
|
content: Mapped[str] = mapped_column(Text)
|
|
is_active: Mapped[bool] = mapped_column(default=True)
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
class LoginAudit(Base):
|
|
"""
|
|
登录审计表
|
|
用途:记录密码登录与企业微信登录结果,便于排障与审计
|
|
"""
|
|
__tablename__ = 'login_audits'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
|
username: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)
|
|
source: Mapped[str] = mapped_column(String(20), default='password') # password / wecom
|
|
status: Mapped[str] = mapped_column(String(20), default='success') # success / failed
|
|
reason: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
error_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
match_rules: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
|
match_operator: Mapped[Optional[str]] = mapped_column(String(8), nullable=True)
|
|
match_result: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
wecom_userid: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
|
ip: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
user_agent: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
|
|
|
|
|
|
class WecomNotifyAudit(Base):
|
|
"""
|
|
企业微信触达审计表
|
|
用途:记录企微消息发送结果与回流点击数据
|
|
"""
|
|
__tablename__ = 'wecom_notify_audits'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
event_type: Mapped[str] = mapped_column(String(64), index=True)
|
|
status: Mapped[str] = mapped_column(String(20), default='sent', index=True) # sent / failed
|
|
trace_id: Mapped[str] = mapped_column(String(64), unique=True, index=True)
|
|
user_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True, index=True)
|
|
username: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)
|
|
wecom_userid: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
|
title: Mapped[Optional[str]] = mapped_column(String(120), nullable=True)
|
|
content: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
jump_url: Mapped[Optional[str]] = mapped_column(String(512), nullable=True)
|
|
error_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
error_message: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
|
|
opened_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True, index=True)
|
|
|
|
|
|
class AppSetting(Base):
|
|
"""
|
|
应用运行时配置表(Key/Value)
|
|
用途:将企业微信等管理员可编辑的配置长效保存到数据库,
|
|
避免容器/部署重建后写入 backend/.env 的修改丢失。
|
|
"""
|
|
__tablename__ = 'app_settings'
|
|
|
|
key: Mapped[str] = mapped_column(String(128), primary_key=True)
|
|
value: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
|
|
)
|
|
|
|
|
|
class KbGroup(Base):
|
|
"""
|
|
知识库分组
|
|
用途:承载数据清洗/知识库管理模块中的分组/知识库
|
|
"""
|
|
__tablename__ = 'kb_groups'
|
|
__table_args__ = (
|
|
UniqueConstraint('department_id', 'name', name='uq_kb_groups_department_name'),
|
|
Index('ix_kb_groups_department_active', 'department_id', 'is_active'),
|
|
Index('ix_kb_groups_dify_dataset_id', 'dify_dataset_id'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
name: Mapped[str] = mapped_column(String(128), index=True)
|
|
description: Mapped[Optional[str]] = mapped_column(Text)
|
|
department_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True, index=True)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
dify_dataset_id: Mapped[Optional[str]] = mapped_column(String(128))
|
|
dify_dataset_name: Mapped[Optional[str]] = mapped_column(String(255))
|
|
created_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'))
|
|
updated_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'))
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
created_by: Mapped[Optional["User"]] = relationship("User", foreign_keys=[created_by_id])
|
|
updated_by: Mapped[Optional["User"]] = relationship("User", foreign_keys=[updated_by_id])
|
|
documents: Mapped[List["KbDocument"]] = relationship(
|
|
"KbDocument",
|
|
back_populates="group",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
|
|
|
|
class KbDocument(Base):
|
|
"""
|
|
知识库逻辑文档
|
|
用途:表示一个文档条目,不直接等同于某一次上传文件
|
|
"""
|
|
__tablename__ = 'kb_documents'
|
|
__table_args__ = (
|
|
UniqueConstraint('group_id', 'title', name='uq_kb_documents_group_title'),
|
|
Index('ix_kb_documents_group_status', 'group_id', 'status'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
group_id: Mapped[int] = mapped_column(ForeignKey('kb_groups.id'), index=True)
|
|
title: Mapped[str] = mapped_column(String(255))
|
|
description: Mapped[Optional[str]] = mapped_column(Text)
|
|
status: Mapped[str] = mapped_column(String(32), default='draft', index=True)
|
|
source_type: Mapped[Optional[str]] = mapped_column(String(32))
|
|
is_deleted: Mapped[bool] = mapped_column(Boolean, default=False, index=True)
|
|
created_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'))
|
|
updated_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'))
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
group: Mapped["KbGroup"] = relationship("KbGroup", back_populates="documents")
|
|
created_by: Mapped[Optional["User"]] = relationship("User", foreign_keys=[created_by_id])
|
|
updated_by: Mapped[Optional["User"]] = relationship("User", foreign_keys=[updated_by_id])
|
|
versions: Mapped[List["KbDocumentVersion"]] = relationship(
|
|
"KbDocumentVersion",
|
|
back_populates="document",
|
|
cascade="all, delete-orphan",
|
|
order_by="desc(KbDocumentVersion.version_no)",
|
|
)
|
|
|
|
|
|
class KbDocumentVersion(Base):
|
|
"""
|
|
知识库文档版本
|
|
用途:记录每次上传、清洗、同步产生的版本信息
|
|
"""
|
|
__tablename__ = 'kb_document_versions'
|
|
__table_args__ = (
|
|
UniqueConstraint('document_id', 'version_no', name='uq_kb_document_versions_doc_version'),
|
|
Index('ix_kb_document_versions_doc_status', 'document_id', 'status'),
|
|
Index('ix_kb_document_versions_sha256', 'sha256'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
document_id: Mapped[int] = mapped_column(ForeignKey('kb_documents.id'), index=True)
|
|
version_no: Mapped[int] = mapped_column(Integer)
|
|
status: Mapped[str] = mapped_column(String(32), default='uploaded', index=True)
|
|
is_current: Mapped[bool] = mapped_column(Boolean, default=True, index=True)
|
|
source_file_name: Mapped[str] = mapped_column(String(255))
|
|
source_file_path: Mapped[str] = mapped_column(Text)
|
|
source_file_ext: Mapped[Optional[str]] = mapped_column(String(32))
|
|
source_file_size: Mapped[Optional[int]] = mapped_column(Integer)
|
|
cleaned_file_path: Mapped[Optional[str]] = mapped_column(Text)
|
|
cleaned_preview_path: Mapped[Optional[str]] = mapped_column(Text)
|
|
sha256: Mapped[Optional[str]] = mapped_column(String(64))
|
|
word_count: Mapped[int] = mapped_column(Integer, default=0)
|
|
error_message: Mapped[Optional[str]] = mapped_column(Text)
|
|
warnings_json: Mapped[Optional[dict]] = mapped_column(JSON)
|
|
uploaded_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'))
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
converted_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
|
|
|
|
document: Mapped["KbDocument"] = relationship("KbDocument", back_populates="versions")
|
|
uploaded_by: Mapped[Optional["User"]] = relationship("User", foreign_keys=[uploaded_by_id])
|
|
jobs: Mapped[List["KbJob"]] = relationship(
|
|
"KbJob",
|
|
back_populates="document_version",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
sync_records: Mapped[List["KbDifySyncRecord"]] = relationship(
|
|
"KbDifySyncRecord",
|
|
back_populates="document_version",
|
|
cascade="all, delete-orphan",
|
|
order_by="desc(KbDifySyncRecord.updated_at)",
|
|
)
|
|
|
|
|
|
class KbJob(Base):
|
|
"""
|
|
知识库后台任务
|
|
用途:记录上传、清洗、同步等后台任务执行情况
|
|
"""
|
|
__tablename__ = 'kb_jobs'
|
|
__table_args__ = (
|
|
Index('ix_kb_jobs_status_created', 'status', 'created_at'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
job_type: Mapped[str] = mapped_column(String(32), index=True)
|
|
status: Mapped[str] = mapped_column(String(32), default='queued', index=True)
|
|
queue_job_id: Mapped[Optional[str]] = mapped_column(String(64))
|
|
group_id: Mapped[Optional[int]] = mapped_column(ForeignKey('kb_groups.id'))
|
|
document_id: Mapped[Optional[int]] = mapped_column(ForeignKey('kb_documents.id'))
|
|
document_version_id: Mapped[Optional[int]] = mapped_column(ForeignKey('kb_document_versions.id'), index=True)
|
|
progress: Mapped[int] = mapped_column(Integer, default=0)
|
|
message: Mapped[Optional[str]] = mapped_column(Text)
|
|
error_message: Mapped[Optional[str]] = mapped_column(Text)
|
|
payload_json: Mapped[Optional[dict]] = mapped_column(JSON)
|
|
result_json: Mapped[Optional[dict]] = mapped_column(JSON)
|
|
created_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'))
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
started_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
|
|
finished_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
|
|
|
|
created_by: Mapped[Optional["User"]] = relationship("User", foreign_keys=[created_by_id])
|
|
document_version: Mapped[Optional["KbDocumentVersion"]] = relationship(
|
|
"KbDocumentVersion",
|
|
back_populates="jobs",
|
|
)
|
|
|
|
|
|
class KbDifySyncRecord(Base):
|
|
"""
|
|
知识库 Dify 同步记录
|
|
用途:记录本地文档版本与 Dify 远端文档之间的同步映射
|
|
"""
|
|
__tablename__ = 'kb_dify_sync_records'
|
|
__table_args__ = (
|
|
UniqueConstraint(
|
|
'document_version_id',
|
|
'remote_document_id',
|
|
name='uq_kb_dify_sync_records_version_remote'
|
|
),
|
|
Index('ix_kb_dify_sync_records_dataset_id', 'dataset_id'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
group_id: Mapped[Optional[int]] = mapped_column(ForeignKey('kb_groups.id'))
|
|
document_id: Mapped[Optional[int]] = mapped_column(ForeignKey('kb_documents.id'))
|
|
document_version_id: Mapped[int] = mapped_column(ForeignKey('kb_document_versions.id'), index=True)
|
|
dataset_id: Mapped[str] = mapped_column(String(128))
|
|
dataset_name: Mapped[Optional[str]] = mapped_column(String(255))
|
|
remote_document_id: Mapped[str] = mapped_column(String(128))
|
|
remote_document_name: Mapped[Optional[str]] = mapped_column(String(255))
|
|
status: Mapped[str] = mapped_column(String(32), default='pending', index=True)
|
|
sync_hash: Mapped[Optional[str]] = mapped_column(String(64))
|
|
error_message: Mapped[Optional[str]] = mapped_column(Text)
|
|
synced_by_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'))
|
|
synced_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
document_version: Mapped["KbDocumentVersion"] = relationship(
|
|
"KbDocumentVersion",
|
|
back_populates="sync_records",
|
|
)
|
|
synced_by: Mapped[Optional["User"]] = relationship("User", foreign_keys=[synced_by_id])
|
|
|
|
|
|
class WecomCallbackAudit(Base):
|
|
"""
|
|
企业微信回调审计表
|
|
用途:记录消息与事件回调(安全模式解密后的内容摘要)
|
|
"""
|
|
__tablename__ = 'wecom_callback_audits'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
msg_signature: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
|
timestamp: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
|
|
nonce: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
encrypt_raw: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
decrypted_xml: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
msg_type: Mapped[Optional[str]] = mapped_column(String(32), nullable=True, index=True)
|
|
event: Mapped[Optional[str]] = mapped_column(String(64), nullable=True, index=True)
|
|
from_user: Mapped[Optional[str]] = mapped_column(String(128), nullable=True, index=True)
|
|
agent_id: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
|
|
msg_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True, index=True)
|
|
error_message: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
|
|
|
|
|
|
class ScheduleTask(Base):
|
|
__tablename__ = 'schedule_tasks'
|
|
__table_args__ = (
|
|
Index('ix_schedule_tasks_enabled_next_run', 'enabled', 'next_run_at'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
schedule_no: Mapped[str] = mapped_column(String(64), unique=True, index=True)
|
|
name: Mapped[str] = mapped_column(String(128), index=True)
|
|
schedule_type: Mapped[str] = mapped_column(String(32), default='platform_app')
|
|
owner_user_id: Mapped[int] = mapped_column(ForeignKey('users.id'), index=True)
|
|
cron_expr: Mapped[str] = mapped_column(String(64))
|
|
wait_seconds: Mapped[int] = mapped_column(Integer, default=0)
|
|
dify_agent_id: Mapped[Optional[int]] = mapped_column(ForeignKey('agents.id'), nullable=True, index=True)
|
|
platform_api_path: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
|
|
data_source_type: Mapped[str] = mapped_column(String(32), default='database')
|
|
notify_user_ids_json: Mapped[Optional[list]] = mapped_column(JSON, default=[])
|
|
notify_enabled: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
enabled: Mapped[bool] = mapped_column(Boolean, default=True, index=True)
|
|
next_run_at: Mapped[Optional[datetime]] = mapped_column(DateTime, index=True)
|
|
last_run_at: Mapped[Optional[datetime]] = mapped_column(DateTime, index=True)
|
|
created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'))
|
|
updated_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'))
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
|
|
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
owner: Mapped[Optional["User"]] = relationship("User", foreign_keys=[owner_user_id])
|
|
dify_agent: Mapped[Optional["Agent"]] = relationship("Agent", foreign_keys=[dify_agent_id])
|
|
config: Mapped[Optional["ScheduleTaskConfig"]] = relationship("ScheduleTaskConfig", back_populates="task", uselist=False, cascade="all, delete-orphan")
|
|
|
|
|
|
class ScheduleTaskConfig(Base):
|
|
__tablename__ = 'schedule_task_configs'
|
|
__table_args__ = (
|
|
UniqueConstraint('schedule_task_id'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
schedule_task_id: Mapped[int] = mapped_column(ForeignKey('schedule_tasks.id'), index=True)
|
|
|
|
row_id_key: Mapped[str] = mapped_column(String(64), default='id')
|
|
log_prefix: Mapped[str] = mapped_column(String(128), default='')
|
|
|
|
base_fetch_sql: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
fetch_where_conditions: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
fetch_order_by: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
|
|
writeback_sql: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
detail_sql: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
alias_order: Mapped[Optional[list]] = mapped_column(JSON, default=[])
|
|
detail_alias_order: Mapped[Optional[list]] = mapped_column(JSON, default=[])
|
|
header_labels: Mapped[Optional[dict]] = mapped_column(JSON, default={})
|
|
|
|
ds_db_type: Mapped[str] = mapped_column(String(32), default='sqlserver')
|
|
ds_host: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
ds_port: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
|
ds_database: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
|
ds_username: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
|
ds_password: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
ds_driver: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
|
ds_encrypt: Mapped[Optional[str]] = mapped_column(String(16), nullable=True)
|
|
ds_trust_cert: Mapped[Optional[str]] = mapped_column(String(16), nullable=True)
|
|
|
|
api_url: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
|
|
api_method: Mapped[str] = mapped_column(String(16), default='POST')
|
|
api_headers: Mapped[Optional[dict]] = mapped_column(JSON, default={})
|
|
api_body_template: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
dify_system_prompt: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
task: Mapped[Optional["ScheduleTask"]] = relationship("ScheduleTask", back_populates="config")
|
|
|
|
|
|
class ScheduleRun(Base):
|
|
__tablename__ = 'schedule_runs'
|
|
__table_args__ = (
|
|
Index('ix_schedule_runs_task_created', 'schedule_task_id', 'created_at'),
|
|
Index('ix_schedule_runs_status_created', 'status', 'created_at'),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
run_no: Mapped[str] = mapped_column(String(64), unique=True, index=True)
|
|
schedule_task_id: Mapped[int] = mapped_column(ForeignKey('schedule_tasks.id'), index=True)
|
|
trigger_type: Mapped[str] = mapped_column(String(16), default='cron')
|
|
triggered_by_user_id: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'))
|
|
started_at: Mapped[Optional[datetime]] = mapped_column(DateTime, index=True)
|
|
finished_at: Mapped[Optional[datetime]] = mapped_column(DateTime, index=True)
|
|
duration_ms: Mapped[int] = mapped_column(Integer, default=0)
|
|
status: Mapped[str] = mapped_column(String(32), default='queued', index=True)
|
|
total_count: Mapped[int] = mapped_column(Integer, default=0)
|
|
success_count: Mapped[int] = mapped_column(Integer, default=0)
|
|
failed_count: Mapped[int] = mapped_column(Integer, default=0)
|
|
skipped_count: Mapped[int] = mapped_column(Integer, default=0)
|
|
api_request_json: Mapped[Optional[dict]] = mapped_column(JSON)
|
|
api_response_json: Mapped[Optional[dict]] = mapped_column(JSON)
|
|
response_summary_json: Mapped[Optional[dict]] = mapped_column(JSON)
|
|
step_logs: Mapped[Optional[list]] = mapped_column(JSON)
|
|
dify_execution_snapshot: Mapped[Optional[dict]] = mapped_column(JSON)
|
|
error_message: Mapped[Optional[str]] = mapped_column(Text)
|
|
excel_file_data: Mapped[Optional[bytes]] = mapped_column(LargeBinary)
|
|
excel_file_name: Mapped[Optional[str]] = mapped_column(String(255))
|
|
excel_file_size: Mapped[Optional[int]] = mapped_column(Integer)
|
|
wecom_notified: Mapped[bool] = mapped_column(Boolean, default=False, server_default=text('false'))
|
|
wecom_notified_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
|
|
wecom_notify_error: Mapped[Optional[str]] = mapped_column(Text)
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, index=True)
|
|
|
|
task: Mapped[Optional["ScheduleTask"]] = relationship("ScheduleTask", foreign_keys=[schedule_task_id])
|
|
|
|
class AgentGuide(Base):
|
|
"""
|
|
平台应用新手指南表
|
|
用途:存储 Agent 的新手指南文件信息 (如视频、PDF)
|
|
"""
|
|
__tablename__ = 'agent_guides'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
agent_id: Mapped[int] = mapped_column(ForeignKey('agents.id'), index=True)
|
|
file_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
file_type: Mapped[str] = mapped_column(String(50), nullable=False) # video, pdf, image
|
|
file_path: Mapped[str] = mapped_column(String(500), nullable=False)
|
|
file_size: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
|
remark: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
|
sort_order: Mapped[int] = mapped_column(Integer, default=0)
|
|
status: Mapped[int] = mapped_column(Integer, default=1) # 1: 启用, 0: 禁用
|
|
created_by: Mapped[Optional[int]] = mapped_column(ForeignKey('users.id'), nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
|
|
updated_at: Mapped[datetime] = mapped_column(default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
agent: Mapped[Optional["Agent"]] = relationship("Agent", foreign_keys=[agent_id])
|
|
|
|
|