UNPKG

@nestbox-ai/cli

Version:

The cli tools that helps developers to build agents

47 lines (41 loc) 2.13 kB
import { Command } from "commander"; import { withTokenRefresh } from "../../utils/error"; import chalk from "chalk"; import ora from "ora"; import { resolveProject } from "../../utils/project"; import { createDocumentApis } from "./apiUtils"; export function registerCollectionCreateCommand(collectionCommand: Command): void { const createCmd = collectionCommand .command('create') .description('Create a new document collection for a specific instance') .requiredOption('--instance <instanceId>', 'Instance ID') .requiredOption('--name <name>', 'Name of the document collection') .option('--metadata <json>', 'Metadata for the document collection in JSON format') .option('--project <projectId>', 'Project ID or name (defaults to the current project)'); createCmd.action(async (options) => { await withTokenRefresh(async () => { const apis = createDocumentApis(); const project = await resolveProject(apis.projectsApi, options); const spinner = ora(`Creating document collection "${options.name}" for instance ${options.instance} in project ${project.name}...`).start(); try { const metadataObj = options.metadata ? JSON.parse(options.metadata) : {}; await apis.documentsApi.documentControllerCreateCollection(project.id, options.instance, { name: options.name, metadata: metadataObj, }); spinner.succeed('Successfully created document collection'); console.log(chalk.green(`Document collection "${options.name}" created successfully.`)); } catch (error: any) { spinner.fail('Operation failed'); if (error.response && error.response.status === 401) { console.error(chalk.red('Authentication token has expired. Please login again using "nestbox login <domain>".')); } else if (error.response?.data?.message) { console.error(chalk.red('API Error:'), error.response.data.message); } else { console.error(chalk.red('Error:'), error.message || 'Unknown error'); } throw error; } }); }); }