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:
@@ -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.3:assistant 消息挂 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},
|
||||
|
||||
Reference in New Issue
Block a user