Skip to content

Commit 44e2eff

Browse files
mmetylerslaton
andauthored
Enable shared state for local Mastra agents (#152)
* feat(mastra): support shared state for local agents) Signed-off-by: Tyler Slaton <[email protected]> --------- Signed-off-by: Tyler Slaton <[email protected]> Co-authored-by: Tyler Slaton <[email protected]>
1 parent f8c6ec6 commit 44e2eff

File tree

11 files changed

+5892
-1045
lines changed

11 files changed

+5892
-1045
lines changed

typescript-sdk/apps/dojo/next.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const nextConfig: NextConfig = {
2727

2828
return config;
2929
},
30+
serverExternalPackages: ["@mastra/libsql"],
3031
};
3132

3233
// Merge MDX config with Next.js config

typescript-sdk/apps/dojo/package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,30 @@
66
"dev": "next dev",
77
"build": "next build",
88
"start": "next start",
9-
"lint": "next lint"
9+
"lint": "next lint",
10+
"mastra:dev": "mastra dev"
1011
},
1112
"dependencies": {
1213
"@ag-ui/agno": "workspace:*",
14+
"@ag-ui/crewai": "workspace:*",
1315
"@ag-ui/langgraph": "workspace:*",
1416
"@ag-ui/llamaindex": "workspace:*",
1517
"@ag-ui/mastra": "workspace:*",
1618
"@ag-ui/middleware-starter": "workspace:*",
1719
"@ag-ui/server-starter": "workspace:*",
1820
"@ag-ui/server-starter-all-features": "workspace:*",
1921
"@ag-ui/vercel-ai-sdk": "workspace:*",
20-
"@ag-ui/crewai": "workspace:*",
2122
"@ai-sdk/openai": "^1.3.22",
2223
"@copilotkit/react-core": "1.8.14-next.4",
2324
"@copilotkit/react-ui": "1.8.14-next.4",
2425
"@copilotkit/runtime": "1.8.14-next.4",
2526
"@copilotkit/runtime-client-gql": "1.8.14-next.4",
2627
"@copilotkit/shared": "1.8.14-next.4",
27-
"@mastra/client-js": "^0.10.1",
28+
"@mastra/client-js": "^0.10.9",
29+
"@mastra/core": "^0.10.10",
30+
"@mastra/libsql": "^0.11.0",
31+
"@mastra/loggers": "^0.10.3",
32+
"@mastra/memory": "^0.11.1",
2833
"@mdx-js/loader": "^3.1.0",
2934
"@mdx-js/mdx": "^3.1.0",
3035
"@mdx-js/react": "^3.1.0",
@@ -56,7 +61,8 @@
5661
"rxjs": "7.8.1",
5762
"tailwind-merge": "^3.0.2",
5863
"tailwindcss-animate": "^1.0.7",
59-
"uuid": "^11.1.0"
64+
"uuid": "^11.1.0",
65+
"zod": "^3.22.4"
6066
},
6167
"peerDependencies": {
6268
"@ag-ui/client": "workspace:*",
@@ -76,6 +82,7 @@
7682
"@types/react-dom": "^19",
7783
"eslint": "^9",
7884
"eslint-config-next": "15.2.1",
85+
"mastra": "^0.10.10",
7986
"tailwindcss": "^4",
8087
"typescript": "^5"
8188
}

typescript-sdk/apps/dojo/src/agents.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import "server-only";
2+
13
import { AgentIntegrationConfig } from "./types/integration";
24
import { MiddlewareStarterAgent } from "@ag-ui/middleware-starter";
35
import { ServerStarterAgent } from "@ag-ui/server-starter";
@@ -10,6 +12,7 @@ import { LangGraphAgent } from "@ag-ui/langgraph";
1012
import { AgnoAgent } from "@ag-ui/agno";
1113
import { LlamaIndexAgent } from "@ag-ui/llamaindex";
1214
import { CrewAIAgent } from "@ag-ui/crewai";
15+
import { mastra } from "./mastra";
1316

