create-eliza
Version:
Initialize an Eliza project
150 lines (146 loc) • 5.43 kB
JavaScript
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import {
character
} from "./chunk-34HYEHJA.js";
import {
logger,
v4_default
} from "./chunk-UIEY2LL5.js";
// src/project.ts
import * as fs from "node:fs";
import path from "node:path";
function isPlugin(module) {
if (module && typeof module === "object" && typeof module.name === "string" && typeof module.description === "string") {
return true;
}
if (module && typeof module === "object" && module.default && typeof module.default === "object" && typeof module.default.name === "string" && typeof module.default.description === "string") {
return true;
}
for (const key in module) {
if (key !== "default" && module[key] && typeof module[key] === "object" && typeof module[key].name === "string" && typeof module[key].description === "string") {
return true;
}
}
return false;
}
function extractPlugin(module) {
if (module && typeof module === "object" && typeof module.name === "string" && typeof module.description === "string") {
return module;
}
if (module && typeof module === "object" && module.default && typeof module.default === "object" && typeof module.default.name === "string" && typeof module.default.description === "string") {
return module.default;
}
for (const key in module) {
if (key !== "default" && module[key] && typeof module[key] === "object" && typeof module[key].name === "string" && typeof module[key].description === "string") {
return module[key];
}
}
throw new Error("Could not extract plugin from module");
}
async function loadProject(dir) {
try {
const entryPoints = [
path.join(dir, "src/index.ts"),
path.join(dir, "src/index.js"),
path.join(dir, "dist/index.js"),
path.join(dir, "index.ts"),
path.join(dir, "index.js")
];
let projectModule = null;
for (const entryPoint of entryPoints) {
if (fs.existsSync(entryPoint)) {
try {
const importPath = path.resolve(entryPoint);
projectModule = await import(importPath);
logger.info(`Loaded project from ${entryPoint}`);
const exportKeys = Object.keys(projectModule);
logger.debug(`Module exports: ${exportKeys.join(", ")}`);
if (exportKeys.includes("default")) {
logger.debug(`Default export type: ${typeof projectModule.default}`);
if (typeof projectModule.default === "object" && projectModule.default !== null) {
logger.debug(`Default export keys: ${Object.keys(projectModule.default).join(", ")}`);
}
}
break;
} catch (error) {
logger.warn(`Failed to import project from ${entryPoint}:`, error);
}
}
}
if (!projectModule) {
throw new Error("Could not find project entry point");
}
const moduleIsPlugin = isPlugin(projectModule);
logger.debug(`Is this a plugin? ${moduleIsPlugin}`);
if (moduleIsPlugin) {
logger.info("Detected plugin module instead of project");
try {
const plugin = extractPlugin(projectModule);
logger.debug(`Found plugin: ${plugin.name} - ${plugin.description}`);
logger.debug(`Plugin has the following properties: ${Object.keys(plugin).join(", ")}`);
const completePlugin = {
name: plugin.name || "unknown-plugin",
description: plugin.description || "No description",
init: plugin.init || (async (config, runtime) => {
logger.info(`Dummy init for plugin: ${plugin.name}`);
}),
// Copy all other properties from the original plugin
...plugin
};
const testCharacter = {
...character,
id: v4_default(),
name: "Eliza (Test Mode)",
system: `${character.system} Testing the plugin: ${completePlugin.name}.`
};
logger.info(`Using Eliza character as test agent for plugin: ${completePlugin.name}`);
const testAgent = {
character: testCharacter,
plugins: [completePlugin],
// Only include the plugin being tested
// Add an init function that just registers the plugin
init: async (runtime) => {
logger.info(`Initializing Eliza test agent for plugin: ${completePlugin.name}`);
}
};
return {
agents: [testAgent],
dir,
isPlugin: true,
pluginModule: completePlugin
};
} catch (error) {
logger.error("Error extracting plugin from module:", error);
throw error;
}
}
const agents = [];
for (const [key, value] of Object.entries(projectModule)) {
if (key === "default" && value && typeof value === "object") {
if (Array.isArray(value.agents)) {
agents.push(...value.agents);
} else if (value.character && value.init) {
agents.push(value);
}
} else if (value && typeof value === "object" && value.character && value.init) {
agents.push(value);
}
}
if (agents.length === 0) {
throw new Error("No agents found in project");
}
const project = {
agents,
dir
};
return project;
} catch (error) {
logger.error("Error loading project:", error);
throw error;
}
}
export {
loadProject
};
//# sourceMappingURL=chunk-BSMOZJDR.js.map