@mastra/cloudflare-d1 0.0.0-vnext-20251104230439 → 0.0.0-vnext-20251119160359
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 +277 -3
- package/README.md +9 -4
- package/dist/index.cjs +12 -89
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +13 -90
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +1 -7
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +1 -1
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +8 -15
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +15 -10
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
2
|
-
import { MastraStorage, StoreOperations, ScoresStorage, TABLE_SCORERS, normalizePerPage, calculatePagination, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ensureDate, MemoryStorage, TABLE_RESOURCES, TABLE_THREADS, TABLE_MESSAGES,
|
|
2
|
+
import { MastraStorage, StoreOperations, ScoresStorage, TABLE_SCORERS, normalizePerPage, calculatePagination, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ensureDate, MemoryStorage, TABLE_RESOURCES, TABLE_THREADS, TABLE_MESSAGES, serializeDate, safelyParseJSON } from '@mastra/core/storage';
|
|
3
3
|
import Cloudflare from 'cloudflare';
|
|
4
4
|
import { MessageList } from '@mastra/core/agent';
|
|
5
5
|
import { parseSqlIdentifier } from '@mastra/core/utils';
|
|
@@ -627,9 +627,8 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
627
627
|
);
|
|
628
628
|
}
|
|
629
629
|
}
|
|
630
|
-
async _getIncludedMessages(threadId,
|
|
630
|
+
async _getIncludedMessages(threadId, include) {
|
|
631
631
|
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
632
|
-
const include = selectBy?.include;
|
|
633
632
|
if (!include) return null;
|
|
634
633
|
const unionQueries = [];
|
|
635
634
|
const params = [];
|
|
@@ -685,73 +684,6 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
685
684
|
});
|
|
686
685
|
return processedMessages;
|
|
687
686
|
}
|
|
688
|
-
/**
|
|
689
|
-
* @deprecated use listMessages instead
|
|
690
|
-
*/
|
|
691
|
-
async getMessages({
|
|
692
|
-
threadId,
|
|
693
|
-
resourceId,
|
|
694
|
-
selectBy
|
|
695
|
-
}) {
|
|
696
|
-
try {
|
|
697
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
698
|
-
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
699
|
-
const limit = resolveMessageLimit({
|
|
700
|
-
last: selectBy?.last,
|
|
701
|
-
defaultLimit: 40
|
|
702
|
-
});
|
|
703
|
-
const include = selectBy?.include || [];
|
|
704
|
-
const messages = [];
|
|
705
|
-
if (include.length) {
|
|
706
|
-
const includeResult = await this._getIncludedMessages(threadId, selectBy);
|
|
707
|
-
if (Array.isArray(includeResult)) messages.push(...includeResult);
|
|
708
|
-
}
|
|
709
|
-
const excludeIds = messages.map((m) => m.id);
|
|
710
|
-
const query = createSqlBuilder().select(["id", "content", "role", "type", "createdAt", "thread_id AS threadId"]).from(fullTableName).where("thread_id = ?", threadId);
|
|
711
|
-
if (resourceId) {
|
|
712
|
-
query.andWhere("resourceId = ?", resourceId);
|
|
713
|
-
}
|
|
714
|
-
if (excludeIds.length > 0) {
|
|
715
|
-
query.andWhere(`id NOT IN (${excludeIds.map(() => "?").join(",")})`, ...excludeIds);
|
|
716
|
-
}
|
|
717
|
-
query.orderBy("createdAt", "DESC").limit(limit);
|
|
718
|
-
const { sql, params } = query.build();
|
|
719
|
-
const result = await this.operations.executeQuery({ sql, params });
|
|
720
|
-
if (Array.isArray(result)) messages.push(...result);
|
|
721
|
-
messages.sort((a, b) => {
|
|
722
|
-
const aRecord = a;
|
|
723
|
-
const bRecord = b;
|
|
724
|
-
const timeA = new Date(aRecord.createdAt).getTime();
|
|
725
|
-
const timeB = new Date(bRecord.createdAt).getTime();
|
|
726
|
-
return timeA - timeB;
|
|
727
|
-
});
|
|
728
|
-
const processedMessages = messages.map((message) => {
|
|
729
|
-
const processedMsg = {};
|
|
730
|
-
for (const [key, value] of Object.entries(message)) {
|
|
731
|
-
if (key === `type` && value === `v2`) continue;
|
|
732
|
-
processedMsg[key] = deserializeValue(value);
|
|
733
|
-
}
|
|
734
|
-
return processedMsg;
|
|
735
|
-
});
|
|
736
|
-
this.logger.debug(`Retrieved ${messages.length} messages for thread ${threadId}`);
|
|
737
|
-
const list = new MessageList().add(processedMessages, "memory");
|
|
738
|
-
return { messages: list.get.all.db() };
|
|
739
|
-
} catch (error) {
|
|
740
|
-
const mastraError = new MastraError(
|
|
741
|
-
{
|
|
742
|
-
id: "CLOUDFLARE_D1_STORAGE_GET_MESSAGES_ERROR",
|
|
743
|
-
domain: ErrorDomain.STORAGE,
|
|
744
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
745
|
-
text: `Failed to retrieve messages for thread ${threadId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
746
|
-
details: { threadId, resourceId: resourceId ?? "" }
|
|
747
|
-
},
|
|
748
|
-
error
|
|
749
|
-
);
|
|
750
|
-
this.logger?.error(mastraError.toString());
|
|
751
|
-
this.logger?.trackException(mastraError);
|
|
752
|
-
throw mastraError;
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
687
|
async listMessagesById({ messageIds }) {
|
|
756
688
|
if (messageIds.length === 0) return { messages: [] };
|
|
757
689
|
const fullTableName = this.operations.getTableName(TABLE_MESSAGES);
|
|
@@ -838,7 +770,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
838
770
|
query += ` AND createdAt <= ?`;
|
|
839
771
|
queryParams.push(endDate);
|
|
840
772
|
}
|
|
841
|
-
const { field, direction } = this.parseOrderBy(orderBy);
|
|
773
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
842
774
|
query += ` ORDER BY "${field}" ${direction}`;
|
|
843
775
|
if (perPage !== Number.MAX_SAFE_INTEGER) {
|
|
844
776
|
query += ` LIMIT ? OFFSET ?`;
|
|
@@ -884,8 +816,7 @@ var MemoryStorageD1 = class extends MemoryStorage {
|
|
|
884
816
|
const messageIds = new Set(paginatedMessages.map((m) => m.id));
|
|
885
817
|
let includeMessages = [];
|
|
886
818
|
if (include && include.length > 0) {
|
|
887
|
-
const
|
|
888
|
-
const includeResult = await this._getIncludedMessages(threadId, selectBy);
|
|
819
|
+
const includeResult = await this._getIncludedMessages(threadId, include);
|
|
889
820
|
if (Array.isArray(includeResult)) {
|
|
890
821
|
includeMessages = includeResult;
|
|
891
822
|
for (const includeMsg of includeMessages) {
|
|
@@ -1867,13 +1798,18 @@ var WorkflowsStorageD1 = class extends WorkflowsStorage {
|
|
|
1867
1798
|
toDate,
|
|
1868
1799
|
page,
|
|
1869
1800
|
perPage,
|
|
1870
|
-
resourceId
|
|
1801
|
+
resourceId,
|
|
1802
|
+
status
|
|
1871
1803
|
} = {}) {
|
|
1872
1804
|
const fullTableName = this.operations.getTableName(TABLE_WORKFLOW_SNAPSHOT);
|
|
1873
1805
|
try {
|
|
1874
1806
|
const builder = createSqlBuilder().select().from(fullTableName);
|
|
1875
1807
|
const countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
1876
1808
|
if (workflowName) builder.whereAnd("workflow_name = ?", workflowName);
|
|
1809
|
+
if (status) {
|
|
1810
|
+
builder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
1811
|
+
countBuilder.whereAnd("json_extract(snapshot, '$.status') = ?", status);
|
|
1812
|
+
}
|
|
1877
1813
|
if (resourceId) {
|
|
1878
1814
|
const hasResourceId = await this.operations.hasColumn(fullTableName, "resourceId");
|
|
1879
1815
|
if (hasResourceId) {
|
|
@@ -1976,7 +1912,7 @@ var D1Store = class extends MastraStorage {
|
|
|
1976
1912
|
*/
|
|
1977
1913
|
constructor(config) {
|
|
1978
1914
|
try {
|
|
1979
|
-
super({ name: "D1" });
|
|
1915
|
+
super({ id: config.id, name: "D1" });
|
|
1980
1916
|
if (config.tablePrefix && !/^[a-zA-Z0-9_]*$/.test(config.tablePrefix)) {
|
|
1981
1917
|
throw new Error("Invalid tablePrefix: only letters, numbers, and underscores are allowed.");
|
|
1982
1918
|
}
|
|
@@ -2106,12 +2042,6 @@ var D1Store = class extends MastraStorage {
|
|
|
2106
2042
|
async saveMessages(args) {
|
|
2107
2043
|
return this.stores.memory.saveMessages(args);
|
|
2108
2044
|
}
|
|
2109
|
-
/**
|
|
2110
|
-
* @deprecated use listMessages instead
|
|
2111
|
-
*/
|
|
2112
|
-
async getMessages({ threadId, selectBy }) {
|
|
2113
|
-
return this.stores.memory.getMessages({ threadId, selectBy });
|
|
2114
|
-
}
|
|
2115
2045
|
async updateWorkflowResults({
|
|
2116
2046
|
workflowName,
|
|
2117
2047
|
runId,
|
|
@@ -2139,15 +2069,8 @@ var D1Store = class extends MastraStorage {
|
|
|
2139
2069
|
async loadWorkflowSnapshot(params) {
|
|
2140
2070
|
return this.stores.workflows.loadWorkflowSnapshot(params);
|
|
2141
2071
|
}
|
|
2142
|
-
async listWorkflowRuns({
|
|
2143
|
-
|
|
2144
|
-
fromDate,
|
|
2145
|
-
toDate,
|
|
2146
|
-
perPage,
|
|
2147
|
-
page,
|
|
2148
|
-
resourceId
|
|
2149
|
-
} = {}) {
|
|
2150
|
-
return this.stores.workflows.listWorkflowRuns({ workflowName, fromDate, toDate, perPage, page, resourceId });
|
|
2072
|
+
async listWorkflowRuns(args = {}) {
|
|
2073
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
2151
2074
|
}
|
|
2152
2075
|
async getWorkflowRunById({
|
|
2153
2076
|
runId,
|