1417
export const agentsIntegrations: AgentIntegrationConfig[] = [
1518
{
@@ -65,6 +68,12 @@ export const agentsIntegrations: AgentIntegrationConfig[] = [
6568
});
6669
},
6770
},
71+
{
72+
id: "mastra-agent-local",
73+
agents: async () => {
74+
return MastraAgent.getLocalAgents({ mastra });
75+
},
76+
},
6877
{
6978
id: "vercel-ai-sdk",
7079
agents: async () => {

typescript-sdk/apps/dojo/src/app/[integrationId]/feature/agentic_chat/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import React, { useState } from "react";
33
import "@copilotkit/react-ui/styles.css";
44
import "./style.css";
5-
import { CopilotKit, useCopilotAction, useCopilotChat } from "@copilotkit/react-core";
5+
import { CopilotKit, useCoAgent, useCopilotAction, useCopilotChat } from "@copilotkit/react-core";
66
import { CopilotChat } from "@copilotkit/react-ui";
77

88
interface AgenticChatProps {
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { openai } from "@ai-sdk/openai";
2+
import { Agent } from "@mastra/core/agent";
3+
import { Memory } from "@mastra/memory";
4+
import { LibSQLStore } from "@mastra/libsql";
5+
import { Mastra } from "@mastra/core";
6+
import { z } from "zod";
7+
8+
// import { weatherTool } from "../tools/weather-tool";
9+
10+
export const mastra = new Mastra({
11+
agents: {
12+
agentic_chat: new Agent({
13+
name: "agentic_chat",
14+
instructions: `
15+
You are a helpful weather assistant that provides accurate weather information.
16+
17+
Your primary function is to help users get weather details for specific locations. When responding:
18+
- Always ask for a location if none is provided
19+
- If the location name isn’t in English, please translate it
20+
- If giving a location with multiple parts (e.g. "New York, NY"), use the most relevant part (e.g. "New York")
21+
- Include relevant details like humidity, wind conditions, and precipitation
22+
- Keep responses concise but informative
23+
24+
Use the weatherTool to fetch current weather data.
25+
`,
26+
model: openai("gpt-4o"),
27+
// tools: { weatherTool },
28+
memory: new Memory({
29+
storage: new LibSQLStore({ url: "file::memory:" }),
30+
options: {
31+
workingMemory: {
32+
enabled: true,
33+
schema: z.object({
34+
firstName: z.string(),
35+
}),
36+
},
37+
},
38+
}),
39+
}),
40+
shared_state: new Agent({
41+
name: "shared_state",
42+
instructions: `
43+
You are a helpful assistant for creating recipes.
44+
45+
IMPORTANT:
46+
1. Create a recipe using the existing ingredients and instructions. Make sure the recipe is complete.
47+
2. For ingredients, append new ingredients to the existing ones.
48+
3. For instructions, append new steps to the existing ones.
49+
4. 'ingredients' is always an array of objects with 'icon', 'name', and 'amount' fields
50+
5. 'instructions' is always an array of strings
51+
52+
If you have just created or modified the recipe, just answer in one sentence what you did. dont describe the recipe, just say what you did.
53+
`,
54+
model: openai("gpt-4o"),
55+
memory: new Memory({
56+
storage: new LibSQLStore({ url: "file::memory:" }),
57+
options: {
58+
workingMemory: {
59+
enabled: true,
60+
schema: z.object({
61+
recipe: z.object({
62+
skill_level: z
63+
.enum(["Beginner", "Intermediate", "Advanced"])
64+
.describe("The skill level required for the recipe"),
65+
special_preferences: z
66+
.array(
67+
z.enum([
68+
"High Protein",
69+
"Low Carb",
70+
"Spicy",
71+
"Budget-Friendly",
72+
"One-Pot Meal",
73+
"Vegetarian",
74+
"Vegan",
75+
]),
76+
)
77+
.describe("A list of special preferences for the recipe"),
78+
cooking_time: z
79+
.enum(["5 min", "15 min", "30 min", "45 min", "60+ min"])
80+
.describe("The cooking time of the recipe"),
81+
ingredients: z
82+
.array(
83+
z.object({
84+
icon: z
85+
.string()
86+
.describe(
87+
"The icon emoji (not emoji code like '\x1f35e', but the actual emoji like 🥕) of the ingredient",
88+
),
89+
name: z.string().describe("The name of the ingredient"),
90+
amount: z.string().describe("The amount of the ingredient"),
91+
}),
92+
)
93+
.describe(
94+
"Entire list of ingredients for the recipe, including the new ingredients and the ones that are already in the recipe",
95+
),
96+
instructions: z
97+
.array(z.string())
98+
.describe(
99+
"Entire list of instructions for the recipe, including the new instructions and the ones that are already there",
100+
),
101+
changes: z.string().describe("A description of the changes made to the recipe"),
102+
}),
103+
}),
104+
},
105+
},
106+
}),
107+
}),
108+
},
109+
});

typescript-sdk/apps/dojo/src/menu.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const menuIntegrations: MenuIntegrationConfig[] = [
1313
},
1414
{
1515
id: "server-starter-all-features",
16-
name: "Server Starter All Features",
16+
name: "Server Starter (All Features)",
1717
features: [
1818
"agentic_chat",
1919
"human_in_the_loop",
@@ -28,6 +28,11 @@ export const menuIntegrations: MenuIntegrationConfig[] = [
2828
name: "Mastra",
2929
features: ["agentic_chat"],
3030
},
31+
{
32+
id: "mastra-agent-local",
33+
name: "Mastra Agent (Local)",
34+
features: ["agentic_chat", "shared_state"],
35+
},
3136
{
3237
id: "vercel-ai-sdk",
3338
name: "Vercel AI SDK",

typescript-sdk/integrations/mastra/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525
"dependencies": {
2626
"@ag-ui/client": "workspace:*",
2727
"@ai-sdk/ui-utils": "^1.1.19",
28-
"@mastra/client-js": "^0.10.1",
29-
"hono": "^4.5.1",
28+
"@mastra/client-js": "^0.10.9",
3029
"rxjs": "7.8.1"
3130
},
3231
"peerDependencies": {
3332
"@copilotkit/runtime": "^1.8.13",
34-
"@mastra/core": "^0.10.1",
35-
"zod": "^3.0.0"
33+
"@mastra/core": "^0.10.10",
34+
"zod": "^3.25.67"
3635
},
3736
"devDependencies": {
37+
"@mastra/core": "^0.10.10",
3838
"@types/jest": "^29.5.14",
3939
"@types/node": "^20.11.19",
4040
"jest": "^29.7.0",

0 commit comments

Comments
 (0)