feat: 舆情分析智能体改为经 Gateway 转发外部 AG-UI 网关
前端不再直连上游 SSE,改为 POST /api/sentiment-agent/stream 由后台代理转发,支持关闭 TLS 校验并回传完整错误信息。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
5c0864bf0a
commit
62a3faa7c8
17 files changed
+398
-130
No files matched your search
@@ -36,11 +36,11 @@ from app.gateway.routers import (
|
||||
browser_context,
|
||||
business_mapping,
|
||||
channels,
|
||||
checkpoint_migration,
|
||||
compact_menu_overrides,
|
||||
external_llmwiki,
|
||||
feedback,
|
||||
fixed_questions,
|
||||
checkpoint_migration,
|
||||
compact_menu_overrides,
|
||||
external_llmwiki,
|
||||
feedback,
|
||||
fixed_questions,
|
||||
dashboard_sessions,
|
||||
deep_research,
|
||||
html_page_favorites,
|
||||
@@ -48,9 +48,9 @@ from app.gateway.routers import (
|
||||
knowledge,
|
||||
knowledge_ingest,
|
||||
light_apps,
|
||||
llm_metrics,
|
||||
llmwiki,
|
||||
llmwiki_index,
|
||||
llm_metrics,
|
||||
llmwiki,
|
||||
llmwiki_index,
|
||||
mcp,
|
||||
memory,
|
||||
menu_overrides,
|
||||
@@ -82,6 +82,7 @@ from app.gateway.routers import (
|
||||
runs,
|
||||
scheduled_tasks,
|
||||
sensitive_words,
|
||||
sentiment_agent,
|
||||
skill_addresses,
|
||||
skills,
|
||||
suggestions,
|
||||
@@ -989,23 +990,23 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
async with langgraph_runtime(app):
|
||||
logger.info("LangGraph runtime initialised")
|
||||
|
||||
# Run Alembic migrations before anything else touches the schema.
|
||||
await _run_db_migrations()
|
||||
|
||||
# Local Wiki mirror/index scans are opt-in and run outside request
|
||||
# handling. Per-library database leases keep multi-worker startup safe.
|
||||
app.state.llmwiki_index_scheduler_task = None
|
||||
local_wiki_index = app.state.config.llmwiki.local_wiki_index
|
||||
if (
|
||||
local_wiki_index.enabled
|
||||
and local_wiki_index.auto_sync
|
||||
and getattr(app.state, "llmwiki_sync_service", None) is not None
|
||||
):
|
||||
from app.gateway.llmwiki_index_scheduler import local_wiki_index_scheduler_loop
|
||||
|
||||
app.state.llmwiki_index_scheduler_task = asyncio.create_task(
|
||||
local_wiki_index_scheduler_loop(app)
|
||||
)
|
||||
# Run Alembic migrations before anything else touches the schema.
|
||||
await _run_db_migrations()
|
||||
|
||||
# Local Wiki mirror/index scans are opt-in and run outside request
|
||||
# handling. Per-library database leases keep multi-worker startup safe.
|
||||
app.state.llmwiki_index_scheduler_task = None
|
||||
local_wiki_index = app.state.config.llmwiki.local_wiki_index
|
||||
if (
|
||||
local_wiki_index.enabled
|
||||
and local_wiki_index.auto_sync
|
||||
and getattr(app.state, "llmwiki_sync_service", None) is not None
|
||||
):
|
||||
from app.gateway.llmwiki_index_scheduler import local_wiki_index_scheduler_loop
|
||||
|
||||
app.state.llmwiki_index_scheduler_task = asyncio.create_task(
|
||||
local_wiki_index_scheduler_loop(app)
|
||||
)
|
||||
|
||||
# ?? recall_budget cache?? DB ????,? get_effective_memory_config ?????
|
||||
try:
|
||||
@@ -1083,26 +1084,26 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
)
|
||||
|
||||
ensure_deep_research_collector_agent()
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Deep-research collector agent seeding failed at startup (non-fatal)"
|
||||
)
|
||||
|
||||
# 内置「强制检索输出助手」:必须在 _sync_legacy_agents 之前补建,
|
||||
# 使首次启动就能在智能体管理/广场中使用。每轮检索门禁由 harness
|
||||
# 的 ForcedResearchMiddleware 执行,而不是仅依赖 SOUL 提示词。
|
||||
try:
|
||||
from app.gateway.routers._forced_research_seed import (
|
||||
ensure_forced_research_agent,
|
||||
)
|
||||
|
||||
ensure_forced_research_agent()
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Forced-research responder agent seeding failed at startup (non-fatal)"
|
||||
)
|
||||
|
||||
# ?????????????????3Q/6BF/7BF/8BF??agent/chain ?????
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Deep-research collector agent seeding failed at startup (non-fatal)"
|
||||
)
|
||||
|
||||
# 内置「强制检索输出助手」:必须在 _sync_legacy_agents 之前补建,
|
||||
# 使首次启动就能在智能体管理/广场中使用。每轮检索门禁由 harness
|
||||
# 的 ForcedResearchMiddleware 执行,而不是仅依赖 SOUL 提示词。
|
||||
try:
|
||||
from app.gateway.routers._forced_research_seed import (
|
||||
ensure_forced_research_agent,
|
||||
)
|
||||
|
||||
ensure_forced_research_agent()
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Forced-research responder agent seeding failed at startup (non-fatal)"
|
||||
)
|
||||
|
||||
# ?????????????????3Q/6BF/7BF/8BF??agent/chain ?????
|
||||
try:
|
||||
business_mapping_store = getattr(app.state, "business_mapping_store", None)
|
||||
if business_mapping_store is not None:
|
||||
@@ -1202,22 +1203,22 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
except Exception:
|
||||
logger.exception("Scheduled task service failed to start")
|
||||
|
||||
yield
|
||||
|
||||
llmwiki_index_scheduler_task = getattr(app.state, "llmwiki_index_scheduler_task", None)
|
||||
if llmwiki_index_scheduler_task is not None:
|
||||
llmwiki_index_scheduler_task.cancel()
|
||||
try:
|
||||
await llmwiki_index_scheduler_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
except Exception:
|
||||
logger.exception("Local Wiki index scheduler shutdown error")
|
||||
llmwiki_index_jobs = list(getattr(app.state, "llmwiki_index_jobs", set()))
|
||||
for job in llmwiki_index_jobs:
|
||||
job.cancel()
|
||||
if llmwiki_index_jobs:
|
||||
await asyncio.gather(*llmwiki_index_jobs, return_exceptions=True)
|
||||
yield
|
||||
|
||||
llmwiki_index_scheduler_task = getattr(app.state, "llmwiki_index_scheduler_task", None)
|
||||
if llmwiki_index_scheduler_task is not None:
|
||||
llmwiki_index_scheduler_task.cancel()
|
||||
try:
|
||||
await llmwiki_index_scheduler_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
except Exception:
|
||||
logger.exception("Local Wiki index scheduler shutdown error")
|
||||
llmwiki_index_jobs = list(getattr(app.state, "llmwiki_index_jobs", set()))
|
||||
for job in llmwiki_index_jobs:
|
||||
job.cancel()
|
||||
if llmwiki_index_jobs:
|
||||
await asyncio.gather(*llmwiki_index_jobs, return_exceptions=True)
|
||||
|
||||
# ?? curator ???
|
||||
curator_task = getattr(app.state, "curator_scheduler_task", None)
|
||||
@@ -1502,10 +1503,10 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
|
||||
# MCP API is mounted at /api/mcp
|
||||
app.include_router(mcp.router)
|
||||
|
||||
# DeerFlow-native LLMWiki management/search BFF (conditionally backed by WeKnora).
|
||||
app.include_router(llmwiki.router)
|
||||
app.include_router(llmwiki_index.router)
|
||||
app.include_router(external_llmwiki.router)
|
||||
# DeerFlow-native LLMWiki management/search BFF (conditionally backed by WeKnora).
|
||||
app.include_router(llmwiki.router)
|
||||
app.include_router(llmwiki_index.router)
|
||||
app.include_router(external_llmwiki.router)
|
||||
|
||||
# Memory API is mounted at /api/memory
|
||||
app.include_router(memory.router)
|
||||
@@ -1580,17 +1581,20 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
|
||||
# Sensitive-word map (敏感词管理 / 脱敏词) at /api/sensitive-words
|
||||
app.include_router(sensitive_words.router)
|
||||
|
||||
# Sentiment-analysis virtual agent AG-UI SSE proxy at /api/sentiment-agent
|
||||
app.include_router(sentiment_agent.router)
|
||||
|
||||
# Skill URL/IP batch replace (技能地址管理) at /api/skill-addresses
|
||||
app.include_router(skill_addresses.router)
|
||||
|
||||
# 大屏绘制智能体独立页面会话(按 taskId 存取,不分权)at /api/dashboard-sessions
|
||||
app.include_router(dashboard_sessions.router)
|
||||
|
||||
# Task-workspace configurable jump buttons (按钮管理) at /api/task-buttons
|
||||
app.include_router(task_buttons.router)
|
||||
|
||||
# Admin exact-match question/answer short-circuit at /api/fixed-questions
|
||||
app.include_router(fixed_questions.router)
|
||||
# Task-workspace configurable jump buttons (按钮管理) at /api/task-buttons
|
||||
app.include_router(task_buttons.router)
|
||||
|
||||
# Admin exact-match question/answer short-circuit at /api/fixed-questions
|
||||
app.include_router(fixed_questions.router)
|
||||
|
||||
# Deep-research report structures (工作流配置 / 报告结构) at /api/report-structures
|
||||
app.include_router(report_structures.router)
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
"""舆情分析虚拟智能体的 AG-UI SSE 反向代理。
|
||||
|
||||
前端对话页不直连外部网关(避免浏览器 CORS / 混合内容),改为把
|
||||
``runtime-config.js`` 里的 ``apiUrl`` / Basic Auth 随 AG-UI 请求体传给本路由。
|
||||
Gateway 关闭 TLS 校验后按传入地址转发上游 SSE,字节级透传给前端。
|
||||
连接失败或上游非 2xx 时,把完整错误信息放进 HTTP ``detail``(流已开始则发
|
||||
AG-UI ``RUN_ERROR`` 帧),前端原样展示。
|
||||
|
||||
Routes (prefix ``/api/sentiment-agent``):
|
||||
POST /stream 透传上游 ``text/event-stream``
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
import logging
|
||||
import ssl
|
||||
from collections.abc import AsyncIterator, Mapping
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from deerflow.config import get_app_config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/sentiment-agent", tags=["sentiment-agent"])
|
||||
|
||||
_MAX_ERROR_BODY_CHARS = 32_768
|
||||
|
||||
|
||||
class SentimentAgentStreamRequest(BaseModel):
|
||||
"""前端传入的 AG-UI 体 + runtime-config 上游地址/凭据。"""
|
||||
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
apiUrl: str = Field(default="", min_length=0, max_length=2048)
|
||||
authUsername: str = Field(default="", max_length=512)
|
||||
authPassword: str = Field(default="", max_length=512)
|
||||
threadId: str = Field(..., min_length=1, max_length=256)
|
||||
runId: str = Field(..., min_length=1, max_length=256)
|
||||
state: dict[str, Any] = Field(default_factory=dict)
|
||||
tools: list[Any] = Field(default_factory=list)
|
||||
context: list[Any] = Field(default_factory=list)
|
||||
forwardedProps: dict[str, Any] = Field(default_factory=dict)
|
||||
messages: list[Any] = Field(default_factory=list)
|
||||
|
||||
|
||||
def _unverified_ssl_context() -> ssl.SSLContext:
|
||||
"""Do not validate the upstream certificate (intranet / self-signed hosts)."""
|
||||
|
||||
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
||||
ctx.check_hostname = False
|
||||
ctx.verify_mode = ssl.CERT_NONE
|
||||
return ctx
|
||||
|
||||
|
||||
def _build_http_client(timeout_seconds: float) -> httpx.AsyncClient:
|
||||
"""Upstream client: skip TLS cert checks, ignore env proxy, no redirects."""
|
||||
|
||||
return httpx.AsyncClient(
|
||||
verify=_unverified_ssl_context(),
|
||||
trust_env=False,
|
||||
follow_redirects=False,
|
||||
timeout=httpx.Timeout(timeout_seconds, connect=30.0),
|
||||
)
|
||||
|
||||
|
||||
def _format_exception(exc: BaseException) -> str:
|
||||
parts = [f"{type(exc).__name__}: {exc}"]
|
||||
seen = {id(exc)}
|
||||
cause: BaseException | None = exc.__cause__ or exc.__context__
|
||||
while cause is not None and id(cause) not in seen:
|
||||
seen.add(id(cause))
|
||||
parts.append(f"Caused by {type(cause).__name__}: {cause}")
|
||||
cause = cause.__cause__ or cause.__context__
|
||||
return " | ".join(parts)
|
||||
|
||||
|
||||
def _decode_body(content: bytes | None) -> str:
|
||||
if not content:
|
||||
return ""
|
||||
text = content.decode("utf-8", errors="replace").strip()
|
||||
if len(text) > _MAX_ERROR_BODY_CHARS:
|
||||
return text[:_MAX_ERROR_BODY_CHARS] + f"\n…(truncated, total {len(text)} chars)"
|
||||
return text
|
||||
|
||||
|
||||
def format_upstream_error(
|
||||
*,
|
||||
kind: str,
|
||||
url: str,
|
||||
status: int | None = None,
|
||||
headers: Mapping[str, str] | None = None,
|
||||
body: str = "",
|
||||
exc: BaseException | None = None,
|
||||
) -> str:
|
||||
"""Assemble a complete, user-visible upstream failure string."""
|
||||
|
||||
lines = [f"舆情分析上游调用失败({kind})", f"url={url}"]
|
||||
if status is not None:
|
||||
lines.append(f"http_status={status}")
|
||||
if headers:
|
||||
location = headers.get("location") or headers.get("Location")
|
||||
content_type = headers.get("content-type") or headers.get("Content-Type")
|
||||
if location:
|
||||
lines.append(f"location={location}")
|
||||
if content_type:
|
||||
lines.append(f"content_type={content_type}")
|
||||
if body:
|
||||
lines.append(f"response={body}")
|
||||
if exc is not None:
|
||||
lines.append(_format_exception(exc))
|
||||
request_obj = getattr(exc, "request", None)
|
||||
method = getattr(request_obj, "method", None)
|
||||
url_obj = getattr(request_obj, "url", None)
|
||||
if method and url_obj is not None:
|
||||
lines.append(f"request={method} {url_obj}")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _upstream_headers(username: str, password: str) -> dict[str, str]:
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "text/event-stream",
|
||||
}
|
||||
if username and password:
|
||||
token = base64.b64encode(f"{username}:{password}".encode()).decode("ascii")
|
||||
headers["Authorization"] = f"Basic {token}"
|
||||
return headers
|
||||
|
||||
|
||||
def _agui_payload(body: SentimentAgentStreamRequest) -> dict[str, Any]:
|
||||
return {
|
||||
"threadId": body.threadId,
|
||||
"runId": body.runId,
|
||||
"state": body.state,
|
||||
"tools": body.tools,
|
||||
"context": body.context,
|
||||
"forwardedProps": body.forwardedProps,
|
||||
"messages": body.messages,
|
||||
}
|
||||
|
||||
|
||||
def _run_error_frame(message: str) -> bytes:
|
||||
payload = json.dumps({"type": "RUN_ERROR", "message": message}, ensure_ascii=False)
|
||||
return f"data: {payload}\n\n".encode()
|
||||
|
||||
|
||||
@router.post("/stream")
|
||||
async def stream_sentiment_agent(
|
||||
request: Request,
|
||||
body: SentimentAgentStreamRequest,
|
||||
) -> StreamingResponse:
|
||||
api_url = (body.apiUrl or "").strip()
|
||||
if not api_url:
|
||||
raise HTTPException(status_code=400, detail="缺少上游地址 apiUrl(runtime-config.js 的 VITE_SENTIMENT_AGENT_API_URL)。")
|
||||
|
||||
config = get_app_config().sentiment_agent
|
||||
|
||||
payload = _agui_payload(body)
|
||||
headers = _upstream_headers(body.authUsername, body.authPassword)
|
||||
client = _build_http_client(config.timeout_seconds)
|
||||
upstream: httpx.Response | None = None
|
||||
try:
|
||||
upstream = await client.send(
|
||||
client.build_request("POST", api_url, headers=headers, json=payload),
|
||||
stream=True,
|
||||
)
|
||||
except httpx.RequestError as exc:
|
||||
await client.aclose()
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail=format_upstream_error(kind="network", url=api_url, exc=exc),
|
||||
) from exc
|
||||
|
||||
if upstream.status_code >= 400:
|
||||
err_body = _decode_body(await upstream.aread())
|
||||
err_headers = dict(upstream.headers)
|
||||
status = upstream.status_code
|
||||
await upstream.aclose()
|
||||
await client.aclose()
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail=format_upstream_error(
|
||||
kind="http",
|
||||
url=api_url,
|
||||
status=status,
|
||||
headers=err_headers,
|
||||
body=err_body,
|
||||
),
|
||||
)
|
||||
|
||||
async def pump() -> AsyncIterator[bytes]:
|
||||
assert upstream is not None
|
||||
try:
|
||||
async for chunk in upstream.aiter_bytes():
|
||||
if await request.is_disconnected():
|
||||
break
|
||||
if chunk:
|
||||
yield chunk
|
||||
except httpx.RequestError as exc:
|
||||
yield _run_error_frame(format_upstream_error(kind="stream", url=api_url, exc=exc))
|
||||
finally:
|
||||
await upstream.aclose()
|
||||
await client.aclose()
|
||||
|
||||
return StreamingResponse(
|
||||
pump(),
|
||||
media_type="text/event-stream",
|
||||
headers={
|
||||
"Cache-Control": "no-cache",
|
||||
"Connection": "keep-alive",
|
||||
"X-Accel-Buffering": "no",
|
||||
},
|
||||
)
|
||||
Reference in new issue
Block a user