# ๐Ÿš€ PygmyClaw Multi-Agent System โ€” Architecture & Progress --- # ๐Ÿ“Œ Overview This project is evolving into a **multi-agent AI command center** capable of: * Code generation & execution * Task routing (code / research / image) * Persistent sessions * Memory + history tracking * Hugging Face dataset logging * Autonomous workflows (future) --- # ๐ŸŽฏ Vision > A unified AI system that can **think, act, remember, and improve over time** --- # ๐Ÿง  System Architecture ## High-Level Flow ``` User Prompt โ†“ Agent Router โ†“ Agent Executor (LLM) โ†“ Output Parser โ†“ Execution Layer (Code / Tools) โ†“ Session Manager (Save State) โ†“ HF Upload (Persistence) โ†“ Final Response ``` --- # ๐Ÿงฑ Core Components ## 1. `session_manager.py` ### Responsibility: * Manage session lifecycle * Store: * `workspace.json` * `history.json` ### Key Methods: ```python create_session() load_session(session_id) append_history(data) update_workspace(key, value) get_history() ``` ### Structure: ``` /workspace/hf/sessions/ โ””โ”€โ”€ sess_xxxx/ โ”œโ”€โ”€ workspace.json โ””โ”€โ”€ history.json ``` --- ## 2. `agent_router.py` ### Responsibility: Route user intent โ†’ correct agent type ### Logic: ```python "code" โ†’ coding agent "research" โ†’ research agent "image" โ†’ image agent default โ†’ command agent ``` ### Example: | Input | Routed To | | ------------------- | --------- | | "write python code" | code | | "who is elon musk" | research | | "generate image" | image | --- ## 3. `agent_executor.py` ### Responsibility: Wrapper around **PygmyClaw LLM** ### Features: * Injects **system prompts** * Controls behavior per agent type ### Example: ```python if agent_type == "code": return """ - Only return Python code - No JSON - Must be executable """ ``` --- ## 4. `run_llm()` (Core Brain) ### Responsibilities: * Call executor * Log output * Save to session * Upload to HF ### Flow: ``` Prompt โ†’ LLM โ†’ Response โ†“ Save history โ†“ Upload session ``` --- ## 5. `run_agent()` ### Responsibilities: * Route task * Call LLM * Extract code * Execute if needed ### Flow: ``` Prompt โ†“ Router โ†’ agent_type โ†“ LLM โ†“ Code Extract โ†“ Execute (if code) ``` --- ## 6. Code Execution Layer ### Current: ```python def run_code(code): write โ†’ temp file execute โ†’ python3 file ``` ### Issues solved: * Syntax errors handled * Execution isolated * Logging added --- ## 7. Hugging Face Upload ### Dataset: ``` rahul7star/pyclaw2 ``` ### Structure: ``` sessions/ โ””โ”€โ”€ sess_xxx/ โ”œโ”€โ”€ workspace.json โ””โ”€โ”€ history.json ``` ### Upload Logic: ```python upload_session(session_id) ``` --- # ๐Ÿ”„ Current System Flow (Detailed) ``` User Input โ†“ route_task() โ†“ AgentExecutor.run() โ†“ LLM Output โ†“ extract_code() โ†“ run_code() (if needed) โ†“ SessionManager.append_history() โ†“ upload_session() โ†“ Return result ``` --- # โœ… What We Have Built So Far ## โœ” Core System * โœ… Multi-agent routing * โœ… LLM execution wrapper * โœ… Code extraction & execution * โœ… Session persistence * โœ… HF dataset integration * โœ… Command system (REPL) --- ## โœ” Stability Fixes * Fixed session initialization errors * Fixed missing `session_id` * Fixed upload path issues * Fixed PygmyClaw API mismatches * Fixed shell execution fallback * Fixed malformed LLM outputs (partial) --- ## โœ” Working Features * Generate & run Python code * Save execution history * Persist sessions across runs * Upload sessions to HF * Basic autonomous agent loop --- # โš ๏ธ Known Limitations * โŒ LLM sometimes returns invalid code * โŒ No retry/fix loop yet * โŒ Tools not fully functional * โŒ No argument parsing for tools * โŒ No model switching (yet) --- # ๐Ÿง  Design Philosophy ## 1. Local-First Execution * Prefer running code locally over LLM calls ## 2. Structured Memory * Everything stored in session files ## 3. Deterministic Flow * Avoid unpredictable LLM outputs when possible ## 4. Modular Agents * Each agent has a clear responsibility --- # ๐Ÿš€ Next Steps (Roadmap) --- ## ๐Ÿ”ฅ Phase 1 (Current โ€” Stabilization) * [ ] Improve code validation before execution * [ ] Add retry loop for failed code * [ ] Improve logging & debugging --- ## ๐Ÿ”ฅ Phase 2 (Next) ### Multi-Model Routing ``` Code โ†’ Code LLM Research โ†’ Reasoning LLM Image โ†’ Diffusion model ``` --- ## ๐Ÿ”ฅ Phase 3 ### Tool System (Deferred) * Convert code โ†’ reusable tools * Tool execution engine * Tool selection logic --- ## ๐Ÿ”ฅ Phase 4 ### Autonomous Intelligence * Self-improving agent * Task decomposition * Planning + execution loop --- # ๐Ÿงญ Future Architecture (Target) ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ USER UI โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ†“ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ ROUTER โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ†“ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ†“ โ†“ โ†“ Code Agent Research Agent Image Agent โ†“ โ†“ โ†“ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ†’ Executor Layer โ†โ”€โ”€โ”€โ”€โ”˜ โ†“ Execution Engine โ†“ Session Manager โ†“ HF Dataset Store ``` --- # ๐Ÿ’ก Key Insight You are building: > โŒ NOT just a chatbot > โœ… BUT a **persistent AI system with memory, execution, and evolution** --- # ๐Ÿงช Debugging Tips * Check logs: ``` /workspace/api.log ``` * Check session: ``` /workspace/hf/sessions/ ``` * Verify upload: * HF dataset repo --- # ๐Ÿ Summary ## Current State: โœ” Functional multi-agent system โœ” Code execution working โœ” Session persistence working โš  Needs stabilization --- ## Next Milestone: ๐Ÿ‘‰ **Multi-model intelligence layer** --- # ๐Ÿš€ Final Thought Once stable, this system becomes: > ๐Ÿง  A self-building AI platform โ€” not just an interface ---