@mastra/libsql 1.0.0-beta.12 → 1.0.0-beta.14

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 CHANGED
@@ -1,5 +1,109 @@
1
1
  # @mastra/libsql
2
2
 
3
+ ## 1.0.0-beta.14
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed duplicate spans migration issue across all storage backends. When upgrading from older versions, existing duplicate (traceId, spanId) combinations in the spans table could prevent the unique constraint from being created. The migration deduplicates spans before adding the constraint. ([#12073](https://github.com/mastra-ai/mastra/pull/12073))
8
+
9
+ **Deduplication rules (in priority order):**
10
+ 1. Keep completed spans (those with `endedAt` set) over incomplete spans
11
+ 2. Among spans with the same completion status, keep the one with the newest `updatedAt`
12
+ 3. Use `createdAt` as the final tiebreaker
13
+
14
+ **What changed:**
15
+ - Added `migrateSpans()` method to observability stores for manual migration
16
+ - Added `checkSpansMigrationStatus()` method to check if migration is needed
17
+ - All stores use optimized single-query deduplication to avoid memory issues on large tables
18
+
19
+ **Usage example:**
20
+
21
+ ```typescript
22
+ const observability = await storage.getStore('observability');
23
+ const status = await observability.checkSpansMigrationStatus();
24
+ if (status.needsMigration) {
25
+ const result = await observability.migrateSpans();
26
+ console.log(`Migration complete: ${result.duplicatesRemoved} duplicates removed`);
27
+ }
28
+ ```
29
+
30
+ Fixes #11840
31
+
32
+ - Renamed MastraStorage to MastraCompositeStore for better clarity. The old MastraStorage name remains available as a deprecated alias for backward compatibility, but will be removed in a future version. ([#12093](https://github.com/mastra-ai/mastra/pull/12093))
33
+
34
+ **Migration:**
35
+
36
+ Update your imports and usage:
37
+
38
+ ```typescript
39
+ // Before
40
+ import { MastraStorage } from '@mastra/core/storage';
41
+
42
+ const storage = new MastraStorage({
43
+ id: 'composite',
44
+ domains: { ... }
45
+ });
46
+
47
+ // After
48
+ import { MastraCompositeStore } from '@mastra/core/storage';
49
+
50
+ const storage = new MastraCompositeStore({
51
+ id: 'composite',
52
+ domains: { ... }
53
+ });
54
+ ```
55
+
56
+ The new name better reflects that this is a composite storage implementation that routes different domains (workflows, traces, messages) to different underlying stores, avoiding confusion with the general "Mastra Storage" concept.
57
+
58
+ - Updated dependencies [[`026b848`](https://github.com/mastra-ai/mastra/commit/026b8483fbf5b6d977be8f7e6aac8d15c75558ac), [`ffa553a`](https://github.com/mastra-ai/mastra/commit/ffa553a3edc1bd17d73669fba66d6b6f4ac10897)]:
59
+ - @mastra/core@1.0.0-beta.26
60
+
61
+ ## 1.0.0-beta.13
62
+
63
+ ### Patch Changes
64
+
65
+ - Added new `listThreads` method for flexible thread filtering across all storage adapters. ([#11832](https://github.com/mastra-ai/mastra/pull/11832))
66
+
67
+ **New Features**
68
+ - Filter threads by `resourceId`, `metadata`, or both (with AND logic for metadata key-value pairs)
69
+ - All filter parameters are optional, allowing you to list all threads or filter as needed
70
+ - Full pagination and sorting support
71
+
72
+ **Example Usage**
73
+
74
+ ```typescript
75
+ // List all threads
76
+ const allThreads = await memory.listThreads({});
77
+
78
+ // Filter by resourceId only
79
+ const userThreads = await memory.listThreads({
80
+ filter: { resourceId: 'user-123' },
81
+ });
82
+
83
+ // Filter by metadata only
84
+ const supportThreads = await memory.listThreads({
85
+ filter: { metadata: { category: 'support' } },
86
+ });
87
+
88
+ // Filter by both with pagination
89
+ const filteredThreads = await memory.listThreads({
90
+ filter: {
91
+ resourceId: 'user-123',
92
+ metadata: { priority: 'high', status: 'open' },
93
+ },
94
+ orderBy: { field: 'updatedAt', direction: 'DESC' },
95
+ page: 0,
96
+ perPage: 20,
97
+ });
98
+ ```
99
+
100
+ **Security Improvements**
101
+ - Added validation to prevent SQL injection via malicious metadata keys
102
+ - Added pagination parameter validation to prevent integer overflow attacks
103
+
104
+ - Updated dependencies [[`ed3e3dd`](https://github.com/mastra-ai/mastra/commit/ed3e3ddec69d564fe2b125e083437f76331f1283), [`6833c69`](https://github.com/mastra-ai/mastra/commit/6833c69607418d257750bbcdd84638993d343539), [`47b1c16`](https://github.com/mastra-ai/mastra/commit/47b1c16a01c7ffb6765fe1e499b49092f8b7eba3), [`3a76a80`](https://github.com/mastra-ai/mastra/commit/3a76a80284cb71a0faa975abb3d4b2a9631e60cd), [`8538a0d`](https://github.com/mastra-ai/mastra/commit/8538a0d232619bf55dad7ddc2a8b0ca77c679a87), [`9312dcd`](https://github.com/mastra-ai/mastra/commit/9312dcd1c6f5b321929e7d382e763d95fdc030f5)]:
105
+ - @mastra/core@1.0.0-beta.25
106
+
3
107
  ## 1.0.0-beta.12
4
108
 
5
109
  ### Minor Changes
@@ -36,4 +36,4 @@ docs/
36
36
  ## Version
37
37
 
38
38
  Package: @mastra/libsql
39
- Version: 1.0.0-beta.12
39
+ Version: 1.0.0-beta.14
@@ -5,7 +5,7 @@ description: Documentation for @mastra/libsql. Includes links to type definition
5
5
 
6
6
  # @mastra/libsql Documentation
7
7
 
8
- > **Version**: 1.0.0-beta.12
8
+ > **Version**: 1.0.0-beta.14
9
9
  > **Package**: @mastra/libsql
10
10
 
11
11
  ## Quick Navigation
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.0-beta.12",
2
+ "version": "1.0.0-beta.14",
3
3
  "package": "@mastra/libsql",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -90,6 +90,9 @@ export const memoryAgent = new Agent({
90
90
  });
91
91
  ```
92
92
 
93
+ > **Mastra Cloud Store limitation**
94
+ Agent-level storage is not supported when using [Mastra Cloud Store](https://mastra.ai/docs/v1/mastra-cloud/deployment#using-mastra-cloud-store). If you use Mastra Cloud Store, configure storage on the Mastra instance instead. This limitation does not apply if you bring your own database.
95
+
93
96
  ## Message history
94
97
 
95
98
  Include a `memory` object with both `resource` and `thread` to track message history during agent calls.
@@ -122,6 +125,9 @@ const response = await memoryAgent.generate("What's my favorite color?", {
122
125
  });
123
126
  ```
124
127
 
128
+ > **Note:**
129
+ Each thread has an owner (`resourceId`) that cannot be changed after creation. Avoid reusing the same thread ID for threads with different owners, as this will cause errors when querying.
130
+
125
131
  To learn more about memory see the [Memory](../memory/overview) documentation.
126
132
 
127
133
  ## Using `RequestContext`
@@ -64,6 +64,67 @@ const handleDecline = async () => {
64
64
  };
65
65
  ```
66
66
 
67
+ ## Tool approval with generate()
68
+
69
+ Tool approval also works with the `generate()` method for non-streaming use cases. When using `generate()` with `requireToolApproval: true`, the method returns immediately when a tool requires approval instead of executing it.
70
+
71
+ ### How it works
72
+
73
+ When a tool requires approval during a `generate()` call, the response includes:
74
+
75
+ - `finishReason: 'suspended'` - indicates the agent is waiting for approval
76
+ - `suspendPayload` - contains tool call details (`toolCallId`, `toolName`, `args`)
77
+ - `runId` - needed to approve or decline the tool call
78
+
79
+ ### Approving tool calls
80
+
81
+ To approve a tool call with `generate()`, use the `approveToolCallGenerate` method:
82
+
83
+ ```typescript
84
+ const output = await agent.generate("Find user John", {
85
+ requireToolApproval: true,
86
+ });
87
+
88
+ if (output.finishReason === "suspended") {
89
+ console.log("Tool requires approval:", output.suspendPayload.toolName);
90
+ console.log("Arguments:", output.suspendPayload.args);
91
+
92
+ // Approve the tool call and get the final result
93
+ const result = await agent.approveToolCallGenerate({
94
+ runId: output.runId,
95
+ toolCallId: output.suspendPayload.toolCallId,
96
+ });
97
+
98
+ console.log("Final result:", result.text);
99
+ }
100
+ ```
101
+
102
+ ### Declining tool calls
103
+
104
+ To decline a tool call, use the `declineToolCallGenerate` method:
105
+
106
+ ```typescript
107
+ if (output.finishReason === "suspended") {
108
+ const result = await agent.declineToolCallGenerate({
109
+ runId: output.runId,
110
+ toolCallId: output.suspendPayload.toolCallId,
111
+ });
112
+
113
+ // Agent will respond acknowledging the declined tool
114
+ console.log(result.text);
115
+ }
116
+ ```
117
+
118
+ ### Stream vs Generate comparison
119
+
120
+ | Aspect | `stream()` | `generate()` |
121
+ |--------|-----------|--------------|
122
+ | Response type | Streaming chunks | Complete response |
123
+ | Approval detection | `tool-call-approval` chunk | `finishReason: 'suspended'` |
124
+ | Approve method | `approveToolCall({ runId })` | `approveToolCallGenerate({ runId, toolCallId })` |
125
+ | Decline method | `declineToolCall({ runId })` | `declineToolCallGenerate({ runId, toolCallId })` |
126
+ | Result | Stream to iterate | Full output object |
127
+
67
128
  ## Tool-level approval
68
129
 
69
130
  There are two types of tool call approval. The first uses `requireApproval`, which is a property on the tool definition, while `requireToolApproval` is a parameter passed to `agent.stream()`. The second uses `suspend` and lets the agent provide context or confirmation prompts so the user can decide whether the tool call should continue.
@@ -63,17 +63,17 @@ This is useful when all primitives share the same storage backend and have simil
63
63
 
64
64
  #### Composite storage
65
65
 
66
- Add storage to your Mastra instance using `MastraStorage` and configure individual storage domains to use different storage providers.
66
+ Add storage to your Mastra instance using `MastraCompositeStore` and configure individual storage domains to use different storage providers.
67
67
 
68
68
  ```typescript title="src/mastra/index.ts"
69
69
  import { Mastra } from "@mastra/core";
70
- import { MastraStorage } from "@mastra/core/storage";
70
+ import { MastraCompositeStore } from "@mastra/core/storage";
71
71
  import { MemoryLibSQL } from "@mastra/libsql";
72
72
  import { WorkflowsPG } from "@mastra/pg";
73
73
  import { ObservabilityStorageClickhouse } from "@mastra/clickhouse";
74
74
 
75
75
  export const mastra = new Mastra({
76
- storage: new MastraStorage({
76
+ storage: new MastraCompositeStore({
77
77
  id: "composite",
78
78
  domains: {
79
79
  memory: new MemoryLibSQL({ url: "file:./memory.db" }),
@@ -115,6 +115,9 @@ export const agent = new Agent({
115
115
 
116
116
  This is useful when different agents need to store data in separate databases for security, compliance, or organizational reasons.
117
117
 
118
+ > **Mastra Cloud Store limitation**
119
+ Agent-level storage is not supported when using [Mastra Cloud Store](https://mastra.ai/docs/v1/mastra-cloud/deployment#using-mastra-cloud-store). If you use Mastra Cloud Store, configure storage on the Mastra instance instead. This limitation does not apply if you bring your own database.
120
+
118
121
  ## Threads and resources
119
122
 
120
123
  Mastra organizes memory into threads using two identifiers:
@@ -136,6 +139,21 @@ const stream = await agent.stream("message for agent", {
136
139
  > **Note:**
137
140
  [Studio](https://mastra.ai/docs/v1/getting-started/studio) automatically generates a thread and resource ID for you. Remember to to pass these explicitly when calling `stream` or `generate` yourself.
138
141
 
142
+ ### Thread and resource relationship
143
+
144
+ Each thread has an owner (its `resourceId`) that is set when the thread is created and cannot be changed. When you query a thread, you must use the correct owner's resource ID. Attempting to query a thread with a different resource ID will result in an error:
145
+
146
+ ```text
147
+ Thread with id <thread_id> is for resource with id <resource_a>
148
+ but resource <resource_b> was queried
149
+ ```
150
+
151
+ Note that while each thread has one owner, messages within that thread can have different `resourceId` values. This is used for message attribution and filtering (e.g., distinguishing between different agents in a multi-agent system, or filtering messages for analytics).
152
+
153
+ **Security:** Memory is a storage layer, not an authorization layer. Your application must implement access control before calling memory APIs. The `resourceId` parameter controls both validation and filtering - provide it to validate ownership and filter messages, or omit it for server-side access without validation.
154
+
155
+ To avoid accidentally reusing thread IDs across different owners, use UUIDs: `crypto.randomUUID()`
156
+
139
157
  ### Thread title generation
140
158
 
141
159
  Mastra can automatically generate descriptive thread titles based on the user's first message.
@@ -127,7 +127,7 @@ export const agent = new Agent({
127
127
  - [createThread](https://mastra.ai/reference/v1/memory/createThread)
128
128
  - [recall](https://mastra.ai/reference/v1/memory/recall)
129
129
  - [getThreadById](https://mastra.ai/reference/v1/memory/getThreadById)
130
- - [listThreadsByResourceId](https://mastra.ai/reference/v1/memory/listThreadsByResourceId)
130
+ - [listThreads](https://mastra.ai/reference/v1/memory/listThreads)
131
131
  - [deleteMessages](https://mastra.ai/reference/v1/memory/deleteMessages)
132
132
  - [cloneThread](https://mastra.ai/reference/v1/memory/cloneThread)
133
133
  - [Clone Utility Methods](https://mastra.ai/reference/v1/memory/clone-utilities)
@@ -9,11 +9,11 @@
9
9
 
10
10
  > Documentation for combining multiple storage backends in Mastra.
11
11
 
12
- `MastraStorage` can compose storage domains from different providers. Use it when you need different databases for different purposes. For example, use LibSQL for memory and PostgreSQL for workflows.
12
+ `MastraCompositeStore` can compose storage domains from different providers. Use it when you need different databases for different purposes. For example, use LibSQL for memory and PostgreSQL for workflows.
13
13
 
14
14
  ## Installation
15
15
 
16
- `MastraStorage` is included in `@mastra/core`:
16
+ `MastraCompositeStore` is included in `@mastra/core`:
17
17
 
18
18
  ```bash
19
19
  npm install @mastra/core@beta
@@ -44,13 +44,13 @@ Mastra organizes storage into five specialized domains, each handling a specific
44
44
  Import domain classes directly from each store package and compose them:
45
45
 
46
46
  ```typescript title="src/mastra/index.ts"
47
- import { MastraStorage } from "@mastra/core/storage";
47
+ import { MastraCompositeStore } from "@mastra/core/storage";
48
48
  import { WorkflowsPG, ScoresPG } from "@mastra/pg";
49
49
  import { MemoryLibSQL } from "@mastra/libsql";
50
50
  import { Mastra } from "@mastra/core";
51
51
 
52
52
  export const mastra = new Mastra({
53
- storage: new MastraStorage({
53
+ storage: new MastraCompositeStore({
54
54
  id: "composite",
55
55
  domains: {
56
56
  memory: new MemoryLibSQL({ url: "file:./local.db" }),
@@ -66,7 +66,7 @@ export const mastra = new Mastra({
66
66
  Use `default` to specify a fallback storage, then override specific domains:
67
67
 
68
68
  ```typescript title="src/mastra/index.ts"
69
- import { MastraStorage } from "@mastra/core/storage";
69
+ import { MastraCompositeStore } from "@mastra/core/storage";
70
70
  import { PostgresStore } from "@mastra/pg";
71
71
  import { MemoryLibSQL } from "@mastra/libsql";
72
72
  import { Mastra } from "@mastra/core";
@@ -77,7 +77,7 @@ const pgStore = new PostgresStore({
77
77
  });
78
78
 
79
79
  export const mastra = new Mastra({
80
- storage: new MastraStorage({
80
+ storage: new MastraCompositeStore({
81
81
  id: "composite",
82
82
  default: pgStore,
83
83
  domains: {
@@ -91,14 +91,14 @@ export const mastra = new Mastra({
91
91
 
92
92
  ## Initialization
93
93
 
94
- `MastraStorage` initializes each configured domain independently. When passed to the Mastra class, `init()` is called automatically:
94
+ `MastraCompositeStore` initializes each configured domain independently. When passed to the Mastra class, `init()` is called automatically:
95
95
 
96
96
  ```typescript title="src/mastra/index.ts"
97
- import { MastraStorage } from "@mastra/core/storage";
97
+ import { MastraCompositeStore } from "@mastra/core/storage";
98
98
  import { MemoryPG, WorkflowsPG, ScoresPG } from "@mastra/pg";
99
99
  import { Mastra } from "@mastra/core";
100
100
 
101
- const storage = new MastraStorage({
101
+ const storage = new MastraCompositeStore({
102
102
  id: "composite",
103
103
  domains: {
104
104
  memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
@@ -115,10 +115,10 @@ export const mastra = new Mastra({
115
115
  If using storage directly, call `init()` explicitly:
116
116
 
117
117
  ```typescript
118
- import { MastraStorage } from "@mastra/core/storage";
118
+ import { MastraCompositeStore } from "@mastra/core/storage";
119
119
  import { MemoryPG } from "@mastra/pg";
120
120
 
121
- const storage = new MastraStorage({
121
+ const storage = new MastraCompositeStore({
122
122
  id: "composite",
123
123
  domains: {
124
124
  memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
@@ -139,11 +139,11 @@ const thread = await memoryStore?.getThreadById({ threadId: "..." });
139
139
  Use a local database for development while keeping production data in a managed service:
140
140
 
141
141
  ```typescript
142
- import { MastraStorage } from "@mastra/core/storage";
142
+ import { MastraCompositeStore } from "@mastra/core/storage";
143
143
  import { MemoryPG, WorkflowsPG, ScoresPG } from "@mastra/pg";
144
144
  import { MemoryLibSQL } from "@mastra/libsql";
145
145
 
146
- const storage = new MastraStorage({
146
+ const storage = new MastraCompositeStore({
147
147
  id: "composite",
148
148
  domains: {
149
149
  // Use local SQLite for development, PostgreSQL for production
@@ -162,11 +162,11 @@ const storage = new MastraStorage({
162
162
  Use a time-series database for traces while keeping other data in PostgreSQL:
163
163
 
164
164
  ```typescript
165
- import { MastraStorage } from "@mastra/core/storage";
165
+ import { MastraCompositeStore } from "@mastra/core/storage";
166
166
  import { MemoryPG, WorkflowsPG, ScoresPG } from "@mastra/pg";
167
167
  import { ObservabilityStorageClickhouse } from "@mastra/clickhouse";
168
168
 
169
- const storage = new MastraStorage({
169
+ const storage = new MastraCompositeStore({
170
170
  id: "composite",
171
171
  domains: {
172
172
  memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),