Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.diffAiAgents = exports.restoreAiAgents = exports.pushAiAgent = exports.pullAiAgent = exports.cloneAiAgents = void 0; const fs = __importStar(require("fs-extra")); const cognigyClient_1 = __importDefault(require("../utils/cognigyClient")); const config_1 = __importDefault(require("../utils/config")); const checks_1 = require("../utils/checks"); const indexAll_1 = require("../utils/indexAll"); const progressBar_1 = require("../utils/progressBar"); const jsonDiff = __importStar(require("json-diff")); const cli_spinner_1 = require("cli-spinner"); const chalk_1 = __importDefault(require("../utils/chalk")); const cloneAiAgents = async (progressIncrement = 100) => { const aiAgentsDir = getAiAgentsDir(); await (0, checks_1.checkCreateDir)(aiAgentsDir); const { items: aiAgents } = await (0, indexAll_1.indexAll)(cognigyClient_1.default.indexAiAgents)({ projectId: config_1.default.agent, }); const incrementPerAgent = progressIncrement / aiAgents.length; for (const agent of aiAgents) { const agentDir = getAgentDir(agent.name); await (0, checks_1.removeCreateDir)(agentDir); await (0, checks_1.checkCreateDir)(agentDir); const fullAiAgent = await cognigyClient_1.default.readAiAgent({ aiAgentId: agent._id, }); await fs.writeJSON(`${agentDir}/config.json`, fullAiAgent, { spaces: 2 }); (0, progressBar_1.addToProgressBar)(incrementPerAgent); } }; exports.cloneAiAgents = cloneAiAgents; const pullAiAgent = async (resourceName) => { const aiAgentsDir = getAiAgentsDir(); await (0, checks_1.checkCreateDir)(aiAgentsDir); if (resourceName) { const aiAgent = await getAiAgentByName(resourceName); const fullAiAgent = await cognigyClient_1.default.readAiAgent({ aiAgentId: aiAgent._id, }); const agentDir = getAgentDir(resourceName); await (0, checks_1.checkCreateDir)(agentDir); await fs.writeJSON(`${agentDir}/config.json`, fullAiAgent, { spaces: 2 }); } else { const { items: aiAgents } = await (0, indexAll_1.indexAll)(cognigyClient_1.default.indexAiAgents)({ projectId: config_1.default.agent, }); await Promise.all(aiAgents.map(async (agent) => { const agentDir = getAgentDir(agent.name); await (0, checks_1.checkCreateDir)(agentDir); const fullAiAgent = await cognigyClient_1.default.readAiAgent({ aiAgentId: agent._id, }); await fs.writeJSON(`${agentDir}/config.json`, fullAiAgent, { spaces: 2, }); })); } }; exports.pullAiAgent = pullAiAgent; const pushAiAgent = async (resourceName, availableProgress = 100) => { const agentDir = getAgentDir(resourceName); const aiAgentConfig = await fs.readJSON(`${agentDir}/config.json`); const existingAgent = await getAiAgentByName(resourceName); if (!existingAgent) { throw new Error(`AI Agent '${resourceName}' not found`); } try { const cleanedConfig = cleanAiAgentConfig(aiAgentConfig); await cognigyClient_1.default.updateAiAgent({ ...cleanedConfig }); } catch (error) { console.error(error); throw new Error(`Failed to update AI Agent '${resourceName}' in Cognigy.AI`); } (0, progressBar_1.addToProgressBar)(availableProgress); }; exports.pushAiAgent = pushAiAgent; const restoreAiAgents = async (availableProgress) => { const aiAgentsDir = `${config_1.default.agentDir}/aiAgents`; const aiAgentDirectories = fs.readdirSync(aiAgentsDir); if (!aiAgentDirectories || aiAgentDirectories.length === 0) { console.log('No AI Agents found, aborting...\n'); return; } const incrementPerAgent = availableProgress / aiAgentDirectories.length; for (let aiAgent of aiAgentDirectories) { await (0, exports.pushAiAgent)(aiAgent, incrementPerAgent); } return Promise.resolve(); }; exports.restoreAiAgents = restoreAiAgents; const getAiAgentByName = async (resourceName) => { const { items: aiAgents } = await (0, indexAll_1.indexAll)(cognigyClient_1.default.indexAiAgents)({ projectId: config_1.default.agent, }); const aiAgent = aiAgents.find((agent) => agent.name === resourceName); if (!aiAgent) { throw new Error(`AI Agent '${resourceName}' not found`); } return aiAgent; }; const cleanAiAgentConfig = (config) => { const cleanConfig = { ...config }; cleanConfig.aiAgentId = cleanConfig._id; delete cleanConfig._id; delete cleanConfig.referenceId; delete cleanConfig.createdBy; delete cleanConfig.createdAt; delete cleanConfig.organisationId; delete cleanConfig.projectReference; delete cleanConfig.organisationReference; delete cleanConfig.lastChanged; delete cleanConfig.lastChangedBy; return cleanConfig; }; const AI_AGENTS_DIR = 'aiAgents'; const getAiAgentsDir = () => `${config_1.default.agentDir}/${AI_AGENTS_DIR}`; const getAgentDir = (agentName) => `${getAiAgentsDir()}/${agentName}`; const diffAiAgents = async (aiAgentName, mode = 'full') => { try { if (['full'].indexOf(mode) === -1) { console.log(`Selected mode not supported for AI Agents. Supported modes:\n\n- full\n`); process.exit(0); } const spinner = new cli_spinner_1.Spinner(`Comparing ${chalk_1.default.green('local')} and ${chalk_1.default.red('remote')} AI Agent resource ${aiAgentName}... %s`); spinner.setSpinnerString('|/-\\'); spinner.start(); const aiAgentDir = getAiAgentsDir(); const agentDir = getAgentDir(aiAgentName); if (!fs.existsSync(agentDir) || !fs.existsSync(`${agentDir}/config.json`)) { spinner.stop(); console.log(`\nThe requested AI Agent resource (${aiAgentName}) couldn't be found ${chalk_1.default.green('locally')}. Aborting...`); process.exit(0); } const localConfig = await fs.readJSON(`${agentDir}/config.json`); const remoteConfig = await cognigyClient_1.default.readAiAgent({ aiAgentId: localConfig._id, }); const diffString = jsonDiff.diffString(remoteConfig, localConfig); spinner.stop(); if (diffString) console.log(`\n\n ${diffString}`); else console.log(`\n\nThe local and remote resource are identical.`); return; } catch (err) { console.log(err.message); process.exit(0); } }; exports.diffAiAgents = diffAiAgents; |