@mastra/core
Version:
The core foundation of the Mastra framework, providing essential components and interfaces for building AI-powered applications.
221 lines (218 loc) • 7.37 kB
JavaScript
'use strict';
var chunkWYJ5BHEW_cjs = require('./chunk-WYJ5BHEW.cjs');
var chunk2WZYQCRK_cjs = require('./chunk-2WZYQCRK.cjs');
// src/storage/base.ts
function ensureDate(date) {
if (!date) return void 0;
return date instanceof Date ? date : new Date(date);
}
function serializeDate(date) {
if (!date) return void 0;
const dateObj = ensureDate(date);
return dateObj?.toISOString();
}
function resolveMessageLimit({
last,
defaultLimit
}) {
if (typeof last === "number") return Math.max(0, last);
if (last === false) return 0;
return defaultLimit;
}
var MastraStorage = class extends chunk2WZYQCRK_cjs.MastraBase {
/** @deprecated import from { TABLE_WORKFLOW_SNAPSHOT } '@mastra/core/storage' instead */
static TABLE_WORKFLOW_SNAPSHOT = chunkWYJ5BHEW_cjs.TABLE_WORKFLOW_SNAPSHOT;
/** @deprecated import from { TABLE_EVALS } '@mastra/core/storage' instead */
static TABLE_EVALS = chunkWYJ5BHEW_cjs.TABLE_EVALS;
/** @deprecated import from { TABLE_MESSAGES } '@mastra/core/storage' instead */
static TABLE_MESSAGES = chunkWYJ5BHEW_cjs.TABLE_MESSAGES;
/** @deprecated import from { TABLE_THREADS } '@mastra/core/storage' instead */
static TABLE_THREADS = chunkWYJ5BHEW_cjs.TABLE_THREADS;
/** @deprecated import { TABLE_TRACES } from '@mastra/core/storage' instead */
static TABLE_TRACES = chunkWYJ5BHEW_cjs.TABLE_TRACES;
hasInitialized = null;
shouldCacheInit = true;
stores;
constructor({ name }) {
super({
component: "STORAGE",
name
});
}
get supports() {
return {
selectByIncludeResourceScope: false,
resourceWorkingMemory: false,
hasColumn: false,
createTable: false,
deleteMessages: false
};
}
ensureDate(date) {
return ensureDate(date);
}
serializeDate(date) {
return serializeDate(date);
}
/**
* Resolves limit for how many messages to fetch
*
* @param last The number of messages to fetch
* @param defaultLimit The default limit to use if last is not provided
* @returns The resolved limit
*/
resolveMessageLimit({
last,
defaultLimit
}) {
return resolveMessageLimit({ last, defaultLimit });
}
getSqlType(type) {
switch (type) {
case "text":
return "TEXT";
case "timestamp":
return "TIMESTAMP";
case "float":
return "FLOAT";
case "integer":
return "INTEGER";
case "bigint":
return "BIGINT";
case "jsonb":
return "JSONB";
case "float":
return "FLOAT";
default:
return "TEXT";
}
}
getDefaultValue(type) {
switch (type) {
case "text":
case "uuid":
return "DEFAULT ''";
case "timestamp":
return "DEFAULT '1970-01-01 00:00:00'";
case "integer":
case "float":
case "bigint":
return "DEFAULT 0";
case "jsonb":
return "DEFAULT '{}'";
default:
return "DEFAULT ''";
}
}
batchTraceInsert({ records }) {
if (this.stores?.traces) {
return this.stores.traces.batchTraceInsert({ records });
}
return this.batchInsert({ tableName: chunkWYJ5BHEW_cjs.TABLE_TRACES, records });
}
async getResourceById(_) {
throw new Error(
`Resource working memory is not supported by this storage adapter (${this.constructor.name}). Supported storage adapters: LibSQL (/libsql), PostgreSQL (/pg), Upstash (/upstash). To use per-resource working memory, switch to one of these supported storage adapters.`
);
}
async saveResource(_) {
throw new Error(
`Resource working memory is not supported by this storage adapter (${this.constructor.name}). Supported storage adapters: LibSQL (/libsql), PostgreSQL (/pg), Upstash (/upstash). To use per-resource working memory, switch to one of these supported storage adapters.`
);
}
async updateResource(_) {
throw new Error(
`Resource working memory is not supported by this storage adapter (${this.constructor.name}). Supported storage adapters: LibSQL (/libsql), PostgreSQL (/pg), Upstash (/upstash). To use per-resource working memory, switch to one of these supported storage adapters.`
);
}
async deleteMessages(_messageIds) {
throw new Error(
`Message deletion is not supported by this storage adapter (${this.constructor.name}). The deleteMessages method needs to be implemented in the storage adapter.`
);
}
async init() {
if (this.shouldCacheInit && await this.hasInitialized) {
return;
}
const tableCreationTasks = [
this.createTable({
tableName: chunkWYJ5BHEW_cjs.TABLE_WORKFLOW_SNAPSHOT,
schema: chunkWYJ5BHEW_cjs.TABLE_SCHEMAS[chunkWYJ5BHEW_cjs.TABLE_WORKFLOW_SNAPSHOT]
}),
this.createTable({
tableName: chunkWYJ5BHEW_cjs.TABLE_EVALS,
schema: chunkWYJ5BHEW_cjs.TABLE_SCHEMAS[chunkWYJ5BHEW_cjs.TABLE_EVALS]
}),
this.createTable({
tableName: chunkWYJ5BHEW_cjs.TABLE_THREADS,
schema: chunkWYJ5BHEW_cjs.TABLE_SCHEMAS[chunkWYJ5BHEW_cjs.TABLE_THREADS]
}),
this.createTable({
tableName: chunkWYJ5BHEW_cjs.TABLE_MESSAGES,
schema: chunkWYJ5BHEW_cjs.TABLE_SCHEMAS[chunkWYJ5BHEW_cjs.TABLE_MESSAGES]
}),
this.createTable({
tableName: chunkWYJ5BHEW_cjs.TABLE_TRACES,
schema: chunkWYJ5BHEW_cjs.TABLE_SCHEMAS[chunkWYJ5BHEW_cjs.TABLE_TRACES]
}),
this.createTable({
tableName: chunkWYJ5BHEW_cjs.TABLE_SCORERS,
schema: chunkWYJ5BHEW_cjs.TABLE_SCHEMAS[chunkWYJ5BHEW_cjs.TABLE_SCORERS]
})
];
if (this.supports.resourceWorkingMemory) {
tableCreationTasks.push(
this.createTable({
tableName: chunkWYJ5BHEW_cjs.TABLE_RESOURCES,
schema: chunkWYJ5BHEW_cjs.TABLE_SCHEMAS[chunkWYJ5BHEW_cjs.TABLE_RESOURCES]
})
);
}
this.hasInitialized = Promise.all(tableCreationTasks).then(() => true);
await this.hasInitialized;
await this?.alterTable?.({
tableName: chunkWYJ5BHEW_cjs.TABLE_MESSAGES,
schema: chunkWYJ5BHEW_cjs.TABLE_SCHEMAS[chunkWYJ5BHEW_cjs.TABLE_MESSAGES],
ifNotExists: ["resourceId"]
});
}
async persistWorkflowSnapshot({
workflowName,
runId,
snapshot
}) {
await this.init();
const data = {
workflow_name: workflowName,
run_id: runId,
snapshot,
createdAt: /* @__PURE__ */ new Date(),
updatedAt: /* @__PURE__ */ new Date()
};
this.logger.debug("Persisting workflow snapshot", { workflowName, runId, data });
await this.insert({
tableName: chunkWYJ5BHEW_cjs.TABLE_WORKFLOW_SNAPSHOT,
record: data
});
}
async loadWorkflowSnapshot({
workflowName,
runId
}) {
if (!this.hasInitialized) {
await this.init();
}
this.logger.debug("Loading workflow snapshot", { workflowName, runId });
const d = await this.load({
tableName: chunkWYJ5BHEW_cjs.TABLE_WORKFLOW_SNAPSHOT,
keys: { workflow_name: workflowName, run_id: runId }
});
return d ? d.snapshot : null;
}
};
exports.MastraStorage = MastraStorage;
exports.ensureDate = ensureDate;
exports.resolveMessageLimit = resolveMessageLimit;
exports.serializeDate = serializeDate;
//# sourceMappingURL=chunk-2HXWRK3P.cjs.map
//# sourceMappingURL=chunk-2HXWRK3P.cjs.map