# zCode CLI X Agentic coding assistant with **Z.AI + Telegram integration** โ€” autonomous code execution with real-time streaming, self-correction loops, persistent self-learning memory, and RTK token optimization. > ๐Ÿ’ก **Get 10% OFF Z.AI** โ€” Use code **ROK78RJKNW** at [z.ai/subscribe](https://z.ai/subscribe?ic=ROK78RJKNW) for the Coding Plan ## โšก Features ### Core - **๐Ÿค– AI-Powered Code Generation**: Powered by Z.AI GLM-5.1 (Coding Plan) - **๐Ÿ“ฑ Telegram Bot**: 24/7 via grammy + webhook with real-time SSE streaming - **๐Ÿ› ๏ธ Full Engineering Access**: Bash, FileEdit, WebSearch, Git tools - **๐Ÿง  Agent System**: Code Reviewer, System Architect, DevOps Engineer - **๐Ÿ“š Skills System**: Pre-built skills for common tasks ### ๐Ÿง  Self-Learning Memory - **Persistent across sessions**: JSON-backed memory store survives restarts - **5 categories**: `lesson`, `pattern`, `preference`, `discovery`, `gotcha` - **Auto-injected into system prompt**: AI knows what it learned before โ€” every conversation builds on the last - **Smart eviction**: Max 500 memories with priority-based eviction (old discoveries first, lessons/gotchas kept) - **Deduplication**: Same memory won't be stored twice โ€” access count increments instead ### ๐Ÿ”ฌ Curiosity Engine The bot doesn't just respond โ€” it **learns from every interaction**. After each response, an asynchronous analysis pass runs: ``` User message + AI response โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Pattern Detector โ”‚ โ† runs AFTER delivery (zero latency) โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ–ผ โ–ผ โ–ผ โ–ผ โ–ผ Error User Successful First-time New API + Fix Correct Complex Tool Quirk โ”‚ โ”‚ Solution Usage Found โ–ผ โ–ผ โ–ผ โ–ผ โ–ผ gotcha lesson pattern discovery discovery ``` **What triggers learning:** | Trigger | Category | Example | |---|---|---| | Error with fix found | `gotcha` | `ENOENT: no such file โ†’ use absolute paths` | | User says "wrong" or "fix" | `lesson` | `Correction on "npm install": use --legacy-peer-deps` | | Complex successful solution | `pattern` | `Solution for "deploy to VPS": 12-step process with SSH` | | First tool usage works | `discovery` | `Bash tool works for shell commands on this server` | | New API quirk discovered | `discovery` | `Z.AI SSE sends empty data lines between chunks` | | Repeated user preference | `preference` | `User always wants TypeScript over JavaScript` | **Commands:** | Command | Description | |---|---| | `/memory` | View memory stats + recent memories | | `/remember ` | Manually save a memory (auto-detects category) | | `/recall ` | Search memories by keyword | | `/forget ` | Delete a specific memory | ### ๐ŸŽค Voice I/O (Speech-to-Text + Text-to-Speech) Fully local voice processing. No API keys, no cloud services, no costs. ``` User sends voice message โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Download OGG โ”‚ โ† Telegram Bot API โ”‚ to /tmp โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ ffmpeg โ†’ WAV โ”‚ โ† 16kHz mono (Vosk requirement) โ”‚ (16kHz mono) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Vosk STT โ”‚ โ† Offline, ~200ms, 68MB model โ”‚ Python bridgeโ”‚ Zero network calls โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ {"text": "...", "confidence": 0.95} โ”‚ โ–ผ Feed into chatWithAI โ†’ AI responds (optionally via TTS tool โ†’ voice reply) ``` | Component | Technology | Size | Latency | Cost | |---|---|---|---|---| | **STT** (voiceโ†’text) | [Vosk](https://alphacephei.com/vosk/) โ€” offline speech recognition | 68MB model | ~200ms | Free | | **TTS** (textโ†’voice) | [node-edge-tts](https://github.com/yayuyokit/Edge-TTS-node) โ€” Microsoft Edge voices | No download | ~2s | Free | | **Audio conversion** | ffmpeg (system) | N/A | ~100ms | Free | **How it works:** 1. Telegram sends voice as OGG Opus. Bot downloads to `/tmp`. 2. `scripts/stt.py` โ€” Python bridge that converts to WAV (ffmpeg) and runs Vosk inference. 3. Returns JSON `{"text": "...", "confidence": 0.95}` to Node.js. 4. Transcribed text enters the normal `handleTextMessage()` pipeline โ€” full AI response with streaming, tools, memory, self-correction. 5. AI can optionally use the `tts` tool to reply with a voice message. **Why Vosk over Whisper:** - **No GPU needed** โ€” runs on CPU, ~200MB RAM (Whisper needs 1-4GB) - **Fast** โ€” 200ms vs 5-10s for Whisper on CPU - **Tiny model** โ€” 68MB vs 1-3GB for Whisper - **Offline** โ€” zero network calls, zero API costs - **Good enough** โ€” ~95% accuracy for English speech ### ๐Ÿงฌ Self-Evolution zCode can **modify its own source code** โ€” and survive the attempt. Every change goes through a safety chain that guarantees rollback on any failure. **Safety chain (8 steps):** ``` AI requests patch โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ 1. Git stash โ”‚ โ† save uncommitted work โ”‚ 2. Git commit โ”‚ โ† checkpoint current state โ”‚ 3. File backup โ”‚ โ† copy original to .self-evolve-backups/ โ”‚ 4. Apply patch โ”‚ โ† string find/replace in target file โ”‚ 5. Syntax check โ”‚ โ† node --check / py_compile / JSON.parse โ”‚ 6. Git commit โ”‚ โ† commit the change โ”‚ 7. Push + restartโ”‚ โ† deploy to production โ”‚ 8. Health + smokeโ”‚ โ† verify bot is alive and responding โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”Œโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ” โ”‚ FAIL? โ”‚โ”€โ”€โ”€โ”€ auto git reset + restart + restore from backup โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ”‚ OK โ–ผ โœ… Live ``` **Protected files** โ€” the bot **cannot** modify these (would brick itself): - `SelfEvolveTool.js` โ€” the safety system itself - `stt.py` โ€” voice recognition bridge **Backup system:** - Every `patch` auto-saves the original file to `.self-evolve-backups/` with a timestamp ID - `backups` action lists all backups (file, size, timestamp) - `restore` action copies any backup back to production (with its own safety checks) - Backups survive git corruption โ€” they're plain file copies, not git objects - Restoring also creates a pre-restore backup (undo the undo) **Actions:** `read`, `patch`, `list_files`, `git_log`, `diff`, `backups`, `restore` **Rate limit:** 1 patch per 60 seconds (prevents runaway self-modification loops) ### ๐Ÿง  Intelligence Routing The core of zCode CLI X's reliability. A unified agentic loop that handles both streaming and non-streaming through the same execution path โ€” no more split paths that lose context or hang silently. ``` User Message โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ chatWithAI() โ€” Main Loop โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Call API (stream or non-stream) โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ–ผ โ–ผ โ”‚ โ”‚ tool_calls? text content โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ” return answer โ”‚ โ”‚ โ–ผ โ–ผ โ”‚ โ”‚ Execute Feed results โ”‚ โ”‚ tools back to AI โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ–ผ โ”‚ โ”‚ Append to messages โ†’ loop back โ”‚ โ”‚ (max 10 turns, forced final answer) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` **How it works:** | Component | Role | |---|---| | `chatWithAI()` | Single entry point. While loop (max 10 turns) that calls API, handles tools, loops back. | | `streamChat()` | SSE transport. Streams tokens to user via `onDelta()`. **Accumulates** tool_call deltas instead of aborting. Returns `{ content, tool_calls, error }`. | | `nonStreamChat()` | REST transport. Single POST, returns `{ content, tool_calls, error }`. | | Tool execution | Runs all tool_calls in parallel, appends results as `tool` messages, loops back. | **Key design decisions:** - **No recursive fallbacks.** Old code had stream โ†’ detect tool โ†’ abort โ†’ call non-stream (which lost the stream context). Now both paths return the same struct and feed into the same loop. - **Tool call accumulation.** SSE sends tool_calls in delta chunks (name in one chunk, arguments in the next). `streamChat()` accumulates these across chunks and builds a complete `tool_calls` array โ€” same as the non-streaming response. - **Max 10 turns safety net.** If the AI keeps calling tools (infinite loop), after 10 turns a final non-streaming call without tools forces a text answer. - **Streaming is transport, not logic.** Whether tokens stream to the user or not, the tool execution loop is identical. **Before Intelligence Routing:** ``` Stream starts โ†’ detects tool_call โ†’ ABORT stream โ†’ recursive non-stream call โ†’ non-stream returns raw tool output as response โ†’ NO final answer from AI โ†’ user sees raw bash output or silence ``` **After Intelligence Routing:** ``` Stream starts โ†’ detects tool_call โ†’ accumulates full tool_call โ†’ executes tool โ†’ feeds result back to AI in same conversation โ†’ AI synthesizes final answer โ†’ streams final answer to user โ†’ done ``` ### Streaming & Formatting - **โšก Real-time SSE Streaming**: Token-by-token delivery via `StreamConsumer` โ€” adapted from [Hermes Agent's GatewayStreamConsumer](https://github.com/nousresearch/hermes-agent) - Queued token buffer โ†’ rate-limited `editMessageText` loop (1s base interval) - Adaptive backoff on Telegram flood control (429) - Typing cursor `โ–‰` during generation, clean final message - Graceful fallback to plain send on repeated failures - **โŒจ๏ธ Persistent Typing Indicator**: `sendChatAction('typing')` every 4s - Active from message receipt through entire response (tool turns + streaming) - Telegram typing expires after ~5s โ€” 4s interval keeps it alive continuously - Only stops when final response is fully delivered - Ensures user always sees activity, even during long tool execution gaps - **๐ŸŽจ Telegram HTML Formatting**: AI markdown โ†’ clean Telegram HTML - `**bold**`, `*italic*`, `` `code` ``, fenced code blocks, `[links](url)`, `~~strike~~`, headings, blockquotes, lists - Double fallback: HTML โ†’ stripped plain text (never shows raw `**`) ### Reliability - **๐Ÿ”„ Self-Correction Loops**: Automatic retry with exponential backoff - 2 retry attempts (500ms โ†’ 1s โ†’ 1.5s delay) - Triggers: API errors, rate limits, timeouts, 5xx server errors - Auto-simplification: prompts simplified on retry to avoid recurring errors - Full logging of all retry attempts with reason tracking - **๐Ÿ” Auto-Restart**: Process supervisor restarts the bot on crash (3s delay) - **๐Ÿ›ก๏ธ RTK (Rust Token Killer)**: Token optimization for supported commands - 60-90% savings on git, npm, cargo, pytest, docker, and more - Active tracking stats via `getTrackingStats()` ### Architecture - **๐Ÿ“จ Multi-Channel Delivery**: Hub-based routing (Telegram + Discord + WebSocket + log) - **๐Ÿ” Deduplication**: 60s TTL message deduplication - **๐Ÿ“‹ Request Queue**: Per-chat sequential processing (no race conditions) - **๐Ÿ”Œ MCP Protocol**: Full MCP client + server management - **โฐ Cron Scheduling**: 1s interval, task locking, auto-recovery - **๐Ÿ›ก๏ธ Unhandled rejection guard**: Catches any async error that slips through ## ๐Ÿ“ฆ Installation ```bash cd zcode-cli-x npm install ``` ## โš™๏ธ Configuration Copy `.env.example` to `.env` and configure: ```bash cp .env.example .env ``` ### Required Environment Variables ```env # Z.AI Configuration (Coding Plan) GLM_BASE_URL=https://api.z.ai/api/coding/paas/v4 ZAI_API_KEY=*** # Telegram Bot Configuration TELEGRAM_BOT_TOKEN=*** TELEGRAM_ALLOWED_USERS=your_telegram_id ZCODE_WEBHOOK_URL=https://your-domain.com/telegram/webhook ``` ## ๐ŸŽฎ Usage ### Run as CLI ```bash node bin/zcode.js ``` ### Run as Telegram Bot (24/7) ```bash node bin/zcode.js --no-cli ``` ### Run as systemd service (recommended) ```ini # /etc/systemd/system/zcode.service [Unit] Description=zCode CLI X Bot After=network.target [Service] Type=simple User= WorkingDirectory=/path/to/zcode-cli-x ExecStart=/usr/bin/node bin/zcode.js --no-cli Restart=always RestartSec=5 [Install] WantedBy=multi-user.target ``` ```bash sudo systemctl enable zcode sudo systemctl start zcode ``` ### Quick restart (no systemd) ```bash bash restart.sh ``` ## ๐Ÿค– Telegram Bot Commands | Command | Description | |---|---| | `/start` | Show help and capabilities | | `/tools` | List available tools | | `/skills` | List loaded skills | | `/agents` | List agent roles | | `/model ` | Switch AI model | | `/stats` | System & RTK stats | | `/memory` | ๐Ÿง  Persistent memory stats | | `/remember ` | ๐Ÿ“ Save to memory | | `/recall ` | ๐Ÿ” Search memory | | `/forget ` | ๐Ÿ—‘ Delete a memory | | `/selfcorrection` | Self-correction status | | `/bash ` | Execute shell command | | `/web ` | Search the web | | `/git ` | Git operations | | `/cancel` | Cancel current operation | Or just chat โ€” zCode uses tools automatically when needed. ## ๐Ÿ› ๏ธ Tools | Tool | Description | |---|---| | **BashTool** | Shell command execution with timeout control | | **FileEditTool** | Diff-aware file operations (read, write, patch) | | **WebSearchTool** | Web search with result ranking | | **GitTool** | Git operations (status, log, diff, commit, push, pull) | ## ๐Ÿง  Agents | Agent | Role | |---|---| | **Code Reviewer** | Review code for bugs, security issues, and improvements | | **System Architect** | Design system architecture and patterns | | **DevOps Engineer** | Handle deployment, CI/CD, and infrastructure | ## ๐Ÿ—๏ธ Architecture zCode CLI X uses a hybrid architecture: ``` User Message โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Telegram โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚ dedup.js โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚ request-queue โ”‚ โ”‚ (grammy) โ”‚ โ”‚ (60s TTL) โ”‚ โ”‚ (per-chat seq) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ self-correction โ”‚ โ”‚ (2 retries + โ”‚ โ”‚ backoff) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ–ผ โ–ผ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ chatWithAI โ”‚ โ”‚ Tool โ”‚ โ”‚ Agent โ”‚ โ”‚ (SSE stream) โ”‚ โ”‚ Handlers โ”‚ โ”‚ Delegation โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ–ผ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Stream โ”‚ โ”‚ sendFormattedโ”‚ โ”‚ Consumer โ”‚ โ”‚ (HTML mode) โ”‚ โ”‚ (edit-in- โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ place) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ markdown โ”‚ โ”‚ ToHtml() โ”‚ โ”‚ converter โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Telegram API โ”‚ โ”‚ (HTML mode) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ ๐Ÿง  Self- โ”‚ โ† async, zero latency โ”‚ Learning โ”‚ extracts patterns โ”‚ Engine โ”‚ stores to memory.json โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ### Core Components ``` zcode-cli-x/ โ”œโ”€โ”€ bin/ โ”‚ โ””โ”€โ”€ zcode.js # CLI entry point โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ bot/ โ”‚ โ”‚ โ”œโ”€โ”€ index.js # Telegram bot (grammy + SSE streaming + memory) โ”‚ โ”‚ โ”œโ”€โ”€ message-sender.js # StreamConsumer + markdownToHtml converter โ”‚ โ”‚ โ”œโ”€โ”€ memory.js # Persistent self-learning memory store โ”‚ โ”‚ โ”œโ”€โ”€ deduplication.js # Message deduplication (60s TTL) โ”‚ โ”‚ โ”œโ”€โ”€ request-queue.js # Per-chat request queuing โ”‚ โ”‚ โ”œโ”€โ”€ delivery-hub.js # Multi-channel delivery โ”‚ โ”‚ โ”œโ”€โ”€ discord.js # Discord integration (discord.js v14) โ”‚ โ”‚ โ””โ”€โ”€ self-correction.js # Self-correction wrapper (2 retries + backoff) โ”‚ โ”œโ”€โ”€ api/ โ”‚ โ”‚ โ””โ”€โ”€ index.js # Z.AI API adapter (GLM-5.1, SSE support) โ”‚ โ”œโ”€โ”€ tools/ โ”‚ โ”‚ โ”œโ”€โ”€ BashTool.js # Shell command executor (RTK-aware) โ”‚ โ”‚ โ”œโ”€โ”€ FileEditTool.js # File operations โ”‚ โ”‚ โ”œโ”€โ”€ WebSearchTool.js # Web search โ”‚ โ”‚ โ””โ”€โ”€ GitTool.js # Git operations (RTK-aware) โ”‚ โ”œโ”€โ”€ agents/ โ”‚ โ”‚ โ””โ”€โ”€ index.js # Agent orchestration โ”‚ โ”œโ”€โ”€ skills/ โ”‚ โ”‚ โ””โ”€โ”€ index.js # Skills system โ”‚ โ””โ”€โ”€ utils/ โ”‚ โ”œโ”€โ”€ logger.js # Winston logger โ”‚ โ”œโ”€โ”€ env.js # Environment validation โ”‚ โ””โ”€โ”€ rtk.js # RTK (Rust Token Killer) integration โ”œโ”€โ”€ data/ โ”‚ โ””โ”€โ”€ memory.json # Persistent memory (auto-created, gitignored) โ”œโ”€โ”€ logs/ # Runtime logs (gitignored) โ”œโ”€โ”€ .env # Configuration โ””โ”€โ”€ package.json ``` ### Bot Message Flow 1. **Message Reception**: Telegram webhook โ†’ grammy handler 2. **Deduplication**: `deduplication.js` (60s TTL, prevents double-processing) 3. **Request Queue**: `request-queue.js` (per-chat sequential processing) 4. **Memory Injection**: Memory context injected into system prompt 5. **Self-Correction**: `self-correction.js` (2 retries + exponential backoff + auto-simplification) 6. **AI Chat + Streaming**: `chatWithAI()` โ†’ SSE stream โ†’ `StreamConsumer` โ†’ real-time edits 7. **Formatting**: `markdownToHtml()` converts AI markdown โ†’ Telegram HTML 8. **Final Delivery**: `editMessageText` with HTML parse_mode (or fallback to stripped plain text) 9. **Self-Learning**: `selfLearn()` analyzes interaction โ†’ extracts patterns โ†’ saves to memory.json ### StreamConsumer Pipeline ``` Z.AI API (SSE) โ”‚ โ–ผ onDelta(token) โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Token Buffer โ”‚ โ† accumulates tokens from SSE stream โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ (every ~1s or 40 chars) โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ editMessage โ”‚ โ† plain text + cursor โ–‰ (no parse_mode) โ”‚ Text() โ”‚ rate-limited, adaptive backoff on flood โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ (on finish) โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ markdownTo โ”‚ โ† converts **bold**, *italic*, `code`, etc. โ”‚ Html() โ”‚ to , , ,
 HTML tags
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ editMessage  โ”‚  โ† final message with parse_mode: 'HTML'
โ”‚ Text()       โ”‚     fallback: stripped plain text (no raw **)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚
       โ–ผ (async, after delivery)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ selfLearn()  โ”‚  โ† pattern detector extracts learnable insights
