docs: update README with Discord and multi-channel features
This commit is contained in:
87
src/zcode.js
87
src/zcode.js
@@ -5,11 +5,11 @@ import { initTools } from './tools/index.js';
|
||||
import { initSkills } from './skills/index.js';
|
||||
import { initAgents } from './agents/index.js';
|
||||
import { checkEnv } from './utils/env.js';
|
||||
import { registerChannel, getChannels } from './bot/delivery-hub.js';
|
||||
|
||||
export async function zcode(options) {
|
||||
logger.info('🚀 Initializing zCode CLI X...');
|
||||
|
||||
// 1. Check environment
|
||||
|
||||
const env = checkEnv();
|
||||
if (!env.valid) {
|
||||
logger.error('Missing required environment variables:');
|
||||
@@ -19,53 +19,74 @@ export async function zcode(options) {
|
||||
|
||||
logger.info('✓ Environment validated');
|
||||
logger.info(`Z.AI API Key: ${env.ZAI_API_KEY.substring(0, 10)}...`);
|
||||
logger.info(`Telegram Bot Token: ${env.TELEGRAM_BOT_TOKEN ? 'Configured' : 'Not configured'}`);
|
||||
logger.info(`Telegram: ${env.TELEGRAM_BOT_TOKEN ? '✅' : '❌'}`);
|
||||
|
||||
// 2. Initialize configuration
|
||||
// Init core services
|
||||
const config = await initConfig();
|
||||
logger.info('✓ Configuration loaded');
|
||||
|
||||
// 3. Initialize Z.AI API
|
||||
const api = await initAPI();
|
||||
logger.info('✓ Z.AI API connected');
|
||||
|
||||
// 4. Initialize tools
|
||||
const tools = await initTools();
|
||||
logger.info(`✓ Tools loaded: ${tools.length} available`);
|
||||
|
||||
// 5. Initialize skills
|
||||
const skills = await initSkills();
|
||||
logger.info(`✓ Skills loaded: ${skills.length} available`);
|
||||
|
||||
// 6. Initialize agents
|
||||
const agents = await initAgents();
|
||||
logger.info(`✓ Agents loaded: ${agents.length} available`);
|
||||
|
||||
// 7. Initialize Telegram bot (if enabled)
|
||||
// Register telegram delivery channel (filled after bot init)
|
||||
const deliveryTargets = new Map();
|
||||
|
||||
registerChannel('log', async (msg) => {
|
||||
logger.info(`[broadcast] ${msg.substring(0, 200)}`);
|
||||
});
|
||||
|
||||
// Init Telegram bot
|
||||
let bot;
|
||||
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, agents);
|
||||
logger.info('✓ Telegram bot initialized');
|
||||
|
||||
// Keep process alive for bot
|
||||
logger.info('🤖 zCode CLI X is now running 24/7');
|
||||
logger.info('Type your commands or just chat with the bot!');
|
||||
|
||||
// Wait for bot to handle messages
|
||||
bot = await botModule.initBot(config, api, tools, skills, agents);
|
||||
if (bot) {
|
||||
deliveryTargets.set('telegram', bot.send);
|
||||
registerChannel('telegram', (msg) => bot.send(env.TELEGRAM_ALLOWED_USERS?.split(',')[0] || '6352861167', msg));
|
||||
logger.info('✓ Telegram bot initialized');
|
||||
}
|
||||
}
|
||||
|
||||
// Init Discord bot (opt-in via DISCORD_TOKEN env)
|
||||
if (env.DISCORD_TOKEN) {
|
||||
try {
|
||||
const { initDiscord } = await import('./bot/discord.js');
|
||||
const discordClient = await initDiscord(env.DISCORD_TOKEN, {
|
||||
config, api, tools, skills, agents,
|
||||
}, (messages) => {
|
||||
// Inline minimal chat for Discord
|
||||
return api.client.post('/chat/completions', {
|
||||
model: config.api?.models?.default || 'glm-5.1',
|
||||
messages,
|
||||
temperature: 0.7,
|
||||
max_tokens: 4096,
|
||||
}).then(r => r.data.choices?.[0]?.message?.content || '✅ Done.').catch(e => `❌ ${e.message}`);
|
||||
});
|
||||
if (discordClient) {
|
||||
deliveryTargets.set('discord', discordClient);
|
||||
logger.info('✓ Discord bot initialized');
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn('⚠ Discord init skipped:', e.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Log loaded services
|
||||
logger.info(`✓ ${tools.length} tools · ${skills.length} skills · ${agents.length} agents`);
|
||||
logger.info(`📡 Delivery channels: ${getChannels().join(', ') || 'none'}`);
|
||||
|
||||
// Keep alive
|
||||
if (bot) {
|
||||
logger.info('🤖 zCode CLI X running 24/7');
|
||||
await bot.waitForMessages();
|
||||
} else if (options.cli !== false) {
|
||||
// CLI-only mode
|
||||
logger.info('🔧 CLI mode: Run interactive mode');
|
||||
logger.info('🔧 CLI mode');
|
||||
await runInteractiveMode(config, api, tools, skills);
|
||||
} else {
|
||||
logger.info('🤖 Bot mode: zCode is running in the background');
|
||||
logger.info(' Telegram bot will handle all interactions');
|
||||
logger.info('🤖 Background mode');
|
||||
}
|
||||
}
|
||||
|
||||
async function runInteractiveMode(config, api, tools, skills) {
|
||||
// TODO: Implement interactive CLI mode
|
||||
console.log('Interactive mode coming soon!');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user