create-eliza
Version:
Initialize an Eliza project
154 lines (150 loc) • 5.83 kB
JavaScript
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import {
AgentRuntime,
logger,
v4_default
} from "./chunk-MQCRBT4U.js";
// src/project.ts
import { resolve } from "node:path";
import * as fs from "node:fs";
async function loadProject(path) {
try {
const projectPath = resolve(path);
logger.info(`Loading project from ${projectPath}`);
let projectConfig;
const packageJsonPath = resolve(projectPath, "package.json");
logger.info(`Looking for package.json at ${packageJsonPath}`);
if (fs.existsSync(packageJsonPath)) {
try {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
logger.info("Found package.json:", packageJson);
const isProject = packageJson.eliza?.type === "project" || packageJson.description?.toLowerCase().includes("project");
logger.info(`Is this a project? ${isProject}`);
if (isProject) {
const srcIndexPaths = [
resolve(projectPath, "src", "index.ts"),
resolve(projectPath, "src", "index.js"),
resolve(projectPath, "dist", "index.js")
];
logger.info("Looking for project entry points:", srcIndexPaths);
for (const indexPath of srcIndexPaths) {
if (fs.existsSync(indexPath)) {
try {
logger.info(`Trying to load project from ${indexPath}`);
const importedModule = await import(`file://${indexPath}`);
logger.info("Imported module:", importedModule);
if (importedModule.default?.agents || importedModule.project?.agents) {
projectConfig = importedModule.default || importedModule.project;
logger.info("Found project configuration:", projectConfig);
break;
}
logger.warn(`No project configuration found in ${indexPath}`);
} catch (importError) {
logger.error(`Error importing ${indexPath}:`, importError);
if (importError instanceof Error) {
logger.error("Error details:", importError.message);
logger.error("Stack trace:", importError.stack);
}
}
continue;
}
logger.info(`Entry point ${indexPath} does not exist`);
}
}
} catch (parseError) {
logger.error("Error parsing package.json:", parseError);
if (parseError instanceof Error) {
logger.error("Error details:", parseError.message);
logger.error("Stack trace:", parseError.stack);
}
}
} else {
logger.warn(`No package.json found at ${packageJsonPath}`);
}
if (!projectConfig) {
logger.info("No project configuration found in package.json, looking for project files...");
const projectFiles = ["project.json", "eliza.json", "agents.json"];
for (const file of projectFiles) {
const filePath = resolve(projectPath, file);
logger.info(`Looking for ${filePath}`);
if (fs.existsSync(filePath)) {
try {
const fileContent = fs.readFileSync(filePath, "utf-8");
const projectData = JSON.parse(fileContent);
logger.info(`Found project data in ${file}:`, projectData);
if (projectData.agents || projectData.agent) {
projectConfig = projectData;
logger.info("Using project configuration from file");
break;
}
} catch (error) {
logger.error(`Error reading project file ${file}:`, error);
if (error instanceof Error) {
logger.error("Error details:", error.message);
logger.error("Stack trace:", error.stack);
}
}
} else {
logger.info(`Project file ${file} not found`);
}
}
}
if (!projectConfig) {
throw new Error("No project configuration found");
}
const agents = projectConfig.agents;
if (!agents || agents.length === 0) {
logger.error("No agents defined in project configuration");
throw new Error("No agents defined in project configuration");
}
logger.info(`Found ${agents.length} agents in project`);
const runtimes = [];
for (const agent of agents) {
logger.info(`Creating runtime for agent: ${agent.character.name}`);
try {
const runtime = new AgentRuntime({
agentId: v4_default(),
character: agent.character,
plugins: agent.plugins || []
});
if (agent.init) {
try {
await agent.init(runtime);
} catch (initError) {
logger.error(`Error initializing agent ${agent.character.name}:`, initError);
if (initError instanceof Error) {
logger.error("Error details:", initError.message);
logger.error("Stack trace:", initError.stack);
}
throw initError;
}
}
runtimes.push(runtime);
} catch (runtimeError) {
logger.error(`Error creating runtime for agent ${agent.character.name}:`, runtimeError);
if (runtimeError instanceof Error) {
logger.error("Error details:", runtimeError.message);
logger.error("Stack trace:", runtimeError.stack);
}
throw runtimeError;
}
}
return {
runtimes,
path: projectPath,
agents
};
} catch (error) {
logger.error("Failed to load project:", error);
if (error instanceof Error) {
logger.error("Error details:", error.message);
logger.error("Stack trace:", error.stack);
}
throw error;
}
}
export {
loadProject
};
//# sourceMappingURL=chunk-BIRCN3GQ.js.map