feat:新增投顾agent和nl2sqlagent

This commit is contained in:
2026-09-13 16:19:24 +08:00
parent c80c6acac0
commit 163192bf55
122 changed files with 7488 additions and 362 deletions
+41
View File
@@ -0,0 +1,41 @@
"""投顾 Agent 草稿 ORM 模型。"""
from __future__ import annotations
from datetime import datetime
from decimal import Decimal
from sqlalchemy import BigInteger, CheckConstraint, DateTime, JSON, Numeric, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column
from common.common_const import DRAFT_STATUS_DRAFT
from model.base import Base
class AdvisorDraft(Base):
__tablename__ = "advisor_draft"
__table_args__ = (
CheckConstraint(
"status IN ('draft', 'discarded')",
name="ck_advisor_draft_status",
),
{"comment": "投顾 Agent 草稿(不存 sent,不生成交易指令)"},
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
draft_id: Mapped[str] = mapped_column(String(64), unique=True, index=True)
customer_id: Mapped[int] = mapped_column(BigInteger, index=True)
advisor_id: Mapped[int] = mapped_column(BigInteger, index=True)
intent: Mapped[str] = mapped_column(String(32), index=True)
title: Mapped[str] = mapped_column(String(128))
content: Mapped[str] = mapped_column(Text)
structured_data: Mapped[dict | None] = mapped_column(JSON)
status: Mapped[str] = mapped_column(String(16), default=DRAFT_STATUS_DRAFT, index=True)
deviation: Mapped[Decimal | None] = mapped_column(Numeric(10, 4))
disclaimer_ok: Mapped[bool] = mapped_column(default=False)
warning: Mapped[str | None] = mapped_column(String(512))
create_time: Mapped[datetime] = mapped_column(
DateTime, server_default=func.now(), index=True
)
update_time: Mapped[datetime] = mapped_column(
DateTime, server_default=func.now(), onupdate=func.now()
)
+13 -4
View File
@@ -1,7 +1,7 @@
"""customer_relation 客户-投顾关系表 ORM 模型。
同时服务于:
- 投顾工作台(advisor):签约状态 unsigned/signed/closed,工作台为唯一写入方;
- 投顾工作台(advisor):签约状态 已分配/已签约/已结束,工作台为唯一写入方;
- 记忆/client_agent 模块:读取客户与投顾的当前或历史关系。
"""
@@ -9,15 +9,24 @@ from __future__ import annotations
from datetime import datetime
from sqlalchemy import BigInteger, DateTime, String, func
from sqlalchemy import BigInteger, CheckConstraint, DateTime, String, func
from sqlalchemy.orm import Mapped, mapped_column
from common.common_const import (
CUSTOMER_REL_STATUS_UNSIGNED,
)
from model.base import Base
class CustomerRelation(Base):
__tablename__ = "customer_relation"
__table_args__ = {"comment": "客户-投顾关系表(状态驱动:签约后投顾Agent方案正式触达客户)"}
__table_args__ = (
CheckConstraint(
"status IN ('已分配', '已签约', '已结束')",
name="ck_customer_relation_status",
),
{"comment": "客户-投顾关系表(状态驱动:签约后投顾Agent方案正式触达客户)"},
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
customer_id: Mapped[int] = mapped_column(BigInteger, nullable=False)
@@ -28,6 +37,6 @@ class CustomerRelation(Base):
signed_time: Mapped[datetime | None] = mapped_column(DateTime)
end_time: Mapped[datetime | None] = mapped_column(DateTime)
status: Mapped[str] = mapped_column(
String(16), nullable=False, server_default="unsigned"
String(16), nullable=False, server_default=CUSTOMER_REL_STATUS_UNSIGNED
)
reason: Mapped[str | None] = mapped_column(String(128))
+1 -1
View File
@@ -19,6 +19,6 @@ class EventLog(Base):
event_name: Mapped[str] = mapped_column(String(64))
payload: Mapped[dict[str, Any] | None] = mapped_column(JSON)
trace_id: Mapped[str | None] = mapped_column(String(64))
status: Mapped[str] = mapped_column(String(16), server_default="待消费")
status: Mapped[str] = mapped_column(String(16), server_default="pending")
create_time: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
consume_time: Mapped[datetime | None] = mapped_column(DateTime)
+24
View File
@@ -0,0 +1,24 @@
"""基金业绩指标缓存 ORM 模型。"""
from __future__ import annotations
from datetime import date, datetime
from decimal import Decimal
from sqlalchemy import BigInteger, Date, DateTime, Numeric, String, func
from sqlalchemy.orm import Mapped, mapped_column
from model.base import Base
class FundPerformance(Base):
__tablename__ = "fund_performance"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
product_id: Mapped[int] = mapped_column(BigInteger, index=True)
period: Mapped[str] = mapped_column(String(16))
return_rate: Mapped[Decimal | None] = mapped_column(Numeric(10, 4))
annual_volatility: Mapped[Decimal | None] = mapped_column(Numeric(10, 4))
max_drawdown: Mapped[Decimal | None] = mapped_column(Numeric(10, 4))
sharpe: Mapped[Decimal | None] = mapped_column(Numeric(10, 4))
calc_date: Mapped[date] = mapped_column(Date)
create_time: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
+129
View File
@@ -0,0 +1,129 @@
"""NL2SQL 查询权限和查询历史 ORM 模型。"""
from __future__ import annotations
from datetime import datetime
from sqlalchemy import BigInteger, Boolean, CheckConstraint, DateTime, JSON, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column
from model.base import Base
class Nl2SqlQueryRole(Base):
__tablename__ = "nl2sql_query_role"
__table_args__ = (
CheckConstraint("can_query IN (0, 1)", name="ck_nl2sql_role_can_query"),
CheckConstraint("status IN ('active', 'inactive')", name="ck_nl2sql_role_status"),
{"comment": "NL2SQL 查询角色,按 sys_user.employee_role 映射"},
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
role_code: Mapped[str] = mapped_column(String(64), unique=True)
role_name: Mapped[str] = mapped_column(String(128))
employee_role: Mapped[str] = mapped_column(String(32), unique=True)
can_query: Mapped[bool] = mapped_column(Boolean, default=False)
max_rows: Mapped[int] = mapped_column(BigInteger, default=1000)
daily_quota: Mapped[int] = mapped_column(BigInteger, default=0)
status: Mapped[str] = mapped_column(String(16), default="active", index=True)
create_time: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
update_time: Mapped[datetime] = mapped_column(
DateTime, server_default=func.now(), onupdate=func.now()
)
class Nl2SqlRoleTablePermission(Base):
__tablename__ = "nl2sql_role_table_permission"
__table_args__ = (
CheckConstraint("permission = 'SELECT'", name="ck_nl2sql_table_permission"),
CheckConstraint(
"row_scope_type IN ('none', 'customer_ids', 'product_ids')",
name="ck_nl2sql_row_scope_type",
),
CheckConstraint("status IN ('active', 'inactive')", name="ck_nl2sql_table_status"),
{"comment": "NL2SQL 查询角色的表和行级权限"},
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
role_id: Mapped[int] = mapped_column(BigInteger, index=True)
table_name: Mapped[str] = mapped_column(String(128))
permission: Mapped[str] = mapped_column(String(16), default="SELECT")
row_scope_type: Mapped[str] = mapped_column(String(32), default="none")
row_scope_column: Mapped[str | None] = mapped_column(String(128))
status: Mapped[str] = mapped_column(String(16), default="active", index=True)
create_time: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
update_time: Mapped[datetime] = mapped_column(
DateTime, server_default=func.now(), onupdate=func.now()
)
class Nl2SqlRoleColumnPermission(Base):
__tablename__ = "nl2sql_role_column_permission"
__table_args__ = (
CheckConstraint(
"access_mode IN ('allow', 'deny', 'mask')",
name="ck_nl2sql_column_access_mode",
),
CheckConstraint("status IN ('active', 'inactive')", name="ck_nl2sql_column_status"),
{"comment": "NL2SQL 查询角色的字段权限"},
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
role_id: Mapped[int] = mapped_column(BigInteger, index=True)
table_name: Mapped[str] = mapped_column(String(128))
column_name: Mapped[str] = mapped_column(String(128))
access_mode: Mapped[str] = mapped_column(String(16), default="allow")
mask_type: Mapped[str | None] = mapped_column(String(32))
status: Mapped[str] = mapped_column(String(16), default="active", index=True)
create_time: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
update_time: Mapped[datetime] = mapped_column(
DateTime, server_default=func.now(), onupdate=func.now()
)
class Nl2SqlSensitiveField(Base):
__tablename__ = "nl2sql_sensitive_field"
__table_args__ = (
CheckConstraint("status IN ('active', 'inactive')", name="ck_nl2sql_sensitive_status"),
{"comment": "NL2SQL 全局敏感字段规则"},
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
table_name: Mapped[str] = mapped_column(String(128))
column_name: Mapped[str] = mapped_column(String(128))
mask_type: Mapped[str] = mapped_column(String(32), default="partial")
status: Mapped[str] = mapped_column(String(16), default="active", index=True)
description: Mapped[str | None] = mapped_column(String(255))
create_time: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
update_time: Mapped[datetime] = mapped_column(
DateTime, server_default=func.now(), onupdate=func.now()
)
class Nl2SqlQueryHistory(Base):
__tablename__ = "nl2sql_query_history"
__table_args__ = (
CheckConstraint(
"status IN ('success', 'failed', 'blocked', 'timeout')",
name="ck_nl2sql_history_status",
),
{"comment": "NL2SQL 查询归档,不保存完整结果行"},
)
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
query_id: Mapped[str] = mapped_column(String(64), unique=True, index=True)
user_id: Mapped[int] = mapped_column(BigInteger, index=True)
session_id: Mapped[str | None] = mapped_column(String(64), index=True)
caller_agent: Mapped[str | None] = mapped_column(String(64), index=True)
question: Mapped[str] = mapped_column(Text)
generated_sql: Mapped[str | None] = mapped_column(Text)
access_tables: Mapped[list | None] = mapped_column(JSON)
status: Mapped[str] = mapped_column(String(16), index=True)
error_code: Mapped[str | None] = mapped_column(String(64))
error_message: Mapped[str | None] = mapped_column(String(512))
row_count: Mapped[int] = mapped_column(BigInteger, default=0)
truncated: Mapped[bool] = mapped_column(Boolean, default=False)
elapsed_ms: Mapped[float | None] = mapped_column()
trace_id: Mapped[str | None] = mapped_column(String(64), index=True)
create_time: Mapped[datetime] = mapped_column(
DateTime, server_default=func.now(), index=True
)