@mastra/pg 0.0.0-error-handler-fix-20251020202607 → 0.0.0-execa-dynamic-import-20260304221256
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 +3220 -3
- package/LICENSE.md +15 -0
- package/README.md +72 -20
- package/dist/docs/SKILL.md +40 -0
- package/dist/docs/assets/SOURCE_MAP.json +6 -0
- package/dist/docs/references/docs-memory-semantic-recall.md +288 -0
- package/dist/docs/references/docs-memory-storage.md +261 -0
- package/dist/docs/references/docs-memory-working-memory.md +400 -0
- package/dist/docs/references/docs-rag-overview.md +72 -0
- package/dist/docs/references/docs-rag-retrieval.md +515 -0
- package/dist/docs/references/docs-rag-vector-databases.md +645 -0
- package/dist/docs/references/reference-memory-memory-class.md +147 -0
- package/dist/docs/references/reference-processors-message-history-processor.md +85 -0
- package/dist/docs/references/reference-processors-semantic-recall-processor.md +117 -0
- package/dist/docs/references/reference-processors-working-memory-processor.md +152 -0
- package/dist/docs/references/reference-rag-metadata-filters.md +216 -0
- package/dist/docs/references/reference-storage-composite.md +235 -0
- package/dist/docs/references/reference-storage-dynamodb.md +282 -0
- package/dist/docs/references/reference-storage-postgresql.md +526 -0
- package/dist/docs/references/reference-tools-vector-query-tool.md +459 -0
- package/dist/docs/references/reference-vectors-pg.md +408 -0
- package/dist/index.cjs +11374 -2692
- 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 +11362 -2694
- package/dist/index.js.map +1 -1
- package/dist/shared/config.d.ts +122 -31
- 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/constraint-utils.d.ts +16 -0
- package/dist/storage/db/constraint-utils.d.ts.map +1 -0
- package/dist/storage/db/index.d.ts +235 -0
- package/dist/storage/db/index.d.ts.map +1 -0
- package/dist/storage/domains/agents/index.d.ts +52 -0
- package/dist/storage/domains/agents/index.d.ts.map +1 -0
- package/dist/storage/domains/blobs/index.d.ts +17 -0
- package/dist/storage/domains/blobs/index.d.ts.map +1 -0
- package/dist/storage/domains/datasets/index.d.ts +48 -0
- package/dist/storage/domains/datasets/index.d.ts.map +1 -0
- package/dist/storage/domains/experiments/index.d.ts +34 -0
- package/dist/storage/domains/experiments/index.d.ts.map +1 -0
- package/dist/storage/domains/mcp-clients/index.d.ts +33 -0
- package/dist/storage/domains/mcp-clients/index.d.ts.map +1 -0
- package/dist/storage/domains/mcp-servers/index.d.ts +33 -0
- package/dist/storage/domains/mcp-servers/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +74 -55
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/index.d.ts +65 -39
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/dist/storage/domains/prompt-blocks/index.d.ts +43 -0
- package/dist/storage/domains/prompt-blocks/index.d.ts.map +1 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts +43 -0
- package/dist/storage/domains/scorer-definitions/index.d.ts.map +1 -0
- package/dist/storage/domains/scores/index.d.ts +40 -33
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/skills/index.d.ts +33 -0
- package/dist/storage/domains/skills/index.d.ts.map +1 -0
- package/dist/storage/domains/utils.d.ts +1 -5
- package/dist/storage/domains/utils.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +42 -30
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/domains/workspaces/index.d.ts +33 -0
- package/dist/storage/domains/workspaces/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +66 -255
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/performance-indexes/performance-test.d.ts +3 -1
- package/dist/storage/performance-indexes/performance-test.d.ts.map +1 -1
- package/dist/storage/test-utils.d.ts.map +1 -1
- package/dist/vector/index.d.ts +45 -7
- package/dist/vector/index.d.ts.map +1 -1
- package/dist/vector/sql-builder.d.ts +4 -0
- package/dist/vector/sql-builder.d.ts.map +1 -1
- package/dist/vector/types.d.ts +10 -0
- package/dist/vector/types.d.ts.map +1 -1
- package/package.json +18 -15
- package/dist/storage/domains/legacy-evals/index.d.ts +0 -20
- package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
- package/dist/storage/domains/operations/index.d.ts +0 -118
- package/dist/storage/domains/operations/index.d.ts.map +0 -1
- package/dist/storage/domains/traces/index.d.ts +0 -23
- package/dist/storage/domains/traces/index.d.ts.map +0 -1
package/LICENSE.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
Portions of this software are licensed as follows:
|
|
2
|
+
|
|
3
|
+
- All content that resides under any directory named "ee/" within this
|
|
4
|
+
repository, including but not limited to:
|
|
5
|
+
- `packages/core/src/auth/ee/`
|
|
6
|
+
- `packages/server/src/server/auth/ee/`
|
|
7
|
+
is licensed under the license defined in `ee/LICENSE`.
|
|
8
|
+
|
|
9
|
+
- All third-party components incorporated into the Mastra Software are
|
|
10
|
+
licensed under the original license provided by the owner of the
|
|
11
|
+
applicable component.
|
|
12
|
+
|
|
13
|
+
- Content outside of the above-mentioned directories or restrictions is
|
|
14
|
+
available under the "Apache License 2.0" as defined below.
|
|
15
|
+
|
|
1
16
|
# Apache License 2.0
|
|
2
17
|
|
|
3
18
|
Copyright (c) 2025 Kepler Software, Inc.
|
package/README.md
CHANGED
|
@@ -115,26 +115,32 @@ const store = new PostgresStore({
|
|
|
115
115
|
|
|
116
116
|
// Create a thread
|
|
117
117
|
await store.saveThread({
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
118
|
+
thread: {
|
|
119
|
+
id: 'thread-123',
|
|
120
|
+
resourceId: 'resource-456',
|
|
121
|
+
title: 'My Thread',
|
|
122
|
+
metadata: { key: 'value' },
|
|
123
|
+
createdAt: new Date(),
|
|
124
|
+
},
|
|
122
125
|
});
|
|
123
126
|
|
|
124
127
|
// Add messages to thread
|
|
125
|
-
await store.saveMessages(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
128
|
+
await store.saveMessages({
|
|
129
|
+
messages: [
|
|
130
|
+
{
|
|
131
|
+
id: 'msg-789',
|
|
132
|
+
threadId: 'thread-123',
|
|
133
|
+
role: 'user',
|
|
134
|
+
content: { content: 'Hello' },
|
|
135
|
+
resourceId: 'resource-456',
|
|
136
|
+
createdAt: new Date(),
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
});
|
|
134
140
|
|
|
135
141
|
// Query threads and messages
|
|
136
|
-
const savedThread = await store.
|
|
137
|
-
const messages = await store.
|
|
142
|
+
const savedThread = await store.getThreadById({ threadId: 'thread-123' });
|
|
143
|
+
const messages = await store.listMessages({ threadId: 'thread-123' });
|
|
138
144
|
```
|
|
139
145
|
|
|
140
146
|
## Configuration
|
|
@@ -321,6 +327,9 @@ The system automatically detects configuration changes and only rebuilds indexes
|
|
|
321
327
|
- `buildIndex({indexName, metric?, indexConfig?})`: Build or rebuild vector index
|
|
322
328
|
- `upsert({indexName, vectors, metadata?, ids?})`: Add or update vectors
|
|
323
329
|
- `query({indexName, queryVector, topK?, filter?, includeVector?, minScore?})`: Search for similar vectors
|
|
330
|
+
- `updateVector({ indexName, id?, filter?, update })`: Update a single vector by ID or metadata filter
|
|
331
|
+
- `deleteVector({ indexName, id })`: Delete a single vector by ID
|
|
332
|
+
- `deleteVectors({ indexName, ids?, filter? })`: Delete multiple vectors by IDs or metadata filter
|
|
324
333
|
- `listIndexes()`: List all vector-enabled tables
|
|
325
334
|
- `describeIndex(indexName)`: Get table statistics and index configuration
|
|
326
335
|
- `deleteIndex(indexName)`: Delete a table
|
|
@@ -329,13 +338,56 @@ The system automatically detects configuration changes and only rebuilds indexes
|
|
|
329
338
|
|
|
330
339
|
## Storage Methods
|
|
331
340
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
- `
|
|
335
|
-
- `
|
|
336
|
-
- `
|
|
341
|
+
### Thread Operations
|
|
342
|
+
|
|
343
|
+
- `saveThread({ thread })`: Create or update a thread
|
|
344
|
+
- `getThreadById({ threadId })`: Get a thread by ID
|
|
345
|
+
- `updateThread({ id, title, metadata })`: Update thread title and/or metadata
|
|
346
|
+
- `deleteThread({ threadId })`: Delete a thread and its messages
|
|
347
|
+
- `listThreadsByResourceId({ resourceId, offset, limit, orderBy? })`: List paginated threads for a resource
|
|
348
|
+
|
|
349
|
+
### Message Operations
|
|
350
|
+
|
|
351
|
+
- `saveMessages({ messages })`: Save multiple messages in a transaction
|
|
352
|
+
- `listMessages({ threadId, resourceId?, perPage?, page?, orderBy?, filter? })`: Get messages for a thread with pagination
|
|
353
|
+
- `listMessagesById({ messageIds })`: Get specific messages by their IDs
|
|
354
|
+
- `updateMessages({ messages })`: Update existing messages
|
|
337
355
|
- `deleteMessages(messageIds)`: Delete specific messages
|
|
338
356
|
|
|
357
|
+
### Resource Operations
|
|
358
|
+
|
|
359
|
+
- `getResourceById({ resourceId })`: Get a resource by ID
|
|
360
|
+
- `saveResource({ resource })`: Create or save a resource
|
|
361
|
+
- `updateResource({ resourceId, workingMemory })`: Update resource working memory
|
|
362
|
+
|
|
363
|
+
### Workflow Operations
|
|
364
|
+
|
|
365
|
+
- `persistWorkflowSnapshot({ workflowName, runId, snapshot })`: Save workflow state
|
|
366
|
+
- `loadWorkflowSnapshot({ workflowName, runId })`: Load workflow state
|
|
367
|
+
- `listWorkflowRuns({ workflowName, pagination })`: List workflow runs with pagination
|
|
368
|
+
- `getWorkflowRunById({ workflowName, runId })`: Get a specific workflow run
|
|
369
|
+
- `updateWorkflowState({ workflowName, runId, state })`: Update workflow state
|
|
370
|
+
- `updateWorkflowResults({ workflowName, runId, results })`: Update workflow results
|
|
371
|
+
|
|
372
|
+
### AI Observability Operations
|
|
373
|
+
|
|
374
|
+
- `createSpan(span)`: Create a single AI span
|
|
375
|
+
- `batchCreateSpans({ records })`: Create multiple AI spans
|
|
376
|
+
- `updateSpan({ traceId, spanId, updates })`: Update an AI span
|
|
377
|
+
- `batchUpdateSpans({ updates })`: Update multiple AI spans
|
|
378
|
+
- `getTrace(traceId)`: Get an trace by ID
|
|
379
|
+
- `getTracesPaginated({ ...filters, pagination })`: Get paginated traces with filtering
|
|
380
|
+
- `batchDeleteTraces({ traceIds })`: Delete multiple traces
|
|
381
|
+
|
|
382
|
+
### Evaluation/Scoring Operations
|
|
383
|
+
|
|
384
|
+
- `getScoreById({ id })`: Get a score by ID
|
|
385
|
+
- `saveScore(score)`: Save an evaluation score
|
|
386
|
+
- `listScoresByScorerId({ scorerId, pagination })`: List scores by scorer with pagination
|
|
387
|
+
- `listScoresByRunId({ runId, pagination })`: List scores by run with pagination
|
|
388
|
+
- `listScoresByEntityId({ entityId, entityType, pagination })`: List scores by entity with pagination
|
|
389
|
+
- `listScoresBySpan({ traceId, spanId, pagination })`: List scores by span with pagination
|
|
390
|
+
|
|
339
391
|
## Index Management
|
|
340
392
|
|
|
341
393
|
The PostgreSQL store provides comprehensive index management capabilities to optimize query performance.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mastra-pg
|
|
3
|
+
description: Documentation for @mastra/pg. Use when working with @mastra/pg APIs, configuration, or implementation.
|
|
4
|
+
metadata:
|
|
5
|
+
package: "@mastra/pg"
|
|
6
|
+
version: "0.0.0-execa-dynamic-import-20260304221256"
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## When to use
|
|
10
|
+
|
|
11
|
+
Use this skill whenever you are working with @mastra/pg to obtain the domain-specific knowledge.
|
|
12
|
+
|
|
13
|
+
## How to use
|
|
14
|
+
|
|
15
|
+
Read the individual reference documents for detailed explanations and code examples.
|
|
16
|
+
|
|
17
|
+
### Docs
|
|
18
|
+
|
|
19
|
+
- [Semantic Recall](references/docs-memory-semantic-recall.md) - Learn how to use semantic recall in Mastra to retrieve relevant messages from past conversations using vector search and embeddings.
|
|
20
|
+
- [Storage](references/docs-memory-storage.md) - Configure storage for Mastra's memory system to persist conversations, workflows, and traces.
|
|
21
|
+
- [Working Memory](references/docs-memory-working-memory.md) - Learn how to configure working memory in Mastra to store persistent user data, preferences.
|
|
22
|
+
- [RAG (Retrieval-Augmented Generation) in Mastra](references/docs-rag-overview.md) - Overview of Retrieval-Augmented Generation (RAG) in Mastra, detailing its capabilities for enhancing LLM outputs with relevant context.
|
|
23
|
+
- [Retrieval, Semantic Search, Reranking](references/docs-rag-retrieval.md) - Guide on retrieval processes in Mastra's RAG systems, including semantic search, filtering, and re-ranking.
|
|
24
|
+
- [Storing Embeddings in A Vector Database](references/docs-rag-vector-databases.md) - Guide on vector storage options in Mastra, including embedded and dedicated vector databases for similarity search.
|
|
25
|
+
|
|
26
|
+
### Reference
|
|
27
|
+
|
|
28
|
+
- [Reference: Memory Class](references/reference-memory-memory-class.md) - Documentation for the `Memory` class in Mastra, which provides a robust system for managing conversation history and thread-based message storage.
|
|
29
|
+
- [Reference: Message History Processor](references/reference-processors-message-history-processor.md) - Documentation for the MessageHistory processor in Mastra, which handles retrieval and persistence of conversation history.
|
|
30
|
+
- [Reference: Semantic Recall Processor](references/reference-processors-semantic-recall-processor.md) - Documentation for the SemanticRecall processor in Mastra, which enables semantic search over conversation history using vector embeddings.
|
|
31
|
+
- [Reference: Working Memory Processor](references/reference-processors-working-memory-processor.md) - Documentation for the WorkingMemory processor in Mastra, which injects persistent user/context data as system instructions.
|
|
32
|
+
- [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.
|
|
33
|
+
- [Reference: Composite Storage](references/reference-storage-composite.md) - Documentation for combining multiple storage backends in Mastra.
|
|
34
|
+
- [Reference: DynamoDB Storage](references/reference-storage-dynamodb.md) - Documentation for the DynamoDB storage implementation in Mastra, using a single-table design with ElectroDB.
|
|
35
|
+
- [Reference: PostgreSQL Storage](references/reference-storage-postgresql.md) - Documentation for the PostgreSQL storage implementation in Mastra.
|
|
36
|
+
- [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.
|
|
37
|
+
- [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.
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
Read [assets/SOURCE_MAP.json](assets/SOURCE_MAP.json) for source code references.
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# Semantic Recall
|
|
2
|
+
|
|
3
|
+
If you ask your friend what they did last weekend, they will search in their memory for events associated with "last weekend" and then tell you what they did. That's sort of like how semantic recall works in Mastra.
|
|
4
|
+
|
|
5
|
+
> **Watch 📹:** What semantic recall is, how it works, and how to configure it in Mastra → [YouTube (5 minutes)](https://youtu.be/UVZtK8cK8xQ)
|
|
6
|
+
|
|
7
|
+
## How Semantic Recall Works
|
|
8
|
+
|
|
9
|
+
Semantic recall is RAG-based search that helps agents maintain context across longer interactions when messages are no longer within [recent message history](https://mastra.ai/docs/memory/message-history).
|
|
10
|
+
|
|
11
|
+
It uses vector embeddings of messages for similarity search, integrates with various vector stores, and has configurable context windows around retrieved messages.
|
|
12
|
+
|
|
13
|
+

|
|
14
|
+
|
|
15
|
+
When it's enabled, new messages are used to query a vector DB for semantically similar messages.
|
|
16
|
+
|
|
17
|
+
After getting a response from the LLM, all new messages (user, assistant, and tool calls/results) are inserted into the vector DB to be recalled in later interactions.
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
Semantic recall is enabled by default, so if you give your agent memory it will be included:
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { Agent } from '@mastra/core/agent'
|
|
25
|
+
import { Memory } from '@mastra/memory'
|
|
26
|
+
|
|
27
|
+
const agent = new Agent({
|
|
28
|
+
id: 'support-agent',
|
|
29
|
+
name: 'SupportAgent',
|
|
30
|
+
instructions: 'You are a helpful support agent.',
|
|
31
|
+
model: 'openai/gpt-5.1',
|
|
32
|
+
memory: new Memory(),
|
|
33
|
+
})
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Using the recall() Method
|
|
37
|
+
|
|
38
|
+
While `listMessages` retrieves messages by thread ID with basic pagination, [`recall()`](https://mastra.ai/reference/memory/recall) adds support for **semantic search**. When you need to find messages by meaning rather than just recency, use `recall()` with a `vectorSearchString`:
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
const memory = await agent.getMemory()
|
|
42
|
+
|
|
43
|
+
// Basic recall - similar to listMessages
|
|
44
|
+
const { messages } = await memory!.recall({
|
|
45
|
+
threadId: 'thread-123',
|
|
46
|
+
perPage: 50,
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
// Semantic recall - find messages by meaning
|
|
50
|
+
const { messages: relevantMessages } = await memory!.recall({
|
|
51
|
+
threadId: 'thread-123',
|
|
52
|
+
vectorSearchString: 'What did we discuss about the project deadline?',
|
|
53
|
+
threadConfig: {
|
|
54
|
+
semanticRecall: true,
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Storage configuration
|
|
60
|
+
|
|
61
|
+
Semantic recall relies on a [storage and vector db](https://mastra.ai/reference/memory/memory-class) to store messages and their embeddings.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { Memory } from '@mastra/memory'
|
|
65
|
+
import { Agent } from '@mastra/core/agent'
|
|
66
|
+
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
|
|
67
|
+
|
|
68
|
+
const agent = new Agent({
|
|
69
|
+
memory: new Memory({
|
|
70
|
+
// this is the default storage db if omitted
|
|
71
|
+
storage: new LibSQLStore({
|
|
72
|
+
id: 'agent-storage',
|
|
73
|
+
url: 'file:./local.db',
|
|
74
|
+
}),
|
|
75
|
+
// this is the default vector db if omitted
|
|
76
|
+
vector: new LibSQLVector({
|
|
77
|
+
id: 'agent-vector',
|
|
78
|
+
url: 'file:./local.db',
|
|
79
|
+
}),
|
|
80
|
+
}),
|
|
81
|
+
})
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Each vector store page below includes installation instructions, configuration parameters, and usage examples:
|
|
85
|
+
|
|
86
|
+
- [Astra](https://mastra.ai/reference/vectors/astra)
|
|
87
|
+
- [Chroma](https://mastra.ai/reference/vectors/chroma)
|
|
88
|
+
- [Cloudflare Vectorize](https://mastra.ai/reference/vectors/vectorize)
|
|
89
|
+
- [Convex](https://mastra.ai/reference/vectors/convex)
|
|
90
|
+
- [Couchbase](https://mastra.ai/reference/vectors/couchbase)
|
|
91
|
+
- [DuckDB](https://mastra.ai/reference/vectors/duckdb)
|
|
92
|
+
- [Elasticsearch](https://mastra.ai/reference/vectors/elasticsearch)
|
|
93
|
+
- [LanceDB](https://mastra.ai/reference/vectors/lance)
|
|
94
|
+
- [libSQL](https://mastra.ai/reference/vectors/libsql)
|
|
95
|
+
- [MongoDB](https://mastra.ai/reference/vectors/mongodb)
|
|
96
|
+
- [OpenSearch](https://mastra.ai/reference/vectors/opensearch)
|
|
97
|
+
- [Pinecone](https://mastra.ai/reference/vectors/pinecone)
|
|
98
|
+
- [PostgreSQL](https://mastra.ai/reference/vectors/pg)
|
|
99
|
+
- [Qdrant](https://mastra.ai/reference/vectors/qdrant)
|
|
100
|
+
- [S3 Vectors](https://mastra.ai/reference/vectors/s3vectors)
|
|
101
|
+
- [Turbopuffer](https://mastra.ai/reference/vectors/turbopuffer)
|
|
102
|
+
- [Upstash](https://mastra.ai/reference/vectors/upstash)
|
|
103
|
+
|
|
104
|
+
## Recall configuration
|
|
105
|
+
|
|
106
|
+
The three main parameters that control semantic recall behavior are:
|
|
107
|
+
|
|
108
|
+
1. **topK**: How many semantically similar messages to retrieve
|
|
109
|
+
2. **messageRange**: How much surrounding context to include with each match
|
|
110
|
+
3. **scope**: Whether to search within the current thread or across all threads owned by a resource (the default is resource scope).
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
const agent = new Agent({
|
|
114
|
+
memory: new Memory({
|
|
115
|
+
options: {
|
|
116
|
+
semanticRecall: {
|
|
117
|
+
topK: 3, // Retrieve 3 most similar messages
|
|
118
|
+
messageRange: 2, // Include 2 messages before and after each match
|
|
119
|
+
scope: 'resource', // Search across all threads for this user (default setting if omitted)
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
}),
|
|
123
|
+
})
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Embedder configuration
|
|
127
|
+
|
|
128
|
+
Semantic recall relies on an [embedding model](https://mastra.ai/reference/memory/memory-class) to convert messages into embeddings. Mastra supports embedding models through the model router using `provider/model` strings, or you can use any [embedding model](https://sdk.vercel.ai/docs/ai-sdk-core/embeddings) compatible with the AI SDK.
|
|
129
|
+
|
|
130
|
+
### Using the Model Router (Recommended)
|
|
131
|
+
|
|
132
|
+
The simplest way is to use a `provider/model` string with autocomplete support:
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
import { Memory } from '@mastra/memory'
|
|
136
|
+
import { Agent } from '@mastra/core/agent'
|
|
137
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
138
|
+
|
|
139
|
+
const agent = new Agent({
|
|
140
|
+
memory: new Memory({
|
|
141
|
+
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
142
|
+
}),
|
|
143
|
+
})
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Supported embedding models:
|
|
147
|
+
|
|
148
|
+
- **OpenAI**: `text-embedding-3-small`, `text-embedding-3-large`, `text-embedding-ada-002`
|
|
149
|
+
- **Google**: `gemini-embedding-001`
|
|
150
|
+
- **OpenRouter**: Access embedding models from various providers
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
import { Agent } from '@mastra/core/agent'
|
|
154
|
+
import { Memory } from '@mastra/memory'
|
|
155
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
156
|
+
|
|
157
|
+
const agent = new Agent({
|
|
158
|
+
memory: new Memory({
|
|
159
|
+
embedder: new ModelRouterEmbeddingModel({
|
|
160
|
+
providerId: 'openrouter',
|
|
161
|
+
modelId: 'openai/text-embedding-3-small',
|
|
162
|
+
}),
|
|
163
|
+
}),
|
|
164
|
+
})
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
The model router automatically handles API key detection from environment variables (`OPENAI_API_KEY`, `GOOGLE_GENERATIVE_AI_API_KEY`, `OPENROUTER_API_KEY`).
|
|
168
|
+
|
|
169
|
+
### Using AI SDK Packages
|
|
170
|
+
|
|
171
|
+
You can also use AI SDK embedding models directly:
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
import { Memory } from '@mastra/memory'
|
|
175
|
+
import { Agent } from '@mastra/core/agent'
|
|
176
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
177
|
+
|
|
178
|
+
const agent = new Agent({
|
|
179
|
+
memory: new Memory({
|
|
180
|
+
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
181
|
+
}),
|
|
182
|
+
})
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Using FastEmbed (Local)
|
|
186
|
+
|
|
187
|
+
To use FastEmbed (a local embedding model), install `@mastra/fastembed`:
|
|
188
|
+
|
|
189
|
+
**npm**:
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
npm install @mastra/fastembed@latest
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
**pnpm**:
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
pnpm add @mastra/fastembed@latest
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
**Yarn**:
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
yarn add @mastra/fastembed@latest
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
**Bun**:
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
bun add @mastra/fastembed@latest
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Then configure it in your memory:
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { Memory } from '@mastra/memory'
|
|
217
|
+
import { Agent } from '@mastra/core/agent'
|
|
218
|
+
import { fastembed } from '@mastra/fastembed'
|
|
219
|
+
|
|
220
|
+
const agent = new Agent({
|
|
221
|
+
memory: new Memory({
|
|
222
|
+
embedder: fastembed,
|
|
223
|
+
}),
|
|
224
|
+
})
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## PostgreSQL Index Optimization
|
|
228
|
+
|
|
229
|
+
When using PostgreSQL as your vector store, you can optimize semantic recall performance by configuring the vector index. This is particularly important for large-scale deployments with thousands of messages.
|
|
230
|
+
|
|
231
|
+
PostgreSQL supports both IVFFlat and HNSW indexes. By default, Mastra creates an IVFFlat index, but HNSW indexes typically provide better performance, especially with OpenAI embeddings which use inner product distance.
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
import { Memory } from '@mastra/memory'
|
|
235
|
+
import { PgStore, PgVector } from '@mastra/pg'
|
|
236
|
+
|
|
237
|
+
const agent = new Agent({
|
|
238
|
+
memory: new Memory({
|
|
239
|
+
storage: new PgStore({
|
|
240
|
+
id: 'agent-storage',
|
|
241
|
+
connectionString: process.env.DATABASE_URL,
|
|
242
|
+
}),
|
|
243
|
+
vector: new PgVector({
|
|
244
|
+
id: 'agent-vector',
|
|
245
|
+
connectionString: process.env.DATABASE_URL,
|
|
246
|
+
}),
|
|
247
|
+
options: {
|
|
248
|
+
semanticRecall: {
|
|
249
|
+
topK: 5,
|
|
250
|
+
messageRange: 2,
|
|
251
|
+
indexConfig: {
|
|
252
|
+
type: 'hnsw', // Use HNSW for better performance
|
|
253
|
+
metric: 'dotproduct', // Best for OpenAI embeddings
|
|
254
|
+
m: 16, // Number of bi-directional links (default: 16)
|
|
255
|
+
efConstruction: 64, // Size of candidate list during construction (default: 64)
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
}),
|
|
260
|
+
})
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
For detailed information about index configuration options and performance tuning, see the [PgVector configuration guide](https://mastra.ai/reference/vectors/pg).
|
|
264
|
+
|
|
265
|
+
## Disabling
|
|
266
|
+
|
|
267
|
+
There is a performance impact to using semantic recall. New messages are converted into embeddings and used to query a vector database before new messages are sent to the LLM.
|
|
268
|
+
|
|
269
|
+
Semantic recall is enabled by default but can be disabled when not needed:
|
|
270
|
+
|
|
271
|
+
```typescript
|
|
272
|
+
const agent = new Agent({
|
|
273
|
+
memory: new Memory({
|
|
274
|
+
options: {
|
|
275
|
+
semanticRecall: false,
|
|
276
|
+
},
|
|
277
|
+
}),
|
|
278
|
+
})
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
You might want to disable semantic recall in scenarios like:
|
|
282
|
+
|
|
283
|
+
- When message history provides sufficient context for the current conversation.
|
|
284
|
+
- In performance-sensitive applications, like realtime two-way audio, where the added latency of creating embeddings and running vector queries is noticeable.
|
|
285
|
+
|
|
286
|
+
## Viewing Recalled Messages
|
|
287
|
+
|
|
288
|
+
When tracing is enabled, any messages retrieved via semantic recall will appear in the agent's trace output, alongside recent message history (if configured).
|