@mastra/core
Version:
62 lines • 3.35 kB
TypeScript
import type { IMastraLogger } from '../../logger/index.js';
import type { MastraMemory } from '../../memory/memory.js';
import type { MastraDBMessage, MemoryInfo, SerializedMessage, SerializedMessageListState } from './state/index.js';
/**
* Stands in for a memory-recalled message in a persisted transcript. The
* message already lives in memory storage, so the snapshot keeps only its id
* plus a fingerprint of the stored version it replaced.
*/
export type MemoryMessageRef = {
__ref: 'memory';
id: string;
fingerprint: string;
};
/** A serialized transcript whose memory-recalled messages may be refs. */
export type DehydratedMessageListState = Omit<SerializedMessageListState, 'messages'> & {
messages: Array<SerializedMessage | MemoryMessageRef>;
};
/** Loads stored messages by id. Resolves `undefined` when no store can answer. */
export type StoredMessageLoader = (ids: string[]) => Promise<MastraDBMessage[] | undefined>;
export declare function isMemoryMessageRef(value: unknown): value is MemoryMessageRef;
/** Loads messages through the memory instance's store, when it can list by id. */
export declare function createStoredMessageLoader(memory: MastraMemory | undefined): StoredMessageLoader;
/**
* Keeps messages memory already stores out of persisted run transcripts: the
* ones the run recalled, and the ones it saved itself (`recordSaved`), so a
* long run stops rewriting its whole conversation on every checkpoint.
*
* `dehydrate` swaps such a message for a {@link MemoryMessageRef} only
* when it is byte-for-byte the stored row as a recall adds it — anything a
* processor changed after recall stays inline, so resuming never trades the
* run's view of a message for the stored one. `hydrate` restores refs, from
* the rows this instance already verified or else from storage; a row edited
* since is restored as stored and a deleted one is dropped, both logged at
* debug level.
*
* One instance serves one run, so verified rows are reused across every
* transcript the run persists.
*/
export declare class MemoryMessageRefs {
#private;
/**
* Loads the stored rows of recalled messages not yet verified, so
* `dehydrate` can reference them. Best effort: a failed lookup only leaves
* those messages inline. Calls run one at a time, so concurrent stores of
* one run share a single lookup.
*/
verify(state: SerializedMessageListState | undefined, load: StoredMessageLoader, logger?: IMastraLogger): Promise<void>;
/**
* Records rows this run just saved to memory, so `dehydrate` can reference
* them without a storage read. A message the run changes after saving no
* longer matches its row and stays inline until it is saved again.
*/
recordSaved(rows: MastraDBMessage[], memoryInfo: MemoryInfo | null): void;
/** Replaces verified or saved messages that match their stored row with refs. Never reads storage. */
dehydrate(state: SerializedMessageListState): DehydratedMessageListState;
/** Restores every ref in `state`, loading rows this instance has not verified. */
hydrate(state: DehydratedMessageListState, load: StoredMessageLoader, context?: {
logger?: IMastraLogger;
runId?: string;
}): Promise<SerializedMessageListState>;
}
//# sourceMappingURL=memory-message-refs.d.ts.map