File size: 4,479 Bytes
f676764
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
559fdd1
f676764
 
 
 
 
 
 
 
 
 
 
 
 
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
"""FastAPI app for the Developer Control Room benchmark."""

from __future__ import annotations

import json
import os
import subprocess
import sys
from pathlib import Path

from fastapi import HTTPException

from openenv.core.env_server.web_interface import create_web_interface_app

try:
    from ..graders import grade
    from ..models import DeveloperControlRoomAction, DeveloperControlRoomObservation
    from ..tasks import ALL_TASKS, list_tasks
    from .developer_control_room_environment import DeveloperControlRoomEnvironment
    from .ui import build_developer_control_room_ui
except ImportError:
    from graders import grade
    from models import DeveloperControlRoomAction, DeveloperControlRoomObservation
    from tasks import ALL_TASKS, list_tasks
    from server.developer_control_room_environment import DeveloperControlRoomEnvironment
    from server.ui import build_developer_control_room_ui

ROOT = Path(__file__).resolve().parents[1]
ENV = DeveloperControlRoomEnvironment()


def _get_env() -> DeveloperControlRoomEnvironment:
    return ENV


app = create_web_interface_app(
    _get_env,
    DeveloperControlRoomAction,
    DeveloperControlRoomObservation,
    env_name="developer_control_room",
    max_concurrent_envs=1,
    gradio_builder=build_developer_control_room_ui,
)


@app.get("/tasks")
def tasks() -> dict:
    return {
        "tasks": list_tasks(),
        "total": len(ALL_TASKS),
        "action_schema": {
            "search_workspace": {"query": "string"},
            "read_file": {"path": "string"},
            "inspect_schema": {"asset": "string"},
            "inspect_lineage": {"asset": "string"},
            "inspect_llm_draft": {"draft_id": "string"},
            "edit_file": {"path": "string", "content": "string"},
            "run_validator": {"validator": "string"},
            "submit_repair": {
                "root_cause": "string",
                "fix_path": "string",
                "summary": "string",
            },
            "submit_review": {
                "verdict": "approve|reject",
                "issue_type": "string",
                "summary": "string",
            },
            "submit_workspace": {"summary": "string"},
        },
    }


@app.get("/grader")
def grader() -> dict:
    state = ENV.state.model_dump()
    if not state.get("task_id"):
        raise HTTPException(status_code=400, detail="No active episode. Call /reset first.")
    result = grade(state["task_id"], state, ENV._scenario)
    return {
        "task_id": state["task_id"],
        "scenario_id": state.get("scenario_id"),
        "steps_used": state.get("step_count", 0),
        "done": state.get("done", False),
        "total": result["total"],
        "solved": result.get("solved", False),
        "breakdown": result["breakdown"],
        "feedback": result["feedback"],
    }


@app.post("/baseline")
def baseline() -> dict:
    script = ROOT / "inference.py"
    if not script.exists():
        raise HTTPException(status_code=500, detail="inference.py not found")
    env = {
        **os.environ,
        "ENV_URL": os.environ.get("ENV_URL", "http://localhost:7860"),
    }
    try:
        result = subprocess.run(
            [sys.executable, str(script)],
            cwd=ROOT,
            capture_output=True,
            text=True,
            timeout=1200,
            env=env,
            check=False,
        )
    except subprocess.TimeoutExpired as exc:
        raise HTTPException(status_code=500, detail=f"inference.py timed out: {exc}") from exc

    if result.returncode != 0:
        raise HTTPException(
            status_code=500,
            detail={
                "returncode": result.returncode,
                "stdout_tail": result.stdout.splitlines()[-20:],
                "stderr_tail": result.stderr.splitlines()[-20:],
            },
        )

    return {
        "status": "completed",
        "stdout_tail": result.stdout.splitlines()[-20:],
        "stderr_tail": result.stderr.splitlines()[-20:],
    }


@app.get("/api/info")
def api_info() -> dict:
    return {
        "name": "developer_control_room",
        "version": "0.1.0",
        "description": "FlowOS end-to-end benchmark for production AI agents",
        "tasks": [task["id"] for task in list_tasks()],
        "docs": "/docs",
    }


def main(host: str = "0.0.0.0", port: int = 7860) -> None:
    import uvicorn

    uvicorn.run(app, host=host, port=port)


if __name__ == "__main__":
    main()