M
MastraAI2mo ago
Raid55

How do I make RuntimeContext Typesafe?

if seems that the RuntimeContext becomes type unknown when used in a workflow or step. for example here I need to re-define finished every time I want to use it which feels very counter intuitive.
.dountil(mainLoopWorkflow, async ({ inputData, runtimeContext }) => {
const loopCount = runtimeContext.get("loop-count") as number;
const finished = runtimeContext.get("finished") as {
type: "success" | "error";
summary: string;
};

if (loopCount >= MAX_LOOP_COUNT) return true;
if (finished) return true;

return false;
})
.dountil(mainLoopWorkflow, async ({ inputData, runtimeContext }) => {
const loopCount = runtimeContext.get("loop-count") as number;
const finished = runtimeContext.get("finished") as {
type: "success" | "error";
summary: string;
};

if (loopCount >= MAX_LOOP_COUNT) return true;
if (finished) return true;

return false;
})
same goes with:
const userId = runtimeContext.get("userId") as string;
const userId = runtimeContext.get("userId") as string;
I define runtime context with a set of types here before calling my workflow
interface ChatContext {
userId: string;
model: LLMType;
threadId: string;
}
// ...
const runtimeContext = new RuntimeContext<ChatContext>();
// ...
const workflowResult = await workflowRun.start({
inputData,
runtimeContext,
});
interface ChatContext {
userId: string;
model: LLMType;
threadId: string;
}
// ...
const runtimeContext = new RuntimeContext<ChatContext>();
// ...
const workflowResult = await workflowRun.start({
inputData,
runtimeContext,
});
I would not mind if I had to inject this when declaring a step or workflow since I know it can be tough with typescript to pass the types around, but it seems that there is no way to do this.
const testWorkflow = createWorkflow<ChatContext>({ // <-- provide Context here
id: "test-workflow",
description: "Tests the workflow",
inputSchema: ...,
outputSchema: ...,
})
.map(async ({ inputData, runtimeContext}) => {
runtimeContext // here this would be of type RuntimeContext<ChatContext>
})
.then(testStep)
.commit();
const testWorkflow = createWorkflow<ChatContext>({ // <-- provide Context here
id: "test-workflow",
description: "Tests the workflow",
inputSchema: ...,
outputSchema: ...,
})
.map(async ({ inputData, runtimeContext}) => {
runtimeContext // here this would be of type RuntimeContext<ChatContext>
})
.then(testStep)
.commit();
const testStep = createStep<ChatContext>({ // <-- provide Context here
id: "test-workflow",
description: "Tests the workflow",
inputSchema: ...,
outputSchema: ...,
execute: async ({ runtimeContext }) => {
runtimeContext // here this would be of type RuntimeContext<ChatContext>
},
})
const testStep = createStep<ChatContext>({ // <-- provide Context here
id: "test-workflow",
description: "Tests the workflow",
inputSchema: ...,
outputSchema: ...,
execute: async ({ runtimeContext }) => {
runtimeContext // here this would be of type RuntimeContext<ChatContext>
},
})
the same would apply to the Agent definition.
3 Replies
_roamin_
_roamin_2mo ago
Hi @Raid55 ! Yes, that's known limitation currently. I'll let our triager bot open a GH feature request.
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
Mastra Triager
Mastra Triager2mo ago
GitHub
[DISCORD:1418302235397460111] How do I make RuntimeContext Typesafe...
This issue was created from Discord post: https://discord.com/channels/1309558646228779139/1418302235397460111 if seems that the RuntimeContext becomes type unknown when used in a workflow or step....

Did you find this page helpful?