@mastra/pg 1.20.0-alpha.3 → 1.20.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 +102 -0
- package/dist/docs/SKILL.md +9 -7
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-deployment-workers.md +1 -1
- package/dist/docs/references/docs-storage-overview.md +14 -13
- package/dist/docs/references/{reference-storage-dynamodb.md → integrations-databases-dynamodb.md} +1 -1
- package/dist/docs/references/integrations-databases-neon.md +220 -0
- package/dist/docs/references/{reference-storage-postgresql.md → integrations-databases-postgresql.md} +1 -1
- package/dist/docs/references/reference-rag-chunking-and-embedding.md +182 -0
- package/dist/docs/references/{guides-rag-overview.md → reference-rag-overview.md} +2 -2
- package/dist/docs/references/{guides-rag-retrieval.md → reference-rag-retrieval.md} +1 -1
- package/dist/docs/references/{guides-rag-vector-databases.md → reference-rag-vector-databases.md} +1 -1
- package/dist/docs/references/reference-storage-composite.md +4 -4
- package/dist/docs/references/reference-storage-retention.md +4 -4
- package/dist/index.cjs +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +1 -0
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,107 @@
|
|
|
1
1
|
# @mastra/pg
|
|
2
2
|
|
|
3
|
+
## 1.20.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Added experiment provenance and grouping support to the LibSQL, MongoDB, MySQL, PostgreSQL, and Spanner storage adapters. These fields remain available for later grouping and filtering. ([#20645](https://github.com/mastra-ai/mastra/pull/20645))
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { PostgresStore } from '@mastra/pg';
|
|
11
|
+
|
|
12
|
+
const storage = new PostgresStore({
|
|
13
|
+
id: 'postgres-storage',
|
|
14
|
+
connectionString: process.env.DATABASE_URL!,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
await dataset.startExperiment({
|
|
18
|
+
task,
|
|
19
|
+
scorers,
|
|
20
|
+
provenance: { source: 'github', sourceVersion: 'abc123' },
|
|
21
|
+
grouping: { experimentSetId: 'benchmark-1', variantId: 'candidate', trialIndex: 0 },
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
- Memory list reads now surface database errors instead of silently returning empty results. ([#17910](https://github.com/mastra-ai/mastra/pull/17910))
|
|
26
|
+
|
|
27
|
+
Previously, the paginated memory reads (`listThreads`, `listMessages`, `listMessagesByResourceId`, and `listMessagesById`) caught backend failures, logged them, and returned an empty payload like `{ threads: [], total: 0, hasMore: false }`. A transient outage (locked table, dropped connection) was therefore indistinguishable from a genuinely empty result, so an agent reading conversation history during a brief failure would treat it as "no history" and could overwrite real state. These methods now re-throw the failure as a `MastraError`. Validation (USER) errors and genuinely empty results are unchanged.
|
|
28
|
+
|
|
29
|
+
**Behavior change**
|
|
30
|
+
|
|
31
|
+
Callers that previously received an empty result on a backend failure will now receive a thrown `MastraError`. If you call these read methods directly (rather than through an agent, which already surfaces errors), wrap them so a transient outage doesn't crash the caller:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
try {
|
|
35
|
+
const { threads } = await storage.listThreads({ resourceId });
|
|
36
|
+
// ...use threads
|
|
37
|
+
} catch (error) {
|
|
38
|
+
// a real backend failure. Decide whether to retry, surface, or degrade.
|
|
39
|
+
// An empty thread list no longer hides here; it only means "no threads".
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Patch Changes
|
|
44
|
+
|
|
45
|
+
- Fixed `include` in `listMessages` and `listMessagesByResourceId` so it can no longer return a message that belongs to a different resource. When you pass a `resourceId`, the target message and its surrounding context messages now stay inside that resource. Includes that cross threads inside the same resource keep working, so semantic recall with `scope: 'resource'` is unchanged. ([#20984](https://github.com/mastra-ai/mastra/pull/20984))
|
|
46
|
+
|
|
47
|
+
**Behaviour change in the in-memory store**
|
|
48
|
+
|
|
49
|
+
The in-memory store read the context window from the thread you queried. It now reads the window from the thread that owns the target message, which is what the SQL stores already did. This only changes the result when an `include` entry names a message from another thread.
|
|
50
|
+
|
|
51
|
+
The in-memory store also ignored `include` in `listMessagesByResourceId`. It now returns the included messages, like `@mastra/libsql` and `@mastra/pg` do.
|
|
52
|
+
|
|
53
|
+
Fixes #20604.
|
|
54
|
+
|
|
55
|
+
- Improved chat response time with PostgresStore. Message history now reads a page of messages and its total count in one query instead of two. When semantic recall is on, the recall read also starts at the same time as the page read. Each agent turn therefore makes fewer database round-trips, which is most noticeable on remote Postgres. ([#20979](https://github.com/mastra-ai/mastra/pull/20979))
|
|
56
|
+
|
|
57
|
+
- Fixed PgVector top-level metadata equality filters so configured B-tree metadata indexes can match filtered vector queries. ([#20789](https://github.com/mastra-ai/mastra/pull/20789))
|
|
58
|
+
|
|
59
|
+
- Fixed a crash where updating a thread without a title (for example during observational memory buffering) could write a null title and violate the database's not-null constraint when running a newer @mastra/memory against an older storage package. Memory now checks whether the connected storage adapter supports partial thread updates and backfills the existing title for older adapters, so mixed-version deployments keep working. See #21041 for the original title-clobbering fix this makes backward compatible. ([#21257](https://github.com/mastra-ai/mastra/pull/21257))
|
|
60
|
+
|
|
61
|
+
- Storage adapters now declare support for partial thread updates, letting newer @mastra/memory preserve existing thread titles instead of overwriting them, while remaining safe against older versions. ([#21257](https://github.com/mastra-ai/mastra/pull/21257))
|
|
62
|
+
|
|
63
|
+
- Add a persistent `threadState` domain to `PostgresStore` ([#20994](https://github.com/mastra-ai/mastra/pull/20994))
|
|
64
|
+
|
|
65
|
+
`PostgresStore` did not register a `threadState` domain, so durable task and goal
|
|
66
|
+
state fell back to in-memory storage and was lost on restart. Anything relying on
|
|
67
|
+
thread state (task tools, long-running agent goals) could not be backed by Postgres
|
|
68
|
+
alone, and required composing a second adapter through `MastraCompositeStore`.
|
|
69
|
+
|
|
70
|
+
`ThreadStatePG` now stores state as JSONB keyed by `(threadId, type)` with an atomic
|
|
71
|
+
upsert that preserves `createdAt`, participates in `exportSchemas()` and `init()`, and
|
|
72
|
+
supports retention pruning anchored on `updatedAtZ` so state for still-active threads
|
|
73
|
+
survives age-based pruning.
|
|
74
|
+
|
|
75
|
+
- Fixed generated thread titles being clobbered during a turn ([#21041](https://github.com/mastra-ai/mastra/pull/21041))
|
|
76
|
+
|
|
77
|
+
`updateThread` required both `title` and `metadata`, so callers that only needed to
|
|
78
|
+
change metadata (message persistence, working memory, observational memory, channel
|
|
79
|
+
subscriptions) had to read the thread and pass its title back. When title generation
|
|
80
|
+
finished between that read and the write, the freshly generated title was overwritten
|
|
81
|
+
with the stale one.
|
|
82
|
+
|
|
83
|
+
`title` and `metadata` are now independently optional: omitting one leaves that column
|
|
84
|
+
untouched. Callers that only change metadata no longer send a title, and message
|
|
85
|
+
persistence no longer rewrites a thread row it just read.
|
|
86
|
+
|
|
87
|
+
- Fixed transaction completion when applications start several database operations at the same time. Pending operations now finish before the transaction completes or is cancelled, preventing query conflicts after batch failures and operations that application code does not await. ([#20869](https://github.com/mastra-ai/mastra/pull/20869))
|
|
88
|
+
|
|
89
|
+
- Fixed PostgreSQL memory timestamps to stay aligned across server timezones. ([#20831](https://github.com/mastra-ai/mastra/pull/20831))
|
|
90
|
+
|
|
91
|
+
- Updated dependencies [[`e7109ee`](https://github.com/mastra-ai/mastra/commit/e7109ee6f731bacc79c885906f3c7dca8d8f013a), [`b8ce7ec`](https://github.com/mastra-ai/mastra/commit/b8ce7ec96e39343c6c2f36d12d68a9ad816c09f7), [`2e4624e`](https://github.com/mastra-ai/mastra/commit/2e4624edb6917e61249cb60ee377735e7af7e4a9), [`45a9147`](https://github.com/mastra-ai/mastra/commit/45a914741f578754d79d8b7de7b4e4f304d8e14a), [`a3a3624`](https://github.com/mastra-ai/mastra/commit/a3a3624f646b98e409424d8defccbd334da9e8b8), [`6246914`](https://github.com/mastra-ai/mastra/commit/62469146636911f3cbbe0880bd011c6a897a59a7), [`6445eba`](https://github.com/mastra-ai/mastra/commit/6445eba6020abac681aba1cc9289f446cb400cbe), [`86b7b77`](https://github.com/mastra-ai/mastra/commit/86b7b777980d30f66e1fd134a37d2af4c22e54cc), [`1c75e32`](https://github.com/mastra-ai/mastra/commit/1c75e32f7fc0b9fb6f548b4407feaec8a1440212), [`296dc9a`](https://github.com/mastra-ai/mastra/commit/296dc9af29f3616e786c7825ec32e0df92d754c5), [`f59032a`](https://github.com/mastra-ai/mastra/commit/f59032a73699443555a08a479e7ac578975784f2), [`cdd5c33`](https://github.com/mastra-ai/mastra/commit/cdd5c33ac6c7118a9f139e6dc0e14e6a8ae31658), [`3f73c07`](https://github.com/mastra-ai/mastra/commit/3f73c076727e8c36b4fff7a1b40290fb68957fa8), [`772c0c8`](https://github.com/mastra-ai/mastra/commit/772c0c897cec383258de2e6178147f8014767c7b), [`d7cf7fa`](https://github.com/mastra-ai/mastra/commit/d7cf7fafc1ae1b50bd8462dd0e6c671a8606db93), [`7c1ebb1`](https://github.com/mastra-ai/mastra/commit/7c1ebb15690c4b3f0eabb19077cf8af573311e57), [`0f9a448`](https://github.com/mastra-ai/mastra/commit/0f9a448502157e59f7b76f24360ad497168f5ef8), [`578bf2e`](https://github.com/mastra-ai/mastra/commit/578bf2e6a88e9d5b8bf502204e15a95dfbb679ae), [`c47165c`](https://github.com/mastra-ai/mastra/commit/c47165c983c87594c6952f1fd2fa51a90205034c), [`289f4ce`](https://github.com/mastra-ai/mastra/commit/289f4ce16e3293370440172132c52ee787cbc09f), [`df31eb0`](https://github.com/mastra-ai/mastra/commit/df31eb0c7087d782a0d9346e467f9a4af4b0eef6), [`9571e3a`](https://github.com/mastra-ai/mastra/commit/9571e3a06ed2c5220196460bf82a2129255c3a8b), [`4f16ff8`](https://github.com/mastra-ai/mastra/commit/4f16ff824bf2f9b0ddc93f210477c10c8a4fb1ab), [`b4c89b4`](https://github.com/mastra-ai/mastra/commit/b4c89b4371b0c86da57403ad1a3b3ef0681f3128), [`e6534fa`](https://github.com/mastra-ai/mastra/commit/e6534fab031216f6cb48c4c9907cbfdce9d60bc6), [`210cb7a`](https://github.com/mastra-ai/mastra/commit/210cb7a167998c7bbf72cb3b93e6eb0563330239), [`06b2d87`](https://github.com/mastra-ai/mastra/commit/06b2d87e63bcdd0ed59215c6789692b9b12de376), [`1c67d85`](https://github.com/mastra-ai/mastra/commit/1c67d85e9da8285662f4dbbf47e0378c3fee0747), [`ac01d63`](https://github.com/mastra-ai/mastra/commit/ac01d6355974aec73fdb8781449ed12bac582094), [`80a3324`](https://github.com/mastra-ai/mastra/commit/80a33245d3110204de6f56d61211523ffe338692), [`e44e8f3`](https://github.com/mastra-ai/mastra/commit/e44e8f370b66c339ddcaba946d33da6d3c3f06cd), [`d9d2881`](https://github.com/mastra-ai/mastra/commit/d9d2881ede6dd6c023d144215fc812062aed0890), [`a810a05`](https://github.com/mastra-ai/mastra/commit/a810a058f62ad407cfc1701e0be36ae91145d7cf), [`ba24be6`](https://github.com/mastra-ai/mastra/commit/ba24be662439c331ab23a600041f93803c89eca8), [`842b5fe`](https://github.com/mastra-ai/mastra/commit/842b5fe22b6a7fa811bd14e48eb9af523ac989f2), [`990611b`](https://github.com/mastra-ai/mastra/commit/990611ba76eb876d86c9c594371ae5f02f94b432), [`80bdf3a`](https://github.com/mastra-ai/mastra/commit/80bdf3ae16ade6ff63bde0cb16fa2df8ab7dd4dd), [`c967a5e`](https://github.com/mastra-ai/mastra/commit/c967a5eec150c5dc5418c4a4388982d1fb7ad27c), [`dc4a25d`](https://github.com/mastra-ai/mastra/commit/dc4a25d41af4e2fe97a816070eaec6aa963ab53b), [`9ba1247`](https://github.com/mastra-ai/mastra/commit/9ba12470c77f1c03642d720ce67e517e878f666e), [`fd96298`](https://github.com/mastra-ai/mastra/commit/fd96298a8367622f4ebfcaa97b5b6c1fbbd14564), [`66bbfb5`](https://github.com/mastra-ai/mastra/commit/66bbfb5f05b473d39f88c0e4a481ccac41634f3a), [`dc4a25d`](https://github.com/mastra-ai/mastra/commit/dc4a25d41af4e2fe97a816070eaec6aa963ab53b), [`f8da216`](https://github.com/mastra-ai/mastra/commit/f8da21633e7eb0e31c9ce0fc30567870d19416d3), [`4a09a9c`](https://github.com/mastra-ai/mastra/commit/4a09a9c0474ef643558fcb5f0edc542b82f1cab0), [`5f798b3`](https://github.com/mastra-ai/mastra/commit/5f798b3362e9bdf4d690f85245606e146eef60b9), [`6a84954`](https://github.com/mastra-ai/mastra/commit/6a84954a2667f85b6d59da652dab1bbff007ccb0), [`1e83a47`](https://github.com/mastra-ai/mastra/commit/1e83a4734ab61ba5926af6793e3569a78b72ed37), [`52d8ef0`](https://github.com/mastra-ai/mastra/commit/52d8ef03801f1deb7ee48532fc4190dd4a33916c), [`cdd5c33`](https://github.com/mastra-ai/mastra/commit/cdd5c33ac6c7118a9f139e6dc0e14e6a8ae31658), [`7fdcaa6`](https://github.com/mastra-ai/mastra/commit/7fdcaa66105d64290f9b14432a12ec99f39c4d3a), [`d6c56f9`](https://github.com/mastra-ai/mastra/commit/d6c56f951db3213330b98b0abafa9778c8770e58), [`e08e789`](https://github.com/mastra-ai/mastra/commit/e08e789c1bf4cd2fe46363f7a4728536ceccc9bd), [`bf936e2`](https://github.com/mastra-ai/mastra/commit/bf936e2c89b2ff0dad5695b873ddc009ba96d41e), [`7fb580a`](https://github.com/mastra-ai/mastra/commit/7fb580ac73fbcacf2ff00872a3395f73ae1b9fa5), [`ed5d606`](https://github.com/mastra-ai/mastra/commit/ed5d606739c5e3fbdfa9f272df7809aa5ab43b1d), [`f53d5bd`](https://github.com/mastra-ai/mastra/commit/f53d5bd4885b29e4ac29a428a6044088ea8d6aa3), [`32980a3`](https://github.com/mastra-ai/mastra/commit/32980a3e2413d0274ac244d32c37d910edc13f00), [`01a2943`](https://github.com/mastra-ai/mastra/commit/01a2943a7d886edefdff072bfa51f055bab54437), [`82e3365`](https://github.com/mastra-ai/mastra/commit/82e3365ef7c9bf7bee2e7a7029035ea262d68895), [`6104347`](https://github.com/mastra-ai/mastra/commit/61043473ba6bfd0a25156824e853e13165562e6c), [`35cc901`](https://github.com/mastra-ai/mastra/commit/35cc90102cf834a84827acaf9eee0b6d6d1e2a3b), [`a8b4cf0`](https://github.com/mastra-ai/mastra/commit/a8b4cf02823cffebc4751a53337dfacf097c1ae1), [`9571e3a`](https://github.com/mastra-ai/mastra/commit/9571e3a06ed2c5220196460bf82a2129255c3a8b), [`333785c`](https://github.com/mastra-ai/mastra/commit/333785c93cbb01e42c60167e995457c28897ddbf), [`bda2235`](https://github.com/mastra-ai/mastra/commit/bda22353ee28f2df0eaea555f7cae1549f979c0b), [`efd5c81`](https://github.com/mastra-ai/mastra/commit/efd5c81cc25fde3c2ddd86fc1178deb4ec176e19), [`1b482c2`](https://github.com/mastra-ai/mastra/commit/1b482c2d89244dd758c41e5f927a2b44041388d2), [`45bfb88`](https://github.com/mastra-ai/mastra/commit/45bfb88fd52f1dd3be20e2a38905777c96499c90), [`ff28284`](https://github.com/mastra-ai/mastra/commit/ff2828416f14daff9d956e6a352fdaa23c950979), [`4bcdfaf`](https://github.com/mastra-ai/mastra/commit/4bcdfaf0eac3199d7cb171b0a19a92c9c341eea4), [`e3b9307`](https://github.com/mastra-ai/mastra/commit/e3b9307098daefbfae2a52ae2ef51bc9fc701190), [`d6834c5`](https://github.com/mastra-ai/mastra/commit/d6834c5a7866b16734d23900163c2414ed70d791), [`f33264f`](https://github.com/mastra-ai/mastra/commit/f33264f517ae603279afd5c4251e2b40f6dd3618), [`689f2c4`](https://github.com/mastra-ai/mastra/commit/689f2c4b6c0835fe455702b01d21daa8abcd9331), [`fcd0667`](https://github.com/mastra-ai/mastra/commit/fcd0667a4e378be35c9a1b1eb19cce78fbfd7282), [`cfd0d9e`](https://github.com/mastra-ai/mastra/commit/cfd0d9ec77ec3c69dd96f79cdb579e03d79f22ce), [`acc3513`](https://github.com/mastra-ai/mastra/commit/acc3513b19f79bf0a7ec2998694580edca54086c), [`1670533`](https://github.com/mastra-ai/mastra/commit/1670533986f6bacf567746245348125e3a106448), [`a7eb4a1`](https://github.com/mastra-ai/mastra/commit/a7eb4a11450f6170274ed5141bffe821d4fdd5a6), [`0976933`](https://github.com/mastra-ai/mastra/commit/0976933142333ec78451feef265b68bcb45aa5e7), [`242b945`](https://github.com/mastra-ai/mastra/commit/242b94558777bfbdeb42cbfea84afff0b6ad0633), [`c52d346`](https://github.com/mastra-ai/mastra/commit/c52d3462ec831a5d95926ecd3d3373f5928ad2e5), [`af4636a`](https://github.com/mastra-ai/mastra/commit/af4636a74463275d71c1d13a38f7d2b738f128bf), [`01a2943`](https://github.com/mastra-ai/mastra/commit/01a2943a7d886edefdff072bfa51f055bab54437), [`2eabc09`](https://github.com/mastra-ai/mastra/commit/2eabc097d86d52fbd0123da36a7c874154cc384f), [`0023e79`](https://github.com/mastra-ai/mastra/commit/0023e7919431078280abd11c89d1edeae35fcc69), [`c2ad51e`](https://github.com/mastra-ai/mastra/commit/c2ad51e2467f901eecba8c9f4a45e22a50bd7c18), [`25ca73d`](https://github.com/mastra-ai/mastra/commit/25ca73d25dee7ce9f0ca72939e3a505c4db7257e), [`2f9ef3f`](https://github.com/mastra-ai/mastra/commit/2f9ef3f4ca06fc2dcdd5088c26b7f4da6a016791), [`e7eefcb`](https://github.com/mastra-ai/mastra/commit/e7eefcb162cda7c493e8c3bf43050ead0efbcb2c), [`fea5cae`](https://github.com/mastra-ai/mastra/commit/fea5caedc7e2cfea51784a15e015952692027abf), [`4d7aca2`](https://github.com/mastra-ai/mastra/commit/4d7aca2fe75f225c83d1502d63079568e6ec163f), [`e1cead1`](https://github.com/mastra-ai/mastra/commit/e1cead17b5f3653cf00d2f90cc19b113119c02ba), [`01a2943`](https://github.com/mastra-ai/mastra/commit/01a2943a7d886edefdff072bfa51f055bab54437), [`d9d93b2`](https://github.com/mastra-ai/mastra/commit/d9d93b25e4a65ad5fa153fa35be7ed149c8d587f), [`c4ec889`](https://github.com/mastra-ai/mastra/commit/c4ec889561c0264c43f66d04d587bee4ce35e792), [`4b59f78`](https://github.com/mastra-ai/mastra/commit/4b59f786cbc9a7d1ef07a07517dbd4b96865e99d), [`eeae63e`](https://github.com/mastra-ai/mastra/commit/eeae63e7fbe8e1f237adc69bca6e2ac13c5ca907), [`3dc97ea`](https://github.com/mastra-ai/mastra/commit/3dc97ea415fad353b48a13095fad1835933cc12a), [`94e7ae9`](https://github.com/mastra-ai/mastra/commit/94e7ae970b37c888cd1244ef013292639a2fe6d1), [`e6a2860`](https://github.com/mastra-ai/mastra/commit/e6a2860649cc51f87d32d78b766ae2126446ba07), [`7010c5d`](https://github.com/mastra-ai/mastra/commit/7010c5d15728bf9c5dfe4fb6b1bf80ce23bf143a), [`bab06b1`](https://github.com/mastra-ai/mastra/commit/bab06b18923873a584bdfc71a6b4ec7fb4727fb7), [`3d01cd3`](https://github.com/mastra-ai/mastra/commit/3d01cd387321b6f9c5cac31d487c84bf51b19c78), [`7bf3086`](https://github.com/mastra-ai/mastra/commit/7bf308663f0115ca74ad20554ade740f06640859), [`4c186a0`](https://github.com/mastra-ai/mastra/commit/4c186a017275f45e6ed4c09de0f89550e2d09e8c), [`b0fa077`](https://github.com/mastra-ai/mastra/commit/b0fa077bcbc9b08551846fe372a0d3d15b71ed72), [`0282e16`](https://github.com/mastra-ai/mastra/commit/0282e16115538c8e9b248b90f0748eb01cb5dc98), [`a8dd139`](https://github.com/mastra-ai/mastra/commit/a8dd1391a9fe9a6632c25809ef236980afa9a020), [`6a667b4`](https://github.com/mastra-ai/mastra/commit/6a667b4b7cd6a93fe41fcdd357b08c5a8c09b9ab), [`9be8878`](https://github.com/mastra-ai/mastra/commit/9be8878dcf0388e84fc4873e0eec27bd49b881a4), [`e5786be`](https://github.com/mastra-ai/mastra/commit/e5786be02bb903073082bd9d6da880ebaacc343f), [`2440e09`](https://github.com/mastra-ai/mastra/commit/2440e096ea6c2def1ccc1eb2d0f3f5b88c4af940), [`2093fbd`](https://github.com/mastra-ai/mastra/commit/2093fbd53bb744bae19ec89f6d73db9a66fbe8a7), [`a59049b`](https://github.com/mastra-ai/mastra/commit/a59049b1652a13efff66ac826326b5ed9a550342), [`7bd85ea`](https://github.com/mastra-ai/mastra/commit/7bd85ea7588b71c25ce9f4019c88f8539be5dcbc), [`83fa004`](https://github.com/mastra-ai/mastra/commit/83fa0044bfda8b703a83883dbd8bef204844d13f), [`a463cdf`](https://github.com/mastra-ai/mastra/commit/a463cdf1c95c3059e70f0bff27959e8558bb899d), [`e7a5da4`](https://github.com/mastra-ai/mastra/commit/e7a5da4ef8e4dd452d2f232961b4e682a85ffe43), [`7b4393d`](https://github.com/mastra-ai/mastra/commit/7b4393d557411fdcf07b0e30e5acaf7cc85154ae), [`0ea6b80`](https://github.com/mastra-ai/mastra/commit/0ea6b8001408ce02b56e8be0536b0fd8cbaf8ad2)]:
|
|
92
|
+
- @mastra/core@1.58.0
|
|
93
|
+
|
|
94
|
+
## 1.20.0-alpha.4
|
|
95
|
+
|
|
96
|
+
### Patch Changes
|
|
97
|
+
|
|
98
|
+
- Fixed a crash where updating a thread without a title (for example during observational memory buffering) could write a null title and violate the database's not-null constraint when running a newer @mastra/memory against an older storage package. Memory now checks whether the connected storage adapter supports partial thread updates and backfills the existing title for older adapters, so mixed-version deployments keep working. See #21041 for the original title-clobbering fix this makes backward compatible. ([#21257](https://github.com/mastra-ai/mastra/pull/21257))
|
|
99
|
+
|
|
100
|
+
- Storage adapters now declare support for partial thread updates, letting newer @mastra/memory preserve existing thread titles instead of overwriting them, while remaining safe against older versions. ([#21257](https://github.com/mastra-ai/mastra/pull/21257))
|
|
101
|
+
|
|
102
|
+
- Updated dependencies [[`dc4a25d`](https://github.com/mastra-ai/mastra/commit/dc4a25d41af4e2fe97a816070eaec6aa963ab53b), [`dc4a25d`](https://github.com/mastra-ai/mastra/commit/dc4a25d41af4e2fe97a816070eaec6aa963ab53b)]:
|
|
103
|
+
- @mastra/core@1.58.0-alpha.15
|
|
104
|
+
|
|
3
105
|
## 1.20.0-alpha.3
|
|
4
106
|
|
|
5
107
|
### Patch Changes
|
package/dist/docs/SKILL.md
CHANGED
|
@@ -3,7 +3,7 @@ name: mastra-pg
|
|
|
3
3
|
description: Documentation for @mastra/pg. Use when working with @mastra/pg APIs, configuration, or implementation.
|
|
4
4
|
metadata:
|
|
5
5
|
package: "@mastra/pg"
|
|
6
|
-
version: "1.20.0
|
|
6
|
+
version: "1.20.0"
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
## When to use
|
|
@@ -21,11 +21,11 @@ Read the individual reference documents for detailed explanations and code examp
|
|
|
21
21
|
- [Working memory](references/docs-memory-working-memory.md) - Learn how to configure working memory in Mastra to store persistent user data, preferences.
|
|
22
22
|
- [Storage overview](references/docs-storage-overview.md) - Configure storage for Mastra to persist runtime state across agents, workflows, observability, evals, schedules, and memory.
|
|
23
23
|
|
|
24
|
-
###
|
|
24
|
+
### Integrations
|
|
25
25
|
|
|
26
|
-
- [
|
|
27
|
-
- [
|
|
28
|
-
- [
|
|
26
|
+
- [DynamoDB](references/integrations-databases-dynamodb.md) - Documentation for the DynamoDB storage implementation in Mastra, using a single-table design with ElectroDB.
|
|
27
|
+
- [Neon Postgres](references/integrations-databases-neon.md) - Use Neon Postgres as a Mastra storage and vector backend.
|
|
28
|
+
- [PostgreSQL](references/integrations-databases-postgresql.md) - Documentation for the PostgreSQL storage implementation in Mastra.
|
|
29
29
|
|
|
30
30
|
### Reference
|
|
31
31
|
|
|
@@ -33,10 +33,12 @@ Read the individual reference documents for detailed explanations and code examp
|
|
|
33
33
|
- [Reference: MessageHistory](references/reference-processors-message-history-processor.md) - Documentation for the MessageHistory processor in Mastra, which handles retrieval and persistence of conversation history.
|
|
34
34
|
- [Reference: SemanticRecall](references/reference-processors-semantic-recall-processor.md) - Documentation for the SemanticRecall processor in Mastra, which enables semantic search over conversation history using vector embeddings.
|
|
35
35
|
- [Reference: WorkingMemory](references/reference-processors-working-memory-processor.md) - Documentation for the WorkingMemory processor in Mastra, which injects persistent user/context data as system instructions.
|
|
36
|
+
- [Chunking and embedding documents](references/reference-rag-chunking-and-embedding.md) - Guide on chunking and embedding documents in Mastra for efficient processing and retrieval.
|
|
36
37
|
- [Reference: Metadata filters](references/reference-rag-metadata-filters.md) - Documentation for metadata filtering capabilities in Mastra, which allow for precise querying of vector search results across different vector stores.
|
|
38
|
+
- [RAG (Retrieval-Augmented Generation) in Mastra](references/reference-rag-overview.md) - Overview of Retrieval-Augmented Generation (RAG) in Mastra, detailing its capabilities for enhancing LLM outputs with relevant context.
|
|
39
|
+
- [Retrieval, semantic search, reranking](references/reference-rag-retrieval.md) - Guide on retrieval processes in Mastra's RAG systems, including semantic search, filtering, and re-ranking.
|
|
40
|
+
- [Storing embeddings in a vector database](references/reference-rag-vector-databases.md) - Guide on vector storage options in Mastra, including embedded and dedicated vector databases for similarity search.
|
|
37
41
|
- [Reference: Composite storage](references/reference-storage-composite.md) - Documentation for combining multiple storage backends in Mastra.
|
|
38
|
-
- [Reference: DynamoDB storage](references/reference-storage-dynamodb.md) - Documentation for the DynamoDB storage implementation in Mastra, using a single-table design with ElectroDB.
|
|
39
|
-
- [Reference: PostgreSQL storage](references/reference-storage-postgresql.md) - Documentation for the PostgreSQL storage implementation in Mastra.
|
|
40
42
|
- [Reference: Storage retention (prune)](references/reference-storage-retention.md) - API reference for retention policies and prune() on Mastra storage.
|
|
41
43
|
- [Reference: createVectorQueryTool()](references/reference-tools-vector-query-tool.md) - Documentation for the Vector Query Tool in Mastra, which facilitates semantic search over vector stores with filtering and reranking capabilities.
|
|
42
44
|
- [Reference: PG vector store](references/reference-vectors-pg.md) - Documentation for the PgVector class in Mastra, which provides vector search using PostgreSQL with pgvector extension.
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# Workers
|
|
4
4
|
|
|
5
|
-
> **Beta:**
|
|
5
|
+
> **Beta:** Breaking changes may occur without a major version bump until the API is stable. See [known limitations](#known-limitations) for current gaps.
|
|
6
6
|
|
|
7
7
|
Workers handle background processing outside the request-response cycle. Workflow step execution, cron-based scheduling, and long-running tool calls all run in workers, keeping the API responsive.
|
|
8
8
|
|
|
@@ -190,19 +190,20 @@ You can also route `observability` to a dedicated analytics backend. See the [ob
|
|
|
190
190
|
|
|
191
191
|
Each provider page includes installation instructions, configuration parameters, and usage examples:
|
|
192
192
|
|
|
193
|
-
- [
|
|
194
|
-
- [
|
|
195
|
-
- [
|
|
196
|
-
- [
|
|
197
|
-
- [
|
|
198
|
-
- [
|
|
199
|
-
- [
|
|
200
|
-
- [
|
|
201
|
-
- [
|
|
202
|
-
- [
|
|
203
|
-
- [
|
|
204
|
-
- [
|
|
205
|
-
- [
|
|
193
|
+
- [Cloudflare D1](https://mastra.ai/integrations/databases/cloudflare-d1)
|
|
194
|
+
- [Cloudflare KV & Durable Objects](https://mastra.ai/integrations/databases/cloudflare-kv)
|
|
195
|
+
- [Convex](https://mastra.ai/integrations/databases/convex)
|
|
196
|
+
- [DynamoDB](https://mastra.ai/integrations/databases/dynamodb)
|
|
197
|
+
- [Google Cloud Spanner](https://mastra.ai/integrations/databases/spanner)
|
|
198
|
+
- [LanceDB](https://mastra.ai/integrations/databases/lancedb)
|
|
199
|
+
- [libSQL](https://mastra.ai/integrations/databases/libsql)
|
|
200
|
+
- [Microsoft SQL Server](https://mastra.ai/integrations/databases/mssql)
|
|
201
|
+
- [MongoDB](https://mastra.ai/integrations/databases/mongodb)
|
|
202
|
+
- [Neon Postgres](https://mastra.ai/integrations/databases/neon)
|
|
203
|
+
- [OracleDB](https://mastra.ai/integrations/databases/oracledb)
|
|
204
|
+
- [PostgreSQL](https://mastra.ai/integrations/databases/postgresql)
|
|
205
|
+
- [Redis](https://mastra.ai/integrations/databases/redis)
|
|
206
|
+
- [Upstash](https://mastra.ai/integrations/databases/upstash)
|
|
206
207
|
|
|
207
208
|
> **Tip:** libSQL is the fastest path for local development because it doesn't require running a separate database server.
|
|
208
209
|
|
package/dist/docs/references/{reference-storage-dynamodb.md → integrations-databases-dynamodb.md}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
2
|
|
|
3
|
-
# DynamoDB
|
|
3
|
+
# DynamoDB
|
|
4
4
|
|
|
5
5
|
The DynamoDB storage implementation provides a high-capacity and performant NoSQL database solution for Mastra, using a single-table design pattern with [ElectroDB](https://electrodb.dev/).
|
|
6
6
|
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Neon Postgres
|
|
4
|
+
|
|
5
|
+
[Neon](https://neon.com) is a managed PostgreSQL service. Mastra connects to Neon through [`PostgresStore`](https://mastra.ai/integrations/databases/postgresql), which uses the Node.js `pg` driver. Neon doesn't require a separate Mastra storage package.
|
|
6
|
+
|
|
7
|
+
## Quickstart
|
|
8
|
+
|
|
9
|
+
Create a Neon project, then copy its [pooled connection string](https://neon.com/docs/connect/connect-from-any-app) into `DATABASE_URL`.
|
|
10
|
+
|
|
11
|
+
Install the PostgreSQL storage package:
|
|
12
|
+
|
|
13
|
+
**npm**:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @mastra/pg@latest
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**pnpm**:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pnpm add @mastra/pg@latest
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Yarn**:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
yarn add @mastra/pg@latest
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**Bun**:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
bun add @mastra/pg@latest
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Pass the pooled connection string to `PostgresStore`:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { Mastra } from '@mastra/core/mastra'
|
|
41
|
+
import { PostgresStore } from '@mastra/pg'
|
|
42
|
+
|
|
43
|
+
export const mastra = new Mastra({
|
|
44
|
+
storage: new PostgresStore({
|
|
45
|
+
id: 'neon-storage',
|
|
46
|
+
connectionString: process.env.DATABASE_URL!,
|
|
47
|
+
}),
|
|
48
|
+
})
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
On startup, Mastra calls `storage.init()` and creates its tables and indexes in Neon.
|
|
52
|
+
|
|
53
|
+
## Set up Neon with the CLI
|
|
54
|
+
|
|
55
|
+
The Neon CLI requires Node.js 20.19 or newer.
|
|
56
|
+
|
|
57
|
+
Install the [Neon CLI](https://neon.com/docs/cli/install):
|
|
58
|
+
|
|
59
|
+
**npm**:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
npm install -g neon
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
**pnpm**:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pnpm add -g neon
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Yarn**:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
yarn global add neon
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Bun**:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
bun add --global neon
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
From your project directory, link an existing Neon project or create one:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
neon link
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`neon link` signs you in and writes project context to `.neon`. It also pulls the current branch's environment variables. Run `neon env pull` again whenever you need to refresh them:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
neon env pull
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
The command writes `DATABASE_URL` and `DATABASE_URL_UNPOOLED` to `.env.local` or to `.env` when that file already exists.
|
|
96
|
+
|
|
97
|
+
### Add `neon.ts`
|
|
98
|
+
|
|
99
|
+
To manage branch settings in the repository, create a [`neon.ts`](https://neon.com/docs/reference/neon-ts) file:
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
neon config init
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
This installs `@neon/config` and `@neon/env` and creates a starter config. For example, the following policy expires new non-default branches after seven days:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { defineConfig } from '@neon/config/v1'
|
|
109
|
+
|
|
110
|
+
export default defineConfig({
|
|
111
|
+
branch: branch => {
|
|
112
|
+
if (branch.isDefault) {
|
|
113
|
+
return {}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (!branch.exists) {
|
|
117
|
+
return { ttl: '7d' }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return {}
|
|
121
|
+
},
|
|
122
|
+
})
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Apply the config:
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
neon deploy
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Install Neon agent skills
|
|
132
|
+
|
|
133
|
+
Install the [`neon` and `neon-postgres` agent skills](https://neon.com/docs/ai/agent-skills) from your project directory:
|
|
134
|
+
|
|
135
|
+
**npm**:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
npx skills add neondatabase/agent-skills --skill neon --skill neon-postgres -y
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
**pnpm**:
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
pnpm dlx skills add neondatabase/agent-skills --skill neon --skill neon-postgres -y
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
**Yarn**:
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
yarn dlx skills add neondatabase/agent-skills --skill neon --skill neon-postgres -y
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
**Bun**:
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
bun x skills add neondatabase/agent-skills --skill neon --skill neon-postgres -y
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
The skills give compatible coding agents Neon setup instructions and Postgres-specific guidance.
|
|
160
|
+
|
|
161
|
+
## Production connections
|
|
162
|
+
|
|
163
|
+
Neon provides direct and pooled connection strings:
|
|
164
|
+
|
|
165
|
+
- `DATABASE_URL` uses Neon's pooled endpoint. Use it for normal application traffic.
|
|
166
|
+
- `DATABASE_URL_UNPOOLED` connects directly to Postgres. Use it for administrative tasks or schema changes that run outside the application.
|
|
167
|
+
|
|
168
|
+
For most applications, use the pooled `DATABASE_URL` shown in the quickstart and let Mastra initialize the schema at startup. `PostgresStore` supports initialization through a transaction-pooled connection.
|
|
169
|
+
|
|
170
|
+
### Separate schema initialization
|
|
171
|
+
|
|
172
|
+
If the runtime database role shouldn't create tables or indexes, initialize the schema during deployment with the direct connection:
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
import { PostgresStore } from '@mastra/pg'
|
|
176
|
+
|
|
177
|
+
const storage = new PostgresStore({
|
|
178
|
+
id: 'neon-storage-init',
|
|
179
|
+
connectionString: process.env.DATABASE_URL_UNPOOLED!,
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
await storage.init()
|
|
184
|
+
} finally {
|
|
185
|
+
await storage.close()
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
After the schema exists, use the pooled connection at runtime and disable automatic initialization:
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
import { Mastra } from '@mastra/core/mastra'
|
|
193
|
+
import { PostgresStore } from '@mastra/pg'
|
|
194
|
+
|
|
195
|
+
export const mastra = new Mastra({
|
|
196
|
+
storage: new PostgresStore({
|
|
197
|
+
id: 'neon-storage',
|
|
198
|
+
connectionString: process.env.DATABASE_URL!,
|
|
199
|
+
disableInit: true,
|
|
200
|
+
}),
|
|
201
|
+
})
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
See [Neon connection pooling](https://neon.com/docs/connect/connection-pooling) for details.
|
|
205
|
+
|
|
206
|
+
## Vector search
|
|
207
|
+
|
|
208
|
+
Neon supports the [`pgvector` extension](https://neon.com/docs/extensions/pgvector). Use the direct `DATABASE_URL_UNPOOLED` connection while creating the extension and indexes. After setup, configure [`PgVector`](https://mastra.ai/reference/vectors/pg) with the pooled connection for runtime queries:
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
import { PgVector } from '@mastra/pg'
|
|
212
|
+
|
|
213
|
+
const vector = new PgVector({
|
|
214
|
+
id: 'neon-vector',
|
|
215
|
+
connectionString: process.env.DATABASE_URL!,
|
|
216
|
+
disableInit: true,
|
|
217
|
+
})
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
Setting `disableInit: true` prevents the runtime process from creating or changing the extension, tables and indexes.
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Chunking and embedding documents
|
|
4
|
+
|
|
5
|
+
Before processing, create a MDocument instance from your content. You can initialize it from different formats:
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { MDocument } from '@mastra/rag'
|
|
9
|
+
|
|
10
|
+
const doc = MDocument.fromText('Your plain text content...')
|
|
11
|
+
const docFromHTML = MDocument.fromHTML('<html>Your HTML content...</html>')
|
|
12
|
+
const docFromMarkdown = MDocument.fromMarkdown('# Your Markdown content...')
|
|
13
|
+
const docFromJSON = MDocument.fromJSON(`{ "key": "value" }`)
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Document processing
|
|
17
|
+
|
|
18
|
+
Use `chunk` to split documents into manageable pieces. Mastra supports multiple chunking strategies optimized for different document types:
|
|
19
|
+
|
|
20
|
+
- `recursive`: Smart splitting based on content structure
|
|
21
|
+
- `character`: Simple character-based splits
|
|
22
|
+
- `token`: Token-aware splitting
|
|
23
|
+
- `markdown`: Markdown-aware splitting
|
|
24
|
+
- `semantic-markdown`: Markdown splitting based on related header families
|
|
25
|
+
- `html`: HTML structure-aware splitting
|
|
26
|
+
- `json`: JSON structure-aware splitting
|
|
27
|
+
- `latex`: LaTeX structure-aware splitting
|
|
28
|
+
- `sentence`: Sentence-aware splitting
|
|
29
|
+
|
|
30
|
+
> **Note:** Each strategy accepts different parameters optimized for its chunking approach.
|
|
31
|
+
|
|
32
|
+
Here's an example of how to use the `recursive` strategy:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
const chunks = await doc.chunk({
|
|
36
|
+
strategy: 'recursive',
|
|
37
|
+
maxSize: 512,
|
|
38
|
+
overlap: 50,
|
|
39
|
+
separators: ['\n'],
|
|
40
|
+
extract: {
|
|
41
|
+
metadata: true, // Optionally extract metadata
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
For text where preserving sentence structure is important, here's an example of how to use the `sentence` strategy:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const chunks = await doc.chunk({
|
|
50
|
+
strategy: 'sentence',
|
|
51
|
+
maxSize: 450,
|
|
52
|
+
minSize: 50,
|
|
53
|
+
overlap: 0,
|
|
54
|
+
sentenceEnders: ['.'],
|
|
55
|
+
})
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
For markdown documents where preserving the semantic relationships between sections is important, here's an example of how to use the `semantic-markdown` strategy:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
const chunks = await doc.chunk({
|
|
62
|
+
strategy: 'semantic-markdown',
|
|
63
|
+
joinThreshold: 500,
|
|
64
|
+
modelName: 'gpt-5',
|
|
65
|
+
})
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
> **Note:** Metadata extraction may use LLM calls, so ensure your API key is set.
|
|
69
|
+
|
|
70
|
+
We go deeper into chunking strategies in our [`chunk()` reference documentation](https://mastra.ai/reference/rag/chunk).
|
|
71
|
+
|
|
72
|
+
## Embedding generation
|
|
73
|
+
|
|
74
|
+
Transform chunks into embeddings using your preferred provider. Mastra supports embedding models through the model router.
|
|
75
|
+
|
|
76
|
+
### Using the Model Router
|
|
77
|
+
|
|
78
|
+
The simplest way is to use Mastra's model router with `provider/model` strings:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
82
|
+
import { embedMany } from 'ai'
|
|
83
|
+
|
|
84
|
+
const { embeddings } = await embedMany({
|
|
85
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
86
|
+
values: chunks.map(chunk => chunk.text),
|
|
87
|
+
})
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Mastra supports OpenAI and Google embedding models. For a complete list of supported embedding models, see the [embeddings reference](https://mastra.ai/reference/rag/embeddings).
|
|
91
|
+
|
|
92
|
+
The model router automatically handles API key detection from environment variables.
|
|
93
|
+
|
|
94
|
+
The embedding functions return vectors, arrays of numbers representing the semantic meaning of your text, ready for similarity searches in your vector database.
|
|
95
|
+
|
|
96
|
+
### Configuring Embedding Dimensions
|
|
97
|
+
|
|
98
|
+
Embedding models typically output vectors with a fixed number of dimensions (e.g., 1536 for OpenAI's `text-embedding-3-small`). Some models support reducing this dimensionality, which can help:
|
|
99
|
+
|
|
100
|
+
- Decrease storage requirements in vector databases
|
|
101
|
+
- Reduce computational costs for similarity searches
|
|
102
|
+
|
|
103
|
+
Here are some supported models:
|
|
104
|
+
|
|
105
|
+
OpenAI (text-embedding-3 models):
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
109
|
+
|
|
110
|
+
const { embeddings } = await embedMany({
|
|
111
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
112
|
+
options: {
|
|
113
|
+
dimensions: 256, // Only supported in text-embedding-3 and later
|
|
114
|
+
},
|
|
115
|
+
values: chunks.map(chunk => chunk.text),
|
|
116
|
+
})
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Google (text-embedding-001):
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
const { embeddings } = await embedMany({
|
|
123
|
+
model: google('gemini-embedding-001', {
|
|
124
|
+
outputDimensionality: 256, // Truncates excessive values from the end
|
|
125
|
+
}),
|
|
126
|
+
values: chunks.map(chunk => chunk.text),
|
|
127
|
+
})
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
> **Vector Database Compatibility:** When storing embeddings, the vector database index must be configured to match the output size of your embedding model. If the dimensions don't match, you may get errors or data corruption.
|
|
131
|
+
|
|
132
|
+
## Example: Complete pipeline
|
|
133
|
+
|
|
134
|
+
Here's an example showing document processing, embedding generation, and vector storage:
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
138
|
+
import { PgVector } from '@mastra/pg'
|
|
139
|
+
import { MDocument } from '@mastra/rag'
|
|
140
|
+
import { embedMany } from 'ai'
|
|
141
|
+
|
|
142
|
+
const doc = MDocument.fromText(`
|
|
143
|
+
Climate change poses significant challenges to global agriculture.
|
|
144
|
+
Rising temperatures and changing precipitation patterns affect crop yields.
|
|
145
|
+
`)
|
|
146
|
+
|
|
147
|
+
const chunks = await doc.chunk({
|
|
148
|
+
strategy: 'recursive',
|
|
149
|
+
maxSize: 256,
|
|
150
|
+
overlap: 50,
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
const { embeddings } = await embedMany({
|
|
154
|
+
model: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
155
|
+
values: chunks.map(chunk => chunk.text),
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
const vectorStore = new PgVector({
|
|
159
|
+
id: 'pg-vector',
|
|
160
|
+
connectionString: process.env.POSTGRES_CONNECTION_STRING,
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
await vectorStore.createIndex({
|
|
164
|
+
indexName: 'embeddings',
|
|
165
|
+
dimension: 1536,
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
await vectorStore.upsert({
|
|
169
|
+
indexName: 'embeddings',
|
|
170
|
+
vectors: embeddings,
|
|
171
|
+
})
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
For more examples of different chunking strategies and embedding configurations, see:
|
|
175
|
+
|
|
176
|
+
- [Chunk Reference](https://mastra.ai/reference/rag/chunk)
|
|
177
|
+
- [Embeddings Reference](https://mastra.ai/reference/rag/embeddings)
|
|
178
|
+
|
|
179
|
+
For more details on vector databases and embeddings, see:
|
|
180
|
+
|
|
181
|
+
- [Vector Databases](https://mastra.ai/reference/rag/vector-databases)
|
|
182
|
+
- [Embedding API Reference](https://mastra.ai/reference/rag/embeddings)
|