@nestbox-ai/cli 1.0.37 → 1.0.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/.github/workflows/test.yml +54 -0
  2. package/README.md +2 -1
  3. package/dist/commands/agent/apiUtils.d.ts +20 -0
  4. package/dist/commands/agent/apiUtils.js +75 -2
  5. package/dist/commands/agent/apiUtils.js.map +1 -1
  6. package/dist/commands/agent/create.d.ts +2 -29
  7. package/dist/commands/agent/create.js +123 -61
  8. package/dist/commands/agent/create.js.map +1 -1
  9. package/dist/commands/agent/deploy.js +183 -137
  10. package/dist/commands/agent/deploy.js.map +1 -1
  11. package/dist/commands/agent/index.d.ts +1 -2
  12. package/dist/commands/agent/index.js +3 -6
  13. package/dist/commands/agent/index.js.map +1 -1
  14. package/dist/commands/agent/yaml-schema.d.ts +72 -0
  15. package/dist/commands/agent/yaml-schema.js +61 -0
  16. package/dist/commands/agent/yaml-schema.js.map +1 -0
  17. package/dist/commands/agent.js +2 -2
  18. package/dist/commands/agent.js.map +1 -1
  19. package/dist/commands/generate/project.js +32 -28
  20. package/dist/commands/generate/project.js.map +1 -1
  21. package/dist/utils/agent.js +18 -20
  22. package/dist/utils/agent.js.map +1 -1
  23. package/package.json +3 -2
  24. package/src/commands/agent/apiUtils.ts +103 -14
  25. package/src/commands/agent/create.ts +266 -100
  26. package/src/commands/agent/deploy.ts +515 -264
  27. package/src/commands/agent/index.ts +1 -4
  28. package/src/commands/agent/yaml-schema.ts +57 -0
  29. package/src/commands/agent.ts +10 -10
  30. package/src/commands/generate/project.ts +179 -147
  31. package/src/utils/agent.ts +141 -125
  32. package/test/agent.test.ts +153 -124
  33. package/dist/commands/agent/createFromYaml.d.ts +0 -2
  34. package/dist/commands/agent/createFromYaml.js +0 -172
  35. package/dist/commands/agent/createFromYaml.js.map +0 -1
  36. package/src/commands/agent/createFromYaml.ts +0 -192
