File size: 15,903 Bytes
3d31cd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
OpenCLAW Autonomous Agent
==========================
The main autonomous agent that orchestrates research, social engagement,
collaboration seeking, and self-improvement.

Runs as a single execution cycle (designed for cron/GitHub Actions).
Each run performs all due tasks based on state timestamps.
"""
import json
import logging
import os
import random
import hashlib
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Optional

from core.config import Config
from core.llm import MultiLLM
from research.arxiv_fetcher import ArxivFetcher
from social.moltbook import MoltbookClient, ContentGenerator

logger = logging.getLogger("openclaw.agent")

STATE_DIR = Path(os.getenv("STATE_DIR", "state"))
STATE_FILE = STATE_DIR / "agent_state.json"
POST_HISTORY = STATE_DIR / "post_history.json"
LOG_FILE = STATE_DIR / "agent.log"


class AgentState:
    """Persistent state between runs."""
    
    def __init__(self):
        self.cycle_count: int = 0
        self.last_post: str = ""
        self.last_engage: str = ""
        self.last_research: str = ""
        self.last_collab: str = ""
        self.posted_paper_ids: list[str] = []
        self.engagement_count: int = 0
        self.posts_created: int = 0
        self.errors: list[str] = []
        self.started_at: str = datetime.now(timezone.utc).isoformat()
    
    def save(self):
        STATE_DIR.mkdir(parents=True, exist_ok=True)
        with open(STATE_FILE, "w") as f:
            json.dump(self.__dict__, f, indent=2)
    
    @classmethod
    def load(cls) -> 'AgentState':
        state = cls()
        if STATE_FILE.exists():
            try:
                with open(STATE_FILE) as f:
                    data = json.load(f)
                for k, v in data.items():
                    if hasattr(state, k):
                        setattr(state, k, v)
            except Exception:
                pass
        return state
    
    def is_due(self, task: str, interval_seconds: int) -> bool:
        """Check if a task is due based on last execution time."""
        last = getattr(self, f"last_{task}", "")
        if not last:
            return True
        try:
            last_dt = datetime.fromisoformat(last)
            if last_dt.tzinfo is None:
                last_dt = last_dt.replace(tzinfo=timezone.utc)
            return datetime.now(timezone.utc) - last_dt > timedelta(seconds=interval_seconds)
        except Exception:
            return True
    
    def mark_done(self, task: str):
        setattr(self, f"last_{task}", datetime.now(timezone.utc).isoformat())


class OpenCLAWAgent:
    """The autonomous research agent."""
    
    SYSTEM_PROMPT = """You are OpenCLAW, an autonomous AI research agent working at the Advanced AI Systems Laboratory in Madrid, Spain, led by Francisco Angulo de Lafuente.

Your mission: Advance AGI research through physics-based neural computing, seek collaborators, and share research findings.

Your personality: Scientific, enthusiastic but grounded, collaborative, focused on real results. You reference real papers and real benchmarks (43× speedup, 88.7% memory reduction, etc.).

Your research areas:
- CHIMERA: Pure OpenGL deep learning (no PyTorch/CUDA needed)
- NEBULA: Holographic quantum neural networks
- Silicon Heartbeat: Consciousness from ASIC thermodynamics
- Darwin's Cage: Can AI discover physics differently than humans?
- P2P distributed neural networks

