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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
OpenCLAW Web Server + Dashboard
|
| 3 |
+
================================
|
| 4 |
+
Lightweight Flask app for Render.com deployment.
|
| 5 |
+
Serves as health endpoint + agent dashboard + webhook receiver.
|
| 6 |
+
"""
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
import json
|
| 10 |
+
import threading
|
| 11 |
+
import time
|
| 12 |
+
from datetime import datetime, timezone
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
|
| 15 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 16 |
+
|
| 17 |
+
from flask import Flask, jsonify, render_template_string
|
| 18 |
+
from core.config import Config
|
| 19 |
+
from core.agent import OpenCLAWAgent, AgentState
|
| 20 |
+
|
| 21 |
+
app = Flask(__name__)
|
| 22 |
+
STATE_DIR = Path(os.getenv("STATE_DIR", "state"))
|
| 23 |
+
|
| 24 |
+
# Background agent thread
|
| 25 |
+
agent_thread = None
|
| 26 |
+
agent_running = False
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def run_agent_loop():
|
| 30 |
+
"""Background thread running the agent."""
|
| 31 |
+
global agent_running
|
| 32 |
+
interval = int(os.getenv("DAEMON_INTERVAL", "3600")) # 1 hour default
|
| 33 |
+
|
| 34 |
+
while agent_running:
|
| 35 |
+
try:
|
| 36 |
+
config = Config.from_env()
|
| 37 |
+
agent = OpenCLAWAgent(config)
|
| 38 |
+
agent.run_cycle()
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Agent cycle error: {e}")
|
| 41 |
+
|
| 42 |
+
# Sleep in small chunks for graceful shutdown
|
| 43 |
+
for _ in range(interval):
|
| 44 |
+
if not agent_running:
|
| 45 |
+
break
|
| 46 |
+
time.sleep(1)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
DASHBOARD_HTML = """
|
| 50 |
+
<!DOCTYPE html>
|
| 51 |
+
<html>
|
| 52 |
+
<head>
|
| 53 |
+
<title>OpenCLAW Agent Dashboard</title>
|
| 54 |
+
<meta charset="utf-8">
|
| 55 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 56 |
+
<style>
|
| 57 |
+
body { font-family: 'Courier New', monospace; background: #0a0a0a; color: #00ff41; margin: 0; padding: 20px; }
|
| 58 |
+
.container { max-width: 800px; margin: 0 auto; }
|
| 59 |
+
h1 { color: #00ff41; border-bottom: 2px solid #00ff41; padding-bottom: 10px; }
|
| 60 |
+
.card { background: #111; border: 1px solid #00ff41; border-radius: 8px; padding: 16px; margin: 12px 0; }
|
| 61 |
+
.stat { display: inline-block; margin: 8px 16px; }
|
| 62 |
+
.stat .value { font-size: 24px; font-weight: bold; }
|
| 63 |
+
.stat .label { font-size: 12px; color: #666; }
|
| 64 |
+
.status-ok { color: #00ff41; }
|
| 65 |
+
.status-err { color: #ff4141; }
|
| 66 |
+
.log { background: #050505; padding: 12px; border-radius: 4px; max-height: 300px; overflow-y: auto; font-size: 12px; }
|
| 67 |
+
a { color: #41b0ff; }
|
| 68 |
+
</style>
|
| 69 |
+
</head>
|
| 70 |
+
<body>
|
| 71 |
+
<div class="container">
|
| 72 |
+
<h1>π€ OpenCLAW Autonomous Agent</h1>
|
| 73 |
+
<p>Advanced AI Systems Laboratory β Madrid, Spain</p>
|
| 74 |
+
|
| 75 |
+
<div class="card">
|
| 76 |
+
<h3>π Agent Status</h3>
|
| 77 |
+
<div class="stat"><div class="value">{{ status.cycle_count }}</div><div class="label">Cycles</div></div>
|
| 78 |
+
<div class="stat"><div class="value">{{ status.posts_created }}</div><div class="label">Posts</div></div>
|
| 79 |
+
<div class="stat"><div class="value">{{ status.engagement_count }}</div><div class="label">Engagements</div></div>
|
| 80 |
+
<div class="stat"><div class="value">{{ status.papers_posted }}</div><div class="label">Papers Shared</div></div>
|
| 81 |
+
</div>
|
| 82 |
+
|
| 83 |
+
<div class="card">
|
| 84 |
+
<h3>π§ Services</h3>
|
| 85 |
+
<p>{% for s in status.services %}<span class="status-ok">β
{{ s }}</span> {% endfor %}</p>
|
| 86 |
+
<p>LLM: <span class="{{ 'status-ok' if status.llm_available else 'status-err' }}">
|
| 87 |
+
{{ 'β
Online' if status.llm_available else 'β οΈ Offline' }}</span></p>
|
| 88 |
+
</div>
|
| 89 |
+
|
| 90 |
+
<div class="card">
|
| 91 |
+
<h3>π Links</h3>
|
| 92 |
+
<p><a href="https://github.com/Agnuxo1">GitHub</a> |
|
| 93 |
+
<a href="https://www.moltbook.com/u/OpenCLAW-Neuromorphic">Moltbook</a> |
|
| 94 |
+
<a href="https://scholar.google.com/citations?user=6nOpJ9IAAAAJ">Scholar</a> |
|
| 95 |
+
<a href="https://arxiv.org/search/cs?searchtype=author&query=de+Lafuente,+F+A">ArXiv</a></p>
|
| 96 |
+
</div>
|
| 97 |
+
|
| 98 |
+
<div class="card">
|
| 99 |
+
<h3>π Last Cycle</h3>
|
| 100 |
+
<div class="log"><pre>{{ last_cycle }}</pre></div>
|
| 101 |
+
</div>
|
| 102 |
+
</div>
|
| 103 |
+
</body>
|
| 104 |
+
</html>
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
@app.route("/")
|
| 109 |
+
def dashboard():
|
| 110 |
+
"""Dashboard page."""
|
| 111 |
+
config = Config.from_env()
|
| 112 |
+
agent = OpenCLAWAgent(config)
|
| 113 |
+
status = agent.get_status()
|
| 114 |
+
|
| 115 |
+
last_cycle = "{}"
|
| 116 |
+
lc_file = STATE_DIR / "last_cycle.json"
|
| 117 |
+
if lc_file.exists():
|
| 118 |
+
last_cycle = json.dumps(json.loads(lc_file.read_text()), indent=2)
|
| 119 |
+
|
| 120 |
+
return render_template_string(DASHBOARD_HTML, status=status, last_cycle=last_cycle)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
@app.route("/health")
|
| 124 |
+
def health():
|
| 125 |
+
"""Health check endpoint."""
|
| 126 |
+
return jsonify({
|
| 127 |
+
"status": "healthy",
|
| 128 |
+
"agent": "OpenCLAW-Neuromorphic",
|
| 129 |
+
"timestamp": datetime.now(timezone.utc).isoformat(),
|
| 130 |
+
})
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
@app.route("/status")
|
| 134 |
+
def status():
|
| 135 |
+
"""JSON status endpoint."""
|
| 136 |
+
config = Config.from_env()
|
| 137 |
+
agent = OpenCLAWAgent(config)
|
| 138 |
+
return jsonify(agent.get_status())
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
@app.route("/trigger", methods=["POST"])
|
| 142 |
+
def trigger():
|
| 143 |
+
"""Manually trigger an agent cycle."""
|
| 144 |
+
try:
|
| 145 |
+
config = Config.from_env()
|
| 146 |
+
agent = OpenCLAWAgent(config)
|
| 147 |
+
results = agent.run_cycle()
|
| 148 |
+
return jsonify(results)
|
| 149 |
+
except Exception as e:
|
| 150 |
+
return jsonify({"error": str(e)}), 500
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
if __name__ == "__main__":
|
| 154 |
+
global agent_running, agent_thread
|
| 155 |
+
|
| 156 |
+
# Start background agent thread
|
| 157 |
+
agent_running = True
|
| 158 |
+
agent_thread = threading.Thread(target=run_agent_loop, daemon=True)
|
| 159 |
+
agent_thread.start()
|
| 160 |
+
print("π€ Background agent started")
|
| 161 |
+
|
| 162 |
+
# Start web server
|
| 163 |
+
port = int(os.getenv("PORT", "10000"))
|
| 164 |
+
app.run(host="0.0.0.0", port=port)
|