Skip to content

Commit 20090bf

Browse files
authored
Merge pull request #1581 from treppers/memories_endpoint
add an client-direct endpoint to get memories by agentid and roomid
2 parents 50f8b5a + 0134fae commit 20090bf

File tree

1 file changed

+62
-0
lines changed
  • packages/client-direct/src

1 file changed

+62
-0
lines changed

packages/client-direct/src/api.ts

+62
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111

1212
import { REST, Routes } from "discord.js";
1313
import { DirectClient } from ".";
14+
import { stringToUuid } from "@elizaos/core";
1415

1516
export function createApiRouter(
1617
agents: Map<string, AgentRuntime>,
@@ -121,5 +122,66 @@ export function createApiRouter(
121122
}
122123
});
123124

125+
router.get("/agents/:agentId/:roomId/memories", async (req, res) => {
126+
const agentId = req.params.agentId;
127+
const roomId = stringToUuid(req.params.roomId);
128+
let runtime = agents.get(agentId);
129+
130+
// if runtime is null, look for runtime with the same name
131+
if (!runtime) {
132+
runtime = Array.from(agents.values()).find(
133+
(a) => a.character.name.toLowerCase() === agentId.toLowerCase()
134+
);
135+
}
136+
137+
if (!runtime) {
138+
res.status(404).send("Agent not found");
139+
return;
140+
}
141+
142+
try {
143+
const memories = await runtime.messageManager.getMemories({
144+
roomId,
145+
});
146+
const response = {
147+
agentId,
148+
roomId,
149+
memories: memories.map((memory) => ({
150+
id: memory.id,
151+
userId: memory.userId,
152+
agentId: memory.agentId,
153+
createdAt: memory.createdAt,
154+
content: {
155+
text: memory.content.text,
156+
action: memory.content.action,
157+
source: memory.content.source,
158+
url: memory.content.url,
159+
inReplyTo: memory.content.inReplyTo,
160+
attachments: memory.content.attachments?.map(
161+
(attachment) => ({
162+
id: attachment.id,
163+
url: attachment.url,
164+
title: attachment.title,
165+
source: attachment.source,
166+
description: attachment.description,
167+
text: attachment.text,
168+
contentType: attachment.contentType,
169+
})
170+
),
171+
},
172+
embedding: memory.embedding,
173+
roomId: memory.roomId,
174+
unique: memory.unique,
175+
similarity: memory.similarity,
176+
})),
177+
};
178+
179+
res.json(response);
180+
} catch (error) {
181+
console.error("Error fetching memories:", error);
182+
res.status(500).json({ error: "Failed to fetch memories" });
183+
}
184+
});
185+
124186
return router;
125187
}

0 commit comments

Comments
 (0)