M
Mastra•3w ago
Nic

Re-generate a message

Hi, I am trying to implement a regeneration mechanism. The user should be able to re-generate any message from the history. Right now, I am deleting the message to regenerate + all messages after that one but for some reason, this query always returns all messages:
const messages = await memory.query({
threadId: chat.threadId,
resourceId: chat.resourceId,
selectBy: {
include: [
{
id: messageId,
threadId: chat.threadId,
withPreviousMessages: 1,
withNextMessages: 1000,
},
],
},
});
const messages = await memory.query({
threadId: chat.threadId,
resourceId: chat.resourceId,
selectBy: {
include: [
{
id: messageId,
threadId: chat.threadId,
withPreviousMessages: 1,
withNextMessages: 1000,
},
],
},
});
3 Replies
Mastra Triager
Mastra Triager•3w ago
šŸ“ Created GitHub issue: https://github.com/mastra-ai/mastra/issues/10276 šŸ” If you're experiencing an error, please provide a minimal reproducible example to help us resolve it quickly. šŸ™ Thank you @Nic for helping us improve Mastra!
Abhi Aiyer
Abhi Aiyer•3w ago
Thanks for reporting this! We have a new query format in 1.0.0-beta that fixes quirks here. But let me see if we can have a fix in the 0.x version
į²¼
᲼•3w ago
My solution:
// actions.ts
const agent = mastra.getAgent("myAgent");
export async function deleteLastMessage(chatId: string) {
const supabase = createClient();
const { data: userData } = await supabase.auth.getUser();
if (!userData?.user) return;
const memory = await agent.getMemory();
const { uiMessages } = await memory!.query({
threadId: chatId,
resourceId: userData?.user?.id,
});
if (uiMessages.length > 0) {
const lastMessage = uiMessages[uiMessages.length - 1];
if (!lastMessage) return;
await memory?.deleteMessages([lastMessage.id]);
}
}

// client.tsx
import {deleteLastMessage} from 'actions.ts';
//...
const handleRegenerate = async () => {// Call when regenerate is pressed
if (status === 'ready' && chatId) {
await deleteLastMessage(chatId);
await regenerate();// from useChat
}
};
// actions.ts
const agent = mastra.getAgent("myAgent");
export async function deleteLastMessage(chatId: string) {
const supabase = createClient();
const { data: userData } = await supabase.auth.getUser();
if (!userData?.user) return;
const memory = await agent.getMemory();
const { uiMessages } = await memory!.query({
threadId: chatId,
resourceId: userData?.user?.id,
});
if (uiMessages.length > 0) {
const lastMessage = uiMessages[uiMessages.length - 1];
if (!lastMessage) return;
await memory?.deleteMessages([lastMessage.id]);
}
}

// client.tsx
import {deleteLastMessage} from 'actions.ts';
//...
const handleRegenerate = async () => {// Call when regenerate is pressed
if (status === 'ready' && chatId) {
await deleteLastMessage(chatId);
await regenerate();// from useChat
}
};
Only works for regenerating the last message. You could probably update it to work for regenerating any message but I wouldn't suggest that without also implementing history trees.

Did you find this page helpful?