@mastra/cloudflare-d1 1.0.4 → 1.0.5-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 +9 -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 +247 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +248 -5
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/background-tasks/index.d.ts +18 -0
- package/dist/storage/domains/background-tasks/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +2 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
2
|
-
import {
|
|
2
|
+
import { BackgroundTasksStorage, TABLE_SCHEMAS, TABLE_BACKGROUND_TASKS, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ensureDate, createStorageErrorId, normalizePerPage, calculatePagination, serializeDate, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraCompositeStore, getSqlType, getDefaultValue, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
|
|
3
3
|
import Cloudflare from 'cloudflare';
|
|
4
|
-
import { MessageList } from '@mastra/core/agent';
|
|
5
4
|
import { MastraBase } from '@mastra/core/base';
|
|
6
5
|
import { parseSqlIdentifier } from '@mastra/core/utils';
|
|
6
|
+
import { MessageList } from '@mastra/core/agent';
|
|
7
7
|
import { saveScorePayloadSchema } from '@mastra/core/evals';
|
|
8
8
|
|
|
9
9
|
// src/storage/index.ts
|
|
@@ -724,7 +724,246 @@ var D1DB = class extends MastraBase {
|
|
|
724
724
|
}
|
|
725
725
|
};
|
|
726
726
|
|
|
727
|
-
// src/storage/domains/
|
|
727
|
+
// src/storage/domains/background-tasks/index.ts
|
|
728
|
+
function serializeJson(v) {
|
|
729
|
+
if (typeof v === "object" && v != null) return JSON.stringify(v);
|
|
730
|
+
return v ?? null;
|
|
731
|
+
}
|
|
732
|
+
function rowToTask(row) {
|
|
733
|
+
const parseJson = (val) => {
|
|
734
|
+
if (val == null || val === "") return void 0;
|
|
735
|
+
if (typeof val === "string") {
|
|
736
|
+
try {
|
|
737
|
+
return JSON.parse(val);
|
|
738
|
+
} catch {
|
|
739
|
+
return val;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
return val;
|
|
743
|
+
};
|
|
744
|
+
return {
|
|
745
|
+
id: row.id,
|
|
746
|
+
status: row.status,
|
|
747
|
+
toolName: row.tool_name,
|
|
748
|
+
toolCallId: row.tool_call_id,
|
|
749
|
+
args: parseJson(row.args) ?? {},
|
|
750
|
+
agentId: row.agent_id,
|
|
751
|
+
threadId: row.thread_id || void 0,
|
|
752
|
+
resourceId: row.resource_id || void 0,
|
|
753
|
+
runId: row.run_id ?? "",
|
|
754
|
+
result: parseJson(row.result),
|
|
755
|
+
error: parseJson(row.error),
|
|
756
|
+
retryCount: Number(row.retry_count ?? 0),
|
|
757
|
+
maxRetries: Number(row.max_retries ?? 0),
|
|
758
|
+
timeoutMs: Number(row.timeout_ms ?? 3e5),
|
|
759
|
+
createdAt: new Date(row.createdAt),
|
|
760
|
+
startedAt: row.startedAt ? new Date(row.startedAt) : void 0,
|
|
761
|
+
completedAt: row.completedAt ? new Date(row.completedAt) : void 0
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
var BackgroundTasksStorageD1 = class extends BackgroundTasksStorage {
|
|
765
|
+
#db;
|
|
766
|
+
constructor(config) {
|
|
767
|
+
super();
|
|
768
|
+
const resolved = resolveD1Config(config);
|
|
769
|
+
this.#db = new D1DB(resolved);
|
|
770
|
+
}
|
|
771
|
+
async init() {
|
|
772
|
+
await this.#db.createTable({ tableName: TABLE_BACKGROUND_TASKS, schema: TABLE_SCHEMAS[TABLE_BACKGROUND_TASKS] });
|
|
773
|
+
}
|
|
774
|
+
async dangerouslyClearAll() {
|
|
775
|
+
await this.#db.clearTable({ tableName: TABLE_BACKGROUND_TASKS });
|
|
776
|
+
}
|
|
777
|
+
async createTask(task) {
|
|
778
|
+
const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
|
|
779
|
+
const { sql, params } = createSqlBuilder().insert(
|
|
780
|
+
fullTableName,
|
|
781
|
+
[
|
|
782
|
+
"id",
|
|
783
|
+
"tool_call_id",
|
|
784
|
+
"tool_name",
|
|
785
|
+
"agent_id",
|
|
786
|
+
"thread_id",
|
|
787
|
+
"resource_id",
|
|
788
|
+
"run_id",
|
|
789
|
+
"status",
|
|
790
|
+
"args",
|
|
791
|
+
"result",
|
|
792
|
+
"error",
|
|
793
|
+
"retry_count",
|
|
794
|
+
"max_retries",
|
|
795
|
+
"timeout_ms",
|
|
796
|
+
"createdAt",
|
|
797
|
+
"startedAt",
|
|
798
|
+
"completedAt"
|
|
799
|
+
],
|
|
800
|
+
[
|
|
801
|
+
task.id,
|
|
802
|
+
task.toolCallId,
|
|
803
|
+
task.toolName,
|
|
804
|
+
task.agentId,
|
|
805
|
+
task.threadId ?? null,
|
|
806
|
+
task.resourceId ?? null,
|
|
807
|
+
task.runId,
|
|
808
|
+
task.status,
|
|
809
|
+
serializeJson(task.args),
|
|
810
|
+
serializeJson(task.result),
|
|
811
|
+
serializeJson(task.error),
|
|
812
|
+
task.retryCount,
|
|
813
|
+
task.maxRetries,
|
|
814
|
+
task.timeoutMs,
|
|
815
|
+
task.createdAt.toISOString(),
|
|
816
|
+
task.startedAt?.toISOString() ?? null,
|
|
817
|
+
task.completedAt?.toISOString() ?? null
|
|
818
|
+
]
|
|
819
|
+
).build();
|
|
820
|
+
await this.#db.executeQuery({ sql, params });
|
|
821
|
+
}
|
|
822
|
+
async updateTask(taskId, update) {
|
|
823
|
+
const sets = [];
|
|
824
|
+
const params = [];
|
|
825
|
+
if ("status" in update) {
|
|
826
|
+
sets.push("status = ?");
|
|
827
|
+
params.push(update.status);
|
|
828
|
+
}
|
|
829
|
+
if ("result" in update) {
|
|
830
|
+
sets.push("result = ?");
|
|
831
|
+
params.push(serializeJson(update.result));
|
|
832
|
+
}
|
|
833
|
+
if ("error" in update) {
|
|
834
|
+
sets.push("error = ?");
|
|
835
|
+
params.push(serializeJson(update.error));
|
|
836
|
+
}
|
|
837
|
+
if ("retryCount" in update) {
|
|
838
|
+
sets.push("retry_count = ?");
|
|
839
|
+
params.push(update.retryCount);
|
|
840
|
+
}
|
|
841
|
+
if ("startedAt" in update) {
|
|
842
|
+
sets.push("startedAt = ?");
|
|
843
|
+
params.push(update.startedAt?.toISOString() ?? null);
|
|
844
|
+
}
|
|
845
|
+
if ("completedAt" in update) {
|
|
846
|
+
sets.push("completedAt = ?");
|
|
847
|
+
params.push(update.completedAt?.toISOString() ?? null);
|
|
848
|
+
}
|
|
849
|
+
if (sets.length === 0) return;
|
|
850
|
+
params.push(taskId);
|
|
851
|
+
const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
|
|
852
|
+
await this.#db.executeQuery({
|
|
853
|
+
sql: `UPDATE ${fullTableName} SET ${sets.join(", ")} WHERE id = ?`,
|
|
854
|
+
params
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
async getTask(taskId) {
|
|
858
|
+
const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
|
|
859
|
+
const { sql, params } = createSqlBuilder().select("*").from(fullTableName).where("id = ?", taskId).build();
|
|
860
|
+
const row = await this.#db.executeQuery({ sql, params, first: true });
|
|
861
|
+
return row ? rowToTask(row) : null;
|
|
862
|
+
}
|
|
863
|
+
async listTasks(filter) {
|
|
864
|
+
const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
|
|
865
|
+
let builder = createSqlBuilder().select("*").from(fullTableName);
|
|
866
|
+
let countBuilder = createSqlBuilder().count().from(fullTableName);
|
|
867
|
+
if (filter.status) {
|
|
868
|
+
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
869
|
+
builder = builder.where(`status IN (${statuses.map(() => "?").join(",")})`, ...statuses);
|
|
870
|
+
countBuilder = countBuilder.where(`status IN (${statuses.map(() => "?").join(",")})`, ...statuses);
|
|
871
|
+
}
|
|
872
|
+
if (filter.agentId) {
|
|
873
|
+
builder = builder.whereAnd("agent_id = ?", filter.agentId);
|
|
874
|
+
countBuilder = countBuilder.whereAnd("agent_id = ?", filter.agentId);
|
|
875
|
+
}
|
|
876
|
+
if (filter.threadId) {
|
|
877
|
+
builder = builder.whereAnd("thread_id = ?", filter.threadId);
|
|
878
|
+
countBuilder = countBuilder.whereAnd("thread_id = ?", filter.threadId);
|
|
879
|
+
}
|
|
880
|
+
if (filter.runId) {
|
|
881
|
+
builder = builder.whereAnd("run_id = ?", filter.runId);
|
|
882
|
+
countBuilder = countBuilder.whereAnd("run_id = ?", filter.runId);
|
|
883
|
+
}
|
|
884
|
+
if (filter.toolName) {
|
|
885
|
+
builder = builder.whereAnd("tool_name = ?", filter.toolName);
|
|
886
|
+
countBuilder = countBuilder.whereAnd("tool_name = ?", filter.toolName);
|
|
887
|
+
}
|
|
888
|
+
const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
|
|
889
|
+
if (filter.fromDate) {
|
|
890
|
+
builder = builder.whereAnd(`${dateCol} >= ?`, filter.fromDate.toISOString());
|
|
891
|
+
countBuilder = countBuilder.whereAnd(`${dateCol} >= ?`, filter.fromDate.toISOString());
|
|
892
|
+
}
|
|
893
|
+
if (filter.toDate) {
|
|
894
|
+
builder = builder.whereAnd(`${dateCol} < ?`, filter.toDate.toISOString());
|
|
895
|
+
countBuilder = countBuilder.whereAnd(`${dateCol} < ?`, filter.toDate.toISOString());
|
|
896
|
+
}
|
|
897
|
+
const { sql: countSql, params: countParams } = countBuilder.build();
|
|
898
|
+
const countRow = await this.#db.executeQuery({ sql: countSql, params: countParams, first: true });
|
|
899
|
+
const total = Number(countRow?.count ?? 0);
|
|
900
|
+
const orderCol = filter.orderBy === "startedAt" ? "startedAt" : filter.orderBy === "completedAt" ? "completedAt" : "createdAt";
|
|
901
|
+
builder = builder.orderBy(orderCol, filter.orderDirection === "desc" ? "DESC" : "ASC");
|
|
902
|
+
if (filter.perPage != null) {
|
|
903
|
+
builder = builder.limit(filter.perPage);
|
|
904
|
+
if (filter.page != null) {
|
|
905
|
+
builder = builder.offset(filter.page * filter.perPage);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
const { sql, params } = builder.build();
|
|
909
|
+
const rows = await this.#db.executeQuery({ sql, params });
|
|
910
|
+
return { tasks: rows.map(rowToTask), total };
|
|
911
|
+
}
|
|
912
|
+
async deleteTask(taskId) {
|
|
913
|
+
const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
|
|
914
|
+
await this.#db.executeQuery({ sql: `DELETE FROM ${fullTableName} WHERE id = ?`, params: [taskId] });
|
|
915
|
+
}
|
|
916
|
+
async deleteTasks(filter) {
|
|
917
|
+
const conditions = [];
|
|
918
|
+
const params = [];
|
|
919
|
+
if (filter.status) {
|
|
920
|
+
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
921
|
+
conditions.push(`status IN (${statuses.map(() => "?").join(",")})`);
|
|
922
|
+
params.push(...statuses);
|
|
923
|
+
}
|
|
924
|
+
const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
|
|
925
|
+
if (filter.fromDate) {
|
|
926
|
+
conditions.push(`${dateCol} >= ?`);
|
|
927
|
+
params.push(filter.fromDate.toISOString());
|
|
928
|
+
}
|
|
929
|
+
if (filter.toDate) {
|
|
930
|
+
conditions.push(`${dateCol} < ?`);
|
|
931
|
+
params.push(filter.toDate.toISOString());
|
|
932
|
+
}
|
|
933
|
+
if (filter.agentId) {
|
|
934
|
+
conditions.push("agent_id = ?");
|
|
935
|
+
params.push(filter.agentId);
|
|
936
|
+
}
|
|
937
|
+
if (filter.runId) {
|
|
938
|
+
conditions.push("run_id = ?");
|
|
939
|
+
params.push(filter.runId);
|
|
940
|
+
}
|
|
941
|
+
if (conditions.length === 0) return;
|
|
942
|
+
const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
|
|
943
|
+
await this.#db.executeQuery({
|
|
944
|
+
sql: `DELETE FROM ${fullTableName} WHERE ${conditions.join(" AND ")}`,
|
|
945
|
+
params
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
async getRunningCount() {
|
|
949
|
+
const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
|
|
950
|
+
const row = await this.#db.executeQuery({
|
|
951
|
+
sql: `SELECT COUNT(*) as count FROM ${fullTableName} WHERE status = 'running'`,
|
|
952
|
+
params: [],
|
|
953
|
+
first: true
|
|
954
|
+
});
|
|
955
|
+
return Number(row?.count ?? 0);
|
|
956
|
+
}
|
|
957
|
+
async getRunningCountByAgent(agentId) {
|
|
958
|
+
const fullTableName = this.#db.getTableName(TABLE_BACKGROUND_TASKS);
|
|
959
|
+
const row = await this.#db.executeQuery({
|
|
960
|
+
sql: `SELECT COUNT(*) as count FROM ${fullTableName} WHERE status = 'running' AND agent_id = ?`,
|
|
961
|
+
params: [agentId],
|
|
962
|
+
first: true
|
|
963
|
+
});
|
|
964
|
+
return Number(row?.count ?? 0);
|
|
965
|
+
}
|
|
966
|
+
};
|
|
728
967
|
var MemoryStorageD1 = class extends MemoryStorage {
|
|
729
968
|
#db;
|
|
730
969
|
constructor(config) {
|
|
@@ -2223,21 +2462,25 @@ var D1Store = class extends MastraCompositeStore {
|
|
|
2223
2462
|
let scores;
|
|
2224
2463
|
let workflows;
|
|
2225
2464
|
let memory;
|
|
2465
|
+
let backgroundTasks;
|
|
2226
2466
|
if (this.binding) {
|
|
2227
2467
|
const domainConfig = { binding: this.binding, tablePrefix: this.tablePrefix };
|
|
2228
2468
|
scores = new ScoresStorageD1(domainConfig);
|
|
2229
2469
|
workflows = new WorkflowsStorageD1(domainConfig);
|
|
2230
2470
|
memory = new MemoryStorageD1(domainConfig);
|
|
2471
|
+
backgroundTasks = new BackgroundTasksStorageD1(domainConfig);
|
|
2231
2472
|
} else {
|
|
2232
2473
|
const domainConfig = { client: this.client, tablePrefix: this.tablePrefix };
|
|
2233
2474
|
scores = new ScoresStorageD1(domainConfig);
|
|
2234
2475
|
workflows = new WorkflowsStorageD1(domainConfig);
|
|
2235
2476
|
memory = new MemoryStorageD1(domainConfig);
|
|
2477
|
+
backgroundTasks = new BackgroundTasksStorageD1(domainConfig);
|
|
2236
2478
|
}
|
|
2237
2479
|
this.stores = {
|
|
2238
2480
|
scores,
|
|
2239
2481
|
workflows,
|
|
2240
|
-
memory
|
|
2482
|
+
memory,
|
|
2483
|
+
backgroundTasks
|
|
2241
2484
|
};
|
|
2242
2485
|
}
|
|
2243
2486
|
/**
|
|
@@ -2249,6 +2492,6 @@ var D1Store = class extends MastraCompositeStore {
|
|
|
2249
2492
|
}
|
|
2250
2493
|
};
|
|
2251
2494
|
|
|
2252
|
-
export { D1Store, MemoryStorageD1, ScoresStorageD1, WorkflowsStorageD1 };
|
|
2495
|
+
export { BackgroundTasksStorageD1, D1Store, MemoryStorageD1, ScoresStorageD1, WorkflowsStorageD1 };
|
|
2253
2496
|
//# sourceMappingURL=index.js.map
|
|
2254
2497
|
//# sourceMappingURL=index.js.map
|