@mastra/core
Version:
The core foundation of the Mastra framework, providing essential components and interfaces for building AI-powered applications.
234 lines • 9.17 kB
TypeScript
import type { MastraMessageContentV2, MastraMessageV2 } from '../agent';
import { MastraBase } from '../base';
import type { MastraMessageV1, StorageThreadType } from '../memory/types';
import type { ScoreRowData } from '../scores';
import type { Trace } from '../telemetry';
import type { WorkflowRunState } from '../workflows';
import type { TABLE_NAMES } from './constants';
import type { ScoresStorage, StoreOperations, WorkflowsStorage, TracesStorage, MemoryStorage, LegacyEvalsStorage } from './domains';
import type { EvalRow, PaginationInfo, StorageColumn, StorageGetMessagesArg, StorageResourceType, StoragePagination, ThreadSortOptions, WorkflowRun, WorkflowRuns, StorageGetTracesArg, PaginationArgs, StorageGetTracesPaginatedArg } from './types';
export type StorageDomains = {
legacyEvals: LegacyEvalsStorage;
operations: StoreOperations;
workflows: WorkflowsStorage;
scores: ScoresStorage;
traces: TracesStorage;
memory: MemoryStorage;
};
export declare function ensureDate(date: Date | string | undefined): Date | undefined;
export declare function serializeDate(date: Date | string | undefined): string | undefined;
export declare function resolveMessageLimit({ last, defaultLimit, }: {
last: number | false | undefined;
defaultLimit: number;
}): number;
export declare abstract class MastraStorage extends MastraBase {
/** @deprecated import from { TABLE_WORKFLOW_SNAPSHOT } '@mastra/core/storage' instead */
static readonly TABLE_WORKFLOW_SNAPSHOT = "mastra_workflow_snapshot";
/** @deprecated import from { TABLE_EVALS } '@mastra/core/storage' instead */
static readonly TABLE_EVALS = "mastra_evals";
/** @deprecated import from { TABLE_MESSAGES } '@mastra/core/storage' instead */
static readonly TABLE_MESSAGES = "mastra_messages";
/** @deprecated import from { TABLE_THREADS } '@mastra/core/storage' instead */
static readonly TABLE_THREADS = "mastra_threads";
/** @deprecated import { TABLE_TRACES } from '@mastra/core/storage' instead */
static readonly TABLE_TRACES = "mastra_traces";
protected hasInitialized: null | Promise<boolean>;
protected shouldCacheInit: boolean;
stores?: StorageDomains;
constructor({ name }: {
name: string;
});
get supports(): {
selectByIncludeResourceScope: boolean;
resourceWorkingMemory: boolean;
hasColumn: boolean;
createTable: boolean;
deleteMessages: boolean;
};
protected ensureDate(date: Date | string | undefined): Date | undefined;
protected serializeDate(date: Date | string | undefined): string | undefined;
/**
* 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
*/
protected resolveMessageLimit({ last, defaultLimit, }: {
last: number | false | undefined;
defaultLimit: number;
}): number;
protected getSqlType(type: StorageColumn['type']): string;
protected getDefaultValue(type: StorageColumn['type']): string;
abstract createTable({ tableName }: {
tableName: TABLE_NAMES;
schema: Record<string, StorageColumn>;
}): Promise<void>;
abstract clearTable({ tableName }: {
tableName: TABLE_NAMES;
}): Promise<void>;
abstract dropTable({ tableName }: {
tableName: TABLE_NAMES;
}): Promise<void>;
abstract alterTable(args: {
tableName: TABLE_NAMES;
schema: Record<string, StorageColumn>;
ifNotExists: string[];
}): Promise<void>;
abstract insert({ tableName, record }: {
tableName: TABLE_NAMES;
record: Record<string, any>;
}): Promise<void>;
abstract batchInsert({ tableName, records, }: {
tableName: TABLE_NAMES;
records: Record<string, any>[];
}): Promise<void>;
batchTraceInsert({ records }: {
records: Record<string, any>[];
}): Promise<void>;
abstract load<R>({ tableName, keys }: {
tableName: TABLE_NAMES;
keys: Record<string, any>;
}): Promise<R | null>;
abstract getThreadById({ threadId }: {
threadId: string;
}): Promise<StorageThreadType | null>;
abstract getThreadsByResourceId({ resourceId, orderBy, sortDirection, }: {
resourceId: string;
} & ThreadSortOptions): Promise<StorageThreadType[]>;
abstract saveThread({ thread }: {
thread: StorageThreadType;
}): Promise<StorageThreadType>;
abstract updateThread({ id, title, metadata, }: {
id: string;
title: string;
metadata: Record<string, unknown>;
}): Promise<StorageThreadType>;
abstract deleteThread({ threadId }: {
threadId: string;
}): Promise<void>;
getResourceById(_: {
resourceId: string;
}): Promise<StorageResourceType | null>;
saveResource(_: {
resource: StorageResourceType;
}): Promise<StorageResourceType>;
updateResource(_: {
resourceId: string;
workingMemory?: string;
metadata?: Record<string, unknown>;
}): Promise<StorageResourceType>;
abstract getMessages(args: StorageGetMessagesArg & {
format?: 'v1';
}): Promise<MastraMessageV1[]>;
abstract getMessages(args: StorageGetMessagesArg & {
format: 'v2';
}): Promise<MastraMessageV2[]>;
abstract getMessages({ threadId, resourceId, selectBy, format, }: StorageGetMessagesArg & {
format?: 'v1' | 'v2';
}): Promise<MastraMessageV1[] | MastraMessageV2[]>;
abstract saveMessages(args: {
messages: MastraMessageV1[];
format?: undefined | 'v1';
}): Promise<MastraMessageV1[]>;
abstract saveMessages(args: {
messages: MastraMessageV2[];
format: 'v2';
}): Promise<MastraMessageV2[]>;
abstract saveMessages(args: {
messages: MastraMessageV1[];
format?: undefined | 'v1';
} | {
messages: MastraMessageV2[];
format: 'v2';
}): Promise<MastraMessageV2[] | MastraMessageV1[]>;
abstract updateMessages(args: {
messages: Partial<Omit<MastraMessageV2, 'createdAt'>> & {
id: string;
content?: {
metadata?: MastraMessageContentV2['metadata'];
content?: MastraMessageContentV2['content'];
};
}[];
}): Promise<MastraMessageV2[]>;
deleteMessages(_messageIds: string[]): Promise<void>;
abstract getTraces(args: StorageGetTracesArg): Promise<Trace[]>;
abstract getTracesPaginated(args: StorageGetTracesPaginatedArg): Promise<PaginationInfo & {
traces: Trace[];
}>;
init(): Promise<void>;
persistWorkflowSnapshot({ workflowName, runId, snapshot, }: {
workflowName: string;
runId: string;
snapshot: WorkflowRunState;
}): Promise<void>;
loadWorkflowSnapshot({ workflowName, runId, }: {
workflowName: string;
runId: string;
}): Promise<WorkflowRunState | null>;
/**
* SCORERS
*/
abstract getScoreById({ id }: {
id: string;
}): Promise<ScoreRowData | null>;
abstract saveScore(score: Omit<ScoreRowData, 'id' | 'createdAt' | 'updatedAt'>): Promise<{
score: ScoreRowData;
}>;
abstract getScoresByScorerId({ scorerId, pagination, entityId, entityType, }: {
scorerId: string;
pagination: StoragePagination;
entityId?: string;
entityType?: string;
}): Promise<{
pagination: PaginationInfo;
scores: ScoreRowData[];
}>;
abstract getScoresByRunId({ runId, pagination, }: {
runId: string;
pagination: StoragePagination;
}): Promise<{
pagination: PaginationInfo;
scores: ScoreRowData[];
}>;
abstract getScoresByEntityId({ entityId, entityType, pagination, }: {
pagination: StoragePagination;
entityId: string;
entityType: string;
}): Promise<{
pagination: PaginationInfo;
scores: ScoreRowData[];
}>;
abstract getEvals(options: {
agentName?: string;
type?: 'test' | 'live';
} & PaginationArgs): Promise<PaginationInfo & {
evals: EvalRow[];
}>;
abstract getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]>;
abstract getWorkflowRuns(args?: {
workflowName?: string;
fromDate?: Date;
toDate?: Date;
limit?: number;
offset?: number;
resourceId?: string;
}): Promise<WorkflowRuns>;
abstract getWorkflowRunById(args: {
runId: string;
workflowName?: string;
}): Promise<WorkflowRun | null>;
abstract getThreadsByResourceIdPaginated(args: {
resourceId: string;
page: number;
perPage: number;
} & ThreadSortOptions): Promise<PaginationInfo & {
threads: StorageThreadType[];
}>;
abstract getMessagesPaginated(args: StorageGetMessagesArg & {
format?: 'v1' | 'v2';
}): Promise<PaginationInfo & {
messages: MastraMessageV1[] | MastraMessageV2[];
}>;
}
//# sourceMappingURL=base.d.ts.map