Spaces:
Running
Running
File size: 11,310 Bytes
b8277c4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 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 | """
Test script for the new /tenant-run endpoint.
1. Sets up tenant in data_sources API
2. Creates tenant API key
3. Registers PostgreSQL source
4. Tests the /tenant-run endpoint with streaming and non-streaming
"""
import httpx
import json
import sys
import os
from datetime import datetime
# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# ============================================================================
# CONFIGURATION
# ============================================================================
# Data Sources API
DATA_SOURCES_API_URL = "http://127.0.0.1:8000"
ADMIN_API_KEY = os.environ.get("SIRUS_ADMIN_API_KEY", "426f27d994943c874c22d42e77596e33c455dac315c361d35215cae5f39941b3")
# Your AgentOS server
AGENT_OS_URL = "http://127.0.0.1:5559"
AGENT_ID = "sirus-sql-agent"
# Tenant configuration
TENANT_ID = "demo_tenant_001"
SOURCE_NAME = "scv_sample_db"
USER_ID = "test-user-007"
SESSION_ID = "my-test-session-123"
# PostgreSQL connection
DB_CONFIG = {
"uri": "postgresql://neondb_owner:npg_dfWNsn2ZGk7c@ep-cool-poetry-a1puamly-pooler.ap-southeast-1.aws.neon.tech:5432/scv-sample?sslmode=require"
}
# Will be set after tenant setup
TENANT_API_KEY = None
# ============================================================================
# TENANT SETUP FUNCTIONS
# ============================================================================
def setup_tenant():
"""Register tenant and source in data_sources API, return API key."""
global TENANT_API_KEY
print("\n" + "="*80)
print("π§ SETTING UP TENANT IN DATA SOURCES API")
print("="*80)
client = httpx.Client(base_url=DATA_SOURCES_API_URL, timeout=120)
# Step 1: Create API key for tenant
print(f"\nπ Creating API key for tenant '{TENANT_ID}'...")
try:
api_key_response = client.post(
f"/api/v1/data-sources/tenants/{TENANT_ID}/api-keys",
json={
"description": f"Test key for {TENANT_ID} - {datetime.now().isoformat()}",
"expires_in_days": 30
},
headers={"X-Sirus-Admin-Key": ADMIN_API_KEY}
)
if api_key_response.status_code != 201:
print(f"β Failed to create API key: {api_key_response.status_code}")
print(f" Response: {api_key_response.text}")
return False
api_key_data = api_key_response.json()
TENANT_API_KEY = api_key_data["api_key"]
print(f"β
API key created: {TENANT_API_KEY[:20]}...")
except Exception as e:
print(f"β Error creating API key: {e}")
return False
# Step 2: Register PostgreSQL source
print(f"\nπ Registering PostgreSQL source '{SOURCE_NAME}'...")
try:
source_response = client.post(
f"/api/v1/data-sources/tenants/{TENANT_ID}/sources",
json={
"sources": [
{
"source_name": SOURCE_NAME,
"source_type": "ibis",
"config": {
"uri": DB_CONFIG["uri"]
}
}
],
"validate_connection": True
},
headers={"X-Sirus-Api-Key": TENANT_API_KEY}
)
if source_response.status_code != 201:
print(f"β Failed to register source: {source_response.status_code}")
print(f" Response: {source_response.text}")
return False
source_data = source_response.json()
probe_result = source_data["probe_results"][0]
if probe_result["status"] == "success":
print(f"β
Source registered and validated")
print(f" Tables found: {probe_result.get('tables_found', 'N/A')}")
else:
print(f"β οΈ Source registered but validation failed: {probe_result.get('message', 'Unknown error')}")
except Exception as e:
print(f"β Error registering source: {e}")
return False
# Step 3: Verify source is accessible
print(f"\nπ Verifying source access...")
try:
list_response = client.get(
"/api/v1/data-sources/list",
params={"tenant_id": TENANT_ID},
headers={"X-Sirus-Api-Key": TENANT_API_KEY}
)
if list_response.status_code == 200:
sources = list_response.json()["available_sources"]
print(f"β
Tenant sources accessible: {sources}")
else:
print(f"β οΈ Failed to list sources: {list_response.text}")
except Exception as e:
print(f"β οΈ Error verifying sources: {e}")
print("\n" + "="*80)
print("β
TENANT SETUP COMPLETE")
print("="*80)
return True
# ============================================================================
# TEST QUERIES
# ============================================================================
test_queries = [
"What tables are available in this database?",
"How many records are in each table?",
"Show me the first 3 rows from the action_items table",
]
# ============================================================================
# TEST FUNCTIONS
# ============================================================================
def test_streaming_request(question: str):
"""Test the endpoint with streaming enabled (real-time output)"""
print(f"\n{'='*80}")
print(f"π STREAMING TEST: {question}")
print(f"{'='*80}\n")
payload = {
"message": question,
"tenant_id": TENANT_ID,
"source_name": SOURCE_NAME,
"api_key": TENANT_API_KEY,
"session_id": SESSION_ID,
"user_id": USER_ID,
"stream": True # Enable streaming
}
try:
with httpx.stream(
"POST",
f"{AGENT_OS_URL}/tenant-run/{AGENT_ID}",
json=payload,
timeout=60.0
) as response:
print(f"β
Connected with status: {response.status_code}")
response.raise_for_status()
print("\n--- Agent Stream Output ---")
for line in response.iter_lines():
if line.startswith("event:"):
event_type = line[6:].strip()
print(f"\n[EVENT: {event_type}]")
elif line.startswith("data:"):
try:
data = json.loads(line[5:])
if isinstance(data, dict):
# Pretty print structured data
if data.get('event') == 'RunCompleted':
print("\n[β
Run Completed]")
elif 'content' in data:
print(f" {data['content']}")
else:
print(f" {json.dumps(data, indent=2)}")
else:
print(f" {data}")
except json.JSONDecodeError:
# Raw text output
print(f" {line[5:]}")
print("--- End of Stream ---\n")
except httpx.HTTPStatusError as e:
print(f"β HTTP Error: {e.response.status_code}")
print(f" Response: {e.response.text}")
except httpx.RequestError as e:
print(f"β Connection Error: Failed to connect to {e.request.url}")
except Exception as e:
print(f"β Unexpected Error: {e}")
def test_non_streaming_request(question: str):
"""Test the endpoint with streaming disabled (get full response at once)"""
print(f"\n{'='*80}")
print(f"π NON-STREAMING TEST: {question}")
print(f"{'='*80}\n")
payload = {
"message": question,
"tenant_id": TENANT_ID,
"source_name": SOURCE_NAME,
"api_key": TENANT_API_KEY,
"session_id": SESSION_ID + "-non-streaming", # Different session
"user_id": USER_ID,
"stream": False # Disable streaming
}
try:
response = httpx.post(
f"{AGENT_OS_URL}/tenant-run/{AGENT_ID}",
json=payload,
timeout=60.0
)
response.raise_for_status()
result = response.json()
print(f"β
Response received (status: {response.status_code})")
print(f"\nSession ID: {result.get('session_id')}")
print(f"Tenant ID: {result.get('tenant_id')}")
print(f"\n--- Agent Response ---")
print(result.get('response', 'No response content'))
print("--- End of Response ---\n")
except httpx.HTTPStatusError as e:
print(f"β HTTP Error: {e.response.status_code}")
print(f" Response: {e.response.text}")
except httpx.RequestError as e:
print(f"β Connection Error: Failed to connect to {e.request.url}")
except Exception as e:
print(f"β Unexpected Error: {e}")
def test_health_check():
"""Test that the server is running and accessible"""
print(f"\n{'='*80}")
print(f"π₯ HEALTH CHECK")
print(f"{'='*80}\n")
try:
# Test the standard AgentOS config endpoint
response = httpx.get(f"{AGENT_OS_URL}/config", timeout=5.0)
response.raise_for_status()
print(f"β
Server is running at {AGENT_OS_URL}")
print(f" Status: {response.status_code}")
# Try to get agents list
agents_response = httpx.get(f"{AGENT_OS_URL}/agents", timeout=5.0)
agents_response.raise_for_status()
agents = agents_response.json()
print(f" Available agents: {[agent.get('id') for agent in agents]}")
except httpx.HTTPStatusError as e:
print(f"β HTTP Error: {e.response.status_code}")
print(f" Response: {e.response.text}")
except httpx.RequestError as e:
print(f"β Connection Error: Failed to connect to {AGENT_OS_URL}")
print(f" Is the agent server running? Start it with: python agent.py")
except Exception as e:
print(f"β Unexpected Error: {e}")
# ============================================================================
# MAIN TEST RUNNER
# ============================================================================
def main():
# Step 1: Check if agent server is running
test_health_check()
# Step 2: Setup tenant in data_sources API
if not setup_tenant():
print("\nβ Tenant setup failed. Cannot proceed with tests.")
print(" Make sure the data_sources API is running at:", DATA_SOURCES_API_URL)
return
# Step 3: Test with the first query using streaming
if test_queries:
print("\n\n" + "="*80)
print("π‘ STREAMING MODE TESTS")
print("="*80)
test_streaming_request(test_queries[0])
# Step 4: Test with the second query using non-streaming
if len(test_queries) > 1:
print("\n\n" + "="*80)
print("π¦ NON-STREAMING MODE TESTS")
print("="*80)
test_non_streaming_request(test_queries[1])
if __name__ == "__main__":
main()
|