|
7 | 7 | import json |
8 | 8 | import io |
9 | 9 | from test_base import TestBase, servicePreparer |
10 | | -from devtools_testutils import recorded_by_proxy |
| 10 | +from devtools_testutils import recorded_by_proxy, RecordedTransport |
11 | 11 | from azure.ai.projects.models import PromptAgentDefinition, AgentDetails, AgentVersionDetails |
12 | 12 |
|
13 | 13 |
|
@@ -111,3 +111,98 @@ def test_agents_crud(self, **kwargs): |
111 | 111 | agent_name=second_agent_name, agent_version=agent2_version1.version |
112 | 112 | ) |
113 | 113 | assert result.deleted |
| 114 | + |
| 115 | + # To run this test: |
| 116 | + # pytest tests\agents\test_agents_crud.py::TestAgentCrud::test_agent_disable_enable -s |
| 117 | + @servicePreparer() |
| 118 | + @recorded_by_proxy(RecordedTransport.AZURE_CORE, RecordedTransport.HTTPX) |
| 119 | + def test_agent_disable_enable(self, **kwargs): |
| 120 | + """ |
| 121 | + Test disable and enable operations for Agents. |
| 122 | +
|
| 123 | + This test creates an agent, verifies it can respond to requests, |
| 124 | + disables it and verifies requests fail, then enables it and |
| 125 | + verifies requests work again. |
| 126 | +
|
| 127 | + Routes used in this test: |
| 128 | +
|
| 129 | + Action REST API Route Client Method |
| 130 | + ------+---------------------------------------------+----------------------------------- |
| 131 | + POST /agents/{agent_name}/versions project_client.agents.create_version() |
| 132 | + POST /openai/conversations openai_client.conversations.create() |
| 133 | + POST /openai/responses openai_client.responses.create() |
| 134 | + POST /agents/{agent_name}:disable project_client.agents.disable() |
| 135 | + POST /agents/{agent_name}:enable project_client.agents.enable() |
| 136 | + DELETE /agents/{agent_name}/versions/{agent_version} project_client.agents.delete_version() |
| 137 | + """ |
| 138 | + print("\n") |
| 139 | + model = kwargs.get("foundry_model_name") |
| 140 | + agent_name = "DisableEnableTestAgent" |
| 141 | + |
| 142 | + # Setup |
| 143 | + project_client = self.create_client(operation_group="agents", **kwargs) |
| 144 | + openai_client = project_client.get_openai_client() |
| 145 | + |
| 146 | + # Create an Agent |
| 147 | + agent = project_client.agents.create_version( |
| 148 | + agent_name=agent_name, |
| 149 | + definition=PromptAgentDefinition( |
| 150 | + model=model, |
| 151 | + instructions="You are a helpful assistant that answers general questions", |
| 152 | + ), |
| 153 | + ) |
| 154 | + print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})") |
| 155 | + self._validate_agent_version(agent) |
| 156 | + |
| 157 | + # Create a conversation |
| 158 | + conversation = openai_client.conversations.create( |
| 159 | + items=[{"type": "message", "role": "user", "content": "How many feet in a mile?"}] |
| 160 | + ) |
| 161 | + print(f"Created conversation with initial user message (id: {conversation.id})") |
| 162 | + |
| 163 | + # Verify the agent can respond to requests |
| 164 | + response = openai_client.responses.create( |
| 165 | + conversation=conversation.id, |
| 166 | + extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}}, |
| 167 | + ) |
| 168 | + print(f"Response id: {response.id}, output text: {response.output_text}") |
| 169 | + assert "5280" in response.output_text or "5,280" in response.output_text |
| 170 | + |
| 171 | + # Disable the agent |
| 172 | + project_client.agents.disable(agent_name=agent_name) |
| 173 | + print(f"Agent disabled") |
| 174 | + |
| 175 | + # Verify requests fail when agent is disabled |
| 176 | + error_raised = False |
| 177 | + try: |
| 178 | + _ = openai_client.responses.create( |
| 179 | + conversation=conversation.id, |
| 180 | + extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}}, |
| 181 | + ) |
| 182 | + except Exception as e: |
| 183 | + error_raised = True |
| 184 | + print(f"Expected error when calling disabled agent: {e}") |
| 185 | + assert error_raised, "Expected an error when calling a disabled agent" |
| 186 | + |
| 187 | + # Enable the agent |
| 188 | + project_client.agents.enable(agent_name=agent_name) |
| 189 | + print(f"Agent enabled") |
| 190 | + |
| 191 | + # Add a new message to the conversation for the next request |
| 192 | + _ = openai_client.conversations.items.create( |
| 193 | + conversation.id, |
| 194 | + items=[{"type": "message", "role": "user", "content": "And how many meters?"}], |
| 195 | + ) |
| 196 | + |
| 197 | + # Verify the agent can respond to requests again |
| 198 | + response = openai_client.responses.create( |
| 199 | + conversation=conversation.id, |
| 200 | + extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}}, |
| 201 | + ) |
| 202 | + print(f"Response id: {response.id}, output text: {response.output_text}") |
| 203 | + assert "1609" in response.output_text or "1,609" in response.output_text |
| 204 | + |
| 205 | + # Cleanup - delete the agent |
| 206 | + result = project_client.agents.delete_version(agent_name=agent_name, agent_version=agent.version) |
| 207 | + assert result.deleted |
| 208 | + print(f"Agent deleted") |
0 commit comments