โ”‚              โ”‚     saves to data/memory.json
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

### Memory System Architecture

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              data/memory.json                โ”‚
โ”‚  (persistent, survives restarts, gitignored) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚   MemoryStore     โ”‚
         โ”‚   (singleton)     โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ–ผ              โ–ผ              โ–ผ              โ–ผ              โ–ผ
  ๐Ÿ“– lesson    ๐Ÿ”ง pattern    ๐Ÿ‘ค preference  ๐Ÿ’ก discovery   โš ๏ธ gotcha
  "Always       "For deploy:  "User prefers   "Z.AI SSE      "ENOENT โ†’
   use abs      use scp..."    TS over JS"     sends empty    use absolute
   paths"                                     data lines"     paths"
    โ”‚              โ”‚              โ”‚              โ”‚              โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
    buildContextSummary() โ†’ injected into system prompt
    recall(query) โ†’ search memories
    remember(cat, text) โ†’ save new memory
    forget(id) โ†’ delete memory
```

**Priority in system prompt:** gotchas > lessons > patterns > preferences > discoveries

**Eviction policy:** When memory exceeds 500 entries, old single-access discoveries are evicted first. Lessons and gotchas are never evicted unless all else fails.

## ๐Ÿ zCode Swarm โ€” Multi-Agent Orchestration

Distributed multi-agent coordination system inspired by [Ruflo](https://github.com/ruvnet/ruflo). Spawns specialized agent swarms that work in parallel โ€” code reviews, security audits, performance analysis, architecture validation, test orchestration, and git operations.

### Architecture

```
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚  SwarmOrchestrator  โ”‚
        โ”‚  (main entry point) โ”‚
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ–ผ             โ–ผ             โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚Neural  โ”‚  โ”‚Federated โ”‚  โ”‚Agent     โ”‚
โ”‚Network โ”‚  โ”‚Memory    โ”‚  โ”‚Market-   โ”‚
โ”‚(ML)    โ”‚  โ”‚(6 NS)    โ”‚  โ”‚place     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ–ผ             โ–ผ             โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚Hiera-    โ”‚ โ”‚ Mesh +   โ”‚ โ”‚ Gossip + โ”‚
โ”‚rchical   โ”‚ โ”‚ CRDT     โ”‚ โ”‚ Consensusโ”‚
โ”‚Queen     โ”‚ โ”‚ P2P      โ”‚ โ”‚ BFT      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”ดโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ–ผ     โ–ผ     โ–ผ      โ–ผ      โ–ผ      โ–ผ
  Code  Perf  Sec   Arch   Test   Git
 Review Opt   Audit Analy  Orch   Swarm
