-
-
Notifications
You must be signed in to change notification settings - Fork 761
Added Mastra + Trigger example #2278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
docs/guides/example-projects/mastra-agents-with-memory.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
--- | ||
title: "Mastra agents with memory sharing + Trigger.dev task orchestration" | ||
sidebarTitle: "Mastra agents with memory" | ||
description: "Multi-agent workflow with persistent memory sharing using Mastra and Trigger.dev for clothing recommendations based on weather data." | ||
tag: "v4" | ||
--- | ||
|
||
import UpgradeToV4Note from "/snippets/upgrade-to-v4-note.mdx"; | ||
|
||
<UpgradeToV4Note /> | ||
|
||
## Overview | ||
|
||
Enter a city and an activity, and get a clothing recommendation generated for you based on today's weather. | ||
|
||
 | ||
|
||
By combining Mastra's persistent memory system and agent orchestration with Trigger.dev's durable task execution, retries and observability, you get production-ready AI workflows that survive failures, scale automatically, and maintain context across long-running operations. | ||
|
||
## Tech stack | ||
|
||
- **[Node.js](https://nodejs.org)** runtime environment | ||
- **[Mastra](https://mastra.ai)** for AI agent orchestration and memory management (Mastra is a Typescript framework for building AI agents, and uses Vercel's AI Agent SDK under the hood.) | ||
- **[PostgreSQL](https://postgresql.org)** for persistent storage and memory sharing | ||
- **[Trigger.dev](https://trigger.dev)** for task orchestration, batching, and observability | ||
- **[OpenAI GPT-4](https://openai.com)** for natural language processing | ||
- **[Open-Meteo API](https://open-meteo.com)** for weather data (no API key required) | ||
- **[Zod](https://zod.dev)** for schema validation and type safety | ||
|
||
## GitHub repo | ||
|
||
<Card | ||
title="View the Mastra agents with memory repo" | ||
icon="GitHub" | ||
href="https://github.com/triggerdotdev/examples/tree/main/mastra-agents" | ||
> | ||
Click here to view the full code for this project in our examples repository on GitHub. You can | ||
fork it and use it as a starting point for your own project. | ||
</Card> | ||
|
||
## Featured patterns | ||
|
||
- **[Agent Memory Sharing](https://github.com/triggerdotdev/examples/blob/main/mastra-agents/src/trigger/weather-task.ts)**: Efficient data sharing between agents using Mastra's working memory system | ||
- **[Task Orchestration](https://github.com/triggerdotdev/examples/blob/main/mastra-agents/src/trigger/weather-task.ts)**: Multi-step workflows with `triggerAndWait` for sequential agent execution | ||
- **[Centralized Storage](https://github.com/triggerdotdev/examples/blob/main/mastra-agents/src/mastra/index.ts)**: Single PostgreSQL storage instance shared across all agents to prevent connection duplication | ||
- **[Custom Tools](https://github.com/triggerdotdev/examples/blob/main/mastra-agents/src/mastra/tools/weather-tool.ts)**: External API integration with structured output validation | ||
- **[Agent Specialization](https://github.com/triggerdotdev/examples/blob/main/mastra-agents/src/mastra/agents/)**: Purpose-built agents with specific roles and instructions | ||
- **[Schema Optimization](https://github.com/triggerdotdev/examples/blob/main/mastra-agents/src/mastra/schemas/weather-data.ts)**: Lightweight data structures for performance | ||
|
||
## Project Structure | ||
|
||
``` | ||
src/ | ||
├── mastra/ | ||
│ ├── agents/ | ||
│ │ ├── weather-analyst.ts # Weather data collection | ||
│ │ ├── clothing-advisor.ts # Clothing recommendations | ||
│ ├── tools/ | ||
│ │ └── weather-tool.ts # Enhanced weather API tool | ||
│ ├── schemas/ | ||
│ │ └── weather-data.ts # Weather schema | ||
│ └── index.ts # Mastra configuration | ||
├── trigger/ | ||
│ └── weather-task.ts # Trigger.dev tasks | ||
``` | ||
|
||
## Relevant code | ||
|
||
- **[Multi-step task orchestration](https://github.com/triggerdotdev/examples/blob/main/mastra-agents/src/trigger/weather-task.ts)**: Multi-step task orchestration with `triggerAndWait` for sequential agent execution and shared memory context | ||
- **[Weather analyst agent](https://github.com/triggerdotdev/examples/blob/main/mastra-agents/src/mastra/agents/weather-analyst.ts)**: Specialized agent for weather data collection with external API integration and memory storage | ||
- **[Clothing advisor agent](https://github.com/triggerdotdev/examples/blob/main/mastra-agents/src/mastra/agents/clothing-advisor.ts)**: Purpose-built agent that reads from working memory and generates natural language responses | ||
- **[Weather tool](https://github.com/triggerdotdev/examples/blob/main/mastra-agents/src/mastra/tools/weather-tool.ts)**: Custom Mastra tool with Zod validation for external API calls and error handling | ||
- **[Weather data schema](https://github.com/triggerdotdev/examples/blob/main/mastra-agents/src/mastra/schemas/weather-data.ts)**: Optimized Zod schema for efficient memory storage and type safety | ||
- **[Mastra configuration](https://github.com/triggerdotdev/examples/blob/main/mastra-agents/src/mastra/index.ts)**: Mastra configuration with PostgreSQL storage and agent registration | ||
|
||
## Storage Architecture | ||
|
||
This project uses a **centralized PostgreSQL storage** approach where a single database connection is shared across all Mastra agents. This prevents duplicate database connections and ensures efficient memory sharing between the weather analyst and clothing advisor agents. | ||
|
||
### Storage Configuration | ||
|
||
The storage is configured once in the main Mastra instance (`src/mastra/index.ts`) and automatically inherited by all agent Memory instances. This eliminates the "duplicate database object" warning that can occur with multiple PostgreSQL connections. | ||
|
||
The PostgreSQL storage works seamlessly in both local development and serverless environments with any PostgreSQL provider, such as: | ||
|
||
- [Local PostgreSQL instance](https://postgresql.org) | ||
- [Supabase](https://supabase.com) - Serverless PostgreSQL | ||
- [Neon](https://neon.tech) - Serverless PostgreSQL | ||
- [Railway](https://railway.app) - Simple PostgreSQL hosting | ||
- [AWS RDS](https://aws.amazon.com/rds/postgresql/) - Managed PostgreSQL | ||
|
||
## Learn More | ||
|
||
To learn more about the technologies used in this project, check out the following resources: | ||
|
||
- [Mastra docs](https://mastra.ai/en/docs) - learn about AI agent orchestration and memory management | ||
- [Mastra working memory](https://mastra.ai/en/docs/memory/overview) - learn about efficient data sharing between agents |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.