Files
wecom_it_smart_desk/backend/app/models/exclusion_rule.py
T

147 lines
4.5 KiB
Python
Raw Normal View History

# =============================================================================
# 企微IT智能服务台 — 代答排除规则模型
# =============================================================================
# 说明:对应数据库 exclusion_rules 表
# 存储代答排除规则,AI回复前按优先级依次检查,命中则执行对应动作。
# =============================================================================
import uuid
from datetime import datetime
from typing import List, Optional
from sqlalchemy import DateTime, Index, Integer, JSON, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
class ExclusionRule(Base):
"""代答排除规则模型 — 对应 exclusion_rules 表。
存储排除规则配置,支持4种匹配方式(关键词/正则/意图/分类),
命中后执行4种动作(转人工/转人工+上下文/仅提示/静默转人工)。
Attributes:
id: 规则IDUUID
rule_name: 规则名称(唯一)
rule_description: 规则描述
priority: 优先级(P0/P1/P2/P3
match_type: 匹配方式(keyword/regex/intent/category
match_condition: 匹配条件(关键词列表/正则表达式/意图ID列表/分类名称列表)
match_scope: 匹配范围(JSON数组,如 ["ai_auto_reply"]
action_type: 命中后动作类型
transfer_message: 转人工提示语
status: 状态(enabled/disabled
hit_count: 命中次数
created_by: 创建人ID
created_at: 创建时间
updated_at: 更新时间
"""
__tablename__ = "exclusion_rules"
# 主键
id: Mapped[str] = mapped_column(
String(36),
primary_key=True,
default=lambda: str(uuid.uuid4()),
)
# 规则基本信息
rule_name: Mapped[str] = mapped_column(
String(200),
nullable=False,
unique=True,
comment="规则名称(唯一)",
)
rule_description: Mapped[Optional[str]] = mapped_column(
Text,
nullable=True,
comment="规则描述",
)
priority: Mapped[str] = mapped_column(
String(5),
nullable=False,
default="P2",
comment="优先级:P0/P1/P2/P3",
)
# 匹配配置
match_type: Mapped[str] = mapped_column(
String(20),
nullable=False,
comment="匹配方式:keyword/regex/intent/category",
)
match_condition: Mapped[str] = mapped_column(
Text,
nullable=False,
comment="匹配条件:关键词列表/正则/意图ID列表/分类名称列表",
)
match_scope: Mapped[List[str]] = mapped_column(
JSON,
nullable=False,
default=lambda: ["ai_auto_reply"],
comment="匹配范围",
)
# 命中后动作
action_type: Mapped[str] = mapped_column(
String(50),
nullable=False,
default="transfer_human",
comment="命中后动作:transfer_human/transfer_human_with_context/prompt_transfer/silent_transfer",
)
transfer_message: Mapped[Optional[str]] = mapped_column(
Text,
nullable=True,
comment="转人工提示语",
)
# 状态
status: Mapped[str] = mapped_column(
String(10),
nullable=False,
default="enabled",
comment="状态:enabled/disabled",
)
hit_count: Mapped[int] = mapped_column(
Integer,
nullable=False,
default=0,
comment="命中次数",
)
# 审计
created_by: Mapped[str] = mapped_column(
String(100),
nullable=False,
comment="创建人ID",
)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
nullable=False,
default=datetime.now,
comment="创建时间",
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
nullable=False,
default=datetime.now,
onupdate=datetime.now,
comment="更新时间",
)
# 索引
__table_args__ = (
Index("idx_exclusion_rules_status", "status"),
Index("idx_exclusion_rules_priority", "priority"),
Index("idx_exclusion_rules_match_type", "match_type"),
)
def __repr__(self) -> str:
"""排除规则对象的字符串表示。"""
return (
f"<ExclusionRule(id={self.id}, name={self.rule_name}, "
f"priority={self.priority}, status={self.status})>"
)