Skip to content

Commit d591357

Browse files
committed
replace process.env with settings, fix .env path
1 parent 6ef7e56 commit d591357

File tree

13 files changed

+841
-789
lines changed

13 files changed

+841
-789
lines changed
File renamed without changes.

packages/core/.env.test .env.test

File renamed without changes.

packages/agent/src/index.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
followRoom,
99
getTokenForProvider,
1010
IAgentRuntime,
11+
settings,
1112
initializeClients,
1213
initializeDatabase,
1314
loadActionConfigs,
@@ -21,7 +22,6 @@ import {
2122
walletProvider,
2223
} from "@eliza/core";
2324
import readline from "readline";
24-
console.log("Program starting")
2525
const args = parseArguments();
2626

2727
let charactersArg = args.characters || args.character;
@@ -34,7 +34,7 @@ if (charactersArg) {
3434

3535
const directClient = new DirectClient();
3636

37-
const serverPort = parseInt(process.env.SERVER_PORT || "3000");
37+
const serverPort = parseInt(settings.SERVER_PORT || "3000");
3838
directClient.start(serverPort);
3939

4040
export async function createAgent(
@@ -43,7 +43,6 @@ export async function createAgent(
4343
token: string,
4444
configPath: string = "./elizaConfig.yaml"
4545
) {
46-
console.log("Creating runtime for character", character.name);
4746
return new AgentRuntime({
4847
databaseAdapter: db,
4948
token,
@@ -53,7 +52,7 @@ export async function createAgent(
5352
providers: [
5453
timeProvider,
5554
boredomProvider,
56-
character.settings?.secrets?.WALLET_PUBLIC_KEY && walletProvider,
55+
(character.settings?.secrets?.WALLET_PUBLIC_KEY !== undefined) && walletProvider,
5756
].filter(Boolean),
5857
actions: [
5958
...defaultActions,
@@ -115,14 +114,12 @@ const rl = readline.createInterface({
115114
});
116115

117116
async function handleUserInput(input) {
118-
console.log("input --> ", input)
119117
if (input.toLowerCase() === "exit") {
120118
rl.close();
121119
return;
122120
}
123121

124122
const agentId = characters[0].name.toLowerCase();
125-
console.log("agnetId --> ", agentId)
126123
try {
127124
const response = await fetch(
128125
`http://localhost:${serverPort}/${agentId}/message`,

packages/agent/tsconfig.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@
77
"moduleResolution": "Bundler",
88
"types": ["node"]
99
},
10-
"include": ["src"],
11-
"references": [
12-
{ "path": "../core" }
13-
]
10+
"include": ["src"]
1411
}

packages/core/src/cli/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ export function getTokenForProvider(
130130
}
131131
}
132132
export function initializeDatabase() {
133-
if (process.env.POSTGRES_URL) {
133+
if (settings.POSTGRES_URL) {
134134
return new Adapter.PostgresDatabaseAdapter({
135-
connectionString: process.env.POSTGRES_URL,
135+
connectionString: settings.POSTGRES_URL,
136136
});
137137
} else {
138138
return new Adapter.SqliteDatabaseAdapter(new Database("./db.sqlite"));

packages/core/src/core/settings.ts

+52-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,55 @@
11
import { config } from "dotenv";
2-
config();
2+
import fs from "fs";
3+
import path from "path";
34

4-
const settings = process.env;
5+
/**
6+
* Recursively searches for a .env file starting from the current directory
7+
* and moving up through parent directories
8+
* @param {string} [startDir=process.cwd()] - Starting directory for the search
9+
* @returns {string|null} Path to the nearest .env file or null if not found
10+
*/
11+
function findNearestEnvFile(startDir = process.cwd()) {
12+
let currentDir = startDir;
13+
14+
// Continue searching until we reach the root directory
15+
while (currentDir !== path.parse(currentDir).root) {
16+
const envPath = path.join(currentDir, '.env');
17+
18+
if (fs.existsSync(envPath)) {
19+
return envPath;
20+
}
21+
22+
// Move up to parent directory
23+
currentDir = path.dirname(currentDir);
24+
}
25+
26+
// Check root directory as well
27+
const rootEnvPath = path.join(path.parse(currentDir).root, '.env');
28+
return fs.existsSync(rootEnvPath) ? rootEnvPath : null;
29+
}
530

6-
export default settings;
31+
/**
32+
* Loads environment variables from the nearest .env file
33+
* @returns {Object} Environment variables object
34+
* @throws {Error} If no .env file is found
35+
*/
36+
function loadEnvConfig() {
37+
const envPath = findNearestEnvFile();
38+
39+
if (!envPath) {
40+
throw new Error("No .env file found in current or parent directories.");
41+
}
42+
43+
// Load the .env file
44+
const result = config({ path: envPath });
45+
46+
if (result.error) {
47+
throw new Error(`Error loading .env file: ${result.error}`);
48+
}
49+
50+
console.log(`Loaded .env file from: ${envPath}`);
51+
return process.env;
52+
}
53+
54+
export const settings = loadEnvConfig();
55+
export default settings;

packages/core/src/providers/trustScoreProvider.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ interface sellDetails {
3131
export class TrustScoreProvider {
3232
private tokenProvider: TokenProvider;
3333
private trustScoreDb: TrustScoreDatabase;
34-
private connection: Connection = new Connection(process.env.RPC_URL!);
35-
private baseMint: PublicKey = new PublicKey(process.env.BASE_MINT!);
34+
private connection: Connection = new Connection(settings.RPC_URL!);
35+
private baseMint: PublicKey = new PublicKey(settings.BASE_MINT!);
3636
private DECAY_RATE = 0.95;
3737
private MAX_DECAY_DAYS = 30;
3838
constructor(

0 commit comments

Comments
 (0)