@mastra/cloudflare-d1 1.1.1 → 1.2.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/reference-storage-cloudflare-d1.md +1 -1
- package/dist/index.cjs +46 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +47 -3
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/package.json +15 -14
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
# @mastra/cloudflare-d1
|
|
2
2
|
|
|
3
|
+
## 1.2.0-alpha.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Added exact metadata filtering to message history queries across Memory APIs and supported storage providers. ([#19991](https://github.com/mastra-ai/mastra/pull/19991))
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const messages = await memory.recall({
|
|
11
|
+
threadId: 'thread-1',
|
|
12
|
+
filter: {
|
|
13
|
+
metadata: {
|
|
14
|
+
status: 'done',
|
|
15
|
+
priority: 'high',
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Multiple fields use AND semantics. Supported values are strings, finite numbers, booleans, and `null`.
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- Updated dependencies [[`0dca9d0`](https://github.com/mastra-ai/mastra/commit/0dca9d0b1356024a53b72ea6f040db528b126caa)]:
|
|
26
|
+
- @mastra/core@1.54.0-alpha.0
|
|
27
|
+
|
|
3
28
|
## 1.1.1
|
|
4
29
|
|
|
5
30
|
### Patch Changes
|
package/dist/docs/SKILL.md
CHANGED
|
@@ -3,7 +3,7 @@ name: mastra-cloudflare-d1
|
|
|
3
3
|
description: Documentation for @mastra/cloudflare-d1. Use when working with @mastra/cloudflare-d1 APIs, configuration, or implementation.
|
|
4
4
|
metadata:
|
|
5
5
|
package: "@mastra/cloudflare-d1"
|
|
6
|
-
version: "1.
|
|
6
|
+
version: "1.2.0-alpha.0"
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
## When to use
|
|
@@ -6,7 +6,7 @@ The Cloudflare D1 storage implementation provides a serverless SQL database solu
|
|
|
6
6
|
|
|
7
7
|
> **Observability Not Supported:** Cloudflare D1 storage **doesn't support the observability domain**. Traces from the `MastraStorageExporter` can't be persisted to D1, and [Studio's](https://mastra.ai/docs/studio/overview) observability features won't work with D1 as your only storage provider. To enable observability, use [composite storage](https://mastra.ai/reference/storage/composite) to route observability data to a supported provider like ClickHouse.
|
|
8
8
|
|
|
9
|
-
> **Row Size Limit:** Cloudflare D1 enforces a **1 MiB maximum row size**. This limit can be exceeded when storing messages with base64-encoded attachments such as images. See [Handling large attachments](https://mastra.ai/docs/memory/
|
|
9
|
+
> **Row Size Limit:** Cloudflare D1 enforces a **1 MiB maximum row size**. This limit can be exceeded when storing messages with base64-encoded attachments such as images. See [Handling large attachments](https://mastra.ai/docs/memory/memory-processors) for workarounds including uploading attachments to external storage.
|
|
10
10
|
|
|
11
11
|
## Installation
|
|
12
12
|
|
package/dist/index.cjs
CHANGED
|
@@ -997,6 +997,40 @@ var BackgroundTasksStorageD1 = class extends storage.BackgroundTasksStorage {
|
|
|
997
997
|
return Number(row?.count ?? 0);
|
|
998
998
|
}
|
|
999
999
|
};
|
|
1000
|
+
function addSqliteMetadataFilter(conditions, params, metadataFilter) {
|
|
1001
|
+
if (!metadataFilter) return;
|
|
1002
|
+
for (const [key, value] of Object.entries(metadataFilter)) {
|
|
1003
|
+
const path = `$.metadata.${key}`;
|
|
1004
|
+
conditions.push(`CASE WHEN json_valid(content) THEN json_type(content, ?) IS NOT NULL ELSE 0 END`);
|
|
1005
|
+
params.push(path);
|
|
1006
|
+
addSqliteMetadataValuePredicate(conditions, params, path, value);
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
function addSqliteMetadataValuePredicate(conditions, params, path, value) {
|
|
1010
|
+
if (value === null) {
|
|
1011
|
+
conditions.push(`CASE WHEN json_valid(content) THEN json_type(content, ?) = 'null' ELSE 0 END`);
|
|
1012
|
+
params.push(path);
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
if (typeof value === "string") {
|
|
1016
|
+
conditions.push(
|
|
1017
|
+
`CASE WHEN json_valid(content) THEN json_type(content, ?) = 'text' AND json_extract(content, ?) = ? ELSE 0 END`
|
|
1018
|
+
);
|
|
1019
|
+
params.push(path, path, value);
|
|
1020
|
+
return;
|
|
1021
|
+
}
|
|
1022
|
+
if (typeof value === "number") {
|
|
1023
|
+
conditions.push(
|
|
1024
|
+
`CASE WHEN json_valid(content) THEN json_type(content, ?) IN ('integer', 'real') AND json_extract(content, ?) = ? ELSE 0 END`
|
|
1025
|
+
);
|
|
1026
|
+
params.push(path, path, value);
|
|
1027
|
+
return;
|
|
1028
|
+
}
|
|
1029
|
+
conditions.push(
|
|
1030
|
+
`CASE WHEN json_valid(content) THEN json_type(content, ?) = ? AND json_extract(content, ?) = ? ELSE 0 END`
|
|
1031
|
+
);
|
|
1032
|
+
params.push(path, value ? "true" : "false", path, value ? 1 : 0);
|
|
1033
|
+
}
|
|
1000
1034
|
var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
1001
1035
|
#db;
|
|
1002
1036
|
constructor(config) {
|
|
@@ -1602,6 +1636,7 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
1602
1636
|
}
|
|
1603
1637
|
const perPage = storage.normalizePerPage(perPageInput, 40);
|
|
1604
1638
|
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
1639
|
+
const metadataFilter = storage.validateStorageMetadataFilter(filter?.metadata);
|
|
1605
1640
|
try {
|
|
1606
1641
|
const fullTableName = this.#db.getTableName(storage.TABLE_MESSAGES);
|
|
1607
1642
|
let query = `
|
|
@@ -1627,6 +1662,11 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
1627
1662
|
query += ` AND createdAt ${endOp} ?`;
|
|
1628
1663
|
queryParams.push(endDate);
|
|
1629
1664
|
}
|
|
1665
|
+
const metadataConditions = [];
|
|
1666
|
+
addSqliteMetadataFilter(metadataConditions, queryParams, metadataFilter);
|
|
1667
|
+
if (metadataConditions.length > 0) {
|
|
1668
|
+
query += ` AND ${metadataConditions.join(" AND ")}`;
|
|
1669
|
+
}
|
|
1630
1670
|
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
1631
1671
|
if (perPage === 0 && (!include || include.length === 0)) {
|
|
1632
1672
|
return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
|
|
@@ -1678,6 +1718,11 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
1678
1718
|
countQuery += ` AND createdAt ${endOp} ?`;
|
|
1679
1719
|
countParams.push(endDate);
|
|
1680
1720
|
}
|
|
1721
|
+
const countMetadataConditions = [];
|
|
1722
|
+
addSqliteMetadataFilter(countMetadataConditions, countParams, metadataFilter);
|
|
1723
|
+
if (countMetadataConditions.length > 0) {
|
|
1724
|
+
countQuery += ` AND ${countMetadataConditions.join(" AND ")}`;
|
|
1725
|
+
}
|
|
1681
1726
|
const countResult = await this.#db.executeQuery({ sql: countQuery, params: countParams });
|
|
1682
1727
|
const total = Number(countResult[0]?.count ?? 0);
|
|
1683
1728
|
if (total === 0 && paginatedCount === 0 && (!include || include.length === 0)) {
|
|
@@ -1706,8 +1751,7 @@ var MemoryStorageD1 = class extends storage.MemoryStorage {
|
|
|
1706
1751
|
const list = new agent.MessageList().add(paginatedMessages, "memory");
|
|
1707
1752
|
const finalMessages = this._sortMessages(list.get.all.db(), field, direction);
|
|
1708
1753
|
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
1709
|
-
const
|
|
1710
|
-
const hasMore = perPageInput === false ? false : allThreadMessagesReturned ? false : offset + paginatedCount < total;
|
|
1754
|
+
const hasMore = perPageInput !== false && (metadataFilter || returnedThreadMessageIds.size < total) && offset + paginatedCount < total;
|
|
1711
1755
|
return {
|
|
1712
1756
|
messages: finalMessages,
|
|
1713
1757
|
total,
|