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>
132 lines
4.3 KiB
Python
132 lines
4.3 KiB
Python
"""应用 3 客户数据维客线索分析 Prompt 拼装。
|
||
|
||
消费事件触发,从客户消费数据提取维客线索。
|
||
- 数据源:fetch_member_consumption_data(DWS)
|
||
- 金额口径:items_sum(禁止 consume_money)
|
||
- 线索 category:客户基础 / 消费习惯 / 玩法偏好(3 选 1)
|
||
- 线索 providers 统一为"系统"
|
||
- system prompt 在百炼控制台配置,本模块只拼数据上下文 JSON
|
||
|
||
返回:单个 prompt 字符串(直接传给 Application.call)。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import logging
|
||
from typing import Any
|
||
|
||
from app.ai.cache_service import AICacheService
|
||
from app.ai.data_fetchers import fetch_member_consumption_data
|
||
from app.ai.schemas import CacheTypeEnum
|
||
from app.services.runtime_context import as_runtime_business_now_str
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
# prompt 观测阈值:历史上 4000 字会触发裁剪;现保留完整消费明细,仅用于测试/审计参考
|
||
_MAX_PROMPT_LEN = 4000
|
||
|
||
|
||
async def build_prompt(
|
||
context: dict,
|
||
cache_svc: AICacheService | None = None,
|
||
) -> str:
|
||
"""构建 App3 prompt 字符串。
|
||
|
||
Args:
|
||
context: site_id, member_id
|
||
cache_svc: 缓存服务,用于读取 reference 历史数据
|
||
|
||
Returns:
|
||
JSON 序列化后的 prompt 字符串
|
||
"""
|
||
site_id = context["site_id"]
|
||
member_id = context["member_id"]
|
||
|
||
# 数据获取(失败降级)
|
||
fetch_failed = False
|
||
try:
|
||
member_data = await fetch_member_consumption_data(site_id, member_id)
|
||
except Exception:
|
||
logger.warning(
|
||
"App3 消费数据获取失败: site_id=%s member_id=%s",
|
||
site_id, member_id, exc_info=True,
|
||
)
|
||
member_data = _default_member_data()
|
||
fetch_failed = True
|
||
|
||
consumption_records = member_data.get("consumption_records") or []
|
||
if not consumption_records:
|
||
consumption_records = (
|
||
"⚠ 消费数据获取失败,该客户暂无消费记录可供分析"
|
||
if fetch_failed else "该客户暂无消费记录"
|
||
)
|
||
|
||
payload: dict[str, Any] = {
|
||
"current_time": as_runtime_business_now_str(site_id, fmt="%Y-%m-%d %H:%M"),
|
||
"member_id": member_id,
|
||
"member_nickname": member_data.get("member_nickname", ""),
|
||
"main_data": {
|
||
"consumption_records": consumption_records,
|
||
"member_cards": member_data.get("member_cards", []),
|
||
"card_balance_total": member_data.get("card_balance_total", 0),
|
||
"stored_value_balance_total": member_data.get("stored_value_balance_total", 0),
|
||
"expected_visit_date": member_data.get("expected_visit_date"),
|
||
"days_since_last_visit": member_data.get("days_since_last_visit"),
|
||
},
|
||
"reference": _build_reference(site_id, member_id, cache_svc),
|
||
}
|
||
|
||
# 完整明细策略:App3 需要尽量保留消费行为模式,不在本地裁剪消费记录。
|
||
# 真实 App3 完整 100 条明细调用已验证可在 180s 单步超时内返回。
|
||
text = json.dumps(payload, ensure_ascii=False, default=str)
|
||
return text
|
||
|
||
|
||
def _default_member_data() -> dict:
|
||
return {
|
||
"member_nickname": "",
|
||
"consumption_records": [],
|
||
"member_cards": [],
|
||
"card_balance_total": 0,
|
||
"stored_value_balance_total": 0,
|
||
"expected_visit_date": None,
|
||
"days_since_last_visit": None,
|
||
}
|
||
|
||
|
||
def _build_reference(
|
||
site_id: int,
|
||
member_id: int,
|
||
cache_svc: AICacheService | None,
|
||
) -> dict:
|
||
"""组装参考字段:App6 备注线索最新 + App8 历史最近 2 条。"""
|
||
if cache_svc is None:
|
||
return {}
|
||
|
||
ref: dict = {}
|
||
target_id = str(member_id)
|
||
|
||
app6_latest = cache_svc.get_latest(
|
||
CacheTypeEnum.APP6_NOTE.value, site_id, target_id,
|
||
)
|
||
if app6_latest:
|
||
ref["app6_note_clues"] = {
|
||
"result_json": app6_latest.get("result_json"),
|
||
"generated_at": app6_latest.get("created_at"),
|
||
}
|
||
|
||
app8_history = cache_svc.get_history(
|
||
CacheTypeEnum.APP8_CONSOLIDATION.value, site_id, target_id, limit=2,
|
||
)
|
||
if app8_history:
|
||
ref["app8_history"] = [
|
||
{
|
||
"result_json": h.get("result_json"),
|
||
"generated_at": h.get("created_at"),
|
||
}
|
||
for h in app8_history
|
||
]
|
||
|
||
return ref
|