Skip to content

Commit 612053c

Browse files
chore: mastra example
1 parent 88d29e6 commit 612053c

File tree

18 files changed

+8699
-61
lines changed

18 files changed

+8699
-61
lines changed

examples/mastra/README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Mastra Integration
2+
3+
This example demonstrates how to integrate Rivet with Mastra for AI agents with persistent state.
4+
5+
## Features
6+
7+
- **Persistent AI conversations** that survive server restarts
8+
- **Real-time weather data** using Open-Meteo API
9+
- **Memory system** for saving and recalling information
10+
- **Per-user state isolation** with automatic persistence
11+
- **Web interface** for testing and interaction
12+
13+
## Getting Started
14+
15+
### Prerequisites
16+
17+
- Node.js 18+
18+
- OpenAI API key
19+
20+
### Environment Variables
21+
22+
Create a `.env` file with your OpenAI API key:
23+
24+
```bash
25+
OPENAI_API_KEY=your-openai-api-key-here
26+
```
27+
28+
### Development
29+
30+
Install dependencies and start the development server:
31+
32+
```bash
33+
npm install
34+
npm run dev
35+
```
36+
37+
The server will start at http://localhost:8080 with a web interface for testing.
38+
39+
### Testing
40+
41+
Test the API endpoints directly:
42+
43+
```bash
44+
# Send a message
45+
curl -X POST http://localhost:8080/chat \
46+
-H "Content-Type: application/json" \
47+
-d '{"userId": "test-user", "message": "What'\''s the weather in Tokyo?"}'
48+
49+
# Get conversation history
50+
curl http://localhost:8080/chat/test-user/history
51+
52+
# Clear conversation
53+
curl -X DELETE http://localhost:8080/chat/test-user
54+
```
55+
56+
## API Reference
57+
58+
### Chat Endpoint
59+
```
60+
POST /chat
61+
Body: { "userId": string, "message": string }
62+
Response: { "response": string, "messageId": string, "timestamp": number }
63+
```
64+
65+
### History Endpoint
66+
```
67+
GET /chat/:userId/history
68+
Response: { "history": Message[], "total": number, "actorName": string }
69+
```
70+
71+
### Clear Endpoint
72+
```
73+
DELETE /chat/:userId
74+
Response: { "success": boolean, "message": string }
75+
```
76+
77+
## How It Works
78+
79+
The integration combines Rivet Actors for state persistence with Mastra agents for AI processing:
80+
81+
1. **Rivet Actors** store conversation history, user memory, and tool data
82+
2. **Mastra Agents** process messages using OpenAI with access to tools
83+
3. **Tools** can call external APIs (weather) and modify persistent state
84+
4. **State automatically persists** across server restarts and user sessions
85+
86+
Each user gets their own isolated actor instance that maintains state between interactions.
87+
88+
## Architecture
89+
90+
```
91+
┌─────────────────────────────────┐
92+
│ Rivet Actor │
93+
│ ┌───────────────────────────┐ │
94+
│ │ Mastra Agent │ │
95+
│ │ • OpenAI GPT-4o-mini │ │
96+
│ │ • Weather Tool │ │
97+
│ │ • Memory Tool │ │
98+
│ │ • Recall Tool │ │
99+
│ └───────────────────────────┘ │
100+
│ │
101+
│ Persistent State: │
102+
│ • messages[] │
103+
│ • userMemory{} │
104+
│ • toolData{} │
105+
└─────────────────────────────────┘
106+
```
107+
108+
## Example Interactions
109+
110+
**Weather Query:**
111+
```
112+
User: "What's the weather in Tokyo?"
113+
AI: "The current weather in Tokyo is clear sky with a temperature of 18°C, feels like 16°C, humidity at 65%, and wind speed of 12 km/h."
114+
```
115+
116+
**Memory System:**
117+
```
118+
User: "Remember my favorite color is blue"
119+
AI: "I've remembered: my favorite color is blue"
120+
121+
User: "What do you remember about me?"
122+
AI: "Here's what I remember:
123+
- You told me: my favorite color is blue
124+
- Last weather: Tokyo - Clear sky, 18°C"
125+
```

0 commit comments

Comments
 (0)