File size: 10,062 Bytes
4719066
2305b9f
 
 
 
 
 
 
 
 
 
 
 
 
4719066
 
2305b9f
 
4719066
2305b9f
 
4719066
2305b9f
 
4719066
 
2305b9f
 
 
 
 
 
 
4719066
 
2305b9f
 
 
 
 
 
4719066
 
2305b9f
 
 
 
 
 
 
4719066
 
2305b9f
 
 
 
4719066
2305b9f
4719066
 
 
2305b9f
4719066
 
 
 
 
 
2305b9f
4719066
 
 
 
2305b9f
4719066
 
 
 
 
2305b9f
4719066
2305b9f
 
 
 
4719066
2305b9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4719066
 
 
 
 
 
 
 
 
 
2305b9f
 
 
4719066
2305b9f
4719066
 
 
 
2305b9f
 
 
 
 
 
 
 
 
 
 
 
 
 
4719066
 
 
 
 
2305b9f
 
 
 
 
 
 
 
 
4719066
 
 
 
 
 
2305b9f
4719066
2305b9f
 
 
4719066
 
 
 
 
 
 
2305b9f
 
 
dd5113f
4719066
 
 
2305b9f
 
dd5113f
 
 
 
 
 
 
4719066
 
 
2305b9f
4719066
 
 
 
 
2305b9f
4719066
 
 
 
 
2305b9f
 
 
4719066
 
 
2305b9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4719066
 
 
2305b9f
4719066
 
 
2305b9f
4719066
 
 
 
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
"""
FastAPI application β€” OrgOS OpenEnv HTTP API.

Endpoints (OpenEnv-compatible):
  GET  /health       β€” liveness probe
  GET  /metadata     β€” env description
  GET  /schema       β€” action / observation schema
  POST /reset        β€” start new episode
  POST /step         β€” take one action
  GET  /state        β€” current episode metadata
  POST /state        β€” same (backward compat)
  GET  /schema/apps  β€” per-app operation catalogue (used by UI)
  GET  /             β€” serve the demo dashboard UI
  GET  /ui/run-agent β€” SSE stream of one inference episode (for UI)
"""

import json
import os
from typing import Any, Dict, Optional

import uvicorn
from fastapi import Body, FastAPI, HTTPException
from fastapi.responses import FileResponse, HTMLResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel

from models import OrgOSAction, OrgOSObservation, OrgOSState
from server.environment import OrgOSEnvironment


# ------------------------------------------------------------------
# App setup
# ------------------------------------------------------------------

app = FastAPI(
    title="OrgOS β€” Multi-App Enterprise RL Environment",
    description=(
        "A Salesforce + Zendesk + Jira + Workday simulator for training agents "
        "that handle real enterprise workflows under schema drift and policy changes."
    ),
    version="2.0.0",
)

# Mount static assets (JS, CSS) if the ui/ directory exists
_UI_STATIC = os.path.join(os.path.dirname(__file__), "..", "ui", "static")
if os.path.isdir(_UI_STATIC):
    app.mount("/static", StaticFiles(directory=_UI_STATIC), name="static")

# Single shared environment instance (stateful per-process)
env = OrgOSEnvironment()


# ------------------------------------------------------------------
# Request / response helpers
# ------------------------------------------------------------------

class ResetRequest(BaseModel):
    workflow_id: Optional[str] = None   # "A", "B", "C", or None for round-robin


class StepResponse(BaseModel):
    observation: OrgOSObservation
    reward: float
    done: bool
    info: dict = {}


# ------------------------------------------------------------------
# Core OpenEnv routes
# ------------------------------------------------------------------

@app.get("/health")
def health():
    return {"status": "healthy", "env": "orgos", "version": "2.0.0"}


@app.get("/metadata")
def metadata():
    return {
        "name":        "orgos-openenv",
        "description": (
            "OrgOS: multi-app enterprise RL environment. "
            "The agent completes cross-app business workflows (triage, onboarding, churn) "
            "across Jira, Zendesk, Salesforce, and Workday simulators. "
            "Schema drift and policy changes challenge the agent to generalise."
        ),
        "version": "2.0.0",
        "tags": ["openenv", "enterprise", "multi-app", "schema-drift", "rl"],
        "workflows": [
            {
                "id":         "A",
                "name":       "Customer Bug Fix",
                "difficulty": "medium",
                "apps":       ["zendesk", "jira", "salesforce", "workday"],
            },
            {
                "id":         "B",
                "name":       "Employee Onboarding",
                "difficulty": "medium",
                "apps":       ["workday", "salesforce", "zendesk"],
            },
            {
                "id":         "C",
                "name":       "Churn Risk Alert",
                "difficulty": "hard",
                "apps":       ["salesforce", "zendesk", "jira"],
            },
        ],
    }


