You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/docs/en/sdk/basic-usage.md
+54-73Lines changed: 54 additions & 73 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,10 +37,10 @@ The development environment includes the following additional features:
37
37
### Basic Import
38
38
39
39
```python
40
-
import nexent
41
-
from nexent.core importMessageObserver, ProcessType
42
-
from nexent.core.agents import CoreAgent, NexentAgent
43
-
from nexent.core.models import OpenAIModel
40
+
from nexent.core.utils.observer import MessageObserver, ProcessType
41
+
from nexent.core.agents.core_agentimportCoreAgent
42
+
from nexent.core.agents.nexent_agentimport NexentAgent
43
+
from nexent.core.models.openai_llmimport OpenAIModel
44
44
from nexent.core.tools import ExaSearchTool, KnowledgeBaseSearchTool
45
45
```
46
46
@@ -64,7 +64,7 @@ model = OpenAIModel(
64
64
### 🛠️ Adding Tools
65
65
66
66
```python
67
-
# Create search tools
67
+
# Create search tool
68
68
search_tool = ExaSearchTool(
69
69
exa_api_key="your-exa-key",
70
70
observer=observer,
@@ -95,46 +95,58 @@ agent = CoreAgent(
95
95
96
96
```python
97
97
# Run Agent with your question
98
-
result = agent.run("Your question here")
99
-
100
-
# Access the final answer
101
-
print(result.final_answer)
98
+
agent.run("Your question here")
102
99
```
103
100
104
-
## 🎯 Advanced Usage Patterns
101
+
## 📡 Using agent_run (recommended for streaming)
105
102
106
-
### 🔧 Custom Tool Integration
103
+
When you need to consume messages as an "event stream" on server or client, use `agent_run`. It executes the agent in a background thread and continuously yields JSON messages, making it easy to render in UIs and collect logs.
`agent_run` provides a concise and thread-friendly way to run an agent while exposing real-time streaming output via `MessageObserver`. It is ideal for server-side or frontend event stream rendering, as well as MCP tool integration scenarios.
4
+
5
+
## Quick Start
6
+
7
+
```python
8
+
import json
9
+
import asyncio
10
+
import logging
11
+
from threading import Event
12
+
13
+
from nexent.core.agents.run_agent import agent_run
14
+
from nexent.core.agents.agent_model import (
15
+
AgentRunInfo,
16
+
AgentConfig,
17
+
ModelConfig
18
+
)
19
+
from nexent.core.utils.observer import MessageObserver
Tip: Store sensitive config such as `api_key` in environment variables or a secrets manager, not in code.
81
+
82
+
## Message Stream Format and Handling
83
+
84
+
Internally, `agent_run` executes the agent in a background thread and continuously yields JSON strings from the `MessageObserver` message buffer. You can parse these fields for categorized display or logging.
85
+
86
+
- Important fields
87
+
-`type`: message type (corresponds to `ProcessType`)
88
+
-`content`: text content
89
+
-`agent_name`: optional, which agent produced this message
90
+
91
+
Common `type` values (from `ProcessType`):
92
+
-`AGENT_NEW_RUN`: new task started
93
+
-`STEP_COUNT`: step updates
94
+
-`MODEL_OUTPUT_THINKING` / `MODEL_OUTPUT_CODE`: model thinking/code snippets
95
+
-`PARSE`: code parsing results
96
+
-`EXECUTION_LOGS`: Python execution logs
97
+
-`FINAL_ANSWER`: final answer
98
+
-`ERROR`: error information
99
+
100
+
## Configuration Reference
101
+
102
+
### ModelConfig
103
+
104
+
-`cite_name`: model alias (referenced by `AgentConfig.model_name`)
-`tools`: tool configuration list (see ToolConfig)
116
+
-`max_steps`: maximum steps
117
+
-`model_name`: model alias (corresponds to `ModelConfig.cite_name`)
118
+
-`provide_run_summary`: whether sub-agents provide run summary
119
+
-`managed_agents`: list of sub-agent configurations
120
+
121
+
### Pass Chat History (optional)
122
+
123
+
You can pass historical messages via `AgentRunInfo.history`, and Nexent will write them into internal memory:
124
+
125
+
```python
126
+
from nexent.core.agents.agent_model import AgentHistory
127
+
128
+
history = [
129
+
AgentHistory(role="user", content="Hi"),
130
+
AgentHistory(role="assistant", content="Hello, how can I help you?"),
131
+
]
132
+
133
+
agent_run_info = AgentRunInfo(
134
+
# ... other fields omitted
135
+
history=history,
136
+
)
137
+
```
138
+
139
+
## MCP Tool Integration (optional)
140
+
141
+
If you provide `mcp_host` (list of MCP service addresses), Nexent will automatically pull remote tools through `ToolCollection.from_mcp` and inject them into the agent:
142
+
143
+
```python
144
+
agent_run_info = AgentRunInfo(
145
+
# ... other fields omitted
146
+
mcp_host=["http://localhost:3000"],
147
+
)
148
+
```
149
+
150
+
Friendly error messages (EN/ZH) will be produced if the connection fails.
151
+
152
+
## Interrupt Execution
153
+
154
+
During execution, you can trigger interruption via `stop_event.set()`:
155
+
156
+
```python
157
+
stop_event.set() # The agent will gracefully stop after the current step completes
158
+
```
159
+
160
+
## Relation to CoreAgent
161
+
162
+
-`agent_run` is a wrapper over `NexentAgent` and `CoreAgent`, responsible for:
163
+
- Constructing `CoreAgent` (including models and tools)
164
+
- Injecting history into memory
165
+
- Driving streaming execution and forwarding buffered messages from `MessageObserver`
166
+
- You can also directly use `CoreAgent.run(stream=True)` to handle streaming yourself (see `core/agents.md`); `agent_run` provides a more convenient threaded and JSON-message oriented interface.
0 commit comments