Environment Module¶
The Environment module provides an interface for the agent to interact with the outside world. It handles tool execution, approval requests, and resource management.
Overview¶
The Environment class is a network implementation of the ToolEnvironmentConnection interface. It routes all calls through a specific Protocol instance that has a registered environment. This allows the agent to:
- Execute tools with the given parameters
- Request approval for tool usage
- Clean up resources when done
Key Concepts¶
Tool Execution¶
The environment executes tools through the execute_tool() method, which:
- Takes a
ToolCallPartcontaining the tool name and parameters - Sends a request to the protocol to execute the tool
- Returns a response containing the tool execution result
Approval Requests¶
Before executing a tool, the environment may request approval through the approve_tool_use() method, which:
- Takes a
ToolCallPartcontaining the tool name and parameters - Sends a request to the protocol to approve the tool
- Returns a response indicating whether the tool was approved
Response Types¶
The environment uses several response types to communicate with the agent:
EnvironmentResponse: A generic response container for environment operationsWorkingDirectoryResponse: Response data for get_working_directoryToolMetadataResponse: Response data for get_all_tool_metadataToolNamesResponse: Response data for get_available_tool_namesApprovalResponse: Response data for approval operationsValidatePathResponse: Response data for validate_path operation
Implementation Details¶
The Environment class is initialized with a Protocol instance, which it uses to communicate with the client. It maintains a logger for logging events and a cache for tool metadata.
The execute_tool() and approve_tool_use() methods use the protocol's request_response() method to send requests and receive responses.
Usage Example¶
Here's a simplified example of how to use the Environment:
# Create an environment
environment = Environment(protocol)
# Execute a tool
tool_call = ToolCallPart(
tool_call_id="123",
tool_name="write_file",
args={"file_path": "example.txt", "content": "Hello, world!"}
)
response = await environment.execute_tool(tool_call)
# Check if the tool execution was successful
if response.success:
print(f"Tool execution successful: {response.data}")
else:
print(f"Tool execution failed: {response.message}")
Important Considerations¶
When working with the Environment:
-
Protocol Initialization: Make sure the protocol is initialized before creating the environment.
-
Tool Approval: Some tools may require approval before execution. Always check if a tool requires approval and request it if needed.
-
Error Handling: Handle errors from tool execution appropriately. The environment returns a success flag and an error message if the tool execution fails.
-
Resource Cleanup: Call the
cleanup()method when done with the environment to clean up any resources.
API Reference¶
azad.environment ¶
Network tool environment connection implementation.
Attributes¶
Classes¶
EnvironmentResponse ¶
WorkingDirectoryResponse ¶
ToolMetadataResponse ¶
ToolNamesResponse ¶
ApprovalResponse ¶
Bases: BaseModel
Response data for approval operations.
Attributes¶
images
class-attribute
instance-attribute
¶
images: List[ImagePart] = Field(default_factory=list, description='List of image URLs')
ValidatePathResponse ¶
Environment ¶
Environment(protocol: Protocol)
Network implementation of ToolEnvironmentConnection.
This class provides a network implementation of the ToolEnvironmentConnection interface by routing all calls through a specific Protocol instance that has a registered environment.
Initialize the connection.
Parameters:
-
protocol(Protocol) –Protocol instance to use for communication
Source code in azad/environment.py
Attributes¶
Functions¶
execute_tool
async
¶
execute_tool(tool_call: ToolCallPart) -> EnvironmentResponse[ToolResultPart]
Execute a tool with the given parameters using request/response pattern.
Parameters:
-
tool_name–Name of the tool to execute
-
parameters–Tool parameters as primitive types
Returns:
-
EnvironmentResponse[ToolResultPart]–EnvironmentResponse[ToolResultPart]: Response containing tool execution result
Source code in azad/environment.py
approve_tool_use
async
¶
approve_tool_use(tool_call: ToolCallPart) -> EnvironmentResponse[ApprovalResponse]
Request approval for tool usage.
Parameters:
-
tool_name–Name of the tool to approve
-
parameters–Tool parameters to approve
Returns:
-
EnvironmentResponse[ApprovalResponse]–EnvironmentResponse[ApprovalResponse]: Response containing approval status