Skip to content

Commit 286d0c7

Browse files
committed
fix up cli stuff
1 parent 9990153 commit 286d0c7

File tree

2 files changed

+99
-15
lines changed

2 files changed

+99
-15
lines changed

packages/cli/src/commands/start.ts

+99-14
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import {
1111
type Plugin,
1212
logger,
1313
settings,
14-
stringToUuid
14+
stringToUuid,
15+
ModelTypes
1516
} from "@elizaos/core";
16-
import chalk from "chalk";
1717
import { Command } from "commander";
1818
import * as dotenv from "dotenv";
1919
import { AgentServer } from "../server/index";
@@ -114,6 +114,7 @@ async function startAgent(
114114
options: {
115115
dataDir?: string;
116116
postgresUrl?: string;
117+
isPluginTestMode?: boolean;
117118
} = {},
118119
): Promise<IAgentRuntime> {
119120
character.id ??= stringToUuid(character.name);
@@ -129,6 +130,71 @@ async function startAgent(
129130
// start services/plugins/process knowledge
130131
await runtime.initialize();
131132

133+
// Check if TEXT_EMBEDDING model is registered, if not, try to load one
134+
// Skip auto-loading embedding models if we're in plugin test mode
135+
const embeddingModel = runtime.getModel(ModelTypes.TEXT_EMBEDDING);
136+
if (!embeddingModel && !options.isPluginTestMode) {
137+
logger.info("No TEXT_EMBEDDING model registered. Attempting to load one automatically...");
138+
139+
// First check if OpenAI API key exists
140+
if (process.env.OPENAI_API_KEY) {
141+
logger.info("Found OpenAI API key. Loading OpenAI plugin for embeddings...");
142+
143+
try {
144+
// Use Node's require mechanism to dynamically load the plugin
145+
const pluginName = "@elizaos/plugin-openai";
146+
147+
try {
148+
// Using require for dynamic imports in Node.js is safer than eval
149+
// This is a CommonJS approach that works in Node.js
150+
// @ts-ignore - Ignoring TypeScript's static checking for dynamic requires
151+
const pluginModule = require(pluginName);
152+
const plugin = pluginModule.default || pluginModule.openaiPlugin;
153+
154+
if (plugin) {
155+
await runtime.registerPlugin(plugin);
156+
logger.success("Successfully loaded OpenAI plugin for embeddings");
157+
} else {
158+
logger.warn(`Could not find a valid plugin export in ${pluginName}`);
159+
}
160+
} catch (error) {
161+
logger.warn(`Failed to import OpenAI plugin: ${error}`);
162+
logger.warn(`You may need to install it with: npm install ${pluginName}`);
163+
}
164+
} catch (error) {
165+
logger.warn(`Error loading OpenAI plugin: ${error}`);
166+
}
167+
} else {
168+
logger.info("No OpenAI API key found. Loading local-ai plugin for embeddings...");
169+
170+
try {
171+
// Use Node's require mechanism to dynamically load the plugin
172+
const pluginName = "@elizaos/plugin-local-ai";
173+
174+
try {
175+
// Using require for dynamic imports in Node.js is safer than eval
176+
// @ts-ignore - Ignoring TypeScript's static checking for dynamic requires
177+
const pluginModule = require(pluginName);
178+
const plugin = pluginModule.default || pluginModule.localAIPlugin;
179+
180+
if (plugin) {
181+
await runtime.registerPlugin(plugin);
182+
logger.success("Successfully loaded local-ai plugin for embeddings");
183+
} else {
184+
logger.warn(`Could not find a valid plugin export in ${pluginName}`);
185+
}
186+
} catch (error) {
187+
logger.warn(`Failed to import local-ai plugin: ${error}`);
188+
logger.warn(`You may need to install it with: npm install ${pluginName}`);
189+
}
190+
} catch (error) {
191+
logger.warn(`Error loading local-ai plugin: ${error}`);
192+
}
193+
}
194+
} else if (!embeddingModel && options.isPluginTestMode) {
195+
logger.info("No TEXT_EMBEDDING model registered, but running in plugin test mode - skipping auto-loading");
196+
}
197+
132198
// add to container
133199
server.registerAgent(runtime);
134200

@@ -330,9 +396,12 @@ const startAgents = async (options: {
330396
try {
331397
// Check if we're in a project with a package.json
332398
const packageJsonPath = path.join(process.cwd(), "package.json");
399+
logger.debug(`Checking for package.json at: ${packageJsonPath}`);
400+
333401
if (fs.existsSync(packageJsonPath)) {
334402
// Read and parse package.json to check if it's a project or plugin
335403
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
404+
logger.debug(`Found package.json with name: ${packageJson.name || 'unnamed'}`);
336405

337406
// Check if this is a plugin (package.json contains 'eliza' section with type='plugin')
338407
if (packageJson.eliza?.type && packageJson.eliza.type === "plugin") {
@@ -423,6 +492,8 @@ const startAgents = async (options: {
423492
}
424493

425494
// Log what was found
495+
logger.debug(`Classification results - isProject: ${isProject}, isPlugin: ${isPlugin}`);
496+
426497
if (isProject) {
427498
logger.info("Found project configuration");
428499
if (projectModule?.default) {
@@ -446,7 +517,9 @@ const startAgents = async (options: {
446517
} else if (isPlugin) {
447518
logger.info(`Found plugin: ${pluginModule?.name || "unnamed"}`);
448519
} else {
449-
logger.info("No project or plugin found, will use custom character");
520+
// Change the log message to be clearer about what we're doing
521+
logger.info("Running in standalone mode - using default Eliza character");
522+
logger.debug("Will load the default Eliza character from ../characters/eliza");
450523
}
451524

452525
// Start agents based on project, plugin, or custom configuration
@@ -521,20 +594,32 @@ const startAgents = async (options: {
521594
}
522595
}
523596

524-
// Load the custom character and add the plugin to it
597+
// Load the default character with all its default plugins, then add the test plugin
525598
logger.info(
526-
`Starting custom character with plugin: ${pluginModule.name || "unnamed plugin"}`,
599+
`Starting default Eliza character with plugin: ${pluginModule.name || "unnamed plugin"}`,
527600
);
528601

529-
// Create a proper array of plugins, including the explicitly loaded one
602+
// Import the default character with all its plugins
603+
const { character: defaultElizaCharacter } = await import("../characters/eliza");
604+
605+
// Create an array of plugins, including the explicitly loaded one
606+
// We're using our test plugin plus all the plugins from the default character
530607
const pluginsToLoad = [pluginModule];
531-
532-
// Start the agent with our custom character and plugins
533-
await startAgent(customCharacter, server, undefined, pluginsToLoad);
608+
609+
logger.debug(`Using default character with plugins: ${defaultElizaCharacter.plugins.join(", ")}`);
610+
logger.info("Plugin test mode: Using default character's plugins plus the plugin being tested");
611+
612+
// Start the agent with the default character and our test plugin
613+
// We're in plugin test mode, so we should skip auto-loading embedding models
614+
await startAgent(defaultElizaCharacter, server, undefined, pluginsToLoad, {
615+
isPluginTestMode: true
616+
});
534617
logger.info("Character started with plugin successfully");
535618
} else {
536-
logger.info("Starting with custom character");
537-
await startAgent(customCharacter, server);
619+
// When not in a project or plugin, load the default character with all plugins
620+
const { character: defaultElizaCharacter } = await import("../characters/eliza");
621+
logger.info("Using default Eliza character with all plugins");
622+
await startAgent(defaultElizaCharacter, server);
538623
}
539624

540625
// Rest of the function remains the same...
@@ -555,7 +640,7 @@ const startAgents = async (options: {
555640

556641
// If not found, fall back to the old relative path for development
557642
if (!fs.existsSync(clientPath)) {
558-
clientPath = path.join(__dirname, "../../../../..", "packages/client/dist");
643+
clientPath = path.join(__dirname, "../../../..", "client/dist");
559644
}
560645

561646
if (fs.existsSync(clientPath)) {
@@ -565,8 +650,8 @@ const startAgents = async (options: {
565650
} else {
566651
const clientSrcPath = path.join(
567652
__dirname,
568-
"../../../..",
569-
"packages/client",
653+
"../../..",
654+
"client",
570655
);
571656
if (fs.existsSync(clientSrcPath)) {
572657
logger.info(

packages/project-starter/src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,6 @@ export const character: Character = {
343343
const initCharacter = ({ runtime }: { runtime: IAgentRuntime }) => {
344344
console.log("Initializing character");
345345
console.log("Name: ", character.name);
346-
runtime.registerPlugin(starterPlugin);
347346
};
348347

349348
export const projectAgent: ProjectAgent = {

0 commit comments

Comments
 (0)