Skip to content

Commit

Permalink
Merge pull request #2267 from jseparovic/direct-client_agent-crud
Browse files Browse the repository at this point in the history
Improvement: Direct Client API - Add Delete Agent functionality
  • Loading branch information
wtfsayo authored Jan 14, 2025
2 parents d3305c3 + f47f7e4 commit a3bbaf6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
14 changes: 14 additions & 0 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
settings,
stringToUuid,
validateCharacterConfig,
parseBooleanFromText,
} from "@elizaos/core";
import { zgPlugin } from "@elizaos/plugin-0g";

Expand Down Expand Up @@ -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);
});
}
33 changes: 30 additions & 3 deletions packages/client-direct/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -330,3 +356,4 @@ export function createApiRouter(

return router;
}

0 comments on commit a3bbaf6

Please sign in to comment.