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:
Neo
2026-05-04 02:30:19 +08:00
parent 2010034840
commit caf179a5da
130 changed files with 14543 additions and 2717 deletions

View File

@@ -0,0 +1,309 @@
# -*- coding: utf-8 -*-
"""业务运行上下文管理 API。"""
from __future__ import annotations
import logging
from datetime import date
from fastapi import APIRouter, Depends, HTTPException, Query, status
from psycopg2.extras import RealDictCursor
from app.auth.dependencies import CurrentUser, get_current_user
from app.database import get_connection
from app.schemas.runtime_context import (
RuntimeContextResponse,
RuntimeSwitchRequest,
RuntimeSwitchResponse,
RuntimeTransitionStep,
)
from app.services.runtime_context import (
MODE_LIVE,
MODE_SANDBOX,
RuntimeContext,
get_runtime_context,
new_sandbox_instance_id,
)
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api/admin/runtime-context", tags=["业务运行上下文"])
config_router = APIRouter(prefix="/api/config", tags=["业务配置"])
def _require_super_admin(user: CurrentUser) -> None:
if "super_admin" not in user.roles:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="仅超级管理员可切换业务运行上下文",
)
def _context_response(ctx: RuntimeContext) -> RuntimeContextResponse:
return RuntimeContextResponse(**ctx.to_dict())
@config_router.get("/runtime-context", response_model=RuntimeContextResponse)
async def get_current_runtime_context(
user: CurrentUser = Depends(get_current_user),
) -> RuntimeContextResponse:
"""返回当前登录用户门店的业务运行上下文。"""
return _context_response(get_runtime_context(user.site_id))
@router.get("", response_model=RuntimeContextResponse)
async def get_admin_runtime_context(
site_id: int = Query(..., ge=1),
user: CurrentUser = Depends(get_current_user),
) -> RuntimeContextResponse:
"""系统管理端按门店查看业务运行上下文。"""
_require_super_admin(user)
return _context_response(get_runtime_context(site_id))
@router.get("/sites")
async def list_runtime_sites(
user: CurrentUser = Depends(get_current_user),
) -> list[dict]:
"""列出可配置门店及其当前运行上下文。"""
_require_super_admin(user)
conn = get_connection()
try:
with conn.cursor(cursor_factory=RealDictCursor) as cur:
cur.execute(
"""
SELECT s.site_id, s.site_name, s.site_code, s.is_active,
c.mode, c.sandbox_date, c.sandbox_instance_id,
c.ai_mode, c.status, c.updated_at
FROM biz.sites s
LEFT JOIN biz.site_runtime_context c ON c.site_id = s.site_id
ORDER BY s.is_active DESC, s.site_id
"""
)
rows = cur.fetchall()
conn.commit()
except Exception:
conn.rollback()
raise
finally:
conn.close()
return [dict(row) for row in rows]
@router.patch("", response_model=RuntimeSwitchResponse)
async def switch_runtime_context(
body: RuntimeSwitchRequest,
user: CurrentUser = Depends(get_current_user),
) -> RuntimeSwitchResponse:
"""切换门店业务运行上下文。
切换前会终止当前运行中的 ETL、取消未完成 AI 触发记录。
`biz.trigger_jobs` 是全局调度表(无 site_id 列),不随单门店沙箱切换暂停;
多门店隔离完全通过 runtime_mode + sandbox_instance_id 实现。
"""
_require_super_admin(user)
if body.mode == MODE_SANDBOX and body.sandbox_date is None:
raise HTTPException(status_code=422, detail="沙箱模式必须设置 sandbox_date")
if body.mode == MODE_LIVE and body.sandbox_date is not None:
raise HTTPException(status_code=422, detail="live 模式不能设置 sandbox_date")
if body.mode == MODE_SANDBOX and body.sandbox_date and body.sandbox_date > date.today():
raise HTTPException(status_code=422, detail="sandbox_date 不能晚于真实今天")
steps: list[RuntimeTransitionStep] = []
steps.extend(await _stop_runtime_activity(body.site_id))
conn = get_connection()
try:
with conn.cursor() as cur:
old_ctx = get_runtime_context(body.site_id, conn=conn)
sandbox_instance_id = None
if body.mode == MODE_SANDBOX:
if body.reset_sandbox or not old_ctx.sandbox_instance_id:
sandbox_instance_id = new_sandbox_instance_id()
else:
sandbox_instance_id = old_ctx.sandbox_instance_id
cur.execute(
"""
INSERT INTO biz.site_runtime_context
(site_id, mode, sandbox_date, sandbox_instance_id, ai_mode,
status, updated_by, updated_at, reason)
VALUES (%s, %s, %s, %s, 'live', 'active', %s, NOW(), %s)
ON CONFLICT (site_id)
DO UPDATE SET
mode = EXCLUDED.mode,
sandbox_date = EXCLUDED.sandbox_date,
sandbox_instance_id = EXCLUDED.sandbox_instance_id,
ai_mode = EXCLUDED.ai_mode,
status = EXCLUDED.status,
updated_by = EXCLUDED.updated_by,
updated_at = NOW(),
reason = EXCLUDED.reason
""",
(
body.site_id,
body.mode,
body.sandbox_date,
sandbox_instance_id,
user.user_id,
body.reason,
),
)
steps.append(RuntimeTransitionStep(
key="biz_triggers_unchanged",
title="保持业务触发器",
status="skipped",
count=0,
detail=(
"biz.trigger_jobs 为全局调度表(无 site_id 列),单门店沙箱切换不影响其它门店;"
"沙箱隔离由 runtime_mode + sandbox_instance_id 在数据写入层完成。"
),
))
conn.commit()
except Exception:
conn.rollback()
logger.exception("切换业务运行上下文失败: site_id=%s", body.site_id)
raise
finally:
conn.close()
ctx = get_runtime_context(body.site_id)
steps.append(RuntimeTransitionStep(
key="apply_context",
title="写入业务运行上下文",
status="success",
detail=(
f"当前模式={ctx.mode},业务日期={ctx.business_date}"
+ (f",沙箱实例={ctx.sandbox_instance_id}" if ctx.is_sandbox else "")
),
))
return RuntimeSwitchResponse(context=_context_response(ctx), steps=steps)
async def _stop_runtime_activity(site_id: int) -> list[RuntimeTransitionStep]:
"""终止切换前仍在运行的 ETL/AI/队列活动。"""
steps: list[RuntimeTransitionStep] = []
# 1. 终止当前进程内 ETL 执行。
try:
from app.services.task_executor import task_executor
running_ids = task_executor.get_running_ids()
cancelled = 0
for execution_id in running_ids:
if await task_executor.cancel(execution_id):
cancelled += 1
steps.append(RuntimeTransitionStep(
key="cancel_etl_processes",
title="终止当前 ETL 执行",
status="success",
count=cancelled,
detail=f"检测到 {len(running_ids)} 个当前进程内执行,已发送取消信号。",
))
except Exception as exc:
logger.exception("终止 ETL 执行失败")
steps.append(RuntimeTransitionStep(
key="cancel_etl_processes",
title="终止当前 ETL 执行",
status="warning",
detail=str(exc)[:300],
))
# 2. 清理当前门店队列中未完成任务。
conn = get_connection()
try:
with conn.cursor() as cur:
cur.execute(
"""
UPDATE task_queue
SET status = 'cancelled',
finished_at = NOW(),
error_message = COALESCE(error_message, '') || E'\n[runtime-context] 切换业务运行上下文时取消'
WHERE site_id = %s
AND status IN ('pending', 'running')
""",
(site_id,),
)
queue_cancelled = cur.rowcount
conn.commit()
steps.append(RuntimeTransitionStep(
key="cancel_task_queue",
title="取消 ETL 队列",
status="success",
count=queue_cancelled,
detail="已取消当前门店 pending/running 的 task_queue 记录。",
))
except Exception as exc:
conn.rollback()
logger.exception("取消 ETL 队列失败")
steps.append(RuntimeTransitionStep(
key="cancel_task_queue",
title="取消 ETL 队列",
status="warning",
detail=str(exc)[:300],
))
finally:
conn.close()
# 3. 取消当前站点内存 AI 调用链,并标记未完成 ai_trigger_jobs。
try:
from app.ai.dispatcher import get_dispatcher
dispatcher = get_dispatcher()
cancelled = dispatcher.cancel_running(site_id)
steps.append(RuntimeTransitionStep(
key="cancel_ai_runtime",
title="取消当前 AI 调用链",
status="success",
count=cancelled,
detail="已取消当前进程内属于该门店的 AI 异步调用链。",
))
except Exception as exc:
steps.append(RuntimeTransitionStep(
key="cancel_ai_runtime",
title="取消当前 AI 调用链",
status="warning",
detail=f"AI Dispatcher 不可用或取消失败:{str(exc)[:240]}",
))
conn = get_connection()
try:
with conn.cursor() as cur:
cur.execute(
"""
UPDATE biz.ai_trigger_jobs
SET status = 'cancelled',
finished_at = NOW(),
error_message = COALESCE(error_message, '') || E'\n[runtime-context] 切换业务运行上下文时取消'
WHERE site_id = %s
AND status IN ('pending', 'running')
""",
(site_id,),
)
ai_cancelled = cur.rowcount
conn.commit()
steps.append(RuntimeTransitionStep(
key="cancel_ai_jobs",
title="标记未完成 AI 触发",
status="success",
count=ai_cancelled,
detail="已将当前门店 pending/running 的 ai_trigger_jobs 标记为 cancelled。",
))
except Exception as exc:
conn.rollback()
logger.exception("标记 AI 触发失败")
steps.append(RuntimeTransitionStep(
key="cancel_ai_jobs",
title="标记未完成 AI 触发",
status="warning",
detail=str(exc)[:300],
))
finally:
conn.close()
return steps

