Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an client-direct endpoint to get memories by agentid and roomid #1581

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
monilpat marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
console.error("Error fetching memories:", error);
monilpat marked this conversation as resolved.
Show resolved Hide resolved
res.status(500).json({ error: "Failed to fetch memories" });
}
});

return router;
}
Loading