diff --git a/agent/src/index.ts b/agent/src/index.ts index 9130c0ac1b..df62d2ef02 100644 --- a/agent/src/index.ts +++ b/agent/src/index.ts @@ -32,6 +32,7 @@ import { settings, stringToUuid, validateCharacterConfig, + parseBooleanFromText, } from "@elizaos/core"; import { zgPlugin } from "@elizaos/plugin-0g"; @@ -1113,3 +1114,16 @@ startAgents().catch((error) => { elizaLogger.error("Unhandled error in startAgents:", error); process.exit(1); }); + +// Prevent unhandled exceptions from crashing the process if desired +if (process.env.PREVENT_UNHANDLED_EXIT && parseBooleanFromText(process.env.PREVENT_UNHANDLED_EXIT)) { + // Handle uncaught exceptions to prevent the process from crashing + process.on('uncaughtException', function(err) { + console.error("uncaughtException", err); + }); + + // Handle unhandled rejections to prevent the process from crashing + process.on('unhandledRejection', function(err) { + console.error("unhandledRejection", err); + }); +} diff --git a/packages/client-direct/src/api.ts b/packages/client-direct/src/api.ts index c19ac5279c..0cecbb4b69 100644 --- a/packages/client-direct/src/api.ts +++ b/packages/client-direct/src/api.ts @@ -103,6 +103,24 @@ export function createApiRouter( }); }); + router.delete("/agents/:agentId", async (req, res) => { + const { agentId } = validateUUIDParams(req.params, res) ?? { + agentId: null, + }; + if (!agentId) return; + + let agent: AgentRuntime = agents.get(agentId); + + if (agent) { + agent.stop(); + directClient.unregisterAgent(agent); + res.status(204).send(); + } + else { + res.status(404).json({ error: "Agent not found" }); + } + }); + router.post("/agents/:agentId/set", async (req, res) => { const { agentId } = validateUUIDParams(req.params, res) ?? { agentId: null, @@ -133,9 +151,17 @@ export function createApiRouter( } // start it up (and register it) - agent = await directClient.startAgent(character); - elizaLogger.log(`${character.name} started`); - + try { + await directClient.startAgent(character); + elizaLogger.log(`${character.name} started`); + } catch (e) { + elizaLogger.error(`Error starting agent: ${e}`); + res.status(500).json({ + success: false, + message: e.message, + }); + return; + } res.json({ id: character.id, character: character, @@ -330,3 +356,4 @@ export function createApiRouter( return router; } +