본문으로 건너뛰기

Memory.리콜()

그만큼Memory.recall()메소드는 페이지 매김, 필터링 옵션 및 의미 검색을 지원하여 특정 스레드에서 메시지를 검색합니다.

사용예
사용예에 대한 직접 링크

const { messages } = await memory.recall({
threadId: 'thread-123',
perPage: 20,
})

매개변수
매개변수에 대한 직접 링크

threadId:

string
메시지를 가져올 스레드의 고유 식별자입니다.

resourceId?:

string
스레드를 소유한 리소스의 선택적 ID입니다. 제공하면 스레드 소유권을 검증합니다.

vectorSearchString?:

string
의미적으로 유사한 메시지를 찾기 위한 검색 문자열입니다. threadConfig에서 의미 기반 recall이 활성화되어 있어야 합니다.

perPage?:

number | false
페이지당 가져올 메시지 수입니다. 페이지네이션 없이 모든 메시지를 가져오려면 false로 설정하세요. 제공하지 않으면 기본값은 threadConfig.lastMessages입니다.

page?:

number
페이지네이션을 위한 0부터 시작하는 페이지 번호입니다. perPage와 함께 사용하여 메시지를 여러 묶음으로 가져옵니다.

include?:

{ id: string; threadId?: string; withPreviousMessages?: number; withNextMessages?: number }[]
선택적 컨텍스트 메시지와 함께 포함할 특정 메시지 ID의 배열입니다. 각 항목에는 id(필수), 선택적 threadId(기본값은 기본 threadId), withPreviousMessages(앞에 있는 메시지 수, 벡터 검색에서는 기본값 2, 그 외에는 0), withNextMessages(뒤에 있는 메시지 수, 벡터 검색에서는 기본값 2, 그 외에는 0)가 있습니다.

filter?:

{ dateRange?: { start?: Date; end?: Date; startExclusive?: boolean; endExclusive?: boolean }; metadata?: Record<string, string | number | boolean | null> }
메시지 검색을 위한 필터 옵션입니다. dateRange는 생성 날짜로 메시지를 필터링합니다. metadata는 AND 의미 체계를 사용해 얕은 메시지 메타데이터를 정확한 스칼라 키-값 쌍으로 필터링합니다. 메타데이터 값에는 문자열, 유한한 숫자, 불리언 또는 null을 사용할 수 있습니다.

orderBy?:

{ field: 'createdAt'; direction: 'ASC' | 'DESC' }
가져온 메시지의 정렬 순서입니다. 기본값은 생성 날짜 기준 내림차순입니다.

threadConfig?:

MemoryConfig
메시지 검색 및 의미 검색을 위한 구성 옵션입니다.
MemoryConfig

lastMessages?:

number | false
가져올 최신 메시지 수입니다. 비활성화하려면 false로 설정하세요. perPage를 명시적으로 제공하지 않으면 이 값이 기본값으로 사용됩니다.

semanticRecall?:

boolean | { topK: number; messageRange: number | { before: number; after: number }; scope?: 'thread' | 'resource' }
메시지 기록의 의미 검색을 활성화합니다. 불리언 또는 구성 옵션이 포함된 객체를 사용할 수 있습니다. 활성화하려면 벡터 스토어와 임베더가 모두 구성되어 있어야 합니다.

workingMemory?:

WorkingMemory
작업 Memory 기능의 구성입니다. { enabled: boolean; template?: string; schema?: ZodObject<any> | JSONSchema7; scope?: 'thread' | 'resource' }를 사용하거나, 비활성화하려면 { enabled: boolean }을 사용할 수 있습니다.

threads?:

{ generateTitle?: boolean | { model: DynamicArgument<MastraLanguageModel>; instructions?: DynamicArgument<string> } }
Memory 스레드 생성 관련 설정입니다. generateTitle은 대화 기록에서 스레드 제목을 자동으로 생성할지 제어합니다. 불리언 또는 사용자 지정 Model과 지침이 포함된 객체를 사용할 수 있습니다.

메타데이터 필터링
메타데이터 필터링에 대한 직접 링크

메시지에 저장된 얕은 스칼라 메타데이터와 일치시키려면 filter.metadata를 사용하세요.

const { messages } = await memory.recall({
threadId: 'thread-123',
filter: {
metadata: {
category: 'billing',
escalated: true,
priority: 2,
archivedAt: null,
},
},
})

모든 메타데이터 항목은 AND 의미 체계로 결합됩니다. 메시지는 모든 키와 값에 대해 타입까지 정확히 일치해야 합니다. null은 명시적으로 null로 설정된 메타데이터와 일치하며, 키가 없는 경우와는 일치하지 않습니다. 메타데이터 필터는 얕은 스칼라 값인 string, 유한한 number, boolean, null만 지원합니다. 중첩 객체, 배열, NaN, 무한대는 지원되지 않습니다. 메타데이터 키는 문자 또는 밑줄로 시작해야 하며 영숫자 또는 밑줄만 포함할 수 있습니다. 길이는 128자로 제한됩니다. __proto__, constructor, prototype과 같은 예약된 프로토타입 키는 허용되지 않습니다. 성능은 스토리지 백엔드에 따라 달라집니다. 임의의 메타데이터 필터는 후보 메시지를 검사해야 할 수 있으므로 가능하면 threadId, resourceId 또는 dateRange를 사용하여 쿼리 범위를 좁히세요.

반환값
반환값에 대한 직접 링크

messages:

MastraDBMessage[]
데이터베이스 형식으로 가져온 메시지의 배열입니다.

확장된 사용 예
확장된 사용 예에 대한 직접 링크

src/test-memory.ts
import { mastra } from './mastra'

const agent = mastra.getAgent('agent')
const memory = await agent.getMemory()

// Retrieve messages with pagination
const { messages } = await memory!.recall({
threadId: 'thread-123',
perPage: 50,
vectorSearchString: 'What messages are there?',
include: [
{
id: 'msg-123',
},
{
id: 'msg-456',
withPreviousMessages: 3,
withNextMessages: 1,
},
],
threadConfig: {
semanticRecall: true,
},
})

console.log(messages) // MastraDBMessage[]

// Fetch all messages without pagination
const allMessages = await memory!.recall({
threadId: 'thread-123',
perPage: false, // Fetch all
})

// Convert to AI SDK format if needed
import { toAISdkV5Messages } from '@mastra/ai-sdk/ui'
const uiMessages = toAISdkV5Messages(messages)