Przeglądaj źródła

support frontend visualization on server

guantao 4 dni temu
rodzic
commit
0bbfee3aa3

+ 0 - 8
.env.template

@@ -1,8 +0,0 @@
-# 完成配置后,将 .env.template 重命名为 .env
-
-
-# OpenRouter API Key,用于sonnet-4.6模型
-OPEN_ROUTER_API_KEY=
-
-# BrowserUse API Key
-BROWSER_USE_API_KEY=

+ 6 - 6
docs/README.md

@@ -336,27 +336,27 @@ agent 工具的合成结果对齐正常返回值格式(含 `sub_trace_id` 字
 
 ```bash
 # 新建
-curl -X POST http://localhost:8000/api/traces \
+curl -X POST http://43.106.118.91:8000/api/traces \
   -H "Content-Type: application/json" \
   -d '{"messages": [{"role": "user", "content": "分析项目架构"}], "model": "gpt-4o"}'
 
 # 续跑(after_sequence 为 null 或省略)
-curl -X POST http://localhost:8000/api/traces/{trace_id}/run \
+curl -X POST http://43.106.118.91:8000/api/traces/{trace_id}/run \
   -d '{"messages": [{"role": "user", "content": "继续深入分析"}]}'
 
 # 回溯:从 sequence 5 处截断,插入新消息重新执行
-curl -X POST http://localhost:8000/api/traces/{trace_id}/run \
+curl -X POST http://43.106.118.91:8000/api/traces/{trace_id}/run \
   -d '{"after_sequence": 5, "messages": [{"role": "user", "content": "换一个方案"}]}'
 
 # 重新生成:回溯到 sequence 5,不插入新消息,直接重跑
-curl -X POST http://localhost:8000/api/traces/{trace_id}/run \
+curl -X POST http://43.106.118.91:8000/api/traces/{trace_id}/run \
   -d '{"after_sequence": 5, "messages": []}'
 
 # 停止
-curl -X POST http://localhost:8000/api/traces/{trace_id}/stop
+curl -X POST http://43.106.118.91:8000/api/traces/{trace_id}/stop
 
 # 反思:追加反思 prompt 运行,结果追加到 experiences 文件
-curl -X POST http://localhost:8000/api/traces/{trace_id}/reflect \
+curl -X POST http://43.106.118.91:8000/api/traces/{trace_id}/reflect \
   -d '{"focus": "为什么第三步选择了错误的方案"}'
 ```
 

+ 1 - 1
docs/trace-api.md

@@ -304,7 +304,7 @@ GET /api/experiences
 ### 连接
 
 ```
-ws://localhost:8000/api/traces/{trace_id}/watch?since_event_id=0
+ws://43.106.118.91:8000/api/traces/{trace_id}/watch?since_event_id=0
 ```
 
 ### 事件类型

+ 1 - 1
examples/analyze_story/run.py

@@ -562,7 +562,7 @@ async def main():
         print("   python3 api_server.py")
         print()
         print("2. 浏览器访问:")
-        print("   http://localhost:8000/api/traces")
+        print("   http://43.106.118.91:8000/api/traces")
         print()
         print(f"3. Trace ID: {current_trace_id}")
         print("=" * 60)

+ 1 - 1
examples/deep_research/run.py

@@ -571,7 +571,7 @@ async def main():
         print("   python3 api_server.py")
         print()
         print("2. 浏览器访问:")
-        print("   http://localhost:8000/api/traces")
+        print("   http://43.106.118.91:8000/api/traces")
         print()
         print(f"3. Trace ID: {current_trace_id}")
         print("=" * 60)

+ 1 - 1
examples/feature_extract/run.py

@@ -174,7 +174,7 @@ async def main():
     print("   python3 api_server.py")
     print()
     print("2. 浏览器访问:")
-    print("   http://localhost:8000/api/traces")
+    print("   http://43.106.118.91:8000/api/traces")
     print()
     print(f"3. Trace ID: {current_trace_id}")
     print("=" * 60)

+ 1 - 1
examples/how/run.py

@@ -600,7 +600,7 @@ async def main():
         print("   python3 api_server.py")
         print()
         print("2. 浏览器访问:")
-        print("   http://localhost:8000/api/traces")
+        print("   http://43.106.118.91:8000/api/traces")
         print()
         print(f"3. Trace ID: {current_trace_id}")
         print("=" * 60)

+ 15 - 2
examples/restore/run.py

@@ -19,6 +19,10 @@ import select
 import asyncio
 from pathlib import Path
 
+# ===== 浏览器模式配置 =====
+# 可选值: "cloud" (云浏览器) 或 "local" (本地浏览器)
+BROWSER_TYPE = "cloud"  # 修改这里来切换浏览器模式
+HEADLESS = False  # 是否无头模式运行
 # Clash Verge TUN 模式兼容:禁止 httpx/urllib 自动检测系统 HTTP 代理
 # TUN 虚拟网卡已在网络层接管所有流量,不需要应用层再走 HTTP 代理,
 # 否则 httpx 检测到 macOS 系统代理 (127.0.0.1:7897) 会导致 ConnectError
@@ -33,6 +37,7 @@ load_dotenv()
 from agent.llm.prompts import SimplePrompt
 from agent.core.runner import AgentRunner, RunConfig
 from agent.core.presets import AgentPreset, register_preset
+from agent.tools.builtin.browser.baseClass import init_browser_session, kill_browser_session
 from agent.trace import (
     FileSystemTraceStore,
     Trace,
@@ -344,7 +349,15 @@ async def main():
     # 加载自定义工具
     print("   - 加载自定义工具: nanobanana")
     import examples.how.tool  # 导入自定义工具模块,触发 @tool 装饰器注册
-
+    # 3. 初始化浏览器会话
+    browser_mode_name = "云浏览器" if BROWSER_TYPE == "cloud" else "本地浏览器"
+    print(f"🌐 正在初始化{browser_mode_name}...")
+    await init_browser_session(
+        browser_type=BROWSER_TYPE,
+        headless=HEADLESS,
+        url="about:blank"
+    )
+    print(f"✅ {browser_mode_name}初始化完成\n")
     store = FileSystemTraceStore(base_path=".trace")
     runner = AgentRunner(
         trace_store=store,
@@ -614,7 +627,7 @@ async def main():
         print("   python3 api_server.py")
         print()
         print("2. 浏览器访问:")
-        print("   http://localhost:8000/api/traces")
+        print("   http://43.106.118.91:8000/api/traces")
         print()
         print(f"3. Trace ID: {current_trace_id}")
         print("=" * 60)

+ 1 - 1
examples/restore_old/run.py

@@ -612,7 +612,7 @@ async def main():
         print("   python3 api_server.py")
         print()
         print("2. 浏览器访问:")
-        print("   http://localhost:8000/api/traces")
+        print("   http://43.106.118.91:8000/api/traces")
         print()
         print(f"3. Trace ID: {current_trace_id}")
         print("=" * 60)

+ 1 - 1
examples/tool_research/run.py

@@ -488,7 +488,7 @@ async def main():
         print("   python3 api_server.py")
         print()
         print("2. 浏览器访问:")
-        print("   http://localhost:8000/api/traces")
+        print("   http://43.106.118.91:8000/api/traces")
         print()
         print(f"3. Trace ID: {current_trace_id}")
         print("=" * 60)

+ 3 - 3
frontend/API.md

@@ -55,7 +55,7 @@
 
 ### 基础信息
 
-- **Base URL**: `http://localhost:8000`
+- **Base URL**: `http://43.106.118.91:8000`
 - **Content-Type**: `application/json`
 
 ---
@@ -396,7 +396,7 @@ GET /api/traces/abc123.A/messages?goal_id=2
 
 ```javascript
 const ws = new WebSocket(
-  'ws://localhost:8000/api/traces/{trace_id}/watch?since_event_id=0'
+  'ws://43.106.118.91:8000/api/traces/{trace_id}/watch?since_event_id=0'
 )
 ```
 
@@ -1150,7 +1150,7 @@ let lastEventId = 0
 
 function connect(traceId) {
   const ws = new WebSocket(
-    `ws://localhost:8000/api/traces/${traceId}/watch?since_event_id=${lastEventId}`
+    `ws://43.106.118.91:8000/api/traces/${traceId}/watch?since_event_id=${lastEventId}`
   )
 
   ws.onmessage = (event) => {

+ 6 - 6
frontend/htmlTemplate/templateData.py

@@ -19,7 +19,7 @@ msgGroups: Dict[str, List[Dict[str, Any]]] = {}
 
 
 def generate_trace_list(
-    base_url: str = "http://localhost:8000",
+    base_url: str = "http://43.106.118.91:8000",
     status: Optional[str] = None,
     mode: Optional[str] = None,
     limit: int = 20,
@@ -36,7 +36,7 @@ def generate_trace_list(
 
 
 def generate_goal_list(
-    trace_id: str = "trace_001", base_url: str = "http://localhost:8000"
+    trace_id: str = "trace_001", base_url: str = "http://43.106.118.91:8000"
 ) -> Dict[str, Any]:
     url = f"{base_url.rstrip('/')}/api/traces/{trace_id}"
     response = httpx.get(url, timeout=10.0)
@@ -45,7 +45,7 @@ def generate_goal_list(
 
 
 def generate_subgoal_list(
-    sub_trace_id: str, base_url: str = "http://localhost:8000"
+    sub_trace_id: str, base_url: str = "http://43.106.118.91:8000"
 ) -> Dict[str, Any]:
     url = f"{base_url.rstrip('/')}/api/traces/{sub_trace_id}"
     response = httpx.get(url, timeout=10.0)
@@ -54,7 +54,7 @@ def generate_subgoal_list(
 
 
 def generate_messages_list(
-    trace_id: str, goal_id: Optional[str] = None, base_url: str = "http://localhost:8000"
+    trace_id: str, goal_id: Optional[str] = None, base_url: str = "http://43.106.118.91:8000"
 ) -> Dict[str, Any]:
     url = f"{base_url.rstrip('/')}/api/traces/{trace_id}/messages"
     params = {}
@@ -154,7 +154,7 @@ def generate_mock_branch_detail(trace_id: str = "trace_001", branch_id: str = "b
 
 
 async def _fetch_ws_connected_event(trace_id: str, since_event_id: int = 0, ws_url: Optional[str] = None) -> Dict[str, Any]:
-    url = ws_url or f"ws://localhost:8000/api/traces/{trace_id}/watch?since_event_id={since_event_id}"
+    url = ws_url or f"ws://43.106.118.91:8000/api/traces/{trace_id}/watch?since_event_id={since_event_id}"
     async with websockets.connect(url) as ws:
         while True:
             raw_message = await ws.recv()
@@ -234,7 +234,7 @@ def _append_event_jsonl(event_data: Dict[str, Any], mock_dir: str):
 
 
 async def _watch_ws_events(trace_id: str, since_event_id: int = 0, ws_url: Optional[str] = None):
-    url = ws_url or f"ws://localhost:8000/api/traces/{trace_id}/watch?since_event_id={since_event_id}"
+    url = ws_url or f"ws://43.106.118.91:8000/api/traces/{trace_id}/watch?since_event_id={since_event_id}"
     mock_dir = os.path.join(os.path.dirname(__file__), "ws_data")
     os.makedirs(mock_dir, exist_ok=True)
     while True:

+ 2 - 2
frontend/htmlTemplate/需求.md

@@ -253,7 +253,7 @@ preview: string | null;
 
 ### 5.2 WebSocket 实时通信
 
-**连接地址**: `ws://localhost:8000/api/traces/{trace_id}/watch?since_event_id=0`
+**连接地址**: `ws://43.106.118.91:8000/api/traces/{trace_id}/watch?since_event_id=0`
 
 **查询参数**:
 | 参数 | 类型 | 默认值 | 说明 |
@@ -380,7 +380,7 @@ last_message: any;
 2. 顶部 title 显示 `trace.task`
 3. 调用 `GET /api/traces/{trace_id}` 获取 GoalTree 数据
 4. 根据 `goal_tree.goals` 渲染流程节点
-5. 建立 WebSocket 连接 `ws://localhost:8000/api/traces/{trace_id}/watch` 进行实时更新
+5. 建立 WebSocket 连接 `ws://43.106.118.91:8000/api/traces/{trace_id}/watch` 进行实时更新
 
 **节点交互流程**:
 

+ 3 - 3
frontend/react-template/package.json

@@ -4,9 +4,9 @@
   "version": "0.1.0",
   "type": "module",
   "scripts": {
-    "dev": "vite",
+    "dev": "vite --host 0.0.0.0 --port 3000",
     "build": "tsc && vite build",
-    "preview": "vite preview",
+    "preview": "vite preview --host 0.0.0.0",
     "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0"
   },
   "dependencies": {
@@ -42,4 +42,4 @@
     "vite": "^5.0.8",
     "vitest": "^4.0.18"
   }
-}
+}

+ 4 - 4
frontend/react-template/react.md

@@ -190,7 +190,7 @@ interface DetailPanelState {
 
 ### 5.1 HTTP 接口
 
-**Base URL**: `http://localhost:8000`
+**Base URL**: `http://43.106.118.91:8000`
 
 #### 5.1.1 获取 Trace 列表
 
@@ -271,7 +271,7 @@ interface MessagesResponse {
 
 ### 5.2 WebSocket 实时通信
 
-**连接地址**: `ws://localhost:8000/api/traces/{trace_id}/watch?since_event_id=0`
+**连接地址**: `ws://43.106.118.91:8000/api/traces/{trace_id}/watch?since_event_id=0`
 
 **事件类型**:
 
@@ -397,7 +397,7 @@ export const messageApi = {
 import axios from "axios";
 
 export const client = axios.create({
-  baseURL: "http://localhost:8000",
+  baseURL: "http://43.106.118.91:8000",
   timeout: 10000,
   headers: {
     "Content-Type": "application/json",
@@ -552,7 +552,7 @@ export const useWebSocket = (traceId: string | null, options: UseWebSocketOption
   useEffect(() => {
     if (!traceId) return;
 
-    const url = `ws://localhost:8000/api/traces/${traceId}/watch?since_event_id=0`;
+    const url = `ws://43.106.118.91:8000/api/traces/${traceId}/watch?since_event_id=0`;
     const ws = new WebSocket(url);
 
     ws.onopen = () => {

+ 1 - 1
frontend/react-template/src/api/client.ts

@@ -2,7 +2,7 @@ import axios from "axios";
 import { Toast } from "@douyinfe/semi-ui";
 
 // Determine base URL from environment variables, or fallback to default
-const DEFAULT_BASE_URL = "http://localhost:8000";
+const DEFAULT_BASE_URL = "http://43.106.118.91:8000";
 
 const getBaseUrl = () => {
   const winConfig =

+ 1 - 1
frontend/react-template/src/components/Terminal/Terminal.tsx

@@ -22,7 +22,7 @@ export const Terminal: FC<TerminalProps> = ({ onClose }) => {
 
   useEffect(() => {
     // 连接WebSocket
-    const ws = new WebSocket("ws://localhost:8000/api/logs/watch");
+    const ws = new WebSocket("ws://43.106.118.91:8000/api/logs/watch");
     wsRef.current = ws;
 
     ws.onopen = () => {

+ 1 - 1
frontend/react-template/src/hooks/useWebSocket.ts

@@ -21,7 +21,7 @@ export const useWebSocket = (traceId: string | null, options: UseWebSocketOption
         : undefined) ||
       (typeof import.meta !== "undefined" && import.meta.env && import.meta.env.VITE_API_BASE_URL
         ? import.meta.env.VITE_API_BASE_URL
-        : "http://localhost:8000");
+        : "http://43.106.118.91:8000");
 
     const wsBase = httpBase.replace(/^http(s?):\/\//, "ws$1://").replace(/\/+$/, "");
     const url = `${wsBase}/api/traces/${traceId}/watch?since_event_id=${sinceEventId}`;

+ 1 - 1
frontend/react-template/vite.config.ts

@@ -15,7 +15,7 @@ export default defineConfig({
     port: 3000,
     proxy: {
       "/api": {
-        target: "http://localhost:8000",
+        target: "http://43.106.118.91:8000",
         changeOrigin: true,
       },
     },

+ 2 - 2
frontend/test_api.py

@@ -28,8 +28,8 @@ except ImportError:
 
 
 # 配置
-API_BASE = "http://localhost:8000"
-WS_BASE = "ws://localhost:8000"
+API_BASE = "http://43.106.118.91:8000"
+WS_BASE = "ws://43.106.118.91:8000"
 
 
 class TestResult: