feat(shipping-notice): 新增货代客户管理功能,支持客户导入、识别证据管理与客户确认
This commit is contained in:
1 parent
b595e157e9
commit
4fbaed2b4c
176 files changed
+14011
-994
No files matched your search
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
backend/app/services/chat/executors/__init__.py
|
||||
|
||||
用途:RUNTIME_PROVIDER_REGISTRY 与聊天 Executor 解析入口。
|
||||
用途:RUNTIME_PROVIDER_REGISTRY、聊天 Executor 解析及同步 Handler 前置准备入口。
|
||||
依赖:dify_chat、hybrid、builtin executors;agent_registry.resolver。
|
||||
逻辑:chat.py completions 默认 Dify 路径调用 stream_chat_via_executor。
|
||||
逻辑:chat.py 先调用 stream_chat_via_executor 同步执行 prepare_stream,再下发 preface 并惰性迭代上游流。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -12,7 +12,11 @@ from typing import Iterator
|
||||
|
||||
from app.core.errors import ValidationError
|
||||
from app.services.agent_registry.handlers.base import ChatStreamContext
|
||||
from app.services.agent_registry.resolver import resolve_runtime_provider
|
||||
from app.services.agent_registry.resolver import (
|
||||
resolve_handler,
|
||||
resolve_handler_for_agent_code,
|
||||
resolve_runtime_provider,
|
||||
)
|
||||
from app.services.chat.executors.base import ChatExecutor
|
||||
from app.services.chat.executors.builtin import BuiltinChatExecutor
|
||||
from app.services.chat.executors.dify_chat import DifyChatExecutor
|
||||
@@ -61,10 +65,19 @@ def stream_chat_via_executor(ctx: ChatStreamContext) -> Iterator[str]:
|
||||
Args:
|
||||
ctx: ChatStreamContext。
|
||||
|
||||
核心逻辑:必须在返回惰性生成器前同步调用 Handler.prepare_stream,确保 chat.py 随后的 preface
|
||||
能读取到已登记的领域状态;Hybrid/Builtin 强制要求 Handler,Dify Chatflow 允许无 Handler。
|
||||
|
||||
Returns:
|
||||
上游 SSE 行迭代器。
|
||||
已完成 Handler 前置准备的上游 SSE 行迭代器。
|
||||
"""
|
||||
executor = resolve_chat_executor(ctx.agent)
|
||||
if executor.provider_key in {'hybrid', 'builtin'}:
|
||||
handler = resolve_handler(ctx.agent)
|
||||
else:
|
||||
handler = resolve_handler_for_agent_code(ctx.agent)
|
||||
if handler is not None:
|
||||
ctx = handler.prepare_stream(ctx)
|
||||
return executor.stream(ctx)
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ backend/app/services/chat/executors/builtin.py
|
||||
|
||||
用途:runtime_provider=builtin 的聊天 Executor。
|
||||
依赖:agent_registry.resolver.resolve_handler。
|
||||
逻辑:必须存在 Handler;由 Handler 驱动内置流(v1 未实现完整 builtin 流时显式报错)。
|
||||
逻辑:stream_chat_via_executor 已校验并准备 Handler;v1 未实现完整 builtin 流时显式报错。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -21,8 +21,8 @@ class BuiltinChatExecutor(ChatExecutor):
|
||||
provider_key = 'builtin'
|
||||
|
||||
def stream(self, ctx: ChatStreamContext) -> Iterator[str]:
|
||||
"""返回尚未实现的明确错误,并保留 Handler key 便于诊断。"""
|
||||
handler = resolve_handler(ctx.agent)
|
||||
ctx = handler.prepare_stream(ctx)
|
||||
raise AppError(
|
||||
code='AGENT.BUILTIN_NOT_IMPLEMENTED',
|
||||
message='runtime_provider=builtin 的聊天流尚未实现,请使用 hybrid 或 dify',
|
||||
|
||||
@@ -2,15 +2,14 @@
|
||||
backend/app/services/chat/executors/dify_chat.py
|
||||
|
||||
用途:runtime_provider=dify 的 Chatflow 聊天 Executor。
|
||||
依赖:DifyService.stream_chat、executors.base、agent_registry.resolver。
|
||||
逻辑:Handler.prepare_stream → build_chat_dify_inputs → DifyService.stream_chat。
|
||||
依赖:DifyService.stream_chat、executors.base。
|
||||
逻辑:stream_chat_via_executor 已完成 Handler 前置准备;本文件构造 inputs 并惰性转发 Dify SSE。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Iterator
|
||||
|
||||
from app.services.agent_registry.resolver import resolve_handler_for_agent_code
|
||||
from app.services.chat.executors.base import (
|
||||
ChatExecutor,
|
||||
ChatStreamContext,
|
||||
@@ -25,9 +24,7 @@ class DifyChatExecutor(ChatExecutor):
|
||||
provider_key = 'dify'
|
||||
|
||||
def stream(self, ctx: ChatStreamContext) -> Iterator[str]:
|
||||
handler = resolve_handler_for_agent_code(ctx.agent)
|
||||
if handler is not None:
|
||||
ctx = handler.prepare_stream(ctx)
|
||||
"""使用已经完成 Handler 前置准备的上下文调用 Dify Chatflow。"""
|
||||
dify_inputs = build_dify_inputs_for_context(ctx)
|
||||
yield from DifyService.stream_chat(
|
||||
ctx.query,
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
"""
|
||||
backend/app/services/chat/executors/hybrid.py
|
||||
|
||||
用途:runtime_provider=hybrid 的聊天 Executor(Handler 前置 + Dify 流)。
|
||||
依赖:resolve_handler、DifyChatExecutor。
|
||||
逻辑:resolve_handler 校验 → handler.prepare_stream → DifyChatExecutor.stream。
|
||||
用途:runtime_provider=hybrid 的聊天 Executor(强制 Handler + Dify 流)。
|
||||
依赖:DifyChatExecutor;Handler 校验与前置准备由 stream_chat_via_executor 统一完成。
|
||||
逻辑:接收已准备上下文并委托 DifyChatExecutor.stream。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Iterator
|
||||
|
||||
from app.services.agent_registry.resolver import resolve_handler
|
||||
from app.services.chat.executors.base import ChatExecutor, ChatStreamContext
|
||||
from app.services.chat.executors.dify_chat import DifyChatExecutor
|
||||
|
||||
@@ -24,6 +23,5 @@ class HybridChatExecutor(ChatExecutor):
|
||||
self._dify = DifyChatExecutor()
|
||||
|
||||
def stream(self, ctx: ChatStreamContext) -> Iterator[str]:
|
||||
handler = resolve_handler(ctx.agent)
|
||||
ctx = handler.prepare_stream(ctx)
|
||||
"""把已完成 Handler 准备的 Hybrid 上下文委托给 Dify Chatflow。"""
|
||||
yield from self._dify.stream(ctx)
|
||||
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
backend/app/services/chat/log_service.py
|
||||
|
||||
用途:Chat Turn 与对话历史的 ChatLog 统一写入。
|
||||
用途:Chat Turn 与对话历史的 ChatLog 统一写入及结构化 payload 合并。
|
||||
依赖:models.ChatLog、SQLAlchemy。
|
||||
逻辑:turn_runtime 在 run.completed 时调用 save_turn_chat_log;api/chat 历史路由可逐步迁入。
|
||||
逻辑:turn_runtime 在 run.completed 时调用 save_turn_chat_log;领域 Handler 可在上游终态后安全合并回放 payload。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -72,3 +72,61 @@ def save_turn_chat_log(
|
||||
current_app.logger.exception("[chat-turn] failed to save chat log: %s", exc)
|
||||
db.session.rollback()
|
||||
return None
|
||||
|
||||
|
||||
def merge_chat_log_payload(
|
||||
*,
|
||||
chat_log_id: int,
|
||||
user_id: int,
|
||||
agent_id: int,
|
||||
session_id: str,
|
||||
payload_patch: dict[str, Any],
|
||||
) -> bool:
|
||||
"""
|
||||
在归属校验通过后幂等合并 ChatLog 结构化 payload。
|
||||
|
||||
核心逻辑:锁定指定消息并同时校验用户、Agent、本地会话,随后以顶层键覆盖方式合并领域终态;
|
||||
该函数不修改正文,适合 SSE terminal 之后补写可回放的状态卡片数据。
|
||||
|
||||
Args:
|
||||
chat_log_id: 已持久化的 ChatLog 主键,必须为正整数。
|
||||
user_id: 当前请求用户主键。
|
||||
agent_id: 当前请求 Agent 主键。
|
||||
session_id: 当前 HXOPS 本地会话号。
|
||||
payload_patch: 要合并的顶层结构化字段,不得为空。
|
||||
|
||||
Returns:
|
||||
找到且成功写入时返回 True;身份不匹配、参数为空或写入失败时返回 False。
|
||||
"""
|
||||
if chat_log_id <= 0 or user_id <= 0 or agent_id <= 0 or not str(session_id or '').strip() or not payload_patch:
|
||||
return False
|
||||
try:
|
||||
chat_log = (
|
||||
db.session.query(ChatLog)
|
||||
.filter(
|
||||
ChatLog.id == int(chat_log_id),
|
||||
ChatLog.user_id == int(user_id),
|
||||
ChatLog.agent_id == int(agent_id),
|
||||
ChatLog.session_id == str(session_id).strip(),
|
||||
)
|
||||
.with_for_update()
|
||||
.one_or_none()
|
||||
)
|
||||
if chat_log is None:
|
||||
db.session.rollback()
|
||||
return False
|
||||
chat_log.payload = {
|
||||
**(chat_log.payload if isinstance(chat_log.payload, dict) else {}),
|
||||
**payload_patch,
|
||||
}
|
||||
db.session.commit()
|
||||
return True
|
||||
except Exception as exc:
|
||||
if current_app:
|
||||
current_app.logger.exception(
|
||||
"[chat-log] failed to merge payload chat_log_id=%s: %s",
|
||||
chat_log_id,
|
||||
exc,
|
||||
)
|
||||
db.session.rollback()
|
||||
return False
|
||||
@@ -42,7 +42,7 @@ def iter_handler_terminal_sse(
|
||||
*,
|
||||
emitted_keys: set[str],
|
||||
) -> Iterator[str]:
|
||||
"""上游终态事件后追加 Handler SSE(如 shipping_ui_state 刷新)。"""
|
||||
"""上游终态/error 事件后追加 Handler SSE(如订单抽取收口与 shipping_ui_state 刷新)。"""
|
||||
handler = resolve_handler_for_agent_code(ctx.agent)
|
||||
if handler is None:
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user