plazbot-cli
Version:
CLI para Plazbot SDK
159 lines (136 loc) • 5.59 kB
text/typescript
import { Command } from 'commander';
import { Agent } from 'plazbot';
import { getStoredCredentials } from '../../utils/credentials';
import { logger } from '../../utils/logger';
import { createSpinner, createTable, theme, section, statusBadge } from '../../utils/ui';
import { AgentCommandOptions } from '../../types/agent';
const filesGroup = new 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: string, 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" })
});
const spinner = createSpinner('Cargando archivos...');
spinner.start();
const agentData = await agent.getAgentById({ id: agentId });
spinner.stop();
const files = agentData.files || [];
console.log(section('Archivos - ' + (agentData.name || agentId)));
if (files.length === 0) {
console.log(theme.muted('\n No hay archivos en la base de conocimiento'));
console.log(theme.muted(' Usa: plazbot agent files add <agentId> --url <url>\n'));
return;
}
const rows = files.map((f: any) => [
f.fileId || f.id || 'N/A',
f.name || f.reference || 'Sin nombre',
(f.tags || []).join(', ') || '-',
]);
console.log(createTable(['ID', 'Nombre', 'Tags'], rows));
} catch (error) {
const message = error instanceof Error ? error.message : 'Error desconocido';
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: string, options: any) => {
try {
const credentials = await getStoredCredentials();
const agent = new Agent({
workspaceId: credentials.workspace,
apiKey: credentials.apiKey,
zone: credentials.zone,
...(options.dev && { customUrl: "http://localhost:5090" })
});
const spinner = createSpinner('Subiendo archivo...');
spinner.start();
const tags = options.tags ? options.tags.split(',').map((t: string) => 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.label('File ID', result.fileId || result.id);
logger.dim('El archivo esta siendo procesado. Verifica con:');
logger.dim(`plazbot agent files status ${result.fileId || result.id}`);
}
} catch (error) {
const message = error instanceof Error ? error.message : 'Error desconocido';
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: string, 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" })
});
const spinner = createSpinner('Verificando...');
spinner.start();
const result = await agent.validateFile({ fileId });
spinner.stop();
logger.title('Estado del archivo');
logger.label('File ID', fileId);
logger.label('Estado', result?.status || result?.stateId === 2 ? 'Completado' : 'En proceso');
} catch (error) {
const message = error instanceof Error ? error.message : 'Error desconocido';
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: string, fileId: string, 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" })
});
const spinner = 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.error(message);
process.exit(1);
}
});
export const filesCommand = filesGroup;