UNPKG

plazbot-cli

Version:
143 lines (142 loc) 6.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.filesCommand = void 0; const commander_1 = require("commander"); const plazbot_1 = require("plazbot"); const credentials_1 = require("../../utils/credentials"); const logger_1 = require("../../utils/logger"); const ui_1 = require("../../utils/ui"); const filesGroup = new commander_1.Command('files') .description('Gestionar archivos de la base de conocimiento del agente'); // Listar archivos filesGroup.command('list') .description('Listar archivos del agente') .argument('<agentId>', 'ID del agente') .option('--dev', 'Usar ambiente de desarrollo', false) .action(async (agentId, options) => { try { const credentials = await (0, credentials_1.getStoredCredentials)(); const agent = new plazbot_1.Agent({ workspaceId: credentials.workspace, apiKey: credentials.apiKey, zone: credentials.zone, ...(options.dev && { customUrl: "http://localhost:5090" }) }); const spinner = (0, ui_1.createSpinner)('Cargando archivos...'); spinner.start(); const agentData = await agent.getAgentById({ id: agentId }); spinner.stop(); const files = agentData.files || []; console.log((0, ui_1.section)('Archivos - ' + (agentData.name || agentId))); if (files.length === 0) { console.log(ui_1.theme.muted('\n No hay archivos en la base de conocimiento')); console.log(ui_1.theme.muted(' Usa: plazbot agent files add <agentId> --url <url>\n')); return; } const rows = files.map((f) => [ f.fileId || f.id || 'N/A', f.name || f.reference || 'Sin nombre', (f.tags || []).join(', ') || '-', ]); console.log((0, ui_1.createTable)(['ID', 'Nombre', 'Tags'], rows)); } catch (error) { const message = error instanceof Error ? error.message : 'Error desconocido'; logger_1.logger.error(message); process.exit(1); } }); // Agregar archivo filesGroup.command('add') .description('Agregar archivo a la base de conocimiento') .argument('<agentId>', 'ID del agente') .requiredOption('-u, --url <url>', 'URL del archivo (PDF, DOC, DOCX)') .option('-r, --reference <name>', 'Nombre de referencia', 'documento') .option('-t, --tags <tags>', 'Tags separados por coma', '') .option('--dev', 'Usar ambiente de desarrollo', false) .action(async (agentId, options) => { try { const credentials = await (0, credentials_1.getStoredCredentials)(); const agent = new plazbot_1.Agent({ workspaceId: credentials.workspace, apiKey: credentials.apiKey, zone: credentials.zone, ...(options.dev && { customUrl: "http://localhost:5090" }) }); const spinner = (0, ui_1.createSpinner)('Subiendo archivo...'); spinner.start(); const tags = options.tags ? options.tags.split(',').map((t) => t.trim()) : []; const result = await agent.addFile({ fileUrl: options.url, reference: options.reference, agentId, tags, }); spinner.succeed('Archivo agregado'); if (result?.fileId || result?.id) { logger_1.logger.label('File ID', result.fileId || result.id); logger_1.logger.dim('El archivo esta siendo procesado. Verifica con:'); logger_1.logger.dim(`plazbot agent files status ${result.fileId || result.id}`); } } catch (error) { const message = error instanceof Error ? error.message : 'Error desconocido'; logger_1.logger.error(message); process.exit(1); } }); // Verificar estado filesGroup.command('status') .description('Verificar estado de procesamiento de un archivo') .argument('<fileId>', 'ID del archivo') .option('--dev', 'Usar ambiente de desarrollo', false) .action(async (fileId, options) => { try { const credentials = await (0, credentials_1.getStoredCredentials)(); const agent = new plazbot_1.Agent({ workspaceId: credentials.workspace, apiKey: credentials.apiKey, zone: credentials.zone, ...(options.dev && { customUrl: "http://localhost:5090" }) }); const spinner = (0, ui_1.createSpinner)('Verificando...'); spinner.start(); const result = await agent.validateFile({ fileId }); spinner.stop(); logger_1.logger.title('Estado del archivo'); logger_1.logger.label('File ID', fileId); logger_1.logger.label('Estado', result?.status || result?.stateId === 2 ? 'Completado' : 'En proceso'); } catch (error) { const message = error instanceof Error ? error.message : 'Error desconocido'; logger_1.logger.error(message); process.exit(1); } }); // Eliminar archivo filesGroup.command('delete') .description('Eliminar archivo de la base de conocimiento') .argument('<agentId>', 'ID del agente') .argument('<fileId>', 'ID del archivo') .option('--dev', 'Usar ambiente de desarrollo', false) .action(async (agentId, fileId, options) => { try { const credentials = await (0, credentials_1.getStoredCredentials)(); const agent = new plazbot_1.Agent({ workspaceId: credentials.workspace, apiKey: credentials.apiKey, zone: credentials.zone, ...(options.dev && { customUrl: "http://localhost:5090" }) }); const spinner = (0, ui_1.createSpinner)('Eliminando archivo...'); spinner.start(); await agent.deleteFile({ fileId, agentId }); spinner.succeed('Archivo eliminado'); } catch (error) { const message = error instanceof Error ? error.message : 'Error desconocido'; logger_1.logger.error(message); process.exit(1); } }); exports.filesCommand = filesGroup;