Skip to content

Commit

Permalink
Merge pull request #1581 from treppers/memories_endpoint
Browse files Browse the repository at this point in the history
add an client-direct endpoint to get memories by agentid and roomid
  • Loading branch information
monilpat authored Dec 31, 2024
2 parents 50f8b5a + 0134fae commit 20090bf
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions packages/client-direct/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {

import { REST, Routes } from "discord.js";
import { DirectClient } from ".";
import { stringToUuid } from "@elizaos/core";

export function createApiRouter(
agents: Map<string, AgentRuntime>,
Expand Down Expand Up @@ -121,5 +122,66 @@ export function createApiRouter(
}
});

router.get("/agents/:agentId/:roomId/memories", async (req, res) => {
const agentId = req.params.agentId;
const roomId = stringToUuid(req.params.roomId);
let runtime = agents.get(agentId);

// if runtime is null, look for runtime with the same name
if (!runtime) {
runtime = Array.from(agents.values()).find(
(a) => a.character.name.toLowerCase() === agentId.toLowerCase()
);
}

if (!runtime) {
res.status(404).send("Agent not found");
return;
}

try {
const memories = await runtime.messageManager.getMemories({
roomId,
});
const response = {
agentId,
roomId,
memories: memories.map((memory) => ({
id: memory.id,
userId: memory.userId,
agentId: memory.agentId,
createdAt: memory.createdAt,
content: {
text: memory.content.text,
action: memory.content.action,
source: memory.content.source,
url: memory.content.url,
inReplyTo: memory.content.inReplyTo,
attachments: memory.content.attachments?.map(
(attachment) => ({
id: attachment.id,
url: attachment.url,
title: attachment.title,
source: attachment.source,
description: attachment.description,
text: attachment.text,
contentType: attachment.contentType,
})
),
},
embedding: memory.embedding,
roomId: memory.roomId,
unique: memory.unique,
similarity: memory.similarity,
})),
};

res.json(response);
} catch (error) {
console.error("Error fetching memories:", error);
res.status(500).json({ error: "Failed to fetch memories" });
}
});

return router;
}

0 comments on commit 20090bf

Please sign in to comment.