feat(ai): W1-AI-CLOSURE 超级 Sprint — 9 APP 全链路收口 + chat 上下文真激活

Phase 2.3 chat 上下文捕获链路从未真正激活到完整工作:
- 14 处 ai-float-button 补 sourcePage,chat.ts 三分支同步设 pageFilters.contextId
- 后端 page_context 4 层 BUG 修(列名错位 + RLS site_id 未重设)
- xcx_chat filters.pop 破坏 body.page_context 引用 — dict() 浅拷贝隔离
- chat 流式 markdown 实时解析(表格/标题/列表/加粗 + KPI 富卡)
- reference_card KPI 富卡接入 SSE 路径,db 真写入
- 维客线索 source 显示规则:AI 来源用机器人 icon 替代长文字

数据库:
- public.member_retention_clue 加 emoji + runtime_mode + sandbox_instance_id
- biz.ai_run_logs 加 assistant_id + 复合索引
- chk_ai_cache_type CHECK 约束 8 类应用名
- cache_type / app_type 命名统一(app6_note / app7_customer / app8_consolidation)
- 历史 emoji 抽取脚本 44/44 成功

后端 silent failure 修:
- cleanup_service WHERE app_type → cache_type(90 天清理 + 20K 上限重新生效)
- _build_ai_insight 字段错位修复(app4 → app7 + 字段对齐 prompt schema)
- task_manager talkingPoints 改 app5_tactics + tactics 字段
- task_manager aiSuggestion 改取 one_line_summary
- cache_service.CACHE_EXPIRY_DAYS 加 app2a_finance_area
- WS /ws/ai-cache 加 token + JWT + site_id 校验(P0 信息泄露漏洞)
- internal_ai token 改 hmac.compare_digest

工具/文档:
- main.py 加 RotatingFileHandler logs/backend.log + uvicorn /health 过滤
- 新建 utils/clue_category.py(VI 6 类配色 + emoji fallback + source 显示规则)
- 新建 utils/markdown.ts(轻量 md 转 rich-text 解析 + streaming 容错)
- audit + 数据库变更说明 + backlog §七 #14 收口 + #15-#38 残余子任务
- backlog 追加 §十一 App1 参数/MCP/沙箱审计 + §十二 百炼/SQL MCP 主任务线

实地 MCP 走查:14 入口数据层 + 5 代表入口 sourcePage 注入 + customer-detail 全模块 + chat md 渲染 + reference_card 富卡 都已验证。9 项预先 BUG/UX 登记 §七 #29-#38 后续修复。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Neo
2026-05-06 16:39:07 +08:00
parent c9c2bce101
commit 2dfc926f96
56 changed files with 1983 additions and 278 deletions

View File

