M
Mastra2w ago
Jenil

How to fetch memory in a workflow?

Hi, I have one main Workflow as the entry point of the app and which uses 2 agent in the flow, one after another and few maps in between. I have a storage option set while creating an instance of mastra, i.e., new Mastra({..., storage: .... }). I have NOT set memory options on any agents. Now few questions around it: 1. Each and every run on my main workflow will be stored in the storage right? 2. How to retrieve those workflow memory objects of each run? 3. How to set Resource and Thread in a Workflow for a particular User? 4. How to use Resource and ThreadId to give context to the agents while being called by the main workflow? Any example that can be shared?
3 Replies
Mastra Triager
GitHub
How to fetch memory in a workflow? · Issue #9686 · mastra-ai/mastra
This issue was created from Discord post 1434973887392645305: Hi, I have one main Workflow as the entry point of the app and which uses 2 agent in the flow, one after another and few maps in betwee...
_roamin_
_roamin_2w ago
Hi @Jenil ! 1. yes, so long you have storage setup on the Mastra instance 2. where are you looking at doing that? from withing a workflow step, from the client sdk? elsewhere? 3. workflows are not stored using thread & resource ids, but you can pass these using the runtimeContext 4. you can get the runtimeContext form the step execute args, and then pass that to the agent.
execute: async ({ mastra, runtimeContext }) => {
const myAgent = mastra.getAgent("myAgent");

const result = myAgent.generate("...", {
runtimeContext
})

return {
// ...
};
},
execute: async ({ mastra, runtimeContext }) => {
const myAgent = mastra.getAgent("myAgent");

const result = myAgent.generate("...", {
runtimeContext
})

return {
// ...
};
},
Jenil
JenilOP2w ago
Hi @Romain , I have this flow of code. Now in your example above, you have passed the runtime context directly, will it work in my case too below? Not using client-sdk yet for anything. Backend is hosted and connected via Websocket. 2 more question: - How will I load the previous history conversation in UI from my mainWorkflow which is the entry point of the app, I want to load the input and response of my each workflow executions? I am working on a chatbot. - How to pass last 10 messages in the agent with threadId and resourceId from runtimecontext?
const mastraApp = new Mastra(mastraConfig);

const runtimeContext = new RuntimeContext<AppRuntime>();
runtimeContext.set(runtimeKeys.threadId, threadId);
runtimeContext.set(runtimeKeys.resourceId, resourceId);

export const agentAStep = createStep({
id: "agentAStep",
inputSchema: ..., // define schema
outputSchema: ..., // define schema
execute: async ({ inputData: { prompt }, mastra, runtimeContext }) => {
const threadId = runtimeContext.get(runtimeKeys.threadId) as ThreadId;
const resourceId = runtimeContext.get(runtimeKeys.resourceId) as ResourceId;

const agentA = mastra.getAgent("agentA");
const agentResult = await agentA.generate(prompt, {
memory: { thread: threadId, resource: resourceId },
});

return agentResult.text;
},
});

export const mainWorkflow = createWorkflow({
id: "mainWorkflow",
description: "mainWorkflow workflow",
inputSchema: ..., // define schema
outputSchema: ..., // define schema
steps: [agentAStep],
})
.then(agentAStep)
.commit();

const mainWorkflowRun = await mastraApp
.getWorkflowById("mainWorkflow")
.createRunAsync();

const mainWorkflowResponse = await mainWorkflowRun.start({
inputData: { prompt: "hi" },
runtimeContext,
});

console.log(mainWorkflowResponse);
const mastraApp = new Mastra(mastraConfig);

const runtimeContext = new RuntimeContext<AppRuntime>();
runtimeContext.set(runtimeKeys.threadId, threadId);
runtimeContext.set(runtimeKeys.resourceId, resourceId);

export const agentAStep = createStep({
id: "agentAStep",
inputSchema: ..., // define schema
outputSchema: ..., // define schema
execute: async ({ inputData: { prompt }, mastra, runtimeContext }) => {
const threadId = runtimeContext.get(runtimeKeys.threadId) as ThreadId;
const resourceId = runtimeContext.get(runtimeKeys.resourceId) as ResourceId;

const agentA = mastra.getAgent("agentA");
const agentResult = await agentA.generate(prompt, {
memory: { thread: threadId, resource: resourceId },
});

return agentResult.text;
},
});

export const mainWorkflow = createWorkflow({
id: "mainWorkflow",
description: "mainWorkflow workflow",
inputSchema: ..., // define schema
outputSchema: ..., // define schema
steps: [agentAStep],
})
.then(agentAStep)
.commit();

const mainWorkflowRun = await mastraApp
.getWorkflowById("mainWorkflow")
.createRunAsync();

const mainWorkflowResponse = await mainWorkflowRun.start({
inputData: { prompt: "hi" },
runtimeContext,
});

console.log(mainWorkflowResponse);

Did you find this page helpful?