# Agent API

### Agent API

The `Agent` class is the central orchestrator that combines skills, sensors, and perceptors.

#### Creating an Agent

```python
from composabl import Agent

# Create a new agent
agent = Agent()

# Create with ID
agent = Agent(id="temperature-controller-v1")
```

#### Agent Methods

**Adding Components**

```python
# Add single sensor
agent.add_sensor(sensor)

# Add multiple sensors
agent.add_sensors([sensor1, sensor2, sensor3])

# Add skill
agent.add_skill(skill)

# Add multiple skills
agent.add_skills([skill1, skill2])

# Add perceptor
agent.add_perceptor(perceptor)

# Add scenario
agent.add_scenario(scenario)
```

**Serialization**

```python
# Export to file
agent.export("path/to/agent.json")

# Load from file
agent = Agent.load("path/to/agent.json")
```

**Visualization**

```python
# Display agent structure
agent.draw()

# Get structure as string
structure = agent.get_structure()
```

#### Complete Agent Example

```python
from composabl import Agent, Sensor, Skill, Scenario, Perceptor
from composabl import MaintainGoal

# Create agent
agent = Agent(id="reactor-controller")

# Add sensors
agent.add_sensors([
    Sensor("temp", "Temperature in Celsius", lambda obs: obs["temperature"]),
    Sensor("pressure", "Pressure in bar", lambda obs: obs["pressure_reading"]),
    Sensor("flow", "Flow rate L/min", lambda obs: obs["flow_rate"])
])

# Add perceptor
class RateCalculator(PerceptorImpl):
    def __init__(self):
        self.last_temp = None
        
    async def compute(self, obs_spec, obs):
        rate = 0
        if self.last_temp is not None:
            rate = obs["temperature"] - self.last_temp
        self.last_temp = obs["temperature"]
        return {"temp_rate": rate}
    
    def filtered_sensor_space(self, obs):
        return ["temperature"]

agent.add_perceptor(Perceptor("rate-calc", RateCalculator))

# Add skills
temp_skill = Skill("maintain-temp", 
                  MaintainGoal("temp", "Keep temperature stable", 
                              target=75.0, stop_distance=2.0))
agent.add_skill(temp_skill)

# Add scenarios
agent.add_scenario(Scenario({
    "temperature": {"min": 70, "max": 80},
    "pressure": {"min": 5, "max": 7}
}))

# Visualize
agent.draw()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.amesa.com/reference/sdk-reference/core/agent-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
