@nestbox-ai/cli 1.0.23 → 1.0.25

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 (61) hide show
  1. package/dist/commands/agent/apiUtils.d.ts +10 -0
  2. package/dist/commands/agent/apiUtils.js +20 -0
  3. package/dist/commands/agent/apiUtils.js.map +1 -0
  4. package/dist/commands/agent/create.d.ts +29 -0
  5. package/dist/commands/agent/create.js +88 -0
  6. package/dist/commands/agent/create.js.map +1 -0
  7. package/dist/commands/agent/createFromYaml.d.ts +2 -0
  8. package/dist/commands/agent/createFromYaml.js +172 -0
  9. package/dist/commands/agent/createFromYaml.js.map +1 -0
  10. package/dist/commands/agent/deploy.d.ts +2 -0
  11. package/dist/commands/agent/deploy.js +243 -0
  12. package/dist/commands/agent/deploy.js.map +1 -0
  13. package/dist/commands/agent/generate.d.ts +2 -0
  14. package/dist/commands/agent/generate.js +141 -0
  15. package/dist/commands/agent/generate.js.map +1 -0
  16. package/dist/commands/agent/index.d.ts +7 -0
  17. package/dist/commands/agent/index.js +21 -0
  18. package/dist/commands/agent/index.js.map +1 -0
  19. package/dist/commands/agent/list.d.ts +2 -0
  20. package/dist/commands/agent/list.js +94 -0
  21. package/dist/commands/agent/list.js.map +1 -0
  22. package/dist/commands/agent/remove.d.ts +2 -0
  23. package/dist/commands/agent/remove.js +85 -0
  24. package/dist/commands/agent/remove.js.map +1 -0
  25. package/dist/commands/agent.d.ts +4 -0
  26. package/dist/commands/agent.js +16 -731
  27. package/dist/commands/agent.js.map +1 -1
  28. package/dist/commands/compute/apiUtils.d.ts +11 -0
  29. package/dist/commands/compute/apiUtils.js +22 -0
  30. package/dist/commands/compute/apiUtils.js.map +1 -0
  31. package/dist/commands/compute/create.d.ts +2 -0
  32. package/dist/commands/compute/create.js +183 -0
  33. package/dist/commands/compute/create.js.map +1 -0
  34. package/dist/commands/compute/delete.d.ts +2 -0
  35. package/dist/commands/compute/delete.js +145 -0
  36. package/dist/commands/compute/delete.js.map +1 -0
  37. package/dist/commands/compute/index.d.ts +4 -0
  38. package/dist/commands/compute/index.js +14 -0
  39. package/dist/commands/compute/index.js.map +1 -0
  40. package/dist/commands/compute/list.d.ts +2 -0
  41. package/dist/commands/compute/list.js +128 -0
  42. package/dist/commands/compute/list.js.map +1 -0
  43. package/dist/commands/compute.d.ts +3 -0
  44. package/dist/commands/compute.js +10 -400
  45. package/dist/commands/compute.js.map +1 -1
  46. package/package.json +1 -1
  47. package/src/commands/agent/apiUtils.ts +24 -0
  48. package/src/commands/agent/create.ts +105 -0
  49. package/src/commands/agent/createFromYaml.ts +192 -0
  50. package/src/commands/agent/deploy.ts +272 -0
  51. package/src/commands/agent/generate.ts +151 -0
  52. package/src/commands/agent/index.ts +12 -0
  53. package/src/commands/agent/list.ts +104 -0
  54. package/src/commands/agent/remove.ts +103 -0
  55. package/src/commands/agent.ts +15 -909
  56. package/src/commands/compute/apiUtils.ts +28 -0
  57. package/src/commands/compute/create.ts +195 -0
  58. package/src/commands/compute/delete.ts +147 -0
  59. package/src/commands/compute/index.ts +7 -0
  60. package/src/commands/compute/list.ts +125 -0
  61. package/src/commands/compute.ts +16 -449
@@ -0,0 +1,28 @@
1
+ import { MachineInstancesApi, MiscellaneousApi, ProjectsApi } from "@nestbox-ai/admin";
2
+ import { setupAuthAndConfig } from "../../utils/api";
3
+
4
+ export interface ComputeApiInstances {
5
+ machineInstanceApi: MachineInstancesApi;
6
+ miscellaneousApi: MiscellaneousApi;
7
+ projectsApi: ProjectsApi;
8
+ authToken: any;
9
+ }
10
+
11
+ /**
12
+ * Create API instances for compute commands using setupAuthAndConfig
13
+ */
14
+ export function createComputeApis(): ComputeApiInstances | null {
15
+ const authResult = setupAuthAndConfig();
16
+ if (!authResult) {
17
+ return null;
18
+ }
19
+
20
+ const { authToken, configuration } = authResult;
21
+
22
+ return {
23
+ machineInstanceApi: new MachineInstancesApi(configuration),
24
+ miscellaneousApi: new MiscellaneousApi(configuration),
25
+ projectsApi: new ProjectsApi(configuration),
26
+ authToken
27
+ };
28
+ }
@@ -0,0 +1,195 @@
1
+ import { Command } from "commander";
2
+ import chalk from "chalk";
3
+ import ora from "ora";
4
+ import { resolveProject } from "../../utils/project";
5
+ import { createComputeApis } from "./apiUtils";
6
+ import inquirer from "inquirer";
7
+ import axios from "axios";
8
+ import { userData } from "../../utils/user";
9
+
10
+ export function registerCreateCommand(computeCommand: Command): void {
11
+ computeCommand
12
+ .command('create')
13
+ .description('Create a new compute instance')
14
+ .requiredOption('--image <imageId>', 'Image ID to use for the compute instance')
15
+ .option('--project <projectId>', 'Project ID or name (defaults to the current project)')
16
+ .action(async (options) => {
17
+ try {
18
+ const apis = createComputeApis();
19
+ if (!apis) {
20
+ return;
21
+ }
22
+
23
+ const { miscellaneousApi, projectsApi, authToken } = apis;
24
+
25
+ // Resolve project using the shared utility
26
+ const project = await resolveProject(projectsApi, options);
27
+
28
+ const spinner = ora(`Fetching available images...`).start();
29
+
30
+ try {
31
+ // Get available images data
32
+ const response = await miscellaneousApi.miscellaneousControllerGetData();
33
+ const availableImages: any = response.data || [];
34
+
35
+ // Find the selected image
36
+ const selectedImage = availableImages.find((image: any) => image.id === options.image);
37
+
38
+ if (!selectedImage) {
39
+ spinner.fail(`Image ID '${options.image}' does not exist.`);
40
+ console.log(chalk.yellow('Available image IDs:'));
41
+ availableImages.forEach((img: any) => {
42
+ console.log(` ${chalk.cyan(img.id)} - ${img.name} (${img.type})`);
43
+ });
44
+ return;
45
+ }
46
+
47
+ spinner.succeed(`Found image: ${selectedImage.name}`);
48
+
49
+ // Ask for instance name first
50
+ const instanceNameAnswer = await inquirer.prompt([
51
+ {
52
+ type: 'input',
53
+ name: 'instanceName',
54
+ message: 'Enter a name for this compute instance:',
55
+ validate: (input: string) => {
56
+ if (!input || input.trim() === '') {
57
+ return 'Instance name cannot be empty';
58
+ }
59
+ return true;
60
+ }
61
+ }
62
+ ]);
63
+
64
+ const instanceName = instanceNameAnswer.instanceName;
65
+
66
+ // Directly create the provisioningParams object with the structure needed for API
67
+ let provisioningParams: Record<string, any> = {};
68
+
69
+ if (selectedImage.provisioningParameters &&
70
+ selectedImage.provisioningParameters.properties) {
71
+
72
+ const requiredParams = selectedImage.provisioningParameters.required || [];
73
+ const paramProperties = selectedImage.provisioningParameters.properties;
74
+
75
+ // Build questions for inquirer
76
+ const questions = [];
77
+
78
+ for (const [paramName, paramConfig] of Object.entries(paramProperties) as any) {
79
+ const isRequired = requiredParams.includes(paramName);
80
+ const paramUI = selectedImage.provisioningParametersUI?.[paramName] || {};
81
+
82
+ let question: any = {
83
+ name: paramName,
84
+ message: paramUI['ui:title'] || `Enter ${paramName}:`,
85
+ when: isRequired // Only ask required params by default
86
+ };
87
+
88
+ // Handle different parameter types
89
+ if (paramConfig.type === 'string') {
90
+ if (paramConfig.enum) {
91
+ question.type = 'list';
92
+ question.choices = paramConfig.enum;
93
+ } else if (paramUI['ui:widget'] === 'password') {
94
+ question.type = 'password';
95
+ } else {
96
+ question.type = 'input';
97
+ }
98
+ } else if (paramConfig.type === 'array') {
99
+ if (paramConfig.items && paramConfig.items.enum) {
100
+ question.type = 'checkbox';
101
+ question.choices = paramConfig.items.enum;
102
+ // Ensure at least one option is selected for required array parameters
103
+ question.validate = (input: any[]) => {
104
+ if (isRequired && (!input || input.length === 0)) {
105
+ return 'Please select at least one option';
106
+ }
107
+ return true;
108
+ };
109
+ }
110
+ }
111
+
112
+ if (paramUI['ui:help']) {
113
+ question.message += ` (${paramUI['ui:help']})`;
114
+ }
115
+
116
+ questions.push(question);
117
+ }
118
+
119
+ if (questions.length > 0) {
120
+ console.log(chalk.blue(`\nPlease provide the required parameters for ${selectedImage.name}:`));
121
+ const answers = await inquirer.prompt(questions);
122
+
123
+ // Assign the answers directly to provisioningParams
124
+ for (const [key, value] of Object.entries(answers)) {
125
+ provisioningParams[key] = value;
126
+ }
127
+ }
128
+ }
129
+
130
+ // Now we have all the required params in provisioningParams
131
+ console.log(chalk.green(`\nCreating compute instance '${instanceName}' with image '${selectedImage.name}'`));
132
+ console.log(chalk.dim('Using the following parameters:'));
133
+
134
+ for (const [key, value] of Object.entries(provisioningParams)) {
135
+ console.log(chalk.dim(` ${key}: ${Array.isArray(value) ? value.join(', ') : value}`));
136
+ }
137
+
138
+ // Start a spinner for the creation process
139
+ const creationSpinner = ora('Creating compute instance...').start();
140
+
141
+ const getUserData = await userData();
142
+
143
+ try {
144
+ const createParams = {
145
+ userId: getUserData.id,
146
+ machineId: selectedImage.id,
147
+ machineTitle: selectedImage.name,
148
+ instanceName: instanceName,
149
+ ...provisioningParams
150
+ };
151
+
152
+ const createResponse = await axios.post(
153
+ `${authToken.serverUrl}/projects/${project.id}/instances`,
154
+ createParams,
155
+ {
156
+ headers: {
157
+ Authorization: authToken.token,
158
+ },
159
+ }
160
+ );
161
+ creationSpinner.succeed('Compute instance created successfully!');
162
+
163
+ console.log(chalk.green(`\nInstance '${instanceName}' is now being provisioned`));
164
+ console.log(chalk.gray('You can check the status with: nestbox compute list'));
165
+ console.log(chalk.green("Instance created successfully!"));
166
+ } catch (createError: any) {
167
+ creationSpinner.fail('Failed to create compute instance');
168
+ if (createError.response && createError.response.status === 401) {
169
+ console.error(chalk.red('Authentication token has expired. Please login again using "nestbox login <domain>".'));
170
+ } else if (createError.response) {
171
+ console.error(chalk.red('API Error:'), createError.response.data?.message || 'Unknown error');
172
+ } else {
173
+ console.error(chalk.red('Error:'), createError.message || 'Unknown error');
174
+ }
175
+ }
176
+
177
+ } catch (error: any) {
178
+ spinner.fail('Failed to fetch available images');
179
+ if (error.response && error.response.status === 401) {
180
+ console.error(chalk.red('Authentication token has expired. Please login again using "nestbox login <domain>".'));
181
+ } else if (error.response) {
182
+ console.error(chalk.red('API Error:'), error.response.data?.message || 'Unknown error');
183
+ } else {
184
+ console.error(chalk.red('Error:'), error.message || 'Unknown error');
185
+ }
186
+ }
187
+ } catch (error: any) {
188
+ if (error.response && error.response.status === 401) {
189
+ console.error(chalk.red('Authentication token has expired. Please login again using "nestbox login <domain>".'));
190
+ } else {
191
+ console.error(chalk.red('Error:'), error.message || 'Unknown error');
192
+ }
193
+ }
194
+ });
195
+ }
@@ -0,0 +1,147 @@
1
+ import { Command } from "commander";
2
+ import chalk from "chalk";
3
+ import ora from "ora";
4
+ import { resolveProject } from "../../utils/project";
5
+ import { createComputeApis } from "./apiUtils";
6
+ import inquirer from "inquirer";
7
+ import axios from "axios";
8
+
9
+ export function registerDeleteCommand(computeCommand: Command): void {
10
+ computeCommand
11
+ .command('delete')
12
+ .description('Delete one or more compute instances')
13
+ .option('--project <projectId>', 'Project ID or name (defaults to the current project)')
14
+ .option('--force', 'Skip confirmation prompt')
15
+ .action(async (options) => {
16
+ try {
17
+ const apis = createComputeApis();
18
+ if (!apis) {
19
+ return;
20
+ }
21
+
22
+ const { machineInstanceApi, projectsApi, authToken } = apis;
23
+
24
+ // Resolve project using the shared utility
25
+ const project = await resolveProject(projectsApi, options);
26
+
27
+ const spinner = ora(`Fetching compute instances for project: ${project.name}`).start();
28
+
29
+ try {
30
+ // Fetch machine instances for the project
31
+ const instancesResponse: any = await machineInstanceApi.machineInstancesControllerGetMachineInstanceByUserId(
32
+ project.id,
33
+ 0, // page
34
+ 50 // limit - increased to show more instances
35
+ );
36
+
37
+ spinner.succeed('Successfully retrieved compute instances');
38
+
39
+ const instances = instancesResponse.data?.machineInstances || [];
40
+
41
+ if (instances.length === 0) {
42
+ console.log(chalk.yellow('No compute instances found for this project.'));
43
+ return;
44
+ }
45
+
46
+ // Create choices for the selection prompt
47
+ const instanceChoices = instances.map((instance: any) => ({
48
+ name: `${instance.instanceName || 'Unnamed'} (${instance.id})`,
49
+ value: instance.id,
50
+ short: instance.instanceName || instance.id
51
+ }));
52
+
53
+ // Prompt user to select instances to delete
54
+ const { selectedInstances } = await inquirer.prompt([
55
+ {
56
+ type: 'checkbox',
57
+ name: 'selectedInstances',
58
+ message: 'Select compute instances to delete:',
59
+ choices: instanceChoices,
60
+ validate: (input) => {
61
+ if (input.length === 0) {
62
+ return 'Please select at least one instance to delete';
63
+ }
64
+ return true;
65
+ }
66
+ }
67
+ ]);
68
+
69
+ if (selectedInstances.length === 0) {
70
+ console.log(chalk.yellow('No instances selected for deletion.'));
71
+ return;
72
+ }
73
+
74
+ // Show selected instances
75
+ console.log(chalk.yellow('\nSelected instances for deletion:'));
76
+ const selectedInstanceDetails = instances
77
+ .filter((instance: any) => selectedInstances.includes(instance.id))
78
+ .map((instance: any) => ` - ${chalk.cyan(instance.instanceName || 'Unnamed')} (${instance.id})`);
79
+
80
+ console.log(selectedInstanceDetails.join('\n'));
81
+
82
+ // Confirm deletion if not using --force
83
+ if (!options.force) {
84
+ const { confirmDeletion } = await inquirer.prompt([
85
+ {
86
+ type: 'confirm',
87
+ name: 'confirmDeletion',
88
+ message: chalk.red('Are you sure you want to delete these instances? This cannot be undone.'),
89
+ default: false
90
+ }
91
+ ]);
92
+
93
+ if (!confirmDeletion) {
94
+ console.log(chalk.yellow('Deletion cancelled.'));
95
+ return;
96
+ }
97
+ }
98
+
99
+ // Process deletion - using single request with all selected IDs
100
+ const deleteSpinner = ora(`Deleting ${selectedInstances.length} instance(s)...`).start();
101
+
102
+ try {
103
+ await axios.delete(
104
+ `${authToken.serverUrl}/projects/${project.id}/instances`,
105
+ {
106
+ data: { ids: selectedInstances },
107
+ headers: {
108
+ Authorization: authToken.token,
109
+ }
110
+ }
111
+ );
112
+
113
+ deleteSpinner.succeed(`Successfully deleted ${selectedInstances.length} instance(s)`);
114
+
115
+ console.log(chalk.green('\nAll selected instances have been deleted'));
116
+ console.log(chalk.gray('You can verify with: nestbox compute list'));
117
+
118
+ } catch (error: any) {
119
+ deleteSpinner.fail(`Failed to delete instances`);
120
+ if (error.response && error.response.status === 401) {
121
+ console.error(chalk.red('Authentication token has expired. Please login again using "nestbox login <domain>".'));
122
+ } else if (error.response) {
123
+ console.error(chalk.red('API Error:'), error.response.data?.message || 'Unknown error');
124
+ } else {
125
+ console.error(chalk.red('Error:'), error.message || 'Unknown error');
126
+ }
127
+ }
128
+
129
+ } catch (error: any) {
130
+ spinner.fail('Failed to retrieve compute instances');
131
+ if (error.response && error.response.status === 401) {
132
+ console.error(chalk.red('Authentication token has expired. Please login again using "nestbox login <domain>".'));
133
+ } else if (error.response) {
134
+ console.error(chalk.red('API Error:'), error.response.data?.message || 'Unknown error');
135
+ } else {
136
+ console.error(chalk.red('Error:'), error.message || 'Unknown error');
137
+ }
138
+ }
139
+ } catch (error: any) {
140
+ if (error.response && error.response.status === 401) {
141
+ console.error(chalk.red('Authentication token has expired. Please login again using "nestbox login <domain>".'));
142
+ } else {
143
+ console.error(chalk.red('Error:'), error.message || 'Unknown error');
144
+ }
145
+ }
146
+ });
147
+ }
@@ -0,0 +1,7 @@
1
+ // Compute command exports
2
+ export { registerListCommand } from "./list";
3
+ export { registerCreateCommand } from "./create";
4
+ export { registerDeleteCommand } from "./delete";
5
+
6
+ // API utilities
7
+ export { createComputeApis, type ComputeApiInstances } from "./apiUtils";
@@ -0,0 +1,125 @@
1
+ import { Command } from "commander";
2
+ import chalk from "chalk";
3
+ import ora from "ora";
4
+ import Table from "cli-table3";
5
+ import { resolveProject } from "../../utils/project";
6
+ import { createComputeApis } from "./apiUtils";
7
+
8
+ export function registerListCommand(computeCommand: Command): void {
9
+ computeCommand
10
+ .command('list')
11
+ .description('List all compute instances')
12
+ .option('--project <projectId>', 'Project ID or name (defaults to the current project)')
13
+ .action(async (options) => {
14
+ try {
15
+ const apis = createComputeApis();
16
+ if (!apis) {
17
+ return;
18
+ }
19
+
20
+ const { machineInstanceApi, projectsApi } = apis;
21
+
22
+ // Resolve project using the shared utility
23
+ const project = await resolveProject(projectsApi, options);
24
+
25
+ const spinner = ora(`Fetching compute instances for project: ${project.name}`).start();
26
+
27
+ try {
28
+ // Fetch machine instances for the project
29
+ const instancesResponse: any = await machineInstanceApi.machineInstancesControllerGetMachineInstanceByUserId(
30
+ project.id,
31
+ 0, // page
32
+ 10 // limit
33
+ );
34
+
35
+ spinner.succeed('Successfully retrieved compute instances');
36
+
37
+ const instances = instancesResponse.data?.machineInstances || [];
38
+
39
+ if (instances.length === 0) {
40
+ console.log(chalk.yellow('No compute instances found for this project.'));
41
+ return;
42
+ }
43
+
44
+ // Create table for display
45
+ const table = new Table({
46
+ head: [
47
+ chalk.white.bold('ID'),
48
+ chalk.white.bold('Name'),
49
+ chalk.white.bold('Status'),
50
+ chalk.white.bold('API Key')
51
+ ],
52
+ style: {
53
+ head: [], // Disable the default styling
54
+ border: []
55
+ }
56
+ });
57
+
58
+ // Status mappings
59
+ const statusMappings: any = {
60
+ 'Job Scheduled': 'Scheduled',
61
+ 'Job Executed': 'Ready',
62
+ 'Job in Progress': 'Initializing',
63
+ 'Job Failed': 'Failed',
64
+ 'Deleting': 'Deleting',
65
+ };
66
+
67
+ // Add rows to the table
68
+ instances.forEach((instance: any) => {
69
+ // Map the status if a mapping exists
70
+ const originalStatus = instance.runningStatus || 'unknown';
71
+ const displayStatus = statusMappings[originalStatus] || originalStatus;
72
+
73
+ // Color the status based on its mapped value
74
+ let statusColor;
75
+
76
+ switch(displayStatus.toLowerCase()) {
77
+ case 'ready':
78
+ statusColor = chalk.green(displayStatus);
79
+ break;
80
+ case 'failed':
81
+ statusColor = chalk.red(displayStatus);
82
+ break;
83
+ case 'initializing':
84
+ statusColor = chalk.yellow(displayStatus);
85
+ break;
86
+ case 'scheduled':
87
+ statusColor = chalk.blue(displayStatus);
88
+ break;
89
+ case 'deleting':
90
+ statusColor = chalk.red(displayStatus);
91
+ break;
92
+ default:
93
+ statusColor = chalk.gray(displayStatus);
94
+ }
95
+
96
+ table.push([
97
+ instance.id || 'N/A',
98
+ instance.instanceName || 'N/A',
99
+ statusColor,
100
+ instance.instanceApiKey || 'N/A'
101
+ ]);
102
+ });
103
+
104
+ // Display the table
105
+ console.log(table.toString());
106
+
107
+ } catch (error: any) {
108
+ spinner.fail('Failed to retrieve compute instances');
109
+ if (error.response && error.response.status === 401) {
110
+ console.error(chalk.red('Authentication token has expired. Please login again using "nestbox login <domain>".'));
111
+ } else if (error.response) {
112
+ console.error(chalk.red('API Error:'), error.response.data?.message || 'Unknown error');
113
+ } else {
114
+ console.error(chalk.red('Error:'), error.message || 'Unknown error');
115
+ }
116
+ }
117
+ } catch (error: any) {
118
+ if (error.response && error.response.status === 401) {
119
+ console.error(chalk.red('Authentication token has expired. Please login again using "nestbox login <domain>".'));
120
+ } else {
121
+ console.error(chalk.red('Error:'), error.message || 'Unknown error');
122
+ }
123
+ }
124
+ });
125
+ }