|
| 1 | +--- |
| 2 | +name: develop-agent-module |
| 3 | +description: Use when modifying, extending, or debugging anything inside the agent/ module — the ReAct agent graph, tools, guardrails, exceptions, multimodal, or wrappers. Guides clean architecture, module boundaries, and correct integration patterns. Triggers on "add a node to the agent graph", "modify routing", "add a guardrail action", "change agent state", "fix agent exceptions", "add multimodal support", or any work touching files under src/uipath_langchain/agent/. |
| 4 | +allowed-tools: Read, Write, Edit, Glob, Grep, Bash, Agent |
| 5 | +--- |
| 6 | + |
| 7 | +# Develop the Agent Module |
| 8 | + |
| 9 | +Guidance for working inside `src/uipath_langchain/agent/` — the ReAct agent orchestration layer. |
| 10 | + |
| 11 | +**Read first, code second.** Before changing anything, read the existing code in the subsystem you're modifying. Follow established patterns unless you have a concrete reason to diverge — and if you do, discuss it first. |
| 12 | + |
| 13 | +## Architectural Constraints |
| 14 | + |
| 15 | +The agent module is designed so that subsystems can be composed into different loop patterns without coupling: |
| 16 | + |
| 17 | +- **`tools/` must not know about the loop.** Tools are standalone capabilities — they receive input, do work, return output. They must not import from `react/` (except shared types in `react/types.py` and helpers in `react/utils.py`). A tool should work regardless of whether it's orchestrated by a ReAct loop, a plan-and-execute loop, or something else entirely. |
| 18 | +- **`guardrails/` must not know about the loop.** Guardrail evaluation and actions are generic validation — they inspect data, return pass/fail, and execute actions. The *wiring* of guardrails into a specific loop happens in `react/guardrails/`, not in `guardrails/` itself. |
| 19 | +- **`react/` is one loop implementation, not the only possible one.** It owns graph construction, routing, and node lifecycle. It composes tools and guardrails but those subsystems don't depend back on it. |
| 20 | +- **`exceptions/`, `multimodal/`, `messages/` are fully standalone.** They have no knowledge of the loop or each other. |
| 21 | +- **`wrappers/` are loop-agnostic but impose state contracts.** They don't import the loop, but they do depend on specific state fields existing (e.g., `inner_state.job_attachments`). When adding a wrapper, ensure the state fields it expects are documented in `react/types.py`. |
| 22 | + |
| 23 | +This means: if you're adding a tool and find yourself importing from `react/agent.py` or `react/router.py`, you're coupling the tool to the loop. Stop and rethink. |
| 24 | + |
| 25 | +## Upstream Types (`uipath.agent` in uipath-python) |
| 26 | + |
| 27 | +Agent definition models, resource configs, guardrail models, flow control tools (`END_EXECUTION_TOOL`, `RAISE_ERROR_TOOL`), and prompt templates all live in `uipath.agent.models.agent` in uipath-python. This package consumes them — it doesn't define them. |
| 28 | + |
| 29 | +Key connection points: |
| 30 | +- `tools/tool_factory.py` dispatches on `isinstance(resource, AgentXResourceConfig)` — each resource config type maps to a tool factory |
| 31 | +- `guardrails/guardrails_factory.py` converts `AgentGuardrail` models into executable `(BaseGuardrail, GuardrailAction)` pairs |
| 32 | + |
| 33 | +**Adding a new tool type requires the `AgentXResourceConfig` to exist in uipath-python first.** If it doesn't, that's a cross-package change. |
| 34 | + |
| 35 | +## Where Does My Change Go? |
| 36 | + |
| 37 | +| Change | Owner | Read first | |
| 38 | +|--------|-------|------------| |
| 39 | +| New tool type | `tools/<name>_tool.py` | `tools/process_tool.py` (simple), `tools/context_tool.py` (complex) | |
| 40 | +| New guardrail action | `guardrails/actions/<name>_action.py` | `guardrails/actions/log_action.py` (simple), `guardrails/actions/escalate_action.py` (complex) | |
| 41 | +| New graph node | `react/<name>_node.py` | `react/llm_node.py`, `react/types.py` | |
| 42 | +| Routing changes | `react/router.py` or `react/router_conversational.py` | Both routers + `react/utils.py` | |
| 43 | +| State fields | `react/types.py` + `react/reducers.py` | Existing state classes and their reducers | |
| 44 | +| Error codes | `exceptions/exceptions.py` | Existing error code enums | |
| 45 | +| Tool wrapper | `wrappers/` | `wrappers/job_attachment_wrapper.py` | |
| 46 | +| Multimodal | `multimodal/` | `multimodal/invoke.py` | |
| 47 | +| Guardrail evaluation | `guardrails/guardrail_nodes.py` | Existing scope-specific node creators | |
| 48 | +| Guardrail subgraph wiring | `react/guardrails/guardrails_subgraph.py` | Existing subgraph creators | |
| 49 | + |
| 50 | +After adding a new tool: **also** register it in `tools/tool_factory.py` → `_build_tool_for_resource()` and export from `tools/__init__.py`. |
| 51 | + |
| 52 | +After adding a new guardrail action: **also** register it in `guardrails/guardrails_factory.py` → `build_guardrails_with_actions()` and export from `guardrails/actions/__init__.py`. |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## Import Rules (Non-Obvious) |
| 57 | + |
| 58 | +These are the constraints you can't discover by reading any single file: |
| 59 | + |
| 60 | +**`react/agent.py` is the composition root.** It imports from everywhere in agent/. Nothing else may import from `react/agent.py` — only the runtime layer above does. |
| 61 | + |
| 62 | +``` |
| 63 | +react/agent.py ← react/*, tools/, guardrails/, exceptions/ |
| 64 | +tools/* ← tools/*, react/types, react/utils, exceptions/, chat.hitl |
| 65 | +guardrails/* ← guardrails/*, react/types, exceptions/, messages/ |
| 66 | +wrappers/* ← react/*, tools/tool_node |
| 67 | +multimodal/* ← standalone (uipath._utils._ssl_context only) |
| 68 | +exceptions/* ← standalone (uipath.runtime.errors, uipath.platform.errors) |
| 69 | +messages/* ← standalone (langchain_core.messages) |
| 70 | +``` |
| 71 | + |
| 72 | +**Hard rules:** |
| 73 | +- tools/ and guardrails/ must **never** import from `react/agent.py` — import from `react/types.py` or `react/utils.py` instead |
| 74 | +- agent/ must **never** import from downstream consumers — the dependency flows one way into this package, not out |
| 75 | +- No direct `os.environ` reads — dependencies come through constructor parameters or SDK instances |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +## State Management Rules |
| 80 | + |
| 81 | +The LangGraph state system has sharp edges. These rules prevent silent corruption: |
| 82 | + |
| 83 | +- **Always return state updates as dicts from nodes** — LangGraph passes copies, so mutations on the state object are silently lost. The only way to update state is by returning dicts that the reducers merge. |
| 84 | +- New fields on `InnerAgentGraphState` **must** have a reducer via `Annotated[T, reducer_func]` — use `merge_dicts` for dicts, `merge_objects` for nested BaseModel fields. |
| 85 | +- To append messages: return `{"messages": [new_msg]}` — the `add_messages` reducer handles it. |
| 86 | +- To **replace** messages (rare): use `Overwrite` from `langgraph.types`. |
| 87 | +- `tools_storage` (in inner_state) is the shared key-value store for inter-tool communication — use it instead of inventing new state fields. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## Anti-Patterns |
| 92 | + |
| 93 | +| Don't | Do Instead | |
| 94 | +|-------|------------| |
| 95 | +| Import from `react/agent.py` in tools/ or guardrails/ | Import from `react/types.py` or `react/utils.py` | |
| 96 | +| Put routing logic in tool nodes | Return results; let the router decide | |
| 97 | +| Mutate state object in a node (silently lost) | Return `{"messages": [...]}` dicts; let reducers merge | |
| 98 | +| Read env vars inside tool/node functions | Accept config through factory parameters | |
| 99 | +| Create tool classes | Use `create_<name>_tool()` factory functions | |
| 100 | +| Add `AgentGraphState` fields without reducers | Use `Annotated[T, reducer_func]` | |
| 101 | +| Catch and silently swallow exceptions | Re-raise or wrap with `AgentRuntimeError` | |
| 102 | +| Use `asyncio.run()` for sync→async bridging | Use `asyncio.run_coroutine_threadsafe()` | |
| 103 | +| Add guardrail eval logic outside `guardrail_nodes.py` | Create a scope-specific creator there | |
| 104 | +| Put HITL logic in tool functions | Set `REQUIRE_CONVERSATIONAL_CONFIRMATION` metadata on the tool | |
| 105 | +| Skip registering new tools in `tool_factory.py` | Always add dispatch entry in `_build_tool_for_resource()` | |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## Exception Handling |
| 110 | + |
| 111 | +Use the structured error types in `exceptions/` — never raise raw `Exception`, `ValueError`, or `RuntimeError`: |
| 112 | + |
| 113 | +- **Runtime errors** (during execution): `AgentRuntimeError(code=AgentRuntimeErrorCode.X, title=..., detail=..., category=...)` |
| 114 | +- **Startup errors** (during init): `AgentStartupError(code=AgentStartupErrorCode.X, title=..., detail=..., category=...)` |
| 115 | +- **HTTP errors from platform calls**: catch `EnrichedException`, map via `raise_for_enriched()` in `exceptions/helpers.py` |
| 116 | +- **LLM provider errors**: handled by `raise_for_provider_http_error()` in `exceptions/licensing.py` |
| 117 | +- Always chain exceptions: `raise AgentRuntimeError(...) from e` |
| 118 | + |
| 119 | +## Testing |
| 120 | + |
| 121 | +Tests live in `tests/agent/` mirroring the source structure. Before writing new tests, read `tests/agent/tools/test_process_tool.py` for the standard fixture and mocking patterns. |
| 122 | + |
| 123 | +Key conventions: |
| 124 | +- Use `pytest-httpx` (`HTTPXMock`) for HTTP mocking — never make real network calls |
| 125 | +- Use `monkeypatch.setenv()` / `monkeypatch.delenv()` for environment isolation |
| 126 | +- Async tests need no decorator (`asyncio_mode = "auto"`) |
| 127 | +- All test functions require type annotations |
| 128 | +- Mock SDK dependencies via `AsyncMock` / `MagicMock` — tools and nodes receive them through constructor params |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +## Verification |
| 133 | + |
| 134 | +After making changes: |
| 135 | + |
| 136 | +- [ ] Code is in the correct subsystem (see table above) |
| 137 | +- [ ] No imports from `react/agent.py` in tools/, guardrails/, or other leaf modules |
| 138 | +- [ ] No imports from downstream consumers |
| 139 | +- [ ] New state fields have reducers |
| 140 | +- [ ] Node functions return dicts (not mutating state) |
| 141 | +- [ ] New tools/actions registered in their respective factories |
| 142 | +- [ ] New exports added to the subsystem's `__init__.py` and `__all__` |
| 143 | +- [ ] `uv run ruff check src/uipath_langchain/agent/` passes |
| 144 | +- [ ] `uv run mypy src/uipath_langchain/agent/` passes |
| 145 | +- [ ] `uv run pytest tests/agent/` passes |
0 commit comments