@mastra/core
Version:
232 lines • 8.43 kB
TypeScript
import type { Processor } from '..';
import { MessageList } from '../../agent/index.js';
import type { IMastraLogger } from '../../logger/index.js';
import type { MastraDBMessage } from '../../memory/index.js';
import type { ObservabilityContext } from '../../observability/index.js';
import type { RequestContext } from '../../request-context/index.js';
import type { MemoryStorage } from '../../storage/index.js';
import type { MastraEmbeddingModel, MastraEmbeddingOptions, MastraVector, VectorFilter } from '../../vector/index.js';
export interface SemanticRecallOptions {
/**
* Storage instance for retrieving messages
*/
storage: MemoryStorage;
/**
* Vector store for semantic search
*/
vector: MastraVector;
/**
* Embedder for generating query embeddings. Optional only when `vector` is self-embedding,
* where the store takes text and produces the vectors itself.
*/
embedder?: MastraEmbeddingModel<string>;
/**
* Number of most similar messages to retrieve
* @default 4
*/
topK?: number;
/**
* Number of context messages to include before/after each match
* Can be a number (same for before/after) or an object with before/after
* @default 1
*/
messageRange?: number | {
before: number;
after: number;
};
/**
* Scope of semantic search
* - 'thread': Search within the current thread only
* - 'resource': Search across all threads for the resource
* @default 'resource'
*/
scope?: 'thread' | 'resource';
/**
* Minimum similarity score threshold (0-1)
* Messages below this threshold will be filtered out
*/
threshold?: number;
/**
* Metadata filter for semantic search queries, combined with the scope filter using `$and`.
* Messages are embedded with their thread's metadata, so it can match thread metadata.
*
* @example
* ```typescript
* filter: { projectId: { $eq: 'project-a' } }
* ```
*/
filter?: VectorFilter;
/**
* Index name for the vector store
* If not provided, will be auto-generated based on embedder model
*/
indexName?: string;
/**
* Optional logger instance for structured logging
*/
logger?: IMastraLogger;
/**
* Options to pass to the embedder when generating embeddings.
* Use this to pass provider-specific options like outputDimensionality for Google models.
*
* @example
* ```typescript
* embedderOptions: {
* providerOptions: {
* google: {
* outputDimensionality: 768,
* taskType: 'RETRIEVAL_DOCUMENT'
* }
* }
* }
* ```
*/
embedderOptions?: MastraEmbeddingOptions;
/**
* Expected embedding dimension. When set, an embedding of any other length throws
* before the vector index is created or queried.
*/
embeddingDimension?: number;
/**
* Largest number of characters of message text sent to the embedder in one input. Longer
* messages are split into chunks, each stored as its own vector row.
*
* Characters, not tokens: no single tokenizer matches every embedder. The default of 16,384
* is about 4096 English tokens. Lower it for Chinese, Japanese or Korean text, or code, which
* use more tokens per character. When unset and the embedder declares `maxInputTokens`, the
* ceiling is derived from that instead.
*
* @default 16384
*/
maxChunkChars?: number;
}
/**
* SemanticRecall is both an input and output processor that:
* - On input: performs semantic search on historical messages and adds relevant context
* - On output: creates embeddings for messages being saved to enable future semantic search
*
* It uses vector embeddings to find messages similar to the user's query,
* then retrieves those messages along with surrounding context.
*
* @example
* ```typescript
* const processor = new SemanticRecall({
* storage: memoryStorage,
* vector: vectorStore,
* embedder: openaiEmbedder,
* topK: 5,
* messageRange: 2,
* scope: 'resource'
* });
*
* // Use with agent
* const agent = new Agent({
* inputProcessors: [processor],
* outputProcessors: [processor]
* });
* ```
*/
export declare class SemanticRecall implements Processor {
readonly id = "semantic-recall";
readonly name = "SemanticRecall";
private storage;
private vector;
private embedder?;
private topK;
private messageRange;
private scope;
private threshold?;
private filter?;
private indexName?;
private logger?;
private embedderOptions?;
private embeddingDimension?;
private maxChunkChars;
private hasher;
private indexValidationCache;
/**
* True when the store embeds text itself, so this processor sends it text. False for every
* store that expects the caller to supply vectors.
*/
private get isSelfEmbedding();
constructor(options: SemanticRecallOptions);
processInput(args: {
messages: MastraDBMessage[];
messageList: MessageList;
abort: (reason?: string) => never;
requestContext?: RequestContext;
} & Partial<ObservabilityContext>): Promise<MessageList | MastraDBMessage[]>;
/**
* Sort recalled messages into a stable order before formatting so that
* vector-query result ordering (which depends on similarity scores and can
* vary between runs for equivalent results) doesn't change the rendered
* prompt. Ordering: createdAt, then threadId, then role (user → assistant →
* tool → system), then id. Uses plain string comparison (ASCII identifiers)
* to stay locale-independent across CI/dev machines.
*/
private sortMessagesForRecall;
/**
* Format cross-thread messages as a system message with timestamps and labels
* Uses the exact formatting logic from main that was tested with longmemeval benchmark
*/
private formatCrossThreadMessages;
/**
* Check if a message is a user turn delivered through the agent signal
* pipeline. User messages sent via `agent.sendMessage` (e.g. the Studio
* playground) are stored as `role: 'signal'` rows and only projected to
* `role: 'user'` at prompt time — after input processors run — so they must
* be treated as user messages when extracting the recall query.
*/
private isUserMessageSignal;
/**
* Extract the user query from messages for semantic search
*/
private extractUserQuery;
/**
* Perform semantic search using vector embeddings
*/
private performSemanticSearch;
/**
* Generate embeddings for message content
*/
/**
* Hash content using xxhash for fast cache key generation
* Includes index name to ensure cache isolation between different embedding models/dimensions
*/
private hashContent;
private embedMessageContent;
/**
* Embeds the chunks of one message, reusing cached embeddings. The cache misses go out in one
* `doEmbed` call (split only by the model's `maxEmbeddingsPerCall`), so a long message doesn't
* fan out into one request per chunk.
*
* @returns One embedding per chunk, in order.
*/
private embedChunks;
/**
* Metadata of the threads messages are written for. The run's thread carries its metadata when
* the agent loaded it; storage is read only for threads that arrive without it, such as a
* durable finalize step that rebuilt the request context from an id.
*/
private loadThreadMetadata;
/**
* Get default index name based on embedder model
*/
private getDefaultIndexName;
/**
* Ensure vector index exists with correct dimensions
* Uses in-memory cache to avoid redundant validation calls
*/
private ensureVectorIndex;
/**
* Process output messages to create embeddings for messages being saved
* This allows semantic recall to index new messages for future retrieval
*/
processOutputResult(args: {
messages: MastraDBMessage[];
messageList?: MessageList;
abort: (reason?: string) => never;
requestContext?: RequestContext;
} & Partial<ObservabilityContext>): Promise<MessageList | MastraDBMessage[]>;
}
//# sourceMappingURL=semantic-recall.d.ts.map