@nestbox-ai/cli
Version:
The cli tools that helps developers to build agents
93 lines • 4.24 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.createApis = createApis;
exports.loadAgentFromYaml = loadAgentFromYaml;
exports.loadAllAgentNamesFromYaml = loadAllAgentNamesFromYaml;
const admin_1 = require("@nestbox-ai/admin");
const api_1 = require("../../utils/api");
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
const js_yaml_1 = __importDefault(require("js-yaml"));
const ajv_1 = __importDefault(require("ajv"));
const yaml_schema_1 = require("./yaml-schema");
/**
* Create API instances with current authentication using setupAuthAndConfig
*/
function createApis() {
const authResult = (0, api_1.setupAuthAndConfig)();
if (!authResult) {
throw new Error("No authentication token found. Please log in first.");
}
return {
agentsApi: new admin_1.MachineAgentApi(authResult.configuration),
projectsApi: new admin_1.ProjectsApi(authResult.configuration),
instanceApi: new admin_1.MachineInstancesApi(authResult.configuration),
};
}
const ajv = new ajv_1.default({ strict: false });
const validate = ajv.compile(yaml_schema_1.yamlSchema);
function loadAgentFromYaml(agentName) {
return __awaiter(this, void 0, void 0, function* () {
const file = path_1.default.join(process.cwd(), "nestbox-agents.yaml");
try {
yield promises_1.default.access(file);
}
catch (err) {
if ((err === null || err === void 0 ? void 0 : err.code) === "ENOENT")
return undefined;
throw new Error(`cannot access nestbox-agents.yaml: ${err.message}`);
}
const raw = yield promises_1.default.readFile(file, "utf8");
const doc = js_yaml_1.default.load(raw);
if (!validate(doc)) {
const msg = ajv.errorsText(validate.errors, { separator: "\n- " });
throw new Error(`invalid nestbox-agents.yaml:\n- ${msg}`);
}
const mf = doc;
return mf.agents.find(a => (a === null || a === void 0 ? void 0 : a.name) === agentName);
});
}
function loadAllAgentNamesFromYaml() {
return __awaiter(this, void 0, void 0, function* () {
const file = path_1.default.join(process.cwd(), "nestbox-agents.yaml");
try {
yield promises_1.default.access(file);
}
catch (err) {
if ((err === null || err === void 0 ? void 0 : err.code) === "ENOENT")
return [];
throw new Error(`cannot access nestbox-agents.yaml: ${err.message}`);
}
const raw = yield promises_1.default.readFile(file, "utf8");
const doc = js_yaml_1.default.load(raw);
if (!validate(doc)) {
const msg = ajv.errorsText(validate.errors, { separator: "\n- " });
throw new Error(`invalid nestbox-agents.yaml:\n- ${msg}`);
}
const mf = doc;
const names = Array.isArray(mf === null || mf === void 0 ? void 0 : mf.agents)
? mf.agents.map(a => a === null || a === void 0 ? void 0 : a.name).filter(Boolean)
: [];
// de-duplicate by name, keep first occurrence
const seen = new Set();
return names.filter(n => {
if (seen.has(n))
return false;
seen.add(n);
return true;
});
});
}
//# sourceMappingURL=apiUtils.js.map