@mastra/core
Version:
307 lines (306 loc) • 10.3 kB
JavaScript
import { t as deepEqual } from "./deep-equal-D_JPy4kj.js";
import { d as normalizePerPage, o as VersionedStorageDomain, t as FilesystemVersionedHelpers, u as calculatePagination } from "./filesystem-versioned-M7zmYTpZ.js";
//#region src/storage/domains/prompt-blocks/base.ts
var PromptBlocksStorage = class extends VersionedStorageDomain {
listKey = "promptBlocks";
versionMetadataFields = [
"id",
"blockId",
"versionNumber",
"changedFields",
"changeMessage",
"createdAt"
];
constructor() {
super({
component: "STORAGE",
name: "PROMPT_BLOCKS"
});
}
};
//#endregion
//#region src/storage/domains/prompt-blocks/inmemory.ts
var InMemoryPromptBlocksStorage = class extends PromptBlocksStorage {
db;
constructor({ db }) {
super();
this.db = db;
}
async dangerouslyClearAll() {
this.db.promptBlocks.clear();
this.db.promptBlockVersions.clear();
}
async getById(id) {
const block = this.db.promptBlocks.get(id);
return block ? this.deepCopyBlock(block) : null;
}
async create(input) {
const { promptBlock } = input;
if (this.db.promptBlocks.has(promptBlock.id)) throw new Error(`Prompt block with id ${promptBlock.id} already exists`);
const now = /* @__PURE__ */ new Date();
const newBlock = {
id: promptBlock.id,
status: "draft",
activeVersionId: void 0,
authorId: promptBlock.authorId,
metadata: promptBlock.metadata,
createdAt: now,
updatedAt: now
};
this.db.promptBlocks.set(promptBlock.id, newBlock);
const { id: _id, authorId: _authorId, metadata: _metadata, ...snapshotConfig } = promptBlock;
const versionId = crypto.randomUUID();
await this.createVersion({
id: versionId,
blockId: promptBlock.id,
versionNumber: 1,
...snapshotConfig,
changedFields: Object.keys(snapshotConfig),
changeMessage: "Initial version"
});
return this.deepCopyBlock(newBlock);
}
async update(input) {
const { id, ...updates } = input;
const existingBlock = this.db.promptBlocks.get(id);
if (!existingBlock) throw new Error(`Prompt block with id ${id} not found`);
const { authorId, activeVersionId, metadata, status } = updates;
const updatedBlock = {
...existingBlock,
...authorId !== void 0 && { authorId },
...activeVersionId !== void 0 && { activeVersionId },
...status !== void 0 && { status },
...metadata !== void 0 && { metadata: {
...existingBlock.metadata,
...metadata
} },
updatedAt: /* @__PURE__ */ new Date()
};
this.db.promptBlocks.set(id, updatedBlock);
return this.deepCopyBlock(updatedBlock);
}
async delete(id) {
this.db.promptBlocks.delete(id);
await this.deleteVersionsByParentId(id);
}
async list(args) {
const { page = 0, perPage: perPageInput, orderBy, authorId, metadata, status } = args || {};
const { field, direction } = this.parseOrderBy(orderBy);
const perPage = normalizePerPage(perPageInput, 100);
if (page < 0) throw new Error("page must be >= 0");
const maxOffset = Number.MAX_SAFE_INTEGER / 2;
if (page * perPage > maxOffset) throw new Error("page value too large");
let blocks = Array.from(this.db.promptBlocks.values());
if (status) blocks = blocks.filter((block) => block.status === status);
if (authorId !== void 0) blocks = blocks.filter((block) => block.authorId === authorId);
if (metadata && Object.keys(metadata).length > 0) blocks = blocks.filter((block) => {
if (!block.metadata) return false;
return Object.entries(metadata).every(([key, value]) => deepEqual(block.metadata[key], value));
});
const clonedBlocks = this.sortBlocks(blocks, field, direction).map((block) => this.deepCopyBlock(block));
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
return {
promptBlocks: clonedBlocks.slice(offset, offset + perPage),
total: clonedBlocks.length,
page,
perPage: perPageForResponse,
hasMore: offset + perPage < clonedBlocks.length
};
}
async createVersion(input) {
if (this.db.promptBlockVersions.has(input.id)) throw new Error(`Version with id ${input.id} already exists`);
for (const version of this.db.promptBlockVersions.values()) if (version.blockId === input.blockId && version.versionNumber === input.versionNumber) throw new Error(`Version number ${input.versionNumber} already exists for prompt block ${input.blockId}`);
const version = {
...input,
createdAt: /* @__PURE__ */ new Date()
};
this.db.promptBlockVersions.set(input.id, this.deepCopyVersion(version));
return this.deepCopyVersion(version);
}
async getVersion(id) {
const version = this.db.promptBlockVersions.get(id);
return version ? this.deepCopyVersion(version) : null;
}
async getVersionByNumber(blockId, versionNumber) {
for (const version of this.db.promptBlockVersions.values()) if (version.blockId === blockId && version.versionNumber === versionNumber) return this.deepCopyVersion(version);
return null;
}
async getLatestVersion(blockId) {
let latest = null;
for (const version of this.db.promptBlockVersions.values()) if (version.blockId === blockId) {
if (!latest || version.versionNumber > latest.versionNumber) latest = version;
}
return latest ? this.deepCopyVersion(latest) : null;
}
async listVersions(input) {
const { blockId, page = 0, perPage: perPageInput, orderBy } = input;
const { field, direction } = this.parseVersionOrderBy(orderBy);
const perPage = normalizePerPage(perPageInput, 20);
if (page < 0) throw new Error("page must be >= 0");
const maxOffset = Number.MAX_SAFE_INTEGER / 2;
if (page * perPage > maxOffset) throw new Error("page value too large");
let versions = Array.from(this.db.promptBlockVersions.values()).filter((v) => v.blockId === blockId);
versions = this.sortVersions(versions, field, direction);
const clonedVersions = versions.map((v) => this.deepCopyVersion(v));
const total = clonedVersions.length;
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
return {
versions: clonedVersions.slice(offset, offset + perPage),
total,
page,
perPage: perPageForResponse,
hasMore: offset + perPage < total
};
}
async deleteVersion(id) {
this.db.promptBlockVersions.delete(id);
}
async deleteVersionsByParentId(entityId) {
const idsToDelete = [];
for (const [id, version] of this.db.promptBlockVersions.entries()) if (version.blockId === entityId) idsToDelete.push(id);
for (const id of idsToDelete) this.db.promptBlockVersions.delete(id);
}
async countVersions(blockId) {
let count = 0;
for (const version of this.db.promptBlockVersions.values()) if (version.blockId === blockId) count++;
return count;
}
deepCopyBlock(block) {
return {
...block,
metadata: block.metadata ? { ...block.metadata } : block.metadata
};
}
deepCopyVersion(version) {
return {
...version,
rules: version.rules ? JSON.parse(JSON.stringify(version.rules)) : version.rules,
changedFields: version.changedFields ? [...version.changedFields] : version.changedFields
};
}
sortBlocks(blocks, field, direction) {
return blocks.sort((a, b) => {
const aValue = a[field].getTime();
const bValue = b[field].getTime();
return direction === "ASC" ? aValue - bValue : bValue - aValue;
});
}
sortVersions(versions, field, direction) {
return versions.sort((a, b) => {
let aVal;
let bVal;
if (field === "createdAt") {
aVal = a.createdAt.getTime();
bVal = b.createdAt.getTime();
} else {
aVal = a.versionNumber;
bVal = b.versionNumber;
}
return direction === "ASC" ? aVal - bVal : bVal - aVal;
});
}
};
//#endregion
//#region src/storage/domains/prompt-blocks/filesystem.ts
var FilesystemPromptBlocksStorage = class extends PromptBlocksStorage {
helpers;
constructor({ db }) {
super();
this.helpers = new FilesystemVersionedHelpers({
db,
entitiesFile: "prompt-blocks.json",
parentIdField: "blockId",
name: "FilesystemPromptBlocksStorage",
versionMetadataFields: [
"id",
"blockId",
"versionNumber",
"changedFields",
"changeMessage",
"createdAt"
]
});
}
async init() {
await this.helpers.db.init();
}
async dangerouslyClearAll() {
await this.helpers.dangerouslyClearAll();
}
async getById(id) {
return this.helpers.getById(id);
}
async create(input) {
const { promptBlock } = input;
const now = /* @__PURE__ */ new Date();
const entity = {
id: promptBlock.id,
status: "draft",
activeVersionId: void 0,
authorId: promptBlock.authorId,
metadata: promptBlock.metadata,
createdAt: now,
updatedAt: now
};
await this.helpers.createEntity(promptBlock.id, entity);
const { id: _id, authorId: _authorId, metadata: _metadata, ...snapshotConfig } = promptBlock;
const versionId = crypto.randomUUID();
await this.createVersion({
id: versionId,
blockId: promptBlock.id,
versionNumber: 1,
...snapshotConfig,
changedFields: Object.keys(snapshotConfig),
changeMessage: "Initial version"
});
return structuredClone(entity);
}
async update(input) {
const { id, ...updates } = input;
return this.helpers.updateEntity(id, updates);
}
async delete(id) {
await this.helpers.deleteEntity(id);
}
async list(args) {
const { page, perPage, orderBy, authorId, metadata, status } = args || {};
return await this.helpers.listEntities({
page,
perPage,
orderBy,
listKey: "promptBlocks",
filters: {
authorId,
metadata,
status
}
});
}
async createVersion(input) {
return this.helpers.createVersion(input);
}
async getVersion(id) {
return this.helpers.getVersion(id);
}
async getVersionByNumber(blockId, versionNumber) {
return this.helpers.getVersionByNumber(blockId, versionNumber);
}
async getLatestVersion(blockId) {
return this.helpers.getLatestVersion(blockId);
}
async listVersions(input) {
return await this.helpers.listVersions(input, "blockId");
}
async deleteVersion(id) {
await this.helpers.deleteVersion(id);
}
async deleteVersionsByParentId(entityId) {
await this.helpers.deleteVersionsByParentId(entityId);
}
async countVersions(blockId) {
return this.helpers.countVersions(blockId);
}
};
//#endregion
export { InMemoryPromptBlocksStorage as n, PromptBlocksStorage as r, FilesystemPromptBlocksStorage as t };
//# sourceMappingURL=filesystem-hzUov7lW.js.map