File size: 17,481 Bytes
d02897f
 
 
 
 
 
 
 
 
 
 
cc49071
49aae56
d02897f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc49071
d02897f
 
 
cc49071
d02897f
 
cc49071
d02897f
 
49aae56
 
 
 
 
d02897f
49aae56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d02897f
 
 
 
 
 
 
 
49aae56
 
d02897f
 
 
49aae56
 
 
 
d02897f
cc49071
d02897f
 
cc49071
d02897f
 
cc49071
d02897f
 
 
 
 
 
 
 
 
cc49071
d02897f
cc49071
 
d02897f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc49071
d02897f
 
 
 
 
cc49071
 
d02897f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49aae56
 
 
 
d02897f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49aae56
 
 
710a404
5381665
710a404
 
 
 
d02897f
 
 
 
 
5381665
 
 
 
49aae56
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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
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
"""
OpenOps FastAPI Server
Provides REST API endpoints for the incident management environment
"""

import sys
from pathlib import Path

# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent.parent))

from fastapi import FastAPI, HTTPException, Query
from fastapi.responses import JSONResponse, HTMLResponse
from pydantic import BaseModel
from typing import Dict, Any, Optional
import uvicorn

from server.my_env_environment import MyEnvEnvironment
from models import IncidentAction, IncidentObservation


# FastAPI app
app = FastAPI(
    title="OpenOps API",
    description="Production Incident Management Environment API",
    version="1.0.0"
)

# Global environment instance
env_instance: Optional[MyEnvEnvironment] = None


# Request Models
class StepRequest(BaseModel):
    action_id: int
    task_id: Optional[int] = 1


# ============================================================
# LANDING PAGE WITH STATUS
# ============================================================

