@mastra/mongodb 1.7.4 → 1.8.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 +11 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/index.cjs +205 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +206 -4
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/schedules/index.d.ts +25 -0
- package/dist/storage/domains/schedules/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 +2 -2
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { v4 } from '@lukeed/uuid';
|
|
2
2
|
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
3
|
-
import { createVectorErrorId, AgentsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, createStorageErrorId, normalizePerPage, calculatePagination, BackgroundTasksStorage, TABLE_BACKGROUND_TASKS, BlobStore, TABLE_SKILL_BLOBS, DatasetsStorage, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, ensureDate, safelyParseJSON, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, ExperimentsStorage, MCPClientsStorage, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, MCPServersStorage, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ObservabilityStorage, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, ScorerDefinitionsStorage, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, TABLE_SCORERS, SkillsStorage, TABLE_SKILLS, TABLE_SKILL_VERSIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, WorkspacesStorage, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, MastraCompositeStore, TraceStatus, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
|
|
3
|
+
import { createVectorErrorId, AgentsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, createStorageErrorId, normalizePerPage, calculatePagination, BackgroundTasksStorage, TABLE_BACKGROUND_TASKS, BlobStore, TABLE_SKILL_BLOBS, DatasetsStorage, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, ensureDate, safelyParseJSON, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, ExperimentsStorage, MCPClientsStorage, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, MCPServersStorage, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ObservabilityStorage, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, SchedulesStorage, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, ScorerDefinitionsStorage, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, TABLE_SCORERS, SkillsStorage, TABLE_SKILLS, TABLE_SKILL_VERSIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, WorkspacesStorage, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, MastraCompositeStore, TraceStatus, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
|
|
4
4
|
import { MastraVector, validateUpsertInput, validateVectorValues } from '@mastra/core/vector';
|
|
5
5
|
import { MongoClient } from 'mongodb';
|
|
6
6
|
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
@@ -12,7 +12,7 @@ import { saveScorePayloadSchema } from '@mastra/core/evals';
|
|
|
12
12
|
|
|
13
13
|
// package.json
|
|
14
14
|
var package_default = {
|
|
15
|
-
version: "1.
|
|
15
|
+
version: "1.8.0-alpha.0"};
|
|
16
16
|
var MongoDBFilterTranslator = class extends BaseFilterTranslator {
|
|
17
17
|
getSupportedOperators() {
|
|
18
18
|
return {
|
|
@@ -7593,6 +7593,206 @@ var MongoDBPromptBlocksStorage = class _MongoDBPromptBlocksStorage extends Promp
|
|
|
7593
7593
|
return result;
|
|
7594
7594
|
}
|
|
7595
7595
|
};
|
|
7596
|
+
function scheduleToDoc(schedule) {
|
|
7597
|
+
return {
|
|
7598
|
+
id: schedule.id,
|
|
7599
|
+
target: schedule.target,
|
|
7600
|
+
cron: schedule.cron,
|
|
7601
|
+
timezone: schedule.timezone ?? null,
|
|
7602
|
+
status: schedule.status,
|
|
7603
|
+
next_fire_at: schedule.nextFireAt,
|
|
7604
|
+
last_fire_at: schedule.lastFireAt ?? null,
|
|
7605
|
+
last_run_id: schedule.lastRunId ?? null,
|
|
7606
|
+
created_at: schedule.createdAt,
|
|
7607
|
+
updated_at: schedule.updatedAt,
|
|
7608
|
+
metadata: schedule.metadata ?? null
|
|
7609
|
+
};
|
|
7610
|
+
}
|
|
7611
|
+
function docToSchedule(doc) {
|
|
7612
|
+
const target = doc.target;
|
|
7613
|
+
if (!target) {
|
|
7614
|
+
throw new Error(`Schedule ${doc.id} has invalid target`);
|
|
7615
|
+
}
|
|
7616
|
+
const schedule = {
|
|
7617
|
+
id: String(doc.id),
|
|
7618
|
+
target,
|
|
7619
|
+
cron: String(doc.cron),
|
|
7620
|
+
status: String(doc.status),
|
|
7621
|
+
nextFireAt: Number(doc.next_fire_at),
|
|
7622
|
+
createdAt: Number(doc.created_at),
|
|
7623
|
+
updatedAt: Number(doc.updated_at)
|
|
7624
|
+
};
|
|
7625
|
+
if (doc.timezone != null) schedule.timezone = String(doc.timezone);
|
|
7626
|
+
if (doc.last_fire_at != null) schedule.lastFireAt = Number(doc.last_fire_at);
|
|
7627
|
+
if (doc.last_run_id != null) schedule.lastRunId = String(doc.last_run_id);
|
|
7628
|
+
if (doc.metadata != null) schedule.metadata = doc.metadata;
|
|
7629
|
+
return schedule;
|
|
7630
|
+
}
|
|
7631
|
+
function triggerToDoc(trigger) {
|
|
7632
|
+
return {
|
|
7633
|
+
schedule_id: trigger.scheduleId,
|
|
7634
|
+
run_id: trigger.runId,
|
|
7635
|
+
scheduled_fire_at: trigger.scheduledFireAt,
|
|
7636
|
+
actual_fire_at: trigger.actualFireAt,
|
|
7637
|
+
status: trigger.status,
|
|
7638
|
+
error: trigger.error ?? null
|
|
7639
|
+
};
|
|
7640
|
+
}
|
|
7641
|
+
function docToTrigger(doc) {
|
|
7642
|
+
const trigger = {
|
|
7643
|
+
scheduleId: String(doc.schedule_id),
|
|
7644
|
+
runId: String(doc.run_id),
|
|
7645
|
+
scheduledFireAt: Number(doc.scheduled_fire_at),
|
|
7646
|
+
actualFireAt: Number(doc.actual_fire_at),
|
|
7647
|
+
status: String(doc.status)
|
|
7648
|
+
};
|
|
7649
|
+
if (doc.error != null) trigger.error = String(doc.error);
|
|
7650
|
+
return trigger;
|
|
7651
|
+
}
|
|
7652
|
+
var SchedulesMongoDB = class _SchedulesMongoDB extends SchedulesStorage {
|
|
7653
|
+
#connector;
|
|
7654
|
+
#skipDefaultIndexes;
|
|
7655
|
+
#indexes;
|
|
7656
|
+
static MANAGED_COLLECTIONS = [TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS];
|
|
7657
|
+
constructor(config) {
|
|
7658
|
+
super();
|
|
7659
|
+
this.#connector = resolveMongoDBConfig(config);
|
|
7660
|
+
this.#skipDefaultIndexes = config.skipDefaultIndexes;
|
|
7661
|
+
this.#indexes = config.indexes?.filter(
|
|
7662
|
+
(idx) => _SchedulesMongoDB.MANAGED_COLLECTIONS.includes(idx.collection)
|
|
7663
|
+
);
|
|
7664
|
+
}
|
|
7665
|
+
getSchedulesCollection() {
|
|
7666
|
+
return this.#connector.getCollection(TABLE_SCHEDULES);
|
|
7667
|
+
}
|
|
7668
|
+
getTriggersCollection() {
|
|
7669
|
+
return this.#connector.getCollection(TABLE_SCHEDULE_TRIGGERS);
|
|
7670
|
+
}
|
|
7671
|
+
getDefaultIndexDefinitions() {
|
|
7672
|
+
return [
|
|
7673
|
+
{ collection: TABLE_SCHEDULES, keys: { id: 1 }, options: { unique: true } },
|
|
7674
|
+
{ collection: TABLE_SCHEDULES, keys: { status: 1, next_fire_at: 1 } },
|
|
7675
|
+
{ collection: TABLE_SCHEDULES, keys: { "target.workflowId": 1 } },
|
|
7676
|
+
{ collection: TABLE_SCHEDULE_TRIGGERS, keys: { schedule_id: 1, actual_fire_at: -1 } },
|
|
7677
|
+
{ collection: TABLE_SCHEDULE_TRIGGERS, keys: { run_id: 1 }, options: { unique: true } }
|
|
7678
|
+
];
|
|
7679
|
+
}
|
|
7680
|
+
async createDefaultIndexes() {
|
|
7681
|
+
if (this.#skipDefaultIndexes) return;
|
|
7682
|
+
for (const indexDef of this.getDefaultIndexDefinitions()) {
|
|
7683
|
+
try {
|
|
7684
|
+
const collection = await this.#connector.getCollection(indexDef.collection);
|
|
7685
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
7686
|
+
} catch (error) {
|
|
7687
|
+
this.logger?.warn?.(`Failed to create index on ${indexDef.collection}:`, error);
|
|
7688
|
+
}
|
|
7689
|
+
}
|
|
7690
|
+
}
|
|
7691
|
+
async createCustomIndexes() {
|
|
7692
|
+
if (!this.#indexes || this.#indexes.length === 0) return;
|
|
7693
|
+
for (const indexDef of this.#indexes) {
|
|
7694
|
+
try {
|
|
7695
|
+
const collection = await this.#connector.getCollection(indexDef.collection);
|
|
7696
|
+
await collection.createIndex(indexDef.keys, indexDef.options);
|
|
7697
|
+
} catch (error) {
|
|
7698
|
+
this.logger?.warn?.(`Failed to create custom index on ${indexDef.collection}:`, error);
|
|
7699
|
+
}
|
|
7700
|
+
}
|
|
7701
|
+
}
|
|
7702
|
+
async init() {
|
|
7703
|
+
await this.createDefaultIndexes();
|
|
7704
|
+
await this.createCustomIndexes();
|
|
7705
|
+
}
|
|
7706
|
+
async dangerouslyClearAll() {
|
|
7707
|
+
const triggers = await this.getTriggersCollection();
|
|
7708
|
+
await triggers.deleteMany({});
|
|
7709
|
+
const schedules = await this.getSchedulesCollection();
|
|
7710
|
+
await schedules.deleteMany({});
|
|
7711
|
+
}
|
|
7712
|
+
async createSchedule(schedule) {
|
|
7713
|
+
const collection = await this.getSchedulesCollection();
|
|
7714
|
+
const existing = await collection.findOne({ id: schedule.id });
|
|
7715
|
+
if (existing) {
|
|
7716
|
+
throw new Error(`Schedule with id "${schedule.id}" already exists`);
|
|
7717
|
+
}
|
|
7718
|
+
await collection.insertOne(scheduleToDoc(schedule));
|
|
7719
|
+
return schedule;
|
|
7720
|
+
}
|
|
7721
|
+
async getSchedule(id) {
|
|
7722
|
+
const collection = await this.getSchedulesCollection();
|
|
7723
|
+
const doc = await collection.findOne({ id });
|
|
7724
|
+
return doc ? docToSchedule(doc) : null;
|
|
7725
|
+
}
|
|
7726
|
+
async listSchedules(filter) {
|
|
7727
|
+
const query = {};
|
|
7728
|
+
if (filter?.status) query.status = filter.status;
|
|
7729
|
+
if (filter?.workflowId) query["target.workflowId"] = filter.workflowId;
|
|
7730
|
+
const collection = await this.getSchedulesCollection();
|
|
7731
|
+
const docs = await collection.find(query).sort({ created_at: 1 }).toArray();
|
|
7732
|
+
return docs.map(docToSchedule);
|
|
7733
|
+
}
|
|
7734
|
+
async listDueSchedules(now, limit) {
|
|
7735
|
+
const cap = limit ?? 100;
|
|
7736
|
+
const collection = await this.getSchedulesCollection();
|
|
7737
|
+
const docs = await collection.find({ status: "active", next_fire_at: { $lte: now } }).sort({ next_fire_at: 1 }).limit(cap).toArray();
|
|
7738
|
+
return docs.map(docToSchedule);
|
|
7739
|
+
}
|
|
7740
|
+
async updateSchedule(id, patch) {
|
|
7741
|
+
const $set = {};
|
|
7742
|
+
if ("cron" in patch && patch.cron !== void 0) $set.cron = patch.cron;
|
|
7743
|
+
if ("timezone" in patch) $set.timezone = patch.timezone ?? null;
|
|
7744
|
+
if ("status" in patch && patch.status !== void 0) $set.status = patch.status;
|
|
7745
|
+
if ("nextFireAt" in patch && patch.nextFireAt !== void 0) $set.next_fire_at = patch.nextFireAt;
|
|
7746
|
+
if ("target" in patch && patch.target !== void 0) $set.target = patch.target;
|
|
7747
|
+
if ("metadata" in patch) $set.metadata = patch.metadata ?? null;
|
|
7748
|
+
$set.updated_at = Date.now();
|
|
7749
|
+
const collection = await this.getSchedulesCollection();
|
|
7750
|
+
const result = await collection.findOneAndUpdate({ id }, { $set }, { returnDocument: "after" });
|
|
7751
|
+
if (!result) throw new Error(`Schedule ${id} not found`);
|
|
7752
|
+
return docToSchedule(result);
|
|
7753
|
+
}
|
|
7754
|
+
async updateScheduleNextFire(id, expectedNextFireAt, newNextFireAt, lastFireAt, lastRunId) {
|
|
7755
|
+
const collection = await this.getSchedulesCollection();
|
|
7756
|
+
const result = await collection.updateOne(
|
|
7757
|
+
{ id, next_fire_at: expectedNextFireAt, status: "active" },
|
|
7758
|
+
{
|
|
7759
|
+
$set: {
|
|
7760
|
+
next_fire_at: newNextFireAt,
|
|
7761
|
+
last_fire_at: lastFireAt,
|
|
7762
|
+
last_run_id: lastRunId,
|
|
7763
|
+
updated_at: Date.now()
|
|
7764
|
+
}
|
|
7765
|
+
}
|
|
7766
|
+
);
|
|
7767
|
+
return result.matchedCount > 0;
|
|
7768
|
+
}
|
|
7769
|
+
async deleteSchedule(id) {
|
|
7770
|
+
const triggers = await this.getTriggersCollection();
|
|
7771
|
+
await triggers.deleteMany({ schedule_id: id });
|
|
7772
|
+
const schedules = await this.getSchedulesCollection();
|
|
7773
|
+
await schedules.deleteOne({ id });
|
|
7774
|
+
}
|
|
7775
|
+
async recordTrigger(trigger) {
|
|
7776
|
+
const collection = await this.getTriggersCollection();
|
|
7777
|
+
await collection.insertOne(triggerToDoc(trigger));
|
|
7778
|
+
}
|
|
7779
|
+
async listTriggers(scheduleId, opts) {
|
|
7780
|
+
const query = { schedule_id: scheduleId };
|
|
7781
|
+
if (opts?.fromActualFireAt != null || opts?.toActualFireAt != null) {
|
|
7782
|
+
const range = {};
|
|
7783
|
+
if (opts?.fromActualFireAt != null) range.$gte = opts.fromActualFireAt;
|
|
7784
|
+
if (opts?.toActualFireAt != null) range.$lt = opts.toActualFireAt;
|
|
7785
|
+
query.actual_fire_at = range;
|
|
7786
|
+
}
|
|
7787
|
+
const collection = await this.getTriggersCollection();
|
|
7788
|
+
let cursor = collection.find(query).sort({ actual_fire_at: -1 });
|
|
7789
|
+
if (opts?.limit != null) {
|
|
7790
|
+
cursor = cursor.limit(Math.floor(opts.limit));
|
|
7791
|
+
}
|
|
7792
|
+
const docs = await cursor.toArray();
|
|
7793
|
+
return docs.map(docToTrigger);
|
|
7794
|
+
}
|
|
7795
|
+
};
|
|
7596
7796
|
var SNAPSHOT_FIELDS5 = [
|
|
7597
7797
|
"name",
|
|
7598
7798
|
"description",
|
|
@@ -10105,6 +10305,7 @@ var MongoDBStore = class extends MastraCompositeStore {
|
|
|
10105
10305
|
const datasets = new MongoDBDatasetsStorage(domainConfig);
|
|
10106
10306
|
const experiments = new MongoDBExperimentsStorage(domainConfig);
|
|
10107
10307
|
const backgroundTasks = new BackgroundTasksStorageMongoDB(domainConfig);
|
|
10308
|
+
const schedules = new SchedulesMongoDB(domainConfig);
|
|
10108
10309
|
this.stores = {
|
|
10109
10310
|
memory,
|
|
10110
10311
|
scores,
|
|
@@ -10120,7 +10321,8 @@ var MongoDBStore = class extends MastraCompositeStore {
|
|
|
10120
10321
|
blobs,
|
|
10121
10322
|
backgroundTasks,
|
|
10122
10323
|
datasets,
|
|
10123
|
-
experiments
|
|
10324
|
+
experiments,
|
|
10325
|
+
schedules
|
|
10124
10326
|
};
|
|
10125
10327
|
}
|
|
10126
10328
|
/**
|
|
@@ -10239,6 +10441,6 @@ Example Complex Query:
|
|
|
10239
10441
|
]
|
|
10240
10442
|
}`;
|
|
10241
10443
|
|
|
10242
|
-
export { BackgroundTasksStorageMongoDB, MONGODB_PROMPT, MemoryStorageMongoDB, MongoDBAgentsStorage, MongoDBBlobStore, MongoDBDatasetsStorage, MongoDBExperimentsStorage, MongoDBMCPClientsStorage, MongoDBMCPServersStorage, MongoDBPromptBlocksStorage, MongoDBScorerDefinitionsStorage, MongoDBSkillsStorage, MongoDBStore, MongoDBVector, MongoDBWorkspacesStorage, ObservabilityMongoDB, ScoresStorageMongoDB, WorkflowsStorageMongoDB };
|
|
10444
|
+
export { BackgroundTasksStorageMongoDB, MONGODB_PROMPT, MemoryStorageMongoDB, MongoDBAgentsStorage, MongoDBBlobStore, MongoDBDatasetsStorage, MongoDBExperimentsStorage, MongoDBMCPClientsStorage, MongoDBMCPServersStorage, MongoDBPromptBlocksStorage, MongoDBScorerDefinitionsStorage, MongoDBSkillsStorage, MongoDBStore, MongoDBVector, MongoDBWorkspacesStorage, ObservabilityMongoDB, SchedulesMongoDB, ScoresStorageMongoDB, WorkflowsStorageMongoDB };
|
|
10243
10445
|
//# sourceMappingURL=index.js.map
|
|
10244
10446
|
//# sourceMappingURL=index.js.map
|