@@ -26,15 +26,18 @@ class AICleanupService:
RETENTION_DAYS = 90
CACHE_LIMIT_PER_APP = 20_000
CACHE_APP_TYPES = [
# 8 类需要走 ai_cache 的应用(app1_chat 走 ai_messages,不进 ai_cache)
# 命名与 prompt 文件名一致(W1-AI-CLOSURE 组 1 数据库迁移已统一)。
CACHE_TYPES: tuple[str, ...] = (
"app2_finance",
"app2a_finance_area", # P0-11 修正:之前漏掉,导致 64 区域组合永不清理
"app3_clue",
"app4_analysis",
"app5_tactics",
"app6_note_analysis",
"app7_customer_analysis",
"app8_clue_consolidated",
]
"app6_note",
"app7_customer",
"app8_consolidation",
)
async def run_cleanup(self) -> dict:
"""执行全部清理,返回各步骤删除记录数。
@@ -119,7 +122,12 @@ class AICleanupService:
conn.close()
async def _cleanup_cache(self) -> dict[str, int]:
"""每个 App 类型保留最新 20,000 条,删除超出部分。"""
"""每个 cache_type 保留最新 20,000 条,删除超出部分。
P0-8 修正:ai_cache 表列名是 cache_type 不是 app_type;之前 SQL `WHERE
app_type=%s` 一直抛 UndefinedColumn 错误被 except 静默吞,导致 90 天清理
与 20K 上限完全失效,生产 ai_cache 表无限膨胀。
"""
from app.database import get_connection
result: dict[str, int] = {}
@@ -127,35 +135,35 @@ class AICleanupService:
try:
with conn.cursor() as cur:
cur.execute("SET statement_timeout = 300000")
for app_type in self.CACHE_APP_TYPES:
for cache_type in self.CACHE_TYPES:
try:
# 子查询找到该 app_type 第 20001 条的 created_at 作为截断点
# 子查询:找到该 cache_type 第 20001 条的 created_at 作为截断点
cur.execute(
"""
DELETE FROM biz.ai_cache
WHERE app_type = %s
WHERE cache_type = %s
AND id NOT IN (
SELECT id FROM biz.ai_cache
WHERE app_type = %s
WHERE cache_type = %s
ORDER BY created_at DESC
LIMIT %s
)
""",
(app_type, app_type, self.CACHE_LIMIT_PER_APP),
(cache_type, cache_type, self.CACHE_LIMIT_PER_APP),
)
deleted = cur.rowcount
result[app_type] = deleted
result[cache_type] = deleted
if deleted > 0:
logger.info(
"清理 ai_cache [%s]: 删除 %d",
app_type,
cache_type,
deleted,
)
except Exception:
logger.exception("清理 ai_cache [%s] 失败", app_type)
result[app_type] = -1
logger.exception("清理 ai_cache [%s] 失败", cache_type)
result[cache_type] = -1
conn.rollback()
# 重新开始事务以继续后续 app_type
# 重新开始事务以继续后续 cache_type
continue
conn.commit()
return result

View File

@@ -316,8 +316,13 @@ class ChatService:
conn = get_connection()
try:
with conn.cursor() as cur:
# W1-AI-CLOSURE 组 4 修正(P0-12):排除 role='system' 消息;
# 历史 35 条 system 行被前端误渲染成内容暴露 prompt 拼装细节。
cur.execute(
"SELECT COUNT(*) FROM biz.ai_messages WHERE conversation_id = %s",
"""
SELECT COUNT(*) FROM biz.ai_messages
WHERE conversation_id = %s AND role IN ('user', 'assistant')
""",
(chat_id,),
)
total = cur.fetchone()[0]
@@ -326,7 +331,7 @@ class ChatService:
"""
SELECT id, role, content, created_at, reference_card
FROM biz.ai_messages
WHERE conversation_id = %s
WHERE conversation_id = %s AND role IN ('user', 'assistant')
ORDER BY created_at ASC
LIMIT %s OFFSET %s
""",

View File

@@ -143,13 +143,13 @@ async def get_customer_detail(customer_id: int, site_id: int) -> dict:
# ── 扩展模块(独立 try/except 优雅降级)──
try:
ai_insight = _build_ai_insight(customer_id, conn)
ai_insight = _build_ai_insight(customer_id, site_id, conn)
except Exception:
logger.warning("构建 aiInsight 失败,降级为空", exc_info=True)
ai_insight = {"summary": "", "strategies": []}
try:
retention_clues = _build_retention_clues(customer_id, conn)
retention_clues = _build_retention_clues(customer_id, site_id, conn)
except Exception:
logger.warning("构建 retentionClues 失败,降级为空列表", exc_info=True)
retention_clues = []
@@ -223,24 +223,29 @@ async def get_customer_detail(customer_id: int, site_id: int) -> dict:
# ── 3.2 AI 洞察 / 维客线索 / 备注 ──────────────────────────
def _build_ai_insight(customer_id: int, conn) -> dict:
def _build_ai_insight(customer_id: int, site_id: int, conn) -> dict:
"""
构建 aiInsight 模块。
构建 aiInsight 模块(来自 App7 客户分析缓存)
查询 biz.ai_cache WHERE cache_type='app4_analysis' AND target_id=customerId
解析 result_json JSON。无缓存时返回空默认值。
查询 biz.ai_cache WHERE cache_type='app7_customer' AND target_id=customerId
AND site_id=site_id。无缓存时返回空默认值。
App7Result schema: {strategies: [{title, content}], summary: str}
"""
with conn.cursor() as cur:
cur.execute(
"""
SELECT result_json
FROM biz.ai_cache
WHERE cache_type = 'app4_analysis'
WHERE cache_type = 'app7_customer'
AND site_id = %s
AND target_id = %s
AND COALESCE(status, 'valid') = 'valid'
AND (expires_at IS NULL OR expires_at > now())
ORDER BY created_at DESC
LIMIT 1
""",
(str(customer_id),),
(site_id, str(customer_id)),
)
row = cur.fetchone()
@@ -255,37 +260,69 @@ def _build_ai_insight(customer_id: int, conn) -> dict:
summary = data.get("summary", "")
strategies_raw = data.get("strategies", [])
strategies = []
# App7Result schema: strategies = [{title, content}],但前端 demo 标杆 wxml 用单
# 字段 text(`{{item.text}}`)。在 service 层拼接成 demo 形态,前端保持 demo 一致。
for s in strategies_raw:
if isinstance(s, dict):
strategies.append({
"color": s.get("color", ""),
"text": s.get("text", ""),
})
t = (s.get("title") or "").strip()
c = (s.get("content") or "").strip()
text = f"{t}{c}" if t and c else (c or t)
strategies.append({"text": text})
return {"summary": summary, "strategies": strategies}
def _build_retention_clues(customer_id: int, conn) -> list[dict]:
def _build_retention_clues(customer_id: int, site_id: int, conn) -> list[dict]:
"""
构建 retentionClues 模块。
查询 public.member_retention_clue,按 recorded_at 倒序。
查询 public.member_retention_clue(含 emoji 独立列、source、detail
按 recorded_at 倒序,跨 source(manual / ai_consumption / ai_note)合并。
返回字段对齐 clue-card 组件契约 + xcx_customers.RetentionClue schema:
{tag, tag_color, emoji, text, source, desc}
"""
# CHANGE 2026-03-23 | BUG: clue_type/clue_text 列不存在,应为 category/summarycreated_at → recorded_at
from app.utils.clue_category import (
CATEGORY_TAG_COLOR,
SOURCE_DISPLAY_NAME,
format_category_tag,
)
with conn.cursor() as cur:
cur.execute(
"""
SELECT category, summary
SELECT category, summary, detail, emoji, source, recorded_by_name
FROM public.member_retention_clue
WHERE member_id = %s
AND site_id = %s
AND is_hidden = false
ORDER BY recorded_at DESC
""",
(customer_id,),
(customer_id, site_id),
)
rows = cur.fetchall()
return [{"type": r[0] or "", "text": r[1] or ""} for r in rows]
result: list[dict] = []
for category, summary, detail, emoji, source, recorded_by_name in rows:
category_str = category or ""
# source 显示规则(用户拍板:AI 来源用统一短标识 "AI",前端渲染 AI icon):
# - ai_consumption / ai_note → "AI"(舍弃 providers 详细文字,前端用 icon)
# - manual + recorded_by_name 非空 → 姓名(如"小燕")
# - manual + 空 → "系统"
src = source or "manual"
if src in ("ai_consumption", "ai_note"):
display_source = "AI"
else:
display_source = recorded_by_name or SOURCE_DISPLAY_NAME.get(src, "系统")
result.append({
"tag": format_category_tag(category_str),
"tag_color": CATEGORY_TAG_COLOR.get(category_str, "primary"),
"emoji": emoji or "",
"text": summary or "",
"source": display_source,
"desc": detail or "",
})
return result
NOTE_TYPE_LABELS = {"normal": "备注", "follow_up": "回访", "system": "系统", "ai": "AI"}

View File

@@ -468,19 +468,10 @@ _COURSE_TYPE_CLASS_MAP: dict[str, str] = {
"激励": "incentive",
}
# 维客线索 category → tag_color 映射
# CHANGE 2026-03-24 | 值改为前端 clue-card 组件 CSS 类名后缀primary/success/...
# 不再用十六进制颜色——WXSS 类名 `clue-tag-#0052d9` 无效。
_CATEGORY_COLOR_MAP: dict[str, str] = {
"客户基础": "primary",
"客户基础信息": "primary",
"消费习惯": "error",
"玩法偏好": "success",
"促销偏好": "orange",
"促销接受": "orange",
"社交关系": "purple",
"重要反馈": "error",
}
# W1-AI-CLOSURE 组 5:维客线索 category → tag_color 映射已迁移至
# app.utils.clue_category.CATEGORY_TAG_COLOR(VI-DESIGN-SYSTEM v1.1 权威映射,
# 与 customer_service 共享)。原本地 _CATEGORY_COLOR_MAP 与 VI 规范不一致
# (消费习惯=error 应是 success;玩法偏好=success 应是 orange),已删除。
@trace_service(description_zh="map_course_type_class", description_en="Map Course Type Class")
@@ -514,20 +505,10 @@ def sanitize_tag(raw_tag: str | None) -> str:
return raw_tag.replace("\n", " ").strip()
def _extract_emoji_and_text(summary: str | None) -> tuple[str, str]:
"""
从 summary 中提取 emoji 前缀和正文。
AI 写入格式: "📅 偏好周末下午时段消费" → ("📅", "偏好周末下午时段消费")
手动写入无 emoji: "喜欢打中式" → ("", "喜欢打中式")
"""
if not summary:
return "", ""
# 检查第一个字符是否为 emoji非 ASCII 且非中文常用范围)
first_char = summary[0]
if ord(first_char) > 0x2600 and summary[1:2] == " ":
return first_char, summary[2:].strip()
return "", summary.strip()
# W1-AI-CLOSURE 组 5:_extract_emoji_and_text 已删除。
# 维客线索 emoji 在数据库已独立列(public.member_retention_clue.emoji),
# 不再需要从 summary 字符串解析。dispatcher._write_retention_clue 直接写
# emoji 列,task_manager / customer_service 直接读 emoji 列。
def _format_time(dt: datetime | None) -> str | None:
@@ -693,6 +674,8 @@ async def get_task_list_v2(
logger.warning("ETL 批量查询失败,降级为空数据", exc_info=True)
# ── 6. 查询 ai_cache 获取 aiSuggestion优雅降级 ──
# App4Result schema: {task_description, action_suggestions[], one_line_summary}
# aiSuggestion 用于任务列表一行简短提示,取 one_line_summary 最贴合。
ai_suggestion_map: dict[int, str] = {}
try:
runtime_ctx = get_runtime_context(site_id, conn=conn)
@@ -708,6 +691,8 @@ async def get_task_list_v2(
WHERE site_id = %s
AND target_id = ANY(%s)
AND cache_type = 'app4_analysis'
AND COALESCE(status, 'valid') = 'valid'
AND (expires_at IS NULL OR expires_at > now())
ORDER BY created_at DESC
""",
(site_id, member_id_strs),
@@ -718,10 +703,11 @@ async def get_task_list_v2(
if target_id_str not in seen:
seen.add(target_id_str)
result = row[1] if isinstance(row[1], dict) else {}
summary = result.get("summary", "")
if summary:
# P0-7: App4 schema 是 one_line_summary,不是 summary
suggestion = result.get("one_line_summary", "")
if suggestion:
raw_target = target_id_str.split(":", 1)[-1]
ai_suggestion_map[int(raw_target)] = summary
ai_suggestion_map[int(raw_target)] = suggestion
conn.commit()
except Exception:
logger.warning("查询 ai_cache aiSuggestion 失败", exc_info=True)
@@ -1110,6 +1096,13 @@ async def get_task_detail(
logger.warning("ETL 查询 RS 指数失败", exc_info=True)
# ── 3. 查询维客线索 ──
# 字段对齐 xcx_customers.RetentionClue schema(W1-AI-CLOSURE 组 3):
# 直接读 emoji 独立列(不再从 summary 字符串抽取),tag_color 走 utils 共用映射。
from app.utils.clue_category import (
CATEGORY_TAG_COLOR,
SOURCE_DISPLAY_NAME,
format_category_tag,
)
retention_clues = []
try:
runtime_ctx = get_runtime_context(site_id, conn=conn)
@@ -1121,7 +1114,7 @@ async def get_task_detail(
with conn.cursor() as cur:
cur.execute(
"""
SELECT id, category, summary, detail, source
SELECT category, summary, detail, source, emoji, recorded_by_name
FROM public.member_retention_clue
WHERE member_id = %s AND site_id = %s
AND is_hidden = false
@@ -1130,29 +1123,38 @@ async def get_task_detail(
(member_id, site_id),
)
for clue_row in cur.fetchall():
category = clue_row[1] or ""
summary_raw = clue_row[2] or ""
detail = clue_row[3]
source = clue_row[4] or "manual"
category = clue_row[0] or ""
summary = clue_row[1] or ""
detail = clue_row[2]
source = clue_row[3] or "manual"
emoji = clue_row[4] or ""
recorded_by_name = clue_row[5] or ""
emoji, text = _extract_emoji_and_text(summary_raw)
tag = sanitize_tag(category)
tag_color = _CATEGORY_COLOR_MAP.get(tag, "primary")
# source 显示规则(用户拍板):AI 类用 "AI" 短标识(前端渲染 icon)
if source in ("ai_consumption", "ai_note"):
display_src = "AI"
else:
display_src = recorded_by_name or SOURCE_DISPLAY_NAME.get(source, "系统")
retention_clues.append({
"tag": tag,
"tag_color": tag_color,
"tag": format_category_tag(tag),
"tag_color": CATEGORY_TAG_COLOR.get(tag, "primary"),
"emoji": emoji,
"text": text,
"source": source,
"desc": detail,
"text": summary,
"source": display_src,
"desc": detail or "",
})
conn.commit()
except Exception:
logger.warning("查询维客线索失败", exc_info=True)
# ── 4. 查询 AI 缓存talkingPoints + aiAnalysis ──
talking_points: list[str] = []
# ── 4. 查询 AI 缓存(talkingPoints + aiAnalysis) ──
# P0-6 修正:cache_type 从不存在的 'app5_talking_points' 改为正确的 'app5_tactics';
# 字段从不存在的 'talking_points' 改为正确的 tactics[{scenario, script}]。
# P0-7 修正:App4 schema 字段不存在 summary/suggestions,实际为
# one_line_summary / action_suggestions / task_description。
# tactics 直接以对象列表透出,前端 task-detail 渲染 scenario 与 script 两栏。
talking_points: list[dict] = []
ai_analysis = {"summary": "", "suggestions": []}
try:
with conn.cursor() as cur:
@@ -1161,7 +1163,9 @@ async def get_task_detail(
SELECT cache_type, result_json
FROM biz.ai_cache
WHERE target_id = %s AND site_id = %s
AND cache_type IN ('app4_analysis', 'app5_talking_points')
AND cache_type IN ('app4_analysis', 'app5_tactics')
AND COALESCE(status, 'valid') = 'valid'
AND (expires_at IS NULL OR expires_at > now())
ORDER BY created_at DESC
""",
(member_target_id, site_id),
@@ -1175,16 +1179,21 @@ async def get_task_detail(
result = cache_row[1] if isinstance(cache_row[1], dict) else {}
if cache_type == "app5_talking_points":
# talkingPoints: 话术列表
points = result.get("talking_points", [])
if isinstance(points, list):
talking_points = [str(p) for p in points]
if cache_type == "app5_tactics":
tactics = result.get("tactics", [])
if isinstance(tactics, list):
talking_points = [
{
"scenario": t.get("scenario", ""),
"script": t.get("script", ""),
}
for t in tactics
if isinstance(t, dict)
]
elif cache_type == "app4_analysis":
# aiAnalysis: summary + suggestions
ai_analysis = {
"summary": result.get("summary", ""),
"suggestions": result.get("suggestions", []),
"summary": result.get("one_line_summary", ""),
"suggestions": result.get("action_suggestions", []),
}
conn.commit()
except Exception: