File size: 15,174 Bytes
4405139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326bff5
 
4405139
580b044
4405139
 
ac3a96a
 
 
 
 
 
 
 
4405139
 
 
 
 
326bff5
c91027d
4405139
326bff5
4405139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3502ad
4405139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3502ad
 
 
4405139
 
 
 
a3502ad
4405139
 
 
 
 
a3502ad
4405139
 
 
 
a3502ad
4405139
 
 
 
a3502ad
4405139
 
 
 
a3502ad
4405139
 
 
 
 
326bff5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4405139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96ac176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326bff5
 
4405139
a3502ad
4405139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326bff5
4405139
a3502ad
580b044
 
4405139
 
a3502ad
4405139
 
a3502ad
326bff5
4405139
 
 
326bff5
 
 
4405139
 
 
 
 
a3502ad
4405139
96ac176
 
 
 
4405139
 
 
 
 
 
 
 
326bff5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4405139
326bff5
 
 
 
 
 
 
 
 
 
 
 
 
 
4405139
 
 
 
 
 
 
 
 
504eb62
4405139
 
 
 
 
 
 
a3502ad
 
 
4405139
a3502ad
4405139
 
a3502ad
 
4405139
 
a3502ad
 
4405139
a3502ad
326bff5
 
 
 
 
4405139
 
 
 
326bff5
4405139
 
 
 
326bff5
 
4405139
 
 
 
 
ac3a96a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4405139
ac3a96a
 
4405139
883600a
c0710ef
4405139
580b044
 
4405139
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Gradio frontend for remotely controlling an ECS-hosted OpAgent worker.

Environment variables:
    OPAGENT_API_BASE_URL   ECS API base URL, e.g. https://your-ecs-host:8000
    OPAGENT_API_KEY        Optional API key sent as X-API-Key
"""

from __future__ import annotations

from functools import lru_cache
import json
import os
import time
from io import BytesIO
from typing import Any, Dict, Generator, List, Optional, Tuple
from urllib.parse import urlparse, urlunparse

import gradio as gr
import requests
from PIL import Image
import subprocess
import sys
from alibabacloud_ecs20140526.client import Client as EcsClient
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_ecs20140526 import models as ecs_models

DEFAULT_API_BASE_URL = os.getenv("OPAGENT_API_BASE_URL")
DEFAULT_API_KEY = os.getenv("OPAGENT_API_KEY")
DEFAULT_POLL_INTERVAL = float(os.getenv("OPAGENT_POLL_INTERVAL", "5"))
DEFAULT_TIMEOUT = float(os.getenv("OPAGENT_HTTP_TIMEOUT", "300"))
DEFAULT_SUBMIT_TIMEOUT = float(os.getenv("OPAGENT_SUBMIT_TIMEOUT", str(DEFAULT_TIMEOUT)))
DEFAULT_STATUS_TIMEOUT = float(os.getenv("OPAGENT_STATUS_TIMEOUT", str(DEFAULT_TIMEOUT)))
DEFAULT_IMAGE_TIMEOUT = float(os.getenv("OPAGENT_IMAGE_TIMEOUT", str(DEFAULT_TIMEOUT)))
DEFAULT_START_URL = os.getenv("OPAGENT_DEFAULT_START_URL", "https://www.amazon.com")
DEFAULT_QUERY = os.getenv("OPAGENT_DEFAULT_QUERY", "find a physical PS5 copy of NBA 2K25")
USE_SYSTEM_PROXY = os.getenv("OPAGENT_USE_SYSTEM_PROXY", "0") == "1"
EMPTY_HISTORY_MARKDOWN = "### Step History\n_No steps yet._"


@lru_cache(maxsize=1)
def get_http_session() -> requests.Session:
    session = requests.Session()
    if not USE_SYSTEM_PROXY:
        session.trust_env = False
        session.proxies.clear()
    return session


def build_headers(api_key: str) -> Dict[str, str]:
    headers = {"Content-Type": "application/json"}
    if api_key.strip():
        headers["X-API-Key"] = api_key.strip()
    return headers


def normalize_base_url(base_url: str) -> str:
    base_url = base_url.strip().rstrip("/")
    if not base_url:
        raise ValueError("API base URL cannot be empty")
    return base_url


def fetch_image(image_url: Optional[str], api_key: str) -> Optional[Image.Image]:
    if not image_url:
        return None

    response = get_http_session().get(
        image_url,
        headers={"X-API-Key": api_key.strip()} if api_key.strip() else None,
        timeout=DEFAULT_IMAGE_TIMEOUT,
    )
    response.raise_for_status()
    return Image.open(BytesIO(response.content)).convert("RGB")


def format_status(task: Dict[str, Any]) -> str:
    lines = [
        f"### Task Status: {task.get('status', 'unknown')}",
        f"- Current Step: {task.get('current_step', 0)} / {task.get('max_steps', 0)}",
        f"- Current Page: {task.get('current_url') or '-'}",
    ]

    latest_action = task.get("latest_action")
    if latest_action:
        lines.append("- Latest Action:")
        lines.append(f"\n```json\n{json.dumps(latest_action, ensure_ascii=False)}\n```")

    latest_think = task.get("latest_think")
    if latest_think:
        preview = latest_think[:500] + ("..." if len(latest_think) > 500 else "")
        lines.append("- Latest Reasoning:")
        lines.append(f"\n> {preview}")

    final_answer = task.get("final_answer")
    if final_answer:
        lines.append("\n### Final Answer")
        lines.append(final_answer)

    error = task.get("error")
    if error:
        lines.append("\n### Error")
        lines.append(error)

    events = task.get("events") or []
    if events:
        lines.append("\n### Recent Logs")
        lines.extend([f"- {event}" for event in events[-12:]])

    return "\n".join(lines)


def fetch_trajectory(api_base_url: str, api_key: str, task_id: str) -> Dict[str, Any]:
    response = get_http_session().get(
        f"{normalize_base_url(api_base_url)}/tasks/{task_id}/trajectory",
        headers=build_headers(api_key),
        timeout=DEFAULT_STATUS_TIMEOUT,
    )
    response.raise_for_status()
    return response.json()


def build_artifact_url(trajectory_url: Optional[str], artifact_path: Optional[str]) -> Optional[str]:
    if not trajectory_url or not artifact_path:
        return None

    parsed = urlparse(trajectory_url)
    trajectory_dir = parsed.path.rsplit("/", 1)[0]
    artifact = os.path.basename(artifact_path)
    artifact_dir = os.path.basename(os.path.dirname(artifact_path))
    artifact_url_path = f"{trajectory_dir}/{artifact_dir}/{artifact}"
    return urlunparse(parsed._replace(path=artifact_url_path, query="", fragment=""))


def format_history_markdown(steps: List[Dict[str, Any]]) -> str:
    if not steps:
        return EMPTY_HISTORY_MARKDOWN

    lines = ["### Step History"]
    for step in steps:
        step_no = step.get("step", "-")
        action = step.get("action_type", "unknown")
        url = step.get("url") or "-"
        params = step.get("params") or {}
        error = step.get("error") or ""

        lines.append(f"#### Step {step_no} · {action}")
        lines.append(f"- URL: {url}")
        lines.append(f"- Params: `{json.dumps(params, ensure_ascii=False)}`")
        if error:
            lines.append(f"- Error: {error}")

    return "\n".join(lines)


def build_history_from_trajectory(
    api_key: str,
    trajectory_url: Optional[str],
    trajectory: Dict[str, Any],
) -> Tuple[List[Tuple[Image.Image, str]], str]:
    steps = trajectory.get("steps") or []
    gallery_items: List[Tuple[Image.Image, str]] = []

    for step in steps:
        image_url = build_artifact_url(
            trajectory_url,
            step.get("annotated_screenshot") or step.get("screenshot"),
        )
        if not image_url:
            continue
        try:
            image = fetch_image(image_url, api_key)
        except Exception:
            continue
        if image is None:
            continue

        caption = f"Step {step.get('step', '-')}: {step.get('action_type', 'unknown')}"
        gallery_items.append((image, caption))

    return gallery_items, format_history_markdown(steps)


def submit_task(
    api_base_url: str,
    api_key: str,
    start_url: str,
    query: str,
    max_steps: int,
) -> Dict[str, Any]:
    payload = {
        "url": start_url.strip() or None,
        "query": query.strip(),
        "max_steps": int(max_steps),
    }
    response = get_http_session().post(
        f"{normalize_base_url(api_base_url)}/tasks",
        headers=build_headers(api_key),
        json=payload,
        timeout=DEFAULT_SUBMIT_TIMEOUT,
    )
    response.raise_for_status()
    return response.json()


def get_task_status(api_base_url: str, api_key: str, task_id: str) -> Dict[str, Any]:
    response = get_http_session().get(
        f"{normalize_base_url(api_base_url)}/tasks/{task_id}",
        headers=build_headers(api_key),
        timeout=DEFAULT_STATUS_TIMEOUT,
    )
    response.raise_for_status()
    return response.json()


def send_heartbeat(api_base_url: str, api_key: str, task_id: str) -> None:
    """Notify the ECS server that the frontend is still alive.

    Swallows all errors so a transient network blip never kills the UI loop.
    """
    try:
        get_http_session().post(
            f"{normalize_base_url(api_base_url)}/tasks/{task_id}/heartbeat",
            headers=build_headers(api_key),
            timeout=5,
        )
    except Exception:
        pass


def cancel_remote_task(api_base_url: str, api_key: str, task_state: Dict[str, Any]) -> Tuple[str, Dict[str, Any]]:
    task_id = (task_state or {}).get("task_id")
    if not task_id:
        raise gr.Error("There is no running task to cancel")

    response = get_http_session().post(
        f"{normalize_base_url(api_base_url)}/tasks/{task_id}/cancel",
        headers=build_headers(api_key),
        timeout=DEFAULT_STATUS_TIMEOUT,
    )
    response.raise_for_status()
    cancel_result = response.json()

    task = get_task_status(api_base_url, api_key, task_id)
    task.setdefault("events", [])
    if cancel_result.get("message"):
        task["events"].append(cancel_result["message"])
    return format_status(task), task


def run_remote_task(
    api_base_url: str,
    api_key: str,
    start_url: str,
    query: str,
    max_steps: int,
    poll_interval: float,
) -> Generator[Tuple[str, Optional[Image.Image], str, Dict[str, Any], List[Tuple[Image.Image, str]], str], None, None]:
    if not query.strip():
        raise gr.Error("Task description cannot be empty")

    try:
        created = submit_task(api_base_url, api_key, start_url, query, max_steps)
    except Exception as exc:
        raise gr.Error(f"Failed to submit task: {exc}") from exc

    task_id = created["task_id"]
    intro = f"### Task Submitted\n- Task ID: `{task_id}`\n- Status URL: {created.get('status_url', '-')}"
    yield intro, None, "", {"task_id": task_id, "status": "queued"}, [], EMPTY_HISTORY_MARKDOWN

    last_image_url = None
    last_image = None
    history_gallery: List[Tuple[Image.Image, str]] = []
    recorded_steps = set()
    history_steps: List[Dict[str, Any]] = []

    while True:
        try:
            task = get_task_status(api_base_url, api_key, task_id)
        except Exception as exc:
            raise gr.Error(f"Failed to query task status: {exc}") from exc

        # Keep the ECS heartbeat watchdog satisfied so it doesn't auto-cancel
        # this task if it thinks the frontend has disconnected.
        send_heartbeat(api_base_url, api_key, task_id)

        image_url = task.get("display_image_url") or task.get("latest_annotated_screenshot_url") or task.get("latest_screenshot_url")
        if image_url and image_url != last_image_url:
            try:
                last_image = fetch_image(image_url, api_key)
                last_image_url = image_url
            except Exception:
                pass

        current_step = int(task.get("current_step") or 0)
        latest_action = task.get("latest_action") or {}
        if current_step > 0 and latest_action and current_step not in recorded_steps:
            history_steps.append(
                {
                    "step": current_step,
                    "action_type": latest_action.get("action_type", "unknown"),
                    "url": task.get("current_url") or "-",
                    "params": latest_action,
                    "error": task.get("error", ""),
                }
            )
            if last_image is not None:
                history_gallery.append(
                    (last_image.copy(), f"Step {current_step}: {latest_action.get('action_type', 'unknown')}")
                )
            recorded_steps.add(current_step)

        final_answer = task.get("final_answer", "")
        current_history_markdown = format_history_markdown(history_steps)

        if task.get("status") in {"done", "error", "cancelled"}:
            try:
                trajectory = fetch_trajectory(api_base_url, api_key, task_id)
                history_gallery, current_history_markdown = build_history_from_trajectory(
                    api_key,
                    task.get("trajectory_url"),
                    trajectory,
                )
            except Exception:
                pass

        yield format_status(task), last_image, final_answer, task, history_gallery, current_history_markdown

        if task.get("status") in {"done", "error", "cancelled"}:
            break

        time.sleep(max(0.5, float(poll_interval)))


def build_demo() -> gr.Blocks:
    with gr.Blocks(title="OpAgent Remote Demo") as demo:
        gr.Markdown("# OpAgent Remote Demo\nHuggingface frontend for remote execution on ECS.")

        # with gr.Row():
        #     api_base_url = gr.Textbox(label="ECS API Base URL", value=DEFAULT_API_BASE_URL)
        api_base_url = gr.State(DEFAULT_API_BASE_URL)
        api_key = gr.State(DEFAULT_API_KEY)

        with gr.Row():
            start_url = gr.Textbox(label="Start URL", value=DEFAULT_START_URL, placeholder="https://www.bilibili.com")
            max_steps = gr.Slider(label="Max Steps", minimum=5, maximum=100, value=30, step=1)
            poll_interval = gr.Slider(label="Polling Interval (seconds)", minimum=1, maximum=10, value=DEFAULT_POLL_INTERVAL, step=1)

        query = gr.Textbox(label="Task Description", value=DEFAULT_QUERY, lines=4, placeholder="Describe what you want the browser agent to do")

        with gr.Row():
            submit_button = gr.Button("Submit Task", variant="primary")
            cancel_button = gr.Button("Stop Task", variant="stop")

        with gr.Row():
            status_markdown = gr.Markdown(label="Status")
            latest_image = gr.Image(label="Execution Screenshot", type="pil")

        final_answer = gr.Textbox(label="Final Answer", lines=6)
        task_state = gr.State({})

        gr.Markdown("### Historical Steps")
        history_gallery = gr.Gallery(label="Step Screenshots", columns=4, height=360, preview=True)
        history_markdown = gr.Markdown(EMPTY_HISTORY_MARKDOWN)

        submit_button.click(
            fn=run_remote_task,
            inputs=[api_base_url, api_key, start_url, query, max_steps, poll_interval],
            outputs=[status_markdown, latest_image, final_answer, task_state, history_gallery, history_markdown],
        )

        cancel_button.click(
            fn=cancel_remote_task,
            inputs=[api_base_url, api_key, task_state],
            outputs=[status_markdown, task_state],
        )

    return demo


def get_my_public_ip():
    try:
        # 使用多个接口尝试获取出口 IP
        for url in ['https://ifconfig.me', 'https://icanhazip.com']:
            ip = subprocess.check_output(f"curl -s -m 5 {url}", shell=True, text=True).strip()
            if ip: return ip
    except:
        pass
    return None

def update_security_group(ip):
    ak = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID')
    sk = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET')
    sg_id = os.environ.get('SG_ID')
    
    
    config = open_api_models.Config(access_key_id=ak, access_key_secret=sk, endpoint='ecs.cn-hangzhou.aliyuncs.com')
    client = EcsClient(config)
    
    print(f"[*] 自动配置安全组,允许 IP: {ip}")
    for port in [8000, 8080]:
        try:
            req = ecs_models.AuthorizeSecurityGroupRequest(
                region_id="cn-hangzhou",
                security_group_id=sg_id,
                ip_protocol='tcp',
                port_range=f'{port}/{port}',
                source_cidr_ip=f'{ip}/32',
                description='Auto-opened for HF Space'
            )
            client.authorize_security_group(req)
            print(f"[+] 端口 {port} 已对 {ip} 放行")
        except Exception as e:
            if 'RuleAlreadyExist' not in str(e):
                print(f"[!] 规则配置异常: {e}")

def main() -> None:
    my_ip = get_my_public_ip()
    update_security_group(my_ip)
    demo = build_demo()
    demo.queue(default_concurrency_limit=1)
    demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))


if __name__ == "__main__":
    main()