@app.get("/schema")
def schema():
    return {
        "action": {
            "type": "object",
            "properties": {
                "app":       {"type": "string", "enum": ["jira", "zendesk", "salesforce", "workday"]},
                "operation": {"type": "string", "description": "App-specific operation name"},
                "args":      {"type": "object", "description": "Operation arguments"},
            },
            "required": ["app", "operation"],
        },
        "observation": {
            "type": "object",
            "properties": {
                "done":            {"type": "boolean"},
                "reward":          {"type": "number"},
                "current_score":   {"type": "number"},
                "workflow_id":     {"type": "string"},
                "step_count":      {"type": "integer"},
                "app_states":      {"type": "object"},
                "workflow_goal":   {"type": "string"},
                "completed_steps": {"type": "array"},
                "pending_steps":   {"type": "array"},
                "schema_hints":    {"type": "object"},
                "active_rules":    {"type": "object"},
                "rule_violations": {"type": "array"},
                "reward_breakdown":{"type": "object"},
                "message":         {"type": "string"},
            },
        },
        "state": {
            "type": "object",
            "properties": {
                "episode_id":           {"type": "string"},
                "workflow_id":          {"type": "string"},
                "schema_versions":      {"type": "object"},
                "step_count":           {"type": "integer"},
                "max_steps":            {"type": "integer"},
                "rule_violation_count": {"type": "integer"},
                "workflow_completion":  {"type": "number"},
                "rule_compliance_rate": {"type": "number"},
                "policy_drift_active":  {"type": "boolean"},
            },
        },
    }


@app.post("/reset", response_model=StepResponse)
def reset(req: ResetRequest = Body(default=ResetRequest())):
    try:
        obs = env.reset(workflow_id=req.workflow_id)
    except (ValueError, KeyError) as exc:
        raise HTTPException(status_code=400, detail=str(exc))
    return StepResponse(observation=obs, reward=obs.reward, done=False)


@app.post("/step", response_model=StepResponse)
async def step(body: Dict[str, Any] = Body(...)):
    """
    Accept both openenv-core wrapped format:
        {"action": {"app": "...", "operation": "...", "args": {...}}, "timeout_s": 15}
    and direct format:
        {"app": "...", "operation": "...", "args": {...}}
    Invalid or empty actions return a -0.05 penalty observation rather than HTTP 400.
    """
    action_data = body.get("action", body)
    try:
        action = OrgOSAction(**action_data)
        obs    = env.step(action)
    except Exception as exc:
        # Return a graceful error observation so the inference loop can continue
        obs = env._build_obs(
            reward=-0.05,
            done=False,
            message=f"Invalid action format: {exc}. Use {{\"app\": \"...\", \"operation\": \"...\", \"args\": {{...}}}}",
        )
    return StepResponse(observation=obs, reward=obs.reward, done=obs.done)


@app.get("/state", response_model=OrgOSState)
def state_get():
    """GET /state β€” openenv-core spec."""
    return env.state()


@app.post("/state", response_model=OrgOSState)
def state_post():
    """POST /state β€” backward compatibility."""
    return env.state()


# ------------------------------------------------------------------
# UI helper routes
# ------------------------------------------------------------------

@app.get("/schema/apps")
def app_schemas():
    """Return per-app operation catalogue. Used by the dashboard UI."""
    from server.apps.jira import JiraApp
    from server.apps.zendesk import ZendeskApp
    from server.apps.salesforce import SalesforceApp
    from server.apps.workday import WorkdayApp
    return {
        "jira":       {"operations": JiraApp.OPERATIONS},
        "zendesk":    {"operations": ZendeskApp.OPERATIONS},
        "salesforce": {"operations": SalesforceApp.OPERATIONS},
        "workday":    {"operations": WorkdayApp.OPERATIONS},
    }


@app.get("/ui/run-agent")
async def run_agent_sse(workflow_id: str = "A", model: str = "gpt-4o-mini"):
    """
    Server-Sent Events stream.
    Runs one inference episode and streams step events to the UI.
    Each event is: data: <json>\n\n
    """
    import asyncio

    async def _event_stream():
        import json as _json
        from inference import run_workflow_generator
        try:
            async for event in run_workflow_generator(workflow_id=workflow_id, env_ref=env):
                yield f"data: {_json.dumps(event)}\n\n"
                await asyncio.sleep(0)   # yield control
        except Exception as exc:
            yield f"data: {_json.dumps({'type': 'error', 'message': str(exc)})}\n\n"
        yield "data: {\"type\": \"done\"}\n\n"

    return StreamingResponse(
        _event_stream(),
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "X-Accel-Buffering": "no",
        },
    )


@app.get("/", response_class=HTMLResponse)
def ui():
    """Serve the OrgOS demo dashboard."""
    ui_path = os.path.join(os.path.dirname(__file__), "..", "ui", "index.html")
    if os.path.exists(ui_path):
        return FileResponse(ui_path, media_type="text/html")
    # Minimal inline fallback if ui/ hasn't been built yet
    return HTMLResponse(content="""
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>OrgOS Dashboard</title>
<style>body{font-family:monospace;background:#0f172a;color:#94a3b8;padding:2rem}
h1{color:#38bdf8}a{color:#38bdf8}</style></head>
<body>
<h1>OrgOS β€” Enterprise RL Environment</h1>
<p>The full dashboard UI is at <code>ui/index.html</code>.</p>
<p>API docs: <a href="/docs">/docs</a> &nbsp;|&nbsp;
   Health: <a href="/health">/health</a></p>
</body></html>
""")


# ------------------------------------------------------------------
# Entry point
# ------------------------------------------------------------------

def main():
    uvicorn.run("server.app:app", host="0.0.0.0", port=8000, reload=False)


if __name__ == "__main__":
    main()