Files
zCode-CLI-X/src/zcode.js

93 lines
3.1 KiB
JavaScript

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!');
}