Skip to content

Commit d91e1d9

Browse files
committed
Fixes and refactor
1 parent 86e6d48 commit d91e1d9

File tree

2 files changed

+79
-47
lines changed

2 files changed

+79
-47
lines changed

agent/src/index.ts

+70-41
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ import net from "net";
102102
import path from "path";
103103
import { fileURLToPath } from "url";
104104
import yargs from "yargs";
105-
import { Plugin } from "@ai16z/eliza/src/types";
106-
import {dominosPlugin} from "@elizaos/plugin-dominos";
105+
import { Plugin } from "@elizaos/core";
106+
import { dominosPlugin } from "@elizaos/plugin-dominos";
107107

108108
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
109109
const __dirname = path.dirname(__filename); // get the name of the directory
@@ -121,6 +121,10 @@ const logFetch = async (url: string, options: any) => {
121121
return fetch(url, options);
122122
};
123123

124+
function isAllStrings(arr: unknown[]): boolean {
125+
return Array.isArray(arr) && arr.every((item) => typeof item === "string");
126+
}
127+
124128
export function parseArguments(): {
125129
character?: string;
126130
characters?: string;
@@ -153,14 +157,29 @@ function tryLoadFile(filePath: string): string | null {
153157
function mergeCharacters(base: Character, child: Character): Character {
154158
const mergeObjects = (baseObj: any, childObj: any) => {
155159
const result: any = {};
156-
const keys = new Set([...Object.keys(baseObj || {}), ...Object.keys(childObj || {})]);
157-
keys.forEach(key => {
158-
if (typeof baseObj[key] === 'object' && typeof childObj[key] === 'object' && !Array.isArray(baseObj[key]) && !Array.isArray(childObj[key])) {
160+
const keys = new Set([
161+
...Object.keys(baseObj || {}),
162+
...Object.keys(childObj || {}),
163+
]);
164+
keys.forEach((key) => {
165+
if (
166+
typeof baseObj[key] === "object" &&
167+
typeof childObj[key] === "object" &&
168+
!Array.isArray(baseObj[key]) &&
169+
!Array.isArray(childObj[key])
170+
) {
159171
result[key] = mergeObjects(baseObj[key], childObj[key]);
160-
} else if (Array.isArray(baseObj[key]) || Array.isArray(childObj[key])) {
161-
result[key] = [...(baseObj[key] || []), ...(childObj[key] || [])];
172+
} else if (
173+
Array.isArray(baseObj[key]) ||
174+
Array.isArray(childObj[key])
175+
) {
176+
result[key] = [
177+
...(baseObj[key] || []),
178+
...(childObj[key] || []),
179+
];
162180
} else {
163-
result[key] = childObj[key] !== undefined ? childObj[key] : baseObj[key];
181+
result[key] =
182+
childObj[key] !== undefined ? childObj[key] : baseObj[key];
164183
}
165184
});
166185
return result;
@@ -175,32 +194,36 @@ async function loadCharacter(filePath: string): Promise<Character> {
175194
let character = JSON.parse(content);
176195
validateCharacterConfig(character);
177196

178-
// .id isn't really valid
179-
const characterId = character.id || character.name;
180-
const characterPrefix = `CHARACTER.${characterId.toUpperCase().replace(/ /g, "_")}.`;
181-
const characterSettings = Object.entries(process.env)
182-
.filter(([key]) => key.startsWith(characterPrefix))
183-
.reduce((settings, [key, value]) => {
184-
const settingKey = key.slice(characterPrefix.length);
185-
return { ...settings, [settingKey]: value };
186-
}, {});
187-
if (Object.keys(characterSettings).length > 0) {
188-
character.settings = character.settings || {};
189-
character.settings.secrets = {
190-
...characterSettings,
191-
...character.settings.secrets,
192-
};
193-
}
194-
// Handle plugins
195-
character.plugins = await handlePluginImporting(
196-
character.plugins
197-
);
197+
// .id isn't really valid
198+
const characterId = character.id || character.name;
199+
const characterPrefix = `CHARACTER.${characterId.toUpperCase().replace(/ /g, "_")}.`;
200+
const characterSettings = Object.entries(process.env)
201+
.filter(([key]) => key.startsWith(characterPrefix))
202+
.reduce((settings, [key, value]) => {
203+
const settingKey = key.slice(characterPrefix.length);
204+
return { ...settings, [settingKey]: value };
205+
}, {});
206+
if (Object.keys(characterSettings).length > 0) {
207+
character.settings = character.settings || {};
208+
character.settings.secrets = {
209+
...characterSettings,
210+
...character.settings.secrets,
211+
};
212+
}
213+
// Handle plugins
214+
character.plugins = await handlePluginImporting(character.plugins);
198215
if (character.extends) {
199-
elizaLogger.info(`Merging ${character.name} character with parent characters`);
216+
elizaLogger.info(
217+
`Merging ${character.name} character with parent characters`
218+
);
200219
for (const extendPath of character.extends) {
201-
const baseCharacter = await loadCharacter(path.resolve(path.dirname(filePath), extendPath));
220+
const baseCharacter = await loadCharacter(
221+
path.resolve(path.dirname(filePath), extendPath)
222+
);
202223
character = mergeCharacters(baseCharacter, character);
203-
elizaLogger.info(`Merged ${character.name} with ${baseCharacter.name}`);
224+
elizaLogger.info(
225+
`Merged ${character.name} with ${baseCharacter.name}`
226+
);
204227
}
205228
}
206229
return character;
@@ -289,7 +312,7 @@ export async function loadCharacters(
289312
...character.settings.secrets,
290313
};
291314
}
292-
315+
293316
function isPlugin(value: any): value is Plugin {
294317
return (
295318
typeof value === "object" &&
@@ -307,7 +330,7 @@ export async function loadCharacters(
307330
(value.clients === undefined ||
308331
Array.isArray(value.clients))
309332
);
310-
}
333+
}
311334

312335
// Handle plugins
313336
if (isAllStrings(character.plugins)) {
@@ -555,7 +578,9 @@ function initializeDatabase(dataDir: string) {
555578
// Test the connection
556579
db.init()
557580
.then(() => {
558-
elizaLogger.success("Successfully connected to Supabase database");
581+
elizaLogger.success(
582+
"Successfully connected to Supabase database"
583+
);
559584
})
560585
.catch((error) => {
561586
elizaLogger.error("Failed to connect to Supabase:", error);
@@ -572,7 +597,9 @@ function initializeDatabase(dataDir: string) {
572597
// Test the connection
573598
db.init()
574599
.then(() => {
575-
elizaLogger.success("Successfully connected to PostgreSQL database");
600+
elizaLogger.success(
601+
"Successfully connected to PostgreSQL database"
602+
);
576603
})
577604
.catch((error) => {
578605
elizaLogger.error("Failed to connect to PostgreSQL:", error);
@@ -587,14 +614,17 @@ function initializeDatabase(dataDir: string) {
587614
});
588615
return db;
589616
} else {
590-
const filePath = process.env.SQLITE_FILE ?? path.resolve(dataDir, "db.sqlite");
617+
const filePath =
618+
process.env.SQLITE_FILE ?? path.resolve(dataDir, "db.sqlite");
591619
elizaLogger.info(`Initializing SQLite database at ${filePath}...`);
592620
const db = new SqliteDatabaseAdapter(new Database(filePath));
593621

594622
// Test the connection
595623
db.init()
596624
.then(() => {
597-
elizaLogger.success("Successfully connected to SQLite database");
625+
elizaLogger.success(
626+
"Successfully connected to SQLite database"
627+
);
598628
})
599629
.catch((error) => {
600630
elizaLogger.error("Failed to connect to SQLite:", error);
@@ -772,7 +802,8 @@ export async function createAgent(
772802
if (
773803
process.env.PRIMUS_APP_ID &&
774804
process.env.PRIMUS_APP_SECRET &&
775-
process.env.VERIFIABLE_INFERENCE_ENABLED === "true"){
805+
process.env.VERIFIABLE_INFERENCE_ENABLED === "true"
806+
) {
776807
verifiableInferenceAdapter = new PrimusAdapter({
777808
appId: process.env.PRIMUS_APP_ID,
778809
appSecret: process.env.PRIMUS_APP_SECRET,
@@ -934,9 +965,7 @@ export async function createAgent(
934965
getSecret(character, "AKASH_WALLET_ADDRESS")
935966
? akashPlugin
936967
: null,
937-
getSecret(character, "QUAI_PRIVATE_KEY")
938-
? quaiPlugin
939-
: null,
968+
getSecret(character, "QUAI_PRIVATE_KEY") ? quaiPlugin : null,
940969
].filter(Boolean),
941970
providers: [],
942971
actions: [],

packages/core/src/runtime.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ import {
5353
type Memory,
5454
} from "./types.ts";
5555
import { stringToUuid } from "./uuid.ts";
56-
import _ from "lodash";
57-
import { readFile } from 'fs/promises';
58-
import { join } from 'path';
59-
6056

6157
/**
6258
* Represents the runtime environment for an agent, handling message processing,
@@ -375,12 +371,19 @@ export class AgentRuntime implements IAgentRuntime {
375371

376372
this.token = opts.token;
377373

378-
const allPlugins = [
374+
const combinedPlugins = [
379375
...(opts.character?.plugins ?? []),
380376
...(opts.plugins ?? []),
381377
];
382378

383-
this.plugins = _.uniqBy(allPlugins, "name");
379+
const seen = new Set();
380+
this.plugins = combinedPlugins.filter((plugin) => {
381+
if (seen.has(plugin.name)) {
382+
return false;
383+
}
384+
seen.add(plugin.name);
385+
return true;
386+
});
384387

385388
this.plugins.forEach((plugin) => {
386389
plugin.actions?.forEach((action) => {

0 commit comments

Comments
 (0)