UNPKG

@nestbox-ai/cli

Version:

The cli tools that helps developers to build agents

64 lines 3.83 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerDocAddCommand = registerDocAddCommand; const error_1 = require("../../utils/error"); const chalk_1 = __importDefault(require("chalk")); const ora_1 = __importDefault(require("ora")); const project_1 = require("../../utils/project"); const apiUtils_1 = require("./apiUtils"); function registerDocAddCommand(docCommand) { const addDocCmd = docCommand .command('add') .description('Add a new document to a collection') .requiredOption('--instance <instanceId>', 'Instance ID') .requiredOption('--collection <collectionId>', 'Collection ID') .requiredOption('--id <id>', 'Document ID') .requiredOption('--document <json>', 'Document content in JSON format') .option('--metadata <json>', 'Document metadata in JSON format (optional)') .option('--project <projectId>', 'Project ID or name (defaults to the current project)'); addDocCmd.action((options) => __awaiter(this, void 0, void 0, function* () { yield (0, error_1.withTokenRefresh)(() => __awaiter(this, void 0, void 0, function* () { var _a, _b; const apis = (0, apiUtils_1.createDocumentApis)(); const project = yield (0, project_1.resolveProject)(apis.projectsApi, options); const spinner = (0, ora_1.default)(`Adding document to collection "${options.collection}" in instance ${options.instance}...`).start(); try { const documentContent = JSON.parse(options.document); const metadata = options.metadata ? JSON.parse(options.metadata) : {}; yield apis.documentsApi.documentControllerAddDocToCollection(project.id, options.instance, options.collection, { id: options.id, document: JSON.stringify(documentContent), metadata: metadata }); spinner.succeed('Successfully added document to collection'); console.log(chalk_1.default.green(`Document with ID "${options.id}" added successfully to collection "${options.collection}".`)); } catch (error) { spinner.fail('Operation failed'); if (error.response && error.response.status === 401) { console.error(chalk_1.default.red('Authentication token has expired. Please login again using "nestbox login <domain>".')); } else if ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) { console.error(chalk_1.default.red('API Error:'), error.response.data.message); } else { console.error(chalk_1.default.red('Error:'), error.message || 'Unknown error'); } throw error; } })); })); } //# sourceMappingURL=docAdd.js.map