Repository: agentuity/templates
Pull request #6: Add Mastra TypeScript Agent Framework templates
Author: login: devin-ai-integration[bot]
Created: 2025-04-17T20:40:16Z | Merged: 2025-04-19T00:45:55Z

# Add Mastra TypeScript Agent Framework Templates

This PR adds support for the Mastra TypeScript Agent Framework to both Bun and Node.js templates.

## Changes
- Added Mastra template to Bun in `bunjs/templates.yaml`
- Added Mastra template to Node.js in `nodejs/templates.yaml`
- Created a simple example agent file in `common/js/mastra/agent.ts`

The template installs the necessary packages (`mastra`, `@mastra/core`, `@ai-sdk/openai`, `ai`, and `zod`) and creates a basic agent example that demonstrates the core functionality of Mastra.

## Testing
- Verified template structure follows the schema defined in `templates.schema.json`
- Ensured no other templates were modified in this PR

Link to Devin run: https://app.devin.ai/sessions/7e803f1bdda744c884567bf8f0688c3a
Requested by: jhaynie@agentuity.com



Diff:
diff --git a/bunjs/templates.yaml b/bunjs/templates.yaml
index 9880b13..d4eedec 100644
--- a/bunjs/templates.yaml
+++ b/bunjs/templates.yaml
@@ -61,3 +61,18 @@
     - action: create_file
       filename: "src/agents/{{ .AgentName | safe_filename }}/index.ts"
       from: "common/js/vercel/anthropic.ts"
+- name: "Mastra TypeScript Agent Framework"
+  description: "Mastra is a TypeScript agent framework that provides primitives to build AI applications and features."
+  steps:
+    - command: bun
+      args:
+        - add
+        - --silent
+        - mastra
+        - "@mastra/core"
+        - "@ai-sdk/openai"
+        - "ai"
+        - "zod"
+    - action: create_file
+      filename: "src/agents/{{ .AgentName | safe_filename }}/index.ts"
+      template: "common/js/mastra/agent.ts"
diff --git a/common/js/mastra/agent.ts b/common/js/mastra/agent.ts
new file mode 100644
index 0000000..492bc4e
--- /dev/null
+++ b/common/js/mastra/agent.ts
@@ -0,0 +1,23 @@
+import type { AgentRequest, AgentResponse, AgentContext } from "@agentuity/sdk";
+import { Agent } from '@mastra/core/agent';
+import { openai } from '@ai-sdk/openai';
+
+export default async function AgentuityAgent(
+  req: AgentRequest,
+  resp: AgentResponse,
+  ctx: AgentContext,
+) {
+  try {
+    const agent = new Agent({
+      name: '{{ .AgentName }}',
+      instructions: 'You are a helpful assistant that provides concise and accurate information.',
+      model: openai('gpt-4o-mini'),
+    });
+
+    const result = await agent.generate(req.data.text || 'Hello, who are you?');
+    return resp.text(result.text);
+  } catch (error) {
+    ctx.logger.error('Error running agent:', error);
+    return resp.text('Sorry, there was an error processing your request.');
+  }
+}
diff --git a/nodejs/templates.yaml b/nodejs/templates.yaml
index 252ab8c..cde6279 100644
--- a/nodejs/templates.yaml
+++ b/nodejs/templates.yaml
@@ -66,3 +66,19 @@
     - action: create_file
       filename: "src/agents/{{ .AgentName | safe_filename }}/index.ts"
       from: "common/js/vercel/anthropic.ts"
+- name: "Mastra TypeScript Agent Framework"
+  description: "Mastra is a TypeScript agent framework that provides primitives to build AI applications and features."
+  steps:
+    - command: npm
+      args:
+        - install
+        - --no-fund
+        - --no-audit
+        - mastra
+        - "@mastra/core"
+        - "@ai-sdk/openai"
+        - "ai"
+        - "zod"
+    - action: create_file
+      filename: "src/agents/{{ .AgentName | safe_filename }}/index.ts"
+      template: "common/js/mastra/agent.ts"


Reviews:
user: login: jhaynie
state: CHANGES_REQUESTED
author_association: MEMBER
submitted_at: 2025-04-17T20:44:30Z
user: login: jhaynie
state: CHANGES_REQUESTED
author_association: MEMBER
submitted_at: 2025-04-17T20:47:15Z
user: login: jhaynie
state: CHANGES_REQUESTED
author_association: MEMBER
submitted_at: 2025-04-19T00:16:27Z
user: login: jhaynie
state: CHANGES_REQUESTED
author_association: MEMBER
submitted_at: 2025-04-19T00:17:40Z
user: login: jhaynie
state: CHANGES_REQUESTED
author_association: MEMBER
submitted_at: 2025-04-19T00:37:14Z

