@mastra/lance 0.0.0-add-libsql-changeset-20250910154739

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/README.md ADDED
@@ -0,0 +1,263 @@
1
+ # Mastra LanceDB Storage & Vector Usage Guide
2
+
3
+ This guide explains how to use LanceDB as both a storage backend and vector database with Mastra. LanceDB provides a high-performance, open-source, and embeddable vector database built on [Lance](https://github.com/eto-ai/lance) file format.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @mastra/lance @lancedb/lancedb apache-arrow
9
+ ```
10
+
11
+ ## Setup & Configuration
12
+
13
+ ### Basic Setup
14
+
15
+ ```typescript
16
+ import { LanceStorage } from '@mastra/lance';
17
+ import { Mastra } from '@mastra/core/mastra';
18
+
19
+ // Initialize LanceStorage
20
+ const storage = await LanceStorage.create(
21
+ 'myApp', // Name for your storage instance
22
+ 'path/to/db', // Path to database directory
23
+ );
24
+
25
+ // Configure Mastra with LanceStorage
26
+ const mastra = new Mastra({
27
+ storage: storage,
28
+ });
29
+ ```
30
+
31
+ ### Connection Options
32
+
33
+ LanceStorage supports multiple connection configurations:
34
+
35
+ ```typescript
36
+ // Local database
37
+ const localStore = await LanceStorage.create('myApp', '/path/to/db');
38
+
39
+ // LanceDB Cloud
40
+ const cloudStore = await LanceStorage.create('myApp', 'db://host:port');
41
+
42
+ // S3 bucket
43
+ const s3Store = await LanceStorage.create('myApp', 's3://bucket/db', { storageOptions: { timeout: '60s' } });
44
+ ```
45
+
46
+ ## Basic Operations
47
+
48
+ ### Creating Tables
49
+
50
+ ```typescript
51
+ import { TABLE_MESSAGES } from '@mastra/core/storage';
52
+ import type { StorageColumn } from '@mastra/core/storage';
53
+
54
+ // Define schema with appropriate types
55
+ const schema: Record<string, StorageColumn> = {
56
+ id: { type: 'uuid', nullable: false },
57
+ threadId: { type: 'uuid', nullable: false },
58
+ content: { type: 'text', nullable: true },
59
+ createdAt: { type: 'timestamp', nullable: false },
60
+ metadata: { type: 'jsonb', nullable: true },
61
+ };
62
+
63
+ // Create table
64
+ await storage.createTable({
65
+ tableName: TABLE_MESSAGES,
66
+ schema,
67
+ });
68
+ ```
69
+
70
+ ### Inserting Records
71
+
72
+ ```typescript
73
+ // Insert a single record
74
+ await storage.insert({
75
+ tableName: TABLE_MESSAGES,
76
+ record: {
77
+ id: '123e4567-e89b-12d3-a456-426614174000',
78
+ threadId: '123e4567-e89b-12d3-a456-426614174001',
79
+ content: 'Hello, world!',
80
+ createdAt: new Date(),
81
+ metadata: { tags: ['important', 'greeting'] },
82
+ },
83
+ });
84
+
85
+ // Batch insert multiple records
86
+ await storage.batchInsert({
87
+ tableName: TABLE_MESSAGES,
88
+ records: [
89
+ {
90
+ id: '123e4567-e89b-12d3-a456-426614174002',
91
+ threadId: '123e4567-e89b-12d3-a456-426614174001',
92
+ content: 'First message',
93
+ createdAt: new Date(),
94
+ metadata: { priority: 'high' },
95
+ },
96
+ {
97
+ id: '123e4567-e89b-12d3-a456-426614174003',
98
+ threadId: '123e4567-e89b-12d3-a456-426614174001',
99
+ content: 'Second message',
100
+ createdAt: new Date(),
101
+ metadata: { priority: 'low' },
102
+ },
103
+ ],
104
+ });
105
+ ```
106
+
107
+ ### Querying Data
108
+
109
+ ```typescript
110
+ // Load a record by id
111
+ const message = await storage.load({
112
+ tableName: TABLE_MESSAGES,
113
+ keys: { id: '123e4567-e89b-12d3-a456-426614174000' },
114
+ });
115
+
116
+ // Load messages from a thread
117
+ const messages = await storage.getMessages({
118
+ threadId: '123e4567-e89b-12d3-a456-426614174001',
119
+ });
120
+ ```
121
+
122
+ ## Working with Threads & Messages
123
+
124
+ ### Creating Threads
125
+
126
+ ```typescript
127
+ import type { StorageThreadType } from '@mastra/core/storage';
128
+
129
+ // Create a new thread
130
+ const thread: StorageThreadType = {
131
+ id: '123e4567-e89b-12d3-a456-426614174010',
132
+ resourceId: 'resource-123',
133
+ title: 'New Discussion',
134
+ createdAt: new Date(),
135
+ metadata: { topic: 'technical-support' },
136
+ };
137
+
138
+ // Save the thread
139
+ const savedThread = await storage.saveThread({ thread });
140
+ ```
141
+
142
+ ### Working with Messages
143
+
144
+ ```typescript
145
+ import type { MessageType } from '@mastra/core/memory';
146
+
147
+ // Create messages
148
+ const messages: MessageType[] = [
149
+ {
150
+ id: 'msg-001',
151
+ threadId: '123e4567-e89b-12d3-a456-426614174010',
152
+ resourceId: 'resource-123',
153
+ role: 'user',
154
+ content: 'How can I use LanceDB with Mastra?',
155
+ createdAt: new Date(),
156
+ type: 'text',
157
+ toolCallIds: [],
158
+ toolCallArgs: [],
159
+ toolNames: [],
160
+ },
161
+ {
162
+ id: 'msg-002',
163
+ threadId: '123e4567-e89b-12d3-a456-426614174010',
164
+ resourceId: 'resource-123',
165
+ role: 'assistant',
166
+ content: 'You can use LanceDB with Mastra by installing @mastra/lance package.',
167
+ createdAt: new Date(),
168
+ type: 'text',
169
+ toolCallIds: [],
170
+ toolCallArgs: [],
171
+ toolNames: [],
172
+ },
173
+ ];
174
+
175
+ // Save messages
176
+ await storage.saveMessages({ messages });
177
+
178
+ // Retrieve messages with context
179
+ const retrievedMessages = await storage.getMessages({
180
+ threadId: '123e4567-e89b-12d3-a456-426614174010',
181
+ selectBy: [
182
+ {
183
+ id: 'msg-001',
184
+ withPreviousMessages: 5, // Include up to 5 messages before this one
185
+ withNextMessages: 5, // Include up to 5 messages after this one
186
+ },
187
+ ],
188
+ });
189
+ ```
190
+
191
+ ## Working with Workflows
192
+
193
+ Mastra's workflow system uses LanceDB to persist workflow state for continuity across runs:
194
+
195
+ ```typescript
196
+ import type { WorkflowRunState } from '@mastra/core/storage';
197
+
198
+ // Persist a workflow snapshot
199
+ await storage.persistWorkflowSnapshot({
200
+ workflowName: 'documentProcessing',
201
+ runId: 'run-123',
202
+ snapshot: {
203
+ context: {
204
+ steps: {
205
+ step1: { status: 'success', payload: { data: 'processed' } },
206
+ step2: { status: 'waiting' },
207
+ },
208
+ triggerData: { documentId: 'doc-123' },
209
+ attempts: { step1: 1, step2: 0 },
210
+ },
211
+ } as WorkflowRunState,
212
+ });
213
+
214
+ // Load a workflow snapshot
215
+ const workflowState = await storage.loadWorkflowSnapshot({
216
+ workflowName: 'documentProcessing',
217
+ runId: 'run-123',
218
+ });
219
+ ```
220
+
221
+ ## Using Lance for Vector Storage
222
+
223
+ The LanceDB integration in Mastra can be used for both traditional storage and vector search:
224
+
225
+ ```typescript
226
+ // Create a schema with vector field
227
+ const vectorSchema: Record<string, StorageColumn> = {
228
+ id: { type: 'uuid', nullable: false },
229
+ content: { type: 'text', nullable: true },
230
+ embedding: { type: 'binary', nullable: false }, // Vector embedding
231
+ metadata: { type: 'jsonb', nullable: true },
232
+ };
233
+
234
+ // Create a vector table
235
+ await storage.createTable({
236
+ tableName: 'vector_store',
237
+ schema: vectorSchema,
238
+ });
239
+
240
+ // Insert a vector with content and metadata
241
+ await storage.insert({
242
+ tableName: 'vector_store',
243
+ record: {
244
+ id: 'vec-001',
245
+ content: 'This is a document about LanceDB and Mastra integration',
246
+ embedding: new Float32Array([0.1, 0.2, 0.3, 0.4]), // Your embedding vector
247
+ metadata: { source: 'documentation', category: 'integration' },
248
+ },
249
+ });
250
+ ```
251
+
252
+ ## Data Management
253
+
254
+ ```typescript
255
+ // Drop a table
256
+ await storage.dropTable(TABLE_MESSAGES);
257
+
258
+ // Clear all records from a table
259
+ await storage.clearTable({ tableName: TABLE_MESSAGES });
260
+
261
+ // Get table schema
262
+ const schema = await storage.getTableSchema(TABLE_MESSAGES);
263
+ ```