代码修改
This commit is contained in:
1 parent
6d02e2c89e
commit
eeb7f30585
5 files changed
+81
-52
No files matched your search
@@ -71,6 +71,7 @@ from app.gateway.routers.multi_agent import (
|
||||
_find_trailing_ai_text,
|
||||
_flatten_content,
|
||||
_loopback_base,
|
||||
_make_loopback_client,
|
||||
_parse_last_messages,
|
||||
_run_payload,
|
||||
_stream_upstream,
|
||||
@@ -108,13 +109,10 @@ async def init_intent(request: Request, payload: dict = Body(...)) -> dict[str,
|
||||
"""
|
||||
base = _loopback_base(request)
|
||||
headers = _auth_headers(request)
|
||||
# trust_env=False: this client only talks to the same-process gateway at
|
||||
# 127.0.0.1, so honoring HTTP_PROXY / HTTPS_PROXY env vars (set system-wide
|
||||
# on the dev machine) makes httpx route loopback calls through an upstream
|
||||
# proxy that can't reach 127.0.0.1, costing 40+ seconds of timeout per
|
||||
# request before falling back. Symptom in logs: `headers_in=42.160s`.
|
||||
# ASGITransport:进程内直调本机 ASGI app,**不走 TCP / 不经过网络栈**,
|
||||
# 因此既无须 trust_env=False 防代理,也不存在 loopback 40s 超时这种坑。
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=None, trust_env=False) as client:
|
||||
async with _make_loopback_client(request) as client:
|
||||
thread_id = await _create_thread(client, base, headers)
|
||||
except HTTPException:
|
||||
# 上游已用结构化方式失败,原样传给前端不需要二次包装。
|
||||
@@ -165,11 +163,9 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
|
||||
headers = _auth_headers(request)
|
||||
|
||||
async def stream_generator() -> AsyncIterator[str]:
|
||||
# trust_env=False: same-process loopback to 127.0.0.1 — honoring
|
||||
# HTTP_PROXY / HTTPS_PROXY env vars (commonly set on dev machines)
|
||||
# causes httpx to route loopback calls through an unreachable upstream
|
||||
# proxy and wait ~40 seconds for timeout before falling back.
|
||||
async with httpx.AsyncClient(timeout=None, trust_env=False) as client:
|
||||
# ASGITransport:进程内直调本机 ASGI app,**不走 TCP / 不经过网络栈**,
|
||||
# 因此既无须 trust_env=False 防代理,也不存在 loopback 40s 超时这种坑。
|
||||
async with _make_loopback_client(request) as client:
|
||||
try:
|
||||
body = _run_payload(
|
||||
agent_name=INTENT_AGENT_ID,
|
||||
|
||||
@@ -97,13 +97,12 @@ def _diagnose_exception(exc: BaseException, *, context: str = "") -> dict[str, A
|
||||
|
||||
if isinstance(root, httpx.ConnectError):
|
||||
target = str(root).strip().lower()
|
||||
# loopback 失败 = Gateway 自己没起 / 代理拦回环;outbound 失败 = LLM 提供商不可达。
|
||||
# loopback 已改用 ASGITransport(进程内直调),不再经网络;ConnectError 几乎全部来自上游 LLM。
|
||||
if "127.0.0.1" in target or "localhost" in target or "loopback" in target:
|
||||
code = "loopback_connect_failed"
|
||||
hint = (
|
||||
"本地 Gateway loopback 连接失败:确认后端进程在 8001 端口监听;"
|
||||
"另检查 HTTP_PROXY / HTTPS_PROXY 是否把 127.0.0.1 也走了代理"
|
||||
"(代码已显式 trust_env=False,理论上不该走代理)。"
|
||||
"本地 Gateway ASGI loopback 失败:这种情况罕见,通常意味着 ASGI app "
|
||||
"未正确注入到 request.app,或路由层抛出未捕获异常。请查后端日志栈。"
|
||||
)
|
||||
else:
|
||||
code = "upstream_connect_failed"
|
||||
@@ -160,12 +159,44 @@ def _diagnose_exception(exc: BaseException, *, context: str = "") -> dict[str, A
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Loopback 辅助函数(Gateway → Gateway,同进程)
|
||||
#
|
||||
# 通过 httpx ASGITransport 直接调用本进程的 ASGI app,**不走 TCP / 不经过网络栈**:
|
||||
# - 彻底规避「开发机 HTTP_PROXY 把 loopback 也走代理」导致的 40s 超时
|
||||
# - 不再需要 trust_env=False / 真实端口 / 真实 host
|
||||
# - 路由分发、鉴权 middleware、Pydantic 校验、SSE 流式响应一切照旧
|
||||
#
|
||||
# 历史背景:早期实现走 `httpx.AsyncClient(trust_env=False)` + `127.0.0.1:<port>`,
|
||||
# 见 git log 与 multi-agent-backend-dev.md §9.1。改用 ASGITransport 后已不需要
|
||||
# 防代理 workaround,但 _loopback_base 仍返回一个占位 base_url 以保持原有 helper
|
||||
# 函数签名(_create_thread / _stream_upstream 等)零改动。
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ASGITransport 不在意 host,仅作 URL 拼接占位。
|
||||
_LOOPBACK_BASE_URL = "http://loopback"
|
||||
|
||||
|
||||
def _loopback_base(request: Request) -> str:
|
||||
"""当前请求的 scheme+host,用于 httpx 打回本机 Gateway。"""
|
||||
return f"{request.url.scheme}://{request.url.netloc}"
|
||||
"""返回 loopback base URL 占位串。
|
||||
|
||||
ASGI transport 模式下不再用真实 scheme+host,但保留此函数以兼容
|
||||
`_create_thread / _stream_upstream` 等内部 helper 的 `base` 参数。
|
||||
"""
|
||||
return _LOOPBACK_BASE_URL
|
||||
|
||||
|
||||
def _make_loopback_client(request: Request) -> httpx.AsyncClient:
|
||||
"""构造一个走 ASGITransport 的 httpx.AsyncClient,**不走网络栈**。
|
||||
|
||||
所有圆桌路由(intent / recommend / multi_agent)的 loopback 调用都走这里,
|
||||
替代原先的 `httpx.AsyncClient(timeout=None, trust_env=False)` + 127.0.0.1。
|
||||
"""
|
||||
transport = httpx.ASGITransport(app=request.app)
|
||||
return httpx.AsyncClient(
|
||||
transport=transport,
|
||||
base_url=_LOOPBACK_BASE_URL,
|
||||
timeout=None,
|
||||
)
|
||||
|
||||
|
||||
def _auth_headers(request: Request) -> dict[str, str]:
|
||||
@@ -431,11 +462,9 @@ async def init_multi_agent(request: Request, payload: dict = Body(...)) -> Strea
|
||||
return f"data: {json.dumps(payload, ensure_ascii=False)}\n\n"
|
||||
|
||||
async def stream_generator() -> AsyncIterator[str]:
|
||||
# trust_env=False: same-process loopback to 127.0.0.1 — honoring
|
||||
# HTTP_PROXY / HTTPS_PROXY env vars (commonly set on dev machines)
|
||||
# causes httpx to route loopback calls through an unreachable upstream
|
||||
# proxy and wait ~40 seconds for timeout before falling back.
|
||||
async with httpx.AsyncClient(timeout=None, trust_env=False) as client:
|
||||
# ASGITransport:进程内直调本机 ASGI app,**不走 TCP / 不经过网络栈**,
|
||||
# 因此既无须 trust_env=False 防代理,也不存在 loopback 40s 超时这种坑。
|
||||
async with _make_loopback_client(request) as client:
|
||||
try:
|
||||
# Per-seat prepare task — same body as before, but we no longer
|
||||
# await them with asyncio.gather. Instead, asyncio.as_completed
|
||||
@@ -552,7 +581,7 @@ async def _stream_upstream(
|
||||
- ``(None, captured)`` — 流结束,``captured`` 为所有 ``data:`` 行文本列表,
|
||||
供 ``_parse_last_messages`` 从倒数 values 快照提取 ``messages``。
|
||||
|
||||
日志 ``[upstream-timing]``:排查首 token 慢、代理导致 loopback 超时(见 trust_env=False)。
|
||||
日志 ``[upstream-timing]``:排查首 token 慢;loopback 已走 ASGITransport,不会再有代理超时。
|
||||
"""
|
||||
|
||||
url = f"{base}/api/threads/{thread_id}/runs/stream"
|
||||
@@ -968,11 +997,9 @@ async def run_multi_agent_stream(request: Request, payload: dict = Body(...)) ->
|
||||
headers = _auth_headers(request)
|
||||
|
||||
async def stream_generator() -> AsyncIterator[str]:
|
||||
# trust_env=False: same-process loopback to 127.0.0.1 — honoring
|
||||
# HTTP_PROXY / HTTPS_PROXY env vars (commonly set on dev machines)
|
||||
# causes httpx to route loopback calls through an unreachable upstream
|
||||
# proxy and wait ~40 seconds for timeout before falling back.
|
||||
async with httpx.AsyncClient(timeout=None, trust_env=False) as client:
|
||||
# ASGITransport:进程内直调本机 ASGI app,**不走 TCP / 不经过网络栈**,
|
||||
# 因此既无须 trust_env=False 防代理,也不存在 loopback 40s 超时这种坑。
|
||||
async with _make_loopback_client(request) as client:
|
||||
try:
|
||||
if agent_type == "leader":
|
||||
gen = _leader_run(
|
||||
|
||||
@@ -52,6 +52,7 @@ from app.gateway.routers.multi_agent import (
|
||||
_create_thread,
|
||||
_flatten_content,
|
||||
_loopback_base,
|
||||
_make_loopback_client,
|
||||
_parse_last_messages,
|
||||
_run_payload,
|
||||
_stream_upstream,
|
||||
@@ -193,11 +194,9 @@ async def stream_recommend(request: Request, payload: dict = Body(...)) -> Strea
|
||||
headers = _auth_headers(request)
|
||||
|
||||
async def stream_generator() -> AsyncIterator[str]:
|
||||
# trust_env=False: same-process loopback to 127.0.0.1 — honoring
|
||||
# HTTP_PROXY / HTTPS_PROXY env vars (commonly set on dev machines)
|
||||
# causes httpx to route loopback calls through an unreachable upstream
|
||||
# proxy and wait ~40 seconds for timeout before falling back.
|
||||
async with httpx.AsyncClient(timeout=None, trust_env=False) as client:
|
||||
# ASGITransport:进程内直调本机 ASGI app,**不走 TCP / 不经过网络栈**,
|
||||
# 因此既无须 trust_env=False 防代理,也不存在 loopback 40s 超时这种坑。
|
||||
async with _make_loopback_client(request) as client:
|
||||
try:
|
||||
# 每次推荐独立 thread;prompt 已包含完整 intent + 候选列表,无需多轮。
|
||||
thread_id = await _create_thread(client, base, headers)
|
||||
|
||||
Reference in new issue
Block a user