```

### 6 Agent Skills

| Skill | ID | What It Does |
|---|---|---|
| ๐Ÿ” Code Review | `code-review-swarm` | Multi-agent review: security, performance, style, architecture |
| โšก Performance | `performance-optimizer` | Bottleneck detection, N+1 queries, memory leaks, score calculation |
| ๐Ÿ”’ Security | `security-auditor` | Injection, XSS, auth, data leakage โ€” severity classification |
| ๐Ÿ—๏ธ Architecture | `architecture-analyzer` | Coupling/cohesion analysis, SOLID compliance scoring |
| ๐Ÿงช Testing | `test-orchestrator` | Test generation, coverage analysis, execution tracking |
| ๐Ÿ”„ Git | `git-swarm` | Multi-repo PR analysis, review, branch management |

### 4 Coordination Modes

| Mode | Pattern | Best For |
|---|---|---|
| **Hierarchical** | Queen-led | Default โ€” single decision-maker routes tasks |
| **Mesh** | P2P + CRDT | Decentralized teams, conflict-free replication |
| **Gossip** | Random propagation | Large networks, eventual consistency |
| **Consensus** | Byzantine fault-tolerant | Critical decisions, 10% fault tolerance |

### Advanced Features

- **๐Ÿง  Neural Network Integration** โ€” ML-based agent recommendation with confidence scoring and real-time learning
- **๐Ÿ“ฆ Agent Marketplace** โ€” Plugin discovery, install/uninstall, capability-based search
- **๐Ÿ“Š Real-Time Dashboard** โ€” Terminal monitoring with 5s refresh, performance scores, memory stats
- **๐Ÿ“ˆ Performance Metrics** โ€” CPU/memory tracking, trend analysis, automated recommendations
- **๐Ÿงฉ Federated Memory** โ€” 6 namespaces (coordination, project-context, patterns, knowledge, session, metrics)

### Quick Start

```bash
# Verify installation (22 files)
node verify-swarm.cjs

