-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathreact_agent.py
More file actions
87 lines (67 loc) · 2.87 KB
/
react_agent.py
File metadata and controls
87 lines (67 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
"""Simple ReAct Agent using LangGraph's Prebuilt create_react_agent.
This example demonstrates how to use LangGraph's prebuilt create_react_agent
function to quickly build a ReAct agent with Nexus filesystem integration.
Authentication:
API keys are REQUIRED via metadata.x_auth: "Bearer <token>"
Frontend automatically passes the authenticated user's API key in request metadata.
Each tool extracts and uses the token to create an authenticated nexus.connect() instance.
Requirements:
pip install langgraph langchain-anthropic
Usage from Frontend (HTTP):
POST http://localhost:2024/runs/stream
{
"assistant_id": "agent",
"input": {
"messages": [{"role": "user", "content": "Find all Python files"}]
},
"metadata": {
"x_auth": "Bearer sk-your-api-key-here",
"user_id": "user-123",
"tenant_id": "tenant-123",
"opened_file_path": "/workspace/admin/script.py" // Optional: currently opened file
}
}
Note: The frontend automatically includes x_auth and opened_file_path in metadata when user is logged in.
"""
import os
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import SystemMessage
from langchain_core.runnables import RunnableConfig, RunnableLambda
from langgraph.prebuilt import create_react_agent
from nexus_tools import get_nexus_tools
# Import official system prompt from Nexus tools
from nexus.bricks.tools import get_prompt
# Get configuration from environment variables
E2B_TEMPLATE_ID = os.getenv("E2B_TEMPLATE_ID")
print("API key will be provided per-request via config.configurable.nexus_api_key")
# Check E2B configuration
if E2B_TEMPLATE_ID:
print(f"E2B sandbox enabled with template: {E2B_TEMPLATE_ID}")
else:
print("E2B sandbox disabled (E2B_TEMPLATE_ID not set)")
# Create tools (no API key needed - will be passed per-request)
tools = get_nexus_tools()
# Create LLM
llm = ChatAnthropic(
model="claude-sonnet-4-5-20250929",
max_tokens=10000,
)
def build_prompt(state: dict, config: RunnableConfig) -> list:
"""Build prompt with optional opened file context from metadata.
This function is called before each LLM invocation and can access
the config which includes metadata from the frontend.
"""
# Get complete prompt with skills and opened file context using the convenience function
# get_prompt automatically extracts opened_file_path from config.metadata
system_content = get_prompt(config, role="general", state=state)
# Return system message + user messages
return [SystemMessage(content=system_content)] + state["messages"]
# Create a runnable that wraps the prompt builder
prompt_runnable = RunnableLambda(build_prompt)
# Create prebuilt ReAct agent with dynamic prompt
agent = create_react_agent(
model=llm,
tools=tools,
prompt=prompt_runnable,
)