Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: character file plugins import error #2025

Closed
wants to merge 13 commits into from
64 changes: 61 additions & 3 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import net from "net";
import path from "path";
import { fileURLToPath } from "url";
import yargs from "yargs";
import { Plugin } from "@ai16z/eliza/src/types";

const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
const __dirname = path.dirname(__filename); // get the name of the directory
Expand Down Expand Up @@ -227,11 +228,68 @@ export async function loadCharacters(
...character.settings.secrets,
};
}

function isPlugin(value: any): value is Plugin {
return (
typeof value === "object" &&
value !== null &&
typeof value.name === "string" &&
typeof value.description === "string" &&
(value.actions === undefined ||
Array.isArray(value.actions)) &&
(value.providers === undefined ||
Array.isArray(value.providers)) &&
(value.evaluators === undefined ||
Array.isArray(value.evaluators)) &&
(value.services === undefined ||
Array.isArray(value.services)) &&
(value.clients === undefined ||
Array.isArray(value.clients))
);
}

// Handle plugins
character.plugins = await handlePluginImporting(
character.plugins
);
if (isAllStrings(character.plugins)) {
elizaLogger.info("Plugins are: ", character.plugins);

const importedPlugins = await Promise.all(
character.plugins.map(async (plugin) => {
try {
// Dynamically import the plugin
const importedPlugin = await import(plugin);

// Check if there's a default export
if (importedPlugin.default) {
return importedPlugin.default;
}

// Check other exports for potential plugins
const possiblePlugins = [];
for (const [key, value] of Object.entries(
importedPlugin
)) {
// Check if the export matches the plugin type
if (isPlugin(value)) {
possiblePlugins.push(value);
}
}

return possiblePlugins.length > 0
? possiblePlugins
: null;
} catch (error) {
elizaLogger.error(
`Failed to import plugin "${plugin}":`,
error
);
return null; // Return null for failed imports
}
})
);

// Flatten and filter out null or empty plugin arrays
character.plugins = importedPlugins.flat().filter(Boolean);
}

loadedCharacters.push(character);
elizaLogger.info(
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ import {
IVerifiableInferenceAdapter,
} from "./types.ts";
import { stringToUuid } from "./uuid.ts";
import _ from "lodash";
import { readFile } from 'fs/promises';
import { join } from 'path';


/**
* Represents the runtime environment for an agent, handling message processing,
* action registration, and interaction with external services like OpenAI and Supabase.
Expand Down Expand Up @@ -371,11 +373,13 @@ export class AgentRuntime implements IAgentRuntime {

this.token = opts.token;

this.plugins = [
const allPlugins = [
...(opts.character?.plugins ?? []),
...(opts.plugins ?? []),
];

this.plugins = _.uniqBy(allPlugins, "name");

this.plugins.forEach((plugin) => {
plugin.actions?.forEach((action) => {
this.registerAction(action);
Expand Down
Loading