@nestbox-ai/cli
Version:
The cli tools that helps developers to build agents
150 lines • 8.84 kB
JavaScript
;
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.registerCreateCommand = registerCreateCommand;
const chalk_1 = __importDefault(require("chalk"));
const project_1 = require("../../utils/project");
const agent_1 = require("../../utils/agent");
const apiUtils_1 = require("./apiUtils");
function buildAgentData(options_1) {
return __awaiter(this, arguments, void 0, function* (options, machineInstanceData = {}) {
const createAgentData = {
agentName: "",
goal: "",
inputSchema: {},
machineManifestId: machineInstanceData.machineId,
machineName: options.instance,
machineInstanceId: machineInstanceData.id,
instanceIP: machineInstanceData.internalIP,
projectId: options.project,
type: (options === null || options === void 0 ? void 0 : options.type) || "REGULAR",
userId: 0,
modelBaseId: "",
entryFunctionName: "",
};
// check agent name and add prefix
if (!options.agent) {
throw new Error("Missing required argument <agent>.");
}
createAgentData.agentName = options.prefix
? options.prefix + "-" + options.agent
: options.agent;
// agent creation using arguments
if (options.description || options.inputSchema) {
if (!options.description) {
throw new Error("Missing required argument <description>.");
}
if (!options.inputSchema) {
throw new Error("Missing required argument <inputSchema>.");
}
createAgentData.goal = options.description;
createAgentData.inputSchema = JSON.parse(options.inputSchema);
}
else {
const manifestAgent = yield (0, apiUtils_1.loadAgentFromYaml)(options.agent);
if (!manifestAgent) {
throw new Error("Could not find a yaml file definition of an agent or agent not defined in yaml file.");
}
createAgentData.goal = manifestAgent.description;
createAgentData.inputSchema = manifestAgent.inputSchema || {};
createAgentData.type = options.type || (manifestAgent === null || manifestAgent === void 0 ? void 0 : manifestAgent.type) || "REGULAR";
}
return createAgentData;
});
}
function registerCreateCommand(agentCommand) {
agentCommand
.command("create")
.description("Create an agent with direct arguments or YAML.")
.option("--agent <agent>", "Agent name to deploy")
.option("--all", "Deploy all agents defined in nestbox-agents.yaml")
.option("--project <project>", "Project ID (defaults to current project)")
.option("--type <type>", "Agent type (e.g. CHAT, AGENT, REGULAR)")
.option("--prefix <prefix>", "A prefix added to beginning of the agent name.")
.option("--description <description>", "Description of the agent")
.option("--instance <instance>", "Machine name")
.option("--inputSchema <inputSchema>", "Agent input schema")
.action((options) => __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
try {
const apis = (0, apiUtils_1.createApis)();
// resolve project
const projectData = yield (0, project_1.resolveProject)(apis.projectsApi, Object.assign({ project: (options === null || options === void 0 ? void 0 : options.project) || "", instance: (options === null || options === void 0 ? void 0 : options.instance) || "" }, options));
const projectRoot = process.cwd();
const nestboxConfig = (0, agent_1.loadNestboxConfig)(projectRoot);
if (!(options === null || options === void 0 ? void 0 : options.instance) && !(nestboxConfig === null || nestboxConfig === void 0 ? void 0 : nestboxConfig.instance)) {
console.log(chalk_1.default.red("Parameter <instance> not provided."));
return;
}
const machineName = (options === null || options === void 0 ? void 0 : options.instance) || (nestboxConfig === null || nestboxConfig === void 0 ? void 0 : nestboxConfig.instance);
const instanceData = yield apis.instanceApi.machineInstancesControllerGetMachineInstanceByUserId(projectData.id, 0, 10);
const targetInstance = instanceData.data.machineInstances.find((instance) => instance.instanceName === machineName);
if (!targetInstance) {
console.error(chalk_1.default.red(`Instance with name "${machineName}" not found in project "${projectData.name}".`));
console.log(chalk_1.default.yellow("Available instances:"));
instanceData.data.machineInstances.forEach((instance) => {
console.log(chalk_1.default.yellow(` - ${instance.instanceName} (ID: ${instance.id})`));
});
return;
}
// handle --all (iterate all manifest agent names)
if (options.all) {
let created = 0;
let failed = 0;
const names = yield (0, apiUtils_1.loadAllAgentNamesFromYaml)();
if (!names.length) {
console.log(chalk_1.default.yellow("No agents found in YAML manifest."));
return;
}
console.log(chalk_1.default.cyan(`Deploying ${names.length} agent(s) from YAML${options.prefix ? ` with prefix "${options.prefix}"` : ""}...`));
const results = [];
for (const name of names) {
try {
const data = yield buildAgentData(Object.assign(Object.assign({}, options), { project: projectData.id, agent: name }), targetInstance);
const res = yield apis.agentsApi.machineAgentControllerCreateMachineAgent(projectData.id, Object.assign({}, data));
created++;
results.push(res.data);
console.log(chalk_1.default.green(`✔ Created: ${data.agentName}`));
}
catch (err) {
failed++;
const msg = ((_b = (_a = err === null || err === void 0 ? void 0 : err.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ||
(err === null || err === void 0 ? void 0 : err.message) ||
"Unknown error";
console.log(chalk_1.default.red(`✖ Failed: ${name} — ${msg}`));
}
}
console.log(chalk_1.default.cyan(`Done. ${chalk_1.default.green(`${created} created`)}, ${chalk_1.default.red(`${failed} failed`)}.`));
return results;
}
// original single-agent flow
const data = yield buildAgentData(Object.assign(Object.assign({}, options), { project: projectData.id }), targetInstance);
const response = yield apis.agentsApi.machineAgentControllerCreateMachineAgent(projectData.id, Object.assign({}, data));
console.log(chalk_1.default.green("Agent successfully created."));
return response.data;
}
catch (error) {
if (error.response && error.response.status === 401) {
console.log(chalk_1.default.red('Authentication token has expired. Please login again using "nestbox login <domain>".'));
}
else if (error.response) {
console.log(chalk_1.default.red(`API Error (${error.response.status}): ${((_c = error.response.data) === null || _c === void 0 ? void 0 : _c.message) || "Unknown error"}`));
}
else {
console.log(chalk_1.default.red(error.message || "Unknown error"));
}
}
}));
}
//# sourceMappingURL=create.js.map