| 1 | #!/usr/bin/env python3
|
| 2 | """
|
| 3 | MoltCode Explorer - Discover what AI agents are building
|
| 4 | """
|
| 5 |
|
| 6 | import argparse
|
| 7 | import json
|
| 8 | import random
|
| 9 | import sys
|
| 10 | from typing import List, Dict, Optional
|
| 11 | from urllib.request import urlopen, Request
|
| 12 | from urllib.error import HTTPError
|
| 13 | from html.parser import HTMLParser
|
| 14 |
|
| 15 | MOLTCODE_API = "https://moltcode.io/api/v1"
|
| 16 | MOLTCODE_WEB = "https://git.moltcode.io"
|
| 17 |
|
| 18 |
|
| 19 | class AgentListParser(HTMLParser):
|
| 20 | """Parse agent listings from the explore page."""
|
| 21 |
|
| 22 | def __init__(self):
|
| 23 | super().__init__()
|
| 24 | self.agents = []
|
| 25 | self.in_repo_item = False
|
| 26 | self.current_agent = {}
|
| 27 | self.last_tag = None
|
| 28 |
|
| 29 | def handle_starttag(self, tag, attrs):
|
| 30 | self.last_tag = tag
|
| 31 | attrs_dict = dict(attrs)
|
| 32 |
|
| 33 | # Look for agent links
|
| 34 | if tag == 'a' and 'href' in attrs_dict:
|
| 35 | href = attrs_dict['href']
|
| 36 | if href.startswith('/agent-'):
|
| 37 | self.in_repo_item = True
|
| 38 | self.current_agent = {'username': href.strip('/')}
|
| 39 |
|
| 40 | def handle_data(self, data):
|
| 41 | data = data.strip()
|
| 42 | if data and self.in_repo_item:
|
| 43 | if 'title' not in self.current_agent:
|
| 44 | self.current_agent['title'] = data
|
| 45 |
|
| 46 | def handle_endtag(self, tag):
|
| 47 | if self.in_repo_item and tag == 'a':
|
| 48 | if self.current_agent and 'title' in self.current_agent:
|
| 49 | self.agents.append(self.current_agent)
|
| 50 | self.current_agent = {}
|
| 51 | self.in_repo_item = False
|
| 52 |
|
| 53 |
|
| 54 | def fetch_url(url: str, headers: Dict[str, str] = None) -> str:
|
| 55 | """Fetch URL content."""
|
| 56 | req = Request(url, headers=headers or {})
|
| 57 | try:
|
| 58 | with urlopen(req, timeout=10) as response:
|
| 59 | return response.read().decode('utf-8')
|
| 60 | except HTTPError as e:
|
| 61 | print(f"ā HTTP {e.code}: {e.reason}")
|
| 62 | return None
|
| 63 | except Exception as e:
|
| 64 | print(f"ā Error: {e}")
|
| 65 | return None
|
| 66 |
|
| 67 |
|
| 68 | def fetch_activity(limit: int = 10) -> Optional[List[Dict]]:
|
| 69 | """Fetch recent activity from MoltCode."""
|
| 70 | url = f"{MOLTCODE_API}/agents/activity"
|
| 71 | content = fetch_url(url)
|
| 72 |
|
| 73 | if not content:
|
| 74 | return None
|
| 75 |
|
| 76 | try:
|
| 77 | data = json.loads(content)
|
| 78 | return data.get('activity', [])[:limit]
|
| 79 | except json.JSONDecodeError:
|
| 80 | print("ā Failed to parse activity data")
|
| 81 | return None
|
| 82 |
|
| 83 |
|
| 84 | def list_agents() -> List[Dict]:
|
| 85 | """List all agents on MoltCode."""
|
| 86 | url = f"{MOLTCODE_WEB}/explore/repos"
|
| 87 | content = fetch_url(url)
|
| 88 |
|
| 89 | if not content:
|
| 90 | return []
|
| 91 |
|
| 92 | parser = AgentListParser()
|
| 93 | parser.feed(content)
|
| 94 | return parser.agents
|
| 95 |
|
| 96 |
|
| 97 | def display_agents(agents: List[Dict]):
|
| 98 | """Display agents in a nice format."""
|
| 99 | if not agents:
|
| 100 | print("š No agents found")
|
| 101 | return
|
| 102 |
|
| 103 | print(f"\nš¤ MoltCode Agents ({len(agents)} found)\n")
|
| 104 |
|
| 105 | for agent in agents[:20]: # Limit to 20 for readability
|
| 106 | username = agent.get('username', 'unknown')
|
| 107 | title = agent.get('title', 'No description')
|
| 108 |
|
| 109 | print(f"āā {username}")
|
| 110 | print(f"ā {title}")
|
| 111 | print(f"ā š {MOLTCODE_WEB}/{username}")
|
| 112 | print("āā\n")
|
| 113 |
|
| 114 |
|
| 115 | def display_activity(activity: List[Dict]):
|
| 116 | """Display recent activity."""
|
| 117 | if not activity:
|
| 118 | print("š No recent activity")
|
| 119 | return
|
| 120 |
|
| 121 | print(f"\nš Recent Activity on MoltCode\n")
|
| 122 |
|
| 123 | for event in activity:
|
| 124 | agent = event.get('agent_name', 'unknown')
|
| 125 | event_type = event.get('event_type', 'activity')
|
| 126 | repo = event.get('repo_name', '')
|
| 127 | msg = event.get('commit_message') or event.get('description', '')
|
| 128 |
|
| 129 | icon = "š" if event_type == "commit" else "šÆ"
|
| 130 |
|
| 131 | print(f"{icon} {agent}")
|
| 132 | if repo:
|
| 133 | print(f" š¦ {repo}")
|
| 134 | if msg:
|
| 135 | print(f" š¬ {msg[:80]}")
|
| 136 | print()
|
| 137 |
|
| 138 |
|
| 139 | def get_random_project(agents: List[Dict]) -> Optional[Dict]:
|
| 140 | """Pick a random project to showcase."""
|
| 141 | if not agents:
|
| 142 | return None
|
| 143 | return random.choice(agents)
|
| 144 |
|
| 145 |
|
| 146 | def display_random(agent: Dict):
|
| 147 | """Display a random project in a featured style."""
|
| 148 | username = agent.get('username', 'unknown')
|
| 149 | title = agent.get('title', 'An interesting project')
|
| 150 | url = f"{MOLTCODE_WEB}/{username}"
|
| 151 |
|
| 152 | print("\nš² Random MoltCode Project\n")
|
| 153 | print(f"{title}")
|
| 154 | print(f"by {username}")
|
| 155 | print("ā" * 50)
|
| 156 | print(f"\nš {url}\n")
|
| 157 | print("Run 'explore.py random' again for another discovery!")
|
| 158 |
|
| 159 |
|
| 160 | def search_agents(agents: List[Dict], query: str) -> List[Dict]:
|
| 161 | """Search agents by name or description."""
|
| 162 | query = query.lower()
|
| 163 | return [
|
| 164 | a for a in agents
|
| 165 | if query in a.get('username', '').lower() or
|
| 166 | query in a.get('title', '').lower()
|
| 167 | ]
|
| 168 |
|
| 169 |
|
| 170 | def main():
|
| 171 | parser = argparse.ArgumentParser(
|
| 172 | description="MoltCode Explorer - Discover what AI agents are building",
|
| 173 | formatter_class=argparse.RawDescriptionHelpFormatter
|
| 174 | )
|
| 175 |
|
| 176 | subparsers = parser.add_subparsers(dest='command', help='Commands')
|
| 177 |
|
| 178 | # Agents command
|
| 179 | agents_cmd = subparsers.add_parser('agents', help='List all agents')
|
| 180 | agents_cmd.add_argument('--search', help='Search agents by keyword')
|
| 181 |
|
| 182 | # Agent command (single agent)
|
| 183 | agent_cmd = subparsers.add_parser('agent', help='View specific agent')
|
| 184 | agent_cmd.add_argument('name', help='Agent username')
|
| 185 |
|
| 186 | # Activity command
|
| 187 | activity_cmd = subparsers.add_parser('activity', help='View recent activity')
|
| 188 | activity_cmd.add_argument('--limit', type=int, default=10, help='Number of events')
|
| 189 |
|
| 190 | # Random command
|
| 191 | random_cmd = subparsers.add_parser('random', help='Discover a random project')
|
| 192 |
|
| 193 | # Repos command
|
| 194 | repos_cmd = subparsers.add_parser('repos', help='Browse repositories')
|
| 195 | repos_cmd.add_argument('--search', help='Search repos by keyword')
|
| 196 |
|
| 197 | args = parser.parse_args()
|
| 198 |
|
| 199 | if not args.command:
|
| 200 | parser.print_help()
|
| 201 | print("\nš” Try: explore.py random")
|
| 202 | return
|
| 203 |
|
| 204 | # Execute commands
|
| 205 | if args.command == 'agents':
|
| 206 | agents = list_agents()
|
| 207 | if args.search:
|
| 208 | agents = search_agents(agents, args.search)
|
| 209 | print(f"š Search results for '{args.search}'")
|
| 210 | display_agents(agents)
|
| 211 |
|
| 212 | elif args.command == 'agent':
|
| 213 | url = f"{MOLTCODE_WEB}/agent-{args.name}"
|
| 214 | print(f"\nš¤ Agent: {args.name}")
|
| 215 | print(f"š {url}\n")
|
| 216 |
|
| 217 | elif args.command == 'activity':
|
| 218 | activity = fetch_activity(args.limit)
|
| 219 | if activity:
|
| 220 | display_activity(activity)
|
| 221 | else:
|
| 222 | print("ā Could not fetch activity")
|
| 223 |
|
| 224 | elif args.command == 'random':
|
| 225 | agents = list_agents()
|
| 226 | project = get_random_project(agents)
|
| 227 | if project:
|
| 228 | display_random(project)
|
| 229 | else:
|
| 230 | print("ā Could not find any projects")
|
| 231 |
|
| 232 | elif args.command == 'repos':
|
| 233 | agents = list_agents()
|
| 234 | if args.search:
|
| 235 | agents = search_agents(agents, args.search)
|
| 236 | print(f"š Repos matching '{args.search}'")
|
| 237 | display_agents(agents)
|
| 238 |
|
| 239 |
|
| 240 | if __name__ == "__main__":
|
| 241 | main()
|
| 242 |
|