新增 Google Chat 对接的全套工具模块,包括: - 会话线程管理、消息解析与格式化 - Pub/Sub 消息解码、提及和命令识别 - 权限审批、目录管理和审计日志 - 消息缓存、媒体上传下载和 SSFR 防护 - 策略配置、卡片构建和流式回复支持
88 lines
2.6 KiB
Python
88 lines
2.6 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import Any
|
|
|
|
from yuxi.utils.logging_config import logger
|
|
|
|
|
|
def resolve_proxy_config(config: dict[str, Any] | None = None) -> dict[str, str | None]:
|
|
proxy_config: dict[str, str | None] = {
|
|
"http": None,
|
|
"https": None,
|
|
"no_proxy": None,
|
|
}
|
|
|
|
if config:
|
|
proxy_config["http"] = config.get("httpProxy", config.get("HTTP_PROXY"))
|
|
proxy_config["https"] = config.get("httpsProxy", config.get("HTTPS_PROXY"))
|
|
proxy_config["no_proxy"] = config.get("noProxy", config.get("NO_PROXY"))
|
|
|
|
if not proxy_config["http"]:
|
|
proxy_config["http"] = os.getenv("HTTP_PROXY")
|
|
if not proxy_config["https"]:
|
|
proxy_config["https"] = os.getenv("HTTPS_PROXY")
|
|
if not proxy_config["no_proxy"]:
|
|
proxy_config["no_proxy"] = os.getenv("NO_PROXY")
|
|
|
|
if proxy_config["http"] and not proxy_config["https"]:
|
|
proxy_config["https"] = proxy_config["http"]
|
|
|
|
return proxy_config
|
|
|
|
|
|
def build_proxies_dict(proxy_config: dict[str, str | None]) -> dict[str, str] | None:
|
|
proxies: dict[str, str] = {}
|
|
if proxy_config.get("http"):
|
|
proxies["http"] = proxy_config["http"]
|
|
if proxy_config.get("https"):
|
|
proxies["https"] = proxy_config["https"]
|
|
if proxy_config.get("no_proxy"):
|
|
proxies["no_proxy"] = proxy_config["no_proxy"]
|
|
|
|
if proxies:
|
|
logger.debug(f"Proxy configured: https={proxies.get('https')}")
|
|
return proxies
|
|
return None
|
|
|
|
|
|
def build_google_auth_request(proxy_config: dict[str, str | None] | None = None) -> Any:
|
|
try:
|
|
from google.auth.transport.requests import Request as GARequest
|
|
except ImportError:
|
|
return None
|
|
|
|
proxies = build_proxies_dict(proxy_config or {})
|
|
if not proxies:
|
|
return GARequest()
|
|
|
|
try:
|
|
result = GARequest()
|
|
result.session.proxies.update(proxies)
|
|
return result
|
|
except Exception:
|
|
logger.debug("Failed to apply proxy to Google Auth request, using default")
|
|
return GARequest()
|
|
|
|
|
|
def resolve_tls_config(config: dict[str, Any] | None = None) -> dict[str, str | None]:
|
|
tls_config: dict[str, str | None] = {
|
|
"cert": None,
|
|
"key": None,
|
|
}
|
|
|
|
if config:
|
|
tls_config["cert"] = config.get("cert", config.get("tlsCert"))
|
|
tls_config["key"] = config.get("key", config.get("tlsKey"))
|
|
|
|
if not tls_config["cert"]:
|
|
tls_config["cert"] = os.getenv("GOOGLE_CHAT_TLS_CERT")
|
|
if not tls_config["key"]:
|
|
tls_config["key"] = os.getenv("GOOGLE_CHAT_TLS_KEY")
|
|
|
|
return tls_config
|
|
|
|
|
|
def has_tls_config(tls_config: dict[str, str | None]) -> bool:
|
|
return bool(tls_config.get("cert"))
|