|
| 1 | +"""Synchronous graph compile (middleware + ``create_agent``).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections.abc import Sequence |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from deepagents import __version__ as deepagents_version |
| 9 | +from langchain.agents import create_agent |
| 10 | +from langchain_core.language_models import BaseChatModel |
| 11 | +from langchain_core.tools import BaseTool |
| 12 | +from langgraph.types import Checkpointer |
| 13 | + |
| 14 | +from app.agents.multi_agent_chat.middleware import ( |
| 15 | + build_main_agent_deepagent_middleware, |
| 16 | +) |
| 17 | +from app.agents.multi_agent_chat.subagents.shared.permissions import ( |
| 18 | + ToolsPermissions, |
| 19 | +) |
| 20 | +from app.agents.new_chat.context import SurfSenseContextSchema |
| 21 | +from app.agents.new_chat.feature_flags import AgentFeatureFlags |
| 22 | +from app.agents.new_chat.filesystem_selection import FilesystemMode |
| 23 | +from app.db import ChatVisibility |
| 24 | + |
| 25 | + |
| 26 | +def build_compiled_agent_graph_sync( |
| 27 | + *, |
| 28 | + llm: BaseChatModel, |
| 29 | + tools: Sequence[BaseTool], |
| 30 | + final_system_prompt: str, |
| 31 | + backend_resolver: Any, |
| 32 | + filesystem_mode: FilesystemMode, |
| 33 | + search_space_id: int, |
| 34 | + user_id: str | None, |
| 35 | + thread_id: int | None, |
| 36 | + visibility: ChatVisibility, |
| 37 | + anon_session_id: str | None, |
| 38 | + available_connectors: list[str] | None, |
| 39 | + available_document_types: list[str] | None, |
| 40 | + mentioned_document_ids: list[int] | None, |
| 41 | + max_input_tokens: int | None, |
| 42 | + flags: AgentFeatureFlags, |
| 43 | + checkpointer: Checkpointer, |
| 44 | + subagent_dependencies: dict[str, Any], |
| 45 | + mcp_tools_by_agent: dict[str, ToolsPermissions] | None = None, |
| 46 | + disabled_tools: list[str] | None = None, |
| 47 | +): |
| 48 | + """Sync compile: middleware + ``create_agent`` (run via ``asyncio.to_thread``).""" |
| 49 | + main_agent_middleware = build_main_agent_deepagent_middleware( |
| 50 | + llm=llm, |
| 51 | + tools=tools, |
| 52 | + backend_resolver=backend_resolver, |
| 53 | + filesystem_mode=filesystem_mode, |
| 54 | + search_space_id=search_space_id, |
| 55 | + user_id=user_id, |
| 56 | + thread_id=thread_id, |
| 57 | + visibility=visibility, |
| 58 | + anon_session_id=anon_session_id, |
| 59 | + available_connectors=available_connectors, |
| 60 | + available_document_types=available_document_types, |
| 61 | + mentioned_document_ids=mentioned_document_ids, |
| 62 | + max_input_tokens=max_input_tokens, |
| 63 | + flags=flags, |
| 64 | + subagent_dependencies=subagent_dependencies, |
| 65 | + checkpointer=checkpointer, |
| 66 | + mcp_tools_by_agent=mcp_tools_by_agent, |
| 67 | + disabled_tools=disabled_tools, |
| 68 | + ) |
| 69 | + |
| 70 | + agent = create_agent( |
| 71 | + llm, |
| 72 | + system_prompt=final_system_prompt, |
| 73 | + tools=list(tools), |
| 74 | + middleware=main_agent_middleware, |
| 75 | + context_schema=SurfSenseContextSchema, |
| 76 | + checkpointer=checkpointer, |
| 77 | + ) |
| 78 | + return agent.with_config( |
| 79 | + { |
| 80 | + "recursion_limit": 10_000, |
| 81 | + "metadata": { |
| 82 | + "ls_integration": "deepagents", |
| 83 | + "versions": {"deepagents": deepagents_version}, |
| 84 | + }, |
| 85 | + } |
| 86 | + ) |
0 commit comments