一、客服 Agent 智能增强(正面回应"不智能、动不动就转人工")
- 决策链由 2 个出口扩到 5 个:E1 澄清 / E2 计算型 / E3 知识直返 / E4 证据约束生成 / E5 分级回退
- 转人工从"默认动作"降为最后一档 E5c,只保留 4 类白名单:
P0 反诈 / P1 账户与个人数据 / P2 写操作与争议 / 用户明确要求人工
- 46 条金标实测(修复前 → 修复后):
转人工率 43.5% → 10.9%;出口准确率 45.7% → 100%;事实正确率 69.6% → 100%
禁忌违反 1 → 0;档位越权 / 无出处数字 / 误拒 四项零容忍全 0
- 安全不变量 INV-1~INV-5;零容忍规则未删,改的是挂载点
(输出侧字面黑名单 → 检索层档位隔离 + 判定层合规词表 + 输出守护)
二、知识库:档位单点化与物理隔离
- 新增 app/core/knowledge_tier.py 作为档位规则唯一落点(G-03),
knowledge_contracts.py 原定义块改为显式再导出(X as X,非副本)
- 档位过滤由 bool 默认值(fail-open)改为 tiers 必填集合(缺参即 TypeError)
- Milvus 侧四集合按 visibility 分区键物理隔离;双 schema 收敛为一套
- 新增 app/core/actor.py:访客三元组与匿名判定的唯一构造/判定点(G-01/G-01b)
- 新增 app/core/fund_fee_rules.py:费率计算纯函数
三、前端入参边界对齐(本轮 W11 新修,4 处"校验宽于存储")
- message 加 max_length=8000(与浮窗 widget.js 的 maxlength 一致)
- session_id 加 1—64;idempotency_key 上限 128 → 64(对齐列宽 String(64))
- feedback_type 加 max_length=32(对齐列宽 String(32))
- 8 条路径参数补 min_length=1 + max_length=64 + 字符集正则
({session_id} / {run_id} / {handover_id})
- 改前超限值会落到 MySQL 才失败(500);改后一律 422 AGENT_INPUT_INVALID + 字段级定位
- 新增 tests/unit/api/test_frontend_boundaries.py(33 例),含"端点表 ↔ OpenAPI 全量对照"
四、投顾模块整体清除(D4.4 / D4.5)
- 删除投顾相关 controller / schema / model / repository / service 及门户页面
- tools/portal_api_check.py 同步作废 AD003/AD005/AD011/A047 四条用例与 advisor_t 登录
(端点与账号均已不存在,此前稳定报 3 条假红)
五、验证(提交前实测)
- pytest -q:1856 passed / 2 skipped / 0 failed
- ruff check app tools tests:19(= 基线);mypy app:2(= 基线)
- 前端接口契约体检 portal_api_check.py:38 项,通过 34,失败 0,跳过 4
- 全链路冒烟 e2e_smoke_test.py --read-only:31/31
- HTTP 全链路探针 http_probe.py:11/11 succeeded
- 跨文档一致性 _consistency.py:GATE PASS
- 真机边界复验 12 条:12/12 符合预期
六、纪律与文档
- 可改文件白名单 A-09(docs/46)与底座会签申请单 A-10(docs/47,组 1—组 4 全部受理)
- 零 DDL:未新增/修改任何表结构,89 张业务表与基线一致
- 证据留痕:docs/evidence/**(含 46 条金标 score、快照、清除与重建记录)
- 未提交(刻意排除,见提交说明):仓库内 客服agent/ 与 开发文档/ 是 2026-09-16 前的
过期副本(Todolist 440 行 vs 权威 D2.1 1167 行),权威正本在仓库外;
_chunks_report.txt 是 tools/build_knowledge_chunks.py 生成的本地产物
174 lines
7.3 KiB
Python
174 lines
7.3 KiB
Python
from typing import Any, Literal
|
||
|
||
from fastapi import APIRouter, Depends, Header, Path, Query
|
||
from pydantic import Field
|
||
|
||
from app.api.dependencies.auth import build_request_context
|
||
from app.api.dependencies.rate_limit import enforce_rate_limit
|
||
from app.api.schemas.admin import StrictPayload
|
||
from app.api.schemas.conversations import HandoverRequest
|
||
from app.core.contracts import RequestContext
|
||
from app.service.public_platform_service import PublicPlatformService
|
||
from app.service.public_product_service import PublicProductService
|
||
|
||
router = APIRouter(prefix="/api/v1", tags=["public-platform"],
|
||
dependencies=[Depends(enforce_rate_limit)])
|
||
|
||
|
||
class SessionCreate(StrictPayload):
|
||
agent_type: str = Field(pattern=r"^[a-z][a-z0-9_]{1,31}$")
|
||
|
||
|
||
class Cancellation(StrictPayload):
|
||
reason: str = Field(default="user_cancelled", max_length=128)
|
||
|
||
|
||
class CandidateDecisionPayload(StrictPayload):
|
||
decision: Literal["confirmed", "rejected"]
|
||
|
||
|
||
@router.post("/conversations", status_code=201)
|
||
async def create_session(
|
||
payload: SessionCreate,
|
||
key: str | None = Header(default=None, alias="Idempotency-Key"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().write("create", "", context, key, payload.model_dump())
|
||
|
||
|
||
@router.get("/conversations/{session_id}")
|
||
async def get_session(
|
||
session_id: str = Path(min_length=1, max_length=64, pattern=r"^[A-Za-z0-9_-]+$"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().session(session_id, context)
|
||
|
||
|
||
@router.post("/conversations/{session_id}/closures")
|
||
async def close_session(
|
||
session_id: str = Path(min_length=1, max_length=64, pattern=r"^[A-Za-z0-9_-]+$"),
|
||
key: str | None = Header(default=None, alias="Idempotency-Key"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().write("close", session_id, context, key, {})
|
||
|
||
|
||
@router.post("/agent-runs/{run_id}/cancellations", status_code=202)
|
||
async def cancel_run(
|
||
payload: Cancellation,
|
||
run_id: str = Path(min_length=1, max_length=64, pattern=r"^[A-Za-z0-9_-]+$"),
|
||
key: str | None = Header(default=None, alias="Idempotency-Key"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().write("cancel", run_id, context, key, payload.model_dump())
|
||
|
||
|
||
@router.get("/handover-requests/{handover_id}")
|
||
async def get_handover(
|
||
handover_id: str = Path(min_length=1, max_length=64, pattern=r"^[A-Za-z0-9_-]+$"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().handover(handover_id, context)
|
||
|
||
|
||
# 转人工申请的唯一入口:必须走 PublicPlatformService.write("handover"),
|
||
# 它在同一事务内写工单 + Outbox 事件(conversation.transfer_requested)+ 审计。
|
||
# 历史上 conversations.py 另有一份只写审计的实现,因注册顺序覆盖了本入口,
|
||
# 导致转人工的异步链路(Outbox → Worker)永不触发,已合并删除。
|
||
@router.post("/conversations/{session_id}/handover-requests", status_code=202)
|
||
async def request_handover(
|
||
payload: HandoverRequest,
|
||
session_id: str = Path(min_length=1, max_length=64, pattern=r"^[A-Za-z0-9_-]+$"),
|
||
key: str | None = Header(default=None, alias="Idempotency-Key"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().write(
|
||
"handover", session_id, context, key, payload.model_dump()
|
||
)
|
||
|
||
|
||
@router.get("/users/me/memory-profile")
|
||
async def my_memory(
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().memory(int(context.user_id), context)
|
||
|
||
|
||
@router.get("/customers/{customer_id}/memory-profile")
|
||
async def customer_memory(
|
||
customer_id: int, context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().memory(customer_id, context)
|
||
|
||
|
||
@router.get("/users/me/memories")
|
||
async def my_memories_debug(
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
query: str | None = None,
|
||
limit: int = 10,
|
||
) -> dict[str, Any]:
|
||
"""记忆系统可观测端点:一次返回「库里有什么」「能不能召回到」「事件有没有被消费」。
|
||
|
||
这是排查"记忆到底有没有在工作"的唯一出口 —— 在此之前,`memory_unit` 原始行
|
||
没有任何读接口,`GET /users/me/memory-profile` 只返回画像快照(记忆的下游产物),
|
||
因此"写入成功但画像还没重建"与"根本没写入"在外部完全无法区分。
|
||
"""
|
||
return await PublicPlatformService().memories_debug(
|
||
int(context.user_id), context, query=query, limit=limit
|
||
)
|
||
|
||
|
||
@router.get("/users/me/memory-candidates")
|
||
async def my_memory_candidates(
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
"""返回当前用户可确认的画像候选,不返回证据原文。"""
|
||
from app.service.customer_profile_candidate_service import CustomerProfileCandidateService
|
||
|
||
return await CustomerProfileCandidateService().list_for_customer(context)
|
||
|
||
|
||
@router.post("/users/me/memory-candidates/{candidate_id}/decisions")
|
||
async def decide_memory_candidate(
|
||
candidate_id: int,
|
||
payload: CandidateDecisionPayload,
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
"""用户确认或拒绝自己的候选;确认后仍需管理员审核才能激活。"""
|
||
from app.service.customer_profile_candidate_service import CustomerProfileCandidateService
|
||
|
||
return await CustomerProfileCandidateService().decide_by_customer(
|
||
candidate_id, payload.decision, context
|
||
)
|
||
|
||
|
||
@router.get("/products")
|
||
async def list_products(
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
"""公开产品列表:在售场内基金 + 各自最新行情(访客令牌即可访问)。
|
||
|
||
这是访客三个页面(首页推荐 / 产品列表 / 产品详情)的数据源,
|
||
替代原先前端手写的 `common/mock-data.js`。
|
||
|
||
只要求**有效令牌**、不检查权限码:访客令牌的上下文只有 `roles=("visitor",)`
|
||
且不带权限,与 `/api/v1/conversations`、`/api/v1/agent-runs` 的访客口径一致。
|
||
"""
|
||
return await PublicProductService().list_products(context)
|
||
|
||
|
||
@router.get("/products/{product_code}/nav-history")
|
||
async def product_nav_history(
|
||
product_code: str,
|
||
days: int = Query(default=90, ge=1, le=365),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
"""产品历史净值序列(编号 `P002`):产品详情页净值走势图的数据源。
|
||
|
||
数据来自 `fin_nav_history`,由 `tools/sync_nav_history.py` 从东财净值接口同步。
|
||
鉴权口径与 P001 相同:**要求有效令牌但不校验权限码**(访客令牌可用)。
|
||
|
||
表为空时返回 `count=0` 与空数组,**不是错误** —— 前端据此显示"尚未接入"。
|
||
"""
|
||
return await PublicProductService().nav_history(product_code, context, days=days)
|