# zCode CLI X Agentic coding assistant with **Z.AI + Telegram integration** โ€” autonomous code execution with real-time streaming, self-correction loops, 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 ### 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 - **๐ŸŽจ 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 - **๐Ÿ›ก๏ธ 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 ## ๐Ÿ“ฆ 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 --bot ``` ### Run as systemd service ```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 --bot Restart=always RestartSec=5 [Install] WantedBy=multi-user.target ``` ```bash sudo systemctl enable zcode sudo systemctl start zcode ``` ## ๐Ÿค– 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 | | `/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) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ### Core Components ``` zcode-cli-x/ โ”œโ”€โ”€ bin/ โ”‚ โ””โ”€โ”€ zcode.js # CLI entry point โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ bot/ โ”‚ โ”‚ โ”œโ”€โ”€ index.js # Telegram bot (grammy + SSE streaming) โ”‚ โ”‚ โ”œโ”€โ”€ message-sender.js # StreamConsumer + markdownToHtml converter โ”‚ โ”‚ โ”œโ”€โ”€ 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 โ”œโ”€โ”€ .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. **Self-Correction**: `self-correction.js` (2 retries + exponential backoff + auto-simplification) 5. **AI Chat + Streaming**: `chatWithAI()` โ†’ SSE stream โ†’ `StreamConsumer` โ†’ real-time edits 6. **Formatting**: `markdownToHtml()` converts AI markdown โ†’ Telegram HTML 7. **Final Delivery**: `editMessageText` with HTML parse_mode (or fallback to stripped plain text) ### 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 **)
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

## ๐Ÿ“Š 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 | โœ… Multi-agent (swarm) | โœ… delegate_task + batch | โŒ Single agent only |
| Agent roles | โœ… Code Reviewer, Architect, DevOps | โœ… Agent Registry (10+ roles) | โŒ Fixed single role |
| Self-correction loops | โœ… 2 retries + backoff + auto-simplification | โœ… Agent self-correction skill | โŒ None |
| **Streaming** | | | |
| Real-time SSE streaming | โœ… StreamConsumer (edit-in-place) | โœ… GatewayStreamConsumer | โŒ 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 |
| Discord | โœ… Native bot (discord.js) | โœ… Full Discord integration | โŒ None |
| Multi-channel delivery | โœ… Delivery hub (TG + DC + WS + log) | โœ… Cronโ†’multi-platform | โŒ None |
| **Infrastructure** | | | |
| Model routing | โœ… Multi-provider | โœ… Multi-provider routing | โŒ Single model |
| Context compression | โœ… Compact pipeline | โœ… lean-ctx MCP (90% savings) | โŒ None |
| Memory persistence | โœ… Session memory | โœ… Cross-session memory | โŒ None |
| Cron scheduling | โœ… 1s interval, jitter, locks | โœ… Cron jobs with delivery | โŒ None |

### Summary

- **zCode CLI X** โ€” Lightweight agentic coder focused on Telegram + Z.AI. Real-time SSE streaming, self-correction loops, RTK optimization, and beautiful HTML formatting. 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 + runner + 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)

## ๐Ÿค 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 credit)

---

Built with โšก by zCode CLI X