AdithyaSK HF Staff commited on
Commit
698641d
·
verified ·
1 Parent(s): 1a3a8ee

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. server/app.py +4 -3
  2. server/gradio_ui.py +41 -21
server/app.py CHANGED
@@ -55,10 +55,11 @@ def _custom_gradio_builder(
55
  ):
56
  """Callback invoked by ``create_app`` to build our custom Gradio UI.
57
 
58
- The ``web_manager`` lets the UI drive rollouts through the same
59
- ``CallToolAction`` pipeline as MCP clients; we ignore the other hints.
 
60
  """
61
- return opencode_ui_builder(web_manager=web_manager)
62
 
63
 
64
  # Enable OpenEnv's built-in Gradio mounting at the standard /web path.
 
55
  ):
56
  """Callback invoked by ``create_app`` to build our custom Gradio UI.
57
 
58
+ We ignore ``web_manager`` (its public API is ``reset_environment`` /
59
+ ``step_environment`` / ``connect_websocket`` not an env instance) and
60
+ hand the UI the env class directly, matching e2b_desktop's pattern.
61
  """
62
+ return opencode_ui_builder(env_factory=OpenCodeEnvironment)
63
 
64
 
65
  # Enable OpenEnv's built-in Gradio mounting at the standard /web path.
server/gradio_ui.py CHANGED
@@ -58,16 +58,29 @@ _EXAMPLE_VLLM_URLS = [
58
 
59
  def opencode_ui_builder(
60
  *,
61
- web_manager: Any,
 
62
  title: str = "OpenCode Env",
63
  **_: Any,
64
  ) -> gr.Blocks:
65
- """Build the Gradio Blocks UI bound to ``web_manager``.
66
 
67
- The web manager is a thin wrapper around ``OpenCodeEnvironment``
68
- calling ``call_tool("run_rollout", ...)`` on it drives one rollout.
 
69
  """
70
 
 
 
 
 
 
 
 
 
 
 
 
71
  with gr.Blocks(title=title, analytics_enabled=False) as demo:
72
  gr.Markdown(f"# {title}\nRun one OpenCode rollout against any OpenAI-compatible endpoint.")
73
 
@@ -168,25 +181,32 @@ def opencode_ui_builder(
168
  setup_shell_v: str,
169
  ):
170
  try:
171
- env = web_manager.get_environment()
 
 
172
  env.reset()
173
- result_raw = env.call_tool(
174
- "run_rollout",
175
- vllm_url=vllm_url_v,
176
- model=model_v,
177
- instruction=instruction_v,
178
- test_script=test_script_v,
179
- task_id=task_id_v,
180
- setup_shell=setup_shell_v,
181
- upload_files={},
182
- provider=provider_v,
183
- api_key=api_key_v,
184
- mode=mode_v,
185
- disable_thinking=bool(disable_thinking_v),
186
- max_tokens_cap=int(max_tokens_cap_v),
187
- agent_timeout_s=float(agent_timeout_s_v),
 
 
 
 
 
188
  )
189
- result = _parse_result(result_raw)
190
  except Exception as exc:
191
  msg = f"**Error:** `{type(exc).__name__}: {exc}`"
192
  return (msg, None, None, None, None, "", [], "", "", {"error": str(exc)})
 
58
 
59
  def opencode_ui_builder(
60
  *,
61
+ env_factory: Any = None,
62
+ web_manager: Any = None, # kept for backward compat; unused
63
  title: str = "OpenCode Env",
64
  **_: Any,
65
  ) -> gr.Blocks:
66
+ """Build the Gradio Blocks UI.
67
 
68
+ ``env_factory`` is a zero-arg callable that returns a fresh
69
+ :class:`OpenCodeEnvironment` on first click (lazy, so the Space's
70
+ cold-start path doesn't pay the E2B cost until someone hits Run).
71
  """
72
 
73
+ _env_cache: dict[str, Any] = {"instance": None}
74
+
75
+ def _get_env():
76
+ inst = _env_cache.get("instance")
77
+ if inst is None:
78
+ if env_factory is None:
79
+ raise RuntimeError("opencode_ui_builder needs env_factory=...")
80
+ inst = env_factory()
81
+ _env_cache["instance"] = inst
82
+ return inst
83
+
84
  with gr.Blocks(title=title, analytics_enabled=False) as demo:
85
  gr.Markdown(f"# {title}\nRun one OpenCode rollout against any OpenAI-compatible endpoint.")
86
 
 
181
  setup_shell_v: str,
182
  ):
183
  try:
184
+ from openenv.core.env_server.mcp_types import CallToolAction
185
+
186
+ env = _get_env()
187
  env.reset()
188
+ obs = env.step(
189
+ CallToolAction(
190
+ tool_name="run_rollout",
191
+ arguments={
192
+ "vllm_url": vllm_url_v,
193
+ "model": model_v,
194
+ "instruction": instruction_v,
195
+ "test_script": test_script_v,
196
+ "task_id": task_id_v,
197
+ "setup_shell": setup_shell_v,
198
+ "upload_files": {},
199
+ "provider": provider_v,
200
+ "api_key": api_key_v,
201
+ "mode": mode_v,
202
+ "disable_thinking": bool(disable_thinking_v),
203
+ "max_tokens_cap": int(max_tokens_cap_v),
204
+ "agent_timeout_s": float(agent_timeout_s_v),
205
+ },
206
+ ),
207
+ timeout_s=float(agent_timeout_s_v) + 300,
208
  )
209
+ result = _parse_result(obs)
210
  except Exception as exc:
211
  msg = f"**Error:** `{type(exc).__name__}: {exc}`"
212
  return (msg, None, None, None, None, "", [], "", "", {"error": str(exc)})