genlayer 0.0.23 → 0.0.24

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.
@@ -6,6 +6,10 @@ module.exports = {
6
6
  outfile: "dist/index.js", // Output file
7
7
  platform: "node",
8
8
  target: "es6",
9
+ define: { 'import.meta.url': '_importMetaUrl' },
10
+ banner: {
11
+ js: "const _importMetaUrl=require('url').pathToFileURL(__filename)",
12
+ },
9
13
  },
10
14
  watch: true,
11
15
  };
@@ -6,6 +6,10 @@ module.exports = {
6
6
  outfile: "dist/index.js", // Output file
7
7
  platform: "node",
8
8
  target: "es6",
9
+ define: { 'import.meta.url': '_importMetaUrl' },
10
+ banner: {
11
+ js: "const _importMetaUrl=require('url').pathToFileURL(__filename)",
12
+ },
9
13
  },
10
14
  watch: false,
11
15
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "genlayer",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
4
4
  "description": "GenLayer Command Line Tool",
5
5
  "main": "src/index.ts",
6
6
  "bin": {
@@ -56,6 +56,7 @@
56
56
  "dotenv": "^16.4.5",
57
57
  "inquirer": "^9.2.19",
58
58
  "node-fetch": "^3.3.2",
59
+ "open": "^10.1.0",
59
60
  "uuid": "^9.0.1"
60
61
  }
61
62
  }
@@ -1,6 +1,7 @@
1
1
  import {Command} from "commander";
2
2
 
3
3
  import {initAction} from "./init";
4
+ import {startAction} from "./start";
4
5
 
