Files
syops/backend/app/models/agent.py
T

86 lines
3.3 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.
"""
backend/app/models/agent.py
用途:Dify Agent 配置、新手指南、权限申请模型。
依赖:models.base(Base、user_agent_permissions)。
"""
from __future__ import annotations
from datetime import datetime
from typing import List, Optional
from sqlalchemy import DateTime, ForeignKey, Integer, JSON, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.models.base import Base, user_agent_permissions
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)
icon_url: Mapped[Optional[str]] = mapped_column(String(255))
dify_app_id: Mapped[Optional[str]] = mapped_column(String(100), unique=True, nullable=True)
dify_api_key: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
type: Mapped[str] = mapped_column(String(20), default='api')
api_base_url: Mapped[Optional[str]] = mapped_column(String(255))
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 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)
file_path: Mapped[str] = mapped_column(String(500), nullable=False)
file_size: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
guide_usage: Mapped[str] = mapped_column(String(50), nullable=False, default='general')
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)
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])
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')
created_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)
reviewed_at: Mapped[Optional[datetime]] = mapped_column(DateTime)