ForcePilot/backend/package/yuxi/external_systems/integrations/servicenow/operations.py
Kris 55188ff7fa refactor: 整理代码结构并修复多处细节问题
1. 调整导入顺序与格式,修复重复导入
2. 替换通用异常捕获为更精准的ValueError
3. 重构持久层仓库的get_deleted/list_deleted方法,添加ORM转领域模型逻辑
4. 完善配置校验与参数合法性检查,新增缺失字段校验
5. 优化多行字符串与函数调用的换行格式
6. 修复通知服务状态判定逻辑与文档描述
7. 简化无用的TYPE_CHECKING代码块
8. 重构ServiceNow元数据发现逻辑,增加字段合法性校验
9. 修复数据库查询验证器的子查询处理逻辑
10. 完善领域模型的初始化校验与属性语义
2026-07-11 07:34:48 +08:00

250 lines
9.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""ServiceNow discover / preview_tools / create_tools handler 实现。
handler 签名遵循 ``OperationHandler`` 协议:
``(source_type: str, system_config: dict[str, Any]) -> Awaitable[Any]``。
handler 内部职责:
1. 从 ``system_config`` 提取 ``base_url`` / ``api_version`` / ``_resolved_token`` 等
2. 构建 ``auth_headers``token 由 use_cases 层注入到 ``system_config["_resolved_token"]``
3. 构造带 429 退避的 ``request_fn``,注入到生成器 payload
4. 调用 ``ServiceNowSourceGenerator.generate(payload)``
429 退避分层(见设计文档 §6.2 / §8.2
- discover/preview/create 阶段:本模块 ``_request_with_retry`` 实现(解析 Retry-After
- 运行时工具执行:通过工具 ``retry_policy.retry_status_codes: [429]`` 配置
"""
from __future__ import annotations
import asyncio
from collections.abc import Awaitable, Callable
from typing import TYPE_CHECKING, Any
from yuxi.external_systems.exceptions import (
AuthError,
DomainValidationError,
ExecutionError,
RateLimitExceededError,
)
from yuxi.external_systems.integrations.schemas import GeneratedToolsDraft
from yuxi.external_systems.integrations.servicenow.error_extractor import (
map_servicenow_error,
)
from yuxi.external_systems.integrations.servicenow.generators import (
ServiceNowSourceGenerator,
)
if TYPE_CHECKING:
import httpx
# discover/preview/create 阶段 429 退避参数
_RETRY_MAX_ATTEMPTS = 5
_RETRY_BASE_DELAY = 1.0
# ServiceNow 通用请求头(除 Authorization 外)
_SERVICENOW_HEADERS: dict[str, str] = {
"Accept": "application/json",
}
def _build_auth_headers(system_config: dict[str, Any]) -> dict[str, str]:
"""从 system_config 提取 token构建 ServiceNow 请求头。
system_config 中 token 由 use_cases 层通过 ``token_manager.get_token`` 预处理后
注入到 ``system_config["_resolved_token"]["access_token"]``。handler 不自行换取
token——token 获取是 use_cases 层的职责。
"""
token_info = system_config.get("_resolved_token")
if not isinstance(token_info, dict) or not token_info.get("access_token"):
raise AuthError("servicenow: system_config 缺少 _resolved_tokenuse_cases 层未注入 token")
access_token = token_info["access_token"]
token_type = token_info.get("token_type", "Bearer")
headers = dict(_SERVICENOW_HEADERS)
headers["Authorization"] = f"{token_type} {access_token}"
headers["Content-Type"] = "application/json"
return headers
def _resolve_base_url(system_config: dict[str, Any]) -> str:
"""从 system_config 提取 base_url支持 instance 动态拼接。"""
connection_config = system_config.get("connection_config") or {}
base_url = connection_config.get("base_url")
instance = connection_config.get("instance")
if not base_url and instance:
base_url = f"https://{instance}.service-now.com"
if not base_url:
raise DomainValidationError("servicenow: system_config.connection_config.base_url 或 instance 至少需填写一项")
return str(base_url)
def _resolve_api_version(system_config: dict[str, Any]) -> str | None:
"""从 system_config 提取 api_version。"""
connection_config = system_config.get("connection_config") or {}
return connection_config.get("api_version")
def _resolve_tables(system_config: dict[str, Any]) -> list[str] | None:
"""从 system_config.discovery_options.tables 提取待生成的表名列表。"""
discovery_options = system_config.get("discovery_options") or {}
return discovery_options.get("tables")
async def _request_with_retry(
method: str,
url: str,
headers: dict[str, str],
*,
max_retries: int = _RETRY_MAX_ATTEMPTS,
base_delay: float = _RETRY_BASE_DELAY,
) -> httpx.Response:
"""handler 内部 HTTP 调用的 429 退避(仅用于 discover/preview/create 阶段)。
优先使用响应头 ``Retry-After``,否则指数退避(``base_delay * 2 ** attempt``)。
运行时工具执行的 429 退避由框架 executor + 工具 retry_policy 处理,
不在此函数范围内。
非 2xx 响应(非 429通过 ``map_servicenow_error`` 转换为 ExternalSystemError 子类。
网络层异常(超时 / 连接错误)转换为 ``ExecutionError``。
"""
import httpx
try:
async with httpx.AsyncClient() as client:
last_response: httpx.Response | None = None
for attempt in range(max_retries + 1):
response = await client.request(method, url, headers=headers)
if response.status_code != 429:
last_response = response
break
if attempt == max_retries:
retry_after_header = response.headers.get("Retry-After", "60")
try:
retry_after = int(retry_after_header)
except ValueError:
retry_after = 60
raise RateLimitExceededError(
f"ServiceNow 限流,已重试 {max_retries} 次仍失败: {url}",
retry_after=retry_after,
limit_type="qps",
)
# 优先使用 Retry-After 头,否则指数退避
retry_after_header = response.headers.get("Retry-After")
if retry_after_header:
try:
delay = float(retry_after_header)
except ValueError:
delay = base_delay * (2**attempt)
else:
delay = base_delay * (2**attempt)
await asyncio.sleep(delay)
except httpx.RequestError as exc:
raise ExecutionError(f"ServiceNow 网络请求失败: {exc}") from exc
if last_response is None:
raise ExecutionError("ServiceNow 请求未返回响应last_response 为空)")
if last_response.is_success:
return last_response
# 非 2xx解析错误 body 并映射为 ExternalSystemError 子类
try:
error_body = last_response.json()
except ValueError:
error_body = {"error": {"message": last_response.text}}
raise map_servicenow_error(last_response.status_code, error_body)
def _make_request_fn() -> Callable[[str, str, dict[str, str]], Awaitable[httpx.Response]]:
"""构造带 429 退避的 request_fn供 ServiceNowMetadataClient 使用。"""
async def request_fn(method: str, url: str, headers: dict[str, str]) -> httpx.Response:
return await _request_with_retry(method, url, headers)
return request_fn
def _build_generator_payload(system_config: dict[str, Any]) -> dict[str, Any]:
"""从 system_config 构造生成器 payload。
生成器 payload 是 system_config 的子集,包含:
- base_url: str
- auth_headers: dict[str, str]
- auth_config: dict — 透传到工具的 auth_config 字段
- auth_type: str — 透传到工具的 auth_type 字段
- api_version: str | None
- tables: list[str] | None
- request_fn: 带 429 退避的 HTTP 请求函数
"""
return {
"base_url": _resolve_base_url(system_config),
"auth_headers": _build_auth_headers(system_config),
"auth_config": system_config.get("auth_config") or {},
"auth_type": system_config.get("auth_type", "oauth2_client_credentials"),
"api_version": _resolve_api_version(system_config),
"tables": _resolve_tables(system_config),
"request_fn": _make_request_fn(),
}
async def discover_servicenow(
source_type: str,
system_config: dict[str, Any],
) -> list[dict[str, Any]]:
"""discover handler发现 ServiceNow 实例可用表sys_db_object
Returns:
表摘要列表,每项含 ``name`` / ``label``。
"""
from yuxi.external_systems.integrations.servicenow.metadata_discovery import (
ServiceNowMetadataClient,
)
base_url = _resolve_base_url(system_config)
auth_headers = _build_auth_headers(system_config)
request_fn = _make_request_fn()
client = ServiceNowMetadataClient(
base_url=base_url,
auth_headers=auth_headers,
request_fn=request_fn,
)
options = system_config.get("discovery_options") or {}
tables = await client.list_tables(
include_patterns=options.get("include_patterns"),
exclude_patterns=options.get("exclude_patterns"),
)
summaries: list[dict[str, Any]] = []
for t in tables:
name = t.get("name")
if not name:
raise ExecutionError("ServiceNow discover: 返回的表记录缺少 name 字段")
summaries.append({"name": name, "label": t.get("label", name)})
return summaries
async def preview_servicenow_tools(
source_type: str,
system_config: dict[str, Any],
) -> list[dict[str, Any]]:
"""preview_tools handler预览将生成的工具列表不持久化
Returns:
工具预览列表,每项为 ExternalToolCreateInput 兼容的 dict 摘要。
"""
payload = _build_generator_payload(system_config)
generator = ServiceNowSourceGenerator()
tools = await generator.generate(payload)
return [{"slug": t["slug"], "name": t["name"], "description": t["description"]} for t in tools]
async def create_servicenow_tools(
source_type: str,
system_config: dict[str, Any],
) -> GeneratedToolsDraft:
"""create_tools handler生成工具草稿不直接持久化
Returns:
GeneratedToolsDraft由 use_cases 层统一持久化。
"""
payload = _build_generator_payload(system_config)
generator = ServiceNowSourceGenerator()
tool_configs = await generator.generate(payload)
return GeneratedToolsDraft(tool_configs=tool_configs, override_existing=False)