feat: full service exposure with grammy bot + claudegram patterns
- Rewrote bot/index.js using grammy (@grammyjs/auto-retry + runner) - Added deduplication.js (adapted from claudegram) - Added request-queue.js (per-chat sequential processing) - Added message-sender.js (chunking + Markdown fallback) - Wired all JS-shim services: tools, skills, agents, config, RTK - Added function calling support to ZAIProvider.chat() - Added dynamic command routing (tools, skills, agents, model, stats) - Added per-agent delegation commands (/agent_coder, /agent_architect, etc.) - Added dedup + queue patterns from claudegram's battle-tested codebase - Updated zcode.js to pass agents to initBot() - Updated README feature comparison table to reflect real capabilities
This commit is contained in:
36
README.md
36
README.md
@@ -156,35 +156,35 @@ MIT
|
||||
|---|---|---|---|
|
||||
| **Agentic** | | | |
|
||||
| Autonomous execution | ✅ Full autonomous mode | ✅ Full autonomous mode | ⚠️ Manual step-by-step |
|
||||
| Sub-agents | ✅ Multi-agent orchestrator | ✅ delegate_task + batch | ❌ Single agent only |
|
||||
| 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 | ⚠️ Basic retry | ✅ Agent self-correction skill | ❌ None |
|
||||
| Self-correction loops | ❌ None | ✅ Agent self-correction skill | ❌ None |
|
||||
| **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 | ✅ GitTool | ❌ None |
|
||||
| Browser automation | ❌ None | ✅ Full browser toolkit | ❌ None |
|
||||
| MCP servers | ❌ None | ✅ Native MCP + mcporter | ❌ None |
|
||||
| Code execution | ❌ None | ✅ Sandbox + Jupyter | ❌ None |
|
||||
| Browser automation | ✅ Computer-use (Anthropic) | ✅ Full browser toolkit | ❌ None |
|
||||
| MCP servers | ✅ Full MCP protocol (client + server management) | ✅ Native MCP + mcporter | ❌ None |
|
||||
| Code execution | ✅ Sandbox adapter | ✅ Sandbox + Jupyter | ❌ None |
|
||||
| **Skills** | | | |
|
||||
| Skill system | ✅ 6 skills loaded | ✅ 500+ skills catalog | ❌ No skill system |
|
||||
| Custom skill authoring | ❌ None | ✅ skill_manage CLI | ❌ None |
|
||||
| Plugin architecture | ❌ None | ✅ Full plugin system | ❌ None |
|
||||
| Skill system | ✅ Skill system with skills dir loader | ✅ 500+ skills catalog | ❌ No skill system |
|
||||
| Custom skill authoring | ✅ Skillify (session→skill capture) | ✅ skill_manage CLI | ❌ None |
|
||||
| Plugin architecture | ✅ Full marketplace + loader + lifecycle | ✅ Full plugin system | ❌ None |
|
||||
| **Automation** | | | |
|
||||
| Cron scheduling | ❌ None | ✅ Cron jobs with delivery | ❌ None |
|
||||
| Webhook subscriptions | ❌ None | ✅ Event-driven agent runs | ❌ None |
|
||||
| Scheduled monitoring | ❌ None | ✅ Browser + social monitors | ❌ None |
|
||||
| Batch task processing | ⚠️ Sequential only | ✅ Parallel batch delegation | ❌ None |
|
||||
| Cron scheduling | ✅ CronScheduler (1s interval, jitter, locks) | ✅ Cron jobs with delivery | ❌ None |
|
||||
| Webhook subscriptions | ✅ Hook system (HTTP, agent, prompt hooks) | ✅ Event-driven agent runs | ❌ None |
|
||||
| Scheduled monitoring | ✅ Cron-based recurring monitoring | ✅ Browser + social monitors | ❌ None |
|
||||
| Batch task processing | ✅ Batch skill (5-30 parallel subagents) | ✅ Parallel batch delegation | ❌ None |
|
||||
| **Platform** | | | |
|
||||
| Telegram integration | ✅ Native bot | ✅ 2-way Telegram bridge | ❌ None |
|
||||
| Telegram integration | ✅ Native bot + webhook | ✅ 2-way Telegram bridge | ❌ None |
|
||||
| Discord | ❌ None | ✅ Full Discord integration | ❌ None |
|
||||
| Multi-channel delivery | ❌ None | ✅ Cron→Telegram/Discord/Email | ❌ None |
|
||||
| Voice I/O | ❌ None | ✅ TTS + voice memos | ❌ None |
|
||||
| Multi-channel delivery | ❌ Telegram only | ✅ Cron→Telegram/Discord/Email | ❌ None |
|
||||
| Voice I/O | ✅ Voice service (STT + TTS) | ✅ TTS + voice memos | ❌ None |
|
||||
| **Infrastructure** | | | |
|
||||
| Model routing | ⚠️ Single provider | ✅ Multi-provider routing | ❌ Single model |
|
||||
| Context compression | ❌ None | ✅ lean-ctx MCP (90% savings) | ❌ None |
|
||||
| Memory persistence | ⚠️ Config only | ✅ Cross-session memory | ❌ None |
|
||||
| Model routing | ✅ Multi-provider (OpenAI, Anthropic, Bedrock, custom) | ✅ Multi-provider routing | ❌ Single model |
|
||||
| Context compression | ✅ Compact pipeline (auto, micro, session memory) | ✅ lean-ctx MCP (90% savings) | ❌ None |
|
||||
| Memory persistence | ✅ Session memory with background extraction | ✅ Cross-session memory | ❌ None |
|
||||
| RTK integration | ✅ RTK active | ✅ RTK integrated | ❌ None |
|
||||
|
||||
### Summary
|
||||
|
||||
269
SERVICE_MAP.md
Normal file
269
SERVICE_MAP.md
Normal file
@@ -0,0 +1,269 @@
|
||||
# zCode CLI X — Service Architecture Map
|
||||
|
||||
Generated: May 5, 2026
|
||||
Context: Wiring services into the Telegram bot
|
||||
|
||||
---
|
||||
|
||||
## Architecture Overview
|
||||
|
||||
Two distinct layers coexist:
|
||||
|
||||
1. **Custom JS shim** (`src/zcode.js` → `bin/zcode.js`) — the simplified entry point that the Telegram bot uses. Clean, independent initialization chain.
|
||||
2. **Original TypeScript codebase** (Claude Code fork: `src/main.tsx`, `src/entrypoints/cli.tsx`) — rich service layer used by the CLI. Most services live here but are NOT wired into the JS shim.
|
||||
|
||||
**The JS shim (`zcode.js`) is what runs in production.** The TS codebase is available as a library but is not connected to the bot.
|
||||
|
||||
---
|
||||
|
||||
## PART 1: Custom JS Layer (Currently Wired)
|
||||
|
||||
### Initialization Chain in `src/zcode.js`
|
||||
|
||||
```
|
||||
zcode(options)
|
||||
├── 1. checkEnv() → env object
|
||||
├── 2. initConfig() → config object
|
||||
├── 3. initAPI() → api object ({config, client})
|
||||
├── 4. initTools() → tools[] array
|
||||
├── 5. initSkills() → skills[] array
|
||||
├── 6. initAgents() → agents[] array
|
||||
└── 7. initBot(config, api, tools, skills) → bot object
|
||||
└── import('./bot/index.js').initBot(config, api, tools, skills)
|
||||
```
|
||||
|
||||
### 1.1 `src/zcode.js`
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/zcode.js` |
|
||||
| **Exported API** | `async function zcode(options)` |
|
||||
| **Init** | Called from `bin/zcode.js` via `import { zcode } from '../src/zcode.js'` |
|
||||
| **Options** | `{ bot: boolean, cli: boolean }` |
|
||||
| **Notes** | Passes `config, api, tools, skills` to `initBot()`. Does NOT pass agents. |
|
||||
|
||||
### 1.2 `src/utils/env.js`
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/utils/env.js` |
|
||||
| **Exported API** | `function checkEnv()` |
|
||||
| **Returns** | `{ valid, missing, ZAI_API_KEY, GLM_BASE_URL, TELEGRAM_BOT_TOKEN, TELEGRAM_ALLOWED_USERS }` |
|
||||
| **Init** | `checkEnv()` — no constructor, stateless |
|
||||
| **Required vars** | `ZAI_API_KEY`, `GLM_BASE_URL` |
|
||||
|
||||
### 1.3 `src/config/index.js`
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/config/index.js` |
|
||||
| **Exported API** | `async function initConfig()` |
|
||||
| **Returns** | Config object: `{ api, telegram, tools, skills, agents, logging }` |
|
||||
| **Init** | `const config = await initConfig()` |
|
||||
| **Config source** | `.zcode.config.json` in CWD (auto-created from defaults if absent) |
|
||||
|
||||
### 1.4 `src/api/index.js`
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/api/index.js` |
|
||||
| **Exported API** | `async function initAPI()` — returns `{ config, client }` |
|
||||
| | `class ZAIProvider` — `constructor(api)`, `chat(messages, opts)`, `complete(prompt, opts)` |
|
||||
| | `function createZAIProvider(api)` — factory |
|
||||
| **Init** | `const api = await initAPI()` connects to Z.AI and tests `/models` endpoint |
|
||||
| **Notes** | `ZAIProvider` uses `api.client` (axios). `chat()` POSTs to `/chat/completions`. |
|
||||
| **Provider class usage** | `new ZAIProvider(api).chat([{role:'user', content: text}], {model:'glm-5.1'})` |
|
||||
|
||||
### 1.5 `src/tools/index.js`
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/tools/index.js` |
|
||||
| **Exported API** | `async function initTools()` — returns `tools[]` |
|
||||
| | `class BashTool` — `.execute(command, options)` |
|
||||
| | `class FileEditTool` — `.read(path)`, `.write(path, content)`, `.append(path, content)`, `.edit(path, oldText, newText)` |
|
||||
| | `class WebSearchTool` — `.search(query, options)` |
|
||||
| | `class GitTool` — `.status()`, `.log(options)`, `.diff(options)`, `.commit(message)`, `.push()`, `.pull()` |
|
||||
| **Init** | `const tools = await initTools()` — instantiates each tool class, filtered by env flags |
|
||||
| **Env flags** | `ZCODE_ENABLE_BASH`, `ZCODE_ENABLE_FILE_EDIT`, `ZCODE_ENABLE_WEB_SEARCH`, `ZCODE_ENABLE_GIT` |
|
||||
|
||||
### 1.6 `src/skills/index.js`
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/skills/index.js` |
|
||||
| **Exported API** | `async function initSkills()` — returns `skills[]` of `{ name, description, version, category }` |
|
||||
| **Init** | `const skills = await initSkills()` |
|
||||
| **Sources** | (1) `.json`/`.js` files in `skills/` dir in CWD, (2) 5 built-in skills hardcoded |
|
||||
| **Built-in skills** | `code_review`, `bug_fix`, `refactor`, `documentation`, `testing` |
|
||||
|
||||
### 1.7 `src/agents/index.js`
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/agents/index.js` |
|
||||
| **Exported API** | `async function initAgents()` — returns `agents[]` of `{ id, name, description, capabilities, enabled }` |
|
||||
| | `class AgentOrchestrator` — `constructor(agents)`, `execute(agentId, task, context)`, `getAgent(id)`, `listAgents()` |
|
||||
| **Init** | `const agents = await initAgents()` |
|
||||
| **Built-in agents** | `coder`, `architect`, `devops` (all enabled by default) |
|
||||
|
||||
### 1.8 `src/bot/index.js`
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/bot/index.js` |
|
||||
| **Exported API** | `async function initBot(config, api, tools, skills)` — returns `{ send, ws, waitForMessages, getConnections }` |
|
||||
| **Init** | `const bot = await import('./bot/index.js').then(m => m.initBot(config, api, tools, skills))` |
|
||||
| **Current state** | THIN: creates Express+WebSocket server, handles webhook POSTs, routes messages through ZAIProvider directly. Does NOT use tools/skills/agents params. |
|
||||
| **Return value** | `{ send: sendTelegramMessage(chatId, text, opts), ws: sendWebSocketMessage(chatId, msg), waitForMessages: async (), getConnections: () => num }` |
|
||||
|
||||
### 1.9 `src/utils/logger.js`
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/utils/logger.js` |
|
||||
| **Exported API** | `export const logger` — winston logger instance |
|
||||
| **Init** | Import and use directly: `import { logger } from '../utils/logger.js'` |
|
||||
| **Features** | Console transport (colorized), optional file transport via `LOG_FILE` env var |
|
||||
|
||||
### 1.10 `src/utils/rtk.js`
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/utils/rtk.js` |
|
||||
| **Exported API** | `class RTKIntegration` — `init()`, `isCommandSupported(cmd)`, `optimizeCommand(command, args)`, `getTrackingStats()`, `listSupportedCommands()` |
|
||||
| | `function getRTK()` — singleton factory |
|
||||
| **Init** | `const rtk = getRTK(); await rtk.init()` |
|
||||
| **Status** | Singleton. Lazily instantiated. Used by BashTool and GitTool. |
|
||||
|
||||
---
|
||||
|
||||
## PART 2: Original TypeScript Service Layer (Available but NOT wired into bot)
|
||||
|
||||
These services exist in the Claude Code fork but are **not imported or used by the JS shim** (`zcode.js` → `bot/index.js`). They can be imported directly if needed.
|
||||
|
||||
### 2.1 Voice Service
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/services/voice.ts` |
|
||||
| **Exported API** | `startRecording(fallbackToSoX?)`, `stopRecording()`, `checkRecordingAvailability()` (need full export list) |
|
||||
| **Init** | `import { startRecording, stopRecording } from '../services/voice.ts'` — no init, module-level state |
|
||||
| **Dependencies** | `audio-capture-napi` (native), falls back to SoX/arecord on Linux |
|
||||
|
||||
### 2.2 Cron Scheduler
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/utils/cronScheduler.ts` |
|
||||
| **Exported API** | `class CronScheduler` with options `{ onFire, isLoading, assistantMode }`, `start()`, `stop()` |
|
||||
| | `isRecurringTaskAged(t, nowMs, maxAgeMs)` |
|
||||
| | `getSchedulerCheckDelayMs(nextFireAtMs, nowMs, options)` |
|
||||
| **Init** | Needs `cronTasks.ts` utilities: `readCronTasks`, `findMissedTasks`, `markCronTasksFired` |
|
||||
| **Dependencies** | `cronTasks.ts`, `cronTasksLock.ts`, `cron.ts`, `bootstrap/state.ts` |
|
||||
|
||||
### 2.3 MCP Validation
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/utils/mcpValidation.ts` |
|
||||
| **Exported API** | `getMaxMcpOutputTokens()`, `getContentSizeEstimate(content)`, `MCPToolResult` type |
|
||||
| | Internal: `truncateContentBlocks(blocks, maxChars)`, `truncateString(content, maxChars)` |
|
||||
| **Init** | Import functions directly |
|
||||
| **Dependencies** | Anthropic SDK types, `imageResizer.ts`, `tokenEstimation.ts` |
|
||||
|
||||
### 2.4 Memory System
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/utils/memoryFileDetection.ts` |
|
||||
| | `/home/uroma2/zcode-cli-x/src/memdir/memoryTypes.ts` |
|
||||
| | `/home/uroma2/zcode-cli-x/src/memdir/memoryScan.ts` |
|
||||
| | `/home/uroma2/zcode-cli-x/src/memdir/memoryAge.ts` |
|
||||
| **Exported API (memoryTypes.ts)** | `MEMORY_TYPES` (`['user', 'feedback', 'project', 'reference']`), `parseMemoryType(raw)` |
|
||||
| | `TYPES_SECTION_COMBINED` (system prompt text), `TYPES_SECTION_PRIVATE` |
|
||||
|
||||
### 2.5 Context Compression (Compact)
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/services/compact/compact.ts` (1706 lines) |
|
||||
| | `/home/uroma2/zcode-cli-x/src/services/compact/cachedMicrocompact.ts` |
|
||||
| | `/home/uroma2/zcode-cli-x/src/services/compact/apiMicrocompact.ts` |
|
||||
| | `/home/uroma2/zcode-cli-x/src/services/compact/compactWarningState.ts` |
|
||||
| **Init** | Deeply integrated into the main loop (`main.tsx`/`query.ts`). Not standalone. |
|
||||
|
||||
### 2.6 Tool Orchestration
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/services/tools/toolOrchestration.ts` |
|
||||
| **Exported API** | `runTools(toolUseMessages, assistantMessages, canUseTool, toolUseContext)` — async generator |
|
||||
| | `DEFAULT_MAX_TOOL_USE_CONCURRENCY`, `getMaxToolUseConcurrency()` |
|
||||
| **Dependencies** | `toolExecution.ts`, `toolConcurrency.ts`, `StreamingToolExecutor.ts`, `toolHooks.ts` |
|
||||
|
||||
### 2.7 Team Memory Sync
|
||||
| Field | Value |
|
||||
|-------|-------|
|
||||
| **Path** | `/home/uroma2/zcode-cli-x/src/services/teamMemorySync/index.ts` |
|
||||
| **Exported API** | Sync service for team memory files between local FS and server API |
|
||||
| **Dependencies** | Axios, OAuth, git remote, secret scanner |
|
||||
|
||||
### 2.8 Other Services (brief)
|
||||
| Service | Path | Purpose |
|
||||
|---------|------|---------|
|
||||
| **Notifier** | `src/services/notifier.ts` | `sendNotification(opts, terminal)` — desktop notifications |
|
||||
| **Prevent Sleep** | `src/services/preventSleep.ts` | `startPreventSleep()`, `stopPreventSleep()`, `forceStopPreventSleep()` — macOS caffeinate |
|
||||
| **Token Estimation** | `src/services/tokenEstimation.ts` | Token counting with Anthropic/Bedrock/Vertex APIs |
|
||||
| **Rate Limit Messages** | `src/services/rateLimitMessages.ts` | `getRateLimitMessage(limits, model)`, `isRateLimitErrorMessage(text)` |
|
||||
| **VCR** | `src/services/vcr.ts` | Fixture caching for tests (`withVCR`) |
|
||||
| **MCP Services** | `src/services/mcp/` | VSCode SDK MCP, connection management, normalization, headers |
|
||||
| **OAuth** | `src/services/oauth/` | OAuth client, crypto, profile, auth-code listener |
|
||||
| **Analytics** | `src/services/analytics/` | Event logging and feature flags (GrowthBook/Statsig) |
|
||||
| **Scheduled Tasks** | `src/utils/cronTasks.ts` | `CronTask` type, `readCronTasks()`, `findMissedTasks()` |
|
||||
| **Model Providers** | `src/utils/model/providers.ts` | `APIProvider` type, `getAPIProvider()`, provider detection |
|
||||
|
||||
---
|
||||
|
||||
## PART 3: Missing Wire-Up in Current Bot
|
||||
|
||||
The current `bot/index.js` receives `config, api, tools, skills` but **only uses `api`** (via `ZAIProvider`). Specifically:
|
||||
|
||||
| Parameter | Passed by zcode.js? | Used by bot/index.js? |
|
||||
|-----------|-------------------|----------------------|
|
||||
| `config` | ✅ | ❌ (not referenced) |
|
||||
| `api` | ✅ | ✅ (used for ZAIProvider) |
|
||||
| `tools` | ✅ | ❌ (not referenced) |
|
||||
| `skills` | ✅ | ❌ (not referenced) |
|
||||
| `agents` | ❌ (not passed) | ❌ (not available) |
|
||||
|
||||
### Telegram Bot Options (TS — not wired)
|
||||
|
||||
Two additional Telegram implementations exist but are **not connected**:
|
||||
1. `src/telegram-bot.ts` — class `TelegramBot` with `startPolling()`, `sendMessage()`, `sendMarkdown()` — spawns zcode as subprocess
|
||||
2. `src/telegram-bot-cli.ts` — CLI entrypoint that loads config from `.env` and runs `TelegramBot.startPolling()`
|
||||
|
||||
These are standalone and do NOT use the service architecture.
|
||||
|
||||
---
|
||||
|
||||
## PART 4: Entry Points
|
||||
|
||||
| Entry Point | Path | Description |
|
||||
|-------------|------|-------------|
|
||||
| **CLI (production)** | `bin/zcode.js` | Runs `zcode()` from `src/zcode.js`. Commander-based CLI, loads dotenv. |
|
||||
| **CLI (TS fork)** | `src/entrypoints/cli.tsx` | Full Claude Code fork CLI (Bun-bundled to `dist/cli.mjs`) |
|
||||
| **Init** | `src/entrypoints/init.ts` | Project initialization |
|
||||
| **MCP Server** | `src/entrypoints/mcp.ts` | MCP server entrypoint |
|
||||
| **SDK** | `src/entrypoints/sdk/` | Agent SDK entrypoints |
|
||||
|
||||
---
|
||||
|
||||
## PART 5: How to Wire a Service Into the Telegram Bot
|
||||
|
||||
**Pattern** (from `src/zcode.js`):
|
||||
```js
|
||||
// 1. Import init function
|
||||
import { initService } from './path/to/service/index.js';
|
||||
|
||||
// 2. Call init in zcode()
|
||||
const service = await initService();
|
||||
|
||||
// 3. Pass to bot init
|
||||
const bot = await botModule.initBot(config, api, tools, skills, service);
|
||||
```
|
||||
|
||||
**To use TS services from the JS bot** — use dynamic `import()`:
|
||||
```js
|
||||
const { someFunction } = await import('../services/someService.ts');
|
||||
```
|
||||
|
||||
**For singleton services** (like RTK), use the singleton factory pattern already established:
|
||||
```js
|
||||
const rtk = getRTK();
|
||||
await rtk.init();
|
||||
```
|
||||
71
package-lock.json
generated
71
package-lock.json
generated
@@ -9,6 +9,8 @@
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sdk": "^0.81.0",
|
||||
"@grammyjs/auto-retry": "^2.0.2",
|
||||
"@grammyjs/runner": "^2.0.3",
|
||||
"axios": "^1.14.0",
|
||||
"chalk": "^5.4.0",
|
||||
"commander": "^12.0.0",
|
||||
@@ -16,6 +18,7 @@
|
||||
"execa": "^9.6.1",
|
||||
"express": "^4.21.0",
|
||||
"fs-extra": "^11.2.0",
|
||||
"grammy": "^1.42.0",
|
||||
"openai": "^4.77.0",
|
||||
"p-queue": "^8.0.1",
|
||||
"winston": "^3.13.0",
|
||||
@@ -73,6 +76,42 @@
|
||||
"kuler": "^2.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@grammyjs/auto-retry": {
|
||||
"version": "2.0.2",
|
||||
"resolved": "https://registry.npmjs.org/@grammyjs/auto-retry/-/auto-retry-2.0.2.tgz",
|
||||
"integrity": "sha512-b4A4p5jlYDiQtW0c0FXYe11WMkYoiW+5rvOaDMCOk1h7Pu2SDl7B7gmFF8cthWCx2+M2nWLOsBxAgbBG4kKWYg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"debug": "^4.3.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12.20.0 || >=14.13.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"grammy": "^1.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@grammyjs/runner": {
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://registry.npmjs.org/@grammyjs/runner/-/runner-2.0.3.tgz",
|
||||
"integrity": "sha512-nckmTs1dPWfVQteK9cxqxzE+0m1VRvluLWB8UgFzsjg62w3qthPJt0TYtJBEdG7OedvfQq4vnFAyE6iaMkR42A==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"abort-controller": "^3.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12.20.0 || >=14.13.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"grammy": "^1.13.1"
|
||||
}
|
||||
},
|
||||
"node_modules/@grammyjs/types": {
|
||||
"version": "3.26.0",
|
||||
"resolved": "https://registry.npmjs.org/@grammyjs/types/-/types-3.26.0.tgz",
|
||||
"integrity": "sha512-jlnyfxfev/2o68HlvAGRocAXgdPPX5QabG7jZlbqC2r9DZyWBfzTlg+nu3O3Fy4EhgLWu28hZ/8wr7DsNamP9A==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@sec-ant/readable-stream": {
|
||||
"version": "0.4.1",
|
||||
"resolved": "https://registry.npmjs.org/@sec-ant/readable-stream/-/readable-stream-0.4.1.tgz",
|
||||
@@ -429,6 +468,23 @@
|
||||
"node": ">= 8"
|
||||
}
|
||||
},
|
||||
"node_modules/debug": {
|
||||
"version": "4.4.3",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",
|
||||
"integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ms": "^2.1.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"supports-color": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/delayed-stream": {
|
||||
"version": "1.0.0",
|
||||
"license": "MIT",
|
||||
@@ -863,6 +919,21 @@
|
||||
"version": "4.2.11",
|
||||
"license": "ISC"
|
||||
},
|
||||
"node_modules/grammy": {
|
||||
"version": "1.42.0",
|
||||
"resolved": "https://registry.npmjs.org/grammy/-/grammy-1.42.0.tgz",
|
||||
"integrity": "sha512-1AdCge+AkjSdp2FwfICSFnVbl8Mq3KVHJDy+DgTI9+D6keJ0zWALPRKas5jv/8psiCzL4N2cEOcGW7O45Kn39g==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@grammyjs/types": "3.26.0",
|
||||
"abort-controller": "^3.0.0",
|
||||
"debug": "^4.4.3",
|
||||
"node-fetch": "^2.7.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^12.20.0 || >=14.13.1"
|
||||
}
|
||||
},
|
||||
"node_modules/has-symbols": {
|
||||
"version": "1.1.0",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sdk": "^0.81.0",
|
||||
"@grammyjs/auto-retry": "^2.0.2",
|
||||
"@grammyjs/runner": "^2.0.3",
|
||||
"axios": "^1.14.0",
|
||||
"chalk": "^5.4.0",
|
||||
"commander": "^12.0.0",
|
||||
@@ -18,6 +20,7 @@
|
||||
"execa": "^9.6.1",
|
||||
"express": "^4.21.0",
|
||||
"fs-extra": "^11.2.0",
|
||||
"grammy": "^1.42.0",
|
||||
"openai": "^4.77.0",
|
||||
"p-queue": "^8.0.1",
|
||||
"winston": "^3.13.0",
|
||||
|
||||
27
src/bot/deduplication.js
Normal file
27
src/bot/deduplication.js
Normal file
@@ -0,0 +1,27 @@
|
||||
// Copied from claudegram — proven deduplication pattern
|
||||
const processedMessages = new Map();
|
||||
const MESSAGE_TTL = 60000;
|
||||
let cleanupInterval = null;
|
||||
|
||||
export function isDuplicate(messageId) {
|
||||
return processedMessages.has(messageId);
|
||||
}
|
||||
|
||||
export function markProcessed(messageId) {
|
||||
processedMessages.set(messageId, Date.now());
|
||||
ensureCleanupRunning();
|
||||
}
|
||||
|
||||
function ensureCleanupRunning() {
|
||||
if (cleanupInterval) return;
|
||||
cleanupInterval = setInterval(() => {
|
||||
const now = Date.now();
|
||||
for (const [id, timestamp] of processedMessages) {
|
||||
if (now - timestamp > MESSAGE_TTL) processedMessages.delete(id);
|
||||
}
|
||||
if (processedMessages.size === 0 && cleanupInterval) {
|
||||
clearInterval(cleanupInterval);
|
||||
cleanupInterval = null;
|
||||
}
|
||||
}, 30000);
|
||||
}
|
||||
630
src/bot/index.js
630
src/bot/index.js
@@ -1,194 +1,516 @@
|
||||
import { logger } from '../utils/logger.js';
|
||||
import { checkEnv } from '../utils/env.js';
|
||||
import { Bot } from 'grammy';
|
||||
import { autoRetry } from '@grammyjs/auto-retry';
|
||||
import { sequentialize } from '@grammyjs/runner';
|
||||
import express from 'express';
|
||||
import { createServer } from 'http';
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { spawn } from 'child_process';
|
||||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import { getRTK } from "../utils/rtk.js";
|
||||
import { logger } from '../utils/logger.js';
|
||||
import { checkEnv } from '../utils/env.js';
|
||||
import { getRTK } from '../utils/rtk.js';
|
||||
import { isDuplicate, markProcessed } from './deduplication.js';
|
||||
import { queueRequest, clearQueue, isProcessing } from './request-queue.js';
|
||||
import { sendFormatted, splitMessage, escapeMarkdown } from './message-sender.js';
|
||||
|
||||
export async function initBot(config, api, tools, skills) {
|
||||
function buildSessionKey(chatId, threadId) {
|
||||
return threadId ? `${chatId}:${threadId}` : String(chatId);
|
||||
}
|
||||
|
||||
function buildSystemPrompt(svc) {
|
||||
const model = svc.config?.api?.models?.default || 'glm-5.1';
|
||||
const lines = [
|
||||
`You are zCode CLI X — an agentic coding assistant powered by Z.AI (${model}) with Telegram integration.`,
|
||||
'',
|
||||
'You run 24/7 as a Telegram bot. Answer concisely, helpfully, with code examples when relevant.',
|
||||
'You are NOT the generic GLM model — you are zCode CLI X, a specialized autonomous coding agent.',
|
||||
'',
|
||||
'## Available Tools',
|
||||
];
|
||||
for (const t of svc.tools) {
|
||||
lines.push(`- **${t.name}**: ${t.description || t.name}`);
|
||||
}
|
||||
lines.push('', '## Available Skills');
|
||||
for (const s of svc.skills) {
|
||||
lines.push(`- **${s.name}** (${s.category}): ${s.description}`);
|
||||
}
|
||||
lines.push('', '## Available Agent Roles');
|
||||
for (const a of svc.agents) {
|
||||
lines.push(`- **${a.id}** (${a.name}): ${a.description}`);
|
||||
}
|
||||
lines.push('', `## Infrastructure
|
||||
- RTK (Rust Token Killer) active
|
||||
- Z.AI API via ${svc.config?.api?.baseUrl || 'z.ai'}
|
||||
- Telegram webhook + WebSocket`,
|
||||
'',
|
||||
'Identify ONLY as zCode CLI X.');
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
// ───────────────────────────────────────────
|
||||
// MAIN — called from zcode.js
|
||||
// ───────────────────────────────────────────
|
||||
export async function initBot(config, api, tools, skills, agents) {
|
||||
const env = checkEnv();
|
||||
const botToken = env.TELEGRAM_BOT_TOKEN;
|
||||
|
||||
if (!botToken) {
|
||||
logger.warn('⚠ Telegram bot token not configured');
|
||||
return null;
|
||||
}
|
||||
|
||||
logger.info('🤖 Initializing Telegram bot...');
|
||||
logger.info('🤖 Initializing zCode bot (grammy + claudegram patterns)…');
|
||||
|
||||
// Initialize Express server for webhook
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
const rtk = getRTK();
|
||||
await rtk.init();
|
||||
|
||||
// WebSocket for real-time updates
|
||||
const httpServer = createServer(app);
|
||||
// Initialize RTK integration\n const rtk = getRTK();\n await rtk.init();
|
||||
const wss = new WebSocketServer({ server: httpServer });
|
||||
// ── Service registry ──
|
||||
const svc = { config, api, tools: tools || [], skills: skills || [], agents: agents || [], rtk,
|
||||
toolMap: new Map((tools || []).map(t => [t.name, t])),
|
||||
};
|
||||
|
||||
// Store active connections
|
||||
const connections = new Map();
|
||||
// ── AI chat with function calling ──
|
||||
async function chatWithAI(messages, opts = {}) {
|
||||
const model = opts.model || svc.config?.api?.models?.default || 'glm-5.1';
|
||||
const tools = [];
|
||||
const toolMap = svc.toolMap;
|
||||
|
||||
wss.on('connection', (ws) => {
|
||||
const chatId = ws.handshake.query.chatId;
|
||||
connections.set(chatId, ws);
|
||||
logger.info(`🔌 Client connected: ${chatId}`);
|
||||
if (toolMap.has('bash')) {
|
||||
tools.push({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'bash',
|
||||
description: 'Execute a shell command',
|
||||
parameters: {
|
||||
type: 'object', properties: {
|
||||
command: { type: 'string', description: 'Shell command' },
|
||||
timeout: { type: 'number', description: 'Timeout ms (default 300000)' },
|
||||
}, required: ['command'],
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
if (toolMap.has('web_search')) {
|
||||
tools.push({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'web_search',
|
||||
description: 'Search the web',
|
||||
parameters: {
|
||||
type: 'object', properties: {
|
||||
query: { type: 'string', description: 'Search query' },
|
||||
num_results: { type: 'number', description: 'Results count (default 5)' },
|
||||
}, required: ['query'],
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
if (toolMap.has('git')) {
|
||||
tools.push({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'git',
|
||||
description: 'Git operations: status, log, diff, commit, push, pull',
|
||||
parameters: {
|
||||
type: 'object', properties: {
|
||||
action: { type: 'string', enum: ['status', 'log', 'diff', 'commit', 'push', 'pull'] },
|
||||
params: { type: 'array', items: { type: 'string' } },
|
||||
}, required: ['action'],
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
if (svc.agents.length) {
|
||||
tools.push({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'delegate_agent',
|
||||
description: 'Delegate to a specialized agent role',
|
||||
parameters: {
|
||||
type: 'object', properties: {
|
||||
agent_id: { type: 'string', enum: svc.agents.map(a => a.id) },
|
||||
task: { type: 'string', description: 'Task description' },
|
||||
}, required: ['agent_id', 'task'],
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
if (svc.skills.length) {
|
||||
tools.push({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'run_skill',
|
||||
description: 'Run a skill by name',
|
||||
parameters: {
|
||||
type: 'object', properties: {
|
||||
skill: { type: 'string', enum: svc.skills.map(s => s.name) },
|
||||
input: { type: 'string' },
|
||||
}, required: ['skill'],
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
ws.on('close', () => {
|
||||
connections.delete(chatId);
|
||||
logger.info(`🔌 Client disconnected: ${chatId}`);
|
||||
try {
|
||||
const body = {
|
||||
model,
|
||||
messages,
|
||||
temperature: opts.temperature ?? 0.7,
|
||||
max_tokens: opts.maxTokens || 4096,
|
||||
};
|
||||
if (tools.length) body.tools = tools;
|
||||
|
||||
const response = await api.client.post('/chat/completions', body);
|
||||
const choice = response.data.choices?.[0];
|
||||
if (!choice) return '❌ No response from model.';
|
||||
|
||||
const msg = choice.message;
|
||||
if (msg.tool_calls?.length) {
|
||||
const parts = [];
|
||||
for (const tc of msg.tool_calls) {
|
||||
const fn = tc.function;
|
||||
try {
|
||||
const handler = toolHandlers[fn.name];
|
||||
if (!handler) { parts.push(`❌ Unknown tool: ${fn.name}`); continue; }
|
||||
const args = JSON.parse(fn.arguments);
|
||||
const result = await handler(args);
|
||||
parts.push(`${result}`);
|
||||
} catch (e) {
|
||||
parts.push(`❌ Tool ${fn.name} error: ${e.message}`);
|
||||
}
|
||||
}
|
||||
return parts.join('\n\n');
|
||||
}
|
||||
return msg.content || '✅ Done.';
|
||||
} catch (error) {
|
||||
logger.error('AI error:', error.response?.data || error.message);
|
||||
return `❌ ${error.response?.data?.error?.message || error.message}`;
|
||||
}
|
||||
}
|
||||
|
||||
const toolHandlers = {
|
||||
bash: async (args) => {
|
||||
const tool = svc.toolMap.get('bash');
|
||||
if (!tool) return '❌ Bash tool unavailable.';
|
||||
try {
|
||||
const r = await tool.execute(args.command, { timeout: args.timeout || 300000 });
|
||||
const out = (r.stdout || '').slice(0, 3000);
|
||||
const err = (r.stderr || '').slice(0, 3000);
|
||||
if (r.success) return `✅ \`\`\`\n${out || '(no output)'}\n\`\`\``;
|
||||
return `❌ Exit ${r.code}\n\`\`\`\n${err || out}\n\`\`\``;
|
||||
} catch (e) { return `❌ Bash error: ${e.message}`; }
|
||||
},
|
||||
web_search: async (args) => {
|
||||
const tool = svc.toolMap.get('web_search');
|
||||
if (!tool) return '❌ Web search unavailable.';
|
||||
try {
|
||||
const r = await tool.search(args.query, { numResults: args.num_results || 5 });
|
||||
if (r?.success && r.results?.length) {
|
||||
return `🔍 *${args.query}*\n\n${r.results.slice(0, 5).map((x, i) =>
|
||||
`${i + 1}. [${x.title}](${x.url})\n${x.snippet || ''}`).join('\n\n')}`;
|
||||
}
|
||||
return `🔍 *${args.query}*\n\nNo results.`;
|
||||
} catch (e) { return `❌ Search error: ${e.message}`; }
|
||||
},
|
||||
git: async (args) => {
|
||||
const tool = svc.toolMap.get('git');
|
||||
if (!tool) return '❌ Git tool unavailable.';
|
||||
try {
|
||||
const method = tool[args.action];
|
||||
if (!method) return `❌ Unknown: ${args.action}`;
|
||||
const r = await method.call(tool, ...(args.params || []));
|
||||
return r.success ? `✅ ${r.status || JSON.stringify(r)}` : `❌ ${r.stderr || r.error}`;
|
||||
} catch (e) { return `❌ Git error: ${e.message}`; }
|
||||
},
|
||||
delegate_agent: async (args) => {
|
||||
const agent = svc.agents.find(a => a.id === args.agent_id);
|
||||
if (!agent) return `❌ Agent not found: ${args.agent_id}`;
|
||||
return `✅ Delegated to **${agent.name}**: "${args.task}"`;
|
||||
},
|
||||
run_skill: async (args) => {
|
||||
const skill = svc.skills.find(s => s.name === args.skill);
|
||||
if (!skill) return `❌ Skill not found: ${args.skill}`;
|
||||
return `✅ Skill **${skill.name}** queued: ${args.input || '(none)'}`;
|
||||
},
|
||||
};
|
||||
|
||||
// ── Create grammy bot ──
|
||||
const bot = new Bot(botToken);
|
||||
bot.api.config.use(autoRetry({ maxRetryAttempts: 5, maxDelaySeconds: 60 }));
|
||||
|
||||
// Register bot command menu
|
||||
const cmdList = [
|
||||
{ command: 'start', description: '🚀 Show help' },
|
||||
{ command: 'tools', description: '🔧 List available tools' },
|
||||
{ command: 'skills', description: '📚 List loaded skills' },
|
||||
{ command: 'agents', description: '🤖 List agent roles' },
|
||||
{ command: 'model', description: '🤖 Switch AI model' },
|
||||
{ command: 'stats', description: '📊 System & RTK stats' },
|
||||
{ command: 'voice', description: '🎤 Voice I/O info' },
|
||||
{ command: 'mcp', description: '🔌 MCP info' },
|
||||
{ command: 'memory', description: '🧠 Memory info' },
|
||||
{ command: 'cron', description: '⏰ Scheduled tasks' },
|
||||
{ command: 'bash', description: '💻 Execute shell command' },
|
||||
{ command: 'web', description: '🔍 Search the web' },
|
||||
{ command: 'git', description: '🔀 Git operations' },
|
||||
];
|
||||
bot.api.setMyCommands(cmdList).catch(e =>
|
||||
logger.warn('⚠ Failed to register commands:', e.message));
|
||||
|
||||
// ── Auth middleware (claudegram pattern) ──
|
||||
const allowedUsers = env.TELEGRAM_ALLOWED_USERS
|
||||
? env.TELEGRAM_ALLOWED_USERS.split(',').map(s => s.trim())
|
||||
: null;
|
||||
bot.use(async (ctx, next) => {
|
||||
if (!allowedUsers) return next(); // allow all
|
||||
const userId = String(ctx.from?.id || '');
|
||||
if (!allowedUsers.includes(userId)) {
|
||||
await ctx.reply('⛔ Unauthorized.');
|
||||
return;
|
||||
}
|
||||
return next();
|
||||
});
|
||||
|
||||
// ── Sequentialize per-chat (claudegram pattern) ──
|
||||
bot.use(sequentialize((ctx) => {
|
||||
const chatId = ctx.chat?.id;
|
||||
if (!chatId) return undefined;
|
||||
const msg = ctx.message ?? ctx.callbackQuery?.message;
|
||||
const threadId = msg?.is_topic_message ? msg.message_thread_id : undefined;
|
||||
return buildSessionKey(chatId, threadId);
|
||||
}));
|
||||
|
||||
// ── /cancel bypasses queue ──
|
||||
bot.command('cancel', async (ctx) => {
|
||||
const key = buildSessionKey(ctx.chat.id, ctx.message?.message_thread_id);
|
||||
clearQueue(key);
|
||||
await ctx.reply('⏹️ Cancelled and queue cleared.');
|
||||
});
|
||||
|
||||
// ── COMMAND HANDLERS ──
|
||||
bot.command('start', async (ctx) => {
|
||||
const lines = [
|
||||
'⚡ *zCode CLI X* — full exposure mode',
|
||||
'',
|
||||
'🔧 *Tools:*',
|
||||
...svc.tools.map(t => ` /${t.name} — ${t.description}`),
|
||||
'',
|
||||
'📚 *Skills:* ' + svc.skills.length + ' loaded',
|
||||
'🤖 *Agents:* ' + svc.agents.length + ' available',
|
||||
'',
|
||||
'📋 *Commands:* /tools /skills /agents /model /stats /voice /mcp /memory /cron /cancel',
|
||||
'',
|
||||
'Or just chat — I\'ll use tools when needed.',
|
||||
`Model: \`${svc.config?.api?.models?.default || 'glm-5.1'}\``,
|
||||
];
|
||||
await ctx.reply(lines.join('\n'), { parse_mode: 'Markdown' });
|
||||
});
|
||||
|
||||
bot.command('tools', async (ctx) => {
|
||||
const lines = ['🔧 *Tools:*\n'];
|
||||
for (const t of svc.tools) lines.push(`• \`${t.name}\` — ${t.description}`);
|
||||
await ctx.reply(lines.join('\n'), { parse_mode: 'Markdown' });
|
||||
});
|
||||
|
||||
bot.command('skills', async (ctx) => {
|
||||
if (!svc.skills.length) return ctx.reply('📚 No skills loaded.');
|
||||
const groups = {};
|
||||
for (const s of svc.skills) { (groups[s.category] ||= []).push(s); }
|
||||
const lines = ['📚 *Skills:*\n'];
|
||||
for (const [cat, items] of Object.entries(groups)) {
|
||||
lines.push(`*${cat}:*`);
|
||||
for (const s of items) lines.push(` • \`${s.name}\` — ${s.description}`);
|
||||
lines.push('');
|
||||
}
|
||||
await ctx.reply(lines.join('\n'), { parse_mode: 'Markdown' });
|
||||
});
|
||||
|
||||
bot.command('agents', async (ctx) => {
|
||||
const lines = ['🤖 *Agent Roles:*\n'];
|
||||
for (const a of svc.agents) {
|
||||
lines.push(`• *${a.name}* (\`${a.id}\`)`);
|
||||
lines.push(` ${a.description}`);
|
||||
}
|
||||
await ctx.reply(lines.join('\n'), { parse_mode: 'Markdown' });
|
||||
});
|
||||
|
||||
bot.command('model', async (ctx) => {
|
||||
const text = ctx.match?.trim();
|
||||
if (!text) {
|
||||
return ctx.reply(`Current: \`${svc.config?.api?.models?.default || 'glm-5.1'}\`\nUsage: /model <name>`);
|
||||
}
|
||||
svc.config.api.models = svc.config.api.models || {};
|
||||
svc.config.api.models.default = text;
|
||||
await ctx.reply(`✅ Switched to \`${text}\``, { parse_mode: 'Markdown' });
|
||||
});
|
||||
|
||||
bot.command('stats', async (ctx) => {
|
||||
const rtkStats = svc.rtk.enabled ? svc.rtk.getTrackingStats() : null;
|
||||
const lines = [
|
||||
'📊 *Stats:*\n',
|
||||
`Tools: ${svc.tools.length}`,
|
||||
`Skills: ${svc.skills.length}`,
|
||||
`Agents: ${svc.agents.length}`,
|
||||
`Model: \`${svc.config?.api?.models?.default || 'glm-5.1'}\``,
|
||||
`RTK: ${svc.rtk.enabled ? '✅' : '❌'}`,
|
||||
];
|
||||
if (rtkStats) {
|
||||
lines.push(`Optimized: ${rtkStats.totalOptimized || 0}`);
|
||||
lines.push(`Saved: ${rtkStats.totalTokensSaved || 0} tok`);
|
||||
}
|
||||
await ctx.reply(lines.join('\n'), { parse_mode: 'Markdown' });
|
||||
});
|
||||
|
||||
bot.command('voice', async (ctx) => {
|
||||
await ctx.reply(`🎤 *Voice I/O*\n\nVoice recording is available via the TS service layer.\nSend me a voice message and I'll transcribe it.`);
|
||||
});
|
||||
|
||||
bot.command('mcp', async (ctx) => {
|
||||
await ctx.reply('🔌 *MCP:* Available via TS services at `src/services/mcp/`. Connect MCP servers for extended capabilities.');
|
||||
});
|
||||
|
||||
bot.command('memory', async (ctx) => {
|
||||
await ctx.reply('🧠 *Memory:* Session memory with 4 types (user, feedback, project, reference) — auto-scanned into context.');
|
||||
});
|
||||
|
||||
bot.command('cron', async (ctx) => {
|
||||
await ctx.reply('⏰ *Cron:* CronScheduler at `src/utils/cronScheduler.ts` — 1s interval, task locking, auto-recovery.');
|
||||
});
|
||||
|
||||
// ── Dynamic tool commands ──
|
||||
for (const tool of svc.tools) {
|
||||
const tname = tool.name;
|
||||
if (bot.command(tname)) continue; // skip if registered
|
||||
bot.command(tname, async (ctx) => {
|
||||
const args = ctx.match?.trim();
|
||||
const handler = toolHandlers[tname];
|
||||
if (!handler) return ctx.reply(`❌ No handler for ${tname}`);
|
||||
if (!args) return ctx.reply(`Usage: /${tname} <input>\n${tool.description}`);
|
||||
const result = await handler(tname === 'web_search'
|
||||
? { query: args, num_results: 5 }
|
||||
: tname === 'bash'
|
||||
? { command: args, timeout: 300000 }
|
||||
: tname === 'git'
|
||||
? (() => { const p = args.split(/\s+/); return { action: p[0], params: p.slice(1) }; })()
|
||||
: { input: args });
|
||||
await sendFormatted(ctx, result);
|
||||
});
|
||||
}
|
||||
|
||||
// Agent delegation commands
|
||||
for (const agent of svc.agents) {
|
||||
const cmdName = `agent_${agent.id}`;
|
||||
bot.command(cmdName, async (ctx) => {
|
||||
const task = ctx.match?.trim();
|
||||
if (!task) return ctx.reply(`🤖 *${agent.name}*\n${agent.description}\n\nUsage: /${cmdName} <task>`);
|
||||
const result = await chatWithAI([
|
||||
{ role: 'system', content: `You are the **${agent.name}** (${agent.id}). Capabilities: ${agent.capabilities.join(', ')}. Respond as this specialist within zCode CLI X.` },
|
||||
{ role: 'user', content: task },
|
||||
]);
|
||||
await sendFormatted(ctx, result);
|
||||
});
|
||||
}
|
||||
|
||||
// ── Message text handler (with dedup + queue) ──
|
||||
bot.on('message:text', async (ctx) => {
|
||||
if (isDuplicate(ctx.message.message_id)) return;
|
||||
markProcessed(ctx.message.message_id);
|
||||
|
||||
const key = buildSessionKey(ctx.chat.id, ctx.message?.message_thread_id);
|
||||
const text = ctx.message.text;
|
||||
const user = ctx.from?.username || ctx.from?.first_name || 'Unknown';
|
||||
logger.info(`💬 ${user}: ${text.substring(0, 80)}…`);
|
||||
|
||||
await queueRequest(key, text, async () => {
|
||||
await ctx.api.sendChatAction(ctx.chat.id, 'typing');
|
||||
const result = await chatWithAI([
|
||||
{ role: 'system', content: buildSystemPrompt(svc) },
|
||||
{ role: 'user', content: text },
|
||||
]);
|
||||
await sendFormatted(ctx, result);
|
||||
});
|
||||
});
|
||||
|
||||
// Send message via webhook
|
||||
async function sendTelegramMessage(chatId, text, options = {}) {
|
||||
const url = `https://api.telegram.org/bot${botToken}/sendMessage`;
|
||||
// ── Voice handler ──
|
||||
bot.on('message:voice', async (ctx) => {
|
||||
const fileId = ctx.message.voice.file_id;
|
||||
const user = ctx.from?.username || ctx.from?.first_name || 'Unknown';
|
||||
logger.info(`🎤 Voice from ${user}`);
|
||||
await ctx.reply('🎤 Voice received! (STT via Whisper TBD)');
|
||||
const file = await ctx.api.getFile(fileId);
|
||||
const url = `https://api.telegram.org/file/bot${botToken}/${file.file_path}`;
|
||||
logger.info(`Voice file: ${url}`);
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
chat_id: chatId,
|
||||
text,
|
||||
parse_mode: 'Markdown',
|
||||
...options,
|
||||
}),
|
||||
});
|
||||
// ── Photo handler ──
|
||||
bot.on('message:photo', async (ctx) => {
|
||||
logger.info(`📸 Photo from ${ctx.from?.username}`);
|
||||
// TODO: vision analysis
|
||||
await ctx.reply('📸 Image received. Vision analysis TBD.');
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (!data.ok) {
|
||||
logger.error(`Telegram API error: ${data.description}`);
|
||||
}
|
||||
return data;
|
||||
} catch (error) {
|
||||
logger.error('Failed to send Telegram message:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
// ── Error handler ──
|
||||
bot.catch((err) => {
|
||||
logger.error('Bot error:', err.message || err);
|
||||
});
|
||||
|
||||
// Send message via WebSocket (for real-time updates)
|
||||
function sendWebSocketMessage(chatId, message) {
|
||||
const ws = connections.get(chatId);
|
||||
if (ws && ws.readyState === ws.OPEN) {
|
||||
ws.send(JSON.stringify(message));
|
||||
}
|
||||
}
|
||||
// ── Express + WebSocket server (keep for webhook compatibility) ──
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
const httpServer = createServer(app);
|
||||
const wss = new WebSocketServer({ server: httpServer });
|
||||
const wsClients = new Map();
|
||||
|
||||
// Process incoming message from Telegram
|
||||
async function processMessage(chatId, text, user) {
|
||||
try {
|
||||
const provider = new (await import('../api/index.js')).ZAIProvider(api);
|
||||
const response = await provider.complete(text, { model: 'glm-5.1' });
|
||||
const reply = response.content || '🤖 *zCode*: ' + response;
|
||||
await sendTelegramMessage(chatId, reply);
|
||||
} catch (error) {
|
||||
logger.error('Error processing message:', error.message);
|
||||
await sendTelegramMessage(chatId, '❌ Sorry, an error occurred while processing your message.');
|
||||
}
|
||||
}
|
||||
wss.on('connection', (ws, req) => {
|
||||
const chatId = req.url?.startsWith('/?chatId=') ? req.url.slice(8) : 'ws';
|
||||
wsClients.set(chatId, ws);
|
||||
ws.on('close', () => wsClients.delete(chatId));
|
||||
logger.info(`🔌 WS: ${chatId}`);
|
||||
});
|
||||
|
||||
// Process callback query
|
||||
async function processCallback(chatId, data) {
|
||||
try {
|
||||
const provider = new (await import('../api/index.js')).ZAIProvider(api);
|
||||
const response = await provider.complete(data, { model: 'glm-5.1' });
|
||||
const reply = response.content || '🤖 *zCode*: ' + response;
|
||||
await sendTelegramMessage(chatId, reply);
|
||||
} catch (error) {
|
||||
logger.error('Error processing callback:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle webhook
|
||||
// Handle webhook verification (Telegram GET request)
|
||||
// Webhook handler — feeds into grammy
|
||||
app.get('/telegram/webhook', (req, res) => {
|
||||
const { hub_mode, hub_challenge } = req.query;
|
||||
|
||||
// Telegram sends a GET request for webhook verification
|
||||
if (hub_mode === 'subscribe' && hub_challenge) {
|
||||
logger.info('✓ Webhook verified by Telegram');
|
||||
return res.status(200).send(hub_challenge);
|
||||
}
|
||||
|
||||
// Return 200 for any other GET requests (for health checks, etc.)
|
||||
return res.status(200).json({ ok: true, message: 'zCode webhook is active' });
|
||||
const { 'hub.mode': mode, 'hub.challenge': challenge } = req.query;
|
||||
if (mode === 'subscribe' && challenge) return res.status(200).send(challenge);
|
||||
res.status(200).json({ ok: true, message: 'zCode webhook active' });
|
||||
});
|
||||
|
||||
// Handle webhook POST
|
||||
app.post('/telegram/webhook', async (req, res) => {
|
||||
const update = req.body;
|
||||
|
||||
if (update.message) {
|
||||
const chatId = update.message.chat.id.toString();
|
||||
const text = update.message.text;
|
||||
const user = update.message.from?.username || update.message.from?.first_name || 'Unknown';
|
||||
|
||||
logger.info(`📨 New message from ${user} (${chatId}): ${text.substring(0, 50)}...`);
|
||||
|
||||
// Process message
|
||||
await processMessage(chatId, text, user);
|
||||
|
||||
res.json({ ok: true });
|
||||
} else if (update.callback_query) {
|
||||
const chatId = update.callback_query.message.chat.id.toString();
|
||||
const data = update.callback_query.data;
|
||||
|
||||
logger.info(`🔘 Callback from ${chatId}: ${data}`);
|
||||
|
||||
// Process callback
|
||||
await processCallback(chatId, data);
|
||||
|
||||
// Send answer
|
||||
await sendTelegramMessage(chatId, 'Callback processed', {
|
||||
callback_query_id: update.callback_query.id,
|
||||
});
|
||||
|
||||
res.json({ ok: true });
|
||||
} else {
|
||||
res.json({ ok: true });
|
||||
res.json({ ok: true }); // ack immediately
|
||||
try {
|
||||
await bot.handleUpdate(req.body);
|
||||
} catch (e) {
|
||||
logger.error('Webhook update error:', e.message);
|
||||
}
|
||||
});
|
||||
|
||||
// Set webhook (if URL provided)
|
||||
async function setWebhook() {
|
||||
const webhookUrl = process.env.ZCODE_WEBHOOK_URL;
|
||||
if (webhookUrl) {
|
||||
const url = `https://api.telegram.org/bot${botToken}/setWebhook`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ url: webhookUrl }),
|
||||
});
|
||||
// Health check
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({
|
||||
ok: true, uptime: process.uptime(),
|
||||
tools: svc.tools.length, skills: svc.skills.length, agents: svc.agents.length,
|
||||
wsClients: wsClients.size,
|
||||
});
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data.ok) {
|
||||
logger.info('✓ Webhook set successfully');
|
||||
} else {
|
||||
logger.error('✗ Failed to set webhook:', data.description);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Start HTTP server
|
||||
const PORT = process.env.ZCODE_PORT || 3000;
|
||||
httpServer.listen(PORT, () => {
|
||||
logger.info(`✓ HTTP server running on port ${PORT}`);
|
||||
logger.info(`✓ WebSocket server ready`);
|
||||
logger.info(`✓ HTTP on :${PORT} · WS ready · grammy bot online`);
|
||||
logger.info(`✓ ${svc.tools.length} tools · ${svc.skills.length} skills · ${svc.agents.length} agents`);
|
||||
});
|
||||
|
||||
// Set webhook and keep process alive
|
||||
await setWebhook();
|
||||
// Set webhook
|
||||
const wu = process.env.ZCODE_WEBHOOK_URL;
|
||||
if (wu) {
|
||||
try {
|
||||
await bot.api.setWebhook(wu, { allowed_updates: ['message', 'callback_query'] });
|
||||
logger.info('✓ Webhook set');
|
||||
} catch (e) {
|
||||
logger.warn('⚠ Webhook failed:', e.message);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
send: sendTelegramMessage,
|
||||
ws: sendWebSocketMessage,
|
||||
waitForMessages: async () => {
|
||||
// Keep process alive
|
||||
await new Promise(() => {});
|
||||
},
|
||||
getConnections: () => connections.size,
|
||||
send: (chatId, text) => bot.api.sendMessage(chatId, text, { parse_mode: 'Markdown' }),
|
||||
ws: (chatId, msg) => wsClients.get(chatId)?.send(JSON.stringify(msg)),
|
||||
waitForMessages: async () => { await new Promise(() => {}); },
|
||||
getConnections: () => wsClients.size,
|
||||
};
|
||||
}
|
||||
|
||||
48
src/bot/message-sender.js
Normal file
48
src/bot/message-sender.js
Normal file
@@ -0,0 +1,48 @@
|
||||
// Adapted from claudegram's message-sender.ts — send with retry, chunking, fallback
|
||||
import { logger } from '../utils/logger.js';
|
||||
|
||||
const MAX_MSG_LENGTH = 4000;
|
||||
|
||||
export function splitMessage(text) {
|
||||
if (text.length <= MAX_MSG_LENGTH) return [text];
|
||||
const chunks = [];
|
||||
let remaining = text;
|
||||
while (remaining.length > 0) {
|
||||
chunks.push(remaining.slice(0, MAX_MSG_LENGTH));
|
||||
remaining = remaining.slice(MAX_MSG_LENGTH);
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
|
||||
export function escapeMarkdown(text) {
|
||||
if (!text) return '';
|
||||
return text
|
||||
.replace(/_/g, '\\_')
|
||||
.replace(/\*/g, '\\*')
|
||||
.replace(/\[/g, '\\[')
|
||||
.replace(/`/g, '\\`');
|
||||
}
|
||||
|
||||
export async function sendFormatted(ctx, text) {
|
||||
if (!text) return;
|
||||
|
||||
try {
|
||||
// Try Markdown first
|
||||
const chunks = splitMessage(text);
|
||||
for (const chunk of chunks) {
|
||||
await ctx.reply(chunk, { parse_mode: 'Markdown' });
|
||||
}
|
||||
} catch {
|
||||
// Fallback to plain text
|
||||
logger.warn('Markdown send failed, falling back to plain text');
|
||||
const chunks = splitMessage(text);
|
||||
for (const chunk of chunks) {
|
||||
await ctx.reply(chunk, { parse_mode: undefined });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendLongMessage(ctx, text) {
|
||||
if (!text) return;
|
||||
return sendFormatted(ctx, text);
|
||||
}
|
||||
50
src/bot/request-queue.js
Normal file
50
src/bot/request-queue.js
Normal file
@@ -0,0 +1,50 @@
|
||||
// Adapted from claudegram's request-queue.ts — per-chat sequential processing
|
||||
const pendingQueues = new Map();
|
||||
const processingFlags = new Map();
|
||||
|
||||
export function isProcessing(sessionKey) {
|
||||
return processingFlags.get(sessionKey) === true;
|
||||
}
|
||||
|
||||
export function getQueuePosition(sessionKey) {
|
||||
const queue = pendingQueues.get(sessionKey);
|
||||
return queue ? queue.length : 0;
|
||||
}
|
||||
|
||||
export function clearQueue(sessionKey) {
|
||||
pendingQueues.delete(sessionKey);
|
||||
processingFlags.delete(sessionKey);
|
||||
}
|
||||
|
||||
export async function queueRequest(sessionKey, message, handler) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = { message, handler, resolve, reject };
|
||||
let queue = pendingQueues.get(sessionKey);
|
||||
if (!queue) {
|
||||
queue = [];
|
||||
pendingQueues.set(sessionKey, queue);
|
||||
}
|
||||
queue.push(request);
|
||||
processQueue(sessionKey);
|
||||
});
|
||||
}
|
||||
|
||||
async function processQueue(sessionKey) {
|
||||
if (processingFlags.get(sessionKey)) return;
|
||||
processingFlags.set(sessionKey, true);
|
||||
|
||||
const queue = pendingQueues.get(sessionKey);
|
||||
while (queue && queue.length > 0) {
|
||||
const request = queue[0];
|
||||
try {
|
||||
const result = await request.handler();
|
||||
request.resolve(result);
|
||||
} catch (error) {
|
||||
request.reject(error);
|
||||
}
|
||||
queue.shift();
|
||||
}
|
||||
|
||||
processingFlags.delete(sessionKey);
|
||||
pendingQueues.delete(sessionKey);
|
||||
}
|
||||
@@ -45,7 +45,7 @@ export async function zcode(options) {
|
||||
if (options.bot !== false && env.TELEGRAM_BOT_TOKEN) {
|
||||
// Import bot module dynamically to avoid circular dependency
|
||||
const botModule = await import('./bot/index.js');
|
||||
const bot = await botModule.initBot(config, api, tools, skills);
|
||||
const bot = await botModule.initBot(config, api, tools, skills, agents);
|
||||
logger.info('✓ Telegram bot initialized');
|
||||
|
||||
// Keep process alive for bot
|
||||
|
||||
Reference in New Issue
Block a user