@@ -1,172 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.registerCreateFromYamlCommand = registerCreateFromYamlCommand;
16
- const error_1 = require("../../utils/error");
17
- const chalk_1 = __importDefault(require("chalk"));
18
- const ora_1 = __importDefault(require("ora"));
19
- const cli_table3_1 = __importDefault(require("cli-table3"));
20
- const fs_1 = __importDefault(require("fs"));
21
- const js_yaml_1 = __importDefault(require("js-yaml"));
22
- const user_1 = require("../../utils/user");
23
- const create_1 = require("./create");
24
- const apiUtils_1 = require("./apiUtils");
25
- function registerCreateFromYamlCommand(agentCommand) {
26
- agentCommand
27
- .command("create [firstArg] [secondArg]")
28
- .description("Create multiple agents from a YAML configuration file")
29
- .option("--project <projectId>", "Project ID (defaults to the current project)")
30
- .action((firstArg, secondArg, options) => __awaiter(this, void 0, void 0, function* () {
31
- try {
32
- let apis = (0, apiUtils_1.createApis)();
33
- // Determine which argument is the YAML file path
34
- let yamlFilePath;
35
- if (firstArg === 'file' && secondArg) {
36
- yamlFilePath = secondArg;
37
- }
38
- else if (firstArg) {
39
- yamlFilePath = firstArg;
40
- if (typeof secondArg === 'object' && !options) {
41
- options = secondArg;
42
- }
43
- }
44
- else {
45
- console.error(chalk_1.default.red("Missing YAML file path. Usage: nestbox agent create <yamlFile> OR nestbox agent create file <yamlFile>"));
46
- return;
47
- }
48
- // Check if file exists
49
- if (!fs_1.default.existsSync(yamlFilePath)) {
50
- console.error(chalk_1.default.red(`YAML file not found: ${yamlFilePath}`));
51
- return;
52
- }
53
- // Read and parse the YAML file
54
- const spinner = (0, ora_1.default)(`Reading agents configuration from ${yamlFilePath}...`).start();
55
- try {
56
- const fileContents = fs_1.default.readFileSync(yamlFilePath, 'utf8');
57
- const config = js_yaml_1.default.load(fileContents);
58
- if (!config || !config.agents || !Array.isArray(config.agents)) {
59
- spinner.fail("Invalid YAML configuration: Missing 'agents' array");
60
- console.error(chalk_1.default.red("The YAML file should contain an 'agents' array with agent configurations"));
61
- return;
62
- }
63
- spinner.succeed(`Found ${config.agents.length} agents in configuration file`);
64
- // Process each agent with token refresh support
65
- const results = {
66
- success: 0,
67
- failed: 0,
68
- agents: []
69
- };
70
- // Get user data once
71
- const user = yield (0, user_1.userData)();
72
- for (const agent of config.agents) {
73
- if (!agent.name) {
74
- console.log(chalk_1.default.yellow("Skipping agent with no name defined"));
75
- results.failed++;
76
- results.agents.push({
77
- name: "unnamed",
78
- success: false,
79
- message: "Name is required"
80
- });
81
- continue;
82
- }
83
- let agentType = agent.type || "CHAT";
84
- const resourceType = agentType === "AGENT" ? "Agent" : "Chatbot";
85
- const agentSpinner = (0, ora_1.default)(`Creating ${resourceType.toLowerCase()} '${agent.name}'...`).start();
86
- try {
87
- // Create agent with token refresh support
88
- yield (0, error_1.withTokenRefresh)(() => __awaiter(this, void 0, void 0, function* () {
89
- // Map YAML config to createAgent options
90
- const createOptions = Object.assign(Object.assign({}, options), { goal: agent.goal || "No goal specified", modelBaseId: agent.modelBaseId || "", instanceIP: agent.instanceIP || "localhost", machineInstanceId: agent.machineInstanceId || 1, machineManifestId: agent.machineManifestId || "default", machineName: agent.machineName || `agent-${agent.name.toLowerCase()}`, type: agentType, userId: user.id, parameters: agent.parameters ? agent.parameters.map((p) => {
91
- return {
92
- name: p.name || "unnamed",
93
- description: p.description || "",
94
- default: p.default || "",
95
- isUserParam: p.isUserParam !== undefined ? p.isUserParam : true
96
- };
97
- }) : [] });
98
- yield (0, create_1.createAgent)(agent.name, createOptions, apis.agentsApi, apis.projectsApi);
99
- }), () => {
100
- apis = (0, apiUtils_1.createApis)();
101
- });
102
- agentSpinner.stop();
103
- results.success++;
104
- results.agents.push({
105
- name: agent.name,
106
- success: true,
107
- message: `Created successfully`
108
- });
109
- }
110
- catch (error) {
111
- agentSpinner.fail(`Failed to create ${resourceType.toLowerCase()} '${agent.name}'`);
112
- console.error(chalk_1.default.red(`Error: ${error.message}`));
113
- results.failed++;
114
- results.agents.push({
115
- name: agent.name,
116
- success: false,
117
- message: error.message
118
- });
119
- }
120
- }
121
- // Final summary
122
- console.log(chalk_1.default.blue("\nResource creation summary:"));
123
- const table = new cli_table3_1.default({
124
- head: [
125
- chalk_1.default.white.bold("Name"),
126
- chalk_1.default.white.bold("Type"),
127
- chalk_1.default.white.bold("Status"),
128
- chalk_1.default.white.bold("Message"),
129
- ],
130
- style: {
131
- head: [],
132
- border: [],
133
- },
134
- });
135
- results.agents.forEach((agent, index) => {
136
- const agentConfig = config.agents.find(a => a.name === agent.name) || config.agents[index];
137
- const agentType = (agentConfig === null || agentConfig === void 0 ? void 0 : agentConfig.type) || "CHAT";
138
- const resourceType = agentType === "AGENT" ? "Agent" : "Chatbot";
139
- table.push([
140
- agent.name,
141
- resourceType,
142
- agent.success ? chalk_1.default.green("Success") : chalk_1.default.red("Failed"),
143
- agent.message
144
- ]);
145
- });
146
- console.log(table.toString());
147
- console.log(`\nTotal: ${results.success + results.failed}, Successful: ${results.success}, Failed: ${results.failed}`);
148
- }
149
- catch (error) {
150
- spinner.fail("Failed to process YAML file");
151
- if (error.code === 'ENOENT') {
152
- console.error(chalk_1.default.red(`File not found: ${yamlFilePath}`));
153
- }
154
- else if (error.name === 'YAMLException') {
155
- console.error(chalk_1.default.red(`Invalid YAML format: ${error.message}`));
156
- }
157
- else {
158
- console.error(chalk_1.default.red("Error:"), error.message || "Unknown error");
159
- }
160
- }
161
- }
162
- catch (error) {
163
- if (error.message && error.message.includes('Authentication')) {
164
- console.error(chalk_1.default.red(error.message));
165
- }
166
- else {
167
- console.error(chalk_1.default.red("Error:"), error instanceof Error ? error.message : "Unknown error");
168
- }
169
- }
170
- }));
171
- }
172
- //# sourceMappingURL=createFromYaml.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createFromYaml.js","sourceRoot":"","sources":["../../../src/commands/agent/createFromYaml.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAYA,sEAmLC;AA9LD,6CAAqD;AACrD,kDAA0B;AAC1B,8CAAsB;AACtB,4DAA+B;AAC/B,4CAAoB;AACpB,sDAA2B;AAE3B,2CAA4C;AAC5C,qCAAuC;AACvC,yCAAwC;AAExC,SAAgB,6BAA6B,CAAC,YAAqB;IACjE,YAAY;SACT,OAAO,CAAC,+BAA+B,CAAC;SACxC,WAAW,CAAC,uDAAuD,CAAC;SACpE,MAAM,CAAC,uBAAuB,EAAE,8CAA8C,CAAC;SAC/E,MAAM,CAAC,CAAO,QAAgB,EAAE,SAAc,EAAE,OAAY,EAAE,EAAE;QAC/D,IAAI,CAAC;YACH,IAAI,IAAI,GAAG,IAAA,qBAAU,GAAE,CAAC;YAExB,iDAAiD;YACjD,IAAI,YAAoB,CAAC;YAEzB,IAAI,QAAQ,KAAK,MAAM,IAAI,SAAS,EAAE,CAAC;gBACrC,YAAY,GAAG,SAAS,CAAC;YAC3B,CAAC;iBAAM,IAAI,QAAQ,EAAE,CAAC;gBACpB,YAAY,GAAG,QAAQ,CAAC;gBACxB,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,CAAC,OAAO,EAAE,CAAC;oBAC9C,OAAO,GAAG,SAAS,CAAC;gBACtB,CAAC;YACH,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,KAAK,CAAC,eAAK,CAAC,GAAG,CAAC,wGAAwG,CAAC,CAAC,CAAC;gBACnI,OAAO;YACT,CAAC;YAED,uBAAuB;YACvB,IAAI,CAAC,YAAE,CA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h } from "../../utils/error";
3
- import chalk from "chalk";
4
- import ora from "ora";
5
- import Table from "cli-table3";
6
- import fs from "fs";
7
- import yaml from 'js-yaml';
8
- import { AgentYamlConfig } from "../../types/agentYaml";
9
- import { userData } from "../../utils/user";
10
- import { createAgent } from "./create";
11
- import { createApis } from "./apiUtils";
12
-
13
- export function registerCreateFromYamlCommand(agentCommand: Command): void {
14
- agentCommand
15
- .command("create [firstArg] [secondArg]")
16
- .description("Create multiple agents from a YAML configuration file")
17
- .option("--project <projectId>", "Project ID (defaults to the current project)")
18
- .action(async (firstArg: string, secondArg: any, options: any) => {
19
- try {
20
- let apis = createApis();
21
-
22
- // Determine which argument is the YAML file path
23
- let yamlFilePath: string;
24
-
25
- if (firstArg === 'file' && secondArg) {
26
- yamlFilePath = secondArg;
27
- } else if (firstArg) {
28
- yamlFilePath = firstArg;
29
- if (typeof secondArg === 'object' && !options) {
30
- options = secondArg;
31
- }
32
- } else {
33
- console.error(chalk.red("Missing YAML file path. Usage: nestbox agent create <yamlFile> OR nestbox agent create file <yamlFile>"));
34
- return;
35
- }
36
-
37
- // Check if file exists
38
- if (!fs.existsSync(yamlFilePath)) {
39
- console.error(chalk.red(`YAML file not found: ${yamlFilePath}`));
40
- return;
41
- }
42
-
43
- // Read and parse the YAML file
44
- const spinner = ora(`Reading agents configuration from ${yamlFilePath}...`).start();
45
-
46
- try {
47
- const fileContents = fs.readFileSync(yamlFilePath, 'utf8');
48
- const config = yaml.load(fileContents) as AgentYamlConfig;
49
-
50
- if (!config || !config.agents || !Array.isArray(config.agents)) {
51
- spinner.fail("Invalid YAML configuration: Missing 'agents' array");
52
- console.error(chalk.red("The YAML file should contain an 'agents' array with agent configurations"));
53
- return;
54
- }
55
-
56
- spinner.succeed(`Found ${config.agents.length} agents in configuration file`);
57
-
58
- // Process each agent with token refresh support
59
- const results = {
60
- success: 0,
61
- failed: 0,
62
- agents: [] as Array<{name: string; success: boolean; message: string}>
63
- };
64
-
65
- // Get user data once
66
- const user = await userData();
67
-
68
- for (const agent of config.agents) {
69
- if (!agent.name) {
70
- console.log(chalk.yellow("Skipping agent with no name defined"));
71
- results.failed++;
72
- results.agents.push({
73
- name: "unnamed",
74
- success: false,
75
- message: "Name is required"
76
- });
77
- continue;
78
- }
79
-
80
- let agentType = agent.type || "CHAT";
81
- const resourceType = agentType === "AGENT" ? "Agent" : "Chatbot";
82
-
83
- const agentSpinner = ora(`Creating ${resourceType.toLowerCase()} '${agent.name}'...`).start();
84
-
85
- try {
86
- // Create agent with token refresh support
87
- await withTokenRefresh(
88
- async () => {
89
- // Map YAML config to createAgent options
90
- const createOptions = {
91
- ...options,
92
- goal: agent.goal || "No goal specified",
93
- modelBaseId: agent.modelBaseId || "",
94
- instanceIP: agent.instanceIP || "localhost",
95
- machineInstanceId: agent.machineInstanceId || 1,
96
- machineManifestId: agent.machineManifestId || "default",
97
- machineName: agent.machineName || `agent-${agent.name.toLowerCase()}`,
98
- type: agentType,
99
- userId: user.id,
100
- parameters: agent.parameters ? agent.parameters.map((p: any) => {
101
- return {
102
- name: p.name || "unnamed",
103
- description: p.description || "",
104
- default: p.default || "",
105
- isUserParam: p.isUserParam !== undefined ? p.isUserParam : true
106
- };
107
- }) : []
108
- };
109
-
110
- await createAgent(agent.name, createOptions, apis.agentsApi, apis.projectsApi);
111
- },
112
- () => {
113
- apis = createApis();
114
- }
115
- );
116
-
117
- agentSpinner.stop();
118
-
119
- results.success++;
120
- results.agents.push({
121
- name: agent.name,
122
- success: true,
123
- message: `Created successfully`
124
- });
125
- } catch (error: any) {
126
- agentSpinner.fail(`Failed to create ${resourceType.toLowerCase()} '${agent.name}'`);
127
- console.error(chalk.red(`Error: ${error.message}`));
128
- results.failed++;
129
- results.agents.push({
130
- name: agent.name,
131
- success: false,
132
- message: error.message
133
- });
134
- }
135
- }
136
-
137
- // Final summary
138
- console.log(chalk.blue("\nResource creation summary:"));
139
- const table = new Table({
140
- head: [
141
- chalk.white.bold("Name"),
142
- chalk.white.bold("Type"),
143
- chalk.white.bold("Status"),
144
- chalk.white.bold("Message"),
145
- ],
146
- style: {
147
- head: [],
148
- border: [],
149
- },
150
- });
151
-
152
- results.agents.forEach((agent, index) => {
153
- const agentConfig = config.agents.find(a => a.name === agent.name) || config.agents[index];
154
- const agentType = agentConfig?.type || "CHAT";
155
- const resourceType = agentType === "AGENT" ? "Agent" : "Chatbot";
156
-
157
- table.push([
158
- agent.name,
159
- resourceType,
160
- agent.success ? chalk.green("Success") : chalk.red("Failed"),
161
- agent.message
162
- ]);
163
- });
164
-
165
- console.log(table.toString());
166
- console.log(`\nTotal: ${results.success + results.failed}, Successful: ${results.success}, Failed: ${results.failed}`);
167
-
168
- } catch (error: any) {
169
- spinner.fail("Failed to process YAML file");
170
- if (error.code === 'ENOENT') {
171
- console.error(chalk.red(`File not found: ${yamlFilePath}`));
172
- } else if (error.name === 'YAMLException') {
173
- console.error(chalk.red(`Invalid YAML format: ${error.message}`));
174
- } else {
175
- console.error(
176
- chalk.red("Error:"),
177
- error.message || "Unknown error"
178
- );
179
- }
180
- }
181
- } catch (error: any) {
182
- if (error.message && error.message.includes('Authentication')) {
183
- console.error(chalk.red(error.message));
184
- } else {
185
- console.error(
186
- chalk.red("Error:"),
187
- error instanceof Error ? error.message : "Unknown error"
188
- );
189
- }
190
- }
191
- });
192
- }