save message to memory

I've got a setup where a user hits a chat interface (ai sdk useChat setup). I'm using initial messags to populate the feed BUT, those messages aren't saved to memory. I generate them from an agent.generate call. I tried to add memory to that but I don't want the prompt to land in memory, just the response from the LLM, as if it was starting the conversation. Does anyone have a solution for this using Mastra sdk or do I need to create my own function to write to memory?
9 Replies
_roamin_
_roamin_17h ago
Hey @rogueturnip ! You can manually save messages into the agent memory, see https://mastra.ai/en/reference/client-js/memory#save-messages
Mastra Client Memory API
Learn how to manage conversation threads and message history in Mastra using the client-js SDK.
rogueturnip
rogueturnipOP17h ago
Thanks! I was looking at this. i noticed no option for the resource id. is that not need?
_roamin_
_roamin_17h ago
Are you trying to save the messages from the client sdk or from your backend? I'm checking for the client sdk, but from the backend you should be providing the resource and thread id:
const memory = await agent.getMemory();

await memory?.saveMessages({
messages: [
{
id: generateId(),
type: "text",
role: "user",
content: {
parts: [{ type: "text", text: "this is a test message" }],
format: 2,
},
createdAt: new Date(),
threadId: threadId,
resourceId: organizationId,
},
],
});
const memory = await agent.getMemory();

await memory?.saveMessages({
messages: [
{
id: generateId(),
type: "text",
role: "user",
content: {
parts: [{ type: "text", text: "this is a test message" }],
format: 2,
},
createdAt: new Date(),
threadId: threadId,
resourceId: organizationId,
},
],
});
rogueturnip
rogueturnipOP16h ago
perfect!! thanks. i’m doing it at backend Just for reference, could you point me to the doc url that shows the getMemory/saveMessage? I could found deleteMessage and others but not saveMessage off the agent memory.
rogueturnip
rogueturnipOP16h ago
No description
_roamin_
_roamin_16h ago
It's missing at the moment 😢 We're working on improving the docs this month though and automate our reference docs
rogueturnip
rogueturnipOP16h ago
wonderful thanks! This is probably why I didn't find it on my own.. Honest I looked!
_roamin_
_roamin_16h ago
Thanks for trying to look, a lot of people don't even bother 🤣
rogueturnip
rogueturnipOP12h ago
You guys are busy, I have deliverables.. so I can't expect you to be on my timelines :). I do really really apreciate you taking the time to help

Did you find this page helpful?