sync update from LAN repo (source dhk-1 @ 3b1ec5b4)
This commit is contained in:
1 parent
da6521b6cb
commit
264e2200d6
995 files changed
+148105
-13399
No files matched your search
@@ -78,15 +78,14 @@ async def _enter_runtime_backend_or_fallback[BackendT](
|
||||
return await stack.enter_async_context(context_manager)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"%s initialization failed; continuing Gateway startup with an "
|
||||
"in-memory fallback",
|
||||
"%s initialization failed; continuing Gateway startup with an in-memory fallback",
|
||||
label,
|
||||
)
|
||||
return fallback_factory()
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
async def langgraph_runtime(app: FastAPI, *, background: bool = True) -> AsyncGenerator[None, None]:
|
||||
"""Bootstrap and tear down all LangGraph runtime singletons.
|
||||
|
||||
Usage in ``app.py``::
|
||||
@@ -190,6 +189,10 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
|
||||
app.state.tool_metrics_store = make_tool_metrics_store(sf)
|
||||
|
||||
from deerflow.persistence.stream_truncation import make_stream_truncation_store
|
||||
|
||||
app.state.stream_truncation_store = make_stream_truncation_store(sf)
|
||||
|
||||
from deerflow.persistence.agents import make_agent_store
|
||||
|
||||
app.state.agent_store = make_agent_store(sf)
|
||||
@@ -255,7 +258,7 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
|
||||
app.state.ai_writing_job_executor = AIWritingJobExecutor(app)
|
||||
# 进程重启续跑:把仍标记为 background 的会话重新拉起驱动(best-effort,不阻塞启动)。
|
||||
if app.state.ai_writing_session_store is not None:
|
||||
if background and app.state.ai_writing_session_store is not None:
|
||||
from app.gateway.ai_writing_job_executor import STATUS_BACKGROUND
|
||||
|
||||
async def _reconcile_ai_writing_background() -> None:
|
||||
@@ -346,9 +349,7 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
try:
|
||||
await app.state.position_role_store.ensure_seed(POSITION_ROLE_SEED)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Position-role seeding failed at startup; continuing without seed refresh"
|
||||
)
|
||||
logger.exception("Position-role seeding failed at startup; continuing without seed refresh")
|
||||
|
||||
from deerflow.persistence.business_mapping import make_business_mapping_store
|
||||
|
||||
@@ -384,7 +385,7 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
app.state.report_collaboration_executor = None
|
||||
app.state.report_collaboration_dispatcher = None
|
||||
rc_cfg = getattr(config, "report_collaboration", None)
|
||||
if rc_cfg is not None and rc_cfg.enabled and rc_cfg.worker_enabled:
|
||||
if background and rc_cfg is not None and rc_cfg.enabled and rc_cfg.worker_enabled:
|
||||
from app.report_collaboration.agentscope_runtime.member_kernel import AgentScopeMemberKernel
|
||||
from app.report_collaboration.execution.dispatcher import ReportCollaborationDispatcher
|
||||
from app.report_collaboration.execution.executor import ReportCollaborationExecutor
|
||||
@@ -422,22 +423,40 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
|
||||
app.state.task_button_store = make_task_button_store(sf)
|
||||
|
||||
from deerflow.persistence.model_capabilities import (
|
||||
import_legacy_reasoning_effort_capabilities,
|
||||
make_model_capability_store,
|
||||
)
|
||||
|
||||
app.state.model_capability_store = make_model_capability_store(sf)
|
||||
imported_model_capabilities = await import_legacy_reasoning_effort_capabilities(
|
||||
app.state.model_capability_store,
|
||||
config.models,
|
||||
)
|
||||
if imported_model_capabilities:
|
||||
logger.info(
|
||||
"Imported legacy reasoning-effort capability for %d model(s)",
|
||||
len(imported_model_capabilities),
|
||||
)
|
||||
|
||||
from deerflow.persistence.upload_policy import make_upload_policy_store
|
||||
from deerflow.uploads.policy import default_upload_policy, set_runtime_upload_policy
|
||||
|
||||
app.state.upload_policy_store = make_upload_policy_store(sf)
|
||||
fallback_upload_policy = default_upload_policy()
|
||||
try:
|
||||
upload_policy_row = await app.state.upload_policy_store.ensure_seed(
|
||||
fallback_upload_policy
|
||||
)
|
||||
upload_policy_row = await app.state.upload_policy_store.ensure_seed(fallback_upload_policy)
|
||||
set_runtime_upload_policy(upload_policy_row["policy"])
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Upload-policy seeding failed at startup; using process-local defaults"
|
||||
)
|
||||
logger.exception("Upload-policy seeding failed at startup; using process-local defaults")
|
||||
set_runtime_upload_policy(fallback_upload_policy)
|
||||
|
||||
from deerflow.persistence.knowledge_retrieval_settings import (
|
||||
make_knowledge_retrieval_settings_store,
|
||||
)
|
||||
|
||||
app.state.knowledge_retrieval_settings_store = make_knowledge_retrieval_settings_store(sf)
|
||||
|
||||
from deerflow.persistence.task_analysis import make_task_analysis_store
|
||||
|
||||
app.state.task_analysis_store = make_task_analysis_store(sf)
|
||||
@@ -446,6 +465,12 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
|
||||
app.state.analysis_project_store = AnalysisProjectStore(sf)
|
||||
|
||||
from deerflow.persistence.knowledge_research import (
|
||||
make_knowledge_research_store,
|
||||
)
|
||||
|
||||
app.state.knowledge_research_store = make_knowledge_research_store(sf)
|
||||
|
||||
from app.gateway.analysis_project_memory import (
|
||||
AnalysisProjectMemoryDispatcher,
|
||||
AnalysisProjectMemoryExecutor,
|
||||
@@ -481,7 +506,7 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
app.state.assistant_knowledge_store = make_assistant_knowledge_store(sf)
|
||||
register_assistant_knowledge_store(app.state.assistant_knowledge_store)
|
||||
app.state.skill_knowledge_store = make_skill_knowledge_store(sf)
|
||||
if app.state.skill_knowledge_store is not None and app.state.assistant_knowledge_store is not None:
|
||||
if background and app.state.skill_knowledge_store is not None and app.state.assistant_knowledge_store is not None:
|
||||
from app.gateway.skill_knowledge_job_dispatcher import SkillKnowledgeJobDispatcher
|
||||
from app.gateway.skill_knowledge_job_executor import SkillKnowledgeJobExecutor
|
||||
|
||||
@@ -524,7 +549,8 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
# Start only after the optional embedding runtime is ready. Otherwise
|
||||
# persisted jobs reclaimed during startup would be permanently stored
|
||||
# as lexical-only even when phase-two vector recall is configured.
|
||||
app.state.analysis_project_memory_dispatcher.start()
|
||||
if background:
|
||||
app.state.analysis_project_memory_dispatcher.start()
|
||||
|
||||
from deerflow.persistence.taskcop_tasks import make_taskcop_task_store
|
||||
|
||||
@@ -555,6 +581,10 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
)
|
||||
|
||||
app.state.roundtable_job_store = make_roundtable_job_store(sf)
|
||||
|
||||
from deerflow.persistence.roundtable_rooms import make_roundtable_room_store
|
||||
|
||||
app.state.roundtable_room_store = make_roundtable_room_store(sf)
|
||||
# 让 LangGraph Server 路径下的圆桌编排图节点能跨 app/harness 边界拿到 repo
|
||||
# (Phase 2 后台编排图写进度用)。
|
||||
_register_roundtable_job_default_store(app.state.roundtable_job_store)
|
||||
@@ -639,7 +669,8 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
draft_store=getattr(app.state, "roundtable_draft_store", None),
|
||||
task_draft_store=getattr(app.state, "roundtable_task_draft_store", None),
|
||||
)
|
||||
app.state.roundtable_job_dispatcher.start()
|
||||
if background:
|
||||
app.state.roundtable_job_dispatcher.start()
|
||||
else:
|
||||
app.state.roundtable_job_executor = None
|
||||
app.state.roundtable_job_dispatcher = None
|
||||
@@ -707,7 +738,8 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
app.state.deep_research_job_store,
|
||||
app.state.deep_research_executor,
|
||||
)
|
||||
app.state.deep_research_dispatcher.start()
|
||||
if background:
|
||||
app.state.deep_research_dispatcher.start()
|
||||
else:
|
||||
app.state.deep_research_executor = None
|
||||
app.state.deep_research_dispatcher = None
|
||||
@@ -738,7 +770,8 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
live_publisher=app.state.workflow_live_hub.publish,
|
||||
config=workflow_cfg,
|
||||
)
|
||||
app.state.workflow_dispatcher.start()
|
||||
if background:
|
||||
app.state.workflow_dispatcher.start()
|
||||
from app.gateway.workflow_retention import WorkflowRetentionCleaner
|
||||
|
||||
app.state.workflow_retention = WorkflowRetentionCleaner(
|
||||
@@ -746,7 +779,8 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
app.state.workflow_event_store,
|
||||
config=workflow_cfg,
|
||||
)
|
||||
app.state.workflow_retention.start()
|
||||
if background:
|
||||
app.state.workflow_retention.start()
|
||||
else:
|
||||
app.state.workflow_live_hub = None
|
||||
app.state.workflow_executor = None
|
||||
@@ -759,6 +793,8 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
|
||||
# RunManager with store backing for persistence
|
||||
app.state.run_manager = RunManager(store=app.state.run_store)
|
||||
retention = config.runtime_contention
|
||||
app.state.run_manager.start_retention(retention_seconds=retention.completed_run_retention_seconds, interval_seconds=retention.run_sweep_seconds)
|
||||
from deerflow.runtime import make_redis_concurrency_gate
|
||||
|
||||
app.state.concurrency_gate = make_redis_concurrency_gate(config)
|
||||
@@ -766,12 +802,11 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
await app.state.run_manager.aclose()
|
||||
# Project-memory extraction can use the shared local Wiki encoder.
|
||||
# Stop its jobs before retiring that runtime to avoid an embedding
|
||||
# call racing shutdown.
|
||||
analysis_project_memory_dispatcher = getattr(
|
||||
app.state, "analysis_project_memory_dispatcher", None
|
||||
)
|
||||
analysis_project_memory_dispatcher = getattr(app.state, "analysis_project_memory_dispatcher", None)
|
||||
if analysis_project_memory_dispatcher is not None:
|
||||
await analysis_project_memory_dispatcher.stop()
|
||||
from app.gateway.assistant_file_ingest import stop_file_jobs
|
||||
@@ -905,6 +940,11 @@ def get_tool_metrics_store(request: Request):
|
||||
return getattr(request.app.state, "tool_metrics_store", None)
|
||||
|
||||
|
||||
def get_stream_truncation_store(request: Request):
|
||||
"""Return the stream-truncation event store, or None when not configured."""
|
||||
return getattr(request.app.state, "stream_truncation_store", None)
|
||||
|
||||
|
||||
def get_agent_store(request: Request) -> AgentStore:
|
||||
"""Return the custom-agent store."""
|
||||
val = getattr(request.app.state, "agent_store", None)
|
||||
@@ -1097,6 +1137,7 @@ async def get_current_user_from_request(request: Request):
|
||||
"""
|
||||
from app.gateway.auth import decode_token
|
||||
from app.gateway.auth.errors import AuthErrorCode, AuthErrorResponse, TokenError, token_error_to_code
|
||||
from app.gateway.auth.session_policy import session_policy_error
|
||||
|
||||
access_token = get_request_access_token(request)
|
||||
if not access_token:
|
||||
@@ -1112,6 +1153,15 @@ async def get_current_user_from_request(request: Request):
|
||||
detail=AuthErrorResponse(code=token_error_to_code(payload), message=f"Token error: {payload.value}").model_dump(),
|
||||
)
|
||||
|
||||
# Scoped service tokens are authorized by AuthMiddleware's strict path
|
||||
# allow-list. They are intentionally not accepted by the separate
|
||||
# LangGraph auth path, which still applies session_policy_error directly.
|
||||
if payload.auth_method != "service" and (policy_error := session_policy_error(payload)):
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail=AuthErrorResponse(code=AuthErrorCode.TOKEN_INVALID, message=policy_error).model_dump(),
|
||||
)
|
||||
|
||||
provider = get_local_provider()
|
||||
user = await provider.get_user(payload.sub)
|
||||
if user is None:
|
||||
@@ -1124,7 +1174,7 @@ async def get_current_user_from_request(request: Request):
|
||||
if user.token_version != payload.ver:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail=AuthErrorResponse(code=AuthErrorCode.TOKEN_INVALID, message="Token revoked (password changed)").model_dump(),
|
||||
detail=AuthErrorResponse(code=AuthErrorCode.TOKEN_INVALID, message="登录已失效:密码已更改或账号已在其他位置登录,请重新登录").model_dump(),
|
||||
)
|
||||
|
||||
return user
|
||||
|
||||
Reference in new issue
Block a user