View File

@@ -310,6 +310,24 @@ async def reassign_task(
)
conn.commit()
# 触发 AI 任务分配链App4 → App5
try:
from app.services.trigger_scheduler import fire_event
fire_event(
"ai_task_assigned",
{
"site_id": task["site_id"],
"member_id": task["member_id"],
"assistant_id": body.to_assistant_id,
},
)
except Exception:
logger.exception(
"触发 ai_task_assigned 事件失败: task_id=%s new_task_id=%s",
task_id, new_task_id,
)
return ReassignResponse(success=True, new_task_id=new_task_id)
except HTTPException:
conn.rollback()

View File

@@ -85,6 +85,33 @@ async def etl_completed_endpoint(
logger.exception("ETL 编排 Step2 task_generator 失败")
errors.append("task_generator failed")
# Step 3: 触发 AI 财务洞察预生成App2 × 8 时间维度)
# 若请求未带 site_id查询所有 active site 逐个触发
try:
from app.services.trigger_scheduler import fire_event
site_ids: list[int] = []
if body.site_id is not None:
site_ids = [body.site_id]
else:
from app.database import get_connection as _gc
_c = _gc()
try:
with _c.cursor() as _cur:
_cur.execute("SELECT DISTINCT site_id FROM biz.trigger_jobs WHERE site_id IS NOT NULL")
site_ids = [r[0] for r in _cur.fetchall()]
_c.commit()
finally:
_c.close()
for sid in site_ids:
try:
fire_event("ai_dws_completed", {"site_id": sid})
except Exception:
logger.exception("触发 ai_dws_completed 失败: site_id=%s", sid)
except Exception:
logger.exception("ai_dws_completed 事件批量触发失败")
success = len(errors) == 0
return EtlCompletedResponse(
success=success,

View File

@@ -204,13 +204,17 @@ async def list_site_staff(
# assumptions: cfg_assistant_level_price 有 level_code→level_name 映射
# verify: 弹窗人员下拉显示如 "初级 - 张三 - 手机号 - 入职日期 YYYY-MM-DD"
# 先查等级映射配置表feiqiu-data-rules 规则 6: 禁止硬编码)
# CHANGE 2026-05-02 | 用 business_date 替代 CURRENT_DATE沙箱按当时生效配置
from app.services.runtime_context import as_runtime_today_param
_ref_date = as_runtime_today_param(site_id)
cur.execute(
"""
SELECT DISTINCT level_code, level_name
FROM dws.cfg_assistant_level_price
WHERE effective_from <= CURRENT_DATE
AND effective_to >= CURRENT_DATE
"""
WHERE effective_from <= %s::date
AND effective_to >= %s::date
""",
(_ref_date, _ref_date),
)
level_map = {row[0]: row[1] for row in cur.fetchall()}

View File

@@ -244,27 +244,56 @@ async def chat_stream(
)
# 流式调用 DashScope Application API
async for chunk in client.call_app_stream(
# 返回 (text_chunk, session_id_or_none) 元组:累积最后一次 session_id 用于回写
latest_session_id: str | None = session_id
async for chunk_text, chunk_session_id in client.call_app_stream(
app_id=config.app_id_1_chat,
prompt=prompt,
session_id=session_id,
biz_params=biz_params,
):
full_reply_parts.append(chunk)
if chunk_session_id:
latest_session_id = chunk_session_id
if not chunk_text:
continue
full_reply_parts.append(chunk_text)
tokens_total += 1
# SSE trace: 每 10 个 token 记录一次
record_sse_token(token_count=1, total_tokens=tokens_total)
yield f"event: message\ndata: {json.dumps({'token': chunk}, ensure_ascii=False)}\n\n"
yield f"event: message\ndata: {json.dumps({'token': chunk_text}, ensure_ascii=False)}\n\n"
# 流结束:拼接完整回复并持久化
full_reply = "".join(full_reply_parts)
estimated_tokens = len(full_reply)
# Phase 1.3assistant 消息挂 reference_card若用户从特定详情页入口发起对话
try:
from app.ai.references import build_app1_reference_card
_ref_card = None
_pc = body.page_context or {}
_ctx_id = _pc.get("contextId") or _pc.get("taskId") or _pc.get("customerId") or _pc.get("coachId")
if body.source_page and _ctx_id:
_ref_card = build_app1_reference_card(body.source_page, _ctx_id)
except Exception:
logger.warning("构建 reference_card 失败", exc_info=True)
_ref_card = None
ai_msg_id, ai_created_at = svc._save_message(
body.chat_id, "assistant", full_reply, tokens_used=estimated_tokens,
body.chat_id, "assistant", full_reply,
tokens_used=estimated_tokens,
reference_card=_ref_card,
)
svc._update_session_metadata(body.chat_id, full_reply)
# multi-turn 启用:回写百炼返回的 session_id若首次对话或服务端更新
if latest_session_id and latest_session_id != session_id:
try:
svc.save_session_id(body.chat_id, latest_session_id)
except Exception:
logger.warning(
"save_session_id 失败 chat_id=%s", body.chat_id, exc_info=True,
)
# 发送 done 事件
done_data = json.dumps(
{"messageId": ai_msg_id, "createdAt": ai_created_at},

View File

@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
"""小程序业务时钟路由。
仅用于小程序读取当前门店的"业务日 / 业务年月 / 模式"——sandbox 模式下,
小程序的 performance / task-list / customer-records 等页面应以 RuntimeContext
返回的业务时钟为准,禁止再用 ``new Date()`` 构造请求参数。
端点:
- GET /api/xcx/runtime/clock — 返回当前门店的业务时钟与运行模式live / sandbox
所有端点均需 JWTapproved 状态),但不要求特定模块权限。
"""
from __future__ import annotations
from fastapi import APIRouter, Depends
from app.auth.dependencies import CurrentUser
from app.middleware.permission import require_approved
from app.services.runtime_context import get_runtime_context
from app.trace.decorators import trace_service
router = APIRouter(prefix="/api/xcx/runtime", tags=["小程序业务时钟"])
@router.get("/clock")
@trace_service("获取业务时钟", "Get business clock")
async def get_business_clock(
user: CurrentUser = Depends(require_approved),
):
"""返回当前门店的业务时钟。
返回示例live::
{
"mode": "live",
"business_date": "2026-05-02",
"business_year": 2026,
"business_month": 5,
"business_year_month": "2026-05",
"is_sandbox": false,
"sandbox_date": null
}
sandbox 模式下 ``business_date`` 等于配置的 ``sandbox_date``。
小程序页面应使用本接口结果替代 ``new Date()``,以确保 sandbox 模式下
展示和请求都对齐到 sandbox_date。
"""
ctx = get_runtime_context(user.site_id)
bd = ctx.business_date
return {
"mode": ctx.mode,
"business_date": bd.isoformat(),
"business_year": bd.year,
"business_month": bd.month,
"business_year_month": f"{bd.year:04d}-{bd.month:02d}",
"business_now": ctx.business_now.isoformat(),
"is_sandbox": ctx.is_sandbox,
"sandbox_date": ctx.sandbox_date.isoformat() if ctx.sandbox_date else None,
"sandbox_instance_id": ctx.sandbox_instance_id,
}