feat(backend): knowledge iteration + vision + neo4j + response contract source

dependencies.py 拆分为 dependencies/ 包; 新增 vision/ragflow_ingestion/neo4j 客户端与 h5_ai_task; alembic 045 图置信度迁移; 响应契约统一收尾。
This commit is contained in:
Simon
2026-07-09 11:47:16 +08:00
parent f5374fce9b
commit ead5f83bee
39 changed files with 5276 additions and 545 deletions
+19 -179
View File
@@ -1,185 +1,25 @@
# =============================================================================
# 企微IT智能服务台 — 阶段5 自动化 外部客户端基类
# 企微IT智能服务台 — 外部系统集成 异常/基类 统一出口
# =============================================================================
# 说明:提供异步 HTTP 客户端通用能力:
# 1. 超时控制(连接/读取分开)
# 2. 重试(tenacity:指数退避 + 抖动,仅重试超时/5xx,4xx 不重试)
# 3. 审计钩子(出入参记录到 ActionLog,由调用方注入回调)
#
# 所有外部客户端(Dify / EHR / 未来 aTrust)均继承此类,
# 保证超时、重试、审计行为在全项目一致,且方法签名完整、异常可捕获、
# 出入参可被 ActionLog 记录(满足架构审计要求)。
# 说明:自动化引擎服务层(app.services.automation.*)统一从本模块导入
# BaseClientError / BaseClient。为避免重复定义,此处直接复用
# app.core.clients.base 的权威实现。
# =============================================================================
from __future__ import annotations
import logging
import time
from typing import Any, Awaitable, Callable, Dict, Optional
import httpx
from tenacity import (
AsyncRetrying,
retry_if_exception_type,
stop_after_attempt,
wait_exponential_jitter,
from app.core.clients.base import ( # noqa: F401
BaseClient,
BaseClientError,
ClientAPIError,
ClientAuthError,
ClientConfigError,
ClientConnectionError,
)
logger = logging.getLogger(__name__)
# 默认超时(秒)
DEFAULT_CONNECT_TIMEOUT = 5.0
DEFAULT_READ_TIMEOUT = 20.0
# 默认重试次数
DEFAULT_MAX_ATTEMPTS = 3
class BaseClientError(Exception):
"""外部客户端通用异常(可被自动化引擎捕获并转人工)。"""
def __init__(self, message: str, code: int = -1, detail: str = ""):
super().__init__(message)
self.message = message
self.code = code
self.detail = detail
class BaseClient:
"""异步外部客户端基类。
Attributes:
base_url: 外部系统基址(不含尾部斜杠)
timeout: httpx 超时配置
_audit: 审计回调(可选),签名
async (event, direction, system, request, response, status, latency_ms, error)
"""
# 子类声明系统标识,用于审计与日志
system_name: str = "external"
def __init__(
self,
base_url: str,
timeout: Optional[float] = None,
audit: Optional[Callable[..., Awaitable[None]]] = None,
):
self.base_url = (base_url or "").rstrip("/")
read = timeout or DEFAULT_READ_TIMEOUT
self.timeout = httpx.Timeout(connect=DEFAULT_CONNECT_TIMEOUT, read=read)
self._audit = audit
async def _emit_audit(
self,
event: str,
direction: str,
request: Any = None,
response: Any = None,
status: str = "",
latency_ms: Optional[int] = None,
error: str = "",
) -> None:
"""审计钩子:记录出入参(落 ActionLog)。
为什么单独成方法:审计失败绝不影响主流程(仅记 warning),
避免外部审计系统抖动拖累自动化处置。
"""
if self._audit is None:
return
try:
await self._audit(
event=event,
direction=direction,
system=self.system_name,
request=request,
response=response,
status=status,
latency_ms=latency_ms,
error=error,
)
except Exception as e: # 审计失败不影响主流程
logger.warning(f"[{self.system_name}] 审计钩子执行失败: {e}")
async def request(
self,
method: str,
path: str,
*,
json_data: Optional[Dict] = None,
params: Optional[Dict] = None,
headers: Optional[Dict] = None,
event: str = "",
retry: bool = True,
) -> Dict[str, Any]:
"""统一请求封装(带超时 + 重试 + 审计)。
Args:
method: HTTP 方法
path: 路径(自动拼接 base_url
json_data/params/headers: 请求参数
event: 审计事件名(如 "dify.intent"
retry: 是否启用重试(仅对超时/5xx 重试,4xx 不重试)
Returns:
Dict: 解析后的 JSON 响应
Raises:
BaseClientError: 网络/HTTP/业务错误
"""
url = f"{self.base_url}{path}"
start = time.monotonic()
async def _do() -> httpx.Response:
async with httpx.AsyncClient(timeout=self.timeout) as client:
return await client.request(
method, url, json=json_data, params=params, headers=headers
)
try:
if retry:
resp: Optional[httpx.Response] = None
async for attempt in AsyncRetrying(
stop=stop_after_attempt(DEFAULT_MAX_ATTEMPTS),
wait=wait_exponential_jitter(initial=0.5, max=3.0),
retry=retry_if_exception_type(
(httpx.TimeoutException, httpx.ConnectError, httpx.HTTPStatusError)
),
reraise=True,
):
with attempt:
resp = await _do()
# 4xx 是业务错误,不重试
if resp.status_code >= 400 and resp.status_code < 500:
raise httpx.HTTPStatusError(
message=f"HTTP {resp.status_code}",
request=resp.request,
response=resp,
)
assert resp is not None
else:
resp = await _do()
latency_ms = int((time.monotonic() - start) * 1000)
try:
data = resp.json()
except Exception:
data = {"raw": resp.text}
status = "success" if resp.status_code < 400 else f"http_{resp.status_code}"
await self._emit_audit(event, "out", json_data or params, data, status, latency_ms)
if resp.status_code >= 400:
raise BaseClientError(
message=f"{self.system_name} 返回 HTTP {resp.status_code}",
code=resp.status_code,
detail=resp.text[:500],
)
return data
except (httpx.TimeoutException, httpx.ConnectError) as e:
latency_ms = int((time.monotonic() - start) * 1000)
await self._emit_audit(event, "out", json_data or params, None, "error", latency_ms, str(e))
raise BaseClientError(message=f"{self.system_name} 网络异常: {e}", code=-1, detail=str(e))
except BaseClientError:
raise
except Exception as e:
latency_ms = int((time.monotonic() - start) * 1000)
await self._emit_audit(event, "out", json_data or params, None, "error", latency_ms, str(e))
raise BaseClientError(message=f"{self.system_name} 请求异常: {e}", code=-1, detail=str(e))
__all__ = [
"BaseClient",
"BaseClientError",
"ClientConfigError",
"ClientConnectionError",
"ClientAuthError",
"ClientAPIError",
]
+10
View File
@@ -0,0 +1,10 @@
# =============================================================================
# 企微IT智能服务台 — 北森 EHR 集成包(自动化引擎统一出口)
# =============================================================================
# 说明:提供 BeisenEHRClient / get_ehr_client 的统一导出,
# 实际实现位于 app.core.clients.ehr(环境变量 AUTOMATION_EHR_* 驱动)。
# =============================================================================
from app.core.clients.ehr import BeisenEHRClient, get_ehr_client # noqa: F401
__all__ = ["BeisenEHRClient", "get_ehr_client"]
+42 -77
View File
@@ -1,95 +1,60 @@
# =============================================================================
# 企微IT智能服务台 — 阶段5 自动化 外部客户端工厂
# 企微IT智能服务台 — 外部系统集成 客户端工厂
# =============================================================================
# 说明:集中解析各外部系统的配置来源:
# - 优先使用 settings 中的 AUTOMATION_* 环境变量(架构约定)
# - 缺失时回退到既有 system_configs 表配置
# huorong/lianruan/ragflow 在 app/integrations/*/config.py 中已有 getter
# 说明:自动化引擎服务层(app.services.automation.*)统一从此处构建外部客户端。
#
# 为什么有工厂:自动化引擎既能用新加的 AUTOMATION_* 配置,也能复用阶段1-4
# 已落地的集成配置,避免重复维护两套配置源
# 设计:
# - 所有构建函数均为 coroutine,返回客户端实例或 None(未配置时)
# - 底层客户端来自 app.core.clients.*(环境变量 AUTOMATION_* 驱动),
# 与 app/integrations/{huorong,lianruan,ragflow}(管理后台 DB 配置)区分,
# 避免相互干扰。
# - 返回 None 时,引擎自动降级(关键词兜底 / EHR 兜底 / 转人工)。
#
# 调用约定(保持与现有服务层一致):
# build_huorong_client(db, audit) / build_lianruan_client(db, audit)
# build_dify_client(audit) / build_ehr_client(audit) / build_ragflow_client(audit)
# =============================================================================
from __future__ import annotations
import logging
from typing import Any, Callable, Optional
from typing import Any, Optional
from sqlalchemy.ext.asyncio import AsyncSession
from app.config import settings
logger = logging.getLogger(__name__)
from app.core.clients.dify import get_dify_client
from app.core.clients.ehr import get_ehr_client
from app.core.clients.huorong import get_huorong_client
from app.core.clients.lianruan import get_lianruan_client
from app.core.clients.ragflow import get_ragflow_client
async def build_huorong_client(
db: AsyncSession, audit: Optional[Callable[..., Any]] = None
):
"""构建火绒客户端:settings 优先,否则 system_configs。"""
from app.integrations.huorong.client import HuorongClient
if (
settings.automation_huorong_base_url
and settings.automation_huorong_access_key_id
and settings.automation_huorong_access_key_secret
):
return HuorongClient(
access_key_id=settings.automation_huorong_access_key_id,
access_key_secret=settings.automation_huorong_access_key_secret,
base_url=settings.automation_huorong_base_url,
)
from app.integrations.huorong.config import get_huorong_client
return await get_huorong_client(db)
async def build_huorong_client(db: Any = None, audit: Any = None) -> Optional[Any]:
"""构建火绒客户端(未配置返回 None)。"""
return await get_huorong_client(audit=audit)
async def build_lianruan_client(
db: AsyncSession, audit: Optional[Callable[..., Any]] = None
):
"""构建联软客户端:settings 优先,否则 system_configs。"""
from app.integrations.lianruan.client import LianruanClient
if (
settings.automation_lianruan_base_url
and settings.automation_lianruan_api_account
and settings.automation_lianruan_api_password
):
return LianruanClient(
base_url=settings.automation_lianruan_base_url,
api_account=settings.automation_lianruan_api_account,
api_password=settings.automation_lianruan_api_password,
validate_key=settings.automation_lianruan_validate_key,
)
from app.integrations.lianruan.config import get_lianruan_client
return await get_lianruan_client(db)
async def build_lianruan_client(db: Any = None, audit: Any = None) -> Optional[Any]:
"""构建联软客户端(未配置返回 None)。"""
return await get_lianruan_client(db=db, audit=audit)
async def build_ragflow_client(
db: AsyncSession, audit: Optional[Callable[..., Any]] = None
):
"""构建 RAGFlow 客户端:settings 优先,否则 system_configs。"""
from app.integrations.ragflow.client import RagflowClient
if settings.automation_ragflow_base_url and settings.automation_ragflow_api_key:
return RagflowClient(
api_key=settings.automation_ragflow_api_key,
base_url=settings.automation_ragflow_base_url,
)
from app.integrations.ragflow.config import get_ragflow_client
return await get_ragflow_client(db)
async def build_dify_client(audit: Optional[Callable[..., Any]] = None):
"""构建 Dify 客户端(仅 settings)。"""
from app.integrations.dify import get_dify_client
async def build_dify_client(audit: Any = None) -> Optional[Any]:
"""构建 Dify 意图识别客户端(未配置返回 None)。"""
return await get_dify_client(audit=audit)
async def build_ehr_client(audit: Optional[Callable[..., Any]] = None):
"""构建 EHR 客户端(仅 settings)。"""
from app.integrations.ehr import get_ehr_client
async def build_ehr_client(audit: Any = None) -> Optional[Any]:
"""构建北森 EHR 兜底客户端(未配置返回 None)。"""
return await get_ehr_client(audit=audit)
async def build_ragflow_client(audit: Any = None) -> Optional[Any]:
"""构建 RAGFlow 知识检索客户端(未配置返回 None)。"""
return await get_ragflow_client(audit=audit)
__all__ = [
"build_huorong_client",
"build_lianruan_client",
"build_dify_client",
"build_ehr_client",
"build_ragflow_client",
]