Agent Module¶
The Agent module contains the AzadAgent class, which is the central component of the Azad system. It manages tasks, handles tool calls, and coordinates the execution of the AI agent.
Overview¶
The AzadAgent class is a ProtocolHandler that manages the execution of tasks. It:
- Handles incoming requests from clients
- Manages task state and execution
- Coordinates tool calls and responses
- Communicates with the AI network
The agent uses a request/response pattern for most operations, with streaming responses for operations that produce multiple events over time.
Key Concepts¶
Task Management¶
The agent manages tasks through several methods:
load_task(): Loads a task and its configurationget_loaded_task(): Returns the currently loaded taskstep_task(): Executes a single step of the task
Tool Execution¶
The agent handles tool execution through:
- Tool calls from the language model
- Approval from the user (if required)
- Execution of the tool
- Return of the result to the language model
Server Tools¶
The agent supports server-side tools that run on the server without requiring user approval for each step. These tools:
- Run asynchronously in the background
- Report progress through callbacks
- Access server resources and environments
- Return structured results to the agent
Daemon Mode¶
The agent can run in daemon mode, which:
- Starts a WebSocket server
- Listens for client connections
- Handles client requests
- Manages the agent lifecycle
Implementation Details¶
The AzadAgent class implements the ProtocolHandler interface, which allows it to handle network protocol messages. It uses:
@request_responsedecorators for methods that return a single response@request_streamdecorators for methods that return a stream of responses
The agent maintains state for:
- The current task
- The environment
- The task configuration
- Active server tools
Usage Example¶
Here's a simplified example of how to use the AzadAgent:
# Create an agent
agent = AzadAgent()
# Load a task
await agent.load_task(task, task_config)
# Execute a step
async for event in agent.step_task(task, task_config):
# Process the event
handle_event(event)
# Run a server tool
async for event in agent.run_server_tool("tool_name", {"param": "value"}):
# Process the event
handle_event(event)
Important Considerations¶
When working with the AzadAgent:
-
Task State: The agent maintains the state of the current task. Make sure to load a task before executing steps.
-
Environment Initialization: The agent initializes the environment when a task is loaded. Make sure the protocol is set before initializing the environment.
-
Cancellation: Use the
abort_step()method to cancel a running step. -
Server Tools: Server tools run asynchronously in the background. Use the
_cancel_all_server_tools()method to cancel all running server tools. -
Daemon Mode: Use the
enter_daemon_mode()class method to start the agent in daemon mode.
API Reference¶
azad.agent ¶
Attributes¶
Classes¶
SingleRequest ¶
Bases: BaseModel
ResponseDataOneShot ¶
Bases: BaseModel
AzadAgent ¶
Bases: ProtocolHandler
Source code in azad/agent.py
Attributes¶
Functions¶
step_task
async
¶
step_task(task: Task, task_config: TaskConfig, iteration: int) -> AsyncIterator[AINetworkEventUnion]
Source code in azad/agent.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
abort_step
async
¶
abort_step(request_id: str) -> BaseResponse
Aborts the currently running agent step.
Source code in azad/agent.py
request_response_oneshot
async
¶
request_response_oneshot(request: OneShotRequestUnion) -> BaseResponse
Handles a non-streaming one-shot request to the AI network using litellm directly. Makes a synchronous call to litellm and returns the response.
Source code in azad/agent.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
upload_failed_edit
async
¶
upload_failed_edit(task: Task, task_config: TaskConfig, file_content: str) -> BaseResponse
Schedules the upload of a failed edit to run as a background job.
Source code in azad/agent.py
render_prompt
async
¶
render_prompt(task: Task, task_config: TaskConfig) -> BaseResponse
Renders the prompt messages that would be sent to the LLM without actually sending them.
Source code in azad/agent.py
run_server_tool
async
¶
run_server_tool(tool_name: str, args: Dict[str, Any], task_config: Optional[TaskConfig] = None) -> AsyncIterator[AINetworkEventUnion]
Runs a server-side tool initiated by the client.
Source code in azad/agent.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | |
enter_daemon_mode
async
classmethod
¶
enter_daemon_mode(config: GlobalConfig) -> None
Source code in azad/agent.py
Functions¶
generate_untagged_request_id ¶
generate_untagged_request_id(request_map: Dict[str, SingleRequest]) -> str
Generates a unique request ID that is not already in use.