@mastra/pg 1.0.0-beta.9 → 1.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1481 -0
- package/dist/docs/README.md +36 -0
- package/dist/docs/SKILL.md +37 -0
- package/dist/docs/SOURCE_MAP.json +6 -0
- package/dist/docs/memory/01-storage.md +261 -0
- package/dist/docs/memory/02-working-memory.md +411 -0
- package/dist/docs/memory/03-semantic-recall.md +256 -0
- package/dist/docs/memory/04-reference.md +133 -0
- package/dist/docs/processors/01-reference.md +296 -0
- package/dist/docs/rag/01-overview.md +74 -0
- package/dist/docs/rag/02-vector-databases.md +643 -0
- package/dist/docs/rag/03-retrieval.md +548 -0
- package/dist/docs/rag/04-reference.md +369 -0
- package/dist/docs/storage/01-reference.md +905 -0
- package/dist/docs/tools/01-reference.md +440 -0
- package/dist/docs/vectors/01-reference.md +307 -0
- package/dist/index.cjs +1293 -260
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1290 -262
- package/dist/index.js.map +1 -1
- package/dist/shared/config.d.ts +61 -66
- package/dist/shared/config.d.ts.map +1 -1
- package/dist/storage/client.d.ts +91 -0
- package/dist/storage/client.d.ts.map +1 -0
- package/dist/storage/db/index.d.ts +82 -17
- package/dist/storage/db/index.d.ts.map +1 -1
- package/dist/storage/domains/agents/index.d.ts +11 -1
- package/dist/storage/domains/agents/index.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +3 -2
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/index.d.ts +24 -1
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +1 -0
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +44 -17
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/test-utils.d.ts.map +1 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/dist/vector/sql-builder.d.ts.map +1 -1
- package/package.json +14 -14
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @mastra/pg Documentation
|
|
2
|
+
|
|
3
|
+
> Embedded documentation for coding agents
|
|
4
|
+
|
|
5
|
+
## Quick Start
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Read the skill overview
|
|
9
|
+
cat docs/SKILL.md
|
|
10
|
+
|
|
11
|
+
# Get the source map
|
|
12
|
+
cat docs/SOURCE_MAP.json
|
|
13
|
+
|
|
14
|
+
# Read topic documentation
|
|
15
|
+
cat docs/<topic>/01-overview.md
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Structure
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
docs/
|
|
22
|
+
├── SKILL.md # Entry point
|
|
23
|
+
├── README.md # This file
|
|
24
|
+
├── SOURCE_MAP.json # Export index
|
|
25
|
+
├── memory/ (4 files)
|
|
26
|
+
├── processors/ (3 files)
|
|
27
|
+
├── rag/ (4 files)
|
|
28
|
+
├── storage/ (3 files)
|
|
29
|
+
├── tools/ (1 files)
|
|
30
|
+
├── vectors/ (1 files)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Version
|
|
34
|
+
|
|
35
|
+
Package: @mastra/pg
|
|
36
|
+
Version: 1.1.0-alpha.0
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mastra-pg-docs
|
|
3
|
+
description: Documentation for @mastra/pg. Includes links to type definitions and readable implementation code in dist/.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# @mastra/pg Documentation
|
|
7
|
+
|
|
8
|
+
> **Version**: 1.1.0-alpha.0
|
|
9
|
+
> **Package**: @mastra/pg
|
|
10
|
+
|
|
11
|
+
## Quick Navigation
|
|
12
|
+
|
|
13
|
+
Use SOURCE_MAP.json to find any export:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
cat docs/SOURCE_MAP.json
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Each export maps to:
|
|
20
|
+
- **types**: `.d.ts` file with JSDoc and API signatures
|
|
21
|
+
- **implementation**: `.js` chunk file with readable source
|
|
22
|
+
- **docs**: Conceptual documentation in `docs/`
|
|
23
|
+
|
|
24
|
+
## Top Exports
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
See SOURCE_MAP.json for the complete list.
|
|
29
|
+
|
|
30
|
+
## Available Topics
|
|
31
|
+
|
|
32
|
+
- [Memory](memory/) - 4 file(s)
|
|
33
|
+
- [Processors](processors/) - 3 file(s)
|
|
34
|
+
- [Rag](rag/) - 4 file(s)
|
|
35
|
+
- [Storage](storage/) - 3 file(s)
|
|
36
|
+
- [Tools](tools/) - 1 file(s)
|
|
37
|
+
- [Vectors](vectors/) - 1 file(s)
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
> Configure storage for Mastra
|
|
2
|
+
|
|
3
|
+
# Storage
|
|
4
|
+
|
|
5
|
+
For agents to remember previous interactions, Mastra needs a database. Use a storage adapter for one of the [supported databases](#supported-providers) and pass it to your Mastra instance.
|
|
6
|
+
|
|
7
|
+
```typescript title="src/mastra/index.ts"
|
|
8
|
+
import { Mastra } from "@mastra/core";
|
|
9
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
10
|
+
|
|
11
|
+
export const mastra = new Mastra({
|
|
12
|
+
storage: new LibSQLStore({
|
|
13
|
+
id: 'mastra-storage',
|
|
14
|
+
url: "file:./mastra.db",
|
|
15
|
+
}),
|
|
16
|
+
});
|
|
17
|
+
```
|
|
18
|
+
This configures instance-level storage, which all agents share by default. You can also configure [agent-level storage](#agent-level-storage) for isolated data boundaries.
|
|
19
|
+
|
|
20
|
+
Mastra automatically creates the necessary tables on first interaction. See the [core schema](https://mastra.ai/reference/storage/overview#core-schema) for details on what gets created, including tables for messages, threads, resources, workflows, traces, and evaluation datasets.
|
|
21
|
+
|
|
22
|
+
## Supported providers
|
|
23
|
+
|
|
24
|
+
Each provider page includes installation instructions, configuration parameters, and usage examples:
|
|
25
|
+
|
|
26
|
+
- [libSQL](https://mastra.ai/reference/storage/libsql)
|
|
27
|
+
- [PostgreSQL](https://mastra.ai/reference/storage/postgresql)
|
|
28
|
+
- [MongoDB](https://mastra.ai/reference/storage/mongodb)
|
|
29
|
+
- [Upstash](https://mastra.ai/reference/storage/upstash)
|
|
30
|
+
- [Cloudflare D1](https://mastra.ai/reference/storage/cloudflare-d1)
|
|
31
|
+
- [Cloudflare Durable Objects](https://mastra.ai/reference/storage/cloudflare)
|
|
32
|
+
- [Convex](https://mastra.ai/reference/storage/convex)
|
|
33
|
+
- [DynamoDB](https://mastra.ai/reference/storage/dynamodb)
|
|
34
|
+
- [LanceDB](https://mastra.ai/reference/storage/lance)
|
|
35
|
+
- [Microsoft SQL Server](https://mastra.ai/reference/storage/mssql)
|
|
36
|
+
|
|
37
|
+
> **Note:**
|
|
38
|
+
libSQL is the easiest way to get started because it doesn’t require running a separate database server.
|
|
39
|
+
|
|
40
|
+
## Configuration scope
|
|
41
|
+
|
|
42
|
+
Storage can be configured at the instance level (shared by all agents) or at the agent level (isolated to a specific agent).
|
|
43
|
+
|
|
44
|
+
### Instance-level storage
|
|
45
|
+
|
|
46
|
+
Add storage to your Mastra instance so all agents, workflows, observability traces and scores share the same memory provider:
|
|
47
|
+
|
|
48
|
+
```typescript title="src/mastra/index.ts"
|
|
49
|
+
import { Mastra } from "@mastra/core";
|
|
50
|
+
import { PostgresStore } from "@mastra/pg";
|
|
51
|
+
|
|
52
|
+
export const mastra = new Mastra({
|
|
53
|
+
storage: new PostgresStore({
|
|
54
|
+
id: 'mastra-storage',
|
|
55
|
+
connectionString: process.env.DATABASE_URL,
|
|
56
|
+
}),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Both agents inherit storage from the Mastra instance above
|
|
60
|
+
const agent1 = new Agent({ id: "agent-1", memory: new Memory() });
|
|
61
|
+
const agent2 = new Agent({ id: "agent-2", memory: new Memory() });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
This is useful when all primitives share the same storage backend and have similar performance, scaling, and operational requirements.
|
|
65
|
+
|
|
66
|
+
#### Composite storage
|
|
67
|
+
|
|
68
|
+
[Composite storage](https://mastra.ai/reference/storage/composite) is an alternative way to configure instance-level storage. Use `MastraCompositeStore` to set the `memory` domain (and any other [domains](https://mastra.ai/reference/storage/composite#storage-domains) you need) to different storage providers.
|
|
69
|
+
|
|
70
|
+
```typescript title="src/mastra/index.ts"
|
|
71
|
+
import { Mastra } from "@mastra/core";
|
|
72
|
+
import { MastraCompositeStore } from "@mastra/core/storage";
|
|
73
|
+
import { MemoryLibSQL } from "@mastra/libsql";
|
|
74
|
+
import { WorkflowsPG } from "@mastra/pg";
|
|
75
|
+
import { ObservabilityStorageClickhouse } from "@mastra/clickhouse";
|
|
76
|
+
|
|
77
|
+
export const mastra = new Mastra({
|
|
78
|
+
storage: new MastraCompositeStore({
|
|
79
|
+
id: "composite",
|
|
80
|
+
domains: {
|
|
81
|
+
// highlight-next-line
|
|
82
|
+
memory: new MemoryLibSQL({ url: "file:./memory.db" }),
|
|
83
|
+
workflows: new WorkflowsPG({ connectionString: process.env.DATABASE_URL }),
|
|
84
|
+
observability: new ObservabilityStorageClickhouse({
|
|
85
|
+
url: process.env.CLICKHOUSE_URL,
|
|
86
|
+
username: process.env.CLICKHOUSE_USERNAME,
|
|
87
|
+
password: process.env.CLICKHOUSE_PASSWORD,
|
|
88
|
+
}),
|
|
89
|
+
},
|
|
90
|
+
}),
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
This is useful when different types of data have different performance or operational requirements, such as low-latency storage for memory, durable storage for workflows, and high-throughput storage for observability.
|
|
95
|
+
|
|
96
|
+
### Agent-level storage
|
|
97
|
+
|
|
98
|
+
Agent-level storage overrides storage configured at the instance level. Add storage to a specific agent when you need data boundaries or compliance requirements:
|
|
99
|
+
|
|
100
|
+
```typescript title="src/mastra/agents/your-agent.ts"
|
|
101
|
+
import { Agent } from "@mastra/core/agent";
|
|
102
|
+
import { Memory } from "@mastra/memory";
|
|
103
|
+
import { PostgresStore } from "@mastra/pg";
|
|
104
|
+
|
|
105
|
+
export const agent = new Agent({
|
|
106
|
+
id: "agent",
|
|
107
|
+
memory: new Memory({
|
|
108
|
+
storage: new PostgresStore({
|
|
109
|
+
id: 'agent-storage',
|
|
110
|
+
connectionString: process.env.AGENT_DATABASE_URL,
|
|
111
|
+
}),
|
|
112
|
+
}),
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
> **Note:**
|
|
117
|
+
[Mastra Cloud Store](https://mastra.ai/docs/mastra-cloud/deployment#using-mastra-cloud-store) doesn't support agent-level storage.
|
|
118
|
+
|
|
119
|
+
## Threads and resources
|
|
120
|
+
|
|
121
|
+
Mastra organizes conversations using two identifiers:
|
|
122
|
+
|
|
123
|
+
- **Thread** - a conversation session containing a sequence of messages.
|
|
124
|
+
- **Resource** - the entity that owns the thread, such as a user, organization, project, or any other domain entity in your application.
|
|
125
|
+
|
|
126
|
+
Both identifiers are required for agents to store information:
|
|
127
|
+
|
|
128
|
+
**generate:**
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
const response = await agent.generate("hello", {
|
|
132
|
+
memory: {
|
|
133
|
+
thread: "conversation-abc-123",
|
|
134
|
+
resource: "user_123",
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
**stream:**
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
const stream = await agent.stream("hello", {
|
|
144
|
+
memory: {
|
|
145
|
+
thread: "conversation-abc-123",
|
|
146
|
+
resource: "user_123",
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
> **Note:**
|
|
154
|
+
[Studio](https://mastra.ai/docs/getting-started/studio) automatically generates a thread and resource ID for you. When calling `stream()` or `generate()` yourself, remember to provide these identifiers explicitly.
|
|
155
|
+
|
|
156
|
+
### Thread title generation
|
|
157
|
+
|
|
158
|
+
Mastra can automatically generate descriptive thread titles based on the user's first message when `generateTitle` is enabled.
|
|
159
|
+
|
|
160
|
+
Use this option when implementing a ChatGPT-style chat interface to render a title alongside each thread in the conversation list (for example, in a sidebar) derived from the thread’s initial user message.
|
|
161
|
+
|
|
162
|
+
```typescript title="src/mastra/agents/my-agent.ts"
|
|
163
|
+
export const agent = new Agent({
|
|
164
|
+
id: "agent",
|
|
165
|
+
memory: new Memory({
|
|
166
|
+
options: {
|
|
167
|
+
generateTitle: true,
|
|
168
|
+
},
|
|
169
|
+
}),
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Title generation runs asynchronously after the agent responds and does not affect response time.
|
|
174
|
+
|
|
175
|
+
To optimize cost or behavior, provide a smaller [`model`](/models) and custom `instructions`:
|
|
176
|
+
|
|
177
|
+
```typescript title="src/mastra/agents/my-agent.ts"
|
|
178
|
+
export const agent = new Agent({
|
|
179
|
+
id: "agent",
|
|
180
|
+
memory: new Memory({
|
|
181
|
+
options: {
|
|
182
|
+
generateTitle: {
|
|
183
|
+
model: "openai/gpt-4o-mini",
|
|
184
|
+
instructions: "Generate a 1 word title",
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
}),
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Semantic recall
|
|
192
|
+
|
|
193
|
+
Semantic recall has different storage requirements - it needs a vector database in addition to the standard storage adapter. See [Semantic recall](https://mastra.ai/docs/memory/semantic-recall) for setup and supported vector providers.
|
|
194
|
+
|
|
195
|
+
## Handling large attachments
|
|
196
|
+
|
|
197
|
+
Some storage providers enforce record size limits that base64-encoded file attachments (such as images) can exceed:
|
|
198
|
+
|
|
199
|
+
| Provider | Record size limit |
|
|
200
|
+
| -------- | ----------------- |
|
|
201
|
+
| [DynamoDB](https://mastra.ai/reference/storage/dynamodb) | 400 KB |
|
|
202
|
+
| [Convex](https://mastra.ai/reference/storage/convex) | 1 MiB |
|
|
203
|
+
| [Cloudflare D1](https://mastra.ai/reference/storage/cloudflare-d1) | 1 MiB |
|
|
204
|
+
|
|
205
|
+
PostgreSQL, MongoDB, and libSQL have higher limits and are generally unaffected.
|
|
206
|
+
|
|
207
|
+
To avoid this, use an input processor to upload attachments to external storage (S3, R2, GCS, [Convex file storage](https://docs.convex.dev/file-storage), etc.) and replace them with URL references before persistence.
|
|
208
|
+
|
|
209
|
+
```typescript title="src/mastra/processors/attachment-uploader.ts"
|
|
210
|
+
import type { Processor } from "@mastra/core/processors";
|
|
211
|
+
import type { MastraDBMessage } from "@mastra/core/memory";
|
|
212
|
+
|
|
213
|
+
export class AttachmentUploader implements Processor {
|
|
214
|
+
id = "attachment-uploader";
|
|
215
|
+
|
|
216
|
+
async processInput({ messages }: { messages: MastraDBMessage[] }) {
|
|
217
|
+
return Promise.all(messages.map((msg) => this.processMessage(msg)));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async processMessage(msg: MastraDBMessage) {
|
|
221
|
+
const attachments = msg.content.experimental_attachments;
|
|
222
|
+
if (!attachments?.length) return msg;
|
|
223
|
+
|
|
224
|
+
const uploaded = await Promise.all(
|
|
225
|
+
attachments.map(async (att) => {
|
|
226
|
+
// Skip if already a URL
|
|
227
|
+
if (!att.url?.startsWith("data:")) return att;
|
|
228
|
+
|
|
229
|
+
// Upload base64 data and replace with URL
|
|
230
|
+
const url = await this.upload(att.url, att.contentType);
|
|
231
|
+
return { ...att, url };
|
|
232
|
+
})
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
return { ...msg, content: { ...msg.content, experimental_attachments: uploaded } };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async upload(dataUri: string, contentType?: string): Promise<string> {
|
|
239
|
+
const base64 = dataUri.split(",")[1];
|
|
240
|
+
const buffer = Buffer.from(base64, "base64");
|
|
241
|
+
|
|
242
|
+
// Replace with your storage provider (S3, R2, GCS, Convex, etc.)
|
|
243
|
+
// return await s3.upload(buffer, contentType);
|
|
244
|
+
throw new Error("Implement upload() with your storage provider");
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
Use the processor with your agent:
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
import { Agent } from "@mastra/core/agent";
|
|
253
|
+
import { Memory } from "@mastra/memory";
|
|
254
|
+
import { AttachmentUploader } from "./processors/attachment-uploader";
|
|
255
|
+
|
|
256
|
+
const agent = new Agent({
|
|
257
|
+
id: "my-agent",
|
|
258
|
+
memory: new Memory({ storage: yourStorage }),
|
|
259
|
+
inputProcessors: [new AttachmentUploader()],
|
|
260
|
+
});
|
|
261
|
+
```
|