import { logger } from './utils/logger.js'; import { initConfig } from './config/index.js'; import { initAPI } from './api/index.js'; 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...'); const env = checkEnv(); if (!env.valid) { logger.error('Missing required environment variables:'); env.missing.forEach(key => logger.error(` - ${key}`)); process.exit(1); } logger.info('โœ“ Environment validated'); logger.info(`Z.AI API Key: ${env.ZAI_API_KEY.substring(0, 10)}...`); logger.info(`Telegram: ${env.TELEGRAM_BOT_TOKEN ? 'โœ…' : 'โŒ'}`); // Init core services const config = await initConfig(); const api = await initAPI(); const tools = await initTools(); const skills = await initSkills(); const agents = await initAgents(); // 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) { const botModule = await import('./bot/index.js'); 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) { logger.info('๐Ÿ”ง CLI mode'); await runInteractiveMode(config, api, tools, skills); } else { logger.info('๐Ÿค– Background mode'); } } async function runInteractiveMode(config, api, tools, skills) { console.log('Interactive mode coming soon!'); }