Skip to content

Commit

Permalink
Merge pull request elizaOS#2095 from treppers/plugin_import_fixes
Browse files Browse the repository at this point in the history
fix: Fix plugin loading from a character.json file
  • Loading branch information
odilitime authored Jan 10, 2025
2 parents 88dfc91 + 38d986a commit e0396cf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
1 change: 1 addition & 0 deletions agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"@elizaos/plugin-3d-generation": "workspace:*",
"@elizaos/plugin-fuel": "workspace:*",
"@elizaos/plugin-avalanche": "workspace:*",
"@elizaos/plugin-video-generation": "workspace:*",
"@elizaos/plugin-web-search": "workspace:*",
"@elizaos/plugin-letzai": "workspace:*",
"@elizaos/plugin-thirdweb": "workspace:*",
Expand Down
52 changes: 37 additions & 15 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ function tryLoadFile(filePath: string): string | null {
}
}

function isAllStrings(arr: unknown[]): boolean {
return Array.isArray(arr) && arr.every((item) => typeof item === "string");
}

export async function loadCharacters(
charactersArg: string
): Promise<Character[]> {
Expand Down Expand Up @@ -231,16 +227,9 @@ export async function loadCharacters(
}

// Handle plugins
if (isAllStrings(character.plugins)) {
elizaLogger.info("Plugins are: ", character.plugins);
const importedPlugins = await Promise.all(
character.plugins.map(async (plugin) => {
const importedPlugin = await import(plugin);
return importedPlugin.default;
})
);
character.plugins = importedPlugins;
}
character.plugins = await handlePluginImporting(
character.plugins
);

loadedCharacters.push(character);
elizaLogger.info(
Expand All @@ -263,6 +252,36 @@ export async function loadCharacters(
return loadedCharacters;
}

async function handlePluginImporting(plugins: string[]) {
if (plugins.length > 0) {
elizaLogger.info("Plugins are: ", plugins);
const importedPlugins = await Promise.all(
plugins.map(async (plugin) => {
try {
const importedPlugin = await import(plugin);
const functionName =
plugin
.replace("@elizaos/plugin-", "")
.replace(/-./g, (x) => x[1].toUpperCase()) +
"Plugin"; // Assumes plugin function is camelCased with Plugin suffix
return (
importedPlugin.default || importedPlugin[functionName]
);
} catch (importError) {
elizaLogger.error(
`Failed to import plugin: ${plugin}`,
importError
);
return []; // Return null for failed imports
}
})
);
return importedPlugins;
} else {
return [];
}
}

export function getTokenForProvider(
provider: ModelProviderName,
character: Character
Expand Down Expand Up @@ -921,7 +940,10 @@ const startAgents = async () => {
}

// upload some agent functionality into directClient
directClient.startAgent = async (character: Character) => {
directClient.startAgent = async (character) => {
// Handle plugins
character.plugins = await handlePluginImporting(character.plugins);

// wrap it so we don't have to inject directClient later
return startAgent(character, directClient);
};
Expand Down

0 comments on commit e0396cf

Please sign in to comment.