mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-06 14:22:47 +02:00
refactor(multi-agent): add main-agent observability and lifecycle middleware factories
This commit is contained in:
parent
67036448f9
commit
6a4dacda72
6 changed files with 125 additions and 0 deletions
|
|
@ -0,0 +1,36 @@
|
||||||
|
"""Audit row per tool call (reversibility metadata)."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from app.agents.new_chat.feature_flags import AgentFeatureFlags
|
||||||
|
from app.agents.new_chat.middleware import ActionLogMiddleware
|
||||||
|
from app.agents.new_chat.tools.registry import BUILTIN_TOOLS
|
||||||
|
|
||||||
|
from ..shared.flags import enabled
|
||||||
|
|
||||||
|
|
||||||
|
def build_action_log_mw(
|
||||||
|
*,
|
||||||
|
flags: AgentFeatureFlags,
|
||||||
|
thread_id: int | None,
|
||||||
|
search_space_id: int,
|
||||||
|
user_id: str | None,
|
||||||
|
) -> ActionLogMiddleware | None:
|
||||||
|
if not enabled(flags, "enable_action_log") or thread_id is None:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
tool_defs_by_name = {td.name: td for td in BUILTIN_TOOLS}
|
||||||
|
return ActionLogMiddleware(
|
||||||
|
thread_id=thread_id,
|
||||||
|
search_space_id=search_space_id,
|
||||||
|
user_id=user_id,
|
||||||
|
tool_definitions=tool_defs_by_name,
|
||||||
|
)
|
||||||
|
except Exception: # pragma: no cover - defensive
|
||||||
|
logging.warning(
|
||||||
|
"ActionLogMiddleware init failed; running without it.",
|
||||||
|
exc_info=True,
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
"""Anonymous document hydration from Redis (cloud only)."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from app.agents.new_chat.filesystem_selection import FilesystemMode
|
||||||
|
from app.agents.new_chat.middleware import AnonymousDocumentMiddleware
|
||||||
|
|
||||||
|
|
||||||
|
def build_anonymous_doc_mw(
|
||||||
|
*,
|
||||||
|
filesystem_mode: FilesystemMode,
|
||||||
|
anon_session_id: str | None,
|
||||||
|
) -> AnonymousDocumentMiddleware | None:
|
||||||
|
if filesystem_mode != FilesystemMode.CLOUD:
|
||||||
|
return None
|
||||||
|
return AnonymousDocumentMiddleware(anon_session_id=anon_session_id)
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
"""Per-thread cooperative lock around the whole turn."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from app.agents.new_chat.feature_flags import AgentFeatureFlags
|
||||||
|
from app.agents.new_chat.middleware import BusyMutexMiddleware
|
||||||
|
|
||||||
|
from ..shared.flags import enabled
|
||||||
|
|
||||||
|
|
||||||
|
def build_busy_mutex_mw(flags: AgentFeatureFlags) -> BusyMutexMiddleware | None:
|
||||||
|
return BusyMutexMiddleware() if enabled(flags, "enable_busy_mutex") else None
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
"""OTel spans on model and tool calls."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from app.agents.new_chat.feature_flags import AgentFeatureFlags
|
||||||
|
from app.agents.new_chat.middleware import OtelSpanMiddleware
|
||||||
|
|
||||||
|
from ..shared.flags import enabled
|
||||||
|
|
||||||
|
|
||||||
|
def build_otel_mw(flags: AgentFeatureFlags) -> OtelSpanMiddleware | None:
|
||||||
|
return OtelSpanMiddleware() if enabled(flags, "enable_otel") else None
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
"""Tail-of-stack plugin slot driven by env allowlist."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from langchain_core.language_models import BaseChatModel
|
||||||
|
|
||||||
|
from app.agents.new_chat.feature_flags import AgentFeatureFlags
|
||||||
|
from app.agents.new_chat.plugin_loader import (
|
||||||
|
PluginContext,
|
||||||
|
load_allowed_plugin_names_from_env,
|
||||||
|
load_plugin_middlewares,
|
||||||
|
)
|
||||||
|
from app.db import ChatVisibility
|
||||||
|
|
||||||
|
from ..shared.flags import enabled
|
||||||
|
|
||||||
|
|
||||||
|
def build_plugin_middlewares(
|
||||||
|
*,
|
||||||
|
flags: AgentFeatureFlags,
|
||||||
|
search_space_id: int,
|
||||||
|
user_id: str | None,
|
||||||
|
visibility: ChatVisibility,
|
||||||
|
llm: BaseChatModel,
|
||||||
|
) -> list[Any]:
|
||||||
|
if not enabled(flags, "enable_plugin_loader"):
|
||||||
|
return []
|
||||||
|
try:
|
||||||
|
allowed_names = load_allowed_plugin_names_from_env()
|
||||||
|
if not allowed_names:
|
||||||
|
return []
|
||||||
|
return load_plugin_middlewares(
|
||||||
|
PluginContext.build(
|
||||||
|
search_space_id=search_space_id,
|
||||||
|
user_id=user_id,
|
||||||
|
thread_visibility=visibility,
|
||||||
|
llm=llm,
|
||||||
|
),
|
||||||
|
allowed_plugin_names=allowed_names,
|
||||||
|
)
|
||||||
|
except Exception: # pragma: no cover - defensive
|
||||||
|
logging.warning(
|
||||||
|
"Plugin loader failed; continuing without plugins.",
|
||||||
|
exc_info=True,
|
||||||
|
)
|
||||||
|
return []
|
||||||
Loading…
Add table
Add a link
Reference in a new issue