Review comments:
diff_hunk: @@ -0,0 +1,42 @@
+import { Agent, Memory } from '@mastra/core';
+import { openai } from '@ai-sdk/openai';
+import { z } from 'zod';
+
+const simpleWorkflow = {
+  start: () => ({
+    llm: {
+      messages: [
+        { role: 'system', content: 'You are a helpful assistant.' },
+        { role: 'user', content: 'Hello, who are you?' }
+      ],
+      model: openai('gpt-4o-mini'),
+    },
+    then: (response) => {
+      console.log('Agent response:', response.content);
+      return { result: response.content };
+    }
+  })
+};
+
+const memory = new Memory();
+
+const agent = new Agent({
+  name: '{{ .AgentName }}',
+  instructions: 'You are a helpful assistant that provides concise and accurate information.',
+  model: openai('gpt-4o-mini'),
+  memory,
+  workflow: { simpleWorkflow }
+});
+
+async function runAgent() {
path: common/js/mastra/agent.ts
user: login: jhaynie
body: to be an agentuity agent, you must export a function that implements AgentHandler.

See https://github.com/agentuity/sdk-js/blob/main/src/types.ts.

The agent execution for Mastra should be inside this AgentHandler function.

The most simple Agent example is: https://github.com/agentuity/templates/blob/main/common/js/agent.ts

For logging, make sure to use ctx.logger instead of console.log when possible.
created_at: 2025-04-17T20:44:25Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,45 @@
+import type { AgentRequest, AgentResponse, AgentContext } from "@agentuity/sdk";
+import { Agent, Memory } from '@mastra/core';
+import { openai } from '@ai-sdk/openai';
+
+export default async function Agent(
+  req: AgentRequest,
+  resp: AgentResponse,
+  ctx: AgentContext,
+) {
+  const memory = new Memory();
path: common/js/mastra/agent.ts
user: login: jhaynie
body: can we initialize memory in global scope and re-use or is this per request?
created_at: 2025-04-17T20:46:22Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,45 @@
+import type { AgentRequest, AgentResponse, AgentContext } from "@agentuity/sdk";
+import { Agent, Memory } from '@mastra/core';
+import { openai } from '@ai-sdk/openai';
+
+export default async function Agent(
+  req: AgentRequest,
+  resp: AgentResponse,
+  ctx: AgentContext,
+) {
+  const memory = new Memory();
+  
+  const simpleWorkflow = {
+    start: () => ({
+      llm: {
+        messages: [
+          { role: 'system', content: 'You are a helpful assistant.' },
+          { role: 'user', content: req.input || 'Hello, who are you?' }
+        ],
+        model: openai('gpt-4o-mini'),
+      },
+      then: (response) => {
+        ctx.logger.info('Agent response:', response.content);
+        return { result: response.content };
+      }
+    })
+  };
+
+  const mastraAgent = new Agent({
path: common/js/mastra/agent.ts
user: login: jhaynie
body: does it make sense to define the agent in global scope and then invoke the workflow when the Agent function is invoked per request?
created_at: 2025-04-17T20:47:01Z
author_association: MEMBER
diff_hunk: @@ -61,3 +61,18 @@
     - action: create_file
       filename: "src/agents/{{ .AgentName | safe_filename }}/index.ts"
       from: "common/js/vercel/anthropic.ts"
+- name: "Mastra TypeScript Agent Framework"
+  description: "Mastra is a TypeScript agent framework that provides primitives to build AI applications and features."
+  steps:
+    - command: bun
+      args:
+        - add
+        - --silent
+        - mastra
+        - "@mastra/core"
+        - "@ai-sdk/openai"
+        - "ai"
+        - "zod"
+    - action: create_file
+      filename: "src/agents/{{ .AgentName | safe_filename }}/index.ts"
path: bunjs/templates.yaml
user: login: jhaynie
body: use template since we are using a `{{ .AgentName }}` in the file
created_at: 2025-04-19T00:16:24Z
author_association: MEMBER
diff_hunk: @@ -61,3 +61,18 @@
     - action: create_file
       filename: "src/agents/{{ .AgentName | safe_filename }}/index.ts"
       from: "common/js/vercel/anthropic.ts"
+- name: "Mastra TypeScript Agent Framework"
+  description: "Mastra is a TypeScript agent framework that provides primitives to build AI applications and features."
+  steps:
+    - command: bun
+      args:
+        - add
+        - --silent
+        - mastra
+        - "@mastra/core"
+        - "@ai-sdk/openai"
+        - "ai"
+        - "zod"
+    - action: create_file
+      filename: "src/agents/{{ .AgentName | safe_filename }}/index.ts"
path: bunjs/templates.yaml
user: login: jhaynie
body: rename `filename` to `template`
created_at: 2025-04-19T00:17:35Z
author_association: MEMBER
diff_hunk: @@ -0,0 +1,41 @@
+import type { AgentRequest, AgentResponse, AgentContext } from "@agentuity/sdk";
+import { Agent } from '@mastra/core';
+import { openai } from '@ai-sdk/openai';
+
+const simpleWorkflow = {
+  start: (input: string) => ({
+    llm: {
+      messages: [
+        { role: 'system', content: 'You are a helpful assistant.' },
+        { role: 'user', content: input || 'Hello, who are you?' }
+      ],
+      model: openai('gpt-4o-mini'),
+    },
+    then: (response) => {
+      return { result: response.content };
+    }
+  })
+};
+
+const mastraAgent = new Agent({
+  name: '{{ .AgentName }}',
+  instructions: 'You are a helpful assistant that provides concise and accurate information.',
+  model: openai('gpt-4o-mini'),
+  workflow: { simpleWorkflow }
+});
+
+export default async function Agent(
path: common/js/mastra/agent.ts
user: login: jhaynie
body: maybe rename this AgentuityAgent
created_at: 2025-04-19T00:37:09Z
author_association: MEMBER

PR comments:
user: login: devin-ai-integration[bot]
created_at: 2025-04-17T20:40:25Z
body: ### 🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:
- Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
- Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:
- [ ] Disable automatic comment and CI monitoring

author_association: CONTRIBUTOR
user: login: jhaynie
created_at: 2025-04-19T00:10:53Z
body: i'm getting:

```
⣻ Building project ...
Build Failed

✘ [ERROR] No matching export in "node_modules/@mastra/core/dist/index.js" for import "Memory"

    src/agents/my-agent/index.ts:2:16:
      2 │ import { Agent, Memory } from '@mastra/core';
        ╵                 ~~~~~~
```

Trying to run this template...

Remove memory
author_association: MEMBER