feat: 2026-04-15~05-02 累积变更基线 — AI 重构 + Runtime Context + DWS 修复
涵盖(每条对应已存的审计记录): - AI 模块拆分:apps/backend/app/ai/apps -> prompts/(8 个 APP + app2a 派生) audit: 2026-04-20__ai-module-complete.md - admin-web AI 管理套件:AIDashboard / AIOperations / AIRunLogs / AITriggers / TriggerManager audit: 2026-04-21__admin-web-ai-management-suite.md - App2 财务洞察 prompt v3 -> v5.1 + 小程序 AI 接入(chat / board-finance) audit: 2026-04-22__app2_prompt_v5_1_and_miniprogram_ai_insight.md - App2 prewarm 全过滤器 + AI 触发器 cron reschedule audit: 2026-04-21__app2-finance-prewarm-all-filters.md migration: 20260420_ai_trigger_jobs_and_app2_prewarm.sql / 20260421_app2_prewarm_cron_reschedule.sql - AppType 联合类型对齐 + adminAiAppTypes.test.ts audit: 2026-04-30__admin_web_ai_app_type_alignment.md - DashScope tokens_used 提取修复 audit: 2026-04-30__backend_dashscope_tokens_used_extraction.md - App3 线索完整详情 prompt audit: 2026-05-01__backend_app3_full_detail_prompt.md - Runtime Context 沙箱(5-1~5-2 主线): - 后端 schema/service + admin_runtime_context / xcx_runtime_clock 两个 router - admin-web RuntimeContext.tsx + miniprogram runtime-clock.ts - migration: 20260501__runtime_context_sandbox.sql - tools/db/verify_admin_web_sandbox.py + verify_sandbox_end_to_end.py - database/changes: 7 份 sandbox_* 验证报告 - 飞球 DWS 修复:finance_area_daily 区域汇总 + task_engine 调整 + RLS 视图业务日上界(migration 20260502 + scripts/ops/gen_rls_business_date_migration.py) 合规: - .gitignore 启用 tmp/ 排除 - 不入仓:apps/etl/connectors/feiqiu/.env(API_TOKEN secret,本地修改保留) 待验证清单: - docs/audit/changes/2026-05-04__cumulative_baseline_pending_verification.md 每个主题的功能完整性 / 上线验证几乎都未收口,按优先级 P0~P3 逐一处理
This commit is contained in:
@@ -234,23 +234,14 @@ class ChatService:
|
||||
INSERT INTO biz.ai_conversations
|
||||
(user_id, nickname, app_id, site_id, context_type, context_id)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
RETURNING id, EXTRACT(EPOCH FROM created_at)::bigint
|
||||
RETURNING id
|
||||
""",
|
||||
(str(user_id), nickname, APP_ID, site_id, context_type, context_id),
|
||||
)
|
||||
result = cur.fetchone()
|
||||
new_id = result[0]
|
||||
created_ts = result[1]
|
||||
|
||||
# 生成 session_id 并回写(格式:conv_{id}_{timestamp})
|
||||
session_id = f"conv_{new_id}_{created_ts}"
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE biz.ai_conversations SET session_id = %s WHERE id = %s
|
||||
""",
|
||||
(session_id, new_id),
|
||||
)
|
||||
new_id = cur.fetchone()[0]
|
||||
|
||||
# session_id 初始保持 NULL,首次对话由百炼返回后再回写。
|
||||
# 参见 P14 spec §2.3:后端不再自生 session_id,交由百炼云端管理。
|
||||
conn.commit()
|
||||
return new_id
|
||||
except Exception:
|
||||
@@ -274,6 +265,34 @@ class ChatService:
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
@trace_service("保存百炼 session_id", "Save bailian session ID")
|
||||
def save_session_id(self, chat_id: int, session_id: str) -> None:
|
||||
"""流式回复完成后,将百炼返回的 session_id 回写 ai_conversations。
|
||||
|
||||
multi-turn 启用:
|
||||
- 首次对话 session_id=NULL → 百炼分配新 session → 这里回写
|
||||
- 下次对话 get_session_id 返回该值 → 传给百炼关联历史上下文
|
||||
|
||||
幂等:同一对话多次调用覆盖最新 session_id(通常保持稳定)。
|
||||
"""
|
||||
if not session_id:
|
||||
return
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"UPDATE biz.ai_conversations SET session_id = %s WHERE id = %s",
|
||||
(session_id, chat_id),
|
||||
)
|
||||
conn.commit()
|
||||
except Exception:
|
||||
conn.rollback()
|
||||
logger.warning(
|
||||
"保存 session_id 失败: chat_id=%s", chat_id, exc_info=True,
|
||||
)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# CHAT-2: 消息列表
|
||||
# ------------------------------------------------------------------
|
||||
@@ -662,7 +681,10 @@ class ChatService:
|
||||
"""查询客户近 30 天消费金额(items_sum 口径)。
|
||||
|
||||
⚠️ DWD-DOC 规则 1: 使用 ledger_amount(items_sum 口径),禁用 consume_money。
|
||||
CHANGE 2026-05-02 | 用 business_date 替代 CURRENT_DATE,沙箱不读「未来」消费。
|
||||
"""
|
||||
from app.services.runtime_context import as_runtime_today_param
|
||||
ref = as_runtime_today_param(site_id, conn=conn)
|
||||
with fdw_queries._fdw_context(conn, site_id) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
@@ -670,16 +692,22 @@ class ChatService:
|
||||
FROM app.v_dwd_assistant_service_log
|
||||
WHERE tenant_member_id = %s
|
||||
AND is_delete = 0
|
||||
AND create_time >= (CURRENT_DATE - INTERVAL '30 days')::timestamptz
|
||||
AND create_time >= (%s::date - INTERVAL '30 days')::timestamptz
|
||||
AND create_time < (%s::date + INTERVAL '1 day')::timestamptz
|
||||
""",
|
||||
(member_id,),
|
||||
(member_id, ref, ref),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return Decimal(str(row[0])) if row and row[0] is not None else None
|
||||
|
||||
@staticmethod
|
||||
def _get_visit_count_30d(conn: Any, site_id: int, member_id: int) -> int | None:
|
||||
"""查询客户近 30 天到店次数。"""
|
||||
"""查询客户近 30 天到店次数。
|
||||
|
||||
CHANGE 2026-05-02 | 用 business_date 替代 CURRENT_DATE,沙箱不读「未来」到店。
|
||||
"""
|
||||
from app.services.runtime_context import as_runtime_today_param
|
||||
ref = as_runtime_today_param(site_id, conn=conn)
|
||||
with fdw_queries._fdw_context(conn, site_id) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
@@ -687,9 +715,10 @@ class ChatService:
|
||||
FROM app.v_dwd_assistant_service_log
|
||||
WHERE tenant_member_id = %s
|
||||
AND is_delete = 0
|
||||
AND create_time >= (CURRENT_DATE - INTERVAL '30 days')::timestamptz
|
||||
AND create_time >= (%s::date - INTERVAL '30 days')::timestamptz
|
||||
AND create_time < (%s::date + INTERVAL '1 day')::timestamptz
|
||||
""",
|
||||
(member_id,),
|
||||
(member_id, ref, ref),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return int(row[0]) if row and row[0] is not None else None
|
||||
|
||||
Reference in New Issue
Block a user