@app.get("/", response_class=HTMLResponse)
async def root():
    """Landing page with environment status and documentation."""
    return """
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>OpenOps - AI Incident Commander</title>
        <style>
            * { margin: 0; padding: 0; box-sizing: border-box; }
            body {
                font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                min-height: 100vh;
                padding: 20px;
            }
            .container {
                max-width: 900px;
                margin: 0 auto;
                background: white;
                border-radius: 12px;
                box-shadow: 0 20px 60px rgba(0,0,0,0.3);
                overflow: hidden;
            }
            .header {
                background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
                color: white;
                padding: 40px 30px;
                text-align: center;
            }
            .header h1 {
                font-size: 2.5em;
                margin-bottom: 10px;
                text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
            }
            .status {
                display: inline-block;
                background: rgba(255,255,255,0.2);
                padding: 8px 20px;
                border-radius: 20px;
                font-size: 0.9em;
                margin-top: 10px;
            }
            .content {
                padding: 40px 30px;
            }
            .section {
                margin-bottom: 30px;
            }
            .section h2 {
                color: #333;
                margin-bottom: 15px;
                font-size: 1.5em;
                border-left: 4px solid #667eea;
                padding-left: 15px;
            }
            .endpoint {
                background: #f8f9fa;
                border-left: 4px solid #667eea;
                padding: 15px 20px;
                margin: 10px 0;
                border-radius: 4px;
                transition: all 0.3s;
            }
            .endpoint:hover {
                background: #e9ecef;
                transform: translateX(5px);
            }
            .endpoint strong {
                color: #667eea;
                display: block;
                margin-bottom: 5px;
            }
            .endpoint code {
                background: white;
                padding: 2px 8px;
                border-radius: 3px;
                font-family: 'Courier New', monospace;
                color: #e83e8c;
            }
            .stats {
                display: grid;
                grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
                gap: 20px;
                margin-top: 20px;
            }
            .stat-card {
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                color: white;
                padding: 20px;
                border-radius: 8px;
                text-align: center;
            }
            .stat-card h3 {
                font-size: 2em;
                margin-bottom: 5px;
            }
            .stat-card p {
                opacity: 0.9;
                font-size: 0.9em;
            }
            .features {
                display: grid;
                grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
                gap: 15px;
                margin-top: 20px;
            }
            .feature {
                background: #f8f9fa;
                padding: 20px;
                border-radius: 8px;
                border-left: 4px solid #28a745;
            }
            .feature h3 {
                color: #28a745;
                margin-bottom: 10px;
                font-size: 1.1em;
            }
            .button {
                display: inline-block;
                background: #667eea;
                color: white;
                padding: 12px 30px;
                text-decoration: none;
                border-radius: 6px;
                margin: 10px 10px 10px 0;
                transition: all 0.3s;
                font-weight: 600;
            }
            .button:hover {
                background: #764ba2;
                transform: translateY(-2px);
                box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
            }
            .footer {
                background: #f8f9fa;
                padding: 20px;
                text-align: center;
                color: #6c757d;
                font-size: 0.9em;
            }
            .badge {
                display: inline-block;
                background: #28a745;
                color: white;
                padding: 4px 12px;
                border-radius: 12px;
                font-size: 0.85em;
                margin-left: 10px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                <h1>๐Ÿšจ OpenOps</h1>
                <p style="font-size: 1.2em; margin-top: 10px;">AI Incident Commander Environment</p>
                <div class="status">โœ… Status: Running</div>
            </div>
            
            <div class="content">
                <div class="section">
                    <h2>๐Ÿ“Š Performance Metrics</h2>
                    <div class="stats">
                        <div class="stat-card">
                            <h3>0.85</h3>
                            <p>Task 1 Score<br>(Easy)</p>
                        </div>
                        <div class="stat-card">
                            <h3>0.85</h3>
                            <p>Task 2 Score<br>(Medium)</p>
                        </div>
                        <div class="stat-card">
                            <h3>1.00</h3>
                            <p>Task 3 Score<br>(Hard)</p>
                        </div>
                        <div class="stat-card">
                            <h3>0.90</h3>
                            <p>Average Score<br>โญ</p>
                        </div>
                    </div>
                </div>

                <div class="section">
                    <h2>๐ŸŽฏ Environment Overview</h2>
                    <p style="line-height: 1.8; color: #555;">
                        OpenOps simulates realistic production incidents where AI agents must investigate alerts, 
                        identify root causes, execute mitigation actions, communicate with stakeholders, 
                        and resolve incidents to minimize revenue loss.
                    </p>
                </div>

                <div class="section">
                    <h2>โœจ Key Features</h2>
                    <div class="features">
                        <div class="feature">
                            <h3>๐Ÿ” Smart Detection</h3>
                            <p>Intelligent incident classification based on alerts and logs</p>
                        </div>
                        <div class="feature">
                            <h3>๐Ÿ“ˆ Progressive Difficulty</h3>
                            <p>Three tasks from simple crashes to cascading failures</p>
                        </div>
                        <div class="feature">
                            <h3>โšก Rule-Based Agent</h3>
                            <p>Reliable baseline with optimized playbooks</p>
                        </div>
                        <div class="feature">
                            <h3>๐ŸŽฎ 21 Actions</h3>
                            <p>Investigate, mitigate, communicate, and resolve</p>
                        </div>
                    </div>
                </div>

                <div class="section">
                    <h2>๐Ÿ”Œ API Endpoints</h2>
                    
                    <div class="endpoint">
                        <strong>API Documentation</strong>
                        <code>GET /docs</code>
                        <p style="margin-top: 8px; color: #666;">Interactive Swagger UI with all endpoints</p>
                    </div>
                    
                    <div class="endpoint">
                        <strong>Reset Environment</strong>
                        <code>POST /reset?task_id=1</code>
                        <p style="margin-top: 8px; color: #666;">Initialize a new incident scenario (task_id: 1, 2, or 3)</p>
                    </div>
                    
                    <div class="endpoint">
                        <strong>Execute Action</strong>
                        <code>POST /step</code>
                        <p style="margin-top: 8px; color: #666;">Take an action and receive observation</p>
                    </div>
                    
                    <div class="endpoint">
                        <strong>Get State</strong>
                        <code>GET /state</code>
                        <p style="margin-top: 8px; color: #666;">Retrieve current environment state</p>
                    </div>
                    
                    <div class="endpoint">
                        <strong>List Actions</strong>
                        <code>GET /actions</code>
                        <p style="margin-top: 8px; color: #666;">Get all available actions</p>
                    </div>
                </div>

                <div class="section">
                    <h2>๐Ÿš€ Quick Start</h2>
                    <a href="/docs" class="button">๐Ÿ“š View API Docs</a>
                    <a href="https://github.com/arya89/OpenOps" class="button">๐Ÿ’ป GitHub</a>
                    <a href="https://huggingface.co/spaces/arya89/openops-incident-commander" class="button">๐Ÿค— HuggingFace</a>
                </div>

                <div class="section">
                    <h2>๐Ÿ“– Three Progressive Tasks</h2>
                    <div class="endpoint">
                        <strong>Task 1: Simple API Crash <span class="badge">Easy</span></strong>
                        <p style="margin-top: 8px; color: #666;">API service down due to OOM โ†’ Inspect logs โ†’ Restart โ†’ Resolve</p>
                    </div>
                    <div class="endpoint">
                        <strong>Task 2: Bad Deployment <span class="badge">Medium</span></strong>
                        <p style="margin-top: 8px; color: #666;">Database deployment broke queries โ†’ Rollback โ†’ Notify team โ†’ Resolve</p>
                    </div>
                    <div class="endpoint">
                        <strong>Task 3: Cascading Failure <span class="badge">Hard</span></strong>
                        <p style="margin-top: 8px; color: #666;">DB overload โ†’ API timeouts โ†’ Scale DB โ†’ Restart API โ†’ Communicate โ†’ Resolve</p>
                    </div>
                </div>
            </div>
            
            <div class="footer">
                <p><strong>OpenOps v1.0.0</strong></p>
                <p>Built for OpenEnv AI Hackathon 2026 | Meta PyTorch x Scaler</p>
                <p style="margin-top: 10px;">Created by arya89 | Production-ready incident management environment</p>
            </div>
        </div>
    </body>
    </html>
    """


