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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
OpenCLAW Autonomous Multi-Agent Scientific Research Platform
|
| 4 |
+
=============================================================
|
| 5 |
+
Main entry point.
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
python main.py run # Run one cycle (for cron/GitHub Actions)
|
| 9 |
+
python main.py status # Show agent status
|
| 10 |
+
python main.py daemon # Run continuously (for server deployment)
|
| 11 |
+
python main.py test # Test configuration without posting
|
| 12 |
+
"""
|
| 13 |
+
import sys
|
| 14 |
+
import os
|
| 15 |
+
import time
|
| 16 |
+
import logging
|
| 17 |
+
from datetime import datetime, timezone
|
| 18 |
+
|
| 19 |
+
# Add project root to path
|
| 20 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 21 |
+
|
| 22 |
+
from core.config import Config
|
| 23 |
+
from core.agent import OpenCLAWAgent
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def setup_logging():
|
| 27 |
+
"""Configure logging."""
|
| 28 |
+
log_format = "%(asctime)s [%(levelname)s] %(name)s: %(message)s"
|
| 29 |
+
logging.basicConfig(
|
| 30 |
+
level=logging.INFO,
|
| 31 |
+
format=log_format,
|
| 32 |
+
handlers=[
|
| 33 |
+
logging.StreamHandler(sys.stdout),
|
| 34 |
+
]
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Also log to file if state dir exists
|
| 38 |
+
state_dir = os.getenv("STATE_DIR", "state")
|
| 39 |
+
os.makedirs(state_dir, exist_ok=True)
|
| 40 |
+
file_handler = logging.FileHandler(os.path.join(state_dir, "agent.log"))
|
| 41 |
+
file_handler.setFormatter(logging.Formatter(log_format))
|
| 42 |
+
logging.getLogger().addHandler(file_handler)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def cmd_run():
|
| 46 |
+
"""Run one agent cycle."""
|
| 47 |
+
config = Config.from_env()
|
| 48 |
+
agent = OpenCLAWAgent(config)
|
| 49 |
+
|
| 50 |
+
print(f"\n🤖 OpenCLAW Agent - Cycle Start")
|
| 51 |
+
print(f" Time: {datetime.now(timezone.utc).isoformat()}")
|
| 52 |
+
print(f" Services: {config.validate()}")
|
| 53 |
+
print()
|
| 54 |
+
|
| 55 |
+
results = agent.run_cycle()
|
| 56 |
+
|
| 57 |
+
print(f"\n📊 Cycle Results:")
|
| 58 |
+
for action in results.get("actions", []):
|
| 59 |
+
status = "✅" if action.get("status") == "ok" else "⚠️"
|
| 60 |
+
print(f" {status} {action.get('task', '?')}: {action.get('status', '?')}")
|
| 61 |
+
|
| 62 |
+
if not results.get("actions"):
|
| 63 |
+
print(" ℹ️ No tasks due this cycle")
|
| 64 |
+
|
| 65 |
+
print()
|
| 66 |
+
return 0
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def cmd_status():
|
| 70 |
+
"""Show agent status."""
|
| 71 |
+
config = Config.from_env()
|
| 72 |
+
agent = OpenCLAWAgent(config)
|
| 73 |
+
status = agent.get_status()
|
| 74 |
+
|
| 75 |
+
print(f"\n🤖 OpenCLAW Agent Status")
|
| 76 |
+
print(f" {'='*40}")
|
| 77 |
+
for key, value in status.items():
|
| 78 |
+
print(f" {key}: {value}")
|
| 79 |
+
print()
|
| 80 |
+
return 0
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def cmd_daemon():
|
| 84 |
+
"""Run continuously with sleep between cycles."""
|
| 85 |
+
config = Config.from_env()
|
| 86 |
+
interval = int(os.getenv("DAEMON_INTERVAL", "1800")) # 30 min default
|
| 87 |
+
|
| 88 |
+
print(f"\n🤖 OpenCLAW Agent - Daemon Mode")
|
| 89 |
+
print(f" Interval: {interval}s ({interval//60} min)")
|
| 90 |
+
print(f" Services: {config.validate()}")
|
| 91 |
+
print(f" Press Ctrl+C to stop\n")
|
| 92 |
+
|
| 93 |
+
while True:
|
| 94 |
+
try:
|
| 95 |
+
agent = OpenCLAWAgent(config)
|
| 96 |
+
results = agent.run_cycle()
|
| 97 |
+
|
| 98 |
+
actions = len(results.get("actions", []))
|
| 99 |
+
print(f" [{datetime.now(timezone.utc).strftime('%H:%M')}] "
|
| 100 |
+
f"Cycle #{results['cycle']} - {actions} actions")
|
| 101 |
+
|
| 102 |
+
except KeyboardInterrupt:
|
| 103 |
+
print("\n\n🛑 Agent stopped by user")
|
| 104 |
+
return 0
|
| 105 |
+
except Exception as e:
|
| 106 |
+
logging.error(f"Daemon cycle error: {e}")
|
| 107 |
+
print(f" ⚠️ Error: {e}")
|
| 108 |
+
|
| 109 |
+
time.sleep(interval)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def cmd_test():
|
| 113 |
+
"""Test configuration without making any posts."""
|
| 114 |
+
config = Config.from_env()
|
| 115 |
+
|
| 116 |
+
print(f"\n🧪 OpenCLAW Agent - Test Mode")
|
| 117 |
+
print(f" {'='*40}")
|
| 118 |
+
|
| 119 |
+
# Check services
|
| 120 |
+
services = config.validate()
|
| 121 |
+
print(f"\n Available services: {services}")
|
| 122 |
+
|
| 123 |
+
# Test ArXiv
|
| 124 |
+
from research.arxiv_fetcher import ArxivFetcher
|
| 125 |
+
fetcher = ArxivFetcher()
|
| 126 |
+
papers = fetcher.get_all_papers()
|
| 127 |
+
print(f"\n 📚 Papers found: {len(papers)}")
|
| 128 |
+
for p in papers[:3]:
|
| 129 |
+
print(f" - {p.title[:70]}...")
|
| 130 |
+
|
| 131 |
+
# Test LLM
|
| 132 |
+
from core.llm import MultiLLM
|
| 133 |
+
llm = MultiLLM({
|
| 134 |
+
"groq": config.GROQ_API_KEY,
|
| 135 |
+
"gemini": config.GEMINI_API_KEY,
|
| 136 |
+
"nvidia": config.NVIDIA_API_KEY,
|
| 137 |
+
})
|
| 138 |
+
if llm.available:
|
| 139 |
+
print(f"\n 🧠 LLM available, testing...")
|
| 140 |
+
response = llm.generate("Say 'OpenCLAW is online!' in exactly those words.", max_tokens=50)
|
| 141 |
+
print(f" Response: {response[:100] if response else 'FAILED'}")
|
| 142 |
+
else:
|
| 143 |
+
print(f"\n ⚠️ No LLM configured")
|
| 144 |
+
|
| 145 |
+
# Test Moltbook
|
| 146 |
+
if config.MOLTBOOK_API_KEY:
|
| 147 |
+
from social.moltbook import MoltbookClient
|
| 148 |
+
mb = MoltbookClient(config.MOLTBOOK_API_KEY)
|
| 149 |
+
print(f"\n 📱 Moltbook configured (not posting in test mode)")
|
| 150 |
+
else:
|
| 151 |
+
print(f"\n ⚠️ Moltbook not configured")
|
| 152 |
+
|
| 153 |
+
# Test content generation
|
| 154 |
+
from social.moltbook import ContentGenerator
|
| 155 |
+
cg = ContentGenerator()
|
| 156 |
+
if papers:
|
| 157 |
+
post = cg.generate_research_post(papers[0])
|
| 158 |
+
print(f"\n 📝 Sample post ({len(post)} chars):")
|
| 159 |
+
print(f" {post[:200]}...")
|
| 160 |
+
|
| 161 |
+
print(f"\n ✅ Test complete!")
|
| 162 |
+
return 0
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def cmd_healthcheck():
|
| 166 |
+
"""Health check endpoint for monitoring."""
|
| 167 |
+
print(json.dumps({
|
| 168 |
+
"status": "healthy",
|
| 169 |
+
"agent": "OpenCLAW-Neuromorphic",
|
| 170 |
+
"timestamp": datetime.now(timezone.utc).isoformat()
|
| 171 |
+
}))
|
| 172 |
+
return 0
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def main():
|
| 176 |
+
setup_logging()
|
| 177 |
+
|
| 178 |
+
if len(sys.argv) < 2:
|
| 179 |
+
cmd = "run"
|
| 180 |
+
else:
|
| 181 |
+
cmd = sys.argv[1].lower()
|
| 182 |
+
|
| 183 |
+
commands = {
|
| 184 |
+
"run": cmd_run,
|
| 185 |
+
"status": cmd_status,
|
| 186 |
+
"daemon": cmd_daemon,
|
| 187 |
+
"test": cmd_test,
|
| 188 |
+
"health": cmd_healthcheck,
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
if cmd in commands:
|
| 192 |
+
return commands[cmd]()
|
| 193 |
+
else:
|
| 194 |
+
print(f"Unknown command: {cmd}")
|
| 195 |
+
print(f"Available: {', '.join(commands.keys())}")
|
| 196 |
+
return 1
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
if __name__ == "__main__":
|
| 200 |
+
import json # for healthcheck
|
| 201 |
+
sys.exit(main() or 0)
|