# Run demo
node quick-start.cjs

# Configure
vim .zcode/config/coordinator.yaml
```

### Configuration (`.zcode/config/coordinator.yaml`)

```yaml
coordination:
  mode: hierarchical  # hierarchical | mesh | gossip | consensus

agents:
  enabled:
    - code-review-swarm
    - performance-optimizer
    - security-auditor
    - architecture-analyzer
    - test-orchestrator
    - git-swarm

neural:
  enabled: true
  architecture: multi-layer-perceptron
  layers: [64, 32, 16, 8]

dashboard:
  enabled: false  # true for live terminal dashboard

marketplace:
  enabled: true
```

## ๐Ÿ“Š Feature Comparison

| Feature | zCode CLI X | Hermes Agent | better-clawd |
|---|---|---|---|
| **Agentic** | | | |
| Autonomous execution | โœ… Full autonomous mode | โœ… Full autonomous mode | โš ๏ธ Manual step-by-step |
| Sub-agents | โœ… zCode Swarm (6 skills, 4 modes, ML) | โœ… delegate_task + batch | โŒ Single agent only |
| Agent roles | โœ… Reviewer, Architect, DevOps, Security, Tester, Git | โœ… Agent Registry (10+ roles) | โŒ Fixed single role |
| Neural coordination | โœ… ML agent recommendation | โŒ Rule-based only | โŒ None |
| Agent marketplace | โœ… Plugin discovery + install | โŒ Manual skills | โŒ None |
| Federated memory | โœ… 6-namespace CRDT-backed | โœ… Cross-session memory | โŒ None |
| Self-correction loops | โœ… 2 retries + backoff + auto-simplification | โœ… Agent self-correction skill | โŒ None |
| **Intelligence** | | | |
| Persistent memory | โœ… JSON-backed, 5 categories, auto-learn | โœ… Cross-session memory | โŒ None |
| Self-learning / curiosity | โœ… Pattern detector + auto-extraction | โœ… Knowledge + memory tools | โŒ None |
| Memory-injected prompts | โœ… Every conversation uses past lessons | โœ… Memory injected | โŒ None |
| **Streaming** | | | |
| Intelligence Routing | โœ… Unified agentic loop (stream+non-stream) | โš ๏ธ Separate stream/non-stream paths | โŒ None |
| Real-time SSE streaming | โœ… StreamConsumer (edit-in-place) | โœ… GatewayStreamConsumer | โŒ None |
| Tool call accumulation | โœ… Delta accumulation from SSE chunks | โš ๏ธ Abort on tool detection | โŒ None |
| Telegram HTML formatting | โœ… markdownToHtml + fallback | โœ… Native HTML support | โŒ None |
| Adaptive flood control | โœ… Exponential backoff | โœ… Flood backoff | โŒ N/A |
| **Tooling** | | | |
| Bash/Shell | โœ… BashTool | โœ… TerminalTool | โœ… Shell access |
| File editing | โœ… FileEditTool (diff-aware) | โœ… Patch + Write + Edit | โš ๏ธ Basic write |
| Web search | โœ… WebSearch | โœ… WebSearch + Vane + Exa | โŒ None |
| Git integration | โœ… GitTool (RTK-aware) | โœ… GitTool | โŒ None |
| Browser automation | โœ… Computer-use (Anthropic) | โœ… Full browser toolkit | โŒ None |
| MCP servers | โœ… Full MCP protocol | โœ… Native MCP + mcporter | โŒ None |
| RTK optimization | โœ… RTK active (60-90% savings) | โœ… RTK integrated | โŒ None |
| **Platform** | | | |
|| Telegram integration | โœ… Native bot + webhook + streaming | โœ… 2-way Telegram bridge | โŒ None ||
|| Persistent typing indicator | โœ… 4s interval, active during full response | โŒ None | โŒ None ||
|| Discord | โœ… Native bot (discord.js) | โœ… Full Discord integration | โŒ None ||
| Multi-channel delivery | โœ… Delivery hub (TG + DC + WS + log) | โœ… Cronโ†’multi-platform | โŒ None |
|| **Voice** | | | |
|| Speech-to-Text | โœ… Vosk (offline, ~200ms, 68MB) | โš ๏ธ Whisper (needs GPU) | โŒ None |
|| Text-to-Speech | โœ… Edge TTS (free, 100+ voices) | โœ… node-edge-tts | โŒ None |
|| Voiceโ†’AI pipeline | โœ… Transcribe โ†’ full agentic loop | โš ๏ธ Separate pipeline | โŒ None |
|| **Self-Evolution** | | | |
|| Self-modifying code | โœ… Patch own source + auto-deploy | โŒ Not supported | โŒ None |
|| Auto-backup before change | โœ… File-level + git checkpoint | N/A | N/A |
|| Auto-rollback on failure | โœ… Syntax + health + smoke test | N/A | N/A |
|| Backup history + restore | โœ… Timestamped backups, 1-command restore | N/A | N/A |
|| **Infrastructure** | | | |
| Model routing | โœ… Multi-provider | โœ… Multi-provider routing | โŒ Single model |
| Context compression | โœ… Compact pipeline | โœ… lean-ctx MCP (90% savings) | โŒ None |
| Auto-restart | โœ… Process supervisor | โœ… systemd managed | โŒ None |
| Cron scheduling | โœ… 1s interval, jitter, locks | โœ… Cron jobs with delivery | โŒ None |

### Summary

- **zCode CLI X** โ€” Lightweight agentic coder focused on Telegram + Z.AI. **Intelligence Routing** โ€” a unified agentic loop that handles streaming and non-streaming through one execution path with tool call accumulation from SSE deltas. Real-time SSE streaming, self-correction loops, persistent self-learning memory with curiosity engine, RTK optimization, and beautiful HTML formatting. **zCode Swarm** โ€” distributed multi-agent orchestration with 6 specialized skills (code review, security, performance, architecture, testing, git), 4 coordination modes (hierarchical, mesh, gossip, consensus), neural network-based agent recommendation, federated memory, agent marketplace, and real-time dashboard. Gets smarter with every conversation. Ideal for quick coding tasks via Telegram.
- **Hermes Agent** โ€” Full-stack AI assistant platform. Best for complex multi-agent workflows, scheduled automation, and cross-platform deployment. 500+ skills, MCP ecosystem, deepest toolset.
- **better-clawd** โ€” Minimal Claude Code clone. Useful as a lightweight reference but lacks agentic depth.

## ๐Ÿ”— Integrations

- **Z.AI API**: GLM-5.1 model (Coding Plan) with SSE streaming
- **Telegram Bot API**: grammy + auto-retry + sequentialize + webhook
- **Discord.js v14**: Discord bot with GatewayIntentBits
- **Express.js**: HTTP server for webhook handling
- **Winston**: Structured logging
- **WebSocket**: Real-time updates
- **RTK**: Rust Token Killer (token optimization)
- **Vosk**: Offline speech recognition (STT, 68MB model, no API key)
- **ffmpeg**: Audio conversion (OGG โ†’ WAV for Vosk)
- **zCode Swarm**: Multi-agent orchestration (`.zcode/` directory)

## ๐Ÿค Contributing

Contributions welcome! Based on:
- [better-clawd](https://github.com/x1xhlol/better-clawd.git) โ€” Claude Code clone
- [Hermes Agent](https://hermes-agent.nousresearch.com) โ€” AI assistant platform (streaming architecture + memory system credit)

---

Built with โšก by zCode CLI X