@mastra/core
Version:
85 lines • 3.54 kB
TypeScript
import type { OutputResult, Processor, ProcessorSpanPhase } from '..';
import type { MastraDBMessage, MessageList } from '../../agent/index.js';
import { SpanType } from '../../observability/index.js';
import type { ObservabilityContext, MemoryOperationAttributes } from '../../observability/index.js';
import type { RequestContext } from '../../request-context/index.js';
import type { MemoryStorage } from '../../storage/index.js';
/**
* Options for the MessageHistory processor
*/
export interface MessageHistoryOptions {
storage: MemoryStorage;
lastMessages?: number | false;
tokenLimit?: {
maxTokens: number;
atMaxRemoveTokens: number;
};
tokenCounter?: {
countMessage(message: MastraDBMessage): number | Promise<number>;
};
}
export declare class MessageHistory implements Processor {
readonly id = "message-history";
readonly name = "MessageHistory";
/**
* Trace as a memory operation rather than an anonymous processor run: a user
* configures `memory`, not a processor. The two phases are different memory
* operations — the input phase recalls stored history, the output phase saves
* the turn — so each is named for what it does.
*
* This replaces a MEMORY_OPERATION span the processor used to create *inside*
* its own processor span, which meant two spans per phase describing one
* operation.
*/
readonly spanType = SpanType.MEMORY_OPERATION;
readonly spanName: (phase: ProcessorSpanPhase) => string;
readonly spanAttributes: (phase: ProcessorSpanPhase) => Partial<MemoryOperationAttributes>;
private storage;
private lastMessages?;
private tokenLimit?;
private tokenCounter?;
constructor(options: MessageHistoryOptions);
/**
* Get threadId and resourceId from either RequestContext or MessageList's memoryInfo
*/
private getMemoryContext;
/**
* This processor's own span, which the runner already typed as the memory
* operation. Recording onto it rather than creating a child keeps one span
* per memory operation. The runner owns its lifecycle, so this only ever
* updates — it never ends or errors the span.
*/
private memorySpan;
private loadContinuationToolCallMessages;
/**
* Keep pinned messages inside the token budget: drop the oldest unpinned history first,
* and skip any pinned message that cannot fit on its own.
*/
private fitPinnedMessages;
processInput(args: {
messages: MastraDBMessage[];
messageList: MessageList;
abort: (reason?: string) => never;
requestContext?: RequestContext;
} & Partial<ObservabilityContext>): Promise<MessageList | MastraDBMessage[]>;
processOutputResult(args: {
messages: MastraDBMessage[];
messageList: MessageList;
abort: (reason?: string) => never;
requestContext?: RequestContext;
result?: OutputResult;
} & Partial<ObservabilityContext>): Promise<MessageList>;
/**
* Persist messages to storage, filtering out partial tool calls and working memory tags.
* Also ensures the thread exists (creates if needed).
*
* This method can be called externally by other processors (e.g., ObservationalMemory)
* that need to save messages incrementally.
*/
persistMessages(args: {
messages: MastraDBMessage[];
threadId: string;
resourceId?: string;
}): Promise<void>;
}
//# sourceMappingURL=message-history.d.ts.map