Skip to content

Commit 45931a9

Browse files
committed
tweak the cli on start
1 parent e1692a6 commit 45931a9

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

packages/cli/src/commands/start.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "node:fs";
22
import net from "node:net";
33
import os from "node:os";
4-
import path from "node:path";
4+
import path, { dirname } from "node:path";
55
import { fileURLToPath } from "node:url";
66
import { buildProject } from "@/src/utils/build-project";
77
import {
@@ -115,8 +115,20 @@ async function startAgent(
115115
): Promise<IAgentRuntime> {
116116
character.id ??= stringToUuid(character.name);
117117

118-
// get the cli version
119-
const cliVersion = require("@elizaos/cli/package.json").version;
118+
// For ESM modules we need to use import.meta.url instead of __dirname
119+
const __filename = fileURLToPath(import.meta.url);
120+
const __dirname = dirname(__filename);
121+
122+
// Find package.json relative to the current file
123+
const packageJsonPath = path.resolve(__dirname, "../package.json");
124+
125+
// Add a simple check in case the path is incorrect
126+
let version = "0.0.0"; // Fallback version
127+
if (!fs.existsSync(packageJsonPath)) {
128+
} else {
129+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
130+
version = packageJson.version;
131+
}
120132

121133
// for each plugin, check if it installed, and install if it is not
122134
for (const plugin of character.plugins) {
@@ -125,8 +137,8 @@ async function startAgent(
125137
try {
126138
await import(plugin);
127139
} catch (error) {
128-
logger.info(`Plugin ${plugin} not installed, installing...`);
129-
await installPlugin(plugin, process.cwd(), cliVersion);
140+
logger.info(`Plugin ${plugin} not installed, installing into ${process.cwd()}...`);
141+
await installPlugin(plugin, process.cwd(), version);
130142
}
131143
}
132144

packages/cli/src/utils/install-plugin.ts

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ export async function installPlugin(
1515
cwd: string,
1616
version?: string,
1717
): Promise<boolean> {
18+
if(version) {
19+
try {
20+
await execa('bun', ['add', `${repository}@${version}`], {
21+
cwd,
22+
stdio: 'inherit',
23+
});
24+
return true;
25+
} catch (error) {
26+
logger.debug('Plugin not found on npm, trying to install from registry...');
27+
}
28+
}
1829
try {
1930
// Clean repository URL
2031
let repoUrl = repository;

packages/the-org/package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
"module": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"type": "module",
8-
"publishConfig": {
9-
"access": "public"
10-
},
8+
"private": true,
119
"scripts": {
1210
"start": "bun ../cli/dist/index.js start",
1311
"build": "vite build && tsup",
@@ -80,5 +78,5 @@
8078
"typescript-eslint": "^8.18.2",
8179
"vite": "^6.0.5"
8280
},
83-
"gitHead": "f66ec5b5830658a5b4f45e9369a00bbb1b81224e"
81+
"gitHead": "e98c03cf7e76ba967b203fdc3fc07cb438797559"
8482
}

0 commit comments

Comments
 (0)