Skip to content

Commit e0d9a70

Browse files
committed
wip - shared state
1 parent 3da17c3 commit e0d9a70

File tree

7 files changed

+4319
-608
lines changed

7 files changed

+4319
-608
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: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
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:*",
@@ -24,10 +25,11 @@
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.7",
28-
"@mastra/core": "^0.10.8",
29-
"@mastra/loggers": "^0.10.2",
30-
"@mastra/memory": "^0.11.0",
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",
3133
"@mdx-js/loader": "^3.1.0",
3234
"@mdx-js/mdx": "^3.1.0",
3335
"@mdx-js/react": "^3.1.0",
@@ -80,6 +82,7 @@
8082
"@types/react-dom": "^19",
8183
"eslint": "^9",
8284
"eslint-config-next": "15.2.1",
85+
"mastra": "^0.10.10",
8386
"tailwindcss": "^4",
8487
"typescript": "^5"
8588
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { LangGraphAgent } from "@ag-ui/langgraph";
1212
import { AgnoAgent } from "@ag-ui/agno";
1313
import { LlamaIndexAgent } from "@ag-ui/llamaindex";
1414
import { CrewAIAgent } from "@ag-ui/crewai";
15-
import { LOCAL_MASTRA } from "./local/mastra";
15+
import { mastra } from "./mastra";
1616

1717
export const agentsIntegrations: AgentIntegrationConfig[] = [
1818
{
@@ -71,7 +71,7 @@ export const agentsIntegrations: AgentIntegrationConfig[] = [
7171
{
7272
id: "mastra-agent-local",
7373
agents: async () => {
74-
return MastraAgent.getLocalAgents({ mastra: LOCAL_MASTRA });
74+
return MastraAgent.getLocalAgents({ mastra });
7575
},
7676
},
7777
{

typescript-sdk/apps/dojo/src/local/mastra.ts renamed to typescript-sdk/apps/dojo/src/mastra/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import "server-only";
21
import { openai } from "@ai-sdk/openai";
32
import { Agent } from "@mastra/core/agent";
43
import { Memory } from "@mastra/memory";
5-
import { MockStore } from "@mastra/core/storage";
4+
import { LibSQLStore } from "@mastra/libsql";
65
import { Mastra } from "@mastra/core";
76
import { z } from "zod";
87

98
// import { weatherTool } from "../tools/weather-tool";
109

11-
export const LOCAL_MASTRA = new Mastra({
10+
export const mastra = new Mastra({
1211
agents: {
1312
agentic_chat: new Agent({
1413
name: "agentic_chat",
@@ -24,10 +23,10 @@ export const LOCAL_MASTRA = new Mastra({
2423
2524
Use the weatherTool to fetch current weather data.
2625
`,
27-
model: openai("gpt-4o-mini"),
26+
model: openai("gpt-4o"),
2827
// tools: { weatherTool },
2928
memory: new Memory({
30-
storage: new MockStore(),
29+
storage: new LibSQLStore({ url: "file::memory:" }),
3130
options: {
3231
workingMemory: {
3332
enabled: true,

typescript-sdk/integrations/mastra/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@
2525
"dependencies": {
2626
"@ag-ui/client": "workspace:*",
2727
"@ai-sdk/ui-utils": "^1.1.19",
28-
"@mastra/client-js": "^0.10.7",
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.8",
33+
"@mastra/core": "^0.10.10",
3534
"zod": "^3.25.67"
3635
},
3736
"devDependencies": {
38-
"@mastra/core": "^0.10.8",
37+
"@mastra/core": "^0.10.10",
3938
"@types/jest": "^29.5.14",
4039
"@types/node": "^20.11.19",
4140
"jest": "^29.7.0",

typescript-sdk/integrations/mastra/src/index.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import { processDataStream } from "@ai-sdk/ui-utils";
2626
import type { CoreMessage, Mastra, StorageThreadType } from "@mastra/core";
2727
import { registerApiRoute } from "@mastra/core/server";
2828
import { Agent as LocalMastraAgent } from "@mastra/core/agent";
29-
import type { Context } from "hono";
3029
import { RuntimeContext } from "@mastra/core/runtime-context";
3130
import { randomUUID } from "crypto";
3231
import { Observable } from "rxjs";
@@ -175,7 +174,7 @@ export class MastraAgent extends AbstractAgent {
175174
}
176175

177176
protected run(input: RunAgentInput): Observable<BaseEvent> {
178-
console.log("STATE INPUT", input.state);
177+
// console.log("STATE INPUT", input.state);
179178

180179
const finalMessages: Message[] = [...input.messages];
181180
let messageId = randomUUID();
@@ -189,6 +188,8 @@ export class MastraAgent extends AbstractAgent {
189188

190189
return new Observable<BaseEvent>((subscriber) => {
191190
const run = async () => {
191+
console.log(">>> RUN STARTED", input.threadId, input.runId);
192+
console.log(">>> STATE", JSON.stringify(input.state));
192193
subscriber.next({
193194
type: EventType.RUN_STARTED,
194195
threadId: input.threadId,
@@ -204,6 +205,8 @@ export class MastraAgent extends AbstractAgent {
204205
threadId: input.threadId,
205206
});
206207

208+
console.log(">>> THREAD (getThreadById)", thread);
209+
207210
if (!thread) {
208211
thread = {
209212
id: input.threadId,
@@ -215,15 +218,25 @@ export class MastraAgent extends AbstractAgent {
215218
};
216219
}
217220

221+
// TODO: thread.metadata / use previous memory with spread operator to preserve it
222+
// TODO: state is empty when coming back from AG-UI
223+
218224
// if (thread.resourceId && thread.resourceId !== this.resourceId) {
219225
// throw new Error(
220226
// `Thread with id ${input.threadId} resourceId does not match the current resourceId ${this.resourceId}`,
221227
// );
222-
// }
223228

224229
const { messages, ...rest } = input.state;
225230
const workingMemory = JSON.stringify(rest);
226231

232+
console.log(">>> SAVING THREAD", {
233+
...thread,
234+
metadata: {
235+
...thread.metadata,
236+
workingMemory,
237+
},
238+
});
239+
227240
// Update thread metadata with new working memory
228241
await memory.saveThread({
229242
thread: {
@@ -303,7 +316,12 @@ export class MastraAgent extends AbstractAgent {
303316
if (memory) {
304317
const workingMemory = await memory.getWorkingMemory({
305318
threadId: input.threadId,
306-
format: "json",
319+
// @ts-ignore
320+
memoryConfig: {
321+
workingMemory: {
322+
enabled: true,
323+
},
324+
},
307325
});
308326

309327
console.log(">>> workingMemory", workingMemory);
@@ -519,14 +537,7 @@ export function registerCopilotKit<T extends Record<string, any> | unknown = unk
519537
resourceId: string;
520538
serviceAdapter?: CopilotServiceAdapter;
521539
agents?: Record<string, AbstractAgent>;
522-
setContext?: (
523-
c: Context<{
524-
Variables: {
525-
mastra: Mastra;
526-
};
527-
}>,
528-
runtimeContext: RuntimeContext<T>,
529-
) => void | Promise<void>;
540+
setContext?: (c: any, runtimeContext: RuntimeContext<T>) => void | Promise<void>;
530541
}) {
531542
return registerApiRoute(path, {
532543
method: `ALL`,

0 commit comments

Comments
 (0)