Skip to content

Commit fbae1cf

Browse files
committed
Changed the project to use PostgreSQL for storage, updated readme and a few other corrections
1 parent af6883b commit fbae1cf

File tree

9 files changed

+140
-397
lines changed

9 files changed

+140
-397
lines changed

mastra-agents/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# OpenAI API key for the agent functionality
22
OPENAI_API_KEY=<your-openai-api-key>
3+
# Your PostgresSQL database URL
4+
DATABASE_URL=<your-database-url>
35

46
# Trigger.dev environment variables
57
TRIGGER_API_URL=https://api.trigger.dev

mastra-agents/README.md

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# Mastra agents + Trigger.dev task orchestration example project:
22

3-
> **ℹ️ Note:** This is a v4 project. If you are using v3 and want to upgrade, please refer to our [v4 upgrade guide](https://trigger.dev/docs/v4-upgrade-guide).
3+
> **ℹ️ Note:** This is a Trigger.dev v4 project. If you are using v3 and want to upgrade, please refer to our [v4 upgrade guide](https://trigger.dev/docs/v4-upgrade-guide).
44
5-
Enter a city and activity, and get a clothing recommendation generated based on the weather.
5+
Enter a city and a activity, and get a clothing recommendation generated for you based on today's weather.
66

77
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.
88

99
## Tech stack
1010

1111
- [Node.js](https://nodejs.org) runtime environment
1212
- [Mastra](https://mastra.ai) for AI agent orchestration and memory management
13+
- [PostgreSQL](https://postgresql.org) for persistent storage and memory sharing
1314
- [Trigger.dev](https://trigger.dev) for task orchestration, batching, and observability
1415
- [OpenAI GPT-4](https://openai.com) for natural language processing
1516
- [Open-Meteo API](https://open-meteo.com) for weather data (no API key required)
@@ -19,11 +20,12 @@ By combining Mastra's persistent memory system and agent orchestration with Trig
1920

2021
- **[Agent Memory Sharing](src/trigger/weather-task.ts)**: Efficient data sharing between agents using Mastra's working memory system
2122
- **[Task Orchestration](src/trigger/weather-task.ts)**: Multi-step workflows with `triggerAndWait` for sequential agent execution
23+
- **[Centralized Storage](src/mastra/index.ts)**: Single PostgreSQL storage instance shared across all agents to prevent connection duplication
2224
- **[Custom Tools](src/mastra/tools/weather-tool.ts)**: External API integration with structured output validation
2325
- **[Agent Specialization](src/mastra/agents/)**: Purpose-built agents with specific roles and instructions
2426
- **[Schema Optimization](src/mastra/schemas/weather-data.ts)**: Lightweight data structures for performance
2527

26-
## 📁 Project Structure
28+
## Project Structure
2729

2830
```
2931
src/
@@ -48,38 +50,45 @@ src/
4850
- [src/mastra/agents/clothing-advisor.ts](src/mastra/agents/clothing-advisor.ts) - Purpose-built agent that reads from working memory and generates natural language responses
4951
- [src/mastra/tools/weather-tool.ts](src/mastra/tools/weather-tool.ts) - Custom Mastra tool with Zod validation for external API calls and error handling
5052
- [src/mastra/schemas/weather-data.ts](src/mastra/schemas/weather-data.ts) - Optimized Zod schema for efficient memory storage and type safety
51-
- [src/mastra/index.ts](src/mastra/index.ts) - Mastra configuration with LibSQL storage and agent registration
53+
- [src/mastra/index.ts](src/mastra/index.ts) - Mastra configuration with PostgreSQL storage and agent registration
5254

53-
## Getting started
55+
## Storage Architecture
56+
57+
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.
58+
59+
### Storage Configuration
60+
61+
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.
62+
63+
The PostgreSQL storage works seamlessly in both local development and serverless environments with any PostgreSQL provider, such as:
64+
65+
- [Local PostgreSQL instance](https://postgresql.org)
66+
- [Supabase](https://supabase.com) - Serverless PostgreSQL
67+
- [Neon](https://neon.tech) - Serverless PostgreSQL
68+
- [Railway](https://railway.app) - Simple PostgreSQL hosting
69+
- [AWS RDS](https://aws.amazon.com/rds/postgresql/) - Managed PostgreSQL
70+
71+
## Running the project
5472

5573
1. After cloning the repo, run `npm install` to install the dependencies.
5674
2. Set up your environment variables (see `.env.example`)
5775
3. If you haven't already, sign up for a free Trigger.dev account [here](https://cloud.trigger.dev/login) and create a new project.
5876
4. Copy the project ref from the Trigger.dev dashboard and add it to the `trigger.config.ts` file.
59-
5. In your terminal, run the Trigger.dev dev CLI command with `npx trigger.dev@latest dev`.
77+
5. In your terminal, run the Trigger.dev dev CLI command with `npx trigger.dev@v4-beta dev`.
6078

6179
Now you should be able to visit your Trigger.dev dashboard and test any of the agent tasks with the example payloads provided in each task file.
6280

63-
## Testing locally
64-
65-
Use the Trigger.dev dashboard to test each task:
81+
## Testing
6682

6783
### Example payload
6884

6985
```json
70-
{ "city": "New York", "activity": "walking" }
86+
{
87+
"city": "New York",
88+
"activity": "walking"
89+
}
7190
```
7291

73-
## Deployment
74-
75-
This project uses LibSQL as a local database for development, but **LibSQL doesn't work in serverless environments**. For production deployment, you'll need to switch to a serverless-compatible storage option:
76-
77-
- **Turso** (LibSQL-compatible): Set `TURSO_DATABASE_URL` and `TURSO_AUTH_TOKEN` in your environment variables. Create an account and database [here](https://turso.tech/signup).
78-
- **PostgreSQL** (Supabase): Set `DATABASE_URL` in your environment variables. Create an account and database [here](https://supabase.com/dashboard/sign-in).
79-
- **No persistence**: Remove storage from mastra config entirely.
80-
81-
Update `src/mastra/index.ts` with your chosen storage provider before deploying.
82-
8392
## Learn More
8493

8594
To learn more about the technologies used in this project, check out the following resources:

0 commit comments

Comments
 (0)