|
| 1 | +"""Deep agent builder. |
| 2 | +
|
| 3 | +Thin UiPath wrapper around the `deepagents` library. The deepagents dependency |
| 4 | +is optional — install with one of: |
| 5 | +
|
| 6 | + pip install 'uipath-langchain[deep]' |
| 7 | + uv add 'uipath-langchain[deep]' |
| 8 | +""" |
| 9 | + |
| 10 | +from collections.abc import Callable, Sequence |
| 11 | +from typing import TYPE_CHECKING, Any |
| 12 | + |
| 13 | +from langchain.agents.structured_output import ResponseFormat |
| 14 | +from langchain_core.language_models import BaseChatModel |
| 15 | +from langchain_core.messages import HumanMessage |
| 16 | +from langchain_core.tools import BaseTool |
| 17 | +from langgraph.graph import END, START |
| 18 | +from langgraph.graph.state import CompiledStateGraph, StateGraph |
| 19 | +from pydantic import BaseModel |
| 20 | + |
| 21 | +from .types import DeepAgentGraphState |
| 22 | +from .utils import create_state_with_input |
| 23 | + |
| 24 | +if TYPE_CHECKING: |
| 25 | + from deepagents import CompiledSubAgent, SubAgent |
| 26 | + from deepagents.backends import BackendProtocol |
| 27 | + from deepagents.backends.protocol import BackendFactory |
| 28 | + |
| 29 | + |
| 30 | +_INSTALL_HINT = ( |
| 31 | + "deepagents is required for deep agents. Install with: " |
| 32 | + "pip install 'uipath-langchain[deep]' " |
| 33 | + "(or: uv add 'uipath-langchain[deep]')" |
| 34 | +) |
| 35 | + |
| 36 | + |
| 37 | +def _import_create_deep_agent() -> Any: |
| 38 | + try: |
| 39 | + from deepagents import create_deep_agent as _upstream |
| 40 | + |
| 41 | + return _upstream |
| 42 | + except ImportError as exc: |
| 43 | + raise ImportError(_INSTALL_HINT) from exc |
| 44 | + |
| 45 | + |
| 46 | +def create_deep_agent( |
| 47 | + model: BaseChatModel, |
| 48 | + system_prompt: str = "", |
| 49 | + tools: Sequence[BaseTool] = (), |
| 50 | + subagents: "Sequence[SubAgent | CompiledSubAgent]" = (), |
| 51 | + backend: "BackendProtocol | BackendFactory | None" = None, |
| 52 | + response_format: ResponseFormat[Any] | None = None, |
| 53 | +) -> CompiledStateGraph[Any, Any, Any, Any]: |
| 54 | + """Create a deep agent. |
| 55 | +
|
| 56 | + Deep agents provide built-in capabilities for: |
| 57 | + - Planning (write_todos, read_todos) |
| 58 | + - Filesystem operations (read_file, write_file, edit_file, ls, glob, grep) |
| 59 | + - Sub-agent delegation (task) |
| 60 | + - Auto-summarization for long conversations |
| 61 | +
|
| 62 | + Args: |
| 63 | + model: A BaseChatModel instance. |
| 64 | + system_prompt: Instructions for the agent. |
| 65 | + tools: Custom tools to provide to the agent. |
| 66 | + subagents: Optional list of subagent configurations. Each entry is a |
| 67 | + ``SubAgent`` (name, description, system_prompt, and optional tools/model/middleware) |
| 68 | + or a ``CompiledSubAgent`` (name, description, and a pre-built runnable). |
| 69 | + backend: Storage backend for filesystem operations. Can be a |
| 70 | + ``BackendProtocol`` instance, a factory callable, or ``None`` |
| 71 | + (uses the default in-state backend). |
| 72 | + response_format: Structured output format for the agent response. |
| 73 | +
|
| 74 | + Returns: |
| 75 | + Compiled LangGraph agent ready for execution. |
| 76 | +
|
| 77 | + Raises: |
| 78 | + ImportError: If the ``deepagents`` package is not installed. Install |
| 79 | + with ``pip install 'uipath-langchain[deep]'`` or |
| 80 | + ``uv add 'uipath-langchain[deep]'``. |
| 81 | + """ |
| 82 | + upstream_create_deep_agent = _import_create_deep_agent() |
| 83 | + return upstream_create_deep_agent( |
| 84 | + model=model, |
| 85 | + system_prompt=system_prompt, |
| 86 | + tools=list(tools), |
| 87 | + subagents=list(subagents), |
| 88 | + backend=backend, |
| 89 | + response_format=response_format, |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def create_deep_agent_graph( |
| 94 | + model: BaseChatModel, |
| 95 | + tools: Sequence[BaseTool], |
| 96 | + system_prompt: str, |
| 97 | + backend: "BackendProtocol | BackendFactory | None", |
| 98 | + response_format: ResponseFormat[Any] | None, |
| 99 | + input_schema: type[BaseModel] | None, |
| 100 | + output_schema: type[BaseModel], |
| 101 | + build_user_message: Callable[[dict[str, Any]], str], |
| 102 | +) -> StateGraph[Any, Any, Any, Any]: |
| 103 | + """Build a deep agent wrapped in a parent graph that handles I/O transformation. |
| 104 | +
|
| 105 | + The deep agent only understands messages as input and produces |
| 106 | + structured_response as output. The wrapper graph bridges the gap: |
| 107 | +
|
| 108 | + START -> transform_input -> deep_agent -> transform_output -> END |
| 109 | +
|
| 110 | + Args: |
| 111 | + model: Chat model for the deep agent. |
| 112 | + tools: Tools available to the deep agent. |
| 113 | + system_prompt: Combined system + meta prompt. |
| 114 | + backend: Filesystem backend for the deep agent. |
| 115 | + response_format: Structured output format. |
| 116 | + input_schema: Resolved input Pydantic model (or None). |
| 117 | + output_schema: Resolved output Pydantic model. |
| 118 | + build_user_message: Callable that converts input arguments dict to a user message string. |
| 119 | +
|
| 120 | + Raises: |
| 121 | + ImportError: If the ``deepagents`` package is not installed. Install |
| 122 | + with ``pip install 'uipath-langchain[deep]'`` or |
| 123 | + ``uv add 'uipath-langchain[deep]'``. |
| 124 | + """ |
| 125 | + inner_graph = create_deep_agent( |
| 126 | + model=model, |
| 127 | + tools=tools, |
| 128 | + system_prompt=system_prompt, |
| 129 | + backend=backend, |
| 130 | + response_format=response_format, |
| 131 | + ) |
| 132 | + |
| 133 | + wrapper_state = create_state_with_input(input_schema) |
| 134 | + |
| 135 | + internal_fields = set(DeepAgentGraphState.model_fields.keys()) |
| 136 | + |
| 137 | + def transform_input(state: BaseModel) -> dict[str, Any]: |
| 138 | + state_data = state.model_dump() |
| 139 | + input_data = {k: v for k, v in state_data.items() if k not in internal_fields} |
| 140 | + input_args = ( |
| 141 | + input_schema.model_validate(input_data).model_dump() |
| 142 | + if input_schema is not None |
| 143 | + else {} |
| 144 | + ) |
| 145 | + user_text = build_user_message(input_args) |
| 146 | + return {"messages": [HumanMessage(content=user_text, id="user-input")]} |
| 147 | + |
| 148 | + def transform_output(state: BaseModel) -> dict[str, Any]: |
| 149 | + structured = getattr(state, "structured_response", {}) |
| 150 | + return output_schema.model_validate(structured).model_dump() |
| 151 | + |
| 152 | + wrapper: StateGraph[Any, Any, Any, Any] = StateGraph( |
| 153 | + wrapper_state, input_schema=input_schema, output_schema=output_schema |
| 154 | + ) |
| 155 | + wrapper.add_node("transform_input", transform_input) |
| 156 | + wrapper.add_node("deep_agent", inner_graph) |
| 157 | + wrapper.add_node("transform_output", transform_output) |
| 158 | + wrapper.add_edge(START, "transform_input") |
| 159 | + wrapper.add_edge("transform_input", "deep_agent") |
| 160 | + wrapper.add_edge("deep_agent", "transform_output") |
| 161 | + wrapper.add_edge("transform_output", END) |
| 162 | + |
| 163 | + return wrapper |
0 commit comments