@app.get("/health")
async def health():
    """Detailed health check."""
    return {
        "status": "healthy",
        "environment_loaded": env_instance is not None,
        "current_task": env_instance.task_id if env_instance else None,
        "version": "1.0.0"
    }


# ============================================================
# ENVIRONMENT API ENDPOINTS
# ============================================================

@app.post("/reset")
async def reset(task_id: int = Query(default=1, ge=1, le=3)) -> Dict[str, Any]:
    """
    Reset the environment for a specific task.
    OpenEnv standard endpoint.
    
    Args:
        task_id: Task difficulty (1=easy, 2=medium, 3=hard)
    
    Returns:
        Initial observation after reset
    """
    global env_instance
    
    try:
        # Create new environment instance
        env_instance = MyEnvEnvironment()
        obs = env_instance.reset(task_id=task_id)
        
        # Return observation in OpenEnv format
        return obs.model_dump()
        
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"Failed to reset environment: {str(e)}"
        )


@app.post("/step")
async def step(request: StepRequest) -> Dict[str, Any]:
    """
    Execute an action in the environment.
    
    Args:
        request: StepRequest with action_id and task_id
    
    Returns:
        Observation after taking the action
    """
    global env_instance
    
    try:
        # Check if environment is initialized
        if env_instance is None:
            raise HTTPException(
                status_code=400,
                detail="Environment not initialized. Call /reset first."
            )
        
        # Validate action_id
        if request.action_id < 0 or request.action_id > 20:
            raise HTTPException(
                status_code=400,
                detail=f"Invalid action_id: {request.action_id}. Must be 0-20."
            )
        
        # Create action
        action = IncidentAction(
            action_id=request.action_id,
            task_id=request.task_id if request.task_id else env_instance.task_id
        )
        
        # Execute step
        obs = env_instance.step(action)
        
        # Return observation in OpenEnv format
        return obs.model_dump()
        
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"Failed to execute step: {str(e)}"
        )


@app.get("/state")
async def get_state() -> Dict[str, Any]:
    """
    Get current environment state.
    
    Returns:
        Current state of the environment
    """
    global env_instance
    
    try:
        if env_instance is None:
            raise HTTPException(
                status_code=400,
                detail="Environment not initialized. Call /reset first."
            )
        
        state = env_instance.state
        
        return {
            "state": state.model_dump() if hasattr(state, 'model_dump') else state,
            "task_id": env_instance.task_id,
            "total_reward": env_instance.total_reward,
            "incident_resolved": env_instance.incident_resolved,
            "time_elapsed": env_instance.time_elapsed
        }
        
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"Failed to get state: {str(e)}"
        )


@app.get("/actions")
async def get_actions() -> Dict[str, Any]:
    """
    Get list of available actions.
    
    Returns:
        Dictionary of action IDs and names
    """
    try:
        temp_env = MyEnvEnvironment()
        
        return {
            "actions": temp_env.ACTION_NAMES,
            "total_actions": len(temp_env.ACTION_NAMES)
        }
        
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"Failed to get actions: {str(e)}"
        )


# ============================================================
# ERROR HANDLERS
# ============================================================

@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
    """Handle HTTP exceptions."""
    return JSONResponse(
        status_code=exc.status_code,
        content={
            "error": exc.detail,
            "status_code": exc.status_code
        }
    )


@app.exception_handler(Exception)
async def general_exception_handler(request, exc):
    """Handle general exceptions."""
    return JSONResponse(
        status_code=500,
        content={
            "error": "Internal server error",
            "detail": str(exc)
        }
    )


# ============================================================
# MAIN ENTRY POINT (Required by OpenEnv)
# ============================================================

def main():
    """
    Main entry point for the server.
    Required by OpenEnv validation.
    """
    uvicorn.run(
        app,
        host="0.0.0.0",
        port=7860,
        log_level="info"
    )


if __name__ == "__main__":
    main()