ForcePilot/backend/package/yuxi/external_systems/integrations/servicenow/operations.py

242 lines
9.3 KiB
Python
Raw Normal View History

"""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 不自行换取
tokentoken 获取是 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"]
headers = dict(_SERVICENOW_HEADERS)
headers["Authorization"] = f"Bearer {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
assert last_response is not None
if last_response.is_success:
return last_response
# 非 2xx解析错误 body 并映射为 ExternalSystemError 子类
try:
error_body = last_response.json()
except Exception:
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"),
)
return [{"name": t["name"], "label": t.get("label", t["name"])} for t in tables]
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)