72 lines
2.3 KiB
Python
72 lines
2.3 KiB
Python
"""
|
|
backend/app/services/hxops_tool_api.py
|
|
|
|
用途:解析 HXOPS 两类对外基地址——Dify Tool API(服务端)与用户浏览器访问(下载链等)。
|
|
依赖:Flask current_app、core.http_context。
|
|
逻辑:Dify/RPA 调 Tool API 走 resolve_HXOPS_TOOLS_API_BASE;用户可见下载链走 resolve_hxops_user_facing_base_url。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from flask import current_app
|
|
|
|
from app.core.http_context import has_http_request, host_url
|
|
|
|
|
|
def _resolve_config(config: dict | None = None) -> dict:
|
|
"""读取 Flask config 或空 dict。"""
|
|
if config is not None:
|
|
return config
|
|
if current_app:
|
|
return current_app.config
|
|
return {}
|
|
|
|
|
|
def resolve_HXOPS_TOOLS_API_BASE(*, config: dict | None = None, request_host: str | None = None) -> str:
|
|
"""
|
|
返回 Dify / RPA 等服务端调用 Tool API 时使用的根地址(无尾斜杠)。
|
|
|
|
核心逻辑:仅读 HXOPS_TOOLS_API_BASE(常为内网 IP),与浏览器下载链、PUBLIC_BASE_URL 分离。
|
|
|
|
参数:
|
|
config: 可选配置字典;缺省时读 current_app.config。
|
|
request_host: 可选请求 host_url;缺省且在请求上下文中时取 host_url()。
|
|
|
|
返回:
|
|
无尾斜杠的根地址,如 http://192.168.100.192:8890。
|
|
"""
|
|
cfg = _resolve_config(config)
|
|
|
|
configured = str(cfg.get("HXOPS_TOOLS_API_BASE") or "").strip().rstrip("/")
|
|
if configured:
|
|
return configured
|
|
|
|
host = request_host
|
|
if host is None and has_http_request():
|
|
host = host_url()
|
|
return host or ""
|
|
|
|
|
|
def resolve_hxops_user_facing_base_url(*, config: dict | None = None) -> str:
|
|
"""
|
|
返回浏览器侧可访问的根地址,用于 Excel 签名下载等用户可见链接。
|
|
|
|
核心逻辑:仅读 HXOPS_PUBLIC_BASE_URL / WECOM_PUBLIC_BASE_URL,不复用 Tool API 内网地址或通知地址。
|
|
|
|
参数:
|
|
config: 可选配置字典;缺省时读 current_app.config。
|
|
|
|
返回:
|
|
无尾斜杠的根地址,如 https://ai.example.com。
|
|
"""
|
|
cfg = _resolve_config(config)
|
|
|
|
for key in ("HXOPS_PUBLIC_BASE_URL", "WECOM_PUBLIC_BASE_URL"):
|
|
configured = str(cfg.get(key) or "").strip().rstrip("/")
|
|
if configured:
|
|
return configured
|
|
|
|
if has_http_request():
|
|
return host_url()
|
|
|
|
return resolve_HXOPS_TOOLS_API_BASE(config=cfg)
|