From 19ec40b5b4e5ff6c2873520818f08735c1370347 Mon Sep 17 00:00:00 2001 From: treppers <90061012+treppers@users.noreply.github.com> Date: Mon, 30 Dec 2024 20:53:42 +0000 Subject: [PATCH] add an client-direct endpoint to get memories by agentid and roomid --- packages/client-direct/src/api.ts | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/packages/client-direct/src/api.ts b/packages/client-direct/src/api.ts index f74174e445..6d5ac569f5 100644 --- a/packages/client-direct/src/api.ts +++ b/packages/client-direct/src/api.ts @@ -11,6 +11,7 @@ import { import { REST, Routes } from "discord.js"; import { DirectClient } from "."; +import { stringToUuid } from "@elizaos/core"; export function createApiRouter( agents: Map, @@ -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; }