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

@@ -545,67 +545,86 @@ class AIDispatcher:
consolidate_result: dict,
source: str,
) -> None:
"""全量替换 member_retention_clueDELETE 同源旧记录 + INSERT 新记录事务
"""全量替换 member_retention_clue:DELETE 同源旧记录 + INSERT 新记录(事务)
同一 member 同一 site 同一 source 当天只保留最新一批。
人工线索source='manual')不受影响
同一 member + site + source + (runtime_mode, sandbox_instance_id) 全量替换:
live 模式只覆盖 live 数据,sandbox 实例只覆盖该实例数据;互不污染
人工线索(source='manual')不受影响。
事务失败自动回滚。
字段映射
字段映射(W1-AI-CLOSURE 组 3 修正:emoji 独立列,不再嵌 summary):
- category → category
- emoji + " " + summary → summary"📅 偏好周末下午时段消费"
- summary → summary(原文,不拼 emoji 前缀)
- emoji → emoji(独立列)
- detail → detail
- providers → recorded_by_name
- runtime_mode + sandbox_instance_id → 同列(沙箱隔离)
"""
from app.database import get_connection
from app.services.runtime_context import (
LIVE_INSTANCE_ID,
MODE_LIVE,
MODE_SANDBOX,
get_runtime_context,
)
clues = consolidate_result.get("clues", [])
if not clues:
logger.info(
"App8 无线索数据跳过写入: member_id=%d site_id=%d", member_id, site_id,
"App8 无线索数据,跳过写入: member_id=%d site_id=%d", member_id, site_id,
)
return
conn = get_connection()
try:
ctx = get_runtime_context(site_id, conn=conn)
if ctx.is_sandbox and ctx.sandbox_instance_id:
runtime_mode = MODE_SANDBOX
sandbox_instance_id = ctx.sandbox_instance_id
else:
runtime_mode = MODE_LIVE
sandbox_instance_id = LIVE_INSTANCE_ID
with conn.cursor() as cur:
cur.execute(
"""
DELETE FROM member_retention_clue
DELETE FROM public.member_retention_clue
WHERE member_id = %s AND site_id = %s AND source = %s
AND runtime_mode = %s AND sandbox_instance_id = %s
""",
(member_id, site_id, source),
(member_id, site_id, source, runtime_mode, sandbox_instance_id),
)
deleted = cur.rowcount
for clue in clues:
emoji = clue.get("emoji", "")
raw_summary = clue.get("summary", "")
summary = f"{emoji} {raw_summary}" if emoji else raw_summary
cur.execute(
"""
INSERT INTO member_retention_clue
(member_id, category, summary, detail, site_id,
source, recorded_by_name, recorded_by_assistant_id)
VALUES (%s, %s, %s, %s, %s, %s, %s, NULL)
INSERT INTO public.member_retention_clue
(member_id, category, summary, detail, emoji, site_id,
source, recorded_by_name, recorded_by_assistant_id,
runtime_mode, sandbox_instance_id)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, NULL, %s, %s)
""",
(
member_id,
clue.get("category", "客户基础"),
summary,
clue.get("summary", ""),
clue.get("detail", ""),
clue.get("emoji", ""),
site_id,
source,
clue.get("providers", ""),
runtime_mode,
sandbox_instance_id,
),
)
conn.commit()
logger.info(
"维客线索全量替换完成: member_id=%d site_id=%d source=%s "
"deleted=%d inserted=%d",
member_id, site_id, source, deleted, len(clues),
"runtime=%s/%s deleted=%d inserted=%d",
member_id, site_id, source,
runtime_mode, sandbox_instance_id, deleted, len(clues),
)
except Exception:
conn.rollback()
@@ -682,7 +701,7 @@ class AIDispatcher:
# ── Step 2: App8 线索整合 ──
app3_clues = (app3_result or {}).get("clues", []) if isinstance(app3_result, dict) else []
app6_clues, app6_generated_at = self._fetch_recent_clues(
CacheTypeEnum.APP6_NOTE_ANALYSIS.value, site_id, member_id,
CacheTypeEnum.APP6_NOTE.value, site_id, member_id,
)
try:
@@ -701,11 +720,11 @@ class AIDispatcher:
app8_result = None
if app8_prompt:
app8_result = await self._run_step(
"app8_consolidate", self.config.app_id_8_consolidate,
"app8_consolidation", self.config.app_id_8_consolidate,
app8_prompt, context,
)
self._write_cache(
CacheTypeEnum.APP8_CLUE_CONSOLIDATED.value, site_id, str(member_id),
CacheTypeEnum.APP8_CONSOLIDATION.value, site_id, str(member_id),
app8_result, "consumption",
)
if app8_result is not None:
@@ -738,7 +757,7 @@ class AIDispatcher:
app7_prompt, context,
)
self._write_cache(
CacheTypeEnum.APP7_CUSTOMER_ANALYSIS.value, site_id, str(member_id),
CacheTypeEnum.APP7_CUSTOMER.value, site_id, str(member_id),
app7_result, "consumption",
)
@@ -781,7 +800,7 @@ class AIDispatcher:
)
score = (app6_result or {}).get("score") if isinstance(app6_result, dict) else None
self._write_cache(
CacheTypeEnum.APP6_NOTE_ANALYSIS.value, site_id, str(member_id),
CacheTypeEnum.APP6_NOTE.value, site_id, str(member_id),
app6_result, "note_created", score=score,
)
@@ -805,11 +824,11 @@ class AIDispatcher:
return
app8_result = await self._run_step(
"app8_consolidate", self.config.app_id_8_consolidate,
"app8_consolidation", self.config.app_id_8_consolidate,
app8_prompt, context,
)
self._write_cache(
CacheTypeEnum.APP8_CLUE_CONSOLIDATED.value, site_id, str(member_id),
CacheTypeEnum.APP8_CONSOLIDATION.value, site_id, str(member_id),
app8_result, "note_created",
)
if app8_result is not None:
@@ -1070,7 +1089,7 @@ class AIDispatcher:
raise ValueError("app6_note 需要 member_id")
prompt = await build_app6_prompt(context, self.cache_svc)
app_id = self.config.app_id_6_note
cache_type = CacheTypeEnum.APP6_NOTE_ANALYSIS.value
cache_type = CacheTypeEnum.APP6_NOTE.value
target_id = str(context["member_id"])
elif app_type == "app7_customer":
@@ -1078,7 +1097,7 @@ class AIDispatcher:
raise ValueError("app7_customer 需要 member_id")
prompt = await build_app7_prompt(context, self.cache_svc)
app_id = self.config.app_id_7_customer
cache_type = CacheTypeEnum.APP7_CUSTOMER_ANALYSIS.value
cache_type = CacheTypeEnum.APP7_CUSTOMER.value
target_id = str(context["member_id"])
elif app_type == "app8_consolidation":
@@ -1090,7 +1109,7 @@ class AIDispatcher:
CacheTypeEnum.APP3_CLUE.value, site_id, member_id,
)
app6_clues, app6_at = self._fetch_recent_clues(
CacheTypeEnum.APP6_NOTE_ANALYSIS.value, site_id, member_id,
CacheTypeEnum.APP6_NOTE.value, site_id, member_id,
)
prompt = await build_app8_prompt({
"site_id": site_id,
@@ -1101,7 +1120,7 @@ class AIDispatcher:
"app6_generated_at": app6_at,
})
app_id = self.config.app_id_8_consolidate
cache_type = CacheTypeEnum.APP8_CLUE_CONSOLIDATED.value
cache_type = CacheTypeEnum.APP8_CONSOLIDATION.value
target_id = str(member_id)
else:
raise ValueError(f"不支持的 app_type: {app_type}")