diff --git a/src/openclaw_extra/web/app.py b/src/openclaw_extra/web/app.py index dd3ae41..50c6fd2 100644 --- a/src/openclaw_extra/web/app.py +++ b/src/openclaw_extra/web/app.py @@ -46,6 +46,74 @@ def get_client() -> OpenClawClient: # ============== API 路由 ============== +@app.get("/api/config") +async def api_config(): + """获取 OpenClaw 配置信息""" + try: + client = get_client() + config = client.get_config() + + # 提取关键信息 + agents_config = config.get("agents", {}) + defaults = agents_config.get("defaults", {}) + + # 查找所有智能体目录 + agents_list = agents_config.get("list", []) + agent_dirs = [] + for agent in agents_list: + if agent.get("agentDir"): + agent_dirs.append(agent.get("agentDir")) + + return { + "config_path": config.get("_path", "/home/openclaw/.openclaw/openclaw.json"), + "workspace_root": defaults.get("workspace", "/home/openclaw/.openclaw/workspace"), + "agents_dir": "/home/openclaw/.openclaw", # OpenClaw 根目录 + "agents_count": len(agents_list), + "agent_dirs": agent_dirs, + } + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + + +@app.post("/api/rescan") +async def api_rescan(): + """重新扫描智能体目录""" + try: + import subprocess + import os + + # 调用 openclaw gateway 命令重新加载配置 + result = subprocess.run( + ["openclaw", "gateway", "call", "agents.list", "--json"], + capture_output=True, + text=True, + timeout=10, + ) + + if result.returncode != 0: + raise Exception(f"扫描失败: {result.stderr}") + + # 解析结果 + import json + output_lines = result.stdout.strip().split("\n") + json_line = [l for l in output_lines if l.startswith("{") or l.startswith("[")] + + if json_line: + data = json.loads(json_line[-1]) + agents = data.get("agents", []) + + return { + "success": True, + "agents_count": len(agents), + "message": f"扫描完成,找到 {len(agents)} 个智能体", + } + + + return {"success": True, "agents_count": 0, "message": "扫描完成"} + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + + @app.get("/api/health") async def api_health(): """健康检查""" diff --git a/src/openclaw_extra/web/static/index.html b/src/openclaw_extra/web/static/index.html index 930ff15..7bdb01c 100644 --- a/src/openclaw_extra/web/static/index.html +++ b/src/openclaw_extra/web/static/index.html @@ -18,8 +18,15 @@

🤖 OpenClaw Extra

智能体管理面板

+
+ 根目录: + +
@@ -31,6 +38,13 @@ + +
+ + + +
+
@@ -104,14 +118,19 @@ loading: true, error: null, gatewayOk: false, + config: null, + rescanMessage: '', get totalSessions() { return this.agents.reduce((sum, a) => sum + a.sessions_count, 0) }, async init() { - await this.loadAgents() - await this.checkHealth() + await Promise.all([ + this.loadAgents(), + this.loadConfig(), + this.checkHealth() + ]) }, async loadAgents() { @@ -126,6 +145,33 @@ } }, + async loadConfig() { + try { + const res = await fetch('/api/config') + this.config = await res.json() + } catch (e) { + console.error('Load config error:', e) + } + }, + + async rescan() { + try { + this.rescanMessage = '正在扫描...' + const res = await fetch('/api/rescan', { method: 'POST' }) + const data = await res.json() + + if (data.success) { + this.rescanMessage = data.message + // 重新加载智能体列表 + await this.loadAgents() + } else { + this.error = data.message || '扫描失败' + } + } catch (e) { + this.error = '扫描失败: ' + e.message + } + }, + async checkHealth() { try { const res = await fetch('/api/health')