5
6
  export function initializeGeneralCommands(program: Command) {
6
7
  program
@@ -8,5 +9,12 @@ export function initializeGeneralCommands(program: Command) {
8
9
  .description("Initialize the GenLayer Environment")
9
10
  .option("-n, --numValidators <numValidators>", "Number of validators", "5")
10
11
  .action(initAction);
12
+
13
+ program
14
+ .command("up")
15
+ .description("Starts GenLayer's simulator")
16
+ .option("-nr, --no-restart", "Don't restart the database and validators", true)
17
+ .action(startAction);
18
+
11
19
  return program;
12
20
  }
@@ -15,7 +15,8 @@ import {
15
15
  pullOllamaModel,
16
16
  getAiProvidersOptions,
17
17
  getSimulatorLocation,
18
- readEnvConfigValue,
18
+ getFrontendUrl,
19
+ openFrontend,
19
20
  } from "@/lib/services/simulator";
20
21
  export interface InitActionOptions {
21
22
  numValidators: number;
@@ -105,7 +106,7 @@ export async function initAction(options: InitActionOptions) {
105
106
  const providerConfig = AI_PROVIDERS_CONFIG[provider];
106
107
  const questions = [
107
108
  {
108
- type: "password",
109
+ type: "input",
109
110
  name: providerConfig.cliOptionValue,
110
111
  message: `Please enter your ${providerConfig.name} API Key:`,
111
112
  validate: function (value: string) {
@@ -158,7 +159,7 @@ export async function initAction(options: InitActionOptions) {
158
159
 
159
160
  // Ollama doesn't need changes in configuration, we just run it
160
161
  if (selectedLlmProviders.includes("ollama")) {
161
- console.log("Pulling llama2 from Ollama...")
162
+ console.log("Pulling llama2 from Ollama...");
162
163
  await pullOllamaModel();
163
164
  }
164
165
 
@@ -190,7 +191,14 @@ export async function initAction(options: InitActionOptions) {
190
191
  console.error(error);
191
192
  return;
192
193
  }
193
-
194
- const frontendPort = readEnvConfigValue('FRONTEND_PORT');
195
- console.log(`GenLayer simulator initialized successfully! Go to http://localhost:${frontendPort} in your browser to access it.`);
194
+
195
+ // Simulator ready
196
+ console.log(
197
+ `GenLayer simulator initialized successfully! Go to ${getFrontendUrl()} in your browser to access it.`,
198
+ );
199
+ try {
200
+ openFrontend();
201
+ } catch (error) {
202
+ console.error(error);
203
+ }
196
204
  }
@@ -0,0 +1,99 @@
1
+ import {
2
+ updateSimulator,
3
+ runSimulator,
4
+ waitForSimulatorToBeReady,
5
+ deleteAllValidators,
6
+ createRandomValidators,
7
+ clearDatabaseTables,
8
+ initializeDatabase,
9
+ getFrontendUrl,
10
+ openFrontend,
11
+ } from "@/lib/services/simulator";
12
+
13
+ export interface StartActionOptions {
14
+ restart: string;
15
+ }
16
+
17
+ export async function startAction(options: StartActionOptions) {
18
+ const restartHintText = options.restart
19
+ ? "restarting the database and validators"
20
+ : "keeping the database and validators previous configuration";
21
+ console.log(`Starting GenLayer simulator ${restartHintText}`);
22
+
23
+ // Update the simulator to the latest version
24
+ console.log(`Updating GenLayer Simulator...`);
25
+ try {
26
+ await updateSimulator();
27
+ } catch (error) {
28
+ console.error(error);
29
+ return;
30
+ }
31
+
32
+ // Run the GenLayer Simulator
33
+ console.log("Running the GenLayer Simulator...");
34
+ try {
35
+ runSimulator();
36
+ } catch (error) {
37
+ console.error(error);
38
+ return;
39
+ }
40
+
41
+ try {
42
+ const {initialized, error} = await waitForSimulatorToBeReady();
43
+ if (!initialized && error === "ERROR") {
44
+ console.error("Unable to initialize the GenLayer simulator. Please try again.");
45
+ return;
46
+ }
47
+ if (!initialized && error === "TIMEOUT") {
48
+ console.error(
49
+ "The simulator is taking too long to initialize. Please try again after the simulator is ready.",
50
+ );
51
+ return;
52
+ }
53
+ console.log("Simulator is running!");
54
+ } catch (error) {
55
+ console.error(error);
56
+ return;
57
+ }
58
+
59
+ if (options.restart) {
60
+ // Initialize the database
61
+ console.log("Initializing the database...");
62
+ try {
63
+ //remove everything from the database
64
+ await clearDatabaseTables();
65
+
66
+ const {createResponse, tablesResponse} = await initializeDatabase();
67
+ if (!createResponse || !tablesResponse) {
68
+ console.error("Unable to initialize the database. Please try again.");
69
+ return;
70
+ }
71
+ } catch (error) {
72
+ console.error(error);
73
+ return;
74
+ }
75
+
76
+ // Initializing validators
77
+ console.log("Initializing validators...");
78
+ try {
79
+ //remove all validators
80
+ await deleteAllValidators();
81
+ // create random validators
82
+ await createRandomValidators();
83
+ } catch (error) {
84
+ console.error("Unable to initialize the validators.");
85
+ console.error(error);
86
+ return;
87
+ }
88
+ }
89
+
90
+ // Simulator ready
91
+ console.log(
92
+ `GenLayer simulator initialized successfully! Go to ${getFrontendUrl()} in your browser to access it.`,
93
+ );
94
+ try {
95
+ openFrontend();
96
+ } catch (error) {
97
+ console.error(error);
98
+ }
99
+ }
@@ -1,6 +1,7 @@
1
1
  import util from "node:util";
2
- import {PromiseWithChild, exec} from "child_process";
2
+ import {ChildProcess, PromiseWithChild, exec} from "child_process";
3
3
  import os from "os";
4
+ import open from "open";
4
5
 
5
6
  import {RunningPlatform, AVAILABLE_PLATFORMS} from "@/lib/config/simulator";
6
7
  import {MissingRequirementError} from "../errors/missingRequirement";
@@ -60,3 +61,7 @@ function getPlatform(): RunningPlatform {
60
61
  }
61
62
  return currentPlatform;
62
63
  }
64
+
65
+ export function openUrl(url: string): Promise<ChildProcess> {
66
+ return open(url);
67
+ }
@@ -16,17 +16,18 @@ import {
16
16
  getHomeDirectory,
17
17
  executeCommand,
18
18
  executeCommandInNewTerminal,
19
+ openUrl,
19
20
  } from "@/lib/clients/system";
20
21
  import {MissingRequirementError} from "../errors/missingRequirement";
21
22
 
22
23
  // Private helper functions
23
24
  export function getSimulatorLocation(): string {
24
- return path.join(getHomeDirectory(), 'genlayer-simulator');
25
+ return path.join(getHomeDirectory(), "genlayer-simulator");
25
26
  }
26
27
 
27
28
  export function readEnvConfigValue(key: string): string {
28
29
  const simulatorLocation = getSimulatorLocation();
29
- const envFilePath = path.join(simulatorLocation, '.env');
30
+ const envFilePath = path.join(simulatorLocation, ".env");
30
31
  // Transform the config string to object
31
32
  const envConfig = dotenv.parse(fs.readFileSync(envFilePath, "utf8"));
32
33
  return envConfig[key];
@@ -38,7 +39,7 @@ function sleep(millliseconds: number): Promise<void> {
38
39
 
39
40
  function addConfigToEnvFile(newConfig: Record<string, string>): void {
40
41
  const simulatorLocation = getSimulatorLocation();
41
- const envFilePath = path.join(simulatorLocation, '.env');
42
+ const envFilePath = path.join(simulatorLocation, ".env");
42
43
 
43
44
  // Create a backup of the original .env file
44
45
  fs.writeFileSync(`${envFilePath}.bak`, fs.readFileSync(envFilePath));
@@ -125,9 +126,9 @@ export async function pullOllamaModel(): Promise<boolean> {
125
126
 
126
127
  export async function configSimulator(newConfig: Record<string, string>): Promise<boolean> {
127
128
  const simulatorLocation = getSimulatorLocation();
128
- const envExample = path.join(simulatorLocation, '.env.example');
129
- const envFilePath = path.join(simulatorLocation, '.env');
130
- fs.copyFileSync(envExample, envFilePath);
129
+ const envExample = path.join(simulatorLocation, ".env.example");
130
+ const envFilePath = path.join(simulatorLocation, ".env");
131
+ fs.copyFileSync(envExample, envFilePath);
131
132
  addConfigToEnvFile(newConfig);
132
133
  return true;
133
134
  }
@@ -195,3 +196,13 @@ export function getAiProvidersOptions(): Array<{name: string; value: string}> {
195
196
  return {name: providerConfig.name, value: providerConfig.cliOptionValue};
196
197
  });
197
198
  }
199
+
200
+ export function getFrontendUrl(): string {
201
+ const frontendPort = readEnvConfigValue("FRONTEND_PORT");
202
+ return `http://localhost:${frontendPort}`;
203
+ }
204
+
205
+ export async function openFrontend(): Promise<boolean> {
206
+ await openUrl(getFrontendUrl());
207
+ return true;
208
+ }