sync update from LAN repo (source dhk-1 @ 3e87be55)

This commit is contained in:
gozeros committed 2026-10-21 15:06:08 +08:00
1 parent ab6068a7c1
commit 42f2a26fa8
1482 files changed
+227759 -9584

No files matched your search

@@ -1,3 +1,5 @@
# ruff: noqa: E402, I001
# The dotenv load below must precede application imports that read environment.
import asyncio
import logging
import os
@@ -7,11 +9,15 @@ import threading
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from datetime import datetime
from pathlib import Path
from time import perf_counter
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.docs import get_redoc_html, get_swagger_ui_html
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
# Load .env BEFORE any module that reads os.environ at import time.
# Without this, GATEWAY_CORS_ORIGINS / AUTH_JWT_SECRET / DEER_FLOW_AUTH_DISABLED
@@ -42,11 +48,16 @@ from app.gateway.routers import (
compact_menu_overrides,
dashboard_sessions,
deep_research,
direct_chat,
external_llmwiki,
feedback,
fixed_questions,
flow_extract,
force_situation,
gongwen_documents,
html_images,
html_page_favorites,
html_runtime,
intent,
knowledge,
knowledge_ingest,
@@ -90,11 +101,17 @@ from app.gateway.routers import (
sentiment_agent,
skill_addresses,
skill_knowledge,
skill_view_diagnostics,
skills,
strategy_agents,
suggestions,
system_settings,
tags,
task_analysis,
task_analysis_qa,
task_buttons,
task_deeplink_details,
task_deeplink_threads,
task_reports,
taskcop_tasks,
thread_runs,
@@ -102,8 +119,10 @@ from app.gateway.routers import (
threads,
tool_metrics,
uploads,
upload_policy,
user_preferences,
user_prompts,
web_app_sites,
workflow_data_sources,
workflow_embed,
workflow_planning,
@@ -114,6 +133,7 @@ from app.gateway.routers import (
workflows_coze_compat,
writing,
)
from app.gateway.runtime_executor import configure_runtime_executor # noqa: E402
from deerflow.config import app_config as deerflow_app_config
from deerflow.config.app_config import apply_logging_level
from deerflow.runtime.scheduler import ScheduledTaskService, set_scheduled_task_service
@@ -130,6 +150,11 @@ logging.basicConfig(
logger = logging.getLogger(__name__)
# Swagger UI and ReDoc are vendored so the API documentation works in isolated
# intranets. The route intentionally lives below /docs so it is covered by the
# existing anonymous docs allow-list in the auth middleware.
_DOCS_STATIC_DIR = Path(__file__).with_name("static") / "docs"
# Upper bound (seconds) each lifespan shutdown hook is allowed to run.
# Bounds worker exit time so uvicorn's reload supervisor does not keep
# firing signals into a worker that is stuck waiting for shutdown cleanup.
@@ -991,6 +1016,7 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
try:
app.state.config = get_app_config()
apply_logging_level(app.state.config.log_level)
await configure_runtime_executor(app.state.config.runtime_performance)
logger.info("Configuration loaded successfully")
except Exception as e:
error_msg = f"Failed to load configuration during gateway startup: {e}"
@@ -1087,6 +1113,21 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
"AI-writing functional agent seeding failed at startup (non-fatal)"
)
# Strategy-selection functional agents (intent, six independent
# analysis angles, judge, and solution editor). Seed them before
# _sync_legacy_agents so they are immediately visible and editable in
# the existing agent-management UI on a fresh deployment.
try:
from app.gateway.routers._strategy_selection_seed import (
ensure_strategy_selection_agents,
)
ensure_strategy_selection_agents()
except Exception:
logger.exception(
"Strategy-selection functional agent seeding failed at startup (non-fatal)"
)
# 内置「模板数据转换器」agent(JSON → HTML 模板填充)。同样必须在
# _sync_legacy_agents **之前**补建目录,后者把它 upsert 进 agents 表,
# 第一次启动 /api/agents 与定时任务下拉框里就有它。
@@ -1128,6 +1169,32 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
"Deep-research collector agent seeding failed at startup (non-fatal)"
)
# Built-in public-document stack: agent + workflow skill + the full
# gongwen-assistant DOCX engine skill, all version-synced into
# skills/public/ (see _gongwen_seed.py). Seed before legacy agent
# sync so it is immediately available to ordinary conversations.
try:
from app.gateway.routers._gongwen_seed import ensure_gongwen_agent
ensure_gongwen_agent()
except Exception:
logger.exception(
"Public-document agent seeding failed at startup (non-fatal)"
)
# 内置「认知态势图」public 技能:skills/ 不随 git 快照搬运,新机器
# 部署后技能缺失,由种子资产在首次启动时补建(目录存在则不动)。
try:
from app.gateway.routers._cognitive_situation_seed import (
ensure_cognitive_situation_skills,
)
ensure_cognitive_situation_skills()
except Exception:
logger.exception(
"Cognitive-situation skill seeding failed at startup (non-fatal)"
)
# Workflow Studio owns a separate planning controller. It must be
# seeded before _sync_legacy_agents, so the planner is available to
# every user's candidate-workflow request on a fresh deployment.
@@ -1156,6 +1223,18 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
"Forced-research responder agent seeding failed at startup (non-fatal)"
)
# 内置「耗时排查助手」:根据执行记录和日志判断空等原因。目录已存在则不覆盖。
try:
from app.gateway.routers._latency_diagnosis_seed import (
ensure_latency_diagnosis_agent,
)
ensure_latency_diagnosis_agent()
except Exception:
logger.exception(
"Latency-diagnosis agent seeding failed at startup (non-fatal)"
)
# 内置「任务研判报告助手」(agentfx 任务深链固定使用):同样必须在
# _sync_legacy_agents 之前补建目录 + 补建配套 task-report-import 技能
# (skills/ 被 .gitignore 排除),由后者 upsert 进 agents 表。
@@ -1168,6 +1247,18 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
"Agentfx analyst agent seeding failed at startup (non-fatal)"
)
# 内置「任务分析总控」(任务分析定时的专用总控:无人值守分解任务并派活)。
try:
from app.gateway.routers._task_analysis_seed import (
ensure_task_analysis_functional_agents,
)
ensure_task_analysis_functional_agents()
except Exception:
logger.exception(
"Task analysis coordinator agent seeding failed at startup (non-fatal)"
)
# ?????????????????3Q/6BF/7BF/8BF??agent/chain ?????
try:
business_mapping_store = getattr(app.state, "business_mapping_store", None)
@@ -1211,6 +1302,15 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
await ensure_builtin_agent_purpose_tags(agent_store, tag_store)
except Exception:
logger.exception("Built-in agent purpose-tag seeding failed at startup (non-fatal)")
# Synchronize every worker's strategy registry only after the
# folder agents have been imported. In memory mode this also
# restores dynamic/default angle lifecycle from sidecars.
try:
await strategy_agents.ensure_strategy_agent_registry(app)
logger.info("Strategy agent registry synchronized for worker")
except Exception:
logger.exception("Strategy agent role seeding failed at startup (non-fatal)")
except Exception:
logger.exception("Legacy agent sync failed at startup (non-fatal)")
@@ -1268,8 +1368,33 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
except Exception:
logger.exception("Scheduled task service failed to start")
# External-task chain-binding scheduler (任务分析定时)。
task_analysis_enabled = os.environ.get("TASK_ANALYSIS_ENABLED", "true").strip().lower()
if task_analysis_enabled in {"0", "false", "no", "off"}:
logger.info("Task analysis service disabled by TASK_ANALYSIS_ENABLED")
else:
try:
from deerflow.runtime.task_analysis import TaskAnalysisService
task_analysis_store = getattr(app.state, "task_analysis_store", None)
if task_analysis_store is not None:
ta_service = TaskAnalysisService(app, task_analysis_store)
app.state.task_analysis_service = ta_service
await ta_service.start()
logger.info("Task analysis service started")
except Exception:
logger.exception("Task analysis service failed to start")
yield
artifact_library_tasks = list(
getattr(app.state, "artifact_library_sync_tasks", {}).values()
)
for task in artifact_library_tasks:
task.cancel()
if artifact_library_tasks:
await asyncio.gather(*artifact_library_tasks, return_exceptions=True)
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()
@@ -1340,6 +1465,13 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
finally:
set_scheduled_task_service(None)
try:
ta_service = getattr(app.state, "task_analysis_service", None)
if ta_service is not None:
await ta_service.stop()
except Exception:
logger.exception("Failed to stop task analysis service")
# Stop channel service on shutdown (bounded to prevent worker hang)
try:
from app.channels.service import stop_channel_service
@@ -1366,7 +1498,12 @@ def create_app() -> FastAPI:
Configured FastAPI application instance.
"""
config = get_gateway_config()
docs_kwargs = {"docs_url": "/docs", "redoc_url": "/redoc", "openapi_url": "/openapi.json"} if config.enable_docs else {"docs_url": None, "redoc_url": None, "openapi_url": None}
logger.info("API documentation endpoints enabled: %s", config.enable_docs)
docs_kwargs = {
"docs_url": None,
"redoc_url": None,
"openapi_url": "/openapi.json",
} if config.enable_docs else {"docs_url": None, "redoc_url": None, "openapi_url": None}
app = FastAPI(
title="DeerFlow API Gateway",
@@ -1452,6 +1589,37 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
],
)
if config.enable_docs:
# Keep these paths relative to the page URL. Nginx strips the
# /deerflow deployment prefix before forwarding to this app, and
# root-relative URLs would otherwise escape that prefix in browsers.
app.mount(
"/docs/static",
StaticFiles(directory=_DOCS_STATIC_DIR),
name="docs-static",
)
@app.get("/docs", include_in_schema=False)
async def swagger_ui() -> HTMLResponse:
return get_swagger_ui_html(
openapi_url="./openapi.json",
title=f"{app.title} - Swagger UI",
swagger_js_url="./docs/static/swagger-ui-bundle.js",
swagger_css_url="./docs/static/swagger-ui.css",
swagger_favicon_url="./docs/static/favicon-32x32.png",
swagger_ui_parameters=app.swagger_ui_parameters,
)
@app.get("/redoc", include_in_schema=False)
async def redoc() -> HTMLResponse:
return get_redoc_html(
openapi_url="./openapi.json",
title=f"{app.title} - ReDoc",
redoc_js_url="./docs/static/redoc.standalone.js",
redoc_favicon_url="./docs/static/favicon-32x32.png",
with_google_fonts=False,
)
# Auth: reject unauthenticated requests to non-public paths (fail-closed safety net)
app.add_middleware(AuthMiddleware)
@@ -1590,21 +1758,32 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
from app.gateway.routers import wiki_packages
app.include_router(wiki_packages.router)
# Artifacts API is mounted at /api/threads/{thread_id}/artifacts
app.include_router(artifacts.router)
# Web App site artifacts (multi-page site / flowchart / map) with signed
# public static hosting at /api/public/site-preview/{token}/
app.include_router(web_app_sites.router)
# Uploads API is mounted at /api/threads/{thread_id}/uploads
app.include_router(uploads.router)
# Global database-backed upload limits at /api/upload-policy.
app.include_router(upload_policy.router)
# Optional Chrome-extension page capture API is mounted at /api/browser-context
app.include_router(browser_context.router)
# Thread cleanup API is mounted at /api/threads/{thread_id}
app.include_router(threads.router)
from app.gateway.routers import analysis_projects
app.include_router(analysis_projects.router)
app.include_router(threads.router)
# Dedicated taskId + goPath history lookup for single-agent task deep-link chats.
app.include_router(task_deeplink_threads.router)
app.include_router(task_deeplink_details.router)
# Agents API is mounted at /api/agents
app.include_router(agents.router)
app.include_router(strategy_agents.router)
# Recommended Questions API is mounted at /api/recommended-questions
app.include_router(recommended_questions.router)
@@ -1671,6 +1850,9 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
# Sensitive-word map (敏感词管理 / 脱敏词) at /api/sensitive-words
app.include_router(sensitive_words.router)
# Public-document workbench: durable originals, review snapshots and DOCX versions.
app.include_router(gongwen_documents.router)
# Sentiment-analysis virtual agent AG-UI SSE proxy at /api/sentiment-agent
app.include_router(sentiment_agent.router)
@@ -1698,6 +1880,9 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
# Task-scoped situation-report details used by dashboard drawing / task gates.
app.include_router(task_reports.router)
# Force-situation task detail documents (new task type; free-form JSON).
app.include_router(force_situation.router)
# Standalone parallel multi-agent panel: fan-out streaming + saved tasks
app.include_router(parallel_agents.router)
app.include_router(parallel_agent_tasks.router)
@@ -1724,6 +1909,10 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
# Scheduled task API is mounted at /api/scheduled-tasks
app.include_router(scheduled_tasks.router)
# External-task multi-agent scheduled analysis (/api/task-analysis)
app.include_router(task_analysis.router)
app.include_router(task_analysis_qa.router)
# Public, unauthenticated read-only scheduled task API
# (/api/public/scheduled-tasks/*). Lets external systems query tasks and
# surface results without a DeerFlow login.
@@ -1735,6 +1924,8 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
# Public embed Q&A status polling (/api/public/embed/*)
app.include_router(public_embed.router)
app.include_router(public_config.router)
app.include_router(html_runtime.router)
app.include_router(html_images.router)
app.include_router(html_page_favorites.router)
# Product knowledge base API is mounted at /api/knowledge
@@ -1757,6 +1948,7 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
app.include_router(admin_users.self_router)
app.include_router(admin_active_runs.router)
app.include_router(skill_view_diagnostics.router)
# Admin tool / skill call metrics audit + export
app.include_router(tool_metrics.router)
@@ -1775,6 +1967,8 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
# Feedback API is mounted at /api/threads/{thread_id}/runs/{run_id}/feedback
app.include_router(feedback.router)
# Aggregate user-feedback answer quality (not a complete capability score).
app.include_router(feedback.agent_score_router)
# Notifications API is mounted at /api/notifications
app.include_router(notifications.router)
@@ -1803,6 +1997,9 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
# 开放问答接口 /api/open/chat —— 无需登录,指定模型 + 智能体做问答,可关闭思考模式
app.include_router(open_chat.router)
# 极速问答 /api/direct-chat/stream —— 直连大模型流式输出,不走 lead agent / 工具 / 技能
app.include_router(direct_chat.router)
# 开放接口 /api/open/3qfx/ask —— 无需登录,按 taskId 直接发起 3qfx 第一次问答(后台运行,落成真实会话)
app.include_router(open_3qfx.router)