@nestbox-ai/cli
Version:
The cli tools that helps developers to build agents
88 lines • 4.54 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAgent = createAgent;
const admin_1 = require("@nestbox-ai/admin");
const auth_1 = require("../../utils/auth");
const project_1 = require("../../utils/project");
/**
* Create a new agent in the Nestbox platform
*
* @param agentName The name of the agent
* @param options Options including lang, template, and project
* @param agentsApi The MachineAgentApi instance
* @param projectsApi The ProjectsApi instance
*/
function createAgent(agentName, options, agentsApi, projectsApi) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
const authToken = (0, auth_1.getAuthToken)();
if (!authToken) {
throw new Error("No authentication token found. Please login first.");
}
// Create API instances if not provided
if (!agentsApi || !projectsApi) {
const configuration = new admin_1.Configuration({
basePath: authToken === null || authToken === void 0 ? void 0 : authToken.serverUrl,
baseOptions: {
headers: {
Authorization: authToken === null || authToken === void 0 ? void 0 : authToken.token,
},
},
});
agentsApi = agentsApi || new admin_1.MachineAgentApi(configuration);
projectsApi = projectsApi || new admin_1.ProjectsApi(configuration);
}
// Resolve project - convert options to match CommandOptions interface
const projectData = yield (0, project_1.resolveProject)(projectsApi, Object.assign({ project: options.project, instance: options.instanceName || '' }, options));
// Prepare agent creation payload
// Determine the correct type value based on options.type
const agentTypeValue = ((_a = options.type) === null || _a === void 0 ? void 0 : _a.includes("AGENT")) ? "REGULAR" : options.type || "CHAT";
const payload = {
agentName,
goal: options.goal || `AI agent for ${agentName}`,
modelBaseId: options.modelBaseId || "",
machineName: options.machineName,
machineInstanceId: options.machineInstanceId,
instanceIP: options.instanceIP,
machineManifestId: options.machineManifestId,
parameters: ((_b = options.parameters) === null || _b === void 0 ? void 0 : _b.map((param) => ({
name: param.name,
description: param.description,
default_value: param.default || "",
isUserParam: param.isUserParam !== undefined ? param.isUserParam : true
}))) || [],
projectId: projectData.id,
type: agentTypeValue,
userId: options.userId || 0,
};
// Determine the type of resource (Agent or Chat)
const agentType = options.type || "CHAT";
const resourceType = agentType === "AGENT" || agentType === "REGULAR" ? "Agent" : "Chatbot";
// Create the agent
try {
const response = yield agentsApi.machineAgentControllerCreateMachineAgent(projectData.id, payload);
return response.data;
}
catch (error) {
if (error.response && error.response.status === 401) {
throw new Error('Authentication token has expired. Please login again using "nestbox login <domain>".');
}
else if (error.response) {
throw new Error(`API Error (${error.response.status}): ${((_c = error.response.data) === null || _c === void 0 ? void 0 : _c.message) || "Unknown error"}`);
}
else {
throw new Error(error.message || "Unknown error");
}
}
});
}
//# sourceMappingURL=create.js.map