|
| 1 | +import path from "node:path"; |
| 2 | +import fs from "node:fs"; |
1 | 3 | import logger from "./logger";
|
2 | 4 |
|
3 | 5 | const registrations = new Map<string, any>();
|
@@ -25,14 +27,34 @@ export async function handlePluginImporting(plugins: string[]) {
|
25 | 27 | logger.debug("Imported are: ", plugins);
|
26 | 28 | const importedPlugins = await Promise.all(
|
27 | 29 | plugins.map(async (plugin) => {
|
| 30 | + const functionName = `${plugin |
| 31 | + .replace("@elizaos/plugin-", "") |
| 32 | + .replace("@elizaos-plugins/", "") |
| 33 | + .replace(/-./g, (x) => x[1].toUpperCase())}Plugin`; // Assumes plugin function is camelCased with Plugin suffix |
| 34 | + |
28 | 35 | try {
|
29 | 36 | const importedPlugin = await import(plugin);
|
30 |
| - const functionName = `${plugin |
31 |
| - .replace("@elizaos/plugin-", "") |
32 |
| - .replace("@elizaos-plugins/", "") |
33 |
| - .replace(/-./g, (x) => x[1].toUpperCase())}Plugin`; // Assumes plugin function is camelCased with Plugin suffix |
34 | 37 | return importedPlugin.default || importedPlugin[functionName];
|
35 | 38 | } catch (importError) {
|
| 39 | + |
| 40 | + // check if there is a node_modules folder in the current directory |
| 41 | + const nodeModulesPath = path.join(process.cwd(), "node_modules"); |
| 42 | + if (fs.existsSync(nodeModulesPath)) { |
| 43 | + // check if the plugin is in the node_modules folder |
| 44 | + const pluginPath = path.join(nodeModulesPath, plugin); |
| 45 | + if (fs.existsSync(pluginPath)) { |
| 46 | + try { |
| 47 | + |
| 48 | + // import the plugin from the node_modules folder |
| 49 | + const importedPlugin = await import(pluginPath); |
| 50 | + return importedPlugin.default || importedPlugin[functionName]; |
| 51 | + } catch (importError) { |
| 52 | + logger.error(`Failed to import plugin: ${plugin}`, importError); |
| 53 | + return []; // Return null for failed imports |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
36 | 58 | logger.error(`Failed to import plugin: ${plugin}`, importError);
|
37 | 59 | return []; // Return null for failed imports
|
38 | 60 | }
|
|
0 commit comments