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

@@ -27,15 +27,19 @@ from app.services.runtime_context import (
logger = logging.getLogger(__name__)
# 缓存过期策略cache_type → 过期天数0 表示当日 23:59:59
# 缓存过期策略:cache_type → 过期天数(0 表示当日 23:59:59)
# 命名与 prompt 文件名一致(W1-AI-CLOSURE 组 1 数据库迁移已统一)。
# P0-11 修正:补 app2a_finance_area,与 app2_finance 同当日过期策略,
# 之前漏配导致 64 个区域组合缓存 expires_at=NULL 永不过期。
CACHE_EXPIRY_DAYS: dict[str, int] = {
"app2_finance": 0, # 当日 23:59:59
"app2_finance": 0,
"app2a_finance_area": 0,
"app3_clue": 7,
"app4_analysis": 7,
"app5_tactics": 7,
"app6_note_analysis": 30,
"app7_customer_analysis": 7,
"app8_clue_consolidated": 7,
"app6_note": 30,
"app7_customer": 7,
"app8_consolidation": 7,
}
# 每 App 保留上限

View File

@@ -192,6 +192,13 @@ def _text_customer_detail(
try:
etl_conn = get_etl_readonly_connection(site_id)
with etl_conn.cursor() as cur:
# W1-AI-CLOSURE 复盘修正:get_etl_readonly_connection 在 SET LOCAL 后
# commit(LOCAL 失效),后续 cursor 进入新事务,RLS 视图 current_setting
# ('app.current_site_id') 拿到空串导致 bigint 转换失败。每次 cursor 必须
# 重新 SET 当前事务的 GUC(对齐 member_data.py / assistant_data.py 模式)。
cur.execute(
"SET LOCAL app.current_site_id = %s", (str(site_id),)
)
cur.execute(
"SET LOCAL statement_timeout = %s",
(f"{FDW_QUERY_TIMEOUT_SEC * 1000}",),
@@ -210,21 +217,34 @@ def _text_customer_detail(
nickname = m_row[0] if m_row else f"ID:{member_id}"
# 最近 5 条消费
# W1-AI-CLOSURE 复盘修正:列名错位 — v_dwd_settlement_head 实际列是
# pay_time / settle_name(无 settle_date / room_name);items_sum 按
# DWD-DOC 强制规则 #1 用合成表达式(consume_money 禁止直接计算)。
cur.execute(
"""
SELECT settle_date, items_sum, room_name
SELECT
pay_time,
(COALESCE(table_charge_money, 0)
+ COALESCE(goods_money, 0)
+ COALESCE(assistant_pd_money, 0)
+ COALESCE(assistant_cx_money, 0)
+ COALESCE(electricity_money, 0)) AS items_sum,
settle_name
FROM app.v_dwd_settlement_head
WHERE member_id = %s AND settle_type IN (1, 3)
ORDER BY settle_date DESC LIMIT 5
ORDER BY pay_time DESC LIMIT 5
""",
(member_id,),
)
recent = cur.fetchall()
# 余额
# W1-AI-CLOSURE 复盘修正:列名 balance_amount 不存在,实际列是
# cash_card_balance / gift_card_balance / total_card_balance;
# total_card_balance 是合计储值(对齐 customer_service.balance 字段)。
cur.execute(
"""
SELECT balance_amount
SELECT total_card_balance
FROM app.v_dws_member_consumption_summary
WHERE member_id = %s LIMIT 1
""",
@@ -233,14 +253,15 @@ def _text_customer_detail(
bal_row = cur.fetchone()
etl_conn.commit()
# 维客线索
# 维客线索(W1-AI-CLOSURE 复盘修正:列名 created_at 不存在,实际是 recorded_at;
# 表加 schema 前缀 public 防 search_path 漂移;补 is_hidden 过滤)
biz_conn = get_connection()
with biz_conn.cursor() as cur:
cur.execute(
"""
SELECT summary FROM member_retention_clue
WHERE member_id = %s AND site_id = %s
ORDER BY created_at DESC LIMIT 5
SELECT summary FROM public.member_retention_clue
WHERE member_id = %s AND site_id = %s AND is_hidden = false
ORDER BY recorded_at DESC LIMIT 5
""",
(member_id, site_id),
)
@@ -281,6 +302,11 @@ def _text_coach_detail(
try:
etl_conn = get_etl_readonly_connection(site_id)
with etl_conn.cursor() as cur:
# W1-AI-CLOSURE 复盘修正:get_etl_readonly_connection 在 SET LOCAL 后
# commit(LOCAL 失效),后续 cursor 在新事务中 RLS 视图查不到 site_id。
cur.execute(
"SET LOCAL app.current_site_id = %s", (str(site_id),)
)
cur.execute(
"SET LOCAL statement_timeout = %s",
(f"{FDW_QUERY_TIMEOUT_SEC * 1000}",),
@@ -348,6 +374,11 @@ def _text_board_finance(
try:
etl_conn = get_etl_readonly_connection(site_id)
with etl_conn.cursor() as cur:
# W1-AI-CLOSURE 复盘修正:get_etl_readonly_connection 在 SET LOCAL 后
# commit(LOCAL 失效),后续 cursor 在新事务中 RLS 视图查不到 site_id。
cur.execute(
"SET LOCAL app.current_site_id = %s", (str(site_id),)
)
cur.execute(
"SET LOCAL statement_timeout = %s",
(f"{FDW_QUERY_TIMEOUT_SEC * 1000}",),
@@ -399,6 +430,11 @@ def _text_board_customer(
try:
etl_conn = get_etl_readonly_connection(site_id)
with etl_conn.cursor() as cur:
# W1-AI-CLOSURE 复盘修正:get_etl_readonly_connection 在 SET LOCAL 后
# commit(LOCAL 失效),后续 cursor 在新事务中 RLS 视图查不到 site_id。
cur.execute(
"SET LOCAL app.current_site_id = %s", (str(site_id),)
)
cur.execute(
"SET LOCAL statement_timeout = %s",
(f"{FDW_QUERY_TIMEOUT_SEC * 1000}",),
@@ -456,6 +492,11 @@ def _text_board_coach(
try:
etl_conn = get_etl_readonly_connection(site_id)
with etl_conn.cursor() as cur:
# W1-AI-CLOSURE 复盘修正:get_etl_readonly_connection 在 SET LOCAL 后
# commit(LOCAL 失效),后续 cursor 在新事务中 RLS 视图查不到 site_id。
cur.execute(
"SET LOCAL app.current_site_id = %s", (str(site_id),)
)
cur.execute(
"SET LOCAL statement_timeout = %s",
(f"{FDW_QUERY_TIMEOUT_SEC * 1000}",),
@@ -515,6 +556,11 @@ def _text_performance(
try:
etl_conn = get_etl_readonly_connection(site_id)
with etl_conn.cursor() as cur:
# W1-AI-CLOSURE 复盘修正:get_etl_readonly_connection 在 SET LOCAL 后
# commit(LOCAL 失效),后续 cursor 在新事务中 RLS 视图查不到 site_id。
cur.execute(
"SET LOCAL app.current_site_id = %s", (str(site_id),)
)
cur.execute(
"SET LOCAL statement_timeout = %s",
(f"{FDW_QUERY_TIMEOUT_SEC * 1000}",),
@@ -599,6 +645,11 @@ def _text_customer_service_records(
try:
etl_conn = get_etl_readonly_connection(site_id)
with etl_conn.cursor() as cur:
# W1-AI-CLOSURE 复盘修正:get_etl_readonly_connection 在 SET LOCAL 后
# commit(LOCAL 失效),后续 cursor 在新事务中 RLS 视图查不到 site_id。
cur.execute(
"SET LOCAL app.current_site_id = %s", (str(site_id),)
)
cur.execute(
"SET LOCAL statement_timeout = %s",
(f"{FDW_QUERY_TIMEOUT_SEC * 1000}",),

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}")

View File

@@ -108,7 +108,7 @@ def _build_reference(
target_id = str(member_id)
app6_latest = cache_svc.get_latest(
CacheTypeEnum.APP6_NOTE_ANALYSIS.value, site_id, target_id,
CacheTypeEnum.APP6_NOTE.value, site_id, target_id,
)
if app6_latest:
ref["app6_note_clues"] = {
@@ -117,7 +117,7 @@ def _build_reference(
}
app8_history = cache_svc.get_history(
CacheTypeEnum.APP8_CLUE_CONSOLIDATED.value, site_id, target_id, limit=2,
CacheTypeEnum.APP8_CONSOLIDATION.value, site_id, target_id, limit=2,
)
if app8_history:
ref["app8_history"] = [

View File

@@ -131,7 +131,7 @@ def _build_reference(
target_id = str(member_id)
latest = cache_svc.get_latest(
CacheTypeEnum.APP8_CLUE_CONSOLIDATED.value, site_id, target_id,
CacheTypeEnum.APP8_CONSOLIDATION.value, site_id, target_id,
)
if latest:
ref["app8_latest"] = {
@@ -140,7 +140,7 @@ def _build_reference(
}
history = cache_svc.get_history(
CacheTypeEnum.APP8_CLUE_CONSOLIDATED.value, site_id, target_id, limit=2,
CacheTypeEnum.APP8_CONSOLIDATION.value, site_id, target_id, limit=2,
)
if history:
ref["app8_history"] = [

View File

@@ -130,7 +130,7 @@ def _build_reference(
ref: dict = {}
history = cache_svc.get_history(
CacheTypeEnum.APP8_CLUE_CONSOLIDATED.value,
CacheTypeEnum.APP8_CONSOLIDATION.value,
site_id,
str(member_id),
limit=2,

View File

@@ -128,7 +128,7 @@ def _build_reference(
}
app8_history = cache_svc.get_history(
CacheTypeEnum.APP8_CLUE_CONSOLIDATED.value, site_id, target_id, limit=2,
CacheTypeEnum.APP8_CONSOLIDATION.value, site_id, target_id, limit=2,
)
if app8_history:
ref["app8_history"] = [

View File

@@ -125,7 +125,7 @@ def _build_reference(
target_id = str(member_id)
latest = cache_svc.get_latest(
CacheTypeEnum.APP8_CLUE_CONSOLIDATED.value, site_id, target_id,
CacheTypeEnum.APP8_CONSOLIDATION.value, site_id, target_id,
)
if latest:
ref["app8_latest"] = {
@@ -134,7 +134,7 @@ def _build_reference(
}
history = cache_svc.get_history(
CacheTypeEnum.APP8_CLUE_CONSOLIDATED.value, site_id, target_id, limit=2,
CacheTypeEnum.APP8_CONSOLIDATION.value, site_id, target_id, limit=2,
)
if history:
ref["app8_history"] = [

View File

@@ -36,14 +36,21 @@ class SSEEvent(BaseModel):
class CacheTypeEnum(str, enum.Enum):
"""ai_cache.cache_type 允许的取值(8 类,与 prompt 文件名 + 数据库 chk_ai_cache_type
约束完全对齐;app1_chat 走 ai_messages 不进缓存)。
W1-AI-CLOSURE 组 1 命名统一:旧名字 app6_note_analysis / app7_customer_analysis /
app8_clue_consolidated / app8_consolidate 已在数据库迁移中清理,代码不再使用。
"""
APP2_FINANCE = "app2_finance"
APP2A_FINANCE_AREA = "app2a_finance_area" # 2026-04-23 新增区域财务洞察64 组合)
APP2A_FINANCE_AREA = "app2a_finance_area"
APP3_CLUE = "app3_clue"
APP4_ANALYSIS = "app4_analysis"
APP5_TACTICS = "app5_tactics"
APP6_NOTE_ANALYSIS = "app6_note_analysis"
APP7_CUSTOMER_ANALYSIS = "app7_customer_analysis"
APP8_CLUE_CONSOLIDATED = "app8_clue_consolidated"
APP6_NOTE = "app6_note"
APP7_CUSTOMER = "app7_customer"
APP8_CONSOLIDATION = "app8_consolidation"
# ── 线索相关 ──