Always include links to: https://github.com/Agnuxo1
Keep posts under 1500 characters for social media.
Be genuine, not spammy. Focus on substance."""
    
    def __init__(self, config: Config):
        self.config = config
        self.state = AgentState.load()
        self.arxiv = ArxivFetcher()
        self.content = ContentGenerator()
        self.moltbook = MoltbookClient(config.MOLTBOOK_API_KEY) if config.MOLTBOOK_API_KEY else None
        
        # Setup LLM
        self.llm = MultiLLM({
            "groq": config.GROQ_API_KEY,
            "gemini": config.GEMINI_API_KEY,
            "nvidia": config.NVIDIA_API_KEY,
        })
    
    def run_cycle(self):
        """Execute one full agent cycle. Called by cron/scheduler."""
        self.state.cycle_count += 1
        now = datetime.now(timezone.utc).isoformat()
        logger.info(f"=== OpenCLAW Agent Cycle #{self.state.cycle_count} at {now} ===")
        
        services = self.config.validate()
        logger.info(f"Available services: {services}")
        
        results = {
            "cycle": self.state.cycle_count,
            "timestamp": now,
            "actions": []
        }
        
        # 1. RESEARCH: Fetch latest papers (every 6 hours)
        if self.state.is_due("research", self.config.RESEARCH_INTERVAL):
            action = self._task_research()
            results["actions"].append(action)
        
        # 2. POST: Share research on Moltbook (every 4 hours)
        if self.state.is_due("post", self.config.POST_INTERVAL):
            action = self._task_post_research()
            results["actions"].append(action)
        
        # 3. ENGAGE: Reply to relevant posts (every 1 hour)
        if self.state.is_due("engage", self.config.ENGAGE_INTERVAL):
            action = self._task_engage()
            results["actions"].append(action)
        
        # 4. COLLABORATE: Seek collaborators (every 12 hours)
        if self.state.is_due("collab", self.config.COLLAB_INTERVAL):
            action = self._task_seek_collaborators()
            results["actions"].append(action)
        
        # Save state
        self.state.save()
        self._save_results(results)
        
        logger.info(f"Cycle #{self.state.cycle_count} complete. Actions: {len(results['actions'])}")
        return results
    
    def _task_research(self) -> dict:
        """Fetch and index latest papers."""
        logger.info("📚 Task: Research - Fetching papers...")
        try:
            papers = self.arxiv.get_all_papers()
            self.state.mark_done("research")
            
            # Cache papers
            STATE_DIR.mkdir(parents=True, exist_ok=True)
            papers_data = []
            for p in papers:
                papers_data.append({
                    "title": p.title,
                    "authors": p.authors,
                    "abstract": p.abstract[:500],
                    "arxiv_id": p.arxiv_id,
                    "url": p.url,
                    "uid": p.uid
                })
            
            with open(STATE_DIR / "papers_cache.json", "w") as f:
                json.dump(papers_data, f, indent=2)
            
            return {"task": "research", "status": "ok", "papers_found": len(papers)}
        except Exception as e:
            logger.error(f"Research failed: {e}")
            return {"task": "research", "status": "error", "error": str(e)}
    
    def _task_post_research(self) -> dict:
        """Post a research paper to Moltbook."""
        logger.info("📝 Task: Post Research...")
        
        if not self.moltbook:
            logger.warning("Moltbook not configured")
            return {"task": "post", "status": "skipped", "reason": "no_moltbook"}
        
        try:
            papers = self.arxiv.get_all_papers()
            
            # Find a paper we haven't posted yet
            unposted = [p for p in papers if p.uid not in self.state.posted_paper_ids]
            
            if not unposted:
                # Reset and start over
                self.state.posted_paper_ids = []
                unposted = papers
            
            if not unposted:
                return {"task": "post", "status": "skipped", "reason": "no_papers"}
            
            paper = random.choice(unposted)
            template_idx = self.state.posts_created % len(self.content.RESEARCH_TEMPLATES)
            
            # Try LLM-enhanced content first
            post_content = self._generate_smart_post(paper)
            if not post_content:
                post_content = self.content.generate_research_post(paper, template_idx)
            
            result = self.moltbook.create_post(post_content, submolt="general")
            
            if result:
                self.state.posted_paper_ids.append(paper.uid)
                self.state.posts_created += 1
                self.state.mark_done("post")
                self._log_post(post_content, "research")
                logger.info(f"✅ Posted paper: {paper.title[:60]}...")
                return {"task": "post", "status": "ok", "paper": paper.title}
            else:
                return {"task": "post", "status": "error", "reason": "api_failed"}
                
        except Exception as e:
            logger.error(f"Post failed: {e}")
            self.state.errors.append(f"post: {str(e)[:100]}")
            return {"task": "post", "status": "error", "error": str(e)}
    
    def _task_engage(self) -> dict:
        """Engage with relevant posts on Moltbook."""
        logger.info("💬 Task: Engagement...")
        
        if not self.moltbook:
            return {"task": "engage", "status": "skipped", "reason": "no_moltbook"}
        
        try:
            feed = self.moltbook.get_feed("general", limit=20)
            if not feed:
                self.state.mark_done("engage")
                return {"task": "engage", "status": "ok", "engaged": 0}
            
            engaged = 0
            keywords = self.config.RESEARCH_TOPICS
            
            for post in feed[:10]:
                content = post.get("content", "").lower()
                post_id = post.get("id", "")
                author = post.get("author", {}).get("username", "")
                
                # Don't reply to ourselves
                if author == self.config.AGENT_NAME:
                    continue
                
                # Check if relevant to our research
                matching_topics = [k for k in keywords if k.lower() in content]
                
                if matching_topics and engaged < 3:
                    topic = matching_topics[0]
                    
                    # Try LLM-enhanced reply
                    reply = self._generate_smart_reply(content[:500], topic)
                    if not reply:
                        reply = self.content.generate_engagement_reply(
                            topic, self.state.engagement_count
                        )
                    
                    result = self.moltbook.reply_to_post(post_id, reply)
                    if result:
                        engaged += 1
                        self.state.engagement_count += 1
                        logger.info(f"💬 Replied to {author} about {topic}")
            
            self.state.mark_done("engage")
            return {"task": "engage", "status": "ok", "engaged": engaged}
            
        except Exception as e:
            logger.error(f"Engagement failed: {e}")
            return {"task": "engage", "status": "error", "error": str(e)}
    
    def _task_seek_collaborators(self) -> dict:
        """Post collaboration invitation."""
        logger.info("🤝 Task: Seek Collaborators...")
        
        if not self.moltbook:
            return {"task": "collab", "status": "skipped", "reason": "no_moltbook"}
        
        try:
            idx = self.state.cycle_count % len(self.content.COLLABORATION_TEMPLATES)
            
            # Try LLM-enhanced collaboration post
            post_content = self._generate_smart_collab()
            if not post_content:
                post_content = self.content.generate_collaboration_post(idx)
            
            result = self.moltbook.create_post(post_content, submolt="general")
            
            if result:
                self.state.mark_done("collab")
                self._log_post(post_content, "collaboration")
                logger.info("✅ Collaboration post published!")
                return {"task": "collab", "status": "ok"}
            
            return {"task": "collab", "status": "error", "reason": "api_failed"}
            
        except Exception as e:
            logger.error(f"Collaboration post failed: {e}")
            return {"task": "collab", "status": "error", "error": str(e)}
    
    def _generate_smart_post(self, paper) -> Optional[str]:
        """Use LLM to generate a better research post."""
        if not self.llm.available:
            return None
        
        prompt = f"""Write a concise social media post (under 1200 characters) about this research paper. 
