File size: 5,690 Bytes
50bf486
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
FastAPI server for the SQL Debug Environment.

Hooks into openenv-core's create_fastapi_app() for the standard /ws endpoint,
then slaps on a landing page and health check on top. Nothing fancy.
"""

from __future__ import annotations

import logging

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse

from openenv.core.env_server import create_fastapi_app

from server.environment import SQLDebugEnvironment
from sql_debug_env.models import SQLAction, SQLObservation

logger = logging.getLogger("sql-debug-env")
logging.basicConfig(level=logging.INFO)


# ── Wire up the OpenEnv app ────────────────────────────────────────

app: FastAPI = create_fastapi_app(
    env=SQLDebugEnvironment,
    action_cls=SQLAction,
    observation_cls=SQLObservation,
)

app.title = "SQL Debug Environment"
app.description = (
    "OpenEnv-compatible environment for SQL debugging and optimization. "
    "Agents fix broken queries through iterative execution feedback."
)
app.version = "1.0.0"
app.contact = {"name": "SQL Debug Env Team"}
app.license_info = {"name": "MIT", "url": "https://opensource.org/licenses/MIT"}

# Wide open CORS β€” this is a dev/hackathon tool, not a bank
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


# ── Landing page ────────────────────────────────────────────────────
#
# Just a quick info card so you know it's alive when you hit the root URL.
# Nothing too wild β€” dark theme, lists the tasks, links to the docs.

LANDING_HTML = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SQL Debug Environment</title>
<style>
  *{margin:0;padding:0;box-sizing:border-box}
  body{font-family:'Segoe UI',system-ui,-apple-system,sans-serif;
       background:#0f172a;color:#e2e8f0;min-height:100vh;
       display:flex;align-items:center;justify-content:center}
  .card{background:#1e293b;border-radius:16px;padding:48px;
        max-width:720px;width:90%;box-shadow:0 25px 50px rgba(0,0,0,.4)}
  h1{font-size:2rem;margin-bottom:8px;
     background:linear-gradient(135deg,#38bdf8,#818cf8);
     -webkit-background-clip:text;-webkit-text-fill-color:transparent}
  .subtitle{color:#94a3b8;margin-bottom:32px;font-size:1.05rem}
  .tasks{display:grid;gap:12px;margin-bottom:32px}
  .task{background:#334155;border-radius:10px;padding:16px 20px;
        display:flex;align-items:center;gap:14px;
        transition:transform .15s}
  .task:hover{transform:translateX(4px)}
  .badge{padding:4px 10px;border-radius:6px;font-size:.75rem;
         font-weight:700;text-transform:uppercase;flex-shrink:0}
  .easy{background:#166534;color:#4ade80}
  .medium{background:#854d0e;color:#facc15}
  .hard{background:#991b1b;color:#f87171}
  .task-name{font-weight:600}
  .task-id{color:#64748b;font-size:.85rem;font-family:monospace}
  .endpoints{margin-bottom:28px}
  .endpoints h2{font-size:1rem;color:#94a3b8;margin-bottom:12px}
  .ep{font-family:monospace;font-size:.9rem;padding:6px 0;
      display:flex;gap:10px;align-items:center}
  .method{padding:2px 8px;border-radius:4px;font-weight:700;font-size:.75rem}
  .post{background:#1d4ed8;color:#bfdbfe}
  .get{background:#15803d;color:#bbf7d0}
  .ws{background:#7e22ce;color:#e9d5ff}
  a{color:#38bdf8;text-decoration:none}
  a:hover{text-decoration:underline}
  .footer{color:#475569;font-size:.85rem;margin-top:24px;
          padding-top:20px;border-top:1px solid #334155}
</style>
</head>
<body>
<div class="card">
  <h1>πŸ› οΈ SQL Debug Environment</h1>
  <p class="subtitle">Interactive SQL debugging &amp; optimization β€” powered by OpenEnv</p>

  <div class="tasks">
    <div class="task">
      <span class="badge easy">Easy</span>
      <div><span class="task-name">Fix SQL Syntax Errors</span><br>
           <span class="task-id">fix_syntax</span></div>
    </div>
    <div class="task">
      <span class="badge medium">Medium</span>
      <div><span class="task-name">Fix Wrong JOIN Logic</span><br>
           <span class="task-id">fix_join</span></div>
    </div>
    <div class="task">
      <span class="badge hard">Hard</span>
      <div><span class="task-name">Optimize Correlated Subquery</span><br>
           <span class="task-id">optimize_query</span></div>
    </div>
  </div>

  <div class="endpoints">
    <h2>Endpoints</h2>
    <div class="ep"><span class="method ws">WS</span> <span>/ws</span> β€” Main interaction (reset / step / state)</div>
    <div class="ep"><span class="method get">GET</span> <span>/health</span> β€” Health check</div>
  </div>

  <p>πŸ“– <a href="/docs">Swagger Docs</a> &nbsp;|&nbsp;
     πŸ“‹ <a href="/redoc">ReDoc</a></p>

  <div class="footer">
    Built with OpenEnv Β· FastAPI Β· SQLite
  </div>
</div>
</body>
</html>
"""


@app.get("/", response_class=HTMLResponse)
async def root():
    """Serve the landing page."""
    return LANDING_HTML


@app.get("/health")
async def health():
    """Quick health check β€” just confirms the server is up."""
    return {"status": "ok", "environment": "sql-debug-env"}


# ── Entry point ─────────────────────────────────────────────────────

def main():
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)


if __name__ == "__main__":
    main()