plazbot-cli
Version:
CLI para Plazbot SDK
89 lines (74 loc) • 3.04 kB
text/typescript
import { Command } from 'commander';
import { Agent } from 'plazbot';
import { getStoredCredentials } from '../../utils/credentials';
import { logger } from '../../utils/logger';
import { AgentCommandOptions } from '../../types/agent';
import { createSpinner } from '../../utils/ui';
import { runAgentWizard } from './wizard';
import fs from 'fs/promises';
export const createCommand = new Command('create')
.description('Crea un nuevo agente de IA (wizard interactivo o archivo JSON)')
.argument('[configPath]', 'Ruta al archivo de configuracion JSON (opcional)')
.option('--dev', 'Usar ambiente de desarrollo', false)
.action(async (configPath: string | undefined, options: AgentCommandOptions) => {
try {
const credentials = await getStoredCredentials();
const agent = new Agent({
workspaceId: credentials.workspace,
apiKey: credentials.apiKey,
zone: credentials.zone,
...(options.dev && { customUrl: "http://localhost:5090" })
});
let agentConfig: any;
if (configPath) {
// Modo archivo: leer JSON
try {
const fileContent = await fs.readFile(configPath, 'utf-8');
agentConfig = JSON.parse(fileContent);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Error desconocido';
throw new Error(`Error al leer el archivo de configuracion: ${errorMessage}`);
}
logger.title('Creando agente desde archivo');
logger.json(agentConfig);
} else {
// Modo wizard interactivo
agentConfig = await runAgentWizard(credentials.zone);
const inquirer = await import('inquirer');
const { confirm } = await inquirer.default.prompt([{
type: 'confirm',
name: 'confirm',
message: 'Crear el agente con esta configuracion?',
default: true,
}]);
if (!confirm) {
logger.warning('Creacion cancelada');
return;
}
}
const spinner = createSpinner('Creando agente...');
spinner.start();
const result = await agent.addAgent(agentConfig);
spinner.succeed('Agente creado exitosamente');
logger.title('Detalles del agente');
if (result.agentId) {
logger.label('ID', result.agentId);
}
if (agentConfig.name) {
logger.label('Nombre', agentConfig.name);
}
logger.label('Tool Calling', agentConfig.useToolCalling ? 'Activado' : 'Desactivado');
if (agentConfig.channels && agentConfig.channels.length > 0) {
logger.label('WhatsApp', agentConfig.channels[0].key);
}
console.log();
logger.dim('Siguiente paso: plazbot agent chat -a ' + (result.agentId || '<agentId>'));
if (options.dev) {
logger.warning('Ambiente: desarrollo');
}
} catch (error) {
const message = error instanceof Error ? error.message : 'Error desconocido al crear el agente';
logger.error(message);
process.exit(1);
}
});