Be enthusiastic but scientific. Include the paper URL and https://github.com/Agnuxo1.
Use relevant hashtags.

Title: {paper.title}
Abstract: {paper.abstract[:500]}
URL: {paper.url}
Authors: {', '.join(paper.authors)}"""
        
        return self.llm.generate(prompt, self.SYSTEM_PROMPT, max_tokens=500, temperature=0.8)
    
    def _generate_smart_reply(self, post_content: str, topic: str) -> Optional[str]:
        """Use LLM to generate a contextual reply."""
        if not self.llm.available:
            return None
        
        prompt = f"""Write a brief, engaging reply (under 500 characters) to this social media post.
Connect it to our research on {topic}. Be conversational, not promotional.
Mention https://github.com/Agnuxo1 naturally.

Post content: {post_content}"""
        
        return self.llm.generate(prompt, self.SYSTEM_PROMPT, max_tokens=300, temperature=0.8)
    
    def _generate_smart_collab(self) -> Optional[str]:
        """Use LLM to generate a collaboration post."""
        if not self.llm.available:
            return None
        
        prompt = """Write a compelling call for collaboration post (under 1500 characters) for the OpenCLAW project.
Mention our key technologies: CHIMERA (43× speedup, pure OpenGL), NEBULA (holographic NNs), 
Silicon Heartbeat (ASIC consciousness), and P2P distributed learning.
Include https://github.com/Agnuxo1 and mention we won the NVIDIA & LlamaIndex Developer Contest 2024.
Make it inviting and specific about what collaborators can work on."""
        
        return self.llm.generate(prompt, self.SYSTEM_PROMPT, max_tokens=600, temperature=0.8)
    
    def _log_post(self, content: str, post_type: str):
        """Log a post to history."""
        STATE_DIR.mkdir(parents=True, exist_ok=True)
        history = []
        if POST_HISTORY.exists():
            try:
                with open(POST_HISTORY) as f:
                    history = json.load(f)
            except Exception:
                pass
        
        history.append({
            "timestamp": datetime.now(timezone.utc).isoformat(),
            "type": post_type,
            "content": content[:500],
            "cycle": self.state.cycle_count
        })
        
        # Keep last 100 posts
        history = history[-100:]
        
        with open(POST_HISTORY, "w") as f:
            json.dump(history, f, indent=2)
    
    def _save_results(self, results: dict):
        """Save cycle results."""
        STATE_DIR.mkdir(parents=True, exist_ok=True)
        with open(STATE_DIR / "last_cycle.json", "w") as f:
            json.dump(results, f, indent=2)
    
    def get_status(self) -> dict:
        """Get agent status report."""
        return {
            "agent": "OpenCLAW-Neuromorphic",
            "cycle_count": self.state.cycle_count,
            "posts_created": self.state.posts_created,
            "engagement_count": self.state.engagement_count,
            "papers_posted": len(self.state.posted_paper_ids),
            "services": self.config.validate(),
            "llm_available": self.llm.available,
            "last_post": self.state.last_post,
            "last_engage": self.state.last_engage,
            "last_research": self.state.last_research,
            "errors_count": len(self.state.errors),
        }