import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core";
import { registerApiRoute } from "@mastra/core/server";
import { Memory } from "@mastra/memory";
const memory = new Memory();
const myAgent = new Agent({
name: "My Agent",
instructions: "You are a helpful assistant.",
model: openai("gpt-4o-mini"),
memory,
});
export const example = registerApiRoute("/example", {
method: "GET",
handler: async (c) => {
const resourceId = "0";
const threadId = "69868de7-1154-433a-831a-4e14412aadf3";
const memory = await myAgent.getMemory();
if (!memory) {
console.error("/example only works with memory");
return c.json({ error: "memory not set" }, 500);
}
const thread =
(await memory.getThreadById({ threadId })) ??
(await memory.createThread({
threadId,
resourceId: "0",
metadata: { foo: "bar" },
}));
await myAgent.generate("What can you do?", {
resourceId,
threadId,
});
return c.json({ thread });
},
});
import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core";
import { registerApiRoute } from "@mastra/core/server";
import { Memory } from "@mastra/memory";
const memory = new Memory();
const myAgent = new Agent({
name: "My Agent",
instructions: "You are a helpful assistant.",
model: openai("gpt-4o-mini"),
memory,
});
export const example = registerApiRoute("/example", {
method: "GET",
handler: async (c) => {
const resourceId = "0";
const threadId = "69868de7-1154-433a-831a-4e14412aadf3";
const memory = await myAgent.getMemory();
if (!memory) {
console.error("/example only works with memory");
return c.json({ error: "memory not set" }, 500);
}
const thread =
(await memory.getThreadById({ threadId })) ??
(await memory.createThread({
threadId,
resourceId: "0",
metadata: { foo: "bar" },
}));
await myAgent.generate("What can you do?", {
resourceId,
threadId,
});
return c.json({ thread });
},
});